OSDN Git Service

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