]> code.delx.au - gnu-emacs/blob - src/category.c
(Fcall_interactively) <'d', 'r'>: Use set_marker_both.
[gnu-emacs] / src / category.c
1 /* GNU Emacs routines to deal with category tables.
2 Copyright (C) 1995, 1997 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
4
5 This file is part of GNU Emacs.
6
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.
11
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.
16
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. */
21
22
23 /* Here we handle three objects: category, category set, and category
24 table. Read comments in the file category.h to understand them. */
25
26 #include <config.h>
27 #include <ctype.h>
28 #include "lisp.h"
29 #include "buffer.h"
30 #include "charset.h"
31 #include "category.h"
32
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.
39
40 For the moment, we are not using this feature. */
41 static int category_table_version;
42
43 Lisp_Object Qcategory_table, Qcategoryp, Qcategorysetp, Qcategory_table_p;
44
45 /* Variables to determine word boundary. */
46 Lisp_Object Vword_combining_categories, Vword_separating_categories;
47
48 /* Temporary internal variable used in macro CHAR_HAS_CATEGORY. */
49 Lisp_Object _temp_category_set;
50
51 \f
52 /* Category set staff. */
53
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.")
57 (categories)
58 Lisp_Object categories;
59 {
60 Lisp_Object val;
61 int len;
62
63 CHECK_STRING (categories, 0);
64 val = MAKE_CATEGORY_SET;
65
66 len = XSTRING (categories)->size;
67 while (--len >= 0)
68 {
69 Lisp_Object category;
70
71 XSETFASTINT (category, XSTRING (categories)->data[len]);
72 CHECK_CATEGORY (category, 0);
73 SET_CATEGORY_SET (val, category, Qt);
74 }
75 return val;
76 }
77
78 \f
79 /* Category staff. */
80
81 Lisp_Object check_category_table ();
82
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;
91 {
92 CHECK_CATEGORY (category, 0);
93 CHECK_STRING (docstring, 1);
94 table = check_category_table (table);
95
96 if (!NILP (CATEGORY_DOCSTRING (table, XFASTINT (category))))
97 error ("Category `%c' is already defined", XFASTINT (category));
98 CATEGORY_DOCSTRING (table, XFASTINT (category)) = docstring;
99
100 return Qnil;
101 }
102
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;
109 {
110 Lisp_Object doc;
111
112 CHECK_CATEGORY (category, 0);
113 table = check_category_table (table);
114
115 return CATEGORY_DOCSTRING (table, XFASTINT (category));
116 }
117
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;
126 {
127 int i;
128 Lisp_Object docstring_vector;
129
130 table = check_category_table (table);
131
132 for (i = ' '; i <= '~'; i++)
133 if (NILP (CATEGORY_DOCSTRING (table, i)))
134 return make_number (i);
135
136 return Qnil;
137 }
138
139 \f
140 /* Category-table staff. */
141
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;
146 {
147 if (CHAR_TABLE_P (arg)
148 && EQ (XCHAR_TABLE (arg)->purpose, Qcategory_table))
149 return Qt;
150 return Qnil;
151 }
152
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. */
157
158 Lisp_Object
159 check_category_table (table)
160 Lisp_Object table;
161 {
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;
168 }
169
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.")
173 ()
174 {
175 return current_buffer->category_table;
176 }
177
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.")
182 ()
183 {
184 return Vstandard_category_table;
185 }
186
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. */
191
192 Lisp_Object
193 copy_category_table (table)
194 Lisp_Object table;
195 {
196 Lisp_Object tmp;
197 int i, to;
198
199 if (!NILP (XCHAR_TABLE (table)->top))
200 {
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);
204
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;
210
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))));
216 }
217 else
218 {
219 i = 32;
220 to = SUB_CHAR_TABLE_ORDINARY_SLOTS;
221 }
222
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);
226
227 /* At last, copy the remaining elements while paying attention to a
228 sub char table. */
229 for (; i < to; i++)
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));
234
235 return table;
236 }
237
238 DEFUN ("copy-category-table", Fcopy_category_table, Scopy_category_table,
239 0, 1, 0,
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.")
242 (table)
243 Lisp_Object table;
244 {
245 if (!NILP (table))
246 check_category_table (table);
247 else
248 table = Vstandard_category_table;
249
250 return copy_category_table (table);
251 }
252
253 DEFUN ("set-category-table", Fset_category_table, Sset_category_table, 1, 1, 0,
254 "Select a new category table for the current buffer.\n\
255 One argument, a category table.")
256 (table)
257 Lisp_Object table;
258 {
259 table = check_category_table (table);
260 current_buffer->category_table = table;
261 /* Indicate that this buffer now has a specified category table. */
262 current_buffer->local_var_flags
263 |= XFASTINT (buffer_local_flags.category_table);
264 return table;
265 }
266
267 \f
268 DEFUN ("char-category-set", Fchar_category_set, Schar_category_set, 1, 1, 0,
269 "Return a category set of CHAR.")
270 (ch)
271 Lisp_Object ch;
272 {
273 Lisp_Object val;
274 int charset;
275 unsigned char c1, c2;
276
277 CHECK_NUMBER (ch, 0);
278 return CATEGORY_SET (XFASTINT (ch));
279 }
280
281 DEFUN ("category-set-mnemonics", Fcategory_set_mnemonics,
282 Scategory_set_mnemonics, 1, 1, 0,
283 "Return a string of mnemonics of all categories in CATEGORY-SET.")
284 (category_set)
285 Lisp_Object category_set;
286 {
287 int i, j;
288 char str[96];
289
290 CHECK_CATEGORY_SET (category_set, 0);
291
292 j = 0;
293 for (i = 32; i < 127; i++)
294 if (CATEGORY_MEMBER (i, category_set))
295 str[j++] = i;
296 str[j] = '\0';
297
298 return build_string (str);
299 }
300
301 /* Modify all category sets stored under sub char-table TABLE so that
302 they contain (SET_VALUE is t) or don't contain (SET_VALUE is nil)
303 CATEGORY. */
304
305 void
306 modify_lower_category_set (table, category, set_value)
307 Lisp_Object table, category, set_value;
308 {
309 Lisp_Object val;
310 int i;
311
312 if (NILP (XCHAR_TABLE (table)->defalt))
313 {
314 val = MAKE_CATEGORY_SET;
315 SET_CATEGORY_SET (val, category, set_value);
316 XCHAR_TABLE (table)->defalt = val;
317 }
318
319 for (i = 32; i < SUB_CHAR_TABLE_ORDINARY_SLOTS; i++)
320 {
321 val = XCHAR_TABLE (table)->contents[i];
322
323 if (CATEGORY_SET_P (val))
324 SET_CATEGORY_SET (val, category, set_value);
325 else if (SUB_CHAR_TABLE_P (val))
326 modify_lower_category_set (val, category, set_value);
327 }
328 }
329
330 void
331 set_category_set (category_set, category, val)
332 Lisp_Object category_set, category, val;
333 {
334 do {
335 int idx = XINT (category) / 8;
336 unsigned char bits = 1 << (XINT (category) % 8);
337
338 if (NILP (val))
339 XCATEGORY_SET (category_set)->data[idx] &= ~bits;
340 else
341 XCATEGORY_SET (category_set)->data[idx] |= bits;
342 } while (0);
343 }
344
345 DEFUN ("modify-category-entry", Fmodify_category_entry,
346 Smodify_category_entry, 2, 4, 0,
347 "Modify the category set of CHAR by adding CATEGORY to it.\n\
348 The category is changed only for table TABLE, which defaults to\n\
349 the current buffer's category table.\n\
350 If optional forth argument RESET is non NIL,\n\
351 CATEGORY is deleted from the category set instead of being added.")
352 (ch, category, table, reset)
353 Lisp_Object ch, category, table, reset;
354 {
355 int c, charset, c1, c2;
356 Lisp_Object set_value; /* Actual value to be set in category sets. */
357 Lisp_Object val, category_set;
358
359 CHECK_NUMBER (ch, 0);
360 c = XINT (ch);
361 CHECK_CATEGORY (category, 1);
362 table = check_category_table (table);
363
364 if (NILP (CATEGORY_DOCSTRING (table, XFASTINT (category))))
365 error ("Undefined category: %c", XFASTINT (category));
366
367 set_value = NILP (reset) ? Qt : Qnil;
368
369 if (c < CHAR_TABLE_SINGLE_BYTE_SLOTS)
370 {
371 val = XCHAR_TABLE (table)->contents[c];
372 if (!CATEGORY_SET_P (val))
373 XCHAR_TABLE (table)->contents[c] = (val = MAKE_CATEGORY_SET);
374 SET_CATEGORY_SET (val, category, set_value);
375 return Qnil;
376 }
377
378 SPLIT_NON_ASCII_CHAR (c, charset, c1, c2);
379
380 /* The top level table. */
381 val = XCHAR_TABLE (table)->contents[charset + 128];
382 if (CATEGORY_SET_P (val))
383 category_set = val;
384 else if (!SUB_CHAR_TABLE_P (val))
385 {
386 category_set = val = MAKE_CATEGORY_SET;
387 XCHAR_TABLE (table)->contents[charset + 128] = category_set;
388 }
389
390 if (c1 <= 0)
391 {
392 /* Only a charset is specified. */
393 if (SUB_CHAR_TABLE_P (val))
394 /* All characters in CHARSET should be the same as for having
395 CATEGORY or not. */
396 modify_lower_category_set (val, category, set_value);
397 else
398 SET_CATEGORY_SET (category_set, category, set_value);
399 return Qnil;
400 }
401
402 /* The second level table. */
403 if (!SUB_CHAR_TABLE_P (val))
404 {
405 val = make_sub_char_table (Qnil);
406 XCHAR_TABLE (table)->contents[charset + 128] = val;
407 /* We must set default category set of CHARSET in `defalt' slot. */
408 XCHAR_TABLE (val)->defalt = category_set;
409 }
410 table = val;
411
412 val = XCHAR_TABLE (table)->contents[c1];
413 if (CATEGORY_SET_P (val))
414 category_set = val;
415 else if (!SUB_CHAR_TABLE_P (val))
416 {
417 category_set = val = Fcopy_sequence (XCHAR_TABLE (table)->defalt);
418 XCHAR_TABLE (table)->contents[c1] = category_set;
419 }
420
421 if (c2 <= 0)
422 {
423 if (SUB_CHAR_TABLE_P (val))
424 /* All characters in C1 group of CHARSET should be the same as
425 for CATEGORY. */
426 modify_lower_category_set (val, category, set_value);
427 else
428 SET_CATEGORY_SET (category_set, category, set_value);
429 return Qnil;
430 }
431
432 /* The third (bottom) level table. */
433 if (!SUB_CHAR_TABLE_P (val))
434 {
435 val = make_sub_char_table (Qnil);
436 XCHAR_TABLE (table)->contents[c1] = val;
437 /* We must set default category set of CHARSET and C1 in
438 `defalt' slot. */
439 XCHAR_TABLE (val)->defalt = category_set;
440 }
441 table = val;
442
443 val = XCHAR_TABLE (table)->contents[c2];
444 if (CATEGORY_SET_P (val))
445 category_set = val;
446 else if (!SUB_CHAR_TABLE_P (val))
447 {
448 category_set = Fcopy_sequence (XCHAR_TABLE (table)->defalt);
449 XCHAR_TABLE (table)->contents[c2] = category_set;
450 }
451 else
452 /* This should never happen. */
453 error ("Invalid category table");
454
455 SET_CATEGORY_SET (category_set, category, set_value);
456
457 return Qnil;
458 }
459 \f
460 /* Dump category table to buffer in human-readable format */
461
462 static void
463 describe_category (value)
464 Lisp_Object value;
465 {
466 Lisp_Object mnemonics;
467
468 Findent_to (make_number (16), make_number (1));
469
470 if (NILP (value))
471 {
472 insert_string ("default\n");
473 return;
474 }
475
476 if (CHAR_TABLE_P (value))
477 {
478 insert_string ("deeper char-table ...\n");
479 return;
480 }
481
482 if (!CATEGORY_SET_P (value))
483 {
484 insert_string ("invalid\n");
485 return;
486 }
487
488 mnemonics = Fcategory_set_mnemonics (value);
489 insert_from_string (mnemonics, 0, XSTRING (mnemonics)->size, 0);
490 insert_string ("\n");
491 return;
492 }
493
494 static Lisp_Object
495 describe_category_1 (vector)
496 Lisp_Object vector;
497 {
498 struct buffer *old = current_buffer;
499 set_buffer_internal (XBUFFER (Vstandard_output));
500 describe_vector (vector, Qnil, describe_category, 0, Qnil, Qnil,
501 (int *)0, 0);
502 {
503 int i;
504 Lisp_Object docs = XCHAR_TABLE (vector)->extras[0];
505 Lisp_Object elt;
506
507 if (!VECTORP (docs) || XVECTOR (docs)->size != 95)
508 {
509 insert_string ("Invalid first extra slot in this char table\n");
510 return Qnil;
511 }
512
513 insert_string ("Meanings of mnemonice characters are:\n");
514 for (i = 0; i < 95; i++)
515 {
516 elt = XVECTOR (docs)->contents[i];
517 if (NILP (elt))
518 continue;
519
520 insert_char (i + 32);
521 insert (": ", 2);
522 insert_from_string (elt, 0, XSTRING (elt)->size, 0);
523 insert ("\n", 1);
524 }
525 }
526
527 while (! NILP (XCHAR_TABLE (vector)->parent))
528 {
529 vector = XCHAR_TABLE (vector)->parent;
530 insert_string ("\nThe parent category table is:");
531 describe_vector (vector, Qnil, describe_category, 0, Qnil, Qnil,
532 (int *) 0, 0);
533 }
534
535 call0 (intern ("help-mode"));
536 set_buffer_internal (old);
537 return Qnil;
538 }
539
540 DEFUN ("describe-category", Fdescribe_category, Sdescribe_category, 0, 0, "",
541 "Describe the category specifications in the category table.\n\
542 The descriptions are inserted in a buffer, which is then displayed.")
543 ()
544 {
545 internal_with_output_to_temp_buffer
546 ("*Help*", describe_category_1, current_buffer->category_table);
547
548 return Qnil;
549 }
550 \f
551 /* Return 1 if there is a word boundary between two word-constituent
552 characters C1 and C2 if they appear in this order, else return 0.
553 Use the macro WORD_BOUNDARY_P instead of calling this function
554 directly. */
555
556 int
557 word_boundary_p (c1, c2)
558 int c1, c2;
559 {
560 Lisp_Object category_set1, category_set2;
561 Lisp_Object tail;
562 int default_result;
563
564 if (CHAR_CHARSET (c1) == CHAR_CHARSET (c2))
565 {
566 tail = Vword_separating_categories;
567 default_result = 0;
568 }
569 else
570 {
571 tail = Vword_combining_categories;
572 default_result = 1;
573 }
574
575 category_set1 = CATEGORY_SET (c1);
576 if (NILP (category_set1))
577 return default_result;
578 category_set2 = CATEGORY_SET (c2);
579 if (NILP (category_set2))
580 return default_result;
581
582 for (; CONSP (tail); tail = XCONS (tail)->cdr)
583 {
584 Lisp_Object elt = XCONS(tail)->car;
585
586 if (CONSP (elt)
587 && CATEGORYP (XCONS (elt)->car)
588 && CATEGORYP (XCONS (elt)->cdr)
589 && CATEGORY_MEMBER (XFASTINT (XCONS (elt)->car), category_set1)
590 && CATEGORY_MEMBER (XFASTINT (XCONS (elt)->cdr), category_set2))
591 return !default_result;
592 }
593 return default_result;
594 }
595
596 \f
597 init_category_once ()
598 {
599 /* This has to be done here, before we call Fmake_char_table. */
600 Qcategory_table = intern ("category-table");
601 staticpro (&Qcategory_table);
602
603 /* Intern this now in case it isn't already done.
604 Setting this variable twice is harmless.
605 But don't staticpro it here--that is done in alloc.c. */
606 Qchar_table_extra_slots = intern ("char-table-extra-slots");
607
608 /* Now we are ready to set up this property, so we can
609 create category tables. */
610 Fput (Qcategory_table, Qchar_table_extra_slots, make_number (2));
611
612 Vstandard_category_table = Fmake_char_table (Qcategory_table, Qnil);
613 /* Set a category set which contains nothing to the default. */
614 XCHAR_TABLE (Vstandard_category_table)->defalt = MAKE_CATEGORY_SET;
615 Fset_char_table_extra_slot (Vstandard_category_table, make_number (0),
616 Fmake_vector (make_number (95), Qnil));
617 }
618
619 syms_of_category ()
620 {
621 Qcategoryp = intern ("categoryp");
622 staticpro (&Qcategoryp);
623 Qcategorysetp = intern ("categorysetp");
624 staticpro (&Qcategorysetp);
625 Qcategory_table_p = intern ("category-table-p");
626 staticpro (&Qcategory_table_p);
627
628 DEFVAR_LISP ("word-combining-categories", &Vword_combining_categories,
629 "List of pair (cons) of categories to determine word boundary.\n\
630 \n\
631 Emacs treats a sequence of word constituent characters as a single\n\
632 word (i.e. finds no word boundary between them) iff they belongs to\n\
633 the same charset. But, exceptions are allowed in the following cases.\n\
634 \n\
635 (1) The case that characters are in different charsets is controlled\n\
636 by the variable `word-combining-categories'.\n\
637 \n\
638 Emacs finds no word boundary between characters of different charsets\n\
639 if they have categories matching some element of this list.\n\
640 \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 no word boundary between C1 and C2.\n\
644 \n\
645 For instance, to tell that ASCII characters and Latin-1 characters can\n\
646 form a single word, the element `(?l . ?l)' should be in this list\n\
647 because both characters have the category `l' (Latin characters).\n\
648 \n\
649 (2) The case that character are in the same charset is controlled by\n\
650 the variable `word-separating-categories'.\n\
651 \n\
652 Emacs find a word boundary between characters of the same charset\n\
653 if they have categories matching some element of this list.\n\
654 \n\
655 More precisely, if an element of this list is a cons of category CAT1\n\
656 and CAT2, and a multibyte character C1 which has CAT1 is followed by\n\
657 C2 which has CAT2, there's a word boundary between C1 and C2.\n\
658 \n\
659 For instance, to tell that there's a word boundary between Japanese\n\
660 Hiragana and Japanese Kanji (both are in the same charset), the\n\
661 element `(?H . ?C) should be in this list.");
662
663 Vword_combining_categories = Qnil;
664
665 DEFVAR_LISP ("word-separating-categories", &Vword_separating_categories,
666 "List of pair (cons) of categories to determine word boundary.\n\
667 See the documentation of the variable `word-combining-categories'.");
668
669 Vword_separating_categories = Qnil;
670
671 defsubr (&Smake_category_set);
672 defsubr (&Sdefine_category);
673 defsubr (&Scategory_docstring);
674 defsubr (&Sget_unused_category);
675 defsubr (&Scategory_table_p);
676 defsubr (&Scategory_table);
677 defsubr (&Sstandard_category_table);
678 defsubr (&Scopy_category_table);
679 defsubr (&Sset_category_table);
680 defsubr (&Schar_category_set);
681 defsubr (&Scategory_set_mnemonics);
682 defsubr (&Smodify_category_entry);
683 defsubr (&Sdescribe_category);
684
685 category_table_version = 0;
686 }