1 /* Declarations having to do with XEmacs syntax tables.
2 Copyright (C) 1985, 1992, 1993 Free Software Foundation, Inc.
3 Copyright (C) 2001 MORIOKA Tomohiko
5 This file is part of XEmacs.
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; 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. */
22 /* Synched up with: FSF 19.28. */
24 #ifndef INCLUDED_syntax_h_
25 #define INCLUDED_syntax_h_
29 /* A syntax table is a type of char table.
31 The low 7 bits of the integer is a code, as follows. The 8th bit is
32 used as the prefix bit flag (see below).
34 The values in a syntax table are either integers or conses of
35 integers and chars. The lowest 7 bits of the integer are the syntax
36 class. If this is Sinherit, then the actual syntax value needs to
37 be retrieved from the standard syntax table.
39 Since the logic involved in finding the actual integer isn't very
40 complex, you'd think the time required to retrieve it is not a
41 factor. If you thought that, however, you'd be wrong, due to the
42 high number of times (many per character) that the syntax value is
43 accessed in functions such as scan_lists(). To speed this up,
44 we maintain a mirror syntax table that contains the actual
45 integers. We can do this successfully because syntax tables are
46 now an abstract type, where we control all access.
49 /* The standard syntax table is stored where it will automatically
50 be used in all new buffers. */
51 extern Lisp_Object Vstandard_syntax_table;
55 Swhitespace, /* whitespace character */
56 Spunct, /* random punctuation character */
57 Sword, /* word constituent */
58 Ssymbol, /* symbol constituent but not word constituent */
59 Sopen, /* a beginning delimiter */
60 Sclose, /* an ending delimiter */
61 Squote, /* a prefix character like Lisp ' */
62 Sstring, /* a string-grouping character like Lisp " */
63 Smath, /* delimiters like $ in TeX. */
64 Sescape, /* a character that begins a C-style escape */
65 Scharquote, /* a character that quotes the following character */
66 Scomment, /* a comment-starting character */
67 Sendcomment, /* a comment-ending character */
68 Sinherit, /* use the standard syntax table for this character */
69 Scomment_fence, /* Starts/ends comment which is delimited on the
70 other side by a char with the same syntaxcode. */
71 Sstring_fence, /* Starts/ends string which is delimited on the
72 other side by a char with the same syntaxcode. */
73 Smax /* Upper bound on codes that are meaningful */
76 enum syntaxcode charset_syntax (struct buffer *buf, Lisp_Object charset,
79 /* Return the syntax code for a particular character and mirror table. */
82 INLINE_HEADER int SYNTAX_CODE_UNSAFE (Lisp_Char_Table *table, Emchar c);
84 SYNTAX_CODE_UNSAFE (Lisp_Char_Table *table, Emchar c)
86 int code = CHAR_TABLE_VALUE_UNSAFE (table, c);
95 code = CHAR_TABLE_VALUE_UNSAFE (XCHAR_TABLE
96 (Vstandard_syntax_table), c);
105 #define SYNTAX_CODE_UNSAFE(table, c) \
106 XINT (CHAR_TABLE_VALUE_UNSAFE (table, c))
109 INLINE_HEADER int SYNTAX_CODE (Lisp_Char_Table *table, Emchar c);
111 SYNTAX_CODE (Lisp_Char_Table *table, Emchar c)
113 return SYNTAX_CODE_UNSAFE (table, c);
116 #define SYNTAX_UNSAFE(table, c) \
117 ((enum syntaxcode) (SYNTAX_CODE_UNSAFE (table, c) & 0177))
119 #define SYNTAX_FROM_CODE(code) ((enum syntaxcode) ((code) & 0177))
120 #define SYNTAX(table, c) SYNTAX_FROM_CODE (SYNTAX_CODE (table, c))
122 INLINE_HEADER int WORD_SYNTAX_P (Lisp_Char_Table *table, Emchar c);
124 WORD_SYNTAX_P (Lisp_Char_Table *table, Emchar c)
126 return SYNTAX (table, c) == Sword;
129 /* OK, here's a graphic diagram of the format of the syntax values:
133 [ 3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 ]
134 [ 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 ]
136 <-----> <-----> <-------------> <-------------> ^ <----------->
137 ELisp unused |comment bits | unused | syntax code
138 tag | | | | | | | | |
139 stuff | | | | | | | | |
141 | | | | | | | | `--> prefix flag
143 | | | | | | | `--> comment end style B, second char
144 | | | | | | `----> comment end style A, second char
145 | | | | | `------> comment end style B, first char
146 | | | | `--------> comment end style A, first char
147 | | | `----------> comment start style B, second char
148 | | `------------> comment start style A, second char
149 | `--------------> comment start style B, first char
150 `----------------> comment start style A, first char
152 In a 64-bit integer, there would be 32 more unused bits between
153 the tag and the comment bits.
155 Clearly, such a scheme will not work for Mule, because the matching
156 paren could be any character and as such requires 19 bits, which
159 Remember that under Mule we use char tables instead of vectors.
160 So what we do is use another char table for the matching paren
161 and store a pointer to it in the first char table. (This frees
162 code from having to worry about passing two tables around.)
166 /* The prefix flag bit for backward-prefix-chars is now put into bit 7. */
168 #define SYNTAX_PREFIX_UNSAFE(table, c) \
169 ((SYNTAX_CODE_UNSAFE (table, c) >> 7) & 1)
170 #define SYNTAX_PREFIX(table, c) \
171 ((SYNTAX_CODE (table, c) >> 7) & 1)
173 /* Bits 23-16 are used to implement up to two comment styles
174 in a single buffer. They have the following meanings:
176 1. first of a one or two character comment-start sequence of style a.
177 2. first of a one or two character comment-start sequence of style b.
178 3. second of a two-character comment-start sequence of style a.
179 4. second of a two-character comment-start sequence of style b.
180 5. first of a one or two character comment-end sequence of style a.
181 6. first of a one or two character comment-end sequence of style b.
182 7. second of a two-character comment-end sequence of style a.
183 8. second of a two-character comment-end sequence of style b.
186 #define SYNTAX_COMMENT_BITS(table, c) \
187 ((SYNTAX_CODE (table, c) >> 16) &0xff)
189 #define SYNTAX_FIRST_OF_START_A 0x80
190 #define SYNTAX_FIRST_OF_START_B 0x40
191 #define SYNTAX_SECOND_OF_START_A 0x20
192 #define SYNTAX_SECOND_OF_START_B 0x10
193 #define SYNTAX_FIRST_OF_END_A 0x08
194 #define SYNTAX_FIRST_OF_END_B 0x04
195 #define SYNTAX_SECOND_OF_END_A 0x02
196 #define SYNTAX_SECOND_OF_END_B 0x01
198 #define SYNTAX_COMMENT_STYLE_A 0xaa
199 #define SYNTAX_COMMENT_STYLE_B 0x55
200 #define SYNTAX_FIRST_CHAR_START 0xc0
201 #define SYNTAX_FIRST_CHAR_END 0x0c
202 #define SYNTAX_FIRST_CHAR 0xcc
203 #define SYNTAX_SECOND_CHAR_START 0x30
204 #define SYNTAX_SECOND_CHAR_END 0x03
205 #define SYNTAX_SECOND_CHAR 0x33
208 /* #### These are now more or less equivalent to
209 SYNTAX_COMMENT_MATCH_START ...*/
210 /* a and b must be first and second start chars for a common type */
211 #define SYNTAX_START_P(table, a, b) \
212 (((SYNTAX_COMMENT_BITS (table, a) & SYNTAX_FIRST_CHAR_START) >> 2) \
213 & (SYNTAX_COMMENT_BITS (table, b) & SYNTAX_SECOND_CHAR_START))
215 /* ... and SYNTAX_COMMENT_MATCH_END */
216 /* a and b must be first and second end chars for a common type */
217 #define SYNTAX_END_P(table, a, b) \
218 (((SYNTAX_COMMENT_BITS (table, a) & SYNTAX_FIRST_CHAR_END) >> 2) \
219 & (SYNTAX_COMMENT_BITS (table, b) & SYNTAX_SECOND_CHAR_END))
221 #define SYNTAX_STYLES_MATCH_START_P(table, a, b, mask) \
222 ((SYNTAX_COMMENT_BITS (table, a) & SYNTAX_FIRST_CHAR_START & (mask)) \
223 && (SYNTAX_COMMENT_BITS (table, b) & SYNTAX_SECOND_CHAR_START & (mask)))
225 #define SYNTAX_STYLES_MATCH_END_P(table, a, b, mask) \
226 ((SYNTAX_COMMENT_BITS (table, a) & SYNTAX_FIRST_CHAR_END & (mask)) \
227 && (SYNTAX_COMMENT_BITS (table, b) & SYNTAX_SECOND_CHAR_END & (mask)))
229 #define SYNTAX_STYLES_MATCH_1CHAR_P(table, a, mask) \
230 ((SYNTAX_COMMENT_BITS (table, a) & (mask)))
232 #define STYLE_FOUND_P(table, a, b, startp, style) \
233 ((SYNTAX_COMMENT_BITS (table, a) & \
234 ((startp) ? SYNTAX_FIRST_CHAR_START : \
235 SYNTAX_FIRST_CHAR_END) & (style)) \
236 && (SYNTAX_COMMENT_BITS (table, b) & \
237 ((startp) ? SYNTAX_SECOND_CHAR_START : \
238 SYNTAX_SECOND_CHAR_END) & (style)))
240 #define SYNTAX_COMMENT_MASK_START(table, a, b) \
241 ((STYLE_FOUND_P (table, a, b, 1, SYNTAX_COMMENT_STYLE_A) \
242 ? SYNTAX_COMMENT_STYLE_A \
243 : (STYLE_FOUND_P (table, a, b, 1, SYNTAX_COMMENT_STYLE_B) \
244 ? SYNTAX_COMMENT_STYLE_B \
247 #define SYNTAX_COMMENT_MASK_END(table, a, b) \
248 ((STYLE_FOUND_P (table, a, b, 0, SYNTAX_COMMENT_STYLE_A) \
249 ? SYNTAX_COMMENT_STYLE_A \
250 : (STYLE_FOUND_P (table, a, b, 0, SYNTAX_COMMENT_STYLE_B) \
251 ? SYNTAX_COMMENT_STYLE_B \
254 #define STYLE_FOUND_1CHAR_P(table, a, style) \
255 ((SYNTAX_COMMENT_BITS (table, a) & (style)))
257 #define SYNTAX_COMMENT_1CHAR_MASK(table, a) \
258 ((STYLE_FOUND_1CHAR_P (table, a, SYNTAX_COMMENT_STYLE_A) \
259 ? SYNTAX_COMMENT_STYLE_A \
260 : (STYLE_FOUND_1CHAR_P (table, a, SYNTAX_COMMENT_STYLE_B) \
261 ? SYNTAX_COMMENT_STYLE_B \
264 EXFUN (Fchar_syntax, 2);
265 EXFUN (Fforward_word, 2);
267 /* This array, indexed by a character, contains the syntax code which
268 that character signifies (as a char).
269 For example, (enum syntaxcode) syntax_spec_code['w'] is Sword. */
271 extern const unsigned char syntax_spec_code[0400];
273 /* Indexed by syntax code, give the letter that describes it. */
275 extern const unsigned char syntax_code_spec[];
277 Lisp_Object scan_lists (struct buffer *buf, Bufpos from, int count,
278 int depth, int sexpflag, int no_error);
279 int char_quoted (struct buffer *buf, Bufpos pos);
281 /* NOTE: This does not refer to the mirror table, but to the
282 syntax table itself. */
283 Lisp_Object syntax_match (Lisp_Object table, Emchar ch);
285 extern int no_quit_in_re_search;
286 extern struct buffer *regex_emacs_buffer;
288 /* This is the string or buffer in which we are matching. It is used
289 for looking up syntax properties. */
290 extern Lisp_Object regex_match_object;
293 void update_syntax_table (Lisp_Char_Table *ct);
298 extern int lookup_syntax_properties;
302 int use_code; /* Whether to use syntax_code
303 or current_syntax_table. */
304 struct buffer* buffer; /* The buffer the current syntax cache
306 Lisp_Object object; /* The buffer or string the current
307 syntax cache applies to. */
308 int syntax_code; /* Syntax code of current char. */
309 Lisp_Object current_syntax_table; /* Syntax table for current pos. */
310 Lisp_Object old_prop; /* Syntax-table prop at prev pos. */
312 Bufpos next_change; /* Position of the next extent
314 Bufpos prev_change; /* Position of the previous
317 extern struct syntax_cache syntax_cache;
319 void update_syntax_cache (int pos, int count, int init);
321 /* Make syntax cache state good for CHARPOS, assuming it is
322 currently good for a position before CHARPOS. */
323 #define UPDATE_SYNTAX_CACHE_FORWARD(pos) \
324 (lookup_syntax_properties \
325 ? (update_syntax_cache ((pos), 1, 0), 1) \
328 /* Make syntax cache state good for CHARPOS, assuming it is
329 currently good for a position after CHARPOS. */
330 #define UPDATE_SYNTAX_CACHE_BACKWARD(pos) \
331 (lookup_syntax_properties \
332 ? (update_syntax_cache ((pos), -1, 0), 1) \
335 /* Make syntax cache state good for CHARPOS */
336 #define UPDATE_SYNTAX_CACHE(pos) \
337 (lookup_syntax_properties \
338 ? (update_syntax_cache ((pos), 0, 0), 1) \
341 #define SYNTAX_FROM_CACHE(table, c) \
342 SYNTAX_FROM_CODE (SYNTAX_CODE_FROM_CACHE (table, c))
344 #define SYNTAX_CODE_FROM_CACHE(table, c) \
345 ( syntax_cache.use_code \
346 ? syntax_cache.syntax_code \
347 : SYNTAX_CODE (XCHAR_TABLE (syntax_cache.current_syntax_table), \
351 /* Convert the byte offset BYTEPOS into a character position,
352 for the object recorded in syntax_cache with SETUP_SYNTAX_TABLE_FOR_OBJECT.
354 The value is meant for use in the UPDATE_SYNTAX_TABLE... macros.
355 These macros do nothing when parse_sexp_lookup_properties is 0,
356 so we return 0 in that case, for speed. */
357 #define SYNTAX_CACHE_BYTE_TO_CHAR(bytepos) \
358 (! lookup_syntax_properties \
360 : STRINGP (syntax_cache.object) \
361 ? bytecount_to_charcount (XSTRING_DATA (syntax_cache.object), bytepos) \
362 : (BUFFERP (syntax_cache.object) || NILP (syntax_cache.object)) \
363 ? bytind_to_bufpos (syntax_cache.buffer, \
364 bytepos + BI_BUF_BEGV (syntax_cache.buffer)) \
367 #define SYNTAX_CACHE_OBJECT_BYTE_TO_CHAR(obj, buf, bytepos) \
368 (! lookup_syntax_properties \
371 ? bytecount_to_charcount (XSTRING_DATA (obj), bytepos) \
372 : (BUFFERP (obj) || NILP (obj)) \
373 ? bytind_to_bufpos (buf, bytepos + BI_BUF_BEGV (buf)) \
376 #else /* not emacs */
378 #define update_syntax_cache(pos, count, init)
379 #define UPDATE_SYNTAX_CACHE_FORWARD(pos)
380 #define UPDATE_SYNTAX_CACHE_BACKWARD(pos)
381 #define UPDATE_SYNTAX_CACHE(pos)
382 #define SYNTAX_FROM_CACHE SYNTAX
383 #define SYNTAX_CODE_FROM_CACHE SYNTAX_CODE
388 #define SETUP_SYNTAX_CACHE(FROM, COUNT) \
390 syntax_cache.buffer = current_buffer; \
391 syntax_cache.object = Qnil; \
392 syntax_cache.current_syntax_table \
393 = current_buffer->syntax_table; \
394 syntax_cache.use_code = 0; \
395 if (lookup_syntax_properties) \
396 update_syntax_cache ((COUNT) > 0 ? (FROM) : (FROM) - 1, \
400 #define SETUP_SYNTAX_CACHE(FROM, COUNT) \
402 syntax_cache.buffer = current_buffer; \
403 syntax_cache.object = Qnil; \
404 syntax_cache.current_syntax_table \
405 = current_buffer->mirror_syntax_table; \
406 syntax_cache.use_code = 0; \
407 if (lookup_syntax_properties) \
408 update_syntax_cache ((COUNT) > 0 ? (FROM) : (FROM) - 1, \
414 #define SETUP_SYNTAX_CACHE_FOR_BUFFER(BUFFER, FROM, COUNT) \
416 syntax_cache.buffer = (BUFFER); \
417 syntax_cache.object = Qnil; \
418 syntax_cache.current_syntax_table = \
419 syntax_cache.buffer->syntax_table; \
420 syntax_cache.use_code = 0; \
421 if (lookup_syntax_properties) \
422 update_syntax_cache ((FROM) + ((COUNT) > 0 ? 0 : -1), \
426 #define SETUP_SYNTAX_CACHE_FOR_BUFFER(BUFFER, FROM, COUNT) \
428 syntax_cache.buffer = (BUFFER); \
429 syntax_cache.object = Qnil; \
430 syntax_cache.current_syntax_table = \
431 syntax_cache.buffer->mirror_syntax_table; \
432 syntax_cache.use_code = 0; \
433 if (lookup_syntax_properties) \
434 update_syntax_cache ((FROM) + ((COUNT) > 0 ? 0 : -1), \
440 #define SETUP_SYNTAX_CACHE_FOR_OBJECT(OBJECT, BUFFER, FROM, COUNT) \
442 syntax_cache.buffer = (BUFFER); \
443 syntax_cache.object = (OBJECT); \
444 if (NILP (syntax_cache.object)) \
448 else if (EQ (syntax_cache.object, Qt)) \
452 else if (STRINGP (syntax_cache.object)) \
456 else if (BUFFERP (syntax_cache.object)) \
458 syntax_cache.buffer = XBUFFER (syntax_cache.object); \
462 /* OBJECT must be buffer/string/t/nil */ \
465 syntax_cache.current_syntax_table \
466 = syntax_cache.buffer->syntax_table; \
467 syntax_cache.use_code = 0; \
468 if (lookup_syntax_properties) \
469 update_syntax_cache ((FROM) + ((COUNT) > 0 ? 0 : -1), \
473 #define SETUP_SYNTAX_CACHE_FOR_OBJECT(OBJECT, BUFFER, FROM, COUNT) \
475 syntax_cache.buffer = (BUFFER); \
476 syntax_cache.object = (OBJECT); \
477 if (NILP (syntax_cache.object)) \
481 else if (EQ (syntax_cache.object, Qt)) \
485 else if (STRINGP (syntax_cache.object)) \
489 else if (BUFFERP (syntax_cache.object)) \
491 syntax_cache.buffer = XBUFFER (syntax_cache.object); \
495 /* OBJECT must be buffer/string/t/nil */ \
498 syntax_cache.current_syntax_table \
499 = syntax_cache.buffer->mirror_syntax_table; \
500 syntax_cache.use_code = 0; \
501 if (lookup_syntax_properties) \
502 update_syntax_cache ((FROM) + ((COUNT) > 0 ? 0 : -1), \
507 #define SYNTAX_CODE_PREFIX(c) \
510 #define SYNTAX_CODE_COMMENT_BITS(c) \
513 #define SYNTAX_CODES_START_P(a, b) \
514 (((SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_START) >> 2) \
515 & (SYNTAX_CODE_COMMENT_BITS (b) & SYNTAX_SECOND_CHAR_START))
517 #define SYNTAX_CODES_END_P(a, b) \
518 (((SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_END) >> 2) \
519 & (SYNTAX_CODE_COMMENT_BITS (b) & SYNTAX_SECOND_CHAR_END))
521 #define SYNTAX_CODES_COMMENT_MASK_START(a, b) \
522 (SYNTAX_CODES_MATCH_START_P (a, b, SYNTAX_COMMENT_STYLE_A) \
523 ? SYNTAX_COMMENT_STYLE_A \
524 : (SYNTAX_CODES_MATCH_START_P (a, b, SYNTAX_COMMENT_STYLE_B) \
525 ? SYNTAX_COMMENT_STYLE_B \
527 #define SYNTAX_CODES_COMMENT_MASK_END(a, b) \
528 (SYNTAX_CODES_MATCH_END_P (a, b, SYNTAX_COMMENT_STYLE_A) \
529 ? SYNTAX_COMMENT_STYLE_A \
530 : (SYNTAX_CODES_MATCH_END_P (a, b, SYNTAX_COMMENT_STYLE_B) \
531 ? SYNTAX_COMMENT_STYLE_B \
534 #define SYNTAX_CODE_START_FIRST_P(a) \
535 (SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_START)
537 #define SYNTAX_CODE_START_SECOND_P(a) \
538 (SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_SECOND_CHAR_START)
540 #define SYNTAX_CODE_END_FIRST_P(a) \
541 (SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_END)
543 #define SYNTAX_CODE_END_SECOND_P(a) \
544 (SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_SECOND_CHAR_END)
547 #define SYNTAX_CODES_MATCH_START_P(a, b, mask) \
548 ((SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_START & (mask)) \
549 && (SYNTAX_CODE_COMMENT_BITS (b) \
550 & SYNTAX_SECOND_CHAR_START & (mask)))
552 #define SYNTAX_CODES_MATCH_END_P(a, b, mask) \
553 ((SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_END & (mask)) \
554 && (SYNTAX_CODE_COMMENT_BITS (b) & SYNTAX_SECOND_CHAR_END & (mask)))
556 #define SYNTAX_CODE_MATCHES_1CHAR_P(a, mask) \
557 ((SYNTAX_CODE_COMMENT_BITS (a) & (mask)))
559 #define SYNTAX_CODE_COMMENT_1CHAR_MASK(a) \
560 ((SYNTAX_CODE_MATCHES_1CHAR_P (a, SYNTAX_COMMENT_STYLE_A) \
561 ? SYNTAX_COMMENT_STYLE_A \
562 : (SYNTAX_CODE_MATCHES_1CHAR_P (a, SYNTAX_COMMENT_STYLE_B) \
563 ? SYNTAX_COMMENT_STYLE_B \
567 #endif /* INCLUDED_syntax_h_ */