(struct gl_state_s): New field `offset'.
[bpt/emacs.git] / src / syntax.h
1 /* Declarations having to do with GNU Emacs syntax tables.
2 Copyright (C) 1985, 1993, 1994, 1997 Free Software Foundation, Inc.
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
8 the Free Software Foundation; either version 2, or (at your option)
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
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 extern Lisp_Object Qsyntax_table_p;
23 extern Lisp_Object Fsyntax_table_p (), Fsyntax_table (), Fset_syntax_table ();
24 extern void update_syntax_table ();
25
26 /* The standard syntax table is stored where it will automatically
27 be used in all new buffers. */
28 #define Vstandard_syntax_table buffer_defaults.syntax_table
29
30 /* A syntax table is a chartable whose elements are cons cells
31 (CODE+FLAGS . MATCHING-CHAR). MATCHING-CHAR can be nil if the char
32 is not a kind of parenthesis.
33
34 The low 8 bits of CODE+FLAGS is a code, as follows: */
35
36 enum syntaxcode
37 {
38 Swhitespace, /* for a whitespace character */
39 Spunct, /* for random punctuation characters */
40 Sword, /* for a word constituent */
41 Ssymbol, /* symbol constituent but not word constituent */
42 Sopen, /* for a beginning delimiter */
43 Sclose, /* for an ending delimiter */
44 Squote, /* for a prefix character like Lisp ' */
45 Sstring, /* for a string-grouping character like Lisp " */
46 Smath, /* for delimiters like $ in Tex. */
47 Sescape, /* for a character that begins a C-style escape */
48 Scharquote, /* for a character that quotes the following character */
49 Scomment, /* for a comment-starting character */
50 Sendcomment, /* for a comment-ending character */
51 Sinherit, /* use the standard syntax table for this character */
52 Scomment_fence, /* Starts/ends comment which is delimited on the
53 other side by a char with the same syntaxcode. */
54 Sstring_fence, /* Starts/ends string which is delimited on the
55 other side by a char with the same syntaxcode. */
56 Smax /* Upper bound on codes that are meaningful */
57 };
58
59 /* Set the syntax entry VAL for char C in table TABLE. */
60
61 #define SET_RAW_SYNTAX_ENTRY(table, c, val) \
62 ((c) < CHAR_TABLE_SINGLE_BYTE_SLOTS \
63 ? (XCHAR_TABLE (table)->contents[(unsigned char) (c)] = (val)) \
64 : Faset ((table), make_number (c), (val)))
65
66 /* Fetch the syntax entry for char C in syntax table TABLE.
67 This macro is called only when C is less than CHAR_TABLE_ORDINARY_SLOTS.
68 Do inheritance. */
69
70 #ifdef __GNUC__
71 #define SYNTAX_ENTRY_FOLLOW_PARENT(table, c) \
72 ({ Lisp_Object tbl = table; \
73 Lisp_Object temp = XCHAR_TABLE (tbl)->contents[(c)]; \
74 while (NILP (temp)) \
75 { \
76 tbl = XCHAR_TABLE (tbl)->parent; \
77 if (NILP (tbl)) \
78 break; \
79 temp = XCHAR_TABLE (tbl)->contents[(c)]; \
80 } \
81 temp; })
82 #else
83 extern Lisp_Object syntax_temp;
84 extern Lisp_Object syntax_parent_lookup ();
85
86 #define SYNTAX_ENTRY_FOLLOW_PARENT(table, c) \
87 (syntax_temp = XCHAR_TABLE (table)->contents[(c)], \
88 (NILP (syntax_temp) \
89 ? syntax_parent_lookup (table, (c)) \
90 : syntax_temp))
91 #endif
92
93 /* SYNTAX_ENTRY fetches the information from the entry for character C
94 in syntax table TABLE, or from globally kept data (gl_state).
95 Does inheritance. */
96 /* CURRENT_SYNTAX_TABLE gives the syntax table valid for current
97 position, it is either the buffer's syntax table, or syntax table
98 found in text properties. */
99
100 #ifdef SYNTAX_ENTRY_VIA_PROPERTY
101 # define SYNTAX_ENTRY(c) \
102 (gl_state.use_global ? gl_state.global_code : SYNTAX_ENTRY_INT (c))
103 # define CURRENT_SYNTAX_TABLE gl_state.current_syntax_table
104 #else
105 # define SYNTAX_ENTRY SYNTAX_ENTRY_INT
106 # define CURRENT_SYNTAX_TABLE current_buffer->syntax_table
107 #endif
108
109 #define SYNTAX_ENTRY_INT(c) \
110 ((c) < CHAR_TABLE_SINGLE_BYTE_SLOTS \
111 ? SYNTAX_ENTRY_FOLLOW_PARENT (CURRENT_SYNTAX_TABLE, \
112 (unsigned char) (c)) \
113 : Faref (CURRENT_SYNTAX_TABLE, make_number ((c))))
114
115 /* Extract the information from the entry for character C
116 in the current syntax table. */
117
118 #ifdef __GNUC__
119 #define SYNTAX(c) \
120 ({ Lisp_Object temp; \
121 temp = SYNTAX_ENTRY (c); \
122 (CONSP (temp) \
123 ? (enum syntaxcode) (XINT (XCONS (temp)->car) & 0xff) \
124 : Swhitespace); })
125
126 #define SYNTAX_WITH_FLAGS(c) \
127 ({ Lisp_Object temp; \
128 temp = SYNTAX_ENTRY (c); \
129 (CONSP (temp) \
130 ? XINT (XCONS (temp)->car) \
131 : (int) Swhitespace); })
132
133 #define SYNTAX_MATCH(c) \
134 ({ Lisp_Object temp; \
135 temp = SYNTAX_ENTRY (c); \
136 (CONSP (temp) \
137 ? XCONS (temp)->cdr \
138 : Qnil); })
139 #else
140 #define SYNTAX(c) \
141 (syntax_temp = SYNTAX_ENTRY ((c)), \
142 (CONSP (syntax_temp) \
143 ? (enum syntaxcode) (XINT (XCONS (syntax_temp)->car) & 0xff) \
144 : Swhitespace))
145
146 #define SYNTAX_WITH_FLAGS(c) \
147 (syntax_temp = SYNTAX_ENTRY ((c)), \
148 (CONSP (syntax_temp) \
149 ? XINT (XCONS (syntax_temp)->car) \
150 : (int) Swhitespace))
151
152 #define SYNTAX_MATCH(c) \
153 (syntax_temp = SYNTAX_ENTRY ((c)), \
154 (CONSP (syntax_temp) \
155 ? XCONS (syntax_temp)->cdr \
156 : Qnil))
157 #endif
158
159 /* Then there are six single-bit flags that have the following meanings:
160 1. This character is the first of a two-character comment-start sequence.
161 2. This character is the second of a two-character comment-start sequence.
162 3. This character is the first of a two-character comment-end sequence.
163 4. This character is the second of a two-character comment-end sequence.
164 5. This character is a prefix, for backward-prefix-chars.
165 Note that any two-character sequence whose first character has flag 1
166 and whose second character has flag 2 will be interpreted as a comment start.
167
168 bit 6 is used to discriminate between two different comment styles.
169 Languages such as C++ allow two orthogonal syntax start/end pairs
170 and bit 6 is used to determine whether a comment-end or Scommentend
171 ends style a or b. Comment start sequences can start style a or b.
172 Style a is always the default.
173 */
174
175 #define SYNTAX_COMSTART_FIRST(c) ((SYNTAX_WITH_FLAGS (c) >> 16) & 1)
176
177 #define SYNTAX_COMSTART_SECOND(c) ((SYNTAX_WITH_FLAGS (c) >> 17) & 1)
178
179 #define SYNTAX_COMEND_FIRST(c) ((SYNTAX_WITH_FLAGS (c) >> 18) & 1)
180
181 #define SYNTAX_COMEND_SECOND(c) ((SYNTAX_WITH_FLAGS (c) >> 19) & 1)
182
183 #define SYNTAX_PREFIX(c) ((SYNTAX_WITH_FLAGS (c) >> 20) & 1)
184
185 /* extract the comment style bit from the syntax table entry */
186 #define SYNTAX_COMMENT_STYLE(c) ((SYNTAX_WITH_FLAGS (c) >> 21) & 1)
187
188 /* This array, indexed by a character, contains the syntax code which that
189 character signifies (as a char). For example,
190 (enum syntaxcode) syntax_spec_code['w'] is Sword. */
191
192 extern unsigned char syntax_spec_code[0400];
193
194 /* Indexed by syntax code, give the letter that describes it. */
195
196 extern char syntax_code_spec[16];
197
198 /* Make syntax table state (gl_state) good for POS, assuming it is
199 currently good for a position before POS. */
200
201 #define UPDATE_SYNTAX_TABLE_FORWARD(pos) \
202 ((pos) >= gl_state.e_property - gl_state.offset \
203 ? (update_syntax_table ((pos) + gl_state.offset, 1, 0), 1) : 0)
204
205 /* Make syntax table state (gl_state) good for POS, assuming it is
206 currently good for a position after POS. */
207
208 #define UPDATE_SYNTAX_TABLE_BACKWARD(pos) \
209 ((pos) <= gl_state.b_property - gl_state.offset \
210 ? (update_syntax_table ((pos) + gl_state.offset, -1, 0), 1) : 0)
211
212 /* Make syntax table good for POS. */
213
214 #define UPDATE_SYNTAX_TABLE(pos) \
215 ((pos) <= gl_state.b_property - gl_state.offset \
216 ? (update_syntax_table ((pos) + gl_state.offset, -1, 0), 1) \
217 : ((pos) >= gl_state.e_property - gl_state.offset \
218 ? (update_syntax_table ((pos) + gl_state.offset, 1, 0), 1) : 0))
219
220 /* This macro should be called with FROM at the start of forward
221 search, or after the last position of the backward search. It
222 makes sure that the first char is picked up with correct table, so
223 one does not need to call UPDATE_SYNTAX_TABLE immediately after the
224 call.
225 Sign of COUNT gives the direction of the search.
226 */
227
228 #define SETUP_SYNTAX_TABLE(from,count) \
229 gl_state.b_property = BEGV - 1; \
230 gl_state.e_property = ZV + 1; \
231 gl_state.use_global = 0; \
232 gl_state.offset = 0; \
233 gl_state.current_syntax_table = current_buffer->syntax_table; \
234 if (parse_sexp_lookup_properties) \
235 update_syntax_table ((count) > 0 ? (from) : (from) - 1, (count), \
236 1, Qnil);
237
238 /* Same as above, but in OBJECT. If OBJECT is nil, use current buffer.
239 If it is t, ignore properties altogether.
240
241 This is meant for regex.c to use. For buffers, regex.c passes arguments
242 to the UPDATE_SYNTAX_TABLE macros which are relative to BEGV.
243 So if it is a buffer,a we set the offset field to BEGV. */
244
245 #define SETUP_SYNTAX_TABLE_FOR_OBJECT(object, from, count) \
246 if (BUFFERP (object) || NILP (object)) \
247 { \
248 gl_state.b_property = BEGV - 1; \
249 gl_state.e_property = ZV; \
250 gl_state.offset = BEGV; \
251 } \
252 else if (EQ (object, Qt)) \
253 { \
254 gl_state.b_property = - 1; \
255 gl_state.e_property = 1500000000; \
256 gl_state.offset = 0; \
257 } \
258 else \
259 { \
260 gl_state.b_property = -1; \
261 gl_state.e_property = 1 + XSTRING (object)->size; \
262 gl_state.offset = 0; \
263 } \
264 gl_state.use_global = 0; \
265 gl_state.current_syntax_table = current_buffer->syntax_table; \
266 if (parse_sexp_lookup_properties) \
267 update_syntax_table (count > 0 ? (from) : (from) - 1, count, 1, object);
268
269 struct gl_state_s
270 {
271 int start; /* Where to stop. */
272 int stop; /* Where to stop. */
273 int use_global; /* Whether to use global_code
274 or c_s_t. */
275 Lisp_Object global_code; /* Syntax code of current char. */
276 Lisp_Object current_syntax_table; /* Syntax table for current pos. */
277 Lisp_Object old_prop; /* Syntax-table prop at prev pos. */
278 int b_property; /* Last index where c_s_t is
279 not valid. */
280 int e_property; /* First index where c_s_t is
281 not valid. */
282 INTERVAL forward_i; /* Where to start lookup on forward */
283 INTERVAL backward_i; /* or backward movement. The
284 data in c_s_t is valid
285 between these intervals,
286 and possibly at the
287 intervals too, depending
288 on: */
289 /* Offset for positions specified to UPDATE_SYNTAX_TABLE. */
290 int offset;
291 char left_ok;
292 char right_ok;
293 };
294
295 extern struct gl_state_s gl_state;
296 extern int parse_sexp_lookup_properties;
297 extern INTERVAL interval_of ();