428
|
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.
|
793
|
4 Copyright (C) 2002 Ben Wing.
|
428
|
5
|
|
6 This file is part of XEmacs.
|
|
7
|
|
8 XEmacs is free software; you can redistribute it and/or modify it
|
|
9 under the terms of the GNU General Public License as published by the
|
|
10 Free Software Foundation; either version 2, or (at your option) any
|
|
11 later version.
|
|
12
|
|
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
16 for more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License
|
|
19 along with XEmacs; see the file COPYING. If not, write to
|
|
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
21 Boston, MA 02111-1307, USA. */
|
|
22
|
771
|
23 /* Synched up with: FSF 19.28. Between FSF 19.28 and 19.30, casetab.c
|
428
|
24 was rewritten to use junky FSF char tables. Meanwhile I rewrote it
|
771
|
25 to use more logical char tables. --ben */
|
428
|
26
|
|
27 /* Written by Howard Gayle. See some mythical and not-in-the-Emacs-
|
|
28 distribution file chartab.c for details. */
|
|
29
|
|
30 /* Modified for Mule by Ben Wing. */
|
|
31
|
771
|
32 /* #### Someone (Yoshiki?) wrote the following comment, which I don't
|
|
33 understand.
|
|
34
|
|
35 Case tables consist of four char-tables. These are for downcase,
|
446
|
36 upcase, canonical and equivalent respectively.
|
|
37
|
771
|
38 The entries are like this:
|
446
|
39
|
|
40 downcase: a -> a, A -> a.
|
|
41 upcase: a -> A, A -> a. (The latter is for NOCASEP.)
|
|
42 canon: a -> a, A -> a.
|
|
43 eqv: a -> A, A -> a.
|
|
44 */
|
428
|
45
|
|
46 #include <config.h>
|
|
47 #include "lisp.h"
|
|
48 #include "buffer.h"
|
|
49 #include "opaque.h"
|
446
|
50 #include "chartab.h"
|
|
51 #include "casetab.h"
|
428
|
52
|
446
|
53 Lisp_Object Qcase_tablep, Qdowncase, Qupcase;
|
|
54 Lisp_Object Vstandard_case_table;
|
428
|
55
|
|
56 static void compute_trt_inverse (Lisp_Object trt, Lisp_Object inverse);
|
446
|
57 Lisp_Object case_table_char (Lisp_Object ch, Lisp_Object table);
|
428
|
58
|
446
|
59 #define STRING256_P(obj) ((STRINGP (obj) && XSTRING_CHAR_LENGTH (obj) == 256))
|
|
60
|
|
61 static Lisp_Object
|
|
62 mark_case_table (Lisp_Object obj)
|
|
63 {
|
|
64 Lisp_Case_Table *ct = XCASE_TABLE (obj);
|
|
65
|
|
66 mark_object (CASE_TABLE_DOWNCASE (ct));
|
|
67 mark_object (CASE_TABLE_UPCASE (ct));
|
|
68 mark_object (CASE_TABLE_CANON (ct));
|
|
69 mark_object (CASE_TABLE_EQV (ct));
|
|
70 return Qnil;
|
|
71 }
|
|
72
|
|
73 static void
|
|
74 print_case_table (Lisp_Object obj, Lisp_Object printcharfun, int escapeflag)
|
|
75 {
|
|
76 Lisp_Case_Table *ct = XCASE_TABLE (obj);
|
|
77 if (print_readably)
|
563
|
78 printing_unreadable_object ("#<case-table 0x%x", ct->header.uid);
|
800
|
79 write_fmt_string (printcharfun, "#<case-table 0x%x>", ct->header.uid);
|
446
|
80 }
|
|
81
|
|
82 static const struct lrecord_description case_table_description [] = {
|
|
83 { XD_LISP_OBJECT, offsetof (Lisp_Case_Table, downcase_table) },
|
|
84 { XD_LISP_OBJECT, offsetof (Lisp_Case_Table, upcase_table) },
|
|
85 { XD_LISP_OBJECT, offsetof (Lisp_Case_Table, case_canon_table) },
|
|
86 { XD_LISP_OBJECT, offsetof (Lisp_Case_Table, case_eqv_table) },
|
|
87 { XD_END }
|
|
88 };
|
|
89
|
|
90 DEFINE_LRECORD_IMPLEMENTATION ("case-table", case_table,
|
|
91 mark_case_table, print_case_table, 0,
|
|
92 0, 0, case_table_description, Lisp_Case_Table);
|
|
93
|
|
94 static Lisp_Object
|
|
95 allocate_case_table (void)
|
|
96 {
|
|
97 Lisp_Case_Table *ct =
|
|
98 alloc_lcrecord_type (Lisp_Case_Table, &lrecord_case_table);
|
|
99
|
|
100 SET_CASE_TABLE_DOWNCASE (ct, Qnil);
|
|
101 SET_CASE_TABLE_UPCASE (ct, Qnil);
|
|
102 SET_CASE_TABLE_CANON (ct, Qnil);
|
|
103 SET_CASE_TABLE_EQV (ct, Qnil);
|
|
104
|
793
|
105 return wrap_case_table (ct);
|
446
|
106 }
|
428
|
107
|
|
108 DEFUN ("case-table-p", Fcase_table_p, 1, 1, 0, /*
|
444
|
109 Return t if OBJECT is a case table.
|
428
|
110 See `set-case-table' for more information on these data structures.
|
|
111 */
|
444
|
112 (object))
|
428
|
113 {
|
446
|
114 if (CASE_TABLEP (object))
|
|
115 return Qt;
|
|
116 else
|
|
117 {
|
|
118 Lisp_Object down, up, canon, eqv;
|
|
119 if (!CONSP (object))
|
|
120 return Qnil;
|
|
121 down = XCAR (object); object = XCDR (object);
|
|
122 if (!CONSP (object))
|
|
123 return Qnil;
|
|
124 up = XCAR (object); object = XCDR (object);
|
|
125 if (!CONSP (object))
|
|
126 return Qnil;
|
|
127 canon = XCAR (object); object = XCDR (object);
|
|
128 if (!CONSP (object))
|
|
129 return Qnil;
|
|
130 eqv = XCAR (object);
|
428
|
131
|
446
|
132 return ((STRING256_P (down)
|
|
133 && (NILP (up) || STRING256_P (up))
|
|
134 && ((NILP (canon) && NILP (eqv))
|
|
135 || STRING256_P (canon))
|
|
136 && (NILP (eqv) || STRING256_P (eqv)))
|
|
137 ? Qt : Qnil);
|
|
138
|
|
139 }
|
428
|
140 }
|
|
141
|
|
142 static Lisp_Object
|
444
|
143 check_case_table (Lisp_Object object)
|
428
|
144 {
|
446
|
145 /* This function can GC */
|
444
|
146 while (NILP (Fcase_table_p (object)))
|
|
147 object = wrong_type_argument (Qcase_tablep, object);
|
|
148 return object;
|
428
|
149 }
|
|
150
|
446
|
151 Lisp_Object
|
|
152 case_table_char (Lisp_Object ch, Lisp_Object table)
|
|
153 {
|
|
154 Lisp_Object ct_char;
|
|
155 ct_char = get_char_table (XCHAR (ch), XCHAR_TABLE (table));
|
|
156 if (NILP (ct_char))
|
|
157 return ch;
|
|
158 else
|
|
159 return ct_char;
|
|
160 }
|
|
161
|
|
162 DEFUN ("get-case-table", Fget_case_table, 3, 3, 0, /*
|
|
163 Return CHAR-CASE version of CHARACTER in CASE-TABLE.
|
|
164
|
|
165 CHAR-CASE is either downcase or upcase.
|
|
166 */
|
|
167 (char_case, character, case_table))
|
|
168 {
|
|
169 CHECK_CHAR (character);
|
|
170 CHECK_CASE_TABLE (case_table);
|
|
171 if (EQ (char_case, Qdowncase))
|
|
172 return case_table_char (character, XCASE_TABLE_DOWNCASE (case_table));
|
|
173 else if (EQ (char_case, Qupcase))
|
|
174 return case_table_char (character, XCASE_TABLE_UPCASE (case_table));
|
|
175 else
|
563
|
176 invalid_constant ("Char case must be downcase or upcase", char_case);
|
446
|
177
|
|
178 return Qnil; /* Not reached. */
|
|
179 }
|
|
180
|
|
181 DEFUN ("put-case-table", Fput_case_table, 4, 4, 0, /*
|
|
182 Set CHAR-CASE version of CHARACTER to be VALUE in CASE-TABLE.
|
|
183
|
|
184 CHAR-CASE is either downcase or upcase.
|
|
185 See also `put-case-table-pair'.
|
|
186 */
|
|
187 (char_case, character, value, case_table))
|
|
188 {
|
|
189 CHECK_CHAR (character);
|
|
190 CHECK_CHAR (value);
|
|
191
|
|
192 if (EQ (char_case, Qdowncase))
|
|
193 {
|
|
194 Fput_char_table (character, value, XCASE_TABLE_DOWNCASE (case_table));
|
|
195 /* This one is not at all intuitive. */
|
|
196 Fput_char_table (character, value, XCASE_TABLE_UPCASE (case_table));
|
|
197 Fput_char_table (character, value, XCASE_TABLE_CANON (case_table));
|
|
198 Fput_char_table (value, value, XCASE_TABLE_CANON (case_table));
|
|
199 Fput_char_table (value, character, XCASE_TABLE_EQV (case_table));
|
|
200 Fput_char_table (character, value, XCASE_TABLE_EQV (case_table));
|
|
201 }
|
|
202 else if (EQ (char_case, Qupcase))
|
|
203 {
|
|
204 Fput_char_table (character, value, XCASE_TABLE_UPCASE (case_table));
|
|
205 Fput_char_table (character, character, XCASE_TABLE_DOWNCASE (case_table));
|
|
206 Fput_char_table (character, character, XCASE_TABLE_CANON (case_table));
|
|
207 Fput_char_table (value, character, XCASE_TABLE_CANON (case_table));
|
|
208 Fput_char_table (value, character, XCASE_TABLE_EQV (case_table));
|
|
209 Fput_char_table (character, value, XCASE_TABLE_EQV (case_table));
|
|
210 }
|
|
211 else
|
563
|
212 invalid_constant ("Char case must be downcase or upcase", char_case);
|
446
|
213
|
|
214 return Qnil;
|
|
215 }
|
|
216
|
|
217 DEFUN ("put-case-table-pair", Fput_case_table_pair, 3, 3, 0, /*
|
|
218 Make UC and LC a pair of inter-case-converting letters in CASE-TABLE.
|
|
219 UC is an uppercase character and LC is a downcase character.
|
|
220 */
|
|
221 (uc, lc, case_table))
|
|
222 {
|
|
223 CHECK_CHAR (uc);
|
|
224 CHECK_CHAR (lc);
|
|
225 CHECK_CASE_TABLE (case_table);
|
|
226
|
|
227 Fput_char_table (lc, lc, XCASE_TABLE_DOWNCASE (case_table));
|
|
228 Fput_char_table (uc, lc, XCASE_TABLE_UPCASE (case_table));
|
|
229 Fput_char_table (uc, lc, XCASE_TABLE_DOWNCASE (case_table));
|
|
230 Fput_char_table (lc, uc, XCASE_TABLE_UPCASE (case_table));
|
|
231
|
|
232 Fput_char_table (lc, lc, XCASE_TABLE_CANON (case_table));
|
|
233 Fput_char_table (uc, lc, XCASE_TABLE_CANON (case_table));
|
|
234 Fput_char_table (uc, lc, XCASE_TABLE_EQV (case_table));
|
|
235 Fput_char_table (lc, uc, XCASE_TABLE_EQV (case_table));
|
|
236 return Qnil;
|
|
237 }
|
|
238
|
|
239 DEFUN ("copy-case-table", Fcopy_case_table, 1, 1, 0, /*
|
|
240 Return a new case table which is a copy of CASE-TABLE
|
|
241 */
|
|
242 (case_table))
|
|
243 {
|
|
244 Lisp_Object new_obj;
|
|
245 CHECK_CASE_TABLE (case_table);
|
|
246
|
|
247 new_obj = allocate_case_table ();
|
|
248 XSET_CASE_TABLE_DOWNCASE
|
|
249 (new_obj, Fcopy_char_table (XCASE_TABLE_DOWNCASE (case_table)));
|
|
250 XSET_CASE_TABLE_UPCASE
|
|
251 (new_obj, Fcopy_char_table (XCASE_TABLE_UPCASE (case_table)));
|
|
252 XSET_CASE_TABLE_CANON
|
|
253 (new_obj, Fcopy_char_table (XCASE_TABLE_CANON (case_table)));
|
|
254 XSET_CASE_TABLE_EQV
|
|
255 (new_obj, Fcopy_char_table (XCASE_TABLE_EQV (case_table)));
|
|
256 return new_obj;
|
|
257 }
|
|
258
|
428
|
259 DEFUN ("current-case-table", Fcurrent_case_table, 0, 1, 0, /*
|
|
260 Return the case table of BUFFER, which defaults to the current buffer.
|
|
261 */
|
|
262 (buffer))
|
|
263 {
|
|
264 struct buffer *buf = decode_buffer (buffer, 0);
|
|
265
|
446
|
266 return buf->case_table;
|
428
|
267 }
|
|
268
|
|
269 DEFUN ("standard-case-table", Fstandard_case_table, 0, 0, 0, /*
|
|
270 Return the standard case table.
|
|
271 This is the one used for new buffers.
|
|
272 */
|
|
273 ())
|
|
274 {
|
446
|
275 return Vstandard_case_table;
|
428
|
276 }
|
|
277
|
|
278 static Lisp_Object set_case_table (Lisp_Object table, int standard);
|
|
279
|
|
280 DEFUN ("set-case-table", Fset_case_table, 1, 1, 0, /*
|
444
|
281 Select CASE-TABLE as the new case table for the current buffer.
|
446
|
282 A case table is a case-table object or list
|
|
283 (DOWNCASE UPCASE CANONICALIZE EQUIVALENCES)
|
428
|
284 where each element is either nil or a string of length 256.
|
446
|
285 The latter is provided for backward-compatibility.
|
428
|
286 DOWNCASE maps each character to its lower-case equivalent.
|
|
287 UPCASE maps each character to its upper-case equivalent;
|
|
288 if lower and upper case characters are in 1-1 correspondence,
|
|
289 you may use nil and the upcase table will be deduced from DOWNCASE.
|
|
290 CANONICALIZE maps each character to a canonical equivalent;
|
|
291 any two characters that are related by case-conversion have the same
|
|
292 canonical equivalent character; it may be nil, in which case it is
|
|
293 deduced from DOWNCASE and UPCASE.
|
|
294 EQUIVALENCES is a map that cyclicly permutes each equivalence class
|
|
295 (of characters with the same canonical equivalent); it may be nil,
|
|
296 in which case it is deduced from CANONICALIZE.
|
|
297
|
446
|
298 See also `get-case-table', `put-case-table' and `put-case-table-pair'.
|
428
|
299 */
|
444
|
300 (case_table))
|
428
|
301 {
|
446
|
302 /* This function can GC */
|
444
|
303 return set_case_table (case_table, 0);
|
428
|
304 }
|
|
305
|
|
306 DEFUN ("set-standard-case-table", Fset_standard_case_table, 1, 1, 0, /*
|
444
|
307 Select CASE-TABLE as the new standard case table for new buffers.
|
428
|
308 See `set-case-table' for more info on case tables.
|
|
309 */
|
444
|
310 (case_table))
|
428
|
311 {
|
446
|
312 /* This function can GC */
|
444
|
313 return set_case_table (case_table, 1);
|
428
|
314 }
|
|
315
|
|
316 static Lisp_Object
|
|
317 set_case_table (Lisp_Object table, int standard)
|
|
318 {
|
446
|
319 /* This function can GC */
|
442
|
320 struct buffer *buf =
|
|
321 standard ? XBUFFER(Vbuffer_defaults) : current_buffer;
|
428
|
322
|
|
323 check_case_table (table);
|
|
324
|
446
|
325 if (CASE_TABLEP (table))
|
|
326 {
|
|
327 if (standard)
|
|
328 Vstandard_case_table = table;
|
428
|
329
|
446
|
330 buf->case_table = table;
|
428
|
331 }
|
446
|
332 else
|
428
|
333 {
|
446
|
334 /* For backward compatibility. */
|
|
335 Lisp_Object down, up, canon, eqv, tail = table;
|
|
336 Lisp_Object temp;
|
|
337 int i;
|
|
338
|
|
339 down = XCAR (tail); tail = XCDR (tail);
|
|
340 up = XCAR (tail); tail = XCDR (tail);
|
|
341 canon = XCAR (tail); tail = XCDR (tail);
|
|
342 eqv = XCAR (tail);
|
|
343
|
|
344 temp = down;
|
|
345 down = MAKE_TRT_TABLE ();
|
|
346 for (i = 0; i < 256; i++)
|
793
|
347 SET_TRT_TABLE_CHAR_1 (down, i, XSTRING_CHAR (temp, i));
|
428
|
348
|
446
|
349 if (NILP (up))
|
|
350 {
|
|
351 up = MAKE_TRT_TABLE ();
|
|
352 compute_trt_inverse (down, up);
|
|
353 }
|
|
354 else
|
|
355 {
|
|
356 temp = up;
|
|
357 up = MAKE_TRT_TABLE ();
|
|
358 for (i = 0; i < 256; i++)
|
793
|
359 SET_TRT_TABLE_CHAR_1 (up, i, XSTRING_CHAR (temp, i));
|
446
|
360 }
|
|
361 if (NILP (canon))
|
|
362 {
|
|
363 canon = MAKE_TRT_TABLE ();
|
428
|
364
|
446
|
365 /* Set up the CANON table; for each character,
|
|
366 this sequence of upcasing and downcasing ought to
|
|
367 get the "preferred" lowercase equivalent. */
|
|
368 for (i = 0; i < 256; i++)
|
|
369 SET_TRT_TABLE_CHAR_1 (canon, i,
|
|
370 TRT_TABLE_CHAR_1
|
|
371 (down,
|
|
372 TRT_TABLE_CHAR_1
|
|
373 (up,
|
|
374 TRT_TABLE_CHAR_1 (down, i))));
|
|
375 }
|
|
376 else
|
|
377 {
|
|
378 temp = canon;
|
|
379 canon = MAKE_TRT_TABLE ();
|
|
380 for (i = 0; i < 256; i++)
|
793
|
381 SET_TRT_TABLE_CHAR_1 (canon, i, XSTRING_CHAR (temp, i));
|
446
|
382 }
|
|
383
|
|
384 if (NILP (eqv))
|
|
385 {
|
|
386 eqv = MAKE_TRT_TABLE ();
|
|
387 compute_trt_inverse (canon, eqv);
|
|
388 }
|
|
389 else
|
|
390 {
|
|
391 temp = eqv;
|
|
392 eqv = MAKE_TRT_TABLE ();
|
|
393 for (i = 0; i < 256; i++)
|
793
|
394 SET_TRT_TABLE_CHAR_1 (eqv, i, XSTRING_CHAR (temp, i));
|
446
|
395 }
|
|
396
|
|
397 if (standard)
|
|
398 {
|
|
399 XSET_CASE_TABLE_DOWNCASE (Vstandard_case_table, down);
|
|
400 XSET_CASE_TABLE_UPCASE (Vstandard_case_table, up);
|
|
401 XSET_CASE_TABLE_CANON (Vstandard_case_table, canon);
|
|
402 XSET_CASE_TABLE_EQV (Vstandard_case_table, eqv);
|
|
403 }
|
|
404
|
|
405 buf->case_table = allocate_case_table ();
|
|
406 XSET_CASE_TABLE_DOWNCASE (buf->case_table, down);
|
|
407 XSET_CASE_TABLE_UPCASE (buf->case_table, up);
|
|
408 XSET_CASE_TABLE_CANON (buf->case_table, canon);
|
|
409 XSET_CASE_TABLE_EQV (buf->case_table, eqv);
|
428
|
410 }
|
|
411
|
446
|
412 return buf->case_table;
|
428
|
413 }
|
|
414
|
|
415 /* Given a translate table TRT, store the inverse mapping into INVERSE.
|
|
416 Since TRT is not one-to-one, INVERSE is not a simple mapping.
|
|
417 Instead, it divides the space of characters into equivalence classes.
|
|
418 All characters in a given class form one circular list, chained through
|
|
419 the elements of INVERSE. */
|
|
420
|
|
421 static void
|
|
422 compute_trt_inverse (Lisp_Object trt, Lisp_Object inverse)
|
|
423 {
|
|
424 Charcount i = 0400;
|
|
425 Emchar c, q;
|
|
426
|
|
427 while (--i)
|
|
428 SET_TRT_TABLE_CHAR_1 (inverse, i, (Emchar) i);
|
|
429 i = 0400;
|
|
430 while (--i)
|
|
431 {
|
|
432 if ((q = TRT_TABLE_CHAR_1 (trt, i)) != (Emchar) i)
|
|
433 {
|
|
434 c = TRT_TABLE_CHAR_1 (inverse, q);
|
|
435 SET_TRT_TABLE_CHAR_1 (inverse, q, (Emchar) i);
|
|
436 SET_TRT_TABLE_CHAR_1 (inverse, i, c);
|
|
437 }
|
|
438 }
|
|
439 }
|
|
440
|
|
441
|
|
442 void
|
|
443 syms_of_casetab (void)
|
|
444 {
|
446
|
445 INIT_LRECORD_IMPLEMENTATION (case_table);
|
|
446
|
563
|
447 DEFSYMBOL_MULTIWORD_PREDICATE (Qcase_tablep);
|
|
448 DEFSYMBOL (Qdowncase);
|
|
449 DEFSYMBOL (Qupcase);
|
428
|
450
|
|
451 DEFSUBR (Fcase_table_p);
|
446
|
452 DEFSUBR (Fget_case_table);
|
|
453 DEFSUBR (Fput_case_table);
|
|
454 DEFSUBR (Fput_case_table_pair);
|
428
|
455 DEFSUBR (Fcurrent_case_table);
|
|
456 DEFSUBR (Fstandard_case_table);
|
446
|
457 DEFSUBR (Fcopy_case_table);
|
428
|
458 DEFSUBR (Fset_case_table);
|
|
459 DEFSUBR (Fset_standard_case_table);
|
|
460 }
|
|
461
|
|
462 void
|
|
463 complex_vars_of_casetab (void)
|
|
464 {
|
|
465 REGISTER Emchar i;
|
|
466 Lisp_Object tem;
|
|
467
|
446
|
468 staticpro (&Vstandard_case_table);
|
428
|
469
|
446
|
470 Vstandard_case_table = allocate_case_table ();
|
428
|
471
|
|
472 tem = MAKE_TRT_TABLE ();
|
446
|
473 XSET_CASE_TABLE_DOWNCASE (Vstandard_case_table, tem);
|
|
474 XSET_CASE_TABLE_CANON (Vstandard_case_table, tem);
|
428
|
475
|
|
476 /* Under Mule, can't do set_string_char() until Vcharset_control_1
|
|
477 and Vcharset_ascii are initialized. */
|
|
478 for (i = 0; i < 256; i++)
|
|
479 {
|
|
480 unsigned char lowered = tolower (i);
|
|
481
|
|
482 SET_TRT_TABLE_CHAR_1 (tem, i, lowered);
|
|
483 }
|
|
484
|
|
485 tem = MAKE_TRT_TABLE ();
|
446
|
486 XSET_CASE_TABLE_UPCASE (Vstandard_case_table, tem);
|
|
487 XSET_CASE_TABLE_EQV (Vstandard_case_table, tem);
|
428
|
488
|
|
489 for (i = 0; i < 256; i++)
|
|
490 {
|
|
491 unsigned char flipped = (isupper (i) ? tolower (i)
|
|
492 : (islower (i) ? toupper (i) : i));
|
|
493
|
|
494 SET_TRT_TABLE_CHAR_1 (tem, i, flipped);
|
|
495 }
|
|
496 }
|