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{ltj-base}[2013/05/14]
19 %%------------------ Tiny helpers
22 %\newcount\ltj@tempcnta % defined at luatexja-core.sty
23 \newcount\ltj@tempcntb
24 \newcount\ltj@tempcntc
25 \newdimen\ltj@tempdima
26 % \newskip\ltj@tempskipa unused
27 % \newtoks\ltj@temptoks unused
29 \countdef\ltj@@count@zero=0 %
30 \luatexattributedef\ltj@@attr@zero=0 %
31 \dimendef\ltj@@dimen@zero=0 %
32 \skipdef\ltj@@skip@zero=0 %
35 %! ただ expl3 の quark とは異なり展開されるとエラーになる.
37 \protected\def\ltj@q@stop{\ltj@q@stop@}
39 \protected\def\ltj@q@nil{\ltj@q@nil@}
41 \protected\def\ltj@q@mark{\ltj@q@mark@}
43 %! etoolbox の \letcs, \cslet, \csletcs.
45 \protected\def\ltj@letcs#1#2{
46 \expandafter\let\expandafter#1\csname#2\endcsname
50 \protected\def\ltj@cslet#1#2{
51 \expandafter\let\csname#1\endcsname#2
55 \protected\def\ltj@csletcs#1#2{
56 \expandafter\let\csname#1\expandafter\endcsname
60 %% \ltj@ifx{<sutff>}{<yes>}{<no>}
61 %! LaTeX 形式の \ifx. この形式の利点は自動的に条件ネストからの
63 % Does \ifx<stuff> test in LaTeX style.
65 \ifx#1\expandafter\ltx@firstoftwo
66 \else\expandafter\ltx@secondoftwo\fi
69 %% \ltj@if@empty{<stuff>}{<yes>}{<no>}
71 % Checks if <stuff> is empty.
72 \long\def\ltj@if@empty#1{
73 \ltj@ifx{\ltj@@q@empty#1\ltj@@q@empty}
75 \protected\def\ltj@@q@empty{\ltj@@q@empty@}
77 %% \ltj@if@blank{<stuff>}{<yes>}{<no>}
78 %! <stuff> が空または空白文字からなるか.
79 % Checks if <stuff> is either empty or consisting only of spaces.
81 \ltj@@if@blankA#1\ltj@@q@empty\ltj@@q@empty
82 \ltx@secondoftwo\ltx@firstoftwo\ltj@q@nil
84 \long\def\ltj@@if@blankA#1#2\ltj@@q@empty#3#4#5\ltj@q@nil{
89 %! トリックに使う \romannumeral をエイリアスしておく.
90 % Gets the head of TEXT expanded repeatedly until an unexpandable
91 % token is seen, and if the token is a space then it is gobbled.
92 \let\ltj@burst\romannumeral
94 %%------------------ LaTeX vs plain
95 \ifltj@in@latex %<*LaTeX>
99 %(Defined in luatexja-core.sty.)
103 \ltj@csletcs{ifltj@in@plain}{iffalse}
105 %% \ltj@require@package{<package>}{<date>}
106 %! サブパッケージを読み込む. LaTeX では \RequirePackage、それ以外では
107 %! \input を使う. <date> は必須だが空でもよい.
108 \def\ltj@require@package#1#2{
109 \RequirePackage{#1}[#2]
112 %% \ltj@print{<message>}
114 \let\ltj@print\typeout
119 \ifnum\pdf@strcmp{\fmtname}{plain}=0 %
120 \ltj@csletcs{ifltj@in@plain}{iftrue}
122 \ltj@csletcs{ifltj@in@plain}{iffalse}
125 %% \ltj@require@package{<package>}{<date>}
126 \def\ltj@require@package#1#2{
130 %% \ltj@print{<message>}
132 \immediate\write16{#1}
136 %%------------------ Value-token handling
140 %% \ltj@gobble@num <number>
141 %! 次に続く整数を(2 回展開で)読み捨てる.
143 \ltj@burst-`>\ltj@@gobble@num
145 \def\ltj@@gobble@num{
146 \expandafter\ltj@@gobble@numA\the\parshapeindent
149 \lccode`8=`p\lccode`9=`t
151 \def\ltj@@gobble@numA#189{ }
154 %% \ltj@gobble@glue <glue>
155 %! 次に続くグルー値を(2 回展開で)読み捨てる.
156 \def\ltj@gobble@glue{%
157 \ltj@burst-`>\ltj@@gobble@glue
159 \def\ltj@@gobble@glue{
160 \expandafter\ltj@@gobble@glueA\the\glueshrinkorder
162 \def\ltj@@gobble@glueA#1{ }
164 %% \ltj@gobble@dimen <dimen>
165 %! 次に続く寸法値を(2 回展開で)読み捨てる.
166 \def\ltj@gobble@dimen{%
167 \ltj@burst-`>\ltj@@gobble@dimen
169 \def\ltj@@gobble@dimen{
170 \expandafter\ltj@@gobble@dimenA\the\glueshrinkorder
173 \def\ltj@@gobble@dimenA#1{ }
176 % This is to be followed by a macro with one argument; if the macro is
177 % followed by an open-group token (catcode 1), then it receives as the
178 % argument a token \bxnt@escape instead of the group initiated by the
179 % open-group, which is left untouched.
180 \def\ltj@@scan@brace{
181 \directlua{luatexja.base.scan_brace()}
185 \def\ltj@@scan@number{
186 \directlua{luatexja.base.scan_number()}
189 %% \ltj@grab@num <number>
190 %! 次に続く整数を読み取って, それと等しい整数を表すトークン列
191 %! (必ずしも整数表記とは限らない)を { } に入れたものに(2 回で)
193 %! ただし, 整数は以下の形式のいずれかでなければならない.
194 %! - 整数表記(10 進, 8 進, 16 進, 文字)
196 %! - 内部整数パラメタ, countdef トークン
197 %! (command_name が "assign_int" であるトークン).
198 %! (注意: まだ文字表記(`A)に対応していません.)
200 \ltj@burst-`>\ltj@@grab@num
203 \ltj@@scan@brace\ltj@@grab@numA
205 \def\ltj@@grab@numA#1{
206 \ltj@ifx{#1\ltj@@q@escape}{}{%else
207 \ltj@@scan@number\ltj@@grab@numB#1
210 \def\ltj@@grab@numB#1{
211 \ltj@ifx{#1\ltj@@q@escape}{
217 \def\ltj@@grab@numC#1\ltj@@q@escapenum{
221 %%------------------ Safe passing
223 % These macros convert a token sequence denoting a TeX value to its
224 % suitable notation in Lua, and when the argument is malformed then
225 % 'nil' is returned. They are all fully-expandable.
226 %! TeX の値を Lua 上の表記に変換する. 不正形式だと nil にする.
228 \def\ltj@@safe@end{\noexpand\ltj@@safe@end}
229 % behaves same as \relax but is distinct from it
232 % The value passed to lua instead of malformed value tokens.
233 \def\ltj@safe@invalid{(nil)}
235 %% \ltj@safe@num{<number>}
237 % For a number (integer).
239 \ltj@safe@num@or\ltj@safe@invalid
241 \def\ltj@safe@num@or#1#2{
242 \expandafter\expandafter\expandafter\ltj@@safe@numA
243 \ltj@gobble@num#2\ltj@@safe@end{#2}{#1}
245 \def\ltj@@safe@numA#1\ltj@@safe@end#2#3{
253 %% \ltj@safe@dimen{<dimen>}
255 % For a dimension. The result is a scaled-point value.
257 \ltj@safe@dimen@or\ltj@safe@invalid
259 \def\ltj@safe@dimen@or#1#2{
260 \expandafter\expandafter\expandafter\ltj@@safe@dimenA
261 \ltj@gobble@dimen#2\ltj@@safe@end{#2}{#1}
263 \def\ltj@@safe@dimenA#1\ltj@@safe@end#2#3{
265 (\number\dimexpr#2\relax)
271 %% \ltj@safe@glue{<glue>}
273 % For a glue. The result is a gluespec object.
275 \ltj@safe@glue@or\ltj@safe@invalid
277 \def\ltj@safe@glue@or#1#2{
278 \expandafter\expandafter\expandafter\ltj@@safe@glueA
279 \ltj@gobble@glue#2\ltj@@safe@end{#2}{#1}
281 \def\ltj@@safe@glueA#1\ltj@@safe@end#2#3{
283 (luatexja.base.to_skip("\the\glueexpr#2\relax"))
289 %% \ltj@safe@real{<real>}
291 % For a real number given in decimal notation or a macro that
292 % expands to such notation.
293 \def\ltj@safe@real#1{
294 (tonumber("\luatexluaescapestring{#1}"))
297 %% \ltj@val@counter{<counter>}
299 % For the current value of a LaTeX counter.
300 \def\ltj@val@counter#1{
301 (\expandafter\number\csname c@#1\endcsname)
304 %% \ltj@val@skip{<skip>}
305 % For the current value of a skip (or LaTeX-length) parameter.
306 %! グルーレジスタ(LaTeX 長さ変数).
309 (luatexja.base.to_skip("\the\glueexpr#1\relax"))
311 %! 整形式の整数 → 数値 : \number#1
312 %! 内部寸法 → 数値 : \number#1
313 %! 整形式の寸法 → 数値 : \number\dimexpr#1\relax
315 %% \ltj@safe@str{<text>}
316 %! 文字列. (トークン列を非トークン化)
317 % Converts a token sequence to Lua string notation.
319 "\luatexluaescapestring{\detokenize{#1}}"
322 %% \ltj@luaescape{<text>}
323 %! 非トークン化して Lua エスケープ.
324 \def\ltj@luaescape#1{
325 \luatexluaescapestring{\detokenize{#1}}
327 %% \ltj@luaxescape{<text>}
328 %! 非トークン化せずに(展開ありで) Lua エスケープ.
329 %! つまり単なる \luatexescape.
330 \let\ltj@luaxescape\luatexluaescapestring
332 %%------------------ Fully-expandable error messaging
334 %! ixerrtrick パッケージから移植.
335 %! 展開限定文脈でも使える \PackageError 等.
336 %! Lua 関数版(luatexja/base.lua 参照)もある.
338 %% \ltj@@error@message@a
341 \xdef\ltj@@error@message@a{%
342 Type~~H <return>~~for immediate help%
346 %%<+> \ltj@MessageBreak
347 % An analogue of \MessageBreak that is used in \ltj@GenericError, etc.
348 % (\MessageBreak cannot be used there.)
349 % NB: It is realized as a macro that expands to two instances
350 % of the character of code 127. This marker is later detected
351 % by the Lua process.
355 \gdef\ltj@MessageBreak{**}
357 luatexja.base._error_set_break("**")
362 %%<+> \ltj@GenericError{<cont>}{<msg-main>}{<msg-ref>}{<msg-help>}
363 % A variant of \GenericError that can be used in expansion-only
364 % situation. The meanings of arguments are the same as the original
366 %%<+> \ltj@PlainError{<msg-main>}{<msg-help>}
367 % A simpler error messenger available in expansion-only situations.
368 % It behaves similarly to:
369 % \errhelp{<msg-help}\errmessage{<msg-main>}
371 % with the same hack as in \GenericError...
376 \catcode`\ =11\relax%
379 \def\ltj@GenericError#1#2#3#4{% not protected
381 luatexja.base._error_set_message("\ltj@luaxescape{#1}",%
382 "\ltj@luaxescape{#2.^^J^^J#3^^J\ltj@@error@message@a}",%
383 "\ltj@luaxescape{#4}")%
385 \ % use csname with four spaces as last expander
387 \def\ {% csname with four spaces
389 luatexja.base._error_show(false)% and many spaces trail!
394 \def\ltj@PlainError#1#2{%
396 luatexja.base._error_set_message(" ",%
397 "\ltj@luaxescape{#1}",%
398 "\ltj@luaxescape{#2}")%
400 \error % again a weird name is used
402 \def\error {% csname with two trailing spaces
404 luatexja.base._error_show(true)% and many spaces trail!
411 %% \ltj@@error@on@line
412 \def\ltj@@error@on@line{
413 on input line \the \inputlineno
416 %% \ltj@generic@warn@info
417 \def\ltj@@generic@warn@info#1#2#3#4{
420 luatexja.base._generic_warn_info("\ltj@luaxescape{#3}",
421 "\ltj@luaxescape{#4}", (#1 > 0), (#2 > 0))
426 %% \ltj@GenericWarning{<cont>}{<msg-main>}
427 \def\ltj@GenericWarning{
428 \ltj@@generic@warn@info{1}{1}
430 %% \ltj@GenericWarningNoLine{<cont>}{<msg-main>}
431 \def\ltj@GenericWarningNoLine{
432 \ltj@@generic@warn@info{1}{0}
434 %% \ltj@GenericInfo{<cont>}{<msg-main>}
435 \def\ltj@GenericInfo{
436 \ltj@@generic@warn@info{0}{1}
438 %% \ltj@GenericInfoNoLine{<cont>}{<msg-main>}
439 \def\ltj@GenericInfoNoLine{
440 \ltj@@generic@warn@info{0}{0}
446 \xdef\ltj@@space@seq@a{~~~~~~~~~~~~~~~~}
447 \xdef\ltj@@space@seq@b{~~~~~~~~~~~~~}
450 %% \ltj@PackageError{<pkg-name>}{<msg-main>}{<msg-help>}
451 \def\ltj@PackageError#1#2#3{%
452 \ltj@GenericError{(#1)\ltj@@space@seq@a}%
453 {Package #1 Error: #2}%
454 {See the #1 package documentation for explanation.}%
457 %% \ltj@PackageWarning{<pkg-name>}{<msg-main>}
458 \def\ltj@PackageWarning#1#2{%
459 \ltj@GenericWarning{(#1)\ltj@@space@seq@a}%
460 {Package #1 Warning: #2}%
462 %% \ltj@PackageWarningNoLine{<pkg-name>}{<msg-main>}
463 \def\ltj@PackageWarningNoLine#1#2{%
464 \ltj@GenericWarningNoLine{(#1)\ltj@@space@seq@a}%
465 {Package #1 Warning: #2}%
467 %% \ltj@PackageInfo{<pkg-name>}{<msg-main>}
468 \def\ltj@PackageInfo#1#2{%
469 \ltj@GenericInfo{(#1)\ltj@@space@seq@b}%
470 {Package #1 Info: #2}%
472 %% \ltj@PackageInfoNoLine{<pkg-name>}{<msg-main>}
473 \def\ltj@PackageInfoNoLine#1#2{%
474 \ltj@GenericInfoNoLine{(#1)\ltj@@space@seq@b}%
475 {Package #1 Info: #2}%
478 %%------------------ debug logging
479 \ifdefined\LuaTeXjaDebugEnabled
482 \RequireLuaModule{luatexja.debug}
484 %% \ltj@debug{<format>}{<arg>,...}
487 luatexja.base.debug(\ltj@safe@str{#1}
488 \ltj@if@blank{#2}{}{, }
493 %% \ltj@package@debug{<package>}{<format>}{<arg>,...}
494 \def\ltj@package@debug#1#2#3{
496 luatexja.base.package_debug(\ltj@safe@str{#1},
498 \ltj@if@blank{#3}{}{, }
506 \def\ltj@package@debug#1#2#3{}
510 %% \ltj@debug@logger\CS{<package>}
511 \def\ltj@debug@logger#1#2{
512 \def#1{\ltj@package@debug{#2}}
515 %% \ltj@afterbox <token><box>
516 %% -> \setbox\ltj@afbox<box><token>
517 %% from Sonja Maus, ``Looking Ahead for a <box>'',
518 %% TUGBoat, 11, No. 4, 1990.
520 \protected\def\ltj@afterbox#1{%
521 \def\ltj@afb@xarg{#1}%
522 \afterassignment\ltj@afb@x
524 \def\ltj@afb@x{\futurelet\next\ltj@afb@xtest}
526 \ifcase\ifx\next\hbox\tw@\fi
527 \ifx\next\vbox\tw@\fi
528 \ifx\next\vtop\tw@\fi
530 \ifx\next\copy\@ne\fi
531 \ifx\next\vsplit\@ne\fi
532 \ifx\next\lastbox\@ne\fi
533 0% ``A <box> was ...'' error will be causes by \setbox later anyway.
534 \or\afterassignment\ltj@afb@xarg
535 \or\afterassignment\ltj@afb@xagarg
539 \def\ltj@afb@xagarg{\aftergroup\ltj@afb@xarg}
542 %%------------------ all done