6 %% Avoid multiple loading.
7 \csname luatexjabaseLoaded\endcsname
9 \endlinechar=\the\endlinechar
12 \def\luatexjabaseLoaded{\endinput}
14 \ifltj@in@latex %<*LaTeX>
15 \NeedsTeXFormat{LaTeX2e}
16 \ProvidesPackage{luatexja-base}[2011/04/01 v0.1]
19 %% Quark tokens needed in Lua modules.
20 \protected\def\ltj@@q@escape{\ltj@@q@escape@}
21 \protected\def\ltj@@q@escapenum{\ltj@@q@escapenum@}
24 \RequireLuaModule{luatexja.base}
25 \RequireLuaModule{luatexja.tangle}
26 \RequireLuaModule{luatexja.charrange}
27 \RequireLuaModule{luatexja.stack}
28 \RequireLuaModule{luatexja.inputbuf}
30 %%------------------ Tiny helpers
33 \newcount\ltj@tempcnta
34 \newcount\ltj@tempcntb
35 \newdimen\ltj@tempdima
38 %! ただ expl3 の quark とは異なり展開されるとエラーになる.
40 \protected\def\ltj@q@stop{\ltj@q@stop@}
42 \protected\def\ltj@q@nil{\ltj@q@nil@}
44 \protected\def\ltj@q@mark{\ltj@q@mark@}
46 %! etoolbox の \letcs, \cslet, \csletcs.
48 \protected\def\ltj@letcs#1#2{
49 \expandafter\let\expandafter#1\csname#2\endcsname
53 \protected\def\ltj@cslet#1#2{
54 \expandafter\let\csname#1\endcsname#2
58 \protected\def\ltj@csletcs#1#2{
59 \expandafter\let\csname#1\expandafter\endcsname
63 %% \ltj@ifx{<sutff>}{<yes>}{<no>}
64 %! LaTeX 形式の \ifx. この形式の利点は自動的に条件ネストからの
66 % Does \ifx<stuff> test in LaTeX style.
68 \ifx#1\expandafter\ltx@firstoftwo
69 \else\expandafter\ltx@secondoftwo\fi
72 %% \ltj@if@empty{<stuff>}{<yes>}{<no>}
74 % Checks if <stuff> is empty.
75 \long\def\ltj@if@empty#1{
76 \ltj@ifx{\ltj@@q@empty#1\ltj@@q@empty}
78 \protected\def\ltj@@q@empty{\ltj@@q@empty@}
80 %% \ltj@if@blank{<stuff>}{<yes>}{<no>}
81 %! <stuff> が空または空白文字からなるか.
82 % Checks if <stuff> is either empty or consisting only of spaces.
84 \ltj@@if@blankA#1\ltj@@q@empty\ltj@@q@empty
85 \ltx@secondoftwo\ltx@firstoftwo\ltj@q@nil
87 \long\def\ltj@@if@blankA#1#2\ltj@@q@empty#3#4#5\ltj@q@nil{
92 %! トリックに使う \romannumeral をエイリアスしておく.
93 % Gets the head of TEXT expanded repeatedly until an unexpandable
94 % token is seen, and if the token is a space then it is gobbled.
95 \let\ltj@burst\romannumeral
97 %%------------------ LaTeX vs plain
98 \ifltj@in@latex %<*LaTeX>
102 %(Defined in luatexja-core.sty.)
106 \ltj@csletcs{ifltj@in@plain}{iffalse}
108 %% \ltj@require@package{<package>}{<date>}
109 %! サブパッケージを読み込む. LaTeX では \RequirePackage、それ以外では
110 %! \input を使う. <date> は必須だが空でもよい.
111 \def\ltj@require@package#1#2{
112 \RequirePackage{#1}[#2]
115 %% \ltj@print{<message>}
117 \let\ltj@print\typeout
122 \ifnum\pdf@strcmp{\fmtname}{plain}=0 %
123 \ltj@csletcs{ifltj@in@plain}{iftrue}
125 \ltj@csletcs{ifltj@in@plain}{iffalse}
128 %% \ltj@require@package{<package>}{<date>}
129 \def\ltj@require@package#1#2{
133 %% \ltj@print{<message>}
135 \immediate\write16{#1}
139 %%------------------ Value-token handling
143 %% \ltj@gobble@num <number>
144 %! 次に続く整数を(2 回展開で)読み捨てる.
146 \ltj@burst-`>\ltj@@gobble@num
148 \def\ltj@@gobble@num{
149 \expandafter\ltj@@gobble@numA\the\parshapeindent
152 \lccode`8=`p\lccode`9=`t
154 \def\ltj@@gobble@numA#189{ }
157 %% \ltj@gobble@glue <glue>
158 %! 次に続くグルー値を(2 回展開で)読み捨てる.
159 \def\ltj@gobble@glue{%
160 \ltj@burst-`>\ltj@@gobble@glue
162 \def\ltj@@gobble@glue{
163 \expandafter\ltj@@gobble@glueA\the\glueshrinkorder
165 \def\ltj@@gobble@glueA#1{ }
167 %% \ltj@gobble@dimen <dimen>
168 %! 次に続く寸法値を(2 回展開で)読み捨てる.
169 \def\ltj@gobble@dimen{%
170 \ltj@burst-`>\ltj@@gobble@dimen
172 \def\ltj@@gobble@dimen{
173 \expandafter\ltj@@gobble@dimenA\the\glueshrinkorder
176 \def\ltj@@gobble@dimenA#1{ }
179 % This is to be followed by a macro with one argument; if the macro is
180 % followed by an open-group token (catcode 1), then it receives as the
181 % argument a token \bxnt@escape instead of the group initiated by the
182 % open-group, which is left untouched.
183 \def\ltj@@scan@brace{
184 \directlua{luatexja.base.scan_brace()}
188 \def\ltj@@scan@number{
189 \directlua{luatexja.base.scan_number()}
192 %% \ltj@grab@num <number>
193 %! 次に続く整数を読み取って, それと等しい整数を表すトークン列
194 %! (必ずしも整数表記とは限らない)を { } に入れたものに(2 回で)
196 %! ただし, 整数は以下の形式のいずれかでなければならない.
197 %! - 整数表記(10 進, 8 進, 16 進, 文字)
199 %! - 内部整数パラメタ, countdef トークン
200 %! (command_name が "assign_int" であるトークン).
201 %! (注意: まだ文字表記(`A)に対応していません.)
203 \ltj@burst-`>\ltj@@grab@num
206 \ltj@@scan@brace\ltj@@grab@numA
208 \def\ltj@@grab@numA#1{
209 \ltj@ifx{#1\ltj@@q@escape}{}{%else
210 \ltj@@scan@number\ltj@@grab@numB#1
213 \def\ltj@@grab@numB#1{
214 \ltj@ifx{#1\ltj@@q@escape}{
220 \def\ltj@@grab@numC#1\ltj@@q@escapenum{
224 %%------------------ Safe passing
226 % These macros convert a token sequence denoting a TeX value to its
227 % suitable notation in Lua, and when the argument is malformed then
228 % 'nil' is returned. They are all fully-expandable.
229 %! TeX の値を Lua 上の表記に変換する. 不正形式だと nil にする.
231 \def\ltj@@safe@end{\noexpand\ltj@@safe@end}
232 % behaves same as \relax but is distinct from it
235 % The value passed to lua instead of malformed value tokens.
236 \def\ltj@safe@invalid{(nil)}
238 %% \ltj@safe@num{<number>}
240 % For a number (integer).
242 \ltj@safe@num@or\ltj@safe@invalid
244 \def\ltj@safe@num@or#1#2{
245 \expandafter\expandafter\expandafter\ltj@@safe@numA
246 \ltj@gobble@num#2\ltj@@safe@end{#2}{#1}
248 \def\ltj@@safe@numA#1\ltj@@safe@end#2#3{
256 %% \ltj@safe@dimen{<dimen>}
258 % For a dimension. The result is a scaled-point value.
260 \ltj@safe@dimen@or\ltj@safe@invalid
262 \def\ltj@safe@dimen@or#1#2{
263 \expandafter\expandafter\expandafter\ltj@@safe@dimenA
264 \ltj@gobble@dimen#2\ltj@@safe@end{#2}{#1}
266 \def\ltj@@safe@dimenA#1\ltj@@safe@end#2#3{
268 (\number\dimexpr#2\relax)
274 %% \ltj@safe@glue{<glue>}
276 % For a glue. The result is a gluespec object.
278 \ltj@safe@glue@or\ltj@safe@invalid
280 \def\ltj@safe@glue@or#1#2{
281 \expandafter\expandafter\expandafter\ltj@@safe@glueA
282 \ltj@gobble@glue#2\ltj@@safe@end{#2}{#1}
284 \def\ltj@@safe@glueA#1\ltj@@safe@end#2#3{
286 (luatexja.base.to_skip("\the\glueexpr#2\relax"))
292 %% \ltj@safe@real{<real>}
294 % For a real number given in decimal notation or a macro that
295 % expands to such notation.
296 \def\ltj@safe@real#1{
297 (tonumber("\luatexluaescapestring{#1}"))
300 %% \ltj@val@counter{<counter>}
302 % For the current value of a LaTeX counter.
303 \def\ltj@val@counter#1{
304 (\expandafter\number\csname c@#1\endcsname)
307 %% \ltj@val@skip{<skip>}
308 % For the current value of a skip (or LaTeX-length) parameter.
309 %! グルーレジスタ(LaTeX 長さ変数).
312 (luatexja.base.to_skip("\the\glueexpr#1\relax"))
314 %! 整形式の整数 → 数値 : \number#1
315 %! 内部寸法 → 数値 : \number#1
316 %! 整形式の寸法 → 数値 : \number\dimexpr#1\relax
318 %% \ltj@safe@str{<text>}
319 %! 文字列. (トークン列を非トークン化)
320 % Converts a token sequence to Lua string notation.
322 "\luatexluaescapestring{\detokenize{#1}}"
325 %% \ltj@luaescape{<text>}
326 %! 非トークン化して Lua エスケープ.
327 \def\ltj@luaescape#1{
328 \luatexluaescapestring{\detokenize{#1}}
330 %% \ltj@luaxescape{<text>}
331 %! 非トークン化せずに(展開ありで) Lua エスケープ.
332 %! つまり単なる \luatexescape.
333 \let\ltj@luaxescape\luatexluaescapestring
335 %%------------------ Fully-expandable error messaging
337 %! ixerrtrick パッケージから移植.
338 %! 展開限定文脈でも使える \PackageError 等.
339 %! Lua 関数版(luatexja/base.lua 参照)もある.
341 %% \ltj@@error@message@a
344 \xdef\ltj@@error@message@a{%
345 Type~~H <return>~~for immediate help%
349 %%<+> \ltj@MessageBreak
350 % An analogue of \MessageBreak that is used in \ltj@GenericError, etc.
351 % (\MessageBreak cannot be used there.)
352 % NB: It is realized as a macro that expands to two instances
353 % of the character of code 127. This marker is later detected
354 % by the Lua process.
358 \gdef\ltj@MessageBreak{**}
360 luatexja.base._error_set_break("**")
365 %%<+> \ltj@GenericError{<cont>}{<msg-main>}{<msg-ref>}{<msg-help>}
366 % A variant of \GenericError that can be used in expansion-only
367 % situation. The meanings of arguments are the same as the original
369 %%<+> \ltj@PlainError{<msg-main>}{<msg-help>}
370 % A simpler error messenger available in expansion-only situations.
371 % It behaves similarly to:
372 % \errhelp{<msg-help}\errmessage{<msg-main>}
374 % with the same hack as in \GenericError...
379 \catcode`\ =11\relax%
382 \def\ltj@GenericError#1#2#3#4{% not protected
384 luatexja.base._error_set_message("\ltj@luaxescape{#1}",%
385 "\ltj@luaxescape{#2.^^J^^J#3^^J\ltj@@error@message@a}",%
386 "\ltj@luaxescape{#4}")%
388 \ % use csname with four spaces as last expander
390 \def\ {% csname with four spaces
392 luatexja.base._error_show(false)% and many spaces trail!
397 \def\ltj@PlainError#1#2{%
399 luatexja.base._error_set_message(" ",%
400 "\ltj@luaxescape{#1}",%
401 "\ltj@luaxescape{#2}")%
403 \error % again a weird name is used
405 \def\error {% csname with two trailing spaces
407 luatexja.base._error_show(true)% and many spaces trail!
414 %% \ltj@@error@on@line
415 \def\ltj@@error@on@line{
416 on input line \the \inputlineno
419 %% \ltj@generic@warn@info
420 \def\ltj@@generic@warn@info#1#2#3#4{
423 luatexja.base._generic_warn_info("\ltj@luaxescape{#3}",
424 "\ltj@luaxescape{#4}", (#1 > 0), (#2 > 0))
429 %% \ltj@GenericWarning{<cont>}{<msg-main>}
430 \def\ltj@GenericWarning{
431 \ltj@@generic@warn@info{1}{1}
433 %% \ltj@GenericWarningNoLine{<cont>}{<msg-main>}
434 \def\ltj@GenericWarningNoLine{
435 \ltj@@generic@warn@info{1}{0}
437 %% \ltj@GenericInfo{<cont>}{<msg-main>}
438 \def\ltj@GenericInfo{
439 \ltj@@generic@warn@info{0}{1}
441 %% \ltj@GenericInfoNoLine{<cont>}{<msg-main>}
442 \def\ltj@GenericInfoNoLine{
443 \ltj@@generic@warn@info{0}{0}
449 \xdef\ltj@@space@seq@a{~~~~~~~~~~~~~~~~}
450 \xdef\ltj@@space@seq@b{~~~~~~~~~~~~~}
453 %% \ltj@PackageError{<pkg-name>}{<msg-main>}{<msg-help>}
454 \def\ltj@PackageError#1#2#3{%
455 \ltj@GenericError{(#1)\ltj@@space@seq@a}%
456 {Package #1 Error: #2}%
457 {See the #1 package documentation for explanation.}%
460 %% \ltj@PackageWarning{<pkg-name>}{<msg-main>}
461 \def\ltj@PackageWarning#1#2{%
462 \ltj@GenericWarning{(#1)\ltj@@space@seq@a}%
463 {Package #1 Warning: #2}%
465 %% \ltj@PackageWarningNoLine{<pkg-name>}{<msg-main>}
466 \def\ltj@PackageWarningNoLine#1#2{%
467 \ltj@GenericWarningNoLine{(#1)\ltj@@space@seq@a}%
468 {Package #1 Warning: #2}%
470 %% \ltj@PackageInfo{<pkg-name>}{<msg-main>}
471 \def\ltj@PackageInfo#1#2{%
472 \ltj@GenericInfo{(#1)\ltj@@space@seq@b}%
473 {Package #1 Info: #2}%
475 %% \ltj@PackageInfoNoLine{<pkg-name>}{<msg-main>}
476 \def\ltj@PackageInfoNoLine#1#2{%
477 \ltj@GenericInfoNoLine{(#1)\ltj@@space@seq@b}%
478 {Package #1 Info: #2}%
481 %%------------------ debug logging
482 \ifdefined\LuaTeXjaDebugEnabled
485 \RequireLuaModule{luatexja.debug}
487 %% \ltj@debug{<format>}{<arg>,...}
490 luatexja.base.debug(\ltj@safe@str{#1}
491 \ltj@if@blank{#2}{}{, }
496 %% \ltj@package@debug{<package>}{<format>}{<arg>,...}
497 \def\ltj@package@debug#1#2#3{
499 luatexja.base.package_debug(\ltj@safe@str{#1},
501 \ltj@if@blank{#3}{}{, }
509 \def\ltj@package@debug#1#2#3{}
513 %% \ltj@debug@logger\CS{<package>}
514 \def\ltj@debug@logger#1#2{
515 \def#1{\ltj@package@debug{#2}}
518 %%------------------ all done