* tramp.texi (Top, Configuration): Insert section `Predefined
[emacs.git] / src / syntax.c
blob1b76f56dea6bf4bb890bf15f9e5af5f974d7629f
1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2013 Free
3 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>
23 #include <sys/types.h>
25 #include "lisp.h"
26 #include "commands.h"
27 #include "character.h"
28 #include "buffer.h"
29 #include "keymap.h"
30 #include "regex.h"
32 /* Make syntax table lookup grant data in gl_state. */
33 #define SYNTAX_ENTRY_VIA_PROPERTY
35 #include "syntax.h"
36 #include "intervals.h"
37 #include "category.h"
39 /* Then there are seven single-bit flags that have the following meanings:
40 1. This character is the first of a two-character comment-start sequence.
41 2. This character is the second of a two-character comment-start sequence.
42 3. This character is the first of a two-character comment-end sequence.
43 4. This character is the second of a two-character comment-end sequence.
44 5. This character is a prefix, for backward-prefix-chars.
45 6. The char is part of a delimiter for comments of style "b".
46 7. This character is part of a nestable comment sequence.
47 8. The char is part of a delimiter for comments of style "c".
48 Note that any two-character sequence whose first character has flag 1
49 and whose second character has flag 2 will be interpreted as a comment start.
51 bit 6 and 8 are used to discriminate between different comment styles.
52 Languages such as C++ allow two orthogonal syntax start/end pairs
53 and bit 6 is used to determine whether a comment-end or Scommentend
54 ends style a or b. Comment markers can start style a, b, c, or bc.
55 Style a is always the default.
56 For 2-char comment markers, the style b flag is only looked up on the second
57 char of the comment marker and on the first char of the comment ender.
58 For style c (like to for the nested flag), the flag can be placed on any
59 one of the chars.
62 /* These macros extract specific flags from an integer
63 that holds the syntax code and the flags. */
65 #define SYNTAX_FLAGS_COMSTART_FIRST(flags) (((flags) >> 16) & 1)
67 #define SYNTAX_FLAGS_COMSTART_SECOND(flags) (((flags) >> 17) & 1)
69 #define SYNTAX_FLAGS_COMEND_FIRST(flags) (((flags) >> 18) & 1)
71 #define SYNTAX_FLAGS_COMEND_SECOND(flags) (((flags) >> 19) & 1)
73 #define SYNTAX_FLAGS_PREFIX(flags) (((flags) >> 20) & 1)
75 #define SYNTAX_FLAGS_COMMENT_STYLEB(flags) (((flags) >> 21) & 1)
76 #define SYNTAX_FLAGS_COMMENT_STYLEC(flags) (((flags) >> 22) & 2)
77 /* FLAGS should be the flags of the main char of the comment marker, e.g.
78 the second for comstart and the first for comend. */
79 #define SYNTAX_FLAGS_COMMENT_STYLE(flags, other_flags) \
80 (SYNTAX_FLAGS_COMMENT_STYLEB (flags) \
81 | SYNTAX_FLAGS_COMMENT_STYLEC (flags) \
82 | SYNTAX_FLAGS_COMMENT_STYLEC (other_flags))
84 #define SYNTAX_FLAGS_COMMENT_NESTED(flags) (((flags) >> 22) & 1)
86 /* These macros extract a particular flag for a given character. */
88 #define SYNTAX_COMEND_FIRST(c) \
89 (SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c)))
90 #define SYNTAX_PREFIX(c) (SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c)))
92 /* We use these constants in place for comment-style and
93 string-ender-char to distinguish comments/strings started by
94 comment_fence and string_fence codes. */
96 #define ST_COMMENT_STYLE (256 + 1)
97 #define ST_STRING_STYLE (256 + 2)
99 static Lisp_Object Qsyntax_table_p;
100 static Lisp_Object Qsyntax_table, Qscan_error;
102 #ifndef __GNUC__
103 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
104 if not compiled with GCC. No need to mark it, since it is used
105 only very temporarily. */
106 Lisp_Object syntax_temp;
107 #endif
109 /* This is the internal form of the parse state used in parse-partial-sexp. */
111 struct lisp_parse_state
113 EMACS_INT depth; /* Depth at end of parsing. */
114 int instring; /* -1 if not within string, else desired terminator. */
115 EMACS_INT incomment; /* -1 if in unnestable comment else comment nesting */
116 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
117 int quoted; /* Nonzero if just after an escape char at end of parsing */
118 EMACS_INT mindepth; /* Minimum depth seen while scanning. */
119 /* Char number of most recent start-of-expression at current level */
120 ptrdiff_t thislevelstart;
121 /* Char number of start of containing expression */
122 ptrdiff_t prevlevelstart;
123 ptrdiff_t location; /* Char number at which parsing stopped. */
124 ptrdiff_t location_byte; /* Corresponding byte position. */
125 ptrdiff_t comstr_start; /* Position of last comment/string starter. */
126 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
127 of levels (starting from outermost). */
130 /* These variables are a cache for finding the start of a defun.
131 find_start_pos is the place for which the defun start was found.
132 find_start_value is the defun start position found for it.
133 find_start_value_byte is the corresponding byte position.
134 find_start_buffer is the buffer it was found in.
135 find_start_begv is the BEGV value when it was found.
136 find_start_modiff is the value of MODIFF when it was found. */
138 static ptrdiff_t find_start_pos;
139 static ptrdiff_t find_start_value;
140 static ptrdiff_t find_start_value_byte;
141 static struct buffer *find_start_buffer;
142 static ptrdiff_t find_start_begv;
143 static EMACS_INT find_start_modiff;
146 static Lisp_Object skip_chars (int, Lisp_Object, Lisp_Object, int);
147 static Lisp_Object skip_syntaxes (int, Lisp_Object, Lisp_Object);
148 static Lisp_Object scan_lists (EMACS_INT, EMACS_INT, EMACS_INT, int);
149 static void scan_sexps_forward (struct lisp_parse_state *,
150 ptrdiff_t, ptrdiff_t, ptrdiff_t, EMACS_INT,
151 int, Lisp_Object, int);
152 static int in_classes (int, Lisp_Object);
154 /* This setter is used only in this file, so it can be private. */
155 static void
156 bset_syntax_table (struct buffer *b, Lisp_Object val)
158 b->INTERNAL_FIELD (syntax_table) = val;
161 /* Whether the syntax of the character C has the prefix flag set. */
162 int syntax_prefix_flag_p (int c)
164 return SYNTAX_PREFIX (c);
167 struct gl_state_s gl_state; /* Global state of syntax parser. */
169 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
170 to scan to property-change. */
172 /* Update gl_state to an appropriate interval which contains CHARPOS. The
173 sign of COUNT give the relative position of CHARPOS wrt the previously
174 valid interval. If INIT, only [be]_property fields of gl_state are
175 valid at start, the rest is filled basing on OBJECT.
177 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
178 direction than the intervals - or in an interval. We update the
179 current syntax-table basing on the property of this interval, and
180 update the interval to start further than CHARPOS - or be
181 NULL. We also update lim_property to be the next value of
182 charpos to call this subroutine again - or be before/after the
183 start/end of OBJECT. */
185 void
186 update_syntax_table (ptrdiff_t charpos, EMACS_INT count, int init,
187 Lisp_Object object)
189 Lisp_Object tmp_table;
190 unsigned cnt = 0;
191 int invalidate = 1;
192 INTERVAL i;
194 if (init)
196 gl_state.old_prop = Qnil;
197 gl_state.start = gl_state.b_property;
198 gl_state.stop = gl_state.e_property;
199 i = interval_of (charpos, object);
200 gl_state.backward_i = gl_state.forward_i = i;
201 invalidate = 0;
202 if (!i)
203 return;
204 /* interval_of updates only ->position of the return value, so
205 update the parents manually to speed up update_interval. */
206 while (!NULL_PARENT (i))
208 if (AM_RIGHT_CHILD (i))
209 INTERVAL_PARENT (i)->position = i->position
210 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
211 - TOTAL_LENGTH (INTERVAL_PARENT (i))
212 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
213 else
214 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
215 + TOTAL_LENGTH (i);
216 i = INTERVAL_PARENT (i);
218 i = gl_state.forward_i;
219 gl_state.b_property = i->position - gl_state.offset;
220 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
221 goto update;
223 i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
225 /* We are guaranteed to be called with CHARPOS either in i,
226 or further off. */
227 if (!i)
228 error ("Error in syntax_table logic for to-the-end intervals");
229 else if (charpos < i->position) /* Move left. */
231 if (count > 0)
232 error ("Error in syntax_table logic for intervals <-");
233 /* Update the interval. */
234 i = update_interval (i, charpos);
235 if (INTERVAL_LAST_POS (i) != gl_state.b_property)
237 invalidate = 0;
238 gl_state.forward_i = i;
239 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
242 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
244 if (count < 0)
245 error ("Error in syntax_table logic for intervals ->");
246 /* Update the interval. */
247 i = update_interval (i, charpos);
248 if (i->position != gl_state.e_property)
250 invalidate = 0;
251 gl_state.backward_i = i;
252 gl_state.b_property = i->position - gl_state.offset;
256 update:
257 tmp_table = textget (i->plist, Qsyntax_table);
259 if (invalidate)
260 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
262 if (invalidate) /* Did not get to adjacent interval. */
263 { /* with the same table => */
264 /* invalidate the old range. */
265 if (count > 0)
267 gl_state.backward_i = i;
268 gl_state.b_property = i->position - gl_state.offset;
270 else
272 gl_state.forward_i = i;
273 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
277 if (!EQ (tmp_table, gl_state.old_prop))
279 gl_state.current_syntax_table = tmp_table;
280 gl_state.old_prop = tmp_table;
281 if (EQ (Fsyntax_table_p (tmp_table), Qt))
283 gl_state.use_global = 0;
285 else if (CONSP (tmp_table))
287 gl_state.use_global = 1;
288 gl_state.global_code = tmp_table;
290 else
292 gl_state.use_global = 0;
293 gl_state.current_syntax_table = BVAR (current_buffer, syntax_table);
297 while (i)
299 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
301 if (count > 0)
303 gl_state.e_property = i->position - gl_state.offset;
304 gl_state.forward_i = i;
306 else
308 gl_state.b_property
309 = i->position + LENGTH (i) - gl_state.offset;
310 gl_state.backward_i = i;
312 return;
314 else if (cnt == INTERVALS_AT_ONCE)
316 if (count > 0)
318 gl_state.e_property
319 = i->position + LENGTH (i) - gl_state.offset
320 /* e_property at EOB is not set to ZV but to ZV+1, so that
321 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
322 having to check eob between the two. */
323 + (next_interval (i) ? 0 : 1);
324 gl_state.forward_i = i;
326 else
328 gl_state.b_property = i->position - gl_state.offset;
329 gl_state.backward_i = i;
331 return;
333 cnt++;
334 i = count > 0 ? next_interval (i) : previous_interval (i);
336 eassert (i == NULL); /* This property goes to the end. */
337 if (count > 0)
338 gl_state.e_property = gl_state.stop;
339 else
340 gl_state.b_property = gl_state.start;
343 /* Returns TRUE if char at CHARPOS is quoted.
344 Global syntax-table data should be set up already to be good at CHARPOS
345 or after. On return global syntax data is good for lookup at CHARPOS. */
347 static int
348 char_quoted (ptrdiff_t charpos, ptrdiff_t bytepos)
350 register enum syntaxcode code;
351 register ptrdiff_t beg = BEGV;
352 register int quoted = 0;
353 ptrdiff_t orig = charpos;
355 while (charpos > beg)
357 int c;
358 DEC_BOTH (charpos, bytepos);
360 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
361 c = FETCH_CHAR_AS_MULTIBYTE (bytepos);
362 code = SYNTAX (c);
363 if (! (code == Scharquote || code == Sescape))
364 break;
366 quoted = !quoted;
369 UPDATE_SYNTAX_TABLE (orig);
370 return quoted;
373 /* Return the bytepos one character before BYTEPOS.
374 We assume that BYTEPOS is not at the start of the buffer. */
376 static ptrdiff_t
377 dec_bytepos (ptrdiff_t bytepos)
379 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
380 return bytepos - 1;
382 DEC_POS (bytepos);
383 return bytepos;
386 /* Return a defun-start position before POS and not too far before.
387 It should be the last one before POS, or nearly the last.
389 When open_paren_in_column_0_is_defun_start is nonzero,
390 only the beginning of the buffer is treated as a defun-start.
392 We record the information about where the scan started
393 and what its result was, so that another call in the same area
394 can return the same value very quickly.
396 There is no promise at which position the global syntax data is
397 valid on return from the subroutine, so the caller should explicitly
398 update the global data. */
400 static ptrdiff_t
401 find_defun_start (ptrdiff_t pos, ptrdiff_t pos_byte)
403 ptrdiff_t opoint = PT, opoint_byte = PT_BYTE;
405 if (!open_paren_in_column_0_is_defun_start)
407 find_start_value = BEGV;
408 find_start_value_byte = BEGV_BYTE;
409 find_start_buffer = current_buffer;
410 find_start_modiff = MODIFF;
411 find_start_begv = BEGV;
412 find_start_pos = pos;
413 return BEGV;
416 /* Use previous finding, if it's valid and applies to this inquiry. */
417 if (current_buffer == find_start_buffer
418 /* Reuse the defun-start even if POS is a little farther on.
419 POS might be in the next defun, but that's ok.
420 Our value may not be the best possible, but will still be usable. */
421 && pos <= find_start_pos + 1000
422 && pos >= find_start_value
423 && BEGV == find_start_begv
424 && MODIFF == find_start_modiff)
425 return find_start_value;
427 /* Back up to start of line. */
428 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
430 /* We optimize syntax-table lookup for rare updates. Thus we accept
431 only those `^\s(' which are good in global _and_ text-property
432 syntax-tables. */
433 SETUP_BUFFER_SYNTAX_TABLE ();
434 while (PT > BEGV)
436 int c;
438 /* Open-paren at start of line means we may have found our
439 defun-start. */
440 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
441 if (SYNTAX (c) == Sopen)
443 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
444 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
445 if (SYNTAX (c) == Sopen)
446 break;
447 /* Now fallback to the default value. */
448 SETUP_BUFFER_SYNTAX_TABLE ();
450 /* Move to beg of previous line. */
451 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
454 /* Record what we found, for the next try. */
455 find_start_value = PT;
456 find_start_value_byte = PT_BYTE;
457 find_start_buffer = current_buffer;
458 find_start_modiff = MODIFF;
459 find_start_begv = BEGV;
460 find_start_pos = pos;
462 TEMP_SET_PT_BOTH (opoint, opoint_byte);
464 return find_start_value;
467 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
469 static int
470 prev_char_comend_first (ptrdiff_t pos, ptrdiff_t pos_byte)
472 int c, val;
474 DEC_BOTH (pos, pos_byte);
475 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
476 c = FETCH_CHAR (pos_byte);
477 val = SYNTAX_COMEND_FIRST (c);
478 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
479 return val;
482 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
484 /* static int
485 * prev_char_comstart_first (pos, pos_byte)
486 * int pos, pos_byte;
488 * int c, val;
490 * DEC_BOTH (pos, pos_byte);
491 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
492 * c = FETCH_CHAR (pos_byte);
493 * val = SYNTAX_COMSTART_FIRST (c);
494 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
495 * return val;
496 * } */
498 /* Checks whether charpos FROM is at the end of a comment.
499 FROM_BYTE is the bytepos corresponding to FROM.
500 Do not move back before STOP.
502 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
503 return -1 otherwise.
505 If successful, store the charpos of the comment's beginning
506 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
508 Global syntax data remains valid for backward search starting at
509 the returned value (or at FROM, if the search was not successful). */
511 static int
512 back_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop, int comnested, int comstyle, ptrdiff_t *charpos_ptr, ptrdiff_t *bytepos_ptr)
514 /* Look back, counting the parity of string-quotes,
515 and recording the comment-starters seen.
516 When we reach a safe place, assume that's not in a string;
517 then step the main scan to the earliest comment-starter seen
518 an even number of string quotes away from the safe place.
520 OFROM[I] is position of the earliest comment-starter seen
521 which is I+2X quotes from the comment-end.
522 PARITY is current parity of quotes from the comment end. */
523 int string_style = -1; /* Presumed outside of any string. */
524 int string_lossage = 0;
525 /* Not a real lossage: indicates that we have passed a matching comment
526 starter plus a non-matching comment-ender, meaning that any matching
527 comment-starter we might see later could be a false positive (hidden
528 inside another comment).
529 Test case: { a (* b } c (* d *) */
530 int comment_lossage = 0;
531 ptrdiff_t comment_end = from;
532 ptrdiff_t comment_end_byte = from_byte;
533 ptrdiff_t comstart_pos = 0;
534 ptrdiff_t comstart_byte IF_LINT (= 0);
535 /* Place where the containing defun starts,
536 or 0 if we didn't come across it yet. */
537 ptrdiff_t defun_start = 0;
538 ptrdiff_t defun_start_byte = 0;
539 register enum syntaxcode code;
540 int nesting = 1; /* current comment nesting */
541 int c;
542 int syntax = 0;
544 /* FIXME: A }} comment-ender style leads to incorrect behavior
545 in the case of {{ c }}} because we ignore the last two chars which are
546 assumed to be comment-enders although they aren't. */
548 /* At beginning of range to scan, we're outside of strings;
549 that determines quote parity to the comment-end. */
550 while (from != stop)
552 ptrdiff_t temp_byte;
553 int prev_syntax, com2start, com2end;
554 int comstart;
556 /* Move back and examine a character. */
557 DEC_BOTH (from, from_byte);
558 UPDATE_SYNTAX_TABLE_BACKWARD (from);
560 prev_syntax = syntax;
561 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
562 syntax = SYNTAX_WITH_FLAGS (c);
563 code = SYNTAX (c);
565 /* Check for 2-char comment markers. */
566 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
567 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
568 && (comstyle
569 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax, syntax))
570 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
571 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
572 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
573 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
574 comstart = (com2start || code == Scomment);
576 /* Nasty cases with overlapping 2-char comment markers:
577 - snmp-mode: -- c -- foo -- c --
578 --- c --
579 ------ c --
580 - c-mode: *||*
581 |* *|* *|
582 |*| |* |*|
583 /// */
585 /* If a 2-char comment sequence partly overlaps with another,
586 we don't try to be clever. E.g. |*| in C, or }% in modes that
587 have %..\n and %{..}%. */
588 if (from > stop && (com2end || comstart))
590 ptrdiff_t next = from, next_byte = from_byte;
591 int next_c, next_syntax;
592 DEC_BOTH (next, next_byte);
593 UPDATE_SYNTAX_TABLE_BACKWARD (next);
594 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
595 next_syntax = SYNTAX_WITH_FLAGS (next_c);
596 if (((comstart || comnested)
597 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
598 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
599 || ((com2end || comnested)
600 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
601 && (comstyle
602 == SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_syntax))
603 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
604 goto lossage;
605 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
608 if (com2start && comstart_pos == 0)
609 /* We're looking at a comment starter. But it might be a comment
610 ender as well (see snmp-mode). The first time we see one, we
611 need to consider it as a comment starter,
612 and the subsequent times as a comment ender. */
613 com2end = 0;
615 /* Turn a 2-char comment sequences into the appropriate syntax. */
616 if (com2end)
617 code = Sendcomment;
618 else if (com2start)
619 code = Scomment;
620 /* Ignore comment starters of a different style. */
621 else if (code == Scomment
622 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0)
623 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
624 continue;
626 /* Ignore escaped characters, except comment-enders. */
627 if (code != Sendcomment && char_quoted (from, from_byte))
628 continue;
630 switch (code)
632 case Sstring_fence:
633 case Scomment_fence:
634 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
635 case Sstring:
636 /* Track parity of quotes. */
637 if (string_style == -1)
638 /* Entering a string. */
639 string_style = c;
640 else if (string_style == c)
641 /* Leaving the string. */
642 string_style = -1;
643 else
644 /* If we have two kinds of string delimiters.
645 There's no way to grok this scanning backwards. */
646 string_lossage = 1;
647 break;
649 case Scomment:
650 /* We've already checked that it is the relevant comstyle. */
651 if (string_style != -1 || comment_lossage || string_lossage)
652 /* There are odd string quotes involved, so let's be careful.
653 Test case in Pascal: " { " a { " } */
654 goto lossage;
656 if (!comnested)
658 /* Record best comment-starter so far. */
659 comstart_pos = from;
660 comstart_byte = from_byte;
662 else if (--nesting <= 0)
663 /* nested comments have to be balanced, so we don't need to
664 keep looking for earlier ones. We use here the same (slightly
665 incorrect) reasoning as below: since it is followed by uniform
666 paired string quotes, this comment-start has to be outside of
667 strings, else the comment-end itself would be inside a string. */
668 goto done;
669 break;
671 case Sendcomment:
672 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == comstyle
673 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
674 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
675 /* This is the same style of comment ender as ours. */
677 if (comnested)
678 nesting++;
679 else
680 /* Anything before that can't count because it would match
681 this comment-ender rather than ours. */
682 from = stop; /* Break out of the loop. */
684 else if (comstart_pos != 0 || c != '\n')
685 /* We're mixing comment styles here, so we'd better be careful.
686 The (comstart_pos != 0 || c != '\n') check is not quite correct
687 (we should just always set comment_lossage), but removing it
688 would imply that any multiline comment in C would go through
689 lossage, which seems overkill.
690 The failure should only happen in the rare cases such as
691 { (* } *) */
692 comment_lossage = 1;
693 break;
695 case Sopen:
696 /* Assume a defun-start point is outside of strings. */
697 if (open_paren_in_column_0_is_defun_start
698 && (from == stop
699 || (temp_byte = dec_bytepos (from_byte),
700 FETCH_CHAR (temp_byte) == '\n')))
702 defun_start = from;
703 defun_start_byte = from_byte;
704 from = stop; /* Break out of the loop. */
706 break;
708 default:
709 break;
713 if (comstart_pos == 0)
715 from = comment_end;
716 from_byte = comment_end_byte;
717 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
719 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
720 or `done'), then we've found the beginning of the non-nested comment. */
721 else if (1) /* !comnested */
723 from = comstart_pos;
724 from_byte = comstart_byte;
725 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
727 else
729 struct lisp_parse_state state;
730 lossage:
731 /* We had two kinds of string delimiters mixed up
732 together. Decode this going forwards.
733 Scan fwd from a known safe place (beginning-of-defun)
734 to the one in question; this records where we
735 last passed a comment starter. */
736 /* If we did not already find the defun start, find it now. */
737 if (defun_start == 0)
739 defun_start = find_defun_start (comment_end, comment_end_byte);
740 defun_start_byte = find_start_value_byte;
744 scan_sexps_forward (&state,
745 defun_start, defun_start_byte,
746 comment_end, TYPE_MINIMUM (EMACS_INT),
747 0, Qnil, 0);
748 defun_start = comment_end;
749 if (state.incomment == (comnested ? 1 : -1)
750 && state.comstyle == comstyle)
751 from = state.comstr_start;
752 else
754 from = comment_end;
755 if (state.incomment)
756 /* If comment_end is inside some other comment, maybe ours
757 is nested, so we need to try again from within the
758 surrounding comment. Example: { a (* " *) */
760 /* FIXME: We should advance by one or two chars. */
761 defun_start = state.comstr_start + 2;
762 defun_start_byte = CHAR_TO_BYTE (defun_start);
765 } while (defun_start < comment_end);
767 from_byte = CHAR_TO_BYTE (from);
768 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
771 done:
772 *charpos_ptr = from;
773 *bytepos_ptr = from_byte;
775 return (from == comment_end) ? -1 : from;
778 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
779 doc: /* Return t if OBJECT is a syntax table.
780 Currently, any char-table counts as a syntax table. */)
781 (Lisp_Object object)
783 if (CHAR_TABLE_P (object)
784 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
785 return Qt;
786 return Qnil;
789 static void
790 check_syntax_table (Lisp_Object obj)
792 CHECK_TYPE (CHAR_TABLE_P (obj) && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table),
793 Qsyntax_table_p, obj);
796 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
797 doc: /* Return the current syntax table.
798 This is the one specified by the current buffer. */)
799 (void)
801 return BVAR (current_buffer, syntax_table);
804 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
805 Sstandard_syntax_table, 0, 0, 0,
806 doc: /* Return the standard syntax table.
807 This is the one used for new buffers. */)
808 (void)
810 return Vstandard_syntax_table;
813 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
814 doc: /* Construct a new syntax table and return it.
815 It is a copy of the TABLE, which defaults to the standard syntax table. */)
816 (Lisp_Object table)
818 Lisp_Object copy;
820 if (!NILP (table))
821 check_syntax_table (table);
822 else
823 table = Vstandard_syntax_table;
825 copy = Fcopy_sequence (table);
827 /* Only the standard syntax table should have a default element.
828 Other syntax tables should inherit from parents instead. */
829 set_char_table_defalt (copy, Qnil);
831 /* Copied syntax tables should all have parents.
832 If we copied one with no parent, such as the standard syntax table,
833 use the standard syntax table as the copy's parent. */
834 if (NILP (XCHAR_TABLE (copy)->parent))
835 Fset_char_table_parent (copy, Vstandard_syntax_table);
836 return copy;
839 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
840 doc: /* Select a new syntax table for the current buffer.
841 One argument, a syntax table. */)
842 (Lisp_Object table)
844 int idx;
845 check_syntax_table (table);
846 bset_syntax_table (current_buffer, table);
847 /* Indicate that this buffer now has a specified syntax table. */
848 idx = PER_BUFFER_VAR_IDX (syntax_table);
849 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
850 return table;
853 /* Convert a letter which signifies a syntax code
854 into the code it signifies.
855 This is used by modify-syntax-entry, and other things. */
857 unsigned char syntax_spec_code[0400] =
858 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
859 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
860 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
861 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
862 (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377,
863 (char) Smath, 0377, 0377, (char) Squote,
864 (char) Sopen, (char) Sclose, 0377, 0377,
865 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
866 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
867 0377, 0377, 0377, 0377,
868 (char) Scomment, 0377, (char) Sendcomment, 0377,
869 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
870 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
871 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
872 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
873 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
874 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
875 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
876 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377
879 /* Indexed by syntax code, give the letter that describes it. */
881 char syntax_code_spec[16] =
883 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
884 '!', '|'
887 /* Indexed by syntax code, give the object (cons of syntax code and
888 nil) to be stored in syntax table. Since these objects can be
889 shared among syntax tables, we generate them in advance. By
890 sharing objects, the function `describe-syntax' can give a more
891 compact listing. */
892 static Lisp_Object Vsyntax_code_object;
895 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
896 doc: /* Return the syntax code of CHARACTER, described by a character.
897 For example, if CHARACTER is a word constituent, the
898 character `w' (119) is returned.
899 The characters that correspond to various syntax codes
900 are listed in the documentation of `modify-syntax-entry'. */)
901 (Lisp_Object character)
903 int char_int;
904 CHECK_CHARACTER (character);
905 char_int = XINT (character);
906 SETUP_BUFFER_SYNTAX_TABLE ();
907 return make_number (syntax_code_spec[(int) SYNTAX (char_int)]);
910 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
911 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
912 (Lisp_Object character)
914 int char_int, code;
915 CHECK_NUMBER (character);
916 char_int = XINT (character);
917 SETUP_BUFFER_SYNTAX_TABLE ();
918 code = SYNTAX (char_int);
919 if (code == Sopen || code == Sclose)
920 return SYNTAX_MATCH (char_int);
921 return Qnil;
924 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
925 doc: /* Convert a syntax descriptor STRING into a raw syntax descriptor.
926 STRING should be a string of the form allowed as argument of
927 `modify-syntax-entry'. The return value is a raw syntax descriptor: a
928 cons cell \(CODE . MATCHING-CHAR) which can be used, for example, as
929 the value of a `syntax-table' text property. */)
930 (Lisp_Object string)
932 register const unsigned char *p;
933 register enum syntaxcode code;
934 int val;
935 Lisp_Object match;
937 CHECK_STRING (string);
939 p = SDATA (string);
940 code = (enum syntaxcode) syntax_spec_code[*p++];
941 if (((int) code & 0377) == 0377)
942 error ("Invalid syntax description letter: %c", p[-1]);
944 if (code == Sinherit)
945 return Qnil;
947 if (*p)
949 int len;
950 int character = STRING_CHAR_AND_LENGTH (p, len);
951 XSETINT (match, character);
952 if (XFASTINT (match) == ' ')
953 match = Qnil;
954 p += len;
956 else
957 match = Qnil;
959 val = (int) code;
960 while (*p)
961 switch (*p++)
963 case '1':
964 val |= 1 << 16;
965 break;
967 case '2':
968 val |= 1 << 17;
969 break;
971 case '3':
972 val |= 1 << 18;
973 break;
975 case '4':
976 val |= 1 << 19;
977 break;
979 case 'p':
980 val |= 1 << 20;
981 break;
983 case 'b':
984 val |= 1 << 21;
985 break;
987 case 'n':
988 val |= 1 << 22;
989 break;
991 case 'c':
992 val |= 1 << 23;
993 break;
996 if (val < ASIZE (Vsyntax_code_object) && NILP (match))
997 return AREF (Vsyntax_code_object, val);
998 else
999 /* Since we can't use a shared object, let's make a new one. */
1000 return Fcons (make_number (val), match);
1003 /* I really don't know why this is interactive
1004 help-form should at least be made useful whilst reading the second arg. */
1005 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
1006 "cSet syntax for character: \nsSet syntax for %s to: ",
1007 doc: /* Set syntax for character CHAR according to string NEWENTRY.
1008 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1009 the current buffer's syntax table.
1010 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1011 in the range MIN to MAX are changed.
1012 The first character of NEWENTRY should be one of the following:
1013 Space or - whitespace syntax. w word constituent.
1014 _ symbol constituent. . punctuation.
1015 ( open-parenthesis. ) close-parenthesis.
1016 " string quote. \\ escape.
1017 $ paired delimiter. ' expression quote or prefix operator.
1018 < comment starter. > comment ender.
1019 / character-quote. @ inherit from parent table.
1020 | generic string fence. ! generic comment fence.
1022 Only single-character comment start and end sequences are represented thus.
1023 Two-character sequences are represented as described below.
1024 The second character of NEWENTRY is the matching parenthesis,
1025 used only if the first character is `(' or `)'.
1026 Any additional characters are flags.
1027 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1028 1 means CHAR is the start of a two-char comment start sequence.
1029 2 means CHAR is the second character of such a sequence.
1030 3 means CHAR is the start of a two-char comment end sequence.
1031 4 means CHAR is the second character of such a sequence.
1033 There can be several orthogonal comment sequences. This is to support
1034 language modes such as C++. By default, all comment sequences are of style
1035 a, but you can set the comment sequence style to b (on the second character
1036 of a comment-start, and the first character of a comment-end sequence) and/or
1037 c (on any of its chars) using this flag:
1038 b means CHAR is part of comment sequence b.
1039 c means CHAR is part of comment sequence c.
1040 n means CHAR is part of a nestable comment sequence.
1042 p means CHAR is a prefix character for `backward-prefix-chars';
1043 such characters are treated as whitespace when they occur
1044 between expressions.
1045 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1046 (Lisp_Object c, Lisp_Object newentry, Lisp_Object syntax_table)
1048 if (CONSP (c))
1050 CHECK_CHARACTER_CAR (c);
1051 CHECK_CHARACTER_CDR (c);
1053 else
1054 CHECK_CHARACTER (c);
1056 if (NILP (syntax_table))
1057 syntax_table = BVAR (current_buffer, syntax_table);
1058 else
1059 check_syntax_table (syntax_table);
1061 newentry = Fstring_to_syntax (newentry);
1062 if (CONSP (c))
1063 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1064 else
1065 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
1067 /* We clear the regexp cache, since character classes can now have
1068 different values from those in the compiled regexps.*/
1069 clear_regexp_cache ();
1071 return Qnil;
1074 /* Dump syntax table to buffer in human-readable format */
1076 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1077 Sinternal_describe_syntax_value, 1, 1, 0,
1078 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1079 (Lisp_Object syntax)
1081 register enum syntaxcode code;
1082 int syntax_code;
1083 char desc, start1, start2, end1, end2, prefix,
1084 comstyleb, comstylec, comnested;
1085 char str[2];
1086 Lisp_Object first, match_lisp, value = syntax;
1088 if (NILP (value))
1090 insert_string ("default");
1091 return syntax;
1094 if (CHAR_TABLE_P (value))
1096 insert_string ("deeper char-table ...");
1097 return syntax;
1100 if (!CONSP (value))
1102 insert_string ("invalid");
1103 return syntax;
1106 first = XCAR (value);
1107 match_lisp = XCDR (value);
1109 if (!INTEGERP (first) || !(NILP (match_lisp) || CHARACTERP (match_lisp)))
1111 insert_string ("invalid");
1112 return syntax;
1115 syntax_code = XINT (first) & INT_MAX;
1116 code = (enum syntaxcode) (syntax_code & 0377);
1117 start1 = SYNTAX_FLAGS_COMSTART_FIRST (syntax_code);
1118 start2 = SYNTAX_FLAGS_COMSTART_SECOND (syntax_code);;
1119 end1 = SYNTAX_FLAGS_COMEND_FIRST (syntax_code);
1120 end2 = SYNTAX_FLAGS_COMEND_SECOND (syntax_code);
1121 prefix = SYNTAX_FLAGS_PREFIX (syntax_code);
1122 comstyleb = SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code);
1123 comstylec = SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code);
1124 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax_code);
1126 if ((int) code < 0 || (int) code >= (int) Smax)
1128 insert_string ("invalid");
1129 return syntax;
1131 desc = syntax_code_spec[(int) code];
1133 str[0] = desc, str[1] = 0;
1134 insert (str, 1);
1136 if (NILP (match_lisp))
1137 insert (" ", 1);
1138 else
1139 insert_char (XINT (match_lisp));
1141 if (start1)
1142 insert ("1", 1);
1143 if (start2)
1144 insert ("2", 1);
1146 if (end1)
1147 insert ("3", 1);
1148 if (end2)
1149 insert ("4", 1);
1151 if (prefix)
1152 insert ("p", 1);
1153 if (comstyleb)
1154 insert ("b", 1);
1155 if (comstylec)
1156 insert ("c", 1);
1157 if (comnested)
1158 insert ("n", 1);
1160 insert_string ("\twhich means: ");
1162 switch (code)
1164 case Swhitespace:
1165 insert_string ("whitespace"); break;
1166 case Spunct:
1167 insert_string ("punctuation"); break;
1168 case Sword:
1169 insert_string ("word"); break;
1170 case Ssymbol:
1171 insert_string ("symbol"); break;
1172 case Sopen:
1173 insert_string ("open"); break;
1174 case Sclose:
1175 insert_string ("close"); break;
1176 case Squote:
1177 insert_string ("prefix"); break;
1178 case Sstring:
1179 insert_string ("string"); break;
1180 case Smath:
1181 insert_string ("math"); break;
1182 case Sescape:
1183 insert_string ("escape"); break;
1184 case Scharquote:
1185 insert_string ("charquote"); break;
1186 case Scomment:
1187 insert_string ("comment"); break;
1188 case Sendcomment:
1189 insert_string ("endcomment"); break;
1190 case Sinherit:
1191 insert_string ("inherit"); break;
1192 case Scomment_fence:
1193 insert_string ("comment fence"); break;
1194 case Sstring_fence:
1195 insert_string ("string fence"); break;
1196 default:
1197 insert_string ("invalid");
1198 return syntax;
1201 if (!NILP (match_lisp))
1203 insert_string (", matches ");
1204 insert_char (XINT (match_lisp));
1207 if (start1)
1208 insert_string (",\n\t is the first character of a comment-start sequence");
1209 if (start2)
1210 insert_string (",\n\t is the second character of a comment-start sequence");
1212 if (end1)
1213 insert_string (",\n\t is the first character of a comment-end sequence");
1214 if (end2)
1215 insert_string (",\n\t is the second character of a comment-end sequence");
1216 if (comstyleb)
1217 insert_string (" (comment style b)");
1218 if (comstylec)
1219 insert_string (" (comment style c)");
1220 if (comnested)
1221 insert_string (" (nestable)");
1223 if (prefix)
1224 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1226 return syntax;
1229 /* Return the position across COUNT words from FROM.
1230 If that many words cannot be found before the end of the buffer, return 0.
1231 COUNT negative means scan backward and stop at word beginning. */
1233 ptrdiff_t
1234 scan_words (register ptrdiff_t from, register EMACS_INT count)
1236 register ptrdiff_t beg = BEGV;
1237 register ptrdiff_t end = ZV;
1238 register ptrdiff_t from_byte = CHAR_TO_BYTE (from);
1239 register enum syntaxcode code;
1240 int ch0, ch1;
1241 Lisp_Object func, pos;
1243 immediate_quit = 1;
1244 QUIT;
1246 SETUP_SYNTAX_TABLE (from, count);
1248 while (count > 0)
1250 while (1)
1252 if (from == end)
1254 immediate_quit = 0;
1255 return 0;
1257 UPDATE_SYNTAX_TABLE_FORWARD (from);
1258 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1259 code = SYNTAX (ch0);
1260 INC_BOTH (from, from_byte);
1261 if (words_include_escapes
1262 && (code == Sescape || code == Scharquote))
1263 break;
1264 if (code == Sword)
1265 break;
1267 /* Now CH0 is a character which begins a word and FROM is the
1268 position of the next character. */
1269 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
1270 if (! NILP (Ffboundp (func)))
1272 pos = call2 (func, make_number (from - 1), make_number (end));
1273 if (INTEGERP (pos) && from < XINT (pos) && XINT (pos) <= ZV)
1275 from = XINT (pos);
1276 from_byte = CHAR_TO_BYTE (from);
1279 else
1281 while (1)
1283 if (from == end) break;
1284 UPDATE_SYNTAX_TABLE_FORWARD (from);
1285 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1286 code = SYNTAX (ch1);
1287 if ((code != Sword
1288 && (! words_include_escapes
1289 || (code != Sescape && code != Scharquote)))
1290 || word_boundary_p (ch0, ch1))
1291 break;
1292 INC_BOTH (from, from_byte);
1293 ch0 = ch1;
1296 count--;
1298 while (count < 0)
1300 while (1)
1302 if (from == beg)
1304 immediate_quit = 0;
1305 return 0;
1307 DEC_BOTH (from, from_byte);
1308 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1309 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1310 code = SYNTAX (ch1);
1311 if (words_include_escapes
1312 && (code == Sescape || code == Scharquote))
1313 break;
1314 if (code == Sword)
1315 break;
1317 /* Now CH1 is a character which ends a word and FROM is the
1318 position of it. */
1319 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
1320 if (! NILP (Ffboundp (func)))
1322 pos = call2 (func, make_number (from), make_number (beg));
1323 if (INTEGERP (pos) && BEGV <= XINT (pos) && XINT (pos) < from)
1325 from = XINT (pos);
1326 from_byte = CHAR_TO_BYTE (from);
1329 else
1331 while (1)
1333 if (from == beg)
1334 break;
1335 DEC_BOTH (from, from_byte);
1336 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1337 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1338 code = SYNTAX (ch0);
1339 if ((code != Sword
1340 && (! words_include_escapes
1341 || (code != Sescape && code != Scharquote)))
1342 || word_boundary_p (ch0, ch1))
1344 INC_BOTH (from, from_byte);
1345 break;
1347 ch1 = ch0;
1350 count++;
1353 immediate_quit = 0;
1355 return from;
1358 DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p",
1359 doc: /* Move point forward ARG words (backward if ARG is negative).
1360 Normally returns t.
1361 If an edge of the buffer or a field boundary is reached, point is left there
1362 and the function returns nil. Field boundaries are not noticed if
1363 `inhibit-field-text-motion' is non-nil. */)
1364 (Lisp_Object arg)
1366 Lisp_Object tmp;
1367 ptrdiff_t orig_val, val;
1369 if (NILP (arg))
1370 XSETFASTINT (arg, 1);
1371 else
1372 CHECK_NUMBER (arg);
1374 val = orig_val = scan_words (PT, XINT (arg));
1375 if (! orig_val)
1376 val = XINT (arg) > 0 ? ZV : BEGV;
1378 /* Avoid jumping out of an input field. */
1379 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1380 Qt, Qnil, Qnil);
1381 val = XFASTINT (tmp);
1383 SET_PT (val);
1384 return val == orig_val ? Qt : Qnil;
1387 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1388 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1389 STRING is like the inside of a `[...]' in a regular expression
1390 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1391 (but not at the end of a range; quoting is never needed there).
1392 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1393 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1394 Char classes, e.g. `[:alpha:]', are supported.
1396 Returns the distance traveled, either zero or positive. */)
1397 (Lisp_Object string, Lisp_Object lim)
1399 return skip_chars (1, string, lim, 1);
1402 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1403 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1404 See `skip-chars-forward' for details.
1405 Returns the distance traveled, either zero or negative. */)
1406 (Lisp_Object string, Lisp_Object lim)
1408 return skip_chars (0, string, lim, 1);
1411 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1412 doc: /* Move point forward across chars in specified syntax classes.
1413 SYNTAX is a string of syntax code characters.
1414 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1415 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1416 This function returns the distance traveled, either zero or positive. */)
1417 (Lisp_Object syntax, Lisp_Object lim)
1419 return skip_syntaxes (1, syntax, lim);
1422 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1423 doc: /* Move point backward across chars in specified syntax classes.
1424 SYNTAX is a string of syntax code characters.
1425 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1426 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1427 This function returns the distance traveled, either zero or negative. */)
1428 (Lisp_Object syntax, Lisp_Object lim)
1430 return skip_syntaxes (0, syntax, lim);
1433 static Lisp_Object
1434 skip_chars (int forwardp, Lisp_Object string, Lisp_Object lim, int handle_iso_classes)
1436 register unsigned int c;
1437 unsigned char fastmap[0400];
1438 /* Store the ranges of non-ASCII characters. */
1439 int *char_ranges IF_LINT (= NULL);
1440 int n_char_ranges = 0;
1441 int negate = 0;
1442 register ptrdiff_t i, i_byte;
1443 /* Set to 1 if the current buffer is multibyte and the region
1444 contains non-ASCII chars. */
1445 int multibyte;
1446 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1447 chars. */
1448 int string_multibyte;
1449 ptrdiff_t size_byte;
1450 const unsigned char *str;
1451 int len;
1452 Lisp_Object iso_classes;
1454 CHECK_STRING (string);
1455 iso_classes = Qnil;
1457 if (NILP (lim))
1458 XSETINT (lim, forwardp ? ZV : BEGV);
1459 else
1460 CHECK_NUMBER_COERCE_MARKER (lim);
1462 /* In any case, don't allow scan outside bounds of buffer. */
1463 if (XINT (lim) > ZV)
1464 XSETFASTINT (lim, ZV);
1465 if (XINT (lim) < BEGV)
1466 XSETFASTINT (lim, BEGV);
1468 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1469 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1470 string_multibyte = SBYTES (string) > SCHARS (string);
1472 memset (fastmap, 0, sizeof fastmap);
1474 str = SDATA (string);
1475 size_byte = SBYTES (string);
1477 i_byte = 0;
1478 if (i_byte < size_byte
1479 && SREF (string, 0) == '^')
1481 negate = 1; i_byte++;
1484 /* Find the characters specified and set their elements of fastmap.
1485 Handle backslashes and ranges specially.
1487 If STRING contains non-ASCII characters, setup char_ranges for
1488 them and use fastmap only for their leading codes. */
1490 if (! string_multibyte)
1492 int string_has_eight_bit = 0;
1494 /* At first setup fastmap. */
1495 while (i_byte < size_byte)
1497 c = str[i_byte++];
1499 if (handle_iso_classes && c == '['
1500 && i_byte < size_byte
1501 && str[i_byte] == ':')
1503 const unsigned char *class_beg = str + i_byte + 1;
1504 const unsigned char *class_end = class_beg;
1505 const unsigned char *class_limit = str + size_byte - 2;
1506 /* Leave room for the null. */
1507 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1508 re_wctype_t cc;
1510 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1511 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1513 while (class_end < class_limit
1514 && *class_end >= 'a' && *class_end <= 'z')
1515 class_end++;
1517 if (class_end == class_beg
1518 || *class_end != ':' || class_end[1] != ']')
1519 goto not_a_class_name;
1521 memcpy (class_name, class_beg, class_end - class_beg);
1522 class_name[class_end - class_beg] = 0;
1524 cc = re_wctype (class_name);
1525 if (cc == 0)
1526 error ("Invalid ISO C character class");
1528 iso_classes = Fcons (make_number (cc), iso_classes);
1530 i_byte = class_end + 2 - str;
1531 continue;
1534 not_a_class_name:
1535 if (c == '\\')
1537 if (i_byte == size_byte)
1538 break;
1540 c = str[i_byte++];
1542 /* Treat `-' as range character only if another character
1543 follows. */
1544 if (i_byte + 1 < size_byte
1545 && str[i_byte] == '-')
1547 unsigned int c2;
1549 /* Skip over the dash. */
1550 i_byte++;
1552 /* Get the end of the range. */
1553 c2 = str[i_byte++];
1554 if (c2 == '\\'
1555 && i_byte < size_byte)
1556 c2 = str[i_byte++];
1558 if (c <= c2)
1560 unsigned lim2 = c2 + 1;
1561 while (c < lim2)
1562 fastmap[c++] = 1;
1563 if (! ASCII_CHAR_P (c2))
1564 string_has_eight_bit = 1;
1567 else
1569 fastmap[c] = 1;
1570 if (! ASCII_CHAR_P (c))
1571 string_has_eight_bit = 1;
1575 /* If the current range is multibyte and STRING contains
1576 eight-bit chars, arrange fastmap and setup char_ranges for
1577 the corresponding multibyte chars. */
1578 if (multibyte && string_has_eight_bit)
1580 char *p1;
1581 char himap[0200 + 1];
1582 memcpy (himap, fastmap + 0200, 0200);
1583 himap[0200] = 0;
1584 memset (fastmap + 0200, 0, 0200);
1585 char_ranges = alloca (sizeof *char_ranges * 128 * 2);
1586 i = 0;
1588 while ((p1 = memchr (himap + i, 1, 0200 - i)))
1590 /* Deduce the next range C..C2 from the next clump of 1s
1591 in HIMAP starting with &HIMAP[I]. HIMAP is the high
1592 order half of the old FASTMAP. */
1593 int c2, leading_code;
1594 i = p1 - himap;
1595 c = BYTE8_TO_CHAR (i + 0200);
1596 i += strlen (p1);
1597 c2 = BYTE8_TO_CHAR (i + 0200 - 1);
1599 char_ranges[n_char_ranges++] = c;
1600 char_ranges[n_char_ranges++] = c2;
1601 leading_code = CHAR_LEADING_CODE (c);
1602 memset (fastmap + leading_code, 1,
1603 CHAR_LEADING_CODE (c2) - leading_code + 1);
1607 else /* STRING is multibyte */
1609 char_ranges = alloca (sizeof *char_ranges * SCHARS (string) * 2);
1611 while (i_byte < size_byte)
1613 unsigned char leading_code;
1615 leading_code = str[i_byte];
1616 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1617 i_byte += len;
1619 if (handle_iso_classes && c == '['
1620 && i_byte < size_byte
1621 && STRING_CHAR (str + i_byte) == ':')
1623 const unsigned char *class_beg = str + i_byte + 1;
1624 const unsigned char *class_end = class_beg;
1625 const unsigned char *class_limit = str + size_byte - 2;
1626 /* Leave room for the null. */
1627 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1628 re_wctype_t cc;
1630 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1631 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1633 while (class_end < class_limit
1634 && *class_end >= 'a' && *class_end <= 'z')
1635 class_end++;
1637 if (class_end == class_beg
1638 || *class_end != ':' || class_end[1] != ']')
1639 goto not_a_class_name_multibyte;
1641 memcpy (class_name, class_beg, class_end - class_beg);
1642 class_name[class_end - class_beg] = 0;
1644 cc = re_wctype (class_name);
1645 if (cc == 0)
1646 error ("Invalid ISO C character class");
1648 iso_classes = Fcons (make_number (cc), iso_classes);
1650 i_byte = class_end + 2 - str;
1651 continue;
1654 not_a_class_name_multibyte:
1655 if (c == '\\')
1657 if (i_byte == size_byte)
1658 break;
1660 leading_code = str[i_byte];
1661 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1662 i_byte += len;
1664 /* Treat `-' as range character only if another character
1665 follows. */
1666 if (i_byte + 1 < size_byte
1667 && str[i_byte] == '-')
1669 unsigned int c2;
1670 unsigned char leading_code2;
1672 /* Skip over the dash. */
1673 i_byte++;
1675 /* Get the end of the range. */
1676 leading_code2 = str[i_byte];
1677 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1678 i_byte += len;
1680 if (c2 == '\\'
1681 && i_byte < size_byte)
1683 leading_code2 = str[i_byte];
1684 c2 =STRING_CHAR_AND_LENGTH (str + i_byte, len);
1685 i_byte += len;
1688 if (c > c2)
1689 continue;
1690 if (ASCII_CHAR_P (c))
1692 while (c <= c2 && c < 0x80)
1693 fastmap[c++] = 1;
1694 leading_code = CHAR_LEADING_CODE (c);
1696 if (! ASCII_CHAR_P (c))
1698 unsigned lim2 = leading_code2 + 1;
1699 while (leading_code < lim2)
1700 fastmap[leading_code++] = 1;
1701 if (c <= c2)
1703 char_ranges[n_char_ranges++] = c;
1704 char_ranges[n_char_ranges++] = c2;
1708 else
1710 if (ASCII_CHAR_P (c))
1711 fastmap[c] = 1;
1712 else
1714 fastmap[leading_code] = 1;
1715 char_ranges[n_char_ranges++] = c;
1716 char_ranges[n_char_ranges++] = c;
1721 /* If the current range is unibyte and STRING contains non-ASCII
1722 chars, arrange fastmap for the corresponding unibyte
1723 chars. */
1725 if (! multibyte && n_char_ranges > 0)
1727 memset (fastmap + 0200, 0, 0200);
1728 for (i = 0; i < n_char_ranges; i += 2)
1730 int c1 = char_ranges[i];
1731 unsigned lim2 = char_ranges[i + 1] + 1;
1733 for (; c1 < lim2; c1++)
1735 int b = CHAR_TO_BYTE_SAFE (c1);
1736 if (b >= 0)
1737 fastmap[b] = 1;
1743 /* If ^ was the first character, complement the fastmap. */
1744 if (negate)
1746 if (! multibyte)
1747 for (i = 0; i < sizeof fastmap; i++)
1748 fastmap[i] ^= 1;
1749 else
1751 for (i = 0; i < 0200; i++)
1752 fastmap[i] ^= 1;
1753 /* All non-ASCII chars possibly match. */
1754 for (; i < sizeof fastmap; i++)
1755 fastmap[i] = 1;
1760 ptrdiff_t start_point = PT;
1761 ptrdiff_t pos = PT;
1762 ptrdiff_t pos_byte = PT_BYTE;
1763 unsigned char *p = PT_ADDR, *endp, *stop;
1765 if (forwardp)
1767 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1768 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1770 else
1772 endp = CHAR_POS_ADDR (XINT (lim));
1773 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1776 immediate_quit = 1;
1777 /* This code may look up syntax tables using macros that rely on the
1778 gl_state object. To make sure this object is not out of date,
1779 let's initialize it manually.
1780 We ignore syntax-table text-properties for now, since that's
1781 what we've done in the past. */
1782 SETUP_BUFFER_SYNTAX_TABLE ();
1783 if (forwardp)
1785 if (multibyte)
1786 while (1)
1788 int nbytes;
1790 if (p >= stop)
1792 if (p >= endp)
1793 break;
1794 p = GAP_END_ADDR;
1795 stop = endp;
1797 c = STRING_CHAR_AND_LENGTH (p, nbytes);
1798 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1800 if (negate)
1801 break;
1802 else
1803 goto fwd_ok;
1806 if (! fastmap[*p])
1807 break;
1808 if (! ASCII_CHAR_P (c))
1810 /* As we are looking at a multibyte character, we
1811 must look up the character in the table
1812 CHAR_RANGES. If there's no data in the table,
1813 that character is not what we want to skip. */
1815 /* The following code do the right thing even if
1816 n_char_ranges is zero (i.e. no data in
1817 CHAR_RANGES). */
1818 for (i = 0; i < n_char_ranges; i += 2)
1819 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1820 break;
1821 if (!(negate ^ (i < n_char_ranges)))
1822 break;
1824 fwd_ok:
1825 p += nbytes, pos++, pos_byte += nbytes;
1827 else
1828 while (1)
1830 if (p >= stop)
1832 if (p >= endp)
1833 break;
1834 p = GAP_END_ADDR;
1835 stop = endp;
1838 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1840 if (negate)
1841 break;
1842 else
1843 goto fwd_unibyte_ok;
1846 if (!fastmap[*p])
1847 break;
1848 fwd_unibyte_ok:
1849 p++, pos++, pos_byte++;
1852 else
1854 if (multibyte)
1855 while (1)
1857 unsigned char *prev_p;
1859 if (p <= stop)
1861 if (p <= endp)
1862 break;
1863 p = GPT_ADDR;
1864 stop = endp;
1866 prev_p = p;
1867 while (--p >= stop && ! CHAR_HEAD_P (*p));
1868 c = STRING_CHAR (p);
1870 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1872 if (negate)
1873 break;
1874 else
1875 goto back_ok;
1878 if (! fastmap[*p])
1879 break;
1880 if (! ASCII_CHAR_P (c))
1882 /* See the comment in the previous similar code. */
1883 for (i = 0; i < n_char_ranges; i += 2)
1884 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1885 break;
1886 if (!(negate ^ (i < n_char_ranges)))
1887 break;
1889 back_ok:
1890 pos--, pos_byte -= prev_p - p;
1892 else
1893 while (1)
1895 if (p <= stop)
1897 if (p <= endp)
1898 break;
1899 p = GPT_ADDR;
1900 stop = endp;
1903 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
1905 if (negate)
1906 break;
1907 else
1908 goto back_unibyte_ok;
1911 if (!fastmap[p[-1]])
1912 break;
1913 back_unibyte_ok:
1914 p--, pos--, pos_byte--;
1918 SET_PT_BOTH (pos, pos_byte);
1919 immediate_quit = 0;
1921 return make_number (PT - start_point);
1926 static Lisp_Object
1927 skip_syntaxes (int forwardp, Lisp_Object string, Lisp_Object lim)
1929 register unsigned int c;
1930 unsigned char fastmap[0400];
1931 int negate = 0;
1932 register ptrdiff_t i, i_byte;
1933 int multibyte;
1934 ptrdiff_t size_byte;
1935 unsigned char *str;
1937 CHECK_STRING (string);
1939 if (NILP (lim))
1940 XSETINT (lim, forwardp ? ZV : BEGV);
1941 else
1942 CHECK_NUMBER_COERCE_MARKER (lim);
1944 /* In any case, don't allow scan outside bounds of buffer. */
1945 if (XINT (lim) > ZV)
1946 XSETFASTINT (lim, ZV);
1947 if (XINT (lim) < BEGV)
1948 XSETFASTINT (lim, BEGV);
1950 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
1951 return make_number (0);
1953 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1954 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1956 memset (fastmap, 0, sizeof fastmap);
1958 if (SBYTES (string) > SCHARS (string))
1959 /* As this is very rare case (syntax spec is ASCII only), don't
1960 consider efficiency. */
1961 string = string_make_unibyte (string);
1963 str = SDATA (string);
1964 size_byte = SBYTES (string);
1966 i_byte = 0;
1967 if (i_byte < size_byte
1968 && SREF (string, 0) == '^')
1970 negate = 1; i_byte++;
1973 /* Find the syntaxes specified and set their elements of fastmap. */
1975 while (i_byte < size_byte)
1977 c = str[i_byte++];
1978 fastmap[syntax_spec_code[c]] = 1;
1981 /* If ^ was the first character, complement the fastmap. */
1982 if (negate)
1983 for (i = 0; i < sizeof fastmap; i++)
1984 fastmap[i] ^= 1;
1987 ptrdiff_t start_point = PT;
1988 ptrdiff_t pos = PT;
1989 ptrdiff_t pos_byte = PT_BYTE;
1990 unsigned char *p = PT_ADDR, *endp, *stop;
1992 if (forwardp)
1994 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1995 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1997 else
1999 endp = CHAR_POS_ADDR (XINT (lim));
2000 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
2003 immediate_quit = 1;
2004 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
2005 if (forwardp)
2007 if (multibyte)
2009 while (1)
2011 int nbytes;
2013 if (p >= stop)
2015 if (p >= endp)
2016 break;
2017 p = GAP_END_ADDR;
2018 stop = endp;
2020 c = STRING_CHAR_AND_LENGTH (p, nbytes);
2021 if (! fastmap[(int) SYNTAX (c)])
2022 break;
2023 p += nbytes, pos++, pos_byte += nbytes;
2024 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2027 else
2029 while (1)
2031 if (p >= stop)
2033 if (p >= endp)
2034 break;
2035 p = GAP_END_ADDR;
2036 stop = endp;
2038 if (! fastmap[(int) SYNTAX (*p)])
2039 break;
2040 p++, pos++, pos_byte++;
2041 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2045 else
2047 if (multibyte)
2049 while (1)
2051 unsigned char *prev_p;
2053 if (p <= stop)
2055 if (p <= endp)
2056 break;
2057 p = GPT_ADDR;
2058 stop = endp;
2060 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2061 prev_p = p;
2062 while (--p >= stop && ! CHAR_HEAD_P (*p));
2063 c = STRING_CHAR (p);
2064 if (! fastmap[(int) SYNTAX (c)])
2065 break;
2066 pos--, pos_byte -= prev_p - p;
2069 else
2071 while (1)
2073 if (p <= stop)
2075 if (p <= endp)
2076 break;
2077 p = GPT_ADDR;
2078 stop = endp;
2080 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2081 if (! fastmap[(int) SYNTAX (p[-1])])
2082 break;
2083 p--, pos--, pos_byte--;
2088 SET_PT_BOTH (pos, pos_byte);
2089 immediate_quit = 0;
2091 return make_number (PT - start_point);
2095 /* Return 1 if character C belongs to one of the ISO classes
2096 in the list ISO_CLASSES. Each class is represented by an
2097 integer which is its type according to re_wctype. */
2099 static int
2100 in_classes (int c, Lisp_Object iso_classes)
2102 int fits_class = 0;
2104 while (CONSP (iso_classes))
2106 Lisp_Object elt;
2107 elt = XCAR (iso_classes);
2108 iso_classes = XCDR (iso_classes);
2110 if (re_iswctype (c, XFASTINT (elt)))
2111 fits_class = 1;
2114 return fits_class;
2117 /* Jump over a comment, assuming we are at the beginning of one.
2118 FROM is the current position.
2119 FROM_BYTE is the bytepos corresponding to FROM.
2120 Do not move past STOP (a charpos).
2121 The comment over which we have to jump is of style STYLE
2122 (either SYNTAX_FLAGS_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2123 NESTING should be positive to indicate the nesting at the beginning
2124 for nested comments and should be zero or negative else.
2125 ST_COMMENT_STYLE cannot be nested.
2126 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2127 (or 0 If the search cannot start in the middle of a two-character).
2129 If successful, return 1 and store the charpos of the comment's end
2130 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2131 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2132 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2133 (as defined for state.incomment) in *INCOMMENT_PTR.
2135 The comment end is the last character of the comment rather than the
2136 character just after the comment.
2138 Global syntax data is assumed to initially be valid for FROM and
2139 remains valid for forward search starting at the returned position. */
2141 static int
2142 forw_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop,
2143 EMACS_INT nesting, int style, int prev_syntax,
2144 ptrdiff_t *charpos_ptr, ptrdiff_t *bytepos_ptr,
2145 EMACS_INT *incomment_ptr)
2147 register int c, c1;
2148 register enum syntaxcode code;
2149 register int syntax, other_syntax;
2151 if (nesting <= 0) nesting = -1;
2153 /* Enter the loop in the middle so that we find
2154 a 2-char comment ender if we start in the middle of it. */
2155 syntax = prev_syntax;
2156 if (syntax != 0) goto forw_incomment;
2158 while (1)
2160 if (from == stop)
2162 *incomment_ptr = nesting;
2163 *charpos_ptr = from;
2164 *bytepos_ptr = from_byte;
2165 return 0;
2167 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2168 syntax = SYNTAX_WITH_FLAGS (c);
2169 code = syntax & 0xff;
2170 if (code == Sendcomment
2171 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style
2172 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2173 (nesting > 0 && --nesting == 0) : nesting < 0))
2174 /* we have encountered a comment end of the same style
2175 as the comment sequence which began this comment
2176 section */
2177 break;
2178 if (code == Scomment_fence
2179 && style == ST_COMMENT_STYLE)
2180 /* we have encountered a comment end of the same style
2181 as the comment sequence which began this comment
2182 section. */
2183 break;
2184 if (nesting > 0
2185 && code == Scomment
2186 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2187 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style)
2188 /* we have encountered a nested comment of the same style
2189 as the comment sequence which began this comment section */
2190 nesting++;
2191 INC_BOTH (from, from_byte);
2192 UPDATE_SYNTAX_TABLE_FORWARD (from);
2194 forw_incomment:
2195 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2196 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2197 other_syntax = SYNTAX_WITH_FLAGS (c1),
2198 SYNTAX_FLAGS_COMEND_SECOND (other_syntax))
2199 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, other_syntax) == style
2200 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2201 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax))
2202 ? nesting > 0 : nesting < 0))
2204 if (--nesting <= 0)
2205 /* we have encountered a comment end of the same style
2206 as the comment sequence which began this comment
2207 section */
2208 break;
2209 else
2211 INC_BOTH (from, from_byte);
2212 UPDATE_SYNTAX_TABLE_FORWARD (from);
2215 if (nesting > 0
2216 && from < stop
2217 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2218 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2219 other_syntax = SYNTAX_WITH_FLAGS (c1),
2220 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax) == style
2221 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2222 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2223 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax)))
2224 /* we have encountered a nested comment of the same style
2225 as the comment sequence which began this comment
2226 section */
2228 INC_BOTH (from, from_byte);
2229 UPDATE_SYNTAX_TABLE_FORWARD (from);
2230 nesting++;
2233 *charpos_ptr = from;
2234 *bytepos_ptr = from_byte;
2235 return 1;
2238 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2239 doc: /*
2240 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2241 Stop scanning if we find something other than a comment or whitespace.
2242 Set point to where scanning stops.
2243 If COUNT comments are found as expected, with nothing except whitespace
2244 between them, return t; otherwise return nil. */)
2245 (Lisp_Object count)
2247 register ptrdiff_t from;
2248 ptrdiff_t from_byte;
2249 register ptrdiff_t stop;
2250 register int c, c1;
2251 register enum syntaxcode code;
2252 int comstyle = 0; /* style of comment encountered */
2253 int comnested = 0; /* whether the comment is nestable or not */
2254 int found;
2255 EMACS_INT count1;
2256 ptrdiff_t out_charpos, out_bytepos;
2257 EMACS_INT dummy;
2259 CHECK_NUMBER (count);
2260 count1 = XINT (count);
2261 stop = count1 > 0 ? ZV : BEGV;
2263 immediate_quit = 1;
2264 QUIT;
2266 from = PT;
2267 from_byte = PT_BYTE;
2269 SETUP_SYNTAX_TABLE (from, count1);
2270 while (count1 > 0)
2274 int comstart_first, syntax, other_syntax;
2276 if (from == stop)
2278 SET_PT_BOTH (from, from_byte);
2279 immediate_quit = 0;
2280 return Qnil;
2282 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2283 syntax = SYNTAX_WITH_FLAGS (c);
2284 code = SYNTAX (c);
2285 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2286 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2287 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2288 INC_BOTH (from, from_byte);
2289 UPDATE_SYNTAX_TABLE_FORWARD (from);
2290 if (from < stop && comstart_first
2291 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2292 other_syntax = SYNTAX_WITH_FLAGS (c1),
2293 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)))
2295 /* We have encountered a comment start sequence and we
2296 are ignoring all text inside comments. We must record
2297 the comment style this sequence begins so that later,
2298 only a comment end of the same style actually ends
2299 the comment section. */
2300 code = Scomment;
2301 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2302 comnested
2303 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2304 INC_BOTH (from, from_byte);
2305 UPDATE_SYNTAX_TABLE_FORWARD (from);
2308 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2310 if (code == Scomment_fence)
2311 comstyle = ST_COMMENT_STYLE;
2312 else if (code != Scomment)
2314 immediate_quit = 0;
2315 DEC_BOTH (from, from_byte);
2316 SET_PT_BOTH (from, from_byte);
2317 return Qnil;
2319 /* We're at the start of a comment. */
2320 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2321 &out_charpos, &out_bytepos, &dummy);
2322 from = out_charpos; from_byte = out_bytepos;
2323 if (!found)
2325 immediate_quit = 0;
2326 SET_PT_BOTH (from, from_byte);
2327 return Qnil;
2329 INC_BOTH (from, from_byte);
2330 UPDATE_SYNTAX_TABLE_FORWARD (from);
2331 /* We have skipped one comment. */
2332 count1--;
2335 while (count1 < 0)
2337 while (1)
2339 int quoted, syntax;
2341 if (from <= stop)
2343 SET_PT_BOTH (BEGV, BEGV_BYTE);
2344 immediate_quit = 0;
2345 return Qnil;
2348 DEC_BOTH (from, from_byte);
2349 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2350 quoted = char_quoted (from, from_byte);
2351 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2352 syntax = SYNTAX_WITH_FLAGS (c);
2353 code = SYNTAX (c);
2354 comstyle = 0;
2355 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2356 if (code == Sendcomment)
2357 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2358 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2359 && prev_char_comend_first (from, from_byte)
2360 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2362 int other_syntax;
2363 /* We must record the comment style encountered so that
2364 later, we can match only the proper comment begin
2365 sequence of the same style. */
2366 DEC_BOTH (from, from_byte);
2367 code = Sendcomment;
2368 /* Calling char_quoted, above, set up global syntax position
2369 at the new value of FROM. */
2370 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2371 other_syntax = SYNTAX_WITH_FLAGS (c1);
2372 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2373 comnested
2374 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2377 if (code == Scomment_fence)
2379 /* Skip until first preceding unquoted comment_fence. */
2380 int fence_found = 0;
2381 ptrdiff_t ini = from, ini_byte = from_byte;
2383 while (1)
2385 DEC_BOTH (from, from_byte);
2386 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2387 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2388 if (SYNTAX (c) == Scomment_fence
2389 && !char_quoted (from, from_byte))
2391 fence_found = 1;
2392 break;
2394 else if (from == stop)
2395 break;
2397 if (fence_found == 0)
2399 from = ini; /* Set point to ini + 1. */
2400 from_byte = ini_byte;
2401 goto leave;
2403 else
2404 /* We have skipped one comment. */
2405 break;
2407 else if (code == Sendcomment)
2409 found = back_comment (from, from_byte, stop, comnested, comstyle,
2410 &out_charpos, &out_bytepos);
2411 if (found == -1)
2413 if (c == '\n')
2414 /* This end-of-line is not an end-of-comment.
2415 Treat it like a whitespace.
2416 CC-mode (and maybe others) relies on this behavior. */
2418 else
2420 /* Failure: we should go back to the end of this
2421 not-quite-endcomment. */
2422 if (SYNTAX (c) != code)
2423 /* It was a two-char Sendcomment. */
2424 INC_BOTH (from, from_byte);
2425 goto leave;
2428 else
2430 /* We have skipped one comment. */
2431 from = out_charpos, from_byte = out_bytepos;
2432 break;
2435 else if (code != Swhitespace || quoted)
2437 leave:
2438 immediate_quit = 0;
2439 INC_BOTH (from, from_byte);
2440 SET_PT_BOTH (from, from_byte);
2441 return Qnil;
2445 count1++;
2448 SET_PT_BOTH (from, from_byte);
2449 immediate_quit = 0;
2450 return Qt;
2453 /* Return syntax code of character C if C is an ASCII character
2454 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2456 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2457 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2458 ? SYNTAX (c) : Ssymbol)
2460 static Lisp_Object
2461 scan_lists (register EMACS_INT from, EMACS_INT count, EMACS_INT depth, int sexpflag)
2463 Lisp_Object val;
2464 register ptrdiff_t stop = count > 0 ? ZV : BEGV;
2465 register int c, c1;
2466 int stringterm;
2467 int quoted;
2468 int mathexit = 0;
2469 register enum syntaxcode code, temp_code;
2470 EMACS_INT min_depth = depth; /* Err out if depth gets less than this. */
2471 int comstyle = 0; /* style of comment encountered */
2472 int comnested = 0; /* whether the comment is nestable or not */
2473 ptrdiff_t temp_pos;
2474 EMACS_INT last_good = from;
2475 int found;
2476 ptrdiff_t from_byte;
2477 ptrdiff_t out_bytepos, out_charpos;
2478 int temp;
2479 EMACS_INT dummy;
2480 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2482 if (depth > 0) min_depth = 0;
2484 if (from > ZV) from = ZV;
2485 if (from < BEGV) from = BEGV;
2487 from_byte = CHAR_TO_BYTE (from);
2489 immediate_quit = 1;
2490 QUIT;
2492 SETUP_SYNTAX_TABLE (from, count);
2493 while (count > 0)
2495 while (from < stop)
2497 int comstart_first, prefix, syntax, other_syntax;
2498 UPDATE_SYNTAX_TABLE_FORWARD (from);
2499 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2500 syntax = SYNTAX_WITH_FLAGS (c);
2501 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2502 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2503 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2504 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2505 prefix = SYNTAX_FLAGS_PREFIX (syntax);
2506 if (depth == min_depth)
2507 last_good = from;
2508 INC_BOTH (from, from_byte);
2509 UPDATE_SYNTAX_TABLE_FORWARD (from);
2510 if (from < stop && comstart_first
2511 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2512 other_syntax = SYNTAX_WITH_FLAGS (c),
2513 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2514 && parse_sexp_ignore_comments)
2516 /* we have encountered a comment start sequence and we
2517 are ignoring all text inside comments. We must record
2518 the comment style this sequence begins so that later,
2519 only a comment end of the same style actually ends
2520 the comment section */
2521 code = Scomment;
2522 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2523 comnested
2524 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2525 INC_BOTH (from, from_byte);
2526 UPDATE_SYNTAX_TABLE_FORWARD (from);
2529 if (prefix)
2530 continue;
2532 switch (code)
2534 case Sescape:
2535 case Scharquote:
2536 if (from == stop)
2537 goto lose;
2538 INC_BOTH (from, from_byte);
2539 /* treat following character as a word constituent */
2540 case Sword:
2541 case Ssymbol:
2542 if (depth || !sexpflag) break;
2543 /* This word counts as a sexp; return at end of it. */
2544 while (from < stop)
2546 UPDATE_SYNTAX_TABLE_FORWARD (from);
2548 /* Some compilers can't handle this inside the switch. */
2549 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2550 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2551 switch (temp)
2553 case Scharquote:
2554 case Sescape:
2555 INC_BOTH (from, from_byte);
2556 if (from == stop)
2557 goto lose;
2558 break;
2559 case Sword:
2560 case Ssymbol:
2561 case Squote:
2562 break;
2563 default:
2564 goto done;
2566 INC_BOTH (from, from_byte);
2568 goto done;
2570 case Scomment_fence:
2571 comstyle = ST_COMMENT_STYLE;
2572 /* FALLTHROUGH */
2573 case Scomment:
2574 if (!parse_sexp_ignore_comments) break;
2575 UPDATE_SYNTAX_TABLE_FORWARD (from);
2576 found = forw_comment (from, from_byte, stop,
2577 comnested, comstyle, 0,
2578 &out_charpos, &out_bytepos, &dummy);
2579 from = out_charpos, from_byte = out_bytepos;
2580 if (!found)
2582 if (depth == 0)
2583 goto done;
2584 goto lose;
2586 INC_BOTH (from, from_byte);
2587 UPDATE_SYNTAX_TABLE_FORWARD (from);
2588 break;
2590 case Smath:
2591 if (!sexpflag)
2592 break;
2593 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2595 INC_BOTH (from, from_byte);
2597 if (mathexit)
2599 mathexit = 0;
2600 goto close1;
2602 mathexit = 1;
2604 case Sopen:
2605 if (!++depth) goto done;
2606 break;
2608 case Sclose:
2609 close1:
2610 if (!--depth) goto done;
2611 if (depth < min_depth)
2612 xsignal3 (Qscan_error,
2613 build_string ("Containing expression ends prematurely"),
2614 make_number (last_good), make_number (from));
2615 break;
2617 case Sstring:
2618 case Sstring_fence:
2619 temp_pos = dec_bytepos (from_byte);
2620 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2621 while (1)
2623 if (from >= stop)
2624 goto lose;
2625 UPDATE_SYNTAX_TABLE_FORWARD (from);
2626 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2627 if (code == Sstring
2628 ? (c == stringterm
2629 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2630 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
2631 break;
2633 /* Some compilers can't handle this inside the switch. */
2634 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2635 switch (temp)
2637 case Scharquote:
2638 case Sescape:
2639 INC_BOTH (from, from_byte);
2641 INC_BOTH (from, from_byte);
2643 INC_BOTH (from, from_byte);
2644 if (!depth && sexpflag) goto done;
2645 break;
2646 default:
2647 /* Ignore whitespace, punctuation, quote, endcomment. */
2648 break;
2652 /* Reached end of buffer. Error if within object, return nil if between */
2653 if (depth)
2654 goto lose;
2656 immediate_quit = 0;
2657 return Qnil;
2659 /* End of object reached */
2660 done:
2661 count--;
2664 while (count < 0)
2666 while (from > stop)
2668 int syntax;
2669 DEC_BOTH (from, from_byte);
2670 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2671 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2672 syntax= SYNTAX_WITH_FLAGS (c);
2673 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2674 if (depth == min_depth)
2675 last_good = from;
2676 comstyle = 0;
2677 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2678 if (code == Sendcomment)
2679 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2680 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2681 && prev_char_comend_first (from, from_byte)
2682 && parse_sexp_ignore_comments)
2684 /* We must record the comment style encountered so that
2685 later, we can match only the proper comment begin
2686 sequence of the same style. */
2687 int c2, other_syntax;
2688 DEC_BOTH (from, from_byte);
2689 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2690 code = Sendcomment;
2691 c2 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2692 other_syntax = SYNTAX_WITH_FLAGS (c2);
2693 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2694 comnested
2695 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2698 /* Quoting turns anything except a comment-ender
2699 into a word character. Note that this cannot be true
2700 if we decremented FROM in the if-statement above. */
2701 if (code != Sendcomment && char_quoted (from, from_byte))
2703 DEC_BOTH (from, from_byte);
2704 code = Sword;
2706 else if (SYNTAX_FLAGS_PREFIX (syntax))
2707 continue;
2709 switch (code)
2711 case Sword:
2712 case Ssymbol:
2713 case Sescape:
2714 case Scharquote:
2715 if (depth || !sexpflag) break;
2716 /* This word counts as a sexp; count object finished
2717 after passing it. */
2718 while (from > stop)
2720 temp_pos = from_byte;
2721 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2722 DEC_POS (temp_pos);
2723 else
2724 temp_pos--;
2725 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2726 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2727 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2728 /* Don't allow comment-end to be quoted. */
2729 if (temp_code == Sendcomment)
2730 goto done2;
2731 quoted = char_quoted (from - 1, temp_pos);
2732 if (quoted)
2734 DEC_BOTH (from, from_byte);
2735 temp_pos = dec_bytepos (temp_pos);
2736 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2738 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2739 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2740 if (! (quoted || temp_code == Sword
2741 || temp_code == Ssymbol
2742 || temp_code == Squote))
2743 goto done2;
2744 DEC_BOTH (from, from_byte);
2746 goto done2;
2748 case Smath:
2749 if (!sexpflag)
2750 break;
2751 temp_pos = dec_bytepos (from_byte);
2752 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2753 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2754 DEC_BOTH (from, from_byte);
2755 if (mathexit)
2757 mathexit = 0;
2758 goto open2;
2760 mathexit = 1;
2762 case Sclose:
2763 if (!++depth) goto done2;
2764 break;
2766 case Sopen:
2767 open2:
2768 if (!--depth) goto done2;
2769 if (depth < min_depth)
2770 xsignal3 (Qscan_error,
2771 build_string ("Containing expression ends prematurely"),
2772 make_number (last_good), make_number (from));
2773 break;
2775 case Sendcomment:
2776 if (!parse_sexp_ignore_comments)
2777 break;
2778 found = back_comment (from, from_byte, stop, comnested, comstyle,
2779 &out_charpos, &out_bytepos);
2780 /* FIXME: if found == -1, then it really wasn't a comment-end.
2781 For single-char Sendcomment, we can't do much about it apart
2782 from skipping the char.
2783 For 2-char endcomments, we could try again, taking both
2784 chars as separate entities, but it's a lot of trouble
2785 for very little gain, so we don't bother either. -sm */
2786 if (found != -1)
2787 from = out_charpos, from_byte = out_bytepos;
2788 break;
2790 case Scomment_fence:
2791 case Sstring_fence:
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 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2800 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
2801 break;
2803 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2804 break;
2806 case Sstring:
2807 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2808 while (1)
2810 if (from == stop)
2811 goto lose;
2812 DEC_BOTH (from, from_byte);
2813 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2814 if (!char_quoted (from, from_byte)
2815 && (stringterm
2816 == (c = FETCH_CHAR_AS_MULTIBYTE (from_byte)))
2817 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2818 break;
2820 if (!depth && sexpflag) goto done2;
2821 break;
2822 default:
2823 /* Ignore whitespace, punctuation, quote, endcomment. */
2824 break;
2828 /* Reached start of buffer. Error if within object, return nil if between */
2829 if (depth)
2830 goto lose;
2832 immediate_quit = 0;
2833 return Qnil;
2835 done2:
2836 count++;
2840 immediate_quit = 0;
2841 XSETFASTINT (val, from);
2842 return val;
2844 lose:
2845 xsignal3 (Qscan_error,
2846 build_string ("Unbalanced parentheses"),
2847 make_number (last_good), make_number (from));
2850 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2851 doc: /* Scan from character number FROM by COUNT lists.
2852 Scan forward if COUNT is positive, backward if COUNT is negative.
2853 Return the character number of the position thus found.
2855 A \"list", in this context, refers to a balanced parenthetical
2856 grouping, as determined by the syntax table.
2858 If DEPTH is nonzero, treat that as the nesting depth of the starting
2859 point (i.e. the starting point is DEPTH parentheses deep). This
2860 function scans over parentheses until the depth goes to zero COUNT
2861 times. Hence, positive DEPTH moves out that number of levels of
2862 parentheses, while negative DEPTH moves to a deeper level.
2864 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2866 If we reach the beginning or end of the accessible part of the buffer
2867 before we have scanned over COUNT lists, return nil if the depth at
2868 that point is zero, and signal a error if the depth is nonzero. */)
2869 (Lisp_Object from, Lisp_Object count, Lisp_Object depth)
2871 CHECK_NUMBER (from);
2872 CHECK_NUMBER (count);
2873 CHECK_NUMBER (depth);
2875 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2878 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2879 doc: /* Scan from character number FROM by COUNT balanced expressions.
2880 If COUNT is negative, scan backwards.
2881 Returns the character number of the position thus found.
2883 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2885 If the beginning or end of (the accessible part of) the buffer is reached
2886 in the middle of a parenthetical grouping, an error is signaled.
2887 If the beginning or end is reached between groupings
2888 but before count is used up, nil is returned. */)
2889 (Lisp_Object from, Lisp_Object count)
2891 CHECK_NUMBER (from);
2892 CHECK_NUMBER (count);
2894 return scan_lists (XINT (from), XINT (count), 0, 1);
2897 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2898 0, 0, 0,
2899 doc: /* Move point backward over any number of chars with prefix syntax.
2900 This includes chars with "quote" or "prefix" syntax (' or p). */)
2901 (void)
2903 ptrdiff_t beg = BEGV;
2904 ptrdiff_t opoint = PT;
2905 ptrdiff_t opoint_byte = PT_BYTE;
2906 ptrdiff_t pos = PT;
2907 ptrdiff_t pos_byte = PT_BYTE;
2908 int c;
2910 if (pos <= beg)
2912 SET_PT_BOTH (opoint, opoint_byte);
2914 return Qnil;
2917 SETUP_SYNTAX_TABLE (pos, -1);
2919 DEC_BOTH (pos, pos_byte);
2921 while (!char_quoted (pos, pos_byte)
2922 /* Previous statement updates syntax table. */
2923 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
2924 || SYNTAX_PREFIX (c)))
2926 opoint = pos;
2927 opoint_byte = pos_byte;
2929 if (pos + 1 > beg)
2930 DEC_BOTH (pos, pos_byte);
2933 SET_PT_BOTH (opoint, opoint_byte);
2935 return Qnil;
2938 /* Parse forward from FROM / FROM_BYTE to END,
2939 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2940 and return a description of the state of the parse at END.
2941 If STOPBEFORE is nonzero, stop at the start of an atom.
2942 If COMMENTSTOP is 1, stop at the start of a comment.
2943 If COMMENTSTOP is -1, stop at the start or end of a comment,
2944 after the beginning of a string, or after the end of a string. */
2946 static void
2947 scan_sexps_forward (struct lisp_parse_state *stateptr,
2948 ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t end,
2949 EMACS_INT targetdepth, int stopbefore,
2950 Lisp_Object oldstate, int commentstop)
2952 struct lisp_parse_state state;
2954 register enum syntaxcode code;
2955 int c1;
2956 int comnested;
2957 struct level { ptrdiff_t last, prev; };
2958 struct level levelstart[100];
2959 register struct level *curlevel = levelstart;
2960 struct level *endlevel = levelstart + 100;
2961 register EMACS_INT depth; /* Paren depth of current scanning location.
2962 level - levelstart equals this except
2963 when the depth becomes negative. */
2964 EMACS_INT mindepth; /* Lowest DEPTH value seen. */
2965 int start_quoted = 0; /* Nonzero means starting after a char quote */
2966 Lisp_Object tem;
2967 ptrdiff_t prev_from; /* Keep one character before FROM. */
2968 ptrdiff_t prev_from_byte;
2969 int prev_from_syntax;
2970 int boundary_stop = commentstop == -1;
2971 int nofence;
2972 int found;
2973 ptrdiff_t out_bytepos, out_charpos;
2974 int temp;
2976 prev_from = from;
2977 prev_from_byte = from_byte;
2978 if (from != BEGV)
2979 DEC_BOTH (prev_from, prev_from_byte);
2981 /* Use this macro instead of `from++'. */
2982 #define INC_FROM \
2983 do { prev_from = from; \
2984 prev_from_byte = from_byte; \
2985 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2986 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2987 INC_BOTH (from, from_byte); \
2988 if (from < end) \
2989 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2990 } while (0)
2992 immediate_quit = 1;
2993 QUIT;
2995 if (NILP (oldstate))
2997 depth = 0;
2998 state.instring = -1;
2999 state.incomment = 0;
3000 state.comstyle = 0; /* comment style a by default. */
3001 state.comstr_start = -1; /* no comment/string seen. */
3003 else
3005 tem = Fcar (oldstate);
3006 if (!NILP (tem))
3007 depth = XINT (tem);
3008 else
3009 depth = 0;
3011 oldstate = Fcdr (oldstate);
3012 oldstate = Fcdr (oldstate);
3013 oldstate = Fcdr (oldstate);
3014 tem = Fcar (oldstate);
3015 /* Check whether we are inside string_fence-style string: */
3016 state.instring = (!NILP (tem)
3017 ? (CHARACTERP (tem) ? XFASTINT (tem) : ST_STRING_STYLE)
3018 : -1);
3020 oldstate = Fcdr (oldstate);
3021 tem = Fcar (oldstate);
3022 state.incomment = (!NILP (tem)
3023 ? (INTEGERP (tem) ? XINT (tem) : -1)
3024 : 0);
3026 oldstate = Fcdr (oldstate);
3027 tem = Fcar (oldstate);
3028 start_quoted = !NILP (tem);
3030 /* if the eighth element of the list is nil, we are in comment
3031 style a. If it is non-nil, we are in comment style b */
3032 oldstate = Fcdr (oldstate);
3033 oldstate = Fcdr (oldstate);
3034 tem = Fcar (oldstate);
3035 state.comstyle = (NILP (tem)
3037 : (RANGED_INTEGERP (0, tem, ST_COMMENT_STYLE)
3038 ? XINT (tem)
3039 : ST_COMMENT_STYLE));
3041 oldstate = Fcdr (oldstate);
3042 tem = Fcar (oldstate);
3043 state.comstr_start =
3044 RANGED_INTEGERP (PTRDIFF_MIN, tem, PTRDIFF_MAX) ? XINT (tem) : -1;
3045 oldstate = Fcdr (oldstate);
3046 tem = Fcar (oldstate);
3047 while (!NILP (tem)) /* >= second enclosing sexps. */
3049 Lisp_Object temhd = Fcar (tem);
3050 if (RANGED_INTEGERP (PTRDIFF_MIN, temhd, PTRDIFF_MAX))
3051 curlevel->last = XINT (temhd);
3052 if (++curlevel == endlevel)
3053 curlevel--; /* error ("Nesting too deep for parser"); */
3054 curlevel->prev = -1;
3055 curlevel->last = -1;
3056 tem = Fcdr (tem);
3059 state.quoted = 0;
3060 mindepth = depth;
3062 curlevel->prev = -1;
3063 curlevel->last = -1;
3065 SETUP_SYNTAX_TABLE (prev_from, 1);
3066 temp = FETCH_CHAR (prev_from_byte);
3067 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
3068 UPDATE_SYNTAX_TABLE_FORWARD (from);
3070 /* Enter the loop at a place appropriate for initial state. */
3072 if (state.incomment)
3073 goto startincomment;
3074 if (state.instring >= 0)
3076 nofence = state.instring != ST_STRING_STYLE;
3077 if (start_quoted)
3078 goto startquotedinstring;
3079 goto startinstring;
3081 else if (start_quoted)
3082 goto startquoted;
3084 while (from < end)
3086 int syntax;
3087 INC_FROM;
3088 code = prev_from_syntax & 0xff;
3090 if (from < end
3091 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3092 && (c1 = FETCH_CHAR (from_byte),
3093 syntax = SYNTAX_WITH_FLAGS (c1),
3094 SYNTAX_FLAGS_COMSTART_SECOND (syntax)))
3095 /* Duplicate code to avoid a complex if-expression
3096 which causes trouble for the SGI compiler. */
3098 /* Record the comment style we have entered so that only
3099 the comment-end sequence of the same style actually
3100 terminates the comment section. */
3101 state.comstyle
3102 = SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_from_syntax);
3103 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
3104 comnested = comnested || SYNTAX_FLAGS_COMMENT_NESTED (syntax);
3105 state.incomment = comnested ? 1 : -1;
3106 state.comstr_start = prev_from;
3107 INC_FROM;
3108 code = Scomment;
3110 else if (code == Scomment_fence)
3112 /* Record the comment style we have entered so that only
3113 the comment-end sequence of the same style actually
3114 terminates the comment section. */
3115 state.comstyle = ST_COMMENT_STYLE;
3116 state.incomment = -1;
3117 state.comstr_start = prev_from;
3118 code = Scomment;
3120 else if (code == Scomment)
3122 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax, 0);
3123 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3124 1 : -1);
3125 state.comstr_start = prev_from;
3128 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3129 continue;
3130 switch (code)
3132 case Sescape:
3133 case Scharquote:
3134 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3135 curlevel->last = prev_from;
3136 startquoted:
3137 if (from == end) goto endquoted;
3138 INC_FROM;
3139 goto symstarted;
3140 /* treat following character as a word constituent */
3141 case Sword:
3142 case Ssymbol:
3143 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3144 curlevel->last = prev_from;
3145 symstarted:
3146 while (from < end)
3148 /* Some compilers can't handle this inside the switch. */
3149 temp = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3150 temp = SYNTAX (temp);
3151 switch (temp)
3153 case Scharquote:
3154 case Sescape:
3155 INC_FROM;
3156 if (from == end) goto endquoted;
3157 break;
3158 case Sword:
3159 case Ssymbol:
3160 case Squote:
3161 break;
3162 default:
3163 goto symdone;
3165 INC_FROM;
3167 symdone:
3168 curlevel->prev = curlevel->last;
3169 break;
3171 case Scomment_fence: /* Can't happen because it's handled above. */
3172 case Scomment:
3173 if (commentstop || boundary_stop) goto done;
3174 startincomment:
3175 /* The (from == BEGV) test was to enter the loop in the middle so
3176 that we find a 2-char comment ender even if we start in the
3177 middle of it. We don't want to do that if we're just at the
3178 beginning of the comment (think of (*) ... (*)). */
3179 found = forw_comment (from, from_byte, end,
3180 state.incomment, state.comstyle,
3181 (from == BEGV || from < state.comstr_start + 3)
3182 ? 0 : prev_from_syntax,
3183 &out_charpos, &out_bytepos, &state.incomment);
3184 from = out_charpos; from_byte = out_bytepos;
3185 /* Beware! prev_from and friends are invalid now.
3186 Luckily, the `done' doesn't use them and the INC_FROM
3187 sets them to a sane value without looking at them. */
3188 if (!found) goto done;
3189 INC_FROM;
3190 state.incomment = 0;
3191 state.comstyle = 0; /* reset the comment style */
3192 if (boundary_stop) goto done;
3193 break;
3195 case Sopen:
3196 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3197 depth++;
3198 /* curlevel++->last ran into compiler bug on Apollo */
3199 curlevel->last = prev_from;
3200 if (++curlevel == endlevel)
3201 curlevel--; /* error ("Nesting too deep for parser"); */
3202 curlevel->prev = -1;
3203 curlevel->last = -1;
3204 if (targetdepth == depth) goto done;
3205 break;
3207 case Sclose:
3208 depth--;
3209 if (depth < mindepth)
3210 mindepth = depth;
3211 if (curlevel != levelstart)
3212 curlevel--;
3213 curlevel->prev = curlevel->last;
3214 if (targetdepth == depth) goto done;
3215 break;
3217 case Sstring:
3218 case Sstring_fence:
3219 state.comstr_start = from - 1;
3220 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3221 curlevel->last = prev_from;
3222 state.instring = (code == Sstring
3223 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3224 : ST_STRING_STYLE);
3225 if (boundary_stop) goto done;
3226 startinstring:
3228 nofence = state.instring != ST_STRING_STYLE;
3230 while (1)
3232 int c;
3234 if (from >= end) goto done;
3235 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3236 /* Some compilers can't handle this inside the switch. */
3237 temp = SYNTAX (c);
3239 /* Check TEMP here so that if the char has
3240 a syntax-table property which says it is NOT
3241 a string character, it does not end the string. */
3242 if (nofence && c == state.instring && temp == Sstring)
3243 break;
3245 switch (temp)
3247 case Sstring_fence:
3248 if (!nofence) goto string_end;
3249 break;
3250 case Scharquote:
3251 case Sescape:
3252 INC_FROM;
3253 startquotedinstring:
3254 if (from >= end) goto endquoted;
3256 INC_FROM;
3259 string_end:
3260 state.instring = -1;
3261 curlevel->prev = curlevel->last;
3262 INC_FROM;
3263 if (boundary_stop) goto done;
3264 break;
3266 case Smath:
3267 /* FIXME: We should do something with it. */
3268 break;
3269 default:
3270 /* Ignore whitespace, punctuation, quote, endcomment. */
3271 break;
3274 goto done;
3276 stop: /* Here if stopping before start of sexp. */
3277 from = prev_from; /* We have just fetched the char that starts it; */
3278 from_byte = prev_from_byte;
3279 goto done; /* but return the position before it. */
3281 endquoted:
3282 state.quoted = 1;
3283 done:
3284 state.depth = depth;
3285 state.mindepth = mindepth;
3286 state.thislevelstart = curlevel->prev;
3287 state.prevlevelstart
3288 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3289 state.location = from;
3290 state.location_byte = from_byte;
3291 state.levelstarts = Qnil;
3292 while (curlevel > levelstart)
3293 state.levelstarts = Fcons (make_number ((--curlevel)->last),
3294 state.levelstarts);
3295 immediate_quit = 0;
3297 *stateptr = state;
3300 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3301 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3302 Parsing stops at TO or when certain criteria are met;
3303 point is set to where parsing stops.
3304 If fifth arg OLDSTATE is omitted or nil,
3305 parsing assumes that FROM is the beginning of a function.
3306 Value is a list of elements describing final state of parsing:
3307 0. depth in parens.
3308 1. character address of start of innermost containing list; nil if none.
3309 2. character address of start of last complete sexp terminated.
3310 3. non-nil if inside a string.
3311 (it is the character that will terminate the string,
3312 or t if the string should be terminated by a generic string delimiter.)
3313 4. nil if outside a comment, t if inside a non-nestable comment,
3314 else an integer (the current comment nesting).
3315 5. t if following a quote character.
3316 6. the minimum paren-depth encountered during this scan.
3317 7. style of comment, if any.
3318 8. character address of start of comment or string; nil if not in one.
3319 9. Intermediate data for continuation of parsing (subject to change).
3320 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3321 in parentheses becomes equal to TARGETDEPTH.
3322 Fourth arg STOPBEFORE non-nil means stop when come to
3323 any character that starts a sexp.
3324 Fifth arg OLDSTATE is a list like what this function returns.
3325 It is used to initialize the state of the parse. Elements number 1, 2, 6
3326 are ignored.
3327 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3328 If it is symbol `syntax-table', stop after the start of a comment or a
3329 string, or after end of a comment or a string. */)
3330 (Lisp_Object from, Lisp_Object to, Lisp_Object targetdepth,
3331 Lisp_Object stopbefore, Lisp_Object oldstate, Lisp_Object commentstop)
3333 struct lisp_parse_state state;
3334 EMACS_INT target;
3336 if (!NILP (targetdepth))
3338 CHECK_NUMBER (targetdepth);
3339 target = XINT (targetdepth);
3341 else
3342 target = TYPE_MINIMUM (EMACS_INT); /* We won't reach this depth */
3344 validate_region (&from, &to);
3345 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3346 XINT (to),
3347 target, !NILP (stopbefore), oldstate,
3348 (NILP (commentstop)
3349 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3351 SET_PT_BOTH (state.location, state.location_byte);
3353 return Fcons (make_number (state.depth),
3354 Fcons (state.prevlevelstart < 0
3355 ? Qnil : make_number (state.prevlevelstart),
3356 Fcons (state.thislevelstart < 0
3357 ? Qnil : make_number (state.thislevelstart),
3358 Fcons (state.instring >= 0
3359 ? (state.instring == ST_STRING_STYLE
3360 ? Qt : make_number (state.instring)) : Qnil,
3361 Fcons (state.incomment < 0 ? Qt :
3362 (state.incomment == 0 ? Qnil :
3363 make_number (state.incomment)),
3364 Fcons (state.quoted ? Qt : Qnil,
3365 Fcons (make_number (state.mindepth),
3366 Fcons ((state.comstyle
3367 ? (state.comstyle == ST_COMMENT_STYLE
3368 ? Qsyntax_table
3369 : make_number (state.comstyle))
3370 : Qnil),
3371 Fcons (((state.incomment
3372 || (state.instring >= 0))
3373 ? make_number (state.comstr_start)
3374 : Qnil),
3375 Fcons (state.levelstarts, Qnil))))))))));
3378 void
3379 init_syntax_once (void)
3381 register int i, c;
3382 Lisp_Object temp;
3384 /* This has to be done here, before we call Fmake_char_table. */
3385 DEFSYM (Qsyntax_table, "syntax-table");
3387 /* Intern_C_String this now in case it isn't already done.
3388 Setting this variable twice is harmless.
3389 But don't staticpro it here--that is done in alloc.c. */
3390 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
3392 /* Create objects which can be shared among syntax tables. */
3393 Vsyntax_code_object = make_uninit_vector (Smax);
3394 for (i = 0; i < Smax; i++)
3395 ASET (Vsyntax_code_object, i, Fcons (make_number (i), Qnil));
3397 /* Now we are ready to set up this property, so we can
3398 create syntax tables. */
3399 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3401 temp = AREF (Vsyntax_code_object, (int) Swhitespace);
3403 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3405 /* Control characters should not be whitespace. */
3406 temp = AREF (Vsyntax_code_object, (int) Spunct);
3407 for (i = 0; i <= ' ' - 1; i++)
3408 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3409 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3411 /* Except that a few really are whitespace. */
3412 temp = AREF (Vsyntax_code_object, (int) Swhitespace);
3413 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3414 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3415 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3416 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3417 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3419 temp = AREF (Vsyntax_code_object, (int) Sword);
3420 for (i = 'a'; i <= 'z'; i++)
3421 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3422 for (i = 'A'; i <= 'Z'; i++)
3423 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3424 for (i = '0'; i <= '9'; i++)
3425 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3427 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3428 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3430 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3431 Fcons (make_number (Sopen), make_number (')')));
3432 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3433 Fcons (make_number (Sclose), make_number ('(')));
3434 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3435 Fcons (make_number (Sopen), make_number (']')));
3436 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3437 Fcons (make_number (Sclose), make_number ('[')));
3438 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3439 Fcons (make_number (Sopen), make_number ('}')));
3440 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3441 Fcons (make_number (Sclose), make_number ('{')));
3442 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3443 Fcons (make_number ((int) Sstring), Qnil));
3444 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3445 Fcons (make_number ((int) Sescape), Qnil));
3447 temp = AREF (Vsyntax_code_object, (int) Ssymbol);
3448 for (i = 0; i < 10; i++)
3450 c = "_-+*/&|<>="[i];
3451 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3454 temp = AREF (Vsyntax_code_object, (int) Spunct);
3455 for (i = 0; i < 12; i++)
3457 c = ".,;:?!#@~^'`"[i];
3458 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3461 /* All multibyte characters have syntax `word' by default. */
3462 temp = AREF (Vsyntax_code_object, (int) Sword);
3463 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3466 void
3467 syms_of_syntax (void)
3469 DEFSYM (Qsyntax_table_p, "syntax-table-p");
3471 staticpro (&Vsyntax_code_object);
3473 staticpro (&gl_state.object);
3474 staticpro (&gl_state.global_code);
3475 staticpro (&gl_state.current_syntax_table);
3476 staticpro (&gl_state.old_prop);
3478 /* Defined in regex.c */
3479 staticpro (&re_match_object);
3481 DEFSYM (Qscan_error, "scan-error");
3482 Fput (Qscan_error, Qerror_conditions,
3483 listn (CONSTYPE_PURE, 2, Qscan_error, Qerror));
3484 Fput (Qscan_error, Qerror_message,
3485 build_pure_c_string ("Scan error"));
3487 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments,
3488 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3490 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties,
3491 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3492 Otherwise, that text property is simply ignored.
3493 See the info node `(elisp)Syntax Properties' for a description of the
3494 `syntax-table' property. */);
3496 words_include_escapes = 0;
3497 DEFVAR_BOOL ("words-include-escapes", words_include_escapes,
3498 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3500 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol,
3501 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3502 multibyte_syntax_as_symbol = 0;
3504 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3505 open_paren_in_column_0_is_defun_start,
3506 doc: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3507 open_paren_in_column_0_is_defun_start = 1;
3510 DEFVAR_LISP ("find-word-boundary-function-table",
3511 Vfind_word_boundary_function_table,
3512 doc: /*
3513 Char table of functions to search for the word boundary.
3514 Each function is called with two arguments; POS and LIMIT.
3515 POS and LIMIT are character positions in the current buffer.
3517 If POS is less than LIMIT, POS is at the first character of a word,
3518 and the return value of a function is a position after the last
3519 character of that word.
3521 If POS is not less than LIMIT, POS is at the last character of a word,
3522 and the return value of a function is a position at the first
3523 character of that word.
3525 In both cases, LIMIT bounds the search. */);
3526 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3528 defsubr (&Ssyntax_table_p);
3529 defsubr (&Ssyntax_table);
3530 defsubr (&Sstandard_syntax_table);
3531 defsubr (&Scopy_syntax_table);
3532 defsubr (&Sset_syntax_table);
3533 defsubr (&Schar_syntax);
3534 defsubr (&Smatching_paren);
3535 defsubr (&Sstring_to_syntax);
3536 defsubr (&Smodify_syntax_entry);
3537 defsubr (&Sinternal_describe_syntax_value);
3539 defsubr (&Sforward_word);
3541 defsubr (&Sskip_chars_forward);
3542 defsubr (&Sskip_chars_backward);
3543 defsubr (&Sskip_syntax_forward);
3544 defsubr (&Sskip_syntax_backward);
3546 defsubr (&Sforward_comment);
3547 defsubr (&Sscan_lists);
3548 defsubr (&Sscan_sexps);
3549 defsubr (&Sbackward_prefix_chars);
3550 defsubr (&Sparse_partial_sexp);