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]
20 %%------------------ Lua module loading
22 %% Quark tokens needed in Lua modules.
23 \protected\def\ltj@@q@escape{\ltj@@q@escape@}
24 \protected\def\ltj@@q@escapenum{\ltj@@q@escapenum@}
27 \countdef\ltj@@count@zero=0 %
28 \luatexattributedef\ltj@@attr@zero=0 %
29 \dimendef\ltj@@dimen@zero=0 %
30 \skipdef\ltj@@skip@zero=0 %
33 \RequireLuaModule{luatexja.base}
34 \RequireLuaModule{luatexja.tangle}
35 \RequireLuaModule{luatexja.stack}
37 %%------------------ Tiny helpers
40 \newcount\ltj@tempcnta
41 \newcount\ltj@tempcntb
42 \newdimen\ltj@tempdima
45 %! ただ expl3 の quark とは異なり展開されるとエラーになる.
47 \protected\def\ltj@q@stop{\ltj@q@stop@}
49 \protected\def\ltj@q@nil{\ltj@q@nil@}
51 \protected\def\ltj@q@mark{\ltj@q@mark@}
53 %! etoolbox の \letcs, \cslet, \csletcs.
55 \protected\def\ltj@letcs#1#2{
56 \expandafter\let\expandafter#1\csname#2\endcsname
60 \protected\def\ltj@cslet#1#2{
61 \expandafter\let\csname#1\endcsname#2
65 \protected\def\ltj@csletcs#1#2{
66 \expandafter\let\csname#1\expandafter\endcsname
70 %% \ltj@ifx{<sutff>}{<yes>}{<no>}
71 %! LaTeX 形式の \ifx. この形式の利点は自動的に条件ネストからの
73 % Does \ifx<stuff> test in LaTeX style.
75 \ifx#1\expandafter\ltx@firstoftwo
76 \else\expandafter\ltx@secondoftwo\fi
79 %% \ltj@if@empty{<stuff>}{<yes>}{<no>}
81 % Checks if <stuff> is empty.
82 \long\def\ltj@if@empty#1{
83 \ltj@ifx{\ltj@@q@empty#1\ltj@@q@empty}
85 \protected\def\ltj@@q@empty{\ltj@@q@empty@}
87 %% \ltj@if@blank{<stuff>}{<yes>}{<no>}
88 %! <stuff> が空または空白文字からなるか.
89 % Checks if <stuff> is either empty or consisting only of spaces.
91 \ltj@@if@blankA#1\ltj@@q@empty\ltj@@q@empty
92 \ltx@secondoftwo\ltx@firstoftwo\ltj@q@nil
94 \long\def\ltj@@if@blankA#1#2\ltj@@q@empty#3#4#5\ltj@q@nil{
99 %! トリックに使う \romannumeral をエイリアスしておく.
100 % Gets the head of TEXT expanded repeatedly until an unexpandable
101 % token is seen, and if the token is a space then it is gobbled.
102 \let\ltj@burst\romannumeral
104 %%------------------ LaTeX vs plain
105 \ifltj@in@latex %<*LaTeX>
109 %(Defined in luatexja-core.sty.)
113 \ltj@csletcs{ifltj@in@plain}{iffalse}
115 %% \ltj@require@package{<package>}{<date>}
116 %! サブパッケージを読み込む. LaTeX では \RequirePackage、それ以外では
117 %! \input を使う. <date> は必須だが空でもよい.
118 \def\ltj@require@package#1#2{
119 \RequirePackage{#1}[#2]
122 %% \ltj@print{<message>}
124 \let\ltj@print\typeout
129 \ifnum\pdf@strcmp{\fmtname}{plain}=0 %
130 \ltj@csletcs{ifltj@in@plain}{iftrue}
132 \ltj@csletcs{ifltj@in@plain}{iffalse}
135 %% \ltj@require@package{<package>}{<date>}
136 \def\ltj@require@package#1#2{
140 %% \ltj@print{<message>}
142 \immediate\write16{#1}
146 %%------------------ Value-token handling
150 %% \ltj@gobble@num <number>
151 %! 次に続く整数を(2 回展開で)読み捨てる.
153 \ltj@burst-`>\ltj@@gobble@num
155 \def\ltj@@gobble@num{
156 \expandafter\ltj@@gobble@numA\the\parshapeindent
159 \lccode`8=`p\lccode`9=`t
161 \def\ltj@@gobble@numA#189{ }
164 %% \ltj@gobble@glue <glue>
165 %! 次に続くグルー値を(2 回展開で)読み捨てる.
166 \def\ltj@gobble@glue{%
167 \ltj@burst-`>\ltj@@gobble@glue
169 \def\ltj@@gobble@glue{
170 \expandafter\ltj@@gobble@glueA\the\glueshrinkorder
172 \def\ltj@@gobble@glueA#1{ }
174 %% \ltj@gobble@dimen <dimen>
175 %! 次に続く寸法値を(2 回展開で)読み捨てる.
176 \def\ltj@gobble@dimen{%
177 \ltj@burst-`>\ltj@@gobble@dimen
179 \def\ltj@@gobble@dimen{
180 \expandafter\ltj@@gobble@dimenA\the\glueshrinkorder
183 \def\ltj@@gobble@dimenA#1{ }
186 % This is to be followed by a macro with one argument; if the macro is
187 % followed by an open-group token (catcode 1), then it receives as the
188 % argument a token \bxnt@escape instead of the group initiated by the
189 % open-group, which is left untouched.
190 \def\ltj@@scan@brace{
191 \directlua{luatexja.base.scan_brace()}
195 \def\ltj@@scan@number{
196 \directlua{luatexja.base.scan_number()}
199 %% \ltj@grab@num <number>
200 %! 次に続く整数を読み取って, それと等しい整数を表すトークン列
201 %! (必ずしも整数表記とは限らない)を { } に入れたものに(2 回で)
203 %! ただし, 整数は以下の形式のいずれかでなければならない.
204 %! - 整数表記(10 進, 8 進, 16 進, 文字)
206 %! - 内部整数パラメタ, countdef トークン
207 %! (command_name が "assign_int" であるトークン).
208 %! (注意: まだ文字表記(`A)に対応していません.)
210 \ltj@burst-`>\ltj@@grab@num
213 \ltj@@scan@brace\ltj@@grab@numA
215 \def\ltj@@grab@numA#1{
216 \ltj@ifx{#1\ltj@@q@escape}{}{%else
217 \ltj@@scan@number\ltj@@grab@numB#1
220 \def\ltj@@grab@numB#1{
221 \ltj@ifx{#1\ltj@@q@escape}{
227 \def\ltj@@grab@numC#1\ltj@@q@escapenum{
231 %%------------------ Safe passing
233 % These macros convert a token sequence denoting a TeX value to its
234 % suitable notation in Lua, and when the argument is malformed then
235 % 'nil' is returned. They are all fully-expandable.
236 %! TeX の値を Lua 上の表記に変換する. 不正形式だと nil にする.
238 \def\ltj@@safe@end{\noexpand\ltj@@safe@end}
239 % behaves same as \relax but is distinct from it
242 % The value passed to lua instead of malformed value tokens.
243 \def\ltj@safe@invalid{(nil)}
245 %% \ltj@safe@num{<number>}
247 % For a number (integer).
249 \ltj@safe@num@or\ltj@safe@invalid
251 \def\ltj@safe@num@or#1#2{
252 \expandafter\expandafter\expandafter\ltj@@safe@numA
253 \ltj@gobble@num#2\ltj@@safe@end{#2}{#1}
255 \def\ltj@@safe@numA#1\ltj@@safe@end#2#3{
263 %% \ltj@safe@dimen{<dimen>}
265 % For a dimension. The result is a scaled-point value.
267 \ltj@safe@dimen@or\ltj@safe@invalid
269 \def\ltj@safe@dimen@or#1#2{
270 \expandafter\expandafter\expandafter\ltj@@safe@dimenA
271 \ltj@gobble@dimen#2\ltj@@safe@end{#2}{#1}
273 \def\ltj@@safe@dimenA#1\ltj@@safe@end#2#3{
275 (\number\dimexpr#2\relax)
281 %% \ltj@safe@glue{<glue>}
283 % For a glue. The result is a gluespec object.
285 \ltj@safe@glue@or\ltj@safe@invalid
287 \def\ltj@safe@glue@or#1#2{
288 \expandafter\expandafter\expandafter\ltj@@safe@glueA
289 \ltj@gobble@glue#2\ltj@@safe@end{#2}{#1}
291 \def\ltj@@safe@glueA#1\ltj@@safe@end#2#3{
293 (luatexja.base.to_skip("\the\glueexpr#2\relax"))
299 %% \ltj@safe@real{<real>}
301 % For a real number given in decimal notation or a macro that
302 % expands to such notation.
303 \def\ltj@safe@real#1{
304 (tonumber("\luatexluaescapestring{#1}"))
307 %% \ltj@val@counter{<counter>}
309 % For the current value of a LaTeX counter.
310 \def\ltj@val@counter#1{
311 (\expandafter\number\csname c@#1\endcsname)
314 %% \ltj@val@skip{<skip>}
315 % For the current value of a skip (or LaTeX-length) parameter.
316 %! グルーレジスタ(LaTeX 長さ変数).
319 (luatexja.base.to_skip("\the\glueexpr#1\relax"))
321 %! 整形式の整数 → 数値 : \number#1
322 %! 内部寸法 → 数値 : \number#1
323 %! 整形式の寸法 → 数値 : \number\dimexpr#1\relax
325 %% \ltj@safe@str{<text>}
326 %! 文字列. (トークン列を非トークン化)
327 % Converts a token sequence to Lua string notation.
329 "\luatexluaescapestring{\detokenize{#1}}"
332 %% \ltj@luaescape{<text>}
333 %! 非トークン化して Lua エスケープ.
334 \def\ltj@luaescape#1{
335 \luatexluaescapestring{\detokenize{#1}}
337 %% \ltj@luaxescape{<text>}
338 %! 非トークン化せずに(展開ありで) Lua エスケープ.
339 %! つまり単なる \luatexescape.
340 \let\ltj@luaxescape\luatexluaescapestring
342 %%------------------ Fully-expandable error messaging
344 %! ixerrtrick パッケージから移植.
345 %! 展開限定文脈でも使える \PackageError 等.
346 %! Lua 関数版(luatexja/base.lua 参照)もある.
348 %% \ltj@@error@message@a
351 \xdef\ltj@@error@message@a{%
352 Type~~H <return>~~for immediate help%
356 %%<+> \ltj@MessageBreak
357 % An analogue of \MessageBreak that is used in \ltj@GenericError, etc.
358 % (\MessageBreak cannot be used there.)
359 % NB: It is realized as a macro that expands to two instances
360 % of the character of code 127. This marker is later detected
361 % by the Lua process.
365 \gdef\ltj@MessageBreak{**}
367 luatexja.base._error_set_break("**")
372 %%<+> \ltj@GenericError{<cont>}{<msg-main>}{<msg-ref>}{<msg-help>}
373 % A variant of \GenericError that can be used in expansion-only
374 % situation. The meanings of arguments are the same as the original
376 %%<+> \ltj@PlainError{<msg-main>}{<msg-help>}
377 % A simpler error messenger available in expansion-only situations.
378 % It behaves similarly to:
379 % \errhelp{<msg-help}\errmessage{<msg-main>}
381 % with the same hack as in \GenericError...
386 \catcode`\ =11\relax%
389 \def\ltj@GenericError#1#2#3#4{% not protected
391 luatexja.base._error_set_message("\ltj@luaxescape{#1}",%
392 "\ltj@luaxescape{#2.^^J^^J#3^^J\ltj@@error@message@a}",%
393 "\ltj@luaxescape{#4}")%
395 \ % use csname with four spaces as last expander
397 \def\ {% csname with four spaces
399 luatexja.base._error_show(false)% and many spaces trail!
404 \def\ltj@PlainError#1#2{%
406 luatexja.base._error_set_message(" ",%
407 "\ltj@luaxescape{#1}",%
408 "\ltj@luaxescape{#2}")%
410 \error % again a weird name is used
412 \def\error {% csname with two trailing spaces
414 luatexja.base._error_show(true)% and many spaces trail!
421 %% \ltj@@error@on@line
422 \def\ltj@@error@on@line{
423 on input line \the \inputlineno
426 %% \ltj@generic@warn@info
427 \def\ltj@@generic@warn@info#1#2#3#4{
430 luatexja.base._generic_warn_info("\ltj@luaxescape{#3}",
431 "\ltj@luaxescape{#4}", (#1 > 0), (#2 > 0))
436 %% \ltj@GenericWarning{<cont>}{<msg-main>}
437 \def\ltj@GenericWarning{
438 \ltj@@generic@warn@info{1}{1}
440 %% \ltj@GenericWarningNoLine{<cont>}{<msg-main>}
441 \def\ltj@GenericWarningNoLine{
442 \ltj@@generic@warn@info{1}{0}
444 %% \ltj@GenericInfo{<cont>}{<msg-main>}
445 \def\ltj@GenericInfo{
446 \ltj@@generic@warn@info{0}{1}
448 %% \ltj@GenericInfoNoLine{<cont>}{<msg-main>}
449 \def\ltj@GenericInfoNoLine{
450 \ltj@@generic@warn@info{0}{0}
456 \xdef\ltj@@space@seq@a{~~~~~~~~~~~~~~~~}
457 \xdef\ltj@@space@seq@b{~~~~~~~~~~~~~}
460 %% \ltj@PackageError{<pkg-name>}{<msg-main>}{<msg-help>}
461 \def\ltj@PackageError#1#2#3{%
462 \ltj@GenericError{(#1)\ltj@@space@seq@a}%
463 {Package #1 Error: #2}%
464 {See the #1 package documentation for explanation.}%
467 %% \ltj@PackageWarning{<pkg-name>}{<msg-main>}
468 \def\ltj@PackageWarning#1#2{%
469 \ltj@GenericWarning{(#1)\ltj@@space@seq@a}%
470 {Package #1 Warning: #2}%
472 %% \ltj@PackageWarningNoLine{<pkg-name>}{<msg-main>}
473 \def\ltj@PackageWarningNoLine#1#2{%
474 \ltj@GenericWarningNoLine{(#1)\ltj@@space@seq@a}%
475 {Package #1 Warning: #2}%
477 %% \ltj@PackageInfo{<pkg-name>}{<msg-main>}
478 \def\ltj@PackageInfo#1#2{%
479 \ltj@GenericInfo{(#1)\ltj@@space@seq@b}%
480 {Package #1 Info: #2}%
482 %% \ltj@PackageInfoNoLine{<pkg-name>}{<msg-main>}
483 \def\ltj@PackageInfoNoLine#1#2{%
484 \ltj@GenericInfoNoLine{(#1)\ltj@@space@seq@b}%
485 {Package #1 Info: #2}%
488 %%------------------ debug logging
489 \ifdefined\LuaTeXjaDebugEnabled
492 \RequireLuaModule{luatexja.debug}
494 %% \ltj@debug{<format>}{<arg>,...}
497 luatexja.base.debug(\ltj@safe@str{#1}
498 \ltj@if@blank{#2}{}{, }
503 %% \ltj@package@debug{<package>}{<format>}{<arg>,...}
504 \def\ltj@package@debug#1#2#3{
506 luatexja.base.package_debug(\ltj@safe@str{#1},
508 \ltj@if@blank{#3}{}{, }
516 \def\ltj@package@debug#1#2#3{}
520 %% \ltj@debug@logger\CS{<package>}
521 \def\ltj@debug@logger#1#2{
522 \def#1{\ltj@package@debug{#2}}
525 %%------------------ all done