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