OSDN Git Service

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