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