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