%% This is part of the OpTeX project, see http://petr.olsak.net/optex

\_codedecl \printdoc {Macros for documentation printing <2025-11-07>} % loaded on demand by \load[doc]

   \_doc -----------------------------
   General declarations.
   \_cod -----------------------------

\_fontfam[lmfonts]

\_let \mlinkcolor=\Red    % main doc. points
\_let \ulinkcolor=\Blue   % user doc. points
\_let \fnamecolor=\Brown  % file names in listing headers
\_def \bgverbcolor  {\_setcmykcolor{0 0 .3 .03}} % background for listings
\_def \outlinkcolor {\_setcmykcolor{1 0 1 .2}}   % green for outerlinks
\_def \inlinkcolor  {\_setcmykcolor{0 1 0 .1}}   % magenta for internal links
\_hyperlinks \inlinkcolor \outlinkcolor
\_enlang
\_enquotes

   \_doc -----------------------------
   Maybe, somebody needs `\seccc` or `\secccc`?
   \_cod -----------------------------

\_eoldef\seccc#1{\_medskip \_noindent{\_bf#1}\_par\_nobreak\_firstnoindent}
\_def\secccc{\_medskip\_noindent $\_bullet$ }

   \_doc -----------------------------
   `\enddocument` can be redefined.
   \_cod -----------------------------

\_let\enddocument=\_bye

   \_doc -----------------------------
   A full page of listing causes underfull `\vbox` in output routine.
   We need to add a small tolerance.
   \_cod -----------------------------

\_pgbottomskip=0pt plus10pt minus2pt

   \_doc -----------------------------
   The listing mode is implemented here. The \`\maxlines`
   is maximal lines of code printed in the listing mode.
   \_cod -----------------------------

\_newcount \_maxlines   \_maxlines=100000
\_public \maxlines ;

\_eoldef\_cod#1{\_par \_wipeepar
   \_vskip\_parskip \_medskip \_ttskip
   \_begingroup
   \_typosize[8/10]
   \_let\_printverbline=\_printcodeline
   \_ttline=\_inputlineno
   \_setverb
   \_ifnum\_ttline<0 \_let\_printverblinenum=\_relax \_else \_initverblinenum \_fi
   \_adef{ }{\ }\_adef\^^I{\t}\_parindent=\_ttindent \_parskip=0pt
   \_def\t{\_hskip \_dimexpr\_tabspaces em/2\_relax}%
   \_relax \_ttfont
   \_endlinechar=`^^J
   \_def\_tmpb{\_start}%
   \_readverbline
}
\_def\_readverbline #1^^J{%
   \_def\_tmpa{\_empty#1}%
   \_let\_next=\_readverbline
   \_ea\_isinlist\_ea\_tmpa\_ea{\_Doc}\_iftrue \_let\_next=\_processinput \_fi
   \_ea\_isinlist\_ea\_tmpa\_ea{\_Doctab}\_iftrue \_let\_next=\_processinput \_fi
   \_ea\_isinlist\_ea\_tmpa\_ea{\_Endcode}\_iftrue \_def\_next{\_processinput\_endinput}\_fi
   \_ifx\_next\_readverbline \_addto\_tmpb{#1^^J}\_fi
   \_next
}
{\_catcode`\ =13 \_gdef\_aspace{ }}\_def\_asp{\_ea\_noexpand\_aspace}
\_edef\_Doc{\_asp\_asp\_bslash _doc}
\_bgroup \_lccode`~=`\^^I \_lowercase{\_egroup\_edef\_Doctab{\_noexpand~\_bslash _doc}}
\_edef\_Endcode{\_noexpand\_empty\_bslash _endcode}

   \_doc -----------------------------
   The scanner of the control sequences in the listing mode replaces all
   occurrences of `\` by \`\_makecs`. This macro reads next tokens and
   accumulates them to `\_tmpa` as long as they have category 11. It
   means that `\_tmpa` includes the name of the following control sequence
   when \fw\`\_makecsF` is run. The printing form of the control
   sequence is set to `\_tmpb` and the test of existence `\,;<csname>`is
   performed. If it is true then active hyperlink is created. If not, then
   the first `_` or `.` is removed from `\_tmpa` and the test is repeated.
   \_cod -----------------------------

