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)
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. */
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\
60 Lisp_Object categories
;
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
;
76 XSETFASTINT (category
, XSTRING (categories
)->data
[len
]);
77 CHECK_CATEGORY (category
, 0);
78 SET_CATEGORY_SET (val
, category
, Qt
);
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
;
108 DEFUN ("category-docstring", Fcategory_docstring
, Scategory_docstring
, 1, 2, 0,
109 "Return the documentation string of CATEGORY, as defined in CATEGORY-TABLE.")
111 Lisp_Object category
, table
;
113 CHECK_CATEGORY (category
, 0);
114 table
= check_category_table (table
);
116 return CATEGORY_DOCSTRING (table
, XFASTINT (category
));
119 DEFUN ("get-unused-category", Fget_unused_category
, Sget_unused_category
,
121 "Return a category which is not yet defined in CATEGORY-TABLE.\n\
122 If no category remains available, return nil.\n\
123 The optional argument CATEGORY-TABLE specifies which category table\n\
124 to modify; it defaults to the current buffer's category table.")
130 table
= check_category_table (table
);
132 for (i
= ' '; i
<= '~'; i
++)
133 if (NILP (CATEGORY_DOCSTRING (table
, i
)))
134 return make_number (i
);
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.")
147 if (CHAR_TABLE_P (arg
)
148 && EQ (XCHAR_TABLE (arg
)->purpose
, Qcategory_table
))
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. */
159 check_category_table (table
)
162 register Lisp_Object tem
;
164 return current_buffer
->category_table
;
165 while (tem
= Fcategory_table_p (table
), NILP (tem
))
166 table
= wrong_type_argument (Qcategory_table_p
, 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 binding TABLE to a sub char table. */
193 copy_category_table (table
)
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 /* Also copy the first (and sole) extra slot. It is a vector
212 containing docstring of each category. */
213 Fset_char_table_extra_slot
214 (table
, make_number (0),
215 Fcopy_sequence (Fchar_table_extra_slot (table
, make_number (0))));
220 to
= SUB_CHAR_TABLE_ORDINARY_SLOTS
;
223 /* If the table has non-nil default value, copy it. */
224 if (!NILP (tmp
= XCHAR_TABLE (table
)->defalt
))
225 XCHAR_TABLE (table
)->defalt
= Fcopy_sequence (tmp
);
227 /* At last, copy the remaining elements while paying attention to a
230 if (!NILP (tmp
= XCHAR_TABLE (table
)->contents
[i
]))
231 XCHAR_TABLE (table
)->contents
[i
]
232 = (SUB_CHAR_TABLE_P (tmp
)
233 ? copy_category_table (tmp
) : Fcopy_sequence (tmp
));
238 DEFUN ("copy-category-table", Fcopy_category_table
, Scopy_category_table
,
240 "Construct a new category table and return it.\n\
241 It is a copy of the TABLE, which defaults to the standard category table.")
246 check_category_table (table
);
248 table
= Vstandard_category_table
;
250 return copy_category_table (table
);
253 DEFUN ("make-category-table", Fmake_category_table
, Smake_category_table
,
255 "Construct a new and empty category table and return it.")
260 val
= Fmake_char_table (Qcategory_table
, Qnil
);
261 XCHAR_TABLE (val
)->defalt
= MAKE_CATEGORY_SET
;
262 Fset_char_table_extra_slot (val
, make_number (0),
263 Fmake_vector (make_number (95), Qnil
));
267 DEFUN ("set-category-table", Fset_category_table
, Sset_category_table
, 1, 1, 0,
268 "Specify TABLE as the category table for the current buffer.")
273 table
= check_category_table (table
);
274 current_buffer
->category_table
= table
;
275 /* Indicate that this buffer now has a specified category table. */
276 idx
= PER_BUFFER_VAR_IDX (category_table
);
277 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
282 DEFUN ("char-category-set", Fchar_category_set
, Schar_category_set
, 1, 1, 0,
283 "Return the category set of CHAR.")
287 CHECK_NUMBER (ch
, 0);
288 return CATEGORY_SET (XFASTINT (ch
));
291 DEFUN ("category-set-mnemonics", Fcategory_set_mnemonics
,
292 Scategory_set_mnemonics
, 1, 1, 0,
293 "Return a string containing mnemonics of the categories in CATEGORY-SET.\n\
294 CATEGORY-SET is a bool-vector, and the categories \"in\" it are those\n\
295 that are indexes where t occurs the bool-vector.\n\
296 The return value is a string containing those same categories.")
298 Lisp_Object category_set
;
303 CHECK_CATEGORY_SET (category_set
, 0);
306 for (i
= 32; i
< 127; i
++)
307 if (CATEGORY_MEMBER (i
, category_set
))
311 return build_string (str
);
314 /* Modify all category sets stored under sub char-table TABLE so that
315 they contain (SET_VALUE is t) or don't contain (SET_VALUE is nil)
319 modify_lower_category_set (table
, category
, set_value
)
320 Lisp_Object table
, category
, set_value
;
325 val
= XCHAR_TABLE (table
)->defalt
;
326 if (!CATEGORY_SET_P (val
))
327 val
= MAKE_CATEGORY_SET
;
328 SET_CATEGORY_SET (val
, category
, set_value
);
329 XCHAR_TABLE (table
)->defalt
= val
;
331 for (i
= 32; i
< SUB_CHAR_TABLE_ORDINARY_SLOTS
; i
++)
333 val
= XCHAR_TABLE (table
)->contents
[i
];
335 if (CATEGORY_SET_P (val
))
336 SET_CATEGORY_SET (val
, category
, set_value
);
337 else if (SUB_CHAR_TABLE_P (val
))
338 modify_lower_category_set (val
, category
, set_value
);
343 set_category_set (category_set
, category
, val
)
344 Lisp_Object category_set
, category
, val
;
347 int idx
= XINT (category
) / 8;
348 unsigned char bits
= 1 << (XINT (category
) % 8);
351 XCATEGORY_SET (category_set
)->data
[idx
] &= ~bits
;
353 XCATEGORY_SET (category_set
)->data
[idx
] |= bits
;
357 DEFUN ("modify-category-entry", Fmodify_category_entry
,
358 Smodify_category_entry
, 2, 4, 0,
359 "Modify the category set of CHARACTER by adding CATEGORY to it.\n\
360 The category is changed only for table TABLE, which defaults to\n\
361 the current buffer's category table.\n\
362 If optional fourth argument RESET is non-nil,\n\
363 then delete CATEGORY from the category set instead of adding it.")
364 (character
, category
, table
, reset
)
365 Lisp_Object character
, category
, table
, reset
;
367 int c
, charset
, c1
, c2
;
368 Lisp_Object set_value
; /* Actual value to be set in category sets. */
369 Lisp_Object val
, category_set
;
371 CHECK_NUMBER (character
, 0);
372 c
= XINT (character
);
373 CHECK_CATEGORY (category
, 1);
374 table
= check_category_table (table
);
376 if (NILP (CATEGORY_DOCSTRING (table
, XFASTINT (category
))))
377 error ("Undefined category: %c", XFASTINT (category
));
379 set_value
= NILP (reset
) ? Qt
: Qnil
;
381 if (c
< CHAR_TABLE_SINGLE_BYTE_SLOTS
)
383 val
= XCHAR_TABLE (table
)->contents
[c
];
384 if (!CATEGORY_SET_P (val
))
385 XCHAR_TABLE (table
)->contents
[c
] = (val
= MAKE_CATEGORY_SET
);
386 SET_CATEGORY_SET (val
, category
, set_value
);
390 SPLIT_CHAR (c
, charset
, c1
, c2
);
392 /* The top level table. */
393 val
= XCHAR_TABLE (table
)->contents
[charset
+ 128];
394 if (CATEGORY_SET_P (val
))
396 else if (!SUB_CHAR_TABLE_P (val
))
398 category_set
= val
= MAKE_CATEGORY_SET
;
399 XCHAR_TABLE (table
)->contents
[charset
+ 128] = category_set
;
404 /* Only a charset is specified. */
405 if (SUB_CHAR_TABLE_P (val
))
406 /* All characters in CHARSET should be the same as for having
408 modify_lower_category_set (val
, category
, set_value
);
410 SET_CATEGORY_SET (category_set
, category
, set_value
);
414 /* The second level table. */
415 if (!SUB_CHAR_TABLE_P (val
))
417 val
= make_sub_char_table (Qnil
);
418 XCHAR_TABLE (table
)->contents
[charset
+ 128] = val
;
419 /* We must set default category set of CHARSET in `defalt' slot. */
420 XCHAR_TABLE (val
)->defalt
= category_set
;
424 val
= XCHAR_TABLE (table
)->contents
[c1
];
425 if (CATEGORY_SET_P (val
))
427 else if (!SUB_CHAR_TABLE_P (val
))
429 category_set
= val
= Fcopy_sequence (XCHAR_TABLE (table
)->defalt
);
430 XCHAR_TABLE (table
)->contents
[c1
] = category_set
;
435 if (SUB_CHAR_TABLE_P (val
))
436 /* All characters in C1 group of CHARSET should be the same as
438 modify_lower_category_set (val
, category
, set_value
);
440 SET_CATEGORY_SET (category_set
, category
, set_value
);
444 /* The third (bottom) level table. */
445 if (!SUB_CHAR_TABLE_P (val
))
447 val
= make_sub_char_table (Qnil
);
448 XCHAR_TABLE (table
)->contents
[c1
] = val
;
449 /* We must set default category set of CHARSET and C1 in
451 XCHAR_TABLE (val
)->defalt
= category_set
;
455 val
= XCHAR_TABLE (table
)->contents
[c2
];
456 if (CATEGORY_SET_P (val
))
458 else if (!SUB_CHAR_TABLE_P (val
))
460 category_set
= Fcopy_sequence (XCHAR_TABLE (table
)->defalt
);
461 XCHAR_TABLE (table
)->contents
[c2
] = category_set
;
464 /* This should never happen. */
465 error ("Invalid category table");
467 SET_CATEGORY_SET (category_set
, category
, set_value
);
472 /* Dump category table to buffer in human-readable format */
475 describe_category (value
)
478 Lisp_Object mnemonics
;
480 Findent_to (make_number (16), make_number (1));
484 insert_string ("default\n");
488 if (CHAR_TABLE_P (value
))
490 insert_string ("deeper char-table ...\n");
494 if (!CATEGORY_SET_P (value
))
496 insert_string ("invalid\n");
500 mnemonics
= Fcategory_set_mnemonics (value
);
501 insert_from_string (mnemonics
, 0, 0, XSTRING (mnemonics
)->size
,
502 STRING_BYTES (XSTRING (mnemonics
)), 0);
503 insert_string ("\n");
508 describe_category_1 (vector
)
511 struct buffer
*old
= current_buffer
;
512 set_buffer_internal (XBUFFER (Vstandard_output
));
513 describe_vector (vector
, Qnil
, describe_category
, 0, Qnil
, Qnil
,
517 Lisp_Object docs
= XCHAR_TABLE (vector
)->extras
[0];
520 if (!VECTORP (docs
) || XVECTOR (docs
)->size
!= 95)
522 insert_string ("Invalid first extra slot in this char table\n");
526 insert_string ("Meanings of mnemonice characters are:\n");
527 for (i
= 0; i
< 95; i
++)
529 elt
= XVECTOR (docs
)->contents
[i
];
533 insert_char (i
+ 32);
535 insert_from_string (elt
, 0, 0, XSTRING (elt
)->size
,
536 STRING_BYTES (XSTRING (elt
)), 0);
541 while (! NILP (XCHAR_TABLE (vector
)->parent
))
543 vector
= XCHAR_TABLE (vector
)->parent
;
544 insert_string ("\nThe parent category table is:");
545 describe_vector (vector
, Qnil
, describe_category
, 0, Qnil
, Qnil
,
549 call0 (intern ("help-mode"));
550 set_buffer_internal (old
);
554 DEFUN ("describe-categories", Fdescribe_categories
, Sdescribe_categories
, 0, 0, "",
555 "Describe the category specifications in the current category table.\n\
556 The descriptions are inserted in a buffer, which is then displayed.")
559 internal_with_output_to_temp_buffer
560 ("*Help*", describe_category_1
, current_buffer
->category_table
);
565 /* Return 1 if there is a word boundary between two word-constituent
566 characters C1 and C2 if they appear in this order, else return 0.
567 Use the macro WORD_BOUNDARY_P instead of calling this function
571 word_boundary_p (c1
, c2
)
574 Lisp_Object category_set1
, category_set2
;
578 if (CHAR_CHARSET (c1
) == CHAR_CHARSET (c2
))
580 tail
= Vword_separating_categories
;
585 tail
= Vword_combining_categories
;
589 category_set1
= CATEGORY_SET (c1
);
590 if (NILP (category_set1
))
591 return default_result
;
592 category_set2
= CATEGORY_SET (c2
);
593 if (NILP (category_set2
))
594 return default_result
;
596 for (; CONSP (tail
); tail
= XCDR (tail
))
598 Lisp_Object elt
= XCAR (tail
);
601 && CATEGORYP (XCAR (elt
))
602 && CATEGORYP (XCDR (elt
))
603 && CATEGORY_MEMBER (XFASTINT (XCAR (elt
)), category_set1
)
604 && CATEGORY_MEMBER (XFASTINT (XCDR (elt
)), category_set2
))
605 return !default_result
;
607 return default_result
;
612 init_category_once ()
614 /* This has to be done here, before we call Fmake_char_table. */
615 Qcategory_table
= intern ("category-table");
616 staticpro (&Qcategory_table
);
618 /* Intern this now in case it isn't already done.
619 Setting this variable twice is harmless.
620 But don't staticpro it here--that is done in alloc.c. */
621 Qchar_table_extra_slots
= intern ("char-table-extra-slots");
623 /* Now we are ready to set up this property, so we can
624 create category tables. */
625 Fput (Qcategory_table
, Qchar_table_extra_slots
, make_number (2));
627 Vstandard_category_table
= Fmake_char_table (Qcategory_table
, Qnil
);
628 /* Set a category set which contains nothing to the default. */
629 XCHAR_TABLE (Vstandard_category_table
)->defalt
= MAKE_CATEGORY_SET
;
630 Fset_char_table_extra_slot (Vstandard_category_table
, make_number (0),
631 Fmake_vector (make_number (95), Qnil
));
637 Qcategoryp
= intern ("categoryp");
638 staticpro (&Qcategoryp
);
639 Qcategorysetp
= intern ("categorysetp");
640 staticpro (&Qcategorysetp
);
641 Qcategory_table_p
= intern ("category-table-p");
642 staticpro (&Qcategory_table_p
);
644 DEFVAR_LISP ("word-combining-categories", &Vword_combining_categories
,
645 "List of pair (cons) of categories to determine word boundary.\n\
647 Emacs treats a sequence of word constituent characters as a single\n\
648 word (i.e. finds no word boundary between them) iff they belongs to\n\
649 the same charset. But, exceptions are allowed in the following cases.\n\
651 \(1) The case that characters are in different charsets is controlled\n\
652 by the variable `word-combining-categories'.\n\
654 Emacs finds no word boundary between characters of different charsets\n\
655 if they have categories matching some element of this list.\n\
657 More precisely, if an element of this list is a cons of category CAT1\n\
658 and CAT2, and a multibyte character C1 which has CAT1 is followed by\n\
659 C2 which has CAT2, there's no word boundary between C1 and C2.\n\
661 For instance, to tell that ASCII characters and Latin-1 characters can\n\
662 form a single word, the element `(?l . ?l)' should be in this list\n\
663 because both characters have the category `l' (Latin characters).\n\
665 \(2) The case that character are in the same charset is controlled by\n\
666 the variable `word-separating-categories'.\n\
668 Emacs find a word boundary between characters of the same charset\n\
669 if they have categories matching some element of this list.\n\
671 More precisely, if an element of this list is a cons of category CAT1\n\
672 and CAT2, and a multibyte character C1 which has CAT1 is followed by\n\
673 C2 which has CAT2, there's a word boundary between C1 and C2.\n\
675 For instance, to tell that there's a word boundary between Japanese\n\
676 Hiragana and Japanese Kanji (both are in the same charset), the\n\
677 element `(?H . ?C) should be in this list.");
679 Vword_combining_categories
= Qnil
;
681 DEFVAR_LISP ("word-separating-categories", &Vword_separating_categories
,
682 "List of pair (cons) of categories to determine word boundary.\n\
683 See the documentation of the variable `word-combining-categories'.");
685 Vword_separating_categories
= Qnil
;
687 defsubr (&Smake_category_set
);
688 defsubr (&Sdefine_category
);
689 defsubr (&Scategory_docstring
);
690 defsubr (&Sget_unused_category
);
691 defsubr (&Scategory_table_p
);
692 defsubr (&Scategory_table
);
693 defsubr (&Sstandard_category_table
);
694 defsubr (&Scopy_category_table
);
695 defsubr (&Smake_category_table
);
696 defsubr (&Sset_category_table
);
697 defsubr (&Schar_category_set
);
698 defsubr (&Scategory_set_mnemonics
);
699 defsubr (&Smodify_category_entry
);
700 defsubr (&Sdescribe_categories
);
702 category_table_version
= 0;