Merge from emacs-23; up to 2010-06-08T03:06:47Z!dann@ics.uci.edu.
[emacs.git] / src / syntax.c
blob56176f3241860101beff2a860f9ef67fcee5aa36
1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2011
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
22 #include <ctype.h>
23 #include <setjmp.h>
24 #include "lisp.h"
25 #include "commands.h"
26 #include "buffer.h"
27 #include "character.h"
28 #include "keymap.h"
29 #include "regex.h"
31 /* Make syntax table lookup grant data in gl_state. */
32 #define SYNTAX_ENTRY_VIA_PROPERTY
34 #include "syntax.h"
35 #include "intervals.h"
36 #include "category.h"
38 /* Then there are seven single-bit flags that have the following meanings:
39 1. This character is the first of a two-character comment-start sequence.
40 2. This character is the second of a two-character comment-start sequence.
41 3. This character is the first of a two-character comment-end sequence.
42 4. This character is the second of a two-character comment-end sequence.
43 5. This character is a prefix, for backward-prefix-chars.
44 6. The char is part of a delimiter for comments of style "b".
45 7. This character is part of a nestable comment sequence.
46 8. The char is part of a delimiter for comments of style "c".
47 Note that any two-character sequence whose first character has flag 1
48 and whose second character has flag 2 will be interpreted as a comment start.
50 bit 6 and 8 are used to discriminate between different comment styles.
51 Languages such as C++ allow two orthogonal syntax start/end pairs
52 and bit 6 is used to determine whether a comment-end or Scommentend
53 ends style a or b. Comment markers can start style a, b, c, or bc.
54 Style a is always the default.
55 For 2-char comment markers, the style b flag is only looked up on the second
56 char of the comment marker and on the first char of the comment ender.
57 For style c (like to for the nested flag), the flag can be placed on any
58 one of the chars.
61 /* These macros extract specific flags from an integer
62 that holds the syntax code and the flags. */
64 #define SYNTAX_FLAGS_COMSTART_FIRST(flags) (((flags) >> 16) & 1)
66 #define SYNTAX_FLAGS_COMSTART_SECOND(flags) (((flags) >> 17) & 1)
68 #define SYNTAX_FLAGS_COMEND_FIRST(flags) (((flags) >> 18) & 1)
70 #define SYNTAX_FLAGS_COMEND_SECOND(flags) (((flags) >> 19) & 1)
72 #define SYNTAX_FLAGS_PREFIX(flags) (((flags) >> 20) & 1)
74 #define SYNTAX_FLAGS_COMMENT_STYLEB(flags) (((flags) >> 21) & 1)
75 #define SYNTAX_FLAGS_COMMENT_STYLEC(flags) (((flags) >> 22) & 2)
76 /* FLAGS should be the flags of the main char of the comment marker, e.g.
77 the second for comstart and the first for comend. */
78 #define SYNTAX_FLAGS_COMMENT_STYLE(flags, other_flags) \
79 (SYNTAX_FLAGS_COMMENT_STYLEB (flags) \
80 | SYNTAX_FLAGS_COMMENT_STYLEC (flags) \
81 | SYNTAX_FLAGS_COMMENT_STYLEC (other_flags))
83 #define SYNTAX_FLAGS_COMMENT_NESTED(flags) (((flags) >> 22) & 1)
85 /* These macros extract a particular flag for a given character. */
87 #define SYNTAX_COMEND_FIRST(c) \
88 (SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c)))
89 #define SYNTAX_PREFIX(c) (SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c)))
91 /* We use these constants in place for comment-style and
92 string-ender-char to distinguish comments/strings started by
93 comment_fence and string_fence codes. */
95 #define ST_COMMENT_STYLE (256 + 1)
96 #define ST_STRING_STYLE (256 + 2)
98 Lisp_Object Qsyntax_table_p, Qsyntax_table, Qscan_error;
100 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
101 if not compiled with GCC. No need to mark it, since it is used
102 only very temporarily. */
103 Lisp_Object syntax_temp;
105 /* This is the internal form of the parse state used in parse-partial-sexp. */
107 struct lisp_parse_state
109 int depth; /* Depth at end of parsing. */
110 int instring; /* -1 if not within string, else desired terminator. */
111 int incomment; /* -1 if in unnestable comment else comment nesting */
112 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
113 int quoted; /* Nonzero if just after an escape char at end of parsing */
114 int mindepth; /* Minimum depth seen while scanning. */
115 /* Char number of most recent start-of-expression at current level */
116 EMACS_INT thislevelstart;
117 /* Char number of start of containing expression */
118 EMACS_INT prevlevelstart;
119 EMACS_INT location; /* Char number at which parsing stopped. */
120 EMACS_INT comstr_start; /* Position of last comment/string starter. */
121 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
122 of levels (starting from outermost). */
125 /* These variables are a cache for finding the start of a defun.
126 find_start_pos is the place for which the defun start was found.
127 find_start_value is the defun start position found for it.
128 find_start_value_byte is the corresponding byte position.
129 find_start_buffer is the buffer it was found in.
130 find_start_begv is the BEGV value when it was found.
131 find_start_modiff is the value of MODIFF when it was found. */
133 static EMACS_INT find_start_pos;
134 static EMACS_INT find_start_value;
135 static EMACS_INT find_start_value_byte;
136 static struct buffer *find_start_buffer;
137 static EMACS_INT find_start_begv;
138 static int find_start_modiff;
141 static Lisp_Object skip_chars (int, Lisp_Object, Lisp_Object, int);
142 static Lisp_Object skip_syntaxes (int, Lisp_Object, Lisp_Object);
143 static Lisp_Object scan_lists (EMACS_INT, EMACS_INT, EMACS_INT, int);
144 static void scan_sexps_forward (struct lisp_parse_state *,
145 EMACS_INT, EMACS_INT, EMACS_INT, int,
146 int, Lisp_Object, int);
147 static int in_classes (int, Lisp_Object);
149 /* Whether the syntax of the character C has the prefix flag set. */
150 int syntax_prefix_flag_p (int c)
152 return SYNTAX_PREFIX (c);
155 struct gl_state_s gl_state; /* Global state of syntax parser. */
157 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
158 to scan to property-change. */
160 /* Update gl_state to an appropriate interval which contains CHARPOS. The
161 sign of COUNT give the relative position of CHARPOS wrt the previously
162 valid interval. If INIT, only [be]_property fields of gl_state are
163 valid at start, the rest is filled basing on OBJECT.
165 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
166 direction than the intervals - or in an interval. We update the
167 current syntax-table basing on the property of this interval, and
168 update the interval to start further than CHARPOS - or be
169 NULL_INTERVAL. We also update lim_property to be the next value of
170 charpos to call this subroutine again - or be before/after the
171 start/end of OBJECT. */
173 void
174 update_syntax_table (EMACS_INT charpos, int count, int init,
175 Lisp_Object object)
177 Lisp_Object tmp_table;
178 unsigned cnt = 0;
179 int invalidate = 1;
180 INTERVAL i;
182 if (init)
184 gl_state.old_prop = Qnil;
185 gl_state.start = gl_state.b_property;
186 gl_state.stop = gl_state.e_property;
187 i = interval_of (charpos, object);
188 gl_state.backward_i = gl_state.forward_i = i;
189 invalidate = 0;
190 if (NULL_INTERVAL_P (i))
191 return;
192 /* interval_of updates only ->position of the return value, so
193 update the parents manually to speed up update_interval. */
194 while (!NULL_PARENT (i))
196 if (AM_RIGHT_CHILD (i))
197 INTERVAL_PARENT (i)->position = i->position
198 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
199 - TOTAL_LENGTH (INTERVAL_PARENT (i))
200 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
201 else
202 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
203 + TOTAL_LENGTH (i);
204 i = INTERVAL_PARENT (i);
206 i = gl_state.forward_i;
207 gl_state.b_property = i->position - gl_state.offset;
208 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
209 goto update;
211 i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
213 /* We are guaranteed to be called with CHARPOS either in i,
214 or further off. */
215 if (NULL_INTERVAL_P (i))
216 error ("Error in syntax_table logic for to-the-end intervals");
217 else if (charpos < i->position) /* Move left. */
219 if (count > 0)
220 error ("Error in syntax_table logic for intervals <-");
221 /* Update the interval. */
222 i = update_interval (i, charpos);
223 if (INTERVAL_LAST_POS (i) != gl_state.b_property)
225 invalidate = 0;
226 gl_state.forward_i = i;
227 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
230 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
232 if (count < 0)
233 error ("Error in syntax_table logic for intervals ->");
234 /* Update the interval. */
235 i = update_interval (i, charpos);
236 if (i->position != gl_state.e_property)
238 invalidate = 0;
239 gl_state.backward_i = i;
240 gl_state.b_property = i->position - gl_state.offset;
244 update:
245 tmp_table = textget (i->plist, Qsyntax_table);
247 if (invalidate)
248 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
250 if (invalidate) /* Did not get to adjacent interval. */
251 { /* with the same table => */
252 /* invalidate the old range. */
253 if (count > 0)
255 gl_state.backward_i = i;
256 gl_state.b_property = i->position - gl_state.offset;
258 else
260 gl_state.forward_i = i;
261 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
265 if (!EQ (tmp_table, gl_state.old_prop))
267 gl_state.current_syntax_table = tmp_table;
268 gl_state.old_prop = tmp_table;
269 if (EQ (Fsyntax_table_p (tmp_table), Qt))
271 gl_state.use_global = 0;
273 else if (CONSP (tmp_table))
275 gl_state.use_global = 1;
276 gl_state.global_code = tmp_table;
278 else
280 gl_state.use_global = 0;
281 gl_state.current_syntax_table = BVAR (current_buffer, syntax_table);
285 while (!NULL_INTERVAL_P (i))
287 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
289 if (count > 0)
291 gl_state.e_property = i->position - gl_state.offset;
292 gl_state.forward_i = i;
294 else
296 gl_state.b_property
297 = i->position + LENGTH (i) - gl_state.offset;
298 gl_state.backward_i = i;
300 return;
302 else if (cnt == INTERVALS_AT_ONCE)
304 if (count > 0)
306 gl_state.e_property
307 = i->position + LENGTH (i) - gl_state.offset
308 /* e_property at EOB is not set to ZV but to ZV+1, so that
309 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
310 having to check eob between the two. */
311 + (NULL_INTERVAL_P (next_interval (i)) ? 1 : 0);
312 gl_state.forward_i = i;
314 else
316 gl_state.b_property = i->position - gl_state.offset;
317 gl_state.backward_i = i;
319 return;
321 cnt++;
322 i = count > 0 ? next_interval (i) : previous_interval (i);
324 eassert (NULL_INTERVAL_P (i)); /* This property goes to the end. */
325 if (count > 0)
326 gl_state.e_property = gl_state.stop;
327 else
328 gl_state.b_property = gl_state.start;
331 /* Returns TRUE if char at CHARPOS is quoted.
332 Global syntax-table data should be set up already to be good at CHARPOS
333 or after. On return global syntax data is good for lookup at CHARPOS. */
335 static int
336 char_quoted (EMACS_INT charpos, EMACS_INT bytepos)
338 register enum syntaxcode code;
339 register EMACS_INT beg = BEGV;
340 register int quoted = 0;
341 EMACS_INT orig = charpos;
343 while (charpos > beg)
345 int c;
346 DEC_BOTH (charpos, bytepos);
348 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
349 c = FETCH_CHAR_AS_MULTIBYTE (bytepos);
350 code = SYNTAX (c);
351 if (! (code == Scharquote || code == Sescape))
352 break;
354 quoted = !quoted;
357 UPDATE_SYNTAX_TABLE (orig);
358 return quoted;
361 /* Return the bytepos one character before BYTEPOS.
362 We assume that BYTEPOS is not at the start of the buffer. */
364 static INLINE EMACS_INT
365 dec_bytepos (EMACS_INT bytepos)
367 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
368 return bytepos - 1;
370 DEC_POS (bytepos);
371 return bytepos;
374 /* Return a defun-start position before POS and not too far before.
375 It should be the last one before POS, or nearly the last.
377 When open_paren_in_column_0_is_defun_start is nonzero,
378 only the beginning of the buffer is treated as a defun-start.
380 We record the information about where the scan started
381 and what its result was, so that another call in the same area
382 can return the same value very quickly.
384 There is no promise at which position the global syntax data is
385 valid on return from the subroutine, so the caller should explicitly
386 update the global data. */
388 static EMACS_INT
389 find_defun_start (EMACS_INT pos, EMACS_INT pos_byte)
391 EMACS_INT opoint = PT, opoint_byte = PT_BYTE;
393 if (!open_paren_in_column_0_is_defun_start)
395 find_start_value_byte = BEGV_BYTE;
396 return BEGV;
399 /* Use previous finding, if it's valid and applies to this inquiry. */
400 if (current_buffer == find_start_buffer
401 /* Reuse the defun-start even if POS is a little farther on.
402 POS might be in the next defun, but that's ok.
403 Our value may not be the best possible, but will still be usable. */
404 && pos <= find_start_pos + 1000
405 && pos >= find_start_value
406 && BEGV == find_start_begv
407 && MODIFF == find_start_modiff)
408 return find_start_value;
410 /* Back up to start of line. */
411 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
413 /* We optimize syntax-table lookup for rare updates. Thus we accept
414 only those `^\s(' which are good in global _and_ text-property
415 syntax-tables. */
416 SETUP_BUFFER_SYNTAX_TABLE ();
417 while (PT > BEGV)
419 int c;
421 /* Open-paren at start of line means we may have found our
422 defun-start. */
423 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
424 if (SYNTAX (c) == Sopen)
426 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
427 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
428 if (SYNTAX (c) == Sopen)
429 break;
430 /* Now fallback to the default value. */
431 SETUP_BUFFER_SYNTAX_TABLE ();
433 /* Move to beg of previous line. */
434 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
437 /* Record what we found, for the next try. */
438 find_start_value = PT;
439 find_start_value_byte = PT_BYTE;
440 find_start_buffer = current_buffer;
441 find_start_modiff = MODIFF;
442 find_start_begv = BEGV;
443 find_start_pos = pos;
445 TEMP_SET_PT_BOTH (opoint, opoint_byte);
447 return find_start_value;
450 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
452 static int
453 prev_char_comend_first (EMACS_INT pos, EMACS_INT pos_byte)
455 int c, val;
457 DEC_BOTH (pos, pos_byte);
458 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
459 c = FETCH_CHAR (pos_byte);
460 val = SYNTAX_COMEND_FIRST (c);
461 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
462 return val;
465 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
467 /* static int
468 * prev_char_comstart_first (pos, pos_byte)
469 * int pos, pos_byte;
471 * int c, val;
473 * DEC_BOTH (pos, pos_byte);
474 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
475 * c = FETCH_CHAR (pos_byte);
476 * val = SYNTAX_COMSTART_FIRST (c);
477 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
478 * return val;
479 * } */
481 /* Checks whether charpos FROM is at the end of a comment.
482 FROM_BYTE is the bytepos corresponding to FROM.
483 Do not move back before STOP.
485 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
486 return -1 otherwise.
488 If successful, store the charpos of the comment's beginning
489 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
491 Global syntax data remains valid for backward search starting at
492 the returned value (or at FROM, if the search was not successful). */
494 static int
495 back_comment (EMACS_INT from, EMACS_INT from_byte, EMACS_INT stop, int comnested, int comstyle, EMACS_INT *charpos_ptr, EMACS_INT *bytepos_ptr)
497 /* Look back, counting the parity of string-quotes,
498 and recording the comment-starters seen.
499 When we reach a safe place, assume that's not in a string;
500 then step the main scan to the earliest comment-starter seen
501 an even number of string quotes away from the safe place.
503 OFROM[I] is position of the earliest comment-starter seen
504 which is I+2X quotes from the comment-end.
505 PARITY is current parity of quotes from the comment end. */
506 int string_style = -1; /* Presumed outside of any string. */
507 int string_lossage = 0;
508 /* Not a real lossage: indicates that we have passed a matching comment
509 starter plus a non-matching comment-ender, meaning that any matching
510 comment-starter we might see later could be a false positive (hidden
511 inside another comment).
512 Test case: { a (* b } c (* d *) */
513 int comment_lossage = 0;
514 EMACS_INT comment_end = from;
515 EMACS_INT comment_end_byte = from_byte;
516 EMACS_INT comstart_pos = 0;
517 EMACS_INT comstart_byte IF_LINT (= 0);
518 /* Place where the containing defun starts,
519 or 0 if we didn't come across it yet. */
520 EMACS_INT defun_start = 0;
521 EMACS_INT defun_start_byte = 0;
522 register enum syntaxcode code;
523 int nesting = 1; /* current comment nesting */
524 int c;
525 int syntax = 0;
527 /* FIXME: A }} comment-ender style leads to incorrect behavior
528 in the case of {{ c }}} because we ignore the last two chars which are
529 assumed to be comment-enders although they aren't. */
531 /* At beginning of range to scan, we're outside of strings;
532 that determines quote parity to the comment-end. */
533 while (from != stop)
535 EMACS_INT temp_byte;
536 int prev_syntax, com2start, com2end;
537 int comstart;
539 /* Move back and examine a character. */
540 DEC_BOTH (from, from_byte);
541 UPDATE_SYNTAX_TABLE_BACKWARD (from);
543 prev_syntax = syntax;
544 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
545 syntax = SYNTAX_WITH_FLAGS (c);
546 code = SYNTAX (c);
548 /* Check for 2-char comment markers. */
549 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
550 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
551 && (comstyle
552 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax, syntax))
553 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
554 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
555 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
556 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
557 comstart = (com2start || code == Scomment);
559 /* Nasty cases with overlapping 2-char comment markers:
560 - snmp-mode: -- c -- foo -- c --
561 --- c --
562 ------ c --
563 - c-mode: *||*
564 |* *|* *|
565 |*| |* |*|
566 /// */
568 /* If a 2-char comment sequence partly overlaps with another,
569 we don't try to be clever. E.g. |*| in C, or }% in modes that
570 have %..\n and %{..}%. */
571 if (from > stop && (com2end || comstart))
573 EMACS_INT next = from, next_byte = from_byte;
574 int next_c, next_syntax;
575 DEC_BOTH (next, next_byte);
576 UPDATE_SYNTAX_TABLE_BACKWARD (next);
577 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
578 next_syntax = SYNTAX_WITH_FLAGS (next_c);
579 if (((comstart || comnested)
580 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
581 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
582 || ((com2end || comnested)
583 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
584 && (comstyle
585 == SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_syntax))
586 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
587 goto lossage;
588 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
591 if (com2start && comstart_pos == 0)
592 /* We're looking at a comment starter. But it might be a comment
593 ender as well (see snmp-mode). The first time we see one, we
594 need to consider it as a comment starter,
595 and the subsequent times as a comment ender. */
596 com2end = 0;
598 /* Turn a 2-char comment sequences into the appropriate syntax. */
599 if (com2end)
600 code = Sendcomment;
601 else if (com2start)
602 code = Scomment;
603 /* Ignore comment starters of a different style. */
604 else if (code == Scomment
605 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0)
606 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
607 continue;
609 /* Ignore escaped characters, except comment-enders. */
610 if (code != Sendcomment && char_quoted (from, from_byte))
611 continue;
613 switch (code)
615 case Sstring_fence:
616 case Scomment_fence:
617 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
618 case Sstring:
619 /* Track parity of quotes. */
620 if (string_style == -1)
621 /* Entering a string. */
622 string_style = c;
623 else if (string_style == c)
624 /* Leaving the string. */
625 string_style = -1;
626 else
627 /* If we have two kinds of string delimiters.
628 There's no way to grok this scanning backwards. */
629 string_lossage = 1;
630 break;
632 case Scomment:
633 /* We've already checked that it is the relevant comstyle. */
634 if (string_style != -1 || comment_lossage || string_lossage)
635 /* There are odd string quotes involved, so let's be careful.
636 Test case in Pascal: " { " a { " } */
637 goto lossage;
639 if (!comnested)
641 /* Record best comment-starter so far. */
642 comstart_pos = from;
643 comstart_byte = from_byte;
645 else if (--nesting <= 0)
646 /* nested comments have to be balanced, so we don't need to
647 keep looking for earlier ones. We use here the same (slightly
648 incorrect) reasoning as below: since it is followed by uniform
649 paired string quotes, this comment-start has to be outside of
650 strings, else the comment-end itself would be inside a string. */
651 goto done;
652 break;
654 case Sendcomment:
655 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == comstyle
656 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
657 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
658 /* This is the same style of comment ender as ours. */
660 if (comnested)
661 nesting++;
662 else
663 /* Anything before that can't count because it would match
664 this comment-ender rather than ours. */
665 from = stop; /* Break out of the loop. */
667 else if (comstart_pos != 0 || c != '\n')
668 /* We're mixing comment styles here, so we'd better be careful.
669 The (comstart_pos != 0 || c != '\n') check is not quite correct
670 (we should just always set comment_lossage), but removing it
671 would imply that any multiline comment in C would go through
672 lossage, which seems overkill.
673 The failure should only happen in the rare cases such as
674 { (* } *) */
675 comment_lossage = 1;
676 break;
678 case Sopen:
679 /* Assume a defun-start point is outside of strings. */
680 if (open_paren_in_column_0_is_defun_start
681 && (from == stop
682 || (temp_byte = dec_bytepos (from_byte),
683 FETCH_CHAR (temp_byte) == '\n')))
685 defun_start = from;
686 defun_start_byte = from_byte;
687 from = stop; /* Break out of the loop. */
689 break;
691 default:
692 break;
696 if (comstart_pos == 0)
698 from = comment_end;
699 from_byte = comment_end_byte;
700 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
702 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
703 or `done'), then we've found the beginning of the non-nested comment. */
704 else if (1) /* !comnested */
706 from = comstart_pos;
707 from_byte = comstart_byte;
708 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
710 else
712 struct lisp_parse_state state;
713 lossage:
714 /* We had two kinds of string delimiters mixed up
715 together. Decode this going forwards.
716 Scan fwd from a known safe place (beginning-of-defun)
717 to the one in question; this records where we
718 last passed a comment starter. */
719 /* If we did not already find the defun start, find it now. */
720 if (defun_start == 0)
722 defun_start = find_defun_start (comment_end, comment_end_byte);
723 defun_start_byte = find_start_value_byte;
727 scan_sexps_forward (&state,
728 defun_start, defun_start_byte,
729 comment_end, -10000, 0, Qnil, 0);
730 defun_start = comment_end;
731 if (state.incomment == (comnested ? 1 : -1)
732 && state.comstyle == comstyle)
733 from = state.comstr_start;
734 else
736 from = comment_end;
737 if (state.incomment)
738 /* If comment_end is inside some other comment, maybe ours
739 is nested, so we need to try again from within the
740 surrounding comment. Example: { a (* " *) */
742 /* FIXME: We should advance by one or two chars. */
743 defun_start = state.comstr_start + 2;
744 defun_start_byte = CHAR_TO_BYTE (defun_start);
747 } while (defun_start < comment_end);
749 from_byte = CHAR_TO_BYTE (from);
750 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
753 done:
754 *charpos_ptr = from;
755 *bytepos_ptr = from_byte;
757 return (from == comment_end) ? -1 : from;
760 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
761 doc: /* Return t if OBJECT is a syntax table.
762 Currently, any char-table counts as a syntax table. */)
763 (Lisp_Object object)
765 if (CHAR_TABLE_P (object)
766 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
767 return Qt;
768 return Qnil;
771 static void
772 check_syntax_table (Lisp_Object obj)
774 CHECK_TYPE (CHAR_TABLE_P (obj) && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table),
775 Qsyntax_table_p, obj);
778 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
779 doc: /* Return the current syntax table.
780 This is the one specified by the current buffer. */)
781 (void)
783 return BVAR (current_buffer, syntax_table);
786 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
787 Sstandard_syntax_table, 0, 0, 0,
788 doc: /* Return the standard syntax table.
789 This is the one used for new buffers. */)
790 (void)
792 return Vstandard_syntax_table;
795 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
796 doc: /* Construct a new syntax table and return it.
797 It is a copy of the TABLE, which defaults to the standard syntax table. */)
798 (Lisp_Object table)
800 Lisp_Object copy;
802 if (!NILP (table))
803 check_syntax_table (table);
804 else
805 table = Vstandard_syntax_table;
807 copy = Fcopy_sequence (table);
809 /* Only the standard syntax table should have a default element.
810 Other syntax tables should inherit from parents instead. */
811 XCHAR_TABLE (copy)->defalt = Qnil;
813 /* Copied syntax tables should all have parents.
814 If we copied one with no parent, such as the standard syntax table,
815 use the standard syntax table as the copy's parent. */
816 if (NILP (XCHAR_TABLE (copy)->parent))
817 Fset_char_table_parent (copy, Vstandard_syntax_table);
818 return copy;
821 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
822 doc: /* Select a new syntax table for the current buffer.
823 One argument, a syntax table. */)
824 (Lisp_Object table)
826 int idx;
827 check_syntax_table (table);
828 BVAR (current_buffer, syntax_table) = table;
829 /* Indicate that this buffer now has a specified syntax table. */
830 idx = PER_BUFFER_VAR_IDX (syntax_table);
831 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
832 return table;
835 /* Convert a letter which signifies a syntax code
836 into the code it signifies.
837 This is used by modify-syntax-entry, and other things. */
839 unsigned char syntax_spec_code[0400] =
840 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
841 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
842 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
843 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
844 (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377,
845 (char) Smath, 0377, 0377, (char) Squote,
846 (char) Sopen, (char) Sclose, 0377, 0377,
847 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
848 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
849 0377, 0377, 0377, 0377,
850 (char) Scomment, 0377, (char) Sendcomment, 0377,
851 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
852 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
853 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
854 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
855 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
856 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
857 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
858 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377
861 /* Indexed by syntax code, give the letter that describes it. */
863 char syntax_code_spec[16] =
865 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
866 '!', '|'
869 /* Indexed by syntax code, give the object (cons of syntax code and
870 nil) to be stored in syntax table. Since these objects can be
871 shared among syntax tables, we generate them in advance. By
872 sharing objects, the function `describe-syntax' can give a more
873 compact listing. */
874 static Lisp_Object Vsyntax_code_object;
877 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
878 doc: /* Return the syntax code of CHARACTER, described by a character.
879 For example, if CHARACTER is a word constituent, the
880 character `w' (119) is returned.
881 The characters that correspond to various syntax codes
882 are listed in the documentation of `modify-syntax-entry'. */)
883 (Lisp_Object character)
885 int char_int;
886 CHECK_CHARACTER (character);
887 char_int = XINT (character);
888 SETUP_BUFFER_SYNTAX_TABLE ();
889 return make_number (syntax_code_spec[(int) SYNTAX (char_int)]);
892 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
893 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
894 (Lisp_Object character)
896 int char_int, code;
897 CHECK_NUMBER (character);
898 char_int = XINT (character);
899 SETUP_BUFFER_SYNTAX_TABLE ();
900 code = SYNTAX (char_int);
901 if (code == Sopen || code == Sclose)
902 return SYNTAX_MATCH (char_int);
903 return Qnil;
906 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
907 doc: /* Convert a syntax specification STRING into syntax cell form.
908 STRING should be a string as it is allowed as argument of
909 `modify-syntax-entry'. Value is the equivalent cons cell
910 \(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
911 text property. */)
912 (Lisp_Object string)
914 register const unsigned char *p;
915 register enum syntaxcode code;
916 int val;
917 Lisp_Object match;
919 CHECK_STRING (string);
921 p = SDATA (string);
922 code = (enum syntaxcode) syntax_spec_code[*p++];
923 if (((int) code & 0377) == 0377)
924 error ("Invalid syntax description letter: %c", p[-1]);
926 if (code == Sinherit)
927 return Qnil;
929 if (*p)
931 int len;
932 int character = STRING_CHAR_AND_LENGTH (p, len);
933 XSETINT (match, character);
934 if (XFASTINT (match) == ' ')
935 match = Qnil;
936 p += len;
938 else
939 match = Qnil;
941 val = (int) code;
942 while (*p)
943 switch (*p++)
945 case '1':
946 val |= 1 << 16;
947 break;
949 case '2':
950 val |= 1 << 17;
951 break;
953 case '3':
954 val |= 1 << 18;
955 break;
957 case '4':
958 val |= 1 << 19;
959 break;
961 case 'p':
962 val |= 1 << 20;
963 break;
965 case 'b':
966 val |= 1 << 21;
967 break;
969 case 'n':
970 val |= 1 << 22;
971 break;
973 case 'c':
974 val |= 1 << 23;
975 break;
978 if (val < XVECTOR (Vsyntax_code_object)->size && NILP (match))
979 return XVECTOR (Vsyntax_code_object)->contents[val];
980 else
981 /* Since we can't use a shared object, let's make a new one. */
982 return Fcons (make_number (val), match);
985 /* I really don't know why this is interactive
986 help-form should at least be made useful whilst reading the second arg. */
987 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
988 "cSet syntax for character: \nsSet syntax for %s to: ",
989 doc: /* Set syntax for character CHAR according to string NEWENTRY.
990 The syntax is changed only for table SYNTAX-TABLE, which defaults to
991 the current buffer's syntax table.
992 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
993 in the range MIN to MAX are changed.
994 The first character of NEWENTRY should be one of the following:
995 Space or - whitespace syntax. w word constituent.
996 _ symbol constituent. . punctuation.
997 ( open-parenthesis. ) close-parenthesis.
998 " string quote. \\ escape.
999 $ paired delimiter. ' expression quote or prefix operator.
1000 < comment starter. > comment ender.
1001 / character-quote. @ inherit from `standard-syntax-table'.
1002 | generic string fence. ! generic comment fence.
1004 Only single-character comment start and end sequences are represented thus.
1005 Two-character sequences are represented as described below.
1006 The second character of NEWENTRY is the matching parenthesis,
1007 used only if the first character is `(' or `)'.
1008 Any additional characters are flags.
1009 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1010 1 means CHAR is the start of a two-char comment start sequence.
1011 2 means CHAR is the second character of such a sequence.
1012 3 means CHAR is the start of a two-char comment end sequence.
1013 4 means CHAR is the second character of such a sequence.
1015 There can be several orthogonal comment sequences. This is to support
1016 language modes such as C++. By default, all comment sequences are of style
1017 a, but you can set the comment sequence style to b (on the second character
1018 of a comment-start, and the first character of a comment-end sequence) and/or
1019 c (on any of its chars) using this flag:
1020 b means CHAR is part of comment sequence b.
1021 c means CHAR is part of comment sequence c.
1022 n means CHAR is part of a nestable comment sequence.
1024 p means CHAR is a prefix character for `backward-prefix-chars';
1025 such characters are treated as whitespace when they occur
1026 between expressions.
1027 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1028 (Lisp_Object c, Lisp_Object newentry, Lisp_Object syntax_table)
1030 if (CONSP (c))
1032 CHECK_CHARACTER_CAR (c);
1033 CHECK_CHARACTER_CDR (c);
1035 else
1036 CHECK_CHARACTER (c);
1038 if (NILP (syntax_table))
1039 syntax_table = BVAR (current_buffer, syntax_table);
1040 else
1041 check_syntax_table (syntax_table);
1043 newentry = Fstring_to_syntax (newentry);
1044 if (CONSP (c))
1045 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1046 else
1047 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
1049 /* We clear the regexp cache, since character classes can now have
1050 different values from those in the compiled regexps.*/
1051 clear_regexp_cache ();
1053 return Qnil;
1056 /* Dump syntax table to buffer in human-readable format */
1058 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1059 Sinternal_describe_syntax_value, 1, 1, 0,
1060 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1061 (Lisp_Object syntax)
1063 register enum syntaxcode code;
1064 int syntax_code;
1065 char desc, start1, start2, end1, end2, prefix,
1066 comstyleb, comstylec, comnested;
1067 char str[2];
1068 Lisp_Object first, match_lisp, value = syntax;
1070 if (NILP (value))
1072 insert_string ("default");
1073 return syntax;
1076 if (CHAR_TABLE_P (value))
1078 insert_string ("deeper char-table ...");
1079 return syntax;
1082 if (!CONSP (value))
1084 insert_string ("invalid");
1085 return syntax;
1088 first = XCAR (value);
1089 match_lisp = XCDR (value);
1091 if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp)))
1093 insert_string ("invalid");
1094 return syntax;
1097 syntax_code = XINT (first);
1098 code = (enum syntaxcode) (syntax_code & 0377);
1099 start1 = SYNTAX_FLAGS_COMSTART_FIRST (syntax_code);
1100 start2 = SYNTAX_FLAGS_COMSTART_SECOND (syntax_code);;
1101 end1 = SYNTAX_FLAGS_COMEND_FIRST (syntax_code);
1102 end2 = SYNTAX_FLAGS_COMEND_SECOND (syntax_code);
1103 prefix = SYNTAX_FLAGS_PREFIX (syntax_code);
1104 comstyleb = SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code);
1105 comstylec = SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code);
1106 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax_code);
1108 if ((int) code < 0 || (int) code >= (int) Smax)
1110 insert_string ("invalid");
1111 return syntax;
1113 desc = syntax_code_spec[(int) code];
1115 str[0] = desc, str[1] = 0;
1116 insert (str, 1);
1118 if (NILP (match_lisp))
1119 insert (" ", 1);
1120 else
1121 insert_char (XINT (match_lisp));
1123 if (start1)
1124 insert ("1", 1);
1125 if (start2)
1126 insert ("2", 1);
1128 if (end1)
1129 insert ("3", 1);
1130 if (end2)
1131 insert ("4", 1);
1133 if (prefix)
1134 insert ("p", 1);
1135 if (comstyleb)
1136 insert ("b", 1);
1137 if (comstylec)
1138 insert ("c", 1);
1139 if (comnested)
1140 insert ("n", 1);
1142 insert_string ("\twhich means: ");
1144 switch (SWITCH_ENUM_CAST (code))
1146 case Swhitespace:
1147 insert_string ("whitespace"); break;
1148 case Spunct:
1149 insert_string ("punctuation"); break;
1150 case Sword:
1151 insert_string ("word"); break;
1152 case Ssymbol:
1153 insert_string ("symbol"); break;
1154 case Sopen:
1155 insert_string ("open"); break;
1156 case Sclose:
1157 insert_string ("close"); break;
1158 case Squote:
1159 insert_string ("prefix"); break;
1160 case Sstring:
1161 insert_string ("string"); break;
1162 case Smath:
1163 insert_string ("math"); break;
1164 case Sescape:
1165 insert_string ("escape"); break;
1166 case Scharquote:
1167 insert_string ("charquote"); break;
1168 case Scomment:
1169 insert_string ("comment"); break;
1170 case Sendcomment:
1171 insert_string ("endcomment"); break;
1172 case Sinherit:
1173 insert_string ("inherit"); break;
1174 case Scomment_fence:
1175 insert_string ("comment fence"); break;
1176 case Sstring_fence:
1177 insert_string ("string fence"); break;
1178 default:
1179 insert_string ("invalid");
1180 return syntax;
1183 if (!NILP (match_lisp))
1185 insert_string (", matches ");
1186 insert_char (XINT (match_lisp));
1189 if (start1)
1190 insert_string (",\n\t is the first character of a comment-start sequence");
1191 if (start2)
1192 insert_string (",\n\t is the second character of a comment-start sequence");
1194 if (end1)
1195 insert_string (",\n\t is the first character of a comment-end sequence");
1196 if (end2)
1197 insert_string (",\n\t is the second character of a comment-end sequence");
1198 if (comstyleb)
1199 insert_string (" (comment style b)");
1200 if (comstylec)
1201 insert_string (" (comment style c)");
1202 if (comnested)
1203 insert_string (" (nestable)");
1205 if (prefix)
1206 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1208 return syntax;
1211 /* Return the position across COUNT words from FROM.
1212 If that many words cannot be found before the end of the buffer, return 0.
1213 COUNT negative means scan backward and stop at word beginning. */
1215 EMACS_INT
1216 scan_words (register EMACS_INT from, register EMACS_INT count)
1218 register EMACS_INT beg = BEGV;
1219 register EMACS_INT end = ZV;
1220 register EMACS_INT from_byte = CHAR_TO_BYTE (from);
1221 register enum syntaxcode code;
1222 int ch0, ch1;
1223 Lisp_Object func, pos;
1225 immediate_quit = 1;
1226 QUIT;
1228 SETUP_SYNTAX_TABLE (from, count);
1230 while (count > 0)
1232 while (1)
1234 if (from == end)
1236 immediate_quit = 0;
1237 return 0;
1239 UPDATE_SYNTAX_TABLE_FORWARD (from);
1240 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1241 code = SYNTAX (ch0);
1242 INC_BOTH (from, from_byte);
1243 if (words_include_escapes
1244 && (code == Sescape || code == Scharquote))
1245 break;
1246 if (code == Sword)
1247 break;
1249 /* Now CH0 is a character which begins a word and FROM is the
1250 position of the next character. */
1251 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
1252 if (! NILP (Ffboundp (func)))
1254 pos = call2 (func, make_number (from - 1), make_number (end));
1255 if (INTEGERP (pos) && XINT (pos) > from)
1257 from = XINT (pos);
1258 from_byte = CHAR_TO_BYTE (from);
1261 else
1263 while (1)
1265 if (from == end) break;
1266 UPDATE_SYNTAX_TABLE_FORWARD (from);
1267 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1268 code = SYNTAX (ch1);
1269 if ((code != Sword
1270 && (! words_include_escapes
1271 || (code != Sescape && code != Scharquote)))
1272 || word_boundary_p (ch0, ch1))
1273 break;
1274 INC_BOTH (from, from_byte);
1275 ch0 = ch1;
1278 count--;
1280 while (count < 0)
1282 while (1)
1284 if (from == beg)
1286 immediate_quit = 0;
1287 return 0;
1289 DEC_BOTH (from, from_byte);
1290 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1291 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1292 code = SYNTAX (ch1);
1293 if (words_include_escapes
1294 && (code == Sescape || code == Scharquote))
1295 break;
1296 if (code == Sword)
1297 break;
1299 /* Now CH1 is a character which ends a word and FROM is the
1300 position of it. */
1301 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
1302 if (! NILP (Ffboundp (func)))
1304 pos = call2 (func, make_number (from), make_number (beg));
1305 if (INTEGERP (pos) && XINT (pos) < from)
1307 from = XINT (pos);
1308 from_byte = CHAR_TO_BYTE (from);
1311 else
1313 while (1)
1315 if (from == beg)
1316 break;
1317 DEC_BOTH (from, from_byte);
1318 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1319 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1320 code = SYNTAX (ch0);
1321 if ((code != Sword
1322 && (! words_include_escapes
1323 || (code != Sescape && code != Scharquote)))
1324 || word_boundary_p (ch0, ch1))
1326 INC_BOTH (from, from_byte);
1327 break;
1329 ch1 = ch0;
1332 count++;
1335 immediate_quit = 0;
1337 return from;
1340 DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p",
1341 doc: /* Move point forward ARG words (backward if ARG is negative).
1342 Normally returns t.
1343 If an edge of the buffer or a field boundary is reached, point is left there
1344 and the function returns nil. Field boundaries are not noticed if
1345 `inhibit-field-text-motion' is non-nil. */)
1346 (Lisp_Object arg)
1348 Lisp_Object tmp;
1349 int orig_val, val;
1351 if (NILP (arg))
1352 XSETFASTINT (arg, 1);
1353 else
1354 CHECK_NUMBER (arg);
1356 val = orig_val = scan_words (PT, XINT (arg));
1357 if (! orig_val)
1358 val = XINT (arg) > 0 ? ZV : BEGV;
1360 /* Avoid jumping out of an input field. */
1361 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1362 Qt, Qnil, Qnil);
1363 val = XFASTINT (tmp);
1365 SET_PT (val);
1366 return val == orig_val ? Qt : Qnil;
1369 Lisp_Object skip_chars (int, Lisp_Object, Lisp_Object, int);
1371 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1372 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1373 STRING is like the inside of a `[...]' in a regular expression
1374 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1375 (but not at the end of a range; quoting is never needed there).
1376 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1377 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1378 Char classes, e.g. `[:alpha:]', are supported.
1380 Returns the distance traveled, either zero or positive. */)
1381 (Lisp_Object string, Lisp_Object lim)
1383 return skip_chars (1, string, lim, 1);
1386 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1387 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1388 See `skip-chars-forward' for details.
1389 Returns the distance traveled, either zero or negative. */)
1390 (Lisp_Object string, Lisp_Object lim)
1392 return skip_chars (0, string, lim, 1);
1395 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1396 doc: /* Move point forward across chars in specified syntax classes.
1397 SYNTAX is a string of syntax code characters.
1398 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1399 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1400 This function returns the distance traveled, either zero or positive. */)
1401 (Lisp_Object syntax, Lisp_Object lim)
1403 return skip_syntaxes (1, syntax, lim);
1406 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1407 doc: /* Move point backward across chars in specified syntax classes.
1408 SYNTAX is a string of syntax code characters.
1409 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1410 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1411 This function returns the distance traveled, either zero or negative. */)
1412 (Lisp_Object syntax, Lisp_Object lim)
1414 return skip_syntaxes (0, syntax, lim);
1417 static Lisp_Object
1418 skip_chars (int forwardp, Lisp_Object string, Lisp_Object lim, int handle_iso_classes)
1420 register unsigned int c;
1421 unsigned char fastmap[0400];
1422 /* Store the ranges of non-ASCII characters. */
1423 int *char_ranges IF_LINT (= NULL);
1424 int n_char_ranges = 0;
1425 int negate = 0;
1426 register EMACS_INT i, i_byte;
1427 /* Set to 1 if the current buffer is multibyte and the region
1428 contains non-ASCII chars. */
1429 int multibyte;
1430 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1431 chars. */
1432 int string_multibyte;
1433 EMACS_INT size_byte;
1434 const unsigned char *str;
1435 int len;
1436 Lisp_Object iso_classes;
1438 CHECK_STRING (string);
1439 iso_classes = Qnil;
1441 if (NILP (lim))
1442 XSETINT (lim, forwardp ? ZV : BEGV);
1443 else
1444 CHECK_NUMBER_COERCE_MARKER (lim);
1446 /* In any case, don't allow scan outside bounds of buffer. */
1447 if (XINT (lim) > ZV)
1448 XSETFASTINT (lim, ZV);
1449 if (XINT (lim) < BEGV)
1450 XSETFASTINT (lim, BEGV);
1452 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1453 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1454 string_multibyte = SBYTES (string) > SCHARS (string);
1456 memset (fastmap, 0, sizeof fastmap);
1458 str = SDATA (string);
1459 size_byte = SBYTES (string);
1461 i_byte = 0;
1462 if (i_byte < size_byte
1463 && SREF (string, 0) == '^')
1465 negate = 1; i_byte++;
1468 /* Find the characters specified and set their elements of fastmap.
1469 Handle backslashes and ranges specially.
1471 If STRING contains non-ASCII characters, setup char_ranges for
1472 them and use fastmap only for their leading codes. */
1474 if (! string_multibyte)
1476 int string_has_eight_bit = 0;
1478 /* At first setup fastmap. */
1479 while (i_byte < size_byte)
1481 c = str[i_byte++];
1483 if (handle_iso_classes && c == '['
1484 && i_byte < size_byte
1485 && str[i_byte] == ':')
1487 const unsigned char *class_beg = str + i_byte + 1;
1488 const unsigned char *class_end = class_beg;
1489 const unsigned char *class_limit = str + size_byte - 2;
1490 /* Leave room for the null. */
1491 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1492 re_wctype_t cc;
1494 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1495 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1497 while (class_end < class_limit
1498 && *class_end >= 'a' && *class_end <= 'z')
1499 class_end++;
1501 if (class_end == class_beg
1502 || *class_end != ':' || class_end[1] != ']')
1503 goto not_a_class_name;
1505 memcpy (class_name, class_beg, class_end - class_beg);
1506 class_name[class_end - class_beg] = 0;
1508 cc = re_wctype (class_name);
1509 if (cc == 0)
1510 error ("Invalid ISO C character class");
1512 iso_classes = Fcons (make_number (cc), iso_classes);
1514 i_byte = class_end + 2 - str;
1515 continue;
1518 not_a_class_name:
1519 if (c == '\\')
1521 if (i_byte == size_byte)
1522 break;
1524 c = str[i_byte++];
1526 /* Treat `-' as range character only if another character
1527 follows. */
1528 if (i_byte + 1 < size_byte
1529 && str[i_byte] == '-')
1531 unsigned int c2;
1533 /* Skip over the dash. */
1534 i_byte++;
1536 /* Get the end of the range. */
1537 c2 = str[i_byte++];
1538 if (c2 == '\\'
1539 && i_byte < size_byte)
1540 c2 = str[i_byte++];
1542 if (c <= c2)
1544 while (c <= c2)
1545 fastmap[c++] = 1;
1546 if (! ASCII_CHAR_P (c2))
1547 string_has_eight_bit = 1;
1550 else
1552 fastmap[c] = 1;
1553 if (! ASCII_CHAR_P (c))
1554 string_has_eight_bit = 1;
1558 /* If the current range is multibyte and STRING contains
1559 eight-bit chars, arrange fastmap and setup char_ranges for
1560 the corresponding multibyte chars. */
1561 if (multibyte && string_has_eight_bit)
1563 unsigned char fastmap2[0400];
1564 int range_start_byte, range_start_char;
1566 memcpy (fastmap + 0200, fastmap2 + 0200, 0200);
1567 memset (fastmap + 0200, 0, 0200);
1568 /* We are sure that this loop stops. */
1569 for (i = 0200; ! fastmap2[i]; i++);
1570 c = BYTE8_TO_CHAR (i);
1571 fastmap[CHAR_LEADING_CODE (c)] = 1;
1572 range_start_byte = i;
1573 range_start_char = c;
1574 char_ranges = (int *) alloca (sizeof (int) * 128 * 2);
1575 for (i = 129; i < 0400; i++)
1577 c = BYTE8_TO_CHAR (i);
1578 fastmap[CHAR_LEADING_CODE (c)] = 1;
1579 if (i - range_start_byte != c - range_start_char)
1581 char_ranges[n_char_ranges++] = range_start_char;
1582 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1583 + range_start_char);
1584 range_start_byte = i;
1585 range_start_char = c;
1588 char_ranges[n_char_ranges++] = range_start_char;
1589 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1590 + range_start_char);
1593 else /* STRING is multibyte */
1595 char_ranges = (int *) alloca (sizeof (int) * SCHARS (string) * 2);
1597 while (i_byte < size_byte)
1599 unsigned char leading_code;
1601 leading_code = str[i_byte];
1602 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1603 i_byte += len;
1605 if (handle_iso_classes && c == '['
1606 && i_byte < size_byte
1607 && STRING_CHAR (str + i_byte) == ':')
1609 const unsigned char *class_beg = str + i_byte + 1;
1610 const unsigned char *class_end = class_beg;
1611 const unsigned char *class_limit = str + size_byte - 2;
1612 /* Leave room for the null. */
1613 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1614 re_wctype_t cc;
1616 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1617 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1619 while (class_end < class_limit
1620 && *class_end >= 'a' && *class_end <= 'z')
1621 class_end++;
1623 if (class_end == class_beg
1624 || *class_end != ':' || class_end[1] != ']')
1625 goto not_a_class_name_multibyte;
1627 memcpy (class_name, class_beg, class_end - class_beg);
1628 class_name[class_end - class_beg] = 0;
1630 cc = re_wctype (class_name);
1631 if (cc == 0)
1632 error ("Invalid ISO C character class");
1634 iso_classes = Fcons (make_number (cc), iso_classes);
1636 i_byte = class_end + 2 - str;
1637 continue;
1640 not_a_class_name_multibyte:
1641 if (c == '\\')
1643 if (i_byte == size_byte)
1644 break;
1646 leading_code = str[i_byte];
1647 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1648 i_byte += len;
1650 /* Treat `-' as range character only if another character
1651 follows. */
1652 if (i_byte + 1 < size_byte
1653 && str[i_byte] == '-')
1655 unsigned int c2;
1656 unsigned char leading_code2;
1658 /* Skip over the dash. */
1659 i_byte++;
1661 /* Get the end of the range. */
1662 leading_code2 = str[i_byte];
1663 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1664 i_byte += len;
1666 if (c2 == '\\'
1667 && i_byte < size_byte)
1669 leading_code2 = str[i_byte];
1670 c2 =STRING_CHAR_AND_LENGTH (str + i_byte, len);
1671 i_byte += len;
1674 if (c > c2)
1675 continue;
1676 if (ASCII_CHAR_P (c))
1678 while (c <= c2 && c < 0x80)
1679 fastmap[c++] = 1;
1680 leading_code = CHAR_LEADING_CODE (c);
1682 if (! ASCII_CHAR_P (c))
1684 while (leading_code <= leading_code2)
1685 fastmap[leading_code++] = 1;
1686 if (c <= c2)
1688 char_ranges[n_char_ranges++] = c;
1689 char_ranges[n_char_ranges++] = c2;
1693 else
1695 if (ASCII_CHAR_P (c))
1696 fastmap[c] = 1;
1697 else
1699 fastmap[leading_code] = 1;
1700 char_ranges[n_char_ranges++] = c;
1701 char_ranges[n_char_ranges++] = c;
1706 /* If the current range is unibyte and STRING contains non-ASCII
1707 chars, arrange fastmap for the corresponding unibyte
1708 chars. */
1710 if (! multibyte && n_char_ranges > 0)
1712 memset (fastmap + 0200, 0, 0200);
1713 for (i = 0; i < n_char_ranges; i += 2)
1715 int c1 = char_ranges[i];
1716 int c2 = char_ranges[i + 1];
1718 for (; c1 <= c2; c1++)
1720 int b = CHAR_TO_BYTE_SAFE (c1);
1721 if (b >= 0)
1722 fastmap[b] = 1;
1728 /* If ^ was the first character, complement the fastmap. */
1729 if (negate)
1731 if (! multibyte)
1732 for (i = 0; i < sizeof fastmap; i++)
1733 fastmap[i] ^= 1;
1734 else
1736 for (i = 0; i < 0200; i++)
1737 fastmap[i] ^= 1;
1738 /* All non-ASCII chars possibly match. */
1739 for (; i < sizeof fastmap; i++)
1740 fastmap[i] = 1;
1745 EMACS_INT start_point = PT;
1746 EMACS_INT pos = PT;
1747 EMACS_INT pos_byte = PT_BYTE;
1748 unsigned char *p = PT_ADDR, *endp, *stop;
1750 if (forwardp)
1752 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1753 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1755 else
1757 endp = CHAR_POS_ADDR (XINT (lim));
1758 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1761 immediate_quit = 1;
1762 /* This code may look up syntax tables using macros that rely on the
1763 gl_state object. To make sure this object is not out of date,
1764 let's initialize it manually.
1765 We ignore syntax-table text-properties for now, since that's
1766 what we've done in the past. */
1767 SETUP_BUFFER_SYNTAX_TABLE ();
1768 if (forwardp)
1770 if (multibyte)
1771 while (1)
1773 int nbytes;
1775 if (p >= stop)
1777 if (p >= endp)
1778 break;
1779 p = GAP_END_ADDR;
1780 stop = endp;
1782 c = STRING_CHAR_AND_LENGTH (p, nbytes);
1783 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1785 if (negate)
1786 break;
1787 else
1788 goto fwd_ok;
1791 if (! fastmap[*p])
1792 break;
1793 if (! ASCII_CHAR_P (c))
1795 /* As we are looking at a multibyte character, we
1796 must look up the character in the table
1797 CHAR_RANGES. If there's no data in the table,
1798 that character is not what we want to skip. */
1800 /* The following code do the right thing even if
1801 n_char_ranges is zero (i.e. no data in
1802 CHAR_RANGES). */
1803 for (i = 0; i < n_char_ranges; i += 2)
1804 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1805 break;
1806 if (!(negate ^ (i < n_char_ranges)))
1807 break;
1809 fwd_ok:
1810 p += nbytes, pos++, pos_byte += nbytes;
1812 else
1813 while (1)
1815 if (p >= stop)
1817 if (p >= endp)
1818 break;
1819 p = GAP_END_ADDR;
1820 stop = endp;
1823 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1825 if (negate)
1826 break;
1827 else
1828 goto fwd_unibyte_ok;
1831 if (!fastmap[*p])
1832 break;
1833 fwd_unibyte_ok:
1834 p++, pos++, pos_byte++;
1837 else
1839 if (multibyte)
1840 while (1)
1842 unsigned char *prev_p;
1844 if (p <= stop)
1846 if (p <= endp)
1847 break;
1848 p = GPT_ADDR;
1849 stop = endp;
1851 prev_p = p;
1852 while (--p >= stop && ! CHAR_HEAD_P (*p));
1853 c = STRING_CHAR (p);
1855 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1857 if (negate)
1858 break;
1859 else
1860 goto back_ok;
1863 if (! fastmap[*p])
1864 break;
1865 if (! ASCII_CHAR_P (c))
1867 /* See the comment in the previous similar code. */
1868 for (i = 0; i < n_char_ranges; i += 2)
1869 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1870 break;
1871 if (!(negate ^ (i < n_char_ranges)))
1872 break;
1874 back_ok:
1875 pos--, pos_byte -= prev_p - p;
1877 else
1878 while (1)
1880 if (p <= stop)
1882 if (p <= endp)
1883 break;
1884 p = GPT_ADDR;
1885 stop = endp;
1888 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
1890 if (negate)
1891 break;
1892 else
1893 goto back_unibyte_ok;
1896 if (!fastmap[p[-1]])
1897 break;
1898 back_unibyte_ok:
1899 p--, pos--, pos_byte--;
1903 SET_PT_BOTH (pos, pos_byte);
1904 immediate_quit = 0;
1906 return make_number (PT - start_point);
1911 static Lisp_Object
1912 skip_syntaxes (int forwardp, Lisp_Object string, Lisp_Object lim)
1914 register unsigned int c;
1915 unsigned char fastmap[0400];
1916 int negate = 0;
1917 register EMACS_INT i, i_byte;
1918 int multibyte;
1919 EMACS_INT size_byte;
1920 unsigned char *str;
1922 CHECK_STRING (string);
1924 if (NILP (lim))
1925 XSETINT (lim, forwardp ? ZV : BEGV);
1926 else
1927 CHECK_NUMBER_COERCE_MARKER (lim);
1929 /* In any case, don't allow scan outside bounds of buffer. */
1930 if (XINT (lim) > ZV)
1931 XSETFASTINT (lim, ZV);
1932 if (XINT (lim) < BEGV)
1933 XSETFASTINT (lim, BEGV);
1935 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
1936 return make_number (0);
1938 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1939 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1941 memset (fastmap, 0, sizeof fastmap);
1943 if (SBYTES (string) > SCHARS (string))
1944 /* As this is very rare case (syntax spec is ASCII only), don't
1945 consider efficiency. */
1946 string = string_make_unibyte (string);
1948 str = SDATA (string);
1949 size_byte = SBYTES (string);
1951 i_byte = 0;
1952 if (i_byte < size_byte
1953 && SREF (string, 0) == '^')
1955 negate = 1; i_byte++;
1958 /* Find the syntaxes specified and set their elements of fastmap. */
1960 while (i_byte < size_byte)
1962 c = str[i_byte++];
1963 fastmap[syntax_spec_code[c]] = 1;
1966 /* If ^ was the first character, complement the fastmap. */
1967 if (negate)
1968 for (i = 0; i < sizeof fastmap; i++)
1969 fastmap[i] ^= 1;
1972 EMACS_INT start_point = PT;
1973 EMACS_INT pos = PT;
1974 EMACS_INT pos_byte = PT_BYTE;
1975 unsigned char *p = PT_ADDR, *endp, *stop;
1977 if (forwardp)
1979 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1980 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1982 else
1984 endp = CHAR_POS_ADDR (XINT (lim));
1985 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1988 immediate_quit = 1;
1989 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
1990 if (forwardp)
1992 if (multibyte)
1994 while (1)
1996 int nbytes;
1998 if (p >= stop)
2000 if (p >= endp)
2001 break;
2002 p = GAP_END_ADDR;
2003 stop = endp;
2005 c = STRING_CHAR_AND_LENGTH (p, nbytes);
2006 if (! fastmap[(int) SYNTAX (c)])
2007 break;
2008 p += nbytes, pos++, pos_byte += nbytes;
2009 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2012 else
2014 while (1)
2016 if (p >= stop)
2018 if (p >= endp)
2019 break;
2020 p = GAP_END_ADDR;
2021 stop = endp;
2023 if (! fastmap[(int) SYNTAX (*p)])
2024 break;
2025 p++, pos++, pos_byte++;
2026 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2030 else
2032 if (multibyte)
2034 while (1)
2036 unsigned char *prev_p;
2038 if (p <= stop)
2040 if (p <= endp)
2041 break;
2042 p = GPT_ADDR;
2043 stop = endp;
2045 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2046 prev_p = p;
2047 while (--p >= stop && ! CHAR_HEAD_P (*p));
2048 c = STRING_CHAR (p);
2049 if (! fastmap[(int) SYNTAX (c)])
2050 break;
2051 pos--, pos_byte -= prev_p - p;
2054 else
2056 while (1)
2058 if (p <= stop)
2060 if (p <= endp)
2061 break;
2062 p = GPT_ADDR;
2063 stop = endp;
2065 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2066 if (! fastmap[(int) SYNTAX (p[-1])])
2067 break;
2068 p--, pos--, pos_byte--;
2073 SET_PT_BOTH (pos, pos_byte);
2074 immediate_quit = 0;
2076 return make_number (PT - start_point);
2080 /* Return 1 if character C belongs to one of the ISO classes
2081 in the list ISO_CLASSES. Each class is represented by an
2082 integer which is its type according to re_wctype. */
2084 static int
2085 in_classes (int c, Lisp_Object iso_classes)
2087 int fits_class = 0;
2089 while (CONSP (iso_classes))
2091 Lisp_Object elt;
2092 elt = XCAR (iso_classes);
2093 iso_classes = XCDR (iso_classes);
2095 if (re_iswctype (c, XFASTINT (elt)))
2096 fits_class = 1;
2099 return fits_class;
2102 /* Jump over a comment, assuming we are at the beginning of one.
2103 FROM is the current position.
2104 FROM_BYTE is the bytepos corresponding to FROM.
2105 Do not move past STOP (a charpos).
2106 The comment over which we have to jump is of style STYLE
2107 (either SYNTAX_FLAGS_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2108 NESTING should be positive to indicate the nesting at the beginning
2109 for nested comments and should be zero or negative else.
2110 ST_COMMENT_STYLE cannot be nested.
2111 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2112 (or 0 If the search cannot start in the middle of a two-character).
2114 If successful, return 1 and store the charpos of the comment's end
2115 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2116 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2117 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2118 (as defined for state.incomment) in *INCOMMENT_PTR.
2120 The comment end is the last character of the comment rather than the
2121 character just after the comment.
2123 Global syntax data is assumed to initially be valid for FROM and
2124 remains valid for forward search starting at the returned position. */
2126 static int
2127 forw_comment (EMACS_INT from, EMACS_INT from_byte, EMACS_INT stop,
2128 int nesting, int style, int prev_syntax,
2129 EMACS_INT *charpos_ptr, EMACS_INT *bytepos_ptr,
2130 int *incomment_ptr)
2132 register int c, c1;
2133 register enum syntaxcode code;
2134 register int syntax, other_syntax;
2136 if (nesting <= 0) nesting = -1;
2138 /* Enter the loop in the middle so that we find
2139 a 2-char comment ender if we start in the middle of it. */
2140 syntax = prev_syntax;
2141 if (syntax != 0) goto forw_incomment;
2143 while (1)
2145 if (from == stop)
2147 *incomment_ptr = nesting;
2148 *charpos_ptr = from;
2149 *bytepos_ptr = from_byte;
2150 return 0;
2152 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2153 syntax = SYNTAX_WITH_FLAGS (c);
2154 code = syntax & 0xff;
2155 if (code == Sendcomment
2156 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style
2157 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2158 (nesting > 0 && --nesting == 0) : nesting < 0))
2159 /* we have encountered a comment end of the same style
2160 as the comment sequence which began this comment
2161 section */
2162 break;
2163 if (code == Scomment_fence
2164 && style == ST_COMMENT_STYLE)
2165 /* we have encountered a comment end of the same style
2166 as the comment sequence which began this comment
2167 section. */
2168 break;
2169 if (nesting > 0
2170 && code == Scomment
2171 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2172 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style)
2173 /* we have encountered a nested comment of the same style
2174 as the comment sequence which began this comment section */
2175 nesting++;
2176 INC_BOTH (from, from_byte);
2177 UPDATE_SYNTAX_TABLE_FORWARD (from);
2179 forw_incomment:
2180 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2181 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2182 other_syntax = SYNTAX_WITH_FLAGS (c1),
2183 SYNTAX_FLAGS_COMEND_SECOND (other_syntax))
2184 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, other_syntax) == style
2185 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2186 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax))
2187 ? nesting > 0 : nesting < 0))
2189 if (--nesting <= 0)
2190 /* we have encountered a comment end of the same style
2191 as the comment sequence which began this comment
2192 section */
2193 break;
2194 else
2196 INC_BOTH (from, from_byte);
2197 UPDATE_SYNTAX_TABLE_FORWARD (from);
2200 if (nesting > 0
2201 && from < stop
2202 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2203 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2204 other_syntax = SYNTAX_WITH_FLAGS (c1),
2205 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax) == style
2206 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2207 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2208 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax)))
2209 /* we have encountered a nested comment of the same style
2210 as the comment sequence which began this comment
2211 section */
2213 INC_BOTH (from, from_byte);
2214 UPDATE_SYNTAX_TABLE_FORWARD (from);
2215 nesting++;
2218 *charpos_ptr = from;
2219 *bytepos_ptr = from_byte;
2220 return 1;
2223 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2224 doc: /*
2225 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2226 Stop scanning if we find something other than a comment or whitespace.
2227 Set point to where scanning stops.
2228 If COUNT comments are found as expected, with nothing except whitespace
2229 between them, return t; otherwise return nil. */)
2230 (Lisp_Object count)
2232 register EMACS_INT from;
2233 EMACS_INT from_byte;
2234 register EMACS_INT stop;
2235 register int c, c1;
2236 register enum syntaxcode code;
2237 int comstyle = 0; /* style of comment encountered */
2238 int comnested = 0; /* whether the comment is nestable or not */
2239 int found;
2240 EMACS_INT count1;
2241 EMACS_INT out_charpos, out_bytepos;
2242 int dummy;
2244 CHECK_NUMBER (count);
2245 count1 = XINT (count);
2246 stop = count1 > 0 ? ZV : BEGV;
2248 immediate_quit = 1;
2249 QUIT;
2251 from = PT;
2252 from_byte = PT_BYTE;
2254 SETUP_SYNTAX_TABLE (from, count1);
2255 while (count1 > 0)
2259 int comstart_first, syntax, other_syntax;
2261 if (from == stop)
2263 SET_PT_BOTH (from, from_byte);
2264 immediate_quit = 0;
2265 return Qnil;
2267 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2268 syntax = SYNTAX_WITH_FLAGS (c);
2269 code = SYNTAX (c);
2270 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2271 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2272 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2273 INC_BOTH (from, from_byte);
2274 UPDATE_SYNTAX_TABLE_FORWARD (from);
2275 if (from < stop && comstart_first
2276 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2277 other_syntax = SYNTAX_WITH_FLAGS (c1),
2278 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)))
2280 /* We have encountered a comment start sequence and we
2281 are ignoring all text inside comments. We must record
2282 the comment style this sequence begins so that later,
2283 only a comment end of the same style actually ends
2284 the comment section. */
2285 code = Scomment;
2286 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2287 comnested
2288 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2289 INC_BOTH (from, from_byte);
2290 UPDATE_SYNTAX_TABLE_FORWARD (from);
2293 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2295 if (code == Scomment_fence)
2296 comstyle = ST_COMMENT_STYLE;
2297 else if (code != Scomment)
2299 immediate_quit = 0;
2300 DEC_BOTH (from, from_byte);
2301 SET_PT_BOTH (from, from_byte);
2302 return Qnil;
2304 /* We're at the start of a comment. */
2305 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2306 &out_charpos, &out_bytepos, &dummy);
2307 from = out_charpos; from_byte = out_bytepos;
2308 if (!found)
2310 immediate_quit = 0;
2311 SET_PT_BOTH (from, from_byte);
2312 return Qnil;
2314 INC_BOTH (from, from_byte);
2315 UPDATE_SYNTAX_TABLE_FORWARD (from);
2316 /* We have skipped one comment. */
2317 count1--;
2320 while (count1 < 0)
2322 while (1)
2324 int quoted, syntax;
2326 if (from <= stop)
2328 SET_PT_BOTH (BEGV, BEGV_BYTE);
2329 immediate_quit = 0;
2330 return Qnil;
2333 DEC_BOTH (from, from_byte);
2334 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2335 quoted = char_quoted (from, from_byte);
2336 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2337 syntax = SYNTAX_WITH_FLAGS (c);
2338 code = SYNTAX (c);
2339 comstyle = 0;
2340 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2341 if (code == Sendcomment)
2342 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2343 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2344 && prev_char_comend_first (from, from_byte)
2345 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2347 int other_syntax;
2348 /* We must record the comment style encountered so that
2349 later, we can match only the proper comment begin
2350 sequence of the same style. */
2351 DEC_BOTH (from, from_byte);
2352 code = Sendcomment;
2353 /* Calling char_quoted, above, set up global syntax position
2354 at the new value of FROM. */
2355 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2356 other_syntax = SYNTAX_WITH_FLAGS (c1);
2357 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2358 comnested
2359 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2362 if (code == Scomment_fence)
2364 /* Skip until first preceding unquoted comment_fence. */
2365 int fence_found = 0;
2366 EMACS_INT ini = from, ini_byte = from_byte;
2368 while (1)
2370 DEC_BOTH (from, from_byte);
2371 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2372 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2373 if (SYNTAX (c) == Scomment_fence
2374 && !char_quoted (from, from_byte))
2376 fence_found = 1;
2377 break;
2379 else if (from == stop)
2380 break;
2382 if (fence_found == 0)
2384 from = ini; /* Set point to ini + 1. */
2385 from_byte = ini_byte;
2386 goto leave;
2388 else
2389 /* We have skipped one comment. */
2390 break;
2392 else if (code == Sendcomment)
2394 found = back_comment (from, from_byte, stop, comnested, comstyle,
2395 &out_charpos, &out_bytepos);
2396 if (found == -1)
2398 if (c == '\n')
2399 /* This end-of-line is not an end-of-comment.
2400 Treat it like a whitespace.
2401 CC-mode (and maybe others) relies on this behavior. */
2403 else
2405 /* Failure: we should go back to the end of this
2406 not-quite-endcomment. */
2407 if (SYNTAX (c) != code)
2408 /* It was a two-char Sendcomment. */
2409 INC_BOTH (from, from_byte);
2410 goto leave;
2413 else
2415 /* We have skipped one comment. */
2416 from = out_charpos, from_byte = out_bytepos;
2417 break;
2420 else if (code != Swhitespace || quoted)
2422 leave:
2423 immediate_quit = 0;
2424 INC_BOTH (from, from_byte);
2425 SET_PT_BOTH (from, from_byte);
2426 return Qnil;
2430 count1++;
2433 SET_PT_BOTH (from, from_byte);
2434 immediate_quit = 0;
2435 return Qt;
2438 /* Return syntax code of character C if C is an ASCII character
2439 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2441 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2442 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2443 ? SYNTAX (c) : Ssymbol)
2445 static Lisp_Object
2446 scan_lists (register EMACS_INT from, EMACS_INT count, EMACS_INT depth, int sexpflag)
2448 Lisp_Object val;
2449 register EMACS_INT stop = count > 0 ? ZV : BEGV;
2450 register int c, c1;
2451 int stringterm;
2452 int quoted;
2453 int mathexit = 0;
2454 register enum syntaxcode code, temp_code;
2455 int min_depth = depth; /* Err out if depth gets less than this. */
2456 int comstyle = 0; /* style of comment encountered */
2457 int comnested = 0; /* whether the comment is nestable or not */
2458 EMACS_INT temp_pos;
2459 EMACS_INT last_good = from;
2460 int found;
2461 EMACS_INT from_byte;
2462 EMACS_INT out_bytepos, out_charpos;
2463 int temp, dummy;
2464 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2466 if (depth > 0) min_depth = 0;
2468 if (from > ZV) from = ZV;
2469 if (from < BEGV) from = BEGV;
2471 from_byte = CHAR_TO_BYTE (from);
2473 immediate_quit = 1;
2474 QUIT;
2476 SETUP_SYNTAX_TABLE (from, count);
2477 while (count > 0)
2479 while (from < stop)
2481 int comstart_first, prefix, syntax, other_syntax;
2482 UPDATE_SYNTAX_TABLE_FORWARD (from);
2483 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2484 syntax = SYNTAX_WITH_FLAGS (c);
2485 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2486 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2487 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2488 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2489 prefix = SYNTAX_FLAGS_PREFIX (syntax);
2490 if (depth == min_depth)
2491 last_good = from;
2492 INC_BOTH (from, from_byte);
2493 UPDATE_SYNTAX_TABLE_FORWARD (from);
2494 if (from < stop && comstart_first
2495 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2496 other_syntax = SYNTAX_WITH_FLAGS (c),
2497 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2498 && parse_sexp_ignore_comments)
2500 /* we have encountered a comment start sequence and we
2501 are ignoring all text inside comments. We must record
2502 the comment style this sequence begins so that later,
2503 only a comment end of the same style actually ends
2504 the comment section */
2505 code = Scomment;
2506 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2507 comnested
2508 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2509 INC_BOTH (from, from_byte);
2510 UPDATE_SYNTAX_TABLE_FORWARD (from);
2513 if (prefix)
2514 continue;
2516 switch (SWITCH_ENUM_CAST (code))
2518 case Sescape:
2519 case Scharquote:
2520 if (from == stop)
2521 goto lose;
2522 INC_BOTH (from, from_byte);
2523 /* treat following character as a word constituent */
2524 case Sword:
2525 case Ssymbol:
2526 if (depth || !sexpflag) break;
2527 /* This word counts as a sexp; return at end of it. */
2528 while (from < stop)
2530 UPDATE_SYNTAX_TABLE_FORWARD (from);
2532 /* Some compilers can't handle this inside the switch. */
2533 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2534 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2535 switch (temp)
2537 case Scharquote:
2538 case Sescape:
2539 INC_BOTH (from, from_byte);
2540 if (from == stop)
2541 goto lose;
2542 break;
2543 case Sword:
2544 case Ssymbol:
2545 case Squote:
2546 break;
2547 default:
2548 goto done;
2550 INC_BOTH (from, from_byte);
2552 goto done;
2554 case Scomment_fence:
2555 comstyle = ST_COMMENT_STYLE;
2556 /* FALLTHROUGH */
2557 case Scomment:
2558 if (!parse_sexp_ignore_comments) break;
2559 UPDATE_SYNTAX_TABLE_FORWARD (from);
2560 found = forw_comment (from, from_byte, stop,
2561 comnested, comstyle, 0,
2562 &out_charpos, &out_bytepos, &dummy);
2563 from = out_charpos, from_byte = out_bytepos;
2564 if (!found)
2566 if (depth == 0)
2567 goto done;
2568 goto lose;
2570 INC_BOTH (from, from_byte);
2571 UPDATE_SYNTAX_TABLE_FORWARD (from);
2572 break;
2574 case Smath:
2575 if (!sexpflag)
2576 break;
2577 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2579 INC_BOTH (from, from_byte);
2581 if (mathexit)
2583 mathexit = 0;
2584 goto close1;
2586 mathexit = 1;
2588 case Sopen:
2589 if (!++depth) goto done;
2590 break;
2592 case Sclose:
2593 close1:
2594 if (!--depth) goto done;
2595 if (depth < min_depth)
2596 xsignal3 (Qscan_error,
2597 build_string ("Containing expression ends prematurely"),
2598 make_number (last_good), make_number (from));
2599 break;
2601 case Sstring:
2602 case Sstring_fence:
2603 temp_pos = dec_bytepos (from_byte);
2604 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2605 while (1)
2607 if (from >= stop)
2608 goto lose;
2609 UPDATE_SYNTAX_TABLE_FORWARD (from);
2610 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2611 if (code == Sstring
2612 ? (c == stringterm
2613 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2614 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
2615 break;
2617 /* Some compilers can't handle this inside the switch. */
2618 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2619 switch (temp)
2621 case Scharquote:
2622 case Sescape:
2623 INC_BOTH (from, from_byte);
2625 INC_BOTH (from, from_byte);
2627 INC_BOTH (from, from_byte);
2628 if (!depth && sexpflag) goto done;
2629 break;
2630 default:
2631 /* Ignore whitespace, punctuation, quote, endcomment. */
2632 break;
2636 /* Reached end of buffer. Error if within object, return nil if between */
2637 if (depth)
2638 goto lose;
2640 immediate_quit = 0;
2641 return Qnil;
2643 /* End of object reached */
2644 done:
2645 count--;
2648 while (count < 0)
2650 while (from > stop)
2652 int syntax;
2653 DEC_BOTH (from, from_byte);
2654 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2655 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2656 syntax= SYNTAX_WITH_FLAGS (c);
2657 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2658 if (depth == min_depth)
2659 last_good = from;
2660 comstyle = 0;
2661 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2662 if (code == Sendcomment)
2663 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2664 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2665 && prev_char_comend_first (from, from_byte)
2666 && parse_sexp_ignore_comments)
2668 /* We must record the comment style encountered so that
2669 later, we can match only the proper comment begin
2670 sequence of the same style. */
2671 int c2, other_syntax;
2672 DEC_BOTH (from, from_byte);
2673 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2674 code = Sendcomment;
2675 c2 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2676 other_syntax = SYNTAX_WITH_FLAGS (c2);
2677 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2678 comnested
2679 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2682 /* Quoting turns anything except a comment-ender
2683 into a word character. Note that this cannot be true
2684 if we decremented FROM in the if-statement above. */
2685 if (code != Sendcomment && char_quoted (from, from_byte))
2687 DEC_BOTH (from, from_byte);
2688 code = Sword;
2690 else if (SYNTAX_FLAGS_PREFIX (syntax))
2691 continue;
2693 switch (SWITCH_ENUM_CAST (code))
2695 case Sword:
2696 case Ssymbol:
2697 case Sescape:
2698 case Scharquote:
2699 if (depth || !sexpflag) break;
2700 /* This word counts as a sexp; count object finished
2701 after passing it. */
2702 while (from > stop)
2704 temp_pos = from_byte;
2705 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2706 DEC_POS (temp_pos);
2707 else
2708 temp_pos--;
2709 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2710 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2711 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2712 /* Don't allow comment-end to be quoted. */
2713 if (temp_code == Sendcomment)
2714 goto done2;
2715 quoted = char_quoted (from - 1, temp_pos);
2716 if (quoted)
2718 DEC_BOTH (from, from_byte);
2719 temp_pos = dec_bytepos (temp_pos);
2720 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2722 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2723 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2724 if (! (quoted || temp_code == Sword
2725 || temp_code == Ssymbol
2726 || temp_code == Squote))
2727 goto done2;
2728 DEC_BOTH (from, from_byte);
2730 goto done2;
2732 case Smath:
2733 if (!sexpflag)
2734 break;
2735 temp_pos = dec_bytepos (from_byte);
2736 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2737 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2738 DEC_BOTH (from, from_byte);
2739 if (mathexit)
2741 mathexit = 0;
2742 goto open2;
2744 mathexit = 1;
2746 case Sclose:
2747 if (!++depth) goto done2;
2748 break;
2750 case Sopen:
2751 open2:
2752 if (!--depth) goto done2;
2753 if (depth < min_depth)
2754 xsignal3 (Qscan_error,
2755 build_string ("Containing expression ends prematurely"),
2756 make_number (last_good), make_number (from));
2757 break;
2759 case Sendcomment:
2760 if (!parse_sexp_ignore_comments)
2761 break;
2762 found = back_comment (from, from_byte, stop, comnested, comstyle,
2763 &out_charpos, &out_bytepos);
2764 /* FIXME: if found == -1, then it really wasn't a comment-end.
2765 For single-char Sendcomment, we can't do much about it apart
2766 from skipping the char.
2767 For 2-char endcomments, we could try again, taking both
2768 chars as separate entities, but it's a lot of trouble
2769 for very little gain, so we don't bother either. -sm */
2770 if (found != -1)
2771 from = out_charpos, from_byte = out_bytepos;
2772 break;
2774 case Scomment_fence:
2775 case Sstring_fence:
2776 while (1)
2778 if (from == stop)
2779 goto lose;
2780 DEC_BOTH (from, from_byte);
2781 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2782 if (!char_quoted (from, from_byte)
2783 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2784 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
2785 break;
2787 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2788 break;
2790 case Sstring:
2791 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2792 while (1)
2794 if (from == stop)
2795 goto lose;
2796 DEC_BOTH (from, from_byte);
2797 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2798 if (!char_quoted (from, from_byte)
2799 && (stringterm
2800 == (c = FETCH_CHAR_AS_MULTIBYTE (from_byte)))
2801 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2802 break;
2804 if (!depth && sexpflag) goto done2;
2805 break;
2806 default:
2807 /* Ignore whitespace, punctuation, quote, endcomment. */
2808 break;
2812 /* Reached start of buffer. Error if within object, return nil if between */
2813 if (depth)
2814 goto lose;
2816 immediate_quit = 0;
2817 return Qnil;
2819 done2:
2820 count++;
2824 immediate_quit = 0;
2825 XSETFASTINT (val, from);
2826 return val;
2828 lose:
2829 xsignal3 (Qscan_error,
2830 build_string ("Unbalanced parentheses"),
2831 make_number (last_good), make_number (from));
2834 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2835 doc: /* Scan from character number FROM by COUNT lists.
2836 Returns the character number of the position thus found.
2838 If DEPTH is nonzero, paren depth begins counting from that value,
2839 only places where the depth in parentheses becomes zero
2840 are candidates for stopping; COUNT such places are counted.
2841 Thus, a positive value for DEPTH means go out levels.
2843 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2845 If the beginning or end of (the accessible part of) the buffer is reached
2846 and the depth is wrong, an error is signaled.
2847 If the depth is right but the count is not used up, nil is returned. */)
2848 (Lisp_Object from, Lisp_Object count, Lisp_Object depth)
2850 CHECK_NUMBER (from);
2851 CHECK_NUMBER (count);
2852 CHECK_NUMBER (depth);
2854 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2857 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2858 doc: /* Scan from character number FROM by COUNT balanced expressions.
2859 If COUNT is negative, scan backwards.
2860 Returns the character number of the position thus found.
2862 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2864 If the beginning or end of (the accessible part of) the buffer is reached
2865 in the middle of a parenthetical grouping, an error is signaled.
2866 If the beginning or end is reached between groupings
2867 but before count is used up, nil is returned. */)
2868 (Lisp_Object from, Lisp_Object count)
2870 CHECK_NUMBER (from);
2871 CHECK_NUMBER (count);
2873 return scan_lists (XINT (from), XINT (count), 0, 1);
2876 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2877 0, 0, 0,
2878 doc: /* Move point backward over any number of chars with prefix syntax.
2879 This includes chars with "quote" or "prefix" syntax (' or p). */)
2880 (void)
2882 EMACS_INT beg = BEGV;
2883 EMACS_INT opoint = PT;
2884 EMACS_INT opoint_byte = PT_BYTE;
2885 EMACS_INT pos = PT;
2886 EMACS_INT pos_byte = PT_BYTE;
2887 int c;
2889 if (pos <= beg)
2891 SET_PT_BOTH (opoint, opoint_byte);
2893 return Qnil;
2896 SETUP_SYNTAX_TABLE (pos, -1);
2898 DEC_BOTH (pos, pos_byte);
2900 while (!char_quoted (pos, pos_byte)
2901 /* Previous statement updates syntax table. */
2902 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
2903 || SYNTAX_PREFIX (c)))
2905 opoint = pos;
2906 opoint_byte = pos_byte;
2908 if (pos + 1 > beg)
2909 DEC_BOTH (pos, pos_byte);
2912 SET_PT_BOTH (opoint, opoint_byte);
2914 return Qnil;
2917 /* Parse forward from FROM / FROM_BYTE to END,
2918 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2919 and return a description of the state of the parse at END.
2920 If STOPBEFORE is nonzero, stop at the start of an atom.
2921 If COMMENTSTOP is 1, stop at the start of a comment.
2922 If COMMENTSTOP is -1, stop at the start or end of a comment,
2923 after the beginning of a string, or after the end of a string. */
2925 static void
2926 scan_sexps_forward (struct lisp_parse_state *stateptr,
2927 EMACS_INT from, EMACS_INT from_byte, EMACS_INT end,
2928 int targetdepth, int stopbefore,
2929 Lisp_Object oldstate, int commentstop)
2931 struct lisp_parse_state state;
2933 register enum syntaxcode code;
2934 int c1;
2935 int comnested;
2936 struct level { int last, prev; };
2937 struct level levelstart[100];
2938 register struct level *curlevel = levelstart;
2939 struct level *endlevel = levelstart + 100;
2940 register int depth; /* Paren depth of current scanning location.
2941 level - levelstart equals this except
2942 when the depth becomes negative. */
2943 int mindepth; /* Lowest DEPTH value seen. */
2944 int start_quoted = 0; /* Nonzero means starting after a char quote */
2945 Lisp_Object tem;
2946 EMACS_INT prev_from; /* Keep one character before FROM. */
2947 EMACS_INT prev_from_byte;
2948 int prev_from_syntax;
2949 int boundary_stop = commentstop == -1;
2950 int nofence;
2951 int found;
2952 EMACS_INT out_bytepos, out_charpos;
2953 int temp;
2955 prev_from = from;
2956 prev_from_byte = from_byte;
2957 if (from != BEGV)
2958 DEC_BOTH (prev_from, prev_from_byte);
2960 /* Use this macro instead of `from++'. */
2961 #define INC_FROM \
2962 do { prev_from = from; \
2963 prev_from_byte = from_byte; \
2964 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2965 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2966 INC_BOTH (from, from_byte); \
2967 if (from < end) \
2968 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2969 } while (0)
2971 immediate_quit = 1;
2972 QUIT;
2974 if (NILP (oldstate))
2976 depth = 0;
2977 state.instring = -1;
2978 state.incomment = 0;
2979 state.comstyle = 0; /* comment style a by default. */
2980 state.comstr_start = -1; /* no comment/string seen. */
2982 else
2984 tem = Fcar (oldstate);
2985 if (!NILP (tem))
2986 depth = XINT (tem);
2987 else
2988 depth = 0;
2990 oldstate = Fcdr (oldstate);
2991 oldstate = Fcdr (oldstate);
2992 oldstate = Fcdr (oldstate);
2993 tem = Fcar (oldstate);
2994 /* Check whether we are inside string_fence-style string: */
2995 state.instring = (!NILP (tem)
2996 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
2997 : -1);
2999 oldstate = Fcdr (oldstate);
3000 tem = Fcar (oldstate);
3001 state.incomment = (!NILP (tem)
3002 ? (INTEGERP (tem) ? XINT (tem) : -1)
3003 : 0);
3005 oldstate = Fcdr (oldstate);
3006 tem = Fcar (oldstate);
3007 start_quoted = !NILP (tem);
3009 /* if the eighth element of the list is nil, we are in comment
3010 style a. If it is non-nil, we are in comment style b */
3011 oldstate = Fcdr (oldstate);
3012 oldstate = Fcdr (oldstate);
3013 tem = Fcar (oldstate);
3014 state.comstyle = (NILP (tem)
3016 : (EQ (tem, Qsyntax_table)
3017 ? ST_COMMENT_STYLE
3018 : INTEGERP (tem) ? XINT (tem) : 1));
3020 oldstate = Fcdr (oldstate);
3021 tem = Fcar (oldstate);
3022 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
3023 oldstate = Fcdr (oldstate);
3024 tem = Fcar (oldstate);
3025 while (!NILP (tem)) /* >= second enclosing sexps. */
3027 /* curlevel++->last ran into compiler bug on Apollo */
3028 curlevel->last = XINT (Fcar (tem));
3029 if (++curlevel == endlevel)
3030 curlevel--; /* error ("Nesting too deep for parser"); */
3031 curlevel->prev = -1;
3032 curlevel->last = -1;
3033 tem = Fcdr (tem);
3036 state.quoted = 0;
3037 mindepth = depth;
3039 curlevel->prev = -1;
3040 curlevel->last = -1;
3042 SETUP_SYNTAX_TABLE (prev_from, 1);
3043 temp = FETCH_CHAR (prev_from_byte);
3044 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
3045 UPDATE_SYNTAX_TABLE_FORWARD (from);
3047 /* Enter the loop at a place appropriate for initial state. */
3049 if (state.incomment)
3050 goto startincomment;
3051 if (state.instring >= 0)
3053 nofence = state.instring != ST_STRING_STYLE;
3054 if (start_quoted)
3055 goto startquotedinstring;
3056 goto startinstring;
3058 else if (start_quoted)
3059 goto startquoted;
3061 while (from < end)
3063 int syntax;
3064 INC_FROM;
3065 code = prev_from_syntax & 0xff;
3067 if (from < end
3068 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3069 && (c1 = FETCH_CHAR (from_byte),
3070 syntax = SYNTAX_WITH_FLAGS (c1),
3071 SYNTAX_FLAGS_COMSTART_SECOND (syntax)))
3072 /* Duplicate code to avoid a complex if-expression
3073 which causes trouble for the SGI compiler. */
3075 /* Record the comment style we have entered so that only
3076 the comment-end sequence of the same style actually
3077 terminates the comment section. */
3078 state.comstyle
3079 = SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_from_syntax);
3080 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
3081 comnested = comnested || SYNTAX_FLAGS_COMMENT_NESTED (syntax);
3082 state.incomment = comnested ? 1 : -1;
3083 state.comstr_start = prev_from;
3084 INC_FROM;
3085 code = Scomment;
3087 else if (code == Scomment_fence)
3089 /* Record the comment style we have entered so that only
3090 the comment-end sequence of the same style actually
3091 terminates the comment section. */
3092 state.comstyle = ST_COMMENT_STYLE;
3093 state.incomment = -1;
3094 state.comstr_start = prev_from;
3095 code = Scomment;
3097 else if (code == Scomment)
3099 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax, 0);
3100 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3101 1 : -1);
3102 state.comstr_start = prev_from;
3105 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3106 continue;
3107 switch (SWITCH_ENUM_CAST (code))
3109 case Sescape:
3110 case Scharquote:
3111 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3112 curlevel->last = prev_from;
3113 startquoted:
3114 if (from == end) goto endquoted;
3115 INC_FROM;
3116 goto symstarted;
3117 /* treat following character as a word constituent */
3118 case Sword:
3119 case Ssymbol:
3120 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3121 curlevel->last = prev_from;
3122 symstarted:
3123 while (from < end)
3125 /* Some compilers can't handle this inside the switch. */
3126 temp = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3127 temp = SYNTAX (temp);
3128 switch (temp)
3130 case Scharquote:
3131 case Sescape:
3132 INC_FROM;
3133 if (from == end) goto endquoted;
3134 break;
3135 case Sword:
3136 case Ssymbol:
3137 case Squote:
3138 break;
3139 default:
3140 goto symdone;
3142 INC_FROM;
3144 symdone:
3145 curlevel->prev = curlevel->last;
3146 break;
3148 case Scomment_fence: /* Can't happen because it's handled above. */
3149 case Scomment:
3150 if (commentstop || boundary_stop) goto done;
3151 startincomment:
3152 /* The (from == BEGV) test was to enter the loop in the middle so
3153 that we find a 2-char comment ender even if we start in the
3154 middle of it. We don't want to do that if we're just at the
3155 beginning of the comment (think of (*) ... (*)). */
3156 found = forw_comment (from, from_byte, end,
3157 state.incomment, state.comstyle,
3158 (from == BEGV || from < state.comstr_start + 3)
3159 ? 0 : prev_from_syntax,
3160 &out_charpos, &out_bytepos, &state.incomment);
3161 from = out_charpos; from_byte = out_bytepos;
3162 /* Beware! prev_from and friends are invalid now.
3163 Luckily, the `done' doesn't use them and the INC_FROM
3164 sets them to a sane value without looking at them. */
3165 if (!found) goto done;
3166 INC_FROM;
3167 state.incomment = 0;
3168 state.comstyle = 0; /* reset the comment style */
3169 if (boundary_stop) goto done;
3170 break;
3172 case Sopen:
3173 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3174 depth++;
3175 /* curlevel++->last ran into compiler bug on Apollo */
3176 curlevel->last = prev_from;
3177 if (++curlevel == endlevel)
3178 curlevel--; /* error ("Nesting too deep for parser"); */
3179 curlevel->prev = -1;
3180 curlevel->last = -1;
3181 if (targetdepth == depth) goto done;
3182 break;
3184 case Sclose:
3185 depth--;
3186 if (depth < mindepth)
3187 mindepth = depth;
3188 if (curlevel != levelstart)
3189 curlevel--;
3190 curlevel->prev = curlevel->last;
3191 if (targetdepth == depth) goto done;
3192 break;
3194 case Sstring:
3195 case Sstring_fence:
3196 state.comstr_start = from - 1;
3197 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3198 curlevel->last = prev_from;
3199 state.instring = (code == Sstring
3200 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3201 : ST_STRING_STYLE);
3202 if (boundary_stop) goto done;
3203 startinstring:
3205 nofence = state.instring != ST_STRING_STYLE;
3207 while (1)
3209 int c;
3211 if (from >= end) goto done;
3212 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3213 /* Some compilers can't handle this inside the switch. */
3214 temp = SYNTAX (c);
3216 /* Check TEMP here so that if the char has
3217 a syntax-table property which says it is NOT
3218 a string character, it does not end the string. */
3219 if (nofence && c == state.instring && temp == Sstring)
3220 break;
3222 switch (temp)
3224 case Sstring_fence:
3225 if (!nofence) goto string_end;
3226 break;
3227 case Scharquote:
3228 case Sescape:
3229 INC_FROM;
3230 startquotedinstring:
3231 if (from >= end) goto endquoted;
3233 INC_FROM;
3236 string_end:
3237 state.instring = -1;
3238 curlevel->prev = curlevel->last;
3239 INC_FROM;
3240 if (boundary_stop) goto done;
3241 break;
3243 case Smath:
3244 /* FIXME: We should do something with it. */
3245 break;
3246 default:
3247 /* Ignore whitespace, punctuation, quote, endcomment. */
3248 break;
3251 goto done;
3253 stop: /* Here if stopping before start of sexp. */
3254 from = prev_from; /* We have just fetched the char that starts it; */
3255 goto done; /* but return the position before it. */
3257 endquoted:
3258 state.quoted = 1;
3259 done:
3260 state.depth = depth;
3261 state.mindepth = mindepth;
3262 state.thislevelstart = curlevel->prev;
3263 state.prevlevelstart
3264 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3265 state.location = from;
3266 state.levelstarts = Qnil;
3267 while (curlevel > levelstart)
3268 state.levelstarts = Fcons (make_number ((--curlevel)->last),
3269 state.levelstarts);
3270 immediate_quit = 0;
3272 *stateptr = state;
3275 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3276 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3277 Parsing stops at TO or when certain criteria are met;
3278 point is set to where parsing stops.
3279 If fifth arg OLDSTATE is omitted or nil,
3280 parsing assumes that FROM is the beginning of a function.
3281 Value is a list of elements describing final state of parsing:
3282 0. depth in parens.
3283 1. character address of start of innermost containing list; nil if none.
3284 2. character address of start of last complete sexp terminated.
3285 3. non-nil if inside a string.
3286 (it is the character that will terminate the string,
3287 or t if the string should be terminated by a generic string delimiter.)
3288 4. nil if outside a comment, t if inside a non-nestable comment,
3289 else an integer (the current comment nesting).
3290 5. t if following a quote character.
3291 6. the minimum paren-depth encountered during this scan.
3292 7. style of comment, if any.
3293 8. character address of start of comment or string; nil if not in one.
3294 9. Intermediate data for continuation of parsing (subject to change).
3295 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3296 in parentheses becomes equal to TARGETDEPTH.
3297 Fourth arg STOPBEFORE non-nil means stop when come to
3298 any character that starts a sexp.
3299 Fifth arg OLDSTATE is a list like what this function returns.
3300 It is used to initialize the state of the parse. Elements number 1, 2, 6
3301 and 8 are ignored.
3302 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3303 If it is symbol `syntax-table', stop after the start of a comment or a
3304 string, or after end of a comment or a string. */)
3305 (Lisp_Object from, Lisp_Object to, Lisp_Object targetdepth, Lisp_Object stopbefore, Lisp_Object oldstate, Lisp_Object commentstop)
3307 struct lisp_parse_state state;
3308 int target;
3310 if (!NILP (targetdepth))
3312 CHECK_NUMBER (targetdepth);
3313 target = XINT (targetdepth);
3315 else
3316 target = -100000; /* We won't reach this depth */
3318 validate_region (&from, &to);
3319 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3320 XINT (to),
3321 target, !NILP (stopbefore), oldstate,
3322 (NILP (commentstop)
3323 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3325 SET_PT (state.location);
3327 return Fcons (make_number (state.depth),
3328 Fcons (state.prevlevelstart < 0
3329 ? Qnil : make_number (state.prevlevelstart),
3330 Fcons (state.thislevelstart < 0
3331 ? Qnil : make_number (state.thislevelstart),
3332 Fcons (state.instring >= 0
3333 ? (state.instring == ST_STRING_STYLE
3334 ? Qt : make_number (state.instring)) : Qnil,
3335 Fcons (state.incomment < 0 ? Qt :
3336 (state.incomment == 0 ? Qnil :
3337 make_number (state.incomment)),
3338 Fcons (state.quoted ? Qt : Qnil,
3339 Fcons (make_number (state.mindepth),
3340 Fcons ((state.comstyle
3341 ? (state.comstyle == ST_COMMENT_STYLE
3342 ? Qsyntax_table
3343 : make_number (state.comstyle))
3344 : Qnil),
3345 Fcons (((state.incomment
3346 || (state.instring >= 0))
3347 ? make_number (state.comstr_start)
3348 : Qnil),
3349 Fcons (state.levelstarts, Qnil))))))))));
3352 void
3353 init_syntax_once (void)
3355 register int i, c;
3356 Lisp_Object temp;
3358 /* This has to be done here, before we call Fmake_char_table. */
3359 Qsyntax_table = intern_c_string ("syntax-table");
3360 staticpro (&Qsyntax_table);
3362 /* Intern_C_String this now in case it isn't already done.
3363 Setting this variable twice is harmless.
3364 But don't staticpro it here--that is done in alloc.c. */
3365 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
3367 /* Create objects which can be shared among syntax tables. */
3368 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
3369 for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++)
3370 XVECTOR (Vsyntax_code_object)->contents[i]
3371 = Fcons (make_number (i), Qnil);
3373 /* Now we are ready to set up this property, so we can
3374 create syntax tables. */
3375 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3377 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3379 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3381 /* Control characters should not be whitespace. */
3382 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3383 for (i = 0; i <= ' ' - 1; i++)
3384 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3385 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3387 /* Except that a few really are whitespace. */
3388 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3389 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3390 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3391 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3392 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3393 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3395 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3396 for (i = 'a'; i <= 'z'; i++)
3397 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3398 for (i = 'A'; i <= 'Z'; i++)
3399 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3400 for (i = '0'; i <= '9'; i++)
3401 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3403 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3404 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3406 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3407 Fcons (make_number (Sopen), make_number (')')));
3408 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3409 Fcons (make_number (Sclose), make_number ('(')));
3410 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3411 Fcons (make_number (Sopen), make_number (']')));
3412 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3413 Fcons (make_number (Sclose), make_number ('[')));
3414 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3415 Fcons (make_number (Sopen), make_number ('}')));
3416 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3417 Fcons (make_number (Sclose), make_number ('{')));
3418 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3419 Fcons (make_number ((int) Sstring), Qnil));
3420 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3421 Fcons (make_number ((int) Sescape), Qnil));
3423 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
3424 for (i = 0; i < 10; i++)
3426 c = "_-+*/&|<>="[i];
3427 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3430 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3431 for (i = 0; i < 12; i++)
3433 c = ".,;:?!#@~^'`"[i];
3434 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3437 /* All multibyte characters have syntax `word' by default. */
3438 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3439 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3442 void
3443 syms_of_syntax (void)
3445 Qsyntax_table_p = intern_c_string ("syntax-table-p");
3446 staticpro (&Qsyntax_table_p);
3448 staticpro (&Vsyntax_code_object);
3450 staticpro (&gl_state.object);
3451 staticpro (&gl_state.global_code);
3452 staticpro (&gl_state.current_syntax_table);
3453 staticpro (&gl_state.old_prop);
3455 /* Defined in regex.c */
3456 staticpro (&re_match_object);
3458 Qscan_error = intern_c_string ("scan-error");
3459 staticpro (&Qscan_error);
3460 Fput (Qscan_error, Qerror_conditions,
3461 pure_cons (Qscan_error, pure_cons (Qerror, Qnil)));
3462 Fput (Qscan_error, Qerror_message,
3463 make_pure_c_string ("Scan error"));
3465 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments,
3466 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3468 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties,
3469 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3470 Otherwise, that text property is simply ignored.
3471 See the info node `(elisp)Syntax Properties' for a description of the
3472 `syntax-table' property. */);
3474 words_include_escapes = 0;
3475 DEFVAR_BOOL ("words-include-escapes", words_include_escapes,
3476 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3478 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol,
3479 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3480 multibyte_syntax_as_symbol = 0;
3482 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3483 open_paren_in_column_0_is_defun_start,
3484 doc: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3485 open_paren_in_column_0_is_defun_start = 1;
3488 DEFVAR_LISP ("find-word-boundary-function-table",
3489 Vfind_word_boundary_function_table,
3490 doc: /*
3491 Char table of functions to search for the word boundary.
3492 Each function is called with two arguments; POS and LIMIT.
3493 POS and LIMIT are character positions in the current buffer.
3495 If POS is less than LIMIT, POS is at the first character of a word,
3496 and the return value of a function is a position after the last
3497 character of that word.
3499 If POS is not less than LIMIT, POS is at the last character of a word,
3500 and the return value of a function is a position at the first
3501 character of that word.
3503 In both cases, LIMIT bounds the search. */);
3504 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3506 defsubr (&Ssyntax_table_p);
3507 defsubr (&Ssyntax_table);
3508 defsubr (&Sstandard_syntax_table);
3509 defsubr (&Scopy_syntax_table);
3510 defsubr (&Sset_syntax_table);
3511 defsubr (&Schar_syntax);
3512 defsubr (&Smatching_paren);
3513 defsubr (&Sstring_to_syntax);
3514 defsubr (&Smodify_syntax_entry);
3515 defsubr (&Sinternal_describe_syntax_value);
3517 defsubr (&Sforward_word);
3519 defsubr (&Sskip_chars_forward);
3520 defsubr (&Sskip_chars_backward);
3521 defsubr (&Sskip_syntax_forward);
3522 defsubr (&Sskip_syntax_backward);
3524 defsubr (&Sforward_comment);
3525 defsubr (&Sscan_lists);
3526 defsubr (&Sscan_sexps);
3527 defsubr (&Sbackward_prefix_chars);
3528 defsubr (&Sparse_partial_sexp);