\_def\_makecs{\_def\_tmpa{}\_futurelet\_next\_makecsD}
\_def\_makecsD{\_if.\_next \_ea\_makecsB \_else \_ea\_makecsA \_fi} % \.foo is accepted
\_def\_makecsA{\_ifcat a\_noexpand\_next \_ea\_makecsB \_else \_ea\_makecsF \_fi}
\_def\_makecsB#1{\_addto\_tmpa{#1}\_futurelet\_next\_makecsA}
\_def\_makecsF{\_let\_tmpb=\_tmpa
   \_ifx\_tmpa\_empty \_bslash \_returnfi \_fi
   \_ifcsname el:\_tmpa\_endcsname \_csname el:\_tmpa\_endcsname \_returnfi \_fi
   \_ifcsname ,;\_tmpa\_endcsname \_intlink \_returnfi \_fi
   \_remfirstunderscoreordot\_tmpa
   \_ifcsname el:\_tmpa\_endcsname \_csname el:\_tmpa\_endcsname \_returnfi \_fi
   \_ifcsname ,;\_tmpa\_endcsname \_intlink \_returnfi \_fi
   \_csstring\\\_tmpb \_relax
}
\_def\_processinput{%
   \_let\_start=\_relax
   \_ea\_replstring\_ea\_tmpb\_ea{\_aspace^^J}{^^J}
   \_addto\_tmpb{\_fin}%
   \_isinlist\_tmpb{\_start^^J}\_iftrue \_advance\_ttline by1\_fi
   \_replstring\_tmpb{\_start^^J}{\_start}%
   \_replstring\_tmpb{\_start}{}%
   \_replstring\_tmpb{^^J\_fin}{\_fin}%
   \_replstring\_tmpb{^^J\_fin}{}%
   \_replstring\_tmpb{\_fin}{}%
   \_ea\_prepareverbdata\_ea\_tmpb\_ea{\_tmpb^^J}%
   \_replthis{\_csstring\\}{\_noexpand\_makecs}%
   \_ea\_printverb \_tmpb\_fin
   \_par
   \_endgroup \_ttskip
   \_isnextchar\_par{}{\_noindent}%
}
\_def\_remfirstunderscoreordot#1{\_ea\_remfirstuordotA#1\_relax#1}
\_def\_remfirstuordotA#1#2\_relax#3{\_if _#1\_def#3{#2}\_fi \_if\_string#1.\_def#3{#2}\_fi}

   \_doc -----------------------------
   By default the internal link is created by \`\_intlink` inside listing
   mode. But you can define `\el:<csname>` which has precedence and it can
   create an external link. The `\_tmpa` includes the name used in the link
   and `\_tmpb` is the name to be printed. See \^`\_makecsF` above and the
   example at the beginning of this section.
   \_cod -----------------------------

\_def\_intlink{\_link[cs:\_tmpa]{\ulinkcolor}{\_csstring\\\_tmpb}}

   \_doc -----------------------------
   The lines in the listing mode have a yellow background.
   \_cod -----------------------------

\_def\_printcodeline#1{\_advance \_maxlines by-1
   \_ifnum \_maxlines<0 \_ea \_endverbprinting \_fi
   \_ifx\_printfilename\_relax \_penalty \_ttpenalty \_fi \_vskip-4pt
   \_noindent\_rlap{\bgverbcolor \_vrule height8pt depth5pt width\_hsize}%
   \_printfilename
   \_indent \_printverblinenum #1\_par}

