OSDN Git Service

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