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