Debian Codegraph @

texmf-dist/source/latex/locality/locality.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2010 by Jason Gross ([email protected])
4 %
5 % This file may be distributed and/or modified under the
6 % conditions of the LaTeX Project Public License, either
7 % version 1.3c of this license or (at your option) any later
8 % version. The latest version of this license is in:
9 %
10 % http://www.latex-project.org/lppl.txt
11 %
12 % and version 1.3c or later is part of all distributions of
13 % LaTeX version 2005/12/01 or later.
14 %
15 % \fi
16 %
17 %
18 % \iffalse
19 %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
20 %<package>\ProvidesPackage{locality}
21 %<package> [2010/11/11 v0.2 Various macros for keeping things local]
22 %<package>\RequirePackage{etoolbox}[2009/08/06]
23 %
24 %<*driver>
25 \documentclass{ltxdoc}
26 \usepackage{locality}
27 \EnableCrossrefs
28 \CodelineIndex
29 \RecordChanges
30 \begin{document}
31 \DocInput{locality.dtx}
32 \end{document}
33 %</driver>
34 % \fi
35 %
36 % \CheckSum{573}
37 %
38 % \CharacterTable
39 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
40 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
41 % Digits \0\1\2\3\4\5\6\7\8\9
42 % Exclamation \! Double quote \" Hash (number) \#
43 % Dollar \$ Percent \% Ampersand \&
44 % Acute accent \' Left paren \( Right paren \)
45 % Asterisk \* Plus \+ Comma \,
46 % Minus \- Point \. Solidus \/
47 % Colon \: Semicolon \; Less than \<
48 % Equals \= Greater than \> Question mark \?
49 % Commercial at \@ Left bracket \[ Backslash \\
50 % Right bracket \] Circumflex \^ Underscore \_
51 % Grave accent \` Left brace \{ Vertical bar \|
52 % Right brace \} Tilde \~}
53 %
54 % \changes{v0.1}{2010/06/15}{Initial version. Please note that some macros
55 % have not yet been tested.}
56 % \changes{v0.2}{2010/11/11}{Switched the order of tests in \string
57 % \manyaftergroup, eliminating an infinite loop. Added \string
58 % \DeclareNonlocal*s\space, \string\pushvalues, \string\popvalues,
59 % \string\pushvalue, \string\popvalue, and a few others. Fixed
60 % \string\DeclareNonlocal*\space to work with \string\aftergroup.
61 % Added requirement for etoolbox, so that patching takes place at
62 % the end of the preamble.}
63 %
64 % \GetFileInfo{locality.sty}
65 %
66 % \DoNotIndex{\#,\$,\%,\&,\@,\\,\{,\},\^,\_,\~,\ }
67 % \DoNotIndex{}
68 %
69 % \title{The \textsf{locality} package\thanks{This document
70 % corresponds to \textsf{locality}~\fileversion,
71 % dated~\filedate.}}
72 % \author{Jason Gross \\ \texttt{[email protected]}}
73 %
74 % \maketitle
75 %
76 % \section{Introduction}
77 %
78 % The \textsf{locality} package provides various macros to keep changes local
79 % to the current group. This allows one to (re)define helper macros
80 % without worrying about accidentally changing the functionality of another
81 % package's or the user's definitions. Additionally, it allows recursive
82 % macros to have some definitions persist between calls, and others be local.
83 %
84 % \section{Usage}
85 % I give the usage and specification of every macro defined. I give bugs when
86 % I know them (please email me if you find other bugs, or have fixes for the
87 % bugs I list). I sometimes give extra description or justification.
88 %
89 % \DescribeMacro{\manyaftergroup}
90 % \noindent Usage: |\manyaftergroup|\marg{tokens} \par\noindent
91 % Specification: The \meta{tokens} get placed after the current group \par\noindent
92 % Bugs: No braces are permitted, spaces are stripped
93 %
94 % I've often wanted to use \TeX's |\aftergroup| with a variable-length
95 % argument. This macro allows this. It is expandable (may be used in
96 % |\edef|), but it gobbles spaces.
97 %
98 % ToDo: Write a version of this macro that preserves spaces, allows braces.
99 %
100 % \DescribeMacro{\locallydefine}
101 % \noindent Usage: |\locallydefine|\marg{macro}\marg{processing} \par\noindent
102 % Specification: Execute \meta{processing} inside of a group, and make
103 % the definition of |\macro| persist after the group ends. \par\noindent
104 % Bugs: Changes via |\let| to an unexpandable macro yield an infinite recursive
105 % loop.
106 %
107 % Normally, when you define a macro in a group, it's definition reverts after
108 % the group ends, unless you use |\global|. If you use |\global|, then the
109 % new definition replaces the current definition on all levels. This macro
110 % provides something in between.
111 %
112 % \DescribeMacro{\DeclareNonlocalMacro}
113 % \noindent Usage: |\DeclareNonlocalMacro|\marg{macro} \par\noindent
114 % Specification: Any changes to the definition of |\macro| persist
115 % after the end of the current group. \par\noindent
116 % Bugs: Changes via |\let| to an unexpandable macro yield an infinite recursive
117 % loop. Only works with |\begingroup| |\endgroup| (not with braces).
118 %
119 % This macro generalized |\locallydefine|.
120 %
121 %
122 % \DescribeMacro{\DeclareNonlocalMacros}
123 % \noindent Usage: |\DeclareNonlocalMacros|\marg{macro list} \par\noindent
124 % \noindent Example: |\DeclareNonlocalMacros{\macroi,\macroii}| \par\noindent
125 % Specification: \marg{macro list} Should be a comma-separated list of macros.
126 % This command will run |\DeclareNonlocalMacro| on each argument.
127 %
128 %
129 % \DescribeMacro{\DeclareNonlocalTheRegister}\DescribeMacro{\DeclareNonlocalCount}
130 % \DescribeMacro{\DeclareNonlocalDimen}\DescribeMacro{\DeclareNonlocalSkip}
131 % \DescribeMacro{\DeclareNonlocalMuskip}\DescribeMacro{\DeclareNonlocalToks}
132 % \noindent Usage: |\DeclareNonlocalCount|\marg{count} \par\noindent
133 % \phantom{Usage:} |\DeclareNonlocalDimen|\marg{dimen} \par\noindent
134 % \phantom{Usage:} |\DeclareNonlocalSkip|\marg{skip} \par\noindent
135 % \phantom{Usage:} |\DeclareNonlocalMuskip|\marg{muskip} \par\noindent
136 % \phantom{Usage:} |\DeclareNonlocalToks|\marg{toks} \par\noindent
137 % Specification: Any changes to the value in \meta{register} persist
138 % after the end of the current group. \par\noindent
139 %
140 % Registers for \meta{count}s, \meta{dimen}s, \meta{skip}s, and \meta{muskips}s
141 % can all be made nonlocal with |\DeclareNonlocalTheRegister|.
142 %
143 % Bugs: Only works with |\begingroup| |\endgroup| (not with braces). Only
144 % works if the argument is a single token.
145 %
146 % These do the same thing to registers (\meta{count}s, \meta{length}s, and
147 % \meta{tok}s) that |\DeclareNonlocalMacro| does to macros.
148 %
149 %
150 % \DescribeMacro{\DeclareNonlocalTheRegister}\DescribeMacro{\DeclareNonlocalCount}
151 % \DescribeMacro{\DeclareNonlocalDimen}\DescribeMacro{\DeclareNonlocalSkip}
152 % \DescribeMacro{\DeclareNonlocalMuskip}\DescribeMacro{\DeclareNonlocalToks}
153 % \noindent Usage: |\DeclareNonlocalCounts|\marg{list of counts (comma separated)} \par\noindent
154 % \phantom{Usage:} |\DeclareNonlocalDimens|\marg{list of dimens (comma separated)} \par\noindent
155 % \phantom{Usage:} |\DeclareNonlocalSkips|\marg{list of skips (comma separated)} \par\noindent
156 % \phantom{Usage:} |\DeclareNonlocalMuskips|\marg{list of muskips} \par\noindent
157 % \phantom{Usage:} |\DeclareNonlocalTokses|\marg{list of toks (comma separated)} \par\noindent
158 % Specification: Any changes to the value in \meta{register}s persist
159 % after the end of the current group. \par\noindent
160 %
161 % \DescribeMacro{\pushvalue}\DescribeMacro{\popvalue}\noindent
162 % Usage: |\pushvalue|\marg{macro} \par\noindent
163 % \phantom{Usage:} |\popvalue|\marg{macro} \par
164 % \noindent Specification: The argument \marg{macro} is backed up by
165 % |\pushvalue|, and the most recently backed up value is restored by
166 % |\popvalue|. The macro definitions are saved using a stack.
167 %
168 %
169 % \DescribeMacro{\savevalues}\DescribeMacro{\restorevalues}
170 % \noindent Usage: |\savevalues|\marg{list of macros (no separator)} \par\noindent
171 % \phantom{Usage:} |\restorevalues|\marg{list of macros (no separator)} \par\noindent
172 % Specification: Every token in the passed argument is backed up by
173 % |\savevalues|, and the most recently backed up values are restored by
174 % |\restorevalues|.
175 %
176 %
177 % \DescribeMacro{\pushvalues}\DescribeMacro{\popvalues}\noindent
178 % Usage: |\pushvalues|\marg{list of macros (comma separated)} \par\noindent
179 % \phantom{Usage:} |\popvalues|\marg{list of macros (comma separated)} \par
180 % \noindent Specification: Every macro in the passed argument is backed up by
181 % |\pushvalues|, and the most recently backed up values are restored by
182 % |\popvalues|. The macro definitions are saved using a stack.
183 %
184 %
185 % \DescribeMacro{\makecommandslocal}
186 % \noindent Usage: |\makecommandslocal|\marg{list of macros (no separator)}\marg{code} \par\noindent
187 % Specification: Every token in the first argument is made local to \meta{code};
188 % changes made to their definitions do not perist outside of \meta{code}.
189 %
190 % This macro is the natural opposite of |\DeclareNonlocalMacro|; it allows
191 % some macros to behave as if \meta{code} was inside a group, while the rest
192 % of the macros behave as if they were not.
193 %
194 %
195 % \DescribeMacro{\ignoreglobal}\DescribeMacro{\obeyglobal}\DescribeMacro{\unignoreglobal}
196 % \noindent Usage: |\ignoreglobal| \par\noindent
197 % \noindent Usage: |\unignoreglobal| \par\noindent
198 % \noindent Usage: |\obeyglobal| \par\noindent
199 % Specification: The macro |\ignoreglobal| causes global changes, such as
200 % |\edef|, |\xdef|, and those prefaced by |\global|, to be local. The macro
201 % |\obeyglobal| causes these to be treated as global. The macro
202 % |\unignoreglobal| undoes the changes made by the last |\ignoreglobal|. If
203 % you call |\ignoreglobal| twice, then you must call |\unignoreglobal| twice to
204 % allow global changes.
205 %
206 % \DescribeMacro{\makecounterslocal}
207 % \noindent Usage: |\makecounterslocal| \par\noindent
208 % Specification: The macro |\makecounterslocal| redefines the \LaTeX{}
209 % \meta{counter} macros so that their changes are local, instead of global.
210 % At the end of the group in which |\makecounterslocal| is called,
211 % \meta{counter} macros revert to being global.
212 %
213 % \StopEventually{\PrintChanges\PrintIndex}
214 %
215 % \section{Implementation}
216 % \subsection{Helper functions}
217 % The following definitions are preliminary, to allow various tricks with |\def|.
218 % \begin{macrocode}
219 \def\@nil{\@nil\relax} % this way, I'll know if I've messed up; I'll get
220 % a stack overflow error.
221 \def\if@nil#1{\@if@nil#1\@@nil}
222 \def\@if@nil#1#2\@@nil{\ifx\@nil#1}
223 % We'll be messing with |\global|, so we better have a backup.
224 \let\locality@tex@global=\global
225 % \end{macrocode}
226 %
227 % \begin{macro}{\locality@global@non@collision@unique@count}
228 % At various places, I want to have a macro associated with a certain name
229 % which hasn't been used before. I use this count to number them. I use
230 % a count, instead of a counter, so that I can control whether or not it's
231 % global. The long name, with lots of |@|s, is to (hopefully) avoid
232 % collisions.
233 % \begin{macrocode}
234 \newcount\locality@global@non@collision@unique@count
235 \locality@global@non@collision@unique@count=0
236 % \end{macrocode}
237 % \end{macro}
238 %
239 % \begin{macro}{\manyaftergroup}
240 %
241 % The macro |\manyaftergroup| works by parsing it's argument one token at a
242 % time, and applying |\aftergroup| to each argument. It checks for the end
243 % with |\@nil|.
244 % \begin{macrocode}
245 \long\def\@manyaftergroup#1{\if@nil#1 \else \aftergroup#1
246 \expandafter\@manyaftergroup\fi}
247 \newcommand{\manyaftergroup}[1]{\@manyaftergroup#1\@nil}
248 % \end{macrocode}
249 % \end{macro}
250 %
251 %
252 % \begin{macro}{\locallydefine}
253 % Execute the second argument passed locally, and then preserve the definition
254 % of the first argument passed.
255 % \begin{macrocode}
256 \newcommand{\locallydefine}[2]{{#2\expandafter}%
257 \expandafter\def\expandafter#1\expandafter{#1}}
258 % \end{macrocode}
259 %
260 % \end{macro}
261 %
262 % The |\DeclareNonlocal| macros do some fancy stuff with |\begingroup| and
263 % |\endgroup|, so the old definitions must be saved.
264 % \begin{macrocode}
265 \let\locality@tex@begingroup=\begingroup
266 \let\locality@tex@endgroup=\endgroup
267 % \end{macrocode}
268 % These macros are extended versions of the |\locallydefine| macro; they
269 % redefine |\endgroup| to preserve definitions after the current group ends.
270 %
271 % Because |\aftergroup| would occur before definition restoration, we patch
272 % |\aftergroup| so that it instead appends tokens to the end of |\endgroup|.
273 % This doesn't fix all use cases, but it should fix a problem with the calc
274 % package.
275 % \begin{macrocode}
276 \newcount\locality@global@aftergroup@count
277 \locality@global@aftergroup@count=-1
278 \newcommand\locality@patch@aftergroup{%
279 \def\aftergroup{%
280 \locality@tex@global\advance\locality@global@aftergroup@count by 1
281 \afterassignment\locality@aftergroup\locality@tex@global
282 \expandafter\let\csname locality@nextchar\space\the
283 \locality@global@aftergroup@count\endcsname=%
284 }%
285 }
286 \newcommand\locality@aftergroup{%
287 \expandafter\expandafter\expandafter\def
288 \expandafter\expandafter\expandafter\endgroup
289 \expandafter\expandafter\expandafter{\expandafter\endgroup
290 \csname locality@nextchar\space\the\locality@global@aftergroup@count
291 \endcsname}%
292 }
293 % \end{macrocode}
294 %
295 %
296 % \begin{macro}{\DeclareNonlocalMacro}
297 % This macro redefines |\endgroup| to do this for macro passed to it.
298 %
299 % First, back up |\endgroup| to a new macro.
300 % \begin{macrocode}
301 \newcommand{\DeclareNonlocalMacro}[1]{%
302 \locality@patch@aftergroup % first, patch |\aftergroup|
303 \expandafter\let
304 \csname endgroup \the\locality@local@group@non@local@macro@count
305 \endcsname=\endgroup
306 % \end{macrocode}
307 % Redefine |\endgroup| to, in order: revert it's definition, insert code to
308 % update the definition of the passed macro outside of the group, and call
309 % the (reverted) version of |\endgroup|.
310 % \begin{macrocode}
311 \expandafter\def\expandafter\endgroup\expandafter{%
312 \expandafter\expandafter\expandafter\let\expandafter
313 \expandafter\expandafter\endgroup\expandafter\expandafter
314 \csname endgroup \the\locality@local@group@non@local@macro@count
315 \endcsname\expandafter\endgroup
316 \expandafter\def\expandafter#1\expandafter{#1}}%
317 \advance\locality@local@group@non@local@macro@count by 1
318 }%
319 % \end{macrocode}
320 % \end{macro}
321 %
322 % \begin{macrocode}
323 \newcommand\locality@declarenonlocals[2]{%
324 \@for\locality@declarenonlocals@name:=#2\do{%
325 \expandafter #1\expandafter{\locality@declarenonlocals@name}%
326 }%
327 }
328 % \end{macrocode}
329 %
330 % \begin{macro}{\DeclareNonlocalMacros}
331 % \begin{macrocode}
332 \newcommand{\DeclareNonlocalMacros}[1]{\locality@declarenonlocals{\DeclareNonlocalMacro}{#1}}
333 % \end{macrocode}
334 % \end{macro}
335 %
336 %
337 % \begin{macro}{\DeclareNonlocalTheRegister}
338 % \begin{macro}{\DeclareNonlocalCount}
339 % \begin{macro}{\DeclareNonlocalDimen}
340 % \begin{macro}{\DeclareNonlocalSkip}
341 % \begin{macro}{\DeclareNonlocalMuskip}
342 % This works the same way as as |\DeclareNonlocalMacro|, but uses |\the| instead of
343 % |\def|.
344 % \begin{macrocode}
345 \newcommand{\DeclareNonlocalTheRegister}[1]{%
346 \locality@patch@aftergroup % first, patch |\aftergroup|
347 \expandafter\let
348 \csname endgroup \the\locality@local@group@non@local@macro@count
349 \endcsname=\endgroup
350 \expandafter\def\expandafter\endgroup\expandafter{%
351 \expandafter\expandafter\expandafter\let
352 \expandafter\expandafter\expandafter\endgroup
353 \expandafter\expandafter
354 \csname endgroup \the\locality@local@group@non@local@macro@count
355 \endcsname\expandafter\endgroup
356 \expandafter#1\expandafter=\the#1 }% Note the space. This is to
357 % prevent something like
358 % |\newcount\tempc\begingroup \DeclareNonlocalCount\tempc \tempc=1\endgroup1|
359 % from setting |\tempc| to 11.
360 \advance\locality@local@group@non@local@macro@count by 1
361 }%
362 \let\DeclareNonlocalCount=\DeclareNonlocalTheRegister
363 \let\DeclareNonlocalDimen=\DeclareNonlocalTheRegister
364 \let\DeclareNonlocalSkip=\DeclareNonlocalTheRegister
365 \let\DeclareNonlocalMuskip=\DeclareNonlocalTheRegister
366 % \end{macrocode}
367 % \end{macro}
368 % \end{macro}
369 % \end{macro}
370 % \end{macro}
371 % \end{macro}
372 %
373 % \begin{macro}{\DeclareNonlocalTheRegisters}
374 % \begin{macro}{\DeclareNonlocalCounts}
375 % \begin{macro}{\DeclareNonlocalDimens}
376 % \begin{macro}{\DeclareNonlocalSkips}
377 % \begin{macro}{\DeclareNonlocalMuskips}
378 % \begin{macrocode}
379 \newcommand{\DeclareNonlocalTheRegisters}[1]{\locality@declarenonlocals{\DeclareNonlocalTheRegister}{#1}}
380 \let\DeclareNonlocalCounts=\DeclareNonlocalTheRegisters
381 \let\DeclareNonlocalDimens=\DeclareNonlocalTheRegisters
382 \let\DeclareNonlocalSkips=\DeclareNonlocalTheRegisters
383 \let\DeclareNonlocalMuskips=\DeclareNonlocalTheRegisters
384 % \end{macrocode}
385 % \end{macro}
386 % \end{macro}
387 % \end{macro}
388 % \end{macro}
389 % \end{macro}
390 %
391 % \begin{macro}{\DeclareNonlocalToks}
392 % This works the same way as as |\DeclareNonlocalCount|, but puts braces around
393 % the assigned value; |\toks0=1| fails, and should be |\toks0={1}|.
394 % \begin{macrocode}
395 \newcommand{\DeclareNonlocalToks}[1]{%
396 \locality@patch@aftergroup % first, patch |\aftergroup|
397 \expandafter\let
398 \csname endgroup \the\locality@local@group@non@local@macro@count
399 \endcsname=\endgroup
400 \expandafter\def\expandafter\endgroup\expandafter{%
401 \expandafter\expandafter\expandafter\let\expandafter\expandafter
402 \expandafter\endgroup\expandafter\expandafter
403 \csname endgroup \the\locality@local@group@non@local@macro@count'
404 \endcsname\expandafter\endgroup
405 \expandafter#1\expandafter=\expandafter{\the#1}}%
406 \advance\locality@local@group@non@local@macro@count by 1
407 }%
408 % \end{macrocode}
409 % \end{macro}
410 %
411 % \begin{macro}{\DeclareNonlocalTokses}
412 % \begin{macrocode}
413 \newcommand{\DeclareNonlocalTokses}[1]{\locality@declarenonlocals{\DeclareNonlocalToks}{#1}}
414 % \end{macrocode}
415 % \end{macro}
416 %
417 % I redefine |\begingroup| to reset the locality macros, so nesting works.
418 % \begin{macrocode}
419 \newcount\locality@local@group@non@local@macro@count % Hopefully, this
420 % won't collide with anything. I hope putting this out here allows
421 % proper nesting of groups
422 \def\begingroup{\locality@tex@begingroup
423 \def\endgroup{\locality@tex@endgroup}% not \let, because
424 % that would break my |\aftergroup| patch
425 \locality@local@group@non@local@macro@count=0
426 }
427 % \end{macrocode}
428 %
429 %
430 %
431 % \begin{macro}{\savevalues}
432 % \begin{macro}{\restorevalues}
433 % These macros parse their arguments token by token, renaming each |macro| to
434 % |@\macro backup|, or vice versa.
435 % \begin{macrocode}
436 \def\@savevalues#1{\ifx#1\@nil \else \expandafter\let
437 \csname @\string#1\space backup\endcsname=#1
438 \expandafter\@savevalues\fi}
439 \newcommand{\savevalues}[1]{\@savevalues#1\@nil}
440
441 \def\@restorevalues#1{%
442 \ifx
443 #1\@nil
444 \else
445 \expandafter\let\expandafter#1\expandafter
446 =\csname @\string#1\space backup\endcsname
447 \expandafter
448 \let\csname @\string#1\space backup\endcsname
449 =\relax
450 \expandafter\@restorevalues
451 \fi
452 }
453 \newcommand{\restorevalues}[1]{\@restorevalues#1\@nil}
454 % \end{macrocode}
455 % \end{macro}
456 % \end{macro}
457 %
458 %
459 % \begin{macro}{\pushvalues}
460 % \begin{macro}{\popvalues}
461 % \begin{macro}{\pushvalue}
462 % \begin{macro}{\popvalue}
463 % \begin{macrocode}
464 \newcommand{\pushvalue}[1]{%
465 \@ifundefined{locality\space backup\space \string#1}{}{%
466 \expandafter\pushvalue\csname locality\space backup\space \string#1\endcsname%
467 }%
468 \expandafter\let\csname locality\space backup\space \string#1\endcsname=#1%
469 }
470 \newcommand{\pushvalues}[1]{%
471 \@for\locality@pushvalues@macroname:=#1\do{%
472 \expandafter\pushvalue\locality@pushvalues@macroname
473 }%
474 }
475 \newcommand{\popvalue}[1]{%
476 \@ifundefined{locality\space backup\space \string#1}{%
477 \let#1=\relax
478 }{%
479 \expandafter\let\expandafter#1\expandafter=\csname locality\space backup\space \string#1\endcsname
480 \expandafter\popvalue\csname locality\space backup\space \string#1\endcsname%
481 }%
482 }
483 \newcommand{\popvalues}[1]{%
484 \@for\locality@popvalues@macroname:=#1\do{%
485 \expandafter\popvalue\locality@popvalues@macroname
486 }%
487 }
488 % \end{macrocode}
489 % \end{macro}
490 % \end{macro}
491 % \end{macro}
492 % \end{macro}
493 %
494 % \begin{macro}{\makecommandslocal}
495 % Save the macros, run the code, then restore the values.
496 % \begin{macrocode}
497 \newcommand{\makecommandslocal}[2]{\savevalues{#1}#2\restorevalues{#1}}
498 % \end{macrocode}
499 % \end{macro}
500 %
501 %
502 % \begin{macro}{\makecounterslocal}
503 % To make counters local without redefining them too badly (for example, this
504 % should work with the |calc| package, as long as you load |calc| first),
505 % we disable |\global|, and set |\gdef| and |\xdef| to |\def| and |\edef|
506 % respectively.
507 %
508 % \begin{macro}{\ignoreglobal}
509 % \begin{macro}{\obeyglobal}
510 % \begin{macro}{\unignoreglobal}
511 % We save the values of |\global|, |\gdef|, |\xdef|, globally, so
512 % that multiple calls don't fail.
513 %
514 % We also save the value of |\@cons|, a special macro used in counters, which
515 % uses |\xdef| to append something to a list. Since it must be redefined for
516 % counters, I'll redefine it here to do without |\xdef| fails.
517 %
518 % For reference, the original definition of |\@cons|, from |latex.ltx|, is \\
519 % |\def\@cons#1#2{\begingroup\let\@elt\relax\xdef#1{#1\@elt #2}\endgroup}|.
520 % I try to make this forward-compatible, but if the definition of |\@cons|
521 % changes too badly, this'll break.
522 % \begin{macrocode}
523 \savevalues{\global\gdef\xdef\@cons}
524 {\def\begingroup{\begingroup\DeclareNonlocalMacro{##1}}%
525 \expandafter\expandafter\expandafter
526 }%
527 \expandafter\expandafter\expandafter\def
528 \expandafter\expandafter\expandafter\locality@cons
529 \expandafter\expandafter\expandafter#%
530 \expandafter\expandafter\expandafter1%
531 \expandafter\expandafter\expandafter#%
532 \expandafter\expandafter\expandafter2%
533 \expandafter\expandafter\expandafter{\@cons{#1}{#2}}%
534 \newcommand{\obeyglobal}{\restorevalues{\global\gdef\xdef\@cons}}
535 \newcommand{\unignoreglobal}{\popvalues{\global,\gdef,\xdef,\@cons}}
536 \newcommand{\ignoreglobal}{%
537 \pushvalues{\global,\gdef,\xdef,\@cons}%
538 \let\global=\relax \let\gdef=\def \let\xdef=\edef
539 \let\@cons=\locality@cons
540 \expandafter\def\expandafter\unignoreglobal\expandafter{\expandafter
541 \def\expandafter\unignoreglobal\expandafter{\unignoreglobal}%
542 \unignoreglobal}%
543 }
544 % \end{macrocode}
545 % \end{macro}
546 % \end{macro}
547 % \end{macro}
548 %
549 % Now, the actual macro.
550 %
551 % We redefine |\stepcounter|, |\addtocounter|, |\refstepcounter|,
552 % |\setcounter|, |\@addtoreset|, and |\@definecounter|.
553 %
554 % Since |\newcounter| does everything with |\@addtoreset| and
555 % |\@definecounter|, it doesn't need and changes.
556 % \begin{macrocode}
557 \newcommand{\makecounterslocal}{% FIX, to make more robust
558 \expandafter\def\expandafter\stepcounter
559 \expandafter##\expandafter1\expandafter{%
560 \expandafter\ignoreglobal\stepcounter{##1}%
561 \unignoreglobal
562 }%
563 %
564 \expandafter\def\expandafter\refstepcounter
565 \expandafter##\expandafter1\expandafter{%
566 \expandafter\ignoreglobal\refstepcounter{##1}%
567 \unignoreglobal
568 }%
569 %
570 \expandafter\def\expandafter\setcounter
571 \expandafter##\expandafter1%
572 \expandafter##\expandafter2\expandafter{%
573 \expandafter\ignoreglobal\setcounter{##1}{##2}%
574 \unignoreglobal
575 }%
576 %
577 \expandafter\def\expandafter\addtocounter
578 \expandafter##\expandafter1%
579 \expandafter##\expandafter2\expandafter{%
580 \expandafter\ignoreglobal\addtocounter{##1}{##2}%
581 \unignoreglobal
582 }%
583 %
584 \expandafter\def\expandafter\@addtoreset
585 \expandafter##\expandafter1%
586 \expandafter##\expandafter2\expandafter{%
587 \expandafter\ignoreglobal\@addtoreset{##1}{##2}%
588 \unignoreglobal
589 }%
590 %
591 \expandafter\def\expandafter\@definecounter
592 \expandafter##\expandafter1\expandafter{%
593 \expandafter\ignoreglobal\@definecounter{##1}%
594 \unignoreglobal
595 }%
596 \locality@fix@for@amstext
597 \locality@fix@for@calc
598 }
599 % \end{macrocode}
600 %
601 % Following the example of the |calc| package, if the amstext package is loaded
602 % we must add the |\iffirstchoice@| switch as well. We patch the commands this
603 % way since it's good practice when we know how many arguments they take.
604 %
605 % We use |\AtEndPreamble| to ensure that we catch the other package loads.
606 % \begin{macrocode}
607 \AtEndPreamble{
608 \@ifpackageloaded{amstext}{
609 \newcommand{\locality@fix@for@amstext}{
610 \expandafter\def\expandafter\stepcounter
611 \expandafter##\expandafter1\expandafter{%
612 \expandafter\iffirstchoice@\stepcounter{##1}\fi
613 }
614 \expandafter\def\expandafter\addtocounter
615 \expandafter##\expandafter1%
616 \expandafter##\expandafter2\expandafter{%
617 \expandafter\iffirstchoice@\addtocounter{##1}{##2}\fi
618 }
619 }
620 }{
621 \let\locality@fix@for@amstext=\relax
622 }
623 \@ifpackageloaded{calc}{%
624 \def\locality@fix@for@calc{\expandafter\def\expandafter\begingroup\expandafter{\begingroup\DeclareNonlocalTheRegister{\calc@A}}}
625 }{
626 \let\locality@fix@for@calc=\relax
627 }
628 }
629 % \end{macrocode}
630 % \end{macro}
631 %
632 %
633 % \Finale
634 \endinput