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 %%------------------ Tiny helpers
38 \newcount\ltj@tempcnta
39 \newcount\ltj@tempcntb
40 \newdimen\ltj@tempdima
43 %! ただ expl3 の quark とは異なり展開されるとエラーになる.
45 \protected\def\ltj@q@stop{\ltj@q@stop@}
47 \protected\def\ltj@q@nil{\ltj@q@nil@}
49 \protected\def\ltj@q@mark{\ltj@q@mark@}
51 %! etoolbox の \letcs, \cslet, \csletcs.
53 \protected\def\ltj@letcs#1#2{
54 \expandafter\let\expandafter#1\csname#2\endcsname
58 \protected\def\ltj@cslet#1#2{
59 \expandafter\let\csname#1\endcsname#2
63 \protected\def\ltj@csletcs#1#2{
64 \expandafter\let\csname#1\expandafter\endcsname
68 %% \ltj@ifx{<sutff>}{<yes>}{<no>}
69 %! LaTeX 形式の \ifx. この形式の利点は自動的に条件ネストからの
71 % Does \ifx<stuff> test in LaTeX style.
73 \ifx#1\expandafter\ltx@firstoftwo
74 \else\expandafter\ltx@secondoftwo\fi
77 %% \ltj@if@empty{<stuff>}{<yes>}{<no>}
79 % Checks if <stuff> is empty.
80 \long\def\ltj@if@empty#1{
81 \ltj@ifx{\ltj@@q@empty#1\ltj@@q@empty}
83 \protected\def\ltj@@q@empty{\ltj@@q@empty@}
85 %% \ltj@if@blank{<stuff>}{<yes>}{<no>}
86 %! <stuff> が空または空白文字からなるか.
87 % Checks if <stuff> is either empty or consisting only of spaces.
89 \ltj@@if@blankA#1\ltj@@q@empty\ltj@@q@empty
90 \ltx@secondoftwo\ltx@firstoftwo\ltj@q@nil
92 \long\def\ltj@@if@blankA#1#2\ltj@@q@empty#3#4#5\ltj@q@nil{
97 %! トリックに使う \romannumeral をエイリアスしておく.
98 % Gets the head of TEXT expanded repeatedly until an unexpandable
99 % token is seen, and if the token is a space then it is gobbled.
100 \let\ltj@burst\romannumeral
102 %%------------------ LaTeX vs plain
103 \ifltj@in@latex %<*LaTeX>
107 %(Defined in luatexja-core.sty.)
111 \ltj@csletcs{ifltj@in@plain}{iffalse}
113 %% \ltj@require@package{<package>}{<date>}
114 %! サブパッケージを読み込む. LaTeX では \RequirePackage、それ以外では
115 %! \input を使う. <date> は必須だが空でもよい.
116 \def\ltj@require@package#1#2{
117 \RequirePackage{#1}[#2]
120 %% \ltj@print{<message>}
122 \let\ltj@print\typeout
127 \ifnum\pdf@strcmp{\fmtname}{plain}=0 %
128 \ltj@csletcs{ifltj@in@plain}{iftrue}
130 \ltj@csletcs{ifltj@in@plain}{iffalse}
133 %% \ltj@require@package{<package>}{<date>}
134 \def\ltj@require@package#1#2{
138 %% \ltj@print{<message>}
140 \immediate\write16{#1}
144 %%------------------ Value-token handling
148 %% \ltj@gobble@num <number>
149 %! 次に続く整数を(2 回展開で)読み捨てる.
151 \ltj@burst-`>\ltj@@gobble@num
153 \def\ltj@@gobble@num{
154 \expandafter\ltj@@gobble@numA\the\parshapeindent
157 \lccode`8=`p\lccode`9=`t
159 \def\ltj@@gobble@numA#189{ }
162 %% \ltj@gobble@glue <glue>
163 %! 次に続くグルー値を(2 回展開で)読み捨てる.
164 \def\ltj@gobble@glue{%
165 \ltj@burst-`>\ltj@@gobble@glue
167 \def\ltj@@gobble@glue{
168 \expandafter\ltj@@gobble@glueA\the\glueshrinkorder
170 \def\ltj@@gobble@glueA#1{ }
172 %% \ltj@gobble@dimen <dimen>
173 %! 次に続く寸法値を(2 回展開で)読み捨てる.
174 \def\ltj@gobble@dimen{%
175 \ltj@burst-`>\ltj@@gobble@dimen
177 \def\ltj@@gobble@dimen{
178 \expandafter\ltj@@gobble@dimenA\the\glueshrinkorder
181 \def\ltj@@gobble@dimenA#1{ }
184 % This is to be followed by a macro with one argument; if the macro is
185 % followed by an open-group token (catcode 1), then it receives as the
186 % argument a token \bxnt@escape instead of the group initiated by the
187 % open-group, which is left untouched.
188 \def\ltj@@scan@brace{
189 \directlua{luatexja.base.scan_brace()}
193 \def\ltj@@scan@number{
194 \directlua{luatexja.base.scan_number()}
197 %% \ltj@grab@num <number>
198 %! 次に続く整数を読み取って, それと等しい整数を表すトークン列
199 %! (必ずしも整数表記とは限らない)を { } に入れたものに(2 回で)
201 %! ただし, 整数は以下の形式のいずれかでなければならない.
202 %! - 整数表記(10 進, 8 進, 16 進, 文字)
204 %! - 内部整数パラメタ, countdef トークン
205 %! (command_name が "assign_int" であるトークン).
206 %! (注意: まだ文字表記(`A)に対応していません.)
208 \ltj@burst-`>\ltj@@grab@num
211 \ltj@@scan@brace\ltj@@grab@numA
213 \def\ltj@@grab@numA#1{
214 \ltj@ifx{#1\ltj@@q@escape}{}{%else
215 \ltj@@scan@number\ltj@@grab@numB#1
218 \def\ltj@@grab@numB#1{
219 \ltj@ifx{#1\ltj@@q@escape}{
225 \def\ltj@@grab@numC#1\ltj@@q@escapenum{
229 %%------------------ Safe passing
231 % These macros convert a token sequence denoting a TeX value to its
232 % suitable notation in Lua, and when the argument is malformed then
233 % 'nil' is returned. They are all fully-expandable.
234 %! TeX の値を Lua 上の表記に変換する. 不正形式だと nil にする.
236 \def\ltj@@safe@end{\noexpand\ltj@@safe@end}
237 % behaves same as \relax but is distinct from it
240 % The value passed to lua instead of malformed value tokens.
241 \def\ltj@safe@invalid{(nil)}
243 %% \ltj@safe@num{<number>}
245 % For a number (integer).
247 \ltj@safe@num@or\ltj@safe@invalid
249 \def\ltj@safe@num@or#1#2{
250 \expandafter\expandafter\expandafter\ltj@@safe@numA
251 \ltj@gobble@num#2\ltj@@safe@end{#2}{#1}
253 \def\ltj@@safe@numA#1\ltj@@safe@end#2#3{
261 %% \ltj@safe@dimen{<dimen>}
263 % For a dimension. The result is a scaled-point value.
265 \ltj@safe@dimen@or\ltj@safe@invalid
267 \def\ltj@safe@dimen@or#1#2{
268 \expandafter\expandafter\expandafter\ltj@@safe@dimenA
269 \ltj@gobble@dimen#2\ltj@@safe@end{#2}{#1}
271 \def\ltj@@safe@dimenA#1\ltj@@safe@end#2#3{
273 (\number\dimexpr#2\relax)
279 %% \ltj@safe@glue{<glue>}
281 % For a glue. The result is a gluespec object.
283 \ltj@safe@glue@or\ltj@safe@invalid
285 \def\ltj@safe@glue@or#1#2{
286 \expandafter\expandafter\expandafter\ltj@@safe@glueA
287 \ltj@gobble@glue#2\ltj@@safe@end{#2}{#1}
289 \def\ltj@@safe@glueA#1\ltj@@safe@end#2#3{
291 (luatexja.base.to_skip("\the\glueexpr#2\relax"))
297 %% \ltj@safe@real{<real>}
299 % For a real number given in decimal notation or a macro that
300 % expands to such notation.
301 \def\ltj@safe@real#1{
302 (tonumber("\luatexluaescapestring{#1}"))
305 %% \ltj@val@counter{<counter>}
307 % For the current value of a LaTeX counter.
308 \def\ltj@val@counter#1{
309 (\expandafter\number\csname c@#1\endcsname)
312 %% \ltj@val@skip{<skip>}
313 % For the current value of a skip (or LaTeX-length) parameter.
314 %! グルーレジスタ(LaTeX 長さ変数).
317 (luatexja.base.to_skip("\the\glueexpr#1\relax"))
319 %! 整形式の整数 → 数値 : \number#1
320 %! 内部寸法 → 数値 : \number#1
321 %! 整形式の寸法 → 数値 : \number\dimexpr#1\relax
323 %% \ltj@safe@str{<text>}
324 %! 文字列. (トークン列を非トークン化)
325 % Converts a token sequence to Lua string notation.
327 "\luatexluaescapestring{\detokenize{#1}}"
330 %% \ltj@luaescape{<text>}
331 %! 非トークン化して Lua エスケープ.
332 \def\ltj@luaescape#1{
333 \luatexluaescapestring{\detokenize{#1}}
335 %% \ltj@luaxescape{<text>}
336 %! 非トークン化せずに(展開ありで) Lua エスケープ.
337 %! つまり単なる \luatexescape.
338 \let\ltj@luaxescape\luatexluaescapestring
340 %%------------------ Fully-expandable error messaging
342 %! ixerrtrick パッケージから移植.
343 %! 展開限定文脈でも使える \PackageError 等.
344 %! Lua 関数版(luatexja/base.lua 参照)もある.
346 %% \ltj@@error@message@a
349 \xdef\ltj@@error@message@a{%
350 Type~~H <return>~~for immediate help%
354 %%<+> \ltj@MessageBreak
355 % An analogue of \MessageBreak that is used in \ltj@GenericError, etc.
356 % (\MessageBreak cannot be used there.)
357 % NB: It is realized as a macro that expands to two instances
358 % of the character of code 127. This marker is later detected
359 % by the Lua process.
363 \gdef\ltj@MessageBreak{**}
365 luatexja.base._error_set_break("**")
370 %%<+> \ltj@GenericError{<cont>}{<msg-main>}{<msg-ref>}{<msg-help>}
371 % A variant of \GenericError that can be used in expansion-only
372 % situation. The meanings of arguments are the same as the original
374 %%<+> \ltj@PlainError{<msg-main>}{<msg-help>}
375 % A simpler error messenger available in expansion-only situations.
376 % It behaves similarly to:
377 % \errhelp{<msg-help}\errmessage{<msg-main>}
379 % with the same hack as in \GenericError...
384 \catcode`\ =11\relax%
387 \def\ltj@GenericError#1#2#3#4{% not protected
389 luatexja.base._error_set_message("\ltj@luaxescape{#1}",%
390 "\ltj@luaxescape{#2.^^J^^J#3^^J\ltj@@error@message@a}",%
391 "\ltj@luaxescape{#4}")%
393 \ % use csname with four spaces as last expander
395 \def\ {% csname with four spaces
397 luatexja.base._error_show(false)% and many spaces trail!
402 \def\ltj@PlainError#1#2{%
404 luatexja.base._error_set_message(" ",%
405 "\ltj@luaxescape{#1}",%
406 "\ltj@luaxescape{#2}")%
408 \error % again a weird name is used
410 \def\error {% csname with two trailing spaces
412 luatexja.base._error_show(true)% and many spaces trail!
419 %% \ltj@@error@on@line
420 \def\ltj@@error@on@line{
421 on input line \the \inputlineno
424 %% \ltj@generic@warn@info
425 \def\ltj@@generic@warn@info#1#2#3#4{
428 luatexja.base._generic_warn_info("\ltj@luaxescape{#3}",
429 "\ltj@luaxescape{#4}", (#1 > 0), (#2 > 0))
434 %% \ltj@GenericWarning{<cont>}{<msg-main>}
435 \def\ltj@GenericWarning{
436 \ltj@@generic@warn@info{1}{1}
438 %% \ltj@GenericWarningNoLine{<cont>}{<msg-main>}
439 \def\ltj@GenericWarningNoLine{
440 \ltj@@generic@warn@info{1}{0}
442 %% \ltj@GenericInfo{<cont>}{<msg-main>}
443 \def\ltj@GenericInfo{
444 \ltj@@generic@warn@info{0}{1}
446 %% \ltj@GenericInfoNoLine{<cont>}{<msg-main>}
447 \def\ltj@GenericInfoNoLine{
448 \ltj@@generic@warn@info{0}{0}
454 \xdef\ltj@@space@seq@a{~~~~~~~~~~~~~~~~}
455 \xdef\ltj@@space@seq@b{~~~~~~~~~~~~~}
458 %% \ltj@PackageError{<pkg-name>}{<msg-main>}{<msg-help>}
459 \def\ltj@PackageError#1#2#3{%
460 \ltj@GenericError{(#1)\ltj@@space@seq@a}%
461 {Package #1 Error: #2}%
462 {See the #1 package documentation for explanation.}%
465 %% \ltj@PackageWarning{<pkg-name>}{<msg-main>}
466 \def\ltj@PackageWarning#1#2{%
467 \ltj@GenericWarning{(#1)\ltj@@space@seq@a}%
468 {Package #1 Warning: #2}%
470 %% \ltj@PackageWarningNoLine{<pkg-name>}{<msg-main>}
471 \def\ltj@PackageWarningNoLine#1#2{%
472 \ltj@GenericWarningNoLine{(#1)\ltj@@space@seq@a}%
473 {Package #1 Warning: #2}%
475 %% \ltj@PackageInfo{<pkg-name>}{<msg-main>}
476 \def\ltj@PackageInfo#1#2{%
477 \ltj@GenericInfo{(#1)\ltj@@space@seq@b}%
478 {Package #1 Info: #2}%
480 %% \ltj@PackageInfoNoLine{<pkg-name>}{<msg-main>}
481 \def\ltj@PackageInfoNoLine#1#2{%
482 \ltj@GenericInfoNoLine{(#1)\ltj@@space@seq@b}%
483 {Package #1 Info: #2}%
486 %%------------------ debug logging
487 \ifdefined\LuaTeXjaDebugEnabled
490 \RequireLuaModule{luatexja.debug}
492 %% \ltj@debug{<format>}{<arg>,...}
495 luatexja.base.debug(\ltj@safe@str{#1}
496 \ltj@if@blank{#2}{}{, }
501 %% \ltj@package@debug{<package>}{<format>}{<arg>,...}
502 \def\ltj@package@debug#1#2#3{
504 luatexja.base.package_debug(\ltj@safe@str{#1},
506 \ltj@if@blank{#3}{}{, }
514 \def\ltj@package@debug#1#2#3{}
518 %% \ltj@debug@logger\CS{<package>}
519 \def\ltj@debug@logger#1#2{
520 \def#1{\ltj@package@debug{#2}}
523 %%------------------ all done