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