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