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 %%------------------ Tiny helpers
29 \newcount\ltj@tempcnta
30 \newcount\ltj@tempcntb
31 \newdimen\ltj@tempdima
34 %! ただ expl3 の quark とは異なり展開されるとエラーになる.
36 \protected\def\ltj@q@stop{\ltj@q@stop@}
38 \protected\def\ltj@q@nil{\ltj@q@nil@}
40 \protected\def\ltj@q@mark{\ltj@q@mark@}
42 %! etoolbox の \letcs, \cslet, \csletcs.
44 \protected\def\ltj@letcs#1#2{
45 \expandafter\let\expandafter#1\csname#2\endcsname
49 \protected\def\ltj@cslet#1#2{
50 \expandafter\let\csname#1\endcsname#2
54 \protected\def\ltj@csletcs#1#2{
55 \expandafter\let\csname#1\expandafter\endcsname
59 %% \ltj@ifx{<sutff>}{<yes>}{<no>}
60 %! LaTeX 形式の \ifx. この形式の利点は自動的に条件ネストからの
62 % Does \ifx<stuff> test in LaTeX style.
64 \ifx#1\expandafter\ltx@firstoftwo
65 \else\expandafter\ltx@secondoftwo\fi
68 %% \ltj@if@empty{<stuff>}{<yes>}{<no>}
70 % Checks if <stuff> is empty.
71 \long\def\ltj@if@empty#1{
72 \ltj@ifx{\ltj@@q@empty#1\ltj@@q@empty}
74 \protected\def\ltj@@q@empty{\ltj@@q@empty@}
76 %% \ltj@if@blank{<stuff>}{<yes>}{<no>}
77 %! <stuff> が空または空白文字からなるか.
78 % Checks if <stuff> is either empty or consisting only of spaces.
80 \ltj@@if@blankA#1\ltj@@q@empty\ltj@@q@empty
81 \ltx@secondoftwo\ltx@firstoftwo\ltj@q@nil
83 \long\def\ltj@@if@blankA#1#2\ltj@@q@empty#3#4#5\ltj@q@nil{
88 %! トリックに使う \romannumeral をエイリアスしておく.
89 % Gets the head of TEXT expanded repeatedly until an unexpandable
90 % token is seen, and if the token is a space then it is gobbled.
91 \let\ltj@burst\romannumeral
93 %%------------------ LaTeX vs plain
94 \ifltj@in@latex %<*LaTeX>
98 %(Defined in luatexja-core.sty.)
102 \ltj@csletcs{ifltj@in@plain}{iffalse}
104 %% \ltj@require@package{<package>}{<date>}
105 %! サブパッケージを読み込む. LaTeX では \RequirePackage、それ以外では
106 %! \input を使う. <date> は必須だが空でもよい.
107 \def\ltj@require@package#1#2{
108 \RequirePackage{#1}[#2]
111 %% \ltj@print{<message>}
113 \let\ltj@print\typeout
118 \ifnum\pdf@strcmp{\fmtname}{plain}=0 %
119 \ltj@csletcs{ifltj@in@plain}{iftrue}
121 \ltj@csletcs{ifltj@in@plain}{iffalse}
124 %% \ltj@require@package{<package>}{<date>}
125 \def\ltj@require@package#1#2{
129 %% \ltj@print{<message>}
131 \immediate\write16{#1}
135 %%------------------ Value-token handling
139 %% \ltj@gobble@num <number>
140 %! 次に続く整数を(2 回展開で)読み捨てる.
142 \ltj@burst-`>\ltj@@gobble@num
144 \def\ltj@@gobble@num{
145 \expandafter\ltj@@gobble@numA\the\parshapeindent
148 \lccode`8=`p\lccode`9=`t
150 \def\ltj@@gobble@numA#189{ }
153 %% \ltj@gobble@glue <glue>
154 %! 次に続くグルー値を(2 回展開で)読み捨てる.
155 \def\ltj@gobble@glue{%
156 \ltj@burst-`>\ltj@@gobble@glue
158 \def\ltj@@gobble@glue{
159 \expandafter\ltj@@gobble@glueA\the\glueshrinkorder
161 \def\ltj@@gobble@glueA#1{ }
163 %% \ltj@gobble@dimen <dimen>
164 %! 次に続く寸法値を(2 回展開で)読み捨てる.
165 \def\ltj@gobble@dimen{%
166 \ltj@burst-`>\ltj@@gobble@dimen
168 \def\ltj@@gobble@dimen{
169 \expandafter\ltj@@gobble@dimenA\the\glueshrinkorder
172 \def\ltj@@gobble@dimenA#1{ }
175 % This is to be followed by a macro with one argument; if the macro is
176 % followed by an open-group token (catcode 1), then it receives as the
177 % argument a token \bxnt@escape instead of the group initiated by the
178 % open-group, which is left untouched.
179 \def\ltj@@scan@brace{
180 \directlua{luatexja.base.scan_brace()}
184 \def\ltj@@scan@number{
185 \directlua{luatexja.base.scan_number()}
188 %% \ltj@grab@num <number>
189 %! 次に続く整数を読み取って, それと等しい整数を表すトークン列
190 %! (必ずしも整数表記とは限らない)を { } に入れたものに(2 回で)
192 %! ただし, 整数は以下の形式のいずれかでなければならない.
193 %! - 整数表記(10 進, 8 進, 16 進, 文字)
195 %! - 内部整数パラメタ, countdef トークン
196 %! (command_name が "assign_int" であるトークン).
197 %! (注意: まだ文字表記(`A)に対応していません.)
199 \ltj@burst-`>\ltj@@grab@num
202 \ltj@@scan@brace\ltj@@grab@numA
204 \def\ltj@@grab@numA#1{
205 \ltj@ifx{#1\ltj@@q@escape}{}{%else
206 \ltj@@scan@number\ltj@@grab@numB#1
209 \def\ltj@@grab@numB#1{
210 \ltj@ifx{#1\ltj@@q@escape}{
216 \def\ltj@@grab@numC#1\ltj@@q@escapenum{
220 %%------------------ Safe passing
222 % These macros convert a token sequence denoting a TeX value to its
223 % suitable notation in Lua, and when the argument is malformed then
224 % 'nil' is returned. They are all fully-expandable.
225 %! TeX の値を Lua 上の表記に変換する. 不正形式だと nil にする.
227 \def\ltj@@safe@end{\noexpand\ltj@@safe@end}
228 % behaves same as \relax but is distinct from it
231 % The value passed to lua instead of malformed value tokens.
232 \def\ltj@safe@invalid{(nil)}
234 %% \ltj@safe@num{<number>}
236 % For a number (integer).
238 \ltj@safe@num@or\ltj@safe@invalid
240 \def\ltj@safe@num@or#1#2{
241 \expandafter\expandafter\expandafter\ltj@@safe@numA
242 \ltj@gobble@num#2\ltj@@safe@end{#2}{#1}
244 \def\ltj@@safe@numA#1\ltj@@safe@end#2#3{
252 %% \ltj@safe@dimen{<dimen>}
254 % For a dimension. The result is a scaled-point value.
256 \ltj@safe@dimen@or\ltj@safe@invalid
258 \def\ltj@safe@dimen@or#1#2{
259 \expandafter\expandafter\expandafter\ltj@@safe@dimenA
260 \ltj@gobble@dimen#2\ltj@@safe@end{#2}{#1}
262 \def\ltj@@safe@dimenA#1\ltj@@safe@end#2#3{
264 (\number\dimexpr#2\relax)
270 %% \ltj@safe@glue{<glue>}
272 % For a glue. The result is a gluespec object.
274 \ltj@safe@glue@or\ltj@safe@invalid
276 \def\ltj@safe@glue@or#1#2{
277 \expandafter\expandafter\expandafter\ltj@@safe@glueA
278 \ltj@gobble@glue#2\ltj@@safe@end{#2}{#1}
280 \def\ltj@@safe@glueA#1\ltj@@safe@end#2#3{
282 (luatexja.base.to_skip("\the\glueexpr#2\relax"))
288 %% \ltj@safe@real{<real>}
290 % For a real number given in decimal notation or a macro that
291 % expands to such notation.
292 \def\ltj@safe@real#1{
293 (tonumber("\luatexluaescapestring{#1}"))
296 %% \ltj@val@counter{<counter>}
298 % For the current value of a LaTeX counter.
299 \def\ltj@val@counter#1{
300 (\expandafter\number\csname c@#1\endcsname)
303 %% \ltj@val@skip{<skip>}
304 % For the current value of a skip (or LaTeX-length) parameter.
305 %! グルーレジスタ(LaTeX 長さ変数).
308 (luatexja.base.to_skip("\the\glueexpr#1\relax"))
310 %! 整形式の整数 → 数値 : \number#1
311 %! 内部寸法 → 数値 : \number#1
312 %! 整形式の寸法 → 数値 : \number\dimexpr#1\relax
314 %% \ltj@safe@str{<text>}
315 %! 文字列. (トークン列を非トークン化)
316 % Converts a token sequence to Lua string notation.
318 "\luatexluaescapestring{\detokenize{#1}}"
321 %% \ltj@luaescape{<text>}
322 %! 非トークン化して Lua エスケープ.
323 \def\ltj@luaescape#1{
324 \luatexluaescapestring{\detokenize{#1}}
326 %% \ltj@luaxescape{<text>}
327 %! 非トークン化せずに(展開ありで) Lua エスケープ.
328 %! つまり単なる \luatexescape.
329 \let\ltj@luaxescape\luatexluaescapestring
331 %%------------------ Fully-expandable error messaging
333 %! ixerrtrick パッケージから移植.
334 %! 展開限定文脈でも使える \PackageError 等.
335 %! Lua 関数版(luatexja/base.lua 参照)もある.
337 %% \ltj@@error@message@a
340 \xdef\ltj@@error@message@a{%
341 Type~~H <return>~~for immediate help%
345 %%<+> \ltj@MessageBreak
346 % An analogue of \MessageBreak that is used in \ltj@GenericError, etc.
347 % (\MessageBreak cannot be used there.)
348 % NB: It is realized as a macro that expands to two instances
349 % of the character of code 127. This marker is later detected
350 % by the Lua process.
354 \gdef\ltj@MessageBreak{**}
356 luatexja.base._error_set_break("**")
361 %%<+> \ltj@GenericError{<cont>}{<msg-main>}{<msg-ref>}{<msg-help>}
362 % A variant of \GenericError that can be used in expansion-only
363 % situation. The meanings of arguments are the same as the original
365 %%<+> \ltj@PlainError{<msg-main>}{<msg-help>}
366 % A simpler error messenger available in expansion-only situations.
367 % It behaves similarly to:
368 % \errhelp{<msg-help}\errmessage{<msg-main>}
370 % with the same hack as in \GenericError...
375 \catcode`\ =11\relax%
378 \def\ltj@GenericError#1#2#3#4{% not protected
380 luatexja.base._error_set_message("\ltj@luaxescape{#1}",%
381 "\ltj@luaxescape{#2.^^J^^J#3^^J\ltj@@error@message@a}",%
382 "\ltj@luaxescape{#4}")%
384 \ % use csname with four spaces as last expander
386 \def\ {% csname with four spaces
388 luatexja.base._error_show(false)% and many spaces trail!
393 \def\ltj@PlainError#1#2{%
395 luatexja.base._error_set_message(" ",%
396 "\ltj@luaxescape{#1}",%
397 "\ltj@luaxescape{#2}")%
399 \error % again a weird name is used
401 \def\error {% csname with two trailing spaces
403 luatexja.base._error_show(true)% and many spaces trail!
410 %% \ltj@@error@on@line
411 \def\ltj@@error@on@line{
412 on input line \the \inputlineno
415 %% \ltj@generic@warn@info
416 \def\ltj@@generic@warn@info#1#2#3#4{
419 luatexja.base._generic_warn_info("\ltj@luaxescape{#3}",
420 "\ltj@luaxescape{#4}", (#1 > 0), (#2 > 0))
425 %% \ltj@GenericWarning{<cont>}{<msg-main>}
426 \def\ltj@GenericWarning{
427 \ltj@@generic@warn@info{1}{1}
429 %% \ltj@GenericWarningNoLine{<cont>}{<msg-main>}
430 \def\ltj@GenericWarningNoLine{
431 \ltj@@generic@warn@info{1}{0}
433 %% \ltj@GenericInfo{<cont>}{<msg-main>}
434 \def\ltj@GenericInfo{
435 \ltj@@generic@warn@info{0}{1}
437 %% \ltj@GenericInfoNoLine{<cont>}{<msg-main>}
438 \def\ltj@GenericInfoNoLine{
439 \ltj@@generic@warn@info{0}{0}
445 \xdef\ltj@@space@seq@a{~~~~~~~~~~~~~~~~}
446 \xdef\ltj@@space@seq@b{~~~~~~~~~~~~~}
449 %% \ltj@PackageError{<pkg-name>}{<msg-main>}{<msg-help>}
450 \def\ltj@PackageError#1#2#3{%
451 \ltj@GenericError{(#1)\ltj@@space@seq@a}%
452 {Package #1 Error: #2}%
453 {See the #1 package documentation for explanation.}%
456 %% \ltj@PackageWarning{<pkg-name>}{<msg-main>}
457 \def\ltj@PackageWarning#1#2{%
458 \ltj@GenericWarning{(#1)\ltj@@space@seq@a}%
459 {Package #1 Warning: #2}%
461 %% \ltj@PackageWarningNoLine{<pkg-name>}{<msg-main>}
462 \def\ltj@PackageWarningNoLine#1#2{%
463 \ltj@GenericWarningNoLine{(#1)\ltj@@space@seq@a}%
464 {Package #1 Warning: #2}%
466 %% \ltj@PackageInfo{<pkg-name>}{<msg-main>}
467 \def\ltj@PackageInfo#1#2{%
468 \ltj@GenericInfo{(#1)\ltj@@space@seq@b}%
469 {Package #1 Info: #2}%
471 %% \ltj@PackageInfoNoLine{<pkg-name>}{<msg-main>}
472 \def\ltj@PackageInfoNoLine#1#2{%
473 \ltj@GenericInfoNoLine{(#1)\ltj@@space@seq@b}%
474 {Package #1 Info: #2}%
477 %%------------------ debug logging
478 \ifdefined\LuaTeXjaDebugEnabled
481 \RequireLuaModule{luatexja.debug}
483 %% \ltj@debug{<format>}{<arg>,...}
486 luatexja.base.debug(\ltj@safe@str{#1}
487 \ltj@if@blank{#2}{}{, }
492 %% \ltj@package@debug{<package>}{<format>}{<arg>,...}
493 \def\ltj@package@debug#1#2#3{
495 luatexja.base.package_debug(\ltj@safe@str{#1},
497 \ltj@if@blank{#3}{}{, }
505 \def\ltj@package@debug#1#2#3{}
509 %% \ltj@debug@logger\CS{<package>}
510 \def\ltj@debug@logger#1#2{
511 \def#1{\ltj@package@debug{#2}}
514 %%------------------ all done