Debian Codegraph @

texmf-dist/tex/optex/base/verbatim.opm

raw
1 %% This is part of the OpTeX project, see http://petr.olsak.net/optex
2
3 \_codedecl \begtt {Verbatim <2026-05-20>} % preloaded in format
4
5 \_doc ----------------------------
6 The internal parameters
7 \`\_ttskip`, \`\_ttpenalty`, \`\_viline`, \`\_vifile` and \`\_ttfont`
8 for verbatim macros are set.
9 \_cod ----------------------------
10
11 \_def\_ttskip{\_medskip} % space above and below \begtt, \verbinput
12 \_mathchardef\_ttpenalty=100 % penalty between lines in \begtt, \verbinput
13 \_newcount\_viline % last line number in \verbinput
14 \_newread\_vifile % file given by \verinput
15 \_def\_ttfont{\_tt} % default tt font
16
17 \_doc ----------------------------
18 \`\code``{<text>}` expands to `\detokenize{<text>}` when `\escapechar=-1`. In
19 order to do it more robust when it is used in `\write` then it expands as
20 noexpanded `\code<space>` (followed by space in its csname). This macro
21 does the real work.
22
23 The \`\_printinverbatim``{<text>}` macro is used for `\code{<text>}` printing and for
24 \code{`}<text>\code{`} printing. It is defined as `\hbox`, so the in-verbatim <text>
25 will be never broken. But you can re-define this macro.
26
27 When `\code` occurs in PDF outlines then it does the same as `\detokenize`.
28 The macro for preparing outlines sets `\escapechar` to $-1$ and uses
29 \^`\_regoul` token list before `\edef`.
30
31 The `\code` is not `\proteced` because we want it expands to
32 `\unexpanded{\code<space>{<text>}}` in `\write` parameters. This protect the
33 expansions of the `\code` parameter (like `\\`, `\^` etc.).
34 \_cod ----------------------------
35
36 \_def\_code#1{\_unexpanded\_ea{\_csname _code \_endcsname{#1}}}
37 \_protected\_sdef{_code }#1{{\_escapechar=-1 \_ttfont \_the\_everyintt \_relax
38 \_ea\_printinverbatim\_ea{\_detokenize{#1}}}}
39 \_def\_printinverbatim#1{\_leavevmode\_hbox{#1}}
40
41 \_regmacro {}{}{\_let\code=\_detokenize \_let\_code=\_detokenize}
42 \_public \code ;
43
44 \_doc ----------------------------
45 The \`\_setverb` macro sets all catcodes to \"verbatim mode". It should be used only
46 in a group, so we prepare a new catcode table with \"verbatim" catcodes and we define
47 it as\nl `\_catcodetable`\`\_verbatimcatcodes`. After the group is finished then
48 original catcode table is restored.
49 \_cod ----------------------------
50
51 \_newcatcodetable \_verbatimcatcodes
52 \_def\_setverb{\_begingroup
53 \_def\do##1{\_catcode`##1=12 }
54 \_dospecials
55 \_savecatcodetable\_verbatimcatcodes % all characters are normal
56 \_endgroup
57 }
58 \_setverb
59 \_def\_setverb{\_catcodetable\_verbatimcatcodes }%
60
61 \_doc ----------------------------
62 \`\verbchar``<char>` saves original catcode of previously declared `<char>` (if
63 such character was declared) using \`\_savedttchar` and \`\_savedttcharc`
64 values. Then new such values are stored. The declared character is activated
65 by `\_adef` as a macro (active character) which opens a group,
66 does `\_setverb` and other settings and reads its parameter until second the same
67 character. This is done by the \`\_readverb` macro. Finally, it prints scanned
68 `<text>` by \^`\_printinverbatim` and closes group. Suppose that `\verbchar"` is
69 used. Then the following work is schematically done:
70 \begtt
71 \_def "{\_begingroup \_setverb ... \_readverb}
72 \_def \_readverb #1"{\_printinverbatim{#1}\_endgroup}
73 \endtt
74 Note that the second occurrence of `"` is not active because `\_setverb`
75 deactivates it.
76 \_cod ----------------------------
77
78 \_def\_verbchar#1{%
79 \_ifx\_savedttchar\_undefined\_else \_catcode\_savedttchar=\_savedttcharc \_fi
80 \_chardef\_savedttchar=`#1
81 \_chardef\_savedttcharc=\_catcode`#1
82 \_adef{#1}{\_begingroup \_setverb \_adef{ }{\_dsp}\_ttfont \_the\_everyintt\_relax \_readverb}%
83 \_def\_readverb ##1#1{\_printinverbatim{##1}\_endgroup}%
84 }
85 \_let \_activettchar=\_verbchar % for backward compatibility
86 \_public \verbchar \activettchar ;
87
88 \_doc ----------------------------
89 \`\begtt` is defined only as public. We don't need a private `\_begtt` variant.
90 This macro opens a group and sets `%` as an active character (temporary).
91 This will allow it to be used as the comment character at the same line after
92 `\begtt`. Then \`\_begtti` is run. It is defined by \^`\eoldef`,
93 so users can put a parameter at the
94 same line where `\begtt` is. This `#1` parameter is used after \^`\everytt`
95 parameters settings, so users can change them locally.
96
97 A user can write `\begtt \ttline=0` and we want to interpret this setting
98 locally too. This is the reason of creating the \`\_restorettline` macro
99 before the argument `#1` of `\_begtti` is processed and using the trick
100 `\ifnum\ttline=\maxdimen` after this processing. See the code below.
101
102 The \^`\_begtti` macro does \^`\_setverb` and another preprocessing, sets
103 `\endlinechar` to `^^J` and reads the following text in verbatim mode
104 until \`\endtt` occurs. This scanning is done by \`\_startverb` macro which is
105 defined as:
106 \begtt \adef/{\bslash}
107 \_def\_startverb #1/endtt #2^^J{...}
108 \endtt
109 We must to ensure that the backslash in `\endtt` has category 12 (this is a
110 reason of the `\ea` chain in real code).
111 The `#2` is something between `\endtt` and the end of the same line and it is simply
112 ignored.
113
114 The `\_startverb` puts the scanned data to \`\_prepareverbdata`. It sets the data
115 to `\_tmpb` without changes by default, but you should re-define it in order
116 to do special changes if you want. (For example, \^`\hisyntax` redefines
117 this macro.) The scanned data have `^^J` at each end of line and all spaces are
118 active characters (defined as {\visiblesp`\ `}).
119 Other characters have normal category 11 or 12.
120
121 The `^^J` is appended to verbatim data because we need to be sure that
122 the data are finished by this character. When `\endtt` is preceded by
123 spaces then we need to close these spaces by `^^J` and such line is not
124 printed due to a trick used in \^`\_printverb`.
125
126 When `\_prepareverbdata` finishes then `\_startverb` runs `\_printverb` loop
127 over each line of the data and does a final work: last skip plus `\noindent`
128 in the next paragraph.
129 \_cod ---------------------------
130
131 \_def\begtt{\_par \_begingroup \_adef\%##1\_relax{\_relax}\_begtti}
132 \_eoldef \_begtti#1{\_wipeepar \_setxhsize
133 \_vskip\_parskip \_ttskip
134 \_setverb
135 \_adef{ }{\_dsp}\_adef\^^I{\t}\_parindent=\_ttindent \_parskip=0pt
136 \_def\t{\_hskip \_dimexpr\_tabspaces em/2\_relax}%
137 \_protrudechars=0 % disable protrusion
138 \_xdef\_restorettline{\_global\_ttline=\_the\_ttline\_relax}\_ttline=\_maxdimen
139 \_the\_everytt \_relax #1\_relax
140 \_ifnum\_ttline=\_maxdimen \_restorettline \_else \_aftergroup\_restorettline \_fi
141 \_ifnum\_ttline<0 \_let\_printverblinenum=\_relax \_else \_initverblinenum \_fi
142 \_def\_testcommentchars##1\_iftrue{\_iffalse}\_let\_hicomments=\_relax
143 \_ttfont \_savemathsb \_endlinechar=`^^J
144 \_startverb
145 }
146 \_ea\_def\_ea\_startverb \_ea#\_ea1\_csstring\\endtt#2^^J{%
147 \_prepareverbdata\_tmpb{#1^^J}%
148 \_ea\_printverb \_tmpb\_fin
149 \_par \_restoremathsb
150 \_endgroup \_ttskip
151 \_isnextchar\_par{}{\_noindent}%
152 }
153 \_def\_prepareverbdata#1#2{\_def#1{#2}}
154
155 \_doc
156 The \`\_printverb` macro calls \^`\_printverbline``{<line>}` repeatedly
157 to each scanned line of verbatim text. The `\_printverb` is used from
158 `\begtt...\endtt` and from `\verbinput` too.
159
160 The \^`\_testcommentchars` replaces the following `\_iftrue` to
161 `\_iffalse` by default unless the \~`\commentchars` are set. So, the main body
162 of the loop is written in the `\_else` part of the `\_iftrue` condition.
163 The \^`\_printverbline``{<line>}` is called here.
164
165 The \`\_printverbline``{<line>}` expects that it starts in vertical mode and it must
166 do `\par` to return the vertical mode. The \`\_printverblinenum`
167 is used here: it does nothing when `\_ttline`\code{<0} else it prints the line
168 number using `\_llap`.
169
170 \`\_putttpenalty` puts \^`\_ttpenalty` before second and next lines, but
171 not before first line in each `\begtt...\endtt` environment.
172
173 \`\_nograb``{<line_number>}` allows to select only code but not <line_number>s
174 by mouse in several PDF viewers.
175
176 The `\_ttline` is increased here in the `\_printverb` macro because of
177 comments-blocks: the `\_prinverbline` is not processed in comments-blocks
178 but we need to count the `\_ttline`.
179 \_cod ----------------------------
180
181 \_def\_printverb #1^^J#2{%
182 \_ifx\_printverblinenum\_relax \_else \_incr\_ttline \_fi
183 \_testcommentchars #1\_relax\_relax\_relax
184 \_iftrue
185 \_ifx\_fin#2\_printcomments\_fi
186 \_else
187 \_ifx\_vcomments\_empty\_else \_printcomments \_def\_vcomments{}\_fi
188 \_ifx\_fin#2%
189 \_bgroup \_adef{ }{}\_def\t{}% if the last line is empty, we don't print it
190 \_ifcat&#1&\_egroup \_ifx\_printverblinenum\_relax \_else \_decr\_ttline \_fi
191 \_else\_egroup \_printverbline{#1}\_fi
192 \_else
193 \_printverbline{#1}%
194 \_fi
195 \_fi
196 \_unless\_ifx\_fin#2\_afterfi{\_printverb#2}\_fi
197 }
198 \_def\_printverbline#1{\_putttpenalty \_indent \_printverblinenum \_kern\_ttshift #1\_par}
199 \_def\_initverblinenum{{\_tenrm \_thefontscale[700]\_ea}\_ea\_let\_ea\_sevenrm\_the\_font}
200 \_def\_printverblinenum{\_llap{\_sevenrm \_nograb{\_the\_ttline}\_kern.9em}}
201 \_def\_putttpenalty{\_def\_putttpenalty{\_penalty\_ttpenalty}}
202 \_def\_nograb#1{\_pdfliteral page{/Span<</ActualText<>>>BDC}#1\_pdfliteral page{EMC}}
203
204 \_doc ----------------------------
205 Macro \`\verbinput` uses a file read previously or opens the given file. Then
206 it runs the parameter scanning by \`\_viscanparameter` and \`\_viscanminus`.
207 Finally the \`\_doverbinput` is run. At the beginning of `\_doverbinput`, we have
208 `\_viline`= number of lines already read using previous `\verbinput`,
209 `\_vinolines`= the number of lines we need to skip and `\_vidolnes`= the
210 number of lines we need to print.
211 A similar preparation is done as in `\begtt` after the group is opened. Then
212 we skip \`\_vinolines` lines in a loop a and we read \`\_vidolines` lines. The
213 read data is accumulated into `\_tmpb` macro. The next steps are equal to
214 the steps done in \^`\_startverb` macro: data are processed via
215 \^`\_prepareverbdata` and printed via \^`\_printverb` loop.
216 \_cod \_fin ----------------------
217
218 \_def\_verbinput #1(#2) #3 {\_par \_def\_tmpa{#3}%
219 \_def\_tmpb{#1}% cmds used in local group
220 \_ifx\_vifilename\_tmpa \_else
221 \_openin\_vifile={#3}%
222 \_global\_viline=0 \_glet\_vifilename=\_tmpa
223 \_ifeof\_vifile
224 \_opwarning{\_string\verbinput: file "#3" unable to read}
225 \_ea\_ea\_ea\_skiptorelax
226 \_fi
227 \_fi
228 \_viscanparameter #2+\_relax
229 }
230 \_def\_skiptorelax#1\_relax{}
231
232 \_def \_viscanparameter #1+#2\_relax{%
233 \_if$#2$\_viscanminus(#1)\_else \_viscanplus(#1+#2)\_fi
234 }
235 \_def\_viscanplus(#1+#2+){%
236 \_if$#1$\_tmpnum=\_viline
237 \_else \_ifnum#1<0 \_tmpnum=\_viline \_advance\_tmpnum by-#1
238 \_else \_tmpnum=#1
239 \_advance\_tmpnum by-1
240 \_ifnum\_tmpnum<0 \_tmpnum=0 \_fi % (0+13) = (1+13)
241 \_fi \_fi
242 \_edef\_vinolines{\_the\_tmpnum}%
243 \_if$#2$\_def\_vidolines{0}\_else\_edef\_vidolines{#2}\_fi
244 \_doverbinput
245 }
246 \_def\_viscanminus(#1-#2){%
247 \_if$#1$\_tmpnum=0
248 \_else \_tmpnum=#1 \_advance\_tmpnum by-1 \_fi
249 \_ifnum\_tmpnum<0 \_tmpnum=0 \_fi % (0-13) = (1-13)
250 \_edef\_vinolines{\_the\_tmpnum}%
251 \_if$#2$\_tmpnum=0
252 \_else \_tmpnum=#2 \_advance\_tmpnum by-\_vinolines \_fi
253 \_edef\_vidolines{\_the\_tmpnum}%
254 \_doverbinput
255 }
256 \_def\_doverbinput{%
257 \_tmpnum=\_vinolines
258 \_advance\_tmpnum by-\_viline
259 \_ifnum\_tmpnum<0
260 \_openin\_vifile={\_vifilename}%
261 \_global\_viline=0
262 \_else
263 \_edef\_vinolines{\_the\_tmpnum}%
264 \_fi
265 \_vskip\_parskip \_ttskip \_wipeepar \_setxhsize
266 \_begingroup
267 \_setverb \_adef{ }{\_dsp}\_adef\^^I{\t}\_parindent=\_ttindent \_parskip=0pt
268 \_def\t{\_hskip \_dimexpr\_tabspaces em/2\_relax}%
269 \_protrudechars=0 % disable protrusion
270 \_xdef\_restorettline{\_global\_ttline=\_the\_ttline\_relax}\_ttline=\_maxdimen
271 \_the\_everytt\_relax \_tmpb\_relax
272 \_ifnum\_ttline=\_maxdimen \_restorettline \_else \_aftergroup\_restorettline \_fi
273 \_ifnum\_ttline<-1 \_let\_printverblinenum=\_relax \_else \_initverblinenum \_fi
274 \_ttfont \_savemathsb \_endlinechar=`^^J \_tmpnum=0
275 \_loop \_ifeof\_vifile \_tmpnum=\_vinolines\_space \_fi
276 \_ifnum\_tmpnum<\_vinolines\_space
277 \_vireadline \_advance\_tmpnum by1 \_repeat %% skip lines
278 \_edef\_ttlinesave{\_global\_ttline=\_the\_ttline}%
279 \_ifnum\_ttline=-1 \_ttline=\_viline \_else \_let\_ttlinesave=\_relax \_fi
280 \_tmpnum=0 \_def\_tmpb{}%
281 \_ifnum\_vidolines=0 \_tmpnum=-1 \_fi
282 \_ifeof\_vifile \_tmpnum=\_vidolines\_space \_fi
283 \_loop \_ifnum\_tmpnum<\_vidolines\_space
284 \_vireadline
285 \_ifnum\_vidolines=0 \_else\_advance\_tmpnum by1 \_fi
286 \_ifeof\_vifile \_tmpnum=\_vidolines\_space \_else \_visaveline \_fi %% save line
287 \_repeat
288 \_ea\_prepareverbdata \_ea \_tmpb\_ea{\_tmpb^^J}%
289 \_catcode`\ =10 \_catcode`\%=9 % used in \commentchars comments
290 \_ea\_printverb \_tmpb\_fin
291 \_ttlinesave
292 \_par \_restoremathsb
293 \_endgroup
294 \_ttskip
295 \_isnextchar\_par{}{\_noindent}%
296 }
297 \_def\_vireadline{\_read\_vifile to \_tmp \_incr\_viline }
298 \_def\_visaveline{\_ea\_addto\_ea\_tmpb\_ea{\_tmp}}
299
300 \_public \verbinput ;
301
302 \_doc -----------------------------
303 \`\_savemathsb`, \`\_restoremathsb` pair is used
304 in \^`\begtt`...\^`\endtt` or in \^`\verbinput` to temporary suppress
305 the \^`\mathsbon` because we don't need to print `\int _a`
306 in verbatim mode if `\int``_a` is really written. The \^`\_restoremathsb`
307 is defined locally as \^`\mathsbon` only if it is needed.
308 \_cod -----------------------------
309
310 \_def\_savemathsb{\_ifmathsb \_mathsboff \_def\_restoremathsb{\_mathsbon}\_fi}
311 \_def\_restoremathsb{}
312
313 \_doc -----------------------------
314 If the language of your code\label[commentchars]\wlabel{}
315 printed by \^`\verbinput` supports the
316 format of comments started by two characters from the beginning of the
317 line then you can set these characters by \^`\commentchars``<first><second>`.
318 Such comments are printed in the non-verbatim mode without these two
319 characters and they look like the verbatim printing is interrupted
320 at the places where such comments are.
321 See the section~\ref[lua] for good illustration.
322 The file `optex.lua` is read by a single command
323 `\verbinput (4-) optex.lua` here and the `\commentchars --` was set before it.
324
325 If you need to set a special character by \^`\commentchars` then you must
326 to set the catcode to 12 (and space to 13). Examples:
327 \begtt
328 \commentchars // % C++ comments
329 \commentchars -- % Lua comments
330 {\catcode`\%=12 \_ea}\commentchars %% % TeX comments
331 {\catcode`\#=12 \catcode`\ =13 \_ea}\commentchars#{ } % bash comments
332 \endtt
333
334 There is one limitation when \TeX/ interprets the comments declared by
335 \^`\commentchars`. Each block of comments is accumulated to one line
336 and then it is re-interpreted by \TeX. So, the ends of lines in the
337 comments block are lost. You cannot use macros which
338 need to scan end of lines, for example `\begtt...\endtt` inside the comments.
339 The character `%` is ignored in comments but you can use `\%`
340 for printing or `%` alone for de-activating `\_endpar` from empty
341 comment lines.
342
343 Implementation: The \`\commentchars``<first><second>` redefines the \`\_testcommentchars`
344 used in \^`\_printverb` in order to it removes the following `\_iftrue`
345 and returns `\_iftrue` or `\_iffalse` depending on the fact that
346 the comment characters are or aren't present at the beginning of tested line.
347 If it is true (`\ifnum` expands to `\ifnum 10>0`) then the rest of the
348 line is added to the \`\_vcomments` macro.
349
350 The \`\_hicomments` is `\relax` by default but it is redefined by \^`\commentchars`
351 in order to keep no-colorized comments if we need to use feature from
352 \^`\commentchars`.
353
354 The accumulated comments are printed whenever the non-comment line
355 occurs. This is done by \`\_printcomments` macro.
356 You can re-define it, but the main idea must be kept: it is printed in the
357 group, `\_reloding \_rm` initializes normal font, `\catcodetable0`
358 returns to normal catcode table used before `\verbinput` is started, and
359 the text accumulated in `\_vcomments` must be printed by
360 `\_scantextokens` primitive.
361 \_cod -----------------------------
362
363 \_def\_vcomments{}
364 \_let\_hicomments=\_relax
365
366 \_def\_commentchars#1#2{%
367 \_def\_testcommentchars ##1##2##3\_relax ##4\_iftrue{\_ifnum % not closed in this macro
368 \_ifx #1##1\_ifx#2##21\_fi\_fi 0>0
369 \_ifx\_relax##3\_relax \_addto\_vcomments{\_endgraf}% empty comment=\enfgraf
370 \_else \_addto\_vcomments{##3 }\_fi}%
371 \_def\_hicomments{\_replfromto{\b\n#1#2}{^^J}{\w{#1#2####1}^^J}}% used in \hisyntax
372 }
373 \_def\_testcommentchars #1\_iftrue{\_iffalse} % default value of \_testcommentchar
374 \_def\_printcomments{\_ttskip
375 {\_catcodetable0 \_rm \_everypar={}%
376 \_noindent \_ignorespaces \_scantextokens\_ea{\_vcomments}\_par}%
377 \_ttskip
378 }
379 \_public \commentchars ;
380
381 \_doc -----------------------------
382 The \`\visiblesp` sets spaces as visible characters \char9251.
383 It redefines the \^`\_dsp`, so it is useful for verbatim modes only.
384
385 The \`\_dsp` is equivalent to {\visiblesp`\ `} primitive.
386 It is used in all verbatim environments: spaces are active and defined as
387 `\_dsp` here.
388 \_cod -----------------------------
389
390 \_def \_visiblesp{\_ifx\_initunifonts\_relax \_def\_dsp{\_char9251 }%
391 \_else \_def\_dsp{\_char32 }\_fi}
392 \_let\_dsp=\ % primitive "direct space"
393
394 \_public \visiblesp ;
395
396 \_endcode
397
398 \_endinput
399
400 History:
401 2026-05-20 ... \_sevenrm by local \_thefontscale, collision with OpTeX trick 0159 fixed
402 2025-12-02 ... \_nograb introduced, see aso https://github.com/olsak/OpTeX/pull/215
403 2024-11-05 ... \ttline=0 is local in \begtt, \verbinput, \everytt parameters
404 2022-04-23 ... \_ttline counting: bug fixed
405 2022-02-22 ... \_reloading removed due to changes in font-select
406 2021-04-18 ... \_protrudechars=0 added
407 2021-04-07 ... \_savemathsb, \_restoremathsb introduced
408 2021-01-22 ... \activettchar changed to \verbchar
409 2020-12-30 ... \secc followed by \begtt must be unbreakable
410 2020-11-13 ... \commentchars implemented
411 2020-04-22 ... \ttshift introduced
412 2020-04-06 ... \visiblesp added
413 2020-04-04 ... ^^I activated as \t for multiline verbatim
414 \verbinput <cmds> (...) <filename>, <cmds> added.