summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/generic/simplekv/simplekv.tex
blob: bc811b2e8f88592b69dab6f7985443f036e4e720 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
% !TeX encoding = UTF-8
% Ce fichier contient le code commenté de l'extension "simplekv"
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                    %
\def\skvname                  {simplekv}                             %
\def\skvver                     {0.2c}                               %
%                                                                    %
\def\skvdate                 {2023/10/02}                            %
%                                                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% --------------------------------------------------------------------
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% of this license or (at your option) any later version.
% The latest version of this license is in
%
% %     http://www.latex-project.org/lppl.txt
%
% and version 1.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
% --------------------------------------------------------------------
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Christian Tellechea
% email: unbonpetit@netc.fr
%        Commentaires, suggestions et signalement de bugs bienvenus !
%        Comments, bug reports and suggestions are welcome.
% Copyright: Christian Tellechea 2017-2023
% --------------------------------------------------------------------
% L'extension simplekv est composée des 5 fichiers suivants :
%   - code               : simplekv            (.tex et .sty)
%   - manuel en français : simplekv-fr         (.tex et .pdf)
%   - fichier lisezmoi   : README
% --------------------------------------------------------------------

%##########################################
%################ Préalable ###############
%##########################################
\csname skvloadonce\endcsname
\let\skvloadonce\endinput
\ifdefined\skvfromSTY\else
	\immediate\write -1 {%
		Package: \skvname\space\skvdate\space v\skvver\space Simple keyval package (CT)%
	}%
\fi
%##########################################
%############ Gestion catcodes ############
%##########################################
\begingroup
	\def\X#1{\catcode\number`#1=\number\catcode`#1\relax}
	\expandafter\xdef\csname skv_restorecatcode\endcsname{\X\,\X\=\X\_}
