Merge r21-4-11-chise-0_20-=ucs.
[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    Copyright (C) 2001 MORIOKA Tomohiko
4
5 This file is part of XEmacs.
6
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
10 later version.
11
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
15 for more details.
16
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.  */
21
22 /* Synched up with: FSF 19.28. */
23
24 #ifndef INCLUDED_syntax_h_
25 #define INCLUDED_syntax_h_
26
27 #include "chartab.h"
28
29 /* A syntax table is a type of char table.
30
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).
33
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.
38
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.
47 */
48
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;
52
53 enum syntaxcode
54 {
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 */
74 };
75
76 enum syntaxcode charset_syntax (struct buffer *buf, Lisp_Object charset,
77                                 int *multi_p_out);
78
79 /* Return the syntax code for a particular character and mirror table. */
80
81 #ifdef UTF2000
82 INLINE_HEADER int SYNTAX_CODE_UNSAFE (Lisp_Char_Table *table, Emchar c);
83 INLINE_HEADER int
84 SYNTAX_CODE_UNSAFE (Lisp_Char_Table *table, Emchar c)
85 {
86   int code = CHAR_TABLE_VALUE_UNSAFE (table, c);
87   int ret = Spunct;
88
89   if (CONSP (code))
90     code = XCAR (code);
91   ret = XINT (code);
92
93   if (ret == Sinherit)
94     {
95       code = CHAR_TABLE_VALUE_UNSAFE (XCHAR_TABLE
96                                       (Vstandard_syntax_table), c);
97       if (CONSP (code))
98         code = XCAR (code);
99       return XINT (code);
100     }
101   else
102     return ret;
103 }
104 #else
105 #define SYNTAX_CODE_UNSAFE(table, c) \
106    XINT (CHAR_TABLE_VALUE_UNSAFE (table, c))
107 #endif
108
109 INLINE_HEADER int SYNTAX_CODE (Lisp_Char_Table *table, Emchar c);
110 INLINE_HEADER int
111 SYNTAX_CODE (Lisp_Char_Table *table, Emchar c)
112 {
113   return SYNTAX_CODE_UNSAFE (table, c);
114 }
115
116 #define SYNTAX_UNSAFE(table, c) \
117   ((enum syntaxcode) (SYNTAX_CODE_UNSAFE (table, c) & 0177))
118
119 #define SYNTAX_FROM_CODE(code) ((enum syntaxcode) ((code) & 0177))
120 #define SYNTAX(table, c) SYNTAX_FROM_CODE (SYNTAX_CODE (table, c))
121
122 INLINE_HEADER int WORD_SYNTAX_P (Lisp_Char_Table *table, Emchar c);
123 INLINE_HEADER int
124 WORD_SYNTAX_P (Lisp_Char_Table *table, Emchar c)
125 {
126   return SYNTAX (table, c) == Sword;
127 }
128
129 /* OK, here's a graphic diagram of the format of the syntax values:
130
131    Bit number:
132
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 ]
135
136    <-----> <-----> <-------------> <-------------> ^  <----------->
137     ELisp  unused  |comment bits |     unused      |   syntax code
138      tag           | | | | | | | |                 |
139     stuff          | | | | | | | |                 |
140                    | | | | | | | |                 |
141                    | | | | | | | |                 `--> prefix flag
142                    | | | | | | | |
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
151
152   In a 64-bit integer, there would be 32 more unused bits between
153   the tag and the comment bits.
154
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
157   we don't got.
158
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.)
163 */
164
165
166 /* The prefix flag bit for backward-prefix-chars is now put into bit 7. */
167
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)
172
173 /* Bits 23-16 are used to implement up to two comment styles
174    in a single buffer. They have the following meanings:
175
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.
184
185 From the internals manual:
186
187 Syntax codes are implemented as bitfields in an int.  Bits 0-6 contain
188 the syntax code itself, bit 7 is a special prefix flag used for Lisp,
189 and bits 16-23 contain comment syntax flags.  From the Lisp programmer's
190 point of view, there are 11 flags: 2 styles X 2 characters X @{start,
191 end@} flags for two-character comment delimiters, 2 style flags for
192 one-character comment delimiters, and the prefix flag.
193
194 Internally, however, the characters used in multi-character delimiters
195 will have non-comment-character syntax classes (@emph{e.g.}, the
196 @samp{/} in C's @samp{/}@samp{*} comment-start delimiter has ``punctuation''
197 \(here meaning ``operator-like'') class in C modes).  Thus in a mixed
198 comment style, such as C++'s @samp{//} to end of line, is represented by
199 giving @samp{/} the ``punctuation'' class and the ``style b first
200 character of start sequence'' and ``style b second character of start
201 sequence'' flags.  The fact that class is @emph{not} punctuation allows
202 the syntax scanner to recognize that this is a multi-character
203 delimiter.  The @samp{newline} character is given (single-character)
204 ``comment-end'' @emph{class} and the ``style b first character of end
205 sequence'' @emph{flag}.  The ``comment-end'' class allows the scanner to
206 determine that no second character is needed to terminate the comment.
207  */
208
209 #define SYNTAX_COMMENT_BITS(c) \
210   ((SYNTAX_CODE (mirrortab, c) >> 16) &0xff)
211
212 #define SYNTAX_FIRST_OF_START_A  0x80
213 #define SYNTAX_FIRST_OF_START_B  0x40
214 #define SYNTAX_SECOND_OF_START_A 0x20
215 #define SYNTAX_SECOND_OF_START_B 0x10
216 #define SYNTAX_FIRST_OF_END_A    0x08
217 #define SYNTAX_FIRST_OF_END_B    0x04
218 #define SYNTAX_SECOND_OF_END_A   0x02
219 #define SYNTAX_SECOND_OF_END_B   0x01
220
221 #define SYNTAX_COMMENT_STYLE_A   0xaa
222 #define SYNTAX_COMMENT_STYLE_B   0x55
223 #define SYNTAX_FIRST_CHAR_START  0xc0
224 #define SYNTAX_FIRST_CHAR_END    0x0c
225 #define SYNTAX_FIRST_CHAR        0xcc
226 #define SYNTAX_SECOND_CHAR_START 0x30
227 #define SYNTAX_SECOND_CHAR_END   0x03
228 #define SYNTAX_SECOND_CHAR       0x33
229
230
231 /* #### These are now more or less equivalent to
232    SYNTAX_COMMENT_MATCH_START ...*/
233 /* a and b must be first and second start chars for a common type */
234 #define SYNTAX_START_P(a, b)                                     \
235   (((SYNTAX_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_START) >> 2)    \
236    & (SYNTAX_COMMENT_BITS (b) & SYNTAX_SECOND_CHAR_START))
237
238 /* ... and  SYNTAX_COMMENT_MATCH_END */
239 /* a and b must be first and second end chars for a common type */
240 #define SYNTAX_END_P(a, b)                                       \
241   (((SYNTAX_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_END) >> 2)      \
242    & (SYNTAX_COMMENT_BITS (b) & SYNTAX_SECOND_CHAR_END))
243
244 #define SYNTAX_STYLES_MATCH_START_P(a, b, mask)                 \
245   ((SYNTAX_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_START & (mask)) \
246    && (SYNTAX_COMMENT_BITS (b) & SYNTAX_SECOND_CHAR_START & (mask)))
247
248 #define SYNTAX_STYLES_MATCH_END_P(a, b, mask)                   \
249   ((SYNTAX_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_END & (mask))   \
250    && (SYNTAX_COMMENT_BITS (b) & SYNTAX_SECOND_CHAR_END & (mask)))
251
252 #define SYNTAX_STYLES_MATCH_1CHAR_P(a, mask)    \
253   ((SYNTAX_COMMENT_BITS (a) & (mask)))
254
255 #define STYLE_FOUND_P(a, b, startp, style)              \
256   ((SYNTAX_COMMENT_BITS (a) &                           \
257     ((startp) ? SYNTAX_FIRST_CHAR_START :               \
258      SYNTAX_FIRST_CHAR_END) & (style))                  \
259    && (SYNTAX_COMMENT_BITS (b) &                        \
260     ((startp) ? SYNTAX_SECOND_CHAR_START :              \
261      SYNTAX_SECOND_CHAR_END) & (style)))
262
263 #define SYNTAX_COMMENT_MASK_START(a, b)                 \
264   ((STYLE_FOUND_P (a, b, 1, SYNTAX_COMMENT_STYLE_A)     \
265     ? SYNTAX_COMMENT_STYLE_A                            \
266     : (STYLE_FOUND_P (a, b, 1, SYNTAX_COMMENT_STYLE_B)  \
267          ? SYNTAX_COMMENT_STYLE_B                       \
268          : 0)))
269
270 #define SYNTAX_COMMENT_MASK_END(a, b)                   \
271   ((STYLE_FOUND_P (a, b, 0, SYNTAX_COMMENT_STYLE_A)     \
272    ? SYNTAX_COMMENT_STYLE_A                             \
273    : (STYLE_FOUND_P (a, b, 0, SYNTAX_COMMENT_STYLE_B)   \
274       ? SYNTAX_COMMENT_STYLE_B                          \
275       : 0)))
276
277 #define STYLE_FOUND_1CHAR_P(a, style)   \
278   ((SYNTAX_COMMENT_BITS (a) & (style)))
279
280 #define SYNTAX_COMMENT_1CHAR_MASK(a)                    \
281   ((STYLE_FOUND_1CHAR_P (a, SYNTAX_COMMENT_STYLE_A)     \
282    ? SYNTAX_COMMENT_STYLE_A                             \
283    : (STYLE_FOUND_1CHAR_P (a, SYNTAX_COMMENT_STYLE_B)   \
284       ? SYNTAX_COMMENT_STYLE_B                          \
285          : 0)))
286
287 EXFUN (Fchar_syntax, 2);
288 EXFUN (Fforward_word, 2);
289
290 /* This array, indexed by a character, contains the syntax code which
291    that character signifies (as a char).
292    For example, (enum syntaxcode) syntax_spec_code['w'] is Sword. */
293
294 extern const unsigned char syntax_spec_code[0400];
295
296 /* Indexed by syntax code, give the letter that describes it. */
297
298 extern const unsigned char syntax_code_spec[];
299
300 Lisp_Object scan_lists (struct buffer *buf, Bufpos from, int count,
301                         int depth, int sexpflag, int no_error);
302 int char_quoted (struct buffer *buf, Bufpos pos);
303
304 /* NOTE: This does not refer to the mirror table, but to the
305    syntax table itself. */
306 Lisp_Object syntax_match (Lisp_Object table, Emchar ch);
307
308 extern int no_quit_in_re_search;
309 extern struct buffer *regex_emacs_buffer;
310
311 /* Target text (string or buffer), used for syntax-table properties. */
312 extern Lisp_Object regex_match_object;
313
314 #ifndef UTF2000
315 void update_syntax_table (Lisp_Char_Table *ct);
316 #endif
317
318 /* The syntax table cache */
319
320 /*
321    The *-single-property-change versions turn out to be unbearably slow.
322    Do not enable them in a production or distribution version.
323 */
324 #define NEXT_SINGLE_PROPERTY_CHANGE             0
325 #define PREVIOUS_SINGLE_PROPERTY_CHANGE         0
326
327 /* Test instruments, used in macros below.
328    Define SYNTAX_CACHE_STATISTICS to enable them. */
329 /* #undef SYNTAX_CACHE_STATISTICS */
330
331 #ifdef SYNTAX_CACHE_STATISTICS
332 #define SYNTAX_CACHE_STATISTICS_REPORT_INTERVAL 100000
333
334 enum syntax_cache_statistics_functions {
335   scs_no_function = -1,
336   scs_find_context = 0,
337   scs_find_defun_start,
338   scs_scan_words,
339   scs_Fforward_comment,
340   scs_scan_lists,
341   scs_Fbackward_prefix_characters,
342   scs_scan_sexps_forward,
343   scs_number_of_functions
344 };
345
346 /* keep this in synch with syntax.c */
347 extern char* syntax_cache_statistics_function_names[scs_number_of_functions];
348
349 struct syntax_cache_statistics {
350   /* inits + misses_hi + misses_lo + #HITS = total_updates */
351   int total_updates;
352   int inits;
353   int misses_lo;
354   int misses_hi;
355   int min_length;
356   int max_length;
357   double mean_length;
358   double mean_length_on_miss;
359   enum syntax_cache_statistics_functions this_function;
360   int functions[scs_number_of_functions];
361 };
362
363 extern struct syntax_cache_statistics scs_statistics;
364
365 #define SCS_STATISTICS_SET_FUNCTION(fndx) scs_statistics.this_function = fndx
366 /* used in macros below */
367 #define SYNTAX_CACHE_STATISTICS_COUNT_INIT scs_statistics.inits++
368
369 #else
370
371 #define SCS_STATISTICS_SET_FUNCTION(fndx)
372 #define SYNTAX_CACHE_STATISTICS_COUNT_INIT
373
374 #endif /* SYNTAX_CACHE_STATISTICS */
375
376 /* Theory of the syntax table cache
377
378    This cache cooperates with but is conceptually different from the
379    mirror table.  The mirror table precomputes (and caches, if you like)
380    the syntax codes for characters in a given syntax table, taking into
381    account possible inheritance from a table given by a parent text object.
382    The syntax table cache checks for overriding tables defined by
383    _subobjects_.
384
385    This implementation defines the "subobjects" by _extent properties_.
386    We may restrict them to _text_ properties.  There are two lookup
387    styles for the cache, "single code" and "full table".  In the "single
388    code" style, a given syntax code, kept in the `syntax_code' member, is
389    applied to the entire range (#### check this).  In the "full table"
390    style, a syntax table kept in the `current_syntax_table' member is
391    checked for each character in the range.  If the flag `use_code' is
392    non-zero, the "single code" is used, otherwise the "full table".
393
394    The cache is valid for the range `[prev_change, next_change)' in the
395    text object (buffer or string) `object'.
396
397    If the current position is outside the range valid for the cache, the
398    cache is updated by checking for the text property `syntax-table'.  If
399    present, its value is either a syntax code or a syntax table, and the
400    appropriate member and `use_code' are updated accordingly.  If absent
401    or nil, the default syntax table from the `buffer' member is used.  The
402    extent of the property is used to reinitialize the cache's validity
403    range.  (We would like to improve this by checking the property value
404    against `old_prop', and if the same, extend the validity range of the
405    cache by the extent of the property.)
406
407    Note: the values Qt and Qnil for `object' are not supported in this
408    implementation.  GNU Emacs uses them for reasons not yet (####) clear.
409 */
410
411 extern int lookup_syntax_properties;
412
413 struct syntax_cache
414 {
415   int use_code;                         /* Whether to use syntax_code
416                                            or current_syntax_table. */
417   struct buffer* buffer;                /* The buffer providing the default
418                                            syntax table to the cache. */
419   Lisp_Object object;                   /* The buffer or string the current
420                                            syntax cache applies to. */
421   int syntax_code;                      /* Syntax code of current char. */
422   Lisp_Object current_syntax_table;     /* Syntax table for current pos. */
423   Lisp_Object old_prop;                 /* Syntax-table prop at prev pos. */
424
425   Bufpos next_change;                   /* Position of the next extent
426                                            change. */
427   Bufpos prev_change;                   /* Position of the previous
428                                            extent change. */
429 };
430 extern struct syntax_cache syntax_cache;
431
432 /*
433    The macros below handle the internal structure of the cache.
434    ALWAYS USE THE MACROS TO MANIPULATE THE CACHE.
435
436    o Use the SETUP_SYNTAX_CACHE* macros to set the object and buffer members.
437
438      OBJECT is either a Lisp buffer or a Lisp string.  BUFFER is a
439      pointer to struct buffer.  If OBJECT is a buffer, it must refer to
440      BUFFER.  If OBJECT is a string, then BUFFER will supply the default
441      syntax table when the `syntax-table' property is nil.
442
443      For convenience and backward compatibility, the values Qt and Qnil are
444      accepted for OBJECT.  These are taken to refer to the current buffer,
445      and that substitution is made immediately.  The value Qt is treated
446      specially in the *BYTE_TO_CHAR macros below.  This appears (####) to
447      be a GNU kludge related to `enable-multibyte-characters' and was used
448      only in dired.c.
449
450      FROM is the starting character position in OBJECT.
451      COUNT is currently used only as a flag.  If positive, we are proceeding
452      forward through OBJECT, otherwise in reverse.
453
454    o All other members are updated using the update_syntax_cache
455      function, normally wrapped in the UPDATE_SYNTAX_CACHE* macros.
456 */
457
458 void update_syntax_cache (int pos, int count);
459
460 /* in one example the high misses vastly outweigh the low ones
461    seems plausible, since we typically are moving forward through the buffer */
462 #define UPDATE_SYNTAX_CACHE_INTERNAL(pos, dir)  \
463    ((lookup_syntax_properties &&                \
464      (pos >= syntax_cache.next_change ||                \
465       pos < syntax_cache.prev_change))          \
466     ? (update_syntax_cache ((pos), dir), 1)     \
467     : 0)
468
469 /* In the current implementation, all of the following are identical. */
470 /* Make syntax cache state good for CHARPOS, assuming it is
471    currently good for a position before CHARPOS.  */
472 #define UPDATE_SYNTAX_CACHE_FORWARD(pos) UPDATE_SYNTAX_CACHE_INTERNAL(pos, 1)
473
474 /* Make syntax cache state good for CHARPOS, assuming it is
475    currently good for a position after CHARPOS.  */
476 #define UPDATE_SYNTAX_CACHE_BACKWARD(pos) UPDATE_SYNTAX_CACHE_INTERNAL(pos, -1)
477
478 /* Make syntax cache state good for CHARPOS */
479 #define UPDATE_SYNTAX_CACHE(pos) UPDATE_SYNTAX_CACHE_INTERNAL(pos, 0)
480
481 #define SYNTAX_FROM_CACHE(table, c)                     \
482    SYNTAX_FROM_CODE (SYNTAX_CODE_FROM_CACHE (table, c))
483
484 #define SYNTAX_CODE_FROM_CACHE(table, c)                                \
485   ( syntax_cache.use_code                                               \
486       ? syntax_cache.syntax_code                                        \
487       : SYNTAX_CODE (XCHAR_TABLE (syntax_cache.current_syntax_table),   \
488                      c)                                                 \
489  )
490
491 /* Convert the byte offset BYTEPOS into a character position,
492    for the object recorded in syntax_cache with SETUP_SYNTAX_CACHE*.
493
494    The value is meant for use in the UPDATE_SYNTAX_CACHE... macros.
495    These macros do nothing when lookup_syntax_properties is 0,
496    so we return 0 in that case, for speed.
497
498    The default case does no conversion; this seems (####) to be an
499    evil hangover from GNU Emacs. */
500 #define SYNTAX_CACHE_OBJECT_BYTE_TO_CHAR(obj, buf, bytepos)     \
501   (! lookup_syntax_properties                                   \
502    ? 0                                                          \
503    : STRINGP (obj)                                              \
504    ? bytecount_to_charcount (XSTRING_DATA (obj), bytepos)       \
505    : (BUFFERP (obj) || NILP (obj))                              \
506    ? bytind_to_bufpos (buf, bytepos + BI_BUF_BEGV (buf))        \
507    : (bytepos))
508
509 #define SYNTAX_CACHE_BYTE_TO_CHAR(bytepos)                                     \
510   SYNTAX_CACHE_OBJECT_BYTE_TO_CHAR (syntax_cache.object, syntax_cache.buffer,  \
511                                     (bytepos))
512
513 #define SETUP_SYNTAX_CACHE(FROM, COUNT)                         \
514   SETUP_SYNTAX_CACHE_FOR_BUFFER (current_buffer, (FROM), (COUNT))
515
516 #define SETUP_SYNTAX_CACHE_FOR_BUFFER(BUFFER, FROM, COUNT)      \
517   SETUP_SYNTAX_CACHE_FOR_OBJECT (Qnil, (BUFFER), (FROM), (COUNT))
518
519 #ifdef UTF2000
520 #define SETUP_SYNTAX_CACHE_FOR_OBJECT(OBJECT, BUFFER, FROM, COUNT)      \
521   do {                                                                  \
522     syntax_cache.buffer = (BUFFER);                                     \
523     syntax_cache.object = (OBJECT);                                     \
524     if (NILP (syntax_cache.object))                                     \
525       {                                                                 \
526         XSETBUFFER (syntax_cache.object, syntax_cache.buffer);          \
527       }                                                                 \
528     else if (EQ (syntax_cache.object, Qt))                              \
529       {                                                                 \
530         XSETBUFFER (syntax_cache.object, syntax_cache.buffer);          \
531       }                                                                 \
532     else if (STRINGP (syntax_cache.object))                             \
533       {                                                                 \
534         /* do nothing */;                                               \
535       }                                                                 \
536     else if (BUFFERP (syntax_cache.object))                             \
537       {                                                                 \
538         syntax_cache.buffer = XBUFFER (syntax_cache.object);            \
539       }                                                                 \
540     else                                                                \
541       {                                                                 \
542         /* OBJECT must be buffer/string/t/nil */                        \
543         assert(0);                                                      \
544       }                                                                 \
545     syntax_cache.current_syntax_table                                   \
546       = syntax_cache.buffer->syntax_table;                              \
547     syntax_cache.use_code = 0;                                          \
548     if (lookup_syntax_properties)                                       \
549       {                                                                 \
550         SYNTAX_CACHE_STATISTICS_COUNT_INIT;                             \
551         update_syntax_cache ((FROM) + ((COUNT) > 0 ? 0 : -1), (COUNT)); \
552       }                                                                 \
553   } while (0)
554 #else
555 #define SETUP_SYNTAX_CACHE_FOR_OBJECT(OBJECT, BUFFER, FROM, COUNT)      \
556   do {                                                                  \
557     syntax_cache.buffer = (BUFFER);                                     \
558     syntax_cache.object = (OBJECT);                                     \
559     if (NILP (syntax_cache.object))                                     \
560       {                                                                 \
561         XSETBUFFER (syntax_cache.object, syntax_cache.buffer);          \
562       }                                                                 \
563     else if (EQ (syntax_cache.object, Qt))                              \
564       {                                                                 \
565         XSETBUFFER (syntax_cache.object, syntax_cache.buffer);          \
566       }                                                                 \
567     else if (STRINGP (syntax_cache.object))                             \
568       {                                                                 \
569         /* do nothing */;                                               \
570       }                                                                 \
571     else if (BUFFERP (syntax_cache.object))                             \
572       {                                                                 \
573         syntax_cache.buffer = XBUFFER (syntax_cache.object);            \
574       }                                                                 \
575     else                                                                \
576       {                                                                 \
577         /* OBJECT must be buffer/string/t/nil */                        \
578         assert(0);                                                      \
579       }                                                                 \
580     syntax_cache.current_syntax_table                                   \
581       = syntax_cache.buffer->mirror_syntax_table;                       \
582     syntax_cache.use_code = 0;                                          \
583     if (lookup_syntax_properties)                                       \
584       {                                                                 \
585         SYNTAX_CACHE_STATISTICS_COUNT_INIT;                             \
586         update_syntax_cache ((FROM) + ((COUNT) > 0 ? 0 : -1), (COUNT)); \
587       }                                                                 \
588   } while (0)
589 #endif
590
591 #define SYNTAX_CODE_PREFIX(c) \
592   ((c >> 7) & 1)
593
594 #define SYNTAX_CODE_COMMENT_BITS(c) \
595   ((c >> 16) &0xff)
596
597 #define SYNTAX_CODES_START_P(a, b)                                    \
598   (((SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_START) >> 2)    \
599    & (SYNTAX_CODE_COMMENT_BITS (b) & SYNTAX_SECOND_CHAR_START))
600
601 #define SYNTAX_CODES_END_P(a, b)                                    \
602   (((SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_END) >> 2)    \
603    & (SYNTAX_CODE_COMMENT_BITS (b) & SYNTAX_SECOND_CHAR_END))
604
605 #define SYNTAX_CODES_COMMENT_MASK_START(a, b)                   \
606   (SYNTAX_CODES_MATCH_START_P (a, b, SYNTAX_COMMENT_STYLE_A)    \
607    ? SYNTAX_COMMENT_STYLE_A                                     \
608    : (SYNTAX_CODES_MATCH_START_P (a, b, SYNTAX_COMMENT_STYLE_B) \
609       ? SYNTAX_COMMENT_STYLE_B                                  \
610       : 0))
611 #define SYNTAX_CODES_COMMENT_MASK_END(a, b)                     \
612   (SYNTAX_CODES_MATCH_END_P (a, b, SYNTAX_COMMENT_STYLE_A)      \
613    ? SYNTAX_COMMENT_STYLE_A                                     \
614    : (SYNTAX_CODES_MATCH_END_P (a, b, SYNTAX_COMMENT_STYLE_B)   \
615       ? SYNTAX_COMMENT_STYLE_B                                  \
616       : 0))
617
618 #define SYNTAX_CODE_START_FIRST_P(a) \
619   (SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_START)
620
621 #define SYNTAX_CODE_START_SECOND_P(a) \
622   (SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_SECOND_CHAR_START)
623
624 #define SYNTAX_CODE_END_FIRST_P(a) \
625   (SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_END)
626
627 #define SYNTAX_CODE_END_SECOND_P(a) \
628   (SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_SECOND_CHAR_END)
629
630
631 #define SYNTAX_CODES_MATCH_START_P(a, b, mask)                          \
632   ((SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_START & (mask))    \
633    && (SYNTAX_CODE_COMMENT_BITS (b) & SYNTAX_SECOND_CHAR_START & (mask)))
634
635 #define SYNTAX_CODES_MATCH_END_P(a, b, mask)                            \
636   ((SYNTAX_CODE_COMMENT_BITS (a) & SYNTAX_FIRST_CHAR_END & (mask))      \
637    && (SYNTAX_CODE_COMMENT_BITS (b) & SYNTAX_SECOND_CHAR_END & (mask)))
638
639 #define SYNTAX_CODE_MATCHES_1CHAR_P(a, mask)    \
640   ((SYNTAX_CODE_COMMENT_BITS (a) & (mask)))
641
642 #define SYNTAX_CODE_COMMENT_1CHAR_MASK(a)                       \
643   ((SYNTAX_CODE_MATCHES_1CHAR_P (a, SYNTAX_COMMENT_STYLE_A)     \
644     ? SYNTAX_COMMENT_STYLE_A                                    \
645     : (SYNTAX_CODE_MATCHES_1CHAR_P (a, SYNTAX_COMMENT_STYLE_B)  \
646        ? SYNTAX_COMMENT_STYLE_B                                 \
647        : 0)))
648
649 #if 0
650 /* These are the things that need to be #defined away to create a
651    no syntax-table property version. */
652
653 /* This should be entirely encapsulated in macros
654 #define update_syntax_cache(pos, count)
655 */
656 #define lookup_syntax_properties 0
657
658 #define SETUP_SYNTAX_CACHE(FROM, COUNT)
659 #define SETUP_SYNTAX_CACHE_FOR_BUFFER(BUFFER, FROM, COUNT)
660 #define SETUP_SYNTAX_CACHE_FOR_OBJECT(OBJECT, BUFFER, FROM, COUNT)
661 #define UPDATE_SYNTAX_CACHE_FORWARD(pos)
662 #define UPDATE_SYNTAX_CACHE_BACKWARD(pos)
663 #define UPDATE_SYNTAX_CACHE(pos)
664
665 #define SYNTAX_FROM_CACHE SYNTAX
666 #define SYNTAX_CODE_FROM_CACHE SYNTAX_CODE
667
668 #define SYNTAX_CACHE_BYTE_TO_CHAR(x) 0
669
670 /* cache statistics */
671 #define SCS_STATISTICS_SET_FUNCTION(fndx)
672 #define SYNTAX_CACHE_STATISTICS_COUNT_INIT
673
674 #endif /* 0 */
675 #endif /* INCLUDED_syntax_h_ */