XEmacs 21.2.45 "Thelxepeia".
[chise/xemacs-chise.git.1] / src / syntax.h
1 /* Declarations having to do with XEmacs syntax tables.
2    Copyright (C) 1985, 1992, 1993 Free Software Foundation, Inc.
3
4 This file is part of XEmacs.
5
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
9 later version.
10
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
14 for more details.
15
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.  */
20
21 /* Synched up with: FSF 19.28. */
22
23 #ifndef INCLUDED_syntax_h_
24 #define INCLUDED_syntax_h_
25
26 #include "chartab.h"
27
28 /* A syntax table is a type of char table.
29
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).
32
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.
37
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.
46 */
47
48 enum syntaxcode
49 {
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 */
69 };
70
71 enum syntaxcode charset_syntax (struct buffer *buf, Lisp_Object charset,
72                                 int *multi_p_out);
73
74 /* Return the syntax code for a particular character and mirror table. */
75
76 #define SYNTAX_CODE_UNSAFE(table, c) \
77    XINT (CHAR_TABLE_VALUE_UNSAFE (table, c))
78
79 INLINE_HEADER int SYNTAX_CODE (Lisp_Char_Table *table, Emchar c);
80 INLINE_HEADER int
81 SYNTAX_CODE (Lisp_Char_Table *table, Emchar c)
82 {
83   return SYNTAX_CODE_UNSAFE (table, c);
84 }
85
86 #define SYNTAX_UNSAFE(table, c) \
87   ((enum syntaxcode) (SYNTAX_CODE_UNSAFE (table, c) & 0177))
88
89 #define SYNTAX_FROM_CODE(code) ((enum syntaxcode) ((code) & 0177))
90 #define SYNTAX(table, c) SYNTAX_FROM_CODE (SYNTAX_CODE (table, c))
91
92 INLINE_HEADER int WORD_SYNTAX_P (Lisp_Char_Table *table, Emchar c);
93 INLINE_HEADER int
94 WORD_SYNTAX_P (Lisp_Char_Table *table, Emchar c)
95 {
96   return SYNTAX (table, c) == Sword;
97 }
98
99 /* OK, here's a graphic diagram of the format of the syntax values:
100
101    Bit number:
102
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 ]
105
106    <-----> <-----> <-------------> <-------------> ^  <----------->
107     ELisp  unused  |comment bits |     unused      |   syntax code
108      tag           | | | | | | | |                 |
109     stuff          | | | | | | | |                 |
110                    | | | | | | | |                 |
111                    | | | | | | | |                 `--> prefix flag
112                    | | | | | | | |
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
121
122   In a 64-bit integer, there would be 32 more unused bits between
123   the tag and the comment bits.
124
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
127   we don't got.
128
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.)
133 */
134
135
136 /* The prefix flag bit for backward-prefix-chars is now put into bit 7. */
137
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)
142
143 /* Bits 23-16 are used to implement up to two comment styles
144    in a single buffer. They have the following meanings:
145
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.
154  */
155
156 #define SYNTAX_COMMENT_BITS(table, c) \
157   ((SYNTAX_CODE (table, c) >> 16) &0xff)
158
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
167
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
176
177
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))
184
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))
190
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)))
194
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)))
198
199 #define SYNTAX_STYLES_MATCH_1CHAR_P(table, a, mask)     \
200   ((SYNTAX_COMMENT_BITS (table, a) & (mask)))
201
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)))
209
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                               \
215          : 0)))
216
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                                  \
222       : 0)))
223
224 #define STYLE_FOUND_1CHAR_P(table, a, style)    \
225   ((SYNTAX_COMMENT_BITS (table, a) & (style)))
226
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                                  \
232          : 0)))
233
234 EXFUN (Fchar_syntax, 2);
235 EXFUN (Fforward_word, 2);
236
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;
240
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. */
244
245 extern const unsigned char syntax_spec_code[0400];
246
247 /* Indexed by syntax code, give the letter that describes it. */
248
249 extern const unsigned char syntax_code_spec[];
250
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);
254
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);
258
259 extern int no_quit_in_re_search;
260 extern struct buffer *regex_emacs_buffer;
261
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;
265
266 void update_syntax_table (Lisp_Char_Table *ct);
267
268 #ifdef emacs
269
270 extern int lookup_syntax_properties;
271
272 struct syntax_cache
273 {
274   int use_code;                         /* Whether to use syntax_code
275                                            or current_syntax_table. */
276   struct buffer* buffer;                /* The buffer the current syntax cache
277                                            applies to. */
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. */
283
284   Bufpos next_change;                   /* Position of the next extent
285                                            change. */
286   Bufpos prev_change;                   /* Position of the previous
287                                            extent change. */
288 };
289 extern struct syntax_cache syntax_cache;
290
291 void update_syntax_cache (int pos, int count, int init);
292
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)    \
298     : 0)
299
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)   \
305     : 0)
306
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)    \
311     : 0)
312
313 #define SYNTAX_FROM_CACHE(table, c)                     \
314    SYNTAX_FROM_CODE (SYNTAX_CODE_FROM_CACHE (table, c))
315
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),   \
320                      c)                                                 \
321  )
322
323 /* Convert the byte offset BYTEPOS into a character position,
324    for the object recorded in syntax_cache with SETUP_SYNTAX_TABLE_FOR_OBJECT.
325
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                                                   \
331    ? 0                                                                          \
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))             \
337    : (bytepos))
338
339 #define SYNTAX_CACHE_OBJECT_BYTE_TO_CHAR(obj, buf, bytepos)     \
340   (! lookup_syntax_properties                                   \
341    ? 0                                                          \
342    : STRINGP (obj)                                              \
343    ? bytecount_to_charcount (XSTRING_DATA (obj), bytepos)       \
344    : (BUFFERP (obj) || NILP (obj))                              \
345    ? bytind_to_bufpos (buf, bytepos + BI_BUF_BEGV (buf))        \
346    : (bytepos))
347
348 #else  /* not emacs */
349
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
356
357 #endif /* emacs */
358
359 #define SETUP_SYNTAX_CACHE(FROM, COUNT)                         \
360   do {                                                          \
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,   \
368                            (COUNT), 1);                         \
369   } while (0)
370
371 #define SETUP_SYNTAX_CACHE_FOR_BUFFER(BUFFER, FROM, COUNT)      \
372   do {                                                          \
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),     \
380                            (COUNT), 1);                         \
381   } while (0)
382
383 #define SETUP_SYNTAX_CACHE_FOR_OBJECT(OBJECT, BUFFER, FROM, COUNT)      \
384   do {                                                                  \
385     syntax_cache.buffer = (BUFFER);                                     \
386     syntax_cache.object = (OBJECT);                                     \
387     if (NILP (syntax_cache.object))                                     \
388       {                                                                 \
389         /* do nothing */;                                               \
390       }                                                                 \
391     else if (EQ (syntax_cache.object, Qt))                              \
392       {                                                                 \
393         /* do nothing */;                                               \
394       }                                                                 \
395     else if (STRINGP (syntax_cache.object))                             \
396       {                                                                 \
397         /* do nothing */;                                               \
398       }                                                                 \
399     else if (BUFFERP (syntax_cache.object))                             \
400       {                                                                 \
401         syntax_cache.buffer = XBUFFER (syntax_cache.object);            \
402       }                                                                 \
403     else                                                                \
404       {                                                                 \
405         /* OBJECT must be buffer/string/t/nil */                        \
406         assert(0);                                                      \
407       }                                                                 \
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),             \
413                            (COUNT), 1);                                 \
414   } while (0)
415
416 #define SYNTAX_CODE_PREFIX(c) \
417   ((c >> 7) & 1)
418
419 #define SYNTAX_CODE_COMMENT_BITS(c) \
420   ((c >> 16) &0xff)
421
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))
425
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))
429
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                                  \
435       : 0))
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                                  \
441       : 0))
442
443 #define SYNTAX_CODE_START_FIRST_P(a) \
444   (SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_START)
445
446 #define SYNTAX_CODE_START_SECOND_P(a) \
447   (SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_SECOND_CHAR_START)
448
449 #define SYNTAX_CODE_END_FIRST_P(a) \
450   (SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_END)
451
452 #define SYNTAX_CODE_END_SECOND_P(a) \
453   (SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_SECOND_CHAR_END)
454
455
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)))
460
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)))
464
465 #define SYNTAX_CODE_MATCHES_1CHAR_P(a, mask)    \
466   ((SYNTAX_CODE_COMMENT_BITS (a) & (mask)))
467
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                                 \
473        : 0)))
474
475
476 #endif /* INCLUDED_syntax_h_ */