OSDN Git Service

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