\_def\_printfilename{\_hbox to0pt{%
   \_hskip\_hsize\_vbox to0pt{\_vss\_llap{\fnamecolor\docfile}\_kern7.5pt}\_hss}%
   \_let\_printfilename=\_relax
}
\_let\_normalprintfilename=\_printfilename
\_addto\_printcomments{\_let\_printfilename=\_normalprintfilename}
\_everytt={\_let\_printverblinenum=\_relax}

\_long\_def\_endverbprinting#1\_fin#2\_fin{\_fi\_fi \_global\_maxlines=100000
   \_noindent\_typosize[8/]\_dots etc. (see {\_tt\fnamecolor\docfile})}

   \_doc -----------------------------
   \`\docfile` is currently documented file.\nl
   \`\printdoc` and \`\printdoctail` macros are defined here.
   \_cod -----------------------------

\_def\docfile{}
\_def\_printdoc #1 {\_par \_def\docfile{#1}%
   \_everytt={\_ttshift=-15pt \_let\_printverblinenum=\_relax}%
   \_ea\_cod \_input #1
   \_everytt={\_let\_printverblinenum=\_relax}%
   \_def\docfile{}%
}
\_def\_printdoctail #1 {\_bgroup
   \_everytt={}\_ttline=-1 \_ea\_printdoctailA \_input #1 \_egroup}
{\_long\_gdef\_printdoctailA#1\_endcode{}}

\_public \printdoc \printdoctail ;

   \_doc -----------------------------
   You can do \^`\verbinput` \`\vitt``{<filename>} (<from>-<to>) <filename>`
   if you need analogical design like in listing mode.
   \_cod -----------------------------

\_def\_vitt#1{\_def\docfile{#1}\_ttline=-1 \_everytt={}%
   \_ifnum\_ttline<0 \_let\_printverblinenum=\_relax \_else \_initverblinenum \_fi
   \_let\_printverblinenum=\_normalprintverblinenum
   \_typosize[8/10]\_let\_printverbline=\_printcodeline \_medskip
}
\_let\_normalprintverblinenum=\_printverblinenum

\_public \vitt ;

   \_doc -----------------------------
   The Index entries of control sequences are with semicolumn instead backslash in `.ref` file.
   It is ignored when sorting but doesn't cause problems in the `.ref` file.
   The Index entries of normal sequences are finished by `|` because these
   entries should be sorted just after control sequences with the same name.
   When printing Index, we firts try to refer to the main documentation
   point (the `\cs:name` or `\ns:name` exists).
   Index entries with only user documentation points refers to this point.
   Other index entries are printed as usual without backslash.
   \_cod -----------------------------

\_addto \_ignoredcharsen {_}  % \foo, \_foo is the same in the fist pass of sorting
\_let\_optexprintii=\_printii
\_def\_printii #1#2&{%
   \_if ;#1\_def\_tmp{#2}\_else \_printiiB #1#2\_end|\_end\_relax{#1#2}\_fi % does \def\_tmp{#1#2}
   \_ea\_definefirstii \_tmp&%
   \_ifx\_firstii\_lastii\_else
      \_ea\_newiiletter\_ea{\_firstii}{\_tmp}\_let\_lastii=\_firstii\_fi
   \_let\_currii=\_tmp \_the\_everyii \_noindent
   \_hskip-\_iindent \_ignorespaces
   \_if ;#1\_printiiC c\_bslash{#1#2}\_else \_printiiC n\_empty{#1#2}\_fi
}
\_def\_printiiB#1|\_end#2\_relax#3{\_ifx^#2^\_def\_tmp{#3}\_else\_def\_tmp{#1}\_fi}
\_def\_printiiC #1#2#3{% #1: c or n, #2: \_bslash or \_empty, \_tmp: name, #3: (raw name)
   \_printiiD #1#2{#3}% maybe other index types
   \_ifcsname #1s:\_tmp\_endcsname {\_tt\_link[#1s:\_tmp]\ulinkcolor{#2\_tmp}}\_returnfi\_fi
   \_ifcsname #1s:^\_tmp\_endcsname {\_tt\_link[#1s:^\_tmp]\ulinkcolor{#2\_tmp}}\_returnfi\_fi
   \_isnextchar<{\_ea\_printiiA\_ignoreit}%
      {\_isnextchar>{\_ea\_printiiA\_ignoreit}{\_printiiA}}#3//% other index types
   \_relax \_space
}
\_def\_printiiD #1#2#3{} % macro programmer can redefine it and declare another index types
\_def\_pgprintA #1{#1}   % no hyperlinks from page numbers

