Debian Codegraph @

texmf-dist/doc/generic/olsak-misc/openclose.tex

raw
1 % openclose.tex
2 %%%%%%%%%%%%%%%%%%%%%%
3 % Petr Olsak 2014
4
5 % see
6 % http://tex.stackexchange.com/questions/196071/macro-to-close-all-open-environments-groups-and-argument-delimiters
7
8 % This macro defines \Open ...\Close pair which reads the text between them
9 % and repairs it to balanced text. The appropriated braces could be added at
10 % the begin (open braces) or at the end (close braces) of the text.
11 % After \Open ...\Close preprocessing is done the repaired text is
12 % normally processed.
13
14 % Examples:
15
16 % \Open abc\Close ... does nothing -> abc
17 % \Open a}b}c\Close ... adds braces -> {{a}b}c
18 % \Open a{b{c\Close ... adds braces -> a{b{c}}
19 % \Open a}b}c{d{e\Close ... adds braces -> {{a}b}c{d{e}}
20
21 % The \Open...\Close pairs would be nested. The processing of repairing
22 % braces is done from inner pairs to outer, like normal parentheses.
23
24 % The \autobracelist macro is empty by default and it can include the list
25 % of control sequences which have special feature between \Open ...\Close:
26 % It such control sequence is followed by open brace then it behaves normally
27 % else the open brace is added after it.
28
29 % Examples:
30
31 % \def\autobrcelist{\x\y}
32 % \Open \x \y \z aha \x \Close -> \x{\y{\z aha \x{}}}
33 % \Open \x{\y} aha\Close -> \x{\y{} aha}
34
35 \newcount\openLnum
36 \newtoks\currtext
37 \def\Open{\begingroup\let\bgroup=\relax \let\egroup=\relax
38 \expandafter\checkbracesJ\autobracelist\end
39 \let\ifIamInGroup=\iffalse \currtext={}\checkbracesA
40 }
41 \def\checkbracesA{\futurelet\tmp\checkbracesB}
42 \def\checkbracesB{%
43 \let\next=\checkbracesN
44 \ifx\tmp\spacetoken \let\next=\checkbracesC \let\nexxt=\checkbracesA \addtocurrtext{ }\fi
45 \ifx\tmp\bgroupOri \let\next=\checkbracesC \let\nexxt=\checkbracesD \fi
46 \ifx\tmp\egroupOri \let\next=\checkbracesC \let\nexxt=\checkbracesE \fi
47 \ifx\tmp\autobraced \let\next=\checkbracesH \fi
48 \ifx\tmp\Close \let\next=\checkbracesC \let\nexxt=\checkbracesF \fi
49 \ifx\tmp\Open \global\advance\openLnum by1 \let\next=\relax \fi
50 \next
51 }
52 \def\checkbracesC{\afterassignment\nexxt \let\next= }
53 \long\def\checkbracesN#1{\addtocurrtext#1\checkbracesA}
54 \def\checkbracesD{\begingroup \let\ifIamInGroup=\iftrue \currtext={}\checkbracesA}
55 \def\checkbracesE{\ifIamInGroup \addtocurrtextclosebrace
56 \else \currtext\expandafter{\expandafter{\the\currtext}}%
57 \fi \checkbracesA
58 }
59 \def\checkbracesF{%
60 \ifIamInGroup \addtocurrtextclosebrace \expandafter\checkbracesF
61 \else \expandafter\checkbracesG \fi
62 }
63 \def\checkbracesG{%
64 \ifnum\openLnum>0 \global\advance\openLnum by-1
65 \def\next{\expandafter\endgroup \expandafter
66 \currtext \expandafter\expandafter\expandafter
67 {\expandafter\the\expandafter\currtext \the\currtext}\checkbracesA}%
68 \else \def\next{\expandafter\endgroup \the\currtext}%
69 \fi \next
70 }
71 \def\checkbracesH#1{\addtocurrtext#1\futurelet\tmp\checkbracesI}
72 \def\checkbracesI{\ifx\tmp\bgroupOri \expandafter\checkbracesB
73 \else \expandafter\checkbracesD \fi
74 }
75 \def\checkbracesJ#1{\ifx#1\end \else \let#1=\autobraced \expandafter\checkbracesJ \fi}
76
77 \def\addtocurrtextclosebrace{\expandafter\endgroup
78 \expandafter\currtext\expandafter\expandafter\expandafter
79 {\expandafter\the\expandafter\currtext\expandafter{\the\currtext}}%
80 }
81 \long\def\addtocurrtext#1{\currtext\expandafter{\the\currtext#1}}
82 \let\bgroupOri=\bgroup
83 \let\egroupOri=\egroup
84 \def\tmp/{\let\spacetoken= }\tmp/ %
85 \def\Close{^\Close^}
86 \def\autobraced{^\autobraced^}
87 \def\autobracelist{}
88
89 \endiput