summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/locality/locality.dtx
blob: f6fb404739240ee75157a1157be64997611bb41f (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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
% \iffalse meta-comment
%
% Copyright (C) 2010 by Jason Gross (JasonGross9+locality@gmail.com)
%
% This file may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either
% version 1.3c 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.3c or later is part of all distributions of
% LaTeX version 2005/12/01 or later.
%
% \fi
%
%
% \iffalse
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{locality}
%<package> [2010/11/11 v0.2 Various macros for keeping things local]
%<package>\RequirePackage{etoolbox}[2009/08/06]
%
%<*driver>
\documentclass{ltxdoc}
\usepackage{locality}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{locality.dtx}
\end{document}
%</driver>
% \fi
%
% \CheckSum{573}
%
% \CharacterTable
% {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
%  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
%  Digits        \0\1\2\3\4\5\6\7\8\9
%  Exclamation   \!     Double quote  \"     Hash (number) \#
%  Dollar        \$     Percent       \%     Ampersand     \&
%  Acute accent  \'     Left paren    \(     Right paren   \)
%  Asterisk      \*     Plus          \+     Comma         \,
%  Minus         \-     Point         \.     Solidus       \/
%  Colon         \:     Semicolon     \;     Less than     \<
%  Equals        \=     Greater than  \>     Question mark \?
%  Commercial at \@     Left bracket  \[     Backslash     \\
%  Right bracket \]     Circumflex    \^     Underscore    \_
%  Grave accent  \`     Left brace    \{     Vertical bar  \|
%  Right brace   \}     Tilde         \~}
%
% \changes{v0.1}{2010/06/15}{Initial version.  Please note that some macros
%                            have not yet been tested.}
% \changes{v0.2}{2010/11/11}{Switched the order of tests in \string
%           \manyaftergroup, eliminating an infinite loop.  Added \string
%           \DeclareNonlocal*s\space, \string\pushvalues, \string\popvalues,
%           \string\pushvalue, \string\popvalue, and a few others. Fixed 
%           \string\DeclareNonlocal*\space to work with \string\aftergroup.
%           Added requirement for etoolbox, so that patching takes place at
%           the end of the preamble.}
%
% \GetFileInfo{locality.sty}
%
% \DoNotIndex{\#,\$,\%,\&,\@,\\,\{,\},\^,\_,\~,\ }
% \DoNotIndex{}
%
% \title{The \textsf{locality} package\thanks{This document
%   corresponds to \textsf{locality}~\fileversion,
%   dated~\filedate.}}
% \author{Jason Gross \\ \texttt{JasonGross9+locality@gmail.com}}
%
% \maketitle
%
% \section{Introduction}
% 
% The \textsf{locality} package provides various macros to keep changes local
% to the current group.  This allows one to (re)define helper macros
% without worrying about accidentally changing the functionality of another
% package's or the user's definitions.  Additionally, it allows recursive
% macros to have some definitions persist between calls, and others be local.
%
% \section{Usage}
% I give the usage and specification of every macro defined.  I give bugs when
% I know them (please email me if you find other bugs, or have fixes for the
% bugs I list).  I sometimes give extra description or justification.
%
% \DescribeMacro{\manyaftergroup}
% \noindent Usage: |\manyaftergroup|\marg{tokens} \par\noindent
% Specification: The \meta{tokens} get placed after the current group \par\noindent
% Bugs: No braces are permitted, spaces are stripped
%
% I've often wanted to use \TeX's |\aftergroup| with a variable-length
% argument. This macro allows this.  It is expandable (may be used in
% |\edef|), but it gobbles spaces.
%
% ToDo: Write a version of this macro that preserves spaces, allows braces.
%
% \DescribeMacro{\locallydefine}
% \noindent Usage: |\locallydefine|\marg{macro}\marg{processing} \par\noindent
% Specification: Execute \meta{processing} inside of a group, and make
% the definition of |\macro| persist after the group ends. \par\noindent
% Bugs: Changes via |\let| to an unexpandable macro yield an infinite recursive
% loop.
%
% Normally, when you define a macro in a group, it's definition reverts after
% the group ends, unless you use |\global|.  If you use |\global|, then the
% new definition replaces the current definition on all levels.  This macro
% provides something in between.
%
% \DescribeMacro{\DeclareNonlocalMacro}
% \noindent Usage: |\DeclareNonlocalMacro|\marg{macro} \par\noindent
% Specification: Any changes to the definition of |\macro| persist
% after the end of the current group. \par\noindent
% Bugs: Changes via |\let| to an unexpandable macro yield an infinite recursive
% loop.  Only works with |\begingroup| |\endgroup| (not with braces).
%
% This macro generalized |\locallydefine|.
%
%
% \DescribeMacro{\DeclareNonlocalMacros}
% \noindent Usage: |\DeclareNonlocalMacros|\marg{macro list} \par\noindent
% \noindent Example: |\DeclareNonlocalMacros{\macroi,\macroii}| \par\noindent
% Specification: \marg{macro list} Should be a comma-separated list of macros.
% This command will run |\DeclareNonlocalMacro| on each argument.
%
%
% \DescribeMacro{\DeclareNonlocalTheRegister}\DescribeMacro{\DeclareNonlocalCount}
% \DescribeMacro{\DeclareNonlocalDimen}\DescribeMacro{\DeclareNonlocalSkip}
% \DescribeMacro{\DeclareNonlocalMuskip}\DescribeMacro{\DeclareNonlocalToks}
% \noindent Usage: |\DeclareNonlocalCount|\marg{count} \par\noindent
% \phantom{Usage:} |\DeclareNonlocalDimen|\marg{dimen} \par\noindent
% \phantom{Usage:} |\DeclareNonlocalSkip|\marg{skip} \par\noindent
% \phantom{Usage:} |\DeclareNonlocalMuskip|\marg{muskip} \par\noindent
% \phantom{Usage:} |\DeclareNonlocalToks|\marg{toks} \par\noindent
% Specification: Any changes to the value in \meta{register} persist
% after the end of the current group. \par\noindent
%
% Registers for \meta{count}s, \meta{dimen}s, \meta{skip}s, and \meta{muskips}s
% can all be made nonlocal with |\DeclareNonlocalTheRegister|.
%
% Bugs: Only works with |\begingroup| |\endgroup| (not with braces).  Only
%  works if the argument is a single token.
%
% These do the same thing to registers (\meta{count}s, \meta{length}s, and 
% \meta{tok}s) that |\DeclareNonlocalMacro| does to macros.
%
%
% \DescribeMacro{\DeclareNonlocalTheRegister}\DescribeMacro{\DeclareNonlocalCount}
% \DescribeMacro{\DeclareNonlocalDimen}\DescribeMacro{\DeclareNonlocalSkip}
% \DescribeMacro{\DeclareNonlocalMuskip}\DescribeMacro{\DeclareNonlocalToks}
% \noindent Usage: |\DeclareNonlocalCounts|\marg{list of counts (comma separated)} \par\noindent
% \phantom{Usage:} |\DeclareNonlocalDimens|\marg{list of dimens (comma separated)} \par\noindent
% \phantom{Usage:} |\DeclareNonlocalSkips|\marg{list of skips (comma separated)} \par\noindent
% \phantom{Usage:} |\DeclareNonlocalMuskips|\marg{list of muskips} \par\noindent
% \phantom{Usage:} |\DeclareNonlocalTokses|\marg{list of toks (comma separated)} \par\noindent
% Specification: Any changes to the value in \meta{register}s persist
% after the end of the current group. \par\noindent
%
% \DescribeMacro{\pushvalue}\DescribeMacro{\popvalue}\noindent 
% Usage: |\pushvalue|\marg{macro} \par\noindent
% \phantom{Usage:} |\popvalue|\marg{macro} \par
% \noindent Specification: The argument \marg{macro} is backed up by
%  |\pushvalue|, and the most recently backed up value is restored by
%  |\popvalue|.  The macro definitions are saved using a stack.
%
%
% \DescribeMacro{\savevalues}\DescribeMacro{\restorevalues}
% \noindent Usage: |\savevalues|\marg{list of macros (no separator)} \par\noindent
% \phantom{Usage:} |\restorevalues|\marg{list of macros (no separator)} \par\noindent
% Specification: Every token in the passed argument is backed up by
%  |\savevalues|, and the most recently backed up values are restored by
%  |\restorevalues|.
%
%
% \DescribeMacro{\pushvalues}\DescribeMacro{\popvalues}\noindent 
% Usage: |\pushvalues|\marg{list of macros (comma separated)} \par\noindent
% \phantom{Usage:} |\popvalues|\marg{list of macros (comma separated)} \par
%  \noindent Specification: Every macro in the passed argument is backed up by
%  |\pushvalues|, and the most recently backed up values are restored by
%  |\popvalues|.  The macro definitions are saved using a stack.
%
%
% \DescribeMacro{\makecommandslocal}
% \noindent Usage: |\makecommandslocal|\marg{list of macros (no separator)}\marg{code} \par\noindent
% Specification: Every token in the first argument is made local to \meta{code};
%  changes made to their definitions do not perist outside of \meta{code}.
%
% This macro is the natural opposite of |\DeclareNonlocalMacro|; it allows 
% some macros to behave as if \meta{code} was inside a group, while the rest
% of the macros behave as if they were not.
%
%
% \DescribeMacro{\ignoreglobal}\DescribeMacro{\obeyglobal}\DescribeMacro{\unignoreglobal}
% \noindent Usage: |\ignoreglobal| \par\noindent
% \noindent Usage: |\unignoreglobal| \par\noindent
% \noindent Usage: |\obeyglobal| \par\noindent
% Specification: The macro |\ignoreglobal| causes global changes, such as
% |\edef|, |\xdef|, and those prefaced by |\global|, to be local.  The macro 
% |\obeyglobal| causes these to be treated as global.  The macro 
% |\unignoreglobal| undoes the changes made by the last |\ignoreglobal|.  If
% you call |\ignoreglobal| twice, then you must call |\unignoreglobal| twice to
% allow global changes.
%
% \DescribeMacro{\makecounterslocal}
% \noindent Usage: |\makecounterslocal| \par\noindent
% Specification: The macro |\makecounterslocal| redefines the \LaTeX{} 
% \meta{counter} macros so that their changes are local, instead of global.
% At the end of the group in which |\makecounterslocal| is called, 
% \meta{counter} macros revert to being global.
%
% \StopEventually{\PrintChanges\PrintIndex}
%
% \section{Implementation}
% \subsection{Helper functions}
% The following definitions are preliminary, to allow various tricks with |\def|.
%    \begin{macrocode}
\def\@nil{\@nil\relax} % this way, I'll know if I've messed up; I'll get
% a stack overflow error.
\def\if@nil#1{\@if@nil#1\@@nil}
\def\@if@nil#1#2\@@nil{\ifx\@nil#1}
% We'll be messing with |\global|, so we better have a backup.
\let\locality@tex@global=\global
%    \end{macrocode}
%
% \begin{macro}{\locality@global@non@collision@unique@count}
% At various places, I want to have a macro associated with a certain name
% which hasn't been used before.  I use this count to number them.  I use
% a count, instead of a counter, so that I can control whether or not it's
% global.  The long name, with lots of |@|s, is to (hopefully) avoid 
% collisions.
%    \begin{macrocode}
\newcount\locality@global@non@collision@unique@count
\locality@global@non@collision@unique@count=0
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\manyaftergroup}
%
% The macro |\manyaftergroup| works by parsing it's argument one token at a
% time, and applying |\aftergroup| to each argument.  It checks for the end
% with |\@nil|.
%    \begin{macrocode}
\long\def\@manyaftergroup#1{\if@nil#1 \else \aftergroup#1 
\expandafter\@manyaftergroup\fi}
\newcommand{\manyaftergroup}[1]{\@manyaftergroup#1\@nil}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\locallydefine}
% Execute the second argument passed locally, and then preserve the definition
% of the first argument passed.
%    \begin{macrocode}
\newcommand{\locallydefine}[2]{{#2\expandafter}%
  \expandafter\def\expandafter#1\expandafter{#1}}
%    \end{macrocode}
%
% \end{macro}
%
% The |\DeclareNonlocal| macros do some fancy stuff with |\begingroup| and
% |\endgroup|, so the old definitions must be saved.
%    \begin{macrocode}
\let\locality@tex@begingroup=\begingroup
\let\locality@tex@endgroup=\endgroup
%    \end{macrocode}
% These macros are extended versions of the |\locallydefine| macro; they 
% redefine |\endgroup| to preserve definitions after the current group ends.
% 
% Because |\aftergroup| would occur before definition restoration, we patch
% |\aftergroup| so that it instead appends tokens to the end of |\endgroup|.
% This doesn't fix all use cases, but it should fix a problem with the calc
% package.
%    \begin{macrocode}
\newcount\locality@global@aftergroup@count
\locality@global@aftergroup@count=-1
\newcommand\locality@patch@aftergroup{%
  \def\aftergroup{%
    \locality@tex@global\advance\locality@global@aftergroup@count by 1
    \afterassignment\locality@aftergroup\locality@tex@global
    \expandafter\let\csname locality@nextchar\space\the
      \locality@global@aftergroup@count\endcsname=%
  }%
}
\newcommand\locality@aftergroup{%
  \expandafter\expandafter\expandafter\def
  \expandafter\expandafter\expandafter\endgroup
  \expandafter\expandafter\expandafter{\expandafter\endgroup
    \csname locality@nextchar\space\the\locality@global@aftergroup@count
    \endcsname}%
}
%    \end{macrocode}
%
%
% \begin{macro}{\DeclareNonlocalMacro}
% This macro redefines |\endgroup| to do this for macro passed to it.
%
% First, back up |\endgroup| to a new macro.
%    \begin{macrocode}
\newcommand{\DeclareNonlocalMacro}[1]{%
  \locality@patch@aftergroup % first, patch |\aftergroup|
  \expandafter\let
  \csname endgroup \the\locality@local@group@non@local@macro@count
    \endcsname=\endgroup
%    \end{macrocode}
% Redefine |\endgroup| to, in order: revert it's definition, insert code to 
% update the definition of the passed macro outside of the group, and call 
% the (reverted) version of |\endgroup|.
%    \begin{macrocode}
  \expandafter\def\expandafter\endgroup\expandafter{%
    \expandafter\expandafter\expandafter\let\expandafter
      \expandafter\expandafter\endgroup\expandafter\expandafter
      \csname endgroup \the\locality@local@group@non@local@macro@count
        \endcsname\expandafter\endgroup
    \expandafter\def\expandafter#1\expandafter{#1}}%
  \advance\locality@local@group@non@local@macro@count by 1
}%
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\newcommand\locality@declarenonlocals[2]{%
  \@for\locality@declarenonlocals@name:=#2\do{%
    \expandafter #1\expandafter{\locality@declarenonlocals@name}%
  }%
}
%    \end{macrocode}
%
% \begin{macro}{\DeclareNonlocalMacros}
%    \begin{macrocode}
\newcommand{\DeclareNonlocalMacros}[1]{\locality@declarenonlocals{\DeclareNonlocalMacro}{#1}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\DeclareNonlocalTheRegister}
% \begin{macro}{\DeclareNonlocalCount}
% \begin{macro}{\DeclareNonlocalDimen}
% \begin{macro}{\DeclareNonlocalSkip}
% \begin{macro}{\DeclareNonlocalMuskip}
% This works the same way as as |\DeclareNonlocalMacro|, but uses |\the| instead of 
% |\def|.
%    \begin{macrocode}
\newcommand{\DeclareNonlocalTheRegister}[1]{%
  \locality@patch@aftergroup % first, patch |\aftergroup|
  \expandafter\let
  \csname endgroup \the\locality@local@group@non@local@macro@count
    \endcsname=\endgroup
  \expandafter\def\expandafter\endgroup\expandafter{%
    \expandafter\expandafter\expandafter\let
      \expandafter\expandafter\expandafter\endgroup
      \expandafter\expandafter
      \csname endgroup \the\locality@local@group@non@local@macro@count
        \endcsname\expandafter\endgroup
    \expandafter#1\expandafter=\the#1 }% Note the space.  This is to 
% prevent something like 
% |\newcount\tempc\begingroup \DeclareNonlocalCount\tempc \tempc=1\endgroup1|
% from setting |\tempc| to 11.
  \advance\locality@local@group@non@local@macro@count by 1
}%
\let\DeclareNonlocalCount=\DeclareNonlocalTheRegister
\let\DeclareNonlocalDimen=\DeclareNonlocalTheRegister
\let\DeclareNonlocalSkip=\DeclareNonlocalTheRegister
\let\DeclareNonlocalMuskip=\DeclareNonlocalTheRegister
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\DeclareNonlocalTheRegisters}
% \begin{macro}{\DeclareNonlocalCounts}
% \begin{macro}{\DeclareNonlocalDimens}
% \begin{macro}{\DeclareNonlocalSkips}
% \begin{macro}{\DeclareNonlocalMuskips}
%    \begin{macrocode}
\newcommand{\DeclareNonlocalTheRegisters}[1]{\locality@declarenonlocals{\DeclareNonlocalTheRegister}{#1}}
\let\DeclareNonlocalCounts=\DeclareNonlocalTheRegisters
\let\DeclareNonlocalDimens=\DeclareNonlocalTheRegisters
\let\DeclareNonlocalSkips=\DeclareNonlocalTheRegisters
\let\DeclareNonlocalMuskips=\DeclareNonlocalTheRegisters
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\DeclareNonlocalToks}
% This works the same way as as |\DeclareNonlocalCount|, but puts braces around
% the assigned value; |\toks0=1| fails, and should be |\toks0={1}|.
%    \begin{macrocode}
\newcommand{\DeclareNonlocalToks}[1]{%
  \locality@patch@aftergroup % first, patch |\aftergroup|
  \expandafter\let
  \csname endgroup \the\locality@local@group@non@local@macro@count
    \endcsname=\endgroup
  \expandafter\def\expandafter\endgroup\expandafter{%
    \expandafter\expandafter\expandafter\let\expandafter\expandafter
      \expandafter\endgroup\expandafter\expandafter
      \csname endgroup \the\locality@local@group@non@local@macro@count'
      \endcsname\expandafter\endgroup
    \expandafter#1\expandafter=\expandafter{\the#1}}%
  \advance\locality@local@group@non@local@macro@count by 1
}%
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\DeclareNonlocalTokses}
%    \begin{macrocode}
\newcommand{\DeclareNonlocalTokses}[1]{\locality@declarenonlocals{\DeclareNonlocalToks}{#1}}
%    \end{macrocode}
% \end{macro}
%
% I redefine |\begingroup| to reset the locality macros, so nesting works.
%    \begin{macrocode}
\newcount\locality@local@group@non@local@macro@count % Hopefully, this
% won't collide with anything.  I hope putting this out here allows
% proper nesting of groups
\def\begingroup{\locality@tex@begingroup
  \def\endgroup{\locality@tex@endgroup}% not \let, because
                     % that would break my |\aftergroup| patch
  \locality@local@group@non@local@macro@count=0
}
%    \end{macrocode}
%
%
%
% \begin{macro}{\savevalues}
% \begin{macro}{\restorevalues}
% These macros parse their arguments token by token, renaming each |macro| to 
% |@\macro backup|, or vice versa.
%    \begin{macrocode}
\def\@savevalues#1{\ifx#1\@nil \else \expandafter\let
  \csname @\string#1\space backup\endcsname=#1
  \expandafter\@savevalues\fi}
\newcommand{\savevalues}[1]{\@savevalues#1\@nil}

\def\@restorevalues#1{%
\ifx
  #1\@nil
\else
  \expandafter\let\expandafter#1\expandafter
    =\csname @\string#1\space backup\endcsname
    \expandafter
  \let\csname @\string#1\space backup\endcsname
    =\relax
  \expandafter\@restorevalues
\fi
}
\newcommand{\restorevalues}[1]{\@restorevalues#1\@nil}
%    \end{macrocode}
% \end{macro}
% \end{macro}
%
%
% \begin{macro}{\pushvalues}
% \begin{macro}{\popvalues}
% \begin{macro}{\pushvalue}
% \begin{macro}{\popvalue}
%    \begin{macrocode}
\newcommand{\pushvalue}[1]{%
  \@ifundefined{locality\space backup\space \string#1}{}{%
    \expandafter\pushvalue\csname locality\space backup\space \string#1\endcsname%
  }%
  \expandafter\let\csname locality\space backup\space \string#1\endcsname=#1%
}
\newcommand{\pushvalues}[1]{%
  \@for\locality@pushvalues@macroname:=#1\do{%
    \expandafter\pushvalue\locality@pushvalues@macroname
  }%
}
\newcommand{\popvalue}[1]{%
  \@ifundefined{locality\space backup\space \string#1}{%
    \let#1=\relax
  }{%
    \expandafter\let\expandafter#1\expandafter=\csname locality\space backup\space \string#1\endcsname
    \expandafter\popvalue\csname locality\space backup\space \string#1\endcsname%
  }%
}
\newcommand{\popvalues}[1]{%
  \@for\locality@popvalues@macroname:=#1\do{%
    \expandafter\popvalue\locality@popvalues@macroname
  }%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% \end{macro}
%
% \begin{macro}{\makecommandslocal}
% Save the macros, run the code, then restore the values.
%    \begin{macrocode}
\newcommand{\makecommandslocal}[2]{\savevalues{#1}#2\restorevalues{#1}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\makecounterslocal}
% To make counters local without redefining them too badly (for example, this
% should work with the |calc| package, as long as you load |calc| first),
% we disable |\global|, and set |\gdef| and |\xdef| to |\def| and |\edef|
% respectively.
%
% \begin{macro}{\ignoreglobal}
% \begin{macro}{\obeyglobal}
% \begin{macro}{\unignoreglobal}
% We save the values of |\global|, |\gdef|, |\xdef|, globally, so
% that multiple calls don't fail.
%
% We also save the value of |\@cons|, a special macro used in counters, which
% uses |\xdef| to append something to a list.  Since it must be redefined for 
% counters, I'll redefine it here to do without |\xdef| fails.
%
% For reference, the original definition of |\@cons|, from |latex.ltx|, is \\
% |\def\@cons#1#2{\begingroup\let\@elt\relax\xdef#1{#1\@elt #2}\endgroup}|.
% I try to make this forward-compatible, but if the definition of |\@cons|
% changes too badly, this'll break.
%    \begin{macrocode}
\savevalues{\global\gdef\xdef\@cons}
{\def\begingroup{\begingroup\DeclareNonlocalMacro{##1}}%
  \expandafter\expandafter\expandafter
}%
\expandafter\expandafter\expandafter\def
\expandafter\expandafter\expandafter\locality@cons
\expandafter\expandafter\expandafter#%
\expandafter\expandafter\expandafter1%
\expandafter\expandafter\expandafter#%
\expandafter\expandafter\expandafter2%
\expandafter\expandafter\expandafter{\@cons{#1}{#2}}%
\newcommand{\obeyglobal}{\restorevalues{\global\gdef\xdef\@cons}}
\newcommand{\unignoreglobal}{\popvalues{\global,\gdef,\xdef,\@cons}}
\newcommand{\ignoreglobal}{%
  \pushvalues{\global,\gdef,\xdef,\@cons}%
  \let\global=\relax \let\gdef=\def \let\xdef=\edef
  \let\@cons=\locality@cons
  \expandafter\def\expandafter\unignoreglobal\expandafter{\expandafter
  \def\expandafter\unignoreglobal\expandafter{\unignoreglobal}%
  \unignoreglobal}%
}
%    \end{macrocode}
% \end{macro}
% \end{macro}
% \end{macro}
% 
% Now, the actual macro.
%
% We redefine |\stepcounter|, |\addtocounter|, |\refstepcounter|, 
% |\setcounter|, |\@addtoreset|, and |\@definecounter|.
%
% Since |\newcounter| does everything with |\@addtoreset| and
% |\@definecounter|, it doesn't need and changes.
%    \begin{macrocode}
\newcommand{\makecounterslocal}{% FIX, to make more robust
  \expandafter\def\expandafter\stepcounter
    \expandafter##\expandafter1\expandafter{%
    \expandafter\ignoreglobal\stepcounter{##1}%
    \unignoreglobal
  }%
 %
  \expandafter\def\expandafter\refstepcounter
    \expandafter##\expandafter1\expandafter{%
    \expandafter\ignoreglobal\refstepcounter{##1}%
    \unignoreglobal
  }%
 %
  \expandafter\def\expandafter\setcounter
    \expandafter##\expandafter1%
    \expandafter##\expandafter2\expandafter{%
    \expandafter\ignoreglobal\setcounter{##1}{##2}%
    \unignoreglobal
  }%
 %
  \expandafter\def\expandafter\addtocounter
    \expandafter##\expandafter1%
    \expandafter##\expandafter2\expandafter{%
    \expandafter\ignoreglobal\addtocounter{##1}{##2}%
    \unignoreglobal
  }%
 %
  \expandafter\def\expandafter\@addtoreset
    \expandafter##\expandafter1%
    \expandafter##\expandafter2\expandafter{%
    \expandafter\ignoreglobal\@addtoreset{##1}{##2}%
    \unignoreglobal
  }%
 %
  \expandafter\def\expandafter\@definecounter
    \expandafter##\expandafter1\expandafter{%
    \expandafter\ignoreglobal\@definecounter{##1}%
    \unignoreglobal
  }%
  \locality@fix@for@amstext
  \locality@fix@for@calc
}
%    \end{macrocode}
%
% Following the example of the |calc| package, if the amstext package is loaded
% we must add the |\iffirstchoice@| switch as well. We patch the commands this 
% way since it's good practice when we know how many arguments they take.
%
% We use |\AtEndPreamble| to ensure that we catch the other package loads.
%    \begin{macrocode}
\AtEndPreamble{
  \@ifpackageloaded{amstext}{
    \newcommand{\locality@fix@for@amstext}{
      \expandafter\def\expandafter\stepcounter
        \expandafter##\expandafter1\expandafter{%
        \expandafter\iffirstchoice@\stepcounter{##1}\fi
      }
      \expandafter\def\expandafter\addtocounter
        \expandafter##\expandafter1%
        \expandafter##\expandafter2\expandafter{%
        \expandafter\iffirstchoice@\addtocounter{##1}{##2}\fi
      }
    }
  }{
    \let\locality@fix@for@amstext=\relax
  }
  \@ifpackageloaded{calc}{%
    \def\locality@fix@for@calc{\expandafter\def\expandafter\begingroup\expandafter{\begingroup\DeclareNonlocalTheRegister{\calc@A}}}
  }{
    \let\locality@fix@for@calc=\relax
  }
}
%    \end{macrocode}
% \end{macro}
%
%
% \Finale
\endinput