summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/tex/latex/bezos/babeltools.sty
blob: 2818aa365190324de51084c3c34eddf5ff5fc39c (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
% Copyright (c) 2010 by Javier Bezos.
% All Rights Reserved.
%
% It 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 2003/12/01 or later.
% 
% This work has the LPPL maintenance status "maintained".
% 
% The Current Maintainer of this work is Javier Bezos.
%
% TODO:
% noshorthands -> shorthands=off, nocitations, nocrossrefs

\ProvidesPackage{babeltools}[2010/10/14 v0.2 Tools for the babel system (beta)]

\edef\bbtl@savedcatcodes{%
 \catcode`\noexpand\~=\the\catcode`\~
 \catcode`\noexpand\:=\the\catcode`\:
 \catcode`\noexpand\"=\the\catcode`\"}
\catcode`\~=\active
\catcode`\"=12
\catcode`\:=12

\def\bbtl@meaning{\expandafter\strip@prefix\meaning}

\def\bbtl@ifshorthand#1{%
  \@expandtwoargs\in@{#1}{\bbtl@o@shorthands}%
  \ifin@
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi}
   
\let\bbtl@afterbabelhook\@empty
\newcommand\AfterBabel[1]{%
  \g@addto@macro\bbtl@afterbabelhook{#1}}

\let\bbtl@atendofbabelhook\@empty
\newcommand\AtEndOfBabel[1]{%
  \g@addto@macro\bbtl@atendofbabelhook{#1}}

\let\bbtl@beforebabelhook\@empty
\newcommand\BeforeBabel[1]{%
  \g@addto@macro\bbtl@beforebabelhook{#1}}

% Not to be redefined. The current value, saved by babel, is
% restored. More robust code is necessary, I think.

\def\bbtl@restore@org#1{%
  \@ifundefined{#1 }% Not robust?
    {\expandafter\let\csname #1\expandafter\endcsname
     \csname org@#1\endcsname}%
    {\expandafter\let\csname #1 \expandafter\endcsname
     \csname org@#1\endcsname}}

\def\bbtl@save@org#1{%
  \expandafter\let\csname org@#1\expandafter\endcsname
  \csname #1\endcsname}

% Citations and cross refs
% ~~~~~~~~~~~~~~~~~~~~~~~~

\let\bbtl@ignorepackages\@empty

\DeclareOption{nocitations}
  {\g@addto@macro\bbtl@ignorepackages{,cite,natbib,}%
   \AfterBabel{%
     \bbtl@restore@org{nocite}%
     \bbtl@restore@org{bibcite}%
     \bbtl@restore@org{@bibitem}%S
     \bbtl@restore@org{@citex}}}

\DeclareOption{nocrossrefs}
  {\g@addto@macro\bbtl@ignorepackages{,varioref,}%
   \BeforeBabel{%
     \bbtl@save@org{@newl@bel}}% babel DOESN'T save it!
   \AfterBabel{%
     \bbtl@restore@org{@newl@bel}%
     \bbtl@restore@org{ref}%  Robust
     \bbtl@restore@org{pageref}}}%  Robust

% More cases are to be added, but they must be delayed at the end of
% begin document, because they are related to loaded packages (see
% babel.dtx)
%
% Shorthands
% ~~~~~~~~~~
% Some languages use the shortand mechanism for tools not defined
% otherwise, so you can still use them with \babelshorthand{..}.
% Then you can use it conveniently to define your own macros.

% ~ is an special case, as it's defined in babel.def

% New shorthands
% --------------

\DeclareOption{adaptive}%
  {\AfterBabel{%
     \let\bbtl@initiate@active@char\initiate@active@char
     \let\initiate@active@char\bbtl@set@shorthand
     \let\bbtl@declare@shorthand\declare@shorthand
     \let\declare@shorthand\bbtl@declare
     \let\bbl@activate\bbtl@activate
     \let\bbtl@deactivate\bbl@deactivate
     \let\bbl@deactivate\bbtl@restore}}

% Do \bbtl@initiate@active@char but don't make the char active.
% Not necessary and a source of bugs.

\def\bbtl@set@shorthand#1{%
  \expandafter\edef\csname bbtl@savecat\string#1\endcsname
    {\the\catcode`#1}%
  \bbtl@initiate@active@char{#1}%
  \catcode`#1=\csname bbtl@savecat\string#1\endcsname\relax
  \addto\bbtl@select{\shorthandon{#1}}}

\def\bbtl@use@shorthand{%
  \if@safe@actives
    \bbl@afterelse
    \string
  \else
    \bbl@afterfi
    {\ifx\thepage\relax
       \bbl@afterelse
       \string
     \else
       \bbl@afterfi
       \bbtl@use@sh
     \fi}%
  \fi}

\def\bbtl@use@sh#1{%
  \ifx\protect\@unexpandable@protect
     \bbl@afterelse
     \noexpand#1%
   \else%
     \bbl@afterfi
     \textormath
       {\csname active@char\string#1\endcsname}%
       {\csname normal@char\string#1\endcsname}%
   \fi}

\gdef\bbtl@activate#1{%
  \begingroup
    \lccode`\~=`#1
    \lowercase{%
   \endgroup
   \def~{\bbtl@use@shorthand~}}}

\def\bbtl@restore#1{%
  \shorthandon{#1}%
  \begingroup
    \lccode`\~=`#1
    \lowercase{%
  \endgroup
  \bbtl@deactivate{~}}}

\def\bbtl@declare#1#2{%
  \@ifundefined{bbtl@savecat\expandafter\string\@firstoftwo#2\@empty}%
    {\@gobble}%
    {\bbtl@declare@shorthand{#1}{#2}}}

% No shorthands
% -------------

% This is done after 'adaptive', if present.

\DeclareOption{shorthands=off}%
  {\AfterBabel{\bbl@doshorthands}}

\def\babelshorthand#1{%
  \def\bbtl@tempa{#1}%
  \@ifundefined{bbtl@@\languagename @@\bbtl@meaning\bbtl@tempa}%
    {#1}%
    {\@nameuse{bbtl@@\languagename @@\bbtl@meaning\bbtl@tempa}}}

% Select
% ----------------------
\let\bbtl@o@shorthands\@empty

% TO DO: package options are expanded by LaTeX, and ~ raises
% an error, but not \string~. Is there a way to fix it?

% This is done after 'adaptive', if present.

\def\bbl@doshorthands{%
   \let\bbtl@s@initiate@active@char\initiate@active@char
   \def\initiate@active@char##1{%
     \bbtl@ifshorthand{\string##1}%
       {\bbtl@s@initiate@active@char{##1}}%
       {}}%
   \let\bbtl@s@declare@shorthand\declare@shorthand
   \def\declare@shorthand##1##2{%
     \bbtl@ifshorthand{\expandafter\string\@firstoftwo##2\@empty}%
       {\bbtl@s@declare@shorthand{##1}{##2}}%
       {\def\bbtl@tempa{##2}%
        \@namedef{bbtl@@##1@@\bbtl@meaning\bbtl@tempa}}}%
   \let\bbtl@s@activate\bbl@activate
   \def\bbl@activate##1{%
     \bbtl@ifshorthand{\string##1}%
       {\bbtl@s@activate{##1}}%
       {}}%
   \let\bbtl@s@deactivate\bbl@deactivate
   \def\bbl@deactivate##1{%
     \bbtl@ifshorthand{\string##1}%
       {\bbtl@s@deactivate{##1}}%
       {}}}

% No config
% ~~~~~~~~~
% Make sure an unexpected local config doesn't spoil your document.
% Use of config files is strongly discouraged, but some sites
% use them

\DeclareOption{noconfig}
  {\AfterBabel{\let\loadlocalcfg\@gobble}}

% Captions
% ~~~~~~~~

% redefining names. Somewhat experimental.

\def\bbtl@captions{%
  \bbtl@elt\prefacename\bbtl@elt\refname\bbtl@elt\abstractname
  \bbtl@elt\bibname\bbtl@elt\chaptername\bbtl@elt\appendixname
  \bbtl@elt\contentsname\bbtl@elt\listfigurename
  \bbtl@elt\listtablename\bbtl@elt\indexname
  \bbtl@elt\figurename\bbtl@elt\tablename\bbtl@elt\partname
  \bbtl@elt\enclname\bbtl@elt\ccname\bbtl@elt\headtoname
  \bbtl@elt\pagename\bbtl@elt\seename\bbtl@elt\alsoname
  \bbtl@elt\proofname\bbtl@elt\glossaryname}

% 1: \captions... expanded #2 repl fun bbtl@f #3 \...name
% \lang...name
\def\bbtl@rep#1\@@\@@#2#3#4{%
  \in@{#3}{#1}%
  \ifin@
    #2#1\@@\@@#3#4%
  \else
    \PackageWarningNoLine{babeltools}%
      {No \string#3 in \bbtl@l}%
  \fi}

% \bbtl@f is a helper macro:
% 1 \...name
% bbtl@f:
% #1 prev, #2 def of \...name, #3 after
% #4 \...name, #5 \lang...name
\DeclareOption{langcaptions}
  {\def\bbtl@repdef#1{%
     \def\bbtl@f##1#1##2##3\@@\@@##4##5{%
       \def##5{##2}%
       \@namedef{captions\bbtl@l}{##1##4{##5}##3}}}%
   \def\bbtl@elt#1{%
     \bbtl@repdef#1%
     \@temptokena\expandafter\expandafter\expandafter{%
       \csname captions\bbtl@l\endcsname}%
     % If \lang...name undefined
     \edef\bbtl@t{%
       \noexpand\bbtl@rep
       \the\@temptokena
       \noexpand\@@\noexpand\@@
       \noexpand\bbtl@f
       \noexpand#1
       \expandafter\noexpand
         \csname\bbtl@l\expandafter\@gobble\string#1\endcsname}%
     \bbtl@t}%
   \def\bbtl@repdo{%
     \@for\bbtl@l:=\bbtl@options\do{%
       \@ifundefined{captions\bbtl@l}{}{%
       \bbtl@captions}}}%
   \AtEndOfBabel{\bbtl@repdo}}

% Key=value package options
% ~~~~~~~~~~~~~~~~~~~~~~~~~

\def\bbtl@process#1=#2\bbtl@process#3{%
  \expandafter\ifx\csname bbtl@o@#1\endcsname\@empty
    \expandafter\edef\csname bbtl@o@#1\endcsname{#2}%
    \DeclareOption{\CurrentOption}{}%
  \fi}

\DeclareOption*{%
  \@expandtwoargs\in@{\string=}{\CurrentOption}%
  \ifin@
    \expandafter\bbtl@process\CurrentOption\bbtl@process\CurrentOption
  \fi}

% Process options
% ~~~~~~~~~~~~~~~~

\ProcessOptions

% shorthands=... option is handled here.

\show\bbtl@o@shorthands

\ifx\bbtl@o@shorthands\@empty\else
  \edef\bbtl@o@shorthands{\bbtl@meaning\bbtl@o@shorthands}%
  \bbtl@ifshorthand{'}%
    {\PassOptionsToPackage{activeacute}{babel}}{}
  \bbtl@ifshorthand{`}%
    {\PassOptionsToPackage{activegrave}{babel}}{}
  \bbtl@ifshorthand{:}{}%
    {\g@addto@macro\bbtl@ignorepackages{,hhline,}}
% After any redefinition, the shorthand mechanism comes.
  \AfterBabel{\bbl@doshorthands}
\fi

% The first \LdfInit loads the babel core (the sty file doesn't!)
% That means babeltools is not compatible with classes loading
% a language:

\@ifundefined{ldf@finish}%
  {\def\LdfInit{%
    \expandafter\let\expandafter\bbtl@options
       \csname opt@babel.sty\endcsname
     \bbtl@beforebabelhook
     \AtBeginDocument{%
       \let\bbtl@ifpackageloaded\@ifpackageloaded
       \def\@ifpackageloaded##1{%
         \@expandtwoargs\in@{,##1,}{\bbtl@ignorepackages}%
         \ifin@
           \expandafter\@gobbletwo
         \else
           \expandafter\bbtl@ifpackageloaded
         \fi
         {##1}}}%
     \chardef\atcatcode =\catcode`\@
     \catcode`\@=11\relax
     \input babel.def\relax
     \catcode`\@ =\atcatcode
     \let\atcatcode\relax
     \bbtl@afterbabelhook
     \AtEndOfPackage{\bbtl@atendofbabelhook}%
     \LdfInit}}
  {\PackageError{babeltools}%
     {Too late -- babel has been loaded}%
     {babeltools can be used only before babel.\MessageBreak
      Very likely a package or the class has loaded it.}}

\AtEndOfBabel{%
  \AtBeginDocument{\let\@ifpackageloaded\bbtl@ifpackageloaded}}

\bbtl@savedcatcodes

\endinput

==============================
==============================

- Attributes?
- Quotes ''