\_def\_printiipages#1&{\_let\_pgtype=\_undefined \_tmpnum=0
   {\_rm\_printpages #1,:,\_par}}

\_sdef{_tocl:1}#1#2#3{\_nofirst\_bigskip
   \_bf\_llaptoclink{#1}{#2}\_hfill \_pgn{#3}\_tocpar\_medskip}

   \_doc -----------------------------
   If this macro is loaded by \^`\load` then we need to initialize
   catcodes using the `\_afterload` macro.
   \_cod -----------------------------

\_def\_afterload{\_catcode`\<=13 \_catcode`\`=13
   \_wlog {doc.opm: catcodes of < and ` activated.}%
}

   \_doc -----------------------------
   The \code{<something>} will be print as <something>.
   \_cod -----------------------------

\_let\lt=<
\_catcode`\<=13

\_def<#1>{$\langle\hbox{\it#1\/}\rangle$}
\_everyintt{\_catcode`\<=13 \_catcode`\.=11 }

   \_doc -----------------------------
   Main documentation point to a control sequence is created by
   \code{\\`\\foo`} and its link name is `cs:foo`.
   Main documentation point to another sequence (without backslash)
   is created by \code{\\`foo`} and its link name is `ns:foo`.
   User documentation point to a control sequence is created by
   \code{\\^`\\foo`} and its link name is `cs:^foo`.
   User documentation point to another sequence (without backslash)
   is created by \code{\\^`foo`} and its link name is `ns:^foo`.
   Only the first occurrence of the user documentation point has its
   link destination. User documentation point are linked to relevant main
   decumentation points. When the link destination is created then
   the control sequence with the same name is defined as an empty macro.
   Users can create a link to the user documentation point by \code{\\~`\\foo}.
   \_cod -----------------------------

