Debian Codegraph @

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

raw
1 %% This is part of the OpTeX project, see http://petr.olsak.net/optex
2
3 \_codedecl \printdoc {Macros for documentation printing <2025-11-07>} % loaded on demand by \load[doc]
4
5 \_doc -----------------------------
6 General declarations.
7 \_cod -----------------------------
8
9 \_fontfam[lmfonts]
10
11 \_let \mlinkcolor=\Red % main doc. points
12 \_let \ulinkcolor=\Blue % user doc. points
13 \_let \fnamecolor=\Brown % file names in listing headers
14 \_def \bgverbcolor {\_setcmykcolor{0 0 .3 .03}} % background for listings
15 \_def \outlinkcolor {\_setcmykcolor{1 0 1 .2}} % green for outerlinks
16 \_def \inlinkcolor {\_setcmykcolor{0 1 0 .1}} % magenta for internal links
17 \_hyperlinks \inlinkcolor \outlinkcolor
18 \_enlang
19 \_enquotes
20
21 \_doc -----------------------------
22 Maybe, somebody needs `\seccc` or `\secccc`?
23 \_cod -----------------------------
24
25 \_eoldef\seccc#1{\_medskip \_noindent{\_bf#1}\_par\_nobreak\_firstnoindent}
26 \_def\secccc{\_medskip\_noindent $\_bullet$ }
27
28 \_doc -----------------------------
29 `\enddocument` can be redefined.
30 \_cod -----------------------------
31
32 \_let\enddocument=\_bye
33
34 \_doc -----------------------------
35 A full page of listing causes underfull `\vbox` in output routine.
36 We need to add a small tolerance.
37 \_cod -----------------------------
38
39 \_pgbottomskip=0pt plus10pt minus2pt
40
41 \_doc -----------------------------
42 The listing mode is implemented here. The \`\maxlines`
43 is maximal lines of code printed in the listing mode.
44 \_cod -----------------------------
45
46 \_newcount \_maxlines \_maxlines=100000
47 \_public \maxlines ;
48
49 \_eoldef\_cod#1{\_par \_wipeepar
50 \_vskip\_parskip \_medskip \_ttskip
51 \_begingroup
52 \_typosize[8/10]
53 \_let\_printverbline=\_printcodeline
54 \_ttline=\_inputlineno
55 \_setverb
56 \_ifnum\_ttline<0 \_let\_printverblinenum=\_relax \_else \_initverblinenum \_fi
57 \_adef{ }{\ }\_adef\^^I{\t}\_parindent=\_ttindent \_parskip=0pt
58 \_def\t{\_hskip \_dimexpr\_tabspaces em/2\_relax}%
59 \_relax \_ttfont
60 \_endlinechar=`^^J
61 \_def\_tmpb{\_start}%
62 \_readverbline
63 }
64 \_def\_readverbline #1^^J{%
65 \_def\_tmpa{\_empty#1}%
66 \_let\_next=\_readverbline
67 \_ea\_isinlist\_ea\_tmpa\_ea{\_Doc}\_iftrue \_let\_next=\_processinput \_fi
68 \_ea\_isinlist\_ea\_tmpa\_ea{\_Doctab}\_iftrue \_let\_next=\_processinput \_fi
69 \_ea\_isinlist\_ea\_tmpa\_ea{\_Endcode}\_iftrue \_def\_next{\_processinput\_endinput}\_fi
70 \_ifx\_next\_readverbline \_addto\_tmpb{#1^^J}\_fi
71 \_next
72 }
73 {\_catcode`\ =13 \_gdef\_aspace{ }}\_def\_asp{\_ea\_noexpand\_aspace}
74 \_edef\_Doc{\_asp\_asp\_bslash _doc}
75 \_bgroup \_lccode`~=`\^^I \_lowercase{\_egroup\_edef\_Doctab{\_noexpand~\_bslash _doc}}
76 \_edef\_Endcode{\_noexpand\_empty\_bslash _endcode}
77
78 \_doc -----------------------------
79 The scanner of the control sequences in the listing mode replaces all
80 occurrences of `\` by \`\_makecs`. This macro reads next tokens and
81 accumulates them to `\_tmpa` as long as they have category 11. It
82 means that `\_tmpa` includes the name of the following control sequence
83 when \fw\`\_makecsF` is run. The printing form of the control
84 sequence is set to `\_tmpb` and the test of existence `\,;<csname>`is
85 performed. If it is true then active hyperlink is created. If not, then
86 the first `_` or `.` is removed from `\_tmpa` and the test is repeated.
87 \_cod -----------------------------
88
89 \_def\_makecs{\_def\_tmpa{}\_futurelet\_next\_makecsD}
90 \_def\_makecsD{\_if.\_next \_ea\_makecsB \_else \_ea\_makecsA \_fi} % \.foo is accepted
91 \_def\_makecsA{\_ifcat a\_noexpand\_next \_ea\_makecsB \_else \_ea\_makecsF \_fi}
92 \_def\_makecsB#1{\_addto\_tmpa{#1}\_futurelet\_next\_makecsA}
93 \_def\_makecsF{\_let\_tmpb=\_tmpa
94 \_ifx\_tmpa\_empty \_bslash \_returnfi \_fi
95 \_ifcsname el:\_tmpa\_endcsname \_csname el:\_tmpa\_endcsname \_returnfi \_fi
96 \_ifcsname ,;\_tmpa\_endcsname \_intlink \_returnfi \_fi
97 \_remfirstunderscoreordot\_tmpa
98 \_ifcsname el:\_tmpa\_endcsname \_csname el:\_tmpa\_endcsname \_returnfi \_fi
99 \_ifcsname ,;\_tmpa\_endcsname \_intlink \_returnfi \_fi
100 \_csstring\\\_tmpb \_relax
101 }
102 \_def\_processinput{%
103 \_let\_start=\_relax
104 \_ea\_replstring\_ea\_tmpb\_ea{\_aspace^^J}{^^J}
105 \_addto\_tmpb{\_fin}%
106 \_isinlist\_tmpb{\_start^^J}\_iftrue \_advance\_ttline by1\_fi
107 \_replstring\_tmpb{\_start^^J}{\_start}%
108 \_replstring\_tmpb{\_start}{}%
109 \_replstring\_tmpb{^^J\_fin}{\_fin}%
110 \_replstring\_tmpb{^^J\_fin}{}%
111 \_replstring\_tmpb{\_fin}{}%
112 \_ea\_prepareverbdata\_ea\_tmpb\_ea{\_tmpb^^J}%
113 \_replthis{\_csstring\\}{\_noexpand\_makecs}%
114 \_ea\_printverb \_tmpb\_fin
115 \_par
116 \_endgroup \_ttskip
117 \_isnextchar\_par{}{\_noindent}%
118 }
119 \_def\_remfirstunderscoreordot#1{\_ea\_remfirstuordotA#1\_relax#1}
120 \_def\_remfirstuordotA#1#2\_relax#3{\_if _#1\_def#3{#2}\_fi \_if\_string#1.\_def#3{#2}\_fi}
121
122 \_doc -----------------------------
123 By default the internal link is created by \`\_intlink` inside listing
124 mode. But you can define `\el:<csname>` which has precedence and it can
125 create an external link. The `\_tmpa` includes the name used in the link
126 and `\_tmpb` is the name to be printed. See \^`\_makecsF` above and the
127 example at the beginning of this section.
128 \_cod -----------------------------
129
130 \_def\_intlink{\_link[cs:\_tmpa]{\ulinkcolor}{\_csstring\\\_tmpb}}
131
132 \_doc -----------------------------
133 The lines in the listing mode have a yellow background.
134 \_cod -----------------------------
135
136 \_def\_printcodeline#1{\_advance \_maxlines by-1
137 \_ifnum \_maxlines<0 \_ea \_endverbprinting \_fi
138 \_ifx\_printfilename\_relax \_penalty \_ttpenalty \_fi \_vskip-4pt
139 \_noindent\_rlap{\bgverbcolor \_vrule height8pt depth5pt width\_hsize}%
140 \_printfilename
141 \_indent \_printverblinenum #1\_par}
142
143 \_def\_printfilename{\_hbox to0pt{%
144 \_hskip\_hsize\_vbox to0pt{\_vss\_llap{\fnamecolor\docfile}\_kern7.5pt}\_hss}%
145 \_let\_printfilename=\_relax
146 }
147 \_let\_normalprintfilename=\_printfilename
148 \_addto\_printcomments{\_let\_printfilename=\_normalprintfilename}
149 \_everytt={\_let\_printverblinenum=\_relax}
150
151 \_long\_def\_endverbprinting#1\_fin#2\_fin{\_fi\_fi \_global\_maxlines=100000
152 \_noindent\_typosize[8/]\_dots etc. (see {\_tt\fnamecolor\docfile})}
153
154 \_doc -----------------------------
155 \`\docfile` is currently documented file.\nl
156 \`\printdoc` and \`\printdoctail` macros are defined here.
157 \_cod -----------------------------
158
159 \_def\docfile{}
160 \_def\_printdoc #1 {\_par \_def\docfile{#1}%
161 \_everytt={\_ttshift=-15pt \_let\_printverblinenum=\_relax}%
162 \_ea\_cod \_input #1
163 \_everytt={\_let\_printverblinenum=\_relax}%
164 \_def\docfile{}%
165 }
166 \_def\_printdoctail #1 {\_bgroup
167 \_everytt={}\_ttline=-1 \_ea\_printdoctailA \_input #1 \_egroup}
168 {\_long\_gdef\_printdoctailA#1\_endcode{}}
169
170 \_public \printdoc \printdoctail ;
171
172 \_doc -----------------------------
173 You can do \^`\verbinput` \`\vitt``{<filename>} (<from>-<to>) <filename>`
174 if you need analogical design like in listing mode.
175 \_cod -----------------------------
176
177 \_def\_vitt#1{\_def\docfile{#1}\_ttline=-1 \_everytt={}%
178 \_ifnum\_ttline<0 \_let\_printverblinenum=\_relax \_else \_initverblinenum \_fi
179 \_let\_printverblinenum=\_normalprintverblinenum
180 \_typosize[8/10]\_let\_printverbline=\_printcodeline \_medskip
181 }
182 \_let\_normalprintverblinenum=\_printverblinenum
183
184 \_public \vitt ;
185
186 \_doc -----------------------------
187 The Index entries of control sequences are with semicolumn instead backslash in `.ref` file.
188 It is ignored when sorting but doesn't cause problems in the `.ref` file.
189 The Index entries of normal sequences are finished by `|` because these
190 entries should be sorted just after control sequences with the same name.
191 When printing Index, we firts try to refer to the main documentation
192 point (the `\cs:name` or `\ns:name` exists).
193 Index entries with only user documentation points refers to this point.
194 Other index entries are printed as usual without backslash.
195 \_cod -----------------------------
196
197 \_addto \_ignoredcharsen {_} % \foo, \_foo is the same in the fist pass of sorting
198 \_let\_optexprintii=\_printii
199 \_def\_printii #1#2&{%
200 \_if ;#1\_def\_tmp{#2}\_else \_printiiB #1#2\_end|\_end\_relax{#1#2}\_fi % does \def\_tmp{#1#2}
201 \_ea\_definefirstii \_tmp&%
202 \_ifx\_firstii\_lastii\_else
203 \_ea\_newiiletter\_ea{\_firstii}{\_tmp}\_let\_lastii=\_firstii\_fi
204 \_let\_currii=\_tmp \_the\_everyii \_noindent
205 \_hskip-\_iindent \_ignorespaces
206 \_if ;#1\_printiiC c\_bslash{#1#2}\_else \_printiiC n\_empty{#1#2}\_fi
207 }
208 \_def\_printiiB#1|\_end#2\_relax#3{\_ifx^#2^\_def\_tmp{#3}\_else\_def\_tmp{#1}\_fi}
209 \_def\_printiiC #1#2#3{% #1: c or n, #2: \_bslash or \_empty, \_tmp: name, #3: (raw name)
210 \_printiiD #1#2{#3}% maybe other index types
211 \_ifcsname #1s:\_tmp\_endcsname {\_tt\_link[#1s:\_tmp]\ulinkcolor{#2\_tmp}}\_returnfi\_fi
212 \_ifcsname #1s:^\_tmp\_endcsname {\_tt\_link[#1s:^\_tmp]\ulinkcolor{#2\_tmp}}\_returnfi\_fi
213 \_isnextchar<{\_ea\_printiiA\_ignoreit}%
214 {\_isnextchar>{\_ea\_printiiA\_ignoreit}{\_printiiA}}#3//% other index types
215 \_relax \_space
216 }
217 \_def\_printiiD #1#2#3{} % macro programmer can redefine it and declare another index types
218 \_def\_pgprintA #1{#1} % no hyperlinks from page numbers
219
220 \_def\_printiipages#1&{\_let\_pgtype=\_undefined \_tmpnum=0
221 {\_rm\_printpages #1,:,\_par}}
222
223 \_sdef{_tocl:1}#1#2#3{\_nofirst\_bigskip
224 \_bf\_llaptoclink{#1}{#2}\_hfill \_pgn{#3}\_tocpar\_medskip}
225
226 \_doc -----------------------------
227 If this macro is loaded by \^`\load` then we need to initialize
228 catcodes using the `\_afterload` macro.
229 \_cod -----------------------------
230
231 \_def\_afterload{\_catcode`\<=13 \_catcode`\`=13
232 \_wlog {doc.opm: catcodes of < and ` activated.}%
233 }
234
235 \_doc -----------------------------
236 The \code{<something>} will be print as <something>.
237 \_cod -----------------------------
238
239 \_let\lt=<
240 \_catcode`\<=13
241
242 \_def<#1>{$\langle\hbox{\it#1\/}\rangle$}
243 \_everyintt{\_catcode`\<=13 \_catcode`\.=11 }
244
245 \_doc -----------------------------
246 Main documentation point to a control sequence is created by
247 \code{\\`\\foo`} and its link name is `cs:foo`.
248 Main documentation point to another sequence (without backslash)
249 is created by \code{\\`foo`} and its link name is `ns:foo`.
250 User documentation point to a control sequence is created by
251 \code{\\^`\\foo`} and its link name is `cs:^foo`.
252 User documentation point to another sequence (without backslash)
253 is created by \code{\\^`foo`} and its link name is `ns:^foo`.
254 Only the first occurrence of the user documentation point has its
255 link destination. User documentation point are linked to relevant main
256 decumentation points. When the link destination is created then
257 the control sequence with the same name is defined as an empty macro.
258 Users can create a link to the user documentation point by \code{\\~`\\foo}.
259 \_cod -----------------------------
260
261 \_def\_docrefcodes{\_catcode`\.=11\_relax}
262
263 \_verbchar`
264
265 \_def\`{\_bgroup \_docrefcodes \_mainpoint}
266 \_def\_mainpoint #1`{\_egroup\_leavevmode\_edef\_tmp{\_csstring#1}%
267 \_ea \_mainpointA \_string#1\_relax % \defines \_cn as c or n
268 \_iindex{\_if\_cn c;\_tmp\_else\_tmp|\_fi}%
269 \_ifcsname \_cn s:\_tmp\_endcsname \_moremainpoints \_else \_dest[\_cn s:\_tmp]\_fi
270 \_sxdef{\_cn s:\_tmp}{}%
271 \_hbox{\_ifcsname \_cn s:^\_tmp\_endcsname
272 \_link[\_cn s:^\_tmp]{\mlinkcolor}{\_tt\_if\_cn c\_bslash\_fi \_tmp}\_else
273 {\_tt \mlinkcolor \_if\_cn c\_bslash\_fi \_tmp}\_fi}%
274 }
275 \_def\_mainpointA #1#2\_relax {\_edef\_cn{\_if\_csstring\\#1c\_else n\_fi}}
276 \_def\_moremainpoints{\_opwarning{Second main documentation point \_if\_cn c\_bslash\_fi\_tmp}}
277
278 \_def\^`{\_bgroup \_docrefcodes \_docpoint}
279 \_def\_docpoint #1{\_egroup\_leavevmode\_edef\_tmp{\_csstring#1}%
280 \_ea \_mainpointA \_string#1\_relax % \defines \_cn as c or n
281 \_if\_cn c\_iindex{;\_tmp}%
282 \_hbox{\_ifcsname cs:^\_tmp\_endcsname
283 \_else \_dest[cs:^\_tmp]\_sxdef{cs:^\_tmp}{}\_fi
284 \_link[cs:\_tmp]{\ulinkcolor}{\_tt\_string#1}}%
285 \_afterfi{\_futurelet\_next\_cslinkA}%
286 \_else \_afterfi {\_docpointN #1}\_fi
287 }
288 \_def\_cslinkA{\_ifx\_next`\_ea\_ignoreit \_else \_ea\_ea\_ea`\_ea\_string\_fi}
289 \_def\_docpointN #1`{\_iindex{#1|}%
290 \_hbox{\_ifcsname ns:^#1\_endcsname \_else \_dest[ns:^#1]\_sxdef{ns:^#1}{}\_fi
291 \_link[ns:#1]{\ulinkcolor}{\_tt#1}}%
292 }
293 \_def\~`{\_bgroup \_docrefcodes \_doctpoint}
294 \_def\_doctpoint #1{\_egroup\_leavevmode\_edef\_tmp{\_csstring#1}%\_iindex{\_tmp}%
295 \_ea \_mainpointA \_string#1\_relax % \defines \_cn as c or n
296 \_if\_cn c\_iindex{;\_tmp}%
297 \_hbox{\_link[cs:^\_tmp]{\ulinkcolor}{\_tt\_string#1}}%
298 \_afterfi{\_futurelet\_next\_cslinkA}%
299 \_else \_aferfi{\_docpointNA #1}\_fi
300 }
301 \_def\_docpointNA #1`{\_iindex{#1|}\_hbox{\_link[ns:^#1]{\ulinkcolor}{\_tt#1}}}
302
303
304 \_doc -----------------------------
305 The \`\fw` macro for forward links to user documentation point (given later)
306 is defined here.
307 \_cod -----------------------------
308
309 \_def\_fw\`#1`{{\_slet{cs:^\_csstring#1}{_fw}\_slet{ns:^\_csstring#1}{_fw}\`#1`}}
310 \_public \fw ;
311
312 \_endcode %-------------------------------------------
313
314 \noindent
315 The \^`\printdoc` `<filename><space>` and \^`\printdoctail` `<filename><space>`
316 commands are defined after the file `doc.opm` is load by \^`\load`~`[doc]`.
317
318 The `\printdoc` starts reading of given `<filename>` from the second line.
319 The file is read in {\em the listing mode}.
320 The `\prindoctail` starts reading given `<filename>` from the
321 first occurrence of the `\_endcode`. The file is read
322 in normal mode (like `\input <filename>`).
323
324 The {\em listing mode} prints the lines as a listing of a code. This mode is
325 finished when first {\visiblesp` \_doc`} occurs or first `\_endcode`
326 occurs. At least two spaces or one tab
327 character must precede before such `\_doc`. On the other
328 hand, the `\_endcode` must be at the left edge of the line without spaces.
329 If this rule is not met then the listing mode continues.
330
331 If the first line or the last line of the listing mode is empty then such
332 lines are not printed. The maximal number of printed lines in the listing
333 mode is \^`\maxlines`. It is set to almost infinity (100000). You can set it
334 to a more sensible value. Such a setting is valid only for the first following
335 listing mode.
336
337 When the listing mode is finished by `\_doc` then the next lines are read in the
338 normal way, but the material between `\begtt` ... `\endtt` pair
339 is shifted by three letters left. The reason is that the three spaces of
340 indentation is recommended in the `\_doc` ... `\_cod` pair and this shifting
341 is compensation for this indentation.
342
343 The `\_cod` macro ignores the rest of the current line and starts the listing
344 mode again.
345
346 When the listing mode is finished by the `\_endcode` then the `\endinput` is
347 applied, the reading of the file opened by `\printdoc` is finished.
348
349 You cannot reach the end of the file (without `\_endcode`) in the listing
350 mode.
351
352 The main documentation point can document
353 a control sequence (use \code{\\`\\}`<sequence>`\code{`})
354 or a normal sequence without backslash (use \code{\\`}`<text>`\code{`}).
355 It is printed in red. Examples: \code{\\`\\foo`} (control sequence),
356 \code{\\`foo`} (normal sequence).
357 The user documentation point is the first occurrence of
358 \code{\\^`\\}`<sequence>`\code{`} or \code{\\^`}`<text>`\code{`}
359 There can be more such markups, all of them are hyperlinks to the relevant main
360 documentation point.
361 And main documentation point is a hyperlink to the relevant user documentation point
362 if this point precedes. Finally, the
363 \code{\\~`\\}`<sequence>`\code{`} (for example \code{\\~`\\foo`}) are
364 hyperlinks to the user documentation point.
365
366 By default, the hyperink from main documentation point to the user
367 documentation point is active only if it is backward link, i.e.\
368 the main documentation point is given later. The reason is that we don't
369 know if such user documentation point will exist when creating
370 main documentation point and we don't want
371 broken links. If you are sure that user documentation point will
372 follow then use prefix \^`\fw` before~\code{\\`}, for example
373 \code{\\fw\\`\\foo`} is main documentation point where the user
374 documentation point is given later and forward hyperlink is created here.
375
376 Documented sequences and their page positions of main documentation points and user
377 documentation points are saved to the index.
378
379 The listing mode creates all control sequences which are listed in the
380 index as an active link to the main documentation point of such control sequence
381 and prints them in blue. Moreower, active links are control sequences of the
382 type `\_foo` or `\.foo` although the documentation mentions only `\foo`.
383 Another text is printed in black.
384
385 The listing mode is able to generate external links to another \OpTeX/-like
386 documentation, if the macro `\el:<csname>` is defined.
387 The macro should create a hyperlink using `\_tmpa`
388 where the link name of the <csname> is saved and `\_tmpb` where the name of
389 the <csname> to be printed is saved (`\tmpb` is without backlash but
390 it can include preceding `_` or `.` unlike `\_tmpa`). For example,
391 suppose, that we have created `optex-doc.eref` file by:
392 \begtt
393 TEXINPUTS='.;$TEXMF/{doc,tex}//' optex optex-doc
394 grep Xindex optex-doc.ref > optex-doc.eref
395 \endtt
396 The `.eref` file includes only `\_Xindex{;<csname>}{}` lines from %$
397 `optex-doc.ref` file. Then we can use following macros:
398 \begtt
399 \def\_Xindex#1#2{\slet{el:\ignoreit#1}{optexdoclink}}
400 \def\optexdoclink{%
401 \edef\extlink{\optexdocurl\csstring\#cs:\_tmpa}%
402 \xlink{url}{\extlink}{\Cyan}{\csstring\\\_tmpb}}
403 \def\optexdocurl{http://petr.olsak.net/ftp/olsak/optex/optex-doc.pdf}
404 \isfile{optex-doc.eref}\iftrue \input{optex-doc.eref}\fi
405 \endtt
406 All `\el:<csname>`, where <csname> is from `optex-doc.ref`,
407 have the same meaning: `\optexdoclink` in this example. And
408 `\optexdoclink` creates the external link in `\Cyan` color.
409
410 \secc Implementation
411
412 \endinput
413
414 2025-11-07 \_printfilename repeated after \commentschars interruption
415 2025-11-05 \vitt corrected
416 2025-06-17 \? etc. in listing mode printed, bug from previous patch fixed
417 2025-05-15 normal sequences (without backslash) can be documented too
418 2023-12-10 \catcode`.=11 removed, issue solved by \_makecsD.
419 2022-12-11 \_opwaning "Second main documentation point" introduced.
420 2022-12-11 \_docrefcodes added (bug due to 2022-11-13 fixed).
421 2022-11-21 magenta color for internal links instead green.
422 2022-11-13 \catcode`.=11: only local settings
423 2022-07-01 \_printii improved, colors declaration part added.
424 2021-05-15 \_endinput shifted after \_processinput when \_endcode is scanned.
425 2021-05-14 \_catcodedot, \_Doctab introduced.
426 2021-05-13 \def\t added, bug fixed.
427 2021-05-03 External links fom listing mode allowed.
428 2021-05-02 \_forwadlink replaced by \_fw, to be more consistent.
429 2021-05-02 \fw introduced, \.foo -> \foo allowed.
430 2020-04-28 \levevmode in \^ macros added (bug fixed)
431 2020-04-22 released