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