summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/jlreq/jlreq-helpers.sty
blob: 3594c7d1f82d5ff12698700c39e3314732c2187b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
% エンジン非依存のマクロ集

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{jlreq-helpers}[2018/08/15 jlreq-helpers]
\RequirePackage{xkeyval,etoolbox}

%. helper1
\newcount\jlreq@tempcnta
\newcount\jlreq@tempcntb
\newcount\jlreq@tempcntc
\newdimen\jlreq@tempdima
\newdimen\jlreq@tempdimb
\newdimen\jlreq@tempdimc
\newskip\jlreq@tempskipa
\newskip\jlreq@tempskipb
\newskip\jlreq@tempskipc
\newif\ifjlreq@tempa
\newif\ifjlreq@tempb
\newif\ifjlreq@tempc
\newbox\jlreq@tempboxa
\newbox\jlreq@tempboxb
\newbox\jlreq@tempboxc
% 計算結果を返すための変数
\newdimen\jlreq@resultdimen
\newskip\jlreq@resultskip
\newbox\jlreq@resultbox
\newif\ifjlreq@result
\newcount\jlreq@resultcnt

\def\jlreq@uniqtoken{\jlreq@uniqtoken@may@be@a@bug}
\def\jlreq@endmark{\jlreq@endmark@may@be@a@bug}
\long\def\jlreq@deleteuntileendmark#1\jlreq@endmark{}

\def\jlreq@expandafter@ii{\expandafter\expandafter\expandafter}
\edef\jlreq@expandafter@iii{\expandonce{\jlreq@expandafter@ii}\noexpand\expandafter\expandonce{\jlreq@expandafter@ii}}
\edef\jlreq@expandtwice#1{\expandonce{\jlreq@expandafter@ii}\noexpand\unexpanded\expandonce{\jlreq@expandafter@ii}{#1}}
\edef\jlreq@expand@three#1{\expandonce{\jlreq@expandafter@iii}\noexpand\unexpanded\expandonce{\jlreq@expandafter@iii}{#1}}

% \jlreq@getfirsttoken#1\jlreq@uniqtoken{}\jlreq@endmark@の形で使う
% 二回展開すると,#1の先頭(ただし空白は無視される)を取り出す.#1が空の場合は\jlreq@uniqtoken,先頭が{の場合は\bgroupとなる
\def\jlreq@endmark@{\jlreq@endmark@@may@be@a@bug} % \jlreq@getfirsttokenだけのためのendmark
\long\def\jlreq@getfirsttoken#1#{\jlreq@getfirsttoken@#1\bgroup\jlreq@endmark@}
\long\def\jlreq@getfirsttoken@#1#2\jlreq@endmark@#3\jlreq@endmark@{#1}
% 与えられたトークン列が空か(展開はしないでチェック)
\long\def\jlreq@ifempty#1{\expandafter\ifx\expandafter\relax\detokenize{#1}\relax\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi}
% #1の先頭トークンが#2か(ただし#1が空ならば#2=\jlreq@uniqtoken,#1={ならば#2=\bgroupでTRUEとなる)
\long\edef\jlreq@iffirsttoken#1#2{%
  % スペースチェック
  \unexpanded{\expandafter\expandafter\expandafter\jlreq@deleteuntileendmark\expandafter\ifx\expandafter}\expandonce{\space}\noexpand\detokenize{#1}\unexpanded{\@undefined\jlreq@endmark\expandafter\@firstoftwo\else\jlreq@endmark\expandafter\@secondoftwo\fi}{%
    \noexpand\ifx\expandonce{\space}#2\unexpanded{\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi}%
  }{%
    \noexpand\jlreq@iffirsttoken@ignorespaces{#1}{#2}%
  }%
}
% こちらは先頭のスペースを読み飛ばす
\long\def\jlreq@iffirsttoken@ignorespaces#1#2{%
  \expandafter\expandafter\expandafter\ifx\jlreq@getfirsttoken#1\jlreq@uniqtoken{}\jlreq@endmark@#2\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}

% #1が\jlreq@endmarkか
\long\def\jlreq@ifendmark#1{\jlreq@iffirsttoken{#1}{\jlreq@endmark}}

