diff options
author | Norbert Preining <norbert@preining.info> | 2020-02-27 03:01:13 +0000 |
---|---|---|
committer | Norbert Preining <norbert@preining.info> | 2020-02-27 03:01:13 +0000 |
commit | cfcab0823d16bb9acd6d6a7d3f80a7b2176a6260 (patch) | |
tree | 4db3d6f79edb6570d6c812c4962a4322c0605534 /macros/luatex/optex/verbatim.opm | |
parent | 2dfd6df5d1db270dc874c6c319d51eed43defb91 (diff) |
CTAN sync 202002270301
Diffstat (limited to 'macros/luatex/optex/verbatim.opm')
-rw-r--r-- | macros/luatex/optex/verbatim.opm | 265 |
1 files changed, 174 insertions, 91 deletions
diff --git a/macros/luatex/optex/verbatim.opm b/macros/luatex/optex/verbatim.opm index acc7922a8d..121ec0698e 100644 --- a/macros/luatex/optex/verbatim.opm +++ b/macros/luatex/optex/verbatim.opm @@ -2,21 +2,31 @@ \_codedecl \begtt {Verbatim <2020-02-14>} -\_newdimen\_ttindent \_ttindent=\_parindent - % indentation in \begtt...\endtt and \verbinput -\_def\_ttskip{\medskip} % space above and below \begtt, \verbinput -\_mathchardef\_ttpenalty=100 % penalty between lines in \begtt, \verbinput +%> parameters + +\_newdimen\_ttindent \_ttindent=\_parindent % indentation in \begtt...\endtt and \verbinput +\_def\_ttskip{\_medskip} % space above and below \begtt, \verbinput +\_mathchardef\_ttpenalty=100 % penalty between lines in \begtt, \verbinput + +\_newcount\_ttline \_ttline=-1 % last line number in \begtt...\endtt +\_newcount\_viline % last line number in \verbinput +\_newread\_vifile % file fgiven by \verinput +\_newtoks\_tthook % tokens procesed in \begtt...\endtt +\_newtoks\_intthook % toens procesed in \code or in `...` + +\_public \ttline \tthook \intthook ; + +%> \code \_def\_code#1{\_unexpanded\_ea{\_csname _code \_endcsname{#1}}} -\_protected\_sdef{_code }#1{{\_escapechar=-1 \_ea}% - \_ea\_printinverbatim\_ea{\_detokenize{#1}}} -\_def\_printinverbatim#1{\_leavevmode\_hbox{\_tt \_the\_intthook\_relax #1}} +\_protected\_sdef{_code }#1{{\_escapechar=-1 \_tt \_the\_intthook \_relax + \_ea\_printinverbatim\_ea{\_detokenize{#1}}}} +\_def\_printinverbatim#1{\_leavevmode\_hbox{#1}} +\_toksapp \_pdfstringout{\_let\code=\_detokenize \_let\_code=\_detokenize} \_public \code ; -\_newcount\_ttline \_ttline=-1 -\_newcount\_viline -\_newread\_vifile +%> \_setverb \_newcatcodetable \_verbatimcatcodes \_def\_setverb{\_begingroup @@ -28,45 +38,49 @@ \_setverb \_def\_setverb{\_catcodetable\_verbatimcatcodes }% -% \begtt ... \endtt - -\_eoldef \_begtt #1{% - \_par \_vskip\_parskip \_ttskip \_bgroup \_wipeepar - \_setverb \_adef{ }{\ }% - \_parindent=\_ttindent \_parskip=0pt - \_the\_tthook #1% - \_ifnum\_ttline<0 \_else - \_tenrm \_thefontscale[700]\_ea\_let\_ea\_sevenrm\_the\_font - \_toksapp\_everypar{\_global\_advance\_ttline by1 \_printttline}\_fi - \_def\par##1{\_endgraf\_ifx##1\_egroup\_else\_penalty\_ttpenalty\_leavevmode\_fi ##1}% - \_obeylines \_startverb} - -\_catcode`\|=0 \_catcode`\\=12 - |_def|_startverb#1\endtt{|_tt|_the|_ptthook#1|_endtt} -|_normalcatcodes - -\def\_endtt{\_egroup\_par\_ttskip\_testparA} -\_def\_testparA{\_ea\_testparB\_romannumeral-`\.} -\_def\_testparB{\_futurelet\_tmpa\_testparC} -\_def\_testparC{\_ifx\_tmpa\_par\_else\afternoindent\_fi} - -\_def\_printttline{\_llap{\_sevenrm\_the\_ttline\_kern.9em}} - -\_newtoks\_tthook -\_newtoks\_ptthook -\_newtoks\_intthook +%> \activettchar \_def\_activettchar#1{% \_ifx\_savedttchar\_undefined\_else \_catcode\_savedttchar=\_savedttcharc \_fi - \_chardef\_savedttchar=`#1% - \_chardef\_savedttcharc=\_catcode`#1% - \_adef{#1}{{\_setverb\_adef{ }{\ }\_catcode`#1=13 \_ea}\_readverb}% - \_bgroup\_lccode`\~=`#1\_lowercase{\_egroup\_def\_readverb ##1~}{\_printinverbatim{##1}}% - \_catcode`#1=13 + \_chardef\_savedttchar=`#1 + \_chardef\_savedttcharc=\_catcode`#1 + \_adef{#1}{\_begingroup \_setverb \_adef{ }{\ }\_tt \_the\_intthook\_relax \_readverb}% + \_def\_readverb ##1#1{\_printinverbatim{##1}\_endgroup}% } +\_public \activettchar ; -\_newcount\_viline -\_newread\_vifile +%> \begtt ... \endtt + +\_eoldef \begtt#1{\_par \_wipeepar + \_begingroup + \_vskip\_parskip \_ttskip + \_setverb + \_ifnum\_ttline<0 \_let\_printverblinenum=\_relax \_else \_initverblinenum \_fi + \_adef{ }{\ }\_parindent=\_ttindent \_parskip=0pt + \_the\_tthook #1\_relax \_tt + \_endlinechar=`^^J + \_startverb +} +\_ea\_def\_ea\_startverb \_ea#\ea1\_csstring\\endtt#2^^J{% + \_prepareverbdata\_tmpb{#1^^J}% + \_ea\_printverb \_tmpb\_end + \_par\_ttskip + \_endgroup + \_noindent \_setbox0=\_lastbox +} +\_def\_printverb #1^^J#2{\_ifx\_end#2 + \_bgroup \_adef{ }{}\_if\_relax#1\_relax\_egroup \_else\_egroup \_printverbline{#1}\_fi + \_else + \_printverbline{#1}% + \_ea \_printverb \_ea #2% + \_fi +} +\_def\_prepareverbdata#1#2{\_def#1{#2}} +\_def\_printverbline#1{\_penalty \_ttpenalty \_indent \_printverblinenum #1 \par} +\_def\_initverblinenum{\_tenrm \_thefontscale[700]\_ea\_let\_ea\_sevenrm\_the\_font} +\_def\_printverblinenum{\_global\_advance\_ttline by1 \_llap{\_sevenrm \_the\_ttline\_kern.9em}} + +%> \verbinput \_def\_verbinput (#1) #2 {\_par \_def\_tmpa{#2}% \_ifx\_vifilename\_tmpa \_else @@ -114,76 +128,145 @@ \_else \_edef\_vinolines{\_the\_tmpnum}% \_fi - \_vskip\_parskip \_ttskip \_bgroup \_wipeepar - \_setverb \_adef{ }{\ }% - \_ifx\_savedttchar\_undefined \_else \_catcode\_savedttchar=12 \_fi - \_parindent=\_ttindent \_parskip=0pt - \_the\_tthook\_relax - \_ifnum\_ttline<-1 \_else - \_tenrm \_thefontscale[700]\_ea\_let\_ea\_sevenrm\_the\_font - \_everypar\_ea{\_the\_everypar \_glob\_advance\_ttline by1 \_printttline}\_fi - \_def\par##1{\_endgraf\_ifx##1\_egroup\_else\_penalty\_ttpenalty\_leavevmode\_fi ##1}% - \_obeylines \_tmpnum=0 \_lccode`\~=`\^^M \_lowercase{\_def\_tmpb{~}}% + \_vskip\_parskip \_ttskip \_wipeepar + \_begingroup + \_ifnum\_ttline<-1 \_let\_printverblinenum=\_relax \_else \_initverblinenum \_fi + \_setverb \_adef{ }{\ }\_parindent=\_ttindent \_parskip=0pt + \_the\_tthook\_relax \_tt + \_endlinechar=`^^J \_tmpnum=0 \_loop \_ifeof\_vifile \_tmpnum=\_vinolines\_space \_fi \_ifnum\_tmpnum<\_vinolines\_space - \_vireadline \_advance\_tmpnum by1 \_repeat %% skip line - \_ifnum\_ttline=-1 \_ttline=\_viline \_let\_glob=\_relax \_else\_let\_glob=\_global \_fi - \_tmpnum=0 \_ifnum\_vidolines=0 \_tmpnum=-1 \_fi + \_vireadline \_advance\_tmpnum by1 \_repeat %% skip lines + \_edef\_ttlinesave{\_ttline=\_the\_ttline}% + \_ifnum\_ttline=-1 \_ttline=\_viline \_fi + \_tmpnum=0 \_def\_tmpb{}% + \_ifnum\_vidolines=0 \_tmpnum=-1 \_fi \_ifeof\_vifile \_tmpnum=\_vidolines\_space \_fi \_loop \_ifnum\_tmpnum<\_vidolines\_space \_vireadline - \_ifeof\_vifile \_tmpnum=\_vidolines\_space \_else \_viprintline \_fi %% print line - \_ifnum\_vidolines=0 \_else\_advance\_tmpnum by1 \_fi + \_ifnum\_vidolines=0 \_else\_advance\_tmpnum by1 \_fi + \_ifeof\_vifile \_tmpnum=\_vidolines\_space \_else \_visaveline \_fi %% save line \_repeat - \_tt\_ea\_the\_ea\_ptthook\_tmpb\_egroup\_par\_ttskip\_testparA + \_ea\_prepareverbdata \_ea \_tmpb\_ea{\_tmpb^^J}% + \_ea\_printverb \_tmpb\_end + \_global\_ttlinesave + \_par\_ttskip + \_endgroup + \_noindent \_setbox0=\_lastbox } \_def\_vireadline{\_read\_vifile to \_tmp \_global\_advance\_viline by1 } -\_def\_viprintline{\_ea\_addto\_ea\_tmpb\_ea{\_tmp}} +\_def\_visaveline{\_ea\_addto\_ea\_tmpb\_ea{\_tmp}} \_public - \begtt \activettchar \tthook \ptthook \intthook \verbinput ; - + \verbinput ; \_endcode %--------------------------------------- -`\code{text}` is a macro which writes verbatim-like inline text and it is -robust to use it in parameters of macros or section names etc. User have to -escape the following characters: percent, hash mark, escape, space (if there -are more than one consecutive spaces) and `{`, `}` (if these braces are -unbalanced in the `\code` parameter). For example +\secc Implementation notes -\begtt - \code{\\text, \%\#} ... \text, %# - \code{@{..}*&^$ $} ... prints the same without escaping, but you can - escape these characters too, if you want. - \code{a \ b} ... two spaces between a b, the second one must be escaped - \code{xy\{z} ... unbalanced brace must be escaped -\endtt +\%> parameters -If you set a character as `\activettchar` then it can be used as boundary -character of inline code. The text betweent these boundary characters are -printed without any change. For example -\code{\`}`\print % whatever #&`\code{\`} does `\print % whatever #&` -if `\activettchar`\code{\`} was declared. +The parameters for verbatim macros are set. `\ttline` is set to $-1$ by +default, so `\begtt...\endtt` are not numbered but `\verbinput` are with +numbers. -But this in/line verbatim text cannot be used inside parameters of -macros, for example inside section names. Use `\code` in such situation. +\%> \code +`\code{<text>}` expands to `\detokenize{<text>}` when `\escapechar=-1`. In +order to do it more robust when it is used in `\write` then it expands as +noexpanded `\code<space>` (followed by space in its csname). This macro doew +really the work. -\secc Implementation notes +The `\_printinverbatim{<text>}` macro is used for `\code{<text>}` printing and for +\code{`}<text>\code{`} printing. It is defined as `\_hbox`, so the in-verbatim <text> +will be never broken. But you can re-define this macro. + +When `\code` occurs in PDF outlines then it does the same as `\detokenize`. +The macro for preparing outlines sets `\escapechar` to $-1$ and uses +\_pdftringout` token list before `\edef`. + +The `\code` is not `\_proteced` because we want that it expands to +`\_unexpanded{\code<space>{<text>}` in `\write` parameters. This protect the +expansions of the `\code` parameter (like `\\`, `\^` etc.). + +\%> \_setverb + +The `\_setverb` macro sets all catcodes to \"verbatim mode". It should be used only +in a group, so we prepare a new catcode table with \"verbatim" catcodes and we define +it as `\_catcodetable\_verbatimcatcodes`. After the group is finished then +original catcode table is restored. + +\%> \activettchar -If the code is used in non-detokenized `\write` parameter, we must to -`detokenize` its parameter and write `\code` as prefix again. The second -(internal) `\code` is a macro with the name followed by a space. So +This macro saves original catcode of previously declared `\catcodettchar` (if +such character was decalred) from `\_savedttchar` and `\_savedttcharc` +values. Then new such values are stored. The declared charater is activated +by `\_adef` as a macro (active character) which opens a group, +does `\_setverb` and other settings and reads its parameter until second the same +character. This is done by `\_readverb` macro. Finally it prints scanned +<text> by `\_printinverbatim` and closes group. Suppose that `\activettchar"` is +used. Then the following work is schematically done: \begtt - \code{\\aha\%} ... writes: \code {\\aha\%} into workong file. +\_def "{\_begingroup \_setverb ... \_readverb} +\_def \_readverb #1"{\_printinverbatim{#1}\_endgroup} \endtt +% +Note that the second occurence of `"` is not active because `\_setverb` +deactivates it. + +\%> \begtt ... \endtt + +`\begtt` id defined only as public. We don't need private `\_begtt` variant. +This macro os defined by `\eoldef`, sot user can put an parameter at the +same line where `\begtt` is. This `#1` parameter is used after globap +parameters settings, so user can change it locally. + +The `\begtt` macro opens group, does `\_setverb` and another preprocessing, sets +`\endlinechar` to `^^J` and reads the following text in verbatim mode +until `\endtt` occurs. This scanning is done by `\_startverb` macro which is +edfined as: + +\begtt +\_def\_startverb #1\endtt #2^^J{...} +\endtt +% +We must to ensure that the backslash in `\endtt` has category 12 (this is a +reason of the `\ea` chain in real code). +The `#2` is something between `\endtt` and end of line and it is simply +ignored. + +The `\startverb` puts the scanned data to `\_prepareverbdata`. It sets the data +to `\_tmpb` without changes by default, but you should re-define it in order +to does s special changes (for example colorization of the code), if you +want. The scanned data have `^^J` at each end of line and all spaces are +active characters. Other characters have nomal category 11 or 12. + +When `\prepareverbdata` finishes then `\startverb` runs `\_printverb` loop +over aech line of the data and does a final work: last skip plus `\noindent` +without first indentation box. This trick keeps horizontal mode without +indentation when the empty line after `\endtt` does not exists. But it stops +horizontal mode without any new box in outer vertical mode if `\par` is +processed immediately after `\endtt`. + +The `\_printverb` macro calls `\_printverbline{<line>}` to each scanned line of +verbatim text. This macro expect that it strarts in vertical mode ant it must +do `\par` in order to return the vertical mode. The `\_printverblinenum` +is used here: it does nothing when `\_ttline<0` else it prints the line +number using `\_llap`. -This can be read again without any chnge of meaning of the parameter (the -second space after `\code` sequence will be ingnored). +\%> \verbinput -If the `\code` is used in normal horizontal mode, then escape characters are -ignored using `\ecsapechar=-1 \detokenize`. +Macro `\verbinput` uses a file read perviously or opens the given file. Then +it runss the parameter scanning by `\_viscanparameter` and `\_viscanminus`. +Finally the `\_doverbinput` is run. At begining of `\_doverbinput` we have +`\_viline`= number of lines already read using previous `\verbinput`, +`\_vinolines`= the number of lines we need to skip and `\_vidolnes`= the +number of lines we need to print. +After group is opened then similar preparation is done as in `\begtt`. Then +we skip `\_nolines` lines in a loop a and we read `\_dolines` lines. The +read data is accumulated into `\_tmpb` macro. The next steps are equal to +the steps done in `\_startverb` macro: data are processed via +`\_prepareverbdata` and printed via `\_printverb` loop. |