summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/luatex/optex/base/verbatim.opm
diff options
context:
space:
mode:
Diffstat (limited to 'Master/texmf-dist/tex/luatex/optex/base/verbatim.opm')
-rw-r--r--Master/texmf-dist/tex/luatex/optex/base/verbatim.opm140
1 files changed, 118 insertions, 22 deletions
diff --git a/Master/texmf-dist/tex/luatex/optex/base/verbatim.opm b/Master/texmf-dist/tex/luatex/optex/base/verbatim.opm
index c74338d2e26..3930579e15c 100644
--- a/Master/texmf-dist/tex/luatex/optex/base/verbatim.opm
+++ b/Master/texmf-dist/tex/luatex/optex/base/verbatim.opm
@@ -1,6 +1,6 @@
%% This is part of OpTeX project, see http://petr.olsak.net/optex
-\_codedecl \begtt {Verbatim <2020-04-22>} % preloaded in format
+\_codedecl \begtt {Verbatim <2020-11-13>} % preloaded in format
\_doc ----------------------------
The internal parameters
@@ -79,7 +79,7 @@
\_ifx\_savedttchar\_undefined\_else \_catcode\_savedttchar=\_savedttcharc \_fi
\_chardef\_savedttchar=`#1
\_chardef\_savedttcharc=\_catcode`#1
- \_adef{#1}{\_begingroup \_setverb \_adef{ }{\ }\_ttfont \_the\_everyintt\_relax \_readverb}%
+ \_adef{#1}{\_begingroup \_setverb \_adef{ }{\_dsp}\_ttfont \_the\_everyintt\_relax \_readverb}%
\_def\_readverb ##1#1{\_printinverbatim{##1}\_endgroup}%
}
\_public \activettchar ;
@@ -109,25 +109,20 @@
active characters (defined as {\visiblesp`\ `}).
Other characters have normal category 11 or 12.
- When `\_prepareverbdata` finishes then `\_startverb` runs \`\_printverb` loop
+ When `\_prepareverbdata` finishes then `\_startverb` runs `\_printverb` loop
over each line of the data and does a final work: last skip plus `\noindent`
in the next paragraph.
-
- The `\_printverb` macro calls \`\_printverbline``{<line>}` to each scanned line of
- verbatim text. This macro expect that it strarts in vertical mode and it must
- do `\par` in order to return the vertical mode. The \`\_printverblinenum`
- is used here: it does nothing when `\_ttline`\code{<0} else it prints the line
- number using `\_llap`.
- \_cod ----------------------------
+ \_cod ---------------------------
\_eoldef \begtt#1{\_par \_wipeepar \_setxhsize
\_vskip\_parskip \_ttskip
\_begingroup
\_setverb
\_ifnum\_ttline<0 \_let\_printverblinenum=\_relax \_else \_initverblinenum \_fi
- \_adef{ }{\ }\_adef\^^I{\t}\_parindent=\_ttindent \_parskip=0pt
+ \_adef{ }{\_dsp}\_adef\^^I{\t}\_parindent=\_ttindent \_parskip=0pt
\_def\t{\_hskip \_dimexpr\_tabspaces em/2\_relax}%
\_the\_everytt \_relax #1\_relax \_ttfont
+ \_def\_testcommentchars##1\_iftrue{\_iffalse}\_let\_hicomments=\_relax
\_endlinechar=`^^J
\_startverb
}
@@ -138,19 +133,45 @@
\_endgroup \_ttskip
\_isnextchar\_par{}{\_noindent}%
}
-\_def\_printverb #1^^J#2{\_ifx\_end#2
- \_bgroup \_adef{ }{}\_def\t{}%
- \_ifcat&#1&\_egroup \_else\_egroup \_printverbline{#1}\_fi
+\_def\_prepareverbdata#1#2{\_def#1{#2}}
+
+ \_doc
+ The \`\_printverb` macro calls \^`\_printverbline``{<line>}` repeatedly
+ to each scanned line of verbatim text. The `\_printverb` is used from
+ `\begtt...\endtt` and from `\verbinput` too.
+
+ The \^`\_testcommentchars` replaces the following `\_iftrue` to
+ `\_iffalse` by default unless the \~`\commentchars` are set. So, main body
+ of the loop is written in `\_else` part of the `\_iftrue` condition.
+ The \^`\_printverbline``{<line>}` is called here.
+
+ The \`\_printverbline``{<line>}` expects that it starts in vertical mode and it must
+ do `\par` in order to return the vertical mode. The \`\_printverblinenum`
+ is used here: it does nothing when `\_ttline`\code{<0} else it prints the line
+ number using `\_llap`.
+ \_cod ----------------------------
+
+\_def\_printverb #1^^J#2{%
+ \_ifx\_printverblinenum\_relax \_else \_global\_advance\_ttline by1 \_fi
+ \_testcommentchars #1\_relax\_relax\_relax
+ \_iftrue
+ \_ifx\_end#2 \_printcomments\_fi
\_else
- \_printverbline{#1}%
- \_ea \_printverb \_ea #2%
+ \_ifx\_vcomments\_empty\_else \_printcomments \_def\_vcomments{}\_fi
+ \_ifx\_end#2
+ \_bgroup \_adef{ }{}\_def\t{}% if the last line is emtpy, we don't print it
+ \_ifcat&#1&\_egroup \_else\_egroup \_printverbline{#1}\_fi
+ \_else
+ \_printverbline{#1}%
+ \_fi
\_fi
+ \_ifx\_end#2 \_let\_next=\_relax \_else \_def\_next{\_printverb#2}\_fi
+ \_next
}
-\_def\_prepareverbdata#1#2{\_def#1{#2}}
\_def\_printverbline#1{\_penalty \_ttpenalty
\_indent \_printverblinenum \_kern\_ttshift #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}}
+\_def\_printverblinenum{\_llap{\_sevenrm \_the\_ttline\_kern.9em}}
\_doc ----------------------------
Macro \`\verbinput` uses a file read previously or opens the given file. Then
@@ -216,7 +237,7 @@
\_vskip\_parskip \_ttskip \_wipeepar \_setxhsize
\_begingroup
\_ifnum\_ttline<-1 \_let\_printverblinenum=\_relax \_else \_initverblinenum \_fi
- \_setverb \_adef{ }{\ }\_adef\^^I{\t}\_parindent=\_ttindent \_parskip=0pt
+ \_setverb \_adef{ }{\_dsp}\_adef\^^I{\t}\_parindent=\_ttindent \_parskip=0pt
\_def\t{\_hskip \_dimexpr\_tabspaces em/2\_relax}%
\_the\_everytt\_relax \_tmpb\_relax \_ttfont
\_endlinechar=`^^J \_tmpnum=0
@@ -234,6 +255,7 @@
\_ifeof\_vifile \_tmpnum=\_vidolines\_space \_else \_visaveline \_fi %% save line
\_repeat
\_ea\_prepareverbdata \_ea \_tmpb\_ea{\_tmpb^^J}%
+ \_catcode`\ =10 \_catcode`\%=9 % used in \commentchars comments
\_ea\_printverb \_tmpb\_end
\_global\_ttlinesave
\_par
@@ -247,12 +269,85 @@
\_public \verbinput ;
\_doc -----------------------------
+ If the language of your code\label[commentchars]\wlabel{}
+ printed by \^`\verbinput` supports the
+ format of comments started by two characters from beginning of the
+ line then you can set these characters by \^`\commentchars``<first><second>`.
+ Such comments are printed in non-verbatim mode without these two
+ characters and they look like the verbatim printing is interrupred
+ at the places where such comments are.
+ See the section~\ref[lua] for good illustration.
+ The file `optex.lua` is read by single command
+ `\verbinput (4-) optex.lua` here and the `\commentchars --` was set before it.
+
+ If you need to set a special character by \^`\commentchars` then you must
+ to set the catcode to 12 (and space to 13). Examples:
+ \begtt
+ \commentchars // % C++ comments
+ \commentchars -- % Lua comments
+ {\catcode`\%=12 \_ea}\commentchars %% % TeX comments
+ {\catcode`\#=12 \catcode`\ =13 \_ea}\commentchars#{ } % bash comments
+ \endtt
+
+ There is one limitation when \TeX/ interprets the comments declared by
+ \^`\commentchars`. Each block of comments are accummulated to one line
+ and then it is re-interpreted by \TeX. So, the ends of lines in the
+ comments block are lost. You cannot use macros which
+ need to scan end of lines, for example `\begtt...\endtt` inside comments
+ block does not work. The character `%` is ignored in comments but you can use `\%`
+ for printing or `%` alone for de-activating `\_endpar` from empty
+ comment lines.
+
+ Implementation: The \`\commentchars``<first><second>` redefines the \`\_testcommentchars`
+ used in \^`\_printverb` in order to it removes the following `\_iftrue`
+ and returns `\_iftrue` or `\_iffalse` depending on the fact that
+ the comment characters are or aren't present at the beginning ot tested line.
+ If it is true (`\ifnum` expands to `\ifnum 10>0`) then the rest of the
+ line is added to the \`\_vcomments` macro.
+
+ The \`\_hicomments` is `\relax` by default but it is redefined by \^`\commentchars`
+ in order to keep no-colorized comments if we need to use feature from
+ \^`\commentchars`.
+
+ The accumulated comments are printed whenever the non-comment line
+ occurs. This is done by \`\_printcomments` macro.
+ You can re-define it, but main idea must be kept: it is printed in the
+ group, `\_reloding \_rm` initializes normal font, `\catcodetable0`
+ returns to normal catcode table used before `\verbinput` is started, and
+ the text accumulated in `\_vcomments` must be printed by
+ `\_scantextokens` primitive.
+ \_cod -----------------------------
+
+\_def\_vcomments{}
+\_let\_hicomments=\_relax
+
+\_def\_commentchars#1#2{%
+ \_def\_testcommentchars ##1##2##3\_relax ##4\_iftrue{\_ifnum % not closed in this macro
+ \_ifx #1##1\_ifx#2##21\_fi\_fi 0>0
+ \_ifx\_relax##3\_relax \_addto\_vcomments{\_endgraf}% empty comment=\enfgraf
+ \_else \_addto\_vcomments{##3 }\_fi}%
+ \_def\_hicomments{\_replfromto{\b\n#1#2}{^^J}{\w{#1#2####1}^^J}}% used in \hisyntax
+}
+\_def\_testcommentchars #1\_iftrue{\_iffalse} % default value of \_testcommentchar
+\_def\_printcomments{\_ttskip
+ {\_catcodetable0 \_reloading \_rm \_everypar={}%
+ \_noindent \_ignorespaces \_scantextokens\_ea{\_vcomments}\_par}%
+ \_ttskip
+}
+\_public \commentchars ;
+
+ \_doc -----------------------------
The \`\visiblesp` sets spaces as visible characters \char9251.
- It redefines {\visiblesp`\ `} primitive, so it is useful for verbatim modes only.
+ It redefines the \^`\_dsp`, so it is useful for verbatim modes only.
+
+ The \`\_dsp` is equivalent to {\visiblesp`\ `} primitive.
+ It is used in all verbatim environments: spaces are active and defined as
+ `\_dsp` here.
\_cod -----------------------------
-\_def \_visiblesp{\_ifx\_initunifonts\_relax \_def\ {\_char9251 }%
- \_else \_def\ {\_char32 }\_fi}
+\_def \_visiblesp{\_ifx\_initunifonts\_relax \_def\_dsp{\_char9251 }%
+ \_else \_def\_dsp{\_char32 }\_fi}
+\_let\_dsp=\ % primitive "direct space"
\_public \visiblesp ;
@@ -261,6 +356,7 @@
\_endinput
History:
+2020-11-13 ... \commentchars implemented
2020-04-22 ... \ttshift introduced
2020-04-06 ... \visiblesp added
2020-04-04 ... ^^I activated as \t for multiline verbatim