% 条件.\jlreq@if{\ifnum\@count=1 \fi}{<TRUE>}{<FALSE>}みたいに使う
\def\jlreq@if@endmark{\jlreq@if@endmark}
\def\jlreq@if#1{\jlreq@if@#1\jlreq@if@endmark}
\@gobbletwo\if\if
\def\jlreq@if@#1\fi\jlreq@if@endmark{#1\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi}

% #1の先頭が引数なしのマクロか
\long\def\jlreq@ifmacro@noargument#1{%
  \jlreq@ifempty{#1}{\@secondoftwo}{%
    \jlreq@iffirsttoken{#1}{ }{\@secondoftwo}{%
      \expandafter\expandafter\expandafter\jlreq@ifmacro@noargument@\jlreq@getfirsttoken#1\jlreq@uniqtoken{}\jlreq@endmark@
}}}
\long\def\jlreq@ifmacro@noargument@#1{\expandafter\jlreq@ifmacro@noargument@@\expandafter{\meaning #1}}
\edef\jlreq@do{%
  \unexpanded{\def\jlreq@ifmacro@noargument@@}##1{\noexpand\jlreq@ifmacro@noargument@@@##1\expandonce{\detokenize{macro:->}}\noexpand\jlreq@endmark}
  \unexpanded{\def\jlreq@ifmacro@noargument@@@}##1\expandonce{\detokenize{macro:->}}##2\noexpand\jlreq@endmark{\noexpand\jlreq@ifempty{##1}}
}
\jlreq@do

