0
|
1 /* XEmacs routines to deal with case tables.
|
|
2 Copyright (C) 1987, 1992, 1993, 1994 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995 Sun Microsystems, Inc.
|
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: FSF 19.28. Between FSF 19.28 and 19.30, casetab.c
|
|
23 was rewritten to use junky RMS char tables. Meanwhile I rewrote it
|
|
24 to use more logical char tables. RMS also discards the "list of four
|
|
25 tables" format and instead stuffs the other tables as "extra slots"
|
|
26 in the downcase table. I've kept the four-lists format for now. */
|
|
27
|
|
28 /* Written by Howard Gayle. See some mythical and not-in-the-Emacs-
|
|
29 distribution file chartab.c for details. */
|
|
30
|
|
31 /* Modified for Mule by Ben Wing. */
|
|
32
|
|
33 /* #### We do not currently deal properly with translating non-ASCII
|
|
34 (including Latin-1!) characters under Mule. Getting this right is
|
|
35 *hard*, way fucking hard. So we at least preserve consistency by
|
|
36 sanitizing all the case tables to remove translations that would
|
|
37 get us into trouble and possibly result in inconsistent internal
|
|
38 text, which would likely lead to crashes. */
|
|
39
|
|
40 #include <config.h>
|
|
41 #include "lisp.h"
|
|
42 #include "buffer.h"
|
|
43 #include "opaque.h"
|
|
44
|
|
45 Lisp_Object Qcase_table_p;
|
|
46 Lisp_Object Vascii_downcase_table, Vascii_upcase_table;
|
|
47 Lisp_Object Vascii_canon_table, Vascii_eqv_table;
|
70
|
48 #ifdef MULE
|
|
49 Lisp_Object Vmirror_ascii_downcase_table, Vmirror_ascii_upcase_table;
|
|
50 Lisp_Object Vmirror_ascii_canon_table, Vmirror_ascii_eqv_table;
|
|
51 #endif
|
0
|
52 Lisp_Object Qtranslate_table;
|
|
53
|
|
54 static void compute_trt_inverse (Lisp_Object trt, Lisp_Object inverse);
|
|
55
|
|
56 #define STRING256_P(obj) \
|
|
57 (STRINGP (obj) && string_char_length (XSTRING (obj)) == 256)
|
|
58
|
20
|
59 DEFUN ("case-table-p", Fcase_table_p, 1, 1, 0, /*
|
0
|
60 Return t iff ARG is a case table.
|
|
61 See `set-case-table' for more information on these data structures.
|
20
|
62 */
|
|
63 (table))
|
0
|
64 {
|
|
65 Lisp_Object down, up, canon, eqv;
|
|
66 down = Fcar_safe (table);
|
|
67 up = Fcar_safe (Fcdr_safe (table));
|
|
68 canon = Fcar_safe (Fcdr_safe (Fcdr_safe (table)));
|
|
69 eqv = Fcar_safe (Fcdr_safe (Fcdr_safe (Fcdr_safe (table))));
|
|
70
|
|
71 return (STRING256_P (down)
|
|
72 && (NILP (up) || STRING256_P (up))
|
|
73 && ((NILP (canon) && NILP (eqv))
|
|
74 || (STRING256_P (canon)
|
|
75 && (NILP (eqv) || STRING256_P (eqv))))
|
|
76 ? Qt : Qnil);
|
|
77 }
|
|
78
|
|
79 static Lisp_Object
|
|
80 check_case_table (Lisp_Object obj)
|
|
81 {
|
|
82 REGISTER Lisp_Object tem;
|
|
83
|
|
84 while (tem = Fcase_table_p (obj), NILP (tem))
|
|
85 obj = wrong_type_argument (Qcase_table_p, obj);
|
|
86 return (obj);
|
|
87 }
|
|
88
|
20
|
89 DEFUN ("current-case-table", Fcurrent_case_table, 0, 1, 0, /*
|
0
|
90 Return the case table of BUFFER, which defaults to the current buffer.
|
20
|
91 */
|
|
92 (buffer))
|
0
|
93 {
|
|
94 Lisp_Object down, up, canon, eqv;
|
|
95 struct buffer *buf = decode_buffer (buffer, 0);
|
|
96
|
|
97 down = buf->downcase_table;
|
|
98 up = buf->upcase_table;
|
|
99 canon = buf->case_canon_table;
|
|
100 eqv = buf->case_eqv_table;
|
|
101
|
|
102 return Fcons (down, Fcons (up, Fcons (canon, Fcons (eqv, Qnil))));
|
|
103 }
|
|
104
|
20
|
105 DEFUN ("standard-case-table", Fstandard_case_table, 0, 0, 0, /*
|
0
|
106 Return the standard case table.
|
|
107 This is the one used for new buffers.
|
20
|
108 */
|
|
109 ())
|
0
|
110 {
|
|
111 return Fcons (Vascii_downcase_table,
|
|
112 Fcons (Vascii_upcase_table,
|
|
113 Fcons (Vascii_canon_table,
|
|
114 Fcons (Vascii_eqv_table,
|
|
115 Qnil))));
|
|
116 }
|
|
117
|
|
118 static Lisp_Object set_case_table (Lisp_Object table, int standard);
|
|
119
|
|
120
|
20
|
121 DEFUN ("set-case-table", Fset_case_table, 1, 1, 0, /*
|
0
|
122 Select a new case table for the current buffer.
|
|
123 A case table is a list (DOWNCASE UPCASE CANONICALIZE EQUIVALENCES)
|
|
124 where each element is either nil or a string of length 256.
|
|
125 DOWNCASE maps each character to its lower-case equivalent.
|
|
126 UPCASE maps each character to its upper-case equivalent;
|
|
127 if lower and upper case characters are in 1-1 correspondence,
|
|
128 you may use nil and the upcase table will be deduced from DOWNCASE.
|
|
129 CANONICALIZE maps each character to a canonical equivalent;
|
|
130 any two characters that are related by case-conversion have the same
|
|
131 canonical equivalent character; it may be nil, in which case it is
|
|
132 deduced from DOWNCASE and UPCASE.
|
|
133 EQUIVALENCES is a map that cyclicly permutes each equivalence class
|
|
134 (of characters with the same canonical equivalent); it may be nil,
|
|
135 in which case it is deduced from CANONICALIZE.
|
70
|
136
|
|
137 BUG: Under XEmacs/Mule, translations to or from non-ASCII characters
|
|
138 (this includes chars in the range 128 - 255) are ignored by
|
|
139 the string/buffer-searching routines. Thus, `case-fold-search'
|
|
140 will not correctly conflate a-umlaut and A-umlaut even if the
|
|
141 case tables call for this.
|
20
|
142 */
|
|
143 (table))
|
0
|
144 {
|
|
145 return set_case_table (table, 0);
|
|
146 }
|
|
147
|
20
|
148 DEFUN ("set-standard-case-table", Fset_standard_case_table, 1, 1, 0, /*
|
0
|
149 Select a new standard case table for new buffers.
|
|
150 See `set-case-table' for more info on case tables.
|
20
|
151 */
|
|
152 (table))
|
0
|
153 {
|
|
154 return set_case_table (table, 1);
|
|
155 }
|
|
156
|
70
|
157 #ifdef MULE
|
|
158
|
|
159 static Lisp_Object
|
|
160 make_mirror_trt_table (Lisp_Object table)
|
|
161 {
|
|
162 Lisp_Object new_table;
|
|
163
|
|
164 if (!STRING256_P (table))
|
|
165 {
|
|
166 #ifdef DEBUG_XEMACS
|
|
167 /* This should be caught farther up. */
|
|
168 abort ();
|
|
169 #else
|
|
170 signal_simple_error ("Invalid translate table", table);
|
|
171 #endif
|
|
172 }
|
|
173
|
|
174 new_table = MAKE_MIRROR_TRT_TABLE ();
|
|
175 {
|
|
176 int i;
|
|
177
|
|
178 for (i = 0; i < 256; i++)
|
|
179 {
|
|
180 Emchar newval = string_char (XSTRING (table), i);
|
|
181 if ((i >= 128 && newval != i)
|
|
182 || (i < 128 && newval >= 128))
|
|
183 {
|
|
184 newval = (Emchar) i;
|
|
185 }
|
|
186 SET_MIRROR_TRT_TABLE_CHAR_1 (new_table, i, newval);
|
|
187 }
|
|
188 }
|
|
189 return new_table;
|
|
190 }
|
|
191
|
|
192 #endif /* MULE */
|
|
193
|
0
|
194 static Lisp_Object
|
|
195 set_case_table (Lisp_Object table, int standard)
|
|
196 {
|
|
197 Lisp_Object down, up, canon, eqv;
|
|
198 struct buffer *buf = current_buffer;
|
|
199
|
|
200 check_case_table (table);
|
|
201
|
|
202 down = Fcar_safe (table);
|
|
203 up = Fcar_safe (Fcdr_safe (table));
|
|
204 canon = Fcar_safe (Fcdr_safe (Fcdr_safe (table)));
|
|
205 eqv = Fcar_safe (Fcdr_safe (Fcdr_safe (Fcdr_safe (table))));
|
|
206
|
|
207 if (NILP (up))
|
|
208 {
|
|
209 up = MAKE_TRT_TABLE ();
|
|
210 compute_trt_inverse (down, up);
|
|
211 }
|
|
212
|
|
213 if (NILP (canon))
|
|
214 {
|
|
215 REGISTER Charcount i;
|
|
216
|
|
217 canon = MAKE_TRT_TABLE ();
|
|
218
|
|
219 /* Set up the CANON vector; for each character,
|
|
220 this sequence of upcasing and downcasing ought to
|
|
221 get the "preferred" lowercase equivalent. */
|
|
222 for (i = 0; i < 256; i++)
|
|
223 SET_TRT_TABLE_CHAR_1 (canon, i,
|
|
224 TRT_TABLE_CHAR_1
|
|
225 (down,
|
|
226 TRT_TABLE_CHAR_1
|
|
227 (up,
|
|
228 TRT_TABLE_CHAR_1 (down, i))));
|
|
229 }
|
|
230
|
|
231 if (NILP (eqv))
|
|
232 {
|
|
233 eqv = MAKE_TRT_TABLE ();
|
|
234
|
|
235 compute_trt_inverse (canon, eqv);
|
|
236 }
|
|
237
|
|
238 if (standard)
|
|
239 {
|
|
240 Vascii_downcase_table = down;
|
|
241 Vascii_upcase_table = up;
|
|
242 Vascii_canon_table = canon;
|
|
243 Vascii_eqv_table = eqv;
|
70
|
244 #ifdef MULE
|
|
245 Vmirror_ascii_downcase_table = make_mirror_trt_table (down);
|
|
246 Vmirror_ascii_upcase_table = make_mirror_trt_table (up);
|
|
247 Vmirror_ascii_canon_table = make_mirror_trt_table (canon);
|
|
248 Vmirror_ascii_eqv_table = make_mirror_trt_table (eqv);
|
|
249 #endif
|
0
|
250 }
|
|
251 else
|
|
252 {
|
|
253 buf->downcase_table = down;
|
|
254 buf->upcase_table = up;
|
|
255 buf->case_canon_table = canon;
|
|
256 buf->case_eqv_table = eqv;
|
70
|
257 #ifdef MULE
|
|
258 buf->mirror_downcase_table = make_mirror_trt_table (down);
|
|
259 buf->mirror_upcase_table = make_mirror_trt_table (up);
|
|
260 buf->mirror_case_canon_table = make_mirror_trt_table (canon);
|
|
261 buf->mirror_case_eqv_table = make_mirror_trt_table (eqv);
|
|
262 #endif
|
0
|
263 }
|
|
264 return table;
|
|
265 }
|
|
266
|
|
267 /* Given a translate table TRT, store the inverse mapping into INVERSE.
|
|
268 Since TRT is not one-to-one, INVERSE is not a simple mapping.
|
|
269 Instead, it divides the space of characters into equivalence classes.
|
|
270 All characters in a given class form one circular list, chained through
|
|
271 the elements of INVERSE. */
|
|
272
|
|
273 static void
|
|
274 compute_trt_inverse (Lisp_Object trt, Lisp_Object inverse)
|
|
275 {
|
|
276 Charcount i = 0400;
|
|
277 Emchar c, q;
|
|
278
|
|
279 while (--i)
|
|
280 SET_TRT_TABLE_CHAR_1 (inverse, i, (Emchar) i);
|
|
281 i = 0400;
|
|
282 while (--i)
|
|
283 {
|
|
284 if ((q = TRT_TABLE_CHAR_1 (trt, i)) != (Emchar) i)
|
|
285 {
|
|
286 c = TRT_TABLE_CHAR_1 (inverse, q);
|
|
287 SET_TRT_TABLE_CHAR_1 (inverse, q, (Emchar) i);
|
|
288 SET_TRT_TABLE_CHAR_1 (inverse, i, c);
|
|
289 }
|
|
290 }
|
|
291 }
|
|
292
|
|
293
|
|
294 void
|
|
295 syms_of_casetab (void)
|
|
296 {
|
|
297 defsymbol (&Qcase_table_p, "case-table-p");
|
|
298 defsymbol (&Qtranslate_table, "translate-table");
|
|
299
|
20
|
300 DEFSUBR (Fcase_table_p);
|
|
301 DEFSUBR (Fcurrent_case_table);
|
|
302 DEFSUBR (Fstandard_case_table);
|
|
303 DEFSUBR (Fset_case_table);
|
|
304 DEFSUBR (Fset_standard_case_table);
|
0
|
305 }
|
|
306
|
|
307 void
|
|
308 complex_vars_of_casetab (void)
|
|
309 {
|
|
310 REGISTER Emchar i;
|
|
311 Lisp_Object tem;
|
|
312
|
|
313 staticpro (&Vascii_downcase_table);
|
|
314 staticpro (&Vascii_upcase_table);
|
|
315 staticpro (&Vascii_canon_table);
|
|
316 staticpro (&Vascii_eqv_table);
|
|
317
|
|
318 tem = MAKE_TRT_TABLE ();
|
|
319 Vascii_downcase_table = tem;
|
|
320 Vascii_canon_table = tem;
|
|
321
|
|
322 /* Under Mule, can't do set_string_char() until Vcharset_control_1
|
|
323 and Vcharset_ascii are initialized. */
|
|
324 for (i = 0; i < 256; i++)
|
|
325 {
|
|
326 unsigned char lowered = tolower (i);
|
|
327
|
|
328 SET_TRT_TABLE_CHAR_1 (tem, i, lowered);
|
|
329 }
|
70
|
330
|
|
331 #ifdef MULE
|
|
332 tem = make_mirror_trt_table (tem);
|
|
333 Vmirror_ascii_downcase_table = tem;
|
|
334 Vmirror_ascii_canon_table = tem;
|
|
335 #endif
|
0
|
336
|
|
337 tem = MAKE_TRT_TABLE ();
|
|
338 Vascii_upcase_table = tem;
|
|
339 Vascii_eqv_table = tem;
|
|
340
|
|
341 for (i = 0; i < 256; i++)
|
|
342 {
|
|
343 unsigned char flipped = (isupper (i) ? tolower (i)
|
|
344 : (islower (i) ? toupper (i) : i));
|
|
345
|
|
346 SET_TRT_TABLE_CHAR_1 (tem, i, flipped);
|
|
347 }
|
|
348
|
70
|
349 #ifdef MULE
|
|
350 tem = make_mirror_trt_table (tem);
|
|
351 Vmirror_ascii_upcase_table = tem;
|
|
352 Vmirror_ascii_eqv_table = tem;
|
|
353 #endif
|
0
|
354 }
|