\_def\_docrefcodes{\_catcode`\.=11\_relax}

\_verbchar`

\_def\`{\_bgroup \_docrefcodes \_mainpoint}
\_def\_mainpoint #1`{\_egroup\_leavevmode\_edef\_tmp{\_csstring#1}%
   \_ea \_mainpointA \_string#1\_relax % \defines \_cn as c or n
   \_iindex{\_if\_cn c;\_tmp\_else\_tmp|\_fi}%
   \_ifcsname \_cn s:\_tmp\_endcsname \_moremainpoints \_else \_dest[\_cn s:\_tmp]\_fi
   \_sxdef{\_cn s:\_tmp}{}%
   \_hbox{\_ifcsname \_cn s:^\_tmp\_endcsname
            \_link[\_cn s:^\_tmp]{\mlinkcolor}{\_tt\_if\_cn c\_bslash\_fi \_tmp}\_else
            {\_tt \mlinkcolor \_if\_cn c\_bslash\_fi \_tmp}\_fi}%
}
\_def\_mainpointA #1#2\_relax {\_edef\_cn{\_if\_csstring\\#1c\_else n\_fi}}
\_def\_moremainpoints{\_opwarning{Second main documentation point \_if\_cn c\_bslash\_fi\_tmp}}

\_def\^`{\_bgroup \_docrefcodes \_docpoint}
\_def\_docpoint #1{\_egroup\_leavevmode\_edef\_tmp{\_csstring#1}%
   \_ea \_mainpointA \_string#1\_relax % \defines \_cn as c or n
   \_if\_cn c\_iindex{;\_tmp}%
      \_hbox{\_ifcsname cs:^\_tmp\_endcsname
             \_else \_dest[cs:^\_tmp]\_sxdef{cs:^\_tmp}{}\_fi
             \_link[cs:\_tmp]{\ulinkcolor}{\_tt\_string#1}}%
      \_afterfi{\_futurelet\_next\_cslinkA}%
   \_else \_afterfi {\_docpointN #1}\_fi
}
\_def\_cslinkA{\_ifx\_next`\_ea\_ignoreit \_else \_ea\_ea\_ea`\_ea\_string\_fi}
\_def\_docpointN #1`{\_iindex{#1|}%
      \_hbox{\_ifcsname ns:^#1\_endcsname \_else \_dest[ns:^#1]\_sxdef{ns:^#1}{}\_fi
             \_link[ns:#1]{\ulinkcolor}{\_tt#1}}%
}
\_def\~`{\_bgroup \_docrefcodes \_doctpoint}
\_def\_doctpoint #1{\_egroup\_leavevmode\_edef\_tmp{\_csstring#1}%\_iindex{\_tmp}%
   \_ea \_mainpointA \_string#1\_relax % \defines \_cn as c or n
   \_if\_cn c\_iindex{;\_tmp}%
       \_hbox{\_link[cs:^\_tmp]{\ulinkcolor}{\_tt\_string#1}}%
       \_afterfi{\_futurelet\_next\_cslinkA}%
   \_else \_aferfi{\_docpointNA #1}\_fi
}
\_def\_docpointNA #1`{\_iindex{#1|}\_hbox{\_link[ns:^#1]{\ulinkcolor}{\_tt#1}}}


   \_doc -----------------------------
   The \`\fw` macro for forward links to user documentation point (given later)
   is defined here.
   \_cod -----------------------------

\_def\_fw\`#1`{{\_slet{cs:^\_csstring#1}{_fw}\_slet{ns:^\_csstring#1}{_fw}\`#1`}}
\_public \fw ;

\_endcode %-------------------------------------------

\noindent
The \^`\printdoc` `<filename><space>` and \^`\printdoctail` `<filename><space>`
commands are defined after the file `doc.opm` is load by \^`\load`~`[doc]`.

The `\printdoc` starts reading of given `<filename>` from the second line.
The file is read in {\em the listing mode}.
The `\prindoctail` starts reading given `<filename>` from the
first occurrence of the `\_endcode`. The file is read
in normal mode (like `\input <filename>`).

The {\em listing mode} prints the lines as a listing of a code. This mode is
finished when first {\visiblesp`  \_doc`} occurs or first `\_endcode`
occurs. At least two spaces or one tab
character must precede before such `\_doc`. On the other
hand, the `\_endcode` must be at the left edge of the line without spaces.
If this rule is not met then the listing mode continues.

If the first line or the last line of the listing mode is empty then such
lines are not printed. The maximal number of printed lines in the listing
mode is \^`\maxlines`. It is set to almost infinity (100000). You can set it
to a more sensible value. Such a setting is valid only for the first following
listing mode.

When the listing mode is finished by `\_doc` then the next lines are read in the
normal way, but the material between `\begtt` ... `\endtt` pair
is shifted by three letters left. The reason is that the three spaces of
indentation is recommended in the `\_doc` ... `\_cod` pair and this shifting
is compensation for this indentation.

The `\_cod` macro ignores the rest of the current line and starts the listing
mode again.

When the listing mode is finished by the `\_endcode` then the `\endinput` is
applied, the reading of the file opened by `\printdoc` is finished.

You cannot reach the end of the file (without `\_endcode`) in the listing
mode.

The main documentation point can document
a control sequence (use \code{\\`\\}`<sequence>`\code{`})
or a normal sequence without backslash (use \code{\\`}`<text>`\code{`}).
It is printed in red. Examples: \code{\\`\\foo`} (control sequence),
\code{\\`foo`} (normal sequence).
The user documentation point is the first occurrence of
\code{\\^`\\}`<sequence>`\code{`} or \code{\\^`}`<text>`\code{`}
There can be more such markups, all of them are hyperlinks to the relevant main
documentation point.
And main documentation point is a hyperlink to the relevant user documentation point
if this point precedes. Finally, the
\code{\\~`\\}`<sequence>`\code{`} (for example \code{\\~`\\foo`}) are
hyperlinks to the user documentation point.

By default, the hyperink from main documentation point to the user
documentation point is active only if it is backward link, i.e.\
the main documentation point is given later. The reason is that we don't
know if such user documentation point will exist when creating
main documentation point and we don't want
broken links. If you are sure that user documentation point will
follow then use prefix \^`\fw` before~\code{\\`}, for example
\code{\\fw\\`\\foo`} is main documentation point where the user
documentation point is given later and forward hyperlink is created here.

Documented sequences and their page positions of main documentation points and user
documentation points are saved to the index.

The listing mode creates all control sequences which are listed in the
index as an active link to the main documentation point of such control sequence
and prints them in blue. Moreower, active links are control sequences of the
type `\_foo` or `\.foo` although the documentation mentions only `\foo`.
Another text is printed in black.

The listing mode is able to generate external links to another \OpTeX/-like
documentation, if the macro `\el:<csname>` is defined.
The macro should create a hyperlink using `\_tmpa`
where the link name of the <csname> is saved and `\_tmpb` where the name of
the <csname> to be printed is saved (`\tmpb` is without backlash but
it can include preceding `_` or `.` unlike `\_tmpa`). For example,
suppose, that we have created `optex-doc.eref` file by:
\begtt
TEXINPUTS='.;$TEXMF/{doc,tex}//' optex optex-doc
grep Xindex optex-doc.ref > optex-doc.eref
\endtt
The `.eref` file includes only `\_Xindex{;<csname>}{}` lines from %$
`optex-doc.ref` file. Then we can use following macros:
\begtt
\def\_Xindex#1#2{\slet{el:\ignoreit#1}{optexdoclink}}
\def\optexdoclink{%
   \edef\extlink{\optexdocurl\csstring\#cs:\_tmpa}%
   \xlink{url}{\extlink}{\Cyan}{\csstring\\\_tmpb}}
\def\optexdocurl{http://petr.olsak.net/ftp/olsak/optex/optex-doc.pdf}
\isfile{optex-doc.eref}\iftrue \input{optex-doc.eref}\fi
\endtt
All `\el:<csname>`, where <csname> is from `optex-doc.ref`,
have the same meaning: `\optexdoclink` in this example. And
`\optexdoclink` creates the external link in `\Cyan` color.

\secc Implementation

\endinput

2025-11-07 \_printfilename repeated after \commentschars interruption
2025-11-05 \vitt corrected
2025-06-17 \? etc. in listing mode printed, bug from previous patch fixed
2025-05-15 normal sequences (without backslash) can be documented too
2023-12-10 \catcode`.=11 removed, issue solved by \_makecsD.
2022-12-11 \_opwaning "Second main documentation point" introduced.
2022-12-11 \_docrefcodes added (bug due to 2022-11-13 fixed).
2022-11-21 magenta color for internal links instead green.
2022-11-13 \catcode`.=11: only local settings
2022-07-01 \_printii improved, colors declaration part added.
2021-05-15 \_endinput shifted after \_processinput when \_endcode is scanned.
2021-05-14 \_catcodedot, \_Doctab introduced.
2021-05-13 \def\t added, bug fixed.
2021-05-03 External links fom listing mode allowed.
2021-05-02 \_forwadlink replaced by \_fw, to be more consistent.
2021-05-02 \fw introduced, \.foo -> \foo allowed.
2020-04-28 \levevmode in \^ macros added (bug fixed)
2020-04-22 released
