Debian Codegraph @

texmf-dist/source/luatex/ctablestack/ctablestack.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2015 David Carlisle and Joseph Wright
4 %
5 % It may be distributed and/or modified under the conditions of
6 % the LaTeX Project Public License (LPPL), either version 1.3c of
7 % this license or (at your option) any later version. The latest
8 % version of this license is in the file:
9 %
10 % http://www.latex-project.org/lppl.txt
11 %
12 %<package>\ifx
13 %<package> \ProvidesPackage\undefined\begingroup\def\ProvidesPackage
14 %<package> #1#2[#3]{\endgroup\immediate\write-1{Package: #1 #3}}
15 %<package>\fi
16 %<package>\ProvidesPackage{ctablestack}
17 %<*driver>
18 \ProvidesFile{ctablestack.dtx}
19 %</driver>
20 %<*package>
21 [2015/10/01 v1.0 Catcode table stable support]
22 %</package>
23 %<*driver>
24 \documentclass{ltxdoc}
25 \GetFileInfo{ctablestack.dtx}
26 \begin{document}
27 \title{\filename\\Catcode table stable support}
28 \author{David Carlisle and Joseph Wright}
29 \date{\filedate}
30 \maketitle
31 \setcounter{tocdepth}{2}
32 \tableofcontents
33 \DocInput{\filename}
34 \end{document}
35 %</driver>
36 % \fi
37 %
38 % \section{Overview}
39 %
40 % This small package adds support for a stack of category code tables to
41 % the core support for Lua\TeX{} provided by the \LaTeX{} kernel and
42 % available for plain users as |ltluatex.tex|. As such, the
43 % code here may be used with both plain \TeX{} and \LaTeX{}, and requires
44 % either an up-to-date \LaTeX{} kernel (2016 onward), use of \textsf{latexrelease}
45 % with older kernels or loading |ltluatex.tex| for plain users.
46 %
47 % The commands here are aimed mainly for use by package authors to develop
48 % environments needing specific catcode regimes. As such the package does
49 % not define any user level commands.
50 %
51 % \noindent
52 % \DescribeMacro{\@setrangecatcode}
53 % |\@setrangecatcode{|\meta{start}|}{|\meta{end}|}{|\meta{catcode}|}|\\
54 % Sets all characters in the range \meta{start}--\meta{end} inclusive to
55 % have the \meta{catcode} specified.
56 %
57 % \noindent
58 % \DescribeMacro{\@pushcatcodetable}
59 % \DescribeMacro{\@popcatcodetable}
60 % |\@pushcatcodetable|\\
61 % |\@popcatcodetable|\\
62 % This pair of commands enable the current category code r\'{e}gime to
63 % be saved and restored meaning that arbitrary catcode changes can be made.
64 % This functionality will normally be used in concert with applying
65 % catcode tables. For example
66 % \begin{verbatim}
67 % \catcode`\Z=4 %
68 % \@pushcatcodetable
69 % \catcodetable\catcodetable@latex
70 % % Code here
71 % \@popcatcodetable
72 % \showthe\catcode`\Z
73 % \end{verbatim}
74 % will ensure that the `content' is set with normal category codes but
75 % allow restoration of the non-standard codes at the conclusion. Importantly,
76 % it does not require that anything is known about the catcode situation in
77 % advance (\emph{cf.}~a more traditional approach to saving the state of
78 % targeting characters).
79 %
80 % \StopEventually{}
81 %
82 % \section{Implementation}
83 %
84 % \begin{macrocode}
85 %<*package>
86 % \end{macrocode}
87 %
88 % \begin{macrocode}
89 \edef\ctstackatcatcode{\the\catcode`\@}
90 \catcode`\@=11
91 % \end{macrocode}
92 %
93 % Check for \textsf{ltluatex} functionality using \cs{newluafunction} as a marker.
94 % \begin{macrocode}
95 \ifx\newluafunction\@undefined
96 \input{ltluatex}%
97 \fi
98 % \end{macrocode}
99 %
100 % \begin{macro}{\@setcatcodetable}
101 % Save a catcode table specified in |#1| using the catcode settings specified in |#2|.
102 % These settings are executed in a local group to avoid affecting surrounding code.
103 % (Saving a catcode table is always a global operation.)
104 % \begin{macrocode}1
105 \def\@setcatcodetable#1#2{%
106 \begingroup
107 #2%
108 \savecatcodetable#1%
109 \endgroup
110 }
111 % \end{macrocode}
112 % \end{macro}
113 %
114 % \begin{macro}{\@setrangecatcode}
115 % Set a range of characters from |#1| to |#2| inclusive to the catcode specified in |#3|.
116 % \begin{macrocode}
117 \def\@setrangecatcode#1#2#3{%
118 \ifnum#1>#2 %
119 \expandafter\@gobble
120 \else
121 \expandafter\@firstofone
122 \fi
123 {%
124 \catcode#1=#3 %
125 \expandafter\@setrangecatcode\expandafter
126 {\number\numexpr#1+1\relax}{#2}{#3}%
127 }%
128 }
129 % \end{macrocode}
130 % \end{macro}
131 %
132 % \begin{macro}{\@catcodetablelist}
133 % \begin{macro}{\@catcodetablestack}
134 % Data structures for a stack: a list of free tables in the stack and
135 % the stack record itself.
136 % \begin{macrocode}
137 \def\@catcodetablelist{}
138 \def\@catcodetablestack{}
139 % \end{macrocode}
140 % \end{macro}
141 % \end{macro}
142 %
143 % \begin{macro}{\@catcodetablestackcnt}
144 % A count for adding to the list of scratch tables.
145 % \begin{macrocode}
146 \newcount\@catcodetablestackcnt
147 % \end{macrocode}
148 % \end{macro}
149 %
150 % \begin{macro}{\@pushcatcodetable}
151 % \begin{macro}{\@pushctbl}
152 % To push a table, first check there is a free one in the pool and if
153 % not create one. Then take the top table in the pool and use it to save
154 % the current table.
155 % \begin{macrocode}
156 \def\@pushcatcodetable{%
157 \ifx\@catcodetablelist\empty
158 \global\advance\@catcodetablestackcnt by\@ne
159 \edef\@tempa{\csname ct@\the\@catcodetablestackcnt\endcsname}%
160 \expandafter\newcatcodetable\@tempa
161 \xdef\@catcodetablelist{\@tempa}%
162 \fi
163 \expandafter\@pushctbl\@catcodetablelist\@nil
164 }
165 % \end{macrocode}
166 %
167 % \begin{macrocode}
168 \def\@pushctbl#1#2\@nil{%
169 \gdef\@catcodetablelist{#2}%
170 \xdef\@catcodetablestack{#1\@catcodetablestack}%
171 \savecatcodetable#1%
172 }
173 % \end{macrocode}
174 % \end{macro}
175 % \end{macro}
176 %
177 % \begin{macro}{\@popcatcodetable}
178 % \begin{macro}{\@popctbl}
179 % Much the same in reverse.
180 % \begin{macrocode}
181 \def\@popcatcodetable{%
182 \if!\@catcodetablestack!%
183 \errmessage{Attempt to pop empty catcodetable stack}%
184 \else
185 \expandafter\@popctbl\@catcodetablestack\@nil
186 \fi
187 }
188 % \end{macrocode}
189 %
190 % \begin{macrocode}
191 \def\@popctbl#1#2\@nil{%
192 \gdef\@catcodetablestack{#2}%
193 \xdef\@catcodetablelist{\@catcodetablelist#1}%
194 \catcodetable#1%
195 }
196 % \end{macrocode}
197 % \end{macro}
198 % \end{macro}
199 %
200 % \begin{macrocode}
201 \catcode`\@\ctstackatcatcode\relax
202 % \end{macrocode}
203 % \begin{macrocode}
204 %</package>
205 % \end{macrocode}
206 %
207 % \Finale