-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathgmutils.sty
More file actions
4318 lines (3808 loc) · 143 KB
/
Copy pathgmutils.sty
File metadata and controls
4318 lines (3808 loc) · 143 KB
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%
% \GetFileInfo{gmutils.sty}
% \title{The \pk{gmutils} Package\thfileinfo}
% \author{Grzegorz Murzynowski}
% \maketitle
%
% \let\ty\emph
%
% \begin{copyrnote}
%
%%Written by Grzegorz Murzynowski,
%% natror at o2 dot pl
%%
%% \copyright\,2005–2008 by Grzegorz Murzynowski.
%%
%% This program is subject to the \LaTeX\ Project Public License.
%% See ^^A
%% \url{http://www.ctan.org/tex-archive/help/Catalogue/licenses.lppl.html} ^^A
%% for the details of that license.
%%
%% LPPL status: "author-maintained".\par
%%
%% Many thanks to my \TeX\ Guru Marcin Woliński for his \TeX nical ^^A
%% support.
%
%\end{copyrnote}
%%
%%
% \RecordChanges
% \ChangesStart{}{1000/00/0}
% \chschange{v0.59}{06/09/04}{396}
% \chschange{v0.61}{06/09/06}{432}
% \chschange{v0.62}{06/9/7}{444}
% \chschange{v0.65}{06/9/28}{700}
% \chschange{v0.66}{06/10/5}{698}
% \chschange{v0.67}{06/10/11}{739}
% \chschange{v0.68}{06/10/12}{812}
% \chschange{v0.69}{06/10/17}{844}
% \chschange{v0.70}{06/10/20}{852}
% \chschange{v0.71}{06/10/27}{876}
% \chschange{v0.72}{06/11/14}{906}
% \chschange{v0.74}{06/12/1}{1185}
% \chschange{v0.76}{07/4/9}{1538}
% \chschange{v0.77}{07/4/14}{1547}
% \chschange{v0.78}{07/4/26}{1685}
% \chschange{v0.80}{2007/4/29}{1689}
% \chschange{v0.84}{2007/11/13}{2684}
% \chschange{v0.85}{2007/11/16}{2795}
% \chschange{v0.87}{2008/8/3}{4027}
% \chschange{v0.88}{2008/8/3}{4040}
% \chschange{v0.90}{2008/8/4}{4035}
% \chschange{v0.91}{2008/8/6}{4055}
% \chschange{v0.92}{2008/8/11}{4133}
% \chschange{v0.93}{2008/8/21}{4140}
% \chschange{v0.93}{2008/8/30}{4501}
% \chschange{v0.94}{2008/10/4}{4880}
% \chschange{v0.95}{2008/10/8}{4908}
% \chschange{v0.96}{2008/11/21}{5363}
% \toCTAN{v0.96}{2008/11/21}
% \chschange{v0.97}{2008/11/22}{5375}
% \toCTAN{v0.97}{2008/11/22}
%
%
% \skiplines we skip the driver
\ifnum\catcode`\@=12
\documentclass[debug, countalllines, codespacesgrey, outeroff,
pagella, fontspec=quiet]{gmdocc}
\usepackage{eufrak}% for |\continuum| in the commentary.
\twocoltoc
\begin{document}
\DocInput{\jobname.sty}
\PrintChanges
\thispagestyle{empty}
\typeout{%
Produce change log with^^J%
makeindex -r -s gmglo.ist -o \jobname.gls \jobname.glo^^J
(gmglo.ist should be put into some texmf/makeindex directory.)^^J}
\typeout{%
Produce index with^^J%
makeindex -r \jobname^^J}
\afterfi{\end{document}}
\fi% of driver pass
%\endskiplines
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{gmutils}
[2008/11/22 v0.97 some rather TeXnical macros, some of them tricky (GM)]
%
% \tableofcontents
%
% \division{Intro}
%
% The \pk{gmutils.sty} package provides some macros that are
% analogous to the standard \LaTeX\ ones but extend their
% functionality, such as |\@ifnextcat|, |\addtomacro| or |\begin(*)|.
% The others are just conveniences I~like to use in all my TeX
% works, such as |\afterfi|, |\pk| or |\cs|.
%
% I wouldn't say they are only for the package writers but I~assume
% some nonzero \LaTeXpar-awareness of the user.
%
% For details just read the code part.
%
% \begin{gmlonely}
% \subdivision{Installation} Unpack the \file{gmutils-tds.zip}
% archive (this is an archive that conforms the \acro{TDS} standard,
% see \file{CTAN/tds/tds.pdf}) in some texmf directory or just put
% the \pk{gmutils.sty} somewhere in the \file{texmf/\:tex/\:latex}
% branch. Creating a~\file{texmf/\:tex/\:latex/\:gm} directory may
% be advisable if you consider using other packages written by me.
%
% Then you should refresh your \TeX\ distribution's files' database
% most probably.
% \end{gmlonely}
%
% \subdivision{Contents of the \pk{gmutils.zip} archive}
%
% The distribution of the \pk{gmutils} package consists of the
% following three files and a~\acro{TDS}-compliant archive.
% \begin{verse}
% \pk{gmutils.sty}\\
% \pk{README}\\
% \pk{gmutils.pdf}\\
% \pk{gmutils.tds.zip}
% \end{verse}
%
% \begin{gmlonely}
% \subdivision{Compiling of the documentation}
%
% The last of the above files (the \pk{.pdf}, i.e., \emph{this
% file}) is a~documentation compiled from the \pk{.sty} file by
% running \LaTeX\ on the \pk{gmutils.sty} file twice
% (|xelatex gmutils.sty| in the directory you wish the
% documentation to be in, you don't have copy the \file{.sty} file
% there, \TeX\ will find it),
% then MakeIndex on the \file{gmutils.idx} file, and then \LaTeX\
% on \file{gmutils.sty} once more.
%
% MakeIndex shell command:
% \[\hbox{|makeindex -r gmutilsDoc|}\]
% The |-r| switch is to forbid MakeIndex to make implicit ranges since
% the (code line) numbers will be hyperlinks.
%
% Compiling the documentation requires the packages:
% \pk{gmdoc} (\pk{gmdoc.sty} and \pk{gmdocc.cls}), \pk{gmverb.sty},
% \pk{gmutils.sty}, \pk{gmiflink.sty} and also some standard packages:
% \pk{hyperref.sty}, \pk{color.sty}, \pk{geometry.sty},
% \pk{multicol.sty}, \pk{lmodern.sty}, \pk{fontenc.sty} that should
% be installed on your computer by default.
%
% If you had not installed the \pk{mwcls} classes (available on
% CTAN and present in \TeX\ Live e.g.), the result of your
% compilation might differ a~bit from the \pk{.pdf} provided in this
% \pk{.zip} archive in formatting: If you had not installed
% \pk{mwcls}, the standard \pk{article.cls} class would be used.
% \end{gmlonely}
%
\ifx\XeTeXversion\relax
\let\XeTeXversion\@undefined% If someone earlier used
% \inverb|\@ifundefined{XeTeXversion}| to test
% whether the engine is \XeTeX, then \incs{XeTeXversion} \emph{is} defined
% in the sense of \eTeX\ tests. In that case we |\let| it to something
% really undefined. Well, we might keep sticking to \incs{@ifundefined},
% but it's a~macro and it eats its arguments, freezing their catcodes,
% which is not what we want in line \ref{not@ifundef}.
\fi
\ifdefined\XeTeXversion
\XeTeXinputencoding utf-8 % we use Unicode dashes later in this file.
\fi% and if we are not in \XeTeX, we skip them thanks to \XeTeX-test.
%
% \division{A~couple of abbreviations}
% {\DoIndex{\@nx \@xa} \Define\@xa \Define\@nx
\let\@xa\expandafter
\let\@nx\noexpand
\def\@xau{\@xa\unexpanded\@xa}
%\changes{v0.94}{2008/10/4}{added}
%} \HideDefining*\pdef
\def\pdef{\protected\def}
%\changes{v0.93}{2008/08/20}{added}
% And this one is defined, I~know, but it's not |\long| with the
% standard definition and I~want to be able to |\gobble| a~|\par|
% sometimes.
% \Define\gobbletwo
\long\def\gobble#1{}
% \Define\@gobble
\let\@gobble\gobble
\let\gobbletwo\@gobbletwo
% \HideDefining*\provide
\long\pdef\provide#1{%\changes{v0.93}{2008/08/30}{added}
\ifdefined#1% \UnDef
\ifx\relax#1\afterfifi{\def#1}%
\else\afterfifi{\gmu@gobdef}% \UnDef
\fi
\else\afterfi{\def#1}%
\fi}
\long\def\gmu@gobdef#1#{% \UnDef
\def\gmu@tempa{}% it's a~junk macro assignment
% to absorb possible prefixes.
\@gobble}
% \HideDefining*\pprovide
\def\pprovide{\protected\provide}
% \changes[\pprovide]{v0.93}{2008/08/30}{added} Note that both
% |\provide| and |\pprovide| may be prefixed with |\global|, |\outer|,
% |\long| and |\protected| because the prefixes stick to |\def|
% because all before it is expandable. If the condition(s) is false
% (|#1| is defined) then the prefixes are absorbed by a~junk
% assignment.
%
% Note moreover that unlike \LaTeX's |\providecommand|, our
% \cs{(p)provide} allow any parameters string just like |\def|
% (because they just \emph{expand} to |\def|).
%
\long\def\@nameedef#1#2{% \changes{v0.93}{2008/08/30}{added}
\@xa\edef\csname#1\endcsname{#2}}
% \division{\cs{firstofone} and the queer \cs{catcode}s}
%
% Remember that once a~macro's argument has been read, its
% |\catcode|s are assigned forever and ever. That's what is
% |\firstofone| for. It allows you to change the |\catcode|s locally
% for a~definition \emph{outside} the changed |\catcode|s' group.
% Just see the below usage of this macro `with \TeX's eyes', as my
% \TeX\ Guru taught me.
%
\long\def\firstofone#1{#1}
% The next command, |\foone|, is intended as two-argument for
% shortening of the |\begingroup...\firstofone{\endgroup...}| hack.
%
\long\def\foone#1{\begingroup#1\egfirstofone}
% \UnDef
\long\def\egfirstofone#1{\endgroup#1}
\long\def\fooatletter{\foone\makeatletter}
% \division{Global Boolean switches}
%
% The |\newgif| declaration's effect is used even in the \LaTeXe\
% source by redefining some particular user defined ifs (UD-ifs
% henceforth) step by step. The goal is to make the UD-if's
% assignment global. I~needed it at least twice during \pk{gmdoc}
% writing so I~make it a~macro. It's an almost verbatim copy of
% \LaTeX's |\newif| modulo the letter $g$ and the |\global| prefix.
% (File d: \file{ltdefns.dtx} Date: 2004/02/20 Version v1.3g, lines
% 139--150) \Define\newgif
\pdef\newgif#1{%
{\escapechar\m@ne
\global\let#1\iffalse
\@gif#1\iftrue
\@gif#1\iffalse
}}
% `Almost' is also in the detail that in this case, which deals with
% |\global| assignments, we don't have to bother with storing and
% restoring the value of |\escapechar|: we can do all the work inside
% a~group.
% \DefIndex\@gif
\def\@gif#1#2{%
\protected\@xa\gdef\csname\@xa\@gobbletwo\string#1%
g% the letter $g$ for `|\global|'.
\@xa\@gobbletwo\string#2\endcsname
{\global\let#1#2}}
\pdef\newif#1{% We not only make \incs{newif}
% \incs{protected} but also make it to define \cs{protected}
% assignments so that premature expansion doesn't affect
% \inverb|\if…\fi| nesting.
% \changes{v0.92}{2008/08/09}{added redefinition so that now
% switches defined with it are \cs{protected} so they won't expand to
% a~further expanding or unbalanced \cs{iftrue/false} in an \cs{edef}}
\count@\escapechar \escapechar\m@ne
\let#1\iffalse
\@if#1\iftrue
\@if#1\iffalse
\escapechar\count@}
\def\@if#1#2{%
\protected \@xa\def\csname\@xa\@gobbletwo\string#1%
\@xa\@gobbletwo\string#2\endcsname
{\let#1#2}}
\pdef\hidden@iffalse{\iffalse}
\pdef\hidden@iftrue{\iftrue}
% After |\newgif\iffoo| you may type |{\foogtrue}| and the |\iffoo|
% switch becomes globally equal |\iftrue|. Simili modo |\foogfalse|.
% Note the letter $g$ added to underline globalness of the assignment.
%
% If for any reason, no matter how queer ;-) may it be, you need
% \emph{both} global and local switchers of your |\if...|, declare
% it both with |\newif| and |\newgif|.
%
% Note that it's just a~shorthand. |\global\if|\<switch>|true/false|
% \emph{does} work as expected.
% \Define\grefstepcounter
% \changes{v0.59}{06/09/04}{moved here from \pk{gmdoc}.}
% There's a~trouble with |\refstepcounter|: defining |\@currentlabel|
% is local. So let's |\def| a~|\global| version of |\refstepcounter|.
%
% Warning. I~use it because of very special reasons in \pk{gmdoc} and
% in general it is probably not a~good idea to make |\refstepcounter|
% global since it is contrary to the original \LaTeX\ approach.
\pdef\grefstepcounter#1{%
{\let\protected@edef=\protected@xdef\refstepcounter{#1}}}
% Na\"\i{}ve first try |\globaldefs=\tw@| raised an error
% |unknown command \reserved@e|. The matter was to globalize
% |\protected@edef| of |\@currentlabel|.
%
% Thanks to using the true |\refstepcounter| inside, it observes the
% change made to |\refstepcounter| by \pk{hyperref}.
% \dekmedskip
%
% 2008/08/10 I~spent all the night debugging \cs{penalty 10000} that
% was added after a~hypertarget in vertical mode. I~didn't dare to
% touch \pk{hyperref}'s guts, so I~worked it around with ensuring
% every \cs{grefstepcounter} to be in hmode:
\pdef\hgrefstepcounter#1{%
\ifhmode\leavevmode\fi\grefstepcounter{#1}}
% By the way I~read some lines from \TB\ and was reminded that
% \cs{unskip} strips any last skip, whether horizontal or
% vertical. And I~use \cs{unskip} mostly to replace a~blank space with
% some fixed skip. Therefore define
\pdef\hunskip{\ifhmode\unskip\fi}
% Note the two macros defined above are \cs{protected}. I~think it's
% a~good idea to make \cs{protected} all the macros that contain
% assignments. There is one more thing with \cs{ifhmode}: it can be different
% at the point of \cs{edef} and at the point of execution.
% Another shorthand. It may decrease a~number of |\expandafter|s e.g.
% \Define\glet
\def\glet{\global\let}
% \LaTeX\ provides a~very useful |\g@addto@macro| macro that adds its
% second argument to the current definition of its first argument
% (works iff the first argument is a~no argument macro). But I~needed
% it some times in a~document, where |@| is not a~letter. So:
% \Define\gaddtomacro
\let\gaddtomacro=\g@addto@macro
% The redefining of the first argument of the above macro(s) is
% |\global|. What if we want it local? Here we are:
% \Define\addto@macro
\long\def\addto@macro#1#2{%
\toks@\@xa{#1#2}%
\edef#1{\the\toks@}%
}%(|\toks@| is a~scratch register, namely |\toks0|.)
% And for use in the very document,
% \Define\addtomacro
\let\addtomacro=\addto@macro
% 2008/08/09 I~need to prepend something not add at the end---so
\long\def\prependtomacro#1#2{% \changes{v0.94}{2008/09/15}{order of
% arguments reversed}
\edef#1{\unexpanded{#2}\@xa\unexpanded\@xa{#1}}}
% Note that \cs{prependtomacro} can be prefixed.
% \Define\addtotoks
\long\def\addtotoks#1#2{%
#1=\@xa{\the#1#2}}
% \Define\emptify
\newcommand*\@emptify[1]{\let#1=\@empty}
\@ifdefinable\emptify{\let\emptify\@emptify}
% Note the two following commands are in fact one-argument.
%\Define\g@emptify \Define\gemptify
\newcommand*\g@emptify{\global\@emptify}
\@ifdefinable\gemptify{\let\gemptify\g@emptify}
% \Define\relaxen
\newcommand\@relaxen[1]{\let#1=\relax}
\@ifdefinable\relaxen{\let\relaxen\@relaxen}
% Note the two following commands are in fact one-argument.
% \Define\g@relaxen \Define\grelaxen
\newcommand*\g@relaxen{\global\@relaxen}
\@ifdefinable\grelaxen{\let\grelaxen\g@relaxen}
% \division{\cs{gm@ifundefined}---a~test that doesn't create any\
% hash entry unlike \cs{@ifundefined}}
% I~define it under another name not redefine |\@ifundefined| because
% I~can imagine an odd case when something works thanks to
% |\@ifundefined|'s `relaxation effect'.
\long\def\gm@ifundefined#1{% not |\protected| because expandable.
% \changes{v0.94}{2008/9/12}{added. All \cs{@ifundefined}s used by
% me changed to this}
% \changes{v0.94}{2008/10/03}{made robust to unbalanced \cs{if}s and
% \cs{fi}s the same way as \LaTeX's \cs{@ifundefined} (after a~heavy
% debug :-)}
\ifcsname#1\endcsname% defined
\@xa\ifx\csname #1\endcsname\relax% but as |\relax|
\afterfifi\@firstoftwo%
\else% defined and not |\relax|
\afterfifi\@secondoftwo%
\fi
\else% not defined
\afterfi\@firstoftwo%
\fi}
\long\def\gm@testdefined#1\iftrue{% This is a~macro that expands to
% \incs{iftrue} or \incs{iffalse} depending on whether it's argument
% is defined in the \LaTeX\ sense. Its syntax requires an
% \incs{iftrue} to balance \incs{if}s with \incs{fi}s.
% \changes{v0.95}{2008/10/8}{added}
\csname
\ifdefined#1%
\ifx#1\relax
iffalse%
\else iftrue%
\fi
\else iffalse%
\fi\endcsname
}
\long\def\gm@testundefined#1\iftrue{% we expand the last macro two
% levels. We repeat the parameter string to force the proper syntax.
% \changes{v0.95}{2008/10/8}{added}
\@xa\@xa\@xa\unless\gm@testdefined#1\iftrue}
% \subdivision{Storing and restoring the catcodes of specials}
\newcommand*\gmu@storespecials[1][]{% we provide a~possibility of
% adding stuff. For usage see line \ref{@storespecials}.
\def\do##1{\catcode`\@nx##1=\the\catcode`##1\relax}%
\edef\gmu@restorespecials{\dospecials\do\^^M#1}}
\pdef\gmu@septify{% restoring the standard catcodes of specials. The
% name is the opposite of `sanitize' ^^A(
% :-)\,. It restores also the original catcode of \inverb|^^M|
\def\do{\relax\catcode`}%
\do\ 10\do\\0\do\{1\do\}2\do\$3\do\&4%
\do\#6\do\^7\do\_8\do\%14\do\~13\do\^^M5\relax}
%\division{From the ancient \pk{xparse} of \TeX Live 2007}
% The code of this section is rewritten contents of the \pk{xparse}
% package version 0.17 dated 1999/09/10, the version available in \TeX
% Live 2007-13, in Ubuntu packages at least. It's a~stub `im Erwartung'
% (Schönberg) for the \LaTeX 3 bundle and it does what I~want, namely
% defines |\DeclareDocumentCommand|. I~rewrote the code to use the usual
% catcodes (only with |@| a~letter) and not to use the \pk{ldcsetup}
% package (which caused an error of
% undefined \CS |\KV@def|).
%
% Well, I~add the |\protected| prefix to the first macro.
%
% After exchange of some mails with Morten H\o{}gholm and trying
% \pk{xparse} of 2008/08/03 svn 748 (which beautifully spoils the
% catcodes) I~wrap the
% ancient code in a~conditional to avoid name collision if someone
% loads \pk{xparse} before \pk{gmutils}
\gm@testundefined\DeclareDocumentCommand\iftrue
\unless\ifdefined\@temptokenb
\newtoks\@temptokenb
\fi
\newtoks\xparsed@args
\long\def\DeclareDocumentCommand#1#2#3{%\
% \begin{enumargs}
% \item command to be defined,
% \item arguments specification,
% \item definition body.
% \end{enumargs}
\@tempcnta\z@
\toks@{}%
\@temptokena\toks@
\@temptokenb\toks@
\@ddc#2X% |X| is the guardian of parsing.
\protected\edef#1{% The |\protected| prefix is my (GM) addition.
\@nx\@ddc@
{\the\toks@}%
\@xa\@nx\csname\string#1\endcsname
\@nx#1%
}%
\long\@xa\def\csname\string#1\@xa\endcsname
\the\@temptokena{#3}}
\long\def\DeclareDocumentEnvironment#1#2#3#4{%
\@xa\DeclareDocumentCommand\csname#1\endcsname{#2}{%
\xparsed@args\toks@
#3}%
\@xa\let\csname end#1\endcsname\@parsed@endenv
\long\@xa\def\csname end\string\\#1\@xa\endcsname
\the\@temptokena{#4}}
\def\@parsed@endenv{%
\@xa\@parsed@endenv@\the\xparsed@args}
\def\@parsed@endenv@#1{%
\csname end\string#1\endcsname}
\def\@ddc@#1#2#3{%
\ifx\protect\@typeset@protect
\@xa\@firstofone
\else
\protect#3\@xa\@gobble
\fi
{\toks@{#2}#1\the\toks@}}
\def\@ddc#1{%
\ifx#1X%
\else
\ifx#1m%
\addto@hook\@temptokenb m%
\else
\toks@\@xa{%
\the\@xa\toks@
\csname @ddc@\the\@temptokenb\@xa\endcsname
\csname @ddc@#1\endcsname}%
\@temptokenb{}%
\fi
\advance\@tempcnta\@ne
\@temptokena\@xa{%
\the\@xa\@temptokena\@xa##\the\@tempcnta}%
\@xa
\@ddc
\fi}
\long\def\@ddc@s#1\toks@{%
\@ifstar
{\addto@hook\toks@\BooleanTrue#1\toks@}%
{\addto@hook\toks@\BooleanFalse#1\toks@}}
\long\def\@ddc@m#1\toks@#2{%
\addto@hook\toks@{{#2}}#1\toks@}%
\long\def\@ddc@o#1\toks@{%
\@ifnextchar[%^^A]
{\@ddc@o@{#1}}
{\addto@hook\toks@\NoValue#1\toks@}}
\long\def\@ddc@o@#1[#2]{%
\addto@hook\toks@{{#2}}#1\toks@}
\def\@ddc#1{%
\ifx#1X%
\perhaps@grab@ms
\else
\ifx#1m%
\addto@hook\@temptokenb m%
\else
\toks@\@xa{%
\the\@xa\toks@
\csname @ddc@x\the\@temptokenb\@xa\endcsname
\csname @ddc@#1\endcsname}%
\@temptokenb{}%
\ifx#1O%
\let\next@ddc\grab@default
\else
\ifx#1C%
\let\next@ddc\grab@default
\fi
\fi
\fi
\advance\@tempcnta\@ne
\@temptokena\@xa{%
\the\@xa\@temptokena\@xa##\the\@tempcnta}%
\@xa
\next@ddc
\fi
}%
\let\next@ddc\@ddc
\def\grab@default#1{%
\toks@\@xa{%
\the\toks@
{#1}}%
\let\next@ddc\@ddc
\@ddc
}
\long\def\@ddc@O#1#2\toks@{%
\@ifnextchar[%^^A]
{\@ddc@o@{#2}}%
{\addto@hook\toks@{{#1}}#2\toks@}}
\long\def\@ddc@c#1\toks@{%
\@ifnextchar(%^^A)
{\@ddc@c@#1}%
{\PackageError{gmutils/xparse}{Missing~coordinate~argument}%
{A~value~of~(0,0)~is~assumed}%
\addto@hook\toks@{{00}}#1\toks@}%
}
\long\def\@ddc@c@#1(#2,#3){%
\addto@hook\toks@{{{#2}{#3}}}#1\toks@}
\long\def\@ddc@C#1#2\toks@{%
\@ifnextchar(%^^A)
{\@ddc@c@#2}%
{\addto@hook\toks@{{#1}}#2\toks@}}
\let\perhaps@grab@ms\relax
\def\grab@ms{%
\toks@\@xa{%
\the\@xa\toks@
\csname @ddc@x\the\@temptokenb\endcsname
}}
\let\@ddc@m\undefined
\long\def\@ddc@xm#1\toks@#2{%
\addto@hook\toks@{{#2}}#1\toks@}
\long\def\@ddc@xmm#1\toks@#2#3{%
\addto@hook\toks@{{#2}{#3}}#1\toks@}
\long\def\@ddc@xmmm#1\toks@#2#3#4{%
\addto@hook\toks@{{#2}{#3}{#4}}#1\toks@}
\long\def\@ddc@xmmmm#1\toks@#2#3#4#5{%
\addto@hook\toks@{{#2}{#3}{#4}{#5}}#1\toks@}
\long\def\@ddc@xmmmmm#1\toks@#2#3#4#5#6{%
\addto@hook\toks@{{#2}{#3}{#4}{#5}{#6}}#1\toks@}
\long\def\@ddc@xmmmmmm#1\toks@#2#3#4#5#6#7{%
\addto@hook\toks@{{#2}{#3}{#4}{#5}{#6}{#7}}#1\toks@}
\long\def\@ddc@xmmmmmmm#1\toks@#2#3#4#5#6#7#8{%
\addto@hook\toks@{{#2}{#3}{#4}{#5}{#6}{#7}{#8}}#1\toks@}
\long\def\@ddc@xmmmmmmmm#1\toks@#2#3#4#5#6#7#8#9{%
\addto@hook\toks@{{#2}{#3}{#4}{#5}{#6}{#7}{#8}{#9}}#1\toks@}
\long\def\@ddc@xmmmmmmmmm\the\toks@#1#2#3#4#5#6#7#8#9{%
\addto@hook\toks@{{#1}{#2}{#3}{#4}{#5}{#6}{#7}{#8}{#9}}\the\toks@}
\let\@ddc@x\relax
\long\def\DeclareDocumentEnvironment#1#2#3#4{%
\@xa\DeclareDocumentCommand\csname#1\endcsname{#2}{%
#3}%
\@namedef{end#1}{#4}%
}
\let\@parsed@endenv\undefined
\let\@parsed@endenv@\undefined
\def\IfSomethingTF#1{\def\something@in{#1}\If@SomethingTF}
\def\IfSomethingT#1#2#3{\def\something@in{#1}%
\If@SomethingTF{#2}{#3}\@empty}
\def\IfSomethingF#1#2#3{\def\something@in{#1}%
\If@SomethingTF{#2}\@empty{#3}}
\def\If@SomethingTF#1{%
\def\something@tmp{#1}%
\ifx\something@tmp\something@in
\@xa\@secondofthree
\else
\@xa\def\@xa\something@tmpb\@xa{#1}%
\ifx\something@tmp\something@tmpb
\@xa\@xa\@xa\@thirdofthree
\else
\@xa\@xa\@xa\@firstofone
\fi
\fi
{\@xa\If@SomethingTF\@xa{#1}}%
}
\long\def\@secondofthree#1#2#3{#2}
\long\def\@thirdofthree#1#2#3{#3}
\def\NoValue{-NoValue-}
\def\NoValueInIt{\NoValue}
\def\IfNoValueTF{\IfSomethingTF\NoValue}
\def\IfNoValueT{\IfSomethingT\NoValue}
\def\IfNoValueF{\IfSomethingF\NoValue}
\def\IfValueTF#1#2#3{\IfNoValueTF{#1}{#3}{#2}}
\let\IfValueT\IfNoValueF
\let\IfValueF\IfNoValueT
\def\BooleanFalse{TF}
\def\BooleanTrue{TT}
\def\IfBooleanTF#1{%
\if#1%
\@xa\@firstoftwo
\else
\@xa\@secondoftwo
\fi
}
\def\IfBooleanT#1#2{%
\IfBooleanTF{#1}{#2}\@empty
}
\def\IfBooleanF#1{%
\IfBooleanTF{#1}\@empty
}
\fi% of |\unless\ifdefined\DeclareDocumentCommand|.
% \division{Ampulex Compressa-like modifications of macros}
% Ampulex Compressa is a~wasp that performs brain surgery on its
% victim cockroach to lead it to its lair and keep alive for its
% larva. Well, all we do here with the internal \LaTeX\ macros
% resembles Ampulex's actions but here is a~tool for a~replacement of
% part of macro's definition.
%
% The \cs{ampulexdef} command takes its |#2| which has to be a~macro
% and replaces part of its definition delimited with |#5| and |#6|
% with the replacement |#7|. The redefinition may be prefixed with
% |#1|. |#2| may have parameters and for such a~macro you have to set
% the parameters string and arguments string (the stuff to be taken by
% the one-step expansion of the macro) as the optional |[#3]| and
% |[#4]|. . If \cs{ampulexdef} doesn't find the start and end tokens
% in the meaning of the macro, it does nothing to it. You have to
% write |####| instead of |#| or you can use |\ampulexhash| as
% well. For an example use see line \ref{ampulexset}.
%^^A \showthe\catcode`:
%^^A \showthe\catcode`_
%^^A
%^^A \gmu@storespecials
%^^A
%^^A \RequirePackage{xparse}[2008/08/03]
%^^A
%^^A % This should not happen but well, it did: the catcodes changed the
%^^A % \pk{expl3} way leaked so I~fix them here.
%^^A \gmu@septify
%^^A \@makeother\:
%^^A \catcode`_=8
%^^A \catcode`\ =10
\DeclareDocumentCommand\ampulexdef{O{}mO{}O{}mmm}{%^^B
% \begin{enumargs*}
% \opt definition prefix, empty by default,
% \mand macro to be redefined,
% \opt \cs{def} parameters string, empty by default,
% \opt definition body parameters to be taken in a~one-step
% expansion of the redefined macro, empty by default,
% \mand start token(s),
% \mand end token(s)
% \mand replacement.
% \end{enumargs*}
% \changes{v0.93}{2008/08/29}{added}
% \changes{v0.94}{2008/09/07}{made \pk{xparse}-ish and
% \cs{ampulexset} removed}
% For the example of usage see \ref{ampulexset}.
\def\gmu@tempa{#5}%
\def\gmu@tempb{#6}%
\def\gmu@tempc{#7}% we wrap the start, end and replacement tokens in
% macros to avoid unbalanced \incs{if}s.
\edef\gmu@tempd{%
\long\def\@nx\gmu@tempd
####1\all@other\gmu@tempa
####2\all@other\gmu@tempb
####3\@nx\gmu@tempd{%
\@ifempty{####3}{\hidden@iffalse}{\hidden@iftrue}}}%
%
\gmu@tempd% it defines \incs{gmu@tempc} to produce an open \incs{if}
% depending on whether the start and end token(s) are found in the
% meaning of \inverb|#2|.
%
\edef\gmu@tempe{%
\@nx\gmu@tempd\all@other#2%
\all@other\gmu@tempa
\all@other\gmu@tempb\@nx\gmu@tempd
}%
%
\gmu@tempe% we apply the checker and it
% produces an open \incs{if}.
\edef\gmu@tempd{%
\long\def\@nx\gmu@tempd
####1\@xa\unexpanded\@xa{\gmu@tempa}%
####2\@xa\unexpanded\@xa{\gmu@tempb}%
####3\@nx\ampulexdef{% we define a~temporary
% macro with the parameters delimited with the `start' and `end' parameters
% of \incs{ampulexdef}.
\@nx\unexpanded{####1}%
\@nx\@xa\@nx\unexpanded
\@nx\@xa{\@nx\gmu@tempc}%we replace the part of the redefined
% macro's meaning with the replacement text.
\@nx\unexpanded{####3}}}%
%^^A\show\gmu@tempd
\gmu@tempd
%^^A\show\gmu@tempd
%
\edef\gmu@tempf{#4}%
\edef\gmu@tempe{%
#1\def\@nx#2#3{%
\@xa\@xa\@xa\gmu@tempd\@xa#2\gmu@tempf\ampulexdef}}%
\gmu@tempe
\fi}
\def\ampulexhash{####}% for your convenience (not to count the hashes).
% For the heavy debugs I~was doing while preparing \pk{gmdoc}, as
% a~last resort I~used |\showlists|. But this command alone was
% usually too little: usually it needed setting |\showboxdepth| and
% |\showboxbreadth| to some positive values. So,
%
%^^A \Define\gmshowlists
\def\gmshowlists{\showboxdepth=1000 \showboxbreadth=1000 \showlists}
% \changes{v0.59}{06/09/04}{moved here from \pk{gmdoc}}
\newcommand\nameshow[1]{\@xa\show\csname#1\endcsname}
\newcommand\nameshowthe[1]{\@xa\showthe\csname#1\endcsname}
% Note that to get proper |\showthe\my@dimen14| in the `other' |@|'s
% scope you write |\nameshowthe{my@dimen}14|\,.
% Standard |\string| command returns a~string of `other' chars except
% for the space, for which it returns | |$_{10}$. In \pk{gmdoc}
% I~needed the spaces in macros' and environments' names to be always
% \catother, so I~define
% \Define\xiistring
% \changes{v0.59}{06/09/04}{moved here from \pk{gmdoc}}
\def\xiistring#1{%
\if\@nx#1\xiispace
\xiispace
\else
\string#1%
\fi}
% \division{\cs{@ifnextcat}, \cs{@ifnextac}}
%
% As you guess, we |\def| |\@ifnextcat| \agrave~la |\@ifnextchar|, see
% \LaTeXe\ source dated 2003/12/01, file d, lines 253--271. The
% difference is in the kind of test used: while |\@ifnextchar| does
% |\ifx|, |\@ifnextcat| does |\ifcat| which means it looks not at the
% meaning of a~token(s) but at their |\catcode|(s). As you (should)
% remember from \TeXbook, the former test doesn't expand macros while
% the latter does. But in |\@ifnextcat| the peeked token is protected
% against expanding by |\noexpand|. Note that the first parameter is
% not protected and therefore it shall be expanded if it's
% a~macro. Because an assignment is involved, you can't test whether
% the next token is an active char.
\long\def\@ifnextcat#1#2#3{%
% \changes{v0.75}{2007/01/01}{\cs{let} for \cs[\#]1 changed to
% \cs{def} to allow things like \cs{noexpand\*}}
% \HideDef
\def\reserved@d{#1}%
\def\reserved@a{#2}%
\def\reserved@b{#3}%
\futurelet\@let@token\@ifncat}
% \ResumeDef
\def\@ifncat{%
\ifx\@let@token\@sptoken
\let\reserved@c\@xifncat
\else
\ifcat\reserved@d\@nx\@let@token
\let\reserved@c\reserved@a
\else
\let\reserved@c\reserved@b
\fi
\fi
\reserved@c}
{\def\:{\let\@sptoken= } \global\: % this makes |\@sptoken| a space token.
% \DefIndex\@xifncat
\def\:{\@xifncat} \@xa\gdef\: {\futurelet\@let@token\@ifncat}}
% Note the trick to get a~macro with no parameter and requiring
% a~space after it. We do it inside a~group not to spoil the general
% meaning of |\:| (which we extend later).
% \dekmedskip
%
% \stanza The next command provides the real \cs{if} test for the next
% token. \emph{It} should be called |\@ifnextchar| but that name is
% assigned for the future \cs{ifx} text, as we know. Therefore we call
% it \cs{@ifnextif}.
\long\pdef\@ifnextif#1#2#3{%
% \changes{v0.75}{2007/01/01}{\cs{let} for \cs[\#]1 changed to
% \cs{def} to allow things like \cs{noexpand\*}}
% \HideDef
\def\reserved@d{#1}%
\def\reserved@a{#2}%
\def\reserved@b{#3}%
\futurelet\@let@token\@ifnif}
% \ResumeDef
\def\@ifnif{%
\ifx\@let@token\@sptoken
\let\reserved@c\@xifnif
\else
\if\reserved@d\@nx\@let@token
\let\reserved@c\reserved@a
\else
\let\reserved@c\reserved@b
\fi
\fi
\reserved@c}
% \UnDef
{\def\:{\let\@sptoken= } \: % this makes |\@sptoken| a space token.
% \UnDef
\def\:{\@xifnif} \@xa\gdef\: {\futurelet\@let@token\@ifnif}}
% \dekmedskip
%
% But how to peek at the next token to check whether it's an active
% char? First, we look with |\@ifnextcat| whether there stands a~group
% opener. We do that to avoid taking a~whole |{...}| as the argument
% of the next macro, that doesn't use |\futurelet| but takes the next
% token as an argument, tests it and puts back intact.
% \changes{v0.75}{2007/01/11}{added}
%
\long\pdef\@ifnextac#1#2{%
\@ifnextcat\bgroup{#2}{\gm@ifnac{#1}{#2}}}
\long\def\gm@ifnac#1#2#3{%
\ifcat\@nx~\@nx#3\afterfi{#1#3}\else\afterfi{#2#3}\fi}
% Yes, it won't work for an active char |\let| to |{|${}_1$, but it
% \emph{will} work for an active char |\let| to a~char of
% catcode${}\neq1$. (Is there anybody on Earth who'd make an active
% char working as |\bgroup|?)
%
% \stanza Now, define a~test that checks whether the next token is
% a~genuine space, | |${}_{10}$ that is. First define
% a~\CS let such
% a~space. The assignment needs a~little trick (\TeXbook\ appendix D)
% since |\let|'s syntax includes one optional space after |=|.
%
\let\gmu@reserveda\*%
\def\*{%
\let\*\gmu@reserveda
\let\gm@letspace= }%
\* %
% \Define\@ifnextspace
\def\@ifnextspace#1#2{%
\let\gmu@reserveda\*%
\def\*{%
\let\*\gmu@reserveda
\ifx\@let@token\gm@letspace\afterfi{#1}%
\else\afterfi{#2}%
\fi}%
\futurelet\@let@token\*}
% First use of this macro is for an active |-| that expands to |---|
% if followed by a~space. Another to make dot checking whether is
% followed by |~| without gobbling the space if it occurs instead.
%
% \stanza
% Now a~test if the next token is an active line end. I~use it in
% \pk{gmdoc} and later in this package for active long dashes.
\foone\obeylines{%
\long\pdef\@ifnextMac#1#2{%
\@ifnextchar^^M{#1}{#2}}}
% \division{\cs{afterfi} and pals}
% It happens from time to time that you have some sequence of macros
% in an |\if...| and you would like to expand |\fi| before expanding
% them (e.g., when the macros should take some tokens next to |\fi...|
% as their arguments. If you know how many macros are there, you may
% type a~couple of |\expandafter|s and not to care how terrible it
% looks. But if you don't know how many tokens will there be, you
% seem to be in a~real trouble. There's the Knuthian trick with
% |\next|. And here another, revealed to me by my \TeX\ Guru.
%
% I~think the situations when the Knuthian (the former) trick is not
% available are rather seldom, but they are imaginable at least: the
% |\next| trick involves an assignment so it won't work e.g.\ in
% |\edef|.
%
%^^A % One warning: those macros peel the braces off, i.e.,
%^^A % \[|\if..\afterfi{\@makeother\^^M}\fi|\]
%^^A % causes a~leakage of
%^^A % |^^M|\catother. To avoid pollution write
%^^A % \[|\if..\afterfi{\bgroup\@makeother\^^M\egroup}\fi|\,.\]
%