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.stack}
28 %%------------------ Tiny helpers
31 \newcount\ltj@tempcnta
32 \newcount\ltj@tempcntb
33 \newdimen\ltj@tempdima
36 %! ただ expl3 の quark とは異なり展開されるとエラーになる.
38 \protected\def\ltj@q@stop{\ltj@q@stop@}
40 \protected\def\ltj@q@nil{\ltj@q@nil@}
42 \protected\def\ltj@q@mark{\ltj@q@mark@}
44 %! etoolbox の \letcs, \cslet, \csletcs.
46 \protected\def\ltj@letcs#1#2{
47 \expandafter\let\expandafter#1\csname#2\endcsname
51 \protected\def\ltj@cslet#1#2{
52 \expandafter\let\csname#1\endcsname#2
56 \protected\def\ltj@csletcs#1#2{
57 \expandafter\let\csname#1\expandafter\endcsname
61 %% \ltj@ifx{<sutff>}{<yes>}{<no>}
62 %! LaTeX 形式の \ifx. この形式の利点は自動的に条件ネストからの
64 % Does \ifx<stuff> test in LaTeX style.
66 \ifx#1\expandafter\ltx@firstoftwo
67 \else\expandafter\ltx@secondoftwo\fi
70 %% \ltj@if@empty{<stuff>}{<yes>}{<no>}
72 % Checks if <stuff> is empty.
73 \long\def\ltj@if@empty#1{
74 \ltj@ifx{\ltj@@q@empty#1\ltj@@q@empty}
76 \protected\def\ltj@@q@empty{\ltj@@q@empty@}
78 %% \ltj@if@blank{<stuff>}{<yes>}{<no>}
79 %! <stuff> が空または空白文字からなるか.
80 % Checks if <stuff> is either empty or consisting only of spaces.
82 \ltj@@if@blankA#1\ltj@@q@empty\ltj@@q@empty
83 \ltx@secondoftwo\ltx@firstoftwo\ltj@q@nil
85 \long\def\ltj@@if@blankA#1#2\ltj@@q@empty#3#4#5\ltj@q@nil{
90 %! トリックに使う \romannumeral をエイリアスしておく.
91 % Gets the head of TEXT expanded repeatedly until an unexpandable
92 % token is seen, and if the token is a space then it is gobbled.
93 \let\ltj@burst\romannumeral
95 %%------------------ LaTeX vs plain
96 \ifltj@in@latex %<*LaTeX>
100 %(Defined in luatexja-core.sty.)
104 \ltj@csletcs{ifltj@in@plain}{iffalse}
106 %% \ltj@require@package{<package>}{<date>}
107 %! サブパッケージを読み込む. LaTeX では \RequirePackage、それ以外では
108 %! \input を使う. <date> は必須だが空でもよい.
109 \def\ltj@require@package#1#2{
110 \RequirePackage{#1}[#2]
113 %% \ltj@print{<message>}
115 \let\ltj@print\typeout
120 \ifnum\pdf@strcmp{\fmtname}{plain}=0 %
121 \ltj@csletcs{ifltj@in@plain}{iftrue}
123 \ltj@csletcs{ifltj@in@plain}{iffalse}
126 %% \ltj@require@package{<package>}{<date>}
127 \def\ltj@require@package#1#2{
131 %% \ltj@print{<message>}
133 \immediate\write16{#1}
137 %%------------------ Value-token handling
141 %% \ltj@gobble@num <number>
142 %! 次に続く整数を(2 回展開で)読み捨てる.
144 \ltj@burst-`>\ltj@@gobble@num
146 \def\ltj@@gobble@num{
147 \expandafter\ltj@@gobble@numA\the\parshapeindent
150 \lccode`8=`p\lccode`9=`t
152 \def\ltj@@gobble@numA#189{ }
155 %% \ltj@gobble@glue <glue>
156 %! 次に続くグルー値を(2 回展開で)読み捨てる.
157 \def\ltj@gobble@glue{%
158 \ltj@burst-`>\ltj@@gobble@glue
160 \def\ltj@@gobble@glue{
161 \expandafter\ltj@@gobble@glueA\the\glueshrinkorder
163 \def\ltj@@gobble@glueA#1{ }
165 %% \ltj@gobble@dimen <dimen>
166 %! 次に続く寸法値を(2 回展開で)読み捨てる.
167 \def\ltj@gobble@dimen{%
168 \ltj@burst-`>\ltj@@gobble@dimen
170 \def\ltj@@gobble@dimen{
171 \expandafter\ltj@@gobble@dimenA\the\glueshrinkorder
174 \def\ltj@@gobble@dimenA#1{ }
177 % This is to be followed by a macro with one argument; if the macro is
178 % followed by an open-group token (catcode 1), then it receives as the
179 % argument a token \bxnt@escape instead of the group initiated by the
180 % open-group, which is left untouched.
181 \def\ltj@@scan@brace{
182 \directlua{luatexja.base.scan_brace()}
186 \def\ltj@@scan@number{
187 \directlua{luatexja.base.scan_number()}
190 %% \ltj@grab@num <number>
191 %! 次に続く整数を読み取って, それと等しい整数を表すトークン列
192 %! (必ずしも整数表記とは限らない)を { } に入れたものに(2 回で)
194 %! ただし, 整数は以下の形式のいずれかでなければならない.
195 %! - 整数表記(10 進, 8 進, 16 進, 文字)
197 %! - 内部整数パラメタ, countdef トークン
198 %! (command_name が "assign_int" であるトークン).
199 %! (注意: まだ文字表記(`A)に対応していません.)
201 \ltj@burst-`>\ltj@@grab@num
204 \ltj@@scan@brace\ltj@@grab@numA
206 \def\ltj@@grab@numA#1{
207 \ltj@ifx{#1\ltj@@q@escape}{}{%else
208 \ltj@@scan@number\ltj@@grab@numB#1
211 \def\ltj@@grab@numB#1{
212 \ltj@ifx{#1\ltj@@q@escape}{
218 \def\ltj@@grab@numC#1\ltj@@q@escapenum{
222 %%------------------ Safe passing
224 % These macros convert a token sequence denoting a TeX value to its
225 % suitable notation in Lua, and when the argument is malformed then
226 % 'nil' is returned. They are all fully-expandable.
227 %! TeX の値を Lua 上の表記に変換する. 不正形式だと nil にする.
229 \def\ltj@@safe@end{\noexpand\ltj@@safe@end}
230 % behaves same as \relax but is distinct from it
233 % The value passed to lua instead of malformed value tokens.
234 \def\ltj@safe@invalid{(nil)}
236 %% \ltj@safe@num{<number>}
238 % For a number (integer).
240 \ltj@safe@num@or\ltj@safe@invalid
242 \def\ltj@safe@num@or#1#2{
243 \expandafter\expandafter\expandafter\ltj@@safe@numA
244 \ltj@gobble@num#2\ltj@@safe@end{#2}{#1}
246 \def\ltj@@safe@numA#1\ltj@@safe@end#2#3{
254 %% \ltj@safe@dimen{<dimen>}
256 % For a dimension. The result is a scaled-point value.
258 \ltj@safe@dimen@or\ltj@safe@invalid
260 \def\ltj@safe@dimen@or#1#2{
261 \expandafter\expandafter\expandafter\ltj@@safe@dimenA
262 \ltj@gobble@dimen#2\ltj@@safe@end{#2}{#1}
264 \def\ltj@@safe@dimenA#1\ltj@@safe@end#2#3{
266 (\number\dimexpr#2\relax)
272 %% \ltj@safe@glue{<glue>}
274 % For a glue. The result is a gluespec object.
276 \ltj@safe@glue@or\ltj@safe@invalid
278 \def\ltj@safe@glue@or#1#2{
279 \expandafter\expandafter\expandafter\ltj@@safe@glueA
280 \ltj@gobble@glue#2\ltj@@safe@end{#2}{#1}
282 \def\ltj@@safe@glueA#1\ltj@@safe@end#2#3{
284 (luatexja.base.to_skip("\the\glueexpr#2\relax"))
290 %% \ltj@safe@real{<real>}
292 % For a real number given in decimal notation or a macro that
293 % expands to such notation.
294 \def\ltj@safe@real#1{
295 (tonumber("\luatexluaescapestring{#1}"))
298 %% \ltj@val@counter{<counter>}
300 % For the current value of a LaTeX counter.
301 \def\ltj@val@counter#1{
302 (\expandafter\number\csname c@#1\endcsname)
305 %% \ltj@val@skip{<skip>}
306 % For the current value of a skip (or LaTeX-length) parameter.
307 %! グルーレジスタ(LaTeX 長さ変数).
310 (luatexja.base.to_skip("\the\glueexpr#1\relax"))
312 %! 整形式の整数 → 数値 : \number#1
313 %! 内部寸法 → 数値 : \number#1
314 %! 整形式の寸法 → 数値 : \number\dimexpr#1\relax
316 %% \ltj@safe@str{<text>}
317 %! 文字列. (トークン列を非トークン化)
318 % Converts a token sequence to Lua string notation.
320 "\luatexluaescapestring{\detokenize{#1}}"
323 %% \ltj@luaescape{<text>}
324 %! 非トークン化して Lua エスケープ.
325 \def\ltj@luaescape#1{
326 \luatexluaescapestring{\detokenize{#1}}
328 %% \ltj@luaxescape{<text>}
329 %! 非トークン化せずに(展開ありで) Lua エスケープ.
330 %! つまり単なる \luatexescape.
331 \let\ltj@luaxescape\luatexluaescapestring
333 %%------------------ Fully-expandable error messaging
335 %! ixerrtrick パッケージから移植.
336 %! 展開限定文脈でも使える \PackageError 等.
337 %! Lua 関数版(luatexja/base.lua 参照)もある.
339 %% \ltj@@error@message@a
342 \xdef\ltj@@error@message@a{%
343 Type~~H <return>~~for immediate help%
347 %%<+> \ltj@MessageBreak
348 % An analogue of \MessageBreak that is used in \ltj@GenericError, etc.
349 % (\MessageBreak cannot be used there.)
350 % NB: It is realized as a macro that expands to two instances
351 % of the character of code 127. This marker is later detected
352 % by the Lua process.
356 \gdef\ltj@MessageBreak{**}
358 luatexja.base._error_set_break("**")
363 %%<+> \ltj@GenericError{<cont>}{<msg-main>}{<msg-ref>}{<msg-help>}
364 % A variant of \GenericError that can be used in expansion-only
365 % situation. The meanings of arguments are the same as the original
367 %%<+> \ltj@PlainError{<msg-main>}{<msg-help>}
368 % A simpler error messenger available in expansion-only situations.
369 % It behaves similarly to:
370 % \errhelp{<msg-help}\errmessage{<msg-main>}
372 % with the same hack as in \GenericError...
377 \catcode`\ =11\relax%
380 \def\ltj@GenericError#1#2#3#4{% not protected
382 luatexja.base._error_set_message("\ltj@luaxescape{#1}",%
383 "\ltj@luaxescape{#2.^^J^^J#3^^J\ltj@@error@message@a}",%
384 "\ltj@luaxescape{#4}")%
386 \ % use csname with four spaces as last expander
388 \def\ {% csname with four spaces
390 luatexja.base._error_show(false)% and many spaces trail!
395 \def\ltj@PlainError#1#2{%
397 luatexja.base._error_set_message(" ",%
398 "\ltj@luaxescape{#1}",%
399 "\ltj@luaxescape{#2}")%
401 \error % again a weird name is used
403 \def\error {% csname with two trailing spaces
405 luatexja.base._error_show(true)% and many spaces trail!
412 %% \ltj@@error@on@line
413 \def\ltj@@error@on@line{
414 on input line \the \inputlineno
417 %% \ltj@generic@warn@info
418 \def\ltj@@generic@warn@info#1#2#3#4{
421 luatexja.base._generic_warn_info("\ltj@luaxescape{#3}",
422 "\ltj@luaxescape{#4}", (#1 > 0), (#2 > 0))
427 %% \ltj@GenericWarning{<cont>}{<msg-main>}
428 \def\ltj@GenericWarning{
429 \ltj@@generic@warn@info{1}{1}
431 %% \ltj@GenericWarningNoLine{<cont>}{<msg-main>}
432 \def\ltj@GenericWarningNoLine{
433 \ltj@@generic@warn@info{1}{0}
435 %% \ltj@GenericInfo{<cont>}{<msg-main>}
436 \def\ltj@GenericInfo{
437 \ltj@@generic@warn@info{0}{1}
439 %% \ltj@GenericInfoNoLine{<cont>}{<msg-main>}
440 \def\ltj@GenericInfoNoLine{
441 \ltj@@generic@warn@info{0}{0}
447 \xdef\ltj@@space@seq@a{~~~~~~~~~~~~~~~~}
448 \xdef\ltj@@space@seq@b{~~~~~~~~~~~~~}
451 %% \ltj@PackageError{<pkg-name>}{<msg-main>}{<msg-help>}
452 \def\ltj@PackageError#1#2#3{%
453 \ltj@GenericError{(#1)\ltj@@space@seq@a}%
454 {Package #1 Error: #2}%
455 {See the #1 package documentation for explanation.}%
458 %% \ltj@PackageWarning{<pkg-name>}{<msg-main>}
459 \def\ltj@PackageWarning#1#2{%
460 \ltj@GenericWarning{(#1)\ltj@@space@seq@a}%
461 {Package #1 Warning: #2}%
463 %% \ltj@PackageWarningNoLine{<pkg-name>}{<msg-main>}
464 \def\ltj@PackageWarningNoLine#1#2{%
465 \ltj@GenericWarningNoLine{(#1)\ltj@@space@seq@a}%
466 {Package #1 Warning: #2}%
468 %% \ltj@PackageInfo{<pkg-name>}{<msg-main>}
469 \def\ltj@PackageInfo#1#2{%
470 \ltj@GenericInfo{(#1)\ltj@@space@seq@b}%
471 {Package #1 Info: #2}%
473 %% \ltj@PackageInfoNoLine{<pkg-name>}{<msg-main>}
474 \def\ltj@PackageInfoNoLine#1#2{%
475 \ltj@GenericInfoNoLine{(#1)\ltj@@space@seq@b}%
476 {Package #1 Info: #2}%
479 %%------------------ debug logging
480 \ifdefined\LuaTeXjaDebugEnabled
483 \RequireLuaModule{luatexja.debug}
485 %% \ltj@debug{<format>}{<arg>,...}
488 luatexja.base.debug(\ltj@safe@str{#1}
489 \ltj@if@blank{#2}{}{, }
494 %% \ltj@package@debug{<package>}{<format>}{<arg>,...}
495 \def\ltj@package@debug#1#2#3{
497 luatexja.base.package_debug(\ltj@safe@str{#1},
499 \ltj@if@blank{#3}{}{, }
507 \def\ltj@package@debug#1#2#3{}
511 %% \ltj@debug@logger\CS{<package>}
512 \def\ltj@debug@logger#1#2{
513 \def#1{\ltj@package@debug{#2}}
516 %%------------------ all done