]> code.delx.au - gnu-emacs/blob - src/syntax.h
Convert consecutive FSF copyright years to ranges.
[gnu-emacs] / src / syntax.h
1 /* Declarations having to do with GNU Emacs syntax tables.
2 Copyright (C) 1985, 1993-1994, 1997-1998, 2001-2011 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 3 of the License, or
9 (at your option) 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. If not, see <http://www.gnu.org/licenses/>. */
18
19
20 extern Lisp_Object Qsyntax_table_p;
21 extern void update_syntax_table (EMACS_INT, int, int, Lisp_Object);
22
23 /* The standard syntax table is stored where it will automatically
24 be used in all new buffers. */
25 #define Vstandard_syntax_table buffer_defaults.syntax_table
26
27 /* A syntax table is a chartable whose elements are cons cells
28 (CODE+FLAGS . MATCHING-CHAR). MATCHING-CHAR can be nil if the char
29 is not a kind of parenthesis.
30
31 The low 8 bits of CODE+FLAGS is a code, as follows: */
32
33 enum syntaxcode
34 {
35 Swhitespace, /* for a whitespace character */
36 Spunct, /* for random punctuation characters */
37 Sword, /* for a word constituent */
38 Ssymbol, /* symbol constituent but not word constituent */
39 Sopen, /* for a beginning delimiter */
40 Sclose, /* for an ending delimiter */
41 Squote, /* for a prefix character like Lisp ' */
42 Sstring, /* for a string-grouping character like Lisp " */
43 Smath, /* for delimiters like $ in Tex. */
44 Sescape, /* for a character that begins a C-style escape */
45 Scharquote, /* for a character that quotes the following character */
46 Scomment, /* for a comment-starting character */
47 Sendcomment, /* for a comment-ending character */
48 Sinherit, /* use the standard syntax table for this character */
49 Scomment_fence, /* Starts/ends comment which is delimited on the
50 other side by any char with the same syntaxcode. */
51 Sstring_fence, /* Starts/ends string which is delimited on the
52 other side by any char with the same syntaxcode. */
53 Smax /* Upper bound on codes that are meaningful */
54 };
55
56 /* Set the syntax entry VAL for char C in table TABLE. */
57
58 #define SET_RAW_SYNTAX_ENTRY(table, c, val) \
59 CHAR_TABLE_SET ((table), c, (val))
60
61 /* Set the syntax entry VAL for char-range RANGE in table TABLE.
62 RANGE is a cons (FROM . TO) specifying the range of characters. */
63
64 #define SET_RAW_SYNTAX_ENTRY_RANGE(table, range, val) \
65 Fset_char_table_range ((table), (range), (val))
66
67 /* SYNTAX_ENTRY fetches the information from the entry for character C
68 in syntax table TABLE, or from globally kept data (gl_state).
69 Does inheritance. */
70 /* CURRENT_SYNTAX_TABLE gives the syntax table valid for current
71 position, it is either the buffer's syntax table, or syntax table
72 found in text properties. */
73
74 #ifdef SYNTAX_ENTRY_VIA_PROPERTY
75 # define SYNTAX_ENTRY(c) \
76 (gl_state.use_global ? gl_state.global_code : SYNTAX_ENTRY_INT (c))
77 # define CURRENT_SYNTAX_TABLE gl_state.current_syntax_table
78 #else
79 # define SYNTAX_ENTRY SYNTAX_ENTRY_INT
80 # define CURRENT_SYNTAX_TABLE current_buffer->syntax_table
81 #endif
82
83 #define SYNTAX_ENTRY_INT(c) CHAR_TABLE_REF (CURRENT_SYNTAX_TABLE, (c))
84
85 /* Extract the information from the entry for character C
86 in the current syntax table. */
87
88 #ifdef __GNUC__
89 #define SYNTAX(c) \
90 ({ Lisp_Object _syntax_temp; \
91 _syntax_temp = SYNTAX_ENTRY (c); \
92 (CONSP (_syntax_temp) \
93 ? (enum syntaxcode) (XINT (XCAR (_syntax_temp)) & 0xff) \
94 : Swhitespace); })
95
96 #define SYNTAX_WITH_FLAGS(c) \
97 ({ Lisp_Object _syntax_temp; \
98 _syntax_temp = SYNTAX_ENTRY (c); \
99 (CONSP (_syntax_temp) \
100 ? XINT (XCAR (_syntax_temp)) \
101 : (int) Swhitespace); })
102
103 #define SYNTAX_MATCH(c) \
104 ({ Lisp_Object _syntax_temp; \
105 _syntax_temp = SYNTAX_ENTRY (c); \
106 (CONSP (_syntax_temp) \
107 ? XCDR (_syntax_temp) \
108 : Qnil); })
109 #else
110 extern Lisp_Object syntax_temp;
111 #define SYNTAX(c) \
112 (syntax_temp = SYNTAX_ENTRY ((c)), \
113 (CONSP (syntax_temp) \
114 ? (enum syntaxcode) (XINT (XCAR (syntax_temp)) & 0xff) \
115 : Swhitespace))
116
117 #define SYNTAX_WITH_FLAGS(c) \
118 (syntax_temp = SYNTAX_ENTRY ((c)), \
119 (CONSP (syntax_temp) \
120 ? XINT (XCAR (syntax_temp)) \
121 : (int) Swhitespace))
122
123 #define SYNTAX_MATCH(c) \
124 (syntax_temp = SYNTAX_ENTRY ((c)), \
125 (CONSP (syntax_temp) \
126 ? XCDR (syntax_temp) \
127 : Qnil))
128 #endif
129
130
131 /* Whether the syntax of the character C has the prefix flag set. */
132 extern int syntax_prefix_flag_p (int c);
133
134 /* This array, indexed by a character, contains the syntax code which that
135 character signifies (as a char). For example,
136 (enum syntaxcode) syntax_spec_code['w'] is Sword. */
137
138 extern unsigned char syntax_spec_code[0400];
139
140 /* Indexed by syntax code, give the letter that describes it. */
141
142 extern char syntax_code_spec[16];
143
144 /* Convert the byte offset BYTEPOS into a character position,
145 for the object recorded in gl_state with SETUP_SYNTAX_TABLE_FOR_OBJECT.
146
147 The value is meant for use in the UPDATE_SYNTAX_TABLE... macros.
148 These macros do nothing when parse_sexp_lookup_properties is 0,
149 so we return 0 in that case, for speed. */
150
151 #define SYNTAX_TABLE_BYTE_TO_CHAR(bytepos) \
152 (! parse_sexp_lookup_properties \
153 ? 0 \
154 : STRINGP (gl_state.object) \
155 ? string_byte_to_char (gl_state.object, (bytepos)) \
156 : BUFFERP (gl_state.object) \
157 ? buf_bytepos_to_charpos (XBUFFER (gl_state.object), \
158 (bytepos) + BUF_BEGV_BYTE (XBUFFER (gl_state.object)) - 1) - BUF_BEGV (XBUFFER (gl_state.object)) + 1 \
159 : NILP (gl_state.object) \
160 ? BYTE_TO_CHAR ((bytepos) + BEGV_BYTE - 1) - BEGV + 1 \
161 : (bytepos))
162
163 /* Make syntax table state (gl_state) good for CHARPOS, assuming it is
164 currently good for a position before CHARPOS. */
165
166 #define UPDATE_SYNTAX_TABLE_FORWARD(charpos) \
167 (parse_sexp_lookup_properties \
168 && (charpos) >= gl_state.e_property \
169 ? (update_syntax_table ((charpos) + gl_state.offset, 1, 0, \
170 gl_state.object), \
171 1) \
172 : 0)
173
174 /* Make syntax table state (gl_state) good for CHARPOS, assuming it is
175 currently good for a position after CHARPOS. */
176
177 #define UPDATE_SYNTAX_TABLE_BACKWARD(charpos) \
178 (parse_sexp_lookup_properties \
179 && (charpos) < gl_state.b_property \
180 ? (update_syntax_table ((charpos) + gl_state.offset, -1, 0, \
181 gl_state.object), \
182 1) \
183 : 0)
184
185 /* Make syntax table good for CHARPOS. */
186
187 #define UPDATE_SYNTAX_TABLE(charpos) \
188 (parse_sexp_lookup_properties \
189 && (charpos) < gl_state.b_property \
190 ? (update_syntax_table ((charpos) + gl_state.offset, -1, 0, \
191 gl_state.object), \
192 1) \
193 : (parse_sexp_lookup_properties \
194 && (charpos) >= gl_state.e_property \
195 ? (update_syntax_table ((charpos) + gl_state.offset, 1, 0,\
196 gl_state.object), \
197 1) \
198 : 0))
199
200 /* This macro sets up the buffer-global syntax table. */
201 #define SETUP_BUFFER_SYNTAX_TABLE() \
202 do \
203 { \
204 gl_state.use_global = 0; \
205 gl_state.current_syntax_table = current_buffer->syntax_table; \
206 } while (0)
207
208 /* This macro should be called with FROM at the start of forward
209 search, or after the last position of the backward search. It
210 makes sure that the first char is picked up with correct table, so
211 one does not need to call UPDATE_SYNTAX_TABLE immediately after the
212 call.
213 Sign of COUNT gives the direction of the search.
214 */
215
216 #define SETUP_SYNTAX_TABLE(FROM, COUNT) \
217 do \
218 { \
219 SETUP_BUFFER_SYNTAX_TABLE (); \
220 gl_state.b_property = BEGV; \
221 gl_state.e_property = ZV + 1; \
222 gl_state.object = Qnil; \
223 gl_state.offset = 0; \
224 if (parse_sexp_lookup_properties) \
225 if ((COUNT) > 0 || (FROM) > BEGV) \
226 update_syntax_table ((COUNT) > 0 ? (FROM) : (FROM) - 1, (COUNT),\
227 1, Qnil); \
228 } \
229 while (0)
230
231 /* Same as above, but in OBJECT. If OBJECT is nil, use current buffer.
232 If it is t, ignore properties altogether.
233
234 This is meant for regex.c to use. For buffers, regex.c passes arguments
235 to the UPDATE_SYNTAX_TABLE macros which are relative to BEGV.
236 So if it is a buffer, we set the offset field to BEGV. */
237
238 #define SETUP_SYNTAX_TABLE_FOR_OBJECT(OBJECT, FROM, COUNT) \
239 do \
240 { \
241 SETUP_BUFFER_SYNTAX_TABLE (); \
242 gl_state.object = (OBJECT); \
243 if (BUFFERP (gl_state.object)) \
244 { \
245 struct buffer *buf = XBUFFER (gl_state.object); \
246 gl_state.b_property = 1; \
247 gl_state.e_property = BUF_ZV (buf) - BUF_BEGV (buf) + 1; \
248 gl_state.offset = BUF_BEGV (buf) - 1; \
249 } \
250 else if (NILP (gl_state.object)) \
251 { \
252 gl_state.b_property = 1; \
253 gl_state.e_property = ZV - BEGV + 1; \
254 gl_state.offset = BEGV - 1; \
255 } \
256 else if (EQ (gl_state.object, Qt)) \
257 { \
258 gl_state.b_property = 0; \
259 gl_state.e_property = 1500000000; \
260 gl_state.offset = 0; \
261 } \
262 else \
263 { \
264 gl_state.b_property = 0; \
265 gl_state.e_property = 1 + SCHARS (gl_state.object); \
266 gl_state.offset = 0; \
267 } \
268 if (parse_sexp_lookup_properties) \
269 update_syntax_table (((FROM) + gl_state.offset \
270 + (COUNT > 0 ? 0 : -1)), \
271 COUNT, 1, gl_state.object); \
272 } \
273 while (0)
274
275 struct gl_state_s
276 {
277 Lisp_Object object; /* The object we are scanning. */
278 int start; /* Where to stop. */
279 int stop; /* Where to stop. */
280 int use_global; /* Whether to use global_code
281 or c_s_t. */
282 Lisp_Object global_code; /* Syntax code of current char. */
283 Lisp_Object current_syntax_table; /* Syntax table for current pos. */
284 Lisp_Object old_prop; /* Syntax-table prop at prev pos. */
285 int b_property; /* First index where c_s_t is valid. */
286 int e_property; /* First index where c_s_t is
287 not valid. */
288 INTERVAL forward_i; /* Where to start lookup on forward */
289 INTERVAL backward_i; /* or backward movement. The
290 data in c_s_t is valid
291 between these intervals,
292 and possibly at the
293 intervals too, depending
294 on: */
295 /* Offset for positions specified to UPDATE_SYNTAX_TABLE. */
296 int offset;
297 };
298
299 extern struct gl_state_s gl_state;
300 extern EMACS_INT scan_words (EMACS_INT, EMACS_INT);
301