OSDN Git Service

1013684496a434d2c3639b272ad8b022fe4a9115
[luatex-ja/luatexja.git] / src / luatexja.lua
1 require 'lualibs'
2 ------------------------------------------------------------------------
3 do
4   local ipath = {}
5   function luatexja.input_path_clear() for i in ipairs(ipath) do ipath[i]=nil end end
6   function luatexja.input_path_add(s) ipath[#ipath+1]=s end
7   function luatexja.load_lua(fn)
8     local found = kpse.find_file(fn, 'tex')
9     if not found then
10       for _,v in ipairs(ipath) do
11         found = kpse.find_file(v .. fn, 'tex'); if found then break end
12       end
13     end
14     if not found then
15       tex.error("LuaTeX-ja error: File `" .. fn .. "' not found")
16     else
17       texio.write_nl('(' .. found .. ')'); dofile(found)
18     end
19   end
20 end
21 function luatexja.load_module(name) require('ltj-' .. name.. '.lua') end
22
23 do
24     local dnode = node.direct
25     local getfield, traverse = dnode.getfield, dnode.traverse
26     local node_new, set_attr, get_attr = dnode.new, dnode.set_attribute, dnode.get_attribute
27     local set_attrlist, get_attrlist = dnode.setattributelist, dnode.getattributelist
28     local unset_attr = dnode.unset_attribute
29     local attr_icflag = luatexbase.attributes['ltj@icflag']
30     local function node_inherit_attr(n, b, a)
31         if b or a then
32             local attrlist = get_attrlist(b or a)
33             local nic = get_attr(n, attr_icflag)
34             set_attrlist(n, attrlist); set_attr(n, attr_icflag, nic)
35             if b and a then
36                 for na in traverse(attrlist) do
37                     local id = getfield(na, 'number')
38                     if id and id~=attr_icflag and getfield(na, 'value')~=get_attr(a, id) then
39                         unset_attr(n, id)
40                     end
41                 end
42             end
43         end
44         return n
45     end
46     luatexja.node_inherit_attr = node_inherit_attr
47     luatexja.dnode_new = function (id, subtype, b, a)
48         return node_inherit_attr(node_new(id, subtype), b, a)
49     end
50 end
51
52 --- 以下は全ファイルで共有される定数
53 local icflag_table = {}
54 luatexja.icflag_table = icflag_table
55 icflag_table.ITALIC          = 1
56 icflag_table.PACKED          = 2
57 icflag_table.KINSOKU         = 3
58 icflag_table.FROM_JFM        = 4
59 icflag_table.KANJI_SKIP      = 68 -- = 4+64
60 icflag_table.KANJI_SKIP_JFM  = 69
61 icflag_table.XKANJI_SKIP     = 70
62 icflag_table.XKANJI_SKIP_JFM = 71
63 icflag_table.LINEEND         = 72
64 icflag_table.PROCESSED       = 73
65 icflag_table.IC_PROCESSED    = 74
66 icflag_table.BOXBDD          = 75
67 icflag_table.SPECIAL_JAGLUE  = 76
68 -- 段落組版中のノードリストでは通常のノード (not whatsit) だが
69 -- 和文処理グルー挿入プロセスで長さが決定されるもの
70 icflag_table.PROCESSED_BEGIN_FLAG = 4096 -- sufficiently large power of 2
71
72 local stack_ind = {}
73 luatexja.stack_table_index = stack_ind
74 stack_ind.PRE  = 0x200000 -- characterごと
75 stack_ind.POST = 0x400000 -- characterごと
76 stack_ind.KCAT = 0x600000 -- characterごと
77 stack_ind.XSP  = 0x800000 -- characterごと
78 stack_ind.RIPRE  = 0xA00000 -- characterごと,ruby pre
79 stack_ind.RIPOST = 0xC00000 -- characterごと,ruby post
80 stack_ind.JWP  = 0 -- これだけ
81 stack_ind.KSK  = 1 -- これだけ
82 stack_ind.XSK  = 2 -- これだけ
83 stack_ind.MJT  = 0x100 -- 0--255
84 stack_ind.MJS  = 0x200 -- 0--255
85 stack_ind.MJSS = 0x300 -- 0--255
86 stack_ind.KSJ  = 0x400 -- 0--9
87
88 local uid_table = {}
89 luatexja.userid_table = uid_table
90 uid_table.IHB  = luatexbase.newuserwhatsitid('inhibitglue',  'luatexja') -- \inhibitglue
91 uid_table.STCK = luatexbase.newuserwhatsitid('stack_marker', 'luatexja') -- スタック管理
92 uid_table.BPAR = luatexbase.newuserwhatsitid('begin_par',    'luatexja') -- 「段落始め」
93 uid_table.DIR  = luatexbase.newuserwhatsitid('direction',    'luatexja') -- 組方向
94 uid_table.BOXB = luatexbase.newuserwhatsitid('box_boundary', 'luatexja') -- 「ボックス始め・終わり」
95 uid_table.JA_AL_BDD = luatexbase.newuserwhatsitid('ja_al_boundary', 'luatexja')
96
97 local dir_table = {}
98 luatexja.dir_table = dir_table
99 dir_table.dir_dtou = 1
100 dir_table.dir_tate = 3
101 dir_table.dir_yoko = 4
102 dir_table.dir_math_mod    = 8
103 dir_table.dir_node_auto   = 128 -- 組方向を合わせるために自動で作られたもの
104 dir_table.dir_node_manual = 256 -- 寸法代入によって作られたもの
105 dir_table.dir_utod = dir_table.dir_tate + dir_table.dir_math_mod
106   -- 「縦数式ディレクション」 in pTeX
107 --- 定義終わり
108
109 local load_module = luatexja.load_module
110 load_module 'base';      local ltjb = luatexja.base
111 if tex.outputmode==0 then
112   ltjb.package_error('luatexja',
113     'DVI output is not supported in LuaTeX-ja',
114     'Use lua*tex instead dvilua*tex.')
115 end
116 load_module 'rmlgbm';    local ltjr = luatexja.rmlgbm -- must be 1st
117 if luatexja_debug then load_module 'debug' end
118 load_module 'lotf_aux';  local ltju = luatexja.lotf_aux
119 load_module 'charrange'; local ltjc = luatexja.charrange
120 load_module 'stack';     local ltjs = luatexja.stack
121 load_module 'direction'; local ltjd = luatexja.direction -- +1 hlist +1 attr_list
122 load_module 'lineskip';  local ltjl = luatexja.lineskip -- +1 hlist +1 attr_list
123 load_module 'jfont';     local ltjf = luatexja.jfont
124 load_module 'inputbuf';  local ltji = luatexja.inputbuf
125 load_module 'pretreat';  local ltjp = luatexja.pretreat
126 load_module 'setwidth';  local ltjw = luatexja.setwidth
127 load_module 'jfmglue';   local ltjj = luatexja.jfmglue -- +1 glue +1 gs +1 attr_list
128 load_module 'math';      local ltjm = luatexja.math
129 load_module 'base';      local ltjb = luatexja.base
130
131 local attr_jchar_class = luatexbase.attributes['ltj@charclass']
132 local attr_jchar_code = luatexbase.attributes['ltj@charcode']
133 local attr_curjfnt = luatexbase.attributes['ltj@curjfnt']
134 local attr_yablshift = luatexbase.attributes['ltj@yablshift']
135 local attr_icflag = luatexbase.attributes['ltj@icflag']
136 local attr_uniqid = luatexbase.attributes['ltj@uniqid']
137 local attr_dir = luatexbase.attributes['ltj@dir']
138 local cat_lp = luatexbase.catcodetables['latex-package']
139
140 -- Three aux. functions, borrowed from tex.web
141
142 local floor = math.floor
143 local function print_scaled(s)
144    local out, delta = '', 10
145    if s<0 then s, out = -s, out..'-' end
146    out=out..tostring(floor(s/65536)) .. '.'
147    s=10*(s%65536)+5
148    repeat
149       if delta>65536 then s=s+32768-50000 end
150       out=out .. tostring(floor(s/65536))
151       s=10*(s%65536); delta=delta*10
152    until s<=delta
153    return out
154 end
155 luatexja.print_scaled = print_scaled
156
157 local function print_glue(d,order)
158    local out=print_scaled(d)
159    if order>0 then
160       out=out..'fi'
161       while order>1 do out=out..'l'; order=order-1 end
162    else out=out..'pt'
163    end
164    return out
165 end
166
167 local function print_spec(p)
168    local out=print_scaled(p.width or p[1])..'pt'
169    if (p.stretch or p[2])~=0 then
170       out=out..' plus '..print_glue(p.stretch or p[2], p.stretch_order or p[4])
171    end
172    if (p.shrink or p[3])~=0 then
173       out=out..' minus '..print_glue(p.shrink or p[3], p.shrink_order or p[5])
174    end
175 return out
176 end
177
178
179 ------------------------------------------------------------------------
180 -- CODE FOR GETTING/SETTING PARAMETERS
181 ------------------------------------------------------------------------
182 local getcount, texwrite = tex.getcount, tex.write
183 local cnt_stack = luatexbase.registernumber 'ltj@@stack'
184
185 -- EXT: print parameters that don't need arguments
186 do
187    local tex_getattr, getnest = tex.getattribute, tex.getnest
188    local function getattr(a, d)
189       local r = tex_getattr(a); d = d or 0
190       return (r==-0x7FFFFFFF) and d or r
191    end
192    luatexja.unary_pars = {
193       yalbaselineshift = function(t)
194          return print_scaled(getattr('ltj@yablshift'))..'pt'
195       end,
196       yjabaselineshift = function(t)
197          return print_scaled(getattr('ltj@ykblshift'))..'pt'
198       end,
199       talbaselineshift = function(t)
200          return print_scaled(getattr('ltj@tablshift'))..'pt'
201       end,
202       tjabaselineshift = function(t)
203          return print_scaled(getattr('ltj@tkblshift'))..'pt'
204       end,
205       kanjiskip = function(t)
206          return print_spec(ltjs.get_stack_skip(stack_ind.KSK, t))
207       end,
208       xkanjiskip = function(t)
209          return print_spec(ltjs.get_stack_skip(stack_ind.XSK, t))
210       end,
211       jcharwidowpenalty = function(t)
212          return ltjs.get_stack_table(stack_ind.JWP, 0, t)
213       end,
214       autospacing = function(t)
215          return getattr('ltj@autospc', 1)
216       end,
217       autoxspacing = function(t)
218          return getattr('ltj@autoxspc', 1)
219       end,
220       differentjfm = function(t)
221          local f, r = luatexja.jfmglue.diffmet_rule, '???'
222          if f == math.max then r = 'large'
223          elseif f == math.min then r = 'small'
224          elseif f == math.two_average then r = 'average'
225          elseif f == math.two_paverage then r = 'paverage'
226          elseif f == math.two_pleft then r = 'pleft'
227          elseif f == math.two_pright then r = 'pright'
228          elseif f == math.two_add then r = 'both'
229          end
230          return r
231       end,
232       direction = function()
233          local v = ltjd.get_dir_count()
234          if math.abs(getnest().mode) == ltjs.mmode and v == dir_table.dir_tate then
235             v = dir_table.dir_utod
236          end
237          return v
238       end,
239       adjustdir = ltjd.get_adjust_dir_count,
240    }
241
242    local unary_pars = luatexja.unary_pars
243    local scan_arg = token.scan_argument
244    function luatexja.ext_get_parameter_unary()
245       local k= scan_arg()
246       if unary_pars[k] then
247          texwrite(tostring(unary_pars[k](getcount(cnt_stack))))
248       end
249       ltjb.stop_time_measure 'get_par'
250    end
251 end
252
253
254 -- EXT: print parameters that need arguments
255 do
256    luatexja.binary_pars = {
257       jacharrange = function(c, t)
258          if type(c)~='number' or c<-1 or c>31*ltjc.ATTR_RANGE then
259             -- 0, -1 はエラーにしない(隠し)
260             ltjb.package_error('luatexja',
261                                'invalid character range number (' .. tostring(c) .. ')',
262                                'A character range number should be in the range 1..'
263                                .. 31*ltjc.ATTR_RANGE .. ",\n"..
264                                'So I changed this one to ' .. 31*ltjc.ATTR_RANGE .. ".")
265             c=0 -- external range 217 == internal range 0
266          elseif c==31*ltjc.ATTR_RANGE then c=0
267          end
268          -- 負の値は <U+0080 の文字の文字範囲,として出てくる.この時はいつも欧文文字なので 1 を返す
269          if c<0 then return 1 else return (ltjc.get_range_setting(c)==0) and 0 or 1 end
270       end,
271       prebreakpenalty = function(c, t)
272          return ltjs.get_stack_table(stack_ind.PRE + ltjb.in_unicode(c, true), 0, t)
273       end,
274       postbreakpenalty = function(c, t)
275          return ltjs.get_stack_table(stack_ind.POST + ltjb.in_unicode(c, true), 0, t)
276       end,
277       kcatcode = function(c, t)
278          return ltjs.get_stack_table(stack_ind.KCAT + ltjb.in_unicode(c, false), 0, t)
279       end,
280       chartorange = function(c, t)
281          return ltjc.char_to_range(ltjb.in_unicode(c, false))
282       end,
283       jaxspmode = function(c, t)
284          return ltjs.get_stack_table(stack_ind.XSP + ltjb.in_unicode(c, true), 3, t)
285       end,
286       boxdir = function(c, t)
287          if type(c)~='number' or c<0 or c>65535 then
288             ltjb.package_error('luatexja',
289                                'Bad register code (' .. tostring(c) .. ')',
290                                'A register must be between 0 and 65535.\n'..
291                                'I changed this one to zero.')
292             c=0
293          end
294          return ltjd.get_register_dir(c)
295       end,
296    }
297    local binary_pars = luatexja.binary_pars
298    local scan_arg, scan_int = token.scan_argument, token.scan_int
299    binary_pars.alxspmode = binary_pars.jaxspmode
300    function luatexja.ext_get_parameter_binary(k, c)
301       if binary_pars[k] then
302          texwrite(tostring(binary_pars[k](c, getcount(cnt_stack))))
303       end
304       ltjb.stop_time_measure 'get_par'
305    end
306 end
307
308 -- EXT: print \global if necessary
309 function luatexja.ext_print_global()
310    if luatexja.isglobal=='global' then tex.sprint(cat_lp, '\\global') end
311 end
312
313
314 -- main process
315 do
316    local start_time_measure, stop_time_measure
317       = ltjb.start_time_measure, ltjb.stop_time_measure
318    local nullfunc = function (n) return n end
319    local to_node = node.direct.tonode
320    local to_direct = node.direct.todirect
321    local ensure_tex_attr = ltjb.ensure_tex_attr
322    local slide = node.slide
323    -- mode = true iff main_process is called from pre_linebreak_filter
324    local function main_process(head, mode, dir, gc)
325       ensure_tex_attr(attr_icflag, 0)
326       if gc == 'fin_row' then return head
327       else
328             start_time_measure 'jfmglue'
329             slide(head);
330             local p = ltjj.main(to_direct(head),mode, dir)
331             stop_time_measure 'jfmglue'
332             return to_node(p)
333       end
334    end
335
336    local function adjust_icflag(h)
337       -- kern from luaotfload will have icflag = 1
338       -- (same as italic correction)
339       ensure_tex_attr(attr_icflag, 1)
340       return h
341    end
342
343    -- callbacks
344    ltjb.add_to_callback(
345       'pre_linebreak_filter',
346       function (head,groupcode)
347          return main_process(head, true, tex.textdir, groupcode)
348       end,'ltj.main',
349       luatexbase.priority_in_callback('pre_linebreak_filter', 'luaotfload.node_processor')+1)
350    ltjb.add_to_callback(
351       'hpack_filter',
352       function (head,groupcode,size,packtype, dir)
353          return main_process(head, false, dir, groupcode)
354       end,'ltj.main',
355       luatexbase.priority_in_callback('hpack_filter', 'luaotfload.node_processor')+1)
356    ltjb.add_to_callback('pre_linebreak_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
357    ltjb.add_to_callback('hpack_filter', adjust_icflag, 'ltj.adjust_icflag', 1)
358 end
359
360 -- lastnodechar
361 do
362   local get_attr, traverse_glyph = node.get_attribute, node.traverse_glyph
363   local getnest = tex.getnest
364   local id_hlist = node.id 'hlist'
365   local id_glyph = node.id 'glyph'
366   local PACKED, PROCESSED_BEGIN_FLAG = icflag_table.PACKED, icflag_table.PROCESSED_BEGIN_FLAG
367   function luatexja.pltx_composite_last_node_char()
368     local n = getnest().tail
369     local r = '-1'
370     if n then
371       if n.id==id_hlist
372         and (get_attr(n, attr_icflag) or 0) % PROCESSED_BEGIN_FLAG == PACKED then
373         for i in traverse_glyph(n.head) do n = i; break end
374       end
375       if n.id==id_glyph then
376         while n.components and  n.subtype and n.subtype%4 >= 2 do
377           n = node.tail(n)
378         end
379         r = tostring(n.char)
380       end
381     end
382   tex.sprint(-2, r)
383   end
384 end
385
386 do
387     local cache_ver = 4 -- must be same as ltj-kinsoku.tex
388     local cache_outdate_fn = function (t) return t.version~=cache_ver end
389     local t = ltjs.charprop_stack_table
390     function luatexja.load_kinsoku()
391         for i,_ in pairs(t) do t[i]=nil end
392         local kinsoku = ltjb.load_cache('ltj-kinsoku_default',cache_outdate_fn)
393         if kinsoku and kinsoku[1] then
394             t[0] = kinsoku[1]
395         else
396             t[0] = {}; tex.print(cat_lp, '\\input ltj-kinsoku.tex\\relax')
397         end
398         luatexja.load_kinsoku=nil
399     end
400 end
401
402 -- debug
403
404 do
405
406 local node_type = node.type
407 local node_next = node.next
408 local get_attr = node.get_attribute
409
410 local id_penalty = node.id 'penalty'
411 local id_glyph = node.id 'glyph'
412 local id_glue = node.id 'glue'
413 local id_kern = node.id 'kern'
414 local id_hlist = node.id 'hlist'
415 local id_vlist = node.id 'vlist'
416 local id_rule = node.id 'rule'
417 local id_math = node.id 'math'
418 local id_whatsit = node.id 'whatsit'
419 local sid_user = node.subtype 'user_defined'
420
421 local prefix, inner_depth
422 local utfchar = utf.char
423 local function debug_show_node_X(p,print_fn, limit, inner_depth)
424    local k = prefix
425    local s
426    local pt, pic = node_type(p.id), (get_attr(p, attr_icflag) or 0) % icflag_table.PROCESSED_BEGIN_FLAG
427    local base = prefix .. '[' .. string.format('%7d', node.direct.todirect(p)) .. '] ' ..
428      string.format('%X', pic) .. ' ' .. pt .. ' ' .. tostring(p.subtype) .. ' '
429    if pt == 'glyph' then
430       s = base .. ' '
431           .. (p.char<0xF0000 and utfchar(p.char) or '')
432           .. string.format(' (U+%X) ', p.char)
433           .. tostring(p.font) .. ' (' .. print_scaled(p.height) .. '+'
434           .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
435       if p.xoffset~=0 or p.yoffset~=0 then
436          s = s .. ' off: (' .. print_scaled(p.xoffset)
437                .. ',' .. print_scaled(p.yoffset) .. ')'
438       end
439       print_fn(s)
440    elseif pt=='hlist' or pt=='vlist' or pt=='unset'or pt=='ins' then
441       if pt=='ins' then
442          s = base .. '(' .. print_scaled(p.height) .. '+'
443             .. print_scaled(p.depth) .. ')'
444             .. ', dir=' .. tostring(node.get_attribute(p, attr_dir))
445       else
446          s = base .. '(' .. print_scaled(p.height) .. '+'
447             .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
448             .. ', dir=' .. tostring(node.get_attribute(p, attr_dir))
449       end
450       if (p.shift or 0)~=0 then
451          s = s .. ', shifted ' .. print_scaled(p.shift)
452       end
453       if p.glue_set and p.glue_sign ==2 or ( p.glue_sign==1 and p.glue_set>0) then
454          s = s .. ' glue set '
455          if p.glue_sign == 2 then s = s .. '-' end
456          s = s .. tostring(floor(p.glue_set*10000)/10000)
457          if p.glue_order == 0 then s = s .. 'pt'
458          else
459             s = s .. 'fi'
460             for i = 2, p.glue_order do s = s .. 'l' end
461          end
462       end
463       if pic == icflag_table.PACKED then s = s .. ' (packed)' end
464       print_fn(s);
465       local bid = inner_depth
466       prefix, inner_depth = prefix.. '.', inner_depth + 1
467       if inner_depth < limit then
468          for q in node.traverse(p.head) do
469             debug_show_node_X(q, print_fn, limit, inner_depth)
470          end
471       end
472       prefix=k
473    elseif pt=='rule' then
474       s = base .. '(' .. print_scaled(p.height) .. '+'
475          .. print_scaled(p.depth) .. ')x' .. print_scaled(p.width)
476          .. ', dir=' .. tostring(node.get_attribute(p, attr_dir))
477       print_fn(s)
478    elseif pt=='disc' then
479       print_fn(s)
480       local bid = inner_depth
481       if inner_depth < limit then
482          prefix, inner_depth = k.. 'p.', inner_depth + 1
483          for q in node.traverse(p.pre) do
484             debug_show_node_X(q, print_fn, limit, inner_depth)
485          end
486          prefix = k.. 'P.'
487          for q in node.traverse(p.post) do
488             debug_show_node_X(q, print_fn, limit, inner_depth)
489          end
490          prefix = k.. 'R.'
491          for q in node.traverse(p.replace) do
492             debug_show_node_X(q, print_fn, limit, inner_depth)
493          end
494       end
495       prefix=k
496    elseif pt == 'glue' then
497       s = base .. ' ' ..  print_spec(p)
498       if pic>icflag_table.KINSOKU and pic<icflag_table.KANJI_SKIP then
499          s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
500       elseif pic==icflag_table.KANJI_SKIP then
501          s = s .. ' (kanjiskip)'
502       elseif pic==icflag_table.KANJI_SKIP_JFM then
503          s = s .. ' (kanjiskip, JFM specified)'
504       elseif pic==icflag_table.XKANJI_SKIP then
505          s = s .. ' (xkanjiskip)'
506       elseif pic==icflag_table.XKANJI_SKIP_JFM then
507          s = s .. ' (xkanjiskip, JFM specified)'
508       end
509       print_fn(s)
510    elseif pt == 'kern' then
511       s = base .. ' ' .. print_scaled(p.kern) .. 'pt'
512       if p.subtype==2 then
513          s = s .. ' (for accent)'
514       elseif pic==icflag_table.IC_PROCESSED then
515          s = s .. ' (italic correction)'
516       elseif pic==icflag_table.LINEEND then
517          s = s .. ' (end-of-line)'
518       elseif pic>icflag_table.KINSOKU
519          and pic<icflag_table.KANJI_SKIP then
520          s = s .. ' (from JFM: priority ' .. pic-icflag_table.FROM_JFM .. ')'
521       end
522       print_fn(s)
523    elseif pt == 'penalty' then
524       s = base .. ' ' .. tostring(p.penalty)
525       if pic==icflag_table.KINSOKU then s = s .. ' (for kinsoku)' end
526       print_fn(s)
527    elseif pt == 'dir' then
528       print_fn(base .. ' ' .. tostring(p.dir) .. ' (level ' .. tostring(p.level) .. ')')
529    elseif pt == 'whatsit' then
530       s = base
531       if p.subtype==sid_user then
532          local t = tostring(p.user_id) .. ' (' ..
533             luatexbase.get_user_whatsit_name(p.user_id) .. ') '
534          if p.type ~= 110 then
535             s = s .. ' userid:' .. t .. tostring(p.value)
536             print_fn(s)
537          else
538             s = s .. ' userid:' .. t .. '(node list)'
539             if p.user_id==uid_table.DIR then
540                s = s .. ' dir: ' .. tostring(node.get_attribute(p, attr_dir))
541             end
542             print_fn(s)
543             local bid = inner_depth
544             prefix, inner_depth = prefix.. '.', inner_depth + 1
545             if inner_depth < limit then
546                for q in node.traverse(p.value) do
547                   debug_show_node_X(q, print_fn, limit, inner_depth)
548                end
549             end
550             prefix, inner_depth = k, bid
551          end
552       else
553          s = s .. (node.subtype(p.subtype) or '')
554          if p.subtype==1 then
555             s = s .. ' stream=' .. p.stream
556             print_fn(s)
557             for i=1,#p.data do
558                print_fn(s .. '  [' .. i .. '] = ' .. tostring(p.data[i] and p.date[i].csname))
559             end
560          elseif p.subtype==16 then
561             s = s .. ' mode=' .. p.mode .. ', literal="' .. p.data .. '"'
562             print_fn(s)
563          else
564             print_fn(s)
565          end
566       end
567    -------- math node --------
568    elseif pt=='noad' then
569       print_fn(base)
570       if p.nucleus then
571          prefix = k .. 'N'; debug_show_node_X(p.nucleus, print_fn, limit, inner_depth);
572       end
573       if p.sup then
574          prefix = k .. '^'; debug_show_node_X(p.sup, print_fn, limit, inner_depth);
575       end
576       if p.sub then
577          prefix = k .. '_'; debug_show_node_X(p.sub, print_fn, limit, inner_depth);
578       end
579       prefix = k;
580    elseif pt=='math_char' then
581       s = base .. ' fam: ' .. p.fam .. ' , char = ' .. utfchar(p.char)
582       print_fn(s)
583    elseif pt=='sub_box' or pt=='sub_mlist' then
584       print_fn(base)
585       if p.head then
586          prefix = k .. '.';
587          for q in node.traverse(p.head) do
588             debug_show_node_X(q, print_fn, limit, inner_depth)
589          end
590       end
591    elseif pt == 'attribute' then
592       s = base .. ' [' .. p.number .. '] = ' .. p.value
593       print_fn(s)
594    else
595       print_fn(base)
596    end
597    p=node_next(p)
598 end
599 function luatexja.ext_show_node_list(head,depth,print_fn, lim)
600   prefix = depth
601   inner_depth = 0
602   if head then
603     while head do
604       debug_show_node_X(head, print_fn, lim or 1/0, inner_depth); head = node_next(head)
605     end
606   else
607     print_fn(prefix .. ' (null list)')
608   end
609 end
610 function luatexja.ext_show_node(head,depth,print_fn, lim)
611   prefix = depth
612   inner_depth = 0
613   if head then
614     debug_show_node_X(head, print_fn, lim or 1/0, inner_depth)
615   else
616     print_fn(prefix .. ' (null list)')
617   end
618 end
619
620 end