Mercurial > hg > xemacs-beta
annotate src/chartab.c @ 5118:e0db3c197671 ben-lisp-object
merge up to latest default branch, doesn't compile yet
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Sat, 26 Dec 2009 21:18:49 -0600 |
parents | 3742ea8250b5 1d11ecca9cd0 |
children | d1247f3cc363 |
rev | line source |
---|---|
428 | 1 /* XEmacs routines to deal with char tables. |
2 Copyright (C) 1992, 1995 Free Software Foundation, Inc. | |
3 Copyright (C) 1995 Sun Microsystems, Inc. | |
1296 | 4 Copyright (C) 1995, 1996, 2002, 2003 Ben Wing. |
428 | 5 Copyright (C) 1995, 1997, 1999 Electrotechnical Laboratory, JAPAN. |
6 Licensed to the Free Software Foundation. | |
7 | |
8 This file is part of XEmacs. | |
9 | |
10 XEmacs is free software; you can redistribute it and/or modify it | |
11 under the terms of the GNU General Public License as published by the | |
12 Free Software Foundation; either version 2, or (at your option) any | |
13 later version. | |
14 | |
15 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
18 for more details. | |
19 | |
20 You should have received a copy of the GNU General Public License | |
21 along with XEmacs; see the file COPYING. If not, write to | |
22 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 Boston, MA 02111-1307, USA. */ | |
24 | |
25 /* Synched up with: Mule 2.3. Not synched with FSF. | |
26 | |
27 This file was written independently of the FSF implementation, | |
28 and is not compatible. */ | |
29 | |
30 /* Authorship: | |
31 | |
32 Ben Wing: wrote, for 19.13 (Mule). Some category table stuff | |
33 loosely based on the original Mule. | |
34 Jareth Hein: fixed a couple of bugs in the implementation, and | |
35 added regex support for categories with check_category_at | |
36 */ | |
37 | |
38 #include <config.h> | |
39 #include "lisp.h" | |
40 | |
41 #include "buffer.h" | |
42 #include "chartab.h" | |
43 #include "syntax.h" | |
44 | |
45 Lisp_Object Qchar_tablep, Qchar_table; | |
46 | |
47 Lisp_Object Vall_syntax_tables; | |
48 | |
49 #ifdef MULE | |
50 Lisp_Object Qcategory_table_p; | |
51 Lisp_Object Qcategory_designator_p; | |
52 Lisp_Object Qcategory_table_value_p; | |
53 | |
54 Lisp_Object Vstandard_category_table; | |
55 | |
56 /* Variables to determine word boundary. */ | |
57 Lisp_Object Vword_combining_categories, Vword_separating_categories; | |
58 #endif /* MULE */ | |
59 | |
826 | 60 static int check_valid_char_table_value (Lisp_Object value, |
61 enum char_table_type type, | |
62 Error_Behavior errb); | |
63 | |
428 | 64 |
65 /* A char table maps from ranges of characters to values. | |
66 | |
67 Implementing a general data structure that maps from arbitrary | |
68 ranges of numbers to values is tricky to do efficiently. As it | |
69 happens, it should suffice (and is usually more convenient, anyway) | |
70 when dealing with characters to restrict the sorts of ranges that | |
71 can be assigned values, as follows: | |
72 | |
73 1) All characters. | |
74 2) All characters in a charset. | |
75 3) All characters in a particular row of a charset, where a "row" | |
76 means all characters with the same first byte. | |
77 4) A particular character in a charset. | |
78 | |
79 We use char tables to generalize the 256-element vectors now | |
80 littering the Emacs code. | |
81 | |
82 Possible uses (all should be converted at some point): | |
83 | |
84 1) category tables | |
85 2) syntax tables | |
86 3) display tables | |
87 4) case tables | |
88 5) keyboard-translate-table? | |
89 | |
90 We provide an | |
91 abstract type to generalize the Emacs vectors and Mule | |
92 vectors-of-vectors goo. | |
93 */ | |
94 | |
95 /************************************************************************/ | |
96 /* Char Table object */ | |
97 /************************************************************************/ | |
98 | |
99 #ifdef MULE | |
100 | |
101 static Lisp_Object | |
102 mark_char_table_entry (Lisp_Object obj) | |
103 { | |
440 | 104 Lisp_Char_Table_Entry *cte = XCHAR_TABLE_ENTRY (obj); |
428 | 105 int i; |
106 | |
107 for (i = 0; i < 96; i++) | |
108 { | |
109 mark_object (cte->level2[i]); | |
110 } | |
111 return Qnil; | |
112 } | |
113 | |
114 static int | |
115 char_table_entry_equal (Lisp_Object obj1, Lisp_Object obj2, int depth) | |
116 { | |
440 | 117 Lisp_Char_Table_Entry *cte1 = XCHAR_TABLE_ENTRY (obj1); |
118 Lisp_Char_Table_Entry *cte2 = XCHAR_TABLE_ENTRY (obj2); | |
428 | 119 int i; |
120 | |
121 for (i = 0; i < 96; i++) | |
122 if (!internal_equal (cte1->level2[i], cte2->level2[i], depth + 1)) | |
123 return 0; | |
124 | |
125 return 1; | |
126 } | |
127 | |
665 | 128 static Hashcode |
428 | 129 char_table_entry_hash (Lisp_Object obj, int depth) |
130 { | |
440 | 131 Lisp_Char_Table_Entry *cte = XCHAR_TABLE_ENTRY (obj); |
428 | 132 |
826 | 133 return internal_array_hash (cte->level2, 96, depth + 1); |
428 | 134 } |
135 | |
1204 | 136 static const struct memory_description char_table_entry_description[] = { |
440 | 137 { XD_LISP_OBJECT_ARRAY, offsetof (Lisp_Char_Table_Entry, level2), 96 }, |
428 | 138 { XD_END } |
139 }; | |
140 | |
5118
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
141 DEFINE_DUMPABLE_LISP_OBJECT ("char-table-entry", char_table_entry, |
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
142 mark_char_table_entry, 0, |
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
143 0, char_table_entry_equal, |
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
144 char_table_entry_hash, |
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
145 char_table_entry_description, |
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
146 Lisp_Char_Table_Entry); |
934 | 147 |
428 | 148 #endif /* MULE */ |
149 | |
150 static Lisp_Object | |
151 mark_char_table (Lisp_Object obj) | |
152 { | |
440 | 153 Lisp_Char_Table *ct = XCHAR_TABLE (obj); |
428 | 154 int i; |
155 | |
156 for (i = 0; i < NUM_ASCII_CHARS; i++) | |
157 mark_object (ct->ascii[i]); | |
158 #ifdef MULE | |
159 for (i = 0; i < NUM_LEADING_BYTES; i++) | |
160 mark_object (ct->level1[i]); | |
161 #endif | |
793 | 162 mark_object (ct->parent); |
163 mark_object (ct->default_); | |
428 | 164 return ct->mirror_table; |
165 } | |
166 | |
167 /* WARNING: All functions of this nature need to be written extremely | |
168 carefully to avoid crashes during GC. Cf. prune_specifiers() | |
169 and prune_weak_hash_tables(). */ | |
170 | |
171 void | |
172 prune_syntax_tables (void) | |
173 { | |
174 Lisp_Object rest, prev = Qnil; | |
175 | |
176 for (rest = Vall_syntax_tables; | |
177 !NILP (rest); | |
178 rest = XCHAR_TABLE (rest)->next_table) | |
179 { | |
180 if (! marked_p (rest)) | |
181 { | |
182 /* This table is garbage. Remove it from the list. */ | |
183 if (NILP (prev)) | |
184 Vall_syntax_tables = XCHAR_TABLE (rest)->next_table; | |
185 else | |
186 XCHAR_TABLE (prev)->next_table = | |
187 XCHAR_TABLE (rest)->next_table; | |
188 } | |
189 } | |
190 } | |
191 | |
192 static Lisp_Object | |
193 char_table_type_to_symbol (enum char_table_type type) | |
194 { | |
195 switch (type) | |
196 { | |
2500 | 197 default: ABORT(); |
428 | 198 case CHAR_TABLE_TYPE_GENERIC: return Qgeneric; |
199 case CHAR_TABLE_TYPE_SYNTAX: return Qsyntax; | |
200 case CHAR_TABLE_TYPE_DISPLAY: return Qdisplay; | |
201 case CHAR_TABLE_TYPE_CHAR: return Qchar; | |
202 #ifdef MULE | |
203 case CHAR_TABLE_TYPE_CATEGORY: return Qcategory; | |
204 #endif | |
205 } | |
206 } | |
207 | |
208 static enum char_table_type | |
209 symbol_to_char_table_type (Lisp_Object symbol) | |
210 { | |
211 CHECK_SYMBOL (symbol); | |
212 | |
213 if (EQ (symbol, Qgeneric)) return CHAR_TABLE_TYPE_GENERIC; | |
214 if (EQ (symbol, Qsyntax)) return CHAR_TABLE_TYPE_SYNTAX; | |
215 if (EQ (symbol, Qdisplay)) return CHAR_TABLE_TYPE_DISPLAY; | |
216 if (EQ (symbol, Qchar)) return CHAR_TABLE_TYPE_CHAR; | |
217 #ifdef MULE | |
218 if (EQ (symbol, Qcategory)) return CHAR_TABLE_TYPE_CATEGORY; | |
219 #endif | |
220 | |
563 | 221 invalid_constant ("Unrecognized char table type", symbol); |
1204 | 222 RETURN_NOT_REACHED (CHAR_TABLE_TYPE_GENERIC); |
428 | 223 } |
224 | |
225 static void | |
826 | 226 decode_char_table_range (Lisp_Object range, struct chartab_range *outrange) |
428 | 227 { |
826 | 228 if (EQ (range, Qt)) |
229 outrange->type = CHARTAB_RANGE_ALL; | |
230 else if (CHAR_OR_CHAR_INTP (range)) | |
231 { | |
232 outrange->type = CHARTAB_RANGE_CHAR; | |
233 outrange->ch = XCHAR_OR_CHAR_INT (range); | |
234 } | |
235 #ifndef MULE | |
428 | 236 else |
826 | 237 sferror ("Range must be t or a character", range); |
238 #else /* MULE */ | |
239 else if (VECTORP (range)) | |
240 { | |
241 Lisp_Vector *vec = XVECTOR (range); | |
242 Lisp_Object *elts = vector_data (vec); | |
243 if (vector_length (vec) != 2) | |
244 sferror ("Length of charset row vector must be 2", | |
245 range); | |
246 outrange->type = CHARTAB_RANGE_ROW; | |
247 outrange->charset = Fget_charset (elts[0]); | |
248 CHECK_INT (elts[1]); | |
249 outrange->row = XINT (elts[1]); | |
250 switch (XCHARSET_TYPE (outrange->charset)) | |
251 { | |
252 case CHARSET_TYPE_94: | |
253 case CHARSET_TYPE_96: | |
254 sferror ("Charset in row vector must be multi-byte", | |
255 outrange->charset); | |
256 case CHARSET_TYPE_94X94: | |
257 check_int_range (outrange->row, 33, 126); | |
258 break; | |
259 case CHARSET_TYPE_96X96: | |
260 check_int_range (outrange->row, 32, 127); | |
261 break; | |
262 default: | |
2500 | 263 ABORT (); |
826 | 264 } |
265 } | |
266 else | |
267 { | |
268 if (!CHARSETP (range) && !SYMBOLP (range)) | |
269 sferror | |
270 ("Char table range must be t, charset, char, or vector", range); | |
271 outrange->type = CHARTAB_RANGE_CHARSET; | |
272 outrange->charset = Fget_charset (range); | |
273 } | |
274 #endif /* MULE */ | |
428 | 275 } |
276 | |
826 | 277 static Lisp_Object |
278 encode_char_table_range (struct chartab_range *range) | |
428 | 279 { |
826 | 280 switch (range->type) |
428 | 281 { |
826 | 282 case CHARTAB_RANGE_ALL: |
283 return Qt; | |
284 | |
285 #ifdef MULE | |
286 case CHARTAB_RANGE_CHARSET: | |
287 return XCHARSET_NAME (Fget_charset (range->charset)); | |
428 | 288 |
826 | 289 case CHARTAB_RANGE_ROW: |
290 return vector2 (XCHARSET_NAME (Fget_charset (range->charset)), | |
291 make_int (range->row)); | |
292 #endif | |
293 case CHARTAB_RANGE_CHAR: | |
294 return make_char (range->ch); | |
295 default: | |
2500 | 296 ABORT (); |
428 | 297 } |
826 | 298 return Qnil; /* not reached */ |
428 | 299 } |
300 | |
826 | 301 struct ptemap |
428 | 302 { |
826 | 303 Lisp_Object printcharfun; |
304 int first; | |
305 }; | |
428 | 306 |
826 | 307 static int |
2286 | 308 print_table_entry (struct chartab_range *range, Lisp_Object UNUSED (table), |
826 | 309 Lisp_Object val, void *arg) |
310 { | |
311 struct ptemap *a = (struct ptemap *) arg; | |
312 struct gcpro gcpro1; | |
313 Lisp_Object lisprange; | |
314 if (!a->first) | |
315 write_c_string (a->printcharfun, " "); | |
316 a->first = 0; | |
317 lisprange = encode_char_table_range (range); | |
318 GCPRO1 (lisprange); | |
4580
1d11ecca9cd0
Print char table values correctly.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4469
diff
changeset
|
319 write_fmt_string_lisp (a->printcharfun, "%s %S", 2, lisprange, val); |
826 | 320 UNGCPRO; |
321 return 0; | |
428 | 322 } |
323 | |
324 static void | |
2286 | 325 print_char_table (Lisp_Object obj, Lisp_Object printcharfun, |
326 int UNUSED (escapeflag)) | |
428 | 327 { |
440 | 328 Lisp_Char_Table *ct = XCHAR_TABLE (obj); |
826 | 329 struct chartab_range range; |
330 struct ptemap arg; | |
331 | |
332 range.type = CHARTAB_RANGE_ALL; | |
333 arg.printcharfun = printcharfun; | |
334 arg.first = 1; | |
428 | 335 |
793 | 336 write_fmt_string_lisp (printcharfun, "#s(char-table type %s data (", |
337 1, char_table_type_to_symbol (ct->type)); | |
826 | 338 map_char_table (obj, &range, print_table_entry, &arg); |
339 write_c_string (printcharfun, "))"); | |
428 | 340 |
826 | 341 /* #### need to print and read the default; but that will allow the |
342 default to be modified, which we don't (yet) support -- but FSF does */ | |
428 | 343 } |
344 | |
345 static int | |
346 char_table_equal (Lisp_Object obj1, Lisp_Object obj2, int depth) | |
347 { | |
440 | 348 Lisp_Char_Table *ct1 = XCHAR_TABLE (obj1); |
349 Lisp_Char_Table *ct2 = XCHAR_TABLE (obj2); | |
428 | 350 int i; |
351 | |
352 if (CHAR_TABLE_TYPE (ct1) != CHAR_TABLE_TYPE (ct2)) | |
353 return 0; | |
354 | |
355 for (i = 0; i < NUM_ASCII_CHARS; i++) | |
356 if (!internal_equal (ct1->ascii[i], ct2->ascii[i], depth + 1)) | |
357 return 0; | |
358 | |
359 #ifdef MULE | |
360 for (i = 0; i < NUM_LEADING_BYTES; i++) | |
361 if (!internal_equal (ct1->level1[i], ct2->level1[i], depth + 1)) | |
362 return 0; | |
363 #endif /* MULE */ | |
364 | |
826 | 365 return internal_equal (ct1->default_, ct2->default_, depth + 1); |
428 | 366 } |
367 | |
665 | 368 static Hashcode |
428 | 369 char_table_hash (Lisp_Object obj, int depth) |
370 { | |
440 | 371 Lisp_Char_Table *ct = XCHAR_TABLE (obj); |
665 | 372 Hashcode hashval = internal_array_hash (ct->ascii, NUM_ASCII_CHARS, |
826 | 373 depth + 1); |
428 | 374 #ifdef MULE |
375 hashval = HASH2 (hashval, | |
826 | 376 internal_array_hash (ct->level1, NUM_LEADING_BYTES, |
377 depth + 1)); | |
428 | 378 #endif /* MULE */ |
826 | 379 return HASH2 (hashval, internal_hash (ct->default_, depth + 1)); |
428 | 380 } |
381 | |
1204 | 382 static const struct memory_description char_table_description[] = { |
440 | 383 { XD_LISP_OBJECT_ARRAY, offsetof (Lisp_Char_Table, ascii), NUM_ASCII_CHARS }, |
428 | 384 #ifdef MULE |
440 | 385 { XD_LISP_OBJECT_ARRAY, offsetof (Lisp_Char_Table, level1), NUM_LEADING_BYTES }, |
428 | 386 #endif |
793 | 387 { XD_LISP_OBJECT, offsetof (Lisp_Char_Table, parent) }, |
388 { XD_LISP_OBJECT, offsetof (Lisp_Char_Table, default_) }, | |
440 | 389 { XD_LISP_OBJECT, offsetof (Lisp_Char_Table, mirror_table) }, |
390 { XD_LO_LINK, offsetof (Lisp_Char_Table, next_table) }, | |
428 | 391 { XD_END } |
392 }; | |
393 | |
5118
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
394 DEFINE_DUMPABLE_LISP_OBJECT ("char-table", char_table, |
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
395 mark_char_table, print_char_table, 0, |
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
396 char_table_equal, char_table_hash, |
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
397 char_table_description, |
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
diff
changeset
|
398 Lisp_Char_Table); |
428 | 399 |
400 DEFUN ("char-table-p", Fchar_table_p, 1, 1, 0, /* | |
401 Return non-nil if OBJECT is a char table. | |
402 */ | |
403 (object)) | |
404 { | |
405 return CHAR_TABLEP (object) ? Qt : Qnil; | |
406 } | |
407 | |
408 DEFUN ("char-table-type-list", Fchar_table_type_list, 0, 0, 0, /* | |
409 Return a list of the recognized char table types. | |
800 | 410 See `make-char-table'. |
428 | 411 */ |
412 ()) | |
413 { | |
414 #ifdef MULE | |
415 return list5 (Qchar, Qcategory, Qdisplay, Qgeneric, Qsyntax); | |
416 #else | |
417 return list4 (Qchar, Qdisplay, Qgeneric, Qsyntax); | |
418 #endif | |
419 } | |
420 | |
421 DEFUN ("valid-char-table-type-p", Fvalid_char_table_type_p, 1, 1, 0, /* | |
422 Return t if TYPE if a recognized char table type. | |
800 | 423 See `make-char-table'. |
428 | 424 */ |
425 (type)) | |
426 { | |
427 return (EQ (type, Qchar) || | |
428 #ifdef MULE | |
429 EQ (type, Qcategory) || | |
430 #endif | |
431 EQ (type, Qdisplay) || | |
432 EQ (type, Qgeneric) || | |
433 EQ (type, Qsyntax)) ? Qt : Qnil; | |
434 } | |
435 | |
436 DEFUN ("char-table-type", Fchar_table_type, 1, 1, 0, /* | |
444 | 437 Return the type of CHAR-TABLE. |
800 | 438 See `make-char-table'. |
428 | 439 */ |
444 | 440 (char_table)) |
428 | 441 { |
444 | 442 CHECK_CHAR_TABLE (char_table); |
443 return char_table_type_to_symbol (XCHAR_TABLE (char_table)->type); | |
428 | 444 } |
445 | |
1296 | 446 static void |
447 set_char_table_dirty (Lisp_Object table) | |
448 { | |
449 assert (!XCHAR_TABLE (table)->mirror_table_p); | |
450 XCHAR_TABLE (XCHAR_TABLE (table)->mirror_table)->dirty = 1; | |
451 } | |
452 | |
428 | 453 void |
826 | 454 set_char_table_default (Lisp_Object table, Lisp_Object value) |
455 { | |
456 Lisp_Char_Table *ct = XCHAR_TABLE (table); | |
457 ct->default_ = value; | |
458 if (ct->type == CHAR_TABLE_TYPE_SYNTAX) | |
1296 | 459 set_char_table_dirty (table); |
826 | 460 } |
461 | |
462 static void | |
440 | 463 fill_char_table (Lisp_Char_Table *ct, Lisp_Object value) |
428 | 464 { |
465 int i; | |
466 | |
467 for (i = 0; i < NUM_ASCII_CHARS; i++) | |
468 ct->ascii[i] = value; | |
469 #ifdef MULE | |
470 for (i = 0; i < NUM_LEADING_BYTES; i++) | |
1296 | 471 { |
1330 | 472 /* Don't get stymied when initting the table, or when trying to |
473 free a pdump object. */ | |
1296 | 474 if (!EQ (ct->level1[i], Qnull_pointer) && |
1330 | 475 CHAR_TABLE_ENTRYP (ct->level1[i]) && |
476 !OBJECT_DUMPED_P (ct->level1[1])) | |
3017 | 477 FREE_LCRECORD (ct->level1[i]); |
1296 | 478 ct->level1[i] = value; |
479 } | |
428 | 480 #endif /* MULE */ |
481 | |
482 if (ct->type == CHAR_TABLE_TYPE_SYNTAX) | |
1296 | 483 set_char_table_dirty (wrap_char_table (ct)); |
428 | 484 } |
485 | |
486 DEFUN ("reset-char-table", Freset_char_table, 1, 1, 0, /* | |
444 | 487 Reset CHAR-TABLE to its default state. |
428 | 488 */ |
444 | 489 (char_table)) |
428 | 490 { |
440 | 491 Lisp_Char_Table *ct; |
826 | 492 Lisp_Object def; |
428 | 493 |
444 | 494 CHECK_CHAR_TABLE (char_table); |
495 ct = XCHAR_TABLE (char_table); | |
428 | 496 |
497 switch (ct->type) | |
498 { | |
499 case CHAR_TABLE_TYPE_CHAR: | |
826 | 500 def = make_char (0); |
428 | 501 break; |
502 case CHAR_TABLE_TYPE_DISPLAY: | |
503 case CHAR_TABLE_TYPE_GENERIC: | |
504 #ifdef MULE | |
505 case CHAR_TABLE_TYPE_CATEGORY: | |
506 #endif /* MULE */ | |
826 | 507 def = Qnil; |
428 | 508 break; |
509 | |
510 case CHAR_TABLE_TYPE_SYNTAX: | |
826 | 511 def = make_int (Sinherit); |
428 | 512 break; |
513 | |
514 default: | |
2500 | 515 ABORT (); |
826 | 516 def = Qnil; |
517 break; | |
428 | 518 } |
519 | |
826 | 520 /* Avoid doubly updating the syntax table by setting the default ourselves, |
521 since set_char_table_default() also updates. */ | |
522 ct->default_ = def; | |
523 fill_char_table (ct, Qunbound); | |
524 | |
428 | 525 return Qnil; |
526 } | |
527 | |
528 DEFUN ("make-char-table", Fmake_char_table, 1, 1, 0, /* | |
529 Return a new, empty char table of type TYPE. | |
800 | 530 |
531 A char table is a table that maps characters (or ranges of characters) | |
532 to values. Char tables are specialized for characters, only allowing | |
533 particular sorts of ranges to be assigned values. Although this | |
534 loses in generality, it makes for extremely fast (constant-time) | |
535 lookups, and thus is feasible for applications that do an extremely | |
536 large number of lookups (e.g. scanning a buffer for a character in | |
537 a particular syntax, where a lookup in the syntax table must occur | |
538 once per character). | |
539 | |
540 When Mule support exists, the types of ranges that can be assigned | |
541 values are | |
542 | |
2714 | 543 -- all characters (represented by t) |
800 | 544 -- an entire charset |
2714 | 545 -- a single row in a two-octet charset (represented by a vector of two |
546 elements: a two-octet charset and a row number; the row must be an | |
547 integer, not a character) | |
800 | 548 -- a single character |
549 | |
550 When Mule support is not present, the types of ranges that can be | |
551 assigned values are | |
552 | |
2714 | 553 -- all characters (represented by t) |
800 | 554 -- a single character |
555 | |
556 To create a char table, use `make-char-table'. | |
557 To modify a char table, use `put-char-table' or `remove-char-table'. | |
558 To retrieve the value for a particular character, use `get-char-table'. | |
826 | 559 See also `map-char-table', `reset-char-table', `copy-char-table', |
800 | 560 `char-table-p', `valid-char-table-type-p', `char-table-type-list', |
561 `valid-char-table-value-p', and `check-char-table-value'. | |
562 | |
563 Each char table type is used for a different purpose and allows different | |
564 sorts of values. The different char table types are | |
565 | |
566 `category' | |
4469
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
567 Used for category tables, which specify the regexp categories that a |
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
568 character is in. The valid values are nil or a bit vector of 95 |
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
569 elements, and values default to nil. Higher-level Lisp functions |
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
570 are provided for working with category tables. Currently categories |
800 | 571 and category tables only exist when Mule support is present. |
572 `char' | |
4469
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
573 A generalized char table, for mapping from one character to another. |
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
574 Used for case tables, syntax matching tables, |
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
575 `keyboard-translate-table', etc. The valid values are characters, |
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
576 and the default result given by `get-char-table' if a value hasn't |
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
577 been set for a given character or for a range that includes it, is |
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
578 ?\x00. |
800 | 579 `generic' |
4469
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
580 An even more generalized char table, for mapping from a character to |
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
581 anything. The default result given by `get-char-table' is nil. |
800 | 582 `display' |
4469
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
583 Used for display tables, which specify how a particular character is |
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
584 to appear when displayed. #### Not yet implemented; currently, the |
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
585 display table code uses generic char tables, and it's not clear that |
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
586 implementing this char table type would be useful. |
800 | 587 `syntax' |
588 Used for syntax tables, which specify the syntax of a particular | |
589 character. Higher-level Lisp functions are provided for | |
4469
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
590 working with syntax tables. The valid values are integers, and the |
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
591 default result given by `get-char-table' is the syntax code for |
c661944aa259
Fill out docstrings for #'translate-region, #'make-char-table.
Aidan Kehoe <kehoea@parhasard.net>
parents:
3881
diff
changeset
|
592 `inherit'. |
428 | 593 */ |
594 (type)) | |
595 { | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
596 Lisp_Object obj = ALLOC_LISP_OBJECT (char_table); |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
597 Lisp_Char_Table *ct = XCHAR_TABLE (obj); |
428 | 598 enum char_table_type ty = symbol_to_char_table_type (type); |
599 | |
600 ct->type = ty; | |
601 if (ty == CHAR_TABLE_TYPE_SYNTAX) | |
602 { | |
826 | 603 /* Qgeneric not Qsyntax because a syntax table has a mirror table |
604 and we don't want infinite recursion */ | |
428 | 605 ct->mirror_table = Fmake_char_table (Qgeneric); |
3145 | 606 set_char_table_default (ct->mirror_table, make_int (Sword)); |
1296 | 607 XCHAR_TABLE (ct->mirror_table)->mirror_table_p = 1; |
608 XCHAR_TABLE (ct->mirror_table)->mirror_table = obj; | |
428 | 609 } |
610 else | |
611 ct->mirror_table = Qnil; | |
612 ct->next_table = Qnil; | |
793 | 613 ct->parent = Qnil; |
614 ct->default_ = Qnil; | |
428 | 615 if (ty == CHAR_TABLE_TYPE_SYNTAX) |
616 { | |
617 ct->next_table = Vall_syntax_tables; | |
618 Vall_syntax_tables = obj; | |
619 } | |
620 Freset_char_table (obj); | |
621 return obj; | |
622 } | |
623 | |
624 #ifdef MULE | |
625 | |
626 static Lisp_Object | |
627 make_char_table_entry (Lisp_Object initval) | |
628 { | |
629 int i; | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
630 Lisp_Object obj = ALLOC_LISP_OBJECT (char_table_entry); |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
631 Lisp_Char_Table_Entry *cte = XCHAR_TABLE_ENTRY (obj); |
428 | 632 |
633 for (i = 0; i < 96; i++) | |
634 cte->level2[i] = initval; | |
635 | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
636 return obj; |
428 | 637 } |
638 | |
639 static Lisp_Object | |
640 copy_char_table_entry (Lisp_Object entry) | |
641 { | |
440 | 642 Lisp_Char_Table_Entry *cte = XCHAR_TABLE_ENTRY (entry); |
428 | 643 int i; |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
644 Lisp_Object obj = ALLOC_LISP_OBJECT (char_table_entry); |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
645 Lisp_Char_Table_Entry *ctenew = XCHAR_TABLE_ENTRY (obj); |
428 | 646 |
647 for (i = 0; i < 96; i++) | |
648 { | |
3025 | 649 Lisp_Object new_ = cte->level2[i]; |
650 if (CHAR_TABLE_ENTRYP (new_)) | |
651 ctenew->level2[i] = copy_char_table_entry (new_); | |
428 | 652 else |
3025 | 653 ctenew->level2[i] = new_; |
428 | 654 } |
655 | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
656 return obj; |
428 | 657 } |
658 | |
659 #endif /* MULE */ | |
660 | |
661 DEFUN ("copy-char-table", Fcopy_char_table, 1, 1, 0, /* | |
444 | 662 Return a new char table which is a copy of CHAR-TABLE. |
428 | 663 It will contain the same values for the same characters and ranges |
444 | 664 as CHAR-TABLE. The values will not themselves be copied. |
428 | 665 */ |
444 | 666 (char_table)) |
428 | 667 { |
440 | 668 Lisp_Char_Table *ct, *ctnew; |
428 | 669 Lisp_Object obj; |
670 int i; | |
671 | |
444 | 672 CHECK_CHAR_TABLE (char_table); |
673 ct = XCHAR_TABLE (char_table); | |
3879 | 674 assert(!ct->mirror_table_p); |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
675 obj = ALLOC_LISP_OBJECT (char_table); |
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
676 ctnew = XCHAR_TABLE (obj); |
428 | 677 ctnew->type = ct->type; |
793 | 678 ctnew->parent = ct->parent; |
679 ctnew->default_ = ct->default_; | |
3879 | 680 ctnew->mirror_table_p = 0; |
428 | 681 |
682 for (i = 0; i < NUM_ASCII_CHARS; i++) | |
683 { | |
3025 | 684 Lisp_Object new_ = ct->ascii[i]; |
428 | 685 #ifdef MULE |
3025 | 686 assert (! (CHAR_TABLE_ENTRYP (new_))); |
428 | 687 #endif /* MULE */ |
3025 | 688 ctnew->ascii[i] = new_; |
428 | 689 } |
690 | |
691 #ifdef MULE | |
692 | |
693 for (i = 0; i < NUM_LEADING_BYTES; i++) | |
694 { | |
3025 | 695 Lisp_Object new_ = ct->level1[i]; |
696 if (CHAR_TABLE_ENTRYP (new_)) | |
697 ctnew->level1[i] = copy_char_table_entry (new_); | |
428 | 698 else |
3025 | 699 ctnew->level1[i] = new_; |
428 | 700 } |
701 | |
702 #endif /* MULE */ | |
703 | |
3881 | 704 if (!EQ (ct->mirror_table, Qnil)) |
1296 | 705 { |
3879 | 706 ctnew->mirror_table = Fmake_char_table (Qgeneric); |
707 set_char_table_default (ctnew->mirror_table, make_int (Sword)); | |
1296 | 708 XCHAR_TABLE (ctnew->mirror_table)->mirror_table = obj; |
3879 | 709 XCHAR_TABLE (ctnew->mirror_table)->mirror_table_p = 1; |
710 XCHAR_TABLE (ctnew->mirror_table)->dirty = 1; | |
1296 | 711 } |
428 | 712 else |
3879 | 713 ctnew->mirror_table = Qnil; |
714 | |
428 | 715 ctnew->next_table = Qnil; |
716 if (ctnew->type == CHAR_TABLE_TYPE_SYNTAX) | |
717 { | |
718 ctnew->next_table = Vall_syntax_tables; | |
719 Vall_syntax_tables = obj; | |
720 } | |
721 return obj; | |
722 } | |
723 | |
724 #ifdef MULE | |
725 | |
826 | 726 /* called from get_char_table(). */ |
428 | 727 Lisp_Object |
440 | 728 get_non_ascii_char_table_value (Lisp_Char_Table *ct, int leading_byte, |
867 | 729 Ichar c) |
428 | 730 { |
731 Lisp_Object val; | |
826 | 732 Lisp_Object charset = charset_by_leading_byte (leading_byte); |
428 | 733 int byte1, byte2; |
734 | |
867 | 735 BREAKUP_ICHAR_1_UNSAFE (c, charset, byte1, byte2); |
428 | 736 val = ct->level1[leading_byte - MIN_LEADING_BYTE]; |
737 if (CHAR_TABLE_ENTRYP (val)) | |
738 { | |
440 | 739 Lisp_Char_Table_Entry *cte = XCHAR_TABLE_ENTRY (val); |
428 | 740 val = cte->level2[byte1 - 32]; |
741 if (CHAR_TABLE_ENTRYP (val)) | |
742 { | |
743 cte = XCHAR_TABLE_ENTRY (val); | |
744 assert (byte2 >= 32); | |
745 val = cte->level2[byte2 - 32]; | |
746 assert (!CHAR_TABLE_ENTRYP (val)); | |
747 } | |
748 } | |
749 | |
750 return val; | |
751 } | |
752 | |
753 #endif /* MULE */ | |
754 | |
826 | 755 DEFUN ("char-table-default", Fchar_table_default, 1, 1, 0, /* |
756 Return the default value for CHAR-TABLE. When an entry for a character | |
757 does not exist, the default is returned. | |
758 */ | |
759 (char_table)) | |
428 | 760 { |
826 | 761 CHECK_CHAR_TABLE (char_table); |
762 return XCHAR_TABLE (char_table)->default_; | |
428 | 763 } |
764 | |
826 | 765 DEFUN ("set-char-table-default", Fset_char_table_default, 2, 2, 0, /* |
766 Set the default value for CHAR-TABLE to DEFAULT. | |
767 Currently, the default value for syntax tables cannot be changed. | |
768 (This policy might change in the future.) | |
769 */ | |
770 (char_table, default_)) | |
771 { | |
772 CHECK_CHAR_TABLE (char_table); | |
773 if (XCHAR_TABLE_TYPE (char_table) == CHAR_TABLE_TYPE_SYNTAX) | |
774 invalid_change ("Can't change default for syntax tables", char_table); | |
775 check_valid_char_table_value (default_, XCHAR_TABLE_TYPE (char_table), | |
776 ERROR_ME); | |
777 set_char_table_default (char_table, default_); | |
778 return Qnil; | |
779 } | |
428 | 780 |
781 DEFUN ("get-char-table", Fget_char_table, 2, 2, 0, /* | |
444 | 782 Find value for CHARACTER in CHAR-TABLE. |
428 | 783 */ |
444 | 784 (character, char_table)) |
428 | 785 { |
444 | 786 CHECK_CHAR_TABLE (char_table); |
787 CHECK_CHAR_COERCE_INT (character); | |
428 | 788 |
826 | 789 return get_char_table (XCHAR (character), char_table); |
790 } | |
791 | |
792 static int | |
2286 | 793 copy_mapper (struct chartab_range *range, Lisp_Object UNUSED (table), |
826 | 794 Lisp_Object val, void *arg) |
795 { | |
796 put_char_table (VOID_TO_LISP (arg), range, val); | |
797 return 0; | |
798 } | |
799 | |
800 void | |
801 copy_char_table_range (Lisp_Object from, Lisp_Object to, | |
802 struct chartab_range *range) | |
803 { | |
804 map_char_table (from, range, copy_mapper, LISP_TO_VOID (to)); | |
805 } | |
806 | |
1296 | 807 static Lisp_Object |
808 get_range_char_table_1 (struct chartab_range *range, Lisp_Object table, | |
809 Lisp_Object multi) | |
826 | 810 { |
811 Lisp_Char_Table *ct = XCHAR_TABLE (table); | |
812 Lisp_Object retval = Qnil; | |
813 | |
814 switch (range->type) | |
815 { | |
816 case CHARTAB_RANGE_CHAR: | |
817 return get_char_table (range->ch, table); | |
818 | |
819 case CHARTAB_RANGE_ALL: | |
820 { | |
821 int i; | |
822 retval = ct->ascii[0]; | |
823 | |
824 for (i = 1; i < NUM_ASCII_CHARS; i++) | |
825 if (!EQ (retval, ct->ascii[i])) | |
826 return multi; | |
827 | |
828 #ifdef MULE | |
829 for (i = MIN_LEADING_BYTE; i < MIN_LEADING_BYTE + NUM_LEADING_BYTES; | |
830 i++) | |
831 { | |
832 if (!CHARSETP (charset_by_leading_byte (i)) | |
833 || i == LEADING_BYTE_ASCII | |
834 || i == LEADING_BYTE_CONTROL_1) | |
835 continue; | |
836 if (!EQ (retval, ct->level1[i - MIN_LEADING_BYTE])) | |
837 return multi; | |
838 } | |
839 #endif /* MULE */ | |
840 | |
841 break; | |
842 } | |
843 | |
844 #ifdef MULE | |
845 case CHARTAB_RANGE_CHARSET: | |
846 if (EQ (range->charset, Vcharset_ascii)) | |
847 { | |
848 int i; | |
849 retval = ct->ascii[0]; | |
850 | |
851 for (i = 1; i < 128; i++) | |
852 if (!EQ (retval, ct->ascii[i])) | |
853 return multi; | |
854 break; | |
855 } | |
856 | |
857 if (EQ (range->charset, Vcharset_control_1)) | |
858 { | |
859 int i; | |
860 retval = ct->ascii[128]; | |
861 | |
862 for (i = 129; i < 160; i++) | |
863 if (!EQ (retval, ct->ascii[i])) | |
864 return multi; | |
865 break; | |
866 } | |
867 | |
868 { | |
869 retval = ct->level1[XCHARSET_LEADING_BYTE (range->charset) - | |
870 MIN_LEADING_BYTE]; | |
871 if (CHAR_TABLE_ENTRYP (retval)) | |
872 return multi; | |
873 break; | |
874 } | |
875 | |
876 case CHARTAB_RANGE_ROW: | |
877 { | |
878 retval = ct->level1[XCHARSET_LEADING_BYTE (range->charset) - | |
879 MIN_LEADING_BYTE]; | |
880 if (!CHAR_TABLE_ENTRYP (retval)) | |
881 break; | |
882 retval = XCHAR_TABLE_ENTRY (retval)->level2[range->row - 32]; | |
883 if (CHAR_TABLE_ENTRYP (retval)) | |
884 return multi; | |
885 break; | |
886 } | |
887 #endif /* not MULE */ | |
888 | |
889 default: | |
2500 | 890 ABORT (); |
826 | 891 } |
892 | |
893 if (UNBOUNDP (retval)) | |
894 return ct->default_; | |
895 return retval; | |
428 | 896 } |
897 | |
1296 | 898 Lisp_Object |
899 get_range_char_table (struct chartab_range *range, Lisp_Object table, | |
900 Lisp_Object multi) | |
901 { | |
902 if (range->type == CHARTAB_RANGE_CHAR) | |
903 return get_char_table (range->ch, table); | |
904 else | |
905 return get_range_char_table_1 (range, table, multi); | |
906 } | |
907 | |
908 #ifdef ERROR_CHECK_TYPES | |
909 | |
910 /* Only exists so as not to trip an assert in get_char_table(). */ | |
911 Lisp_Object | |
912 updating_mirror_get_range_char_table (struct chartab_range *range, | |
913 Lisp_Object table, | |
914 Lisp_Object multi) | |
915 { | |
916 if (range->type == CHARTAB_RANGE_CHAR) | |
917 return get_char_table_1 (range->ch, table); | |
918 else | |
919 return get_range_char_table_1 (range, table, multi); | |
920 } | |
921 | |
922 #endif /* ERROR_CHECK_TYPES */ | |
923 | |
428 | 924 DEFUN ("get-range-char-table", Fget_range_char_table, 2, 3, 0, /* |
2714 | 925 Find value for RANGE in CHAR-TABLE. |
428 | 926 If there is more than one value, return MULTI (defaults to nil). |
2714 | 927 |
928 Valid values for RANGE are single characters, charsets, a row in a | |
929 two-octet charset, and all characters. See `put-char-table'. | |
428 | 930 */ |
444 | 931 (range, char_table, multi)) |
428 | 932 { |
933 struct chartab_range rainj; | |
934 | |
935 if (CHAR_OR_CHAR_INTP (range)) | |
444 | 936 return Fget_char_table (range, char_table); |
937 CHECK_CHAR_TABLE (char_table); | |
428 | 938 |
939 decode_char_table_range (range, &rainj); | |
826 | 940 return get_range_char_table (&rainj, char_table, multi); |
428 | 941 } |
826 | 942 |
428 | 943 static int |
944 check_valid_char_table_value (Lisp_Object value, enum char_table_type type, | |
578 | 945 Error_Behavior errb) |
428 | 946 { |
947 switch (type) | |
948 { | |
949 case CHAR_TABLE_TYPE_SYNTAX: | |
950 if (!ERRB_EQ (errb, ERROR_ME)) | |
951 return INTP (value) || (CONSP (value) && INTP (XCAR (value)) | |
952 && CHAR_OR_CHAR_INTP (XCDR (value))); | |
953 if (CONSP (value)) | |
954 { | |
955 Lisp_Object cdr = XCDR (value); | |
956 CHECK_INT (XCAR (value)); | |
957 CHECK_CHAR_COERCE_INT (cdr); | |
958 } | |
959 else | |
960 CHECK_INT (value); | |
961 break; | |
962 | |
963 #ifdef MULE | |
964 case CHAR_TABLE_TYPE_CATEGORY: | |
965 if (!ERRB_EQ (errb, ERROR_ME)) | |
966 return CATEGORY_TABLE_VALUEP (value); | |
967 CHECK_CATEGORY_TABLE_VALUE (value); | |
968 break; | |
969 #endif /* MULE */ | |
970 | |
971 case CHAR_TABLE_TYPE_GENERIC: | |
972 return 1; | |
973 | |
974 case CHAR_TABLE_TYPE_DISPLAY: | |
975 /* #### fix this */ | |
563 | 976 maybe_signal_error (Qunimplemented, |
977 "Display char tables not yet implemented", | |
978 value, Qchar_table, errb); | |
428 | 979 return 0; |
980 | |
981 case CHAR_TABLE_TYPE_CHAR: | |
982 if (!ERRB_EQ (errb, ERROR_ME)) | |
983 return CHAR_OR_CHAR_INTP (value); | |
984 CHECK_CHAR_COERCE_INT (value); | |
985 break; | |
986 | |
987 default: | |
2500 | 988 ABORT (); |
428 | 989 } |
990 | |
801 | 991 return 0; /* not (usually) reached */ |
428 | 992 } |
993 | |
994 static Lisp_Object | |
995 canonicalize_char_table_value (Lisp_Object value, enum char_table_type type) | |
996 { | |
997 switch (type) | |
998 { | |
999 case CHAR_TABLE_TYPE_SYNTAX: | |
1000 if (CONSP (value)) | |
1001 { | |
1002 Lisp_Object car = XCAR (value); | |
1003 Lisp_Object cdr = XCDR (value); | |
1004 CHECK_CHAR_COERCE_INT (cdr); | |
1005 return Fcons (car, cdr); | |
1006 } | |
1007 break; | |
1008 case CHAR_TABLE_TYPE_CHAR: | |
1009 CHECK_CHAR_COERCE_INT (value); | |
1010 break; | |
1011 default: | |
1012 break; | |
1013 } | |
1014 return value; | |
1015 } | |
1016 | |
1017 DEFUN ("valid-char-table-value-p", Fvalid_char_table_value_p, 2, 2, 0, /* | |
1018 Return non-nil if VALUE is a valid value for CHAR-TABLE-TYPE. | |
1019 */ | |
1020 (value, char_table_type)) | |
1021 { | |
1022 enum char_table_type type = symbol_to_char_table_type (char_table_type); | |
1023 | |
1024 return check_valid_char_table_value (value, type, ERROR_ME_NOT) ? Qt : Qnil; | |
1025 } | |
1026 | |
1027 DEFUN ("check-valid-char-table-value", Fcheck_valid_char_table_value, 2, 2, 0, /* | |
1028 Signal an error if VALUE is not a valid value for CHAR-TABLE-TYPE. | |
1029 */ | |
1030 (value, char_table_type)) | |
1031 { | |
1032 enum char_table_type type = symbol_to_char_table_type (char_table_type); | |
1033 | |
1034 check_valid_char_table_value (value, type, ERROR_ME); | |
1035 return Qnil; | |
1036 } | |
1037 | |
826 | 1038 /* Assign VAL to all characters in RANGE in char table TABLE. */ |
428 | 1039 |
1040 void | |
826 | 1041 put_char_table (Lisp_Object table, struct chartab_range *range, |
428 | 1042 Lisp_Object val) |
1043 { | |
826 | 1044 Lisp_Char_Table *ct = XCHAR_TABLE (table); |
1045 | |
428 | 1046 switch (range->type) |
1047 { | |
1048 case CHARTAB_RANGE_ALL: | |
1049 fill_char_table (ct, val); | |
1296 | 1050 return; /* fill_char_table() recorded the table as dirty. */ |
428 | 1051 |
1052 #ifdef MULE | |
1053 case CHARTAB_RANGE_CHARSET: | |
1054 if (EQ (range->charset, Vcharset_ascii)) | |
1055 { | |
1056 int i; | |
1057 for (i = 0; i < 128; i++) | |
1058 ct->ascii[i] = val; | |
1059 } | |
1060 else if (EQ (range->charset, Vcharset_control_1)) | |
1061 { | |
1062 int i; | |
1063 for (i = 128; i < 160; i++) | |
1064 ct->ascii[i] = val; | |
1065 } | |
1066 else | |
1067 { | |
1068 int lb = XCHARSET_LEADING_BYTE (range->charset) - MIN_LEADING_BYTE; | |
1330 | 1069 if (CHAR_TABLE_ENTRYP (ct->level1[lb]) && |
1070 !OBJECT_DUMPED_P (ct->level1[lb])) | |
3017 | 1071 FREE_LCRECORD (ct->level1[lb]); |
428 | 1072 ct->level1[lb] = val; |
1073 } | |
1074 break; | |
1075 | |
1076 case CHARTAB_RANGE_ROW: | |
1077 { | |
440 | 1078 Lisp_Char_Table_Entry *cte; |
428 | 1079 int lb = XCHARSET_LEADING_BYTE (range->charset) - MIN_LEADING_BYTE; |
1080 /* make sure that there is a separate entry for the row. */ | |
1081 if (!CHAR_TABLE_ENTRYP (ct->level1[lb])) | |
1082 ct->level1[lb] = make_char_table_entry (ct->level1[lb]); | |
1083 cte = XCHAR_TABLE_ENTRY (ct->level1[lb]); | |
1084 cte->level2[range->row - 32] = val; | |
1085 } | |
1086 break; | |
1087 #endif /* MULE */ | |
1088 | |
1089 case CHARTAB_RANGE_CHAR: | |
1090 #ifdef MULE | |
1091 { | |
1092 Lisp_Object charset; | |
1093 int byte1, byte2; | |
1094 | |
867 | 1095 BREAKUP_ICHAR (range->ch, charset, byte1, byte2); |
428 | 1096 if (EQ (charset, Vcharset_ascii)) |
1097 ct->ascii[byte1] = val; | |
1098 else if (EQ (charset, Vcharset_control_1)) | |
1099 ct->ascii[byte1 + 128] = val; | |
1100 else | |
1101 { | |
440 | 1102 Lisp_Char_Table_Entry *cte; |
428 | 1103 int lb = XCHARSET_LEADING_BYTE (charset) - MIN_LEADING_BYTE; |
1104 /* make sure that there is a separate entry for the row. */ | |
1105 if (!CHAR_TABLE_ENTRYP (ct->level1[lb])) | |
1106 ct->level1[lb] = make_char_table_entry (ct->level1[lb]); | |
1107 cte = XCHAR_TABLE_ENTRY (ct->level1[lb]); | |
1108 /* now CTE is a char table entry for the charset; | |
1109 each entry is for a single row (or character of | |
1110 a one-octet charset). */ | |
1111 if (XCHARSET_DIMENSION (charset) == 1) | |
1112 cte->level2[byte1 - 32] = val; | |
1113 else | |
1114 { | |
1115 /* assigning to one character in a two-octet charset. */ | |
1116 /* make sure that the charset row contains a separate | |
1117 entry for each character. */ | |
1118 if (!CHAR_TABLE_ENTRYP (cte->level2[byte1 - 32])) | |
1119 cte->level2[byte1 - 32] = | |
1120 make_char_table_entry (cte->level2[byte1 - 32]); | |
1121 cte = XCHAR_TABLE_ENTRY (cte->level2[byte1 - 32]); | |
1122 cte->level2[byte2 - 32] = val; | |
1123 } | |
1124 } | |
1125 } | |
1126 #else /* not MULE */ | |
1127 ct->ascii[(unsigned char) (range->ch)] = val; | |
1128 break; | |
1129 #endif /* not MULE */ | |
1130 } | |
1131 | |
1132 if (ct->type == CHAR_TABLE_TYPE_SYNTAX) | |
1296 | 1133 set_char_table_dirty (wrap_char_table (ct)); |
428 | 1134 } |
1135 | |
1136 DEFUN ("put-char-table", Fput_char_table, 3, 3, 0, /* | |
444 | 1137 Set the value for chars in RANGE to be VALUE in CHAR-TABLE. |
428 | 1138 |
1139 RANGE specifies one or more characters to be affected and should be | |
1140 one of the following: | |
1141 | |
1142 -- t (all characters are affected) | |
1143 -- A charset (only allowed when Mule support is present) | |
2714 | 1144 -- A vector of two elements: a two-octet charset and a row number; the row |
1145 must be an integer, not a character (only allowed when Mule support is | |
1146 present) | |
428 | 1147 -- A single character |
1148 | |
444 | 1149 VALUE must be a value appropriate for the type of CHAR-TABLE. |
800 | 1150 See `make-char-table'. |
428 | 1151 */ |
444 | 1152 (range, value, char_table)) |
428 | 1153 { |
440 | 1154 Lisp_Char_Table *ct; |
428 | 1155 struct chartab_range rainj; |
1156 | |
444 | 1157 CHECK_CHAR_TABLE (char_table); |
1158 ct = XCHAR_TABLE (char_table); | |
1159 check_valid_char_table_value (value, ct->type, ERROR_ME); | |
428 | 1160 decode_char_table_range (range, &rainj); |
444 | 1161 value = canonicalize_char_table_value (value, ct->type); |
826 | 1162 put_char_table (char_table, &rainj, value); |
1163 return Qnil; | |
1164 } | |
1165 | |
1166 DEFUN ("remove-char-table", Fremove_char_table, 2, 2, 0, /* | |
1167 Remove any value from chars in RANGE in CHAR-TABLE. | |
1168 | |
1169 RANGE specifies one or more characters to be affected and should be | |
1170 one of the following: | |
1171 | |
1172 -- t (all characters are affected) | |
1173 -- A charset (only allowed when Mule support is present) | |
1174 -- A vector of two elements: a two-octet charset and a row number | |
1175 (only allowed when Mule support is present) | |
1176 -- A single character | |
1177 | |
2726 | 1178 With all values removed, the default value will be returned by |
1179 `get-char-table' and `get-range-char-table'. | |
826 | 1180 */ |
1181 (range, char_table)) | |
1182 { | |
1183 struct chartab_range rainj; | |
1184 | |
1185 CHECK_CHAR_TABLE (char_table); | |
1186 decode_char_table_range (range, &rainj); | |
1187 put_char_table (char_table, &rainj, Qunbound); | |
428 | 1188 return Qnil; |
1189 } | |
1190 | |
1191 /* Map FN over the ASCII chars in CT. */ | |
1192 | |
1193 static int | |
826 | 1194 map_over_charset_ascii_1 (Lisp_Char_Table *ct, |
1195 int start, int stop, | |
1196 int (*fn) (struct chartab_range *range, | |
1197 Lisp_Object table, Lisp_Object val, | |
1198 void *arg), | |
1199 void *arg) | |
1200 { | |
1201 struct chartab_range rainj; | |
1202 int i, retval; | |
1203 | |
1204 rainj.type = CHARTAB_RANGE_CHAR; | |
1205 | |
1206 for (i = start, retval = 0; i <= stop && retval == 0; i++) | |
1207 { | |
867 | 1208 rainj.ch = (Ichar) i; |
826 | 1209 if (!UNBOUNDP (ct->ascii[i])) |
1210 retval = (fn) (&rainj, wrap_char_table (ct), ct->ascii[i], arg); | |
1211 } | |
1212 | |
1213 return retval; | |
1214 } | |
1215 | |
1216 | |
1217 /* Map FN over the ASCII chars in CT. */ | |
1218 | |
1219 static int | |
440 | 1220 map_over_charset_ascii (Lisp_Char_Table *ct, |
428 | 1221 int (*fn) (struct chartab_range *range, |
826 | 1222 Lisp_Object table, Lisp_Object val, |
1223 void *arg), | |
428 | 1224 void *arg) |
1225 { | |
826 | 1226 return map_over_charset_ascii_1 (ct, 0, |
428 | 1227 #ifdef MULE |
826 | 1228 127, |
428 | 1229 #else |
826 | 1230 255, |
428 | 1231 #endif |
826 | 1232 fn, arg); |
428 | 1233 } |
1234 | |
1235 #ifdef MULE | |
1236 | |
1237 /* Map FN over the Control-1 chars in CT. */ | |
1238 | |
1239 static int | |
440 | 1240 map_over_charset_control_1 (Lisp_Char_Table *ct, |
428 | 1241 int (*fn) (struct chartab_range *range, |
826 | 1242 Lisp_Object table, Lisp_Object val, |
1243 void *arg), | |
428 | 1244 void *arg) |
1245 { | |
826 | 1246 return map_over_charset_ascii_1 (ct, 128, 159, fn, arg); |
428 | 1247 } |
1248 | |
1249 /* Map FN over the row ROW of two-byte charset CHARSET. | |
1250 There must be a separate value for that row in the char table. | |
1251 CTE specifies the char table entry for CHARSET. */ | |
1252 | |
1253 static int | |
826 | 1254 map_over_charset_row (Lisp_Char_Table *ct, |
1255 Lisp_Char_Table_Entry *cte, | |
428 | 1256 Lisp_Object charset, int row, |
1257 int (*fn) (struct chartab_range *range, | |
826 | 1258 Lisp_Object table, Lisp_Object val, |
1259 void *arg), | |
428 | 1260 void *arg) |
1261 { | |
1262 Lisp_Object val = cte->level2[row - 32]; | |
1263 | |
826 | 1264 if (UNBOUNDP (val)) |
1265 return 0; | |
1266 else if (!CHAR_TABLE_ENTRYP (val)) | |
428 | 1267 { |
1268 struct chartab_range rainj; | |
826 | 1269 |
428 | 1270 rainj.type = CHARTAB_RANGE_ROW; |
1271 rainj.charset = charset; | |
1272 rainj.row = row; | |
826 | 1273 return (fn) (&rainj, wrap_char_table (ct), val, arg); |
428 | 1274 } |
1275 else | |
1276 { | |
1277 struct chartab_range rainj; | |
1278 int i, retval; | |
826 | 1279 int start, stop; |
1280 | |
1281 get_charset_limits (charset, &start, &stop); | |
428 | 1282 |
1283 cte = XCHAR_TABLE_ENTRY (val); | |
1284 | |
1285 rainj.type = CHARTAB_RANGE_CHAR; | |
1286 | |
826 | 1287 for (i = start, retval = 0; i <= stop && retval == 0; i++) |
428 | 1288 { |
867 | 1289 rainj.ch = make_ichar (charset, row, i); |
826 | 1290 if (!UNBOUNDP (cte->level2[i - 32])) |
1291 retval = (fn) (&rainj, wrap_char_table (ct), cte->level2[i - 32], | |
1292 arg); | |
428 | 1293 } |
1294 return retval; | |
1295 } | |
1296 } | |
1297 | |
1298 | |
1299 static int | |
440 | 1300 map_over_other_charset (Lisp_Char_Table *ct, int lb, |
428 | 1301 int (*fn) (struct chartab_range *range, |
826 | 1302 Lisp_Object table, Lisp_Object val, |
1303 void *arg), | |
428 | 1304 void *arg) |
1305 { | |
1306 Lisp_Object val = ct->level1[lb - MIN_LEADING_BYTE]; | |
826 | 1307 Lisp_Object charset = charset_by_leading_byte (lb); |
428 | 1308 |
1309 if (!CHARSETP (charset) | |
1310 || lb == LEADING_BYTE_ASCII | |
1311 || lb == LEADING_BYTE_CONTROL_1) | |
1312 return 0; | |
1313 | |
826 | 1314 if (UNBOUNDP (val)) |
1315 return 0; | |
428 | 1316 if (!CHAR_TABLE_ENTRYP (val)) |
1317 { | |
1318 struct chartab_range rainj; | |
1319 | |
1320 rainj.type = CHARTAB_RANGE_CHARSET; | |
1321 rainj.charset = charset; | |
826 | 1322 return (fn) (&rainj, wrap_char_table (ct), val, arg); |
428 | 1323 } |
1324 { | |
440 | 1325 Lisp_Char_Table_Entry *cte = XCHAR_TABLE_ENTRY (val); |
826 | 1326 int start, stop; |
428 | 1327 int i, retval; |
1328 | |
826 | 1329 get_charset_limits (charset, &start, &stop); |
428 | 1330 if (XCHARSET_DIMENSION (charset) == 1) |
1331 { | |
1332 struct chartab_range rainj; | |
1333 rainj.type = CHARTAB_RANGE_CHAR; | |
1334 | |
826 | 1335 for (i = start, retval = 0; i <= stop && retval == 0; i++) |
428 | 1336 { |
867 | 1337 rainj.ch = make_ichar (charset, i, 0); |
826 | 1338 if (!UNBOUNDP (cte->level2[i - 32])) |
1339 retval = (fn) (&rainj, wrap_char_table (ct), cte->level2[i - 32], | |
1340 arg); | |
428 | 1341 } |
1342 } | |
1343 else | |
1344 { | |
826 | 1345 for (i = start, retval = 0; i <= stop && retval == 0; i++) |
1346 retval = map_over_charset_row (ct, cte, charset, i, fn, arg); | |
428 | 1347 } |
1348 | |
1349 return retval; | |
1350 } | |
1351 } | |
1352 | |
1353 #endif /* MULE */ | |
1354 | |
1355 /* Map FN (with client data ARG) over range RANGE in char table CT. | |
1356 Mapping stops the first time FN returns non-zero, and that value | |
826 | 1357 becomes the return value of map_char_table(). |
1358 | |
1359 #### This mapping code is way ugly. The FSF version, in contrast, | |
1360 is short and sweet, and much more recursive. There should be some way | |
1361 of cleaning this up. */ | |
428 | 1362 |
1363 int | |
826 | 1364 map_char_table (Lisp_Object table, |
428 | 1365 struct chartab_range *range, |
1366 int (*fn) (struct chartab_range *range, | |
826 | 1367 Lisp_Object table, Lisp_Object val, void *arg), |
428 | 1368 void *arg) |
1369 { | |
826 | 1370 Lisp_Char_Table *ct = XCHAR_TABLE (table); |
428 | 1371 switch (range->type) |
1372 { | |
1373 case CHARTAB_RANGE_ALL: | |
1374 { | |
1375 int retval; | |
1376 | |
1377 retval = map_over_charset_ascii (ct, fn, arg); | |
1378 if (retval) | |
1379 return retval; | |
1380 #ifdef MULE | |
1381 retval = map_over_charset_control_1 (ct, fn, arg); | |
1382 if (retval) | |
1383 return retval; | |
1384 { | |
1385 int i; | |
1386 int start = MIN_LEADING_BYTE; | |
1387 int stop = start + NUM_LEADING_BYTES; | |
1388 | |
1389 for (i = start, retval = 0; i < stop && retval == 0; i++) | |
1390 { | |
771 | 1391 if (i != LEADING_BYTE_ASCII && i != LEADING_BYTE_CONTROL_1) |
1392 retval = map_over_other_charset (ct, i, fn, arg); | |
428 | 1393 } |
1394 } | |
1395 #endif /* MULE */ | |
1396 return retval; | |
1397 } | |
1398 | |
1399 #ifdef MULE | |
1400 case CHARTAB_RANGE_CHARSET: | |
1401 return map_over_other_charset (ct, | |
1402 XCHARSET_LEADING_BYTE (range->charset), | |
1403 fn, arg); | |
1404 | |
1405 case CHARTAB_RANGE_ROW: | |
1406 { | |
771 | 1407 Lisp_Object val = ct->level1[XCHARSET_LEADING_BYTE (range->charset) - |
1408 MIN_LEADING_BYTE]; | |
826 | 1409 |
1410 if (CHAR_TABLE_ENTRYP (val)) | |
1411 return map_over_charset_row (ct, XCHAR_TABLE_ENTRY (val), | |
1412 range->charset, range->row, fn, arg); | |
1413 else if (!UNBOUNDP (val)) | |
428 | 1414 { |
1415 struct chartab_range rainj; | |
1416 | |
1417 rainj.type = CHARTAB_RANGE_ROW; | |
1418 rainj.charset = range->charset; | |
1419 rainj.row = range->row; | |
826 | 1420 return (fn) (&rainj, table, val, arg); |
428 | 1421 } |
1422 else | |
826 | 1423 return 0; |
428 | 1424 } |
1425 #endif /* MULE */ | |
1426 | |
1427 case CHARTAB_RANGE_CHAR: | |
1428 { | |
867 | 1429 Ichar ch = range->ch; |
826 | 1430 Lisp_Object val = get_char_table (ch, table); |
428 | 1431 struct chartab_range rainj; |
1432 | |
826 | 1433 if (!UNBOUNDP (val)) |
1434 { | |
1435 rainj.type = CHARTAB_RANGE_CHAR; | |
1436 rainj.ch = ch; | |
1437 return (fn) (&rainj, table, val, arg); | |
1438 } | |
1439 else | |
1440 return 0; | |
428 | 1441 } |
1442 | |
1443 default: | |
2500 | 1444 ABORT (); |
428 | 1445 } |
1446 | |
1447 return 0; | |
1448 } | |
1449 | |
1450 struct slow_map_char_table_arg | |
1451 { | |
1452 Lisp_Object function; | |
1453 Lisp_Object retval; | |
1454 }; | |
1455 | |
1456 static int | |
1457 slow_map_char_table_fun (struct chartab_range *range, | |
2286 | 1458 Lisp_Object UNUSED (table), Lisp_Object val, |
1459 void *arg) | |
428 | 1460 { |
1461 struct slow_map_char_table_arg *closure = | |
1462 (struct slow_map_char_table_arg *) arg; | |
1463 | |
826 | 1464 closure->retval = call2 (closure->function, encode_char_table_range (range), |
1465 val); | |
428 | 1466 return !NILP (closure->retval); |
1467 } | |
1468 | |
1469 DEFUN ("map-char-table", Fmap_char_table, 2, 3, 0, /* | |
2726 | 1470 Map FUNCTION over CHAR-TABLE until it returns non-nil; return that value. |
1471 FUNCTION is called with two arguments, each key and entry in the table. | |
1472 | |
1473 RANGE specifies a subrange to map over. If omitted or t, it defaults to | |
1474 the entire table. | |
428 | 1475 |
2726 | 1476 Both RANGE and the keys passed to FUNCTION are in the same format as the |
1477 RANGE argument to `put-char-table'. N.B. This function does NOT map over | |
1478 all characters in RANGE, but over the subranges that have been assigned to. | |
1479 Thus this function is most suitable for searching a char-table, or for | |
1480 populating one char-table based on the contents of another. The current | |
1481 implementation does not coalesce ranges all of whose values are the same. | |
428 | 1482 */ |
444 | 1483 (function, char_table, range)) |
428 | 1484 { |
1485 struct slow_map_char_table_arg slarg; | |
1486 struct gcpro gcpro1, gcpro2; | |
1487 struct chartab_range rainj; | |
1488 | |
444 | 1489 CHECK_CHAR_TABLE (char_table); |
428 | 1490 if (NILP (range)) |
1491 range = Qt; | |
1492 decode_char_table_range (range, &rainj); | |
1493 slarg.function = function; | |
1494 slarg.retval = Qnil; | |
1495 GCPRO2 (slarg.function, slarg.retval); | |
826 | 1496 map_char_table (char_table, &rainj, slow_map_char_table_fun, &slarg); |
428 | 1497 UNGCPRO; |
1498 | |
1499 return slarg.retval; | |
1500 } | |
1501 | |
1502 | |
1503 | |
1504 /************************************************************************/ | |
1505 /* Char table read syntax */ | |
1506 /************************************************************************/ | |
1507 | |
1508 static int | |
2286 | 1509 chartab_type_validate (Lisp_Object UNUSED (keyword), Lisp_Object value, |
1510 Error_Behavior UNUSED (errb)) | |
428 | 1511 { |
1512 /* #### should deal with ERRB */ | |
1513 symbol_to_char_table_type (value); | |
1514 return 1; | |
1515 } | |
1516 | |
826 | 1517 /* #### Document the print/read format; esp. what's this cons element? */ |
1518 | |
428 | 1519 static int |
2286 | 1520 chartab_data_validate (Lisp_Object UNUSED (keyword), Lisp_Object value, |
1521 Error_Behavior UNUSED (errb)) | |
428 | 1522 { |
1523 /* #### should deal with ERRB */ | |
2367 | 1524 EXTERNAL_PROPERTY_LIST_LOOP_3 (range, data, value) |
428 | 1525 { |
1526 struct chartab_range dummy; | |
1527 | |
1528 if (CONSP (range)) | |
1529 { | |
1530 if (!CONSP (XCDR (range)) | |
1531 || !NILP (XCDR (XCDR (range)))) | |
563 | 1532 sferror ("Invalid range format", range); |
428 | 1533 decode_char_table_range (XCAR (range), &dummy); |
1534 decode_char_table_range (XCAR (XCDR (range)), &dummy); | |
1535 } | |
1536 else | |
1537 decode_char_table_range (range, &dummy); | |
1538 } | |
1539 | |
1540 return 1; | |
1541 } | |
1542 | |
1543 static Lisp_Object | |
1544 chartab_instantiate (Lisp_Object data) | |
1545 { | |
1546 Lisp_Object chartab; | |
1547 Lisp_Object type = Qgeneric; | |
1548 Lisp_Object dataval = Qnil; | |
1549 | |
1550 while (!NILP (data)) | |
1551 { | |
1552 Lisp_Object keyw = Fcar (data); | |
1553 Lisp_Object valw; | |
1554 | |
1555 data = Fcdr (data); | |
1556 valw = Fcar (data); | |
1557 data = Fcdr (data); | |
1558 if (EQ (keyw, Qtype)) | |
1559 type = valw; | |
1560 else if (EQ (keyw, Qdata)) | |
1561 dataval = valw; | |
1562 } | |
1563 | |
1564 chartab = Fmake_char_table (type); | |
1565 | |
1566 data = dataval; | |
1567 while (!NILP (data)) | |
1568 { | |
1569 Lisp_Object range = Fcar (data); | |
1570 Lisp_Object val = Fcar (Fcdr (data)); | |
1571 | |
1572 data = Fcdr (Fcdr (data)); | |
1573 if (CONSP (range)) | |
1574 { | |
1575 if (CHAR_OR_CHAR_INTP (XCAR (range))) | |
1576 { | |
867 | 1577 Ichar first = XCHAR_OR_CHAR_INT (Fcar (range)); |
1578 Ichar last = XCHAR_OR_CHAR_INT (Fcar (Fcdr (range))); | |
1579 Ichar i; | |
428 | 1580 |
1581 for (i = first; i <= last; i++) | |
1582 Fput_char_table (make_char (i), val, chartab); | |
1583 } | |
1584 else | |
2500 | 1585 ABORT (); |
428 | 1586 } |
1587 else | |
1588 Fput_char_table (range, val, chartab); | |
1589 } | |
1590 | |
1591 return chartab; | |
1592 } | |
1593 | |
1594 #ifdef MULE | |
1595 | |
1596 | |
1597 /************************************************************************/ | |
1598 /* Category Tables, specifically */ | |
1599 /************************************************************************/ | |
1600 | |
1601 DEFUN ("category-table-p", Fcategory_table_p, 1, 1, 0, /* | |
444 | 1602 Return t if OBJECT is a category table. |
428 | 1603 A category table is a type of char table used for keeping track of |
1604 categories. Categories are used for classifying characters for use | |
1605 in regexps -- you can refer to a category rather than having to use | |
1606 a complicated [] expression (and category lookups are significantly | |
1607 faster). | |
1608 | |
1609 There are 95 different categories available, one for each printable | |
1610 character (including space) in the ASCII charset. Each category | |
1611 is designated by one such character, called a "category designator". | |
1612 They are specified in a regexp using the syntax "\\cX", where X is | |
1613 a category designator. | |
1614 | |
1615 A category table specifies, for each character, the categories that | |
1616 the character is in. Note that a character can be in more than one | |
1617 category. More specifically, a category table maps from a character | |
1618 to either the value nil (meaning the character is in no categories) | |
1619 or a 95-element bit vector, specifying for each of the 95 categories | |
1620 whether the character is in that category. | |
1621 | |
1622 Special Lisp functions are provided that abstract this, so you do not | |
1623 have to directly manipulate bit vectors. | |
1624 */ | |
444 | 1625 (object)) |
428 | 1626 { |
444 | 1627 return (CHAR_TABLEP (object) && |
1628 XCHAR_TABLE_TYPE (object) == CHAR_TABLE_TYPE_CATEGORY) ? | |
428 | 1629 Qt : Qnil; |
1630 } | |
1631 | |
1632 static Lisp_Object | |
444 | 1633 check_category_table (Lisp_Object object, Lisp_Object default_) |
428 | 1634 { |
444 | 1635 if (NILP (object)) |
1636 object = default_; | |
1637 while (NILP (Fcategory_table_p (object))) | |
1638 object = wrong_type_argument (Qcategory_table_p, object); | |
1639 return object; | |
428 | 1640 } |
1641 | |
1642 int | |
867 | 1643 check_category_char (Ichar ch, Lisp_Object table, |
647 | 1644 int designator, int not_p) |
428 | 1645 { |
1646 REGISTER Lisp_Object temp; | |
1647 if (NILP (Fcategory_table_p (table))) | |
563 | 1648 wtaerror ("Expected category table", table); |
826 | 1649 temp = get_char_table (ch, table); |
428 | 1650 if (NILP (temp)) |
458 | 1651 return not_p; |
428 | 1652 |
1653 designator -= ' '; | |
458 | 1654 return bit_vector_bit (XBIT_VECTOR (temp), designator) ? !not_p : not_p; |
428 | 1655 } |
1656 | |
1657 DEFUN ("check-category-at", Fcheck_category_at, 2, 4, 0, /* | |
444 | 1658 Return t if category of the character at POSITION includes DESIGNATOR. |
1659 Optional third arg BUFFER specifies which buffer to use, and defaults | |
1660 to the current buffer. | |
1661 Optional fourth arg CATEGORY-TABLE specifies the category table to | |
1662 use, and defaults to BUFFER's category table. | |
428 | 1663 */ |
444 | 1664 (position, designator, buffer, category_table)) |
428 | 1665 { |
1666 Lisp_Object ctbl; | |
867 | 1667 Ichar ch; |
647 | 1668 int des; |
428 | 1669 struct buffer *buf = decode_buffer (buffer, 0); |
1670 | |
444 | 1671 CHECK_INT (position); |
428 | 1672 CHECK_CATEGORY_DESIGNATOR (designator); |
1673 des = XCHAR (designator); | |
788 | 1674 ctbl = check_category_table (category_table, buf->category_table); |
444 | 1675 ch = BUF_FETCH_CHAR (buf, XINT (position)); |
428 | 1676 return check_category_char (ch, ctbl, des, 0) ? Qt : Qnil; |
1677 } | |
1678 | |
1679 DEFUN ("char-in-category-p", Fchar_in_category_p, 2, 3, 0, /* | |
788 | 1680 Return non-nil if category of CHARACTER includes DESIGNATOR. |
444 | 1681 Optional third arg CATEGORY-TABLE specifies the category table to use, |
788 | 1682 and defaults to the current buffer's category table. |
428 | 1683 */ |
444 | 1684 (character, designator, category_table)) |
428 | 1685 { |
1686 Lisp_Object ctbl; | |
867 | 1687 Ichar ch; |
647 | 1688 int des; |
428 | 1689 |
1690 CHECK_CATEGORY_DESIGNATOR (designator); | |
1691 des = XCHAR (designator); | |
444 | 1692 CHECK_CHAR (character); |
1693 ch = XCHAR (character); | |
788 | 1694 ctbl = check_category_table (category_table, current_buffer->category_table); |
428 | 1695 return check_category_char (ch, ctbl, des, 0) ? Qt : Qnil; |
1696 } | |
1697 | |
1698 DEFUN ("category-table", Fcategory_table, 0, 1, 0, /* | |
444 | 1699 Return BUFFER's current category table. |
1700 BUFFER defaults to the current buffer. | |
428 | 1701 */ |
1702 (buffer)) | |
1703 { | |
1704 return decode_buffer (buffer, 0)->category_table; | |
1705 } | |
1706 | |
1707 DEFUN ("standard-category-table", Fstandard_category_table, 0, 0, 0, /* | |
1708 Return the standard category table. | |
1709 This is the one used for new buffers. | |
1710 */ | |
1711 ()) | |
1712 { | |
1713 return Vstandard_category_table; | |
1714 } | |
1715 | |
1716 DEFUN ("copy-category-table", Fcopy_category_table, 0, 1, 0, /* | |
444 | 1717 Return a new category table which is a copy of CATEGORY-TABLE. |
1718 CATEGORY-TABLE defaults to the standard category table. | |
428 | 1719 */ |
444 | 1720 (category_table)) |
428 | 1721 { |
1722 if (NILP (Vstandard_category_table)) | |
1723 return Fmake_char_table (Qcategory); | |
1724 | |
444 | 1725 category_table = |
1726 check_category_table (category_table, Vstandard_category_table); | |
1727 return Fcopy_char_table (category_table); | |
428 | 1728 } |
1729 | |
1730 DEFUN ("set-category-table", Fset_category_table, 1, 2, 0, /* | |
444 | 1731 Select CATEGORY-TABLE as the new category table for BUFFER. |
428 | 1732 BUFFER defaults to the current buffer if omitted. |
1733 */ | |
444 | 1734 (category_table, buffer)) |
428 | 1735 { |
1736 struct buffer *buf = decode_buffer (buffer, 0); | |
444 | 1737 category_table = check_category_table (category_table, Qnil); |
1738 buf->category_table = category_table; | |
428 | 1739 /* Indicate that this buffer now has a specified category table. */ |
1740 buf->local_var_flags |= XINT (buffer_local_flags.category_table); | |
444 | 1741 return category_table; |
428 | 1742 } |
1743 | |
1744 DEFUN ("category-designator-p", Fcategory_designator_p, 1, 1, 0, /* | |
444 | 1745 Return t if OBJECT is a category designator (a char in the range ' ' to '~'). |
428 | 1746 */ |
444 | 1747 (object)) |
428 | 1748 { |
444 | 1749 return CATEGORY_DESIGNATORP (object) ? Qt : Qnil; |
428 | 1750 } |
1751 | |
1752 DEFUN ("category-table-value-p", Fcategory_table_value_p, 1, 1, 0, /* | |
444 | 1753 Return t if OBJECT is a category table value. |
428 | 1754 Valid values are nil or a bit vector of size 95. |
1755 */ | |
444 | 1756 (object)) |
428 | 1757 { |
444 | 1758 return CATEGORY_TABLE_VALUEP (object) ? Qt : Qnil; |
428 | 1759 } |
1760 | |
1761 | |
1762 #define CATEGORYP(x) \ | |
1763 (CHARP (x) && XCHAR (x) >= 0x20 && XCHAR (x) <= 0x7E) | |
1764 | |
826 | 1765 #define CATEGORY_SET(c) get_char_table (c, current_buffer->category_table) |
428 | 1766 |
1767 /* Return 1 if CATEGORY_SET contains CATEGORY, else return 0. | |
1768 The faster version of `!NILP (Faref (category_set, category))'. */ | |
1769 #define CATEGORY_MEMBER(category, category_set) \ | |
1770 (bit_vector_bit(XBIT_VECTOR (category_set), category - 32)) | |
1771 | |
1772 /* Return 1 if there is a word boundary between two word-constituent | |
1773 characters C1 and C2 if they appear in this order, else return 0. | |
1774 Use the macro WORD_BOUNDARY_P instead of calling this function | |
1775 directly. */ | |
1776 | |
1777 int | |
867 | 1778 word_boundary_p (Ichar c1, Ichar c2) |
428 | 1779 { |
1780 Lisp_Object category_set1, category_set2; | |
1781 Lisp_Object tail; | |
1782 int default_result; | |
1783 | |
1784 #if 0 | |
1785 if (COMPOSITE_CHAR_P (c1)) | |
1786 c1 = cmpchar_component (c1, 0, 1); | |
1787 if (COMPOSITE_CHAR_P (c2)) | |
1788 c2 = cmpchar_component (c2, 0, 1); | |
1789 #endif | |
1790 | |
867 | 1791 if (EQ (ichar_charset (c1), ichar_charset (c2))) |
428 | 1792 { |
1793 tail = Vword_separating_categories; | |
1794 default_result = 0; | |
1795 } | |
1796 else | |
1797 { | |
1798 tail = Vword_combining_categories; | |
1799 default_result = 1; | |
1800 } | |
1801 | |
1802 category_set1 = CATEGORY_SET (c1); | |
1803 if (NILP (category_set1)) | |
1804 return default_result; | |
1805 category_set2 = CATEGORY_SET (c2); | |
1806 if (NILP (category_set2)) | |
1807 return default_result; | |
1808 | |
853 | 1809 for (; CONSP (tail); tail = XCDR (tail)) |
428 | 1810 { |
853 | 1811 Lisp_Object elt = XCAR (tail); |
428 | 1812 |
1813 if (CONSP (elt) | |
853 | 1814 && CATEGORYP (XCAR (elt)) |
1815 && CATEGORYP (XCDR (elt)) | |
1816 && CATEGORY_MEMBER (XCHAR (XCAR (elt)), category_set1) | |
1817 && CATEGORY_MEMBER (XCHAR (XCDR (elt)), category_set2)) | |
428 | 1818 return !default_result; |
1819 } | |
1820 return default_result; | |
1821 } | |
1822 #endif /* MULE */ | |
1823 | |
1824 | |
1825 void | |
1826 syms_of_chartab (void) | |
1827 { | |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
1828 INIT_LISP_OBJECT (char_table); |
442 | 1829 |
428 | 1830 #ifdef MULE |
5117
3742ea8250b5
Checking in final CVS version of workspace 'ben-lisp-object'
Ben Wing <ben@xemacs.org>
parents:
3025
diff
changeset
|
1831 INIT_LISP_OBJECT (char_table_entry); |
442 | 1832 |
563 | 1833 DEFSYMBOL (Qcategory_table_p); |
1834 DEFSYMBOL (Qcategory_designator_p); | |
1835 DEFSYMBOL (Qcategory_table_value_p); | |
428 | 1836 #endif /* MULE */ |
1837 | |
563 | 1838 DEFSYMBOL (Qchar_table); |
1839 DEFSYMBOL_MULTIWORD_PREDICATE (Qchar_tablep); | |
428 | 1840 |
1841 DEFSUBR (Fchar_table_p); | |
1842 DEFSUBR (Fchar_table_type_list); | |
1843 DEFSUBR (Fvalid_char_table_type_p); | |
1844 DEFSUBR (Fchar_table_type); | |
826 | 1845 DEFSUBR (Fchar_table_default); |
1846 DEFSUBR (Fset_char_table_default); | |
428 | 1847 DEFSUBR (Freset_char_table); |
1848 DEFSUBR (Fmake_char_table); | |
1849 DEFSUBR (Fcopy_char_table); | |
1850 DEFSUBR (Fget_char_table); | |
1851 DEFSUBR (Fget_range_char_table); | |
1852 DEFSUBR (Fvalid_char_table_value_p); | |
1853 DEFSUBR (Fcheck_valid_char_table_value); | |
1854 DEFSUBR (Fput_char_table); | |
826 | 1855 DEFSUBR (Fremove_char_table); |
428 | 1856 DEFSUBR (Fmap_char_table); |
1857 | |
1858 #ifdef MULE | |
1859 DEFSUBR (Fcategory_table_p); | |
1860 DEFSUBR (Fcategory_table); | |
1861 DEFSUBR (Fstandard_category_table); | |
1862 DEFSUBR (Fcopy_category_table); | |
1863 DEFSUBR (Fset_category_table); | |
1864 DEFSUBR (Fcheck_category_at); | |
1865 DEFSUBR (Fchar_in_category_p); | |
1866 DEFSUBR (Fcategory_designator_p); | |
1867 DEFSUBR (Fcategory_table_value_p); | |
1868 #endif /* MULE */ | |
1869 | |
1870 } | |
1871 | |
1872 void | |
1873 vars_of_chartab (void) | |
1874 { | |
1875 /* DO NOT staticpro this. It works just like Vweak_hash_tables. */ | |
1876 Vall_syntax_tables = Qnil; | |
452 | 1877 dump_add_weak_object_chain (&Vall_syntax_tables); |
428 | 1878 } |
1879 | |
1880 void | |
1881 structure_type_create_chartab (void) | |
1882 { | |
1883 struct structure_type *st; | |
1884 | |
1885 st = define_structure_type (Qchar_table, 0, chartab_instantiate); | |
1886 | |
1887 define_structure_type_keyword (st, Qtype, chartab_type_validate); | |
1888 define_structure_type_keyword (st, Qdata, chartab_data_validate); | |
1889 } | |
1890 | |
1891 void | |
1892 complex_vars_of_chartab (void) | |
1893 { | |
1894 #ifdef MULE | |
1895 /* Set this now, so first buffer creation can refer to it. */ | |
1896 /* Make it nil before calling copy-category-table | |
1897 so that copy-category-table will know not to try to copy from garbage */ | |
1898 Vstandard_category_table = Qnil; | |
1899 Vstandard_category_table = Fcopy_category_table (Qnil); | |
1900 staticpro (&Vstandard_category_table); | |
1901 | |
1902 DEFVAR_LISP ("word-combining-categories", &Vword_combining_categories /* | |
1903 List of pair (cons) of categories to determine word boundary. | |
1904 | |
1905 Emacs treats a sequence of word constituent characters as a single | |
1906 word (i.e. finds no word boundary between them) iff they belongs to | |
1907 the same charset. But, exceptions are allowed in the following cases. | |
1908 | |
444 | 1909 \(1) The case that characters are in different charsets is controlled |
428 | 1910 by the variable `word-combining-categories'. |
1911 | |
1912 Emacs finds no word boundary between characters of different charsets | |
1913 if they have categories matching some element of this list. | |
1914 | |
1915 More precisely, if an element of this list is a cons of category CAT1 | |
1916 and CAT2, and a multibyte character C1 which has CAT1 is followed by | |
1917 C2 which has CAT2, there's no word boundary between C1 and C2. | |
1918 | |
1919 For instance, to tell that ASCII characters and Latin-1 characters can | |
1920 form a single word, the element `(?l . ?l)' should be in this list | |
1921 because both characters have the category `l' (Latin characters). | |
1922 | |
444 | 1923 \(2) The case that character are in the same charset is controlled by |
428 | 1924 the variable `word-separating-categories'. |
1925 | |
1926 Emacs find a word boundary between characters of the same charset | |
1927 if they have categories matching some element of this list. | |
1928 | |
1929 More precisely, if an element of this list is a cons of category CAT1 | |
1930 and CAT2, and a multibyte character C1 which has CAT1 is followed by | |
1931 C2 which has CAT2, there's a word boundary between C1 and C2. | |
1932 | |
1933 For instance, to tell that there's a word boundary between Japanese | |
1934 Hiragana and Japanese Kanji (both are in the same charset), the | |
1935 element `(?H . ?C) should be in this list. | |
1936 */ ); | |
1937 | |
1938 Vword_combining_categories = Qnil; | |
1939 | |
1940 DEFVAR_LISP ("word-separating-categories", &Vword_separating_categories /* | |
1941 List of pair (cons) of categories to determine word boundary. | |
1942 See the documentation of the variable `word-combining-categories'. | |
1943 */ ); | |
1944 | |
1945 Vword_separating_categories = Qnil; | |
1946 #endif /* MULE */ | |
1947 } |