Debian Codegraph @

texmf-dist/tex/optex/base/hi-syntax.opm

raw
1 %% This is part of the OpTeX project, see http://petr.olsak.net/optex
2
3 \_codedecl \hisyntax {Syntax highlighting of verbatim listings <2022-04-04>} % preloaded in format
4
5 \_doc -----------------------------
6 The macros `\replfromto` and `\replthis` manipulate the
7 verbatim text that is already stored in the `\_tmpb` macro.
8
9 \`\replfromto` `{<from>}{<to>}{<replacement>}` finds the first
10 occurrence of `<from>` and the first occurrence of `<to>` following it. The
11 `<text>` between them is packed into `#1` and available to `<replacement>`
12 which ultimately replaces `<text>`.
13
14 `\replfromto` continues by finding next `<from>`, then, next `<to>`
15 repeatedly over the whole verbatim text. If the verbatim text ends with
16 opening `<from>` but has no closing `<to>`, then `<to>` is appended to the
17 verbatim text automatically and the last part of the verbatim text is replaced too.
18
19 The first two parameters are expanded before use of `\replfromto`. You can
20 use `\csstring\%` or something else here.
21 \_cod -----------------------------
22
23 \_def\_replfromto #1#2{\_edef\_tmpa{{#1}{#2}}\_ea\_replfromtoE\_tmpa}
24 \_def\_replfromtoE#1#2#3{% #1=from #2=to #3=replacement
25 \_def\_replfrom##1#1##2{\_addto\_tmpb{##1}%
26 \_ifx\_fin##2\_ea\_replstop \_else \_afterfi{\_replto##2}\_fi}%
27 \_def\_replto##1#2##2{%
28 \_ifx\_fin##2\_afterfi{\_replfin##1}\_else
29 \_addto\_tmpb{#3}%
30 \_afterfi{\_replfrom##2}\_fi}%
31 \_def\_replfin##1#1\_fin{\_addto\_tmpb{#3}\_replstop}%
32 \_edef\_tmpb{\_ea}\_ea\_replfrom\_tmpb#1\_fin#2\_fin\_fin\_relax
33 }
34 \_def\_replstop#1\_fin\_relax{}
35 \_def\_finrepl{}
36
37 \_doc -----------------------------
38 The \`\replthis` `{<pattern>}{<replacement>}` replaces each `<pattern>`
39 by `<replacement>`.
40 Both parameters of `\replthis` are expanded first.
41 \_cod -----------------------------
42
43 \_def\_replthis#1#2{\_edef\_tmpa{{#1}{#2}}\_ea\_replstring\_ea\_tmpb \_tmpa}
44
45 \_public \replfromto \replthis ;
46
47 \_doc -----------------------------
48 The patterns `<from>`, `<to>` and `<pattern>` are not found when they are
49 hidden in braces `{...}`. E.g.
50 \begtt
51 \replfromto{/*}{*/}{\x C{/*#1/*}}
52 \endtt
53 replaces all C comments by `\x C{...}`. The patterns inside `{...}` are
54 not used by next usage of `\replfromto` or `\replthis` macros.
55
56 The \`\_xscan` macro replaces occurrences of `\x` by `\z` in the post-processing
57 phase. The construct `\x <letter>{<text>}` expands to `\_xscan {<letter>}<text>^^J^`.
58 If `#3` is `\_fin` then it signals that something wrong happens, the
59 `<from>` was not terminated by legal `<to>` when `\replfromto` did work.
60 We must to fix this by using the \`\_xscanR` macro.
61 \_cod -----------------------------
62
63 \_def\_xscan#1#2^^J#3{\_ifx\_fin#3 \_ea\_xscanR\_fi
64 \z{#1}{#2}%
65 \_ifx^#3\_else ^^J\_afterfi{\_xscan{#1}#3}\_fi}
66 \_def\_xscanR#1\_fi#2^{^^J}
67
68 \_doc -----------------------------
69 The \`\hicolor` `<letter> <color>` defines `\_z:<letter>{<text>}`
70 as `{<color><text>}`. It should be used in the context of
71 `\x <letter>{<text>}` macros.
72 \_cod -----------------------------
73
74 \_def\_hicolor #1#2{\_sdef{_z:#1}##1{{#2##1}}}
75
76 \_doc -----------------------------
77 \`\hisyntax``{<name>}` re-defines default \^`\_prepareverbdata``<macro><verbtext>`,
78 but in order to do it does more things:
79 It saves `<verbtext>` to `\_tmpb`, appends `\n` around spaces and
80 `^^J` characters in pre-processing phase, opens `hisyntax-<name>.opm`
81 file if `\_hisyntax<name>` is not defined. Then `\_the\_hisyntax<name>`
82 is processed. Finally, the post-processing phase is realized by setting
83 appropriate values to the `\x` and `\y` macros and doing
84 `\_edef\_tmpb{\_tmpb}`.
85 \_cod -----------------------------
86
87 \_def\_hisyntax#1{\_def\_prepareverbdata##1##2{%
88 \_let\n=\_relax \_let\b=\_relax \_def\t{\n\_noexpand\t\n}\_let\_start=\_relax
89 \_adef{ }{\n\_noexpand\ \n}\_edef\_tmpb{\_start^^J##2\_fin}%
90 \_replthis{^^J}{\n^^J\b\n}\_replthis{\b\n\_fin}{\_fin}%
91 \_let\x=\_relax \_let\y=\_relax \_let\z=\_relax \_let\t=\_relax
92 \_hicomments % keeps comments declared by \commentchars
93 \_endlinechar=`\^^M
94 \_lowercase{\_def\_tmpa{#1}}%
95 \_ifcsname _hialias:\_tmpa\_endcsname \_edef\_tmpa{\_cs{_hialias:\_tmpa}}\_fi
96 \_ifx\_tmpa\_empty \_else
97 \_unless \_ifcsname _hisyntax\_tmpa\_endcsname
98 \_isfile{hisyntax-\_tmpa.opm}\_iftrue \_opinput {hisyntax-\_tmpa.opm} \_fi\_fi
99 \_ifcsname _hisyntax\_tmpa\_endcsname
100 \_ifcsname hicolors\_tmpa\_endcsname
101 \_cs{_hicolors\_tmpa}=\_cs{hicolors\_tmpa}%
102 \_fi
103 \_ea\_the \_csname _hisyntax\_tmpa\_endcsname % \_the\_hisyntax<name>
104 \_the\_hicolors % colors which have precedece
105 \_else\_opwarning{Syntax "\_tmpa" undeclared (no file hisyntax-\_tmpa.opm)}
106 \_fi\_fi
107 \_replthis{\_start\n^^J}{}\_replthis{^^J\_fin}{^^J}%
108 \_def\n{}\_def\b{}\_adef{ }{\_dsp}%
109 \_bgroup \_lccode`\~=`\ \_lowercase{\_egroup\_def\ {\_noexpand~}}%
110 \_def\w####1{####1}\_def\x####1####2{\_xscan{####1}####2^^J^}%
111 \_def\y####1{\_ea \_noexpand \_csname ####1\_endcsname}%
112 \_edef\_tmpb{\_tmpb}%
113 \_def\z####1{\_cs{_z:####1}}%
114 \_def\t{\_hskip \_dimexpr\_tabspaces em/2\_relax}%
115 \_localcolor
116 }}
117 \_public \hisyntax \hicolor ;
118
119 \_doc -----------------------------
120 Aliases for languages can be declared like this.
121 When `\hisyntax{xml}` is used then this is the same as `\hisyntax{html}`.
122 \_cod -----------------------------
123
124 \_sdef{_hialias:xml}{html}
125 \_sdef{_hialias:json}{c}
126
127 \_endcode % -------------------------------------------
128
129 The user can write
130
131 \begtt \adef/{\_csstring\\}
132 \begtt \hisyntax{C}
133 ...
134 /endtt
135 \endtt
136 to colorize the code using C syntax.
137 The user can also write `\everytt={\hisyntax{C}}` to have all verbatim listings
138 colorized.
139
140 \^`\hisyntax``{<name>}` reads the file `hisyntax-<name>.opm` where the
141 colorization is declared. The parameter `<name>` is case insensitive and the
142 file name must include it in lowercase letters. For example, the file
143 `hisyntax-c.opm` looks like this:
144
145 \printdoc hisyntax-c.opm
146
147 \OpTeX/ provides `hisyntax-{c,lua,python,tex,html,kt}.opm` files.
148 You can take inspiration from these files and declare more languages.
149
150 Users can re-declare default colors by \^`\hicolors``={<list of color declarations>}`.
151 This value has precedence over `\_hicolors<name>` values declared in the
152 `hicolors-<name>.opm` file.
153 For example \^`\hicolors``={`\^`\hicolor`` S \Brown}` causes all strings in brown color.
154
155 Another way to set non-default colors is to declare
156 `\newtoks\hicolors<name>` (without the `_` prefix) and set the color palette there.
157 It has precedence before `\_hicolors<name>` (with the `_` prefix) declared in
158 the `hicolors-<name>.opm` file. You must re-declare all colors used in the
159 corresponding `hisyntax-<name>.opm` file.
160
161 \secl4 Notes for hi-syntax macro writers
162 The file `hisyntax-<name>.opm` is read only once and in a \TeX/ group.
163 If there are definitions then they must be declared as global.
164
165 The file `hisyntax-<name>.opm` must (globally) declare `\_hisyntax<name>` token list
166 where the action over verbatim text is declared typically by using the `\replfromto` or
167 `\replthis` macros.
168
169 The verbatim text is prepared by the {\em pre-processing phase}, then
170 `\_hisyntax<name>` is applied and then the {\em post-processing phase} does final
171 corrections. Finally, the verbatim text is printed line by line.
172
173 The pre-processing phase does:
174
175 \begitems
176 * Each space is replaced by {\visiblesp`\n\ \n`}, so `\n<word>\n` is the pattern for
177 matching whole words (no subwords). The `\n` control sequence is removed in
178 the post-processing phase.
179 * Each end of line is represented by `\n^^J\n`.
180 * The `\_start` control sequence is added before the verbatim text and the `\_end` control
181 sequence is appended to the end of the verbatim text. Both are removed
182 in the post-processing phase.
183 \enditems
184
185 Special macros are working only in a group when processing the verbatim
186 text.
187
188
189 \begitems
190 * `\n` represents nothing but it should be used as a boundary of words as mentioned above.
191 * `\t` represents a tabulator. It is prepared as `\n\t\n` because it can be at
192 the boundary word boundary.
193 * `\x <letter>{<text>}` can be used as replacing text. Consider the example
194 \begtt
195 \replfromto{/*}{*/}{\x C{/*#1*/}}
196 \endtt
197 This replaces all C comments `/*...*/` by `\x C{/*...*/}`. But
198 C comments may span multiple lines, i.e. the `^^J` should be inside it.
199
200 The macro `\x <letter>{<text>}` is replaced by one or more occurrences of
201 `\z <letter>{<text>}` in the post-processing phase, each parameter `<text>` of
202 `\z` is from from a single line. Parameters not crossing line boundary are represented
203 by `\x C{<text>}` and replaced by `\z C{<text>}` without any change.
204 But:
205 \begtt \catcode`\<=13
206 \x C{<text1>^^J<text2>^^J<text3>}
207 \endtt
208 is replaced by
209 \begtt \catcode`\<=13
210 \z C{<text1>}^^J\z C{<text2>}^^J\z C{<text3>}
211 \endtt
212 `\z <letter>{<text>}` is expanded to `\_z:<letter>{<text>}` and if
213 `\hicolor <letter> <color>` is declared then
214 `\_z:<letter>{<text>}` expands to `{<color><text>}`. So, required color is
215 activated for each line separately (e.g. for C comments spanning multiple lines).
216 * `\y {<text>}` is replaced by `\<text>` in the post-processing phase. It should
217 be used for macros without a parameters. You cannot use unprotected macros
218 as replacement text before the post-processing phase, because the post-processing
219 phase is based on the expansion of the whole verbatim text.
220 \enditems
221
222 \endinput
223
224 2022-05-12 \hicolors can include only selected re-declarations.
225 2020-04-04 Introduced.