OSDN Git Service

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