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