OSDN Git Service

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