OSDN Git Service

ef4d2db2a1c26b3a5f06b50fec3c2417a2099f25
[luatex-ja/luatexja.git] / src / ltj-cctbreg.sty
1 %
2 % ltj-cctbreg.sty
3 %
4
5 %! luatexbase と luatex パッケージのカテゴリコードテーブルの割当に
6 %! 関する衝突を回避する.
7 %! この衝突に関して「外部のパッチ」で解決しようとするのは, あまり
8 %! 筋が良くなく, できるなら避けたいが, 今のままだと LuaTeX-ja を
9 %! LaTeX + fontspec で用いた時に確実に影響が出る. また衝突を残した
10 %! ままだとカテゴリコードテーブルが実質的に使えない訳で, これも
11 %! 残念なことである.
12 %! 「近いうちに両パッケージの中で解決される」ことを期待しつつ,
13 %! 「衝突がない」という前提で開発を行いたいので, 暫定的処置として,
14 %! このモジュールを用意する.
15
16 %! これは外から読まれない
17 %% Avoid multiple loading.
18 \csname luatexjacctbregLoaded\endcsname
19 \edef\ltj@cctbreg@AtEnd{%
20 \endlinechar=\the\endlinechar
21 \relax}
22 \endlinechar=-1 %
23 \def\luatexjacctbregLoaded{\endinput}
24
25 \ifltj@in@latex                 %<*LaTeX>
26   \NeedsTeXFormat{LaTeX2e}
27   \ProvidesPackage{ltj-cctbreg}[2012/04/21 v0.2]
28 \fi                             %</LaTeX>
29
30 % LS = Size of storage reserved for luatex-pkg.
31 \chardef\ltj@cctb@lower@stor@size=16384
32
33 % LL = (upper bound of cctb index used by luatexbase.sty and luatex.sty) - 1
34 \chardef\ltj@cctb@limit=32766
35
36 %-------------------
37
38 \newcount\ltj@@cctb@failure
39
40 \ltj@@cctb@failure=0
41 \def\ltj@@cctb@check@failure{
42   \ifnum\ltj@@cctb@failure>0 %
43     \@PackageWarningNoLine{ltj-cctbreg}
44       {Cannot apply the patch (\the\ltj@@cctb@failure)}
45     \ltj@cctbreg@AtEnd
46   \expandafter\endinput\fi\relax
47 }
48
49 \chardef\ltj@@cctb@lower@stor@max=
50   \numexpr\ltj@cctb@lower@stor@size-1\relax
51 \chardef\ltj@@cctb@lower@stor@safemax=
52   \numexpr\ltj@cctb@lower@stor@size-8\relax
53
54 \chardef\ltj@@tempcctb@a=\ltj@cctb@lower@stor@size
55 \chardef\ltj@@tempcctb@b=\numexpr\ltj@cctb@lower@stor@size+1\relax
56 \def\ltj@@copy@cctb#1#2{%
57   % Note: The current cctb number is supposed to be zero.
58   \luatexcatcodetable#1\relax
59   \luatexsavecatcodetable#2\relax
60   \luatexcatcodetable0\relax
61 }
62
63 %-------------------
64
65 % The luatexbase-cctb package stores its preset settings to
66 % registers 1--6, and new user allocations go to number 7, 8, ....
67
68 \ifdefined\lltxb@catcodetable@alloc
69   \ifnum\lltxb@catcodetable@alloc>\ltj@@cctb@lower@stor@safemax
70     % Failure: luatexbase-cctb alloc pointer is too high!
71     \ltj@@cctb@failure=2 %
72   \fi
73 \else
74   % Failure: luatexbase-cctb not loaded!
75   \ltj@@cctb@failure=1 %
76 \fi
77
78 \begingroup
79 \endlinechar=13
80 \gdef\ltj@@cctb@org@def#1{%
81   \ifnum\lltxb@catcodetable@alloc<65535\relax
82     \global\advance\lltxb@catcodetable@alloc\tw@
83     \allocationnumber\lltxb@catcodetable@alloc
84     \global\chardef#1\allocationnumber
85     \luatexinitcatcodetable\allocationnumber
86     \begingroup\escapechar\m@ne
87     \luatexbase@directlua{luatexbase.catcodetabledef_from_tex(
88       '\luatexluaescapestring{\string#1}', '\number\allocationnumber')}%
89     \endgroup
90     \wlog{\string#1=\string\luatexcatcodetable\the\allocationnumber}%
91   \else
92     \errmessage{No room for a new \string\luatexcatcodetable}%
93   \fi}
94 \endgroup
95
96 \unless\ifx\ltj@@cctb@org@def\newluatexcatcodetable
97   % Failure: luatexbase-cctb is not of expected version!
98 %%% \bebo <- the meaning of this control sequence?
99   \ltj@@cctb@failure=3 %
100 \fi
101
102 \ifdefined\LuT@AllocAttribute
103   \ifnum\LuT@AllocAttribute>\ltj@@cctb@lower@stor@safemax
104     % Failure: luatex-pkg alloc pointer is too high!
105     \ltj@@cctb@failure=4 %
106   \fi
107 \fi
108 \ifdefined\CatcodeTableStack
109   \ifnum\CatcodeTableStack>\ltj@@cctb@lower@stor@safemax
110     % Failure: luatex-pkg cctb-stack pointer is too high!
111     \ltj@@cctb@failure=5 %
112   \fi
113 \fi
114
115 \ltj@@cctb@check@failure
116
117 \ltx@firstofone{
118   \newluatexcatcodetable\cctb@seven
119   \catcode12345=14 %
120   \luatexsavecatcodetable\cctb@seven
121   \catcode12345=12 %
122 }
123
124 %% Save the register 7 (to a temporary register)
125 \unless\ifnum\lltxb@catcodetable@alloc<7 %
126   \ltj@@copy@cctb{7}{\ltj@@tempcctb@a}
127 \fi
128
129 %-------------------
130
131 \begingroup
132 \endlinechar=13
133 \catcode`\_=12
134 \gdef\newluatexcatcodetable#1{%
135   \ifnum\lltxb@catcodetable@alloc<\ltj@cctb@limit\relax
136     \global\advance\lltxb@catcodetable@alloc\tw@
137     \allocationnumber\lltxb@catcodetable@alloc
138     \global\chardef#1\allocationnumber
139     \luatexinitcatcodetable\allocationnumber
140     \begingroup\escapechar\m@ne
141     \luatexbase@directlua{luatexbase.catcodetabledef_from_tex(
142       '\luatexluaescapestring{\string#1}', '\number\allocationnumber')}%
143     \endgroup
144     \wlog{\string#1=\string\luatexcatcodetable\the\allocationnumber}%
145   \else
146     \errmessage{No room for a new \string\luatexcatcodetable}%
147   \fi}
148 \endgroup
149
150 %-------------------
151
152 % The luatex package stores its preset settings to registers 1, 3, 5, 7
153 % and new user allocations go to odd number 9, 11, 13, etc.; registers
154 % with even number are reserved for the "category code table stack".
155
156 % I relocate all the future allocation by luatexbase-cctb to the "higher"
157 % area (number >= \ltj@cctb@lower@stor@size), which is simply done by
158 % moving the allocation pointer. And all the preset settings of
159 % luatexbase-cctb (those of luatex-pkg is a subset) are prepared again.
160 % The colliding user allocation cannot be helped, but in the fortunate
161 % case where luatex-pkg is not yet loaded or its allocating function
162 % is never invoked, the only endangered user allocation is register 7
163 % of luatexbase-cctb, which is rescued by simply reassining the table,
164 % since the all presets are migrated to the higher area.
165
166 \unless\ifdefined\LuT@AllocAttribute
167   \ifltj@in@latex
168     \RequirePackage{luatex}[2010/03/01]
169   \else
170     \input luatex.sty\relax
171   \fi
172 \fi
173
174 \edef\ltj@@tempa{\meaning\LuT@AllocAttribute}
175 \edef\ltj@@tempb{\meaning\lltxb@catcodetable@alloc}
176 \ifx\ltj@@tempa\ltj@@tempb
177   % Failure: luatexbase-cctb and luatex-pkg seem to be synchronized!
178   \ltj@@cctb@failure=6 %
179 \fi
180 \ltj@@cctb@check@failure
181
182 \unless\ifnum 7=
183  \ifdefined\CatcodeTableLaTeX \CatcodeTableLaTeX
184  \else 0 \fi
185   % Failure: luatex-pkg is not of expected version!!
186   \ltj@@cctb@failure=100 %
187 \fi
188
189 \def\ltj@@cctb@org@def#1{
190   \ifnum\LuT@AllocCatcodeTable<1114110 %
191     \global\advance\LuT@AllocCatcodeTable by\tw@
192     \allocationnumber=\LuT@AllocCatcodeTable
193     \global\chardef#1=\allocationnumber
194     \wlog{
195       \string#1=\string\catcodetable\the\allocationnumber
196     }
197   \else
198     \errmessage{No room for a new \string\catcodetable}
199   \fi
200 }
201 \unless\ifx\ltj@@cctb@org@def\newcatcodetable
202   % Failure: luatex-pkg is not of expected version!!
203   \ltj@@cctb@failure=100 %
204 \fi
205
206 \def\ltj@@cctb@org@def{%
207   \ifnum\CatcodeTableStack<65534\relax
208     \global\advance\CatcodeTableStack\tw@
209   \else
210     \errmessage{CatcodeTableStack overflow}%
211   \fi}
212 \unless\ifx\ltj@@cctb@org@def\IncCatcodeTableStack
213   % Failure: luatex-pkg is not of expected version!!
214   \ltj@@cctb@failure=100 %
215 \fi
216 \ltj@@cctb@check@failure
217
218 %% Restore the register 7 which is overwritten by luatex-pkg.
219 \unless\ifnum\lltxb@catcodetable@alloc<7 %
220   \ltj@@copy@cctb{\ltj@@tempcctb@a}{7}
221 \fi
222
223 %% Move the pointer of luatexbase-cctb to higher area.
224 \lltxb@catcodetable@alloc=\ltj@cctb@lower@stor@size
225
226 %% Restrict the allocation by luatex-pkg to lower area.
227 \chardef\ltj@@cctb@lower@stor@check=
228  \numexpr\ltj@cctb@lower@stor@size-2\relax
229 \def\newcatcodetable#1{
230   \ifnum\LuT@AllocCatcodeTable<\ltj@@cctb@lower@stor@check
231     \global\advance\LuT@AllocCatcodeTable by\tw@
232     \allocationnumber=\LuT@AllocCatcodeTable
233     \global\chardef#1=\allocationnumber
234     \wlog{
235       \string#1=\string\catcodetable\the\allocationnumber
236     }
237   \else
238     \errmessage{No room for a new \string\catcodetable}
239   \fi
240 }
241 \def\IncCatcodeTableStack{%
242   \ifnum\CatcodeTableStack<\ltj@@cctb@lower@stor@check
243     \global\advance\CatcodeTableStack by\tw@
244   \else
245     \@PackageError{luatex}{%
246       Catcode table stack overflow%
247     }\@ehd
248   \fi
249 }
250
251 %------------------- Re-create the presets
252
253 \newluatexcatcodetable\CatcodeTableIniTeX
254 \newluatexcatcodetable\CatcodeTableString
255 \setluatexcatcodetable\CatcodeTableString{%
256   \luatexcatcodetable\CatcodeTableIniTeX
257   \catcode0   12 % nul
258   \catcode13  12 % carriage return
259   \catcode37  12 % percent
260   \SetCatcodeRange{65}{90}{12}% A-Z
261   \SetCatcodeRange{97}{122}{12}% a-z
262   \catcode92  12 % backslash
263   \catcode127 12 }
264 \newluatexcatcodetable\CatcodeTableOther
265 \setluatexcatcodetable\CatcodeTableOther{%
266   \luatexcatcodetable\CatcodeTableString
267   \catcode32 12 }
268 \newluatexcatcodetable\CatcodeTableLaTeX
269 \setluatexcatcodetable\CatcodeTableLaTeX{%
270   \luatexcatcodetable\CatcodeTableIniTeX
271   \SetCatcodeRange{0}{31}{15}%
272   \catcode9   10 % tab
273   \catcode12  13 % form feed
274   \catcode13  5  % carriage return
275   \catcode35  6  % hash
276   \catcode36  3  % dollar
277   \catcode38  4  % ampersand
278   \catcode94  7  % circumflex
279   \catcode95  8  % underscore
280   \catcode123 1  % brace left
281   \catcode125 2  % brace right
282   \catcode126 13 % tilde
283   \catcode127 15 }
284 \newluatexcatcodetable\CatcodeTableLaTeXAtLetter
285 \setluatexcatcodetable\CatcodeTableLaTeXAtLetter{%
286   \luatexcatcodetable\CatcodeTableLaTeX
287   \catcode64=11 }
288 \newluatexcatcodetable\CatcodeTableExpl
289 \setluatexcatcodetable\CatcodeTableExpl{%
290   \luatexcatcodetable\CatcodeTableLaTeX
291   \catcode126 10 % tilde is a space char
292   \catcode32  9  % space is ignored
293   \catcode9   9  % tab also ignored
294   \catcode95  11 % underscore letter
295   \catcode58  11 % colon letter
296   }
297 \luatexbase@directlua{luatexbase.catcodetable_do_shortcuts()}
298
299 %%------------------ all done
300 \ltj@cctbreg@AtEnd
301 \endinput
302 %% EOF