OSDN Git Service

Revert 1b1d3c3 and 3b2bba1.
[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 \catcode`\_=12
81 \gdef\ltj@@cctb@org@def#1{%
82   \ifnum\lltxb@catcodetable@alloc<65535\relax
83     \global\advance\lltxb@catcodetable@alloc\tw@
84     \allocationnumber\lltxb@catcodetable@alloc
85     \global\chardef#1\allocationnumber
86     \luatexinitcatcodetable\allocationnumber
87     \begingroup\escapechar\m@ne
88     \luatexbase@directlua{luatexbase.catcodetabledef_from_tex(
89       '\luatexluaescapestring{\string#1}', '\number\allocationnumber')}%
90     \endgroup
91     \wlog{\string#1=\string\luatexcatcodetable\the\allocationnumber}%
92   \else
93     \errmessage{No room for a new \string\luatexcatcodetable}%
94   \fi}
95 \endgroup
96
97 \unless\ifx\ltj@@cctb@org@def\newluatexcatcodetable
98   % Failure: luatexbase-cctb is not of expected version!
99 %%% \bebo <- the meaning of this control sequence?
100   \ltj@@cctb@failure=3 %
101 \fi
102
103 \ifdefined\LuT@AllocAttribute
104   \ifnum\LuT@AllocAttribute>\ltj@@cctb@lower@stor@safemax
105     % Failure: luatex-pkg alloc pointer is too high!
106     \ltj@@cctb@failure=4 %
107   \fi
108 \fi
109 \ifdefined\CatcodeTableStack
110   \ifnum\CatcodeTableStack>\ltj@@cctb@lower@stor@safemax
111     % Failure: luatex-pkg cctb-stack pointer is too high!
112     \ltj@@cctb@failure=5 %
113   \fi
114 \fi
115
116 \ltj@@cctb@check@failure
117
118 \ltx@firstofone{
119   \newluatexcatcodetable\cctb@seven
120   \catcode12345=14 %
121   \luatexsavecatcodetable\cctb@seven
122   \catcode12345=12 %
123 }
124
125 %% Save the register 7 (to a temporary register)
126 \unless\ifnum\lltxb@catcodetable@alloc<7 %
127   \ltj@@copy@cctb{7}{\ltj@@tempcctb@a}
128 \fi
129
130 %-------------------
131
132 \begingroup
133 \endlinechar=13
134 \catcode`\_=12
135 \gdef\newluatexcatcodetable#1{%
136   \ifnum\lltxb@catcodetable@alloc<\ltj@cctb@limit\relax
137     \global\advance\lltxb@catcodetable@alloc\tw@
138     \allocationnumber\lltxb@catcodetable@alloc
139     \global\chardef#1\allocationnumber
140     \luatexinitcatcodetable\allocationnumber
141     \begingroup\escapechar\m@ne
142     \luatexbase@directlua{luatexbase.catcodetabledef_from_tex(
143       '\luatexluaescapestring{\string#1}', '\number\allocationnumber')}%
144     \endgroup
145     \wlog{\string#1=\string\luatexcatcodetable\the\allocationnumber}%
146   \else
147     \errmessage{No room for a new \string\luatexcatcodetable}%
148   \fi}
149 \endgroup
150
151 %-------------------
152
153 % The luatex package stores its preset settings to registers 1, 3, 5, 7
154 % and new user allocations go to odd number 9, 11, 13, etc.; registers
155 % with even number are reserved for the "category code table stack".
156
157 % I relocate all the future allocation by luatexbase-cctb to the "higher"
158 % area (number >= \ltj@cctb@lower@stor@size), which is simply done by
159 % moving the allocation pointer. And all the preset settings of
160 % luatexbase-cctb (those of luatex-pkg is a subset) are prepared again.
161 % The colliding user allocation cannot be helped, but in the fortunate
162 % case where luatex-pkg is not yet loaded or its allocating function
163 % is never invoked, the only endangered user allocation is register 7
164 % of luatexbase-cctb, which is rescued by simply reassining the table,
165 % since the all presets are migrated to the higher area.
166
167 \unless\ifdefined\LuT@AllocAttribute
168   \ifltj@in@latex
169     \RequirePackage{luatex}[2010/03/01]
170   \else
171     \input luatex.sty\relax
172   \fi
173 \fi
174
175 \edef\ltj@@tempa{\meaning\LuT@AllocAttribute}
176 \edef\ltj@@tempb{\meaning\lltxb@catcodetable@alloc}
177 \ifx\ltj@@tempa\ltj@@tempb
178   % Failure: luatexbase-cctb and luatex-pkg seem to be synchronized!
179   \ltj@@cctb@failure=6 %
180 \fi
181 \ltj@@cctb@check@failure
182
183 \unless\ifnum 7=
184  \ifdefined\CatcodeTableLaTeX \CatcodeTableLaTeX
185  \else 0 \fi
186   % Failure: luatex-pkg is not of expected version!!
187   \ltj@@cctb@failure=100 %
188 \fi
189
190 \def\ltj@@cctb@org@def#1{
191   \ifnum\LuT@AllocCatcodeTable<1114110 %
192     \global\advance\LuT@AllocCatcodeTable by\tw@
193     \allocationnumber=\LuT@AllocCatcodeTable
194     \global\chardef#1=\allocationnumber
195     \wlog{
196       \string#1=\string\catcodetable\the\allocationnumber
197     }
198   \else
199     \errmessage{No room for a new \string\catcodetable}
200   \fi
201 }
202 \unless\ifx\ltj@@cctb@org@def\newcatcodetable
203   % Failure: luatex-pkg is not of expected version!!
204   \ltj@@cctb@failure=100 %
205 \fi
206
207 \def\ltj@@cctb@org@def{%
208   \ifnum\CatcodeTableStack<268435454 %
209     \global\advance\CatcodeTableStack by\tw@
210   \else
211     \@PackageError{luatex}{%
212       Catcode table stack overflow%
213     }\@ehd
214   \fi
215 }
216 \unless\ifx\ltj@@cctb@org@def\IncCatcodeTableStack
217   % Failure: luatex-pkg is not of expected version!!
218   \ltj@@cctb@failure=100 %
219 \fi
220 \ltj@@cctb@check@failure
221
222 %% Restore the register 7 which is overwritten by luatex-pkg.
223 \unless\ifnum\lltxb@catcodetable@alloc<7 %
224   \ltj@@copy@cctb{\ltj@@tempcctb@a}{7}
225 \fi
226
227 %% Move the pointer of luatexbase-cctb to higher area.
228 \lltxb@catcodetable@alloc=\ltj@cctb@lower@stor@size
229
230 %% Restrict the allocation by luatex-pkg to lower area.
231 \chardef\ltj@@cctb@lower@stor@check=
232  \numexpr\ltj@cctb@lower@stor@size-2\relax
233 \def\newcatcodetable#1{
234   \ifnum\LuT@AllocCatcodeTable<\ltj@@cctb@lower@stor@check
235     \global\advance\LuT@AllocCatcodeTable by\tw@
236     \allocationnumber=\LuT@AllocCatcodeTable
237     \global\chardef#1=\allocationnumber
238     \wlog{
239       \string#1=\string\catcodetable\the\allocationnumber
240     }
241   \else
242     \errmessage{No room for a new \string\catcodetable}
243   \fi
244 }
245 \def\IncCatcodeTableStack{%
246   \ifnum\CatcodeTableStack<\ltj@@cctb@lower@stor@check
247     \global\advance\CatcodeTableStack by\tw@
248   \else
249     \@PackageError{luatex}{%
250       Catcode table stack overflow%
251     }\@ehd
252   \fi
253 }
254
255 %------------------- Re-create the presets
256
257 \newluatexcatcodetable\CatcodeTableIniTeX
258 \newluatexcatcodetable\CatcodeTableString
259 \setluatexcatcodetable\CatcodeTableString{%
260   \luatexcatcodetable\CatcodeTableIniTeX
261   \catcode0   12 % nul
262   \catcode13  12 % carriage return
263   \catcode37  12 % percent
264   \setcatcoderange{65}{90}{12}% A-Z
265   \setcatcoderange{97}{122}{12}% a-z
266   \catcode92  12 % backslash
267   \catcode127 12 }
268 \newluatexcatcodetable\CatcodeTableOther
269 \setluatexcatcodetable\CatcodeTableOther{%
270   \luatexcatcodetable\CatcodeTableString
271   \catcode32 12 }
272 \newluatexcatcodetable\CatcodeTableLaTeX
273 \setluatexcatcodetable\CatcodeTableLaTeX{%
274   \luatexcatcodetable\CatcodeTableIniTeX
275   \setcatcoderange{0}{31}{15}%
276   \catcode9   10 % tab
277   \catcode12  13 % form feed
278   \catcode13  5  % carriage return
279   \catcode35  6  % hash
280   \catcode36  3  % dollar
281   \catcode38  4  % ampersand
282   \catcode94  7  % circumflex
283   \catcode95  8  % underscore
284   \catcode123 1  % brace left
285   \catcode125 2  % brace right
286   \catcode126 13 % tilde
287   \catcode127 15 }
288 \newluatexcatcodetable\CatcodeTableLaTeXAtLetter
289 \setluatexcatcodetable\CatcodeTableLaTeXAtLetter{%
290   \luatexcatcodetable\CatcodeTableLaTeX
291   \catcode64=11 }
292 \newluatexcatcodetable\CatcodeTableExpl
293 \setluatexcatcodetable\CatcodeTableExpl{%
294   \luatexcatcodetable\CatcodeTableLaTeX
295   \catcode126 10 % tilde is a space char
296   \catcode32  9  % space is ignored
297   \catcode9   9  % tab also ignored
298   \catcode95  11 % underscore letter
299   \catcode58  11 % colon letter
300   }
301 \luatexbase@directlua{luatexbase.catcodetable_do_shortcuts()}
302
303 %%------------------ all done
304 \ltj@cctbreg@AtEnd
305 \endinput
306 %% EOF