OSDN Git Service

Regenerated luatexja-{ja,en,zh}.pdf.
[luatex-ja/luatexja.git] / src / ltj-base.sty
1 %
2 % ltj-base.sty
3 %
4
5 %! これは外から読まれない
6 %% Avoid multiple loading.
7 \csname luatexjabaseLoaded\endcsname
8 \edef\ltj@base@AtEnd{%
9 \endlinechar=\the\endlinechar
10 \relax}
11 \endlinechar=-1 %
12 \def\luatexjabaseLoaded{\endinput}
13
14 \ifltj@in@latex                 %<*LaTeX>
15   \NeedsTeXFormat{LaTeX2e}
16   \ProvidesPackage{ltj-base}[2012/04/21 v0.2]
17 \fi                             %</LaTeX>
18
19 %%------------------ Tiny helpers
20
21 %% Registers
22 \newcount\ltj@tempcnta
23 \newcount\ltj@tempcntb
24 \newcount\ltj@tempcntc
25 \newdimen\ltj@tempdima
26 \newskip\ltj@tempskipa
27 \newtoks\ltj@temptoks
28
29 \countdef\ltj@@count@zero=0 %
30 \luatexattributedef\ltj@@attr@zero=0 %
31 \dimendef\ltj@@dimen@zero=0 %
32 \skipdef\ltj@@skip@zero=0 %
33
34 %% Quarks
35 %! ただ expl3 の quark とは異なり展開されるとエラーになる.
36 %% \ltj@q@stop
37 \protected\def\ltj@q@stop{\ltj@q@stop@}
38 %% \ltj@q@nil
39 \protected\def\ltj@q@nil{\ltj@q@nil@}
40 %% \ltj@q@mark
41 \protected\def\ltj@q@mark{\ltj@q@mark@}
42
43 %! etoolbox の \letcs, \cslet, \csletcs.
44 %% \ltj@letcs
45 \protected\def\ltj@letcs#1#2{
46   \expandafter\let\expandafter#1\csname#2\endcsname
47 }
48
49 %% \ltj@cslet
50 \protected\def\ltj@cslet#1#2{
51   \expandafter\let\csname#1\endcsname#2
52 }
53
54 %% \ltj@csletcs
55 \protected\def\ltj@csletcs#1#2{
56   \expandafter\let\csname#1\expandafter\endcsname
57   \csname#2\endcsname
58 }
59
60 %% \ltj@ifx{<sutff>}{<yes>}{<no>}
61 %! LaTeX 形式の \ifx. この形式の利点は自動的に条件ネストからの
62 %! 脱出が可能であること.
63 % Does \ifx<stuff> test in LaTeX style.
64 \long\def\ltj@ifx#1{
65   \ifx#1\expandafter\ltx@firstoftwo
66   \else\expandafter\ltx@secondoftwo\fi
67 }
68
69 %% \ltj@if@empty{<stuff>}{<yes>}{<no>}
70 %! <stuff> が空であるか.
71 % Checks if <stuff> is empty.
72 \long\def\ltj@if@empty#1{
73   \ltj@ifx{\ltj@@q@empty#1\ltj@@q@empty}
74 }
75 \protected\def\ltj@@q@empty{\ltj@@q@empty@}
76
77 %% \ltj@if@blank{<stuff>}{<yes>}{<no>}
78 %! <stuff> が空または空白文字からなるか.
79 % Checks if <stuff> is either empty or consisting only of spaces.
80 \def\ltj@if@blank#1{
81   \ltj@@if@blankA#1\ltj@@q@empty\ltj@@q@empty
82    \ltx@secondoftwo\ltx@firstoftwo\ltj@q@nil
83 }
84 \long\def\ltj@@if@blankA#1#2\ltj@@q@empty#3#4#5\ltj@q@nil{
85   #4
86 }
87
88 %% \ltj@burst-`>TEXT
89 %! トリックに使う \romannumeral をエイリアスしておく.
90 % Gets the head of TEXT expanded repeatedly until an unexpandable
91 % token is seen, and if the token is a space then it is gobbled.
92 \let\ltj@burst\romannumeral
93
94 %%------------------ LaTeX vs plain
95 \ifltj@in@latex            %<*LaTeX>
96
97 %% \ifltj@in@latex
98 %! LaTeX であるか.
99 %(Defined in luatexja-core.sty.)
100
101 %% \ifltj@in@plain
102 %! plain であるか.
103 \ltj@csletcs{ifltj@in@plain}{iffalse}
104
105 %% \ltj@require@package{<package>}{<date>}
106 %! サブパッケージを読み込む. LaTeX では \RequirePackage、それ以外では
107 %! \input を使う. <date> は必須だが空でもよい.
108 \def\ltj@require@package#1#2{
109   \RequirePackage{#1}[#2]
110 }
111
112 %% \ltj@print{<message>}
113 %! 端末への出力.
114 \let\ltj@print\typeout
115
116 \else                           %<*!LaTeX>
117
118 %% \ifltj@in@plain
119 \ifnum\pdf@strcmp{\fmtname}{plain}=0 %
120   \ltj@csletcs{ifltj@in@plain}{iftrue}
121 \else
122   \ltj@csletcs{ifltj@in@plain}{iffalse}
123 \fi
124
125 %% \ltj@require@package{<package>}{<date>}
126 \def\ltj@require@package#1#2{
127   \input #1.sty\relax
128 }
129
130 %% \ltj@print{<message>}
131 \def\ltj@print#1{
132   \immediate\write16{#1}
133 }
134
135 \fi                             %</LaTeX>
136 %%------------------ Value-token handling
137
138 %% helper stuffs
139
140 %% \ltj@gobble@num <number>
141 %! 次に続く整数を(2 回展開で)読み捨てる. 
142 \def\ltj@gobble@num{
143   \ltj@burst-`>\ltj@@gobble@num
144 }
145 \def\ltj@@gobble@num{
146   \expandafter\ltj@@gobble@numA\the\parshapeindent
147 }
148 \begingroup
149   \lccode`8=`p\lccode`9=`t
150 \lowercase{\endgroup
151   \def\ltj@@gobble@numA#189{ }
152 }
153
154 %% \ltj@gobble@glue <glue>
155 %! 次に続くグルー値を(2 回展開で)読み捨てる. 
156 \def\ltj@gobble@glue{%
157   \ltj@burst-`>\ltj@@gobble@glue
158 }
159 \def\ltj@@gobble@glue{
160   \expandafter\ltj@@gobble@glueA\the\glueshrinkorder
161 }
162 \def\ltj@@gobble@glueA#1{ }
163
164 %% \ltj@gobble@dimen <dimen>
165 %! 次に続く寸法値を(2 回展開で)読み捨てる. 
166 \def\ltj@gobble@dimen{%
167   \ltj@burst-`>\ltj@@gobble@dimen
168 }
169 \def\ltj@@gobble@dimen{
170   \expandafter\ltj@@gobble@dimenA\the\glueshrinkorder
171   0pt minus
172 }
173 \def\ltj@@gobble@dimenA#1{ }
174
175 %% \ltj@@scan@brace
176 % This is to be followed by a macro with one argument; if the macro is
177 % followed by an open-group token (catcode 1), then it receives as the
178 % argument a token \bxnt@escape instead of the group initiated by the
179 % open-group, which is left untouched.
180 \def\ltj@@scan@brace{
181   \directlua{luatexja.base.scan_brace()}
182 }
183
184 %% \ltj@@scan@number
185 \def\ltj@@scan@number{
186   \directlua{luatexja.base.scan_number()}
187 }
188
189 %% \ltj@grab@num <number>
190 %! 次に続く整数を読み取って, それと等しい整数を表すトークン列
191 %! (必ずしも整数表記とは限らない)を { } に入れたものに(2 回で)
192 %! 展開する.
193 %! ただし, 整数は以下の形式のいずれかでなければならない.
194 %! - 整数表記(10 進, 8 進, 16 進, 文字)
195 %! - chardef トークン
196 %! - 内部整数パラメタ, countdef トークン
197 %!   (command_name が "assign_int" であるトークン).
198 %! (注意: まだ文字表記(`A)に対応していません.)
199 \def\ltj@grab@num{
200   \ltj@burst-`>\ltj@@grab@num
201 }
202 \def\ltj@@grab@num{
203   \ltj@@scan@brace\ltj@@grab@numA
204 }
205 \def\ltj@@grab@numA#1{
206   \ltj@ifx{#1\ltj@@q@escape}{}{%else
207     \ltj@@scan@number\ltj@@grab@numB#1
208   }
209 }
210 \def\ltj@@grab@numB#1{
211   \ltj@ifx{#1\ltj@@q@escape}{
212     {0}
213   }{
214     \ltj@@grab@numC
215   }
216 }
217 \def\ltj@@grab@numC#1\ltj@@q@escapenum{
218   {#1}
219 }
220
221 %%------------------ Safe passing
222
223 % These macros convert a token sequence denoting a TeX value to its
224 % suitable notation in Lua, and when the argument is malformed then
225 % 'nil' is returned. They are all fully-expandable.
226 %! TeX の値を Lua 上の表記に変換する. 不正形式だと nil にする.
227
228 \def\ltj@@safe@end{\noexpand\ltj@@safe@end}
229   % behaves same as \relax but is distinct from it
230
231 %% \ltj@safe@invalid
232 % The value passed to lua instead of malformed value tokens.
233 \def\ltj@safe@invalid{(nil)}
234
235 %% \ltj@safe@num{<number>}
236 %! 整数.
237 % For a number (integer).
238 \def\ltj@safe@num{
239   \ltj@safe@num@or\ltj@safe@invalid
240 }
241 \def\ltj@safe@num@or#1#2{
242   \expandafter\expandafter\expandafter\ltj@@safe@numA
243    \ltj@gobble@num#2\ltj@@safe@end{#2}{#1}
244 }
245 \def\ltj@@safe@numA#1\ltj@@safe@end#2#3{
246   \ltj@if@blank{#1}{
247     (\number#2)
248   }{
249     #3
250   }
251 }
252
253 %% \ltj@safe@dimen{<dimen>}
254 %! 寸法値.
255 % For a dimension. The result is a scaled-point value.
256 \def\ltj@safe@dimen{
257   \ltj@safe@dimen@or\ltj@safe@invalid
258 }
259 \def\ltj@safe@dimen@or#1#2{
260   \expandafter\expandafter\expandafter\ltj@@safe@dimenA
261    \ltj@gobble@dimen#2\ltj@@safe@end{#2}{#1}
262 }
263 \def\ltj@@safe@dimenA#1\ltj@@safe@end#2#3{
264   \ltj@if@blank{#1}{
265     (\number\dimexpr#2\relax)
266   }{
267     #3
268   }
269 }
270
271 %% \ltj@safe@glue{<glue>}
272 %! グルー値.
273 % For a glue. The result is a gluespec object.
274 \def\ltj@safe@glue{
275   \ltj@safe@glue@or\ltj@safe@invalid
276 }
277 \def\ltj@safe@glue@or#1#2{
278   \expandafter\expandafter\expandafter\ltj@@safe@glueA
279    \ltj@gobble@glue#2\ltj@@safe@end{#2}{#1}
280 }
281 \def\ltj@@safe@glueA#1\ltj@@safe@end#2#3{
282   \ltj@if@blank{#1}{
283     (luatexja.base.to_skip("\the\glueexpr#2\relax"))
284   }{
285     #3
286   }
287 }
288
289 %% \ltj@safe@real{<real>}
290 %! 実数. これは十進表記に限る.
291 % For a real number given in decimal notation or a macro that
292 % expands to such notation.
293 \def\ltj@safe@real#1{
294   (tonumber("\luatexluaescapestring{#1}"))
295 }
296
297 %% \ltj@val@counter{<counter>}
298 %! LaTeX カウンタの現在値.
299 % For the current value of a LaTeX counter.
300 \def\ltj@val@counter#1{
301   (\expandafter\number\csname c@#1\endcsname)
302 }
303
304 %% \ltj@val@skip{<skip>}
305 % For the current value of a skip (or LaTeX-length) parameter.
306 %! グルーレジスタ(LaTeX 長さ変数).
307 %! 整形式のグルー値にも使える.
308 \def\ltj@val@skip#1{
309   (luatexja.base.to_skip("\the\glueexpr#1\relax"))
310 }
311 %! 整形式の整数 → 数値 : \number#1
312 %! 内部寸法 → 数値 : \number#1
313 %! 整形式の寸法 → 数値 : \number\dimexpr#1\relax
314
315 %% \ltj@safe@str{<text>}
316 %! 文字列. (トークン列を非トークン化)
317 % Converts a token sequence to Lua string notation.
318 \def\ltj@safe@str#1{
319   "\luatexluaescapestring{\detokenize{#1}}"
320 }
321
322 %% \ltj@luaescape{<text>}
323 %! 非トークン化して Lua エスケープ.
324 \def\ltj@luaescape#1{
325   \luatexluaescapestring{\detokenize{#1}}
326 }
327 %% \ltj@luaxescape{<text>}
328 %! 非トークン化せずに(展開ありで) Lua エスケープ.
329 %! つまり単なる \luatexescape.
330 \let\ltj@luaxescape\luatexluaescapestring
331
332 %%------------------ Fully-expandable error messaging
333
334 %! ixerrtrick パッケージから移植.
335 %! 展開限定文脈でも使える \PackageError 等.
336 %! Lua 関数版(luatexja/base.lua 参照)もある.
337
338 %% \ltj@@error@message@a
339 \begingroup
340 \def~{ }
341 \xdef\ltj@@error@message@a{%
342   Type~~H <return>~~for immediate help%
343 }%
344 \endgroup
345
346 %%<+> \ltj@MessageBreak
347 % An analogue of \MessageBreak that is used in \ltj@GenericError, etc.
348 % (\MessageBreak cannot be used there.)
349 % NB: It is realized as a macro that expands to two instances
350 % of the character of code 127. This marker is later detected
351 % by the Lua process.
352 \begingroup
353 \lccode42=127
354 \lowercase{
355   \gdef\ltj@MessageBreak{**}
356   \directlua{
357     luatexja.base._error_set_break("**")
358   }
359 }
360 \endgroup
361
362 %%<+> \ltj@GenericError{<cont>}{<msg-main>}{<msg-ref>}{<msg-help>}
363 % A variant of \GenericError that can be used in expansion-only
364 % situation. The meanings of arguments are the same as the original
365 % \GenericError.
366 %%<+> \ltj@PlainError{<msg-main>}{<msg-help>}
367 % A simpler error messenger available in expansion-only situations.
368 % It behaves similarly to:
369 %   \errhelp{<msg-help}\errmessage{<msg-main>}
370 \begingroup
371 % with the same hack as in \GenericError...
372 \lccode`\@=`\ %
373 \lccode`\~=`\ %
374 \lccode`\}=`\ %
375 \lccode`\{=`\ %
376 \catcode`\ =11\relax%
377 \lowercase{%
378 \endgroup%
379 \def\ltj@GenericError#1#2#3#4{% not protected
380 \directlua{%
381 luatexja.base._error_set_message("\ltj@luaxescape{#1}",%
382 "\ltj@luaxescape{#2.^^J^^J#3^^J\ltj@@error@message@a}",%
383 "\ltj@luaxescape{#4}")%
384 }%
385 \    % use csname with four spaces as last expander
386 }
387 \def\    {% csname with four spaces
388 \directlua{%
389 luatexja.base._error_show(false)% and many spaces trail!
390                                         %
391                                         %
392 }%
393 }%
394 \def\ltj@PlainError#1#2{%
395 \directlua{%
396 luatexja.base._error_set_message("  ",%
397 "\ltj@luaxescape{#1}",%
398 "\ltj@luaxescape{#2}")%
399 }%
400 \error  % again a weird name is used
401 }
402 \def\error  {% csname with two trailing spaces
403 \directlua{%
404 luatexja.base._error_show(true)% and many spaces trail!
405                                         %
406                                         %
407 }%
408 }%
409 }
410
411 %% \ltj@@error@on@line
412 \def\ltj@@error@on@line{
413   on input line \the \inputlineno
414 }
415
416 %% \ltj@generic@warn@info
417 \def\ltj@@generic@warn@info#1#2#3#4{
418   \begingroup
419     \directlua{
420       luatexja.base._generic_warn_info("\ltj@luaxescape{#3}",
421         "\ltj@luaxescape{#4}", (#1 > 0), (#2 > 0))
422     }
423   \endgroup
424 }
425
426 %% \ltj@GenericWarning{<cont>}{<msg-main>}
427 \def\ltj@GenericWarning{
428   \ltj@@generic@warn@info{1}{1}
429 }
430 %% \ltj@GenericWarningNoLine{<cont>}{<msg-main>}
431 \def\ltj@GenericWarningNoLine{
432   \ltj@@generic@warn@info{1}{0}
433 }
434 %% \ltj@GenericInfo{<cont>}{<msg-main>}
435 \def\ltj@GenericInfo{
436   \ltj@@generic@warn@info{0}{1}
437 }
438 %% \ltj@GenericInfoNoLine{<cont>}{<msg-main>}
439 \def\ltj@GenericInfoNoLine{
440   \ltj@@generic@warn@info{0}{0}
441 }
442
443 %% \ltj@@space@seq@a
444 \begingroup
445 \def~{ }
446 \xdef\ltj@@space@seq@a{~~~~~~~~~~~~~~~~}
447 \xdef\ltj@@space@seq@b{~~~~~~~~~~~~~}
448 \endgroup
449
450 %% \ltj@PackageError{<pkg-name>}{<msg-main>}{<msg-help>}
451 \def\ltj@PackageError#1#2#3{%
452   \ltj@GenericError{(#1)\ltj@@space@seq@a}%
453    {Package #1 Error: #2}%
454    {See the #1 package documentation for explanation.}%
455    {#3}%
456 }
457 %% \ltj@PackageWarning{<pkg-name>}{<msg-main>}
458 \def\ltj@PackageWarning#1#2{%
459   \ltj@GenericWarning{(#1)\ltj@@space@seq@a}%
460    {Package #1 Warning: #2}%
461 }
462 %% \ltj@PackageWarningNoLine{<pkg-name>}{<msg-main>}
463 \def\ltj@PackageWarningNoLine#1#2{%
464   \ltj@GenericWarningNoLine{(#1)\ltj@@space@seq@a}%
465    {Package #1 Warning: #2}%
466 }
467 %% \ltj@PackageInfo{<pkg-name>}{<msg-main>}
468 \def\ltj@PackageInfo#1#2{%
469   \ltj@GenericInfo{(#1)\ltj@@space@seq@b}%
470    {Package #1 Info: #2}%
471 }
472 %% \ltj@PackageInfoNoLine{<pkg-name>}{<msg-main>}
473 \def\ltj@PackageInfoNoLine#1#2{%
474   \ltj@GenericInfoNoLine{(#1)\ltj@@space@seq@b}%
475    {Package #1 Info: #2}%
476 }
477
478 %%------------------ debug logging
479 \ifdefined\LuaTeXjaDebugEnabled
480
481 %% Load Lua module
482 \RequireLuaModule{luatexja.debug}
483
484 %% \ltj@debug{<format>}{<arg>,...}
485 \def\ltj@debug#1#2{
486   \directlua{
487     luatexja.base.debug(\ltj@safe@str{#1}
488       \ltj@if@blank{#2}{}{, }
489       #2)
490   }
491 }
492
493 %% \ltj@package@debug{<package>}{<format>}{<arg>,...}
494 \def\ltj@package@debug#1#2#3{
495   \directlua{
496     luatexja.base.package_debug(\ltj@safe@str{#1},
497       \ltj@safe@str{#2}
498       \ltj@if@blank{#3}{}{, }
499       #3)
500   }
501 }
502
503 \else
504
505 \def\ltj@debug#1#2{}
506 \def\ltj@package@debug#1#2#3{}
507
508 \fi
509
510 %% \ltj@debug@logger\CS{<package>}
511 \def\ltj@debug@logger#1#2{
512   \def#1{\ltj@package@debug{#2}}
513 }
514
515 %%------------------ all done
516 \ltj@base@AtEnd
517 \endinput
518 %% EOF