(kill-comment): Fixed by rewriting it with syntax-tables rather than regexps
[emacs.git] / src / category.c
blobeb1e8c9afaf210a8c2a12110d0bc90c5a878cb2f
1 /* GNU Emacs routines to deal with category tables.
2 Copyright (C) 1995, 1997 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 /* Here we handle three objects: category, category set, and category
24 table. Read comments in the file category.h to understand them. */
26 #include <config.h>
27 #include <ctype.h>
28 #include "lisp.h"
29 #include "buffer.h"
30 #include "charset.h"
31 #include "category.h"
33 /* The version number of the latest category table. Each category
34 table has a unique version number. It is assigned a new number
35 also when it is modified. When a regular expression is compiled
36 into the struct re_pattern_buffer, the version number of the
37 category table (of the current buffer) at that moment is also
38 embedded in the structure.
40 For the moment, we are not using this feature. */
41 static int category_table_version;
43 Lisp_Object Qcategory_table, Qcategoryp, Qcategorysetp, Qcategory_table_p;
45 /* Variables to determine word boundary. */
46 Lisp_Object Vword_combining_categories, Vword_separating_categories;
48 /* Temporary internal variable used in macro CHAR_HAS_CATEGORY. */
49 Lisp_Object _temp_category_set;
52 /* Category set staff. */
54 DEFUN ("make-category-set", Fmake_category_set, Smake_category_set, 1, 1, 0,
55 "Return a newly created category-set which contains CATEGORIES.\n\
56 CATEGORIES is a string of category mnemonics.\n\
57 The value is a bool-vector which has t at the indices corresponding to\n\
58 those categories.")
59 (categories)
60 Lisp_Object categories;
62 Lisp_Object val;
63 int len;
65 CHECK_STRING (categories, 0);
66 val = MAKE_CATEGORY_SET;
68 if (STRING_MULTIBYTE (categories))
69 error ("Multibyte string in make-category-set");
71 len = XSTRING (categories)->size;
72 while (--len >= 0)
74 Lisp_Object category;
76 XSETFASTINT (category, XSTRING (categories)->data[len]);
77 CHECK_CATEGORY (category, 0);
78 SET_CATEGORY_SET (val, category, Qt);
80 return val;
84 /* Category staff. */
86 Lisp_Object check_category_table ();
88 DEFUN ("define-category", Fdefine_category, Sdefine_category, 2, 3, 0,
89 "Define CHAR as a category which is described by DOCSTRING.\n\
90 CHAR should be an ASCII printing character in the range ` ' to `~'.\n\
91 DOCSTRING is a documentation string of the category.\n\
92 The category is defined only in category table TABLE, which defaults to\n\
93 the current buffer's category table.")
94 (category, docstring, table)
95 Lisp_Object category, docstring, table;
97 CHECK_CATEGORY (category, 0);
98 CHECK_STRING (docstring, 1);
99 table = check_category_table (table);
101 if (!NILP (CATEGORY_DOCSTRING (table, XFASTINT (category))))
102 error ("Category `%c' is already defined", XFASTINT (category));
103 CATEGORY_DOCSTRING (table, XFASTINT (category)) = docstring;
105 return Qnil;
108 DEFUN ("category-docstring", Fcategory_docstring, Scategory_docstring, 1, 2, 0,
109 "Return the documentation string of CATEGORY, as defined in CATEGORY-TABLE.")
110 (category, table)
111 Lisp_Object category, table;
113 Lisp_Object doc;
115 CHECK_CATEGORY (category, 0);
116 table = check_category_table (table);
118 return CATEGORY_DOCSTRING (table, XFASTINT (category));
121 DEFUN ("get-unused-category", Fget_unused_category, Sget_unused_category,
122 0, 1, 0,
123 "Return a category which is not yet defined in CATEGORY-TABLE.\n\
124 If no category remains available, return nil.\n\
125 The optional argument CATEGORY-TABLE specifies which category table\n\
126 to modify; it defaults to the current buffer's category table.")
127 (table)
128 Lisp_Object table;
130 int i;
131 Lisp_Object docstring_vector;
133 table = check_category_table (table);
135 for (i = ' '; i <= '~'; i++)
136 if (NILP (CATEGORY_DOCSTRING (table, i)))
137 return make_number (i);
139 return Qnil;
143 /* Category-table staff. */
145 DEFUN ("category-table-p", Fcategory_table_p, Scategory_table_p, 1, 1, 0,
146 "Return t if ARG is a category table.")
147 (arg)
148 Lisp_Object arg;
150 if (CHAR_TABLE_P (arg)
151 && EQ (XCHAR_TABLE (arg)->purpose, Qcategory_table))
152 return Qt;
153 return Qnil;
156 /* If TABLE is nil, return the current category table. If TABLE is
157 not nil, check the validity of TABLE as a category table. If
158 valid, return TABLE itself, but if not valid, signal an error of
159 wrong-type-argument. */
161 Lisp_Object
162 check_category_table (table)
163 Lisp_Object table;
165 register Lisp_Object tem;
166 if (NILP (table))
167 return current_buffer->category_table;
168 while (tem = Fcategory_table_p (table), NILP (tem))
169 table = wrong_type_argument (Qcategory_table_p, table);
170 return table;
173 DEFUN ("category-table", Fcategory_table, Scategory_table, 0, 0, 0,
174 "Return the current category table.\n\
175 This is the one specified by the current buffer.")
178 return current_buffer->category_table;
181 DEFUN ("standard-category-table", Fstandard_category_table,
182 Sstandard_category_table, 0, 0, 0,
183 "Return the standard category table.\n\
184 This is the one used for new buffers.")
187 return Vstandard_category_table;
190 /* Return a copy of category table TABLE. We can't simply use the
191 function copy-sequence because no contents should be shared between
192 the original and the copy. This function is called recursively by
193 binding TABLE to a sub char table. */
195 Lisp_Object
196 copy_category_table (table)
197 Lisp_Object table;
199 Lisp_Object tmp;
200 int i, to;
202 if (!NILP (XCHAR_TABLE (table)->top))
204 /* TABLE is a top level char table.
205 At first, make a copy of tree structure of the table. */
206 table = Fcopy_sequence (table);
208 /* Then, copy elements for single byte characters one by one. */
209 for (i = 0; i < CHAR_TABLE_SINGLE_BYTE_SLOTS; i++)
210 if (!NILP (tmp = XCHAR_TABLE (table)->contents[i]))
211 XCHAR_TABLE (table)->contents[i] = Fcopy_sequence (tmp);
212 to = CHAR_TABLE_ORDINARY_SLOTS;
214 /* Also copy the first (and sole) extra slot. It is a vector
215 containing docstring of each category. */
216 Fset_char_table_extra_slot
217 (table, make_number (0),
218 Fcopy_sequence (Fchar_table_extra_slot (table, make_number (0))));
220 else
222 i = 32;
223 to = SUB_CHAR_TABLE_ORDINARY_SLOTS;
226 /* If the table has non-nil default value, copy it. */
227 if (!NILP (tmp = XCHAR_TABLE (table)->defalt))
228 XCHAR_TABLE (table)->defalt = Fcopy_sequence (tmp);
230 /* At last, copy the remaining elements while paying attention to a
231 sub char table. */
232 for (; i < to; i++)
233 if (!NILP (tmp = XCHAR_TABLE (table)->contents[i]))
234 XCHAR_TABLE (table)->contents[i]
235 = (SUB_CHAR_TABLE_P (tmp)
236 ? copy_category_table (tmp) : Fcopy_sequence (tmp));
238 return table;
241 DEFUN ("copy-category-table", Fcopy_category_table, Scopy_category_table,
242 0, 1, 0,
243 "Construct a new category table and return it.\n\
244 It is a copy of the TABLE, which defaults to the standard category table.")
245 (table)
246 Lisp_Object table;
248 if (!NILP (table))
249 check_category_table (table);
250 else
251 table = Vstandard_category_table;
253 return copy_category_table (table);
256 DEFUN ("set-category-table", Fset_category_table, Sset_category_table, 1, 1, 0,
257 "Specify TABLE as the category table for the current buffer.")
258 (table)
259 Lisp_Object table;
261 table = check_category_table (table);
262 current_buffer->category_table = table;
263 /* Indicate that this buffer now has a specified category table. */
264 current_buffer->local_var_flags
265 |= XFASTINT (buffer_local_flags.category_table);
266 return table;
270 DEFUN ("char-category-set", Fchar_category_set, Schar_category_set, 1, 1, 0,
271 "Return the category set of CHAR.")
272 (ch)
273 Lisp_Object ch;
275 Lisp_Object val;
276 int charset;
277 unsigned char c1, c2;
279 CHECK_NUMBER (ch, 0);
280 return CATEGORY_SET (XFASTINT (ch));
283 DEFUN ("category-set-mnemonics", Fcategory_set_mnemonics,
284 Scategory_set_mnemonics, 1, 1, 0,
285 "Return a string containing mnemonics of the categories in CATEGORY-SET.\n\
286 CATEGORY-SET is a bool-vector, and the categories \"in\" it are those\n\
287 that are indexes where t occurs the bool-vector.\n\
288 The return value is a string containing those same categories.")
289 (category_set)
290 Lisp_Object category_set;
292 int i, j;
293 char str[96];
295 CHECK_CATEGORY_SET (category_set, 0);
297 j = 0;
298 for (i = 32; i < 127; i++)
299 if (CATEGORY_MEMBER (i, category_set))
300 str[j++] = i;
301 str[j] = '\0';
303 return build_string (str);
306 /* Modify all category sets stored under sub char-table TABLE so that
307 they contain (SET_VALUE is t) or don't contain (SET_VALUE is nil)
308 CATEGORY. */
310 void
311 modify_lower_category_set (table, category, set_value)
312 Lisp_Object table, category, set_value;
314 Lisp_Object val;
315 int i;
317 val = XCHAR_TABLE (table)->defalt;
318 if (!CATEGORY_SET_P (val))
319 val = MAKE_CATEGORY_SET;
320 SET_CATEGORY_SET (val, category, set_value);
321 XCHAR_TABLE (table)->defalt = val;
323 for (i = 32; i < SUB_CHAR_TABLE_ORDINARY_SLOTS; i++)
325 val = XCHAR_TABLE (table)->contents[i];
327 if (CATEGORY_SET_P (val))
328 SET_CATEGORY_SET (val, category, set_value);
329 else if (SUB_CHAR_TABLE_P (val))
330 modify_lower_category_set (val, category, set_value);
334 void
335 set_category_set (category_set, category, val)
336 Lisp_Object category_set, category, val;
338 do {
339 int idx = XINT (category) / 8;
340 unsigned char bits = 1 << (XINT (category) % 8);
342 if (NILP (val))
343 XCATEGORY_SET (category_set)->data[idx] &= ~bits;
344 else
345 XCATEGORY_SET (category_set)->data[idx] |= bits;
346 } while (0);
349 DEFUN ("modify-category-entry", Fmodify_category_entry,
350 Smodify_category_entry, 2, 4, 0,
351 "Modify the category set of CHARACTER by adding CATEGORY to it.\n\
352 The category is changed only for table TABLE, which defaults to\n\
353 the current buffer's category table.\n\
354 If optional fourth argument RESET is non-nil,\n\
355 then delete CATEGORY from the category set instead of adding it.")
356 (character, category, table, reset)
357 Lisp_Object character, category, table, reset;
359 int c, charset, c1, c2;
360 Lisp_Object set_value; /* Actual value to be set in category sets. */
361 Lisp_Object val, category_set;
363 CHECK_NUMBER (character, 0);
364 c = XINT (character);
365 CHECK_CATEGORY (category, 1);
366 table = check_category_table (table);
368 if (NILP (CATEGORY_DOCSTRING (table, XFASTINT (category))))
369 error ("Undefined category: %c", XFASTINT (category));
371 set_value = NILP (reset) ? Qt : Qnil;
373 if (c < CHAR_TABLE_SINGLE_BYTE_SLOTS)
375 val = XCHAR_TABLE (table)->contents[c];
376 if (!CATEGORY_SET_P (val))
377 XCHAR_TABLE (table)->contents[c] = (val = MAKE_CATEGORY_SET);
378 SET_CATEGORY_SET (val, category, set_value);
379 return Qnil;
382 SPLIT_NON_ASCII_CHAR (c, charset, c1, c2);
384 /* The top level table. */
385 val = XCHAR_TABLE (table)->contents[charset + 128];
386 if (CATEGORY_SET_P (val))
387 category_set = val;
388 else if (!SUB_CHAR_TABLE_P (val))
390 category_set = val = MAKE_CATEGORY_SET;
391 XCHAR_TABLE (table)->contents[charset + 128] = category_set;
394 if (c1 <= 0)
396 /* Only a charset is specified. */
397 if (SUB_CHAR_TABLE_P (val))
398 /* All characters in CHARSET should be the same as for having
399 CATEGORY or not. */
400 modify_lower_category_set (val, category, set_value);
401 else
402 SET_CATEGORY_SET (category_set, category, set_value);
403 return Qnil;
406 /* The second level table. */
407 if (!SUB_CHAR_TABLE_P (val))
409 val = make_sub_char_table (Qnil);
410 XCHAR_TABLE (table)->contents[charset + 128] = val;
411 /* We must set default category set of CHARSET in `defalt' slot. */
412 XCHAR_TABLE (val)->defalt = category_set;
414 table = val;
416 val = XCHAR_TABLE (table)->contents[c1];
417 if (CATEGORY_SET_P (val))
418 category_set = val;
419 else if (!SUB_CHAR_TABLE_P (val))
421 category_set = val = Fcopy_sequence (XCHAR_TABLE (table)->defalt);
422 XCHAR_TABLE (table)->contents[c1] = category_set;
425 if (c2 <= 0)
427 if (SUB_CHAR_TABLE_P (val))
428 /* All characters in C1 group of CHARSET should be the same as
429 for CATEGORY. */
430 modify_lower_category_set (val, category, set_value);
431 else
432 SET_CATEGORY_SET (category_set, category, set_value);
433 return Qnil;
436 /* The third (bottom) level table. */
437 if (!SUB_CHAR_TABLE_P (val))
439 val = make_sub_char_table (Qnil);
440 XCHAR_TABLE (table)->contents[c1] = val;
441 /* We must set default category set of CHARSET and C1 in
442 `defalt' slot. */
443 XCHAR_TABLE (val)->defalt = category_set;
445 table = val;
447 val = XCHAR_TABLE (table)->contents[c2];
448 if (CATEGORY_SET_P (val))
449 category_set = val;
450 else if (!SUB_CHAR_TABLE_P (val))
452 category_set = Fcopy_sequence (XCHAR_TABLE (table)->defalt);
453 XCHAR_TABLE (table)->contents[c2] = category_set;
455 else
456 /* This should never happen. */
457 error ("Invalid category table");
459 SET_CATEGORY_SET (category_set, category, set_value);
461 return Qnil;
464 /* Dump category table to buffer in human-readable format */
466 static void
467 describe_category (value)
468 Lisp_Object value;
470 Lisp_Object mnemonics;
472 Findent_to (make_number (16), make_number (1));
474 if (NILP (value))
476 insert_string ("default\n");
477 return;
480 if (CHAR_TABLE_P (value))
482 insert_string ("deeper char-table ...\n");
483 return;
486 if (!CATEGORY_SET_P (value))
488 insert_string ("invalid\n");
489 return;
492 mnemonics = Fcategory_set_mnemonics (value);
493 insert_from_string (mnemonics, 0, 0, XSTRING (mnemonics)->size,
494 STRING_BYTES (XSTRING (mnemonics)), 0);
495 insert_string ("\n");
496 return;
499 static Lisp_Object
500 describe_category_1 (vector)
501 Lisp_Object vector;
503 struct buffer *old = current_buffer;
504 set_buffer_internal (XBUFFER (Vstandard_output));
505 describe_vector (vector, Qnil, describe_category, 0, Qnil, Qnil,
506 (int *)0, 0);
508 int i;
509 Lisp_Object docs = XCHAR_TABLE (vector)->extras[0];
510 Lisp_Object elt;
512 if (!VECTORP (docs) || XVECTOR (docs)->size != 95)
514 insert_string ("Invalid first extra slot in this char table\n");
515 return Qnil;
518 insert_string ("Meanings of mnemonice characters are:\n");
519 for (i = 0; i < 95; i++)
521 elt = XVECTOR (docs)->contents[i];
522 if (NILP (elt))
523 continue;
525 insert_char (i + 32);
526 insert (": ", 2);
527 insert_from_string (elt, 0, 0, XSTRING (elt)->size,
528 STRING_BYTES (XSTRING (elt)), 0);
529 insert ("\n", 1);
533 while (! NILP (XCHAR_TABLE (vector)->parent))
535 vector = XCHAR_TABLE (vector)->parent;
536 insert_string ("\nThe parent category table is:");
537 describe_vector (vector, Qnil, describe_category, 0, Qnil, Qnil,
538 (int *) 0, 0);
541 call0 (intern ("help-mode"));
542 set_buffer_internal (old);
543 return Qnil;
546 DEFUN ("describe-categories", Fdescribe_categories, Sdescribe_categories, 0, 0, "",
547 "Describe the category specifications in the current category table.\n\
548 The descriptions are inserted in a buffer, which is then displayed.")
551 internal_with_output_to_temp_buffer
552 ("*Help*", describe_category_1, current_buffer->category_table);
554 return Qnil;
557 /* Return 1 if there is a word boundary between two word-constituent
558 characters C1 and C2 if they appear in this order, else return 0.
559 Use the macro WORD_BOUNDARY_P instead of calling this function
560 directly. */
563 word_boundary_p (c1, c2)
564 int c1, c2;
566 Lisp_Object category_set1, category_set2;
567 Lisp_Object tail;
568 int default_result;
570 if (COMPOSITE_CHAR_P (c1))
571 c1 = cmpchar_component (c1, 0, 1);
572 if (COMPOSITE_CHAR_P (c2))
573 c2 = cmpchar_component (c2, 0, 1);
575 if (CHAR_CHARSET (c1) == CHAR_CHARSET (c2))
577 tail = Vword_separating_categories;
578 default_result = 0;
580 else
582 tail = Vword_combining_categories;
583 default_result = 1;
586 category_set1 = CATEGORY_SET (c1);
587 if (NILP (category_set1))
588 return default_result;
589 category_set2 = CATEGORY_SET (c2);
590 if (NILP (category_set2))
591 return default_result;
593 for (; CONSP (tail); tail = XCDR (tail))
595 Lisp_Object elt = XCAR (tail);
597 if (CONSP (elt)
598 && CATEGORYP (XCAR (elt))
599 && CATEGORYP (XCDR (elt))
600 && CATEGORY_MEMBER (XFASTINT (XCAR (elt)), category_set1)
601 && CATEGORY_MEMBER (XFASTINT (XCDR (elt)), category_set2))
602 return !default_result;
604 return default_result;
608 void
609 init_category_once ()
611 /* This has to be done here, before we call Fmake_char_table. */
612 Qcategory_table = intern ("category-table");
613 staticpro (&Qcategory_table);
615 /* Intern this now in case it isn't already done.
616 Setting this variable twice is harmless.
617 But don't staticpro it here--that is done in alloc.c. */
618 Qchar_table_extra_slots = intern ("char-table-extra-slots");
620 /* Now we are ready to set up this property, so we can
621 create category tables. */
622 Fput (Qcategory_table, Qchar_table_extra_slots, make_number (2));
624 Vstandard_category_table = Fmake_char_table (Qcategory_table, Qnil);
625 /* Set a category set which contains nothing to the default. */
626 XCHAR_TABLE (Vstandard_category_table)->defalt = MAKE_CATEGORY_SET;
627 Fset_char_table_extra_slot (Vstandard_category_table, make_number (0),
628 Fmake_vector (make_number (95), Qnil));
631 void
632 syms_of_category ()
634 Qcategoryp = intern ("categoryp");
635 staticpro (&Qcategoryp);
636 Qcategorysetp = intern ("categorysetp");
637 staticpro (&Qcategorysetp);
638 Qcategory_table_p = intern ("category-table-p");
639 staticpro (&Qcategory_table_p);
641 DEFVAR_LISP ("word-combining-categories", &Vword_combining_categories,
642 "List of pair (cons) of categories to determine word boundary.\n\
644 Emacs treats a sequence of word constituent characters as a single\n\
645 word (i.e. finds no word boundary between them) iff they belongs to\n\
646 the same charset. But, exceptions are allowed in the following cases.\n\
648 \(1) The case that characters are in different charsets is controlled\n\
649 by the variable `word-combining-categories'.\n\
651 Emacs finds no word boundary between characters of different charsets\n\
652 if they have categories matching some element of this list.\n\
654 More precisely, if an element of this list is a cons of category CAT1\n\
655 and CAT2, and a multibyte character C1 which has CAT1 is followed by\n\
656 C2 which has CAT2, there's no word boundary between C1 and C2.\n\
658 For instance, to tell that ASCII characters and Latin-1 characters can\n\
659 form a single word, the element `(?l . ?l)' should be in this list\n\
660 because both characters have the category `l' (Latin characters).\n\
662 \(2) The case that character are in the same charset is controlled by\n\
663 the variable `word-separating-categories'.\n\
665 Emacs find a word boundary between characters of the same charset\n\
666 if they have categories matching some element of this list.\n\
668 More precisely, if an element of this list is a cons of category CAT1\n\
669 and CAT2, and a multibyte character C1 which has CAT1 is followed by\n\
670 C2 which has CAT2, there's a word boundary between C1 and C2.\n\
672 For instance, to tell that there's a word boundary between Japanese\n\
673 Hiragana and Japanese Kanji (both are in the same charset), the\n\
674 element `(?H . ?C) should be in this list.");
676 Vword_combining_categories = Qnil;
678 DEFVAR_LISP ("word-separating-categories", &Vword_separating_categories,
679 "List of pair (cons) of categories to determine word boundary.\n\
680 See the documentation of the variable `word-combining-categories'.");
682 Vword_separating_categories = Qnil;
684 defsubr (&Smake_category_set);
685 defsubr (&Sdefine_category);
686 defsubr (&Scategory_docstring);
687 defsubr (&Sget_unused_category);
688 defsubr (&Scategory_table_p);
689 defsubr (&Scategory_table);
690 defsubr (&Sstandard_category_table);
691 defsubr (&Scopy_category_table);
692 defsubr (&Sset_category_table);
693 defsubr (&Schar_category_set);
694 defsubr (&Scategory_set_mnemonics);
695 defsubr (&Smodify_category_entry);
696 defsubr (&Sdescribe_categories);
698 category_table_version = 0;