Debian Codegraph @

texmf-dist/tex/latex/pgfkeysearch/pgfkeysearch.sty

raw
1 %%%==============================================================================
2 %% Copyright 2023-present by Alceu Frigeri
3 %%
4 %% This work may be distributed and/or modified under the conditions of
5 %%
6 %% * The [LaTeX Project Public License](http://www.latex-project.org/lppl.txt),
7 %% version 1.3c (or later), and/or
8 %% * The [GNU Affero General Public License](https://www.gnu.org/licenses/agpl-3.0.html),
9 %% version 3 (or later)
10 %%
11 %% This work has the LPPL maintenance status *maintained*.
12 %%
13 %% The Current Maintainer of this work is Alceu Frigeri
14 %%
15 %% This is version {1.5} {2025/11/06}
16 %%
17 %% The list of files that compose this work can be found in the README.md file at
18 %% https://ctan.org/pkg/pgfkeysearch
19 %%
20 %%%==============================================================================
21 \NeedsTeXFormat{LaTeX2e}[2022/06/01]
22
23
24 \ProvidesExplPackage
25 {pgfkeysearch}
26 {2025/11/06}
27 {1.5}
28 {pgfkeys Search Extension}
29
30 \ExplSyntaxOn
31 %%%%%%%
32 %%%
33 %%% Just an attempt of having my packages info in a regular way
34 %%% \Pkginfograb_set:nn {<pkg-name>} { props} for each and all.
35 %%%
36 %%%%%%%
37 \RequirePackage{pkginfograb}
38 \pkginfograb_set:nn { pgfkeysearch }
39 {
40 name = {pgfkeysearch} ,
41 prefix = {pgfkeysearch} ,
42 date = {2025/11/06},
43 version = {1.5} ,
44 description = {pgfkeys~ search~ extension}
45 }
46 %%%%%%%
47 %%% End of cut-n-paste
48 %%%%%%%
49
50
51 \msg_new:nnnn {pgfkeysearch} {deprecated}
52 {
53 (ID:#1)~The~ command~#2~is~ deprecated~ use~ #3 ~ instead
54 }
55 {
56 You~tried~to~use~a~deprecated~ command:#2.
57 Use~ #3~ instead.
58 ~Error~Code~ ID:<#1>.
59 }
60
61
62 \cs_set_eq:NN \__pgfkeysearch_exp_arg:NNw \exp_args:NNv
63 \cs_set_eq:NN \__pgfkeysearch_tl_clear:N \tl_clear:N
64 \cs_set_eq:NN \__pgfkeysearch_if_root_search:Nn \use_none:nn
65
66 \keys_define:nn { pgfkeysearch }
67 {
68 root~ search .usage:n = general ,
69 root~ search .choices:nn =
70 { {} , true , false }
71 {
72 \str_if_eq:VnTF \l_keys_choice_str {false}
73 { \cs_set_eq:NN \__pgfkeysearch_if_root_search:Nn \use_none:nn }
74 { \cs_set_eq:NN \__pgfkeysearch_if_root_search:Nn \seq_put_right:Nn }
75 } ,
76
77 key .usage:n = general,
78 key .choice:,
79 key / value .code:n = { \cs_set_eq:NN \__pgfkeysearch_exp_arg:NNw \exp_args:NNv } ,
80 key / macro .code:n = { \cs_set_eq:NN \__pgfkeysearch_exp_arg:NNw \exp_args:NNc } ,
81 key .value_required:n = true ,
82
83 settings .usage:n = general,
84 settings .choice:,
85 settings / old .code:n =
86 {
87 \cs_set_eq:NN \__pgfkeysearch_tl_clear:N \use_none:n
88
89 \cs_set_eq:NN \pgfkeysearchvalueof \__pgfkeysearch_old_valueof:nnN
90 \cs_set_eq:NN \pgfkeysearchvalueofTF \__pgfkeysearch_old_valueofTF:nnNTF
91 \cs_set_eq:NN \pgfkeygetvalueof \__pgfkeysearch_old_getvalueof:nnN
92 \cs_set_eq:NN \pgfkeygetvalueofTF \__pgfkeysearch_old_getvalueofTF:nnNTF
93 \cs_set_eq:NN \pgfkeysearch \pgfkeysearchvalueof
94 \cs_set_eq:NN \pgfkeysearchTF \pgfkeysearchvalueofTF
95 \cs_set_eq:NN \pgfkeyget \pgfkeygetvalueof
96 \cs_set_eq:NN \pgfkeygetTF \pgfkeygetvalueofTF
97 } ,
98 settings / new .code:n =
99 {
100 \cs_set_eq:NN \__pgfkeysearch_tl_clear:N \tl_clear:N
101
102 \cs_set_eq:NN \pgfkeysearchvalueof \pgfkeysearch_multipath_keysearch:nnN
103 \cs_set_eq:NN \pgfkeysearchvalueofTF \pgfkeysearch_multipath_keysearch:nnNTF
104 \cs_set_eq:NN \pgfkeygetvalueof \pgfkeysearch_keyget:nnN
105 \cs_set_eq:NN \pgfkeygetvalueofTF \pgfkeysearch_keyget:nnNTF
106 \cs_set_eq:NN \pgfkeysearch \pgfkeysearchvalueof
107 \cs_set_eq:NN \pgfkeysearchTF \pgfkeysearchvalueofTF
108 \cs_set_eq:NN \pgfkeyget \pgfkeygetvalueof
109 \cs_set_eq:NN \pgfkeygetTF \pgfkeygetvalueofTF
110 } ,
111 settings .value_required:n = true ,
112 }
113
114
115 \cs_new_protected:Npn \pgfkeysearch_settings:n #1
116 {
117 \keys_set:nn {pgfkeysearch}{#1}
118 }
119
120 %%%%%%%%%%%%%%%
121 %%%
122 %%% _keysearch allows to find a key,
123 %%% if it's defined 'anywhere' in the tree. I mean, upwards until, likely, /some/<key>
124 %%% if it isn't defined, returns nothing (no error triggered)
125 %%%
126 %%%%%%%%%%%%%%%
127 \seq_new:N \l__pgfkeysearch_pathterms_seq
128 \seq_new:N \l__pgfkeysearch_pathtree_seq
129 \tl_new:N \l__pgfkeysearch_path_tl
130
131 \cs_generate_variant:Nn \tl_set:Nn {Ne}
132
133
134 %%%%%%%%%%%%%%%
135 %%%
136 %%% This will create a pair
137 %%% *none* expandable
138 %%% #cmd:signature# => cs
139 %%% #cmd:signature#TF => prg
140 %%%
141 %%%%%%%%%
142 %%%
143 %%% #1 -> #cmd:signature# => base-name
144 %%% #2 -> base-code
145 %%%
146 %%%%%%%%%%%%%%%
147 \regex_const:Nn \c__pgfkeysearch_rtn_regex {\c{__pgfkeysearch_set_rtn_true:}|\c{__pgfkeysearch_set_rtn_false:}}
148 \cs_new_protected:Npn \__pgfkeysearch_duo_new:Nn #1#2
149 {
150 \tl_set:Nn \l__pgfkeysearch_tmpa_tl {#2}
151 \regex_replace_all:NnN \c__pgfkeysearch_rtn_regex {} \l__pgfkeysearch_tmpa_tl
152 \__pgfkeysearch_duo_aux:NVn #1 \l__pgfkeysearch_tmpa_tl {#2}
153 }
154
155 \cs_new_protected:Npn \__pgfkeysearch_duo_aux:Nnn #1#2#3
156 {
157 \cs_new_protected:Nn #1
158 { #2 }
159 \prg_new_protected_conditional:Nnn #1 {T , F , TF}
160 {
161 #3
162 \__pgfkeysearch_return:
163 }
164 }
165 \cs_generate_variant:Nn \__pgfkeysearch_duo_aux:Nnn {NV}
166
167
168 %%%%%%%%%%%%%%%
169 %%% so that they can be easily removed (if needed) by _duo_new:Nn
170 %%%%%%%%%%%%%%%
171 \cs_new:Npn \__pgfkeysearch_set_rtn_true:
172 { \cs_set_eq:NN \__pgfkeysearch_return: \prg_return_true: }
173 \cs_new:Npn \__pgfkeysearch_set_rtn_false:
174 { \cs_set_eq:NN \__pgfkeysearch_return: \prg_return_false: }
175
176 %%%%%%%%%%%%%%%
177 %%%
178 %%% This is, basicaly, \pgfkeysvalueof
179 %%% but, assigning the found value (if any) to #3
180 %%%
181 %%%%%%%%%%%%%%%
182 \__pgfkeysearch_duo_new:Nn \pgfkeysearch_keyget:nnN
183 {
184 \cs_if_exist:cTF {pgfk@#1/#2}
185 {
186 \__pgfkeysearch_exp_arg:NNw \tl_set:Nn #3 {pgfk@#1/#2}
187 \__pgfkeysearch_set_rtn_true:
188 }
189 {
190 \__pgfkeysearch_tl_clear:N #3
191 \__pgfkeysearch_set_rtn_false:
192 }
193 }
194
195 %%%%%%%%%%%%%%%
196 %%%
197 %%% Given a single path /A/B/C/D it will look after
198 %%% /A/B/C/D/<key>
199 %%% /A/B/C/<key>
200 %%% /A/B/<key>
201 %%% /A/<key>
202 %%% /<key> (if "root search" set)
203 %%% stoping at the first hit
204 %%%
205 %%%%%%%%%%%%%%%
206 \__pgfkeysearch_duo_new:Nn \pgfkeysearch_keysearch:nnN
207 {
208 \seq_set_split:Nne \l__pgfkeysearch_pathterms_seq {/} {#1}
209 \seq_remove_all:Nn \l__pgfkeysearch_pathterms_seq {}
210 \tl_clear:N \l__pgfkeysearch_path_tl
211 \seq_clear:N \l__pgfkeysearch_pathtree_seq
212
213 \__pgfkeysearch_if_root_search:Nn
214 \l__pgfkeysearch_pathtree_seq {}
215
216 \seq_map_inline:Nn \l__pgfkeysearch_pathterms_seq
217 {
218 \tl_put_right:Nn \l__pgfkeysearch_path_tl {/##1}
219 \seq_put_right:NV \l__pgfkeysearch_pathtree_seq \l__pgfkeysearch_path_tl
220 }
221 \seq_reverse:N \l__pgfkeysearch_pathtree_seq
222
223 \__pgfkeysearch_set_rtn_false:
224 \__pgfkeysearch_tl_clear:N #3
225
226 \seq_map_inline:Nn \l__pgfkeysearch_pathtree_seq
227 {
228 \cs_if_exist:cTF {pgfk@##1/#2}
229 {
230 \__pgfkeysearch_exp_arg:NNw \tl_set:Nn #3 {pgfk@##1/#2}
231 \__pgfkeysearch_set_rtn_true:
232 \seq_map_break:
233 }
234 { }
235 }
236 }
237
238
239 \cs_new_protected:Npn \pgfkeysearch_keysearch:nnnTF
240 {
241 \msg_warning:nnnxx {pgfkeysearch} {deprecated} {singlepath}{\cs_to_str:N \pgfkeysearch_keysearch:nnnTF}{\cs_to_str:N \pgfkeysearch_keysearch:nnNTF}
242 \pgfkeysearch_keysearch:nnNTF
243 }
244 \cs_new_protected:Npn \pgfkeysearch_keysearch:nnnT
245 {
246 \msg_warning:nnnxx {pgfkeysearch} {deprecated} {singlepath}{\cs_to_str:N \pgfkeysearch_keysearch:nnnT}{\cs_to_str:N \pgfkeysearch_keysearch:nnNT}
247 \pgfkeysearch_keysearch:nnNT
248 }
249 \cs_new_protected:Npn \pgfkeysearch_keysearch:nnnF
250 {
251 \msg_warning:nnnxx {pgfkeysearch} {deprecated} {singlepath}{\cs_to_str:N \pgfkeysearch_keysearch:nnnF}{\cs_to_str:N \pgfkeysearch_keysearch:nnNF}
252 \pgfkeysearch_keysearch:nnNF
253 }
254
255 %%%%%%%%%%%%%%%
256 %%%
257 %%% #1 shall be a comma separated list of paths (can be a single one)
258 %%% it searchs for a key in every path, stoping at the first hit.
259 %%%
260 %%%%%%%%%%%%%%%
261 \__pgfkeysearch_duo_new:Nn \pgfkeysearch_multipath_keysearch:nnN
262 {
263 \seq_set_from_clist:Nn \l__pgfkeysearch_tmpa_seq {#1}
264
265 \seq_map_inline:Nn \l__pgfkeysearch_tmpa_seq
266 {
267 \pgfkeysearch_keysearch:nnNTF {##1}{#2} #3
268 { \seq_map_break: }
269 {}
270 }
271 }
272
273 \cs_new_protected:Npn \pgfkeysearch_multipath_keysearch:nnnTF
274 {
275 \msg_warning:nnnxx {pgfkeysearch} {deprecated} {multipath}{\cs_to_str:N \pgfkeysearch_multipath_keysearch:nnnTF}{\cs_to_str:N \pgfkeysearch_multipath_keysearch:nnNTF}
276 \pgfkeysearch_multipath_keysearch:nnNTF
277 }
278 \cs_new_protected:Npn \pgfkeysearch_multipath_keysearch:nnnT
279 {
280 \msg_warning:nnnxx {pgfkeysearch} {deprecated} {multipath}{\cs_to_str:N \pgfkeysearch_multipath_keysearch:nnnT}{\cs_to_str:N \pgfkeysearch_multipath_keysearch:nnNT}
281 \pgfkeysearch_multipath_keysearch:nnNT
282 }
283 \cs_new_protected:Npn \pgfkeysearch_multipath_keysearch:nnnF
284 {
285 \msg_warning:nnnxx {pgfkeysearch} {deprecated} {multipath}{\cs_to_str:N \pgfkeysearch_multipath_keysearch:nnnF}{\cs_to_str:N \pgfkeysearch_multipath_keysearch:nnNF}
286 \pgfkeysearch_multipath_keysearch:nnNF
287 }
288
289
290 %%%%%%%%%%%%%%%
291 %%%
292 %%% The caveats: none of this is expandable.
293 %%%
294 %%%%%%%%%%%%%%%
295 %%%
296 %%% This is the more generic one.
297 %%% #1 is the path (or list of paths)
298 %%% #2 is the <key>
299 %%% #3 is the macro that will receive the key value (if any)
300 %%%
301 %%%%%%%%%%%%%%%
302
303 %\NewDocumentCommand{\pgfkeysearchsettings}{m}
304 % { \pgfkeysearch_settings:n {#1} }
305 \cs_new_eq:NN \pgfkeysearchsettings \pgfkeysearch_settings:n
306
307 \cs_new:Npn \__pgfkeysearch_old_valueof:nnN #1#2#3
308 {
309 \pgfkeysearch_multipath_keysearch:nnNTF {#1}{#2}#3
310 { }
311 { \tl_clear:N #3 }
312 }
313 \cs_new:Npn \__pgfkeysearch_old_valueofTF:nnNTF #1#2#3#4#5
314 {
315 \pgfkeysearch_multipath_keysearch:nnNTF {#1}{#2}#3
316 { #4 }
317 {
318 \tl_clear:N #3
319 #5
320 }
321 }
322 \cs_new:Npn \__pgfkeysearch_old_getvalueof:nnN #1#2#3
323 {
324 \pgfkeysearch_keyget:nnNTF {#1}{#2}#3
325 { }
326 { \tl_clear:N #3 }
327 }
328 \cs_new:Npn \__pgfkeysearch_old_getvalueofTF:nnNTF #1#2#3#4#5
329 {
330 \pgfkeysearch_keyget:nnNTF {#1}{#2}#3
331 { #4 }
332 {
333 \tl_clear:N #3
334 #5
335 }
336 }
337
338 \keys_set:nn {pgfkeysearch}{settings=new}
339
340 \ProcessKeyOptions [pgfkeysearch]
341