1 /* Declarations having to do with XEmacs syntax tables.
2 Copyright (C) 1985, 1992, 1993 Free Software Foundation, Inc.
4 This file is part of XEmacs.
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Synched up with: FSF 19.28. */
23 #ifndef INCLUDED_syntax_h_
24 #define INCLUDED_syntax_h_
28 /* A syntax table is a type of char table.
30 The low 7 bits of the integer is a code, as follows. The 8th bit is
31 used as the prefix bit flag (see below).
33 The values in a syntax table are either integers or conses of
34 integers and chars. The lowest 7 bits of the integer are the syntax
35 class. If this is Sinherit, then the actual syntax value needs to
36 be retrieved from the standard syntax table.
38 Since the logic involved in finding the actual integer isn't very
39 complex, you'd think the time required to retrieve it is not a
40 factor. If you thought that, however, you'd be wrong, due to the
41 high number of times (many per character) that the syntax value is
42 accessed in functions such as scan_lists(). To speed this up,
43 we maintain a mirror syntax table that contains the actual
44 integers. We can do this successfully because syntax tables are
45 now an abstract type, where we control all access.
50 Swhitespace, /* whitespace character */
51 Spunct, /* random punctuation character */
52 Sword, /* word constituent */
53 Ssymbol, /* symbol constituent but not word constituent */
54 Sopen, /* a beginning delimiter */
55 Sclose, /* an ending delimiter */
56 Squote, /* a prefix character like Lisp ' */
57 Sstring, /* a string-grouping character like Lisp " */
58 Smath, /* delimiters like $ in TeX. */
59 Sescape, /* a character that begins a C-style escape */
60 Scharquote, /* a character that quotes the following character */
61 Scomment, /* a comment-starting character */
62 Sendcomment, /* a comment-ending character */
63 Sinherit, /* use the standard syntax table for this character */
64 Scomment_fence, /* Starts/ends comment which is delimited on the
65 other side by a char with the same syntaxcode. */
66 Sstring_fence, /* Starts/ends string which is delimited on the
67 other side by a char with the same syntaxcode. */
68 Smax /* Upper bound on codes that are meaningful */
71 enum syntaxcode charset_syntax (struct buffer *buf, Lisp_Object charset,
74 /* Return the syntax code for a particular character and mirror table. */
76 #define SYNTAX_CODE_UNSAFE(table, c) \
77 XINT (CHAR_TABLE_VALUE_UNSAFE (table, c))
79 INLINE_HEADER int SYNTAX_CODE (Lisp_Char_Table *table, Emchar c);
81 SYNTAX_CODE (Lisp_Char_Table *table, Emchar c)
83 return SYNTAX_CODE_UNSAFE (table, c);
86 #define SYNTAX_UNSAFE(table, c) \
87 ((enum syntaxcode) (SYNTAX_CODE_UNSAFE (table, c) & 0177))
89 #define SYNTAX_FROM_CODE(code) ((enum syntaxcode) ((code) & 0177))
90 #define SYNTAX(table, c) SYNTAX_FROM_CODE (SYNTAX_CODE (table, c))
92 INLINE_HEADER int WORD_SYNTAX_P (Lisp_Char_Table *table, Emchar c);
94 WORD_SYNTAX_P (Lisp_Char_Table *table, Emchar c)
96 return SYNTAX (table, c) == Sword;
99 /* OK, here's a graphic diagram of the format of the syntax values:
103 [ 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 ]
104 [ 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 ]
106 <-----> <-----> <-------------> <-------------> ^ <----------->
107 ELisp unused |comment bits | unused | syntax code
108 tag | | | | | | | | |
109 stuff | | | | | | | | |
111 | | | | | | | | `--> prefix flag
113 | | | | | | | `--> comment end style B, second char
114 | | | | | | `----> comment end style A, second char
115 | | | | | `------> comment end style B, first char
116 | | | | `--------> comment end style A, first char
117 | | | `----------> comment start style B, second char
118 | | `------------> comment start style A, second char
119 | `--------------> comment start style B, first char
120 `----------------> comment start style A, first char
122 In a 64-bit integer, there would be 32 more unused bits between
123 the tag and the comment bits.
125 Clearly, such a scheme will not work for Mule, because the matching
126 paren could be any character and as such requires 19 bits, which
129 Remember that under Mule we use char tables instead of vectors.
130 So what we do is use another char table for the matching paren
131 and store a pointer to it in the first char table. (This frees
132 code from having to worry about passing two tables around.)
136 /* The prefix flag bit for backward-prefix-chars is now put into bit 7. */
138 #define SYNTAX_PREFIX_UNSAFE(table, c) \
139 ((SYNTAX_CODE_UNSAFE (table, c) >> 7) & 1)
140 #define SYNTAX_PREFIX(table, c) \
141 ((SYNTAX_CODE (table, c) >> 7) & 1)
143 /* Bits 23-16 are used to implement up to two comment styles
144 in a single buffer. They have the following meanings:
146 1. first of a one or two character comment-start sequence of style a.
147 2. first of a one or two character comment-start sequence of style b.
148 3. second of a two-character comment-start sequence of style a.
149 4. second of a two-character comment-start sequence of style b.
150 5. first of a one or two character comment-end sequence of style a.
151 6. first of a one or two character comment-end sequence of style b.
152 7. second of a two-character comment-end sequence of style a.
153 8. second of a two-character comment-end sequence of style b.
156 #define SYNTAX_COMMENT_BITS(table, c) \
157 ((SYNTAX_CODE (table, c) >> 16) &0xff)
159 #define SYNTAX_FIRST_OF_START_A 0x80
160 #define SYNTAX_FIRST_OF_START_B 0x40
161 #define SYNTAX_SECOND_OF_START_A 0x20
162 #define SYNTAX_SECOND_OF_START_B 0x10
163 #define SYNTAX_FIRST_OF_END_A 0x08
164 #define SYNTAX_FIRST_OF_END_B 0x04
165 #define SYNTAX_SECOND_OF_END_A 0x02
166 #define SYNTAX_SECOND_OF_END_B 0x01
168 #define SYNTAX_COMMENT_STYLE_A 0xaa
169 #define SYNTAX_COMMENT_STYLE_B 0x55
170 #define SYNTAX_FIRST_CHAR_START 0xc0
171 #define SYNTAX_FIRST_CHAR_END 0x0c
172 #define SYNTAX_FIRST_CHAR 0xcc
173 #define SYNTAX_SECOND_CHAR_START 0x30
174 #define SYNTAX_SECOND_CHAR_END 0x03
175 #define SYNTAX_SECOND_CHAR 0x33
178 /* #### These are now more or less equivalent to
179 SYNTAX_COMMENT_MATCH_START ...*/
180 /* a and b must be first and second start chars for a common type */
181 #define SYNTAX_START_P(table, a, b) \
182 (((SYNTAX_COMMENT_BITS (table, a) & SYNTAX_FIRST_CHAR_START) >> 2) \
183 & (SYNTAX_COMMENT_BITS (table, b) & SYNTAX_SECOND_CHAR_START))
185 /* ... and SYNTAX_COMMENT_MATCH_END */
186 /* a and b must be first and second end chars for a common type */
187 #define SYNTAX_END_P(table, a, b) \
188 (((SYNTAX_COMMENT_BITS (table, a) & SYNTAX_FIRST_CHAR_END) >> 2) \
189 & (SYNTAX_COMMENT_BITS (table, b) & SYNTAX_SECOND_CHAR_END))
191 #define SYNTAX_STYLES_MATCH_START_P(table, a, b, mask) \
192 ((SYNTAX_COMMENT_BITS (table, a) & SYNTAX_FIRST_CHAR_START & (mask)) \
193 && (SYNTAX_COMMENT_BITS (table, b) & SYNTAX_SECOND_CHAR_START & (mask)))
195 #define SYNTAX_STYLES_MATCH_END_P(table, a, b, mask) \
196 ((SYNTAX_COMMENT_BITS (table, a) & SYNTAX_FIRST_CHAR_END & (mask)) \
197 && (SYNTAX_COMMENT_BITS (table, b) & SYNTAX_SECOND_CHAR_END & (mask)))
199 #define SYNTAX_STYLES_MATCH_1CHAR_P(table, a, mask) \
200 ((SYNTAX_COMMENT_BITS (table, a) & (mask)))
202 #define STYLE_FOUND_P(table, a, b, startp, style) \
203 ((SYNTAX_COMMENT_BITS (table, a) & \
204 ((startp) ? SYNTAX_FIRST_CHAR_START : \
205 SYNTAX_FIRST_CHAR_END) & (style)) \
206 && (SYNTAX_COMMENT_BITS (table, b) & \
207 ((startp) ? SYNTAX_SECOND_CHAR_START : \
208 SYNTAX_SECOND_CHAR_END) & (style)))
210 #define SYNTAX_COMMENT_MASK_START(table, a, b) \
211 ((STYLE_FOUND_P (table, a, b, 1, SYNTAX_COMMENT_STYLE_A) \
212 ? SYNTAX_COMMENT_STYLE_A \
213 : (STYLE_FOUND_P (table, a, b, 1, SYNTAX_COMMENT_STYLE_B) \
214 ? SYNTAX_COMMENT_STYLE_B \
217 #define SYNTAX_COMMENT_MASK_END(table, a, b) \
218 ((STYLE_FOUND_P (table, a, b, 0, SYNTAX_COMMENT_STYLE_A) \
219 ? SYNTAX_COMMENT_STYLE_A \
220 : (STYLE_FOUND_P (table, a, b, 0, SYNTAX_COMMENT_STYLE_B) \
221 ? SYNTAX_COMMENT_STYLE_B \
224 #define STYLE_FOUND_1CHAR_P(table, a, style) \
225 ((SYNTAX_COMMENT_BITS (table, a) & (style)))
227 #define SYNTAX_COMMENT_1CHAR_MASK(table, a) \
228 ((STYLE_FOUND_1CHAR_P (table, a, SYNTAX_COMMENT_STYLE_A) \
229 ? SYNTAX_COMMENT_STYLE_A \
230 : (STYLE_FOUND_1CHAR_P (table, a, SYNTAX_COMMENT_STYLE_B) \
231 ? SYNTAX_COMMENT_STYLE_B \
234 EXFUN (Fchar_syntax, 2);
235 EXFUN (Fforward_word, 2);
237 /* The standard syntax table is stored where it will automatically
238 be used in all new buffers. */
239 extern Lisp_Object Vstandard_syntax_table;
241 /* This array, indexed by a character, contains the syntax code which
242 that character signifies (as a char).
243 For example, (enum syntaxcode) syntax_spec_code['w'] is Sword. */
245 extern const unsigned char syntax_spec_code[0400];
247 /* Indexed by syntax code, give the letter that describes it. */
249 extern const unsigned char syntax_code_spec[];
251 Lisp_Object scan_lists (struct buffer *buf, Bufpos from, int count,
252 int depth, int sexpflag, int no_error);
253 int char_quoted (struct buffer *buf, Bufpos pos);
255 /* NOTE: This does not refer to the mirror table, but to the
256 syntax table itself. */
257 Lisp_Object syntax_match (Lisp_Object table, Emchar ch);
259 extern int no_quit_in_re_search;
260 extern struct buffer *regex_emacs_buffer;
262 /* This is the string or buffer in which we are matching. It is used
263 for looking up syntax properties. */
264 extern Lisp_Object regex_match_object;
266 void update_syntax_table (Lisp_Char_Table *ct);
270 extern int lookup_syntax_properties;
274 int use_code; /* Whether to use syntax_code
275 or current_syntax_table. */
276 struct buffer* buffer; /* The buffer the current syntax cache
278 Lisp_Object object; /* The buffer or string the current
279 syntax cache applies to. */
280 int syntax_code; /* Syntax code of current char. */
281 Lisp_Object current_syntax_table; /* Syntax table for current pos. */
282 Lisp_Object old_prop; /* Syntax-table prop at prev pos. */
284 Bufpos next_change; /* Position of the next extent
286 Bufpos prev_change; /* Position of the previous
289 extern struct syntax_cache syntax_cache;
291 void update_syntax_cache (int pos, int count, int init);
293 /* Make syntax cache state good for CHARPOS, assuming it is
294 currently good for a position before CHARPOS. */
295 #define UPDATE_SYNTAX_CACHE_FORWARD(pos) \
296 (lookup_syntax_properties \
297 ? (update_syntax_cache ((pos), 1, 0), 1) \
300 /* Make syntax cache state good for CHARPOS, assuming it is
301 currently good for a position after CHARPOS. */
302 #define UPDATE_SYNTAX_CACHE_BACKWARD(pos) \
303 (lookup_syntax_properties \
304 ? (update_syntax_cache ((pos), -1, 0), 1) \
307 /* Make syntax cache state good for CHARPOS */
308 #define UPDATE_SYNTAX_CACHE(pos) \
309 (lookup_syntax_properties \
310 ? (update_syntax_cache ((pos), 0, 0), 1) \
313 #define SYNTAX_FROM_CACHE(table, c) \
314 SYNTAX_FROM_CODE (SYNTAX_CODE_FROM_CACHE (table, c))
316 #define SYNTAX_CODE_FROM_CACHE(table, c) \
317 ( syntax_cache.use_code \
318 ? syntax_cache.syntax_code \
319 : SYNTAX_CODE (XCHAR_TABLE (syntax_cache.current_syntax_table), \
323 /* Convert the byte offset BYTEPOS into a character position,
324 for the object recorded in syntax_cache with SETUP_SYNTAX_TABLE_FOR_OBJECT.
326 The value is meant for use in the UPDATE_SYNTAX_TABLE... macros.
327 These macros do nothing when parse_sexp_lookup_properties is 0,
328 so we return 0 in that case, for speed. */
329 #define SYNTAX_CACHE_BYTE_TO_CHAR(bytepos) \
330 (! lookup_syntax_properties \
332 : STRINGP (syntax_cache.object) \
333 ? bytecount_to_charcount (XSTRING_DATA (syntax_cache.object), bytepos) \
334 : (BUFFERP (syntax_cache.object) || NILP (syntax_cache.object)) \
335 ? bytind_to_bufpos (syntax_cache.buffer, \
336 bytepos + BI_BUF_BEGV (syntax_cache.buffer)) \
339 #define SYNTAX_CACHE_OBJECT_BYTE_TO_CHAR(obj, buf, bytepos) \
340 (! lookup_syntax_properties \
343 ? bytecount_to_charcount (XSTRING_DATA (obj), bytepos) \
344 : (BUFFERP (obj) || NILP (obj)) \
345 ? bytind_to_bufpos (buf, bytepos + BI_BUF_BEGV (buf)) \
348 #else /* not emacs */
350 #define update_syntax_cache(pos, count, init)
351 #define UPDATE_SYNTAX_CACHE_FORWARD(pos)
352 #define UPDATE_SYNTAX_CACHE_BACKWARD(pos)
353 #define UPDATE_SYNTAX_CACHE(pos)
354 #define SYNTAX_FROM_CACHE SYNTAX
355 #define SYNTAX_CODE_FROM_CACHE SYNTAX_CODE
359 #define SETUP_SYNTAX_CACHE(FROM, COUNT) \
361 syntax_cache.buffer = current_buffer; \
362 syntax_cache.object = Qnil; \
363 syntax_cache.current_syntax_table \
364 = current_buffer->mirror_syntax_table; \
365 syntax_cache.use_code = 0; \
366 if (lookup_syntax_properties) \
367 update_syntax_cache ((COUNT) > 0 ? (FROM) : (FROM) - 1, \
371 #define SETUP_SYNTAX_CACHE_FOR_BUFFER(BUFFER, FROM, COUNT) \
373 syntax_cache.buffer = (BUFFER); \
374 syntax_cache.object = Qnil; \
375 syntax_cache.current_syntax_table = \
376 syntax_cache.buffer->mirror_syntax_table; \
377 syntax_cache.use_code = 0; \
378 if (lookup_syntax_properties) \
379 update_syntax_cache ((FROM) + ((COUNT) > 0 ? 0 : -1), \
383 #define SETUP_SYNTAX_CACHE_FOR_OBJECT(OBJECT, BUFFER, FROM, COUNT) \
385 syntax_cache.buffer = (BUFFER); \
386 syntax_cache.object = (OBJECT); \
387 if (NILP (syntax_cache.object)) \
391 else if (EQ (syntax_cache.object, Qt)) \
395 else if (STRINGP (syntax_cache.object)) \
399 else if (BUFFERP (syntax_cache.object)) \
401 syntax_cache.buffer = XBUFFER (syntax_cache.object); \
405 /* OBJECT must be buffer/string/t/nil */ \
408 syntax_cache.current_syntax_table \
409 = syntax_cache.buffer->mirror_syntax_table; \
410 syntax_cache.use_code = 0; \
411 if (lookup_syntax_properties) \
412 update_syntax_cache ((FROM) + ((COUNT) > 0 ? 0 : -1), \
416 #define SYNTAX_CODE_PREFIX(c) \
419 #define SYNTAX_CODE_COMMENT_BITS(c) \
422 #define SYNTAX_CODES_START_P(a, b) \
423 (((SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_START) >> 2) \
424 & (SYNTAX_CODE_COMMENT_BITS (b) & SYNTAX_SECOND_CHAR_START))
426 #define SYNTAX_CODES_END_P(a, b) \
427 (((SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_END) >> 2) \
428 & (SYNTAX_CODE_COMMENT_BITS (b) & SYNTAX_SECOND_CHAR_END))
430 #define SYNTAX_CODES_COMMENT_MASK_START(a, b) \
431 (SYNTAX_CODES_MATCH_START_P (a, b, SYNTAX_COMMENT_STYLE_A) \
432 ? SYNTAX_COMMENT_STYLE_A \
433 : (SYNTAX_CODES_MATCH_START_P (a, b, SYNTAX_COMMENT_STYLE_B) \
434 ? SYNTAX_COMMENT_STYLE_B \
436 #define SYNTAX_CODES_COMMENT_MASK_END(a, b) \
437 (SYNTAX_CODES_MATCH_END_P (a, b, SYNTAX_COMMENT_STYLE_A) \
438 ? SYNTAX_COMMENT_STYLE_A \
439 : (SYNTAX_CODES_MATCH_END_P (a, b, SYNTAX_COMMENT_STYLE_B) \
440 ? SYNTAX_COMMENT_STYLE_B \
443 #define SYNTAX_CODE_START_FIRST_P(a) \
444 (SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_START)
446 #define SYNTAX_CODE_START_SECOND_P(a) \
447 (SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_SECOND_CHAR_START)
449 #define SYNTAX_CODE_END_FIRST_P(a) \
450 (SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_END)
452 #define SYNTAX_CODE_END_SECOND_P(a) \
453 (SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_SECOND_CHAR_END)
456 #define SYNTAX_CODES_MATCH_START_P(a, b, mask) \
457 ((SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_START & (mask)) \
458 && (SYNTAX_CODE_COMMENT_BITS (b) \
459 & SYNTAX_SECOND_CHAR_START & (mask)))
461 #define SYNTAX_CODES_MATCH_END_P(a, b, mask) \
462 ((SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_END & (mask)) \
463 && (SYNTAX_CODE_COMMENT_BITS (b) & SYNTAX_SECOND_CHAR_END & (mask)))
465 #define SYNTAX_CODE_MATCHES_1CHAR_P(a, mask) \
466 ((SYNTAX_CODE_COMMENT_BITS (a) & (mask)))
468 #define SYNTAX_CODE_COMMENT_1CHAR_MASK(a) \
469 ((SYNTAX_CODE_MATCHES_1CHAR_P (a, SYNTAX_COMMENT_STYLE_A) \
470 ? SYNTAX_COMMENT_STYLE_A \
471 : (SYNTAX_CODE_MATCHES_1CHAR_P (a, SYNTAX_COMMENT_STYLE_B) \
472 ? SYNTAX_COMMENT_STYLE_B \
476 #endif /* INCLUDED_syntax_h_ */