(Optional Mode Line): Document `size-indication-mode'.
[emacs.git] / src / syntax.c
blob79df112d26510dff79e5de73627c4a882caf2912
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.
4 This file is part of GNU Emacs.
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.
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.
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. */
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"
30 /* Make syntax table lookup grant data in gl_state. */
31 #define SYNTAX_ENTRY_VIA_PROPERTY
33 #include "syntax.h"
34 #include "intervals.h"
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. */
40 #define ST_COMMENT_STYLE (256 + 1)
41 #define ST_STRING_STYLE (256 + 2)
42 #include "category.h"
44 Lisp_Object Qsyntax_table_p, Qsyntax_table, Qscan_error;
46 int words_include_escapes;
47 int parse_sexp_lookup_properties;
49 /* Nonzero means `scan-sexps' treat all multibyte characters as symbol. */
50 int multibyte_syntax_as_symbol;
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;
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. */
61 int open_paren_in_column_0_is_defun_start;
63 /* This is the internal form of the parse state used in parse-partial-sexp. */
65 struct lisp_parse_state
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). */
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. */
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;
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));
107 struct gl_state_s gl_state; /* Global state of syntax parser. */
109 INTERVAL interval_of ();
110 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
111 to scan to property-change. */
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.
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. */
126 void
127 update_syntax_table (charpos, count, init, object)
128 int charpos, count, init;
129 Lisp_Object object;
131 Lisp_Object tmp_table;
132 int cnt = 0, invalidate = 1;
133 INTERVAL i, oldi;
135 if (init)
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))
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);
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;
164 oldi = i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
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. */
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)
178 invalidate = 0;
179 gl_state.forward_i = i;
180 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
183 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
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)
191 invalidate = 0;
192 gl_state.backward_i = i;
193 gl_state.b_property = i->position - gl_state.offset;
197 update:
198 tmp_table = textget (i->plist, Qsyntax_table);
200 if (invalidate)
201 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
203 if (invalidate) /* Did not get to adjacent interval. */
204 { /* with the same table => */
205 /* invalidate the old range. */
206 if (count > 0)
208 gl_state.backward_i = i;
209 gl_state.b_property = i->position - gl_state.offset;
211 else
213 gl_state.forward_i = i;
214 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
218 if (!EQ (tmp_table, gl_state.old_prop))
220 gl_state.current_syntax_table = tmp_table;
221 gl_state.old_prop = tmp_table;
222 if (EQ (Fsyntax_table_p (tmp_table), Qt))
224 gl_state.use_global = 0;
226 else if (CONSP (tmp_table))
228 gl_state.use_global = 1;
229 gl_state.global_code = tmp_table;
231 else
233 gl_state.use_global = 0;
234 gl_state.current_syntax_table = current_buffer->syntax_table;
238 while (!NULL_INTERVAL_P (i))
240 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
242 if (count > 0)
244 gl_state.e_property = i->position - gl_state.offset;
245 gl_state.forward_i = i;
247 else
249 gl_state.b_property = i->position + LENGTH (i) - gl_state.offset;
250 gl_state.backward_i = i;
252 return;
254 else if (cnt == INTERVALS_AT_ONCE)
256 if (count > 0)
258 gl_state.e_property = i->position + LENGTH (i) - gl_state.offset;
259 gl_state.forward_i = i;
261 else
263 gl_state.b_property = i->position - gl_state.offset;
264 gl_state.backward_i = i;
266 return;
268 cnt++;
269 i = count > 0 ? next_interval (i) : previous_interval (i);
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;
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. */
282 static int
283 char_quoted (charpos, bytepos)
284 register int charpos, bytepos;
286 register enum syntaxcode code;
287 register int beg = BEGV;
288 register int quoted = 0;
289 int orig = charpos;
291 DEC_BOTH (charpos, bytepos);
293 while (bytepos >= beg)
295 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
296 code = SYNTAX (FETCH_CHAR (bytepos));
297 if (! (code == Scharquote || code == Sescape))
298 break;
300 DEC_BOTH (charpos, bytepos);
301 quoted = !quoted;
304 UPDATE_SYNTAX_TABLE (orig);
305 return quoted;
308 /* Return the bytepos one character after BYTEPOS.
309 We assume that BYTEPOS is not at the end of the buffer. */
311 INLINE int
312 inc_bytepos (bytepos)
313 int bytepos;
315 if (NILP (current_buffer->enable_multibyte_characters))
316 return bytepos + 1;
318 INC_POS (bytepos);
319 return bytepos;
322 /* Return the bytepos one character before BYTEPOS.
323 We assume that BYTEPOS is not at the start of the buffer. */
325 INLINE int
326 dec_bytepos (bytepos)
327 int bytepos;
329 if (NILP (current_buffer->enable_multibyte_characters))
330 return bytepos - 1;
332 DEC_POS (bytepos);
333 return bytepos;
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.
339 When open_paren_in_column_0_is_defun_start is nonzero,
340 only the beginning of the buffer is treated as a defun-start.
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.
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. */
350 static int
351 find_defun_start (pos, pos_byte)
352 int pos, pos_byte;
354 int opoint = PT, opoint_byte = PT_BYTE;
356 if (!open_paren_in_column_0_is_defun_start)
358 find_start_value_byte = BEGV_BYTE;
359 return BEGV;
362 /* Use previous finding, if it's valid and applies to this inquiry. */
363 if (current_buffer == find_start_buffer
364 /* Reuse the defun-start even if POS is a little farther on.
365 POS might be in the next defun, but that's ok.
366 Our value may not be the best possible, but will still be usable. */
367 && pos <= find_start_pos + 1000
368 && pos >= find_start_value
369 && BEGV == find_start_begv
370 && MODIFF == find_start_modiff)
371 return find_start_value;
373 /* Back up to start of line. */
374 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
376 /* We optimize syntax-table lookup for rare updates. Thus we accept
377 only those `^\s(' which are good in global _and_ text-property
378 syntax-tables. */
379 gl_state.current_syntax_table = current_buffer->syntax_table;
380 gl_state.use_global = 0;
381 while (PT > BEGV)
383 /* Open-paren at start of line means we may have found our
384 defun-start. */
385 if (SYNTAX (FETCH_CHAR (PT_BYTE)) == Sopen)
387 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
388 if (SYNTAX (FETCH_CHAR (PT_BYTE)) == Sopen)
389 break;
390 /* Now fallback to the default value. */
391 gl_state.current_syntax_table = current_buffer->syntax_table;
392 gl_state.use_global = 0;
394 /* Move to beg of previous line. */
395 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
398 /* Record what we found, for the next try. */
399 find_start_value = PT;
400 find_start_value_byte = PT_BYTE;
401 find_start_buffer = current_buffer;
402 find_start_modiff = MODIFF;
403 find_start_begv = BEGV;
404 find_start_pos = pos;
406 TEMP_SET_PT_BOTH (opoint, opoint_byte);
408 return find_start_value;
411 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
413 static int
414 prev_char_comend_first (pos, pos_byte)
415 int pos, pos_byte;
417 int c, val;
419 DEC_BOTH (pos, pos_byte);
420 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
421 c = FETCH_CHAR (pos_byte);
422 val = SYNTAX_COMEND_FIRST (c);
423 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
424 return val;
427 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
429 /* static int
430 * prev_char_comstart_first (pos, pos_byte)
431 * int pos, pos_byte;
433 * int c, val;
435 * DEC_BOTH (pos, pos_byte);
436 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
437 * c = FETCH_CHAR (pos_byte);
438 * val = SYNTAX_COMSTART_FIRST (c);
439 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
440 * return val;
441 * } */
443 /* Checks whether charpos FROM is at the end of a comment.
444 FROM_BYTE is the bytepos corresponding to FROM.
445 Do not move back before STOP.
447 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
448 return -1 otherwise.
450 If successful, store the charpos of the comment's beginning
451 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
453 Global syntax data remains valid for backward search starting at
454 the returned value (or at FROM, if the search was not successful). */
456 static int
457 back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_ptr)
458 int from, from_byte, stop;
459 int comnested, comstyle;
460 int *charpos_ptr, *bytepos_ptr;
462 /* Look back, counting the parity of string-quotes,
463 and recording the comment-starters seen.
464 When we reach a safe place, assume that's not in a string;
465 then step the main scan to the earliest comment-starter seen
466 an even number of string quotes away from the safe place.
468 OFROM[I] is position of the earliest comment-starter seen
469 which is I+2X quotes from the comment-end.
470 PARITY is current parity of quotes from the comment end. */
471 int string_style = -1; /* Presumed outside of any string. */
472 int string_lossage = 0;
473 /* Not a real lossage: indicates that we have passed a matching comment
474 starter plus a non-matching comment-ender, meaning that any matching
475 comment-starter we might see later could be a false positive (hidden
476 inside another comment).
477 Test case: { a (* b } c (* d *) */
478 int comment_lossage = 0;
479 int comment_end = from;
480 int comment_end_byte = from_byte;
481 int comstart_pos = 0;
482 int comstart_byte;
483 /* Place where the containing defun starts,
484 or 0 if we didn't come across it yet. */
485 int defun_start = 0;
486 int defun_start_byte = 0;
487 register enum syntaxcode code;
488 int nesting = 1; /* current comment nesting */
489 int c;
490 int syntax = 0;
492 /* FIXME: A }} comment-ender style leads to incorrect behavior
493 in the case of {{ c }}} because we ignore the last two chars which are
494 assumed to be comment-enders although they aren't. */
496 /* At beginning of range to scan, we're outside of strings;
497 that determines quote parity to the comment-end. */
498 while (from != stop)
500 int temp_byte, prev_syntax;
501 int com2start, com2end;
503 /* Move back and examine a character. */
504 DEC_BOTH (from, from_byte);
505 UPDATE_SYNTAX_TABLE_BACKWARD (from);
507 prev_syntax = syntax;
508 c = FETCH_CHAR (from_byte);
509 syntax = SYNTAX_WITH_FLAGS (c);
510 code = SYNTAX (c);
512 /* Check for 2-char comment markers. */
513 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
514 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
515 && comstyle == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax)
516 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
517 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
518 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
519 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
521 /* Nasty cases with overlapping 2-char comment markers:
522 - snmp-mode: -- c -- foo -- c --
523 --- c --
524 ------ c --
525 - c-mode: *||*
526 |* *|* *|
527 |*| |* |*|
528 /// */
530 /* If a 2-char comment sequence partly overlaps with another,
531 we don't try to be clever. */
532 if (from > stop && (com2end || com2start))
534 int next = from, next_byte = from_byte, next_c, next_syntax;
535 DEC_BOTH (next, next_byte);
536 UPDATE_SYNTAX_TABLE_BACKWARD (next);
537 next_c = FETCH_CHAR (next_byte);
538 next_syntax = SYNTAX_WITH_FLAGS (next_c);
539 if (((com2start || comnested)
540 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
541 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
542 || ((com2end || comnested)
543 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
544 && comstyle == SYNTAX_FLAGS_COMMENT_STYLE (syntax)
545 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
546 goto lossage;
547 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
550 if (com2start && comstart_pos == 0)
551 /* We're looking at a comment starter. But it might be a comment
552 ender as well (see snmp-mode). The first time we see one, we
553 need to consider it as a comment starter,
554 and the subsequent times as a comment ender. */
555 com2end = 0;
557 /* Turn a 2-char comment sequences into the appropriate syntax. */
558 if (com2end)
559 code = Sendcomment;
560 else if (com2start)
561 code = Scomment;
562 /* Ignore comment starters of a different style. */
563 else if (code == Scomment
564 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax)
565 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
566 continue;
568 /* Ignore escaped characters, except comment-enders. */
569 if (code != Sendcomment && char_quoted (from, from_byte))
570 continue;
572 switch (code)
574 case Sstring_fence:
575 case Scomment_fence:
576 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
577 case Sstring:
578 /* Track parity of quotes. */
579 if (string_style == -1)
580 /* Entering a string. */
581 string_style = c;
582 else if (string_style == c)
583 /* Leaving the string. */
584 string_style = -1;
585 else
586 /* If we have two kinds of string delimiters.
587 There's no way to grok this scanning backwards. */
588 string_lossage = 1;
589 break;
591 case Scomment:
592 /* We've already checked that it is the relevant comstyle. */
593 if (string_style != -1 || comment_lossage || string_lossage)
594 /* There are odd string quotes involved, so let's be careful.
595 Test case in Pascal: " { " a { " } */
596 goto lossage;
598 if (!comnested)
600 /* Record best comment-starter so far. */
601 comstart_pos = from;
602 comstart_byte = from_byte;
604 else if (--nesting <= 0)
605 /* nested comments have to be balanced, so we don't need to
606 keep looking for earlier ones. We use here the same (slightly
607 incorrect) reasoning as below: since it is followed by uniform
608 paired string quotes, this comment-start has to be outside of
609 strings, else the comment-end itself would be inside a string. */
610 goto done;
611 break;
613 case Sendcomment:
614 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax) == comstyle
615 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
616 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
617 /* This is the same style of comment ender as ours. */
619 if (comnested)
620 nesting++;
621 else
622 /* Anything before that can't count because it would match
623 this comment-ender rather than ours. */
624 from = stop; /* Break out of the loop. */
626 else if (comstart_pos != 0 || c != '\n')
627 /* We're mixing comment styles here, so we'd better be careful.
628 The (comstart_pos != 0 || c != '\n') check is not quite correct
629 (we should just always set comment_lossage), but removing it
630 would imply that any multiline comment in C would go through
631 lossage, which seems overkill.
632 The failure should only happen in the rare cases such as
633 { (* } *) */
634 comment_lossage = 1;
635 break;
637 case Sopen:
638 /* Assume a defun-start point is outside of strings. */
639 if (open_paren_in_column_0_is_defun_start
640 && (from == stop
641 || (temp_byte = dec_bytepos (from_byte),
642 FETCH_CHAR (temp_byte) == '\n')))
644 defun_start = from;
645 defun_start_byte = from_byte;
646 from = stop; /* Break out of the loop. */
648 break;
650 default:
651 break;
655 if (comstart_pos == 0)
657 from = comment_end;
658 from_byte = comment_end_byte;
659 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
661 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
662 or `done'), then we've found the beginning of the non-nested comment. */
663 else if (1) /* !comnested */
665 from = comstart_pos;
666 from_byte = comstart_byte;
667 /* Globals are correct now. */
669 else
671 struct lisp_parse_state state;
672 lossage:
673 /* We had two kinds of string delimiters mixed up
674 together. Decode this going forwards.
675 Scan fwd from a known safe place (beginning-of-defun)
676 to the one in question; this records where we
677 last passed a comment starter. */
678 /* If we did not already find the defun start, find it now. */
679 if (defun_start == 0)
681 defun_start = find_defun_start (comment_end, comment_end_byte);
682 defun_start_byte = find_start_value_byte;
686 scan_sexps_forward (&state,
687 defun_start, defun_start_byte,
688 comment_end, -10000, 0, Qnil, 0);
689 defun_start = comment_end;
690 if (state.incomment == (comnested ? 1 : -1)
691 && state.comstyle == comstyle)
692 from = state.comstr_start;
693 else
695 from = comment_end;
696 if (state.incomment)
697 /* If comment_end is inside some other comment, maybe ours
698 is nested, so we need to try again from within the
699 surrounding comment. Example: { a (* " *) */
701 /* FIXME: We should advance by one or two chars. */
702 defun_start = state.comstr_start + 2;
703 defun_start_byte = CHAR_TO_BYTE (defun_start);
706 } while (defun_start < comment_end);
708 from_byte = CHAR_TO_BYTE (from);
709 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
712 done:
713 *charpos_ptr = from;
714 *bytepos_ptr = from_byte;
716 return (from == comment_end) ? -1 : from;
719 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
720 doc: /* Return t if OBJECT is a syntax table.
721 Currently, any char-table counts as a syntax table. */)
722 (object)
723 Lisp_Object object;
725 if (CHAR_TABLE_P (object)
726 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
727 return Qt;
728 return Qnil;
731 static void
732 check_syntax_table (obj)
733 Lisp_Object obj;
735 if (!(CHAR_TABLE_P (obj)
736 && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table)))
737 wrong_type_argument (Qsyntax_table_p, obj);
740 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
741 doc: /* Return the current syntax table.
742 This is the one specified by the current buffer. */)
745 return current_buffer->syntax_table;
748 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
749 Sstandard_syntax_table, 0, 0, 0,
750 doc: /* Return the standard syntax table.
751 This is the one used for new buffers. */)
754 return Vstandard_syntax_table;
757 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
758 doc: /* Construct a new syntax table and return it.
759 It is a copy of the TABLE, which defaults to the standard syntax table. */)
760 (table)
761 Lisp_Object table;
763 Lisp_Object copy;
765 if (!NILP (table))
766 check_syntax_table (table);
767 else
768 table = Vstandard_syntax_table;
770 copy = Fcopy_sequence (table);
772 /* Only the standard syntax table should have a default element.
773 Other syntax tables should inherit from parents instead. */
774 XCHAR_TABLE (copy)->defalt = Qnil;
776 /* Copied syntax tables should all have parents.
777 If we copied one with no parent, such as the standard syntax table,
778 use the standard syntax table as the copy's parent. */
779 if (NILP (XCHAR_TABLE (copy)->parent))
780 Fset_char_table_parent (copy, Vstandard_syntax_table);
781 return copy;
784 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
785 doc: /* Select a new syntax table for the current buffer.
786 One argument, a syntax table. */)
787 (table)
788 Lisp_Object table;
790 int idx;
791 check_syntax_table (table);
792 current_buffer->syntax_table = table;
793 /* Indicate that this buffer now has a specified syntax table. */
794 idx = PER_BUFFER_VAR_IDX (syntax_table);
795 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
796 return table;
799 /* Convert a letter which signifies a syntax code
800 into the code it signifies.
801 This is used by modify-syntax-entry, and other things. */
803 unsigned char syntax_spec_code[0400] =
804 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
805 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
806 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
807 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
808 (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377,
809 (char) Smath, 0377, 0377, (char) Squote,
810 (char) Sopen, (char) Sclose, 0377, 0377,
811 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
812 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
813 0377, 0377, 0377, 0377,
814 (char) Scomment, 0377, (char) Sendcomment, 0377,
815 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
816 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
817 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
818 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
819 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
820 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
821 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
822 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377
825 /* Indexed by syntax code, give the letter that describes it. */
827 char syntax_code_spec[16] =
829 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
830 '!', '|'
833 /* Indexed by syntax code, give the object (cons of syntax code and
834 nil) to be stored in syntax table. Since these objects can be
835 shared among syntax tables, we generate them in advance. By
836 sharing objects, the function `describe-syntax' can give a more
837 compact listing. */
838 static Lisp_Object Vsyntax_code_object;
841 /* Look up the value for CHARACTER in syntax table TABLE's parent
842 and its parents. SYNTAX_ENTRY calls this, when TABLE itself has nil
843 for CHARACTER. It's actually used only when not compiled with GCC. */
845 Lisp_Object
846 syntax_parent_lookup (table, character)
847 Lisp_Object table;
848 int character;
850 Lisp_Object value;
852 while (1)
854 table = XCHAR_TABLE (table)->parent;
855 if (NILP (table))
856 return Qnil;
858 value = XCHAR_TABLE (table)->contents[character];
859 if (!NILP (value))
860 return value;
864 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
865 doc: /* Return the syntax code of CHARACTER, described by a character.
866 For example, if CHARACTER is a word constituent,
867 the character `w' is returned.
868 The characters that correspond to various syntax codes
869 are listed in the documentation of `modify-syntax-entry'. */)
870 (character)
871 Lisp_Object character;
873 int char_int;
874 gl_state.current_syntax_table = current_buffer->syntax_table;
876 gl_state.use_global = 0;
877 CHECK_NUMBER (character);
878 char_int = XINT (character);
879 return make_number (syntax_code_spec[(int) SYNTAX (char_int)]);
882 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
883 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
884 (character)
885 Lisp_Object character;
887 int char_int, code;
888 gl_state.current_syntax_table = current_buffer->syntax_table;
889 gl_state.use_global = 0;
890 CHECK_NUMBER (character);
891 char_int = XINT (character);
892 code = SYNTAX (char_int);
893 if (code == Sopen || code == Sclose)
894 return SYNTAX_MATCH (char_int);
895 return Qnil;
898 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
899 doc: /* Convert a syntax specification STRING into syntax cell form.
900 STRING should be a string as it is allowed as argument of
901 `modify-syntax-entry'. Value is the equivalent cons cell
902 (CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
903 text property. */)
904 (string)
905 Lisp_Object string;
907 register const unsigned char *p;
908 register enum syntaxcode code;
909 int val;
910 Lisp_Object match;
912 CHECK_STRING (string);
914 p = SDATA (string);
915 code = (enum syntaxcode) syntax_spec_code[*p++];
916 if (((int) code & 0377) == 0377)
917 error ("invalid syntax description letter: %c", p[-1]);
919 if (code == Sinherit)
920 return Qnil;
922 if (*p)
924 int len;
925 int character = (STRING_CHAR_AND_LENGTH
926 (p, SBYTES (string) - 1, len));
927 XSETINT (match, character);
928 if (XFASTINT (match) == ' ')
929 match = Qnil;
930 p += len;
932 else
933 match = Qnil;
935 val = (int) code;
936 while (*p)
937 switch (*p++)
939 case '1':
940 val |= 1 << 16;
941 break;
943 case '2':
944 val |= 1 << 17;
945 break;
947 case '3':
948 val |= 1 << 18;
949 break;
951 case '4':
952 val |= 1 << 19;
953 break;
955 case 'p':
956 val |= 1 << 20;
957 break;
959 case 'b':
960 val |= 1 << 21;
961 break;
963 case 'n':
964 val |= 1 << 22;
965 break;
968 if (val < XVECTOR (Vsyntax_code_object)->size && NILP (match))
969 return XVECTOR (Vsyntax_code_object)->contents[val];
970 else
971 /* Since we can't use a shared object, let's make a new one. */
972 return Fcons (make_number (val), match);
975 /* I really don't know why this is interactive
976 help-form should at least be made useful whilst reading the second arg. */
977 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
978 "cSet syntax for character: \nsSet syntax for %s to: ",
979 doc: /* Set syntax for character CHAR according to string NEWENTRY.
980 The syntax is changed only for table SYNTAX_TABLE, which defaults to
981 the current buffer's syntax table.
982 The first character of NEWENTRY should be one of the following:
983 Space or - whitespace syntax. w word constituent.
984 _ symbol constituent. . punctuation.
985 ( open-parenthesis. ) close-parenthesis.
986 " string quote. \\ escape.
987 $ paired delimiter. ' expression quote or prefix operator.
988 < comment starter. > comment ender.
989 / character-quote. @ inherit from `standard-syntax-table'.
990 | generic string fence. ! generic comment fence.
992 Only single-character comment start and end sequences are represented thus.
993 Two-character sequences are represented as described below.
994 The second character of NEWENTRY is the matching parenthesis,
995 used only if the first character is `(' or `)'.
996 Any additional characters are flags.
997 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
998 1 means CHAR is the start of a two-char comment start sequence.
999 2 means CHAR is the second character of such a sequence.
1000 3 means CHAR is the start of a two-char comment end sequence.
1001 4 means CHAR is the second character of such a sequence.
1003 There can be up to two orthogonal comment sequences. This is to support
1004 language modes such as C++. By default, all comment sequences are of style
1005 a, but you can set the comment sequence style to b (on the second character
1006 of a comment-start, or the first character of a comment-end sequence) using
1007 this flag:
1008 b means CHAR is part of comment sequence b.
1009 n means CHAR is part of a nestable comment sequence.
1011 p means CHAR is a prefix character for `backward-prefix-chars';
1012 such characters are treated as whitespace when they occur
1013 between expressions.
1014 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1015 (c, newentry, syntax_table)
1016 Lisp_Object c, newentry, syntax_table;
1018 CHECK_NUMBER (c);
1020 if (NILP (syntax_table))
1021 syntax_table = current_buffer->syntax_table;
1022 else
1023 check_syntax_table (syntax_table);
1025 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), Fstring_to_syntax (newentry));
1026 return Qnil;
1029 /* Dump syntax table to buffer in human-readable format */
1031 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1032 Sinternal_describe_syntax_value, 1, 1, 0,
1033 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1034 (syntax)
1035 Lisp_Object syntax;
1037 register enum syntaxcode code;
1038 char desc, start1, start2, end1, end2, prefix, comstyle, comnested;
1039 char str[2];
1040 Lisp_Object first, match_lisp, value = syntax;
1042 if (NILP (value))
1044 insert_string ("default");
1045 return syntax;
1048 if (CHAR_TABLE_P (value))
1050 insert_string ("deeper char-table ...");
1051 return syntax;
1054 if (!CONSP (value))
1056 insert_string ("invalid");
1057 return syntax;
1060 first = XCAR (value);
1061 match_lisp = XCDR (value);
1063 if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp)))
1065 insert_string ("invalid");
1066 return syntax;
1069 code = (enum syntaxcode) (XINT (first) & 0377);
1070 start1 = (XINT (first) >> 16) & 1;
1071 start2 = (XINT (first) >> 17) & 1;
1072 end1 = (XINT (first) >> 18) & 1;
1073 end2 = (XINT (first) >> 19) & 1;
1074 prefix = (XINT (first) >> 20) & 1;
1075 comstyle = (XINT (first) >> 21) & 1;
1076 comnested = (XINT (first) >> 22) & 1;
1078 if ((int) code < 0 || (int) code >= (int) Smax)
1080 insert_string ("invalid");
1081 return syntax;
1083 desc = syntax_code_spec[(int) code];
1085 str[0] = desc, str[1] = 0;
1086 insert (str, 1);
1088 if (NILP (match_lisp))
1089 insert (" ", 1);
1090 else
1091 insert_char (XINT (match_lisp));
1093 if (start1)
1094 insert ("1", 1);
1095 if (start2)
1096 insert ("2", 1);
1098 if (end1)
1099 insert ("3", 1);
1100 if (end2)
1101 insert ("4", 1);
1103 if (prefix)
1104 insert ("p", 1);
1105 if (comstyle)
1106 insert ("b", 1);
1107 if (comnested)
1108 insert ("n", 1);
1110 insert_string ("\twhich means: ");
1112 switch (SWITCH_ENUM_CAST (code))
1114 case Swhitespace:
1115 insert_string ("whitespace"); break;
1116 case Spunct:
1117 insert_string ("punctuation"); break;
1118 case Sword:
1119 insert_string ("word"); break;
1120 case Ssymbol:
1121 insert_string ("symbol"); break;
1122 case Sopen:
1123 insert_string ("open"); break;
1124 case Sclose:
1125 insert_string ("close"); break;
1126 case Squote:
1127 insert_string ("prefix"); break;
1128 case Sstring:
1129 insert_string ("string"); break;
1130 case Smath:
1131 insert_string ("math"); break;
1132 case Sescape:
1133 insert_string ("escape"); break;
1134 case Scharquote:
1135 insert_string ("charquote"); break;
1136 case Scomment:
1137 insert_string ("comment"); break;
1138 case Sendcomment:
1139 insert_string ("endcomment"); break;
1140 case Sinherit:
1141 insert_string ("inherit"); break;
1142 case Scomment_fence:
1143 insert_string ("comment fence"); break;
1144 case Sstring_fence:
1145 insert_string ("string fence"); break;
1146 default:
1147 insert_string ("invalid");
1148 return syntax;
1151 if (!NILP (match_lisp))
1153 insert_string (", matches ");
1154 insert_char (XINT (match_lisp));
1157 if (start1)
1158 insert_string (",\n\t is the first character of a comment-start sequence");
1159 if (start2)
1160 insert_string (",\n\t is the second character of a comment-start sequence");
1162 if (end1)
1163 insert_string (",\n\t is the first character of a comment-end sequence");
1164 if (end2)
1165 insert_string (",\n\t is the second character of a comment-end sequence");
1166 if (comstyle)
1167 insert_string (" (comment style b)");
1168 if (comnested)
1169 insert_string (" (nestable)");
1171 if (prefix)
1172 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1174 return syntax;
1177 int parse_sexp_ignore_comments;
1179 /* Return the position across COUNT words from FROM.
1180 If that many words cannot be found before the end of the buffer, return 0.
1181 COUNT negative means scan backward and stop at word beginning. */
1184 scan_words (from, count)
1185 register int from, count;
1187 register int beg = BEGV;
1188 register int end = ZV;
1189 register int from_byte = CHAR_TO_BYTE (from);
1190 register enum syntaxcode code;
1191 int ch0, ch1;
1193 immediate_quit = 1;
1194 QUIT;
1196 SETUP_SYNTAX_TABLE (from, count);
1198 while (count > 0)
1200 while (1)
1202 if (from == end)
1204 immediate_quit = 0;
1205 return 0;
1207 UPDATE_SYNTAX_TABLE_FORWARD (from);
1208 ch0 = FETCH_CHAR (from_byte);
1209 code = SYNTAX (ch0);
1210 INC_BOTH (from, from_byte);
1211 if (words_include_escapes
1212 && (code == Sescape || code == Scharquote))
1213 break;
1214 if (code == Sword)
1215 break;
1217 /* Now CH0 is a character which begins a word and FROM is the
1218 position of the next character. */
1219 while (1)
1221 if (from == end) break;
1222 UPDATE_SYNTAX_TABLE_FORWARD (from);
1223 ch1 = FETCH_CHAR (from_byte);
1224 code = SYNTAX (ch1);
1225 if (!(words_include_escapes
1226 && (code == Sescape || code == Scharquote)))
1227 if (code != Sword || WORD_BOUNDARY_P (ch0, ch1))
1228 break;
1229 INC_BOTH (from, from_byte);
1230 ch0 = ch1;
1232 count--;
1234 while (count < 0)
1236 while (1)
1238 if (from == beg)
1240 immediate_quit = 0;
1241 return 0;
1243 DEC_BOTH (from, from_byte);
1244 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1245 ch1 = FETCH_CHAR (from_byte);
1246 code = SYNTAX (ch1);
1247 if (words_include_escapes
1248 && (code == Sescape || code == Scharquote))
1249 break;
1250 if (code == Sword)
1251 break;
1253 /* Now CH1 is a character which ends a word and FROM is the
1254 position of it. */
1255 while (1)
1257 int temp_byte;
1259 if (from == beg)
1260 break;
1261 temp_byte = dec_bytepos (from_byte);
1262 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1263 ch0 = FETCH_CHAR (temp_byte);
1264 code = SYNTAX (ch0);
1265 if (!(words_include_escapes
1266 && (code == Sescape || code == Scharquote)))
1267 if (code != Sword || WORD_BOUNDARY_P (ch0, ch1))
1268 break;
1269 DEC_BOTH (from, from_byte);
1270 ch1 = ch0;
1272 count++;
1275 immediate_quit = 0;
1277 return from;
1280 DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "p",
1281 doc: /* Move point forward ARG words (backward if ARG is negative).
1282 Normally returns t.
1283 If an edge of the buffer or a field boundary is reached, point is left there
1284 and the function returns nil. Field boundaries are not noticed if
1285 `inhibit-field-text-motion' is non-nil. */)
1286 (arg)
1287 Lisp_Object arg;
1289 int orig_val, val;
1291 if (NILP (arg))
1292 XSETFASTINT (arg, 1);
1293 else
1294 CHECK_NUMBER (arg);
1296 val = orig_val = scan_words (PT, XINT (arg));
1297 if (! orig_val)
1298 val = XINT (arg) > 0 ? ZV : BEGV;
1300 /* Avoid jumping out of an input field. */
1301 val = XFASTINT (Fconstrain_to_field (make_number (val), make_number (PT),
1302 Qt, Qnil, Qnil));
1304 SET_PT (val);
1305 return val == orig_val ? Qt : Qnil;
1308 Lisp_Object skip_chars ();
1310 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1311 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1312 STRING is like the inside of a `[...]' in a regular expression
1313 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1314 (but not as the end of a range; quoting is never needed there).
1315 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1316 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1317 Returns the distance traveled, either zero or positive.
1318 Note that char classes, e.g. `[:alpha:]', are not currently supported;
1319 they will be treated as literals. */)
1320 (string, lim)
1321 Lisp_Object string, lim;
1323 return skip_chars (1, 0, string, lim);
1326 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1327 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1328 See `skip-chars-forward' for details.
1329 Returns the distance traveled, either zero or negative. */)
1330 (string, lim)
1331 Lisp_Object string, lim;
1333 return skip_chars (0, 0, string, lim);
1336 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1337 doc: /* Move point forward across chars in specified syntax classes.
1338 SYNTAX is a string of syntax code characters.
1339 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1340 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1341 This function returns the distance traveled, either zero or positive. */)
1342 (syntax, lim)
1343 Lisp_Object syntax, lim;
1345 return skip_chars (1, 1, syntax, lim);
1348 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1349 doc: /* Move point backward across chars in specified syntax classes.
1350 SYNTAX is a string of syntax code characters.
1351 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1352 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1353 This function returns the distance traveled, either zero or negative. */)
1354 (syntax, lim)
1355 Lisp_Object syntax, lim;
1357 return skip_chars (0, 1, syntax, lim);
1360 static Lisp_Object
1361 skip_chars (forwardp, syntaxp, string, lim)
1362 int forwardp, syntaxp;
1363 Lisp_Object string, lim;
1365 register unsigned int c;
1366 unsigned char fastmap[0400];
1367 /* If SYNTAXP is 0, STRING may contain multi-byte form of characters
1368 of which codes don't fit in FASTMAP. In that case, set the
1369 ranges of characters in CHAR_RANGES. */
1370 int *char_ranges;
1371 int n_char_ranges = 0;
1372 int negate = 0;
1373 register int i, i_byte;
1374 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
1375 int string_multibyte;
1376 int size_byte;
1377 const unsigned char *str;
1378 int len;
1380 CHECK_STRING (string);
1381 char_ranges = (int *) alloca (SCHARS (string) * (sizeof (int)) * 2);
1382 string_multibyte = STRING_MULTIBYTE (string);
1383 str = SDATA (string);
1384 size_byte = SBYTES (string);
1386 /* Adjust the multibyteness of the string to that of the buffer. */
1387 if (multibyte != string_multibyte)
1389 int nbytes;
1391 if (multibyte)
1392 nbytes = count_size_as_multibyte (SDATA (string),
1393 SCHARS (string));
1394 else
1395 nbytes = SCHARS (string);
1396 if (nbytes != size_byte)
1398 unsigned char *tmp = (unsigned char *) alloca (nbytes);
1399 copy_text (SDATA (string), tmp, size_byte,
1400 string_multibyte, multibyte);
1401 size_byte = nbytes;
1402 str = tmp;
1406 if (NILP (lim))
1407 XSETINT (lim, forwardp ? ZV : BEGV);
1408 else
1409 CHECK_NUMBER_COERCE_MARKER (lim);
1411 /* In any case, don't allow scan outside bounds of buffer. */
1412 if (XINT (lim) > ZV)
1413 XSETFASTINT (lim, ZV);
1414 if (XINT (lim) < BEGV)
1415 XSETFASTINT (lim, BEGV);
1417 bzero (fastmap, sizeof fastmap);
1419 i_byte = 0;
1421 if (i_byte < size_byte
1422 && SREF (string, 0) == '^')
1424 negate = 1; i_byte++;
1427 /* Find the characters specified and set their elements of fastmap.
1428 If syntaxp, each character counts as itself.
1429 Otherwise, handle backslashes and ranges specially. */
1431 while (i_byte < size_byte)
1433 c = STRING_CHAR_AND_LENGTH (str + i_byte, size_byte - i_byte, len);
1434 i_byte += len;
1436 if (syntaxp)
1437 fastmap[syntax_spec_code[c & 0377]] = 1;
1438 else
1440 if (c == '\\')
1442 if (i_byte == size_byte)
1443 break;
1445 c = STRING_CHAR_AND_LENGTH (str+i_byte, size_byte-i_byte, len);
1446 i_byte += len;
1448 if (i_byte < size_byte
1449 && str[i_byte] == '-')
1451 unsigned int c2;
1453 /* Skip over the dash. */
1454 i_byte++;
1456 if (i_byte == size_byte)
1457 break;
1459 /* Get the end of the range. */
1460 c2 =STRING_CHAR_AND_LENGTH (str+i_byte, size_byte-i_byte, len);
1461 i_byte += len;
1463 if (SINGLE_BYTE_CHAR_P (c))
1465 if (! SINGLE_BYTE_CHAR_P (c2))
1467 /* Handle a range starting with a character of
1468 less than 256, and ending with a character of
1469 not less than 256. Split that into two
1470 ranges, the low one ending at 0377, and the
1471 high one starting at the smallest character
1472 in the charset of C2 and ending at C2. */
1473 int charset = CHAR_CHARSET (c2);
1474 int c1 = MAKE_CHAR (charset, 0, 0);
1476 char_ranges[n_char_ranges++] = c1;
1477 char_ranges[n_char_ranges++] = c2;
1478 c2 = 0377;
1480 while (c <= c2)
1482 fastmap[c] = 1;
1483 c++;
1486 else if (c <= c2) /* Both C and C2 are multibyte char. */
1488 char_ranges[n_char_ranges++] = c;
1489 char_ranges[n_char_ranges++] = c2;
1492 else
1494 if (SINGLE_BYTE_CHAR_P (c))
1495 fastmap[c] = 1;
1496 else
1498 char_ranges[n_char_ranges++] = c;
1499 char_ranges[n_char_ranges++] = c;
1505 /* If ^ was the first character, complement the fastmap. */
1506 if (negate)
1507 for (i = 0; i < sizeof fastmap; i++)
1508 fastmap[i] ^= 1;
1511 int start_point = PT;
1512 int pos = PT;
1513 int pos_byte = PT_BYTE;
1514 unsigned char *p = PT_ADDR, *endp, *stop;
1516 if (forwardp)
1518 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1519 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1521 else
1523 endp = CHAR_POS_ADDR (XINT (lim));
1524 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1527 immediate_quit = 1;
1528 if (syntaxp)
1530 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
1531 if (forwardp)
1533 if (multibyte)
1534 while (1)
1536 int nbytes;
1538 if (p >= stop)
1540 if (p >= endp)
1541 break;
1542 p = GAP_END_ADDR;
1543 stop = endp;
1545 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, nbytes);
1546 if (! fastmap[(int) SYNTAX (c)])
1547 break;
1548 p += nbytes, pos++, pos_byte += nbytes;
1549 UPDATE_SYNTAX_TABLE_FORWARD (pos);
1551 else
1552 while (1)
1554 if (p >= stop)
1556 if (p >= endp)
1557 break;
1558 p = GAP_END_ADDR;
1559 stop = endp;
1561 if (! fastmap[(int) SYNTAX (*p)])
1562 break;
1563 p++, pos++;
1564 UPDATE_SYNTAX_TABLE_FORWARD (pos);
1567 else
1569 if (multibyte)
1570 while (1)
1572 unsigned char *prev_p;
1573 int nbytes;
1575 if (p <= stop)
1577 if (p <= endp)
1578 break;
1579 p = GPT_ADDR;
1580 stop = endp;
1582 prev_p = p;
1583 while (--p >= stop && ! CHAR_HEAD_P (*p));
1584 PARSE_MULTIBYTE_SEQ (p, MAX_MULTIBYTE_LENGTH, nbytes);
1585 if (prev_p - p > nbytes)
1586 p = prev_p - 1, c = *p, nbytes = 1;
1587 else
1588 c = STRING_CHAR (p, MAX_MULTIBYTE_LENGTH);
1589 pos--, pos_byte -= nbytes;
1590 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
1591 if (! fastmap[(int) SYNTAX (c)])
1593 pos++;
1594 pos_byte += nbytes;
1595 break;
1598 else
1599 while (1)
1601 if (p <= stop)
1603 if (p <= endp)
1604 break;
1605 p = GPT_ADDR;
1606 stop = endp;
1608 if (! fastmap[(int) SYNTAX (p[-1])])
1609 break;
1610 p--, pos--;
1611 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
1615 else
1617 if (forwardp)
1619 if (multibyte)
1620 while (1)
1622 int nbytes;
1624 if (p >= stop)
1626 if (p >= endp)
1627 break;
1628 p = GAP_END_ADDR;
1629 stop = endp;
1631 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, nbytes);
1632 if (SINGLE_BYTE_CHAR_P (c))
1634 if (!fastmap[c])
1635 break;
1637 else
1639 /* If we are looking at a multibyte character,
1640 we must look up the character in the table
1641 CHAR_RANGES. If there's no data in the
1642 table, that character is not what we want to
1643 skip. */
1645 /* The following code do the right thing even if
1646 n_char_ranges is zero (i.e. no data in
1647 CHAR_RANGES). */
1648 for (i = 0; i < n_char_ranges; i += 2)
1649 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1650 break;
1651 if (!(negate ^ (i < n_char_ranges)))
1652 break;
1654 p += nbytes, pos++, pos_byte += nbytes;
1656 else
1657 while (1)
1659 if (p >= stop)
1661 if (p >= endp)
1662 break;
1663 p = GAP_END_ADDR;
1664 stop = endp;
1666 if (!fastmap[*p])
1667 break;
1668 p++, pos++;
1671 else
1673 if (multibyte)
1674 while (1)
1676 unsigned char *prev_p;
1677 int nbytes;
1679 if (p <= stop)
1681 if (p <= endp)
1682 break;
1683 p = GPT_ADDR;
1684 stop = endp;
1686 prev_p = p;
1687 while (--p >= stop && ! CHAR_HEAD_P (*p));
1688 PARSE_MULTIBYTE_SEQ (p, MAX_MULTIBYTE_LENGTH, nbytes);
1689 if (prev_p - p > nbytes)
1690 p = prev_p - 1, c = *p, nbytes = 1;
1691 else
1692 c = STRING_CHAR (p, MAX_MULTIBYTE_LENGTH);
1693 if (SINGLE_BYTE_CHAR_P (c))
1695 if (!fastmap[c])
1696 break;
1698 else
1700 /* See the comment in the previous similar code. */
1701 for (i = 0; i < n_char_ranges; i += 2)
1702 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1703 break;
1704 if (!(negate ^ (i < n_char_ranges)))
1705 break;
1707 pos--, pos_byte -= nbytes;
1709 else
1710 while (1)
1712 if (p <= stop)
1714 if (p <= endp)
1715 break;
1716 p = GPT_ADDR;
1717 stop = endp;
1719 if (!fastmap[p[-1]])
1720 break;
1721 p--, pos--;
1726 #if 0 /* Not needed now that a position in mid-character
1727 cannot be specified in Lisp. */
1728 if (multibyte
1729 /* INC_POS or DEC_POS might have moved POS over LIM. */
1730 && (forwardp ? (pos > XINT (lim)) : (pos < XINT (lim))))
1731 pos = XINT (lim);
1732 #endif
1734 if (! multibyte)
1735 pos_byte = pos;
1737 SET_PT_BOTH (pos, pos_byte);
1738 immediate_quit = 0;
1740 return make_number (PT - start_point);
1744 /* Jump over a comment, assuming we are at the beginning of one.
1745 FROM is the current position.
1746 FROM_BYTE is the bytepos corresponding to FROM.
1747 Do not move past STOP (a charpos).
1748 The comment over which we have to jump is of style STYLE
1749 (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
1750 NESTING should be positive to indicate the nesting at the beginning
1751 for nested comments and should be zero or negative else.
1752 ST_COMMENT_STYLE cannot be nested.
1753 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
1754 (or 0 If the search cannot start in the middle of a two-character).
1756 If successful, return 1 and store the charpos of the comment's end
1757 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
1758 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
1759 corresponding bytepos into *BYTEPOS_PTR and the current nesting
1760 (as defined for state.incomment) in *INCOMMENT_PTR.
1762 The comment end is the last character of the comment rather than the
1763 character just after the comment.
1765 Global syntax data is assumed to initially be valid for FROM and
1766 remains valid for forward search starting at the returned position. */
1768 static int
1769 forw_comment (from, from_byte, stop, nesting, style, prev_syntax,
1770 charpos_ptr, bytepos_ptr, incomment_ptr)
1771 int from, from_byte, stop;
1772 int nesting, style, prev_syntax;
1773 int *charpos_ptr, *bytepos_ptr, *incomment_ptr;
1775 register int c, c1;
1776 register enum syntaxcode code;
1777 register int syntax;
1779 if (nesting <= 0) nesting = -1;
1781 /* Enter the loop in the middle so that we find
1782 a 2-char comment ender if we start in the middle of it. */
1783 syntax = prev_syntax;
1784 if (syntax != 0) goto forw_incomment;
1786 while (1)
1788 if (from == stop)
1790 *incomment_ptr = nesting;
1791 *charpos_ptr = from;
1792 *bytepos_ptr = from_byte;
1793 return 0;
1795 c = FETCH_CHAR (from_byte);
1796 syntax = SYNTAX_WITH_FLAGS (c);
1797 code = syntax & 0xff;
1798 if (code == Sendcomment
1799 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
1800 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
1801 (nesting > 0 && --nesting == 0) : nesting < 0))
1802 /* we have encountered a comment end of the same style
1803 as the comment sequence which began this comment
1804 section */
1805 break;
1806 if (code == Scomment_fence
1807 && style == ST_COMMENT_STYLE)
1808 /* we have encountered a comment end of the same style
1809 as the comment sequence which began this comment
1810 section. */
1811 break;
1812 if (nesting > 0
1813 && code == Scomment
1814 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
1815 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style)
1816 /* we have encountered a nested comment of the same style
1817 as the comment sequence which began this comment section */
1818 nesting++;
1819 INC_BOTH (from, from_byte);
1820 UPDATE_SYNTAX_TABLE_FORWARD (from);
1822 forw_incomment:
1823 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
1824 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
1825 && (c1 = FETCH_CHAR (from_byte),
1826 SYNTAX_COMEND_SECOND (c1))
1827 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
1828 SYNTAX_COMMENT_NESTED (c1)) ? nesting > 0 : nesting < 0))
1830 if (--nesting <= 0)
1831 /* we have encountered a comment end of the same style
1832 as the comment sequence which began this comment
1833 section */
1834 break;
1835 else
1837 INC_BOTH (from, from_byte);
1838 UPDATE_SYNTAX_TABLE_FORWARD (from);
1841 if (nesting > 0
1842 && from < stop
1843 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
1844 && (c1 = FETCH_CHAR (from_byte),
1845 SYNTAX_COMMENT_STYLE (c1) == style
1846 && SYNTAX_COMSTART_SECOND (c1))
1847 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
1848 SYNTAX_COMMENT_NESTED (c1)))
1849 /* we have encountered a nested comment of the same style
1850 as the comment sequence which began this comment
1851 section */
1853 INC_BOTH (from, from_byte);
1854 UPDATE_SYNTAX_TABLE_FORWARD (from);
1855 nesting++;
1858 *charpos_ptr = from;
1859 *bytepos_ptr = from_byte;
1860 return 1;
1863 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
1864 doc: /*
1865 Move forward across up to COUNT comments. If COUNT is negative, move backward.
1866 Stop scanning if we find something other than a comment or whitespace.
1867 Set point to where scanning stops.
1868 If COUNT comments are found as expected, with nothing except whitespace
1869 between them, return t; otherwise return nil. */)
1870 (count)
1871 Lisp_Object count;
1873 register int from;
1874 int from_byte;
1875 register int stop;
1876 register int c, c1;
1877 register enum syntaxcode code;
1878 int comstyle = 0; /* style of comment encountered */
1879 int comnested = 0; /* whether the comment is nestable or not */
1880 int found;
1881 int count1;
1882 int out_charpos, out_bytepos;
1883 int dummy;
1885 CHECK_NUMBER (count);
1886 count1 = XINT (count);
1887 stop = count1 > 0 ? ZV : BEGV;
1889 immediate_quit = 1;
1890 QUIT;
1892 from = PT;
1893 from_byte = PT_BYTE;
1895 SETUP_SYNTAX_TABLE (from, count1);
1896 while (count1 > 0)
1900 int comstart_first;
1902 if (from == stop)
1904 SET_PT_BOTH (from, from_byte);
1905 immediate_quit = 0;
1906 return Qnil;
1908 c = FETCH_CHAR (from_byte);
1909 code = SYNTAX (c);
1910 comstart_first = SYNTAX_COMSTART_FIRST (c);
1911 comnested = SYNTAX_COMMENT_NESTED (c);
1912 comstyle = SYNTAX_COMMENT_STYLE (c);
1913 INC_BOTH (from, from_byte);
1914 UPDATE_SYNTAX_TABLE_FORWARD (from);
1915 if (from < stop && comstart_first
1916 && (c1 = FETCH_CHAR (from_byte),
1917 SYNTAX_COMSTART_SECOND (c1)))
1919 /* We have encountered a comment start sequence and we
1920 are ignoring all text inside comments. We must record
1921 the comment style this sequence begins so that later,
1922 only a comment end of the same style actually ends
1923 the comment section. */
1924 code = Scomment;
1925 comstyle = SYNTAX_COMMENT_STYLE (c1);
1926 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
1927 INC_BOTH (from, from_byte);
1928 UPDATE_SYNTAX_TABLE_FORWARD (from);
1931 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
1933 if (code == Scomment_fence)
1934 comstyle = ST_COMMENT_STYLE;
1935 else if (code != Scomment)
1937 immediate_quit = 0;
1938 DEC_BOTH (from, from_byte);
1939 SET_PT_BOTH (from, from_byte);
1940 return Qnil;
1942 /* We're at the start of a comment. */
1943 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
1944 &out_charpos, &out_bytepos, &dummy);
1945 from = out_charpos; from_byte = out_bytepos;
1946 if (!found)
1948 immediate_quit = 0;
1949 SET_PT_BOTH (from, from_byte);
1950 return Qnil;
1952 INC_BOTH (from, from_byte);
1953 UPDATE_SYNTAX_TABLE_FORWARD (from);
1954 /* We have skipped one comment. */
1955 count1--;
1958 while (count1 < 0)
1960 while (1)
1962 int quoted;
1964 if (from <= stop)
1966 SET_PT_BOTH (BEGV, BEGV_BYTE);
1967 immediate_quit = 0;
1968 return Qnil;
1971 DEC_BOTH (from, from_byte);
1972 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
1973 quoted = char_quoted (from, from_byte);
1974 c = FETCH_CHAR (from_byte);
1975 code = SYNTAX (c);
1976 comstyle = 0;
1977 comnested = SYNTAX_COMMENT_NESTED (c);
1978 if (code == Sendcomment)
1979 comstyle = SYNTAX_COMMENT_STYLE (c);
1980 if (from > stop && SYNTAX_COMEND_SECOND (c)
1981 && prev_char_comend_first (from, from_byte)
1982 && !char_quoted (from - 1, dec_bytepos (from_byte)))
1984 /* We must record the comment style encountered so that
1985 later, we can match only the proper comment begin
1986 sequence of the same style. */
1987 DEC_BOTH (from, from_byte);
1988 code = Sendcomment;
1989 /* Calling char_quoted, above, set up global syntax position
1990 at the new value of FROM. */
1991 c1 = FETCH_CHAR (from_byte);
1992 comstyle = SYNTAX_COMMENT_STYLE (c1);
1993 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
1996 if (code == Scomment_fence)
1998 /* Skip until first preceding unquoted comment_fence. */
1999 int found = 0, ini = from, ini_byte = from_byte;
2001 while (1)
2003 DEC_BOTH (from, from_byte);
2004 if (from == stop)
2005 break;
2006 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2007 c = FETCH_CHAR (from_byte);
2008 if (SYNTAX (c) == Scomment_fence
2009 && !char_quoted (from, from_byte))
2011 found = 1;
2012 break;
2015 if (found == 0)
2017 from = ini; /* Set point to ini + 1. */
2018 from_byte = ini_byte;
2019 goto leave;
2022 else if (code == Sendcomment)
2024 found = back_comment (from, from_byte, stop, comnested, comstyle,
2025 &out_charpos, &out_bytepos);
2026 if (found == -1)
2028 if (c == '\n')
2029 /* This end-of-line is not an end-of-comment.
2030 Treat it like a whitespace.
2031 CC-mode (and maybe others) relies on this behavior. */
2033 else
2035 /* Failure: we should go back to the end of this
2036 not-quite-endcomment. */
2037 if (SYNTAX(c) != code)
2038 /* It was a two-char Sendcomment. */
2039 INC_BOTH (from, from_byte);
2040 goto leave;
2043 else
2045 /* We have skipped one comment. */
2046 from = out_charpos, from_byte = out_bytepos;
2047 break;
2050 else if (code != Swhitespace || quoted)
2052 leave:
2053 immediate_quit = 0;
2054 INC_BOTH (from, from_byte);
2055 SET_PT_BOTH (from, from_byte);
2056 return Qnil;
2060 count1++;
2063 SET_PT_BOTH (from, from_byte);
2064 immediate_quit = 0;
2065 return Qt;
2068 /* Return syntax code of character C if C is a single byte character
2069 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2071 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2072 ((SINGLE_BYTE_CHAR_P (c) || !multibyte_symbol_p) \
2073 ? SYNTAX (c) : Ssymbol)
2075 static Lisp_Object
2076 scan_lists (from, count, depth, sexpflag)
2077 register int from;
2078 int count, depth, sexpflag;
2080 Lisp_Object val;
2081 register int stop = count > 0 ? ZV : BEGV;
2082 register int c, c1;
2083 int stringterm;
2084 int quoted;
2085 int mathexit = 0;
2086 register enum syntaxcode code, temp_code;
2087 int min_depth = depth; /* Err out if depth gets less than this. */
2088 int comstyle = 0; /* style of comment encountered */
2089 int comnested = 0; /* whether the comment is nestable or not */
2090 int temp_pos;
2091 int last_good = from;
2092 int found;
2093 int from_byte;
2094 int out_bytepos, out_charpos;
2095 int temp, dummy;
2096 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2098 if (depth > 0) min_depth = 0;
2100 if (from > ZV) from = ZV;
2101 if (from < BEGV) from = BEGV;
2103 from_byte = CHAR_TO_BYTE (from);
2105 immediate_quit = 1;
2106 QUIT;
2108 SETUP_SYNTAX_TABLE (from, count);
2109 while (count > 0)
2111 while (from < stop)
2113 int comstart_first, prefix;
2114 UPDATE_SYNTAX_TABLE_FORWARD (from);
2115 c = FETCH_CHAR (from_byte);
2116 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2117 comstart_first = SYNTAX_COMSTART_FIRST (c);
2118 comnested = SYNTAX_COMMENT_NESTED (c);
2119 comstyle = SYNTAX_COMMENT_STYLE (c);
2120 prefix = SYNTAX_PREFIX (c);
2121 if (depth == min_depth)
2122 last_good = from;
2123 INC_BOTH (from, from_byte);
2124 UPDATE_SYNTAX_TABLE_FORWARD (from);
2125 if (from < stop && comstart_first
2126 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from_byte))
2127 && parse_sexp_ignore_comments)
2129 /* we have encountered a comment start sequence and we
2130 are ignoring all text inside comments. We must record
2131 the comment style this sequence begins so that later,
2132 only a comment end of the same style actually ends
2133 the comment section */
2134 code = Scomment;
2135 c1 = FETCH_CHAR (from_byte);
2136 comstyle = SYNTAX_COMMENT_STYLE (c1);
2137 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2138 INC_BOTH (from, from_byte);
2139 UPDATE_SYNTAX_TABLE_FORWARD (from);
2142 if (prefix)
2143 continue;
2145 switch (SWITCH_ENUM_CAST (code))
2147 case Sescape:
2148 case Scharquote:
2149 if (from == stop) goto lose;
2150 INC_BOTH (from, from_byte);
2151 /* treat following character as a word constituent */
2152 case Sword:
2153 case Ssymbol:
2154 if (depth || !sexpflag) break;
2155 /* This word counts as a sexp; return at end of it. */
2156 while (from < stop)
2158 UPDATE_SYNTAX_TABLE_FORWARD (from);
2160 /* Some compilers can't handle this inside the switch. */
2161 c = FETCH_CHAR (from_byte);
2162 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2163 switch (temp)
2165 case Scharquote:
2166 case Sescape:
2167 INC_BOTH (from, from_byte);
2168 if (from == stop) goto lose;
2169 break;
2170 case Sword:
2171 case Ssymbol:
2172 case Squote:
2173 break;
2174 default:
2175 goto done;
2177 INC_BOTH (from, from_byte);
2179 goto done;
2181 case Scomment_fence:
2182 comstyle = ST_COMMENT_STYLE;
2183 /* FALLTHROUGH */
2184 case Scomment:
2185 if (!parse_sexp_ignore_comments) break;
2186 UPDATE_SYNTAX_TABLE_FORWARD (from);
2187 found = forw_comment (from, from_byte, stop,
2188 comnested, comstyle, 0,
2189 &out_charpos, &out_bytepos, &dummy);
2190 from = out_charpos, from_byte = out_bytepos;
2191 if (!found)
2193 if (depth == 0)
2194 goto done;
2195 goto lose;
2197 INC_BOTH (from, from_byte);
2198 UPDATE_SYNTAX_TABLE_FORWARD (from);
2199 break;
2201 case Smath:
2202 if (!sexpflag)
2203 break;
2204 if (from != stop && c == FETCH_CHAR (from_byte))
2206 INC_BOTH (from, from_byte);
2208 if (mathexit)
2210 mathexit = 0;
2211 goto close1;
2213 mathexit = 1;
2215 case Sopen:
2216 if (!++depth) goto done;
2217 break;
2219 case Sclose:
2220 close1:
2221 if (!--depth) goto done;
2222 if (depth < min_depth)
2223 Fsignal (Qscan_error,
2224 Fcons (build_string ("Containing expression ends prematurely"),
2225 Fcons (make_number (last_good),
2226 Fcons (make_number (from), Qnil))));
2227 break;
2229 case Sstring:
2230 case Sstring_fence:
2231 temp_pos = dec_bytepos (from_byte);
2232 stringterm = FETCH_CHAR (temp_pos);
2233 while (1)
2235 if (from >= stop) goto lose;
2236 UPDATE_SYNTAX_TABLE_FORWARD (from);
2237 c = FETCH_CHAR (from_byte);
2238 if (code == Sstring
2239 ? (c == stringterm
2240 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2241 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
2242 break;
2244 /* Some compilers can't handle this inside the switch. */
2245 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2246 switch (temp)
2248 case Scharquote:
2249 case Sescape:
2250 INC_BOTH (from, from_byte);
2252 INC_BOTH (from, from_byte);
2254 INC_BOTH (from, from_byte);
2255 if (!depth && sexpflag) goto done;
2256 break;
2257 default:
2258 /* Ignore whitespace, punctuation, quote, endcomment. */
2259 break;
2263 /* Reached end of buffer. Error if within object, return nil if between */
2264 if (depth) goto lose;
2266 immediate_quit = 0;
2267 return Qnil;
2269 /* End of object reached */
2270 done:
2271 count--;
2274 while (count < 0)
2276 while (from > stop)
2278 DEC_BOTH (from, from_byte);
2279 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2280 c = FETCH_CHAR (from_byte);
2281 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2282 if (depth == min_depth)
2283 last_good = from;
2284 comstyle = 0;
2285 comnested = SYNTAX_COMMENT_NESTED (c);
2286 if (code == Sendcomment)
2287 comstyle = SYNTAX_COMMENT_STYLE (c);
2288 if (from > stop && SYNTAX_COMEND_SECOND (c)
2289 && prev_char_comend_first (from, from_byte)
2290 && parse_sexp_ignore_comments)
2292 /* We must record the comment style encountered so that
2293 later, we can match only the proper comment begin
2294 sequence of the same style. */
2295 DEC_BOTH (from, from_byte);
2296 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2297 code = Sendcomment;
2298 c1 = FETCH_CHAR (from_byte);
2299 comstyle = SYNTAX_COMMENT_STYLE (c1);
2300 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2303 /* Quoting turns anything except a comment-ender
2304 into a word character. Note that this cannot be true
2305 if we decremented FROM in the if-statement above. */
2306 if (code != Sendcomment && char_quoted (from, from_byte))
2308 DEC_BOTH (from, from_byte);
2309 code = Sword;
2311 else if (SYNTAX_PREFIX (c))
2312 continue;
2314 switch (SWITCH_ENUM_CAST (code))
2316 case Sword:
2317 case Ssymbol:
2318 case Sescape:
2319 case Scharquote:
2320 if (depth || !sexpflag) break;
2321 /* This word counts as a sexp; count object finished
2322 after passing it. */
2323 while (from > stop)
2325 temp_pos = from_byte;
2326 if (! NILP (current_buffer->enable_multibyte_characters))
2327 DEC_POS (temp_pos);
2328 else
2329 temp_pos--;
2330 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2331 c1 = FETCH_CHAR (temp_pos);
2332 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2333 /* Don't allow comment-end to be quoted. */
2334 if (temp_code == Sendcomment)
2335 goto done2;
2336 quoted = char_quoted (from - 1, temp_pos);
2337 if (quoted)
2339 DEC_BOTH (from, from_byte);
2340 temp_pos = dec_bytepos (temp_pos);
2341 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2343 c1 = FETCH_CHAR (temp_pos);
2344 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2345 if (! (quoted || temp_code == Sword
2346 || temp_code == Ssymbol
2347 || temp_code == Squote))
2348 goto done2;
2349 DEC_BOTH (from, from_byte);
2351 goto done2;
2353 case Smath:
2354 if (!sexpflag)
2355 break;
2356 temp_pos = dec_bytepos (from_byte);
2357 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2358 if (from != stop && c == FETCH_CHAR (temp_pos))
2359 DEC_BOTH (from, from_byte);
2360 if (mathexit)
2362 mathexit = 0;
2363 goto open2;
2365 mathexit = 1;
2367 case Sclose:
2368 if (!++depth) goto done2;
2369 break;
2371 case Sopen:
2372 open2:
2373 if (!--depth) goto done2;
2374 if (depth < min_depth)
2375 Fsignal (Qscan_error,
2376 Fcons (build_string ("Containing expression ends prematurely"),
2377 Fcons (make_number (last_good),
2378 Fcons (make_number (from), Qnil))));
2379 break;
2381 case Sendcomment:
2382 if (!parse_sexp_ignore_comments)
2383 break;
2384 found = back_comment (from, from_byte, stop, comnested, comstyle,
2385 &out_charpos, &out_bytepos);
2386 /* FIXME: if found == -1, then it really wasn't a comment-end.
2387 For single-char Sendcomment, we can't do much about it apart
2388 from skipping the char.
2389 For 2-char endcomments, we could try again, taking both
2390 chars as separate entities, but it's a lot of trouble
2391 for very little gain, so we don't bother either. -sm */
2392 if (found != -1)
2393 from = out_charpos, from_byte = out_bytepos;
2394 break;
2396 case Scomment_fence:
2397 case Sstring_fence:
2398 while (1)
2400 DEC_BOTH (from, from_byte);
2401 if (from == stop) goto lose;
2402 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2403 if (!char_quoted (from, from_byte)
2404 && (c = FETCH_CHAR (from_byte),
2405 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
2406 break;
2408 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2409 break;
2411 case Sstring:
2412 stringterm = FETCH_CHAR (from_byte);
2413 while (1)
2415 if (from == stop) goto lose;
2416 temp_pos = from_byte;
2417 if (! NILP (current_buffer->enable_multibyte_characters))
2418 DEC_POS (temp_pos);
2419 else
2420 temp_pos--;
2421 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2422 if (!char_quoted (from - 1, temp_pos)
2423 && stringterm == (c = FETCH_CHAR (temp_pos))
2424 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2425 break;
2426 DEC_BOTH (from, from_byte);
2428 DEC_BOTH (from, from_byte);
2429 if (!depth && sexpflag) goto done2;
2430 break;
2431 default:
2432 /* Ignore whitespace, punctuation, quote, endcomment. */
2433 break;
2437 /* Reached start of buffer. Error if within object, return nil if between */
2438 if (depth) goto lose;
2440 immediate_quit = 0;
2441 return Qnil;
2443 done2:
2444 count++;
2448 immediate_quit = 0;
2449 XSETFASTINT (val, from);
2450 return val;
2452 lose:
2453 Fsignal (Qscan_error,
2454 Fcons (build_string ("Unbalanced parentheses"),
2455 Fcons (make_number (last_good),
2456 Fcons (make_number (from), Qnil))));
2458 /* NOTREACHED */
2461 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2462 doc: /* Scan from character number FROM by COUNT lists.
2463 Returns the character number of the position thus found.
2465 If DEPTH is nonzero, paren depth begins counting from that value,
2466 only places where the depth in parentheses becomes zero
2467 are candidates for stopping; COUNT such places are counted.
2468 Thus, a positive value for DEPTH means go out levels.
2470 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2472 If the beginning or end of (the accessible part of) the buffer is reached
2473 and the depth is wrong, an error is signaled.
2474 If the depth is right but the count is not used up, nil is returned. */)
2475 (from, count, depth)
2476 Lisp_Object from, count, depth;
2478 CHECK_NUMBER (from);
2479 CHECK_NUMBER (count);
2480 CHECK_NUMBER (depth);
2482 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2485 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2486 doc: /* Scan from character number FROM by COUNT balanced expressions.
2487 If COUNT is negative, scan backwards.
2488 Returns the character number of the position thus found.
2490 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2492 If the beginning or end of (the accessible part of) the buffer is reached
2493 in the middle of a parenthetical grouping, an error is signaled.
2494 If the beginning or end is reached between groupings
2495 but before count is used up, nil is returned. */)
2496 (from, count)
2497 Lisp_Object from, count;
2499 CHECK_NUMBER (from);
2500 CHECK_NUMBER (count);
2502 return scan_lists (XINT (from), XINT (count), 0, 1);
2505 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2506 0, 0, 0,
2507 doc: /* Move point backward over any number of chars with prefix syntax.
2508 This includes chars with "quote" or "prefix" syntax (' or p). */)
2511 int beg = BEGV;
2512 int opoint = PT;
2513 int opoint_byte = PT_BYTE;
2514 int pos = PT;
2515 int pos_byte = PT_BYTE;
2516 int c;
2518 if (pos <= beg)
2520 SET_PT_BOTH (opoint, opoint_byte);
2522 return Qnil;
2525 SETUP_SYNTAX_TABLE (pos, -1);
2527 DEC_BOTH (pos, pos_byte);
2529 while (!char_quoted (pos, pos_byte)
2530 /* Previous statement updates syntax table. */
2531 && ((c = FETCH_CHAR (pos_byte), SYNTAX (c) == Squote)
2532 || SYNTAX_PREFIX (c)))
2534 opoint = pos;
2535 opoint_byte = pos_byte;
2537 if (pos + 1 > beg)
2538 DEC_BOTH (pos, pos_byte);
2541 SET_PT_BOTH (opoint, opoint_byte);
2543 return Qnil;
2546 /* Parse forward from FROM / FROM_BYTE to END,
2547 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2548 and return a description of the state of the parse at END.
2549 If STOPBEFORE is nonzero, stop at the start of an atom.
2550 If COMMENTSTOP is 1, stop at the start of a comment.
2551 If COMMENTSTOP is -1, stop at the start or end of a comment,
2552 after the beginning of a string, or after the end of a string. */
2554 static void
2555 scan_sexps_forward (stateptr, from, from_byte, end, targetdepth,
2556 stopbefore, oldstate, commentstop)
2557 struct lisp_parse_state *stateptr;
2558 register int from;
2559 int end, targetdepth, stopbefore, from_byte;
2560 Lisp_Object oldstate;
2561 int commentstop;
2563 struct lisp_parse_state state;
2565 register enum syntaxcode code;
2566 int c1;
2567 int comnested;
2568 struct level { int last, prev; };
2569 struct level levelstart[100];
2570 register struct level *curlevel = levelstart;
2571 struct level *endlevel = levelstart + 100;
2572 register int depth; /* Paren depth of current scanning location.
2573 level - levelstart equals this except
2574 when the depth becomes negative. */
2575 int mindepth; /* Lowest DEPTH value seen. */
2576 int start_quoted = 0; /* Nonzero means starting after a char quote */
2577 Lisp_Object tem;
2578 int prev_from; /* Keep one character before FROM. */
2579 int prev_from_byte;
2580 int prev_from_syntax;
2581 int boundary_stop = commentstop == -1;
2582 int nofence;
2583 int found;
2584 int out_bytepos, out_charpos;
2585 int temp;
2587 prev_from = from;
2588 prev_from_byte = from_byte;
2589 if (from != BEGV)
2590 DEC_BOTH (prev_from, prev_from_byte);
2592 /* Use this macro instead of `from++'. */
2593 #define INC_FROM \
2594 do { prev_from = from; \
2595 prev_from_byte = from_byte; \
2596 prev_from_syntax \
2597 = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte)); \
2598 INC_BOTH (from, from_byte); \
2599 if (from < end) \
2600 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2601 } while (0)
2603 immediate_quit = 1;
2604 QUIT;
2606 if (NILP (oldstate))
2608 depth = 0;
2609 state.instring = -1;
2610 state.incomment = 0;
2611 state.comstyle = 0; /* comment style a by default. */
2612 state.comstr_start = -1; /* no comment/string seen. */
2614 else
2616 tem = Fcar (oldstate);
2617 if (!NILP (tem))
2618 depth = XINT (tem);
2619 else
2620 depth = 0;
2622 oldstate = Fcdr (oldstate);
2623 oldstate = Fcdr (oldstate);
2624 oldstate = Fcdr (oldstate);
2625 tem = Fcar (oldstate);
2626 /* Check whether we are inside string_fence-style string: */
2627 state.instring = (!NILP (tem)
2628 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
2629 : -1);
2631 oldstate = Fcdr (oldstate);
2632 tem = Fcar (oldstate);
2633 state.incomment = (!NILP (tem)
2634 ? (INTEGERP (tem) ? XINT (tem) : -1)
2635 : 0);
2637 oldstate = Fcdr (oldstate);
2638 tem = Fcar (oldstate);
2639 start_quoted = !NILP (tem);
2641 /* if the eighth element of the list is nil, we are in comment
2642 style a. If it is non-nil, we are in comment style b */
2643 oldstate = Fcdr (oldstate);
2644 oldstate = Fcdr (oldstate);
2645 tem = Fcar (oldstate);
2646 state.comstyle = NILP (tem) ? 0 : (EQ (tem, Qsyntax_table)
2647 ? ST_COMMENT_STYLE : 1);
2649 oldstate = Fcdr (oldstate);
2650 tem = Fcar (oldstate);
2651 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
2652 oldstate = Fcdr (oldstate);
2653 tem = Fcar (oldstate);
2654 while (!NILP (tem)) /* >= second enclosing sexps. */
2656 /* curlevel++->last ran into compiler bug on Apollo */
2657 curlevel->last = XINT (Fcar (tem));
2658 if (++curlevel == endlevel)
2659 curlevel--; /* error ("Nesting too deep for parser"); */
2660 curlevel->prev = -1;
2661 curlevel->last = -1;
2662 tem = Fcdr (tem);
2665 state.quoted = 0;
2666 mindepth = depth;
2668 curlevel->prev = -1;
2669 curlevel->last = -1;
2671 SETUP_SYNTAX_TABLE (prev_from, 1);
2672 prev_from_syntax = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte));
2673 UPDATE_SYNTAX_TABLE_FORWARD (from);
2675 /* Enter the loop at a place appropriate for initial state. */
2677 if (state.incomment)
2678 goto startincomment;
2679 if (state.instring >= 0)
2681 nofence = state.instring != ST_STRING_STYLE;
2682 if (start_quoted)
2683 goto startquotedinstring;
2684 goto startinstring;
2686 else if (start_quoted)
2687 goto startquoted;
2689 while (from < end)
2691 INC_FROM;
2692 code = prev_from_syntax & 0xff;
2694 if (code == Scomment)
2696 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax);
2697 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
2698 1 : -1);
2699 state.comstr_start = prev_from;
2701 else if (code == Scomment_fence)
2703 /* Record the comment style we have entered so that only
2704 the comment-end sequence of the same style actually
2705 terminates the comment section. */
2706 state.comstyle = ST_COMMENT_STYLE;
2707 state.incomment = -1;
2708 state.comstr_start = prev_from;
2709 code = Scomment;
2711 else if (from < end)
2712 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax))
2713 if (c1 = FETCH_CHAR (from_byte),
2714 SYNTAX_COMSTART_SECOND (c1))
2715 /* Duplicate code to avoid a complex if-expression
2716 which causes trouble for the SGI compiler. */
2718 /* Record the comment style we have entered so that only
2719 the comment-end sequence of the same style actually
2720 terminates the comment section. */
2721 state.comstyle = SYNTAX_COMMENT_STYLE (c1);
2722 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
2723 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2724 state.incomment = comnested ? 1 : -1;
2725 state.comstr_start = prev_from;
2726 INC_FROM;
2727 code = Scomment;
2730 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
2731 continue;
2732 switch (SWITCH_ENUM_CAST (code))
2734 case Sescape:
2735 case Scharquote:
2736 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2737 curlevel->last = prev_from;
2738 startquoted:
2739 if (from == end) goto endquoted;
2740 INC_FROM;
2741 goto symstarted;
2742 /* treat following character as a word constituent */
2743 case Sword:
2744 case Ssymbol:
2745 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2746 curlevel->last = prev_from;
2747 symstarted:
2748 while (from < end)
2750 /* Some compilers can't handle this inside the switch. */
2751 temp = SYNTAX (FETCH_CHAR (from_byte));
2752 switch (temp)
2754 case Scharquote:
2755 case Sescape:
2756 INC_FROM;
2757 if (from == end) goto endquoted;
2758 break;
2759 case Sword:
2760 case Ssymbol:
2761 case Squote:
2762 break;
2763 default:
2764 goto symdone;
2766 INC_FROM;
2768 symdone:
2769 curlevel->prev = curlevel->last;
2770 break;
2772 case Scomment_fence: /* Can't happen because it's handled above. */
2773 case Scomment:
2774 if (commentstop || boundary_stop) goto done;
2775 startincomment:
2776 /* The (from == BEGV) test was to enter the loop in the middle so
2777 that we find a 2-char comment ender even if we start in the
2778 middle of it. We don't want to do that if we're just at the
2779 beginning of the comment (think of (*) ... (*)). */
2780 found = forw_comment (from, from_byte, end,
2781 state.incomment, state.comstyle,
2782 (from == BEGV || from < state.comstr_start + 3)
2783 ? 0 : prev_from_syntax,
2784 &out_charpos, &out_bytepos, &state.incomment);
2785 from = out_charpos; from_byte = out_bytepos;
2786 /* Beware! prev_from and friends are invalid now.
2787 Luckily, the `done' doesn't use them and the INC_FROM
2788 sets them to a sane value without looking at them. */
2789 if (!found) goto done;
2790 INC_FROM;
2791 state.incomment = 0;
2792 state.comstyle = 0; /* reset the comment style */
2793 if (boundary_stop) goto done;
2794 break;
2796 case Sopen:
2797 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2798 depth++;
2799 /* curlevel++->last ran into compiler bug on Apollo */
2800 curlevel->last = prev_from;
2801 if (++curlevel == endlevel)
2802 curlevel--; /* error ("Nesting too deep for parser"); */
2803 curlevel->prev = -1;
2804 curlevel->last = -1;
2805 if (targetdepth == depth) goto done;
2806 break;
2808 case Sclose:
2809 depth--;
2810 if (depth < mindepth)
2811 mindepth = depth;
2812 if (curlevel != levelstart)
2813 curlevel--;
2814 curlevel->prev = curlevel->last;
2815 if (targetdepth == depth) goto done;
2816 break;
2818 case Sstring:
2819 case Sstring_fence:
2820 state.comstr_start = from - 1;
2821 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2822 curlevel->last = prev_from;
2823 state.instring = (code == Sstring
2824 ? (FETCH_CHAR (prev_from_byte))
2825 : ST_STRING_STYLE);
2826 if (boundary_stop) goto done;
2827 startinstring:
2829 nofence = state.instring != ST_STRING_STYLE;
2831 while (1)
2833 int c;
2835 if (from >= end) goto done;
2836 c = FETCH_CHAR (from_byte);
2837 /* Some compilers can't handle this inside the switch. */
2838 temp = SYNTAX (c);
2840 /* Check TEMP here so that if the char has
2841 a syntax-table property which says it is NOT
2842 a string character, it does not end the string. */
2843 if (nofence && c == state.instring && temp == Sstring)
2844 break;
2846 switch (temp)
2848 case Sstring_fence:
2849 if (!nofence) goto string_end;
2850 break;
2851 case Scharquote:
2852 case Sescape:
2853 INC_FROM;
2854 startquotedinstring:
2855 if (from >= end) goto endquoted;
2857 INC_FROM;
2860 string_end:
2861 state.instring = -1;
2862 curlevel->prev = curlevel->last;
2863 INC_FROM;
2864 if (boundary_stop) goto done;
2865 break;
2867 case Smath:
2868 /* FIXME: We should do something with it. */
2869 break;
2870 default:
2871 /* Ignore whitespace, punctuation, quote, endcomment. */
2872 break;
2875 goto done;
2877 stop: /* Here if stopping before start of sexp. */
2878 from = prev_from; /* We have just fetched the char that starts it; */
2879 goto done; /* but return the position before it. */
2881 endquoted:
2882 state.quoted = 1;
2883 done:
2884 state.depth = depth;
2885 state.mindepth = mindepth;
2886 state.thislevelstart = curlevel->prev;
2887 state.prevlevelstart
2888 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
2889 state.location = from;
2890 state.levelstarts = Qnil;
2891 while (--curlevel >= levelstart)
2892 state.levelstarts = Fcons (make_number (curlevel->last),
2893 state.levelstarts);
2894 immediate_quit = 0;
2896 *stateptr = state;
2899 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
2900 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
2901 Parsing stops at TO or when certain criteria are met;
2902 point is set to where parsing stops.
2903 If fifth arg OLDSTATE is omitted or nil,
2904 parsing assumes that FROM is the beginning of a function.
2905 Value is a list of ten elements describing final state of parsing:
2906 0. depth in parens.
2907 1. character address of start of innermost containing list; nil if none.
2908 2. character address of start of last complete sexp terminated.
2909 3. non-nil if inside a string.
2910 (it is the character that will terminate the string,
2911 or t if the string should be terminated by a generic string delimiter.)
2912 4. nil if outside a comment, t if inside a non-nestable comment,
2913 else an integer (the current comment nesting).
2914 5. t if following a quote character.
2915 6. the minimum paren-depth encountered during this scan.
2916 7. t if in a comment of style b; symbol `syntax-table' if the comment
2917 should be terminated by a generic comment delimiter.
2918 8. character address of start of comment or string; nil if not in one.
2919 9. Intermediate data for continuation of parsing (subject to change).
2920 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
2921 in parentheses becomes equal to TARGETDEPTH.
2922 Fourth arg STOPBEFORE non-nil means stop when come to
2923 any character that starts a sexp.
2924 Fifth arg OLDSTATE is a nine-element list like what this function returns.
2925 It is used to initialize the state of the parse. Elements number 1, 2, 6
2926 and 8 are ignored; you can leave off element 8 (the last) entirely.
2927 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
2928 If it is symbol `syntax-table', stop after the start of a comment or a
2929 string, or after end of a comment or a string. */)
2930 (from, to, targetdepth, stopbefore, oldstate, commentstop)
2931 Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop;
2933 struct lisp_parse_state state;
2934 int target;
2936 if (!NILP (targetdepth))
2938 CHECK_NUMBER (targetdepth);
2939 target = XINT (targetdepth);
2941 else
2942 target = -100000; /* We won't reach this depth */
2944 validate_region (&from, &to);
2945 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
2946 XINT (to),
2947 target, !NILP (stopbefore), oldstate,
2948 (NILP (commentstop)
2949 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
2951 SET_PT (state.location);
2953 return Fcons (make_number (state.depth),
2954 Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart),
2955 Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart),
2956 Fcons (state.instring >= 0
2957 ? (state.instring == ST_STRING_STYLE
2958 ? Qt : make_number (state.instring)) : Qnil,
2959 Fcons (state.incomment < 0 ? Qt :
2960 (state.incomment == 0 ? Qnil :
2961 make_number (state.incomment)),
2962 Fcons (state.quoted ? Qt : Qnil,
2963 Fcons (make_number (state.mindepth),
2964 Fcons ((state.comstyle
2965 ? (state.comstyle == ST_COMMENT_STYLE
2966 ? Qsyntax_table : Qt) :
2967 Qnil),
2968 Fcons (((state.incomment
2969 || (state.instring >= 0))
2970 ? make_number (state.comstr_start)
2971 : Qnil),
2972 Fcons (state.levelstarts, Qnil))))))))));
2975 void
2976 init_syntax_once ()
2978 register int i, c;
2979 Lisp_Object temp;
2981 /* This has to be done here, before we call Fmake_char_table. */
2982 Qsyntax_table = intern ("syntax-table");
2983 staticpro (&Qsyntax_table);
2985 /* Intern this now in case it isn't already done.
2986 Setting this variable twice is harmless.
2987 But don't staticpro it here--that is done in alloc.c. */
2988 Qchar_table_extra_slots = intern ("char-table-extra-slots");
2990 /* Create objects which can be shared among syntax tables. */
2991 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
2992 for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++)
2993 XVECTOR (Vsyntax_code_object)->contents[i]
2994 = Fcons (make_number (i), Qnil);
2996 /* Now we are ready to set up this property, so we can
2997 create syntax tables. */
2998 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3000 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3002 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3004 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3005 for (i = 'a'; i <= 'z'; i++)
3006 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3007 for (i = 'A'; i <= 'Z'; i++)
3008 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3009 for (i = '0'; i <= '9'; i++)
3010 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3012 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3013 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3015 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3016 Fcons (make_number (Sopen), make_number (')')));
3017 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3018 Fcons (make_number (Sclose), make_number ('(')));
3019 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3020 Fcons (make_number (Sopen), make_number (']')));
3021 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3022 Fcons (make_number (Sclose), make_number ('[')));
3023 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3024 Fcons (make_number (Sopen), make_number ('}')));
3025 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3026 Fcons (make_number (Sclose), make_number ('{')));
3027 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3028 Fcons (make_number ((int) Sstring), Qnil));
3029 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3030 Fcons (make_number ((int) Sescape), Qnil));
3032 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
3033 for (i = 0; i < 10; i++)
3035 c = "_-+*/&|<>="[i];
3036 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3039 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3040 for (i = 0; i < 12; i++)
3042 c = ".,;:?!#@~^'`"[i];
3043 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3046 /* All multibyte characters have syntax `word' by default. */
3047 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3048 for (i = CHAR_TABLE_SINGLE_BYTE_SLOTS; i < CHAR_TABLE_ORDINARY_SLOTS; i++)
3049 XCHAR_TABLE (Vstandard_syntax_table)->contents[i] = temp;
3052 void
3053 syms_of_syntax ()
3055 Qsyntax_table_p = intern ("syntax-table-p");
3056 staticpro (&Qsyntax_table_p);
3058 staticpro (&Vsyntax_code_object);
3060 Qscan_error = intern ("scan-error");
3061 staticpro (&Qscan_error);
3062 Fput (Qscan_error, Qerror_conditions,
3063 Fcons (Qscan_error, Fcons (Qerror, Qnil)));
3064 Fput (Qscan_error, Qerror_message,
3065 build_string ("Scan error"));
3067 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
3068 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3070 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties,
3071 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3072 Otherwise, that text property is simply ignored.
3073 See the info node `(elisp)Syntax Properties' for a description of the
3074 `syntax-table' property. */);
3076 words_include_escapes = 0;
3077 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
3078 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3080 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol,
3081 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3082 multibyte_syntax_as_symbol = 0;
3084 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3085 &open_paren_in_column_0_is_defun_start,
3086 doc: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3087 open_paren_in_column_0_is_defun_start = 1;
3089 defsubr (&Ssyntax_table_p);
3090 defsubr (&Ssyntax_table);
3091 defsubr (&Sstandard_syntax_table);
3092 defsubr (&Scopy_syntax_table);
3093 defsubr (&Sset_syntax_table);
3094 defsubr (&Schar_syntax);
3095 defsubr (&Smatching_paren);
3096 defsubr (&Sstring_to_syntax);
3097 defsubr (&Smodify_syntax_entry);
3098 defsubr (&Sinternal_describe_syntax_value);
3100 defsubr (&Sforward_word);
3102 defsubr (&Sskip_chars_forward);
3103 defsubr (&Sskip_chars_backward);
3104 defsubr (&Sskip_syntax_forward);
3105 defsubr (&Sskip_syntax_backward);
3107 defsubr (&Sforward_comment);
3108 defsubr (&Sscan_lists);
3109 defsubr (&Sscan_sexps);
3110 defsubr (&Sbackward_prefix_chars);
3111 defsubr (&Sparse_partial_sexp);
3114 /* arch-tag: 3e297b9f-088e-4b64-8f4c-fb0b3443e412
3115 (do not change this comment) */