Debian Codegraph @

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

raw
1 %% This is part of the OpTeX project, see http://petr.olsak.net/optex
2
3 \_codedecl \readkv {Key-value dictionaries <2023-11-24>} % preloaded in format
4
5 \_doc ----------------------------
6 {\bf Implementation.}\nl
7 The \`\readkv``<list>` expands its parameter and does replace-strings in order to
8 remove spaces around equal signs and commas.
9 Then \`\_kvscan` reads the parameters list finished by `,\_fin`
10 and saves values to `\_kv:<dict>:<key>` macros.
11 The `\_kvx:<dict>:<key>` is processed (if it is defined) with parameter
12 <value> after it.\nl
13 The \`\kvx``{<key>}{<code>}` defines the `\_kvx:<dict>:<key>#1` macro
14 and \`\nokvx``{<code>}` defines the `\_nokvx:<dict>:<key>` macro.\nl
15 The \`\trykv``{<key>}{<code>}`
16 returns unexpanded value of <key> if declared, else it runs <code>.\nl
17 The \`\kv``{<key>}` returns expanded value of <key> if declared,
18 else returns expanded value of `.notdef` key else expands \`\_kvunknown`.\nl
19 The \`\iskv``{<key>}\iftrue` (or `\iffalse`) is the test,
20 if the `<key>` is defined in current <dict>.
21 \_cod ----------------------------
22
23 \_def\_readkv#1{\_ea\_def\_ea\_tmpb\_ea{#1,}%
24 \_replstring\_tmpb{= }{=}\_replstring\_tmpb{ =}{=}\_replstring\_tmpb{ ,}{,}%
25 \_ea \_nospaceafter \_ea\_kvscan\_tmpb\_fin}
26 \_def\_kvscan#1,#2{\_ifx^#1^\_else \_kvsd #1==\_fin \_fi
27 \_ifx\_fin#2\_empty \_ea\_ignoreit \_else\_ea\_useit \_fi {\_kvscan#2}}
28 \_def\_kvsd#1=#2=#3\_fin{\_sdef{\_kvcs#1}{#2}%
29 \_trycs{_kvx:\_the\_kvdict:#1}%
30 {\_trycs{_nokvx:\_the\_kvdict}{\_ea\_ignoreit}{#1}\_ea\_ignoreit}{#2}}
31 \_def\_kvx#1#2{\_sdef{_kvx:\_the\_kvdict:#1}##1{#2}}
32 \_def\_nokvx#1{\_sdef{_nokvx:\_the\_kvdict}##1\_ea\_ignoreit##2{#1}}
33 \_def\_trykv#1{\_ea\_trykvA \_begincsname\_kvcs#1\_endcsname \_ignoreit}
34 \_def\_trykvA#1{\_ifx #1\_ignoreit \_ea\_useit\_else \_unexpanded\_ea{#1}\_fi}
35 \_def\_kv#1{\_expanded{\_trykv{#1}{\_trykv{.\_csstring\notdef}{\_kvunknown}}}}
36 \_def\_iskv#1#2{#2\_else\_ea\_unless\_fi \_ifcsname\_kvcs#1\_endcsname}
37 \_def\_kvcs{_kv:\_the\_kvdict:}
38 \_def\_kvunknown{???}
39
40 \public \readkv \kvx \nokvx \kv \trykv \iskv ;
41
42 \_endcode
43
44 Users or macro programmers can define macros with options in key=value format.
45 It means a comma-separated list of equations key=value. First, we give an
46 example.
47
48 Suppose that you want to define a macro `\myframe` with options: color of
49 rules, color of text inside the frame, rule-width, space between text and
50 rules. You want to use this macro as:
51
52 \begtt
53 \myframe [margins=5pt,rule-width=2pt,frame-color=\Red,text-color=\Blue] {text1}
54 or
55 \myframe [frame-color=\Blue] {text2} % other parameters are default
56 \endtt
57 %
58 or simply `\myframe {text3}`. You can define `\myframe` as follows:
59
60 \begtt
61 \def\myframedefaults{% defaults:
62 frame-color=\Black, % color of frame rules
63 text-color=\Black, % color of text inside the frame
64 rule-width=0.4pt, % width of rules used in the frame
65 margins=2pt, % space between text inside and rules.
66 }
67 \optdef\myframe [] #1{\bgroup
68 \readkv\myframedefaults \readkv{\the\opt}%
69 \rulewidth=\kv{rule-width}
70 \hhkern=\kv{margins}\vvkern=\kv{margins}\relax
71 \kv{frame-color}\frame{\kv{text-color}\strut #1}%
72 \egroup
73 }
74 \endtt
75 %
76 We recommend using \^`\optdef` for defining macros with optional parameters
77 written in `[]`. Then the optional parameters are saved in the \^`\opt` tokens
78 register. First: we read default parameters by \^`\readkv\myframedefaults`
79 and secondly the actual parameters are read by \^`\readkv{\the\opt}`.
80 The last setting wins.
81 Third: the values can be used by the expandable \^`\kv{<key>}` macro.
82 The \^`\kv{<key>}` returns `???` if such a <key> isn't declared
83 but if `.notdef` key is declared then its value is returned in this case.
84
85 The \^`\kv{<key>}` macro fully expands the value of key. If you don't want to expand
86 the value, use \^`\trykv``{<key>}{<code>}`. It returns unexpanded value of <key> if
87 declared else returns expanded <code>. For example
88 `\edef\macro{\trykv{key}{}}` defines `\macro` as the unexpanded value of the `key`.
89
90 You can use keys without values in the parameters list too.
91 Then you can ask if the key is declared by \^`\iskv``{<key>}\iftrue`
92 or the key is undeclared by \^`\iskv``{<key>}\iffalse`.
93 For example, you write to your documentation of your code that
94 user can set the `draft` option without the value. Then you can do
95 \begtt
96 \optdef\myframe [] #1{...
97 \readkv\myframedefaults \readkv{\the\opt}%
98 \iskv{draft}\iftrue ...draft mode... \else ...final mode... \fi
99 ...}
100 \endtt
101 Maybe, you want to allow not only `draft` option but `final` option (which is
102 opposite to `draft`) too and you want to apply the result from the last given
103 option. Then `\iskv` doesn't work because you can only check if both options
104 are declared but you don't know what one is given as last. But you can
105 use \^`\kvx``{<key>}{<code>}` to declare <code> which is processed
106 immediately when the `<key>` is processed by `\readkv`. For example
107 \begtt
108 \newcount\mydraftmode
109 \kvx{draft}{\mydraftmode=1 }
110 \kvx{final}{\mydraftmode=0 }
111 \optdef\myframe [] #1{...
112 \readkv\myframedefaults \readkv{\the\opt}%
113 \ifnum\mydraftmode=1 ...draft mode... \else ...final mode... \fi
114 ...}
115 \endtt
116 The syntax of \^`\kvx` `{<key>}{<code>}` allows to use `#1` inside the code.
117 It is replaced by the actual `<value>`. Example: `\kvx{opt}{\message{opt is #1}}`,
118 then `\readkv{opt=HELLO}` prints \"opt is HELLO".
119
120 The \^`\nokvx` `{<code>}` can declare a <code> processed for all <keys>
121 undeclared by \^`\kvx`. The `#1` and `#2` can be used in the <code>,
122 `#1` is <key>, `#2` is <value>. If `\nokvx` is unused then nothing is done
123 for undeclared <key>. Example: `\nokvx{\opwarning{Unknown option "#1"}}`.
124
125 The default dictionary name (where key-value pairs are processed) is
126 empty. You can use your specific dictionary by
127 \^`\kvdict``={<name>}`. Then `\redakv`, `\kv`, `\trykv`, `\iskv`, `\kvx` and `\nokvx`
128 macros use this named dictionary of <key>/<value> pairs.
129 Package options can be processed when
130 `\kvdict={pkg:<pkg>}`, example is the `\mathset` macro in
131 \ulink[https://petr.olsak.net/ftp/olsak/optex/math-doc.pdf]{{\tt math.opm} package}.
132
133 Recommendation: If the value of the key-value pair includes `=` or `,` or
134 `]`, then use the syntax `<key>={<value>}`.
135
136 A more extensive example can be found in
137 \ulink[http://petr.olsak.net/optex/optex-tricks.html\#roundframe]{\OpTeX/ trick 0073}.
138
139 \_endinput
140
141 2023-11-24 \trykv introduced.
142 2023-10-23 minor changes in \readkv, empty key isn't processed.
143 2023-03-11 \nokvx: \fi bug (due to \afterfi in \trycs) fixed.
144 2023-01-13 \kvx parameter added, \nokvx introduced.
145 2023-01-07 \kvdict, \kvx, \iskv added.
146 2020-12-21 Released