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}[2014/07/18]
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{
88 %%------------------ LaTeX vs plain
89 \ifltj@in@latex %<*LaTeX>
93 %(Defined in luatexja-core.sty.)
97 \ltj@csletcs{ifltj@in@plain}{iffalse}
99 %% \ltj@require@package{<package>}{<date>}
100 %! サブパッケージを読み込む. LaTeX では \RequirePackage、それ以外では
101 %! \input を使う. <date> は必須だが空でもよい.
102 \def\ltj@require@package#1#2{
103 \RequirePackage{#1}[#2]
106 %% \ltj@print{<message>}
108 \let\ltj@print\typeout
113 \ifnum\pdf@strcmp{\fmtname}{plain}=0 %
114 \ltj@csletcs{ifltj@in@plain}{iftrue}
116 \ltj@csletcs{ifltj@in@plain}{iffalse}
119 %% \ltj@require@package{<package>}{<date>}
120 \def\ltj@require@package#1#2{
124 %% \ltj@print{<message>}
126 \immediate\write16{#1}
130 %%------------------ Value-token handling
134 %% \ltj@gobble@num <number>
135 %! 次に続く整数を(2 回展開で)読み捨てる.
137 \directlua{luatexja.token.scan_int()}
140 %% \ltj@gobble@glue <glue>
141 %! 次に続くグルー値を(2 回展開で)読み捨てる.
142 \def\ltj@gobble@glue{
143 \directlua{luatexja.token.scan_glue()}
146 %% \ltj@gobble@dimen <dimen>
147 %! 次に続く寸法値を(2 回展開で)読み捨てる.
148 \def\ltj@gobble@dimen{
149 \directlua{luatexja.token.scan_dimen()}
152 %% \ltj@grab@num <number>
153 %! 次に続く整数を読み取って, それと等しい整数を表すトークン列
154 %! (必ずしも整数表記とは限らない)を { } に入れたものに(2 回で)
156 %! ただし, 整数は以下の形式のいずれかでなければならない.
157 %! - 整数表記(10 進, 8 進, 16 進, 文字)
159 %! - 内部整数パラメタ, countdef トークン
160 %! (command_name が "assign_int" であるトークン).
161 %! (注意: まだ文字表記(`A)に対応していません.)
163 \directlua{tex.sprint('{' .. luatexja.token.scan_int() .. '}')}
166 %%------------------ Safe passing
168 % These macros convert a token sequence denoting a TeX value to its
169 % suitable notation in Lua, and when the argument is malformed then
170 % 'nil' is returned. They are all fully-expandable.
171 %! TeX の値を Lua 上の表記に変換する. 不正形式だと nil にする.
173 \def\ltj@@safe@end{\noexpand\ltj@@safe@end}
174 % behaves same as \relax but is distinct from it
177 % The value passed to lua instead of malformed value tokens.
178 \def\ltj@safe@invalid{(nil)}
180 %% \ltj@safe@num{<number>}
182 % For a number (integer).
184 \ltj@safe@num@or\ltj@safe@invalid
186 \def\ltj@safe@num@or#1#2{
187 \expandafter\expandafter\expandafter\ltj@@safe@numA
188 \ltj@gobble@num#2\ltj@@safe@end{#2}{#1}
190 \def\ltj@@safe@numA#1\ltj@@safe@end#2#3{
198 %% \ltj@safe@dimen{<dimen>}
200 % For a dimension. The result is a scaled-point value.
202 \ltj@safe@dimen@or\ltj@safe@invalid
204 \def\ltj@safe@dimen@or#1#2{
205 \expandafter\expandafter\expandafter\ltj@@safe@dimenA
206 \ltj@gobble@dimen#2\ltj@@safe@end{#2}{#1}
208 \def\ltj@@safe@dimenA#1\ltj@@safe@end#2#3{
210 (\number\dimexpr#2\relax)
216 %% \ltj@safe@glue{<glue>}
218 % For a glue. The result is a gluespec object.
220 \ltj@safe@glue@or\ltj@safe@invalid
222 \def\ltj@safe@glue@or#1#2{
223 \expandafter\expandafter\expandafter\ltj@@safe@glueA
224 \ltj@gobble@glue#2\ltj@@safe@end{#2}{#1}
226 \def\ltj@@safe@glueA#1\ltj@@safe@end#2#3{
228 (luatexja.base.to_skip("\the\glueexpr#2\relax"))
234 %% \ltj@safe@real{<real>}
236 % For a real number given in decimal notation or a macro that
237 % expands to such notation.
238 \def\ltj@safe@real#1{
239 (tonumber("\luatexluaescapestring{#1}"))
242 %% \ltj@val@counter{<counter>}
244 % For the current value of a LaTeX counter.
245 \def\ltj@val@counter#1{
246 (\expandafter\number\csname c@#1\endcsname)
249 %% \ltj@val@skip{<skip>}
250 % For the current value of a skip (or LaTeX-length) parameter.
251 %! グルーレジスタ(LaTeX 長さ変数).
254 (luatexja.base.to_skip("\the\glueexpr#1\relax"))
256 %! 整形式の整数 → 数値 : \number#1
257 %! 内部寸法 → 数値 : \number#1
258 %! 整形式の寸法 → 数値 : \number\dimexpr#1\relax
260 %% \ltj@safe@str{<text>}
261 %! 文字列. (トークン列を非トークン化)
262 % Converts a token sequence to Lua string notation.
264 "\luatexluaescapestring{\detokenize{#1}}"
267 %% \ltj@luaescape{<text>}
268 %! 非トークン化して Lua エスケープ.
269 \def\ltj@luaescape#1{
270 \luatexluaescapestring{\detokenize{#1}}
272 %% \ltj@luaxescape{<text>}
273 %! 非トークン化せずに(展開ありで) Lua エスケープ.
274 %! つまり単なる \luatexescape.
275 \let\ltj@luaxescape\luatexluaescapestring
277 %%------------------ Fully-expandable error messaging
279 %! ixerrtrick パッケージから移植.
280 %! 展開限定文脈でも使える \PackageError 等.
281 %! Lua 関数版(luatexja/base.lua 参照)もある.
283 %% \ltj@@error@message@a
286 \xdef\ltj@@error@message@a{%
287 Type~~H <return>~~for immediate help%
291 %%<+> \ltj@MessageBreak
292 % An analogue of \MessageBreak that is used in \ltj@GenericError, etc.
293 % (\MessageBreak cannot be used there.)
294 % NB: It is realized as a macro that expands to two instances
295 % of the character of code 127. This marker is later detected
296 % by the Lua process.
300 \gdef\ltj@MessageBreak{**}
302 luatexja.base._error_set_break("**")
307 %%<+> \ltj@GenericError{<cont>}{<msg-main>}{<msg-ref>}{<msg-help>}
308 % A variant of \GenericError that can be used in expansion-only
309 % situation. The meanings of arguments are the same as the original
311 %%<+> \ltj@PlainError{<msg-main>}{<msg-help>}
312 % A simpler error messenger available in expansion-only situations.
313 % It behaves similarly to:
314 % \errhelp{<msg-help}\errmessage{<msg-main>}
316 % with the same hack as in \GenericError...
321 \catcode`\ =11\relax%
324 \def\ltj@GenericError#1#2#3#4{% not protected
326 luatexja.base._error_set_message("\ltj@luaxescape{#1}",%
327 "\ltj@luaxescape{#2.^^J^^J#3^^J\ltj@@error@message@a}",%
328 "\ltj@luaxescape{#4}")%
330 \ % use csname with four spaces as last expander
332 \def\ {% csname with four spaces
334 luatexja.base._error_show(false)% and many spaces trail!
339 \def\ltj@PlainError#1#2{%
341 luatexja.base._error_set_message(" ",%
342 "\ltj@luaxescape{#1}",%
343 "\ltj@luaxescape{#2}")%
345 \error % again a weird name is used
347 \def\error {% csname with two trailing spaces
349 luatexja.base._error_show(true)% and many spaces trail!
356 %% \ltj@@error@on@line
357 \def\ltj@@error@on@line{
358 on input line \the \inputlineno
361 %% \ltj@generic@warn@info
362 \def\ltj@@generic@warn@info#1#2#3#4{
365 luatexja.base._generic_warn_info("\ltj@luaxescape{#3}",
366 "\ltj@luaxescape{#4}", (#1 > 0), (#2 > 0))
371 %% \ltj@GenericWarning{<cont>}{<msg-main>}
372 \def\ltj@GenericWarning{
373 \ltj@@generic@warn@info{1}{1}
375 %% \ltj@GenericWarningNoLine{<cont>}{<msg-main>}
376 \def\ltj@GenericWarningNoLine{
377 \ltj@@generic@warn@info{1}{0}
379 %% \ltj@GenericInfo{<cont>}{<msg-main>}
380 \def\ltj@GenericInfo{
381 \ltj@@generic@warn@info{0}{1}
383 %% \ltj@GenericInfoNoLine{<cont>}{<msg-main>}
384 \def\ltj@GenericInfoNoLine{
385 \ltj@@generic@warn@info{0}{0}
391 \xdef\ltj@@space@seq@a{~~~~~~~~~~~~~~~~}
392 \xdef\ltj@@space@seq@b{~~~~~~~~~~~~~}
395 %% \ltj@PackageError{<pkg-name>}{<msg-main>}{<msg-help>}
396 \def\ltj@PackageError#1#2#3{%
397 \ltj@GenericError{(#1)\ltj@@space@seq@a}%
398 {Package #1 Error: #2}%
399 {See the #1 package documentation for explanation.}%
402 %% \ltj@PackageWarning{<pkg-name>}{<msg-main>}
403 \def\ltj@PackageWarning#1#2{%
404 \ltj@GenericWarning{(#1)\ltj@@space@seq@a}%
405 {Package #1 Warning: #2}%
407 %% \ltj@PackageWarningNoLine{<pkg-name>}{<msg-main>}
408 \def\ltj@PackageWarningNoLine#1#2{%
409 \ltj@GenericWarningNoLine{(#1)\ltj@@space@seq@a}%
410 {Package #1 Warning: #2}%
412 %% \ltj@PackageInfo{<pkg-name>}{<msg-main>}
413 \def\ltj@PackageInfo#1#2{%
414 \ltj@GenericInfo{(#1)\ltj@@space@seq@b}%
415 {Package #1 Info: #2}%
417 %% \ltj@PackageInfoNoLine{<pkg-name>}{<msg-main>}
418 \def\ltj@PackageInfoNoLine#1#2{%
419 \ltj@GenericInfoNoLine{(#1)\ltj@@space@seq@b}%
420 {Package #1 Info: #2}%
423 %%------------------ debug logging
424 \ifdefined\LuaTeXjaDebugEnabled
427 \RequireLuaModule{luatexja.debug}
429 %% \ltj@debug{<format>}{<arg>,...}
432 luatexja.base.debug(\ltj@safe@str{#1}
433 \ltj@if@blank{#2}{}{, }
438 %% \ltj@package@debug{<package>}{<format>}{<arg>,...}
439 \def\ltj@package@debug#1#2#3{
441 luatexja.base.package_debug(\ltj@safe@str{#1},
443 \ltj@if@blank{#3}{}{, }
451 \def\ltj@package@debug#1#2#3{}
455 %% \ltj@debug@logger\CS{<package>}
456 \def\ltj@debug@logger#1#2{
457 \def#1{\ltj@package@debug{#2}}
460 %% \ltj@afterbox <token><box>
461 %% -> \setbox\ltj@afbox<box><token>
462 %% from Sonja Maus, ``Looking Ahead for a <box>'',
463 %% TUGBoat, 11, No. 4, 1990.
465 \protected\def\ltj@afterbox#1{%
466 \def\ltj@afb@xarg{#1}%
467 \afterassignment\ltj@afb@x
469 \def\ltj@afb@x{\futurelet\next\ltj@afb@xtest}
471 \ifcase\ifx\next\hbox\tw@\fi
472 \ifx\next\vbox\tw@\fi
473 \ifx\next\vtop\tw@\fi
475 \ifx\next\copy\@ne\fi
476 \ifx\next\vsplit\@ne\fi
477 \ifx\next\lastbox\@ne\fi
478 0% ``A <box> was ...'' error will be causes by \setbox later anyway.
479 \or\afterassignment\ltj@afb@xarg
480 \or\afterassignment\ltj@afb@xagarg
484 \def\ltj@afb@xagarg{\aftergroup\ltj@afb@xarg}
487 %%------------------ all done