\endgroup
\catcode`\_ = 11 \catcode`\, = 12 \catcode`\= = 12
%##########################################
%############ Macros auxilaires ###########
%##########################################
\chardef\skv_stop 0
\long\def\skv_first#1#2{#1}
\long\def\skv_second#1#2{#2}
\long\def\skv_antefi#1\fi{\fi#1}
\long\def\skv_gob#1{}
\long\def\skv_exe#1{#1}
\expandafter\def\expandafter\skv_gobspace\space{}% pour garder la compatibilité
\long\def\skv_eearg#1#2{\expandafter\expandafter\expandafter\skv_earg_i\expandafter\expandafter\expandafter{#2}{#1}}
\long\def\skv_earg_i#1#2{#2{#1}}
\def\skv_ifcsname#1{\ifcsname#1\endcsname\expandafter\skv_first\else\expandafter\skv_second\fi}
\long\def\skv_ifempty#1{\skv_ifempty_i#1\_nil\_nil\skv_second\skv_first\__nil}%
\long\def\skv_ifempty_i#1#2\_nil#3#4#5\__nil{#4}
\def\skv_stripsp#1{%
	\long\def\skv_stripsp##1##2{\expanded{\skv_stripsp_i\_marksp##2\__nil\_marksp#1\_marksp\_nil{##1}}}%
	\long\def\skv_stripsp_i##1\_marksp#1##2\_marksp##3\_nil{\skv_stripsp_ii##3##1##2\__nil#1\__nil\_nil}%
	\long\def\skv_stripsp_ii##1#1\__nil##2\_nil{\skv_stripsp_iii##1##2\_nil}%
	\long\def\skv_stripsp_iii##1##2\__nil##3\_nil##4{\unexpanded{##4{##2}}}%
}
\skv_stripsp{ }
\def\skv_error#1{\errmessage{Package \skvname\space Error: #1.}}
%##########################################
%########## Macros de définition ##########
%##########################################
\def\setKVdefault{\let\skv_find_kv_ii\skv_find_kv_nocode\skv_readKV\skv_exe}
\def\setKV       {\let\skv_find_kv_ii\skv_find_kv_nocode\skv_readKV\skv_gob}
\def\defKV       {\let\skv_find_kv_ii\skv_find_kv_code  \skv_readKV\skv_gob}
\long\def\skv_readKV#1[#2]#3{%
	#1{\expandafter\def\csname skv_[#2]\endcsname{#3}}% exécute (si \setKVdefault) ou pas
	\def\skv__currentname{#2}%
	\skv_readKV_i#3,\skv_end,%
}
\long\def\skv_readKV_i#1,{%
	\skv_stripsp\skv_ifempty{#1}% Bugfix 0.2c : on ignore un truc vide entre 2 virgules
		{%
		\skv_readKV_i
		}
		{%
		\skv_readKV_ii\skv_find_kv#1=true=\_nil\skv_find_kv\skv_end\__nil% si #1=\skv_end ne rien faire sinon \skv_find_kv#1=true=\_nil
		}%
}
\long\def\skv_readKV_ii#1\skv_find_kv\skv_end#2\__nil{#1}
\long\def\skv_find_kv#1={%
	\edef\skv__currentkey{_[\skv__currentname]_\skv_stripsp\detokenize{#1}}%
	\skv_find_kv_i{}%
}
\long\def\skv_find_kv_i#1=#2\_nil{%
	\expandafter\skv_stripsp\expandafter\skv_find_kv_ii\expandafter{\skv_gob#1}%
	\skv_readKV_i
}
\long\def\skv_find_kv_nocode#1{%
	\expandafter\def\csname skv\skv__currentkey\endcsname{#1}% stocker la valeur
	\ifcsname skvcode\skv__currentkey\endcsname
		\skv_antefi\csname skvcode\skv__currentkey\endcsname{#1}%
	\fi
}
\long\def\skv_find_kv_code{%
	\expandafter\def\csname skvcode\skv__currentkey\endcsname##1%
}
\def\restoreKV[#1]{%
	\skv_ifcsname{skv_[#1]}
		{%
		\skv_eearg{\setKV[#1]}{\csname skv_[#1]\endcsname}%
		}
		{%
		\skv_error{Undefined or not saved set of keys "#1"}%
		}%
}
\let\useKVdefault\restoreKV
%##########################################
%############## Macro \useKV ##############
%##########################################
\def\useKV[#1]{\romannumeral\skv_stripsp{\useKV_i[#1]}}
\def\useKV_i[#1]#2{%
	\ifcsname skv_[#1]_\detokenize{#2}\endcsname
		\expandafter\expandafter\expandafter\skv_stop\csname skv_[#1]_\detokenize{#2}\expandafter\endcsname
	\else
		\skv_stop
		\skv_error{Key "\detokenize{#2}" not defined in group of keys "#1"}%
	\fi
}
%##########################################
%############# Macros de test #############
%##########################################
\def\ifboolKV[#1]{\romannumeral\skv_stripsp{\ifboolKV_i[#1]}}
\def\ifboolKV_i[#1]#2{%
	\ifcsname skv_[#1]_\detokenize{#2}\endcsname
		\expandafter\expandafter\expandafter\ifboolKV_ii\csname skv_[#1]_\detokenize{#2}\expandafter\endcsname
		\expandafter\ifboolKV_ii
	\else
		\skv_stop
		\skv_error{Key "\detokenize{#2}" not defined in group of keys "#1"}%
		\expandafter\skv_second
	\fi
}
\def\ifboolKV_ii#1\ifboolKV_ii{%% Cette macro teste si #1, qui est une <valeur>, vaut "true" ou "false"
	\skv_ifargtrue{#1}
		{%
		\expandafter\skv_stop
		\skv_first
		}
		{%
		\skv_ifargfalse{#1}
			{%
			\expandafter\skv_stop
			\skv_second
			}
			{%
			\skv_stop
			\skv_error{Value "\detokenize{#1}" is not a valid boolean}%
			\skv_second
			}%
		}%
}
\def\testboolKV{\romannumeral\skv_stripsp\testboolKV_i}% macro publique qui teste si #1 est <true> ou <false>, erreur sinon
\def\testboolKV_i#1{%
	\skv_ifempty{#1}
		{%
		\skv_stop
		\skv_error{Empty argument is not a valid boolean}
		\skv_second
		}
		{%
		\ifboolKV_ii#1\ifboolKV_ii
		}%
}
\def\skv_ifargtrue#1{\skv_ifargtrue_i#1true\_nil}
\def\skv_ifargtrue_i#1true#2\_nil{%
	\skv_ifempty{#1}
		{%
		\skv_ifargtrue_ii#2\_nil
		}
		{%
		\skv_second
		}%
}
\def\skv_ifargtrue_ii#1true#2\_nil{\skv_ifempty{#1#2}}
\def\skv_ifargfalse#1{\skv_ifargfalse_i#1false\_nil}
\def\skv_ifargfalse_i#1false#2\_nil{%
	\skv_ifempty{#1}
		{%
		\skv_ifargfalse_ii#2\_nil
		}
		{%
		\skv_second
		}%
}
\def\skv_ifargfalse_ii#1false#2\_nil{\skv_ifempty{#1#2}}
%##########################################
%############# Macro \showKV ##############
%##########################################
\def\showKV[#1]{\skv_stripsp{\showKV_i[#1]}}
\def\showKV_i[#1]#2{%
	\edef\skv__currentkey{_[#1]_\detokenize{#2}}%
	\immediate\write-1 {%
		^^J%
		Key\space\space\detokenize{[#1]#2 -> }%
		\skv_ifcsname{skv\skv__currentkey}
			{%
			\showKV_ii{skv\skv__currentkey}%
			^^J%
			Code \detokenize{[#1]#2 -> }%
			\skv_ifcsname{skvcode\skv__currentkey}
				{%
				\showKV_ii{skvcode\skv__currentkey}%
				}
				{%
				not defined%
				}%
			}
			{%
			not defined%
			}%
	^^J%
	\relax}%
}
\def\showKV_ii#1{%
		\expandafter\expandafter\expandafter
	\skv_show\expandafter
	\meaning\csname#1\endcsname
}
\def\skv_show#1->{}
\skv_restorecatcode
\endinput

Versions :
 _____________________________________________________________________________
| Version |    Date    | Changements                                          |
|-----------------------------------------------------------------------------|
|   0.1   | 08/08/2017 | Première version                                     |
|-----------------------------------------------------------------------------|
|   0.2   | 27/04/2020 | - Un <code> peut être assigné à une <clé>            |
|         |            | - Correction de bugs                                 |
|         |            | - Optimisations                                      |
|-----------------------------------------------------------------------------|
|   0.2a  | 01/10/2022 | - vieux bug corrigé : \useKV envoie désormais une    |
|         |            |   erreur si une clé n'est pas définie                |
|         |            | - la valeur n'est dépouillée que d'une accolade (et  |
|         |            |   non pas de 2 comme auparavant)                     |
|         |            | - quelques petits nettoyages, code en UTF8           |
|-----------------------------------------------------------------------------|
|   0.2b  | 30/10/2022 | - messages d'erreur mieux formatés                   |
|         |            | - <clé> détokénisée dans \ifboolKV et \showKV pour   |
|         |            |   être cohérent avec \setKV et \useKV                |
|-----------------------------------------------------------------------------|
|   0.2c  | 02/10/2023 | - bug corrigé : si un item <clé=val> est vide, il est|
|         |            |   ignoré                                             |
|         |            | - quelques remaniements du code                      |
|-----------------------------------------------------------------------------|