Debian Codegraph @

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

raw
1 %% This is part of the OpTeX project, see http://petr.olsak.net/optex
2
3 \_codedecl \public {Prefixing and code syntax <2024-02-02>} % preloaded in format
4
5 \_doc ---------
6 All \TeX/ primitives have alternative control sequence `\_hbox` `\_string`, ...
7 \_cod ---------
8
9 \let\_directlua = \directlua
10 \_directlua {
11 % enable all TeX primitives with _ prefix
12 tex.enableprimitives('_', tex.extraprimitives('tex'))
13 % enable all primitives without prefixing
14 tex.enableprimitives('', tex.extraprimitives())
15 % enable all primitives with _ prefix
16 tex.enableprimitives('_', tex.extraprimitives())
17 }
18
19 \_doc ------------------
20 \`\ea` is useful shortcut for `\expandafter`. We recommend to use always the
21 private form of \`\_ea`
22 because there is high probability that `\ea` will be redefined by the user.
23 \nl
24 \`\public` `\<sequence> \<sequence> ... ;` does
25 `\let \<sequence> = \_<sequence>` for all sequences.
26 \nl
27 \`\private` `\<sequence> \<sequence> ...;` does
28 `\let \_<sequence> = \<sequence>` for all sequences.
29 \nl
30 \`\newpublic``<do>\<sequence>` prints warning if `\<sequence>`
31 is declared already. Then runs `<do>\<sequence>`.\nl
32 \`\_checkexists` `<where> {<sequence-string>}` prints error
33 if the control sequence given by its name <sequence-string>
34 is not declared. This check is used in `\public`, `\private`,
35 `\_nspublic` and `\_nsprivate` macros in order to avoid mistakes
36 in names when declaring new control sequences.
37 \nl
38 \`\xargs` `<what> <sequence> <sequence> ... ;`
39 does `<what><sequence>` for each sequences.
40 \_cod -----------------
41
42 \_let\_ea =\_expandafter % useful shortcut
43
44 \_long\_def \_xargs #1#2{\_ifx #2;\_else \_ea#1\_ea#2\_ea\_xargs \_ea #1\_fi}
45
46 \_def \_pkglabel{}
47 \_def \_public {\_xargs \_publicA}
48 \_def \_publicA #1{%
49 \_checkexists \public {_\_csstring#1}%
50 \_ea\_let \_ea#1\_csname _\_csstring #1\_endcsname
51 }
52 \_def \_private {\_xargs \_privateA}
53 \_def \_privateA #1{%
54 \_checkexists \private {\_csstring #1}%
55 \_ea\_let \_csname _\_csstring #1\_endcsname =#1%
56 }
57 \_def\_checkexists #1#2{\_unless \_ifcsname #2\_endcsname
58 \_errmessage {\_string#1: \_bslash#2 must be declared}\_fi
59 }
60 \_def\_newpublic #1#2{\_unless\_ifx #2\_undefined
61 \_opwarning{\_string#2 is redefined%
62 \_ifx\_pkglabel\_empty \_else\_space by the \_ea\_ignoreit\_pkglabel\_space package\_fi}\_fi
63 #1#2%
64 }
65 \_public \public \private \newpublic \xargs \ea ;
66
67 \_doc -----------------------------
68 We define the macros
69 \`\_namespace` `{<pkg label>}`, \`\_resetnamespace` `{<pkg label>}`, \`\_endnamespace`,
70 \`\_pkglabel`, \`\_nspublic`, and \`\_nsprivate`
71 for package writers, see section~\ref[pkg-namespace].{\hbadness=2200\par}
72 \_cod -----------------------------
73
74 \_def \_pkglabel{}
75 \_def\_namespace #1{%
76 \_ifcsname _namesp:#1\_endcsname \_errmessage
77 {The name space "#1" is used already, it cannot be used twice}%
78 \_endinput
79 \_else
80 \_ea \_gdef \_csname _namesp:#1\_endcsname {}%
81 \_resetnamespace{#1}\_fi
82 }
83 \_def\_resetnamespace #1{%
84 \_unless \_ifx \_pkglabel\_empty \_endnamespace \_fi
85 \_gdef \_pkglabel{_#1}%
86 \_directlua{
87 callback.add_to_callback("process_input_buffer",
88 function (str)
89 return string.gsub(str, "\_nbb[.]([a-zA-Z])", "\_nbb _#1_\_pcent 1")
90 end, "_namespace")
91 }%
92 }
93 \_def\_endnamespace {%
94 \_directlua{ callback.remove_from_callback("process_input_buffer", "_namespace") }%
95 \_gdef \_pkglabel{}%
96 }
97 \_def \_nspublic {\_xargs \_nspublicA}
98 \_def \_nspublicA #1{%
99 \_checkexists \_nspublic {\_pkglabel _\_csstring #1}%
100 \_ifcsname _eol:\_ea\_ignoreit\_pkglabel _\_csstring #1\_endcsname % defined by \eoldef
101 \_slet {_eol:\_csstring #1}{_eol:\_ea\_ignoreit\_pkglabel _\_csstring #1}\_fi
102 \_ea\_newpublic \_ea\_let \_ea#1\_csname \_pkglabel _\_csstring #1\_endcsname
103 }
104 \_def \_nsprivate {\_xargs \_nsprivateA}
105 \_def \_nsprivateA #1{%
106 \_checkexists \_nsprivate {\_csstring #1}%
107 \_ea\_let \_csname \_pkglabel _\_csstring #1\_endcsname =#1%
108 }
109
110 \_doc -----------------------
111 Each macro file should begin with \`\_codedecl` `\macro {<info>}`.
112 If the `\macro` is defined already then the `\endpinput` protects to read
113 such file more than once. Else the <info> is printed to the terminal
114 and the file is read.
115 The {\`\_endcode`} is defined as `\endinput` in the `optex.ini` file.
116 \`\wterm` `{<text>}` prints the `<text>` to the terminal and to the `.log` file,
117 \`\wlog` `{<text>}` prints the `<text>` only to the `.log` file (as in plain \TeX)
118 \_cod -----------------------
119
120 \_def \_codedecl #1#2{%
121 \_ifx #1\_undefined \_wlog{@:[\_basefilename\_currfile] #2}%
122 \_else \_ea \_endinput \_fi
123 }
124 \_def \_wterm {\_immediate \_write16 }
125 \_def \_wlog {\_immediate\_write-1 } % write on log file (only)
126
127 \_public \wterm \wlog ;
128
129 \_doc -----------------------------
130 \`\currfile` returns the name of the current input file including its path.\nl
131 \`\basefilename``\currfile` returns base name of
132 the current file, without its path and extension.\nl
133 \`\_nofilepath` `<text>/<with>/<slashes>/\_fin` expands to the last segment
134 separated by slashes.\nl
135 \`\_nofileext` `<filename>.\_fin` expands to the file name without extension.
136 \_cod -----------------------------
137
138 \_def\_currfile{\_directlua{tex.print(status.filename)}}
139 \_def\_basefilename #1{\_ea\_nofileext\_expanded{\_ea\_ea\_ea\_nofilepath#1/\_fin}.\_fin}
140 \_def\_nofilepath #1/#2{\_ifx#2\_fin #1\_else \_ea\_nofilepath \_ea#2\_fi}
141 \_def\_nofileext #1.#2\_fin{#1}
142
143 \_public \currfile \basefilename ;
144
145 \_doc ------------------------------
146 We define \`\_fin` as a useless macro. Suppose that its meaning will be never
147 used for another control sequence. You can use `\_fin` as a final delimiter
148 of a list of tokens and your macro can ask `\ifx\_fin#1` in order to
149 decide that the list of tokens is finalized.
150 \_cod ------------------------------
151
152 \_protected\_long \_def \_fin \_fin {}
153
154 \_endcode %----------------------------------------------------
155
156
157 \sec[basic-code] Basic principles of \OpTeX/ sources
158
159 \secc[namespaces] Concept of namespaces of control sequences
160
161 \OpTeX/ sets the category code of the \"`_`" character to 11 (letter)
162 and it is never changed.\fnote
163 {This is only singular exception form category codes given by plain \TeX.}
164 So, we can always construct multiletter control sequence names from letters
165 `A`--`Z`, `a`--`z`, and `_`. The \"letter `_`" works in math mode as a subscript
166 constructor because it is set as math active character (see section~\ref[math-macros]).
167
168 We distinguish following namespaces for multiletter control sequences:
169 \begitems
170 * Only alphabetical names are in the {\em public namespace}. They are intended
171 for end users when creating a document. Sometimes it is called {\em user
172 namespace} too. For example `\hbox`, `\fontfam`, `\MyMacro`.
173 * Only alphabetical lowercase names prefixed by single \"`_`" are in the
174 {\em private namespace}. It is used in \OpTeX/ internal macros.
175 For example `\_hbox`, `\_fontsel`.
176 * Names in the form `\_<pkg>_<name>` are in the {\em package namespace},
177 see section~\ref[pkg-namespace]. For example `\_qr_size`, `\_math_alist`.
178 * Names starting with two \"`_`" are in the {\em reserved namespace}. They can be
179 used for internal control sequences in font family files or in similar cases.
180 * Other names which include \"`_`" but not as the first character can be used
181 too, but with care, see the end of this section.
182 \enditems
183
184 All \TeX/ primitives are initialized with two control sequences with the
185 same meaning: {\em prefixed} control sequence
186 (in private namespace, for example `\_hbox`)
187 and {\em unprefixed} control sequence (in public namespace, for example `\hbox`).
188 All \OpTeX/ macros intended for end users are initialized in these two forms
189 too, for example `\_ref` and `\ref`.
190
191 Users can declare any control sequences in the public namespace without worrying
192 that \OpTeX/ behavior is changed. This is because \OpTeX/ uses exclusively
193 prefixed control sequences in its macros. For example, a user can declare
194 `\def\fi{finito}` and nothing bad happens, if the user doesn't use `\fi` in
195 its original primitive meaning. You don't have to know all \TeX/ primitives
196 and \OpTeX/ macros, you can declare control sequences for your use in the
197 public namespace without limitations and nothing bad will happen.
198
199 You can use control sequences from private or package namespace in
200 a \"read-only manner" without changing \OpTeX/ behavior too.
201 On the other hand, if you re-define a control sequence in the private name
202 space, the \OpTeX/ behavior can be changed. You can do it
203 but we suppose that you know what you are doing and what \OpTeX/
204 behavior is changed.
205
206 All multiletter control sequences declared by \OpTeX/ are defined in the private
207 namespace first (`\_def\_macro{...}`). If the declared control sequences are
208 intended for end users too then they are exported to the public namespace
209 after that. It is done by the \^`\public` macro:
210 \begtt \catcode`\<=13
211 \public <list of control sequences> ;
212 \endtt
213 For example \^`\public`` \foo \bar ;` does `\let\foo=\_foo`, `\let\bar=\_bar`.
214
215 There is an exception of the above mentioned principle. Control sequences
216 which are alternatives to math characters (`\alpha`, `\forall`, `\subset` etc.)
217 are declared only in public name space if they are not used in any internal
218 \OpTeX/ macros.
219
220 The macro \^`\private` does the reverse job of \^`\public` with the same syntax.
221 For example `\private \foo \bar ;` does `\let\_foo=\foo`, `\let\_bar=\bar`.
222 This should be used when an unprefixed variant of a control sequence is declared
223 already but we need the prefixed variant too.
224
225 In this documentation: if both variants of a control sequence are declared
226 (prefixed and unprefixed), then the accompanying text mentions only
227 the unprefixed variant. The code typically defines the prefixed variant
228 and then the \^`\public` (or `\_public`) macro is used.
229
230 The single-letter control sequences like `\%`, `\$`, `\^` etc. are not used
231 in internal macros. Users can redefine them, but (of course) some classical
232 features can be lost (printing percent character by `\%` for example).
233
234 It is very tempting to use control sequence names with `_` in order to
235 distinguish more words in the sequence name. If the first character isn't
236 `_` then such a name is outside private and package namespaces, so they can
237 be used for various purposes. For example `\my_control_sequence`. But there
238 is an exception: control sequences in the form `\<word>_` or `\<word>_<one-letter>`,
239 where <word> is a sequence of letters, are inaccessible, because they
240 are interpreted as `\<word>` followed by `_` or as `\<word>` followed by
241 `_<one-letter>`. This feature is activated because we want to write math
242 formulae as in plain \TeX, for example:
243 \begtt
244 \int_a^b ... is interpreted as \int _a^b
245 \max_M ... is interpreted as \max _M
246 \alpha_{ij} ... is interpreted as \alpha _{ij}
247 \endtt
248 It is implemented using Lua code at input processor level, see
249 the section~\ref[math-macros] for more details. You can deactivate this feature by
250 \^`\mathsboff`. After this, you can still write
251 `$`$\int$`_a^b$` (Unicode) or `$\int _a^b$` % $∫_a^b$ (Unicode) or $\int _a^b$
252 without problems but `\int``_a^b` yields to undefined control sequence
253 `\int``_a`. You can activate this feature again by \^`\mathsbon`. The
254 effect will take shape from next line read from input file.
255
256
257 \secc Macro files syntax
258
259 Segments of \OpTeX/ macros or external macro packages
260 are stored in files with `.opm` extension
261 (means OPtex Macros). Your local macros should be in a normal `*.tex` file.
262
263 The code in macro files starts by \^`\_codedecl` and ends by \^`\_endcode`.
264 The \^`\_endcode` is equivalent for `\endinput`, so documentation can follow.
265 The \^`\_codedecl` has syntax:
266
267 \begtt \catcode`<=13 \adef!{\string}
268 \_codedecl \sequence {<short title> !<<version>>}
269 \endtt
270
271 If the mentioned `\sequence` is undefined then \^`\_codedecl` prints the message
272 \begtt \catcode`<=13 \adef!{\string}
273 @:[<file name>] <short title> !<<version>>
274 \endtt
275 to the log file and \TeX/ continues with reading the following macros.
276 If the `\sequence` is defined, then \^`\_codedecl` acts like
277 `\endinput`: this protects from reading the file twice. We suppose, that
278 `\sequence` is defined in the macro file.
279
280 It is possible to use the \^`\_doc` ... \^`\_cod` pair between the macro
281 definitions. The documentation text should be here.
282 It is ignored when macros are read.
283
284 The \^`\_doc` ... \^`\_cod` parts can be printed after \^`\load[doc]`
285 using \~`\printdoc` macro, see section~\ref[doc].
286 If you have created a documented macro file `pkgname.opm` then you can
287 put macros for creating your documentation between
288 first pair of \^`\_doc` ... \^`\_cod` used after \^`\_endcode`.
289 These macros should \^`\load[doc]` and must be finished by \^`\bye`.
290 Then you have code+documentation together in a single file and
291 user can generate the documentation of your package by
292 \^`\docgen` used at command line:
293 \begtt
294 optex -jobname pkgname-doc '\docgen pkgname'
295 \endtt
296 Example of a \^`\_doc` ... \^`\_cod` code used for creating the
297 documentation using \^`\docgen` can be found in the `math.opm` file. You can see
298 \ulink[https://petr.olsak.net/ftp/olsak/optex/math-doc.pdf]{its documentation}, especially
299 \ulink[https://petr.olsak.net/ftp/olsak/optex/math-doc.pdf\#ref:pkgtemplate]
300 {section about creating packages}.
301
302 \secc[pkg-namespace] Name spaces for package writers
303
304 Package writer should use internal names in the form `\_<pgk>_<sequence>`,
305 where `<pkg>` is a package label. For example:
306 `\_qr_utfstring` from `qrcode.opm` package.
307
308 The package writer does not need to write repeatedly `\_pkg_foo` `\_pkg_bar`
309 etc.\ again and again in the macro file.\fnote
310 {We have not adopted the idea from expl3 language:)}
311 When the \^`\_namespace` `{<pkg>}`
312 is declared at the beginning of the macro file then all occurrences of
313 `\.foo` will be replaced by `\_<pkg>_foo` at the input processor level.
314 The macro writer can write (and backward can read his/her code) simply with
315 `\.foo`, `\.bar` control sequences and `\_<pkg>_foo`, `\_<pkg>_bar`
316 control sequences are processed internally.
317 The scope of the \^`\_namespace` command ends at the \^`\_endnamespace` command
318 or when another \^`\_namespace` is used. This command checks
319 if the same package label is not declared by the \^`\_namespace` twice.
320
321 \^`\_nspublic` `<list of sequences> ;`
322 does `\let\foo = \_<pkg>_foo` for each given sequence when
323 \^`\_namespace{<pkg>}` is declared. Moreover, it prints a warning if `\foo` is
324 defined already. The \^`\_nsprivate` macro does reverse
325 operation to it without warnings. Example: you can define `\def\.macro{...}` and then
326 set it to the public namespace by `\_nspublic \macro;`.
327
328 It could happen that a package writer needs to declare a control sequence
329 (say `\foo`) directly without setting it in `\_<pkg>_foo` namespace
330 followed by using \^`\_nspublic`. The \^`\newpublic` prefix should be used in this
331 case, for example `\_newpublic\_def\foo` or `\_newpublic\_chardef\foo` or
332 `\_newpublic{\_long\_def}\foo`. The \^`\newpublic``<do>\<sequence>` prints
333 a warning if the declared `\<sequence>` is defined already and then runs
334 `<do>\<sequence>`. The reason of the warning is the same as when
335 \^`\_nspublic` warns about doing re-declaration of control sequences already
336 declared.
337
338 Don't load other packages (which are using their own namespace) inside your
339 namespace. Do load them before your \^`\_namespace`~`{<pkg>}` is
340 initialized. Or close your namespace by \^`\_endnamespace` and open it again
341 (after other packages are loaded) by \^`\_resetnamespace`~`{<pkg>}`.
342
343 If the package writer needs to declare a control
344 sequence by \^`\newif`, then there is an exception of the rule described above. Use
345 \^`\_newifi\_if<pkg>_bar`, for example `\_newifi\_ifqr_incorner`.
346 Then the control sequences `\_qr_incornertrue` and
347 `\_qr_incornerfalse` can be used (or the sequences `\.incornertrue`
348 and `\.incornerfalse` when `\_namespace{qr}` is used).
349
350
351 \secc Summary about rules for external macro files published for \OpTeX/
352
353 If you are writing a macro file that is intended to be published for
354 \OpTeX/, then you are greatly welcome. You should follow these rules:
355
356 \begitems
357 * Don't use control sequences from the public namespace in the macro
358 bodies if there is no explicit and documented reason to do this.
359 * Don't declare control sequences in the public namespace if there are no
360 explicit and documented reasons to do this.
361 * Use control sequences from \OpTeX/ and primitive namespace
362 in read-only mode, if there is not an explicit and documented reason to
363 redefine them.
364 * Use `\_<pkg>_<name>` for your internal macros or `\.<name>` if the
365 \^`\_namespace{<pkg>}` is declared. See section~\ref[pkg-namespace].
366 * Use \^`\load` (or better: `\_load`) for loading more external macros
367 if you need them. Don't use `\_input` explicitly in such cases.
368 The reason is: the external macro file is not loaded
369 twice if another macro or the user needs it explicitly too.
370 * Use \^`\_codedecl` as your first command in the macro file and
371 \^`\_endcode` to close the text of macros.
372 * Use \^`\_doc` ... \^`\_cod` pairs for documenting the code pieces.
373 * You can write more documentation after the \^`\_endcode` command.
374 * The \OpTeX/ catcodes are set when \^`\load` your package (i.e. plain \TeX/
375 catcodes plus catcode of `_` is 11). If a catcode is changed during
376 loading your package then it is forgot because \^`\load` returns to catcodes
377 used before loading package. If you want to offer a catcode changing for
378 users then insert it to a macro which can be used after loading.
379 \enditems
380
381 If the macro file accepts these recommendations then it should be
382 named by `<filename>.opm` where `<filename>` differs from file names
383 used directly in \OpTeX/ and from other published macros.
384 This extension `.opm` has precedence before `.tex` when
385 the \^`\load` macro is used.
386
387 The `math.opm` is a good example of how an external macro file for \OpTeX/
388 can look like. Another good and short example is
389 \ulink[https://tex.stackexchange.com/questions/684406\#684406]{here}.
390
391 \endinput
392
393 2024-02-02 \_nspublicA bug correction (#1 may be set as \outer)
394 2024-01-18 \_nspublic modified in order to \eoldef+\bracedparam
395 2023-01-30 doc. about namespaces rewritten
396 2022-11-25 \_resetnamespace: moved \gdef\_namesp:#1 {} to \_namespace
397 2022-11-24 \newpublic introduced, \_checkexists with only two parameters.
398 2022-11-22 \currfile introduced and used in \_codedecl
399 2022-11-13 \_resetnamespace corrected
400 2021-08-16 \_wlog moved from basic macros, \_codedecl uses \_wlog only.
401 2021-04-25 \_checkexists introduced
402 2021-02-15 \_expandafter -> \_ea in \_codedecl
403 2020-02-14 released