% #1(skipレジスタ)が0か(展開不可).
\newcommand*\jlreq@ifgluezero[1]{%
  \begingroup
    \edef\@tempa{\the#1}%
    \edef\@tempb{\detokenize{0.0pt}}%
    \edef\jlreq@do{\ifx\@tempa\@tempb\noexpand\@firstoftwo\else\noexpand\@secondoftwo\fi}%
  \expandafter\endgroup
  \jlreq@do
}

% #1が#2で終わるか(展開不可)
\newcommand*{\jlreq@ifendswith}[2]{%
  \jlreq@ifempty{#2}{\@firstoftwo}{%
    \begingroup
      \def\jlreq@tempa##1#2\jlreq@uniqtoken##2\jlreq@endmark{%
        \edef\jlreq@do{\jlreq@ifempty{##2}{\noexpand\@secondoftwo}{\noexpand\@firstoftwo}}%
      }%
      \jlreq@tempa#1\jlreq@uniqtoken#2\jlreq@uniqtoken\jlreq@endmark
    \expandafter\endgroup
    \jlreq@do
  }%
}
% #1が#2で始まるか(展開不可)
\newcommand*{\jlreq@ifstartswith}[2]{%
  \jlreq@ifempty{#2}{\@firstoftwo}{%
    \jlreq@ifempty{#1}{\@secondoftwo}{%
      \begingroup
        \def\jlreq@tempa##1#2##2\jlreq@endmark{%
          \edef\jlreq@do{\jlreq@ifempty{##1}{\noexpand\@firstoftwo}{\noexpand\@secondoftwo}}%
        }%
        \jlreq@tempa#1#2\jlreq@endmark
      \expandafter\endgroup
      \jlreq@do
    }%
  }%
}
% #1が#2を含むか(展開不可){}内に#2があってもFALSE判定.
\newcommand*{\jlreq@ifcontains}[2]{%
  \jlreq@ifempty{#2}{\@firstoftwo}{%
    \begingroup
      \def\jlreq@tempa##1#2##2\jlreq@endmark{%
        \edef\jlreq@do{\jlreq@ifempty{##2}{\noexpand\@secondoftwo}{\noexpand\@firstoftwo}}%
      }%
      \jlreq@tempa#1#2\jlreq@endmark
    \expandafter\endgroup
    \jlreq@do
  }%
}

% @removeelementの括弧があっても動くかもしれない版
% #2から#1を消して#3に入れる
\newcommand*{\jlreq@helper@removeelement}[3]{%
  \begingroup
    \edef\jlreq@tempa{\unexpanded{#1}}% 消したいやつ
    \def\jlreq@tempb{}% 消していない分を放り込んでいく
    \jlreq@tempatrue% 最初の\jlreq@tempbへの追加か.
    \jlreq@helper@removeelement@#2,\jlreq@endmark,%
    \edef\jlreq@do{\unexpanded{\def#3}{\expandonce{\jlreq@tempb}}}%
  \expandafter\endgroup
  \jlreq@do
}
\def\jlreq@helper@removeelement@#1,{%
  \jlreq@ifendmark{#1}{}{%
    \edef\@tempa{\unexpanded{#1}}%
    \ifx\jlreq@tempa\@tempa\expandafter\@gobble\else\expandafter\@firstofone\fi{%
      \eappto\jlreq@tempb{\ifjlreq@tempa\else,\fi\unexpanded{#1}}%
      \jlreq@tempafalse
    }%
    \jlreq@helper@removeelement@
  }%
}

% 空白を無視しない\@ifnextchar
\newcommand{\jlreq@ifnextchar}[3]{%
  \let\reserved@d=#1%
  \edef\reserved@a{\unexpanded{#2}}%
  \edef\reserved@b{\unexpanded{#3}}%
  \futurelet\jlreq@nextchar\jlreq@ifnextchar@
}
\def\jlreq@ifnextchar@{%
  \ifx\jlreq@nextchar\reserved@d\let\reserved@b\reserved@a\fi
  \reserved@b
}
% 空白を無視しない\@ifsart
\newcommand*{\jlreq@ifstar}[2]{\jlreq@ifnextchar *{\edef\reserved@a*{\unexpanded{#1}}\reserved@a}{#2}}
% カンマで区切って,その前と後ろを\jlreq@resulta,\jlreq@resultbに入れる
% もしカンマがない場合は\ifjlreq@resultがfalseになる.
\newcommand*{\jlreq@helper@dividebycomma}[1]{\jlreq@helper@dividebycomma@#1,\jlreq@endmark}
\def\jlreq@helper@dividebycomma@#1,#2\jlreq@endmark{%
  \jlreq@helper@trim{#1}{\jlreq@resulta}%
  \jlreq@helper@trim{#2}{\jlreq@resultb}%
  \jlreq@if{\ifx\jlreq@resultb\@empty\fi}{%
    \jlreq@resultfalse
  }{%
    \jlreq@resulttrue
    \expandafter\def\expandafter\jlreq@resultb\expandafter{\jlreq@helper@removelastcomma#2\jlreq@endmark}%
    \expandafter\jlreq@helper@trim\expandafter{\jlreq@resultb}{\jlreq@resultb}%
  }%
}
\def\jlreq@helper@removelastcomma#1,\jlreq@endmark{#1}

% vbox #1の最後を\jlreq@resultboxに入れる
% #1の最後の行は消える
\newcommand*{\jlreq@getlastbox}[1]{%
  \setbox#1=\vbox{\unvbox#1\relax
    \unskip\unskip\unpenalty\global\setbox\jlreq@resultbox=\lastbox}%
  \ifhbox\jlreq@resultbox
    \global\setbox\jlreq@resultbox=\hbox{%
      \unhbox\jlreq@resultbox\unskip\unskip\unpenalty}%
  \else
    \global\setbox\jlreq@resultbox=\copy\voidb@x
  \fi
}

% 簡易なkeyvalのパース.
% \jlreq@parsekeyval*[family=jlreq@key]{ % *ありは\setkeys*を使う
%  {key}{\def\val{#1}}
%  {key1,key2}{\def\val{#1}} % key1=valとkey2=valを受け付けるようになる
%  *{key}{\def\val{#1}} % デフォルト値を空文字列にする
%  {key}[choice1,choice2]{\def\valchoice{#1}}
%}{parse対象}
\newcommand*{\jlreq@parsekeyval}{%
  \@ifstar{\jlreq@helper@parsekeyval@{*}}{\jlreq@helper@parsekeyval@{}}%
}
\def\jlreq@helper@parsekeyval@#1{%
  \@ifnextchar[{\jlreq@helper@parsekeyval@@{#1}}{\jlreq@helper@parsekeyval@@{#1}[jlreq@key]}%
}
\long\def\jlreq@helper@parsekeyval@@#1[#2]#3#4{%
  \begingroup
    \def\jlreq@do{}% \setkeysの結果処理がここに入れられる(よってループ内で利用不可)
    \jlreq@helper@parsekeyval@@@{#2}#3{\jlreq@endmark}%
    \setkeys#1{#2}{#4}%
  \expandafter\endgroup
  \jlreq@do
}
\def\jlreq@helper@parsekeyval@@@#1#2#{%
  \jlreq@iffirsttoken@ignorespaces{#2}{*}{%
    \jlreq@helper@parsekeyval@@@definekeywithdefault{#1}%
  }{%
    \jlreq@helper@parsekeyval@@@@{#1}%
  }%
}
\long\def\jlreq@helper@parsekeyval@@@@#1#2{%
  \jlreq@ifendmark{#2}{}{%
    \@ifnextchar[{\jlreq@helper@parsekeyval@@@choicekey{#1}{#2}}{\jlreq@helper@parsekeyval@@@definekey{#1}{#2}}%
  }%
}
\long\def\jlreq@helper@parsekeyval@@@choicekey#1#2[#3]#4{%
  \@for\@tempa:=#2\do{%
    \edef\jlreq@next{\unexpanded{\define@choicekey{#1}}{\expandonce{\@tempa}}}%
    \jlreq@next{#3}{\eappto\jlreq@do{\unexpanded{#4}}}%
  }%
  \jlreq@helper@parsekeyval@@@{#1}%
}
\long\def\jlreq@helper@parsekeyval@@@definekeywithdefault#1#2#3{%
  \@for\@tempa:=#2\do{%
    \edef\jlreq@next{\unexpanded{\define@key{#1}}{\expandonce{\@tempa}}}%
    \jlreq@next[]{\eappto\jlreq@do{\unexpanded{#3}}}%
  }%
  \jlreq@helper@parsekeyval@@@{#1}%
}
\long\def\jlreq@helper@parsekeyval@@@definekey#1#2#3{%
  \@for\@tempa:=#2\do{%
    \edef\jlreq@next{\unexpanded{\define@key{#1}}{\expandonce{\@tempa}}}%
    \jlreq@next{\eappto\jlreq@do{\unexpanded{#3}}}%
  }%
  \jlreq@helper@parsekeyval@@@{#1}%
}

% キー指定しないkeyval(独自実装)
% \jlreq@parsekeyval@nokey{<action>}{<parse対象>}
% action内では#1=key, #2=val
% \ifjlreq@parsekeyval@nokey@novalue{<true code>}{<false code>}でvalueがないか判定できる.
% *つきは<parse対象>を一回だけ展開する.
\def\jlreq@parsekeyval@nokey{\@ifstar{\jlreq@parsekeyval@nokey@{*}}{\jlreq@parsekeyval@nokey@{}}}
\newcommand{\jlreq@parsekeyval@nokey@}[3]{%
  \begingroup
    \long\def\jlreq@do{}%
    \long\def\jlreq@parsekeyval@nokey@execute##1##2{#2}%
    \jlreq@ifempty{#1}{%
      \jlreq@parsekeyval@nokey@i@wuniq\jlreq@uniqtoken#3,\jlreq@endmark,%
    }{%
      \expandafter\jlreq@parsekeyval@nokey@i@wuniq\expandafter\jlreq@uniqtoken#3,\jlreq@endmark,%
    }%
  \expandafter\endgroup
  \jlreq@do
}
\long\def\jlreq@parsekeyval@nokey@i@wuniq#1,{\expandafter\jlreq@parsekeyval@nokey@i\expandafter{\@gobble#1}}
\long\def\jlreq@parsekeyval@nokey@i#1{\jlreq@ifendmark{#1}{}{\jlreq@parsekeyval@nokey@ii@wuniq\jlreq@uniqtoken#1=\jlreq@endmark}}
\long\def\jlreq@parsekeyval@nokey@ii@wuniq#1=#2\jlreq@endmark{\expandafter\jlreq@parsekeyval@nokey@ii\expandafter{\@gobble#1}{#2}}%
\long\def\jlreq@parsekeyval@nokey@ii#1#2{%
  \jlreq@ifempty{#2}{\jlreq@parsekeyval@nokey@iii{*}{#2}{#1}}{%
    \edef\jlreq@tempa{%
      \noexpand\jlreq@parsekeyval@nokey@iii{}{\jlreq@parsekeyval@nokey@removeequal{#2}}{\unexpanded{#1}}%
    }%
    \jlreq@tempa
  }%
}
% valueが与えられていない場合は#1=*になる.そうでなければ#1は空
\long\def\jlreq@parsekeyval@nokey@iii#1#2#3{%
  \jlreq@remove@spaceandbracket{#2}{\jlreq@tempa}%
  \jlreq@remove@spaceandbracket{#3}{\jlreq@tempb}%
  \ifx\jlreq@tempb\@empty\else
    \edef\@tempa{\noexpand\jlreq@parsekeyval@nokey@iv{#1}{\expandonce{\jlreq@tempb}}{\expandonce{\jlreq@tempa}}}%
    \@tempa
  \fi
  \jlreq@parsekeyval@nokey@i@wuniq\jlreq@uniqtoken
}
\long\def\jlreq@parsekeyval@nokey@iv#1#2#3{%
  \eappto\jlreq@do{%
    \unexpanded{\let\ifjlreq@parsekeyval@nokey@novalue}\jlreq@ifempty{#1}{\noexpand\@secondoftwo}{\noexpand\@firstoftwo}%
    \expandonce{\jlreq@parsekeyval@nokey@execute{#2}{#3}}%
  }%
}
\long\def\jlreq@parsekeyval@nokey@removeequal#1{\jlreq@parsekeyval@nokey@removeequal@\jlreq@uniqtoken#1\jlreq@endmark}
\long\def\jlreq@parsekeyval@nokey@removeequal@#1=\jlreq@endmark{\expandafter\unexpanded\expandafter{\@gobble#1}}

% #1の先頭のスペースを削除し,#2に入れる.
\long\def\jlreq@helper@trim#1#2{\edef#2{\jlreq@helper@trim@i{#1}}}
\def\jlreq@do#1{% #1 = " "として後で実行
  \long\def\jlreq@helper@trim@i##1{%
    \jlreq@iffirsttoken{##1}{#1}%
      % 先頭スペースの除去.{}を外さないために\jlreq@uniqtokenを前後にいったん挟む.
      {\jlreq@helper@trim@ii##1\jlreq@uniqtoken\jlreq@endmark}%
      % 後方スペースの除去.先頭に\jlreq@uniqtokenを加えて{}が外れるのを防ぐ.
      {\jlreq@helper@trim@iii\jlreq@uniqtoken##1\jlreq@uniqtoken#1\jlreq@uniqtoken\jlreq@endmark}%
  }
  \long\def\jlreq@helper@trim@ii#1##1\jlreq@endmark{%
    \expandafter\expandafter\expandafter\jlreq@helper@trim@i\jlreq@helper@trim@vi\jlreq@uniqtoken##1\jlreq@endmark
  }
  \long\def\jlreq@helper@trim@iii##1#1\jlreq@uniqtoken##2\jlreq@endmark{%
    \jlreq@ifempty{##2}%
      {\expandafter\jlreq@helper@trim@iv\jlreq@helper@trim@v##1\jlreq@endmark}%
      {\jlreq@helper@trim@iii##1\jlreq@uniqtoken#1\jlreq@uniqtoken\jlreq@endmark}%
  }%
  \long\def\jlreq@helper@trim@iv##1{\expandafter\unexpanded\expandafter{\@gobble ##1}}
  \long\def\jlreq@helper@trim@v##1\jlreq@uniqtoken\jlreq@endmark{{##1}}
  \long\def\jlreq@helper@trim@vi##1\jlreq@uniqtoken\jlreq@endmark{\expandafter{\@gobble ##1}}
}
\jlreq@do{ }

\long\def\jlreq@remove@spaceandbracket#1#2{%
  \begingroup
    \jlreq@helper@trim{#1}{\jlreq@tempa}%
    \edef\jlreq@do{\unexpanded{\jlreq@remove@spaceandbracket@{#2}}\expandonce{\jlreq@tempa}\noexpand\jlreq@endmark}%
  \expandafter\endgroup
  \jlreq@do
}
\long\def\jlreq@remove@spaceandbracket@#1#2\jlreq@endmark{\edef#1{\unexpanded{#2}}}

% \jlreq@switch{str}{
%   {str}{abcd}
%   {str1}{xyz}
%}[default: input = #1] -> abcd
%または
%
% \jlreq@switch{str}{
%   {str}{abcd}
%   {str1}{xyz}
%   {default} % ここは#1を受け付けない(パラメータトークンを受け付けたい場合のため).
%} -> abcd
% strは\edefで展開されるが,\jlreq@switch*の場合は展開されない.
% 第一引数は\jlreq@tempaに入れておく.
\long\def\jlreq@helper@switch@executeoption#1[#2]{%
  \begingroup
    \def\jlreq@do##1{#2}%
  \expandafter\endgroup
  \jlreq@do{#1}%
}
\long\def\jlreq@helper@removeoption[#1]{}
\newcommand{\jlreq@switch}{\@ifstar{\jlreq@switch@{*}}{\jlreq@switch@{}}}
\newcommand{\jlreq@switch@}[3]{%
  \begingroup
    \let\jlreq@do\@undefined
    \edef\jlreq@tempa{\jlreq@ifempty{#1}{#2}{\unexpanded{#2}}}% ループ中ずっと保存する
    \jlreq@helper@switch@{#1}#3\jlreq@endmark
    \ifx\jlreq@do\@undefined
      \edef\jlreq@do{\unexpanded{\jlreq@ifnextchar[{\jlreq@helper@switch@executeoption{#2}}{}}}%
    \else
      \appto\jlreq@do{\@ifnextchar[{\jlreq@helper@removeoption}{}}%
    \fi
  \expandafter\endgroup
  \jlreq@do
}
\long\def\jlreq@helper@switch@#1#2{%
  \jlreq@ifendmark{#2}{}{\jlreq@helper@switch@@{#1}{#2}}%
}

\long\def\jlreq@helper@switch@@#1#2#3{%
  \jlreq@ifendmark{#3}{%
    \edef\jlreq@do{\unexpanded{#2}}%
  }{%
    \edef\jlreq@tempb{\jlreq@ifempty{#1}{#2}{\unexpanded{#2}}}%
    \jlreq@if{\ifx\jlreq@tempa\jlreq@tempb\fi}{%
      \edef\jlreq@do{\unexpanded{#3}}%
      \jlreq@deleteuntileendmark
    }{%
      \jlreq@helper@switch@{#1}%
    }%
  }%
}

% オレオレラベル……
% \jlreq@reference@ref{ラベル名}{デフォルト値}
\def\jlreq@reference@ref#1{\jlreq@if{\expandafter\unless\expandafter\ifx\csname jlreq@reference@#1\endcsname\relax\fi}{\csname jlreq@reference@#1\endcsname}}
% \jlreq@reference@label{ラベル名}{値}
\def\jlreq@reference@label#1#2{\protected@write\@auxout{}{\string\@newl@bel{jlreq@reference}{#1}{#2}}}%
\def\jlreq@reference@immediate@write#1#2{\immediate\write\@auxout{\string\@newl@bel{jlreq@reference}{#1}{#2}}}%
\def\jlreq@reference@ifexist#1{\jlreq@if{\expandafter\unless\expandafter\ifx\csname jlreq@reference@#1\endcsname\relax\fi}}

% 自動生成されるラベル用の番号
\newcount\jlreq@reference@count
\jlreq@reference@count=0

% 現在のページを得る
\newcount\jlreq@currentpage
\newcommand*{\jlreq@setcurrentpage}{%
  \global\advance\jlreq@reference@count by 1
  \jlreq@reference@label{jlreq@currentpage.\the\jlreq@reference@count}{\noexpand\arabic{page}}%
  \global\jlreq@currentpage=\jlreq@reference@ref{jlreq@currentpage.\the\jlreq@reference@count}{\the\c@page}\relax
}

% 先頭にあるマクロを展開しまくった結果の先頭を#2に入れる.
\newcommand{\jlreq@getfirsttoken@expandmacros}[2]{%
  \jlreq@iffirsttoken{#1}{ }{\def#2{ }}{%
    \jlreq@ifempty{#1}{\def#2{}}{%
      \jlreq@ifmacro@noargument{#1}{%
        \expandafter\jlreq@getfirsttoken@expandmacros\expandafter{#1}{#2}%
      }{%
        \expandafter\expandafter\expandafter\jlreq@getfirsttoken@expandmacros@\jlreq@getfirsttoken#1\jlreq@uniqtoken{}\jlreq@endmark@{#2}%
      }%
    }%
  }%
}
\def\jlreq@getfirsttoken@expandmacros@#1#2{\def#2{#1}}

\def\jlreq@helper@checktokenlist#1#2{%
  \jlreq@ifendmark{#2}{\@firstoftwo}%
  {%
    \jlreq@if{\ifnum0#1{#2}>0 \fi}%
      {\jlreq@helper@checktokenlist{#1}}%
      {\expandafter\@secondoftwo\jlreq@deleteuntileendmark}%
  }%
}
\def\jlreq@helper@chknumlist#1{\ifx#101\fi\ifx#111\fi\ifx#121\fi\ifx#131\fi\ifx#141\fi\ifx#151\fi\ifx#161\fi\ifx#171\fi\ifx#181\fi\ifx#191\fi}
\def\jlreq@helper@chkfloatlist#1{\jlreq@helper@chknumlist{#1}\ifx#1.1\fi}
% #1が[0-9\.]*に当てはまるか.
\newcommand*{\jlreq@iffloat}[1]{\jlreq@helper@checktokenlist{\jlreq@helper@chkfloatlist}#1\jlreq@endmark}
\newcommand*{\jlreq@ifnumber}[1]{\jlreq@helper@checktokenlist{\jlreq@helper@chknumlist}#1\jlreq@endmark}

% \jlreq@ifprimitive#1#2#3: #1がprimitiveならば#2を,そうでなければ#3を実行する
% (\if(pdf)primitiveを避けるようにしている.)
\newcommand*{\jlreq@ifprimitive}[1]{%
  \begingroup
    \edef\@tempa{\string#1}%
    \edef\@tempb{\meaning#1}%
  \expandafter\endgroup
  \ifx\@tempa\@tempb\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
}

\newcommand*{\jlreq@helper@guessengine}{%
  \jlreq@ifprimitive\directlua{%
    \let\jlreq@result=l%
  }{%
    \jlreq@ifprimitive\ucs{%
      \let\jlreq@result=u%
    }{%
      \let\jlreq@result=p%
    }%
  }%
}

% #1を\@undefinedに,既に\@undefinedならば#2{#1}を呼ぶ
\newcommand*{\jlreq@helper@undefcs}[2]{%
  \ifundef{#1}{#2{#1}}{\undef{#1}}%
}
% \if版
\newcommand*{\jlreq@helper@undefif}[2]{%
  \ifundef{#1}{#2{#1}}{%
    \edef\@tempa{\expandafter\@gobble\string#1}%
    \edef\@tempa{\expandafter\@gobbletwo\@tempa}%
    \undef{#1}%
    \expandafter\undef\expandafter{\csname\@tempa true\endcsname}%
    \expandafter\undef\expandafter{\csname\@tempa false\endcsname}%
  }%
}

% 紙サイズ一覧
\@namedef{jlreq@helper@papersizelist@a0}{{841mm}{1189mm}}
\@namedef{jlreq@helper@papersizelist@a1}{{594mm}{841mm}}
\@namedef{jlreq@helper@papersizelist@a2}{{420mm}{594mm}}
\@namedef{jlreq@helper@papersizelist@a3}{{297mm}{420mm}}
\@namedef{jlreq@helper@papersizelist@a4}{{210mm}{297mm}}
\@namedef{jlreq@helper@papersizelist@a5}{{148mm}{210mm}}
\@namedef{jlreq@helper@papersizelist@a6}{{105mm}{148mm}}
\@namedef{jlreq@helper@papersizelist@a7}{{74mm}{105mm}}
\@namedef{jlreq@helper@papersizelist@a8}{{52mm}{74mm}}
\@namedef{jlreq@helper@papersizelist@a9}{{37mm}{52mm}}
\@namedef{jlreq@helper@papersizelist@a10}{{26mm}{37mm}}

\@namedef{jlreq@helper@papersizelist@b0}{{1030mm}{1456mm}}
\@namedef{jlreq@helper@papersizelist@b1}{{728mm}{1030mm}}
\@namedef{jlreq@helper@papersizelist@b2}{{515mm}{728mm}}
\@namedef{jlreq@helper@papersizelist@b3}{{364mm}{515mm}}
\@namedef{jlreq@helper@papersizelist@b4}{{257mm}{364mm}}
\@namedef{jlreq@helper@papersizelist@b5}{{182mm}{257mm}}
\@namedef{jlreq@helper@papersizelist@b6}{{128mm}{182mm}}
\@namedef{jlreq@helper@papersizelist@b7}{{91mm}{128mm}}
\@namedef{jlreq@helper@papersizelist@b8}{{64mm}{91mm}}
\@namedef{jlreq@helper@papersizelist@b9}{{45mm}{64mm}}
\@namedef{jlreq@helper@papersizelist@b10}{{32mm}{45mm}}

\@namedef{jlreq@helper@papersizelist@c2}{{458mm}{648mm}}
\@namedef{jlreq@helper@papersizelist@c3}{{324mm}{458mm}}
\@namedef{jlreq@helper@papersizelist@c4}{{229mm}{354mm}}
\@namedef{jlreq@helper@papersizelist@c5}{{162mm}{229mm}}
\@namedef{jlreq@helper@papersizelist@c6}{{114mm}{162mm}}
\@namedef{jlreq@helper@papersizelist@c7}{{81mm}{114mm}}
\@namedef{jlreq@helper@papersizelist@c8}{{57mm}{81mm}}

\@namedef{jlreq@helper@papersizelist@a4var}{{210mm}{283mm}}
\@namedef{jlreq@helper@papersizelist@b5var}{{182mm}{230mm}}

\@namedef{jlreq@helper@papersizelist@letter}{{8.5in}{11in}}
\@namedef{jlreq@helper@papersizelist@legal}{{8.5in}{14in}}
\@namedef{jlreq@helper@papersizelist@executive}{{7.25in}{10.5in}}
\@namedef{jlreq@helper@papersizelist@hagaki}{{100mm}{148mm}}

% #1÷#2を\jlreq@resultdimenに入れる.(#1,#2は寸法を与える.)
\newcommand*{\jlreq@helper@divide}[2]{%
  \begingroup
    \@tempdima=\dimexpr#1\relax\@tempdimb=\dimexpr#2\relax
    % 分母を正にする
    \ifdim\@tempdimb<0pt \@tempdima=-\@tempdima\@tempdimb=-\@tempdimb\fi
    % \jlreq@tempdima*\@tempdimb <= \@tempdima < (\jlreq@tempdima + 1)*\@tempdimb
    % となる整数\jlreq@tempdimaを捜し,\jlreq@tempdimb=\jlreq@tempdima + 1とする
    \jlreq@tempdima=0pt
    \ifdim\@tempdima<0pt
      \loop\ifdim\strip@pt\jlreq@tempdima\@tempdimb>\@tempdima\advance\jlreq@tempdima by -1pt\repeat
    \else
      \loop\ifdim\strip@pt\jlreq@tempdima\@tempdimb<\@tempdima\advance\jlreq@tempdima by 1pt\repeat
      \ifdim\strip@pt\jlreq@tempdima\@tempdimb=\@tempdima\else\advance\jlreq@tempdima by -1pt\fi
    \fi
    \jlreq@tempdimb=\dimexpr\jlreq@tempdima + 1pt\relax
    % \jlreq@tempdimaと\jlreq@tempdimbで挟んでいく二分法
    \jlreq@tempcnta=0 % 無限ループを避けるためのカウンタ
    \loop\ifnum\numexpr\ifdim\dimexpr\jlreq@tempdimb - \jlreq@tempdima\relax>1sp 1\else0\fi*\ifnum\jlreq@tempcnta<100 1\else 0\fi\relax=1
      \jlreq@tempdimc=\dimexpr(\jlreq@tempdima+\jlreq@tempdimb)/2\relax
      \ifdim\strip@pt\jlreq@tempdimc\@tempdimb>\@tempdima\jlreq@tempdimb=\jlreq@tempdimc
      \else\jlreq@tempdima=\jlreq@tempdimc\fi
      \advance\jlreq@tempcnta by 1
    \repeat
    \edef\jlreq@do{\noexpand\jlreq@resultdimen=\the\jlreq@tempdima\noexpand\relax}%
  \expandafter\endgroup
  \jlreq@do
}

\def\jlreq@for@noexpand#1:={\@for#1:=\@empty}

\def\jlreq@helper@swap#1#2{%
  \let\jlreq@helper@swaptoken=#1%
  \let#1=#2%
  \let#2=\jlreq@helper@swaptoken
  \let\jlreq@helper@swaptoken=\@undefined
}
\def\jlreq@helper@swap@register#1#2{%
  \edef\jlreq@helper@swaptoken{\the#1}%
  #1=#2%
  #2=\jlreq@helper@swaptoken\relax
  \let\jlreq@helper@swaptoken=\@undefined
}

\endinput