1 % \iffalse meta-comment
3 % Copyright 2013-2014 Javier Bezos and any individual authors
4 % listed elsewhere in this file. All rights reserved.
6 % This file is part of the Babel system.
7 % --------------------------------------
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.
17 % This work has the LPPL maintenance status "maintained".
19 % The Current Maintainer of this work is Javier Bezos.
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
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.
33 \ProvidesFile{bbunicode.dtx}
34 [2015/05/10 v1.0e Babel hooks for Unicode engines]
37 %% File `bbunicode.dtx'
38 %% Babel package for LaTeX version 2e
39 %% Copyright (C) 2103-2014
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}}
53 \DocInput{bbunicode.dtx}
58 % \GetFileInfo{bbunicode.dtx}
61 % \section{Tentative font handling}
63 % A general solution is far from trivial:
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'').
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{%
84 \edef#3{\csname bbl@#2default#1\endcsname}%
85 \fontfamily{#3}\selectfont
87 \edef#3{\csname bbl@#2default#1\endcsname}%
89 \expandafter\addto\csname noextras#1\endcsname{%
91 \fontfamily{#4}\selectfont
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
101 \def\bbl@FSfeatures#1#2{%
102 \expandafter\addto\csname extras#1\endcsname{%
103 \babel@save\bbl@langfeatures
104 \edef\bbl@langfeatures{#2,}}}
107 % \section{Hooks for XeTeX and LuaTeX}
111 % Unfortunately, the current encoding cannot be retrieved and
112 % therefore it is reset always to |utf8|, which seems a sensible
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.}
124 %<<*Restore Unicode catcodes before loading patterns>>
125 \AddBabelHook{xetex}{loadkernel}{%
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
135 \global\uccode"##1=0 \global\lccode"##1=0
136 \global\catcode"##1=12 \global\sfcode"##1=1000 }%
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 }%
154 %<</Restore Unicode catcodes before loading patterns>>
161 \def\BabelStringsDefault{unicode}
162 \let\xebbl@stop\relax
163 \AddBabelHook{xetex}{encodedcommands}{%
165 \ifx\bbl@tempa\@empty
166 \XeTeXinputencoding"bytes"%
168 \XeTeXinputencoding"#1"%
170 \def\xebbl@stop{\XeTeXinputencoding"utf8"}}
171 \AddBabelHook{xetex}{stopcommands}{%
173 \let\xebbl@stop\relax}
174 <@Restore Unicode catcodes before loading patterns@>
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
188 % \changes{bbunicode~1.0d}{2014/03/21}{Removed the `misfeature' for
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.}
203 require("luatex-hyphen")
205 function Babel.bytes(line)
206 return line:gsub("(.)",
207 function (chr) return unicode.utf8.char(string.byte(chr)) 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')
214 Babel.callback = callback.find('process_input_buffer')
215 callback.register('process_input_buffer',Babel.bytes)
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')
222 callback.register('process_input_buffer',Babel.callback)
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
231 for i in string.utfcharacters(p:gsub('%d', '')) do
232 ss = ss .. '%d?' .. i
234 ss = ss:gsub('^%%d%?%.', '%%.') .. '%d?'
235 ss = ss:gsub('%.%%d%?$', '%%.')
236 pats, n = pats:gsub('%s' .. ss .. '%s', ' ' .. p .. ' ')
239 [[\string\csname\space bbl@info\endcsname{New pattern: ]]
241 pats = pats .. ' ' .. p
244 [[\string\csname\space bbl@info\endcsname{Renew pattern: ]]
248 lang.patterns(lg, pats)
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()}%
259 \directlua{Babel.end_process_input()}}%
261 \AddBabelHook{luatex}{stopcommands}{%
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}{}%
268 luatexhyphen.loadlanguage([[\string#1]],
271 \@ifundefined{bbl@patterns@}{}{%
273 \@expandtwoargs\in@{,\number\language,}{,\bbl@pttnlist}%
275 \ifx\bbl@patterns@\@empty\else
276 \directlua{ Babel.addpatterns(
277 [[\bbl@patterns@]], \number\language) }%
279 \@ifundefined{bbl@patterns@#1}%
281 {\directlua{ Babel.addpatterns(
282 [[\space\csname bbl@patterns@#1\endcsname]],
283 \number\language) }}%
284 \xdef\bbl@pttnlist{\bbl@pttnlist\number\language,}%
287 \AddBabelHook{luatex}{adddialect}{%
288 \ifx\directlua\relax\else
290 luatexhyphen.adddialect([[\string#1]],
294 \AddBabelHook{luatex}{everylanguage}{%
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}{}%
301 \AddBabelHook{luatex}{loadpatterns}{%
302 \ifnum0=\directlua{tex.sprint(processnow and "0" or "1")}\relax
305 \AddBabelHook{luatex}{loadexceptions}{%
306 \ifnum0=\directlua{tex.sprint(processnow and "0" or "1")}\relax
309 \directlua{processnow = nil}}
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}
321 \@onlypreamble\babelpatterns
323 \newcommand\babelpatterns[2][\@empty]{%
324 \ifx\bbl@patterns@\relax
325 \let\bbl@patterns@\@empty
327 \ifx\bbl@pttnlist\@empty\else
329 You must not intermingle \string\selectlanguage\space and\\%
330 \string\babelpatterns\space or some patterns will not\\%
331 be taken into account. Reported}%
334 \protected@edef\bbl@patterns@{\bbl@patterns@\space#2}%
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}%
343 {\csname bbl@patterns@\bbl@tempa\endcsname\space}%
352 <@Restore Unicode catcodes before loading patterns@>
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 \~}