2 -- luatexja/ltj-base.lua
5 local tostring = tostring
6 local node, table, tex, token = node, table, tex, token
8 local cat_lp = luatexbase.catcodetables['latex-package']
14 local public_name = 'luatexja'
15 local public_version = 'alpha'
16 ltjb.public_name = public_name
17 ltjb.public_version = public_version
20 -------------------- Fully-expandable error messaging
21 local _error_set_break, _error_set_message, _error_show
22 local generic_error, _generic_warn_info
23 local generic_warning, generic_warning_no_line
24 local generic_info, generic_info_no_line
25 local package_error, package_warning, package_warning_no_line
26 local package_info, package_info_no_line
27 local ltj_error, ltj_warning_no_line
30 --! LaTeX 形式のエラーメッセージ(\PackageError 等)を
38 local function message_cont(str, c)
39 return str:gsub(err_break, LF .. c)
41 local function into_lines(str)
42 return str:gsub(err_break, LF):explode(LF)
45 _error_set_break = function (str)
49 _error_set_message = function (msgcont, main, help)
50 err_main = message_cont(main, msgcont)
51 err_help = into_lines(help)
54 _error_show = function (escchar)
55 local escapechar = tex.escapechar
56 local newlinechar = tex.newlinechar
57 local errorcontextlines = tex.errorcontextlines
58 if not escchar then tex.escapechar = -1 end
60 tex.errorcontextlines = -1
61 tex.error(err_main, err_help)
62 tex.escapechar = escapechar
63 tex.newlinechar = newlinechar
64 tex.errorcontextlines = errorcontextlines
67 local message_a = "Type H <return> for immediate help"
69 generic_error = function (msgcont, main, ref, help)
70 local mainref = main..".\n\n"..ref.."\n"..message_a
71 _error_set_message(msgcont, mainref, help)
75 _generic_warn_info = function (msgcont, main, warn, line)
76 local mainc = message_cont(main, msgcont)
77 local br = warn and "\n" or ""
78 local out = warn and "term and log" or "log"
79 local on_line = line and (" on input line "..tex.inputlineno) or ""
80 local newlinechar = tex.newlinechar
82 texio.write_nl(out, br..main..on_line.."."..br)
83 tex.newlinechar = newlinechar
86 generic_warning = function (msgcont, main)
87 _generic_warn_info(msgcont, main, true, true)
89 generic_warning_no_line = function (msgcont, main)
90 _generic_warn_info(msgcont, main, true, false)
92 generic_info = function (msgcont, main)
93 _generic_warn_info(msgcont, main, false, true)
95 generic_info_no_line = function (msgcont, main)
96 _generic_warn_info(msgcont, main, false, false)
99 package_error = function (pkgname, main, help)
100 generic_error("("..pkgname.." ",
101 "Package "..pkgname.." Error: "..main,
102 "See the "..pkgname.." package documentation for explanation.",
105 package_warning = function (pkgname, main)
106 generic_warning("("..pkgname.." ",
107 "Package "..pkgname.." Warning: "..main)
109 package_warning_no_line = function (pkgname, main)
110 generic_warning_no_line("("..pkgname.." ",
111 "Package "..pkgname.." Warning: "..main)
113 package_info = function (pkgname, main)
114 generic_info("("..pkgname.." ",
115 "Package "..pkgname.." Info: "..main)
117 package_info_no_line = function (pkgname, main)
118 generic_info_no_line("("..pkgname.." ",
119 "Package "..pkgname.." Info: "..main)
122 ltj_error = function (main, help)
123 package_error(public_name, main, help)
125 ltj_warning_no_line = function (main)
126 package_warning_no_line(public_name, main, help)
130 -------------------- TeX stream I/O
131 --! ixbase.print() と同じ
132 --- Extension to tex.print(). Each argument string may contain
133 -- newline characters, in which case the string is output (to
134 -- TeX input stream) as multiple lines.
135 -- @param ... (string) string to output
136 local function mprint(...)
139 if type(arg[1]) == "number" then
140 table.insert(lines, arg[1])
143 for _, cnk in ipairs(arg) do
144 local ls = cnk:explode("\n")
145 if ls[#ls] == "" then
146 table.remove(ls, #ls)
148 for _, l in ipairs(ls) do
149 table.insert(lines, l)
152 return tex.print(unpack(lines))
156 -------------------- Handling of TeX values
159 --! ixbase.to_dimen() と同じ
160 local function to_dimen(val)
163 elseif type(val) == "number" then
166 return tex.sp(tostring(val))
170 local function parse_dimen(val)
171 val = tostring(val):lower()
172 local r, fil = val:match("([-.%d]+)fi(l*)")
174 val, fil = r.."pt", fil:len() + 1
178 return tex.sp(val), fil
181 ltjb.to_dimen = to_dimen
184 -------------------- Virtual table for LaTeX counters
185 -- not used in current LuaTeX-ja
187 --! ixbase.counter と同じ
189 local mt_counter = {}
190 setmetatable(counter, mt_counter)
192 function mt_counter.__index(tbl, key)
193 return tex.count['c@'..key]
195 function mt_counter.__newindex(tbl, key, val)
196 tex.count['c@'..key] = val
198 ltjb.counter = counter
200 --! ixbase.length は tex.skip と全く同じなので不要.
203 -------------------- common error message
205 local function in_unicode(c, admit_math)
206 local low = admit_math and -1 or 0
207 if type(c)~='number' or c<low or c>0x10FFFF then
208 local s = 'A character number must be between ' .. tostring(low)
209 .. ' and 0x10ffff.\n'
210 .. (admit_math and "(-1 is used for denoting `math boundary')\n" or '')
211 .. 'So I changed this one to zero.'
212 package_error('luatexja',
213 'bad character code (' .. tostring(c) .. ')', s)
218 ltjb.in_unicode = in_unicode
221 -------------------- cache management
222 -- load_cache (filename, outdate)
223 -- * filename: without suffix '.lua'
224 -- * outdate(t): return true iff the cache is outdated
225 -- * return value: non-nil iff the cache is up-to-date
226 -- save_cache (filename, t): no return value
227 -- save_cache_luc (filename, t): no return value
228 -- save_cache always calls save_cache_luc.
229 -- But sometimes we want to create only the precompiled cache,
230 -- when its 'text' version is already present in LuaTeX-ja distribution.
232 require('lualibs-lpeg') -- string.split
233 require('lualibs-os') -- os.type
236 local kpse_var_value = kpse.var_value
237 local path, pathtmp = kpse_var_value("TEXMFVAR")
238 pathtmp = kpse_var_value("TEXMFSYSVAR")
239 if pathtmp then path = (path and path .. ';' or '') .. pathtmp end
240 pathtmp = kpse_var_value("TEXMFCACHE")
241 if pathtmp then path = (path and path .. ';' or '') .. pathtmp end
243 if os.type~='windows' then path = string.gsub(path, ':', ';') end
244 path = table.unique(string.split(path, ';'))
246 local cache_dir = '/luatexja'
247 local find_file = kpse.find_file
248 local join, isreadable = file.join, file.isreadable
249 local tofile, serialize = table.tofile, table.serialize
250 local luc_suffix = jit and '.lub' or '.luc'
252 -- determine save path
254 for _,v in pairs(path) do
255 local testpath = join(v, cache_dir)
256 if not lfs.isdir(testpath) then dir.mkdirs(testpath) end
257 if lfs.isdir(testpath) then savepath = testpath; break end
260 save_cache_luc = function (filename, t, serialized)
261 local fullpath = savepath .. '/' .. filename .. luc_suffix
262 local s = serialized or serialize(t, 'return', false)
265 local f = io.open(fullpath, 'wb')
267 f:write(string.dump(sa, true))
268 texio.write('(save cache: ' .. fullpath .. ')')
274 save_cache = function (filename, t)
275 local fullpath = savepath .. '/' .. filename .. '.lua'
276 local s = serialize(t, 'return', false)
278 local f = io.open(fullpath, 'w')
281 texio.write('(save cache: ' .. fullpath .. ')')
284 save_cache_luc(filename, t, s)
288 local function load_cache_a (filename, outdate)
290 for _,v in pairs(path) do
291 local fn = join(v, cache_dir, filename)
292 if isreadable(fn) then
293 texio.write('(load cache: ' .. fn .. ')')
294 result = loadfile(fn)
295 result = result and result(); break
298 if (not result) or outdate(result) then
305 load_cache = function (filename, outdate)
306 local r = load_cache_a(filename .. luc_suffix, outdate)
310 local r = load_cache_a(filename .. '.lua', outdate)
311 if r then save_cache_luc(filename, r) end -- update the precompiled cache
316 ltjb.load_cache = load_cache
317 ltjb.save_cache_luc = save_cache_luc
318 ltjb.save_cache = save_cache
323 local tex_set_attr, tex_get_attr = tex.setattribute, tex.getattribute
324 function ltjb.ensure_tex_attr(a, v)
325 if tex_get_attr(a)~=v then
332 ltjb._error_set_break = _error_set_break
333 ltjb._error_set_message = _error_set_message
334 ltjb._error_show = _error_show
335 ltjb._generic_warn_info = _generic_warn_info
337 ltjb.package_error = package_error
338 ltjb.package_warning = package_warning
339 ltjb.package_warning_no_line = package_warning_no_line
340 ltjb.package_info = package_info
341 ltjb.package_info_no_line = package_info_no_line
343 ltjb.generic_error = generic_error
344 ltjb.generic_warning = generic_warning
345 ltjb.generic_warning_no_line = generic_warning_no_line
346 ltjb.generic_info = generic_info
347 ltjb.generic_info_no_line = generic_info_no_line
349 ltjb.ltj_warning_no_line = ltj_warning_no_line
350 ltjb.ltj_error = ltj_error
352 ---- deterministic version of luatexbase.add_to_callback
353 function ltjb.add_to_callback(name,fun,description,priority)
354 local priority= priority
355 if priority==nil then
356 priority=#luatexbase.callback_descriptions(name)+1
358 if(luatexbase.callbacktypes[name] == 3 and
360 #luatexbase.callback_descriptions(name)==1) then
361 luatexbase.module_warning("luatexbase",
362 "resetting exclusive callback: " .. name)
363 luatexbase.reset_callback(name)
365 local saved_callback={},ff,dd
366 for k,v in ipairs(luatexbase.callback_descriptions(name)) do
367 if k >= priority then
368 ff,dd= luatexbase.remove_from_callback(name, v)
369 saved_callback[#saved_callback+1]={ff,dd}
372 luatexbase.base_add_to_callback(name,fun,description)
373 for _,v in ipairs(saved_callback) do
374 luatexbase.base_add_to_callback(name,v[1],v[2])
379 -------------------- mock of debug logger
380 if not ltjb.out_debug then
381 local function no_op() end
382 ltjb.start_time_measure = no_op
383 ltjb.stop_time_measure = no_op
384 ltjb.out_debug = no_op
385 ltjb.package_debug = no_op
386 ltjb.debug_logger = function() return no_op end
387 ltjb.show_term = no_op
388 ltjb.show_log = no_op
391 -------------------- all done