Debian Codegraph @

texmf-dist/doc/latex/cweb-latex/src/keyvald.dtx

raw
1 %% $ITI: keyvald.dtx,v 1.2 1995/08/25 19:00:31 schrod Exp $
2 %%------------------------------------------------------------
3 %% (history at end)
4 %
5 % \iffalse
6 %% File: keyvald.dtx; changed version of
7 %% File: keyval.dtx Copyright (C) 1993-1994 David Carlisle
8 %
9 %<*dtx>
10 \ProvidesFile{keyvald.dtx}
11 %</dtx>
12 %<*!plain>
13 %<package&!plain>\NeedsTeXFormat{LaTeX2e}
14 %<package&!plain>\ProvidesPackage{keyvald}
15 %<driver> \ProvidesFile{keyvald.drv}
16 % \fi
17 % \ProvidesFile{keyvald.dtx}
18 [1994/08/23 v1.08.1 key=value parser (DPC) w/ defaults (js)]
19 %
20 % \iffalse
21 %</!plain>
22 %<*driver>
23 \documentclass{ltxdoc}
24 \usepackage{keyvald}
25 \begin{document}
26 \DocInput{keyvald.dtx}
27 \end{document}
28 %</driver>
29 % \fi
30 %
31 % \GetFileInfo{keyvald.dtx}
32 % \title{The \textsf{keyvald} package\thanks{This file
33 % has version number \fileversion, last
34 % revised \filedate.}}
35 % \author{David Carlisle\\[email protected]
36 % \and
37 % changed by\\Joachim Schrod\\
38 % \texttt{<[email protected]>}
39 % }
40 % \date{\filedate}
41 % \maketitle
42 %
43 % \begin{abstract}
44 % A \LaTeX\ package implementing a system allowing the setting
45 % of parameters (or `named arguments' with a
46 % \meta{key}${}={}$\meta{value} syntax.
47 %
48 % Eg: |\foo[height=3in, shadow = true ]{bar}|
49 % \end{abstract}
50 %
51 % \CheckSum{110}
52 %
53 % \changes{v1.03}
54 % {1993/10/13}{Initial version}
55 % \changes{v1.04}{1993/11/15}
56 % {Upgrade after comments from Timothy van Zandt}
57 % \changes{v1.05}{1993/11/17}
58 % {Further small improvements}
59 % \changes{v1.06}{1994/02/01}
60 % {Update to LaTeX2e}
61 % \changes{v1.07}{1994/03/15}
62 % {New style ltxdoc}
63 % \changes{v1.08}{1994/09/12}
64 % {Improve docstrip handling}
65 % \changes{v1.08.1}{1995/08/23}
66 % {Default keys}
67 %
68 %
69 % This package implements a system of defining and using sets of
70 % parameters, which are set using the syntax \meta{key}=\meta{value}.
71 %
72 % For each keyword in such a set, there exists a function which is
73 % called whenever the parameter appears in a parameter list. For
74 % instance if the set |dpc| is to have the keyword |scale| then I
75 % would define.\\
76 % | \define@key{dpc}{scale}{scale ({\tt\string#1})\\}|\\
77 % The first argument of |\define@key| is the set of keywords being
78 % used, the second is the keyword, and the third is the function to
79 % call. This function will be given as |#1| the \meta{value} specified
80 % by the user.
81 %
82 % Normally it is an error to omit the `=\meta{value}' however if an
83 % optional \meta{value} is supplied when the keyword is defined, then
84 % just the keyword need be supplied.\\
85 % |\define@key{dpc}{clip}[true]{...}|\\
86 % For `|clip|' you can go `|clip = true|' or `|clip = false|' or
87 % just `|clip|', which is the same as `|clip = true|'
88 %
89 % To use these keywords, just call `|\setkeys|' with a comma
90 % separated list of settings, each of the form
91 % \meta{key}=\meta{value}, or just \meta{key}. Any white space around
92 % the `|=|' and `|,|' is ignored.
93 %
94 % As the \meta{key} is passed as a macro argument, if it consists
95 % entirely of a |{ }| group, the outer braces are stripped off. Thus
96 % |,key=foo,| and |key=,{foo},| are equivalent. This fact enables one to
97 % `hide' any commas or equals signs that must appear in the value. i.e.\
98 % in |foo={1,2,3},bar=4|, |foo| gets the value |1,2,3|, the comma after
99 % |1| does not terminate the keyval pair, as it is `hidden' by the
100 % braces.
101 %
102 % Empty entries, with nothing between the commas, are silently ignored.
103 % This means that it is not an error to have a comma after the last
104 % term, or before the first.
105 %
106 % \section{Example}
107 %
108 % We may extend the examples above to give a `fake' graphics
109 % inclusion macro, with a syntax similar to that used in the psfig
110 % macros.
111 %
112 % \makeatletter
113 % \def\dpcgraphics{\@ifnextchar[\@dpcgraphics{\@dpcgraphics[]}}
114 %
115 % |\dpcgraphics| has one optional argument which is passed through
116 % |\setkeys|, and one mandatory argument, the filename. It actually
117 % just typesets its arguments, for demonstration.
118 %
119 % \def\@dpcgraphics[#1]#2{{\setkeys{dpc}{#1}INPUT: #2}}%
120 %
121 % \define@key{dpc}{scale}{scale ({\tt\string#1\relax})\\}
122 % \define@key{dpc}{height}{height ({\tt#1})\\}
123 % \define@key{dpc}{width}{width ({\tt#1})\\}
124 % \define@key{dpc}{bb}{bounding box ({\tt#1})\\}
125 % \define@key{dpc}{clip}[true]{clip ({\tt\string#1\relax})\\}
126 % \makeatother
127 %
128 % The declared keys are: |scale|, |height|, |width|, |bb|,
129 % and |clip|. Except for the last, they must all be given a value if
130 % used.
131 %
132 % Note how in the following, any white space arround |=| or |,| is
133 % ignored, as are the `empty' arguments caused by extra commas. Note
134 % also that each macro receives {\em exactly\/} the tokens that you
135 % specify as arguments, no premature expansion is done.
136 %
137 % \begin{verbatim}
138 % \def\dpcgraphics{\@ifnextchar[\@dpcgraphics{\@dpcgraphics[]}}
139 % \def\@dpcgraphics[#1]#2{{\setkeys{dpc}{#1}INPUT: #2}}
140 %
141 % \define@key{dpc}{scale}{scale ({\tt\string#1\relax})\\}
142 % \define@key{dpc}{clip}[true]{clip ({\tt\string#1\relax})\\}
143 % \end{verbatim}
144 %
145 % \begin{minipage}{.4\textwidth}
146 % \begin{verbatim}
147 % \def\scalemacro{9}
148 % \dpcgraphics
149 % [ height =4in, ,
150 % width = 3in,
151 % scale = \scalemacro,
152 % bb = 20 20 300 400 ,
153 % clip,
154 % ]{aaa}
155 % \end{verbatim}
156 % \end{minipage}
157 % \hfill
158 % \begin{minipage}{.4\textwidth}
159 % \def\scalemacro{9}
160 %\dpcgraphics
161 % [ height =4in, ,
162 % width = 3in,
163 % scale = \scalemacro ,
164 % bb = 20 20 300 400 ,
165 % clip ,
166 % ]{aaa}
167 % \end{minipage}
168 %
169 %
170 % \section{The Internal Interface}
171 % A declaration of the form:\\
172 % |\define@key{family}{key}{...}|\\
173 % Defines a macro |\KV@prefix@key| with one argument. When used in a
174 % keyval list, the macro receives the value as its argument.
175 %
176 % A declaration of the form:\\
177 % |\define@key{family}{key}[default]{...}|\\
178 % Defines a macro |\KV@family@key| as above, however it also defines the
179 % macro |\KV@family@key@default| as a macro with no arguments, and
180 % definition\\
181 % |\KV@family@key{default}|.
182 %
183 % Thus if macros are defined using |\define@key|, the use of a key with
184 % no value \ldots|,foo,|\ldots\ is always equivalent to the use of the
185 % key with some value, \ldots|,foo=default,|\ldots. However a package
186 % writer may wish that the `default' behaviour for some key is not
187 % directly equivalent to using that key with a value. (In particular, as
188 % pointed out to me by Timothy Van Zandt, you may wish to omit error
189 % checking on the default value as you know it is correct.) In these
190 % cases one simply needs to define the two macros
191 % |\KV@|\meta{family}|@key| and |\KV@|\meta{family}|@key@default|
192 % directly using |\def| (or |\newcommand|). I do not supply a user
193 % interface for this type of definition, but it is supported in the
194 % sense that I will try to ensure that any future upgrades of this
195 % package do not break styles making use of these `low level'
196 % definitions.
197 %
198 % \StopEventually{}
199 %
200 % \section{The Macros}
201 %
202 % From version~1.05, all `internal' macros associated to keys have names
203 % of the form:\\
204 % |\KV@|\meta{family}|@|\meta{key} or
205 % |\KV@|\meta{family}|@|\meta{key}|@|\meta{default}
206 %
207 % \begin{macrocode}
208 %<*package>
209 % \end{macrocode}
210 %
211 % \begin{macro}{\setkeys}
212 % The top level macro. |#2| should be a comma separated values of the
213 % form \meta{key} |=| \meta{value} or just simply \meta{key}.
214 % The macro associated with this key in the `family' |#1| is called with
215 % argument \meta{value}. The second form is only allowed if the key was
216 % declared with a default value.
217 % \begin{macrocode}
218 \def\setkeys#1#2{%
219 % \end{macrocode}
220 % Save the `family' for later. Then begin acting on the comma
221 % separated list.
222 % \begin{macrocode}
223 \def\KV@prefix{KV@#1@}%
224 \KV@do#2,\relax,}
225 % \end{macrocode}
226 % \end{macro}
227 %
228 % \begin{macro}{\KV@do}
229 % Iterate down the list of comma separated argument pairs.
230 % \begin{macrocode}
231 \def\KV@do#1,{%
232 \ifx\relax#1\empty\else
233 \KV@split#1==\relax
234 \expandafter\KV@do\fi}
235 % \end{macrocode}
236 % \end{macro}
237 %
238 % \begin{macro}{\KV@split}
239 % Split up the keyword and value, and call the appropriate command.
240 % This macro was slightly reorganised for version 1.04, after some
241 % suggestions from Timothy Van Zandt.
242 % \begin{macrocode}
243 \def\KV@split#1=#2=#3\relax{%
244 \KV@@sp@def\KV@key{#1}%
245 \ifx\KV@key\@empty\else
246 \expandafter\let\expandafter\@tempc
247 \csname\KV@prefix\KV@key\endcsname
248 \ifx \@tempc\relax
249 \expandafter\let \expandafter\@tempc
250 \csname\KV@prefix*\endcsname
251 \def\@tempa{*}%
252 \else
253 \let\@tempa\KV@key
254 \fi
255 \ifx\@tempc\relax
256 \KV@err{\KV@key\space undefined}%
257 \else
258 \ifx\@empty#3\@empty
259 \KV@default
260 \else
261 \KV@@sp@def\@tempb{#2}%
262 \expandafter\@tempc\expandafter{\@tempb}\relax
263 \fi
264 \fi
265 \fi}
266 % \end{macrocode}
267 % \end{macro}
268 %
269 % \begin{macro}{\KV@default}
270 % Run the default code, or raise an error.
271 % \begin{macrocode}
272 \def\KV@default{%
273 \expandafter\let\expandafter\@tempb
274 \csname\KV@prefix\@tempa @default\endcsname
275 \ifx\@tempb\relax
276 \KV@err{No value specified for \KV@key}%
277 \else
278 \@tempb\relax
279 \fi}
280 % \end{macrocode}
281 % \end{macro}
282 %
283 % \begin{macro}{\KV@err}
284 % Error messages.
285 % \begin{macrocode}
286 \def\KV@err#1{\PackageError{keyvald}{#1}{}}
287 % \end{macrocode}
288 % \end{macro}
289 %
290 % \begin{macro}{\KV@@sp@def}
291 % \begin{macro}{\KV@@sp@b}
292 % \begin{macro}{\KV@@sp@c}
293 % |\KV@@sp@def|\meta{cmd}\meta{token list} is like |\def|, except that
294 % a space token at the beginning or end of \meta{token list} is
295 % removed before making the assignment. \meta{token list} may not
296 % contain the token |\@nil|, unless it is within a brace group.
297 % The names of these commands were changed at version~1.05 to ensure
298 % that they do not clash with `internal' macros in a key family `sp'.
299 %
300 % The following three lines are equivalent to\\
301 % |\def\KV@@sp@def#1#2{\KV@@sp@b#2\@nil\@nil~\@nil\relax#1}|\\
302 % Where |~| is an explicit space token.
303 % \begin{macrocode}
304 \def\@tempa#1{%
305 \def\KV@@sp@def##1##2{\KV@@sp@b##2\@nil\@nil#1\@nil\relax##1}}
306 \@tempa{ }
307 \def\KV@@sp@b#1#2 \@nil{\KV@@sp@c#1#2}
308 \def\KV@@sp@c#1\@nil#2\relax#3{\def#3{#1}}
309 % \end{macrocode}
310 % \end{macro}
311 % \end{macro}
312 % \end{macro}
313 %
314 % \begin{macro}{\define@key}
315 % Define the command associated to the key |#2| in the family |#1|.
316 % First looks for a default argument (the default value for the
317 % key)
318 % \begin{macrocode}
319 \def\define@key#1#2{%
320 \@ifnextchar[{\KV@def{#1}{#2}}{\@namedef{KV@#1@#2}####1}}
321 % \end{macrocode}
322 % \end{macro}
323 %
324 % \begin{macro}{\KV@def}
325 % Make the definitions of the command, and the default value.
326 % \begin{macrocode}
327 \def\KV@def#1#2[#3]{%
328 \@namedef{KV@#1@#2@default\expandafter}\expandafter
329 {\csname KV@#1@#2\endcsname{#3}}%
330 \@namedef{KV@#1@#2}##1}
331 % \end{macrocode}
332 % \end{macro}
333 %
334 % \begin{macrocode}
335 %</package>
336 % \end{macrocode}
337 % \Finale
338 %
339 \endinput
340
341 %------------------------------------------------------------
342 %
343 % $ITIlog: keyvald.dtx,v $
344 % Revision 1.2 1995/08/25 19:00:31 schrod
345 % Add enhanced version of keyval package, with ability to specify
346 % default action for unknown keyword options.
347 %