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;
|
|
48 Lisp_Object Qtranslate_table;
|
|
49
|
|
50 static void compute_trt_inverse (Lisp_Object trt, Lisp_Object inverse);
|
|
51
|
|
52 #define STRING256_P(obj) \
|
|
53 (STRINGP (obj) && string_char_length (XSTRING (obj)) == 256)
|
|
54
|
20
|
55 DEFUN ("case-table-p", Fcase_table_p, 1, 1, 0, /*
|
0
|
56 Return t iff ARG is a case table.
|
|
57 See `set-case-table' for more information on these data structures.
|
20
|
58 */
|
|
59 (table))
|
0
|
60 {
|
|
61 Lisp_Object down, up, canon, eqv;
|
|
62 down = Fcar_safe (table);
|
|
63 up = Fcar_safe (Fcdr_safe (table));
|
|
64 canon = Fcar_safe (Fcdr_safe (Fcdr_safe (table)));
|
|
65 eqv = Fcar_safe (Fcdr_safe (Fcdr_safe (Fcdr_safe (table))));
|
|
66
|
|
67 return (STRING256_P (down)
|
|
68 && (NILP (up) || STRING256_P (up))
|
|
69 && ((NILP (canon) && NILP (eqv))
|
|
70 || (STRING256_P (canon)
|
|
71 && (NILP (eqv) || STRING256_P (eqv))))
|
|
72 ? Qt : Qnil);
|
|
73 }
|
|
74
|
|
75 static Lisp_Object
|
|
76 check_case_table (Lisp_Object obj)
|
|
77 {
|
|
78 REGISTER Lisp_Object tem;
|
|
79
|
|
80 while (tem = Fcase_table_p (obj), NILP (tem))
|
|
81 obj = wrong_type_argument (Qcase_table_p, obj);
|
|
82 return (obj);
|
|
83 }
|
|
84
|
20
|
85 DEFUN ("current-case-table", Fcurrent_case_table, 0, 1, 0, /*
|
0
|
86 Return the case table of BUFFER, which defaults to the current buffer.
|
20
|
87 */
|
|
88 (buffer))
|
0
|
89 {
|
|
90 Lisp_Object down, up, canon, eqv;
|
|
91 struct buffer *buf = decode_buffer (buffer, 0);
|
|
92
|
|
93 down = buf->downcase_table;
|
|
94 up = buf->upcase_table;
|
|
95 canon = buf->case_canon_table;
|
|
96 eqv = buf->case_eqv_table;
|
|
97
|
|
98 return Fcons (down, Fcons (up, Fcons (canon, Fcons (eqv, Qnil))));
|
|
99 }
|
|
100
|
20
|
101 DEFUN ("standard-case-table", Fstandard_case_table, 0, 0, 0, /*
|
0
|
102 Return the standard case table.
|
|
103 This is the one used for new buffers.
|
20
|
104 */
|
|
105 ())
|
0
|
106 {
|
|
107 return Fcons (Vascii_downcase_table,
|
|
108 Fcons (Vascii_upcase_table,
|
|
109 Fcons (Vascii_canon_table,
|
|
110 Fcons (Vascii_eqv_table,
|
|
111 Qnil))));
|
|
112 }
|
|
113
|
|
114 static Lisp_Object set_case_table (Lisp_Object table, int standard);
|
|
115
|
|
116
|
20
|
117 DEFUN ("set-case-table", Fset_case_table, 1, 1, 0, /*
|
0
|
118 Select a new case table for the current buffer.
|
|
119 A case table is a list (DOWNCASE UPCASE CANONICALIZE EQUIVALENCES)
|
|
120 where each element is either nil or a string of length 256.
|
|
121 DOWNCASE maps each character to its lower-case equivalent.
|
|
122 UPCASE maps each character to its upper-case equivalent;
|
|
123 if lower and upper case characters are in 1-1 correspondence,
|
|
124 you may use nil and the upcase table will be deduced from DOWNCASE.
|
|
125 CANONICALIZE maps each character to a canonical equivalent;
|
|
126 any two characters that are related by case-conversion have the same
|
|
127 canonical equivalent character; it may be nil, in which case it is
|
|
128 deduced from DOWNCASE and UPCASE.
|
|
129 EQUIVALENCES is a map that cyclicly permutes each equivalence class
|
|
130 (of characters with the same canonical equivalent); it may be nil,
|
|
131 in which case it is deduced from CANONICALIZE.
|
20
|
132 */
|
|
133 (table))
|
0
|
134 {
|
|
135 return set_case_table (table, 0);
|
|
136 }
|
|
137
|
20
|
138 DEFUN ("set-standard-case-table", Fset_standard_case_table, 1, 1, 0, /*
|
0
|
139 Select a new standard case table for new buffers.
|
|
140 See `set-case-table' for more info on case tables.
|
20
|
141 */
|
|
142 (table))
|
0
|
143 {
|
|
144 return set_case_table (table, 1);
|
|
145 }
|
|
146
|
|
147 static Lisp_Object
|
|
148 set_case_table (Lisp_Object table, int standard)
|
|
149 {
|
|
150 Lisp_Object down, up, canon, eqv;
|
|
151 struct buffer *buf = current_buffer;
|
|
152
|
|
153 check_case_table (table);
|
|
154
|
|
155 down = Fcar_safe (table);
|
|
156 up = Fcar_safe (Fcdr_safe (table));
|
|
157 canon = Fcar_safe (Fcdr_safe (Fcdr_safe (table)));
|
|
158 eqv = Fcar_safe (Fcdr_safe (Fcdr_safe (Fcdr_safe (table))));
|
|
159
|
|
160 if (NILP (up))
|
|
161 {
|
|
162 up = MAKE_TRT_TABLE ();
|
|
163 compute_trt_inverse (down, up);
|
|
164 }
|
|
165
|
|
166 if (NILP (canon))
|
|
167 {
|
|
168 REGISTER Charcount i;
|
|
169
|
|
170 canon = MAKE_TRT_TABLE ();
|
|
171
|
|
172 /* Set up the CANON vector; for each character,
|
|
173 this sequence of upcasing and downcasing ought to
|
|
174 get the "preferred" lowercase equivalent. */
|
|
175 for (i = 0; i < 256; i++)
|
|
176 SET_TRT_TABLE_CHAR_1 (canon, i,
|
|
177 TRT_TABLE_CHAR_1
|
|
178 (down,
|
|
179 TRT_TABLE_CHAR_1
|
|
180 (up,
|
|
181 TRT_TABLE_CHAR_1 (down, i))));
|
|
182 }
|
|
183
|
|
184 if (NILP (eqv))
|
|
185 {
|
|
186 eqv = MAKE_TRT_TABLE ();
|
|
187
|
|
188 compute_trt_inverse (canon, eqv);
|
|
189 }
|
|
190
|
|
191 if (standard)
|
|
192 {
|
|
193 Vascii_downcase_table = down;
|
|
194 Vascii_upcase_table = up;
|
|
195 Vascii_canon_table = canon;
|
|
196 Vascii_eqv_table = eqv;
|
|
197 }
|
|
198 else
|
|
199 {
|
|
200 buf->downcase_table = down;
|
|
201 buf->upcase_table = up;
|
|
202 buf->case_canon_table = canon;
|
|
203 buf->case_eqv_table = eqv;
|
|
204 }
|
|
205 return table;
|
|
206 }
|
|
207
|
|
208 /* Given a translate table TRT, store the inverse mapping into INVERSE.
|
|
209 Since TRT is not one-to-one, INVERSE is not a simple mapping.
|
|
210 Instead, it divides the space of characters into equivalence classes.
|
|
211 All characters in a given class form one circular list, chained through
|
|
212 the elements of INVERSE. */
|
|
213
|
|
214 static void
|
|
215 compute_trt_inverse (Lisp_Object trt, Lisp_Object inverse)
|
|
216 {
|
|
217 Charcount i = 0400;
|
|
218 Emchar c, q;
|
|
219
|
|
220 while (--i)
|
|
221 SET_TRT_TABLE_CHAR_1 (inverse, i, (Emchar) i);
|
|
222 i = 0400;
|
|
223 while (--i)
|
|
224 {
|
|
225 if ((q = TRT_TABLE_CHAR_1 (trt, i)) != (Emchar) i)
|
|
226 {
|
|
227 c = TRT_TABLE_CHAR_1 (inverse, q);
|
|
228 SET_TRT_TABLE_CHAR_1 (inverse, q, (Emchar) i);
|
|
229 SET_TRT_TABLE_CHAR_1 (inverse, i, c);
|
|
230 }
|
|
231 }
|
|
232 }
|
|
233
|
|
234
|
|
235 void
|
|
236 syms_of_casetab (void)
|
|
237 {
|
|
238 defsymbol (&Qcase_table_p, "case-table-p");
|
|
239 defsymbol (&Qtranslate_table, "translate-table");
|
|
240
|
20
|
241 DEFSUBR (Fcase_table_p);
|
|
242 DEFSUBR (Fcurrent_case_table);
|
|
243 DEFSUBR (Fstandard_case_table);
|
|
244 DEFSUBR (Fset_case_table);
|
|
245 DEFSUBR (Fset_standard_case_table);
|
0
|
246 }
|
|
247
|
|
248 void
|
|
249 complex_vars_of_casetab (void)
|
|
250 {
|
|
251 REGISTER Emchar i;
|
|
252 Lisp_Object tem;
|
|
253
|
|
254 staticpro (&Vascii_downcase_table);
|
|
255 staticpro (&Vascii_upcase_table);
|
|
256 staticpro (&Vascii_canon_table);
|
|
257 staticpro (&Vascii_eqv_table);
|
|
258
|
|
259 tem = MAKE_TRT_TABLE ();
|
|
260 Vascii_downcase_table = tem;
|
|
261 Vascii_canon_table = tem;
|
|
262
|
|
263 /* Under Mule, can't do set_string_char() until Vcharset_control_1
|
|
264 and Vcharset_ascii are initialized. */
|
|
265 for (i = 0; i < 256; i++)
|
|
266 {
|
|
267 unsigned char lowered = tolower (i);
|
|
268
|
|
269 SET_TRT_TABLE_CHAR_1 (tem, i, lowered);
|
|
270 }
|
|
271
|
|
272 tem = MAKE_TRT_TABLE ();
|
|
273 Vascii_upcase_table = tem;
|
|
274 Vascii_eqv_table = tem;
|
|
275
|
|
276 for (i = 0; i < 256; i++)
|
|
277 {
|
|
278 unsigned char flipped = (isupper (i) ? tolower (i)
|
|
279 : (islower (i) ? toupper (i) : i));
|
|
280
|
|
281 SET_TRT_TABLE_CHAR_1 (tem, i, flipped);
|
|
282 }
|
|
283
|
|
284 }
|