Commit | Line | Data |
---|---|---|
9889c728 | 1 | /* Declarations having to do with GNU Emacs syntax tables. |
4a2f9c6a | 2 | Copyright (C) 1985, 93, 94, 97, 1998 Free Software Foundation, Inc. |
9889c728 JB |
3 | |
4 | This file is part of GNU Emacs. | |
5 | ||
6 | GNU Emacs is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
7c938215 | 8 | the Free Software Foundation; either version 2, or (at your option) |
9889c728 JB |
9 | any later version. |
10 | ||
11 | GNU Emacs is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GNU Emacs; see the file COPYING. If not, write to | |
3b7ad313 EN |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
19 | Boston, MA 02111-1307, USA. */ | |
9889c728 JB |
20 | |
21 | ||
22 | extern Lisp_Object Qsyntax_table_p; | |
4c571d09 | 23 | extern void update_syntax_table P_ ((int, int, int, Lisp_Object)); |
9889c728 JB |
24 | |
25 | /* The standard syntax table is stored where it will automatically | |
26 | be used in all new buffers. */ | |
27 | #define Vstandard_syntax_table buffer_defaults.syntax_table | |
28 | ||
e46c910e RS |
29 | /* A syntax table is a chartable whose elements are cons cells |
30 | (CODE+FLAGS . MATCHING-CHAR). MATCHING-CHAR can be nil if the char | |
31 | is not a kind of parenthesis. | |
9889c728 | 32 | |
e46c910e | 33 | The low 8 bits of CODE+FLAGS is a code, as follows: */ |
9889c728 JB |
34 | |
35 | enum syntaxcode | |
36 | { | |
37 | Swhitespace, /* for a whitespace character */ | |
38 | Spunct, /* for random punctuation characters */ | |
39 | Sword, /* for a word constituent */ | |
40 | Ssymbol, /* symbol constituent but not word constituent */ | |
41 | Sopen, /* for a beginning delimiter */ | |
42 | Sclose, /* for an ending delimiter */ | |
43 | Squote, /* for a prefix character like Lisp ' */ | |
44 | Sstring, /* for a string-grouping character like Lisp " */ | |
5eea1c5a | 45 | Smath, /* for delimiters like $ in Tex. */ |
9889c728 JB |
46 | Sescape, /* for a character that begins a C-style escape */ |
47 | Scharquote, /* for a character that quotes the following character */ | |
48 | Scomment, /* for a comment-starting character */ | |
49 | Sendcomment, /* for a comment-ending character */ | |
c8cdcb16 | 50 | Sinherit, /* use the standard syntax table for this character */ |
5eea1c5a | 51 | Scomment_fence, /* Starts/ends comment which is delimited on the |
47ab3db5 | 52 | other side by any char with the same syntaxcode. */ |
5eea1c5a | 53 | Sstring_fence, /* Starts/ends string which is delimited on the |
47ab3db5 | 54 | other side by any char with the same syntaxcode. */ |
9889c728 JB |
55 | Smax /* Upper bound on codes that are meaningful */ |
56 | }; | |
57 | ||
e0b8ff93 | 58 | /* Set the syntax entry VAL for char C in table TABLE. */ |
e46c910e | 59 | |
0e35bfd8 | 60 | #define SET_RAW_SYNTAX_ENTRY(table, c, val) \ |
9ca6ab7d | 61 | ((c) < CHAR_TABLE_SINGLE_BYTE_SLOTS \ |
0e35bfd8 KH |
62 | ? (XCHAR_TABLE (table)->contents[(unsigned char) (c)] = (val)) \ |
63 | : Faset ((table), make_number (c), (val))) | |
e0b8ff93 KH |
64 | |
65 | /* Fetch the syntax entry for char C in syntax table TABLE. | |
66 | This macro is called only when C is less than CHAR_TABLE_ORDINARY_SLOTS. | |
67 | Do inheritance. */ | |
68 | ||
69 | #ifdef __GNUC__ | |
70 | #define SYNTAX_ENTRY_FOLLOW_PARENT(table, c) \ | |
71 | ({ Lisp_Object tbl = table; \ | |
72 | Lisp_Object temp = XCHAR_TABLE (tbl)->contents[(c)]; \ | |
73 | while (NILP (temp)) \ | |
74 | { \ | |
75 | tbl = XCHAR_TABLE (tbl)->parent; \ | |
76 | if (NILP (tbl)) \ | |
77 | break; \ | |
78 | temp = XCHAR_TABLE (tbl)->contents[(c)]; \ | |
79 | } \ | |
80 | temp; }) | |
c8cdcb16 | 81 | #else |
e0b8ff93 | 82 | extern Lisp_Object syntax_temp; |
4c571d09 | 83 | extern Lisp_Object syntax_parent_lookup P_ ((Lisp_Object, int)); |
e46c910e | 84 | |
e0b8ff93 KH |
85 | #define SYNTAX_ENTRY_FOLLOW_PARENT(table, c) \ |
86 | (syntax_temp = XCHAR_TABLE (table)->contents[(c)], \ | |
87 | (NILP (syntax_temp) \ | |
88 | ? syntax_parent_lookup (table, (c)) \ | |
89 | : syntax_temp)) | |
c8cdcb16 | 90 | #endif |
9889c728 | 91 | |
5eea1c5a RS |
92 | /* SYNTAX_ENTRY fetches the information from the entry for character C |
93 | in syntax table TABLE, or from globally kept data (gl_state). | |
94 | Does inheritance. */ | |
95 | /* CURRENT_SYNTAX_TABLE gives the syntax table valid for current | |
96 | position, it is either the buffer's syntax table, or syntax table | |
97 | found in text properties. */ | |
98 | ||
99 | #ifdef SYNTAX_ENTRY_VIA_PROPERTY | |
100 | # define SYNTAX_ENTRY(c) \ | |
101 | (gl_state.use_global ? gl_state.global_code : SYNTAX_ENTRY_INT (c)) | |
102 | # define CURRENT_SYNTAX_TABLE gl_state.current_syntax_table | |
103 | #else | |
104 | # define SYNTAX_ENTRY SYNTAX_ENTRY_INT | |
105 | # define CURRENT_SYNTAX_TABLE current_buffer->syntax_table | |
106 | #endif | |
e0b8ff93 | 107 | |
0b58a513 | 108 | #define SYNTAX_ENTRY_INT(c) \ |
9ca6ab7d | 109 | ((c) < CHAR_TABLE_SINGLE_BYTE_SLOTS \ |
0b58a513 | 110 | ? SYNTAX_ENTRY_FOLLOW_PARENT (CURRENT_SYNTAX_TABLE, \ |
0e35bfd8 | 111 | (unsigned char) (c)) \ |
0b58a513 KH |
112 | : Faref (CURRENT_SYNTAX_TABLE, \ |
113 | make_number (COMPOSITE_CHAR_P (c) \ | |
114 | ? cmpchar_component ((c), 0, 1) \ | |
115 | : (c)))) | |
e0b8ff93 | 116 | |
e46c910e | 117 | /* Extract the information from the entry for character C |
e0b8ff93 | 118 | in the current syntax table. */ |
c8cdcb16 RS |
119 | |
120 | #ifdef __GNUC__ | |
e46c910e RS |
121 | #define SYNTAX(c) \ |
122 | ({ Lisp_Object temp; \ | |
123 | temp = SYNTAX_ENTRY (c); \ | |
124 | (CONSP (temp) \ | |
125 | ? (enum syntaxcode) (XINT (XCONS (temp)->car) & 0xff) \ | |
e0b8ff93 | 126 | : Swhitespace); }) |
e46c910e RS |
127 | |
128 | #define SYNTAX_WITH_FLAGS(c) \ | |
129 | ({ Lisp_Object temp; \ | |
130 | temp = SYNTAX_ENTRY (c); \ | |
131 | (CONSP (temp) \ | |
132 | ? XINT (XCONS (temp)->car) \ | |
e0b8ff93 | 133 | : (int) Swhitespace); }) |
e46c910e RS |
134 | |
135 | #define SYNTAX_MATCH(c) \ | |
136 | ({ Lisp_Object temp; \ | |
137 | temp = SYNTAX_ENTRY (c); \ | |
138 | (CONSP (temp) \ | |
2e34157c | 139 | ? XCONS (temp)->cdr \ |
e0b8ff93 | 140 | : Qnil); }) |
c8cdcb16 | 141 | #else |
e46c910e | 142 | #define SYNTAX(c) \ |
9d40ebd2 | 143 | (syntax_temp = SYNTAX_ENTRY ((c)), \ |
e46c910e RS |
144 | (CONSP (syntax_temp) \ |
145 | ? (enum syntaxcode) (XINT (XCONS (syntax_temp)->car) & 0xff) \ | |
e0b8ff93 | 146 | : Swhitespace)) |
e46c910e RS |
147 | |
148 | #define SYNTAX_WITH_FLAGS(c) \ | |
9d40ebd2 | 149 | (syntax_temp = SYNTAX_ENTRY ((c)), \ |
e46c910e RS |
150 | (CONSP (syntax_temp) \ |
151 | ? XINT (XCONS (syntax_temp)->car) \ | |
e0b8ff93 | 152 | : (int) Swhitespace)) |
e46c910e RS |
153 | |
154 | #define SYNTAX_MATCH(c) \ | |
9d40ebd2 | 155 | (syntax_temp = SYNTAX_ENTRY ((c)), \ |
e46c910e | 156 | (CONSP (syntax_temp) \ |
2e34157c | 157 | ? XCONS (syntax_temp)->cdr \ |
e0b8ff93 | 158 | : Qnil)) |
c8cdcb16 | 159 | #endif |
9889c728 | 160 | |
a306d6f1 | 161 | /* Then there are six single-bit flags that have the following meanings: |
9889c728 JB |
162 | 1. This character is the first of a two-character comment-start sequence. |
163 | 2. This character is the second of a two-character comment-start sequence. | |
164 | 3. This character is the first of a two-character comment-end sequence. | |
165 | 4. This character is the second of a two-character comment-end sequence. | |
166 | 5. This character is a prefix, for backward-prefix-chars. | |
a306d6f1 RS |
167 | Note that any two-character sequence whose first character has flag 1 |
168 | and whose second character has flag 2 will be interpreted as a comment start. | |
169 | ||
170 | bit 6 is used to discriminate between two different comment styles. | |
171 | Languages such as C++ allow two orthogonal syntax start/end pairs | |
172 | and bit 6 is used to determine whether a comment-end or Scommentend | |
5eea1c5a | 173 | ends style a or b. Comment start sequences can start style a or b. |
a306d6f1 RS |
174 | Style a is always the default. |
175 | */ | |
9889c728 | 176 | |
98bcfee8 RS |
177 | /* These macros extract a particular flag for a given character. */ |
178 | ||
e46c910e | 179 | #define SYNTAX_COMSTART_FIRST(c) ((SYNTAX_WITH_FLAGS (c) >> 16) & 1) |
c8cdcb16 | 180 | |
e46c910e | 181 | #define SYNTAX_COMSTART_SECOND(c) ((SYNTAX_WITH_FLAGS (c) >> 17) & 1) |
c8cdcb16 | 182 | |
e46c910e | 183 | #define SYNTAX_COMEND_FIRST(c) ((SYNTAX_WITH_FLAGS (c) >> 18) & 1) |
c8cdcb16 | 184 | |
e46c910e | 185 | #define SYNTAX_COMEND_SECOND(c) ((SYNTAX_WITH_FLAGS (c) >> 19) & 1) |
c8cdcb16 | 186 | |
e46c910e | 187 | #define SYNTAX_PREFIX(c) ((SYNTAX_WITH_FLAGS (c) >> 20) & 1) |
9889c728 | 188 | |
e46c910e | 189 | #define SYNTAX_COMMENT_STYLE(c) ((SYNTAX_WITH_FLAGS (c) >> 21) & 1) |
a306d6f1 | 190 | |
98bcfee8 RS |
191 | /* These macros extract specific flags from an integer |
192 | that holds the syntax code and the flags. */ | |
193 | ||
194 | #define SYNTAX_FLAGS_COMSTART_FIRST(flags) (((flags) >> 16) & 1) | |
195 | ||
196 | #define SYNTAX_FLAGS_COMSTART_SECOND(flags) (((flags) >> 17) & 1) | |
197 | ||
198 | #define SYNTAX_FLAGS_COMEND_FIRST(flags) (((flags) >> 18) & 1) | |
199 | ||
200 | #define SYNTAX_FLAGS_COMEND_SECOND(flags) (((flags) >> 19) & 1) | |
201 | ||
202 | #define SYNTAX_FLAGS_PREFIX(flags) (((flags) >> 20) & 1) | |
203 | ||
204 | #define SYNTAX_FLAGS_COMMENT_STYLE(flags) (((flags) >> 21) & 1) | |
205 | ||
9889c728 JB |
206 | /* This array, indexed by a character, contains the syntax code which that |
207 | character signifies (as a char). For example, | |
5eea1c5a | 208 | (enum syntaxcode) syntax_spec_code['w'] is Sword. */ |
9889c728 JB |
209 | |
210 | extern unsigned char syntax_spec_code[0400]; | |
211 | ||
5eea1c5a RS |
212 | /* Indexed by syntax code, give the letter that describes it. */ |
213 | ||
214 | extern char syntax_code_spec[16]; | |
215 | ||
c292db29 | 216 | /* Convert the byte offset BYTEPOS into a character position, |
2f16e7fd RS |
217 | for the object recorded in gl_state with SETUP_SYNTAX_TABLE_FOR_OBJECT. |
218 | ||
219 | The value is meant for use in the UPDATE_SYNTAX_TABLE... macros. | |
220 | These macros do nothing when parse_sexp_lookup_properties is 0, | |
221 | so we return 0 in that case, for speed. */ | |
c292db29 RS |
222 | |
223 | #define SYNTAX_TABLE_BYTE_TO_CHAR(bytepos) \ | |
2f16e7fd RS |
224 | (! parse_sexp_lookup_properties \ |
225 | ? 0 \ | |
226 | : STRINGP (gl_state.object) \ | |
c292db29 RS |
227 | ? string_byte_to_char (gl_state.object, (bytepos)) \ |
228 | : BUFFERP (gl_state.object) \ | |
f79b4b7e KH |
229 | ? buf_bytepos_to_charpos (XBUFFER (gl_state.object), \ |
230 | (bytepos) + BUF_BEGV_BYTE (XBUFFER (gl_state.object)) - 1) - BUF_BEGV (XBUFFER (gl_state.object)) + 1 \ | |
c292db29 | 231 | : NILP (gl_state.object) \ |
f79b4b7e | 232 | ? BYTE_TO_CHAR ((bytepos) + BEGV_BYTE - 1) - BEGV + 1 \ |
c292db29 RS |
233 | : (bytepos)) |
234 | ||
f79b4b7e KH |
235 | /* Make syntax table state (gl_state) good for CHARPOS, assuming it is |
236 | currently good for a position before CHARPOS. */ | |
5eea1c5a | 237 | |
f79b4b7e | 238 | #define UPDATE_SYNTAX_TABLE_FORWARD(charpos) \ |
2f16e7fd | 239 | (parse_sexp_lookup_properties \ |
f79b4b7e KH |
240 | && (charpos) >= gl_state.e_property \ |
241 | ? (update_syntax_table ((charpos) + gl_state.offset, 1, 0, \ | |
c292db29 RS |
242 | gl_state.object), \ |
243 | 1) \ | |
244 | : 0) | |
5eea1c5a | 245 | |
f79b4b7e KH |
246 | /* Make syntax table state (gl_state) good for CHARPOS, assuming it is |
247 | currently good for a position after CHARPOS. */ | |
5eea1c5a | 248 | |
f79b4b7e | 249 | #define UPDATE_SYNTAX_TABLE_BACKWARD(charpos) \ |
2f16e7fd | 250 | (parse_sexp_lookup_properties \ |
f79b4b7e KH |
251 | && (charpos) <= gl_state.b_property \ |
252 | ? (update_syntax_table ((charpos) + gl_state.offset, -1, 0, \ | |
c292db29 RS |
253 | gl_state.object), \ |
254 | 1) \ | |
255 | : 0) | |
e2d8d746 | 256 | |
f79b4b7e | 257 | /* Make syntax table good for CHARPOS. */ |
e2d8d746 | 258 | |
f79b4b7e | 259 | #define UPDATE_SYNTAX_TABLE(charpos) \ |
2f16e7fd | 260 | (parse_sexp_lookup_properties \ |
f79b4b7e KH |
261 | && (charpos) <= gl_state.b_property \ |
262 | ? (update_syntax_table ((charpos) + gl_state.offset, -1, 0, \ | |
c292db29 RS |
263 | gl_state.object), \ |
264 | 1) \ | |
2f16e7fd | 265 | : (parse_sexp_lookup_properties \ |
f79b4b7e KH |
266 | && (charpos) >= gl_state.e_property \ |
267 | ? (update_syntax_table ((charpos) + gl_state.offset, 1, 0,\ | |
c292db29 RS |
268 | gl_state.object), \ |
269 | 1) \ | |
270 | : 0)) | |
5eea1c5a RS |
271 | |
272 | /* This macro should be called with FROM at the start of forward | |
273 | search, or after the last position of the backward search. It | |
274 | makes sure that the first char is picked up with correct table, so | |
275 | one does not need to call UPDATE_SYNTAX_TABLE immediately after the | |
276 | call. | |
277 | Sign of COUNT gives the direction of the search. | |
278 | */ | |
279 | ||
c292db29 | 280 | #define SETUP_SYNTAX_TABLE(FROM, COUNT) \ |
2f16e7fd RS |
281 | if (1) \ |
282 | { \ | |
283 | gl_state.b_property = BEGV - 1; \ | |
284 | gl_state.e_property = ZV + 1; \ | |
285 | gl_state.object = Qnil; \ | |
286 | gl_state.use_global = 0; \ | |
287 | gl_state.offset = 0; \ | |
288 | gl_state.current_syntax_table = current_buffer->syntax_table; \ | |
289 | if (parse_sexp_lookup_properties) \ | |
9b9794f0 RS |
290 | if ((COUNT) > 0 || (FROM) > BEGV) \ |
291 | update_syntax_table ((COUNT) > 0 ? (FROM) : (FROM) - 1, (COUNT),\ | |
292 | 1, Qnil); \ | |
2f16e7fd RS |
293 | } \ |
294 | else | |
5eea1c5a RS |
295 | |
296 | /* Same as above, but in OBJECT. If OBJECT is nil, use current buffer. | |
e2d8d746 RS |
297 | If it is t, ignore properties altogether. |
298 | ||
299 | This is meant for regex.c to use. For buffers, regex.c passes arguments | |
300 | to the UPDATE_SYNTAX_TABLE macros which are relative to BEGV. | |
4bbd5bc3 | 301 | So if it is a buffer, we set the offset field to BEGV. */ |
5eea1c5a | 302 | |
c292db29 | 303 | #define SETUP_SYNTAX_TABLE_FOR_OBJECT(OBJECT, FROM, COUNT) \ |
4bbd5bc3 RS |
304 | if (1) \ |
305 | { \ | |
c292db29 RS |
306 | gl_state.object = (OBJECT); \ |
307 | if (BUFFERP (gl_state.object)) \ | |
308 | { \ | |
309 | struct buffer *buf = XBUFFER (gl_state.object); \ | |
1d1293dd KH |
310 | gl_state.b_property = 0; \ |
311 | gl_state.e_property = BUF_ZV (buf) - BUF_BEGV (buf) + 1; \ | |
c292db29 RS |
312 | gl_state.offset = BUF_BEGV (buf) - 1; \ |
313 | } \ | |
314 | else if (NILP (gl_state.object)) \ | |
4bbd5bc3 | 315 | { \ |
1d1293dd KH |
316 | gl_state.b_property = 0; \ |
317 | gl_state.e_property = ZV - BEGV + 1; \ | |
4bbd5bc3 RS |
318 | gl_state.offset = BEGV - 1; \ |
319 | } \ | |
c292db29 | 320 | else if (EQ (gl_state.object, Qt)) \ |
4bbd5bc3 RS |
321 | { \ |
322 | gl_state.b_property = - 1; \ | |
323 | gl_state.e_property = 1500000000; \ | |
324 | gl_state.offset = 0; \ | |
325 | } \ | |
326 | else \ | |
327 | { \ | |
328 | gl_state.b_property = -1; \ | |
c292db29 | 329 | gl_state.e_property = 1 + XSTRING (gl_state.object)->size; \ |
4bbd5bc3 RS |
330 | gl_state.offset = 0; \ |
331 | } \ | |
332 | gl_state.use_global = 0; \ | |
333 | gl_state.current_syntax_table = current_buffer->syntax_table; \ | |
334 | if (parse_sexp_lookup_properties) \ | |
f79b4b7e | 335 | update_syntax_table (((FROM) + gl_state.offset \ |
c292db29 RS |
336 | + (COUNT > 0 ? 0 : -1)), \ |
337 | COUNT, 1, gl_state.object); \ | |
4bbd5bc3 RS |
338 | } \ |
339 | else | |
5eea1c5a RS |
340 | |
341 | struct gl_state_s | |
342 | { | |
c292db29 | 343 | Lisp_Object object; /* The object we are scanning. */ |
5eea1c5a RS |
344 | int start; /* Where to stop. */ |
345 | int stop; /* Where to stop. */ | |
346 | int use_global; /* Whether to use global_code | |
347 | or c_s_t. */ | |
348 | Lisp_Object global_code; /* Syntax code of current char. */ | |
349 | Lisp_Object current_syntax_table; /* Syntax table for current pos. */ | |
350 | Lisp_Object old_prop; /* Syntax-table prop at prev pos. */ | |
351 | int b_property; /* Last index where c_s_t is | |
352 | not valid. */ | |
353 | int e_property; /* First index where c_s_t is | |
354 | not valid. */ | |
355 | INTERVAL forward_i; /* Where to start lookup on forward */ | |
356 | INTERVAL backward_i; /* or backward movement. The | |
357 | data in c_s_t is valid | |
358 | between these intervals, | |
359 | and possibly at the | |
360 | intervals too, depending | |
361 | on: */ | |
e2d8d746 RS |
362 | /* Offset for positions specified to UPDATE_SYNTAX_TABLE. */ |
363 | int offset; | |
5eea1c5a RS |
364 | char left_ok; |
365 | char right_ok; | |
366 | }; | |
367 | ||
368 | extern struct gl_state_s gl_state; | |
369 | extern int parse_sexp_lookup_properties; | |
4c571d09 AS |
370 | extern INTERVAL interval_of P_ ((int, Lisp_Object)); |
371 | ||
372 | extern int scan_words P_ ((int, int)); |