Initial revision
[emacs.git] / src / category.c
blobcd87abd5637acbcc2a778814a4367fe0f7357842
1 /* GNU Emacs routines to deal with category tables.
2 Ver.1.0
3 Copyright (C) 1995 Free Software Foundation, Inc.
4 Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* Here we handle three objects: category, category set, and category
25 table. Read comments in the file category.h to understand them. */
27 #include <config.h>
28 #include <ctype.h>
29 #include "lisp.h"
30 #include "buffer.h"
31 #include "charset.h"
32 #include "category.h"
34 /* The version number of the latest category table. Each category
35 table has a unique version number. It is assigned a new number
36 also when it is modified. When a regular expression is compiled
37 into the struct re_pattern_buffer, the version number of the
38 category table (of the current buffer) at that moment is also
39 embedded in the structure.
41 For the moment, we are not using this feature. */
42 static int category_table_version;
44 Lisp_Object Qcategory_table, Qcategoryp, Qcategorysetp, Qcategory_table_p;
46 /* Variables to determine word boundary. */
47 Lisp_Object Vword_combining_categories, Vword_separating_categories;
49 /* Temporary internal variable used in macro CHAR_HAS_CATEGORY. */
50 Lisp_Object _temp_category_set;
53 /* Category set staff. */
55 DEFUN ("make-category-set", Fmake_category_set, Smake_category_set, 1, 1, 0,
56 "Return a newly created category-set which contains CATEGORIES.\n\
57 CATEGORIES is a string of category mnemonics.")
58 (categories)
59 Lisp_Object categories;
61 Lisp_Object val;
62 int len;
64 CHECK_STRING (categories, 0);
65 val = MAKE_CATEGORY_SET;
67 len = XSTRING (categories)->size;
68 while (--len >= 0)
70 Lisp_Object category = make_number (XSTRING (categories)->data[len]);
72 CHECK_CATEGORY (category, 0);
73 SET_CATEGORY_SET (val, category, Qt);
75 return val;
79 /* Category staff. */
81 Lisp_Object check_category_table ();
83 DEFUN ("define-category", Fdefine_category, Sdefine_category, 2, 3, 0,
84 "Define CHAR as a category which is described by DOCSTRING.\n\
85 CHAR should be a visible letter of ` ' thru `~'.\n\
86 DOCSTRING is a documentation string of the category.\n\
87 The category is defined only in category table TABLE, which defaults to\n\
88 the current buffer's category table.")
89 (category, docstring, table)
90 Lisp_Object category, docstring, table;
92 CHECK_CATEGORY (category, 0);
93 CHECK_STRING (docstring, 1);
94 table = check_category_table (table);
96 if (!NILP (CATEGORY_DOCSTRING (table, XFASTINT (category))))
97 error ("Category `%c' is already defined", XFASTINT (category));
98 CATEGORY_DOCSTRING (table, XFASTINT (category)) = docstring;
100 return Qnil;
103 DEFUN ("category-docstring", Fcategory_docstring, Scategory_docstring, 1, 2, 0,
104 "Return a documentation string of CATEGORY.\n\
105 Optional second arg specifies CATEGORY-TABLE,\n\
106 which defaults to the current buffer's category table.")
107 (category, table)
108 Lisp_Object category, table;
110 Lisp_Object doc;
112 CHECK_CATEGORY (category, 0);
113 table = check_category_table (table);
115 return CATEGORY_DOCSTRING (table, XFASTINT (category));
118 DEFUN ("get-unused-category", Fget_unused_category, Sget_unused_category,
119 0, 1, 0,
120 "Return a category which is not yet defined.\n\
121 If total number of categories has reached the limit (95), return nil.\n\
122 Optional argument specifies CATEGORY-TABLE,\n\
123 which defaults to the current buffer's category table.")
124 (table)
125 Lisp_Object table;
127 int i;
128 Lisp_Object docstring_vector;
130 table = check_category_table (table);
132 for (i = ' '; i <= '~'; i++)
133 if (NILP (CATEGORY_DOCSTRING (table, i)))
134 return make_number (i);
136 return Qnil;
140 /* Category-table staff. */
142 DEFUN ("category-table-p", Fcategory_table_p, Scategory_table_p, 1, 1, 0,
143 "Return t if ARG is a category table.")
144 (arg)
145 Lisp_Object arg;
147 if (CHAR_TABLE_P (arg)
148 && EQ (XCHAR_TABLE (arg)->purpose, Qcategory_table))
149 return Qt;
150 return Qnil;
153 /* If TABLE is nil, return the current category table. If TABLE is
154 not nil, check the validity of TABLE as a category table. If
155 valid, return TABLE itself, but if not valid, signal an error of
156 wrong-type-argument. */
158 Lisp_Object
159 check_category_table (table)
160 Lisp_Object table;
162 register Lisp_Object tem;
163 if (NILP (table))
164 return current_buffer->category_table;
165 while (tem = Fcategory_table_p (table), NILP (tem))
166 table = wrong_type_argument (Qcategory_table_p, table);
167 return table;
170 DEFUN ("category-table", Fcategory_table, Scategory_table, 0, 0, 0,
171 "Return the current category table.\n\
172 This is the one specified by the current buffer.")
175 return current_buffer->category_table;
178 DEFUN ("standard-category-table", Fstandard_category_table,
179 Sstandard_category_table, 0, 0, 0,
180 "Return the standard category table.\n\
181 This is the one used for new buffers.")
184 return Vstandard_category_table;
187 /* Return a copy of category table TABLE. We can't simply use the
188 function copy-sequence because no contents should be shared between
189 the original and the copy. This function is called recursively by
190 biding TABLE to a sub char table. */
192 Lisp_Object
193 copy_category_table (table)
194 Lisp_Object table;
196 Lisp_Object tmp;
197 int i, to;
199 if (!NILP (XCHAR_TABLE (table)->top))
201 /* TABLE is a top level char table.
202 At first, make a copy of tree structure of the table. */
203 table = Fcopy_sequence (table);
205 /* Then, copy elements for single byte characters one by one. */
206 for (i = 0; i < CHAR_TABLE_SINGLE_BYTE_SLOTS; i++)
207 if (!NILP (tmp = XCHAR_TABLE (table)->contents[i]))
208 XCHAR_TABLE (table)->contents[i] = Fcopy_sequence (tmp);
209 to = CHAR_TABLE_ORDINARY_SLOTS;
211 else
213 i = 32;
214 to = SUB_CHAR_TABLE_ORDINARY_SLOTS;
217 /* If the table has non-nil default value, copy it. */
218 if (!NILP (tmp = XCHAR_TABLE (table)->defalt))
219 XCHAR_TABLE (table)->defalt = Fcopy_sequence (tmp);
221 /* At last, copy the remaining elements while paying attention to a
222 sub char table. */
223 for (; i < to; i++)
224 if (!NILP (tmp = XCHAR_TABLE (table)->contents[i]))
225 XCHAR_TABLE (table)->contents[i]
226 = (SUB_CHAR_TABLE_P (tmp)
227 ? copy_category_table (tmp) : Fcopy_sequence (tmp));
229 return table;
232 DEFUN ("copy-category-table", Fcopy_category_table, Scopy_category_table,
233 0, 1, 0,
234 "Construct a new category table and return it.\n\
235 It is a copy of the TABLE, which defaults to the standard category table.")
236 (table)
237 Lisp_Object table;
239 if (!NILP (table))
240 check_category_table (table);
241 else
242 table = Vstandard_category_table;
244 return copy_category_table (table, 1);
247 DEFUN ("set-category-table", Fset_category_table, Sset_category_table, 1, 1, 0,
248 "Select a new category table for the current buffer.\n\
249 One argument, a category table.")
250 (table)
251 Lisp_Object table;
253 table = check_category_table (table);
254 current_buffer->category_table = table;
255 /* Indicate that this buffer now has a specified category table. */
256 current_buffer->local_var_flags
257 |= XFASTINT (buffer_local_flags.category_table);
258 return table;
262 DEFUN ("char-category-set", Fchar_category_set, Schar_category_set, 1, 1, 0,
263 "Return a category set of CHAR.")
264 (ch)
265 Lisp_Object ch;
267 Lisp_Object val;
268 int charset;
269 unsigned char c1, c2;
271 CHECK_NUMBER (ch, 0);
272 return CATEGORY_SET (XFASTINT (ch));
275 DEFUN ("category-set-mnemonics", Fcategory_set_mnemonics,
276 Scategory_set_mnemonics, 1, 1, 0,
277 "Return a string of mnemonics of all categories in CATEGORY-SET.")
278 (category_set)
279 Lisp_Object category_set;
281 int i, j;
282 char str[96];
284 CHECK_CATEGORY_SET (category_set, 0);
286 j = 0;
287 for (i = 32; i < 127; i++)
288 if (CATEGORY_MEMBER (i, category_set))
289 str[j++] = i;
290 str[j] = '\0';
292 return build_string (str);
295 /* Modify all category sets stored under sub char-table TABLE so that
296 they contain (SET_VALUE is t) or don't contain (SET_VALUE is nil)
297 CATEGORY. */
299 void
300 modify_lower_category_set (table, category, set_value)
301 Lisp_Object table, category, set_value;
303 Lisp_Object val;
304 int i;
306 if (NILP (XCHAR_TABLE (table)->defalt))
308 val = MAKE_CATEGORY_SET;
309 SET_CATEGORY_SET (val, category, set_value);
310 XCHAR_TABLE (table)->defalt = val;
313 for (i = 32; i < SUB_CHAR_TABLE_ORDINARY_SLOTS; i++)
315 val = XCHAR_TABLE (table)->contents[i];
317 if (CATEGORY_SET_P (val))
318 SET_CATEGORY_SET (val, category, set_value);
319 else if (SUB_CHAR_TABLE_P (val))
320 modify_lower_category_set (val, category, set_value);
324 void
325 set_category_set (category_set, category, val)
326 Lisp_Object category_set, category, val;
328 do {
329 int idx = XINT (category) / 8;
330 unsigned char bits = 1 << (XINT (category) % 8);
332 if (NILP (val))
333 XCATEGORY_SET (category_set)->data[idx] &= ~bits;
334 else
335 XCATEGORY_SET (category_set)->data[idx] |= bits;
336 } while (0);
339 DEFUN ("modify-category-entry", Fmodify_category_entry,
340 Smodify_category_entry, 2, 4, 0,
341 "Modify the category set of CHAR by adding CATEGORY to it.\n\
342 The category is changed only for table TABLE, which defaults to\n\
343 the current buffer's category table.\n\
344 If optional forth argument RESET is non NIL,\n\
345 CATEGORY is deleted from the category set instead of being added.")
346 (ch, category, table, reset)
347 Lisp_Object ch, category, table, reset;
349 int c, charset, c1, c2;
350 Lisp_Object set_value; /* Actual value to be set in category sets. */
351 Lisp_Object val, category_set;
353 CHECK_NUMBER (ch, 0);
354 c = XINT (ch);
355 CHECK_CATEGORY (category, 1);
356 table = check_category_table (table);
358 if (NILP (CATEGORY_DOCSTRING (table, XFASTINT (category))))
359 error ("Undefined category: %c", XFASTINT (category));
361 set_value = NILP (reset) ? Qt : Qnil;
363 if (c < CHAR_TABLE_SINGLE_BYTE_SLOTS)
365 val = XCHAR_TABLE (table)->contents[c];
366 if (!CATEGORY_SET_P (val))
367 XCHAR_TABLE (table)->contents[c] = (val = MAKE_CATEGORY_SET);
368 SET_CATEGORY_SET (val, category, set_value);
369 return Qnil;
372 SPLIT_NON_ASCII_CHAR (c, charset, c1, c2);
374 /* The top level table. */
375 val = XCHAR_TABLE (table)->contents[charset + 128];
376 if (CATEGORY_SET_P (val))
377 category_set = val;
378 else if (!SUB_CHAR_TABLE_P (val))
380 category_set = val = MAKE_CATEGORY_SET;
381 XCHAR_TABLE (table)->contents[charset + 128] = category_set;
384 if (c1 <= 0)
386 /* Only a charset is specified. */
387 if (SUB_CHAR_TABLE_P (val))
388 /* All characters in CHARSET should be the same as for having
389 CATEGORY or not. */
390 modify_lower_category_set (val, category, set_value);
391 else
392 SET_CATEGORY_SET (category_set, category, set_value);
393 return Qnil;
396 /* The second level table. */
397 if (!SUB_CHAR_TABLE_P (val))
399 val = make_sub_char_table (Qnil);
400 XCHAR_TABLE (table)->contents[charset + 128] = val;
401 /* We must set default category set of CHARSET in `defalt' slot. */
402 XCHAR_TABLE (val)->defalt = category_set;
404 table = val;
406 val = XCHAR_TABLE (table)->contents[c1];
407 if (CATEGORY_SET_P (val))
408 category_set = val;
409 else if (!SUB_CHAR_TABLE_P (val))
411 category_set = val = Fcopy_sequence (XCHAR_TABLE (table)->defalt);
412 XCHAR_TABLE (table)->contents[c1] = category_set;
415 if (c2 <= 0)
417 if (SUB_CHAR_TABLE_P (val))
418 /* All characters in C1 group of CHARSET should be the same as
419 for CATEGORY. */
420 modify_lower_category_set (val, category, set_value);
421 else
422 SET_CATEGORY_SET (category_set, category, set_value);
423 return Qnil;
426 /* The third (bottom) level table. */
427 if (!SUB_CHAR_TABLE_P (val))
429 val = make_sub_char_table (Qnil, Qnil);
430 XCHAR_TABLE (table)->contents[c1] = val;
431 /* We must set default category set of CHARSET and C1 in
432 `defalt' slot. */
433 XCHAR_TABLE (val)->defalt = category_set;
435 table = val;
437 val = XCHAR_TABLE (table)->contents[c2];
438 if (CATEGORY_SET_P (val))
439 category_set = val;
440 else if (!SUB_CHAR_TABLE_P (val))
442 category_set = Fcopy_sequence (XCHAR_TABLE (table)->defalt);
443 XCHAR_TABLE (table)->contents[c2] = category_set;
445 else
446 /* This should never happen. */
447 error ("Invalid category table");
449 SET_CATEGORY_SET (category_set, category, set_value);
451 return Qnil;
454 /* Dump category table to buffer in human-readable format */
456 static void
457 describe_category (value)
458 Lisp_Object value;
460 Lisp_Object mnemonics;
462 Findent_to (make_number (16), make_number (1));
464 if (NILP (value))
466 insert_string ("default\n");
467 return;
470 if (!CATEGORY_SET_P (value))
472 insert_string ("invalid\n");
473 return;
476 mnemonics = Fcategory_set_mnemonics (value);
477 insert_from_string (mnemonics, 0, XSTRING (mnemonics)->size, 0);
478 insert_string ("\n");
479 return;
482 static Lisp_Object
483 describe_category_1 (vector)
484 Lisp_Object vector;
486 struct buffer *old = current_buffer;
487 set_buffer_internal (XBUFFER (Vstandard_output));
488 describe_vector (vector, Qnil, describe_category, 0, Qnil, Qnil);
490 int i;
491 Lisp_Object docs = XCHAR_TABLE (vector)->extras[0];
492 Lisp_Object elt;
494 if (!VECTORP (docs) || XVECTOR (docs)->size != 95)
496 insert_string ("Invalid first extra slot in this char table\n");
497 return Qnil;
500 insert_string ("Meanings of mnemonice characters are:\n");
501 for (i = 0; i < 95; i++)
503 elt = XVECTOR (docs)->contents[i];
504 if (NILP (elt))
505 continue;
507 insert_char (i + 32);
508 insert (": ", 2);
509 insert_from_string (elt, 0, XSTRING (elt)->size, 0);
510 insert ("\n", 1);
514 while (! NILP (XCHAR_TABLE (vector)->parent))
516 vector = XCHAR_TABLE (vector)->parent;
517 insert_string ("\nThe parent category table is:");
518 describe_vector (vector, Qnil, describe_category, 0, Qnil, Qnil);
521 call0 (intern ("help-mode"));
522 set_buffer_internal (old);
523 return Qnil;
526 DEFUN ("describe-category", Fdescribe_category, Sdescribe_category, 0, 0, "",
527 "Describe the category specifications in the category table.\n\
528 The descriptions are inserted in a buffer, which is then displayed.")
531 internal_with_output_to_temp_buffer
532 ("*Help*", describe_category_1, current_buffer->category_table);
534 return Qnil;
537 /* Return 1 if there is a word boundary between two word-constituent
538 characters C1 and C2 if they appear in this order, else return 0.
539 Use the macro WORD_BOUNDARY_P instead of calling this function
540 directly. */
543 word_boundary_p (c1, c2)
544 int c1, c2;
546 Lisp_Object category_set1, category_set2;
547 Lisp_Object tail;
548 int default_result;
550 if (CHAR_CHARSET (c1) == CHAR_CHARSET (c2))
552 tail = Vword_separating_categories;
553 default_result = 0;
555 else
557 tail = Vword_combining_categories;
558 default_result = 1;
561 category_set1 = CATEGORY_SET (c1);
562 if (NILP (category_set1))
563 return default_result;
564 category_set2 = CATEGORY_SET (c2);
565 if (NILP (category_set2))
566 return default_result;
568 for (; CONSP (tail); tail = XCONS (tail)->cdr)
570 Lisp_Object elt = XCONS(tail)->car;
572 if (CONSP (elt)
573 && CATEGORYP (XCONS (elt)->car)
574 && CATEGORYP (XCONS (elt)->cdr)
575 && CATEGORY_MEMBER (XCONS (elt)->car, category_set1)
576 && CATEGORY_MEMBER (XCONS (elt)->cdr, category_set2))
577 return !default_result;
579 return default_result;
583 init_category_once ()
585 /* This has to be done here, before we call Fmake_char_table. */
586 Qcategory_table = intern ("category-table");
587 staticpro (&Qcategory_table);
589 /* Intern this now in case it isn't already done.
590 Setting this variable twice is harmless.
591 But don't staticpro it here--that is done in alloc.c. */
592 Qchar_table_extra_slots = intern ("char-table-extra-slots");
594 /* Now we are ready to set up this property, so we can
595 create category tables. */
596 Fput (Qcategory_table, Qchar_table_extra_slots, make_number (2));
598 Vstandard_category_table = Fmake_char_table (Qcategory_table, Qnil);
599 /* Set a category set which contains nothing to the default. */
600 XCHAR_TABLE (Vstandard_category_table)->defalt = MAKE_CATEGORY_SET;
601 Fset_char_table_extra_slot (Vstandard_category_table, 0,
602 Fmake_vector (make_number (95), Qnil));
605 syms_of_category ()
607 Qcategoryp = intern ("categoryp");
608 staticpro (&Qcategoryp);
609 Qcategorysetp = intern ("categorysetp");
610 staticpro (&Qcategorysetp);
611 Qcategory_table_p = intern ("category-table-p");
612 staticpro (&Qcategory_table_p);
614 DEFVAR_LISP ("word-combining-categories", &Vword_combining_categories,
615 "List of pair (cons) of categories to determine word boundary.\n\
617 Emacs treats a sequence of word constituent characters as a single\n\
618 word (i.e. finds no word boundary between them) iff they belongs to\n\
619 the same charset. But, exceptions are allowed in the following cases.\n\
621 (1) The case that characters are in different charsets is controlled\n\
622 by the variable `word-combining-categories'.\n\
624 Emacs finds no word boundary between characters of different charsets\n\
625 if they have categories matching some element of this list.\n\
627 More precisely, if an element of this list is a cons of category CAT1\n\
628 and CAT2, and a multibyte character C1 which has CAT1 is followed by\n\
629 C2 which has CAT2, there's no word boundary between C1 and C2.\n\
631 For instance, to tell that ASCII characters and Latin-1 characters can\n\
632 form a single word, the element `(?l . ?l)' should be in this list\n\
633 because both characters have the category `l' (Latin characters).\n\
635 (2) The case that character are in the same charset is controlled by\n\
636 the variable `word-separating-categories'.\n\
638 Emacs find a word boundary between characters of the same charset\n\
639 if they have categories matching some element of this list.\n\
641 More precisely, if an element of this list is a cons of category CAT1\n\
642 and CAT2, and a multibyte character C1 which has CAT1 is followed by\n\
643 C2 which has CAT2, there's a word boundary between C1 and C2.\n\
645 For instance, to tell that there's a word boundary between Japanese\n\
646 Hiragana and Japanese Kanji (both are in the same charset), the\n\
647 element `(?H . ?C) should be in this list.");
649 Vword_combining_categories = Qnil;
651 DEFVAR_LISP ("word-separating-categories", &Vword_separating_categories,
652 "List of pair (cons) of categories to determine word boundary.\n\
653 See the documentation of the variable `word-combining-categories'.");
655 Vword_separating_categories = Qnil;
657 defsubr (&Smake_category_set);
658 defsubr (&Sdefine_category);
659 defsubr (&Scategory_docstring);
660 defsubr (&Sget_unused_category);
661 defsubr (&Scategory_table_p);
662 defsubr (&Scategory_table);
663 defsubr (&Sstandard_category_table);
664 defsubr (&Scopy_category_table);
665 defsubr (&Sset_category_table);
666 defsubr (&Schar_category_set);
667 defsubr (&Scategory_set_mnemonics);
668 defsubr (&Smodify_category_entry);
669 defsubr (&Sdescribe_category);
671 category_table_version = 0;