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