OSDN Git Service

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