Move a tag
[latex2e.git] / latex2e-20151001 / required / babel / bbunicode.dtx
blobbcbc4792840fa0c020dc7c363e19700f5ed276e7
1 % \iffalse meta-comment
3 % Copyright 2013-2014 Javier Bezos and any individual authors
4 % listed elsewhere in this file.  All rights reserved.
5
6 % This file is part of the Babel system.
7 % --------------------------------------
8
9 % It may be distributed and/or modified under the
10 % conditions of the LaTeX Project Public License, either version 1.3
11 % of this license or (at your option) any later version.
12 % The latest version of this license is in
13 %   http://www.latex-project.org/lppl.txt
14 % and version 1.3 or later is part of all distributions of LaTeX
15 % version 2003/12/01 or later.
16
17 % This work has the LPPL maintenance status "maintained".
18
19 % The Current Maintainer of this work is Javier Bezos.
20
21 % The list of all files belonging to the Babel system is
22 % given in the file `manifest.bbl. See also `legal.bbl' for additional
23 % information.
24
25 % The list of derived (unpacked) files belonging to the distribution
26 % and covered by LPPL is defined by the unpacking scripts (with
27 % extension .ins) which are part of the distribution.
28 % \fi
29 % \CheckSum{795}
31 % \iffalse
32 %<*dtx>
33 \ProvidesFile{bbunicode.dtx}
34        [2015/05/10 v1.0e Babel hooks for Unicode engines]
35 %</dtx>
37 %% File `bbunicode.dtx'
38 %% Babel package for LaTeX version 2e
39 %% Copyright (C) 2103-2014
40 %%           by Javier Bezos
42 %<*filedriver>
43 \documentclass{ltxdoc}
44 \font\manual=logo10 % font used for the METAFONT logo, etc.
45 \newcommand*\MF{{\manual META}\-{\manual FONT}}
46 \newcommand*{\babel}{\textsf{babel}}
47 \newcommand*{\langvar}{$\langle \it lang \rangle$}
48 \newcommand*{\note}[1]{}
49 \newcommand*{\pkg}[1]{\textsf{#1}}
50 \newcommand*{\Lopt}[1]{\textsf{#1}}
51 \newcommand*{\file}[1]{\texttt{#1}}
52 \begin{document}
53  \DocInput{bbunicode.dtx}
54 \end{document}
55 %</filedriver>
56 % \fi
58 % \GetFileInfo{bbunicode.dtx}
59 % \StopEventually{}
61 % \section{Tentative font handling}
63 % A general solution is far from trivial:
64 % \begin{itemize}
65 % \item |\addfontfeature| only sets it for the current family and it's
66 %   not very efficient, and
67 % \item |\defaultfontfeatures| requires to redefine the font (and the
68 %   opti\texttt{}ons aren't ``orthogonal'').
69 % \end{itemize}
71 %    \begin{macrocode}
72 %<<*Font selection>>
73 \def\babelFSstore#1{%
74   \bbl@for\bbl@tempa{#1}{%
75     \edef\bbl@tempb{\noexpand\bbl@FSstore{\bbl@tempa}}
76     \bbl@tempb{rm}\rmdefault\bbl@save@rmdefault
77     \bbl@tempb{sf}\sfdefault\bbl@save@sfdefault
78     \bbl@tempb{tt}\ttdefault\bbl@save@ttdefault}}
79 \def\bbl@FSstore#1#2#3#4{%
80   \bbl@csarg\edef{#2default#1}{#3}%
81   \expandafter\addto\csname extras#1\endcsname{%
82     \let#4#3%
83     \ifx#3\f@family
84       \edef#3{\csname bbl@#2default#1\endcsname}%
85       \fontfamily{#3}\selectfont
86     \else
87       \edef#3{\csname bbl@#2default#1\endcsname}%
88     \fi}%
89   \expandafter\addto\csname noextras#1\endcsname{%
90     \ifx#3\f@family
91       \fontfamily{#4}\selectfont
92     \fi
93     \let#3#4}}
94 \let\bbl@langfeatures\@empty
95 \def\babelFSfeatures{%
96   \let\bbl@ori@fontspec\fontspec
97   \renewcommand\fontspec[1][]{%
98     \bbl@ori@fontspec[\bbl@langfeatures##1]}
99   \let\babelFSfeatures\bbl@FSfeatures
100   \babelFSfeatures}
101 \def\bbl@FSfeatures#1#2{%
102   \expandafter\addto\csname extras#1\endcsname{%
103     \babel@save\bbl@langfeatures
104     \edef\bbl@langfeatures{#2,}}}
105 %<</Font selection>>
106 %    \end{macrocode}
107 %    \section{Hooks for XeTeX and LuaTeX}
109 %    \subsection{XeTeX}
111 %    Unfortunately, the current encoding cannot be retrieved and
112 %    therefore it is reset always to |utf8|, which seems a sensible
113 %    default.
115 %    \LaTeX{} sets many ``codes'' just before loading
116 %    \verb|hyphen.cfg|. That is not a problem in luatex, but in xetex
117 %    they must be reset to the proper value. Most of the work is done in
118 %    \textsf{xe(la)tex.ini}, so here we just ``undo'' some of the
119 %    changes done by \LaTeX. Anyway, for consistency Lua\TeX{} also
120 %    resets the catcodes. 
121 % \changes{bbunicode~1.0c}{2014/03/10}{Reset ``codes'' set by \cs{LaTeX}
122 %    to what xetex expects. Used also in luatex.}
123 %    \begin{macrocode}
124 %<<*Restore Unicode catcodes before loading patterns>>
125 \AddBabelHook{xetex}{loadkernel}{%
126   \begingroup
127       % Reset chars "80-"C0 to category "other", no case mapping:
128     \catcode`\@=11 \count@=128
129     \loop\ifnum\count@<192
130       \global\uccode\count@=0 \global\lccode\count@=0
131       \global\catcode\count@=12 \global\sfcode\count@=1000
132       \advance\count@ by 1 \repeat
133       % Other:
134     \def\O ##1 {%
135       \global\uccode"##1=0 \global\lccode"##1=0
136       \global\catcode"##1=12 \global\sfcode"##1=1000 }%
137       % Letter:
138     \def\L ##1 ##2 ##3 {\global\catcode"##1=11
139       \global\uccode"##1="##2
140       \global\lccode"##1="##3
141       % Uppercase letters have sfcode=999:
142       \ifnum"##1="##3 \else \global\sfcode"##1=999 \fi }%
143       % Letter without case mappings:
144     \def\l ##1 {\L ##1 ##1 ##1 }%
145     \l 00AA
146     \L 00B5 039C 00B5
147     \l 00BA
148     \O 00D7
149     \l 00DF
150     \O 00F7
151     \L 00FF 0178 00FF
152   \endgroup
153   \input #1\relax}
154 %<</Restore Unicode catcodes before loading patterns>>
155 %    \end{macrocode}
157 % Now, the code.
159 %    \begin{macrocode}
160 %<*xetex>
161 \def\BabelStringsDefault{unicode}
162 \let\xebbl@stop\relax
163 \AddBabelHook{xetex}{encodedcommands}{%
164   \def\bbl@tempa{#1}%
165   \ifx\bbl@tempa\@empty
166     \XeTeXinputencoding"bytes"%
167   \else
168       \XeTeXinputencoding"#1"%
169   \fi
170   \def\xebbl@stop{\XeTeXinputencoding"utf8"}}
171 \AddBabelHook{xetex}{stopcommands}{%
172   \xebbl@stop
173   \let\xebbl@stop\relax}
174 <@Restore Unicode catcodes before loading patterns@>
175 <@Font selection@>
176 %</xetex>
177 %    \end{macrocode}
178 %    \subsection{LuaTeX}
180 %    This part relies on the |lua| stripts in \pkg{luatex-hyphen} by Khaled
181 %    Hosny, \'{E}lie Roux, and Manuel
182 %    P\'{e}gouri\'{e}-Gonnard. \'{E}lie also improved the code below.
183 % \changes{bbunicode~1.0b}{2013/04/22}{luatex-hyphen is loaded
184 %    with require. Changes supplied by \'{E}lie Roux.}
185 % \changes{bbunicode~1.0c}{2014/03/10}{Defined hook for
186 %   `initiateactive', to fetch the next token and continue only if
187 %   letter or other}
188 % \changes{bbunicode~1.0d}{2014/03/21}{Removed the `misfeature' for
189 %   `initiateactive'}
190 % \changes{bbunicode~1.0e}{2015/05/10}{Use brackets instead of
191 %   \cs{luaescapestring}}
192 % \changes{bbunicode~1.0e}{2015/07/26}{Added function addpatterns and
193 %   modified the patterns hook.}
195 %    \begin{macrocode}
196 %<*luatex>
197 \begingroup
198 \catcode`\%=12
199 \catcode`\'=12
200 \catcode`\"=12
201 \catcode`\:=12
202 \directlua{
203   require("luatex-hyphen")
204   Babel = {}
205   function Babel.bytes(line)
206     return line:gsub("(.)",
207       function (chr) return unicode.utf8.char(string.byte(chr)) end)
208   end
209   function Babel.begin_process_input()
210     if luatexbase and luatexbase.add_to_callback then
211       luatexbase.add_to_callback('process_input_buffer',
212                                  Babel.bytes,'Babel.bytes')
213     else
214       Babel.callback = callback.find('process_input_buffer')
215       callback.register('process_input_buffer',Babel.bytes)
216     end
217   end
218   function Babel.end_process_input ()
219     if luatexbase and luatexbase.remove_from_callback then
220       luatexbase.remove_from_callback('process_input_buffer','Babel.bytes')
221     else
222       callback.register('process_input_buffer',Babel.callback)
223     end
224   end
225   function Babel.addpatterns(pp, lg)
226     local lg = lang.new(lg)
227     local pats = lang.patterns(lg) or ''
228     lang.clear_patterns(lg)
229     for p in pp:gmatch('[^%s]+') do
230       ss = ''
231       for i in string.utfcharacters(p:gsub('%d', '')) do
232          ss = ss .. '%d?' .. i
233       end
234       ss = ss:gsub('^%%d%?%.', '%%.') .. '%d?'
235       ss = ss:gsub('%.%%d%?$', '%%.')
236       pats, n = pats:gsub('%s' .. ss .. '%s', ' ' .. p .. ' ')
237       if n == 0 then
238         tex.sprint(
239           [[\string\csname\space bbl@info\endcsname{New pattern: ]]
240           .. p .. [[}]])
241         pats = pats .. ' ' .. p
242       else
243         tex.sprint(
244           [[\string\csname\space bbl@info\endcsname{Renew pattern: ]]
245           .. p .. [[}]])
246       end
247     end
248     lang.patterns(lg, pats)
249   end
251 \endgroup
252 \def\BabelStringsDefault{unicode}
253 \let\luabbl@stop\relax
254 \AddBabelHook{luatex}{encodedcommands}{%
255   \def\bbl@tempa{utf8}\def\bbl@tempb{#1}%
256   \ifx\bbl@tempa\bbl@tempb\else
257     \directlua{Babel.begin_process_input()}%
258     \def\luabbl@stop{%
259       \directlua{Babel.end_process_input()}}%
260   \fi}%
261 \AddBabelHook{luatex}{stopcommands}{%
262   \luabbl@stop
263   \let\luabbl@stop\relax}
264 \AddBabelHook{luatex}{patterns}{%
265   \ifcsname lu@texhyphen@loaded@\the\language\endcsname \else
266     \global\@namedef{lu@texhyphen@loaded@\the\language}{}%
267     \directlua{
268       luatexhyphen.loadlanguage([[\string#1]],
269         '\the\language')}%
270   \fi
271   \@ifundefined{bbl@patterns@}{}{%
272     \begingroup
273       \@expandtwoargs\in@{,\number\language,}{,\bbl@pttnlist}%
274       \ifin@\else
275         \ifx\bbl@patterns@\@empty\else
276            \directlua{ Babel.addpatterns(
277              [[\bbl@patterns@]], \number\language) }%
278         \fi
279         \@ifundefined{bbl@patterns@#1}%
280           \@empty
281           {\directlua{ Babel.addpatterns(
282                [[\space\csname bbl@patterns@#1\endcsname]],
283                \number\language) }}%
284         \xdef\bbl@pttnlist{\bbl@pttnlist\number\language,}%
285       \fi
286     \endgroup}}
287 \AddBabelHook{luatex}{adddialect}{%
288   \ifx\directlua\relax\else
289     \directlua{
290       luatexhyphen.adddialect([[\string#1]], 
291         [[\string#2]])
292     }%
293   \fi}
294 \AddBabelHook{luatex}{everylanguage}{%
295   \directlua{
296     processnow = (tex.language == 0) or
297       (luatexhyphen.lookupname([[\string#1]]) == nil)}%
298   \ifnum0=\directlua{tex.sprint(processnow and "0" or "1")}\relax
299     \global\@namedef{lu@texhyphen@loaded@\the\language}{}%
300   \fi}
301 \AddBabelHook{luatex}{loadpatterns}{%
302   \ifnum0=\directlua{tex.sprint(processnow and "0" or "1")}\relax
303     \input #1\relax
304   \fi}
305 \AddBabelHook{luatex}{loadexceptions}{%
306   \ifnum0=\directlua{tex.sprint(processnow and "0" or "1")}\relax
307     \input #1\relax
308   \fi
309   \directlua{processnow = nil}}
310 %    \end{macrocode}
312 %  \begin{macro}{\babelpatterns}
314 %    This macro adds patterns. Two macros are used to store them:
315 %    |\bbl@patterns@| for the global ones and |\bbl@patterns<lang>|
316 %    for language ones. We make sure there is a space between words
317 %    when multiple commands are used.
318 % \changes{bbunicode~1.0e}{2015/07/26}{Macro \cs{babelpatterns} added}
320 %    \begin{macrocode}
321 \@onlypreamble\babelpatterns
322 \AtEndOfPackage{%
323   \newcommand\babelpatterns[2][\@empty]{%
324     \ifx\bbl@patterns@\relax
325       \let\bbl@patterns@\@empty
326     \fi
327     \ifx\bbl@pttnlist\@empty\else
328       \bbl@warning{%
329         You must not intermingle \string\selectlanguage\space and\\%
330         \string\babelpatterns\space or some patterns will not\\%
331         be taken into account. Reported}%
332     \fi
333     \ifx\@empty#1%
334       \protected@edef\bbl@patterns@{\bbl@patterns@\space#2}%
335     \else
336       \edef\bbl@tempb{\zap@space#1 \@empty}%
337       \bbl@for\bbl@tempa\bbl@tempb{%
338         \bbl@fixname\bbl@tempa
339         \bbl@iflanguage\bbl@tempa{%
340           \bbl@csarg\protected@edef{patterns@\bbl@tempa}{%
341             \@ifundefined{bbl@patterns@\bbl@tempa}%
342               \@empty
343               {\csname bbl@patterns@\bbl@tempa\endcsname\space}%
344             #2}}}%
345     \fi}}
346 %    \end{macrocode}
347 %  \end{macro}
349 % Common stuff.
351 %    \begin{macrocode}
352 <@Restore Unicode catcodes before loading patterns@>
353 <@Font selection@>
354 %</luatex>
355 %    \end{macrocode}
357 \endinput
359 %% \CharacterTable
360 %%  {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
361 %%   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
362 %%   Digits        \0\1\2\3\4\5\6\7\8\9
363 %%   Exclamation   \!     Double quote  \"     Hash (number) \#
364 %%   Dollar        \$     Percent       \%     Ampersand     \&
365 %%   Acute accent  \'     Left paren    \(     Right paren   \)
366 %%   Asterisk      \*     Plus          \+     Comma         \,
367 %%   Minus         \-     Point         \.     Solidus       \/
368 %%   Colon         \:     Semicolon     \;     Less than     \<
369 %%   Equals        \=     Greater than  \>     Question mark \?
370 %%   Commercial at \@     Left bracket  \[     Backslash     \\
371 %%   Right bracket \]     Circumflex    \^     Underscore    \_
372 %%   Grave accent  \`     Left brace    \{     Vertical bar  \|
373 %%   Right brace   \}     Tilde         \~}