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