Commit | Line | Data |
---|---|---|
8489eb67 | 1 | /* GNU Emacs routines to deal with syntax tables; also word and list parsing. |
429ab54e | 2 | Copyright (C) 1985, 1987, 1993, 1994, 1995, 1997, 1998, 1999, 2001, |
114f9c96 | 3 | 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
429ab54e | 4 | Free Software Foundation, Inc. |
8489eb67 RS |
5 | |
6 | This file is part of GNU Emacs. | |
7 | ||
9ec0b715 | 8 | GNU Emacs is free software: you can redistribute it and/or modify |
8489eb67 | 9 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
10 | the Free Software Foundation, either version 3 of the License, or |
11 | (at your option) any later version. | |
8489eb67 RS |
12 | |
13 | GNU Emacs is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 19 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
8489eb67 RS |
20 | |
21 | ||
18160b98 | 22 | #include <config.h> |
8489eb67 | 23 | #include <ctype.h> |
d7306fe6 | 24 | #include <setjmp.h> |
8489eb67 RS |
25 | #include "lisp.h" |
26 | #include "commands.h" | |
27 | #include "buffer.h" | |
5c7b02ab | 28 | #include "character.h" |
e35f6ff7 | 29 | #include "keymap.h" |
a1bc88d4 | 30 | #include "regex.h" |
195d1361 RS |
31 | |
32 | /* Make syntax table lookup grant data in gl_state. */ | |
33 | #define SYNTAX_ENTRY_VIA_PROPERTY | |
34 | ||
8489eb67 | 35 | #include "syntax.h" |
195d1361 | 36 | #include "intervals.h" |
c5683ceb SM |
37 | #include "category.h" |
38 | ||
39 | /* Then there are seven single-bit flags that have the following meanings: | |
40 | 1. This character is the first of a two-character comment-start sequence. | |
41 | 2. This character is the second of a two-character comment-start sequence. | |
42 | 3. This character is the first of a two-character comment-end sequence. | |
43 | 4. This character is the second of a two-character comment-end sequence. | |
44 | 5. This character is a prefix, for backward-prefix-chars. | |
45 | 6. The char is part of a delimiter for comments of style "b". | |
46 | 7. This character is part of a nestable comment sequence. | |
47 | 8. The char is part of a delimiter for comments of style "c". | |
48 | Note that any two-character sequence whose first character has flag 1 | |
49 | and whose second character has flag 2 will be interpreted as a comment start. | |
50 | ||
51 | bit 6 and 8 are used to discriminate between different comment styles. | |
52 | Languages such as C++ allow two orthogonal syntax start/end pairs | |
53 | and bit 6 is used to determine whether a comment-end or Scommentend | |
54 | ends style a or b. Comment markers can start style a, b, c, or bc. | |
55 | Style a is always the default. | |
56 | For 2-char comment markers, the style b flag is only looked up on the second | |
57 | char of the comment marker and on the first char of the comment ender. | |
58 | For style c (like to for the nested flag), the flag can be placed on any | |
59 | one of the chars. | |
60 | */ | |
61 | ||
62 | /* These macros extract specific flags from an integer | |
63 | that holds the syntax code and the flags. */ | |
64 | ||
65 | #define SYNTAX_FLAGS_COMSTART_FIRST(flags) (((flags) >> 16) & 1) | |
66 | ||
67 | #define SYNTAX_FLAGS_COMSTART_SECOND(flags) (((flags) >> 17) & 1) | |
68 | ||
69 | #define SYNTAX_FLAGS_COMEND_FIRST(flags) (((flags) >> 18) & 1) | |
70 | ||
71 | #define SYNTAX_FLAGS_COMEND_SECOND(flags) (((flags) >> 19) & 1) | |
72 | ||
73 | #define SYNTAX_FLAGS_PREFIX(flags) (((flags) >> 20) & 1) | |
74 | ||
75 | #define SYNTAX_FLAGS_COMMENT_STYLEB(flags) (((flags) >> 21) & 1) | |
76 | #define SYNTAX_FLAGS_COMMENT_STYLEC(flags) (((flags) >> 22) & 2) | |
77 | /* FLAGS should be the flags of the main char of the comment marker, e.g. | |
78 | the second for comstart and the first for comend. */ | |
79 | #define SYNTAX_FLAGS_COMMENT_STYLE(flags, other_flags) \ | |
80 | (SYNTAX_FLAGS_COMMENT_STYLEB (flags) \ | |
81 | | SYNTAX_FLAGS_COMMENT_STYLEC (flags) \ | |
82 | | SYNTAX_FLAGS_COMMENT_STYLEC (other_flags)) | |
83 | ||
84 | #define SYNTAX_FLAGS_COMMENT_NESTED(flags) (((flags) >> 22) & 1) | |
85 | ||
86 | /* These macros extract a particular flag for a given character. */ | |
87 | ||
88 | #define SYNTAX_COMEND_FIRST(c) \ | |
89 | (SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c))) | |
90 | #define SYNTAX_PREFIX(c) (SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c))) | |
195d1361 RS |
91 | |
92 | /* We use these constants in place for comment-style and | |
93 | string-ender-char to distinguish comments/strings started by | |
94 | comment_fence and string_fence codes. */ | |
95 | ||
96 | #define ST_COMMENT_STYLE (256 + 1) | |
97 | #define ST_STRING_STYLE (256 + 2) | |
8489eb67 | 98 | |
7bf5e9e4 | 99 | Lisp_Object Qsyntax_table_p, Qsyntax_table, Qscan_error; |
8489eb67 RS |
100 | |
101 | int words_include_escapes; | |
195d1361 | 102 | int parse_sexp_lookup_properties; |
8489eb67 | 103 | |
bd25db08 KH |
104 | /* Nonzero means `scan-sexps' treat all multibyte characters as symbol. */ |
105 | int multibyte_syntax_as_symbol; | |
106 | ||
8ea151b2 RS |
107 | /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h, |
108 | if not compiled with GCC. No need to mark it, since it is used | |
109 | only very temporarily. */ | |
110 | Lisp_Object syntax_temp; | |
111 | ||
f4ed767f GM |
112 | /* Non-zero means an open parenthesis in column 0 is always considered |
113 | to be the start of a defun. Zero means an open parenthesis in | |
114 | column 0 has no special meaning. */ | |
115 | ||
116 | int open_paren_in_column_0_is_defun_start; | |
117 | ||
e5d4f4dc RS |
118 | /* This is the internal form of the parse state used in parse-partial-sexp. */ |
119 | ||
120 | struct lisp_parse_state | |
121 | { | |
6449674e SM |
122 | int depth; /* Depth at end of parsing. */ |
123 | int instring; /* -1 if not within string, else desired terminator. */ | |
124 | int incomment; /* -1 if in unnestable comment else comment nesting */ | |
125 | int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */ | |
126 | int quoted; /* Nonzero if just after an escape char at end of parsing */ | |
127 | int mindepth; /* Minimum depth seen while scanning. */ | |
128 | /* Char number of most recent start-of-expression at current level */ | |
129 | EMACS_INT thislevelstart; | |
130 | /* Char number of start of containing expression */ | |
131 | EMACS_INT prevlevelstart; | |
132 | EMACS_INT location; /* Char number at which parsing stopped. */ | |
133 | EMACS_INT comstr_start; /* Position of last comment/string starter. */ | |
134 | Lisp_Object levelstarts; /* Char numbers of starts-of-expression | |
135 | of levels (starting from outermost). */ | |
e5d4f4dc RS |
136 | }; |
137 | \f | |
37bef230 RS |
138 | /* These variables are a cache for finding the start of a defun. |
139 | find_start_pos is the place for which the defun start was found. | |
140 | find_start_value is the defun start position found for it. | |
6a140a74 | 141 | find_start_value_byte is the corresponding byte position. |
37bef230 RS |
142 | find_start_buffer is the buffer it was found in. |
143 | find_start_begv is the BEGV value when it was found. | |
144 | find_start_modiff is the value of MODIFF when it was found. */ | |
145 | ||
8e2911c2 AS |
146 | static EMACS_INT find_start_pos; |
147 | static EMACS_INT find_start_value; | |
148 | static EMACS_INT find_start_value_byte; | |
37bef230 | 149 | static struct buffer *find_start_buffer; |
8e2911c2 | 150 | static EMACS_INT find_start_begv; |
37bef230 | 151 | static int find_start_modiff; |
6a140a74 RS |
152 | |
153 | ||
f57e2426 J |
154 | static Lisp_Object skip_chars (int, Lisp_Object, Lisp_Object, int); |
155 | static Lisp_Object skip_syntaxes (int, Lisp_Object, Lisp_Object); | |
156 | static Lisp_Object scan_lists (EMACS_INT, EMACS_INT, EMACS_INT, int); | |
157 | static void scan_sexps_forward (struct lisp_parse_state *, | |
158 | EMACS_INT, EMACS_INT, EMACS_INT, int, | |
159 | int, Lisp_Object, int); | |
160 | static int in_classes (int, Lisp_Object); | |
195d1361 | 161 | \f |
c5683ceb SM |
162 | /* Whether the syntax of the character C has the prefix flag set. */ |
163 | int syntax_prefix_flag_p (int c) | |
164 | { | |
165 | return SYNTAX_PREFIX (c); | |
166 | } | |
195d1361 RS |
167 | |
168 | struct gl_state_s gl_state; /* Global state of syntax parser. */ | |
169 | ||
971de7fb | 170 | INTERVAL interval_of (int, Lisp_Object); |
195d1361 RS |
171 | #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals |
172 | to scan to property-change. */ | |
173 | ||
6a140a74 RS |
174 | /* Update gl_state to an appropriate interval which contains CHARPOS. The |
175 | sign of COUNT give the relative position of CHARPOS wrt the previously | |
195d1361 | 176 | valid interval. If INIT, only [be]_property fields of gl_state are |
6a140a74 | 177 | valid at start, the rest is filled basing on OBJECT. |
195d1361 | 178 | |
6a140a74 | 179 | `gl_state.*_i' are the intervals, and CHARPOS is further in the search |
195d1361 RS |
180 | direction than the intervals - or in an interval. We update the |
181 | current syntax-table basing on the property of this interval, and | |
6a140a74 | 182 | update the interval to start further than CHARPOS - or be |
195d1361 | 183 | NULL_INTERVAL. We also update lim_property to be the next value of |
6a140a74 | 184 | charpos to call this subroutine again - or be before/after the |
195d1361 RS |
185 | start/end of OBJECT. */ |
186 | ||
187 | void | |
4f3a2f8d EZ |
188 | update_syntax_table (EMACS_INT charpos, int count, int init, |
189 | Lisp_Object object) | |
195d1361 RS |
190 | { |
191 | Lisp_Object tmp_table; | |
4ffe723b | 192 | int cnt = 0, invalidate = 1; |
5a774522 | 193 | INTERVAL i; |
195d1361 RS |
194 | |
195 | if (init) | |
196 | { | |
bb0de084 | 197 | gl_state.old_prop = Qnil; |
195d1361 RS |
198 | gl_state.start = gl_state.b_property; |
199 | gl_state.stop = gl_state.e_property; | |
bb0de084 SM |
200 | i = interval_of (charpos, object); |
201 | gl_state.backward_i = gl_state.forward_i = i; | |
195d1361 RS |
202 | invalidate = 0; |
203 | if (NULL_INTERVAL_P (i)) | |
204 | return; | |
f902a008 | 205 | /* interval_of updates only ->position of the return value, so |
d80f4cc7 | 206 | update the parents manually to speed up update_interval. */ |
bb0de084 | 207 | while (!NULL_PARENT (i)) |
d80f4cc7 RS |
208 | { |
209 | if (AM_RIGHT_CHILD (i)) | |
439d5cb4 | 210 | INTERVAL_PARENT (i)->position = i->position |
d80f4cc7 | 211 | - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */ |
439d5cb4 KR |
212 | - TOTAL_LENGTH (INTERVAL_PARENT (i)) |
213 | + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i)); | |
d80f4cc7 | 214 | else |
439d5cb4 | 215 | INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i) |
d80f4cc7 | 216 | + TOTAL_LENGTH (i); |
439d5cb4 | 217 | i = INTERVAL_PARENT (i); |
d80f4cc7 RS |
218 | } |
219 | i = gl_state.forward_i; | |
bb0de084 | 220 | gl_state.b_property = i->position - gl_state.offset; |
ee0cdb48 | 221 | gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset; |
195d1361 RS |
222 | goto update; |
223 | } | |
5a774522 | 224 | i = count > 0 ? gl_state.forward_i : gl_state.backward_i; |
195d1361 | 225 | |
423e705d | 226 | /* We are guaranteed to be called with CHARPOS either in i, |
6a140a74 | 227 | or further off. */ |
195d1361 RS |
228 | if (NULL_INTERVAL_P (i)) |
229 | error ("Error in syntax_table logic for to-the-end intervals"); | |
6a140a74 | 230 | else if (charpos < i->position) /* Move left. */ |
195d1361 RS |
231 | { |
232 | if (count > 0) | |
f902a008 | 233 | error ("Error in syntax_table logic for intervals <-"); |
195d1361 | 234 | /* Update the interval. */ |
6a140a74 | 235 | i = update_interval (i, charpos); |
bb0de084 | 236 | if (INTERVAL_LAST_POS (i) != gl_state.b_property) |
195d1361 RS |
237 | { |
238 | invalidate = 0; | |
195d1361 | 239 | gl_state.forward_i = i; |
ee0cdb48 | 240 | gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset; |
195d1361 | 241 | } |
423e705d | 242 | } |
6a140a74 | 243 | else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */ |
195d1361 RS |
244 | { |
245 | if (count < 0) | |
f902a008 | 246 | error ("Error in syntax_table logic for intervals ->"); |
195d1361 | 247 | /* Update the interval. */ |
6a140a74 | 248 | i = update_interval (i, charpos); |
bb0de084 | 249 | if (i->position != gl_state.e_property) |
195d1361 RS |
250 | { |
251 | invalidate = 0; | |
195d1361 | 252 | gl_state.backward_i = i; |
bb0de084 | 253 | gl_state.b_property = i->position - gl_state.offset; |
195d1361 RS |
254 | } |
255 | } | |
195d1361 RS |
256 | |
257 | update: | |
258 | tmp_table = textget (i->plist, Qsyntax_table); | |
259 | ||
260 | if (invalidate) | |
261 | invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */ | |
7d0393cf | 262 | |
423e705d SM |
263 | if (invalidate) /* Did not get to adjacent interval. */ |
264 | { /* with the same table => */ | |
265 | /* invalidate the old range. */ | |
195d1361 RS |
266 | if (count > 0) |
267 | { | |
268 | gl_state.backward_i = i; | |
bb0de084 SM |
269 | gl_state.b_property = i->position - gl_state.offset; |
270 | } | |
271 | else | |
195d1361 | 272 | { |
bb0de084 | 273 | gl_state.forward_i = i; |
ee0cdb48 | 274 | gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset; |
195d1361 RS |
275 | } |
276 | } | |
37bef230 | 277 | |
bb0de084 | 278 | if (!EQ (tmp_table, gl_state.old_prop)) |
195d1361 | 279 | { |
bb0de084 SM |
280 | gl_state.current_syntax_table = tmp_table; |
281 | gl_state.old_prop = tmp_table; | |
282 | if (EQ (Fsyntax_table_p (tmp_table), Qt)) | |
283 | { | |
284 | gl_state.use_global = 0; | |
7d0393cf | 285 | } |
bb0de084 SM |
286 | else if (CONSP (tmp_table)) |
287 | { | |
288 | gl_state.use_global = 1; | |
289 | gl_state.global_code = tmp_table; | |
290 | } | |
7d0393cf | 291 | else |
bb0de084 SM |
292 | { |
293 | gl_state.use_global = 0; | |
294 | gl_state.current_syntax_table = current_buffer->syntax_table; | |
295 | } | |
195d1361 RS |
296 | } |
297 | ||
298 | while (!NULL_INTERVAL_P (i)) | |
299 | { | |
300 | if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table))) | |
301 | { | |
302 | if (count > 0) | |
bb0de084 SM |
303 | { |
304 | gl_state.e_property = i->position - gl_state.offset; | |
305 | gl_state.forward_i = i; | |
306 | } | |
307 | else | |
308 | { | |
5a774522 SM |
309 | gl_state.b_property |
310 | = i->position + LENGTH (i) - gl_state.offset; | |
bb0de084 SM |
311 | gl_state.backward_i = i; |
312 | } | |
313 | return; | |
195d1361 | 314 | } |
7d0393cf | 315 | else if (cnt == INTERVALS_AT_ONCE) |
195d1361 RS |
316 | { |
317 | if (count > 0) | |
bb0de084 | 318 | { |
5a774522 SM |
319 | gl_state.e_property |
320 | = i->position + LENGTH (i) - gl_state.offset | |
321 | /* e_property at EOB is not set to ZV but to ZV+1, so that | |
322 | we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without | |
323 | having to check eob between the two. */ | |
324 | + (NULL_INTERVAL_P (next_interval (i)) ? 1 : 0); | |
bb0de084 SM |
325 | gl_state.forward_i = i; |
326 | } | |
327 | else | |
328 | { | |
329 | gl_state.b_property = i->position - gl_state.offset; | |
330 | gl_state.backward_i = i; | |
331 | } | |
332 | return; | |
195d1361 RS |
333 | } |
334 | cnt++; | |
335 | i = count > 0 ? next_interval (i) : previous_interval (i); | |
336 | } | |
bb0de084 SM |
337 | eassert (NULL_INTERVAL_P (i)); /* This property goes to the end. */ |
338 | if (count > 0) | |
339 | gl_state.e_property = gl_state.stop; | |
340 | else | |
341 | gl_state.b_property = gl_state.start; | |
195d1361 RS |
342 | } |
343 | \f | |
6a140a74 RS |
344 | /* Returns TRUE if char at CHARPOS is quoted. |
345 | Global syntax-table data should be set up already to be good at CHARPOS | |
346 | or after. On return global syntax data is good for lookup at CHARPOS. */ | |
195d1361 RS |
347 | |
348 | static int | |
6449674e | 349 | char_quoted (EMACS_INT charpos, EMACS_INT bytepos) |
195d1361 RS |
350 | { |
351 | register enum syntaxcode code; | |
6449674e | 352 | register EMACS_INT beg = BEGV; |
195d1361 | 353 | register int quoted = 0; |
6449674e | 354 | EMACS_INT orig = charpos; |
6a140a74 | 355 | |
02d8b017 | 356 | while (charpos > beg) |
195d1361 | 357 | { |
ab229fdd | 358 | int c; |
02d8b017 | 359 | DEC_BOTH (charpos, bytepos); |
ab229fdd | 360 | |
6a140a74 | 361 | UPDATE_SYNTAX_TABLE_BACKWARD (charpos); |
327719ee | 362 | c = FETCH_CHAR_AS_MULTIBYTE (bytepos); |
ab229fdd | 363 | code = SYNTAX (c); |
6a140a74 RS |
364 | if (! (code == Scharquote || code == Sescape)) |
365 | break; | |
366 | ||
6a140a74 | 367 | quoted = !quoted; |
195d1361 | 368 | } |
6a140a74 RS |
369 | |
370 | UPDATE_SYNTAX_TABLE (orig); | |
195d1361 RS |
371 | return quoted; |
372 | } | |
6a140a74 RS |
373 | |
374 | /* Return the bytepos one character after BYTEPOS. | |
375 | We assume that BYTEPOS is not at the end of the buffer. */ | |
376 | ||
6449674e | 377 | INLINE EMACS_INT |
971de7fb | 378 | inc_bytepos (EMACS_INT bytepos) |
6a140a74 | 379 | { |
ef316cf0 RS |
380 | if (NILP (current_buffer->enable_multibyte_characters)) |
381 | return bytepos + 1; | |
382 | ||
6a140a74 RS |
383 | INC_POS (bytepos); |
384 | return bytepos; | |
385 | } | |
386 | ||
387 | /* Return the bytepos one character before BYTEPOS. | |
388 | We assume that BYTEPOS is not at the start of the buffer. */ | |
389 | ||
6449674e | 390 | INLINE EMACS_INT |
971de7fb | 391 | dec_bytepos (EMACS_INT bytepos) |
6a140a74 | 392 | { |
ef316cf0 RS |
393 | if (NILP (current_buffer->enable_multibyte_characters)) |
394 | return bytepos - 1; | |
395 | ||
6a140a74 RS |
396 | DEC_POS (bytepos); |
397 | return bytepos; | |
398 | } | |
195d1361 | 399 | \f |
2b34df4e | 400 | /* Return a defun-start position before POS and not too far before. |
7cc80f0a RS |
401 | It should be the last one before POS, or nearly the last. |
402 | ||
403 | When open_paren_in_column_0_is_defun_start is nonzero, | |
1fd1cc2f | 404 | only the beginning of the buffer is treated as a defun-start. |
7cc80f0a RS |
405 | |
406 | We record the information about where the scan started | |
407 | and what its result was, so that another call in the same area | |
408 | can return the same value very quickly. | |
195d1361 RS |
409 | |
410 | There is no promise at which position the global syntax data is | |
411 | valid on return from the subroutine, so the caller should explicitly | |
412 | update the global data. */ | |
37bef230 | 413 | |
6449674e | 414 | static EMACS_INT |
971de7fb | 415 | find_defun_start (EMACS_INT pos, EMACS_INT pos_byte) |
37bef230 | 416 | { |
8e2911c2 | 417 | EMACS_INT opoint = PT, opoint_byte = PT_BYTE; |
37bef230 | 418 | |
1fd1cc2f RS |
419 | if (!open_paren_in_column_0_is_defun_start) |
420 | { | |
421 | find_start_value_byte = BEGV_BYTE; | |
422 | return BEGV; | |
423 | } | |
424 | ||
37bef230 RS |
425 | /* Use previous finding, if it's valid and applies to this inquiry. */ |
426 | if (current_buffer == find_start_buffer | |
427 | /* Reuse the defun-start even if POS is a little farther on. | |
428 | POS might be in the next defun, but that's ok. | |
429 | Our value may not be the best possible, but will still be usable. */ | |
430 | && pos <= find_start_pos + 1000 | |
431 | && pos >= find_start_value | |
432 | && BEGV == find_start_begv | |
433 | && MODIFF == find_start_modiff) | |
434 | return find_start_value; | |
435 | ||
436 | /* Back up to start of line. */ | |
6a140a74 | 437 | scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1); |
37bef230 | 438 | |
195d1361 RS |
439 | /* We optimize syntax-table lookup for rare updates. Thus we accept |
440 | only those `^\s(' which are good in global _and_ text-property | |
441 | syntax-tables. */ | |
d48cd3f4 | 442 | SETUP_BUFFER_SYNTAX_TABLE (); |
1fd1cc2f | 443 | while (PT > BEGV) |
37bef230 | 444 | { |
ab229fdd AS |
445 | int c; |
446 | ||
1fd1cc2f RS |
447 | /* Open-paren at start of line means we may have found our |
448 | defun-start. */ | |
327719ee | 449 | c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE); |
ab229fdd | 450 | if (SYNTAX (c) == Sopen) |
195d1361 | 451 | { |
1fd1cc2f | 452 | SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */ |
327719ee | 453 | c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE); |
ab229fdd | 454 | if (SYNTAX (c) == Sopen) |
1fd1cc2f RS |
455 | break; |
456 | /* Now fallback to the default value. */ | |
d48cd3f4 | 457 | SETUP_BUFFER_SYNTAX_TABLE (); |
195d1361 | 458 | } |
1fd1cc2f RS |
459 | /* Move to beg of previous line. */ |
460 | scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1); | |
37bef230 RS |
461 | } |
462 | ||
463 | /* Record what we found, for the next try. */ | |
6a140a74 RS |
464 | find_start_value = PT; |
465 | find_start_value_byte = PT_BYTE; | |
37bef230 RS |
466 | find_start_buffer = current_buffer; |
467 | find_start_modiff = MODIFF; | |
468 | find_start_begv = BEGV; | |
469 | find_start_pos = pos; | |
470 | ||
6a140a74 RS |
471 | TEMP_SET_PT_BOTH (opoint, opoint_byte); |
472 | ||
37bef230 RS |
473 | return find_start_value; |
474 | } | |
475 | \f | |
f902a008 RS |
476 | /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */ |
477 | ||
478 | static int | |
4f3a2f8d | 479 | prev_char_comend_first (EMACS_INT pos, EMACS_INT pos_byte) |
f902a008 RS |
480 | { |
481 | int c, val; | |
482 | ||
483 | DEC_BOTH (pos, pos_byte); | |
484 | UPDATE_SYNTAX_TABLE_BACKWARD (pos); | |
485 | c = FETCH_CHAR (pos_byte); | |
486 | val = SYNTAX_COMEND_FIRST (c); | |
487 | UPDATE_SYNTAX_TABLE_FORWARD (pos + 1); | |
488 | return val; | |
489 | } | |
490 | ||
491 | /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */ | |
492 | ||
3f679f55 SM |
493 | /* static int |
494 | * prev_char_comstart_first (pos, pos_byte) | |
495 | * int pos, pos_byte; | |
496 | * { | |
497 | * int c, val; | |
7d0393cf | 498 | * |
3f679f55 SM |
499 | * DEC_BOTH (pos, pos_byte); |
500 | * UPDATE_SYNTAX_TABLE_BACKWARD (pos); | |
501 | * c = FETCH_CHAR (pos_byte); | |
502 | * val = SYNTAX_COMSTART_FIRST (c); | |
503 | * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1); | |
504 | * return val; | |
505 | * } */ | |
f902a008 | 506 | |
6a140a74 RS |
507 | /* Checks whether charpos FROM is at the end of a comment. |
508 | FROM_BYTE is the bytepos corresponding to FROM. | |
509 | Do not move back before STOP. | |
510 | ||
511 | Return a positive value if we find a comment ending at FROM/FROM_BYTE; | |
512 | return -1 otherwise. | |
513 | ||
514 | If successful, store the charpos of the comment's beginning | |
515 | into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR. | |
527a32d9 KH |
516 | |
517 | Global syntax data remains valid for backward search starting at | |
518 | the returned value (or at FROM, if the search was not successful). */ | |
195d1361 RS |
519 | |
520 | static int | |
971de7fb | 521 | back_comment (EMACS_INT from, EMACS_INT from_byte, EMACS_INT stop, int comnested, int comstyle, EMACS_INT *charpos_ptr, EMACS_INT *bytepos_ptr) |
195d1361 RS |
522 | { |
523 | /* Look back, counting the parity of string-quotes, | |
524 | and recording the comment-starters seen. | |
525 | When we reach a safe place, assume that's not in a string; | |
526 | then step the main scan to the earliest comment-starter seen | |
527 | an even number of string quotes away from the safe place. | |
528 | ||
529 | OFROM[I] is position of the earliest comment-starter seen | |
530 | which is I+2X quotes from the comment-end. | |
531 | PARITY is current parity of quotes from the comment end. */ | |
3ee5041c | 532 | int string_style = -1; /* Presumed outside of any string. */ |
195d1361 | 533 | int string_lossage = 0; |
f7c436c1 | 534 | /* Not a real lossage: indicates that we have passed a matching comment |
7d0393cf | 535 | starter plus a non-matching comment-ender, meaning that any matching |
f7c436c1 SM |
536 | comment-starter we might see later could be a false positive (hidden |
537 | inside another comment). | |
538 | Test case: { a (* b } c (* d *) */ | |
539 | int comment_lossage = 0; | |
8e2911c2 AS |
540 | EMACS_INT comment_end = from; |
541 | EMACS_INT comment_end_byte = from_byte; | |
542 | EMACS_INT comstart_pos = 0; | |
543 | EMACS_INT comstart_byte; | |
eb35b628 RS |
544 | /* Place where the containing defun starts, |
545 | or 0 if we didn't come across it yet. */ | |
8e2911c2 AS |
546 | EMACS_INT defun_start = 0; |
547 | EMACS_INT defun_start_byte = 0; | |
195d1361 | 548 | register enum syntaxcode code; |
95ff8dfc | 549 | int nesting = 1; /* current comment nesting */ |
ea315ed6 | 550 | int c; |
3f679f55 SM |
551 | int syntax = 0; |
552 | ||
553 | /* FIXME: A }} comment-ender style leads to incorrect behavior | |
554 | in the case of {{ c }}} because we ignore the last two chars which are | |
555 | assumed to be comment-enders although they aren't. */ | |
195d1361 RS |
556 | |
557 | /* At beginning of range to scan, we're outside of strings; | |
558 | that determines quote parity to the comment-end. */ | |
559 | while (from != stop) | |
560 | { | |
4f3a2f8d EZ |
561 | EMACS_INT temp_byte; |
562 | int prev_syntax, com2start, com2end; | |
fbb3da77 | 563 | int comstart; |
6a140a74 | 564 | |
195d1361 | 565 | /* Move back and examine a character. */ |
6a140a74 | 566 | DEC_BOTH (from, from_byte); |
195d1361 RS |
567 | UPDATE_SYNTAX_TABLE_BACKWARD (from); |
568 | ||
3f679f55 | 569 | prev_syntax = syntax; |
b7dbcc19 | 570 | c = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
3f679f55 | 571 | syntax = SYNTAX_WITH_FLAGS (c); |
195d1361 RS |
572 | code = SYNTAX (c); |
573 | ||
3f679f55 SM |
574 | /* Check for 2-char comment markers. */ |
575 | com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax) | |
576 | && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax) | |
c5683ceb SM |
577 | && (comstyle |
578 | == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax, syntax)) | |
3f679f55 SM |
579 | && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax) |
580 | || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested); | |
581 | com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax) | |
582 | && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax)); | |
fbb3da77 SM |
583 | comstart = (com2start || code == Scomment); |
584 | ||
3f679f55 SM |
585 | /* Nasty cases with overlapping 2-char comment markers: |
586 | - snmp-mode: -- c -- foo -- c -- | |
587 | --- c -- | |
588 | ------ c -- | |
589 | - c-mode: *||* | |
590 | |* *|* *| | |
591 | |*| |* |*| | |
592 | /// */ | |
593 | ||
594 | /* If a 2-char comment sequence partly overlaps with another, | |
fbb3da77 SM |
595 | we don't try to be clever. E.g. |*| in C, or }% in modes that |
596 | have %..\n and %{..}%. */ | |
597 | if (from > stop && (com2end || comstart)) | |
195d1361 | 598 | { |
4f3a2f8d EZ |
599 | EMACS_INT next = from, next_byte = from_byte; |
600 | int next_c, next_syntax; | |
3f679f55 SM |
601 | DEC_BOTH (next, next_byte); |
602 | UPDATE_SYNTAX_TABLE_BACKWARD (next); | |
b7dbcc19 | 603 | next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte); |
3f679f55 | 604 | next_syntax = SYNTAX_WITH_FLAGS (next_c); |
fbb3da77 | 605 | if (((comstart || comnested) |
3f679f55 SM |
606 | && SYNTAX_FLAGS_COMEND_SECOND (syntax) |
607 | && SYNTAX_FLAGS_COMEND_FIRST (next_syntax)) | |
608 | || ((com2end || comnested) | |
609 | && SYNTAX_FLAGS_COMSTART_SECOND (syntax) | |
c5683ceb SM |
610 | && (comstyle |
611 | == SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_syntax)) | |
3f679f55 SM |
612 | && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax))) |
613 | goto lossage; | |
614 | /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */ | |
f902a008 | 615 | } |
3f679f55 SM |
616 | |
617 | if (com2start && comstart_pos == 0) | |
618 | /* We're looking at a comment starter. But it might be a comment | |
619 | ender as well (see snmp-mode). The first time we see one, we | |
620 | need to consider it as a comment starter, | |
621 | and the subsequent times as a comment ender. */ | |
622 | com2end = 0; | |
623 | ||
624 | /* Turn a 2-char comment sequences into the appropriate syntax. */ | |
625 | if (com2end) | |
626 | code = Sendcomment; | |
627 | else if (com2start) | |
628 | code = Scomment; | |
629 | /* Ignore comment starters of a different style. */ | |
630 | else if (code == Scomment | |
c5683ceb | 631 | && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) |
3f679f55 | 632 | || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested)) |
1aa963c8 | 633 | continue; |
195d1361 | 634 | |
9828a477 KH |
635 | /* Ignore escaped characters, except comment-enders. */ |
636 | if (code != Sendcomment && char_quoted (from, from_byte)) | |
195d1361 RS |
637 | continue; |
638 | ||
3ee5041c | 639 | switch (code) |
195d1361 | 640 | { |
3ee5041c SM |
641 | case Sstring_fence: |
642 | case Scomment_fence: | |
643 | c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE); | |
644 | case Sstring: | |
645 | /* Track parity of quotes. */ | |
646 | if (string_style == -1) | |
647 | /* Entering a string. */ | |
648 | string_style = c; | |
649 | else if (string_style == c) | |
650 | /* Leaving the string. */ | |
651 | string_style = -1; | |
652 | else | |
653 | /* If we have two kinds of string delimiters. | |
654 | There's no way to grok this scanning backwards. */ | |
195d1361 | 655 | string_lossage = 1; |
3ee5041c | 656 | break; |
7d0393cf | 657 | |
3ee5041c SM |
658 | case Scomment: |
659 | /* We've already checked that it is the relevant comstyle. */ | |
f7c436c1 | 660 | if (string_style != -1 || comment_lossage || string_lossage) |
3ee5041c SM |
661 | /* There are odd string quotes involved, so let's be careful. |
662 | Test case in Pascal: " { " a { " } */ | |
663 | goto lossage; | |
664 | ||
f7c436c1 SM |
665 | if (!comnested) |
666 | { | |
667 | /* Record best comment-starter so far. */ | |
668 | comstart_pos = from; | |
669 | comstart_byte = from_byte; | |
670 | } | |
671 | else if (--nesting <= 0) | |
95ff8dfc RS |
672 | /* nested comments have to be balanced, so we don't need to |
673 | keep looking for earlier ones. We use here the same (slightly | |
674 | incorrect) reasoning as below: since it is followed by uniform | |
675 | paired string quotes, this comment-start has to be outside of | |
676 | strings, else the comment-end itself would be inside a string. */ | |
677 | goto done; | |
3ee5041c SM |
678 | break; |
679 | ||
02010917 | 680 | case Sendcomment: |
c5683ceb | 681 | if (SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == comstyle |
d070eb22 | 682 | && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)) |
3f679f55 | 683 | || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested) |
02010917 SM |
684 | /* This is the same style of comment ender as ours. */ |
685 | { | |
686 | if (comnested) | |
687 | nesting++; | |
688 | else | |
689 | /* Anything before that can't count because it would match | |
690 | this comment-ender rather than ours. */ | |
691 | from = stop; /* Break out of the loop. */ | |
692 | } | |
f7c436c1 SM |
693 | else if (comstart_pos != 0 || c != '\n') |
694 | /* We're mixing comment styles here, so we'd better be careful. | |
695 | The (comstart_pos != 0 || c != '\n') check is not quite correct | |
696 | (we should just always set comment_lossage), but removing it | |
697 | would imply that any multiline comment in C would go through | |
698 | lossage, which seems overkill. | |
699 | The failure should only happen in the rare cases such as | |
700 | { (* } *) */ | |
701 | comment_lossage = 1; | |
02010917 | 702 | break; |
195d1361 | 703 | |
02010917 SM |
704 | case Sopen: |
705 | /* Assume a defun-start point is outside of strings. */ | |
706 | if (open_paren_in_column_0_is_defun_start | |
707 | && (from == stop | |
708 | || (temp_byte = dec_bytepos (from_byte), | |
709 | FETCH_CHAR (temp_byte) == '\n'))) | |
710 | { | |
711 | defun_start = from; | |
712 | defun_start_byte = from_byte; | |
713 | from = stop; /* Break out of the loop. */ | |
714 | } | |
eb35b628 | 715 | break; |
02010917 SM |
716 | |
717 | default: | |
f7c436c1 | 718 | break; |
eb35b628 | 719 | } |
195d1361 RS |
720 | } |
721 | ||
722 | if (comstart_pos == 0) | |
723 | { | |
724 | from = comment_end; | |
6a140a74 | 725 | from_byte = comment_end_byte; |
195d1361 RS |
726 | UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1); |
727 | } | |
f7c436c1 SM |
728 | /* If comstart_pos is set and we get here (ie. didn't jump to `lossage' |
729 | or `done'), then we've found the beginning of the non-nested comment. */ | |
730 | else if (1) /* !comnested */ | |
195d1361 RS |
731 | { |
732 | from = comstart_pos; | |
6a140a74 | 733 | from_byte = comstart_byte; |
789f3320 | 734 | UPDATE_SYNTAX_TABLE_FORWARD (from - 1); |
195d1361 RS |
735 | } |
736 | else | |
737 | { | |
3ee5041c SM |
738 | struct lisp_parse_state state; |
739 | lossage: | |
195d1361 RS |
740 | /* We had two kinds of string delimiters mixed up |
741 | together. Decode this going forwards. | |
02010917 | 742 | Scan fwd from a known safe place (beginning-of-defun) |
195d1361 RS |
743 | to the one in question; this records where we |
744 | last passed a comment starter. */ | |
eb35b628 RS |
745 | /* If we did not already find the defun start, find it now. */ |
746 | if (defun_start == 0) | |
747 | { | |
748 | defun_start = find_defun_start (comment_end, comment_end_byte); | |
749 | defun_start_byte = find_start_value_byte; | |
750 | } | |
02010917 | 751 | do |
195d1361 | 752 | { |
02010917 SM |
753 | scan_sexps_forward (&state, |
754 | defun_start, defun_start_byte, | |
755 | comment_end, -10000, 0, Qnil, 0); | |
756 | defun_start = comment_end; | |
757 | if (state.incomment == (comnested ? 1 : -1) | |
758 | && state.comstyle == comstyle) | |
759 | from = state.comstr_start; | |
760 | else | |
761 | { | |
762 | from = comment_end; | |
763 | if (state.incomment) | |
764 | /* If comment_end is inside some other comment, maybe ours | |
765 | is nested, so we need to try again from within the | |
766 | surrounding comment. Example: { a (* " *) */ | |
767 | { | |
768 | /* FIXME: We should advance by one or two chars. */ | |
769 | defun_start = state.comstr_start + 2; | |
770 | defun_start_byte = CHAR_TO_BYTE (defun_start); | |
771 | } | |
772 | } | |
773 | } while (defun_start < comment_end); | |
774 | ||
6a140a74 | 775 | from_byte = CHAR_TO_BYTE (from); |
195d1361 RS |
776 | UPDATE_SYNTAX_TABLE_FORWARD (from - 1); |
777 | } | |
7d0393cf | 778 | |
95ff8dfc | 779 | done: |
6a140a74 RS |
780 | *charpos_ptr = from; |
781 | *bytepos_ptr = from_byte; | |
782 | ||
1aa963c8 | 783 | return (from == comment_end) ? -1 : from; |
195d1361 RS |
784 | } |
785 | \f | |
8489eb67 | 786 | DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0, |
fdb82f93 PJ |
787 | doc: /* Return t if OBJECT is a syntax table. |
788 | Currently, any char-table counts as a syntax table. */) | |
5842a27b | 789 | (Lisp_Object object) |
8489eb67 | 790 | { |
2203e1e8 | 791 | if (CHAR_TABLE_P (object) |
e704cb4b | 792 | && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table)) |
8489eb67 RS |
793 | return Qt; |
794 | return Qnil; | |
795 | } | |
796 | ||
8ea151b2 | 797 | static void |
971de7fb | 798 | check_syntax_table (Lisp_Object obj) |
8489eb67 | 799 | { |
47f5f6ae KS |
800 | CHECK_TYPE (CHAR_TABLE_P (obj) && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table), |
801 | Qsyntax_table_p, obj); | |
7d0393cf | 802 | } |
8489eb67 | 803 | |
8489eb67 | 804 | DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0, |
fdb82f93 PJ |
805 | doc: /* Return the current syntax table. |
806 | This is the one specified by the current buffer. */) | |
5842a27b | 807 | (void) |
8489eb67 RS |
808 | { |
809 | return current_buffer->syntax_table; | |
810 | } | |
811 | ||
812 | DEFUN ("standard-syntax-table", Fstandard_syntax_table, | |
813 | Sstandard_syntax_table, 0, 0, 0, | |
fdb82f93 PJ |
814 | doc: /* Return the standard syntax table. |
815 | This is the one used for new buffers. */) | |
5842a27b | 816 | (void) |
8489eb67 RS |
817 | { |
818 | return Vstandard_syntax_table; | |
819 | } | |
820 | ||
821 | DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0, | |
fdb82f93 PJ |
822 | doc: /* Construct a new syntax table and return it. |
823 | It is a copy of the TABLE, which defaults to the standard syntax table. */) | |
5842a27b | 824 | (Lisp_Object table) |
8489eb67 | 825 | { |
8ea151b2 RS |
826 | Lisp_Object copy; |
827 | ||
265a9e55 | 828 | if (!NILP (table)) |
8ea151b2 RS |
829 | check_syntax_table (table); |
830 | else | |
831 | table = Vstandard_syntax_table; | |
832 | ||
833 | copy = Fcopy_sequence (table); | |
0f867324 RS |
834 | |
835 | /* Only the standard syntax table should have a default element. | |
836 | Other syntax tables should inherit from parents instead. */ | |
837 | XCHAR_TABLE (copy)->defalt = Qnil; | |
838 | ||
839 | /* Copied syntax tables should all have parents. | |
840 | If we copied one with no parent, such as the standard syntax table, | |
841 | use the standard syntax table as the copy's parent. */ | |
842 | if (NILP (XCHAR_TABLE (copy)->parent)) | |
843 | Fset_char_table_parent (copy, Vstandard_syntax_table); | |
8ea151b2 | 844 | return copy; |
8489eb67 RS |
845 | } |
846 | ||
847 | DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0, | |
fdb82f93 PJ |
848 | doc: /* Select a new syntax table for the current buffer. |
849 | One argument, a syntax table. */) | |
5842a27b | 850 | (Lisp_Object table) |
8489eb67 | 851 | { |
4e4dfa78 | 852 | int idx; |
8ea151b2 | 853 | check_syntax_table (table); |
8489eb67 RS |
854 | current_buffer->syntax_table = table; |
855 | /* Indicate that this buffer now has a specified syntax table. */ | |
f6cd0527 GM |
856 | idx = PER_BUFFER_VAR_IDX (syntax_table); |
857 | SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1); | |
8489eb67 RS |
858 | return table; |
859 | } | |
860 | \f | |
861 | /* Convert a letter which signifies a syntax code | |
862 | into the code it signifies. | |
195d1361 | 863 | This is used by modify-syntax-entry, and other things. */ |
8489eb67 RS |
864 | |
865 | unsigned char syntax_spec_code[0400] = | |
866 | { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, | |
867 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, | |
868 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, | |
869 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, | |
195d1361 | 870 | (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377, |
8489eb67 RS |
871 | (char) Smath, 0377, 0377, (char) Squote, |
872 | (char) Sopen, (char) Sclose, 0377, 0377, | |
873 | 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote, | |
874 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, | |
875 | 0377, 0377, 0377, 0377, | |
876 | (char) Scomment, 0377, (char) Sendcomment, 0377, | |
6cb71bf6 | 877 | (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */ |
8489eb67 RS |
878 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, |
879 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword, | |
880 | 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol, | |
881 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */ | |
882 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, | |
883 | 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword, | |
195d1361 | 884 | 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377 |
8489eb67 RS |
885 | }; |
886 | ||
195d1361 | 887 | /* Indexed by syntax code, give the letter that describes it. */ |
8489eb67 | 888 | |
195d1361 | 889 | char syntax_code_spec[16] = |
8489eb67 | 890 | { |
195d1361 RS |
891 | ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@', |
892 | '!', '|' | |
8489eb67 | 893 | }; |
93da5fff KH |
894 | |
895 | /* Indexed by syntax code, give the object (cons of syntax code and | |
896 | nil) to be stored in syntax table. Since these objects can be | |
897 | shared among syntax tables, we generate them in advance. By | |
898 | sharing objects, the function `describe-syntax' can give a more | |
899 | compact listing. */ | |
900 | static Lisp_Object Vsyntax_code_object; | |
901 | ||
8489eb67 RS |
902 | \f |
903 | DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0, | |
fdb82f93 | 904 | doc: /* Return the syntax code of CHARACTER, described by a character. |
e2b6daf4 CY |
905 | For example, if CHARACTER is a word constituent, the |
906 | character `w' (119) is returned. | |
fdb82f93 PJ |
907 | The characters that correspond to various syntax codes |
908 | are listed in the documentation of `modify-syntax-entry'. */) | |
5842a27b | 909 | (Lisp_Object character) |
8489eb67 | 910 | { |
8ea151b2 | 911 | int char_int; |
774b9a60 | 912 | CHECK_CHARACTER (character); |
2203e1e8 | 913 | char_int = XINT (character); |
d48cd3f4 | 914 | SETUP_BUFFER_SYNTAX_TABLE (); |
8ea151b2 | 915 | return make_number (syntax_code_spec[(int) SYNTAX (char_int)]); |
beefa22e RS |
916 | } |
917 | ||
918 | DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0, | |
fdb82f93 | 919 | doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */) |
5842a27b | 920 | (Lisp_Object character) |
beefa22e | 921 | { |
8ea151b2 | 922 | int char_int, code; |
b7826503 | 923 | CHECK_NUMBER (character); |
2203e1e8 | 924 | char_int = XINT (character); |
d48cd3f4 | 925 | SETUP_BUFFER_SYNTAX_TABLE (); |
8ea151b2 | 926 | code = SYNTAX (char_int); |
a8bd7cd8 | 927 | if (code == Sopen || code == Sclose) |
2e34157c | 928 | return SYNTAX_MATCH (char_int); |
beefa22e | 929 | return Qnil; |
8489eb67 RS |
930 | } |
931 | ||
c65adb44 | 932 | DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0, |
fdb82f93 PJ |
933 | doc: /* Convert a syntax specification STRING into syntax cell form. |
934 | STRING should be a string as it is allowed as argument of | |
935 | `modify-syntax-entry'. Value is the equivalent cons cell | |
bce3dc36 | 936 | \(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table' |
fdb82f93 | 937 | text property. */) |
5842a27b | 938 | (Lisp_Object string) |
8489eb67 | 939 | { |
2e567bd3 | 940 | register const unsigned char *p; |
8489eb67 | 941 | register enum syntaxcode code; |
247e20a8 | 942 | int val; |
8ea151b2 | 943 | Lisp_Object match; |
8489eb67 | 944 | |
b7826503 | 945 | CHECK_STRING (string); |
8489eb67 | 946 | |
d5db4077 | 947 | p = SDATA (string); |
8489eb67 RS |
948 | code = (enum syntaxcode) syntax_spec_code[*p++]; |
949 | if (((int) code & 0377) == 0377) | |
b764a653 | 950 | error ("Invalid syntax description letter: %c", p[-1]); |
8489eb67 | 951 | |
8ea151b2 | 952 | if (code == Sinherit) |
c65adb44 | 953 | return Qnil; |
8ea151b2 RS |
954 | |
955 | if (*p) | |
d1be9f0f | 956 | { |
93da5fff | 957 | int len; |
62a6e103 | 958 | int character = STRING_CHAR_AND_LENGTH (p, len); |
93da5fff | 959 | XSETINT (match, character); |
d1be9f0f RS |
960 | if (XFASTINT (match) == ' ') |
961 | match = Qnil; | |
93da5fff | 962 | p += len; |
d1be9f0f RS |
963 | } |
964 | else | |
8ea151b2 | 965 | match = Qnil; |
8489eb67 | 966 | |
8ea151b2 | 967 | val = (int) code; |
8489eb67 RS |
968 | while (*p) |
969 | switch (*p++) | |
970 | { | |
971 | case '1': | |
247e20a8 | 972 | val |= 1 << 16; |
8489eb67 RS |
973 | break; |
974 | ||
975 | case '2': | |
247e20a8 | 976 | val |= 1 << 17; |
8489eb67 RS |
977 | break; |
978 | ||
979 | case '3': | |
247e20a8 | 980 | val |= 1 << 18; |
8489eb67 RS |
981 | break; |
982 | ||
983 | case '4': | |
247e20a8 | 984 | val |= 1 << 19; |
8489eb67 RS |
985 | break; |
986 | ||
987 | case 'p': | |
247e20a8 | 988 | val |= 1 << 20; |
8489eb67 | 989 | break; |
e5d4f4dc RS |
990 | |
991 | case 'b': | |
247e20a8 | 992 | val |= 1 << 21; |
e5d4f4dc | 993 | break; |
95ff8dfc RS |
994 | |
995 | case 'n': | |
996 | val |= 1 << 22; | |
997 | break; | |
c5683ceb SM |
998 | |
999 | case 'c': | |
1000 | val |= 1 << 23; | |
1001 | break; | |
8489eb67 | 1002 | } |
7d0393cf | 1003 | |
93da5fff | 1004 | if (val < XVECTOR (Vsyntax_code_object)->size && NILP (match)) |
c65adb44 | 1005 | return XVECTOR (Vsyntax_code_object)->contents[val]; |
93da5fff KH |
1006 | else |
1007 | /* Since we can't use a shared object, let's make a new one. */ | |
c65adb44 SM |
1008 | return Fcons (make_number (val), match); |
1009 | } | |
1010 | ||
fdb82f93 | 1011 | /* I really don't know why this is interactive |
2c9e1900 | 1012 | help-form should at least be made useful whilst reading the second arg. */ |
7d0393cf | 1013 | DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3, |
c65adb44 | 1014 | "cSet syntax for character: \nsSet syntax for %s to: ", |
a50a10a0 | 1015 | doc: /* Set syntax for character CHAR according to string NEWENTRY. |
d7ee9fab | 1016 | The syntax is changed only for table SYNTAX-TABLE, which defaults to |
fdb82f93 | 1017 | the current buffer's syntax table. |
5c7b02ab | 1018 | CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters |
fedc6ab5 | 1019 | in the range MIN to MAX are changed. |
fdb82f93 PJ |
1020 | The first character of NEWENTRY should be one of the following: |
1021 | Space or - whitespace syntax. w word constituent. | |
1022 | _ symbol constituent. . punctuation. | |
1023 | ( open-parenthesis. ) close-parenthesis. | |
1024 | " string quote. \\ escape. | |
1025 | $ paired delimiter. ' expression quote or prefix operator. | |
1026 | < comment starter. > comment ender. | |
1027 | / character-quote. @ inherit from `standard-syntax-table'. | |
1028 | | generic string fence. ! generic comment fence. | |
1029 | ||
1030 | Only single-character comment start and end sequences are represented thus. | |
1031 | Two-character sequences are represented as described below. | |
1032 | The second character of NEWENTRY is the matching parenthesis, | |
1033 | used only if the first character is `(' or `)'. | |
1034 | Any additional characters are flags. | |
1035 | Defined flags are the characters 1, 2, 3, 4, b, p, and n. | |
a50a10a0 PJ |
1036 | 1 means CHAR is the start of a two-char comment start sequence. |
1037 | 2 means CHAR is the second character of such a sequence. | |
1038 | 3 means CHAR is the start of a two-char comment end sequence. | |
1039 | 4 means CHAR is the second character of such a sequence. | |
fdb82f93 | 1040 | |
c5683ceb | 1041 | There can be several orthogonal comment sequences. This is to support |
fdb82f93 PJ |
1042 | language modes such as C++. By default, all comment sequences are of style |
1043 | a, but you can set the comment sequence style to b (on the second character | |
c5683ceb SM |
1044 | of a comment-start, and the first character of a comment-end sequence) and/or |
1045 | c (on any of its chars) using this flag: | |
a50a10a0 | 1046 | b means CHAR is part of comment sequence b. |
c5683ceb | 1047 | c means CHAR is part of comment sequence c. |
a50a10a0 | 1048 | n means CHAR is part of a nestable comment sequence. |
fdb82f93 | 1049 | |
a50a10a0 | 1050 | p means CHAR is a prefix character for `backward-prefix-chars'; |
fdb82f93 | 1051 | such characters are treated as whitespace when they occur |
a50a10a0 | 1052 | between expressions. |
fedc6ab5 | 1053 | usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */) |
5842a27b | 1054 | (Lisp_Object c, Lisp_Object newentry, Lisp_Object syntax_table) |
c65adb44 | 1055 | { |
5c7b02ab KH |
1056 | if (CONSP (c)) |
1057 | { | |
8f924df7 KH |
1058 | CHECK_CHARACTER_CAR (c); |
1059 | CHECK_CHARACTER_CDR (c); | |
5c7b02ab KH |
1060 | } |
1061 | else | |
1062 | CHECK_CHARACTER (c); | |
c65adb44 SM |
1063 | |
1064 | if (NILP (syntax_table)) | |
1065 | syntax_table = current_buffer->syntax_table; | |
1066 | else | |
1067 | check_syntax_table (syntax_table); | |
8489eb67 | 1068 | |
5c7b02ab KH |
1069 | newentry = Fstring_to_syntax (newentry); |
1070 | if (CONSP (c)) | |
1071 | SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry); | |
1072 | else | |
1073 | SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry); | |
e5b94d44 CY |
1074 | |
1075 | /* We clear the regexp cache, since character classes can now have | |
1076 | different values from those in the compiled regexps.*/ | |
1077 | clear_regexp_cache (); | |
1078 | ||
8489eb67 RS |
1079 | return Qnil; |
1080 | } | |
1081 | \f | |
1082 | /* Dump syntax table to buffer in human-readable format */ | |
1083 | ||
62abe9cb SM |
1084 | DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value, |
1085 | Sinternal_describe_syntax_value, 1, 1, 0, | |
1086 | doc: /* Insert a description of the internal syntax description SYNTAX at point. */) | |
5842a27b | 1087 | (Lisp_Object syntax) |
8489eb67 RS |
1088 | { |
1089 | register enum syntaxcode code; | |
c5683ceb SM |
1090 | int syntax_code; |
1091 | char desc, start1, start2, end1, end2, prefix, | |
1092 | comstyleb, comstylec, comnested; | |
8489eb67 | 1093 | char str[2]; |
62abe9cb | 1094 | Lisp_Object first, match_lisp, value = syntax; |
8489eb67 | 1095 | |
8ea151b2 RS |
1096 | if (NILP (value)) |
1097 | { | |
62abe9cb SM |
1098 | insert_string ("default"); |
1099 | return syntax; | |
8ea151b2 RS |
1100 | } |
1101 | ||
908b7fea KH |
1102 | if (CHAR_TABLE_P (value)) |
1103 | { | |
62abe9cb SM |
1104 | insert_string ("deeper char-table ..."); |
1105 | return syntax; | |
908b7fea KH |
1106 | } |
1107 | ||
8ea151b2 RS |
1108 | if (!CONSP (value)) |
1109 | { | |
62abe9cb SM |
1110 | insert_string ("invalid"); |
1111 | return syntax; | |
8ea151b2 RS |
1112 | } |
1113 | ||
c1d497be KR |
1114 | first = XCAR (value); |
1115 | match_lisp = XCDR (value); | |
8ea151b2 RS |
1116 | |
1117 | if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp))) | |
8489eb67 | 1118 | { |
62abe9cb SM |
1119 | insert_string ("invalid"); |
1120 | return syntax; | |
8489eb67 RS |
1121 | } |
1122 | ||
c5683ceb SM |
1123 | syntax_code = XINT (first); |
1124 | code = (enum syntaxcode) (syntax_code & 0377); | |
1125 | start1 = SYNTAX_FLAGS_COMSTART_FIRST (syntax_code); | |
1126 | start2 = SYNTAX_FLAGS_COMSTART_SECOND (syntax_code);; | |
1127 | end1 = SYNTAX_FLAGS_COMEND_FIRST (syntax_code); | |
1128 | end2 = SYNTAX_FLAGS_COMEND_SECOND (syntax_code); | |
1129 | prefix = SYNTAX_FLAGS_PREFIX (syntax_code); | |
1130 | comstyleb = SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code); | |
1131 | comstylec = SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code); | |
1132 | comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax_code); | |
8489eb67 RS |
1133 | |
1134 | if ((int) code < 0 || (int) code >= (int) Smax) | |
1135 | { | |
1136 | insert_string ("invalid"); | |
62abe9cb | 1137 | return syntax; |
8489eb67 RS |
1138 | } |
1139 | desc = syntax_code_spec[(int) code]; | |
1140 | ||
1141 | str[0] = desc, str[1] = 0; | |
1142 | insert (str, 1); | |
1143 | ||
93da5fff KH |
1144 | if (NILP (match_lisp)) |
1145 | insert (" ", 1); | |
1146 | else | |
1147 | insert_char (XINT (match_lisp)); | |
8489eb67 | 1148 | |
8489eb67 RS |
1149 | if (start1) |
1150 | insert ("1", 1); | |
1151 | if (start2) | |
1152 | insert ("2", 1); | |
1153 | ||
1154 | if (end1) | |
1155 | insert ("3", 1); | |
1156 | if (end2) | |
1157 | insert ("4", 1); | |
1158 | ||
1159 | if (prefix) | |
1160 | insert ("p", 1); | |
c5683ceb | 1161 | if (comstyleb) |
e5d4f4dc | 1162 | insert ("b", 1); |
c5683ceb SM |
1163 | if (comstylec) |
1164 | insert ("c", 1); | |
e6365855 SM |
1165 | if (comnested) |
1166 | insert ("n", 1); | |
8489eb67 RS |
1167 | |
1168 | insert_string ("\twhich means: "); | |
1169 | ||
0220c518 | 1170 | switch (SWITCH_ENUM_CAST (code)) |
8489eb67 RS |
1171 | { |
1172 | case Swhitespace: | |
1173 | insert_string ("whitespace"); break; | |
1174 | case Spunct: | |
1175 | insert_string ("punctuation"); break; | |
1176 | case Sword: | |
1177 | insert_string ("word"); break; | |
1178 | case Ssymbol: | |
1179 | insert_string ("symbol"); break; | |
1180 | case Sopen: | |
1181 | insert_string ("open"); break; | |
1182 | case Sclose: | |
1183 | insert_string ("close"); break; | |
1184 | case Squote: | |
d671382e | 1185 | insert_string ("prefix"); break; |
8489eb67 RS |
1186 | case Sstring: |
1187 | insert_string ("string"); break; | |
1188 | case Smath: | |
1189 | insert_string ("math"); break; | |
1190 | case Sescape: | |
1191 | insert_string ("escape"); break; | |
1192 | case Scharquote: | |
1193 | insert_string ("charquote"); break; | |
1194 | case Scomment: | |
1195 | insert_string ("comment"); break; | |
1196 | case Sendcomment: | |
1197 | insert_string ("endcomment"); break; | |
d671382e SM |
1198 | case Sinherit: |
1199 | insert_string ("inherit"); break; | |
1200 | case Scomment_fence: | |
1201 | insert_string ("comment fence"); break; | |
1202 | case Sstring_fence: | |
1203 | insert_string ("string fence"); break; | |
8489eb67 RS |
1204 | default: |
1205 | insert_string ("invalid"); | |
62abe9cb | 1206 | return syntax; |
8489eb67 RS |
1207 | } |
1208 | ||
8ea151b2 | 1209 | if (!NILP (match_lisp)) |
8489eb67 RS |
1210 | { |
1211 | insert_string (", matches "); | |
8ea151b2 | 1212 | insert_char (XINT (match_lisp)); |
8489eb67 RS |
1213 | } |
1214 | ||
1215 | if (start1) | |
1216 | insert_string (",\n\t is the first character of a comment-start sequence"); | |
1217 | if (start2) | |
1218 | insert_string (",\n\t is the second character of a comment-start sequence"); | |
1219 | ||
1220 | if (end1) | |
1221 | insert_string (",\n\t is the first character of a comment-end sequence"); | |
1222 | if (end2) | |
1223 | insert_string (",\n\t is the second character of a comment-end sequence"); | |
c5683ceb | 1224 | if (comstyleb) |
e5d4f4dc | 1225 | insert_string (" (comment style b)"); |
c5683ceb SM |
1226 | if (comstylec) |
1227 | insert_string (" (comment style c)"); | |
e6365855 SM |
1228 | if (comnested) |
1229 | insert_string (" (nestable)"); | |
e5d4f4dc | 1230 | |
8489eb67 RS |
1231 | if (prefix) |
1232 | insert_string (",\n\t is a prefix character for `backward-prefix-chars'"); | |
1233 | ||
62abe9cb SM |
1234 | return syntax; |
1235 | } | |
8489eb67 | 1236 | \f |
195d1361 RS |
1237 | int parse_sexp_ignore_comments; |
1238 | ||
8f924df7 KH |
1239 | /* Char-table of functions that find the next or previous word |
1240 | boundary. */ | |
1241 | Lisp_Object Vfind_word_boundary_function_table; | |
869bb237 | 1242 | |
8489eb67 RS |
1243 | /* Return the position across COUNT words from FROM. |
1244 | If that many words cannot be found before the end of the buffer, return 0. | |
1245 | COUNT negative means scan backward and stop at word beginning. */ | |
1246 | ||
4f3a2f8d EZ |
1247 | EMACS_INT |
1248 | scan_words (register EMACS_INT from, register EMACS_INT count) | |
8489eb67 | 1249 | { |
4f3a2f8d EZ |
1250 | register EMACS_INT beg = BEGV; |
1251 | register EMACS_INT end = ZV; | |
1252 | register EMACS_INT from_byte = CHAR_TO_BYTE (from); | |
93da5fff KH |
1253 | register enum syntaxcode code; |
1254 | int ch0, ch1; | |
8f924df7 | 1255 | Lisp_Object func, script, pos; |
8489eb67 RS |
1256 | |
1257 | immediate_quit = 1; | |
1258 | QUIT; | |
1259 | ||
195d1361 RS |
1260 | SETUP_SYNTAX_TABLE (from, count); |
1261 | ||
8489eb67 RS |
1262 | while (count > 0) |
1263 | { | |
1264 | while (1) | |
1265 | { | |
1266 | if (from == end) | |
1267 | { | |
1268 | immediate_quit = 0; | |
1269 | return 0; | |
1270 | } | |
195d1361 | 1271 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
b7dbcc19 | 1272 | ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
93da5fff | 1273 | code = SYNTAX (ch0); |
6a140a74 | 1274 | INC_BOTH (from, from_byte); |
8489eb67 RS |
1275 | if (words_include_escapes |
1276 | && (code == Sescape || code == Scharquote)) | |
1277 | break; | |
1278 | if (code == Sword) | |
1279 | break; | |
8489eb67 | 1280 | } |
93da5fff KH |
1281 | /* Now CH0 is a character which begins a word and FROM is the |
1282 | position of the next character. */ | |
8f924df7 | 1283 | func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0); |
869bb237 | 1284 | if (! NILP (Ffboundp (func))) |
8489eb67 | 1285 | { |
869bb237 | 1286 | pos = call2 (func, make_number (from - 1), make_number (end)); |
8f924df7 KH |
1287 | if (INTEGERP (pos) && XINT (pos) > from) |
1288 | { | |
1289 | from = XINT (pos); | |
1290 | from_byte = CHAR_TO_BYTE (from); | |
1291 | } | |
8489eb67 | 1292 | } |
869bb237 | 1293 | else |
8cb8232a | 1294 | { |
8cb8232a KH |
1295 | script = CHAR_TABLE_REF (Vchar_script_table, ch0); |
1296 | while (1) | |
1297 | { | |
1298 | if (from == end) break; | |
1299 | UPDATE_SYNTAX_TABLE_FORWARD (from); | |
b7dbcc19 | 1300 | ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
8cb8232a KH |
1301 | code = SYNTAX (ch1); |
1302 | if ((code != Sword | |
1303 | && (! words_include_escapes | |
1304 | || (code != Sescape && code != Scharquote))) | |
2b70a11c | 1305 | || word_boundary_p (ch0, ch1)) |
869bb237 | 1306 | break; |
8cb8232a KH |
1307 | INC_BOTH (from, from_byte); |
1308 | ch0 = ch1; | |
1309 | } | |
8489eb67 RS |
1310 | } |
1311 | count--; | |
1312 | } | |
1313 | while (count < 0) | |
1314 | { | |
1315 | while (1) | |
1316 | { | |
1317 | if (from == beg) | |
1318 | { | |
1319 | immediate_quit = 0; | |
1320 | return 0; | |
1321 | } | |
6a140a74 | 1322 | DEC_BOTH (from, from_byte); |
195d1361 | 1323 | UPDATE_SYNTAX_TABLE_BACKWARD (from); |
b7dbcc19 | 1324 | ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
93da5fff | 1325 | code = SYNTAX (ch1); |
8489eb67 RS |
1326 | if (words_include_escapes |
1327 | && (code == Sescape || code == Scharquote)) | |
1328 | break; | |
1329 | if (code == Sword) | |
1330 | break; | |
8489eb67 | 1331 | } |
93da5fff KH |
1332 | /* Now CH1 is a character which ends a word and FROM is the |
1333 | position of it. */ | |
8f924df7 | 1334 | func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1); |
869bb237 | 1335 | if (! NILP (Ffboundp (func))) |
8f924df7 | 1336 | { |
869bb237 | 1337 | pos = call2 (func, make_number (from), make_number (beg)); |
8f924df7 KH |
1338 | if (INTEGERP (pos) && XINT (pos) < from) |
1339 | { | |
1340 | from = XINT (pos); | |
1341 | from_byte = CHAR_TO_BYTE (from); | |
1342 | } | |
8489eb67 | 1343 | } |
869bb237 | 1344 | else |
8489eb67 | 1345 | { |
8cb8232a KH |
1346 | script = CHAR_TABLE_REF (Vchar_script_table, ch1); |
1347 | while (1) | |
1348 | { | |
8cb8232a KH |
1349 | if (from == beg) |
1350 | break; | |
262be72a | 1351 | DEC_BOTH (from, from_byte); |
8cb8232a | 1352 | UPDATE_SYNTAX_TABLE_BACKWARD (from); |
262be72a | 1353 | ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
8cb8232a KH |
1354 | code = SYNTAX (ch0); |
1355 | if ((code != Sword | |
1356 | && (! words_include_escapes | |
1357 | || (code != Sescape && code != Scharquote))) | |
2b70a11c | 1358 | || word_boundary_p (ch0, ch1)) |
262be72a MB |
1359 | { |
1360 | INC_BOTH (from, from_byte); | |
1361 | break; | |
1362 | } | |
8cb8232a KH |
1363 | ch1 = ch0; |
1364 | } | |
8489eb67 RS |
1365 | } |
1366 | count++; | |
1367 | } | |
1368 | ||
1369 | immediate_quit = 0; | |
1370 | ||
1371 | return from; | |
1372 | } | |
1373 | ||
1e9dbb5f | 1374 | DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p", |
fdb82f93 PJ |
1375 | doc: /* Move point forward ARG words (backward if ARG is negative). |
1376 | Normally returns t. | |
1377 | If an edge of the buffer or a field boundary is reached, point is left there | |
1378 | and the function returns nil. Field boundaries are not noticed if | |
1379 | `inhibit-field-text-motion' is non-nil. */) | |
5842a27b | 1380 | (Lisp_Object arg) |
8489eb67 | 1381 | { |
c096ae4d | 1382 | Lisp_Object tmp; |
2c6ea900 | 1383 | int orig_val, val; |
8489eb67 | 1384 | |
839966f3 KH |
1385 | if (NILP (arg)) |
1386 | XSETFASTINT (arg, 1); | |
1387 | else | |
1388 | CHECK_NUMBER (arg); | |
1389 | ||
1390 | val = orig_val = scan_words (PT, XINT (arg)); | |
2c6ea900 | 1391 | if (! orig_val) |
839966f3 | 1392 | val = XINT (arg) > 0 ? ZV : BEGV; |
5878ee6f | 1393 | |
b8855607 | 1394 | /* Avoid jumping out of an input field. */ |
c096ae4d SM |
1395 | tmp = Fconstrain_to_field (make_number (val), make_number (PT), |
1396 | Qt, Qnil, Qnil); | |
1397 | val = XFASTINT (tmp); | |
7d0393cf | 1398 | |
8489eb67 | 1399 | SET_PT (val); |
e6d8341f | 1400 | return val == orig_val ? Qt : Qnil; |
8489eb67 RS |
1401 | } |
1402 | \f | |
dd4c5104 | 1403 | Lisp_Object skip_chars (int, Lisp_Object, Lisp_Object, int); |
195d1361 RS |
1404 | |
1405 | DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0, | |
fdb82f93 PJ |
1406 | doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM. |
1407 | STRING is like the inside of a `[...]' in a regular expression | |
1408 | except that `]' is never special and `\\' quotes `^', `-' or `\\' | |
7087d5e9 | 1409 | (but not at the end of a range; quoting is never needed there). |
fdb82f93 PJ |
1410 | Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter. |
1411 | With arg "^a-zA-Z", skips nonletters stopping before first letter. | |
a1bc88d4 RS |
1412 | Char classes, e.g. `[:alpha:]', are supported. |
1413 | ||
1414 | Returns the distance traveled, either zero or positive. */) | |
5842a27b | 1415 | (Lisp_Object string, Lisp_Object lim) |
195d1361 | 1416 | { |
327719ee | 1417 | return skip_chars (1, string, lim, 1); |
195d1361 RS |
1418 | } |
1419 | ||
1420 | DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0, | |
fdb82f93 PJ |
1421 | doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM. |
1422 | See `skip-chars-forward' for details. | |
1423 | Returns the distance traveled, either zero or negative. */) | |
5842a27b | 1424 | (Lisp_Object string, Lisp_Object lim) |
195d1361 | 1425 | { |
327719ee | 1426 | return skip_chars (0, string, lim, 1); |
195d1361 RS |
1427 | } |
1428 | ||
1429 | DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0, | |
fdb82f93 PJ |
1430 | doc: /* Move point forward across chars in specified syntax classes. |
1431 | SYNTAX is a string of syntax code characters. | |
1432 | Stop before a char whose syntax is not in SYNTAX, or at position LIM. | |
1433 | If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX. | |
1434 | This function returns the distance traveled, either zero or positive. */) | |
5842a27b | 1435 | (Lisp_Object syntax, Lisp_Object lim) |
195d1361 | 1436 | { |
b7dbcc19 | 1437 | return skip_syntaxes (1, syntax, lim); |
195d1361 RS |
1438 | } |
1439 | ||
1440 | DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0, | |
fdb82f93 PJ |
1441 | doc: /* Move point backward across chars in specified syntax classes. |
1442 | SYNTAX is a string of syntax code characters. | |
1443 | Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM. | |
1444 | If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX. | |
1445 | This function returns the distance traveled, either zero or negative. */) | |
5842a27b | 1446 | (Lisp_Object syntax, Lisp_Object lim) |
195d1361 | 1447 | { |
b7dbcc19 | 1448 | return skip_syntaxes (0, syntax, lim); |
195d1361 RS |
1449 | } |
1450 | ||
6a140a74 | 1451 | static Lisp_Object |
971de7fb | 1452 | skip_chars (int forwardp, Lisp_Object string, Lisp_Object lim, int handle_iso_classes) |
195d1361 | 1453 | { |
195d1361 RS |
1454 | register unsigned int c; |
1455 | unsigned char fastmap[0400]; | |
b7dbcc19 | 1456 | /* Store the ranges of non-ASCII characters. */ |
5df4982e | 1457 | int *char_ranges; |
7e68b0ea | 1458 | int n_char_ranges = 0; |
195d1361 | 1459 | int negate = 0; |
4f3a2f8d | 1460 | register EMACS_INT i, i_byte; |
b7dbcc19 KH |
1461 | /* Set to 1 if the current buffer is multibyte and the region |
1462 | contains non-ASCII chars. */ | |
1463 | int multibyte; | |
1464 | /* Set to 1 if STRING is multibyte and it contains non-ASCII | |
1465 | chars. */ | |
1674d9a2 | 1466 | int string_multibyte; |
4f3a2f8d | 1467 | EMACS_INT size_byte; |
2e567bd3 | 1468 | const unsigned char *str; |
82d497fc | 1469 | int len; |
a1bc88d4 | 1470 | Lisp_Object iso_classes; |
195d1361 | 1471 | |
b7826503 | 1472 | CHECK_STRING (string); |
a1bc88d4 | 1473 | iso_classes = Qnil; |
82d497fc | 1474 | |
195d1361 RS |
1475 | if (NILP (lim)) |
1476 | XSETINT (lim, forwardp ? ZV : BEGV); | |
1477 | else | |
b7826503 | 1478 | CHECK_NUMBER_COERCE_MARKER (lim); |
195d1361 RS |
1479 | |
1480 | /* In any case, don't allow scan outside bounds of buffer. */ | |
195d1361 RS |
1481 | if (XINT (lim) > ZV) |
1482 | XSETFASTINT (lim, ZV); | |
1483 | if (XINT (lim) < BEGV) | |
1484 | XSETFASTINT (lim, BEGV); | |
1485 | ||
b7dbcc19 | 1486 | multibyte = (!NILP (current_buffer->enable_multibyte_characters) |
92eaa22e | 1487 | && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE)); |
8f924df7 | 1488 | string_multibyte = SBYTES (string) > SCHARS (string); |
b7dbcc19 | 1489 | |
72af86bd | 1490 | memset (fastmap, 0, sizeof fastmap); |
195d1361 | 1491 | |
8f924df7 KH |
1492 | str = SDATA (string); |
1493 | size_byte = SBYTES (string); | |
4101e6fe | 1494 | |
82d497fc | 1495 | i_byte = 0; |
13090112 | 1496 | if (i_byte < size_byte |
d5db4077 | 1497 | && SREF (string, 0) == '^') |
195d1361 | 1498 | { |
82d497fc | 1499 | negate = 1; i_byte++; |
195d1361 RS |
1500 | } |
1501 | ||
1502 | /* Find the characters specified and set their elements of fastmap. | |
b7dbcc19 | 1503 | Handle backslashes and ranges specially. |
195d1361 | 1504 | |
b7dbcc19 KH |
1505 | If STRING contains non-ASCII characters, setup char_ranges for |
1506 | them and use fastmap only for their leading codes. */ | |
4101e6fe | 1507 | |
b7dbcc19 | 1508 | if (! string_multibyte) |
195d1361 | 1509 | { |
b7dbcc19 | 1510 | int string_has_eight_bit = 0; |
4101e6fe | 1511 | |
b7dbcc19 KH |
1512 | /* At first setup fastmap. */ |
1513 | while (i_byte < size_byte) | |
1514 | { | |
1515 | c = str[i_byte++]; | |
1516 | ||
a1bc88d4 RS |
1517 | if (handle_iso_classes && c == '[' |
1518 | && i_byte < size_byte | |
327719ee | 1519 | && str[i_byte] == ':') |
a1bc88d4 RS |
1520 | { |
1521 | const unsigned char *class_beg = str + i_byte + 1; | |
1522 | const unsigned char *class_end = class_beg; | |
b3bda4fd | 1523 | const unsigned char *class_limit = str + size_byte - 2; |
db9cd97a | 1524 | /* Leave room for the null. */ |
a1bc88d4 RS |
1525 | unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1]; |
1526 | re_wctype_t cc; | |
1527 | ||
1528 | if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH) | |
1529 | class_limit = class_beg + CHAR_CLASS_MAX_LENGTH; | |
1530 | ||
b3bda4fd KS |
1531 | while (class_end < class_limit |
1532 | && *class_end >= 'a' && *class_end <= 'z') | |
a1bc88d4 RS |
1533 | class_end++; |
1534 | ||
b3bda4fd KS |
1535 | if (class_end == class_beg |
1536 | || *class_end != ':' || class_end[1] != ']') | |
1537 | goto not_a_class_name; | |
a1bc88d4 | 1538 | |
72af86bd | 1539 | memcpy (class_name, class_beg, class_end - class_beg); |
a1bc88d4 RS |
1540 | class_name[class_end - class_beg] = 0; |
1541 | ||
1542 | cc = re_wctype (class_name); | |
1543 | if (cc == 0) | |
1544 | error ("Invalid ISO C character class"); | |
1545 | ||
1546 | iso_classes = Fcons (make_number (cc), iso_classes); | |
1547 | ||
1548 | i_byte = class_end + 2 - str; | |
1549 | continue; | |
1550 | } | |
1551 | ||
b3bda4fd | 1552 | not_a_class_name: |
b7dbcc19 KH |
1553 | if (c == '\\') |
1554 | { | |
13090112 | 1555 | if (i_byte == size_byte) |
4101e6fe RS |
1556 | break; |
1557 | ||
5c7b02ab | 1558 | c = str[i_byte++]; |
195d1361 | 1559 | } |
839966f3 KH |
1560 | /* Treat `-' as range character only if another character |
1561 | follows. */ | |
1562 | if (i_byte + 1 < size_byte | |
82d497fc | 1563 | && str[i_byte] == '-') |
195d1361 | 1564 | { |
9690d026 | 1565 | unsigned int c2; |
4101e6fe RS |
1566 | |
1567 | /* Skip over the dash. */ | |
82d497fc | 1568 | i_byte++; |
4101e6fe | 1569 | |
4101e6fe | 1570 | /* Get the end of the range. */ |
5c7b02ab | 1571 | c2 = str[i_byte++]; |
b7dbcc19 KH |
1572 | if (c2 == '\\' |
1573 | && i_byte < size_byte) | |
1574 | c2 = str[i_byte++]; | |
1575 | ||
8f924df7 KH |
1576 | if (c <= c2) |
1577 | { | |
1578 | while (c <= c2) | |
1579 | fastmap[c++] = 1; | |
1580 | if (! ASCII_CHAR_P (c2)) | |
1581 | string_has_eight_bit = 1; | |
1582 | } | |
195d1361 RS |
1583 | } |
1584 | else | |
b7dbcc19 KH |
1585 | { |
1586 | fastmap[c] = 1; | |
1587 | if (! ASCII_CHAR_P (c)) | |
1588 | string_has_eight_bit = 1; | |
1589 | } | |
1590 | } | |
1591 | ||
1592 | /* If the current range is multibyte and STRING contains | |
1593 | eight-bit chars, arrange fastmap and setup char_ranges for | |
1594 | the corresponding multibyte chars. */ | |
1595 | if (multibyte && string_has_eight_bit) | |
1596 | { | |
1597 | unsigned char fastmap2[0400]; | |
1598 | int range_start_byte, range_start_char; | |
1599 | ||
72af86bd AS |
1600 | memcpy (fastmap + 0200, fastmap2 + 0200, 0200); |
1601 | memset (fastmap + 0200, 0, 0200); | |
b7dbcc19 KH |
1602 | /* We are sure that this loop stops. */ |
1603 | for (i = 0200; ! fastmap2[i]; i++); | |
4c0354d7 | 1604 | c = BYTE8_TO_CHAR (i); |
b7dbcc19 KH |
1605 | fastmap[CHAR_LEADING_CODE (c)] = 1; |
1606 | range_start_byte = i; | |
1607 | range_start_char = c; | |
f003ca54 | 1608 | char_ranges = (int *) alloca (sizeof (int) * 128 * 2); |
b7dbcc19 KH |
1609 | for (i = 129; i < 0400; i++) |
1610 | { | |
4c0354d7 | 1611 | c = BYTE8_TO_CHAR (i); |
b7dbcc19 KH |
1612 | fastmap[CHAR_LEADING_CODE (c)] = 1; |
1613 | if (i - range_start_byte != c - range_start_char) | |
1614 | { | |
1615 | char_ranges[n_char_ranges++] = range_start_char; | |
1616 | char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte) | |
1617 | + range_start_char); | |
1618 | range_start_byte = i; | |
1619 | range_start_char = c; | |
8f924df7 | 1620 | } |
b7dbcc19 KH |
1621 | } |
1622 | char_ranges[n_char_ranges++] = range_start_char; | |
1623 | char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte) | |
1624 | + range_start_char); | |
195d1361 RS |
1625 | } |
1626 | } | |
f003ca54 | 1627 | else /* STRING is multibyte */ |
b7dbcc19 | 1628 | { |
f003ca54 KH |
1629 | char_ranges = (int *) alloca (sizeof (int) * SCHARS (string) * 2); |
1630 | ||
b7dbcc19 | 1631 | while (i_byte < size_byte) |
195d1361 | 1632 | { |
b7dbcc19 | 1633 | unsigned char leading_code; |
5c7b02ab | 1634 | |
b7dbcc19 | 1635 | leading_code = str[i_byte]; |
62a6e103 | 1636 | c = STRING_CHAR_AND_LENGTH (str + i_byte, len); |
b7dbcc19 | 1637 | i_byte += len; |
5c7b02ab | 1638 | |
327719ee MB |
1639 | if (handle_iso_classes && c == '[' |
1640 | && i_byte < size_byte | |
62a6e103 | 1641 | && STRING_CHAR (str + i_byte) == ':') |
327719ee MB |
1642 | { |
1643 | const unsigned char *class_beg = str + i_byte + 1; | |
1644 | const unsigned char *class_end = class_beg; | |
1645 | const unsigned char *class_limit = str + size_byte - 2; | |
1646 | /* Leave room for the null. */ | |
1647 | unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1]; | |
1648 | re_wctype_t cc; | |
1649 | ||
1650 | if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH) | |
1651 | class_limit = class_beg + CHAR_CLASS_MAX_LENGTH; | |
1652 | ||
1653 | while (class_end < class_limit | |
1654 | && *class_end >= 'a' && *class_end <= 'z') | |
1655 | class_end++; | |
1656 | ||
1657 | if (class_end == class_beg | |
1658 | || *class_end != ':' || class_end[1] != ']') | |
1659 | goto not_a_class_name_multibyte; | |
1660 | ||
72af86bd | 1661 | memcpy (class_name, class_beg, class_end - class_beg); |
327719ee MB |
1662 | class_name[class_end - class_beg] = 0; |
1663 | ||
1664 | cc = re_wctype (class_name); | |
1665 | if (cc == 0) | |
1666 | error ("Invalid ISO C character class"); | |
1667 | ||
1668 | iso_classes = Fcons (make_number (cc), iso_classes); | |
1669 | ||
1670 | i_byte = class_end + 2 - str; | |
1671 | continue; | |
1672 | } | |
1673 | ||
1674 | not_a_class_name_multibyte: | |
195d1361 RS |
1675 | if (c == '\\') |
1676 | { | |
13090112 | 1677 | if (i_byte == size_byte) |
4101e6fe RS |
1678 | break; |
1679 | ||
b7dbcc19 | 1680 | leading_code = str[i_byte]; |
62a6e103 | 1681 | c = STRING_CHAR_AND_LENGTH (str + i_byte, len); |
82d497fc | 1682 | i_byte += len; |
195d1361 | 1683 | } |
839966f3 KH |
1684 | /* Treat `-' as range character only if another character |
1685 | follows. */ | |
1686 | if (i_byte + 1 < size_byte | |
82d497fc | 1687 | && str[i_byte] == '-') |
195d1361 | 1688 | { |
9690d026 | 1689 | unsigned int c2; |
b7dbcc19 | 1690 | unsigned char leading_code2; |
4101e6fe RS |
1691 | |
1692 | /* Skip over the dash. */ | |
82d497fc | 1693 | i_byte++; |
4101e6fe | 1694 | |
4101e6fe | 1695 | /* Get the end of the range. */ |
b7dbcc19 | 1696 | leading_code2 = str[i_byte]; |
62a6e103 | 1697 | c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len); |
82d497fc | 1698 | i_byte += len; |
7e68b0ea | 1699 | |
b7dbcc19 KH |
1700 | if (c2 == '\\' |
1701 | && i_byte < size_byte) | |
1702 | { | |
1703 | leading_code2 = str[i_byte]; | |
62a6e103 | 1704 | c2 =STRING_CHAR_AND_LENGTH (str + i_byte, len); |
b7dbcc19 KH |
1705 | i_byte += len; |
1706 | } | |
1707 | ||
f003ca54 KH |
1708 | if (c > c2) |
1709 | continue; | |
b7dbcc19 | 1710 | if (ASCII_CHAR_P (c)) |
06274af5 | 1711 | { |
5c7b02ab KH |
1712 | while (c <= c2 && c < 0x80) |
1713 | fastmap[c++] = 1; | |
b7dbcc19 KH |
1714 | leading_code = CHAR_LEADING_CODE (c); |
1715 | } | |
1716 | if (! ASCII_CHAR_P (c)) | |
1717 | { | |
1718 | while (leading_code <= leading_code2) | |
1719 | fastmap[leading_code++] = 1; | |
1720 | if (c <= c2) | |
e39091b6 | 1721 | { |
b7dbcc19 | 1722 | char_ranges[n_char_ranges++] = c; |
e39091b6 | 1723 | char_ranges[n_char_ranges++] = c2; |
06274af5 KH |
1724 | } |
1725 | } | |
195d1361 RS |
1726 | } |
1727 | else | |
7e68b0ea | 1728 | { |
b7dbcc19 | 1729 | if (ASCII_CHAR_P (c)) |
e39091b6 KH |
1730 | fastmap[c] = 1; |
1731 | else | |
7e68b0ea | 1732 | { |
b7dbcc19 | 1733 | fastmap[leading_code] = 1; |
4101e6fe RS |
1734 | char_ranges[n_char_ranges++] = c; |
1735 | char_ranges[n_char_ranges++] = c; | |
7e68b0ea RS |
1736 | } |
1737 | } | |
195d1361 | 1738 | } |
b7dbcc19 KH |
1739 | |
1740 | /* If the current range is unibyte and STRING contains non-ASCII | |
1741 | chars, arrange fastmap for the corresponding unibyte | |
1742 | chars. */ | |
1743 | ||
1744 | if (! multibyte && n_char_ranges > 0) | |
1745 | { | |
72af86bd | 1746 | memset (fastmap + 0200, 0, 0200); |
b7dbcc19 KH |
1747 | for (i = 0; i < n_char_ranges; i += 2) |
1748 | { | |
1749 | int c1 = char_ranges[i]; | |
1750 | int c2 = char_ranges[i + 1]; | |
1751 | ||
1752 | for (; c1 <= c2; c1++) | |
2afc21f5 SM |
1753 | { |
1754 | int b = CHAR_TO_BYTE_SAFE (c1); | |
1755 | if (b >= 0) | |
1756 | fastmap[b] = 1; | |
1757 | } | |
b7dbcc19 KH |
1758 | } |
1759 | } | |
195d1361 RS |
1760 | } |
1761 | ||
9690d026 | 1762 | /* If ^ was the first character, complement the fastmap. */ |
195d1361 | 1763 | if (negate) |
b7dbcc19 KH |
1764 | { |
1765 | if (! multibyte) | |
1766 | for (i = 0; i < sizeof fastmap; i++) | |
1767 | fastmap[i] ^= 1; | |
1768 | else | |
1769 | { | |
1770 | for (i = 0; i < 0200; i++) | |
1771 | fastmap[i] ^= 1; | |
1772 | /* All non-ASCII chars possibly match. */ | |
1773 | for (; i < sizeof fastmap; i++) | |
1774 | fastmap[i] = 1; | |
1775 | } | |
1776 | } | |
195d1361 RS |
1777 | |
1778 | { | |
4f3a2f8d EZ |
1779 | EMACS_INT start_point = PT; |
1780 | EMACS_INT pos = PT; | |
1781 | EMACS_INT pos_byte = PT_BYTE; | |
9af7511a KH |
1782 | unsigned char *p = PT_ADDR, *endp, *stop; |
1783 | ||
1784 | if (forwardp) | |
1785 | { | |
1786 | endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim)); | |
d7ee9fab | 1787 | stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp; |
9af7511a KH |
1788 | } |
1789 | else | |
1790 | { | |
1791 | endp = CHAR_POS_ADDR (XINT (lim)); | |
d7ee9fab | 1792 | stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp; |
9af7511a | 1793 | } |
195d1361 RS |
1794 | |
1795 | immediate_quit = 1; | |
618db430 SM |
1796 | /* This code may look up syntax tables using macros that rely on the |
1797 | gl_state object. To make sure this object is not out of date, | |
1798 | let's initialize it manually. | |
1799 | We ignore syntax-table text-properties for now, since that's | |
1800 | what we've done in the past. */ | |
d48cd3f4 | 1801 | SETUP_BUFFER_SYNTAX_TABLE (); |
b7dbcc19 | 1802 | if (forwardp) |
195d1361 | 1803 | { |
b7dbcc19 | 1804 | if (multibyte) |
8f924df7 | 1805 | while (1) |
b7dbcc19 | 1806 | { |
8f924df7 | 1807 | int nbytes; |
9af7511a | 1808 | |
8f924df7 | 1809 | if (p >= stop) |
9af7511a | 1810 | { |
8f924df7 | 1811 | if (p >= endp) |
9af7511a | 1812 | break; |
8f924df7 KH |
1813 | p = GAP_END_ADDR; |
1814 | stop = endp; | |
9af7511a | 1815 | } |
62a6e103 | 1816 | c = STRING_CHAR_AND_LENGTH (p, nbytes); |
327719ee | 1817 | if (! NILP (iso_classes) && in_classes (c, iso_classes)) |
9af7511a | 1818 | { |
327719ee | 1819 | if (negate) |
9af7511a | 1820 | break; |
327719ee MB |
1821 | else |
1822 | goto fwd_ok; | |
9af7511a | 1823 | } |
9af7511a | 1824 | |
8f924df7 | 1825 | if (! fastmap[*p]) |
b7dbcc19 KH |
1826 | break; |
1827 | if (! ASCII_CHAR_P (c)) | |
9af7511a | 1828 | { |
b7dbcc19 KH |
1829 | /* As we are looking at a multibyte character, we |
1830 | must look up the character in the table | |
1831 | CHAR_RANGES. If there's no data in the table, | |
1832 | that character is not what we want to skip. */ | |
1833 | ||
1834 | /* The following code do the right thing even if | |
1835 | n_char_ranges is zero (i.e. no data in | |
1836 | CHAR_RANGES). */ | |
1837 | for (i = 0; i < n_char_ranges; i += 2) | |
1838 | if (c >= char_ranges[i] && c <= char_ranges[i + 1]) | |
9af7511a | 1839 | break; |
b7dbcc19 KH |
1840 | if (!(negate ^ (i < n_char_ranges))) |
1841 | break; | |
9af7511a | 1842 | } |
327719ee | 1843 | fwd_ok: |
8f924df7 | 1844 | p += nbytes, pos++, pos_byte += nbytes; |
b7dbcc19 KH |
1845 | } |
1846 | else | |
8f924df7 KH |
1847 | while (1) |
1848 | { | |
1849 | if (p >= stop) | |
9af7511a | 1850 | { |
8f924df7 | 1851 | if (p >= endp) |
9af7511a | 1852 | break; |
8f924df7 KH |
1853 | p = GAP_END_ADDR; |
1854 | stop = endp; | |
9af7511a | 1855 | } |
327719ee MB |
1856 | |
1857 | if (!NILP (iso_classes) && in_classes (*p, iso_classes)) | |
1858 | { | |
1859 | if (negate) | |
1860 | break; | |
1861 | else | |
1862 | goto fwd_unibyte_ok; | |
1863 | } | |
1864 | ||
8f924df7 KH |
1865 | if (!fastmap[*p]) |
1866 | break; | |
327719ee | 1867 | fwd_unibyte_ok: |
8f924df7 KH |
1868 | p++, pos++, pos_byte++; |
1869 | } | |
195d1361 RS |
1870 | } |
1871 | else | |
1872 | { | |
b7dbcc19 | 1873 | if (multibyte) |
8f924df7 | 1874 | while (1) |
b7dbcc19 | 1875 | { |
8f924df7 | 1876 | unsigned char *prev_p; |
9af7511a | 1877 | |
8f924df7 | 1878 | if (p <= stop) |
9af7511a | 1879 | { |
8f924df7 | 1880 | if (p <= endp) |
9af7511a | 1881 | break; |
8f924df7 KH |
1882 | p = GPT_ADDR; |
1883 | stop = endp; | |
9af7511a | 1884 | } |
8f924df7 KH |
1885 | prev_p = p; |
1886 | while (--p >= stop && ! CHAR_HEAD_P (*p)); | |
62a6e103 | 1887 | c = STRING_CHAR (p); |
a1bc88d4 | 1888 | |
327719ee MB |
1889 | if (! NILP (iso_classes) && in_classes (c, iso_classes)) |
1890 | { | |
1891 | if (negate) | |
1892 | break; | |
9690d026 | 1893 | else |
327719ee | 1894 | goto back_ok; |
7e68b0ea | 1895 | } |
a1bc88d4 | 1896 | |
8f924df7 | 1897 | if (! fastmap[*p]) |
b7dbcc19 KH |
1898 | break; |
1899 | if (! ASCII_CHAR_P (c)) | |
7e68b0ea | 1900 | { |
b7dbcc19 KH |
1901 | /* See the comment in the previous similar code. */ |
1902 | for (i = 0; i < n_char_ranges; i += 2) | |
1903 | if (c >= char_ranges[i] && c <= char_ranges[i + 1]) | |
1904 | break; | |
1905 | if (!(negate ^ (i < n_char_ranges))) | |
1906 | break; | |
7e68b0ea | 1907 | } |
327719ee | 1908 | back_ok: |
8f924df7 | 1909 | pos--, pos_byte -= prev_p - p; |
b7dbcc19 KH |
1910 | } |
1911 | else | |
8f924df7 KH |
1912 | while (1) |
1913 | { | |
1914 | if (p <= stop) | |
9af7511a | 1915 | { |
8f924df7 | 1916 | if (p <= endp) |
9af7511a | 1917 | break; |
8f924df7 KH |
1918 | p = GPT_ADDR; |
1919 | stop = endp; | |
9af7511a | 1920 | } |
e39091b6 | 1921 | |
327719ee MB |
1922 | if (! NILP (iso_classes) && in_classes (p[-1], iso_classes)) |
1923 | { | |
1924 | if (negate) | |
1925 | break; | |
9af7511a | 1926 | else |
327719ee MB |
1927 | goto back_unibyte_ok; |
1928 | } | |
a1bc88d4 | 1929 | |
8f924df7 KH |
1930 | if (!fastmap[p[-1]]) |
1931 | break; | |
327719ee | 1932 | back_unibyte_ok: |
8f924df7 KH |
1933 | p--, pos--, pos_byte--; |
1934 | } | |
195d1361 | 1935 | } |
7e68b0ea | 1936 | |
b7dbcc19 KH |
1937 | SET_PT_BOTH (pos, pos_byte); |
1938 | immediate_quit = 0; | |
1939 | ||
1940 | return make_number (PT - start_point); | |
1941 | } | |
1942 | } | |
7e68b0ea | 1943 | |
b7dbcc19 KH |
1944 | |
1945 | static Lisp_Object | |
971de7fb | 1946 | skip_syntaxes (int forwardp, Lisp_Object string, Lisp_Object lim) |
b7dbcc19 KH |
1947 | { |
1948 | register unsigned int c; | |
1949 | unsigned char fastmap[0400]; | |
1950 | int negate = 0; | |
4f3a2f8d | 1951 | register EMACS_INT i, i_byte; |
b7dbcc19 | 1952 | int multibyte; |
4f3a2f8d | 1953 | EMACS_INT size_byte; |
b7dbcc19 KH |
1954 | unsigned char *str; |
1955 | ||
1956 | CHECK_STRING (string); | |
1957 | ||
1958 | if (NILP (lim)) | |
1959 | XSETINT (lim, forwardp ? ZV : BEGV); | |
1960 | else | |
1961 | CHECK_NUMBER_COERCE_MARKER (lim); | |
1962 | ||
1963 | /* In any case, don't allow scan outside bounds of buffer. */ | |
1964 | if (XINT (lim) > ZV) | |
1965 | XSETFASTINT (lim, ZV); | |
1966 | if (XINT (lim) < BEGV) | |
1967 | XSETFASTINT (lim, BEGV); | |
1968 | ||
ade8ee9e | 1969 | if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim))) |
49feb1cd | 1970 | return make_number (0); |
8c6e735b | 1971 | |
b7dbcc19 | 1972 | multibyte = (!NILP (current_buffer->enable_multibyte_characters) |
92eaa22e | 1973 | && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE)); |
b7dbcc19 | 1974 | |
72af86bd | 1975 | memset (fastmap, 0, sizeof fastmap); |
b7dbcc19 | 1976 | |
8f924df7 KH |
1977 | if (SBYTES (string) > SCHARS (string)) |
1978 | /* As this is very rare case (syntax spec is ASCII only), don't | |
1979 | consider efficiency. */ | |
bc796a59 KH |
1980 | string = string_make_unibyte (string); |
1981 | ||
8f924df7 KH |
1982 | str = SDATA (string); |
1983 | size_byte = SBYTES (string); | |
bc796a59 | 1984 | |
b7dbcc19 KH |
1985 | i_byte = 0; |
1986 | if (i_byte < size_byte | |
8f924df7 | 1987 | && SREF (string, 0) == '^') |
b7dbcc19 KH |
1988 | { |
1989 | negate = 1; i_byte++; | |
1990 | } | |
1991 | ||
b7dbcc19 KH |
1992 | /* Find the syntaxes specified and set their elements of fastmap. */ |
1993 | ||
1994 | while (i_byte < size_byte) | |
1995 | { | |
1996 | c = str[i_byte++]; | |
8f924df7 | 1997 | fastmap[syntax_spec_code[c]] = 1; |
b7dbcc19 | 1998 | } |
195d1361 | 1999 | |
9690d026 | 2000 | /* If ^ was the first character, complement the fastmap. */ |
195d1361 RS |
2001 | if (negate) |
2002 | for (i = 0; i < sizeof fastmap; i++) | |
9690d026 | 2003 | fastmap[i] ^= 1; |
195d1361 RS |
2004 | |
2005 | { | |
4f3a2f8d EZ |
2006 | EMACS_INT start_point = PT; |
2007 | EMACS_INT pos = PT; | |
2008 | EMACS_INT pos_byte = PT_BYTE; | |
8f924df7 KH |
2009 | unsigned char *p = PT_ADDR, *endp, *stop; |
2010 | ||
2011 | if (forwardp) | |
2012 | { | |
2013 | endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim)); | |
2014 | stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp; | |
2015 | } | |
2016 | else | |
2017 | { | |
2018 | endp = CHAR_POS_ADDR (XINT (lim)); | |
2019 | stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp; | |
2020 | } | |
195d1361 RS |
2021 | |
2022 | immediate_quit = 1; | |
b7dbcc19 KH |
2023 | SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1); |
2024 | if (forwardp) | |
195d1361 | 2025 | { |
b7dbcc19 | 2026 | if (multibyte) |
195d1361 | 2027 | { |
8f924df7 | 2028 | while (1) |
8c6e735b | 2029 | { |
8f924df7 KH |
2030 | int nbytes; |
2031 | ||
2032 | if (p >= stop) | |
2033 | { | |
2034 | if (p >= endp) | |
2035 | break; | |
2036 | p = GAP_END_ADDR; | |
2037 | stop = endp; | |
2038 | } | |
62a6e103 | 2039 | c = STRING_CHAR_AND_LENGTH (p, nbytes); |
8f924df7 | 2040 | if (! fastmap[(int) SYNTAX (c)]) |
8c6e735b | 2041 | break; |
8f924df7 | 2042 | p += nbytes, pos++, pos_byte += nbytes; |
8c6e735b KH |
2043 | UPDATE_SYNTAX_TABLE_FORWARD (pos); |
2044 | } | |
195d1361 RS |
2045 | } |
2046 | else | |
2047 | { | |
8c6e735b | 2048 | while (1) |
7e68b0ea | 2049 | { |
8f924df7 KH |
2050 | if (p >= stop) |
2051 | { | |
2052 | if (p >= endp) | |
2053 | break; | |
2054 | p = GAP_END_ADDR; | |
2055 | stop = endp; | |
2056 | } | |
2057 | if (! fastmap[(int) SYNTAX (*p)]) | |
8c6e735b | 2058 | break; |
8f924df7 | 2059 | p++, pos++, pos_byte++; |
b7dbcc19 | 2060 | UPDATE_SYNTAX_TABLE_FORWARD (pos); |
195d1361 RS |
2061 | } |
2062 | } | |
2063 | } | |
2064 | else | |
2065 | { | |
b7dbcc19 | 2066 | if (multibyte) |
195d1361 | 2067 | { |
8c6e735b | 2068 | while (1) |
b7dbcc19 | 2069 | { |
8f924df7 KH |
2070 | unsigned char *prev_p; |
2071 | ||
2072 | if (p <= stop) | |
b7dbcc19 | 2073 | { |
8f924df7 KH |
2074 | if (p <= endp) |
2075 | break; | |
2076 | p = GPT_ADDR; | |
2077 | stop = endp; | |
b7dbcc19 | 2078 | } |
262be72a | 2079 | UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1); |
8f924df7 KH |
2080 | prev_p = p; |
2081 | while (--p >= stop && ! CHAR_HEAD_P (*p)); | |
62a6e103 | 2082 | c = STRING_CHAR (p); |
8f924df7 KH |
2083 | if (! fastmap[(int) SYNTAX (c)]) |
2084 | break; | |
2085 | pos--, pos_byte -= prev_p - p; | |
b7dbcc19 | 2086 | } |
195d1361 RS |
2087 | } |
2088 | else | |
2089 | { | |
8c6e735b KH |
2090 | while (1) |
2091 | { | |
8f924df7 KH |
2092 | if (p <= stop) |
2093 | { | |
2094 | if (p <= endp) | |
2095 | break; | |
2096 | p = GPT_ADDR; | |
2097 | stop = endp; | |
2098 | } | |
102f6132 | 2099 | UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1); |
8f924df7 | 2100 | if (! fastmap[(int) SYNTAX (p[-1])]) |
8c6e735b | 2101 | break; |
8f924df7 | 2102 | p--, pos--, pos_byte--; |
8c6e735b | 2103 | } |
195d1361 RS |
2104 | } |
2105 | } | |
6a140a74 RS |
2106 | |
2107 | SET_PT_BOTH (pos, pos_byte); | |
195d1361 RS |
2108 | immediate_quit = 0; |
2109 | ||
2110 | return make_number (PT - start_point); | |
2111 | } | |
2112 | } | |
a1bc88d4 RS |
2113 | |
2114 | /* Return 1 if character C belongs to one of the ISO classes | |
2115 | in the list ISO_CLASSES. Each class is represented by an | |
2116 | integer which is its type according to re_wctype. */ | |
2117 | ||
2118 | static int | |
971de7fb | 2119 | in_classes (int c, Lisp_Object iso_classes) |
a1bc88d4 RS |
2120 | { |
2121 | int fits_class = 0; | |
2122 | ||
618db430 | 2123 | while (CONSP (iso_classes)) |
a1bc88d4 RS |
2124 | { |
2125 | Lisp_Object elt; | |
2126 | elt = XCAR (iso_classes); | |
2127 | iso_classes = XCDR (iso_classes); | |
2128 | ||
2129 | if (re_iswctype (c, XFASTINT (elt))) | |
2130 | fits_class = 1; | |
2131 | } | |
2132 | ||
2133 | return fits_class; | |
2134 | } | |
195d1361 | 2135 | \f |
95ff8dfc RS |
2136 | /* Jump over a comment, assuming we are at the beginning of one. |
2137 | FROM is the current position. | |
2138 | FROM_BYTE is the bytepos corresponding to FROM. | |
2139 | Do not move past STOP (a charpos). | |
2140 | The comment over which we have to jump is of style STYLE | |
c5683ceb | 2141 | (either SYNTAX_FLAGS_COMMENT_STYLE(foo) or ST_COMMENT_STYLE). |
95ff8dfc RS |
2142 | NESTING should be positive to indicate the nesting at the beginning |
2143 | for nested comments and should be zero or negative else. | |
2144 | ST_COMMENT_STYLE cannot be nested. | |
2145 | PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character | |
2146 | (or 0 If the search cannot start in the middle of a two-character). | |
2147 | ||
2148 | If successful, return 1 and store the charpos of the comment's end | |
2149 | into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR. | |
2150 | Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the | |
2151 | corresponding bytepos into *BYTEPOS_PTR and the current nesting | |
2152 | (as defined for state.incomment) in *INCOMMENT_PTR. | |
2153 | ||
2154 | The comment end is the last character of the comment rather than the | |
2155 | character just after the comment. | |
2156 | ||
2157 | Global syntax data is assumed to initially be valid for FROM and | |
2158 | remains valid for forward search starting at the returned position. */ | |
2159 | ||
2160 | static int | |
dd4c5104 DN |
2161 | forw_comment (EMACS_INT from, EMACS_INT from_byte, EMACS_INT stop, |
2162 | int nesting, int style, int prev_syntax, | |
2163 | EMACS_INT *charpos_ptr, EMACS_INT *bytepos_ptr, | |
2164 | int *incomment_ptr) | |
95ff8dfc RS |
2165 | { |
2166 | register int c, c1; | |
2167 | register enum syntaxcode code; | |
c5683ceb | 2168 | register int syntax, other_syntax; |
95ff8dfc RS |
2169 | |
2170 | if (nesting <= 0) nesting = -1; | |
2171 | ||
2172 | /* Enter the loop in the middle so that we find | |
2173 | a 2-char comment ender if we start in the middle of it. */ | |
2174 | syntax = prev_syntax; | |
2175 | if (syntax != 0) goto forw_incomment; | |
2176 | ||
2177 | while (1) | |
2178 | { | |
2179 | if (from == stop) | |
2180 | { | |
2181 | *incomment_ptr = nesting; | |
2182 | *charpos_ptr = from; | |
2183 | *bytepos_ptr = from_byte; | |
2184 | return 0; | |
2185 | } | |
8f924df7 | 2186 | c = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
95ff8dfc RS |
2187 | syntax = SYNTAX_WITH_FLAGS (c); |
2188 | code = syntax & 0xff; | |
2189 | if (code == Sendcomment | |
c5683ceb | 2190 | && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style |
abf8a9ff SM |
2191 | && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ? |
2192 | (nesting > 0 && --nesting == 0) : nesting < 0)) | |
95ff8dfc RS |
2193 | /* we have encountered a comment end of the same style |
2194 | as the comment sequence which began this comment | |
2195 | section */ | |
2196 | break; | |
2197 | if (code == Scomment_fence | |
2198 | && style == ST_COMMENT_STYLE) | |
2199 | /* we have encountered a comment end of the same style | |
2200 | as the comment sequence which began this comment | |
2201 | section. */ | |
2202 | break; | |
2203 | if (nesting > 0 | |
2204 | && code == Scomment | |
abf8a9ff | 2205 | && SYNTAX_FLAGS_COMMENT_NESTED (syntax) |
c5683ceb | 2206 | && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style) |
95ff8dfc RS |
2207 | /* we have encountered a nested comment of the same style |
2208 | as the comment sequence which began this comment section */ | |
2209 | nesting++; | |
2210 | INC_BOTH (from, from_byte); | |
2211 | UPDATE_SYNTAX_TABLE_FORWARD (from); | |
7d0393cf | 2212 | |
95ff8dfc RS |
2213 | forw_incomment: |
2214 | if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax) | |
8f924df7 | 2215 | && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte), |
c5683ceb SM |
2216 | other_syntax = SYNTAX_WITH_FLAGS (c1), |
2217 | SYNTAX_FLAGS_COMEND_SECOND (other_syntax)) | |
2218 | && SYNTAX_FLAGS_COMMENT_STYLE (syntax, other_syntax) == style | |
abf8a9ff | 2219 | && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) || |
c5683ceb SM |
2220 | SYNTAX_FLAGS_COMMENT_NESTED (other_syntax)) |
2221 | ? nesting > 0 : nesting < 0)) | |
4ffe723b GM |
2222 | { |
2223 | if (--nesting <= 0) | |
2224 | /* we have encountered a comment end of the same style | |
2225 | as the comment sequence which began this comment | |
2226 | section */ | |
2227 | break; | |
2228 | else | |
2229 | { | |
2230 | INC_BOTH (from, from_byte); | |
2231 | UPDATE_SYNTAX_TABLE_FORWARD (from); | |
2232 | } | |
2233 | } | |
95ff8dfc RS |
2234 | if (nesting > 0 |
2235 | && from < stop | |
2236 | && SYNTAX_FLAGS_COMSTART_FIRST (syntax) | |
8f924df7 | 2237 | && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte), |
c5683ceb SM |
2238 | other_syntax = SYNTAX_WITH_FLAGS (c1), |
2239 | SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax) == style | |
2240 | && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)) | |
abf8a9ff | 2241 | && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) || |
c5683ceb | 2242 | SYNTAX_FLAGS_COMMENT_NESTED (other_syntax))) |
95ff8dfc RS |
2243 | /* we have encountered a nested comment of the same style |
2244 | as the comment sequence which began this comment | |
2245 | section */ | |
2246 | { | |
2247 | INC_BOTH (from, from_byte); | |
2248 | UPDATE_SYNTAX_TABLE_FORWARD (from); | |
2249 | nesting++; | |
2250 | } | |
2251 | } | |
2252 | *charpos_ptr = from; | |
2253 | *bytepos_ptr = from_byte; | |
2254 | return 1; | |
2255 | } | |
2256 | ||
b3cfe0c8 | 2257 | DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0, |
177c0ea7 | 2258 | doc: /* |
ee648c11 | 2259 | Move forward across up to COUNT comments. If COUNT is negative, move backward. |
fdb82f93 PJ |
2260 | Stop scanning if we find something other than a comment or whitespace. |
2261 | Set point to where scanning stops. | |
ee648c11 | 2262 | If COUNT comments are found as expected, with nothing except whitespace |
fdb82f93 | 2263 | between them, return t; otherwise return nil. */) |
5842a27b | 2264 | (Lisp_Object count) |
b3cfe0c8 | 2265 | { |
2312c580 SM |
2266 | register EMACS_INT from; |
2267 | EMACS_INT from_byte; | |
2268 | register EMACS_INT stop; | |
8ea151b2 | 2269 | register int c, c1; |
b3cfe0c8 RS |
2270 | register enum syntaxcode code; |
2271 | int comstyle = 0; /* style of comment encountered */ | |
95ff8dfc | 2272 | int comnested = 0; /* whether the comment is nestable or not */ |
be720845 | 2273 | int found; |
2312c580 SM |
2274 | EMACS_INT count1; |
2275 | EMACS_INT out_charpos, out_bytepos; | |
95ff8dfc | 2276 | int dummy; |
840f481c | 2277 | |
b7826503 | 2278 | CHECK_NUMBER (count); |
840f481c | 2279 | count1 = XINT (count); |
195d1361 | 2280 | stop = count1 > 0 ? ZV : BEGV; |
b3cfe0c8 RS |
2281 | |
2282 | immediate_quit = 1; | |
2283 | QUIT; | |
2284 | ||
2285 | from = PT; | |
6a140a74 | 2286 | from_byte = PT_BYTE; |
b3cfe0c8 | 2287 | |
195d1361 | 2288 | SETUP_SYNTAX_TABLE (from, count1); |
840f481c | 2289 | while (count1 > 0) |
b3cfe0c8 | 2290 | { |
04882296 | 2291 | do |
b3cfe0c8 | 2292 | { |
c5683ceb | 2293 | int comstart_first, syntax, other_syntax; |
f902a008 | 2294 | |
04882296 KH |
2295 | if (from == stop) |
2296 | { | |
ef316cf0 | 2297 | SET_PT_BOTH (from, from_byte); |
b7e6e612 | 2298 | immediate_quit = 0; |
04882296 KH |
2299 | return Qnil; |
2300 | } | |
b7dbcc19 | 2301 | c = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
c5683ceb | 2302 | syntax = SYNTAX_WITH_FLAGS (c); |
b3cfe0c8 | 2303 | code = SYNTAX (c); |
c5683ceb SM |
2304 | comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax); |
2305 | comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax); | |
2306 | comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0); | |
6a140a74 | 2307 | INC_BOTH (from, from_byte); |
f902a008 | 2308 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
f902a008 | 2309 | if (from < stop && comstart_first |
b7dbcc19 | 2310 | && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte), |
c5683ceb SM |
2311 | other_syntax = SYNTAX_WITH_FLAGS (c1), |
2312 | SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))) | |
b3cfe0c8 | 2313 | { |
7d0393cf | 2314 | /* We have encountered a comment start sequence and we |
7fc8191e | 2315 | are ignoring all text inside comments. We must record |
b3cfe0c8 RS |
2316 | the comment style this sequence begins so that later, |
2317 | only a comment end of the same style actually ends | |
7fc8191e | 2318 | the comment section. */ |
b3cfe0c8 | 2319 | code = Scomment; |
c5683ceb SM |
2320 | comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax); |
2321 | comnested | |
2322 | = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax); | |
6a140a74 | 2323 | INC_BOTH (from, from_byte); |
95ff8dfc | 2324 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
b3cfe0c8 | 2325 | } |
04882296 | 2326 | } |
abf8a9ff | 2327 | while (code == Swhitespace || (code == Sendcomment && c == '\n')); |
6a140a74 | 2328 | |
da71ab91 KH |
2329 | if (code == Scomment_fence) |
2330 | comstyle = ST_COMMENT_STYLE; | |
2331 | else if (code != Scomment) | |
04882296 KH |
2332 | { |
2333 | immediate_quit = 0; | |
6a140a74 | 2334 | DEC_BOTH (from, from_byte); |
ef316cf0 | 2335 | SET_PT_BOTH (from, from_byte); |
04882296 KH |
2336 | return Qnil; |
2337 | } | |
2338 | /* We're at the start of a comment. */ | |
95ff8dfc RS |
2339 | found = forw_comment (from, from_byte, stop, comnested, comstyle, 0, |
2340 | &out_charpos, &out_bytepos, &dummy); | |
2341 | from = out_charpos; from_byte = out_bytepos; | |
2342 | if (!found) | |
04882296 | 2343 | { |
95ff8dfc RS |
2344 | immediate_quit = 0; |
2345 | SET_PT_BOTH (from, from_byte); | |
2346 | return Qnil; | |
b3cfe0c8 | 2347 | } |
95ff8dfc RS |
2348 | INC_BOTH (from, from_byte); |
2349 | UPDATE_SYNTAX_TABLE_FORWARD (from); | |
04882296 | 2350 | /* We have skipped one comment. */ |
840f481c | 2351 | count1--; |
b3cfe0c8 RS |
2352 | } |
2353 | ||
840f481c | 2354 | while (count1 < 0) |
b3cfe0c8 | 2355 | { |
b9145dbb | 2356 | while (1) |
b3cfe0c8 | 2357 | { |
c5683ceb | 2358 | int quoted, syntax; |
f902a008 | 2359 | |
b9145dbb RS |
2360 | if (from <= stop) |
2361 | { | |
6a140a74 | 2362 | SET_PT_BOTH (BEGV, BEGV_BYTE); |
b9145dbb RS |
2363 | immediate_quit = 0; |
2364 | return Qnil; | |
2365 | } | |
b3cfe0c8 | 2366 | |
6a140a74 | 2367 | DEC_BOTH (from, from_byte); |
f902a008 | 2368 | /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */ |
6a140a74 | 2369 | quoted = char_quoted (from, from_byte); |
b7dbcc19 | 2370 | c = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
c5683ceb | 2371 | syntax = SYNTAX_WITH_FLAGS (c); |
b3cfe0c8 RS |
2372 | code = SYNTAX (c); |
2373 | comstyle = 0; | |
c5683ceb | 2374 | comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax); |
7fc8191e | 2375 | if (code == Sendcomment) |
c5683ceb SM |
2376 | comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0); |
2377 | if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax) | |
f902a008 | 2378 | && prev_char_comend_first (from, from_byte) |
89c6809a | 2379 | && !char_quoted (from - 1, dec_bytepos (from_byte))) |
b3cfe0c8 | 2380 | { |
c5683ceb | 2381 | int other_syntax; |
7fc8191e | 2382 | /* We must record the comment style encountered so that |
b3cfe0c8 | 2383 | later, we can match only the proper comment begin |
7fc8191e | 2384 | sequence of the same style. */ |
6a140a74 | 2385 | DEC_BOTH (from, from_byte); |
f902a008 RS |
2386 | code = Sendcomment; |
2387 | /* Calling char_quoted, above, set up global syntax position | |
2388 | at the new value of FROM. */ | |
b7dbcc19 | 2389 | c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
c5683ceb SM |
2390 | other_syntax = SYNTAX_WITH_FLAGS (c1); |
2391 | comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax); | |
2392 | comnested | |
2393 | = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax); | |
b3cfe0c8 RS |
2394 | } |
2395 | ||
195d1361 RS |
2396 | if (code == Scomment_fence) |
2397 | { | |
2398 | /* Skip until first preceding unquoted comment_fence. */ | |
4f3a2f8d EZ |
2399 | int found = 0; |
2400 | EMACS_INT ini = from, ini_byte = from_byte; | |
7d0393cf | 2401 | |
6a140a74 | 2402 | while (1) |
195d1361 | 2403 | { |
6a140a74 | 2404 | DEC_BOTH (from, from_byte); |
195d1361 | 2405 | UPDATE_SYNTAX_TABLE_BACKWARD (from); |
b7dbcc19 | 2406 | c = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
6a140a74 | 2407 | if (SYNTAX (c) == Scomment_fence |
7d0393cf | 2408 | && !char_quoted (from, from_byte)) |
195d1361 | 2409 | { |
7d0393cf | 2410 | found = 1; |
195d1361 RS |
2411 | break; |
2412 | } | |
fcdd4585 SM |
2413 | else if (from == stop) |
2414 | break; | |
195d1361 RS |
2415 | } |
2416 | if (found == 0) | |
2417 | { | |
2418 | from = ini; /* Set point to ini + 1. */ | |
6a140a74 | 2419 | from_byte = ini_byte; |
195d1361 RS |
2420 | goto leave; |
2421 | } | |
39c41ad4 SM |
2422 | else |
2423 | /* We have skipped one comment. */ | |
2424 | break; | |
195d1361 RS |
2425 | } |
2426 | else if (code == Sendcomment) | |
b3cfe0c8 | 2427 | { |
95ff8dfc | 2428 | found = back_comment (from, from_byte, stop, comnested, comstyle, |
6a140a74 | 2429 | &out_charpos, &out_bytepos); |
1aa963c8 SM |
2430 | if (found == -1) |
2431 | { | |
abf8a9ff SM |
2432 | if (c == '\n') |
2433 | /* This end-of-line is not an end-of-comment. | |
2434 | Treat it like a whitespace. | |
2435 | CC-mode (and maybe others) relies on this behavior. */ | |
2436 | ; | |
2437 | else | |
2438 | { | |
2439 | /* Failure: we should go back to the end of this | |
2440 | not-quite-endcomment. */ | |
c5683ceb | 2441 | if (SYNTAX (c) != code) |
abf8a9ff SM |
2442 | /* It was a two-char Sendcomment. */ |
2443 | INC_BOTH (from, from_byte); | |
2444 | goto leave; | |
2445 | } | |
1aa963c8 | 2446 | } |
2b927d02 | 2447 | else |
abf8a9ff SM |
2448 | { |
2449 | /* We have skipped one comment. */ | |
2450 | from = out_charpos, from_byte = out_bytepos; | |
2451 | break; | |
2452 | } | |
b3cfe0c8 | 2453 | } |
bb0de084 | 2454 | else if (code != Swhitespace || quoted) |
b3cfe0c8 | 2455 | { |
195d1361 | 2456 | leave: |
b3cfe0c8 | 2457 | immediate_quit = 0; |
6a140a74 | 2458 | INC_BOTH (from, from_byte); |
ef316cf0 | 2459 | SET_PT_BOTH (from, from_byte); |
b3cfe0c8 RS |
2460 | return Qnil; |
2461 | } | |
2462 | } | |
2463 | ||
840f481c | 2464 | count1++; |
b3cfe0c8 RS |
2465 | } |
2466 | ||
ef316cf0 | 2467 | SET_PT_BOTH (from, from_byte); |
b3cfe0c8 RS |
2468 | immediate_quit = 0; |
2469 | return Qt; | |
2470 | } | |
2471 | \f | |
b7dbcc19 | 2472 | /* Return syntax code of character C if C is an ASCII character |
db9f48c3 | 2473 | or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */ |
bd25db08 | 2474 | |
b7dbcc19 KH |
2475 | #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \ |
2476 | ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \ | |
bd25db08 KH |
2477 | ? SYNTAX (c) : Ssymbol) |
2478 | ||
6a140a74 | 2479 | static Lisp_Object |
971de7fb | 2480 | scan_lists (register EMACS_INT from, EMACS_INT count, EMACS_INT depth, int sexpflag) |
8489eb67 RS |
2481 | { |
2482 | Lisp_Object val; | |
932e6895 | 2483 | register EMACS_INT stop = count > 0 ? ZV : BEGV; |
93da5fff KH |
2484 | register int c, c1; |
2485 | int stringterm; | |
8489eb67 RS |
2486 | int quoted; |
2487 | int mathexit = 0; | |
93da5fff | 2488 | register enum syntaxcode code, temp_code; |
195d1361 | 2489 | int min_depth = depth; /* Err out if depth gets less than this. */ |
e5d4f4dc | 2490 | int comstyle = 0; /* style of comment encountered */ |
95ff8dfc | 2491 | int comnested = 0; /* whether the comment is nestable or not */ |
932e6895 SM |
2492 | EMACS_INT temp_pos; |
2493 | EMACS_INT last_good = from; | |
195d1361 | 2494 | int found; |
932e6895 SM |
2495 | EMACS_INT from_byte; |
2496 | EMACS_INT out_bytepos, out_charpos; | |
95ff8dfc | 2497 | int temp, dummy; |
bd25db08 | 2498 | int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol; |
8489eb67 RS |
2499 | |
2500 | if (depth > 0) min_depth = 0; | |
2501 | ||
c3907a7e RS |
2502 | if (from > ZV) from = ZV; |
2503 | if (from < BEGV) from = BEGV; | |
2504 | ||
2505 | from_byte = CHAR_TO_BYTE (from); | |
2506 | ||
8489eb67 RS |
2507 | immediate_quit = 1; |
2508 | QUIT; | |
2509 | ||
195d1361 | 2510 | SETUP_SYNTAX_TABLE (from, count); |
8489eb67 RS |
2511 | while (count > 0) |
2512 | { | |
8489eb67 RS |
2513 | while (from < stop) |
2514 | { | |
c5683ceb | 2515 | int comstart_first, prefix, syntax, other_syntax; |
195d1361 | 2516 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
b7dbcc19 | 2517 | c = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
c5683ceb | 2518 | syntax = SYNTAX_WITH_FLAGS (c); |
bd25db08 | 2519 | code = SYNTAX_WITH_MULTIBYTE_CHECK (c); |
c5683ceb SM |
2520 | comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax); |
2521 | comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax); | |
2522 | comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0); | |
2523 | prefix = SYNTAX_FLAGS_PREFIX (syntax); | |
7bf5e9e4 RS |
2524 | if (depth == min_depth) |
2525 | last_good = from; | |
6a140a74 | 2526 | INC_BOTH (from, from_byte); |
195d1361 | 2527 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
f902a008 | 2528 | if (from < stop && comstart_first |
327719ee | 2529 | && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte), |
c5683ceb SM |
2530 | other_syntax = SYNTAX_WITH_FLAGS (c), |
2531 | SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)) | |
8489eb67 | 2532 | && parse_sexp_ignore_comments) |
e5d4f4dc | 2533 | { |
7d0393cf | 2534 | /* we have encountered a comment start sequence and we |
195d1361 | 2535 | are ignoring all text inside comments. We must record |
e5d4f4dc RS |
2536 | the comment style this sequence begins so that later, |
2537 | only a comment end of the same style actually ends | |
2538 | the comment section */ | |
2539 | code = Scomment; | |
c5683ceb SM |
2540 | comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax); |
2541 | comnested | |
2542 | = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax); | |
6a140a74 | 2543 | INC_BOTH (from, from_byte); |
f902a008 | 2544 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
e5d4f4dc | 2545 | } |
7d0393cf | 2546 | |
f902a008 | 2547 | if (prefix) |
8489eb67 RS |
2548 | continue; |
2549 | ||
0220c518 | 2550 | switch (SWITCH_ENUM_CAST (code)) |
8489eb67 RS |
2551 | { |
2552 | case Sescape: | |
2553 | case Scharquote: | |
8b8bf8e6 SM |
2554 | if (from == stop) |
2555 | goto lose; | |
6a140a74 | 2556 | INC_BOTH (from, from_byte); |
8489eb67 RS |
2557 | /* treat following character as a word constituent */ |
2558 | case Sword: | |
2559 | case Ssymbol: | |
2560 | if (depth || !sexpflag) break; | |
195d1361 | 2561 | /* This word counts as a sexp; return at end of it. */ |
8489eb67 RS |
2562 | while (from < stop) |
2563 | { | |
195d1361 | 2564 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
7c7ca3d2 RS |
2565 | |
2566 | /* Some compilers can't handle this inside the switch. */ | |
b7dbcc19 | 2567 | c = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
bd25db08 | 2568 | temp = SYNTAX_WITH_MULTIBYTE_CHECK (c); |
7c7ca3d2 | 2569 | switch (temp) |
8489eb67 RS |
2570 | { |
2571 | case Scharquote: | |
2572 | case Sescape: | |
6a140a74 | 2573 | INC_BOTH (from, from_byte); |
8b8bf8e6 SM |
2574 | if (from == stop) |
2575 | goto lose; | |
8489eb67 RS |
2576 | break; |
2577 | case Sword: | |
2578 | case Ssymbol: | |
2579 | case Squote: | |
2580 | break; | |
2581 | default: | |
2582 | goto done; | |
2583 | } | |
6a140a74 | 2584 | INC_BOTH (from, from_byte); |
8489eb67 RS |
2585 | } |
2586 | goto done; | |
2587 | ||
195d1361 | 2588 | case Scomment_fence: |
95ff8dfc RS |
2589 | comstyle = ST_COMMENT_STYLE; |
2590 | /* FALLTHROUGH */ | |
2591 | case Scomment: | |
8489eb67 | 2592 | if (!parse_sexp_ignore_comments) break; |
95ff8dfc RS |
2593 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
2594 | found = forw_comment (from, from_byte, stop, | |
2595 | comnested, comstyle, 0, | |
2596 | &out_charpos, &out_bytepos, &dummy); | |
2597 | from = out_charpos, from_byte = out_bytepos; | |
2598 | if (!found) | |
8489eb67 | 2599 | { |
95ff8dfc RS |
2600 | if (depth == 0) |
2601 | goto done; | |
2602 | goto lose; | |
8489eb67 | 2603 | } |
95ff8dfc RS |
2604 | INC_BOTH (from, from_byte); |
2605 | UPDATE_SYNTAX_TABLE_FORWARD (from); | |
8489eb67 RS |
2606 | break; |
2607 | ||
2608 | case Smath: | |
2609 | if (!sexpflag) | |
2610 | break; | |
b7dbcc19 | 2611 | if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte)) |
6a140a74 RS |
2612 | { |
2613 | INC_BOTH (from, from_byte); | |
2614 | } | |
8489eb67 RS |
2615 | if (mathexit) |
2616 | { | |
2617 | mathexit = 0; | |
2618 | goto close1; | |
2619 | } | |
2620 | mathexit = 1; | |
2621 | ||
2622 | case Sopen: | |
2623 | if (!++depth) goto done; | |
2624 | break; | |
2625 | ||
2626 | case Sclose: | |
2627 | close1: | |
2628 | if (!--depth) goto done; | |
2629 | if (depth < min_depth) | |
9471945b KS |
2630 | xsignal3 (Qscan_error, |
2631 | build_string ("Containing expression ends prematurely"), | |
2632 | make_number (last_good), make_number (from)); | |
8489eb67 RS |
2633 | break; |
2634 | ||
2635 | case Sstring: | |
195d1361 | 2636 | case Sstring_fence: |
ef316cf0 | 2637 | temp_pos = dec_bytepos (from_byte); |
b7dbcc19 | 2638 | stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos); |
8489eb67 RS |
2639 | while (1) |
2640 | { | |
8b8bf8e6 SM |
2641 | if (from >= stop) |
2642 | goto lose; | |
195d1361 | 2643 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
b7dbcc19 | 2644 | c = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
42ebfa31 SM |
2645 | if (code == Sstring |
2646 | ? (c == stringterm | |
2647 | && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring) | |
bd25db08 | 2648 | : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence) |
195d1361 | 2649 | break; |
7c7ca3d2 RS |
2650 | |
2651 | /* Some compilers can't handle this inside the switch. */ | |
bd25db08 | 2652 | temp = SYNTAX_WITH_MULTIBYTE_CHECK (c); |
7c7ca3d2 | 2653 | switch (temp) |
8489eb67 RS |
2654 | { |
2655 | case Scharquote: | |
2656 | case Sescape: | |
6a140a74 | 2657 | INC_BOTH (from, from_byte); |
8489eb67 | 2658 | } |
6a140a74 | 2659 | INC_BOTH (from, from_byte); |
8489eb67 | 2660 | } |
6a140a74 | 2661 | INC_BOTH (from, from_byte); |
8489eb67 RS |
2662 | if (!depth && sexpflag) goto done; |
2663 | break; | |
240c43e8 SM |
2664 | default: |
2665 | /* Ignore whitespace, punctuation, quote, endcomment. */ | |
2666 | break; | |
8489eb67 RS |
2667 | } |
2668 | } | |
2669 | ||
2670 | /* Reached end of buffer. Error if within object, return nil if between */ | |
8b8bf8e6 SM |
2671 | if (depth) |
2672 | goto lose; | |
8489eb67 RS |
2673 | |
2674 | immediate_quit = 0; | |
2675 | return Qnil; | |
2676 | ||
2677 | /* End of object reached */ | |
2678 | done: | |
2679 | count--; | |
2680 | } | |
2681 | ||
2682 | while (count < 0) | |
2683 | { | |
8489eb67 RS |
2684 | while (from > stop) |
2685 | { | |
c5683ceb | 2686 | int syntax; |
6a140a74 | 2687 | DEC_BOTH (from, from_byte); |
195d1361 | 2688 | UPDATE_SYNTAX_TABLE_BACKWARD (from); |
b7dbcc19 | 2689 | c = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
c5683ceb | 2690 | syntax= SYNTAX_WITH_FLAGS (c); |
bd25db08 | 2691 | code = SYNTAX_WITH_MULTIBYTE_CHECK (c); |
7bf5e9e4 RS |
2692 | if (depth == min_depth) |
2693 | last_good = from; | |
7fc8191e | 2694 | comstyle = 0; |
c5683ceb | 2695 | comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax); |
7fc8191e | 2696 | if (code == Sendcomment) |
c5683ceb SM |
2697 | comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0); |
2698 | if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax) | |
1674d9a2 | 2699 | && prev_char_comend_first (from, from_byte) |
8489eb67 | 2700 | && parse_sexp_ignore_comments) |
e5d4f4dc | 2701 | { |
f902a008 | 2702 | /* We must record the comment style encountered so that |
e5d4f4dc | 2703 | later, we can match only the proper comment begin |
f902a008 | 2704 | sequence of the same style. */ |
c5683ceb | 2705 | int c1, other_syntax; |
6a140a74 | 2706 | DEC_BOTH (from, from_byte); |
f902a008 RS |
2707 | UPDATE_SYNTAX_TABLE_BACKWARD (from); |
2708 | code = Sendcomment; | |
b7dbcc19 | 2709 | c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
c5683ceb SM |
2710 | other_syntax = SYNTAX_WITH_FLAGS (c1); |
2711 | comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax); | |
2712 | comnested | |
2713 | = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax); | |
e5d4f4dc | 2714 | } |
7d0393cf | 2715 | |
9828a477 | 2716 | /* Quoting turns anything except a comment-ender |
e6365855 | 2717 | into a word character. Note that this cannot be true |
f902a008 | 2718 | if we decremented FROM in the if-statement above. */ |
9828a477 | 2719 | if (code != Sendcomment && char_quoted (from, from_byte)) |
240c43e8 SM |
2720 | { |
2721 | DEC_BOTH (from, from_byte); | |
2722 | code = Sword; | |
2723 | } | |
c5683ceb | 2724 | else if (SYNTAX_FLAGS_PREFIX (syntax)) |
8489eb67 RS |
2725 | continue; |
2726 | ||
9828a477 | 2727 | switch (SWITCH_ENUM_CAST (code)) |
8489eb67 RS |
2728 | { |
2729 | case Sword: | |
2730 | case Ssymbol: | |
9828a477 KH |
2731 | case Sescape: |
2732 | case Scharquote: | |
8489eb67 | 2733 | if (depth || !sexpflag) break; |
195d1361 RS |
2734 | /* This word counts as a sexp; count object finished |
2735 | after passing it. */ | |
8489eb67 RS |
2736 | while (from > stop) |
2737 | { | |
6a140a74 | 2738 | temp_pos = from_byte; |
ef316cf0 RS |
2739 | if (! NILP (current_buffer->enable_multibyte_characters)) |
2740 | DEC_POS (temp_pos); | |
2741 | else | |
2742 | temp_pos--; | |
6a140a74 | 2743 | UPDATE_SYNTAX_TABLE_BACKWARD (from - 1); |
b7dbcc19 | 2744 | c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos); |
bd25db08 | 2745 | temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1); |
9828a477 KH |
2746 | /* Don't allow comment-end to be quoted. */ |
2747 | if (temp_code == Sendcomment) | |
2748 | goto done2; | |
6a140a74 | 2749 | quoted = char_quoted (from - 1, temp_pos); |
8489eb67 | 2750 | if (quoted) |
93da5fff | 2751 | { |
6a140a74 | 2752 | DEC_BOTH (from, from_byte); |
ef316cf0 | 2753 | temp_pos = dec_bytepos (temp_pos); |
6a140a74 | 2754 | UPDATE_SYNTAX_TABLE_BACKWARD (from - 1); |
93da5fff | 2755 | } |
b7dbcc19 | 2756 | c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos); |
bd25db08 | 2757 | temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1); |
93da5fff KH |
2758 | if (! (quoted || temp_code == Sword |
2759 | || temp_code == Ssymbol | |
2760 | || temp_code == Squote)) | |
8489eb67 | 2761 | goto done2; |
6a140a74 | 2762 | DEC_BOTH (from, from_byte); |
8489eb67 RS |
2763 | } |
2764 | goto done2; | |
2765 | ||
2766 | case Smath: | |
2767 | if (!sexpflag) | |
2768 | break; | |
ef316cf0 | 2769 | temp_pos = dec_bytepos (from_byte); |
6a140a74 | 2770 | UPDATE_SYNTAX_TABLE_BACKWARD (from - 1); |
b7dbcc19 | 2771 | if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos)) |
6a140a74 | 2772 | DEC_BOTH (from, from_byte); |
8489eb67 RS |
2773 | if (mathexit) |
2774 | { | |
2775 | mathexit = 0; | |
2776 | goto open2; | |
2777 | } | |
2778 | mathexit = 1; | |
2779 | ||
2780 | case Sclose: | |
2781 | if (!++depth) goto done2; | |
2782 | break; | |
2783 | ||
2784 | case Sopen: | |
2785 | open2: | |
2786 | if (!--depth) goto done2; | |
2787 | if (depth < min_depth) | |
9471945b KS |
2788 | xsignal3 (Qscan_error, |
2789 | build_string ("Containing expression ends prematurely"), | |
2790 | make_number (last_good), make_number (from)); | |
8489eb67 RS |
2791 | break; |
2792 | ||
2793 | case Sendcomment: | |
2794 | if (!parse_sexp_ignore_comments) | |
2795 | break; | |
95ff8dfc | 2796 | found = back_comment (from, from_byte, stop, comnested, comstyle, |
6a140a74 | 2797 | &out_charpos, &out_bytepos); |
1aa963c8 SM |
2798 | /* FIXME: if found == -1, then it really wasn't a comment-end. |
2799 | For single-char Sendcomment, we can't do much about it apart | |
2800 | from skipping the char. | |
2801 | For 2-char endcomments, we could try again, taking both | |
2802 | chars as separate entities, but it's a lot of trouble | |
2803 | for very little gain, so we don't bother either. -sm */ | |
6a140a74 RS |
2804 | if (found != -1) |
2805 | from = out_charpos, from_byte = out_bytepos; | |
8489eb67 RS |
2806 | break; |
2807 | ||
195d1361 RS |
2808 | case Scomment_fence: |
2809 | case Sstring_fence: | |
2810 | while (1) | |
2811 | { | |
8b8bf8e6 SM |
2812 | if (from == stop) |
2813 | goto lose; | |
6b61353c | 2814 | DEC_BOTH (from, from_byte); |
195d1361 | 2815 | UPDATE_SYNTAX_TABLE_BACKWARD (from); |
7d0393cf | 2816 | if (!char_quoted (from, from_byte) |
b7dbcc19 | 2817 | && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte), |
bd25db08 | 2818 | SYNTAX_WITH_MULTIBYTE_CHECK (c) == code)) |
195d1361 RS |
2819 | break; |
2820 | } | |
2821 | if (code == Sstring_fence && !depth && sexpflag) goto done2; | |
2822 | break; | |
7d0393cf | 2823 | |
8489eb67 | 2824 | case Sstring: |
b7dbcc19 | 2825 | stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
8489eb67 RS |
2826 | while (1) |
2827 | { | |
8b8bf8e6 SM |
2828 | if (from == stop) |
2829 | goto lose; | |
6b61353c KH |
2830 | DEC_BOTH (from, from_byte); |
2831 | UPDATE_SYNTAX_TABLE_BACKWARD (from); | |
2832 | if (!char_quoted (from, from_byte) | |
2833 | && (stringterm | |
2834 | == (c = FETCH_CHAR_AS_MULTIBYTE (from_byte))) | |
42ebfa31 | 2835 | && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring) |
8489eb67 | 2836 | break; |
8489eb67 | 2837 | } |
8489eb67 RS |
2838 | if (!depth && sexpflag) goto done2; |
2839 | break; | |
240c43e8 SM |
2840 | default: |
2841 | /* Ignore whitespace, punctuation, quote, endcomment. */ | |
2842 | break; | |
8489eb67 RS |
2843 | } |
2844 | } | |
2845 | ||
2846 | /* Reached start of buffer. Error if within object, return nil if between */ | |
8b8bf8e6 SM |
2847 | if (depth) |
2848 | goto lose; | |
8489eb67 RS |
2849 | |
2850 | immediate_quit = 0; | |
2851 | return Qnil; | |
2852 | ||
2853 | done2: | |
2854 | count++; | |
2855 | } | |
2856 | ||
2857 | ||
2858 | immediate_quit = 0; | |
1e142fb7 | 2859 | XSETFASTINT (val, from); |
8489eb67 RS |
2860 | return val; |
2861 | ||
2862 | lose: | |
9471945b KS |
2863 | xsignal3 (Qscan_error, |
2864 | build_string ("Unbalanced parentheses"), | |
2865 | make_number (last_good), make_number (from)); | |
8489eb67 RS |
2866 | } |
2867 | ||
8489eb67 | 2868 | DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0, |
fdb82f93 PJ |
2869 | doc: /* Scan from character number FROM by COUNT lists. |
2870 | Returns the character number of the position thus found. | |
2871 | ||
2872 | If DEPTH is nonzero, paren depth begins counting from that value, | |
2873 | only places where the depth in parentheses becomes zero | |
2874 | are candidates for stopping; COUNT such places are counted. | |
2875 | Thus, a positive value for DEPTH means go out levels. | |
2876 | ||
2877 | Comments are ignored if `parse-sexp-ignore-comments' is non-nil. | |
2878 | ||
2879 | If the beginning or end of (the accessible part of) the buffer is reached | |
2880 | and the depth is wrong, an error is signaled. | |
2881 | If the depth is right but the count is not used up, nil is returned. */) | |
5842a27b | 2882 | (Lisp_Object from, Lisp_Object count, Lisp_Object depth) |
8489eb67 | 2883 | { |
b7826503 PJ |
2884 | CHECK_NUMBER (from); |
2885 | CHECK_NUMBER (count); | |
2886 | CHECK_NUMBER (depth); | |
8489eb67 RS |
2887 | |
2888 | return scan_lists (XINT (from), XINT (count), XINT (depth), 0); | |
2889 | } | |
2890 | ||
2891 | DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0, | |
fdb82f93 PJ |
2892 | doc: /* Scan from character number FROM by COUNT balanced expressions. |
2893 | If COUNT is negative, scan backwards. | |
2894 | Returns the character number of the position thus found. | |
2895 | ||
2896 | Comments are ignored if `parse-sexp-ignore-comments' is non-nil. | |
2897 | ||
2898 | If the beginning or end of (the accessible part of) the buffer is reached | |
2899 | in the middle of a parenthetical grouping, an error is signaled. | |
2900 | If the beginning or end is reached between groupings | |
2901 | but before count is used up, nil is returned. */) | |
5842a27b | 2902 | (Lisp_Object from, Lisp_Object count) |
8489eb67 | 2903 | { |
b7826503 PJ |
2904 | CHECK_NUMBER (from); |
2905 | CHECK_NUMBER (count); | |
8489eb67 RS |
2906 | |
2907 | return scan_lists (XINT (from), XINT (count), 0, 1); | |
2908 | } | |
2909 | ||
2910 | DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars, | |
fdb82f93 PJ |
2911 | 0, 0, 0, |
2912 | doc: /* Move point backward over any number of chars with prefix syntax. | |
2913 | This includes chars with "quote" or "prefix" syntax (' or p). */) | |
5842a27b | 2914 | (void) |
8489eb67 | 2915 | { |
4f3a2f8d EZ |
2916 | EMACS_INT beg = BEGV; |
2917 | EMACS_INT opoint = PT; | |
2918 | EMACS_INT opoint_byte = PT_BYTE; | |
2919 | EMACS_INT pos = PT; | |
2920 | EMACS_INT pos_byte = PT_BYTE; | |
93da5fff | 2921 | int c; |
93da5fff | 2922 | |
7d0393cf | 2923 | if (pos <= beg) |
195d1361 | 2924 | { |
f902a008 RS |
2925 | SET_PT_BOTH (opoint, opoint_byte); |
2926 | ||
2927 | return Qnil; | |
195d1361 | 2928 | } |
8489eb67 | 2929 | |
f902a008 RS |
2930 | SETUP_SYNTAX_TABLE (pos, -1); |
2931 | ||
6a140a74 RS |
2932 | DEC_BOTH (pos, pos_byte); |
2933 | ||
1fd3172d | 2934 | while (!char_quoted (pos, pos_byte) |
195d1361 | 2935 | /* Previous statement updates syntax table. */ |
b7dbcc19 | 2936 | && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote) |
93da5fff KH |
2937 | || SYNTAX_PREFIX (c))) |
2938 | { | |
1fd3172d RS |
2939 | opoint = pos; |
2940 | opoint_byte = pos_byte; | |
2941 | ||
2942 | if (pos + 1 > beg) | |
2943 | DEC_BOTH (pos, pos_byte); | |
93da5fff | 2944 | } |
8489eb67 | 2945 | |
6a140a74 | 2946 | SET_PT_BOTH (opoint, opoint_byte); |
8489eb67 RS |
2947 | |
2948 | return Qnil; | |
2949 | } | |
2950 | \f | |
6a140a74 | 2951 | /* Parse forward from FROM / FROM_BYTE to END, |
e5d4f4dc RS |
2952 | assuming that FROM has state OLDSTATE (nil means FROM is start of function), |
2953 | and return a description of the state of the parse at END. | |
c81a3712 | 2954 | If STOPBEFORE is nonzero, stop at the start of an atom. |
644ea4df RS |
2955 | If COMMENTSTOP is 1, stop at the start of a comment. |
2956 | If COMMENTSTOP is -1, stop at the start or end of a comment, | |
2957 | after the beginning of a string, or after the end of a string. */ | |
8489eb67 | 2958 | |
340f92b5 | 2959 | static void |
d5a3eaaf AS |
2960 | scan_sexps_forward (struct lisp_parse_state *stateptr, |
2961 | EMACS_INT from, EMACS_INT from_byte, EMACS_INT end, | |
2962 | int targetdepth, int stopbefore, | |
2963 | Lisp_Object oldstate, int commentstop) | |
8489eb67 RS |
2964 | { |
2965 | struct lisp_parse_state state; | |
2966 | ||
2967 | register enum syntaxcode code; | |
95ff8dfc RS |
2968 | int c1; |
2969 | int comnested; | |
8489eb67 RS |
2970 | struct level { int last, prev; }; |
2971 | struct level levelstart[100]; | |
2972 | register struct level *curlevel = levelstart; | |
2973 | struct level *endlevel = levelstart + 100; | |
8489eb67 RS |
2974 | register int depth; /* Paren depth of current scanning location. |
2975 | level - levelstart equals this except | |
2976 | when the depth becomes negative. */ | |
2977 | int mindepth; /* Lowest DEPTH value seen. */ | |
2978 | int start_quoted = 0; /* Nonzero means starting after a char quote */ | |
2979 | Lisp_Object tem; | |
8e2911c2 AS |
2980 | EMACS_INT prev_from; /* Keep one character before FROM. */ |
2981 | EMACS_INT prev_from_byte; | |
1fd3172d | 2982 | int prev_from_syntax; |
195d1361 RS |
2983 | int boundary_stop = commentstop == -1; |
2984 | int nofence; | |
95ff8dfc | 2985 | int found; |
60c86a83 | 2986 | EMACS_INT out_bytepos, out_charpos; |
7c7ca3d2 | 2987 | int temp; |
93da5fff KH |
2988 | |
2989 | prev_from = from; | |
6a140a74 RS |
2990 | prev_from_byte = from_byte; |
2991 | if (from != BEGV) | |
2992 | DEC_BOTH (prev_from, prev_from_byte); | |
93da5fff KH |
2993 | |
2994 | /* Use this macro instead of `from++'. */ | |
6a140a74 RS |
2995 | #define INC_FROM \ |
2996 | do { prev_from = from; \ | |
2997 | prev_from_byte = from_byte; \ | |
327719ee | 2998 | temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \ |
ab229fdd | 2999 | prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \ |
ef316cf0 | 3000 | INC_BOTH (from, from_byte); \ |
3ceb4629 SM |
3001 | if (from < end) \ |
3002 | UPDATE_SYNTAX_TABLE_FORWARD (from); \ | |
6a140a74 | 3003 | } while (0) |
8489eb67 RS |
3004 | |
3005 | immediate_quit = 1; | |
3006 | QUIT; | |
3007 | ||
265a9e55 | 3008 | if (NILP (oldstate)) |
8489eb67 RS |
3009 | { |
3010 | depth = 0; | |
3011 | state.instring = -1; | |
3012 | state.incomment = 0; | |
195d1361 RS |
3013 | state.comstyle = 0; /* comment style a by default. */ |
3014 | state.comstr_start = -1; /* no comment/string seen. */ | |
8489eb67 RS |
3015 | } |
3016 | else | |
3017 | { | |
3018 | tem = Fcar (oldstate); | |
265a9e55 | 3019 | if (!NILP (tem)) |
8489eb67 RS |
3020 | depth = XINT (tem); |
3021 | else | |
3022 | depth = 0; | |
3023 | ||
3024 | oldstate = Fcdr (oldstate); | |
3025 | oldstate = Fcdr (oldstate); | |
3026 | oldstate = Fcdr (oldstate); | |
3027 | tem = Fcar (oldstate); | |
195d1361 | 3028 | /* Check whether we are inside string_fence-style string: */ |
7d0393cf JB |
3029 | state.instring = (!NILP (tem) |
3030 | ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE) | |
e76f1c44 | 3031 | : -1); |
8489eb67 RS |
3032 | |
3033 | oldstate = Fcdr (oldstate); | |
3034 | tem = Fcar (oldstate); | |
e76f1c44 GM |
3035 | state.incomment = (!NILP (tem) |
3036 | ? (INTEGERP (tem) ? XINT (tem) : -1) | |
95ff8dfc | 3037 | : 0); |
8489eb67 RS |
3038 | |
3039 | oldstate = Fcdr (oldstate); | |
3040 | tem = Fcar (oldstate); | |
265a9e55 | 3041 | start_quoted = !NILP (tem); |
e5d4f4dc | 3042 | |
95ff8dfc | 3043 | /* if the eighth element of the list is nil, we are in comment |
195d1361 RS |
3044 | style a. If it is non-nil, we are in comment style b */ |
3045 | oldstate = Fcdr (oldstate); | |
e5d4f4dc | 3046 | oldstate = Fcdr (oldstate); |
195d1361 | 3047 | tem = Fcar (oldstate); |
c5683ceb SM |
3048 | state.comstyle = (NILP (tem) |
3049 | ? 0 | |
3050 | : (EQ (tem, Qsyntax_table) | |
3051 | ? ST_COMMENT_STYLE | |
3052 | : INTEGERP (tem) ? XINT (tem) : 1)); | |
195d1361 | 3053 | |
e5d4f4dc | 3054 | oldstate = Fcdr (oldstate); |
e5d4f4dc | 3055 | tem = Fcar (oldstate); |
195d1361 | 3056 | state.comstr_start = NILP (tem) ? -1 : XINT (tem) ; |
1a102a58 RS |
3057 | oldstate = Fcdr (oldstate); |
3058 | tem = Fcar (oldstate); | |
3059 | while (!NILP (tem)) /* >= second enclosing sexps. */ | |
3060 | { | |
3061 | /* curlevel++->last ran into compiler bug on Apollo */ | |
3062 | curlevel->last = XINT (Fcar (tem)); | |
3063 | if (++curlevel == endlevel) | |
02010917 | 3064 | curlevel--; /* error ("Nesting too deep for parser"); */ |
1a102a58 RS |
3065 | curlevel->prev = -1; |
3066 | curlevel->last = -1; | |
3067 | tem = Fcdr (tem); | |
3068 | } | |
8489eb67 RS |
3069 | } |
3070 | state.quoted = 0; | |
3071 | mindepth = depth; | |
3072 | ||
3073 | curlevel->prev = -1; | |
3074 | curlevel->last = -1; | |
3075 | ||
326b283b | 3076 | SETUP_SYNTAX_TABLE (prev_from, 1); |
ab229fdd AS |
3077 | temp = FETCH_CHAR (prev_from_byte); |
3078 | prev_from_syntax = SYNTAX_WITH_FLAGS (temp); | |
3ceb4629 | 3079 | UPDATE_SYNTAX_TABLE_FORWARD (from); |
326b283b | 3080 | |
195d1361 | 3081 | /* Enter the loop at a place appropriate for initial state. */ |
8489eb67 | 3082 | |
326b283b RS |
3083 | if (state.incomment) |
3084 | goto startincomment; | |
8489eb67 RS |
3085 | if (state.instring >= 0) |
3086 | { | |
195d1361 | 3087 | nofence = state.instring != ST_STRING_STYLE; |
326b283b RS |
3088 | if (start_quoted) |
3089 | goto startquotedinstring; | |
8489eb67 RS |
3090 | goto startinstring; |
3091 | } | |
326b283b RS |
3092 | else if (start_quoted) |
3093 | goto startquoted; | |
1fd3172d | 3094 | |
8489eb67 RS |
3095 | while (from < end) |
3096 | { | |
c5683ceb | 3097 | int syntax; |
93da5fff | 3098 | INC_FROM; |
1fd3172d | 3099 | code = prev_from_syntax & 0xff; |
4c920633 | 3100 | |
c5d4b96f SM |
3101 | if (from < end |
3102 | && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax) | |
3103 | && (c1 = FETCH_CHAR (from_byte), | |
c5683ceb SM |
3104 | syntax = SYNTAX_WITH_FLAGS (c1), |
3105 | SYNTAX_FLAGS_COMSTART_SECOND (syntax))) | |
c5d4b96f SM |
3106 | /* Duplicate code to avoid a complex if-expression |
3107 | which causes trouble for the SGI compiler. */ | |
95ff8dfc | 3108 | { |
c5d4b96f SM |
3109 | /* Record the comment style we have entered so that only |
3110 | the comment-end sequence of the same style actually | |
3111 | terminates the comment section. */ | |
c5683ceb SM |
3112 | state.comstyle |
3113 | = SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_from_syntax); | |
c5d4b96f | 3114 | comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax); |
c5683ceb | 3115 | comnested = comnested || SYNTAX_FLAGS_COMMENT_NESTED (syntax); |
c5d4b96f | 3116 | state.incomment = comnested ? 1 : -1; |
95ff8dfc | 3117 | state.comstr_start = prev_from; |
c5d4b96f SM |
3118 | INC_FROM; |
3119 | code = Scomment; | |
95ff8dfc | 3120 | } |
4c920633 | 3121 | else if (code == Scomment_fence) |
e5d4f4dc RS |
3122 | { |
3123 | /* Record the comment style we have entered so that only | |
3124 | the comment-end sequence of the same style actually | |
3125 | terminates the comment section. */ | |
95ff8dfc RS |
3126 | state.comstyle = ST_COMMENT_STYLE; |
3127 | state.incomment = -1; | |
195d1361 | 3128 | state.comstr_start = prev_from; |
e5d4f4dc | 3129 | code = Scomment; |
e5d4f4dc | 3130 | } |
c5d4b96f SM |
3131 | else if (code == Scomment) |
3132 | { | |
c5683ceb | 3133 | state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax, 0); |
c5d4b96f SM |
3134 | state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ? |
3135 | 1 : -1); | |
3136 | state.comstr_start = prev_from; | |
3137 | } | |
e5d4f4dc | 3138 | |
1fd3172d | 3139 | if (SYNTAX_FLAGS_PREFIX (prev_from_syntax)) |
8489eb67 | 3140 | continue; |
0220c518 | 3141 | switch (SWITCH_ENUM_CAST (code)) |
8489eb67 RS |
3142 | { |
3143 | case Sescape: | |
3144 | case Scharquote: | |
3145 | if (stopbefore) goto stop; /* this arg means stop at sexp start */ | |
93da5fff | 3146 | curlevel->last = prev_from; |
8489eb67 RS |
3147 | startquoted: |
3148 | if (from == end) goto endquoted; | |
93da5fff | 3149 | INC_FROM; |
8489eb67 RS |
3150 | goto symstarted; |
3151 | /* treat following character as a word constituent */ | |
3152 | case Sword: | |
3153 | case Ssymbol: | |
3154 | if (stopbefore) goto stop; /* this arg means stop at sexp start */ | |
93da5fff | 3155 | curlevel->last = prev_from; |
8489eb67 RS |
3156 | symstarted: |
3157 | while (from < end) | |
3158 | { | |
7c7ca3d2 | 3159 | /* Some compilers can't handle this inside the switch. */ |
327719ee | 3160 | temp = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
ab229fdd | 3161 | temp = SYNTAX (temp); |
7c7ca3d2 | 3162 | switch (temp) |
8489eb67 RS |
3163 | { |
3164 | case Scharquote: | |
3165 | case Sescape: | |
93da5fff | 3166 | INC_FROM; |
8489eb67 RS |
3167 | if (from == end) goto endquoted; |
3168 | break; | |
3169 | case Sword: | |
3170 | case Ssymbol: | |
3171 | case Squote: | |
3172 | break; | |
3173 | default: | |
3174 | goto symdone; | |
3175 | } | |
93da5fff | 3176 | INC_FROM; |
8489eb67 RS |
3177 | } |
3178 | symdone: | |
3179 | curlevel->prev = curlevel->last; | |
3180 | break; | |
3181 | ||
240c43e8 | 3182 | case Scomment_fence: /* Can't happen because it's handled above. */ |
8489eb67 | 3183 | case Scomment: |
195d1361 | 3184 | if (commentstop || boundary_stop) goto done; |
d355bd8a SM |
3185 | startincomment: |
3186 | /* The (from == BEGV) test was to enter the loop in the middle so | |
95ff8dfc | 3187 | that we find a 2-char comment ender even if we start in the |
e6365855 | 3188 | middle of it. We don't want to do that if we're just at the |
d355bd8a | 3189 | beginning of the comment (think of (*) ... (*)). */ |
95ff8dfc RS |
3190 | found = forw_comment (from, from_byte, end, |
3191 | state.incomment, state.comstyle, | |
e6365855 SM |
3192 | (from == BEGV || from < state.comstr_start + 3) |
3193 | ? 0 : prev_from_syntax, | |
95ff8dfc RS |
3194 | &out_charpos, &out_bytepos, &state.incomment); |
3195 | from = out_charpos; from_byte = out_bytepos; | |
3196 | /* Beware! prev_from and friends are invalid now. | |
3197 | Luckily, the `done' doesn't use them and the INC_FROM | |
3198 | sets them to a sane value without looking at them. */ | |
3199 | if (!found) goto done; | |
d355bd8a | 3200 | INC_FROM; |
8489eb67 | 3201 | state.incomment = 0; |
e5d4f4dc | 3202 | state.comstyle = 0; /* reset the comment style */ |
195d1361 | 3203 | if (boundary_stop) goto done; |
8489eb67 RS |
3204 | break; |
3205 | ||
3206 | case Sopen: | |
3207 | if (stopbefore) goto stop; /* this arg means stop at sexp start */ | |
3208 | depth++; | |
3209 | /* curlevel++->last ran into compiler bug on Apollo */ | |
93da5fff | 3210 | curlevel->last = prev_from; |
8489eb67 | 3211 | if (++curlevel == endlevel) |
02010917 | 3212 | curlevel--; /* error ("Nesting too deep for parser"); */ |
8489eb67 RS |
3213 | curlevel->prev = -1; |
3214 | curlevel->last = -1; | |
30844415 | 3215 | if (targetdepth == depth) goto done; |
8489eb67 RS |
3216 | break; |
3217 | ||
3218 | case Sclose: | |
3219 | depth--; | |
3220 | if (depth < mindepth) | |
3221 | mindepth = depth; | |
3222 | if (curlevel != levelstart) | |
3223 | curlevel--; | |
3224 | curlevel->prev = curlevel->last; | |
30844415 | 3225 | if (targetdepth == depth) goto done; |
8489eb67 RS |
3226 | break; |
3227 | ||
3228 | case Sstring: | |
195d1361 RS |
3229 | case Sstring_fence: |
3230 | state.comstr_start = from - 1; | |
8489eb67 | 3231 | if (stopbefore) goto stop; /* this arg means stop at sexp start */ |
93da5fff | 3232 | curlevel->last = prev_from; |
7d0393cf | 3233 | state.instring = (code == Sstring |
b7dbcc19 | 3234 | ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte)) |
195d1361 RS |
3235 | : ST_STRING_STYLE); |
3236 | if (boundary_stop) goto done; | |
8489eb67 | 3237 | startinstring: |
195d1361 | 3238 | { |
644ea4df | 3239 | nofence = state.instring != ST_STRING_STYLE; |
7d0393cf | 3240 | |
644ea4df RS |
3241 | while (1) |
3242 | { | |
3243 | int c; | |
195d1361 | 3244 | |
644ea4df | 3245 | if (from >= end) goto done; |
b7dbcc19 | 3246 | c = FETCH_CHAR_AS_MULTIBYTE (from_byte); |
7c7ca3d2 RS |
3247 | /* Some compilers can't handle this inside the switch. */ |
3248 | temp = SYNTAX (c); | |
ccf89641 KH |
3249 | |
3250 | /* Check TEMP here so that if the char has | |
3251 | a syntax-table property which says it is NOT | |
3252 | a string character, it does not end the string. */ | |
3253 | if (nofence && c == state.instring && temp == Sstring) | |
3254 | break; | |
3255 | ||
7c7ca3d2 | 3256 | switch (temp) |
644ea4df RS |
3257 | { |
3258 | case Sstring_fence: | |
3259 | if (!nofence) goto string_end; | |
3260 | break; | |
3261 | case Scharquote: | |
3262 | case Sescape: | |
3263 | INC_FROM; | |
3264 | startquotedinstring: | |
3265 | if (from >= end) goto endquoted; | |
195d1361 | 3266 | } |
644ea4df RS |
3267 | INC_FROM; |
3268 | } | |
195d1361 RS |
3269 | } |
3270 | string_end: | |
8489eb67 RS |
3271 | state.instring = -1; |
3272 | curlevel->prev = curlevel->last; | |
93da5fff | 3273 | INC_FROM; |
195d1361 | 3274 | if (boundary_stop) goto done; |
8489eb67 RS |
3275 | break; |
3276 | ||
3277 | case Smath: | |
240c43e8 SM |
3278 | /* FIXME: We should do something with it. */ |
3279 | break; | |
3280 | default: | |
3281 | /* Ignore whitespace, punctuation, quote, endcomment. */ | |
8489eb67 RS |
3282 | break; |
3283 | } | |
3284 | } | |
3285 | goto done; | |
3286 | ||
3287 | stop: /* Here if stopping before start of sexp. */ | |
93da5fff | 3288 | from = prev_from; /* We have just fetched the char that starts it; */ |
8489eb67 RS |
3289 | goto done; /* but return the position before it. */ |
3290 | ||
3291 | endquoted: | |
3292 | state.quoted = 1; | |
3293 | done: | |
3294 | state.depth = depth; | |
3295 | state.mindepth = mindepth; | |
3296 | state.thislevelstart = curlevel->prev; | |
3297 | state.prevlevelstart | |
3298 | = (curlevel == levelstart) ? -1 : (curlevel - 1)->last; | |
3299 | state.location = from; | |
1a102a58 RS |
3300 | state.levelstarts = Qnil; |
3301 | while (--curlevel >= levelstart) | |
3302 | state.levelstarts = Fcons (make_number (curlevel->last), | |
3303 | state.levelstarts); | |
8489eb67 RS |
3304 | immediate_quit = 0; |
3305 | ||
e5d4f4dc | 3306 | *stateptr = state; |
8489eb67 RS |
3307 | } |
3308 | ||
c81a3712 | 3309 | DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0, |
fdb82f93 PJ |
3310 | doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO. |
3311 | Parsing stops at TO or when certain criteria are met; | |
3312 | point is set to where parsing stops. | |
3313 | If fifth arg OLDSTATE is omitted or nil, | |
3314 | parsing assumes that FROM is the beginning of a function. | |
0e6228bc | 3315 | Value is a list of elements describing final state of parsing: |
fdb82f93 PJ |
3316 | 0. depth in parens. |
3317 | 1. character address of start of innermost containing list; nil if none. | |
3318 | 2. character address of start of last complete sexp terminated. | |
3319 | 3. non-nil if inside a string. | |
3320 | (it is the character that will terminate the string, | |
3321 | or t if the string should be terminated by a generic string delimiter.) | |
7d0393cf | 3322 | 4. nil if outside a comment, t if inside a non-nestable comment, |
fdb82f93 PJ |
3323 | else an integer (the current comment nesting). |
3324 | 5. t if following a quote character. | |
3325 | 6. the minimum paren-depth encountered during this scan. | |
c5683ceb | 3326 | 7. style of comment, if any. |
fdb82f93 PJ |
3327 | 8. character address of start of comment or string; nil if not in one. |
3328 | 9. Intermediate data for continuation of parsing (subject to change). | |
3329 | If third arg TARGETDEPTH is non-nil, parsing stops if the depth | |
3330 | in parentheses becomes equal to TARGETDEPTH. | |
3331 | Fourth arg STOPBEFORE non-nil means stop when come to | |
3332 | any character that starts a sexp. | |
0e6228bc | 3333 | Fifth arg OLDSTATE is a list like what this function returns. |
fdb82f93 | 3334 | It is used to initialize the state of the parse. Elements number 1, 2, 6 |
43726c05 | 3335 | and 8 are ignored. |
fdb82f93 PJ |
3336 | Sixth arg COMMENTSTOP non-nil means stop at the start of a comment. |
3337 | If it is symbol `syntax-table', stop after the start of a comment or a | |
3338 | string, or after end of a comment or a string. */) | |
5842a27b | 3339 | (Lisp_Object from, Lisp_Object to, Lisp_Object targetdepth, Lisp_Object stopbefore, Lisp_Object oldstate, Lisp_Object commentstop) |
8489eb67 RS |
3340 | { |
3341 | struct lisp_parse_state state; | |
3342 | int target; | |
3343 | ||
265a9e55 | 3344 | if (!NILP (targetdepth)) |
8489eb67 | 3345 | { |
b7826503 | 3346 | CHECK_NUMBER (targetdepth); |
8489eb67 RS |
3347 | target = XINT (targetdepth); |
3348 | } | |
3349 | else | |
3350 | target = -100000; /* We won't reach this depth */ | |
3351 | ||
3352 | validate_region (&from, &to); | |
6a140a74 RS |
3353 | scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)), |
3354 | XINT (to), | |
c81a3712 | 3355 | target, !NILP (stopbefore), oldstate, |
7d0393cf | 3356 | (NILP (commentstop) |
195d1361 | 3357 | ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1))); |
8489eb67 RS |
3358 | |
3359 | SET_PT (state.location); | |
7d0393cf | 3360 | |
8489eb67 | 3361 | return Fcons (make_number (state.depth), |
c5683ceb SM |
3362 | Fcons (state.prevlevelstart < 0 |
3363 | ? Qnil : make_number (state.prevlevelstart), | |
3364 | Fcons (state.thislevelstart < 0 | |
3365 | ? Qnil : make_number (state.thislevelstart), | |
7d0393cf JB |
3366 | Fcons (state.instring >= 0 |
3367 | ? (state.instring == ST_STRING_STYLE | |
195d1361 | 3368 | ? Qt : make_number (state.instring)) : Qnil, |
95ff8dfc RS |
3369 | Fcons (state.incomment < 0 ? Qt : |
3370 | (state.incomment == 0 ? Qnil : | |
3371 | make_number (state.incomment)), | |
8489eb67 | 3372 | Fcons (state.quoted ? Qt : Qnil, |
bff37d2a | 3373 | Fcons (make_number (state.mindepth), |
7d0393cf | 3374 | Fcons ((state.comstyle |
bff37d2a | 3375 | ? (state.comstyle == ST_COMMENT_STYLE |
c5683ceb SM |
3376 | ? Qsyntax_table |
3377 | : make_number (state.comstyle)) | |
3378 | : Qnil), | |
0beaf54f DL |
3379 | Fcons (((state.incomment |
3380 | || (state.instring >= 0)) | |
bff37d2a RS |
3381 | ? make_number (state.comstr_start) |
3382 | : Qnil), | |
1a102a58 | 3383 | Fcons (state.levelstarts, Qnil)))))))))); |
8489eb67 RS |
3384 | } |
3385 | \f | |
dfcf069d | 3386 | void |
971de7fb | 3387 | init_syntax_once (void) |
8489eb67 | 3388 | { |
78f9a1f7 | 3389 | register int i, c; |
8ea151b2 | 3390 | Lisp_Object temp; |
8489eb67 | 3391 | |
5ebaddf5 | 3392 | /* This has to be done here, before we call Fmake_char_table. */ |
d67b4f80 | 3393 | Qsyntax_table = intern_c_string ("syntax-table"); |
5ebaddf5 RS |
3394 | staticpro (&Qsyntax_table); |
3395 | ||
d67b4f80 | 3396 | /* Intern_C_String this now in case it isn't already done. |
5ebaddf5 RS |
3397 | Setting this variable twice is harmless. |
3398 | But don't staticpro it here--that is done in alloc.c. */ | |
d67b4f80 | 3399 | Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots"); |
5ebaddf5 | 3400 | |
93da5fff | 3401 | /* Create objects which can be shared among syntax tables. */ |
42ebfa31 | 3402 | Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil); |
93da5fff KH |
3403 | for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++) |
3404 | XVECTOR (Vsyntax_code_object)->contents[i] | |
3405 | = Fcons (make_number (i), Qnil); | |
3406 | ||
5ebaddf5 RS |
3407 | /* Now we are ready to set up this property, so we can |
3408 | create syntax tables. */ | |
3409 | Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0)); | |
3410 | ||
93da5fff | 3411 | temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace]; |
8489eb67 | 3412 | |
5ebaddf5 | 3413 | Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp); |
8489eb67 | 3414 | |
aa7b08b4 RS |
3415 | /* Control characters should not be whitespace. */ |
3416 | temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct]; | |
3417 | for (i = 0; i <= ' ' - 1; i++) | |
3418 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp); | |
3419 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp); | |
3420 | ||
3421 | /* Except that a few really are whitespace. */ | |
3422 | temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace]; | |
3423 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp); | |
3424 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp); | |
3425 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp); | |
3426 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp); | |
3427 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp); | |
3428 | ||
93da5fff | 3429 | temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword]; |
8489eb67 | 3430 | for (i = 'a'; i <= 'z'; i++) |
8ea151b2 | 3431 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp); |
8489eb67 | 3432 | for (i = 'A'; i <= 'Z'; i++) |
8ea151b2 | 3433 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp); |
8489eb67 | 3434 | for (i = '0'; i <= '9'; i++) |
8ea151b2 RS |
3435 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp); |
3436 | ||
3437 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp); | |
3438 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp); | |
3439 | ||
3440 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(', | |
3441 | Fcons (make_number (Sopen), make_number (')'))); | |
3442 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')', | |
3443 | Fcons (make_number (Sclose), make_number ('('))); | |
3444 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[', | |
3445 | Fcons (make_number (Sopen), make_number (']'))); | |
3446 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']', | |
3447 | Fcons (make_number (Sclose), make_number ('['))); | |
3448 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{', | |
3449 | Fcons (make_number (Sopen), make_number ('}'))); | |
3450 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}', | |
3451 | Fcons (make_number (Sclose), make_number ('{'))); | |
3452 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"', | |
3453 | Fcons (make_number ((int) Sstring), Qnil)); | |
3454 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\', | |
3455 | Fcons (make_number ((int) Sescape), Qnil)); | |
3456 | ||
93da5fff | 3457 | temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol]; |
8489eb67 | 3458 | for (i = 0; i < 10; i++) |
78f9a1f7 KH |
3459 | { |
3460 | c = "_-+*/&|<>="[i]; | |
3461 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp); | |
3462 | } | |
8489eb67 | 3463 | |
93da5fff | 3464 | temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct]; |
8489eb67 | 3465 | for (i = 0; i < 12; i++) |
78f9a1f7 KH |
3466 | { |
3467 | c = ".,;:?!#@~^'`"[i]; | |
3468 | SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp); | |
3469 | } | |
bd25db08 KH |
3470 | |
3471 | /* All multibyte characters have syntax `word' by default. */ | |
3472 | temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword]; | |
5c7b02ab | 3473 | char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp); |
8489eb67 RS |
3474 | } |
3475 | ||
dfcf069d | 3476 | void |
971de7fb | 3477 | syms_of_syntax (void) |
8489eb67 | 3478 | { |
d67b4f80 | 3479 | Qsyntax_table_p = intern_c_string ("syntax-table-p"); |
8489eb67 RS |
3480 | staticpro (&Qsyntax_table_p); |
3481 | ||
93da5fff KH |
3482 | staticpro (&Vsyntax_code_object); |
3483 | ||
a2994c46 KS |
3484 | staticpro (&gl_state.object); |
3485 | staticpro (&gl_state.global_code); | |
3486 | staticpro (&gl_state.current_syntax_table); | |
3487 | staticpro (&gl_state.old_prop); | |
3488 | ||
3489 | /* Defined in regex.c */ | |
3490 | staticpro (&re_match_object); | |
3491 | ||
d67b4f80 | 3492 | Qscan_error = intern_c_string ("scan-error"); |
7bf5e9e4 RS |
3493 | staticpro (&Qscan_error); |
3494 | Fput (Qscan_error, Qerror_conditions, | |
d67b4f80 | 3495 | pure_cons (Qscan_error, pure_cons (Qerror, Qnil))); |
7bf5e9e4 | 3496 | Fput (Qscan_error, Qerror_message, |
d67b4f80 | 3497 | make_pure_c_string ("Scan error")); |
7bf5e9e4 | 3498 | |
8489eb67 | 3499 | DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments, |
fdb82f93 | 3500 | doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */); |
8489eb67 | 3501 | |
195d1361 | 3502 | DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties, |
fdb82f93 PJ |
3503 | doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property. |
3504 | Otherwise, that text property is simply ignored. | |
3505 | See the info node `(elisp)Syntax Properties' for a description of the | |
3506 | `syntax-table' property. */); | |
195d1361 | 3507 | |
8489eb67 RS |
3508 | words_include_escapes = 0; |
3509 | DEFVAR_BOOL ("words-include-escapes", &words_include_escapes, | |
fdb82f93 | 3510 | doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */); |
8489eb67 | 3511 | |
bd25db08 | 3512 | DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol, |
fdb82f93 | 3513 | doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */); |
bd25db08 KH |
3514 | multibyte_syntax_as_symbol = 0; |
3515 | ||
f4ed767f GM |
3516 | DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start", |
3517 | &open_paren_in_column_0_is_defun_start, | |
b222e415 | 3518 | doc: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */); |
f4ed767f GM |
3519 | open_paren_in_column_0_is_defun_start = 1; |
3520 | ||
8f924df7 KH |
3521 | |
3522 | DEFVAR_LISP ("find-word-boundary-function-table", | |
3523 | &Vfind_word_boundary_function_table, | |
869bb237 | 3524 | doc: /* |
8f924df7 | 3525 | Char table of functions to search for the word boundary. |
869bb237 KH |
3526 | Each function is called with two arguments; POS and LIMIT. |
3527 | POS and LIMIT are character positions in the current buffer. | |
3528 | ||
3529 | If POS is less than LIMIT, POS is at the first character of a word, | |
3530 | and the return value of a function is a position after the last | |
3531 | character of that word. | |
3532 | ||
3533 | If POS is not less than LIMIT, POS is at the last character of a word, | |
3534 | and the return value of a function is a position at the first | |
3535 | character of that word. | |
3536 | ||
3537 | In both cases, LIMIT bounds the search. */); | |
8f924df7 | 3538 | Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil); |
869bb237 | 3539 | |
8489eb67 RS |
3540 | defsubr (&Ssyntax_table_p); |
3541 | defsubr (&Ssyntax_table); | |
3542 | defsubr (&Sstandard_syntax_table); | |
3543 | defsubr (&Scopy_syntax_table); | |
3544 | defsubr (&Sset_syntax_table); | |
3545 | defsubr (&Schar_syntax); | |
beefa22e | 3546 | defsubr (&Smatching_paren); |
c65adb44 | 3547 | defsubr (&Sstring_to_syntax); |
8489eb67 | 3548 | defsubr (&Smodify_syntax_entry); |
62abe9cb | 3549 | defsubr (&Sinternal_describe_syntax_value); |
8489eb67 RS |
3550 | |
3551 | defsubr (&Sforward_word); | |
3552 | ||
195d1361 RS |
3553 | defsubr (&Sskip_chars_forward); |
3554 | defsubr (&Sskip_chars_backward); | |
3555 | defsubr (&Sskip_syntax_forward); | |
3556 | defsubr (&Sskip_syntax_backward); | |
3557 | ||
b3cfe0c8 | 3558 | defsubr (&Sforward_comment); |
8489eb67 RS |
3559 | defsubr (&Sscan_lists); |
3560 | defsubr (&Sscan_sexps); | |
3561 | defsubr (&Sbackward_prefix_chars); | |
3562 | defsubr (&Sparse_partial_sexp); | |
3563 | } | |
839966f3 KH |
3564 | |
3565 | /* arch-tag: 3e297b9f-088e-4b64-8f4c-fb0b3443e412 | |
3566 | (do not change this comment) */ |