| 1 |
% \iffalse meta-comment |
| 2 |
% |
| 3 |
% File: enverb.dtx Copyright (C) 2023-2025 Jonathan P. Spratte |
| 4 |
% |
| 5 |
% This work may be distributed and/or modified under the conditions of the |
| 6 |
% LaTeX Project Public License (LPPL), either version 1.3c of this license or |
| 7 |
% (at your option) any later version. The latest version of this license is in |
| 8 |
% the file: |
| 9 |
% |
| 10 |
% http://www.latex-project.org/lppl.txt |
| 11 |
% |
| 12 |
% ------------------------------------------------------------------------------ |
| 13 |
% |
| 14 |
%<*driver>^^A>>= |
| 15 |
\def\nameofplainTeX{plain} |
| 16 |
\ifx\fmtname\nameofplainTeX\else |
| 17 |
\expandafter\begingroup |
| 18 |
\fi |
| 19 |
\input l3docstrip.tex |
| 20 |
\askforoverwritefalse |
| 21 |
\preamble |
| 22 |
|
| 23 |
-------------------------------------------------------------- |
| 24 |
enverb -- read an environment verbatim |
| 25 |
E-mail: [email protected] |
| 26 |
Released under the LaTeX Project Public License v1.3c or later |
| 27 |
See http://www.latex-project.org/lppl.txt |
| 28 |
-------------------------------------------------------------- |
| 29 |
|
| 30 |
Copyright (C) 2023-2025 Jonathan P. Spratte |
| 31 |
|
| 32 |
This work may be distributed and/or modified under the conditions of the |
| 33 |
LaTeX Project Public License (LPPL), either version 1.3c of this license or |
| 34 |
(at your option) any later version. The latest version of this license is in |
| 35 |
the file: |
| 36 |
|
| 37 |
http://www.latex-project.org/lppl.txt |
| 38 |
|
| 39 |
This work is "maintained" (as per LPPL maintenance status) by |
| 40 |
Jonathan P. Spratte. |
| 41 |
|
| 42 |
This work consists of the files enverb.dtx |
| 43 |
enverb-doc.tex |
| 44 |
and the derived files enverb-doc.pdf |
| 45 |
enverb.sty |
| 46 |
|
| 47 |
\endpreamble |
| 48 |
\postamble |
| 49 |
\endpostamble |
| 50 |
\generate{\file{enverb.sty}{\from{enverb.dtx}{pkg}}} |
| 51 |
\ifx\fmtname\nameofplainTeX |
| 52 |
\expandafter\endbatchfile |
| 53 |
\else |
| 54 |
\expandafter\endgroup |
| 55 |
\fi |
| 56 |
%</driver>^^A=<< |
| 57 |
% \fi |
| 58 |
% |
| 59 |
% \gobbledocstriptag |
| 60 |
%<*pkg> |
| 61 |
% |
| 62 |
% \subsection{Who we are} |
| 63 |
% |
| 64 |
% \begin{macro}[internal]{\enverb@date,\enverb@version} |
| 65 |
% We store the package version and date inside macros. |
| 66 |
% \begin{macrocode} |
| 67 |
\newcommand*\enverb@date{2025-01-10} |
| 68 |
\newcommand*\enverb@version{1.1} |
| 69 |
% \end{macrocode} |
| 70 |
% \end{macro} |
| 71 |
% |
| 72 |
% And the package identification: |
| 73 |
% \begin{macrocode} |
| 74 |
\ProvidesPackage{enverb} |
| 75 |
[\enverb@date\space v\enverb@version\space read an environment verbatim] |
| 76 |
% \end{macrocode} |
| 77 |
% |
| 78 |
% \subsection{Some required \LaTeXe\ style argument grabbers} |
| 79 |
% \begin{macro}[internal]{\@thirdofthree,\@firstofnine} |
| 80 |
% This here are just two argument grabbers that aren't defined in all versions |
| 81 |
% of \LaTeXe. |
| 82 |
% \begin{macrocode} |
| 83 |
\providecommand\@thirdofthree[3]{#3} |
| 84 |
\providecommand\@firstofnine[9]{#1} |
| 85 |
% \end{macrocode} |
| 86 |
% \end{macro} |
| 87 |
% |
| 88 |
% \subsection{Borrow some code} |
| 89 |
% \begin{macro}[internal] |
| 90 |
% {\enverb@count,\enverb@ifxTF,\enverb@chargen,\enverb@othercr} |
| 91 |
% We'll make use of the following \pkg{expl3} functions, but since the |
| 92 |
% remainder of this package is coded in \LaTeXe\ style we stick to that. |
| 93 |
% \begin{macrocode} |
| 94 |
\ExplSyntaxOn |
| 95 |
\cs_new_eq:NN \enverb@count \tl_count:n |
| 96 |
\cs_new_eq:NN \enverb@ifxTF \token_if_eq_meaning:NNTF |
| 97 |
\cs_new_eq:NN \enverb@chargen \char_generate:nn |
| 98 |
\cs_set:Npx \enverb@othercr { \char_generate:nn {13} {12} } |
| 99 |
\ExplSyntaxOff |
| 100 |
% \end{macrocode} |
| 101 |
% \end{macro} |
| 102 |
% |
| 103 |
% \subsection{Key-value Setup} |
| 104 |
% First we need to load a package to allow us defining our keys, then we define |
| 105 |
% a bunch of keys: |
| 106 |
% \begin{macrocode} |
| 107 |
\RequirePackage{expkv-def} |
| 108 |
% \end{macrocode} |
| 109 |
% |
| 110 |
% \begin{macrocode} |
| 111 |
\ekvdefinekeys{enverb} |
| 112 |
{ |
| 113 |
protect code ignore = \let\enverb@ifautoignore\@secondoftwo |
| 114 |
,also eint ignore = \enverb@ignore |
| 115 |
,boolTF auto-ignore = \enverb@ifautoignore |
| 116 |
,initial auto-ignore |
| 117 |
,eint more-ignore = \enverb@moreignore |
| 118 |
,initial more-ignore = 2 |
| 119 |
,long store bol = \enverb@bol@content |
| 120 |
,long code bol+ = \enverb@add\enverb@bol@content{#1} |
| 121 |
,long code +bol = \enverb@pre\enverb@bol@content{#1} |
| 122 |
,long store eol = \enverb@eol@content |
| 123 |
,einitial eol = \enverb@othercr |
| 124 |
,long code eol+ = \enverb@add\enverb@eol@content{#1} |
| 125 |
,long code +eol = \enverb@pre\enverb@eol@content{#1} |
| 126 |
,protect code key-handler = \protected\long\def\enverb@keyhandler##1{#1} |
| 127 |
,protect code key-set = \protected\long\ekvsetdef\enverb@keyhandler{#1} |
| 128 |
,unknown code = \enverb@add\enverb@unknown@kv{, {#3} = {#2} } |
| 129 |
,unknown noval = \enverb@add\enverb@unknown@kv{, {#2} } |
| 130 |
,boolTF oarg-not-enverb = \enverb@ifoargtokeyhandler |
| 131 |
} |
| 132 |
% \end{macrocode} |
| 133 |
% \begin{macro}{\enverbsetup} |
| 134 |
% We need a user facing macro to set our keys. |
| 135 |
% \begin{macrocode} |
| 136 |
\protected\long\ekvsetdef\enverbsetup{enverb} |
| 137 |
% \end{macrocode} |
| 138 |
% \end{macro} |
| 139 |
% |
| 140 |
% \begin{macro}[internal]{\enverb@unknown@kv} |
| 141 |
% This is a storage for all unknown keys encountered. |
| 142 |
% \begin{macrocode} |
| 143 |
\let\enverb@unknown@kv\@empty |
| 144 |
% \end{macrocode} |
| 145 |
% \end{macro} |
| 146 |
% |
| 147 |
% \subsection{Miscellaneous Auxiliary Functions} |
| 148 |
% |
| 149 |
% \begin{macro}[internal]{\enverb@stop} |
| 150 |
% A very simple minded stop-macro, just gobble up to the eponymous mark. |
| 151 |
% \begin{macrocode} |
| 152 |
\long\def\enverb@stop#1\enverb@stop{} |
| 153 |
% \end{macrocode} |
| 154 |
% \end{macro} |
| 155 |
% |
| 156 |
% \begin{macro}[internal]{\enverb@error} |
| 157 |
% \begin{macrocode} |
| 158 |
\protected\def\enverb@error{\PackageError{enverb}} |
| 159 |
% \end{macrocode} |
| 160 |
% \end{macro} |
| 161 |
% |
| 162 |
% \begin{macro}[internal]{\enverb@misused} |
| 163 |
% This is an error message that'd be used inside a |\lowercase| environment if |
| 164 |
% put where it originally should've been. Hence we define it now to get |
| 165 |
% correct formatting. |
| 166 |
% \begin{macrocode} |
| 167 |
\protected\def\enverb@misused |
| 168 |
{% |
| 169 |
\enverb@error |
| 170 |
{Misused \string\enverb. Input already tokenised}% |
| 171 |
{% |
| 172 |
It seems you used the \string\enverb\space based environment |
| 173 |
{\@currenvir} inside another\MessageBreak |
| 174 |
macro's or a primitive's argument; that doesn't work.% |
| 175 |
}% |
| 176 |
} |
| 177 |
% \end{macrocode} |
| 178 |
% \end{macro} |
| 179 |
% |
| 180 |
% \begin{macro}[internal]{\enverb@keyhandler, \enverb@keyhandler@} |
| 181 |
% This is the initial definition of the handler for unknown keys, namely throw |
| 182 |
% an error containing the list of unknown keys. |
| 183 |
% \begin{macrocode} |
| 184 |
\protected\long\def\enverb@keyhandler#1% |
| 185 |
{% |
| 186 |
\if\relax\detokenize{#1}\relax |
| 187 |
\expandafter\@gobbletwo |
| 188 |
\fi |
| 189 |
\@firstofone |
| 190 |
{% |
| 191 |
\enverb@error |
| 192 |
{% |
| 193 |
Unknown keys encountered: |
| 194 |
\expanded |
| 195 |
{% |
| 196 |
\ekvparse |
| 197 |
{\MessageBreak\@spaces\unexpanded}% |
| 198 |
{\MessageBreak\@spaces\enverb@keyhandler@}% |
| 199 |
{#1}% |
| 200 |
}% |
| 201 |
\@gobble |
| 202 |
}% |
| 203 |
{% |
| 204 |
Perhaps you misspelled some key names? Or you forgot to set up |
| 205 |
custom key\MessageBreak |
| 206 |
parsing.% |
| 207 |
}% |
| 208 |
}% |
| 209 |
} |
| 210 |
\long\def\enverb@keyhandler@#1{\unexpanded{#1} = \unexpanded} |
| 211 |
% \end{macrocode} |
| 212 |
% \end{macro} |
| 213 |
% |
| 214 |
% \subsection{Collector} |
| 215 |
% |
| 216 |
% Much of the code in this package works under a weird category code regime, |
| 217 |
% which the following sets up. After this |~| will be an active newline |
| 218 |
% character, |:| will be an active space, and |;| will be an active tab. |
| 219 |
% \begin{macrocode} |
| 220 |
\begingroup |
| 221 |
\lccode`\~=`\^^M |
| 222 |
\catcode`\:=13 |
| 223 |
\lccode`\:=`\ % <- space |
| 224 |
\catcode`\;=13 |
| 225 |
\lccode`\;=`\^^I % <- tab |
| 226 |
\lowercase{\endgroup |
| 227 |
% \end{macrocode} |
| 228 |
% |
| 229 |
% \begin{macro}[internal] |
| 230 |
% {\enverb@body@space,\enverb@body@tab,\enverb@body@newline,\enverb@collect} |
| 231 |
% To quickly check whether a line contains only spaces or tabs we define them |
| 232 |
% as empty, that way we can remove them simply by one step of |\romannumeral| |
| 233 |
% expansion. The newlines on the other hand grab the entire next line, check |
| 234 |
% whether the matching |\end| is encountered, and if not leave the contents |
| 235 |
% in |\unexpanded|. This grabbing is not done by the newline characters |
| 236 |
% directly, but instead via |\enverb@collect| so that low level errors are |
| 237 |
% easier to understand. |
| 238 |
% \begin{macrocode} |
| 239 |
\def\enverb@body@space{} |
| 240 |
\def\enverb@body@tab{} |
| 241 |
\def\enverb@body@newline{\enverb@collect} |
| 242 |
\def\enverb@collect#1~% |
| 243 |
{\enverb@ifnotend{#1}{\enverb@bol\unexpanded{#1}\enverb@eol\enverb@collect}} |
| 244 |
% \end{macrocode} |
| 245 |
% \end{macro} |
| 246 |
% |
| 247 |
% \begin{macro}{\enverbBody} |
| 248 |
% \begin{macro}[internal]{\enverb@body@setup} |
| 249 |
% This function sets up the entire category code regime and initial |
| 250 |
% assignments of special tokens. |
| 251 |
% \begin{macrocode} |
| 252 |
\protected\def\enverb@body@setup |
| 253 |
{% |
| 254 |
\let\enverbBody\@empty |
| 255 |
\let\do\@makeother\dospecials |
| 256 |
\endlinechar=`\^^M |
| 257 |
\catcode`\^^M=13 \let~\enverb@body@newline |
| 258 |
\catcode`\ =13 \let:\enverb@body@space |
| 259 |
\catcode`\^^I=13 \let;\enverb@body@tab |
| 260 |
\let\enverb@bol\relax |
| 261 |
\let\enverb@eol\relax |
| 262 |
} |
| 263 |
% \end{macrocode} |
| 264 |
% \end{macro} |
| 265 |
% \end{macro} |
| 266 |
% |
| 267 |
% \begin{macro}[internal] |
| 268 |
% { |
| 269 |
% \enverb@ifxanyTF, \enverb@ifxanyTF@, \enverb@ifxanyTF@ifdone, |
| 270 |
% \enverb@ifxanyTF@F, \enverb@ifxanyTF@T |
| 271 |
% } |
| 272 |
% This function provides a loop to check a list of symbols whether any of them |
| 273 |
% matches the meaning of the first argument. |
| 274 |
% \begin{macrocode} |
| 275 |
\long\def\enverb@ifxanyTF#1#2% |
| 276 |
{\enverb@ifxanyTF@#1#2\enverb@ifxanyTF@} |
| 277 |
\long\def\enverb@ifxanyTF@#1#2% |
| 278 |
{% |
| 279 |
\enverb@ifxanyTF@ifdone#2\enverb@ifxanyTF@F\enverb@ifxanyTF@ |
| 280 |
\enverb@ifxTF#1#2\enverb@ifxanyTF@T{\enverb@ifxanyTF@#1}% |
| 281 |
} |
| 282 |
\long\def\enverb@ifxanyTF@ifdone#1\enverb@ifxanyTF@{} |
| 283 |
\long\def\enverb@ifxanyTF@F |
| 284 |
\enverb@ifxanyTF@\enverb@ifxTF#1\enverb@ifxanyTF@T#2#3#4% |
| 285 |
{#4} |
| 286 |
\long\def\enverb@ifxanyTF@T#1\enverb@ifxanyTF@#2#3{#2} |
| 287 |
% \end{macrocode} |
| 288 |
% \end{macro} |
| 289 |
% |
| 290 |
% \begin{macro}[internal]{\enverb@search@oarg@a, \enverb@search@oarg@ifx} |
| 291 |
% This function shall search for the optional argument. The first case is |
| 292 |
% rather easy, namely we check for a space (remember that |:| is an active |
| 293 |
% space character in the current context). If this isn't the first line (so |
| 294 |
% the line containing the |\begin| statement) that space is added to the body |
| 295 |
% and searching continues.\par |
| 296 |
% Next repeat the same logic but for an active tab (|;|). Afterwards we check |
| 297 |
% for an active newline (|~|) which if found sets the conditional for the |
| 298 |
% first line to false, still applying the same logic otherwise.\par |
| 299 |
% Lastly we check for the open bracket. If it's found we collect our optional |
| 300 |
% argument, otherwise the body starts, which is either an illegal character |
| 301 |
% immediately following the |\begin| line, or we normally collect the body. |
| 302 |
% |
| 303 |
% \begin{macrocode} |
| 304 |
\def\enverb@search@oarg@ifx#1#2% |
| 305 |
{% |
| 306 |
\enverb@ifxTF#1\@let@token |
| 307 |
{% |
| 308 |
\ifenverb@firsteol#2\else\enverb@body@add{#1}\fi |
| 309 |
\enverb@search@oarg@b |
| 310 |
}% |
| 311 |
} |
| 312 |
\protected\def\enverb@search@oarg@a |
| 313 |
{% |
| 314 |
\enverb@search@oarg@ifx:{}% |
| 315 |
{% |
| 316 |
\enverb@search@oarg@ifx;{}% |
| 317 |
{% |
| 318 |
\enverb@search@oarg@ifx~\enverb@firsteolfalse |
| 319 |
{% |
| 320 |
\enverb@ifxTF{[}\@let@token |
| 321 |
{\enverb@oarg}% |
| 322 |
{% |
| 323 |
\enverb@ifxanyTF\@let@token |
| 324 |
{% |
| 325 |
\@sptoken\par\bgroup\egroup$&##^_% $ |
| 326 |
} |
| 327 |
{% |
| 328 |
\enverb@misused |
| 329 |
\endgroup |
| 330 |
}% |
| 331 |
{% |
| 332 |
\ifenverb@firsteol |
| 333 |
\expandafter\enverb@body@after@begin |
| 334 |
\else |
| 335 |
\expandafter\enverb@body |
| 336 |
\fi |
| 337 |
}% |
| 338 |
}% |
| 339 |
}% |
| 340 |
}% |
| 341 |
}% |
| 342 |
} |
| 343 |
% \end{macrocode} |
| 344 |
% \end{macro} |
| 345 |
% |
| 346 |
% \begin{macro}[internal] |
| 347 |
% {\enverb@body, \enverb@body@after@oarg, \enverb@body@after@begin} |
| 348 |
% The body collection works by collecting everything inside a macro in one |
| 349 |
% sweep, and a second pass over the collected material to strip leading spaces |
| 350 |
% if the |ignore| or |auto-ignore| feature is used. This macro starts the |
| 351 |
% first sweep. Since during the collection of the optional argument we might |
| 352 |
% already have collected a few tokens we place those at the start by expanding |
| 353 |
% the current definition of |\enverbBody| once. |
| 354 |
% \begin{macrocode} |
| 355 |
\protected\def\enverb@body |
| 356 |
{\edef\enverbBody{\iffalse}\fi\expandafter\enverb@collect\enverbBody} |
| 357 |
% \end{macrocode} |
| 358 |
% The other two macros also start collecting the body, but are only called |
| 359 |
% after either an optional argument was found, or if the first non-ignored |
| 360 |
% character of the optional argument search was encountered before the first |
| 361 |
% newline. In both cases we check whether the remainder of that line can be |
| 362 |
% considered blank, and if so collect the body. |
| 363 |
% \begin{macrocode} |
| 364 |
\def\enverb@body@after@oarg#1~% |
| 365 |
{\enverb@ensure@blank{#1}{closing bracket}\enverb@body} |
| 366 |
\def\enverb@body@after@begin#1~% |
| 367 |
{\enverb@ensure@blank{#1}{\string\begin}\enverb@body} |
| 368 |
% \end{macrocode} |
| 369 |
% This is also the last place we need the strange category setup in, so we |
| 370 |
% close the delimiting brace of the |\lowercase| above here. |
| 371 |
% \begin{macrocode} |
| 372 |
} |
| 373 |
% \end{macrocode} |
| 374 |
% \end{macro} |
| 375 |
% |
| 376 |
% \begin{macro}[internal]{\enverb@ensure@blank} |
| 377 |
% We actually need to define the check for an empty line. This check here |
| 378 |
% works for our specifically set up regime of spaces and tabs (which both |
| 379 |
% expand to nothing, the same trick is basically used twice, though we once |
| 380 |
% fully expand and once use romannumeral-expansion). |
| 381 |
% \begin{macrocode} |
| 382 |
\newcommand\enverb@ensure@blank[2] |
| 383 |
{% |
| 384 |
\expandafter\enverb@ifempty\expanded{{#1}}{}% |
| 385 |
{% |
| 386 |
\expanded |
| 387 |
{% |
| 388 |
\noexpand\enverb@error |
| 389 |
{% |
| 390 |
Line after #2 not empty.\noexpand\MessageBreak |
| 391 |
Contains: \detokenize\expandafter{\romannumeral`\^^@#1}% |
| 392 |
}% |
| 393 |
{% |
| 394 |
\noexpand\enverb will try to ignore this. You should clean up |
| 395 |
your input.% |
| 396 |
}% |
| 397 |
}% |
| 398 |
}% |
| 399 |
} |
| 400 |
% \end{macrocode} |
| 401 |
% \end{macro} |
| 402 |
% |
| 403 |
% \begin{macro}[internal] |
| 404 |
% {\enverb@ifempty,\enverb@ifempty@,\enverb@ifempty@true} |
| 405 |
% This is just a quick argument-grabbing based test for an empty argument. |
| 406 |
% \begin{macrocode} |
| 407 |
\newcommand\enverb@ifempty[1] |
| 408 |
{% |
| 409 |
\enverb@ifempty@\enverb@ifempty@A#1\enverb@ifempty@B.\enverb@ifempty@true |
| 410 |
\enverb@ifempty@A\enverb@ifempty@B |
| 411 |
} |
| 412 |
\def\enverb@ifempty@#1\enverb@ifempty@A\enverb@ifempty@B#2#3{#3} |
| 413 |
\def\enverb@ifempty@true\enverb@ifempty@A\enverb@ifempty@B#1#2{#1} |
| 414 |
% \end{macrocode} |
| 415 |
% \end{macro} |
| 416 |
% |
| 417 |
% \begin{macro}[internal]{\enverb@add,\enverb@pre,\enverb@body@add} |
| 418 |
% These are just two functions to add things to a token list storing macro, |
| 419 |
% one is available for arbitrary contents, the other is used for the |body|. |
| 420 |
% \begin{macrocode} |
| 421 |
\protected\long\def\enverb@add#1#2{\edef#1{\unexpanded\expandafter{#1#2}}} |
| 422 |
\protected\long\def\enverb@pre#1#2% |
| 423 |
{\edef#1{\unexpanded{#2}\unexpanded\expandafter{#1}}} |
| 424 |
\protected\def\enverb@body@add{\enverb@add\enverbBody} |
| 425 |
% \end{macrocode} |
| 426 |
% \end{macro} |
| 427 |
% |
| 428 |
% \begin{macro}{\enverb} |
| 429 |
% This macro is the front facing command that starts the entire grabbing |
| 430 |
% logic. The first thing we need to do is define the auxiliary we use to check |
| 431 |
% whether we're done collecting the body, which is dependent on the current |
| 432 |
% environment's name. Next we store the mandatory argument in |marg| and |
| 433 |
% initialise the |oarg|-storage, category regime, and |firsteol|-boolean. |
| 434 |
% Finally we start searching for the optional argument. |
| 435 |
% \begin{macrocode} |
| 436 |
\NewDocumentCommand \enverb { s +m } |
| 437 |
{% |
| 438 |
\expandafter\enverb@ifnotend@setup@perhaps\expanded |
| 439 |
{{\string{\@currenvir\string}}}% |
| 440 |
\let\enverb@collected@oarg\@empty |
| 441 |
\edef\enverb@collected@marg{\unexpanded{#2}}% |
| 442 |
\begingroup |
| 443 |
\enverb@body@setup |
| 444 |
\IfBooleanTF{#1}% |
| 445 |
{\enverb@body@after@begin}% |
| 446 |
{% |
| 447 |
\enverb@firsteoltrue |
| 448 |
\enverb@search@oarg |
| 449 |
}% |
| 450 |
} |
| 451 |
% \end{macrocode} |
| 452 |
% \end{macro} |
| 453 |
% |
| 454 |
% \begin{macro}[internal] |
| 455 |
% {\ifenverb@firsteol,\enverb@firsteoltrue,\enverb@firsteolfalse} |
| 456 |
% A simple \TeX-style boolean to keep track whether we're in front of the |
| 457 |
% first newline. |
| 458 |
% \begin{macrocode} |
| 459 |
\newif\ifenverb@firsteol |
| 460 |
% \end{macrocode} |
| 461 |
% \end{macro} |
| 462 |
% |
| 463 |
% \begin{macro}[internal]{\enverb@search@oarg, \enverb@search@oarg@b} |
| 464 |
% The core function to search for an optional argument was already defined |
| 465 |
% above, these are the first and third step of that functionality (starting |
| 466 |
% and resuming to search). |
| 467 |
% \begin{macrocode} |
| 468 |
\protected\def\enverb@search@oarg{\futurelet\@let@token\enverb@search@oarg@a} |
| 469 |
\protected\def\enverb@search@oarg@b{\expandafter\enverb@search@oarg\@gobble} |
| 470 |
% \end{macrocode} |
| 471 |
% \end{macro} |
| 472 |
% |
| 473 |
% \begin{macro}[internal]{\enverb@oarg, \enverb@oarg@} |
| 474 |
% This is used once the opening brace of the optional argument is found. First |
| 475 |
% we close the group in which the body's category code regime is active, then |
| 476 |
% we actually collect the optional argument with the normal category codes, |
| 477 |
% restore the body's regime and collect it. |
| 478 |
% \begin{macrocode} |
| 479 |
\protected\def\enverb@oarg{\endgroup\enverb@oarg@} |
| 480 |
\NewDocumentCommand\enverb@oarg@{+O{}} |
| 481 |
{% |
| 482 |
\enverb@add\enverb@collected@oarg{#1}% |
| 483 |
\begingroup |
| 484 |
\enverb@body@setup |
| 485 |
\enverb@body@after@oarg |
| 486 |
} |
| 487 |
% \end{macrocode} |
| 488 |
% \end{macro} |
| 489 |
% |
| 490 |
% \begin{macro}[internal]{\enverb@ifnotend, \enverb@ifnotend} |
| 491 |
% These functions check whether the input contains the string representation |
| 492 |
% |\end|. If it does they invoke \cs[no-index]{enverb@ifnotend@maybe} with the |
| 493 |
% tokens in front of |\end| and after it as two arguments. Otherwise the third |
| 494 |
% argument after \cs{enverb@ifnotend@}'s expansion is directly used, which is |
| 495 |
% the second (curried) argument to \cs{enverb@ifnotend}. |
| 496 |
% \begin{macrocode} |
| 497 |
\newcommand\enverb@ifnotend[1]% |
| 498 |
{% |
| 499 |
\def\enverb@ifnotend##1% |
| 500 |
{% |
| 501 |
\enverb@ifnotend@ |
| 502 |
##1\enverb@mark\enverb@ifnotend@maybe |
| 503 |
#1\enverb@mark\@thirdofthree |
| 504 |
\enverb@stop |
| 505 |
}% |
| 506 |
\def\enverb@ifnotend@##1#1##2\enverb@mark##3##4\enverb@stop{##3{##1}{##2}}% |
| 507 |
} |
| 508 |
\expandafter\enverb@ifnotend\expanded{{\expandafter\@gobble\string\\end}} |
| 509 |
% \end{macrocode} |
| 510 |
% \end{macro} |
| 511 |
% |
| 512 |
% \begin{macro}[internal]{\enverb@ifnotend@maybe} |
| 513 |
% This function will be used if an |\end| was encountered and should check |
| 514 |
% whether that is indeed the end of our environment, it uses a second stage |
| 515 |
% \cs[no-index]{enverb@ifnotend@perhaps} for that check. The |\romannumeral| |
| 516 |
% expansion it employs will strip any leading spaces or tabs from |#2|. |
| 517 |
% \begin{macrocode} |
| 518 |
\newcommand\enverb@ifnotend@maybe[2] |
| 519 |
{\expandafter\enverb@ifnotend@perhaps\expandafter{\romannumeral`\^^@#2}{#1}} |
| 520 |
% \end{macrocode} |
| 521 |
% \end{macro} |
| 522 |
% |
| 523 |
% \begin{macro}[internal] |
| 524 |
% { |
| 525 |
% \enverb@ifnotend@setup@perhaps, |
| 526 |
% \enverb@ifnotend@perhaps, \enverb@ifnotend@perhaps@, |
| 527 |
% \@thirdofthree |
| 528 |
% } |
| 529 |
% We need an expandable and fast check to see whether some input really |
| 530 |
% contains the |\end| of our environment. Checking for |\end| itself is |
| 531 |
% already done above, we need to check for the correct argument to it. The |
| 532 |
% following does so by argument grabbing logic. |
| 533 |
% \begin{macrocode} |
| 534 |
\protected\def\enverb@ifnotend@setup@perhaps#1% |
| 535 |
{% |
| 536 |
\def\enverb@ifnotend@perhaps##1% |
| 537 |
{% |
| 538 |
\enverb@ifnotend@perhaps@\enverb@mark##1\enverb@mark\enverb@ifnotend@end |
| 539 |
\enverb@mark#1\enverb@mark\@thirdofthree |
| 540 |
\enverb@stop |
| 541 |
}% |
| 542 |
\def\enverb@ifnotend@perhaps@ |
| 543 |
##1\enverb@mark#1##2\enverb@mark##3##4\enverb@stop |
| 544 |
{##3{##2}}% |
| 545 |
} |
| 546 |
% \end{macrocode} |
| 547 |
% \end{macro} |
| 548 |
% |
| 549 |
% \begin{macro}[internal]{\enverb@ifnotend@end} |
| 550 |
% If the correct |\end| statement was found this function will be called by |
| 551 |
% \cs[no-index]{enverb@ifnotend@perhaps}. It's first argument will be anything |
| 552 |
% in that line following the |\end| statement, the second argument will be the |
| 553 |
% material in front of it. And |#3| will be whatever the argument to |
| 554 |
% \cs[no-index]{enverb@ifnotend} was. |
| 555 |
% |
| 556 |
% The first thing we need to do is end the body collection by placing the |
| 557 |
% matching closing brace. After that we throw an error on lost content after |
| 558 |
% the |\end| if it's non-blank. And finally we need to smuggle the collected |
| 559 |
% body outside the current group's scope. |
| 560 |
% \begin{macrocode} |
| 561 |
\def\enverb@ifnotend@end#1#2#3% |
| 562 |
{% |
| 563 |
\iffalse{\fi}% |
| 564 |
\enverb@ensure@blank{#1}{\string\end}% |
| 565 |
\expandafter\enverb@finalise\expandafter{\enverbBody}{#2}% |
| 566 |
} |
| 567 |
% \end{macrocode} |
| 568 |
% \end{macro} |
| 569 |
% |
| 570 |
% \begin{macro}[internal]{\enverb@finalise} |
| 571 |
% The finalising function closes the group containing the body's catcode |
| 572 |
% regime. The already tokenised body is |#1|, and |#2| is the contents found |
| 573 |
% in front of the |\end| statement, which we might use for the auto-ignore |
| 574 |
% feature. |
| 575 |
% |
| 576 |
% It then does the key=val parsing, sets up the aftertreatment and assigns the |
| 577 |
% final meaning to \cs{enverbBody}. Eventually it calls the end code of the |
| 578 |
% enclosing environment. |
| 579 |
% \begin{macrocode} |
| 580 |
\protected\def\enverb@finalise#1#2% |
| 581 |
{% |
| 582 |
\endgroup |
| 583 |
\expandafter\enverbsetup\expandafter{\enverb@collected@marg}% |
| 584 |
\enverb@ifoargtokeyhandler |
| 585 |
{% |
| 586 |
\expandafter\enverb@keyhandler\expanded |
| 587 |
{{% |
| 588 |
\unexpanded\expandafter{\enverb@unknown@kv},% |
| 589 |
\unexpanded\expandafter{\enverb@collected@oarg}% |
| 590 |
}}% |
| 591 |
}% |
| 592 |
{% |
| 593 |
\expandafter\enverbsetup\expandafter{\enverb@collected@oarg}% |
| 594 |
\expandafter\enverb@keyhandler\expandafter{\enverb@unknown@kv}% |
| 595 |
}% |
| 596 |
\enverb@ifautoignore |
| 597 |
{\enverb@setup@ignore{\enverb@count{#2}+\enverb@moreignore}}% |
| 598 |
{\enverb@setup@ignore\enverb@ignore}% |
| 599 |
\edef\enverb@line##1\enverb@eol |
| 600 |
{% |
| 601 |
\noexpand\unexpanded{##1}% |
| 602 |
%\noexpand\detokenize{##1}% % TODO |
| 603 |
\noexpand\unexpanded{\unexpanded\expandafter{\enverb@eol@content}}% |
| 604 |
}% |
| 605 |
\edef\enverbBody{#1}% |
| 606 |
\expandafter\end\expandafter{\@currenvir}% |
| 607 |
} |
| 608 |
% \end{macrocode} |
| 609 |
% \end{macro} |
| 610 |
% |
| 611 |
% \begin{macro}[internal]{\enverb@setup@ignore, \enverb@setup@ignore@} |
| 612 |
% \begin{macrocode} |
| 613 |
\protected\def\enverb@setup@ignore#1% |
| 614 |
{\expandafter\enverb@setup@ignore@\the\numexpr#1\relax;\enverb@line} |
| 615 |
\protected\def\enverb@setup@ignore@#1;#2% |
| 616 |
{% |
| 617 |
\ifnum#1>9 |
| 618 |
\expandafter\@firstoftwo |
| 619 |
\else |
| 620 |
\expandafter\@secondoftwo |
| 621 |
\fi |
| 622 |
{\expandafter\enverb@setup@ignore@\the\numexpr#1-8;{\@firstofnine{#2}}}% |
| 623 |
% \end{macrocode} |
| 624 |
% Just to make sure that |\renewcommand| works correctly, we ensure the macro |
| 625 |
% exists. |
| 626 |
% \begin{macrocode} |
| 627 |
{% |
| 628 |
\let\enverb@bol@gobble\@empty |
| 629 |
\expanded |
| 630 |
{% |
| 631 |
\unexpanded{\renewcommand\enverb@bol@gobble}\ifnum#1>\z@[#1]\fi |
| 632 |
{\unexpanded{#2}}% |
| 633 |
\unexpanded{\def\enverb@bol##1\enverb@eol}% |
| 634 |
{% |
| 635 |
\noexpand\unexpanded |
| 636 |
{\unexpanded\expandafter{\enverb@bol@content}}% |
| 637 |
\unexpanded{\expandafter\enverb@ifempty\expanded}{{##1}}% |
| 638 |
{\noexpand\enverb@line}% |
| 639 |
{\noexpand\enverb@bol@gobble}% |
| 640 |
##1\noexpand\enverb@eol |
| 641 |
}% |
| 642 |
}% |
| 643 |
}% |
| 644 |
} |
| 645 |
% \end{macrocode} |
| 646 |
% \end{macro} |
| 647 |
% |
| 648 |
% \subsection{Output Oriented Macros} |
| 649 |
% |
| 650 |
% \begin{macro}[internal]{\enverb@rmeol, \enverb@rmeol@} |
| 651 |
% Many of our output routines require the |\scantokens| primitive. Since that |
| 652 |
% one places an additional end of line at the end of its argument we need a |
| 653 |
% way to remove it (this usually results in an additional space in the output |
| 654 |
% otherwise). For that we place a comment character at the end of the input |
| 655 |
% (if it's possible), if that's not available we hope that a stringified |
| 656 |
% |\relax| will do. The code here will use \pkg{expl3} for ease of coding. |
| 657 |
% \begin{macrocode} |
| 658 |
\def\enverb@rmeol |
| 659 |
{% |
| 660 |
\ifnum\catcode`\%=14 |
| 661 |
\enverb@chargen{`\%}{12}% |
| 662 |
\expandafter\enverb@stop |
| 663 |
\fi |
| 664 |
\enverb@rmeol@0;% |
| 665 |
\enverb@stop |
| 666 |
} |
| 667 |
\def\enverb@rmeol@#1;% |
| 668 |
{% |
| 669 |
\ifnum#1=128 |
| 670 |
\string\relax |
| 671 |
\expandafter\enverb@stop |
| 672 |
\fi |
| 673 |
\@firstofone |
| 674 |
{% |
| 675 |
\ifnum\catcode#1=14 |
| 676 |
\enverb@chargen{#1}{12}% |
| 677 |
\expandafter\enverb@stop |
| 678 |
\fi |
| 679 |
\expandafter\enverb@rmeol@\the\numexpr#1+1;% |
| 680 |
}% |
| 681 |
} |
| 682 |
% \end{macrocode} |
| 683 |
% \end{macro} |
| 684 |
% |
| 685 |
% \begin{macro}{\enverbExecute} |
| 686 |
% \begin{macrocode} |
| 687 |
\NewDocumentCommand\enverbExecute{} |
| 688 |
{% |
| 689 |
\begingroup |
| 690 |
\newlinechar=`\^^M |
| 691 |
\expandafter |
| 692 |
\endgroup |
| 693 |
\scantokens\expanded |
| 694 |
{{% |
| 695 |
\detokenize\expandafter{\enverbBody}% |
| 696 |
\enverb@rmeol |
| 697 |
}}% |
| 698 |
} |
| 699 |
% \end{macrocode} |
| 700 |
% \end{macro} |
| 701 |
% |
| 702 |
% \begin{macro}{\enverbListing} |
| 703 |
% \begin{macrocode} |
| 704 |
\NewDocumentCommand \enverbListing { m m } |
| 705 |
{% |
| 706 |
\scantokens\expanded |
| 707 |
{{% |
| 708 |
\string\begin{#1}\detokenize{#2}\enverb@othercr |
| 709 |
\detokenize\expandafter{\enverbBody}% |
| 710 |
\string\end{#1}\enverb@othercr |
| 711 |
\enverb@rmeol |
| 712 |
}}% |
| 713 |
} |
| 714 |
% \end{macrocode} |
| 715 |
% \end{macro} |
| 716 |
% |
| 717 |
% \gobbledocstriptag |
| 718 |
%</pkg> |