Merge from trunk.
[emacs.git] / src / syntax.c
bloba802360582b30909124ac8d2da2495ffd1fa6437
1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2012
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include <ctype.h>
24 #include <sys/types.h>
25 #include <setjmp.h>
26 #include "lisp.h"
27 #include "commands.h"
28 #include "buffer.h"
29 #include "character.h"
30 #include "keymap.h"
31 #include "regex.h"
33 /* Make syntax table lookup grant data in gl_state. */
34 #define SYNTAX_ENTRY_VIA_PROPERTY
36 #include "syntax.h"
37 #include "intervals.h"
38 #include "category.h"
40 /* Then there are seven single-bit flags that have the following meanings:
41 1. This character is the first of a two-character comment-start sequence.
42 2. This character is the second of a two-character comment-start sequence.
43 3. This character is the first of a two-character comment-end sequence.
44 4. This character is the second of a two-character comment-end sequence.
45 5. This character is a prefix, for backward-prefix-chars.
46 6. The char is part of a delimiter for comments of style "b".
47 7. This character is part of a nestable comment sequence.
48 8. The char is part of a delimiter for comments of style "c".
49 Note that any two-character sequence whose first character has flag 1
50 and whose second character has flag 2 will be interpreted as a comment start.
52 bit 6 and 8 are used to discriminate between different comment styles.
53 Languages such as C++ allow two orthogonal syntax start/end pairs
54 and bit 6 is used to determine whether a comment-end or Scommentend
55 ends style a or b. Comment markers can start style a, b, c, or bc.
56 Style a is always the default.
57 For 2-char comment markers, the style b flag is only looked up on the second
58 char of the comment marker and on the first char of the comment ender.
59 For style c (like to for the nested flag), the flag can be placed on any
60 one of the chars.
63 /* These macros extract specific flags from an integer
64 that holds the syntax code and the flags. */
66 #define SYNTAX_FLAGS_COMSTART_FIRST(flags) (((flags) >> 16) & 1)
68 #define SYNTAX_FLAGS_COMSTART_SECOND(flags) (((flags) >> 17) & 1)
70 #define SYNTAX_FLAGS_COMEND_FIRST(flags) (((flags) >> 18) & 1)
72 #define SYNTAX_FLAGS_COMEND_SECOND(flags) (((flags) >> 19) & 1)
74 #define SYNTAX_FLAGS_PREFIX(flags) (((flags) >> 20) & 1)
76 #define SYNTAX_FLAGS_COMMENT_STYLEB(flags) (((flags) >> 21) & 1)
77 #define SYNTAX_FLAGS_COMMENT_STYLEC(flags) (((flags) >> 22) & 2)
78 /* FLAGS should be the flags of the main char of the comment marker, e.g.
79 the second for comstart and the first for comend. */
80 #define SYNTAX_FLAGS_COMMENT_STYLE(flags, other_flags) \
81 (SYNTAX_FLAGS_COMMENT_STYLEB (flags) \
82 | SYNTAX_FLAGS_COMMENT_STYLEC (flags) \
83 | SYNTAX_FLAGS_COMMENT_STYLEC (other_flags))
85 #define SYNTAX_FLAGS_COMMENT_NESTED(flags) (((flags) >> 22) & 1)
87 /* These macros extract a particular flag for a given character. */
89 #define SYNTAX_COMEND_FIRST(c) \
90 (SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c)))
91 #define SYNTAX_PREFIX(c) (SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c)))
93 /* We use these constants in place for comment-style and
94 string-ender-char to distinguish comments/strings started by
95 comment_fence and string_fence codes. */
97 #define ST_COMMENT_STYLE (256 + 1)
98 #define ST_STRING_STYLE (256 + 2)
100 static Lisp_Object Qsyntax_table_p;
101 static Lisp_Object Qsyntax_table, Qscan_error;
103 #ifndef __GNUC__
104 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
105 if not compiled with GCC. No need to mark it, since it is used
106 only very temporarily. */
107 Lisp_Object syntax_temp;
108 #endif
110 /* This is the internal form of the parse state used in parse-partial-sexp. */
112 struct lisp_parse_state
114 EMACS_INT depth; /* Depth at end of parsing. */
115 int instring; /* -1 if not within string, else desired terminator. */
116 EMACS_INT incomment; /* -1 if in unnestable comment else comment nesting */
117 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
118 int quoted; /* Nonzero if just after an escape char at end of parsing */
119 EMACS_INT mindepth; /* Minimum depth seen while scanning. */
120 /* Char number of most recent start-of-expression at current level */
121 ptrdiff_t thislevelstart;
122 /* Char number of start of containing expression */
123 ptrdiff_t prevlevelstart;
124 ptrdiff_t location; /* Char number at which parsing stopped. */
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 Fsyntax_table_p (Lisp_Object);
147 static Lisp_Object skip_chars (int, Lisp_Object, Lisp_Object, int);
148 static Lisp_Object skip_syntaxes (int, Lisp_Object, Lisp_Object);
149 static Lisp_Object scan_lists (EMACS_INT, EMACS_INT, EMACS_INT, int);
150 static void scan_sexps_forward (struct lisp_parse_state *,
151 ptrdiff_t, ptrdiff_t, ptrdiff_t, EMACS_INT,
152 int, Lisp_Object, int);
153 static int in_classes (int, Lisp_Object);
155 /* Whether the syntax of the character C has the prefix flag set. */
156 int syntax_prefix_flag_p (int c)
158 return SYNTAX_PREFIX (c);
161 struct gl_state_s gl_state; /* Global state of syntax parser. */
163 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
164 to scan to property-change. */
166 /* Update gl_state to an appropriate interval which contains CHARPOS. The
167 sign of COUNT give the relative position of CHARPOS wrt the previously
168 valid interval. If INIT, only [be]_property fields of gl_state are
169 valid at start, the rest is filled basing on OBJECT.
171 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
172 direction than the intervals - or in an interval. We update the
173 current syntax-table basing on the property of this interval, and
174 update the interval to start further than CHARPOS - or be
175 NULL_INTERVAL. We also update lim_property to be the next value of
176 charpos to call this subroutine again - or be before/after the
177 start/end of OBJECT. */
179 void
180 update_syntax_table (ptrdiff_t charpos, EMACS_INT count, int init,
181 Lisp_Object object)
183 Lisp_Object tmp_table;
184 unsigned cnt = 0;
185 int invalidate = 1;
186 INTERVAL i;
188 if (init)
190 gl_state.old_prop = Qnil;
191 gl_state.start = gl_state.b_property;
192 gl_state.stop = gl_state.e_property;
193 i = interval_of (charpos, object);
194 gl_state.backward_i = gl_state.forward_i = i;
195 invalidate = 0;
196 if (NULL_INTERVAL_P (i))
197 return;
198 /* interval_of updates only ->position of the return value, so
199 update the parents manually to speed up update_interval. */
200 while (!NULL_PARENT (i))
202 if (AM_RIGHT_CHILD (i))
203 INTERVAL_PARENT (i)->position = i->position
204 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
205 - TOTAL_LENGTH (INTERVAL_PARENT (i))
206 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
207 else
208 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
209 + TOTAL_LENGTH (i);
210 i = INTERVAL_PARENT (i);
212 i = gl_state.forward_i;
213 gl_state.b_property = i->position - gl_state.offset;
214 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
215 goto update;
217 i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
219 /* We are guaranteed to be called with CHARPOS either in i,
220 or further off. */
221 if (NULL_INTERVAL_P (i))
222 error ("Error in syntax_table logic for to-the-end intervals");
223 else if (charpos < i->position) /* Move left. */
225 if (count > 0)
226 error ("Error in syntax_table logic for intervals <-");
227 /* Update the interval. */
228 i = update_interval (i, charpos);
229 if (INTERVAL_LAST_POS (i) != gl_state.b_property)
231 invalidate = 0;
232 gl_state.forward_i = i;
233 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
236 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
238 if (count < 0)
239 error ("Error in syntax_table logic for intervals ->");
240 /* Update the interval. */
241 i = update_interval (i, charpos);
242 if (i->position != gl_state.e_property)
244 invalidate = 0;
245 gl_state.backward_i = i;
246 gl_state.b_property = i->position - gl_state.offset;
250 update:
251 tmp_table = textget (i->plist, Qsyntax_table);
253 if (invalidate)
254 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
256 if (invalidate) /* Did not get to adjacent interval. */
257 { /* with the same table => */
258 /* invalidate the old range. */
259 if (count > 0)
261 gl_state.backward_i = i;
262 gl_state.b_property = i->position - gl_state.offset;
264 else
266 gl_state.forward_i = i;
267 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
271 if (!EQ (tmp_table, gl_state.old_prop))
273 gl_state.current_syntax_table = tmp_table;
274 gl_state.old_prop = tmp_table;
275 if (EQ (Fsyntax_table_p (tmp_table), Qt))
277 gl_state.use_global = 0;
279 else if (CONSP (tmp_table))
281 gl_state.use_global = 1;
282 gl_state.global_code = tmp_table;
284 else
286 gl_state.use_global = 0;
287 gl_state.current_syntax_table = BVAR (current_buffer, syntax_table);
291 while (!NULL_INTERVAL_P (i))
293 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
295 if (count > 0)
297 gl_state.e_property = i->position - gl_state.offset;
298 gl_state.forward_i = i;
300 else
302 gl_state.b_property
303 = i->position + LENGTH (i) - gl_state.offset;
304 gl_state.backward_i = i;
306 return;
308 else if (cnt == INTERVALS_AT_ONCE)
310 if (count > 0)
312 gl_state.e_property
313 = i->position + LENGTH (i) - gl_state.offset
314 /* e_property at EOB is not set to ZV but to ZV+1, so that
315 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
316 having to check eob between the two. */
317 + (NULL_INTERVAL_P (next_interval (i)) ? 1 : 0);
318 gl_state.forward_i = i;
320 else
322 gl_state.b_property = i->position - gl_state.offset;
323 gl_state.backward_i = i;
325 return;
327 cnt++;
328 i = count > 0 ? next_interval (i) : previous_interval (i);
330 eassert (NULL_INTERVAL_P (i)); /* This property goes to the end. */
331 if (count > 0)
332 gl_state.e_property = gl_state.stop;
333 else
334 gl_state.b_property = gl_state.start;
337 /* Returns TRUE if char at CHARPOS is quoted.
338 Global syntax-table data should be set up already to be good at CHARPOS
339 or after. On return global syntax data is good for lookup at CHARPOS. */
341 static int
342 char_quoted (ptrdiff_t charpos, ptrdiff_t bytepos)
344 register enum syntaxcode code;
345 register ptrdiff_t beg = BEGV;
346 register int quoted = 0;
347 ptrdiff_t orig = charpos;
349 while (charpos > beg)
351 int c;
352 DEC_BOTH (charpos, bytepos);
354 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
355 c = FETCH_CHAR_AS_MULTIBYTE (bytepos);
356 code = SYNTAX (c);
357 if (! (code == Scharquote || code == Sescape))
358 break;
360 quoted = !quoted;
363 UPDATE_SYNTAX_TABLE (orig);
364 return quoted;
367 /* Return the bytepos one character before BYTEPOS.
368 We assume that BYTEPOS is not at the start of the buffer. */
370 static inline ptrdiff_t
371 dec_bytepos (ptrdiff_t bytepos)
373 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
374 return bytepos - 1;
376 DEC_POS (bytepos);
377 return bytepos;
380 /* Return a defun-start position before POS and not too far before.
381 It should be the last one before POS, or nearly the last.
383 When open_paren_in_column_0_is_defun_start is nonzero,
384 only the beginning of the buffer is treated as a defun-start.
386 We record the information about where the scan started
387 and what its result was, so that another call in the same area
388 can return the same value very quickly.
390 There is no promise at which position the global syntax data is
391 valid on return from the subroutine, so the caller should explicitly
392 update the global data. */
394 static ptrdiff_t
395 find_defun_start (ptrdiff_t pos, ptrdiff_t pos_byte)
397 ptrdiff_t opoint = PT, opoint_byte = PT_BYTE;
399 if (!open_paren_in_column_0_is_defun_start)
401 find_start_value = BEGV;
402 find_start_value_byte = BEGV_BYTE;
403 find_start_buffer = current_buffer;
404 find_start_modiff = MODIFF;
405 find_start_begv = BEGV;
406 find_start_pos = pos;
407 return BEGV;
410 /* Use previous finding, if it's valid and applies to this inquiry. */
411 if (current_buffer == find_start_buffer
412 /* Reuse the defun-start even if POS is a little farther on.
413 POS might be in the next defun, but that's ok.
414 Our value may not be the best possible, but will still be usable. */
415 && pos <= find_start_pos + 1000
416 && pos >= find_start_value
417 && BEGV == find_start_begv
418 && MODIFF == find_start_modiff)
419 return find_start_value;
421 /* Back up to start of line. */
422 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
424 /* We optimize syntax-table lookup for rare updates. Thus we accept
425 only those `^\s(' which are good in global _and_ text-property
426 syntax-tables. */
427 SETUP_BUFFER_SYNTAX_TABLE ();
428 while (PT > BEGV)
430 int c;
432 /* Open-paren at start of line means we may have found our
433 defun-start. */
434 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
435 if (SYNTAX (c) == Sopen)
437 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
438 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
439 if (SYNTAX (c) == Sopen)
440 break;
441 /* Now fallback to the default value. */
442 SETUP_BUFFER_SYNTAX_TABLE ();
444 /* Move to beg of previous line. */
445 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
448 /* Record what we found, for the next try. */
449 find_start_value = PT;
450 find_start_value_byte = PT_BYTE;
451 find_start_buffer = current_buffer;
452 find_start_modiff = MODIFF;
453 find_start_begv = BEGV;
454 find_start_pos = pos;
456 TEMP_SET_PT_BOTH (opoint, opoint_byte);
458 return find_start_value;
461 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
463 static int
464 prev_char_comend_first (ptrdiff_t pos, ptrdiff_t pos_byte)
466 int c, val;
468 DEC_BOTH (pos, pos_byte);
469 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
470 c = FETCH_CHAR (pos_byte);
471 val = SYNTAX_COMEND_FIRST (c);
472 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
473 return val;
476 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
478 /* static int
479 * prev_char_comstart_first (pos, pos_byte)
480 * int pos, pos_byte;
482 * int c, val;
484 * DEC_BOTH (pos, pos_byte);
485 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
486 * c = FETCH_CHAR (pos_byte);
487 * val = SYNTAX_COMSTART_FIRST (c);
488 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
489 * return val;
490 * } */
492 /* Checks whether charpos FROM is at the end of a comment.
493 FROM_BYTE is the bytepos corresponding to FROM.
494 Do not move back before STOP.
496 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
497 return -1 otherwise.
499 If successful, store the charpos of the comment's beginning
500 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
502 Global syntax data remains valid for backward search starting at
503 the returned value (or at FROM, if the search was not successful). */
505 static int
506 back_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop, int comnested, int comstyle, ptrdiff_t *charpos_ptr, ptrdiff_t *bytepos_ptr)
508 /* Look back, counting the parity of string-quotes,
509 and recording the comment-starters seen.
510 When we reach a safe place, assume that's not in a string;
511 then step the main scan to the earliest comment-starter seen
512 an even number of string quotes away from the safe place.
514 OFROM[I] is position of the earliest comment-starter seen
515 which is I+2X quotes from the comment-end.
516 PARITY is current parity of quotes from the comment end. */
517 int string_style = -1; /* Presumed outside of any string. */
518 int string_lossage = 0;
519 /* Not a real lossage: indicates that we have passed a matching comment
520 starter plus a non-matching comment-ender, meaning that any matching
521 comment-starter we might see later could be a false positive (hidden
522 inside another comment).
523 Test case: { a (* b } c (* d *) */
524 int comment_lossage = 0;
525 ptrdiff_t comment_end = from;
526 ptrdiff_t comment_end_byte = from_byte;
527 ptrdiff_t comstart_pos = 0;
528 ptrdiff_t comstart_byte IF_LINT (= 0);
529 /* Place where the containing defun starts,
530 or 0 if we didn't come across it yet. */
531 ptrdiff_t defun_start = 0;
532 ptrdiff_t defun_start_byte = 0;
533 register enum syntaxcode code;
534 int nesting = 1; /* current comment nesting */
535 int c;
536 int syntax = 0;
538 /* FIXME: A }} comment-ender style leads to incorrect behavior
539 in the case of {{ c }}} because we ignore the last two chars which are
540 assumed to be comment-enders although they aren't. */
542 /* At beginning of range to scan, we're outside of strings;
543 that determines quote parity to the comment-end. */
544 while (from != stop)
546 ptrdiff_t temp_byte;
547 int prev_syntax, com2start, com2end;
548 int comstart;
550 /* Move back and examine a character. */
551 DEC_BOTH (from, from_byte);
552 UPDATE_SYNTAX_TABLE_BACKWARD (from);
554 prev_syntax = syntax;
555 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
556 syntax = SYNTAX_WITH_FLAGS (c);
557 code = SYNTAX (c);
559 /* Check for 2-char comment markers. */
560 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
561 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
562 && (comstyle
563 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax, syntax))
564 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
565 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
566 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
567 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
568 comstart = (com2start || code == Scomment);
570 /* Nasty cases with overlapping 2-char comment markers:
571 - snmp-mode: -- c -- foo -- c --
572 --- c --
573 ------ c --
574 - c-mode: *||*
575 |* *|* *|
576 |*| |* |*|
577 /// */
579 /* If a 2-char comment sequence partly overlaps with another,
580 we don't try to be clever. E.g. |*| in C, or }% in modes that
581 have %..\n and %{..}%. */
582 if (from > stop && (com2end || comstart))
584 ptrdiff_t next = from, next_byte = from_byte;
585 int next_c, next_syntax;
586 DEC_BOTH (next, next_byte);
587 UPDATE_SYNTAX_TABLE_BACKWARD (next);
588 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
589 next_syntax = SYNTAX_WITH_FLAGS (next_c);
590 if (((comstart || comnested)
591 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
592 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
593 || ((com2end || comnested)
594 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
595 && (comstyle
596 == SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_syntax))
597 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
598 goto lossage;
599 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
602 if (com2start && comstart_pos == 0)
603 /* We're looking at a comment starter. But it might be a comment
604 ender as well (see snmp-mode). The first time we see one, we
605 need to consider it as a comment starter,
606 and the subsequent times as a comment ender. */
607 com2end = 0;
609 /* Turn a 2-char comment sequences into the appropriate syntax. */
610 if (com2end)
611 code = Sendcomment;
612 else if (com2start)
613 code = Scomment;
614 /* Ignore comment starters of a different style. */
615 else if (code == Scomment
616 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0)
617 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
618 continue;
620 /* Ignore escaped characters, except comment-enders. */
621 if (code != Sendcomment && char_quoted (from, from_byte))
622 continue;
624 switch (code)
626 case Sstring_fence:
627 case Scomment_fence:
628 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
629 case Sstring:
630 /* Track parity of quotes. */
631 if (string_style == -1)
632 /* Entering a string. */
633 string_style = c;
634 else if (string_style == c)
635 /* Leaving the string. */
636 string_style = -1;
637 else
638 /* If we have two kinds of string delimiters.
639 There's no way to grok this scanning backwards. */
640 string_lossage = 1;
641 break;
643 case Scomment:
644 /* We've already checked that it is the relevant comstyle. */
645 if (string_style != -1 || comment_lossage || string_lossage)
646 /* There are odd string quotes involved, so let's be careful.
647 Test case in Pascal: " { " a { " } */
648 goto lossage;
650 if (!comnested)
652 /* Record best comment-starter so far. */
653 comstart_pos = from;
654 comstart_byte = from_byte;
656 else if (--nesting <= 0)
657 /* nested comments have to be balanced, so we don't need to
658 keep looking for earlier ones. We use here the same (slightly
659 incorrect) reasoning as below: since it is followed by uniform
660 paired string quotes, this comment-start has to be outside of
661 strings, else the comment-end itself would be inside a string. */
662 goto done;
663 break;
665 case Sendcomment:
666 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == comstyle
667 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
668 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
669 /* This is the same style of comment ender as ours. */
671 if (comnested)
672 nesting++;
673 else
674 /* Anything before that can't count because it would match
675 this comment-ender rather than ours. */
676 from = stop; /* Break out of the loop. */
678 else if (comstart_pos != 0 || c != '\n')
679 /* We're mixing comment styles here, so we'd better be careful.
680 The (comstart_pos != 0 || c != '\n') check is not quite correct
681 (we should just always set comment_lossage), but removing it
682 would imply that any multiline comment in C would go through
683 lossage, which seems overkill.
684 The failure should only happen in the rare cases such as
685 { (* } *) */
686 comment_lossage = 1;
687 break;
689 case Sopen:
690 /* Assume a defun-start point is outside of strings. */
691 if (open_paren_in_column_0_is_defun_start
692 && (from == stop
693 || (temp_byte = dec_bytepos (from_byte),
694 FETCH_CHAR (temp_byte) == '\n')))
696 defun_start = from;
697 defun_start_byte = from_byte;
698 from = stop; /* Break out of the loop. */
700 break;
702 default:
703 break;
707 if (comstart_pos == 0)
709 from = comment_end;
710 from_byte = comment_end_byte;
711 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
713 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
714 or `done'), then we've found the beginning of the non-nested comment. */
715 else if (1) /* !comnested */
717 from = comstart_pos;
718 from_byte = comstart_byte;
719 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
721 else
723 struct lisp_parse_state state;
724 lossage:
725 /* We had two kinds of string delimiters mixed up
726 together. Decode this going forwards.
727 Scan fwd from a known safe place (beginning-of-defun)
728 to the one in question; this records where we
729 last passed a comment starter. */
730 /* If we did not already find the defun start, find it now. */
731 if (defun_start == 0)
733 defun_start = find_defun_start (comment_end, comment_end_byte);
734 defun_start_byte = find_start_value_byte;
738 scan_sexps_forward (&state,
739 defun_start, defun_start_byte,
740 comment_end, TYPE_MINIMUM (EMACS_INT),
741 0, Qnil, 0);
742 defun_start = comment_end;
743 if (state.incomment == (comnested ? 1 : -1)
744 && state.comstyle == comstyle)
745 from = state.comstr_start;
746 else
748 from = comment_end;
749 if (state.incomment)
750 /* If comment_end is inside some other comment, maybe ours
751 is nested, so we need to try again from within the
752 surrounding comment. Example: { a (* " *) */
754 /* FIXME: We should advance by one or two chars. */
755 defun_start = state.comstr_start + 2;
756 defun_start_byte = CHAR_TO_BYTE (defun_start);
759 } while (defun_start < comment_end);
761 from_byte = CHAR_TO_BYTE (from);
762 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
765 done:
766 *charpos_ptr = from;
767 *bytepos_ptr = from_byte;
769 return (from == comment_end) ? -1 : from;
772 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
773 doc: /* Return t if OBJECT is a syntax table.
774 Currently, any char-table counts as a syntax table. */)
775 (Lisp_Object object)
777 if (CHAR_TABLE_P (object)
778 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
779 return Qt;
780 return Qnil;
783 static void
784 check_syntax_table (Lisp_Object obj)
786 CHECK_TYPE (CHAR_TABLE_P (obj) && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table),
787 Qsyntax_table_p, obj);
790 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
791 doc: /* Return the current syntax table.
792 This is the one specified by the current buffer. */)
793 (void)
795 return BVAR (current_buffer, syntax_table);
798 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
799 Sstandard_syntax_table, 0, 0, 0,
800 doc: /* Return the standard syntax table.
801 This is the one used for new buffers. */)
802 (void)
804 return Vstandard_syntax_table;
807 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
808 doc: /* Construct a new syntax table and return it.
809 It is a copy of the TABLE, which defaults to the standard syntax table. */)
810 (Lisp_Object table)
812 Lisp_Object copy;
814 if (!NILP (table))
815 check_syntax_table (table);
816 else
817 table = Vstandard_syntax_table;
819 copy = Fcopy_sequence (table);
821 /* Only the standard syntax table should have a default element.
822 Other syntax tables should inherit from parents instead. */
823 XCHAR_TABLE (copy)->defalt = Qnil;
825 /* Copied syntax tables should all have parents.
826 If we copied one with no parent, such as the standard syntax table,
827 use the standard syntax table as the copy's parent. */
828 if (NILP (XCHAR_TABLE (copy)->parent))
829 Fset_char_table_parent (copy, Vstandard_syntax_table);
830 return copy;
833 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
834 doc: /* Select a new syntax table for the current buffer.
835 One argument, a syntax table. */)
836 (Lisp_Object table)
838 int idx;
839 check_syntax_table (table);
840 BVAR (current_buffer, syntax_table) = table;
841 /* Indicate that this buffer now has a specified syntax table. */
842 idx = PER_BUFFER_VAR_IDX (syntax_table);
843 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
844 return table;
847 /* Convert a letter which signifies a syntax code
848 into the code it signifies.
849 This is used by modify-syntax-entry, and other things. */
851 unsigned char syntax_spec_code[0400] =
852 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
853 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
854 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
855 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
856 (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377,
857 (char) Smath, 0377, 0377, (char) Squote,
858 (char) Sopen, (char) Sclose, 0377, 0377,
859 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
860 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
861 0377, 0377, 0377, 0377,
862 (char) Scomment, 0377, (char) Sendcomment, 0377,
863 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
864 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
865 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
866 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
867 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
868 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
869 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
870 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377
873 /* Indexed by syntax code, give the letter that describes it. */
875 char syntax_code_spec[16] =
877 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
878 '!', '|'
881 /* Indexed by syntax code, give the object (cons of syntax code and
882 nil) to be stored in syntax table. Since these objects can be
883 shared among syntax tables, we generate them in advance. By
884 sharing objects, the function `describe-syntax' can give a more
885 compact listing. */
886 static Lisp_Object Vsyntax_code_object;
889 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
890 doc: /* Return the syntax code of CHARACTER, described by a character.
891 For example, if CHARACTER is a word constituent, the
892 character `w' (119) is returned.
893 The characters that correspond to various syntax codes
894 are listed in the documentation of `modify-syntax-entry'. */)
895 (Lisp_Object character)
897 int char_int;
898 CHECK_CHARACTER (character);
899 char_int = XINT (character);
900 SETUP_BUFFER_SYNTAX_TABLE ();
901 return make_number (syntax_code_spec[(int) SYNTAX (char_int)]);
904 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
905 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
906 (Lisp_Object character)
908 int char_int, code;
909 CHECK_NUMBER (character);
910 char_int = XINT (character);
911 SETUP_BUFFER_SYNTAX_TABLE ();
912 code = SYNTAX (char_int);
913 if (code == Sopen || code == Sclose)
914 return SYNTAX_MATCH (char_int);
915 return Qnil;
918 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
919 doc: /* Convert a syntax specification STRING into syntax cell form.
920 STRING should be a string as it is allowed as argument of
921 `modify-syntax-entry'. Value is the equivalent cons cell
922 \(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
923 text property. */)
924 (Lisp_Object string)
926 register const unsigned char *p;
927 register enum syntaxcode code;
928 int val;
929 Lisp_Object match;
931 CHECK_STRING (string);
933 p = SDATA (string);
934 code = (enum syntaxcode) syntax_spec_code[*p++];
935 if (((int) code & 0377) == 0377)
936 error ("Invalid syntax description letter: %c", p[-1]);
938 if (code == Sinherit)
939 return Qnil;
941 if (*p)
943 int len;
944 int character = STRING_CHAR_AND_LENGTH (p, len);
945 XSETINT (match, character);
946 if (XFASTINT (match) == ' ')
947 match = Qnil;
948 p += len;
950 else
951 match = Qnil;
953 val = (int) code;
954 while (*p)
955 switch (*p++)
957 case '1':
958 val |= 1 << 16;
959 break;
961 case '2':
962 val |= 1 << 17;
963 break;
965 case '3':
966 val |= 1 << 18;
967 break;
969 case '4':
970 val |= 1 << 19;
971 break;
973 case 'p':
974 val |= 1 << 20;
975 break;
977 case 'b':
978 val |= 1 << 21;
979 break;
981 case 'n':
982 val |= 1 << 22;
983 break;
985 case 'c':
986 val |= 1 << 23;
987 break;
990 if (val < ASIZE (Vsyntax_code_object) && NILP (match))
991 return XVECTOR (Vsyntax_code_object)->contents[val];
992 else
993 /* Since we can't use a shared object, let's make a new one. */
994 return Fcons (make_number (val), match);
997 /* I really don't know why this is interactive
998 help-form should at least be made useful whilst reading the second arg. */
999 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
1000 "cSet syntax for character: \nsSet syntax for %s to: ",
1001 doc: /* Set syntax for character CHAR according to string NEWENTRY.
1002 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1003 the current buffer's syntax table.
1004 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1005 in the range MIN to MAX are changed.
1006 The first character of NEWENTRY should be one of the following:
1007 Space or - whitespace syntax. w word constituent.
1008 _ symbol constituent. . punctuation.
1009 ( open-parenthesis. ) close-parenthesis.
1010 " string quote. \\ escape.
1011 $ paired delimiter. ' expression quote or prefix operator.
1012 < comment starter. > comment ender.
1013 / character-quote. @ inherit from `standard-syntax-table'.
1014 | generic string fence. ! generic comment fence.
1016 Only single-character comment start and end sequences are represented thus.
1017 Two-character sequences are represented as described below.
1018 The second character of NEWENTRY is the matching parenthesis,
1019 used only if the first character is `(' or `)'.
1020 Any additional characters are flags.
1021 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1022 1 means CHAR is the start of a two-char comment start sequence.
1023 2 means CHAR is the second character of such a sequence.
1024 3 means CHAR is the start of a two-char comment end sequence.
1025 4 means CHAR is the second character of such a sequence.
1027 There can be several orthogonal comment sequences. This is to support
1028 language modes such as C++. By default, all comment sequences are of style
1029 a, but you can set the comment sequence style to b (on the second character
1030 of a comment-start, and the first character of a comment-end sequence) and/or
1031 c (on any of its chars) using this flag:
1032 b means CHAR is part of comment sequence b.
1033 c means CHAR is part of comment sequence c.
1034 n means CHAR is part of a nestable comment sequence.
1036 p means CHAR is a prefix character for `backward-prefix-chars';
1037 such characters are treated as whitespace when they occur
1038 between expressions.
1039 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1040 (Lisp_Object c, Lisp_Object newentry, Lisp_Object syntax_table)
1042 if (CONSP (c))
1044 CHECK_CHARACTER_CAR (c);
1045 CHECK_CHARACTER_CDR (c);
1047 else
1048 CHECK_CHARACTER (c);
1050 if (NILP (syntax_table))
1051 syntax_table = BVAR (current_buffer, syntax_table);
1052 else
1053 check_syntax_table (syntax_table);
1055 newentry = Fstring_to_syntax (newentry);
1056 if (CONSP (c))
1057 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1058 else
1059 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
1061 /* We clear the regexp cache, since character classes can now have
1062 different values from those in the compiled regexps.*/
1063 clear_regexp_cache ();
1065 return Qnil;
1068 /* Dump syntax table to buffer in human-readable format */
1070 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1071 Sinternal_describe_syntax_value, 1, 1, 0,
1072 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1073 (Lisp_Object syntax)
1075 register enum syntaxcode code;
1076 int syntax_code;
1077 char desc, start1, start2, end1, end2, prefix,
1078 comstyleb, comstylec, comnested;
1079 char str[2];
1080 Lisp_Object first, match_lisp, value = syntax;
1082 if (NILP (value))
1084 insert_string ("default");
1085 return syntax;
1088 if (CHAR_TABLE_P (value))
1090 insert_string ("deeper char-table ...");
1091 return syntax;
1094 if (!CONSP (value))
1096 insert_string ("invalid");
1097 return syntax;
1100 first = XCAR (value);
1101 match_lisp = XCDR (value);
1103 if (!INTEGERP (first) || !(NILP (match_lisp) || CHARACTERP (match_lisp)))
1105 insert_string ("invalid");
1106 return syntax;
1109 syntax_code = XINT (first) & INT_MAX;
1110 code = (enum syntaxcode) (syntax_code & 0377);
1111 start1 = SYNTAX_FLAGS_COMSTART_FIRST (syntax_code);
1112 start2 = SYNTAX_FLAGS_COMSTART_SECOND (syntax_code);;
1113 end1 = SYNTAX_FLAGS_COMEND_FIRST (syntax_code);
1114 end2 = SYNTAX_FLAGS_COMEND_SECOND (syntax_code);
1115 prefix = SYNTAX_FLAGS_PREFIX (syntax_code);
1116 comstyleb = SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code);
1117 comstylec = SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code);
1118 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax_code);
1120 if ((int) code < 0 || (int) code >= (int) Smax)
1122 insert_string ("invalid");
1123 return syntax;
1125 desc = syntax_code_spec[(int) code];
1127 str[0] = desc, str[1] = 0;
1128 insert (str, 1);
1130 if (NILP (match_lisp))
1131 insert (" ", 1);
1132 else
1133 insert_char (XINT (match_lisp));
1135 if (start1)
1136 insert ("1", 1);
1137 if (start2)
1138 insert ("2", 1);
1140 if (end1)
1141 insert ("3", 1);
1142 if (end2)
1143 insert ("4", 1);
1145 if (prefix)
1146 insert ("p", 1);
1147 if (comstyleb)
1148 insert ("b", 1);
1149 if (comstylec)
1150 insert ("c", 1);
1151 if (comnested)
1152 insert ("n", 1);
1154 insert_string ("\twhich means: ");
1156 switch (SWITCH_ENUM_CAST (code))
1158 case Swhitespace:
1159 insert_string ("whitespace"); break;
1160 case Spunct:
1161 insert_string ("punctuation"); break;
1162 case Sword:
1163 insert_string ("word"); break;
1164 case Ssymbol:
1165 insert_string ("symbol"); break;
1166 case Sopen:
1167 insert_string ("open"); break;
1168 case Sclose:
1169 insert_string ("close"); break;
1170 case Squote:
1171 insert_string ("prefix"); break;
1172 case Sstring:
1173 insert_string ("string"); break;
1174 case Smath:
1175 insert_string ("math"); break;
1176 case Sescape:
1177 insert_string ("escape"); break;
1178 case Scharquote:
1179 insert_string ("charquote"); break;
1180 case Scomment:
1181 insert_string ("comment"); break;
1182 case Sendcomment:
1183 insert_string ("endcomment"); break;
1184 case Sinherit:
1185 insert_string ("inherit"); break;
1186 case Scomment_fence:
1187 insert_string ("comment fence"); break;
1188 case Sstring_fence:
1189 insert_string ("string fence"); break;
1190 default:
1191 insert_string ("invalid");
1192 return syntax;
1195 if (!NILP (match_lisp))
1197 insert_string (", matches ");
1198 insert_char (XINT (match_lisp));
1201 if (start1)
1202 insert_string (",\n\t is the first character of a comment-start sequence");
1203 if (start2)
1204 insert_string (",\n\t is the second character of a comment-start sequence");
1206 if (end1)
1207 insert_string (",\n\t is the first character of a comment-end sequence");
1208 if (end2)
1209 insert_string (",\n\t is the second character of a comment-end sequence");
1210 if (comstyleb)
1211 insert_string (" (comment style b)");
1212 if (comstylec)
1213 insert_string (" (comment style c)");
1214 if (comnested)
1215 insert_string (" (nestable)");
1217 if (prefix)
1218 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1220 return syntax;
1223 /* Return the position across COUNT words from FROM.
1224 If that many words cannot be found before the end of the buffer, return 0.
1225 COUNT negative means scan backward and stop at word beginning. */
1227 ptrdiff_t
1228 scan_words (register ptrdiff_t from, register EMACS_INT count)
1230 register ptrdiff_t beg = BEGV;
1231 register ptrdiff_t end = ZV;
1232 register ptrdiff_t from_byte = CHAR_TO_BYTE (from);
1233 register enum syntaxcode code;
1234 int ch0, ch1;
1235 Lisp_Object func, pos;
1237 immediate_quit = 1;
1238 QUIT;
1240 SETUP_SYNTAX_TABLE (from, count);
1242 while (count > 0)
1244 while (1)
1246 if (from == end)
1248 immediate_quit = 0;
1249 return 0;
1251 UPDATE_SYNTAX_TABLE_FORWARD (from);
1252 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1253 code = SYNTAX (ch0);
1254 INC_BOTH (from, from_byte);
1255 if (words_include_escapes
1256 && (code == Sescape || code == Scharquote))
1257 break;
1258 if (code == Sword)
1259 break;
1261 /* Now CH0 is a character which begins a word and FROM is the
1262 position of the next character. */
1263 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
1264 if (! NILP (Ffboundp (func)))
1266 pos = call2 (func, make_number (from - 1), make_number (end));
1267 if (INTEGERP (pos) && from < XINT (pos) && XINT (pos) <= ZV)
1269 from = XINT (pos);
1270 from_byte = CHAR_TO_BYTE (from);
1273 else
1275 while (1)
1277 if (from == end) break;
1278 UPDATE_SYNTAX_TABLE_FORWARD (from);
1279 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1280 code = SYNTAX (ch1);
1281 if ((code != Sword
1282 && (! words_include_escapes
1283 || (code != Sescape && code != Scharquote)))
1284 || word_boundary_p (ch0, ch1))
1285 break;
1286 INC_BOTH (from, from_byte);
1287 ch0 = ch1;
1290 count--;
1292 while (count < 0)
1294 while (1)
1296 if (from == beg)
1298 immediate_quit = 0;
1299 return 0;
1301 DEC_BOTH (from, from_byte);
1302 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1303 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1304 code = SYNTAX (ch1);
1305 if (words_include_escapes
1306 && (code == Sescape || code == Scharquote))
1307 break;
1308 if (code == Sword)
1309 break;
1311 /* Now CH1 is a character which ends a word and FROM is the
1312 position of it. */
1313 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
1314 if (! NILP (Ffboundp (func)))
1316 pos = call2 (func, make_number (from), make_number (beg));
1317 if (INTEGERP (pos) && BEGV <= XINT (pos) && XINT (pos) < from)
1319 from = XINT (pos);
1320 from_byte = CHAR_TO_BYTE (from);
1323 else
1325 while (1)
1327 if (from == beg)
1328 break;
1329 DEC_BOTH (from, from_byte);
1330 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1331 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1332 code = SYNTAX (ch0);
1333 if ((code != Sword
1334 && (! words_include_escapes
1335 || (code != Sescape && code != Scharquote)))
1336 || word_boundary_p (ch0, ch1))
1338 INC_BOTH (from, from_byte);
1339 break;
1341 ch1 = ch0;
1344 count++;
1347 immediate_quit = 0;
1349 return from;
1352 DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p",
1353 doc: /* Move point forward ARG words (backward if ARG is negative).
1354 Normally returns t.
1355 If an edge of the buffer or a field boundary is reached, point is left there
1356 and the function returns nil. Field boundaries are not noticed if
1357 `inhibit-field-text-motion' is non-nil. */)
1358 (Lisp_Object arg)
1360 Lisp_Object tmp;
1361 ptrdiff_t orig_val, val;
1363 if (NILP (arg))
1364 XSETFASTINT (arg, 1);
1365 else
1366 CHECK_NUMBER (arg);
1368 val = orig_val = scan_words (PT, XINT (arg));
1369 if (! orig_val)
1370 val = XINT (arg) > 0 ? ZV : BEGV;
1372 /* Avoid jumping out of an input field. */
1373 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1374 Qt, Qnil, Qnil);
1375 val = XFASTINT (tmp);
1377 SET_PT (val);
1378 return val == orig_val ? Qt : Qnil;
1381 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1382 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1383 STRING is like the inside of a `[...]' in a regular expression
1384 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1385 (but not at the end of a range; quoting is never needed there).
1386 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1387 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1388 Char classes, e.g. `[:alpha:]', are supported.
1390 Returns the distance traveled, either zero or positive. */)
1391 (Lisp_Object string, Lisp_Object lim)
1393 return skip_chars (1, string, lim, 1);
1396 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1397 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1398 See `skip-chars-forward' for details.
1399 Returns the distance traveled, either zero or negative. */)
1400 (Lisp_Object string, Lisp_Object lim)
1402 return skip_chars (0, string, lim, 1);
1405 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1406 doc: /* Move point forward across chars in specified syntax classes.
1407 SYNTAX is a string of syntax code characters.
1408 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1409 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1410 This function returns the distance traveled, either zero or positive. */)
1411 (Lisp_Object syntax, Lisp_Object lim)
1413 return skip_syntaxes (1, syntax, lim);
1416 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1417 doc: /* Move point backward across chars in specified syntax classes.
1418 SYNTAX is a string of syntax code characters.
1419 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1420 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1421 This function returns the distance traveled, either zero or negative. */)
1422 (Lisp_Object syntax, Lisp_Object lim)
1424 return skip_syntaxes (0, syntax, lim);
1427 static Lisp_Object
1428 skip_chars (int forwardp, Lisp_Object string, Lisp_Object lim, int handle_iso_classes)
1430 register unsigned int c;
1431 unsigned char fastmap[0400];
1432 /* Store the ranges of non-ASCII characters. */
1433 int *char_ranges IF_LINT (= NULL);
1434 int n_char_ranges = 0;
1435 int negate = 0;
1436 register ptrdiff_t i, i_byte;
1437 /* Set to 1 if the current buffer is multibyte and the region
1438 contains non-ASCII chars. */
1439 int multibyte;
1440 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1441 chars. */
1442 int string_multibyte;
1443 ptrdiff_t size_byte;
1444 const unsigned char *str;
1445 int len;
1446 Lisp_Object iso_classes;
1448 CHECK_STRING (string);
1449 iso_classes = Qnil;
1451 if (NILP (lim))
1452 XSETINT (lim, forwardp ? ZV : BEGV);
1453 else
1454 CHECK_NUMBER_COERCE_MARKER (lim);
1456 /* In any case, don't allow scan outside bounds of buffer. */
1457 if (XINT (lim) > ZV)
1458 XSETFASTINT (lim, ZV);
1459 if (XINT (lim) < BEGV)
1460 XSETFASTINT (lim, BEGV);
1462 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1463 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1464 string_multibyte = SBYTES (string) > SCHARS (string);
1466 memset (fastmap, 0, sizeof fastmap);
1468 str = SDATA (string);
1469 size_byte = SBYTES (string);
1471 i_byte = 0;
1472 if (i_byte < size_byte
1473 && SREF (string, 0) == '^')
1475 negate = 1; i_byte++;
1478 /* Find the characters specified and set their elements of fastmap.
1479 Handle backslashes and ranges specially.
1481 If STRING contains non-ASCII characters, setup char_ranges for
1482 them and use fastmap only for their leading codes. */
1484 if (! string_multibyte)
1486 int string_has_eight_bit = 0;
1488 /* At first setup fastmap. */
1489 while (i_byte < size_byte)
1491 c = str[i_byte++];
1493 if (handle_iso_classes && c == '['
1494 && i_byte < size_byte
1495 && str[i_byte] == ':')
1497 const unsigned char *class_beg = str + i_byte + 1;
1498 const unsigned char *class_end = class_beg;
1499 const unsigned char *class_limit = str + size_byte - 2;
1500 /* Leave room for the null. */
1501 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1502 re_wctype_t cc;
1504 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1505 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1507 while (class_end < class_limit
1508 && *class_end >= 'a' && *class_end <= 'z')
1509 class_end++;
1511 if (class_end == class_beg
1512 || *class_end != ':' || class_end[1] != ']')
1513 goto not_a_class_name;
1515 memcpy (class_name, class_beg, class_end - class_beg);
1516 class_name[class_end - class_beg] = 0;
1518 cc = re_wctype (class_name);
1519 if (cc == 0)
1520 error ("Invalid ISO C character class");
1522 iso_classes = Fcons (make_number (cc), iso_classes);
1524 i_byte = class_end + 2 - str;
1525 continue;
1528 not_a_class_name:
1529 if (c == '\\')
1531 if (i_byte == size_byte)
1532 break;
1534 c = str[i_byte++];
1536 /* Treat `-' as range character only if another character
1537 follows. */
1538 if (i_byte + 1 < size_byte
1539 && str[i_byte] == '-')
1541 unsigned int c2;
1543 /* Skip over the dash. */
1544 i_byte++;
1546 /* Get the end of the range. */
1547 c2 = str[i_byte++];
1548 if (c2 == '\\'
1549 && i_byte < size_byte)
1550 c2 = str[i_byte++];
1552 if (c <= c2)
1554 unsigned lim2 = c2 + 1;
1555 while (c < lim2)
1556 fastmap[c++] = 1;
1557 if (! ASCII_CHAR_P (c2))
1558 string_has_eight_bit = 1;
1561 else
1563 fastmap[c] = 1;
1564 if (! ASCII_CHAR_P (c))
1565 string_has_eight_bit = 1;
1569 /* If the current range is multibyte and STRING contains
1570 eight-bit chars, arrange fastmap and setup char_ranges for
1571 the corresponding multibyte chars. */
1572 if (multibyte && string_has_eight_bit)
1574 unsigned char fastmap2[0400];
1575 int range_start_byte, range_start_char;
1577 memcpy (fastmap + 0200, fastmap2 + 0200, 0200);
1578 memset (fastmap + 0200, 0, 0200);
1579 /* We are sure that this loop stops. */
1580 for (i = 0200; ! fastmap2[i]; i++);
1581 c = BYTE8_TO_CHAR (i);
1582 fastmap[CHAR_LEADING_CODE (c)] = 1;
1583 range_start_byte = i;
1584 range_start_char = c;
1585 char_ranges = (int *) alloca (sizeof (int) * 128 * 2);
1586 for (i = 129; i < 0400; i++)
1588 c = BYTE8_TO_CHAR (i);
1589 fastmap[CHAR_LEADING_CODE (c)] = 1;
1590 if (i - range_start_byte != c - range_start_char)
1592 char_ranges[n_char_ranges++] = range_start_char;
1593 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1594 + range_start_char);
1595 range_start_byte = i;
1596 range_start_char = c;
1599 char_ranges[n_char_ranges++] = range_start_char;
1600 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1601 + range_start_char);
1604 else /* STRING is multibyte */
1606 char_ranges = (int *) alloca (sizeof (int) * SCHARS (string) * 2);
1608 while (i_byte < size_byte)
1610 unsigned char leading_code;
1612 leading_code = str[i_byte];
1613 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1614 i_byte += len;
1616 if (handle_iso_classes && c == '['
1617 && i_byte < size_byte
1618 && STRING_CHAR (str + i_byte) == ':')
1620 const unsigned char *class_beg = str + i_byte + 1;
1621 const unsigned char *class_end = class_beg;
1622 const unsigned char *class_limit = str + size_byte - 2;
1623 /* Leave room for the null. */
1624 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1625 re_wctype_t cc;
1627 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1628 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1630 while (class_end < class_limit
1631 && *class_end >= 'a' && *class_end <= 'z')
1632 class_end++;
1634 if (class_end == class_beg
1635 || *class_end != ':' || class_end[1] != ']')
1636 goto not_a_class_name_multibyte;
1638 memcpy (class_name, class_beg, class_end - class_beg);
1639 class_name[class_end - class_beg] = 0;
1641 cc = re_wctype (class_name);
1642 if (cc == 0)
1643 error ("Invalid ISO C character class");
1645 iso_classes = Fcons (make_number (cc), iso_classes);
1647 i_byte = class_end + 2 - str;
1648 continue;
1651 not_a_class_name_multibyte:
1652 if (c == '\\')
1654 if (i_byte == size_byte)
1655 break;
1657 leading_code = str[i_byte];
1658 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1659 i_byte += len;
1661 /* Treat `-' as range character only if another character
1662 follows. */
1663 if (i_byte + 1 < size_byte
1664 && str[i_byte] == '-')
1666 unsigned int c2;
1667 unsigned char leading_code2;
1669 /* Skip over the dash. */
1670 i_byte++;
1672 /* Get the end of the range. */
1673 leading_code2 = str[i_byte];
1674 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1675 i_byte += len;
1677 if (c2 == '\\'
1678 && i_byte < size_byte)
1680 leading_code2 = str[i_byte];
1681 c2 =STRING_CHAR_AND_LENGTH (str + i_byte, len);
1682 i_byte += len;
1685 if (c > c2)
1686 continue;
1687 if (ASCII_CHAR_P (c))
1689 while (c <= c2 && c < 0x80)
1690 fastmap[c++] = 1;
1691 leading_code = CHAR_LEADING_CODE (c);
1693 if (! ASCII_CHAR_P (c))
1695 unsigned lim2 = leading_code2 + 1;
1696 while (leading_code < lim2)
1697 fastmap[leading_code++] = 1;
1698 if (c <= c2)
1700 char_ranges[n_char_ranges++] = c;
1701 char_ranges[n_char_ranges++] = c2;
1705 else
1707 if (ASCII_CHAR_P (c))
1708 fastmap[c] = 1;
1709 else
1711 fastmap[leading_code] = 1;
1712 char_ranges[n_char_ranges++] = c;
1713 char_ranges[n_char_ranges++] = c;
1718 /* If the current range is unibyte and STRING contains non-ASCII
1719 chars, arrange fastmap for the corresponding unibyte
1720 chars. */
1722 if (! multibyte && n_char_ranges > 0)
1724 memset (fastmap + 0200, 0, 0200);
1725 for (i = 0; i < n_char_ranges; i += 2)
1727 int c1 = char_ranges[i];
1728 unsigned lim2 = char_ranges[i + 1] + 1;
1730 for (; c1 < lim2; c1++)
1732 int b = CHAR_TO_BYTE_SAFE (c1);
1733 if (b >= 0)
1734 fastmap[b] = 1;
1740 /* If ^ was the first character, complement the fastmap. */
1741 if (negate)
1743 if (! multibyte)
1744 for (i = 0; i < sizeof fastmap; i++)
1745 fastmap[i] ^= 1;
1746 else
1748 for (i = 0; i < 0200; i++)
1749 fastmap[i] ^= 1;
1750 /* All non-ASCII chars possibly match. */
1751 for (; i < sizeof fastmap; i++)
1752 fastmap[i] = 1;
1757 ptrdiff_t start_point = PT;
1758 ptrdiff_t pos = PT;
1759 ptrdiff_t pos_byte = PT_BYTE;
1760 unsigned char *p = PT_ADDR, *endp, *stop;
1762 if (forwardp)
1764 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1765 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1767 else
1769 endp = CHAR_POS_ADDR (XINT (lim));
1770 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1773 immediate_quit = 1;
1774 /* This code may look up syntax tables using macros that rely on the
1775 gl_state object. To make sure this object is not out of date,
1776 let's initialize it manually.
1777 We ignore syntax-table text-properties for now, since that's
1778 what we've done in the past. */
1779 SETUP_BUFFER_SYNTAX_TABLE ();
1780 if (forwardp)
1782 if (multibyte)
1783 while (1)
1785 int nbytes;
1787 if (p >= stop)
1789 if (p >= endp)
1790 break;
1791 p = GAP_END_ADDR;
1792 stop = endp;
1794 c = STRING_CHAR_AND_LENGTH (p, nbytes);
1795 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1797 if (negate)
1798 break;
1799 else
1800 goto fwd_ok;
1803 if (! fastmap[*p])
1804 break;
1805 if (! ASCII_CHAR_P (c))
1807 /* As we are looking at a multibyte character, we
1808 must look up the character in the table
1809 CHAR_RANGES. If there's no data in the table,
1810 that character is not what we want to skip. */
1812 /* The following code do the right thing even if
1813 n_char_ranges is zero (i.e. no data in
1814 CHAR_RANGES). */
1815 for (i = 0; i < n_char_ranges; i += 2)
1816 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1817 break;
1818 if (!(negate ^ (i < n_char_ranges)))
1819 break;
1821 fwd_ok:
1822 p += nbytes, pos++, pos_byte += nbytes;
1824 else
1825 while (1)
1827 if (p >= stop)
1829 if (p >= endp)
1830 break;
1831 p = GAP_END_ADDR;
1832 stop = endp;
1835 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1837 if (negate)
1838 break;
1839 else
1840 goto fwd_unibyte_ok;
1843 if (!fastmap[*p])
1844 break;
1845 fwd_unibyte_ok:
1846 p++, pos++, pos_byte++;
1849 else
1851 if (multibyte)
1852 while (1)
1854 unsigned char *prev_p;
1856 if (p <= stop)
1858 if (p <= endp)
1859 break;
1860 p = GPT_ADDR;
1861 stop = endp;
1863 prev_p = p;
1864 while (--p >= stop && ! CHAR_HEAD_P (*p));
1865 c = STRING_CHAR (p);
1867 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1869 if (negate)
1870 break;
1871 else
1872 goto back_ok;
1875 if (! fastmap[*p])
1876 break;
1877 if (! ASCII_CHAR_P (c))
1879 /* See the comment in the previous similar code. */
1880 for (i = 0; i < n_char_ranges; i += 2)
1881 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1882 break;
1883 if (!(negate ^ (i < n_char_ranges)))
1884 break;
1886 back_ok:
1887 pos--, pos_byte -= prev_p - p;
1889 else
1890 while (1)
1892 if (p <= stop)
1894 if (p <= endp)
1895 break;
1896 p = GPT_ADDR;
1897 stop = endp;
1900 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
1902 if (negate)
1903 break;
1904 else
1905 goto back_unibyte_ok;
1908 if (!fastmap[p[-1]])
1909 break;
1910 back_unibyte_ok:
1911 p--, pos--, pos_byte--;
1915 SET_PT_BOTH (pos, pos_byte);
1916 immediate_quit = 0;
1918 return make_number (PT - start_point);
1923 static Lisp_Object
1924 skip_syntaxes (int forwardp, Lisp_Object string, Lisp_Object lim)
1926 register unsigned int c;
1927 unsigned char fastmap[0400];
1928 int negate = 0;
1929 register ptrdiff_t i, i_byte;
1930 int multibyte;
1931 ptrdiff_t size_byte;
1932 unsigned char *str;
1934 CHECK_STRING (string);
1936 if (NILP (lim))
1937 XSETINT (lim, forwardp ? ZV : BEGV);
1938 else
1939 CHECK_NUMBER_COERCE_MARKER (lim);
1941 /* In any case, don't allow scan outside bounds of buffer. */
1942 if (XINT (lim) > ZV)
1943 XSETFASTINT (lim, ZV);
1944 if (XINT (lim) < BEGV)
1945 XSETFASTINT (lim, BEGV);
1947 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
1948 return make_number (0);
1950 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1951 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1953 memset (fastmap, 0, sizeof fastmap);
1955 if (SBYTES (string) > SCHARS (string))
1956 /* As this is very rare case (syntax spec is ASCII only), don't
1957 consider efficiency. */
1958 string = string_make_unibyte (string);
1960 str = SDATA (string);
1961 size_byte = SBYTES (string);
1963 i_byte = 0;
1964 if (i_byte < size_byte
1965 && SREF (string, 0) == '^')
1967 negate = 1; i_byte++;
1970 /* Find the syntaxes specified and set their elements of fastmap. */
1972 while (i_byte < size_byte)
1974 c = str[i_byte++];
1975 fastmap[syntax_spec_code[c]] = 1;
1978 /* If ^ was the first character, complement the fastmap. */
1979 if (negate)
1980 for (i = 0; i < sizeof fastmap; i++)
1981 fastmap[i] ^= 1;
1984 ptrdiff_t start_point = PT;
1985 ptrdiff_t pos = PT;
1986 ptrdiff_t pos_byte = PT_BYTE;
1987 unsigned char *p = PT_ADDR, *endp, *stop;
1989 if (forwardp)
1991 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1992 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1994 else
1996 endp = CHAR_POS_ADDR (XINT (lim));
1997 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
2000 immediate_quit = 1;
2001 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
2002 if (forwardp)
2004 if (multibyte)
2006 while (1)
2008 int nbytes;
2010 if (p >= stop)
2012 if (p >= endp)
2013 break;
2014 p = GAP_END_ADDR;
2015 stop = endp;
2017 c = STRING_CHAR_AND_LENGTH (p, nbytes);
2018 if (! fastmap[(int) SYNTAX (c)])
2019 break;
2020 p += nbytes, pos++, pos_byte += nbytes;
2021 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2024 else
2026 while (1)
2028 if (p >= stop)
2030 if (p >= endp)
2031 break;
2032 p = GAP_END_ADDR;
2033 stop = endp;
2035 if (! fastmap[(int) SYNTAX (*p)])
2036 break;
2037 p++, pos++, pos_byte++;
2038 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2042 else
2044 if (multibyte)
2046 while (1)
2048 unsigned char *prev_p;
2050 if (p <= stop)
2052 if (p <= endp)
2053 break;
2054 p = GPT_ADDR;
2055 stop = endp;
2057 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2058 prev_p = p;
2059 while (--p >= stop && ! CHAR_HEAD_P (*p));
2060 c = STRING_CHAR (p);
2061 if (! fastmap[(int) SYNTAX (c)])
2062 break;
2063 pos--, pos_byte -= prev_p - p;
2066 else
2068 while (1)
2070 if (p <= stop)
2072 if (p <= endp)
2073 break;
2074 p = GPT_ADDR;
2075 stop = endp;
2077 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2078 if (! fastmap[(int) SYNTAX (p[-1])])
2079 break;
2080 p--, pos--, pos_byte--;
2085 SET_PT_BOTH (pos, pos_byte);
2086 immediate_quit = 0;
2088 return make_number (PT - start_point);
2092 /* Return 1 if character C belongs to one of the ISO classes
2093 in the list ISO_CLASSES. Each class is represented by an
2094 integer which is its type according to re_wctype. */
2096 static int
2097 in_classes (int c, Lisp_Object iso_classes)
2099 int fits_class = 0;
2101 while (CONSP (iso_classes))
2103 Lisp_Object elt;
2104 elt = XCAR (iso_classes);
2105 iso_classes = XCDR (iso_classes);
2107 if (re_iswctype (c, XFASTINT (elt)))
2108 fits_class = 1;
2111 return fits_class;
2114 /* Jump over a comment, assuming we are at the beginning of one.
2115 FROM is the current position.
2116 FROM_BYTE is the bytepos corresponding to FROM.
2117 Do not move past STOP (a charpos).
2118 The comment over which we have to jump is of style STYLE
2119 (either SYNTAX_FLAGS_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2120 NESTING should be positive to indicate the nesting at the beginning
2121 for nested comments and should be zero or negative else.
2122 ST_COMMENT_STYLE cannot be nested.
2123 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2124 (or 0 If the search cannot start in the middle of a two-character).
2126 If successful, return 1 and store the charpos of the comment's end
2127 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2128 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2129 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2130 (as defined for state.incomment) in *INCOMMENT_PTR.
2132 The comment end is the last character of the comment rather than the
2133 character just after the comment.
2135 Global syntax data is assumed to initially be valid for FROM and
2136 remains valid for forward search starting at the returned position. */
2138 static int
2139 forw_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop,
2140 EMACS_INT nesting, int style, int prev_syntax,
2141 ptrdiff_t *charpos_ptr, ptrdiff_t *bytepos_ptr,
2142 EMACS_INT *incomment_ptr)
2144 register int c, c1;
2145 register enum syntaxcode code;
2146 register int syntax, other_syntax;
2148 if (nesting <= 0) nesting = -1;
2150 /* Enter the loop in the middle so that we find
2151 a 2-char comment ender if we start in the middle of it. */
2152 syntax = prev_syntax;
2153 if (syntax != 0) goto forw_incomment;
2155 while (1)
2157 if (from == stop)
2159 *incomment_ptr = nesting;
2160 *charpos_ptr = from;
2161 *bytepos_ptr = from_byte;
2162 return 0;
2164 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2165 syntax = SYNTAX_WITH_FLAGS (c);
2166 code = syntax & 0xff;
2167 if (code == Sendcomment
2168 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style
2169 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2170 (nesting > 0 && --nesting == 0) : nesting < 0))
2171 /* we have encountered a comment end of the same style
2172 as the comment sequence which began this comment
2173 section */
2174 break;
2175 if (code == Scomment_fence
2176 && style == ST_COMMENT_STYLE)
2177 /* we have encountered a comment end of the same style
2178 as the comment sequence which began this comment
2179 section. */
2180 break;
2181 if (nesting > 0
2182 && code == Scomment
2183 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2184 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style)
2185 /* we have encountered a nested comment of the same style
2186 as the comment sequence which began this comment section */
2187 nesting++;
2188 INC_BOTH (from, from_byte);
2189 UPDATE_SYNTAX_TABLE_FORWARD (from);
2191 forw_incomment:
2192 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2193 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2194 other_syntax = SYNTAX_WITH_FLAGS (c1),
2195 SYNTAX_FLAGS_COMEND_SECOND (other_syntax))
2196 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, other_syntax) == style
2197 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2198 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax))
2199 ? nesting > 0 : nesting < 0))
2201 if (--nesting <= 0)
2202 /* we have encountered a comment end of the same style
2203 as the comment sequence which began this comment
2204 section */
2205 break;
2206 else
2208 INC_BOTH (from, from_byte);
2209 UPDATE_SYNTAX_TABLE_FORWARD (from);
2212 if (nesting > 0
2213 && from < stop
2214 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2215 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2216 other_syntax = SYNTAX_WITH_FLAGS (c1),
2217 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax) == style
2218 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2219 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2220 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax)))
2221 /* we have encountered a nested comment of the same style
2222 as the comment sequence which began this comment
2223 section */
2225 INC_BOTH (from, from_byte);
2226 UPDATE_SYNTAX_TABLE_FORWARD (from);
2227 nesting++;
2230 *charpos_ptr = from;
2231 *bytepos_ptr = from_byte;
2232 return 1;
2235 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2236 doc: /*
2237 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2238 Stop scanning if we find something other than a comment or whitespace.
2239 Set point to where scanning stops.
2240 If COUNT comments are found as expected, with nothing except whitespace
2241 between them, return t; otherwise return nil. */)
2242 (Lisp_Object count)
2244 register ptrdiff_t from;
2245 ptrdiff_t from_byte;
2246 register ptrdiff_t stop;
2247 register int c, c1;
2248 register enum syntaxcode code;
2249 int comstyle = 0; /* style of comment encountered */
2250 int comnested = 0; /* whether the comment is nestable or not */
2251 int found;
2252 EMACS_INT count1;
2253 ptrdiff_t out_charpos, out_bytepos;
2254 EMACS_INT dummy;
2256 CHECK_NUMBER (count);
2257 count1 = XINT (count);
2258 stop = count1 > 0 ? ZV : BEGV;
2260 immediate_quit = 1;
2261 QUIT;
2263 from = PT;
2264 from_byte = PT_BYTE;
2266 SETUP_SYNTAX_TABLE (from, count1);
2267 while (count1 > 0)
2271 int comstart_first, syntax, other_syntax;
2273 if (from == stop)
2275 SET_PT_BOTH (from, from_byte);
2276 immediate_quit = 0;
2277 return Qnil;
2279 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2280 syntax = SYNTAX_WITH_FLAGS (c);
2281 code = SYNTAX (c);
2282 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2283 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2284 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2285 INC_BOTH (from, from_byte);
2286 UPDATE_SYNTAX_TABLE_FORWARD (from);
2287 if (from < stop && comstart_first
2288 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2289 other_syntax = SYNTAX_WITH_FLAGS (c1),
2290 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)))
2292 /* We have encountered a comment start sequence and we
2293 are ignoring all text inside comments. We must record
2294 the comment style this sequence begins so that later,
2295 only a comment end of the same style actually ends
2296 the comment section. */
2297 code = Scomment;
2298 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2299 comnested
2300 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2301 INC_BOTH (from, from_byte);
2302 UPDATE_SYNTAX_TABLE_FORWARD (from);
2305 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2307 if (code == Scomment_fence)
2308 comstyle = ST_COMMENT_STYLE;
2309 else if (code != Scomment)
2311 immediate_quit = 0;
2312 DEC_BOTH (from, from_byte);
2313 SET_PT_BOTH (from, from_byte);
2314 return Qnil;
2316 /* We're at the start of a comment. */
2317 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2318 &out_charpos, &out_bytepos, &dummy);
2319 from = out_charpos; from_byte = out_bytepos;
2320 if (!found)
2322 immediate_quit = 0;
2323 SET_PT_BOTH (from, from_byte);
2324 return Qnil;
2326 INC_BOTH (from, from_byte);
2327 UPDATE_SYNTAX_TABLE_FORWARD (from);
2328 /* We have skipped one comment. */
2329 count1--;
2332 while (count1 < 0)
2334 while (1)
2336 int quoted, syntax;
2338 if (from <= stop)
2340 SET_PT_BOTH (BEGV, BEGV_BYTE);
2341 immediate_quit = 0;
2342 return Qnil;
2345 DEC_BOTH (from, from_byte);
2346 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2347 quoted = char_quoted (from, from_byte);
2348 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2349 syntax = SYNTAX_WITH_FLAGS (c);
2350 code = SYNTAX (c);
2351 comstyle = 0;
2352 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2353 if (code == Sendcomment)
2354 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2355 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2356 && prev_char_comend_first (from, from_byte)
2357 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2359 int other_syntax;
2360 /* We must record the comment style encountered so that
2361 later, we can match only the proper comment begin
2362 sequence of the same style. */
2363 DEC_BOTH (from, from_byte);
2364 code = Sendcomment;
2365 /* Calling char_quoted, above, set up global syntax position
2366 at the new value of FROM. */
2367 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2368 other_syntax = SYNTAX_WITH_FLAGS (c1);
2369 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2370 comnested
2371 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2374 if (code == Scomment_fence)
2376 /* Skip until first preceding unquoted comment_fence. */
2377 int fence_found = 0;
2378 ptrdiff_t ini = from, ini_byte = from_byte;
2380 while (1)
2382 DEC_BOTH (from, from_byte);
2383 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2384 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2385 if (SYNTAX (c) == Scomment_fence
2386 && !char_quoted (from, from_byte))
2388 fence_found = 1;
2389 break;
2391 else if (from == stop)
2392 break;
2394 if (fence_found == 0)
2396 from = ini; /* Set point to ini + 1. */
2397 from_byte = ini_byte;
2398 goto leave;
2400 else
2401 /* We have skipped one comment. */
2402 break;
2404 else if (code == Sendcomment)
2406 found = back_comment (from, from_byte, stop, comnested, comstyle,
2407 &out_charpos, &out_bytepos);
2408 if (found == -1)
2410 if (c == '\n')
2411 /* This end-of-line is not an end-of-comment.
2412 Treat it like a whitespace.
2413 CC-mode (and maybe others) relies on this behavior. */
2415 else
2417 /* Failure: we should go back to the end of this
2418 not-quite-endcomment. */
2419 if (SYNTAX (c) != code)
2420 /* It was a two-char Sendcomment. */
2421 INC_BOTH (from, from_byte);
2422 goto leave;
2425 else
2427 /* We have skipped one comment. */
2428 from = out_charpos, from_byte = out_bytepos;
2429 break;
2432 else if (code != Swhitespace || quoted)
2434 leave:
2435 immediate_quit = 0;
2436 INC_BOTH (from, from_byte);
2437 SET_PT_BOTH (from, from_byte);
2438 return Qnil;
2442 count1++;
2445 SET_PT_BOTH (from, from_byte);
2446 immediate_quit = 0;
2447 return Qt;
2450 /* Return syntax code of character C if C is an ASCII character
2451 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2453 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2454 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2455 ? SYNTAX (c) : Ssymbol)
2457 static Lisp_Object
2458 scan_lists (register EMACS_INT from, EMACS_INT count, EMACS_INT depth, int sexpflag)
2460 Lisp_Object val;
2461 register ptrdiff_t stop = count > 0 ? ZV : BEGV;
2462 register int c, c1;
2463 int stringterm;
2464 int quoted;
2465 int mathexit = 0;
2466 register enum syntaxcode code, temp_code;
2467 EMACS_INT min_depth = depth; /* Err out if depth gets less than this. */
2468 int comstyle = 0; /* style of comment encountered */
2469 int comnested = 0; /* whether the comment is nestable or not */
2470 ptrdiff_t temp_pos;
2471 EMACS_INT last_good = from;
2472 int found;
2473 ptrdiff_t from_byte;
2474 ptrdiff_t out_bytepos, out_charpos;
2475 int temp;
2476 EMACS_INT dummy;
2477 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2479 if (depth > 0) min_depth = 0;
2481 if (from > ZV) from = ZV;
2482 if (from < BEGV) from = BEGV;
2484 from_byte = CHAR_TO_BYTE (from);
2486 immediate_quit = 1;
2487 QUIT;
2489 SETUP_SYNTAX_TABLE (from, count);
2490 while (count > 0)
2492 while (from < stop)
2494 int comstart_first, prefix, syntax, other_syntax;
2495 UPDATE_SYNTAX_TABLE_FORWARD (from);
2496 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2497 syntax = SYNTAX_WITH_FLAGS (c);
2498 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2499 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2500 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2501 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2502 prefix = SYNTAX_FLAGS_PREFIX (syntax);
2503 if (depth == min_depth)
2504 last_good = from;
2505 INC_BOTH (from, from_byte);
2506 UPDATE_SYNTAX_TABLE_FORWARD (from);
2507 if (from < stop && comstart_first
2508 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2509 other_syntax = SYNTAX_WITH_FLAGS (c),
2510 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2511 && parse_sexp_ignore_comments)
2513 /* we have encountered a comment start sequence and we
2514 are ignoring all text inside comments. We must record
2515 the comment style this sequence begins so that later,
2516 only a comment end of the same style actually ends
2517 the comment section */
2518 code = Scomment;
2519 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2520 comnested
2521 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2522 INC_BOTH (from, from_byte);
2523 UPDATE_SYNTAX_TABLE_FORWARD (from);
2526 if (prefix)
2527 continue;
2529 switch (SWITCH_ENUM_CAST (code))
2531 case Sescape:
2532 case Scharquote:
2533 if (from == stop)
2534 goto lose;
2535 INC_BOTH (from, from_byte);
2536 /* treat following character as a word constituent */
2537 case Sword:
2538 case Ssymbol:
2539 if (depth || !sexpflag) break;
2540 /* This word counts as a sexp; return at end of it. */
2541 while (from < stop)
2543 UPDATE_SYNTAX_TABLE_FORWARD (from);
2545 /* Some compilers can't handle this inside the switch. */
2546 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2547 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2548 switch (temp)
2550 case Scharquote:
2551 case Sescape:
2552 INC_BOTH (from, from_byte);
2553 if (from == stop)
2554 goto lose;
2555 break;
2556 case Sword:
2557 case Ssymbol:
2558 case Squote:
2559 break;
2560 default:
2561 goto done;
2563 INC_BOTH (from, from_byte);
2565 goto done;
2567 case Scomment_fence:
2568 comstyle = ST_COMMENT_STYLE;
2569 /* FALLTHROUGH */
2570 case Scomment:
2571 if (!parse_sexp_ignore_comments) break;
2572 UPDATE_SYNTAX_TABLE_FORWARD (from);
2573 found = forw_comment (from, from_byte, stop,
2574 comnested, comstyle, 0,
2575 &out_charpos, &out_bytepos, &dummy);
2576 from = out_charpos, from_byte = out_bytepos;
2577 if (!found)
2579 if (depth == 0)
2580 goto done;
2581 goto lose;
2583 INC_BOTH (from, from_byte);
2584 UPDATE_SYNTAX_TABLE_FORWARD (from);
2585 break;
2587 case Smath:
2588 if (!sexpflag)
2589 break;
2590 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2592 INC_BOTH (from, from_byte);
2594 if (mathexit)
2596 mathexit = 0;
2597 goto close1;
2599 mathexit = 1;
2601 case Sopen:
2602 if (!++depth) goto done;
2603 break;
2605 case Sclose:
2606 close1:
2607 if (!--depth) goto done;
2608 if (depth < min_depth)
2609 xsignal3 (Qscan_error,
2610 build_string ("Containing expression ends prematurely"),
2611 make_number (last_good), make_number (from));
2612 break;
2614 case Sstring:
2615 case Sstring_fence:
2616 temp_pos = dec_bytepos (from_byte);
2617 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2618 while (1)
2620 if (from >= stop)
2621 goto lose;
2622 UPDATE_SYNTAX_TABLE_FORWARD (from);
2623 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2624 if (code == Sstring
2625 ? (c == stringterm
2626 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2627 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
2628 break;
2630 /* Some compilers can't handle this inside the switch. */
2631 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2632 switch (temp)
2634 case Scharquote:
2635 case Sescape:
2636 INC_BOTH (from, from_byte);
2638 INC_BOTH (from, from_byte);
2640 INC_BOTH (from, from_byte);
2641 if (!depth && sexpflag) goto done;
2642 break;
2643 default:
2644 /* Ignore whitespace, punctuation, quote, endcomment. */
2645 break;
2649 /* Reached end of buffer. Error if within object, return nil if between */
2650 if (depth)
2651 goto lose;
2653 immediate_quit = 0;
2654 return Qnil;
2656 /* End of object reached */
2657 done:
2658 count--;
2661 while (count < 0)
2663 while (from > stop)
2665 int syntax;
2666 DEC_BOTH (from, from_byte);
2667 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2668 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2669 syntax= SYNTAX_WITH_FLAGS (c);
2670 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2671 if (depth == min_depth)
2672 last_good = from;
2673 comstyle = 0;
2674 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2675 if (code == Sendcomment)
2676 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2677 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2678 && prev_char_comend_first (from, from_byte)
2679 && parse_sexp_ignore_comments)
2681 /* We must record the comment style encountered so that
2682 later, we can match only the proper comment begin
2683 sequence of the same style. */
2684 int c2, other_syntax;
2685 DEC_BOTH (from, from_byte);
2686 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2687 code = Sendcomment;
2688 c2 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2689 other_syntax = SYNTAX_WITH_FLAGS (c2);
2690 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2691 comnested
2692 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2695 /* Quoting turns anything except a comment-ender
2696 into a word character. Note that this cannot be true
2697 if we decremented FROM in the if-statement above. */
2698 if (code != Sendcomment && char_quoted (from, from_byte))
2700 DEC_BOTH (from, from_byte);
2701 code = Sword;
2703 else if (SYNTAX_FLAGS_PREFIX (syntax))
2704 continue;
2706 switch (SWITCH_ENUM_CAST (code))
2708 case Sword:
2709 case Ssymbol:
2710 case Sescape:
2711 case Scharquote:
2712 if (depth || !sexpflag) break;
2713 /* This word counts as a sexp; count object finished
2714 after passing it. */
2715 while (from > stop)
2717 temp_pos = from_byte;
2718 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2719 DEC_POS (temp_pos);
2720 else
2721 temp_pos--;
2722 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2723 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2724 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2725 /* Don't allow comment-end to be quoted. */
2726 if (temp_code == Sendcomment)
2727 goto done2;
2728 quoted = char_quoted (from - 1, temp_pos);
2729 if (quoted)
2731 DEC_BOTH (from, from_byte);
2732 temp_pos = dec_bytepos (temp_pos);
2733 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2735 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2736 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2737 if (! (quoted || temp_code == Sword
2738 || temp_code == Ssymbol
2739 || temp_code == Squote))
2740 goto done2;
2741 DEC_BOTH (from, from_byte);
2743 goto done2;
2745 case Smath:
2746 if (!sexpflag)
2747 break;
2748 temp_pos = dec_bytepos (from_byte);
2749 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2750 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2751 DEC_BOTH (from, from_byte);
2752 if (mathexit)
2754 mathexit = 0;
2755 goto open2;
2757 mathexit = 1;
2759 case Sclose:
2760 if (!++depth) goto done2;
2761 break;
2763 case Sopen:
2764 open2:
2765 if (!--depth) goto done2;
2766 if (depth < min_depth)
2767 xsignal3 (Qscan_error,
2768 build_string ("Containing expression ends prematurely"),
2769 make_number (last_good), make_number (from));
2770 break;
2772 case Sendcomment:
2773 if (!parse_sexp_ignore_comments)
2774 break;
2775 found = back_comment (from, from_byte, stop, comnested, comstyle,
2776 &out_charpos, &out_bytepos);
2777 /* FIXME: if found == -1, then it really wasn't a comment-end.
2778 For single-char Sendcomment, we can't do much about it apart
2779 from skipping the char.
2780 For 2-char endcomments, we could try again, taking both
2781 chars as separate entities, but it's a lot of trouble
2782 for very little gain, so we don't bother either. -sm */
2783 if (found != -1)
2784 from = out_charpos, from_byte = out_bytepos;
2785 break;
2787 case Scomment_fence:
2788 case Sstring_fence:
2789 while (1)
2791 if (from == stop)
2792 goto lose;
2793 DEC_BOTH (from, from_byte);
2794 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2795 if (!char_quoted (from, from_byte)
2796 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2797 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
2798 break;
2800 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2801 break;
2803 case Sstring:
2804 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2805 while (1)
2807 if (from == stop)
2808 goto lose;
2809 DEC_BOTH (from, from_byte);
2810 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2811 if (!char_quoted (from, from_byte)
2812 && (stringterm
2813 == (c = FETCH_CHAR_AS_MULTIBYTE (from_byte)))
2814 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2815 break;
2817 if (!depth && sexpflag) goto done2;
2818 break;
2819 default:
2820 /* Ignore whitespace, punctuation, quote, endcomment. */
2821 break;
2825 /* Reached start of buffer. Error if within object, return nil if between */
2826 if (depth)
2827 goto lose;
2829 immediate_quit = 0;
2830 return Qnil;
2832 done2:
2833 count++;
2837 immediate_quit = 0;
2838 XSETFASTINT (val, from);
2839 return val;
2841 lose:
2842 xsignal3 (Qscan_error,
2843 build_string ("Unbalanced parentheses"),
2844 make_number (last_good), make_number (from));
2847 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2848 doc: /* Scan from character number FROM by COUNT lists.
2849 Returns the character number of the position thus found.
2851 If DEPTH is nonzero, paren depth begins counting from that value,
2852 only places where the depth in parentheses becomes zero
2853 are candidates for stopping; COUNT such places are counted.
2854 Thus, a positive value for DEPTH means go out levels.
2856 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2858 If the beginning or end of (the accessible part of) the buffer is reached
2859 and the depth is wrong, an error is signaled.
2860 If the depth is right but the count is not used up, nil is returned. */)
2861 (Lisp_Object from, Lisp_Object count, Lisp_Object depth)
2863 CHECK_NUMBER (from);
2864 CHECK_NUMBER (count);
2865 CHECK_NUMBER (depth);
2867 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2870 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2871 doc: /* Scan from character number FROM by COUNT balanced expressions.
2872 If COUNT is negative, scan backwards.
2873 Returns the character number of the position thus found.
2875 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2877 If the beginning or end of (the accessible part of) the buffer is reached
2878 in the middle of a parenthetical grouping, an error is signaled.
2879 If the beginning or end is reached between groupings
2880 but before count is used up, nil is returned. */)
2881 (Lisp_Object from, Lisp_Object count)
2883 CHECK_NUMBER (from);
2884 CHECK_NUMBER (count);
2886 return scan_lists (XINT (from), XINT (count), 0, 1);
2889 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2890 0, 0, 0,
2891 doc: /* Move point backward over any number of chars with prefix syntax.
2892 This includes chars with "quote" or "prefix" syntax (' or p). */)
2893 (void)
2895 ptrdiff_t beg = BEGV;
2896 ptrdiff_t opoint = PT;
2897 ptrdiff_t opoint_byte = PT_BYTE;
2898 ptrdiff_t pos = PT;
2899 ptrdiff_t pos_byte = PT_BYTE;
2900 int c;
2902 if (pos <= beg)
2904 SET_PT_BOTH (opoint, opoint_byte);
2906 return Qnil;
2909 SETUP_SYNTAX_TABLE (pos, -1);
2911 DEC_BOTH (pos, pos_byte);
2913 while (!char_quoted (pos, pos_byte)
2914 /* Previous statement updates syntax table. */
2915 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
2916 || SYNTAX_PREFIX (c)))
2918 opoint = pos;
2919 opoint_byte = pos_byte;
2921 if (pos + 1 > beg)
2922 DEC_BOTH (pos, pos_byte);
2925 SET_PT_BOTH (opoint, opoint_byte);
2927 return Qnil;
2930 /* Parse forward from FROM / FROM_BYTE to END,
2931 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2932 and return a description of the state of the parse at END.
2933 If STOPBEFORE is nonzero, stop at the start of an atom.
2934 If COMMENTSTOP is 1, stop at the start of a comment.
2935 If COMMENTSTOP is -1, stop at the start or end of a comment,
2936 after the beginning of a string, or after the end of a string. */
2938 static void
2939 scan_sexps_forward (struct lisp_parse_state *stateptr,
2940 ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t end,
2941 EMACS_INT targetdepth, int stopbefore,
2942 Lisp_Object oldstate, int commentstop)
2944 struct lisp_parse_state state;
2946 register enum syntaxcode code;
2947 int c1;
2948 int comnested;
2949 struct level { ptrdiff_t last, prev; };
2950 struct level levelstart[100];
2951 register struct level *curlevel = levelstart;
2952 struct level *endlevel = levelstart + 100;
2953 register EMACS_INT depth; /* Paren depth of current scanning location.
2954 level - levelstart equals this except
2955 when the depth becomes negative. */
2956 EMACS_INT mindepth; /* Lowest DEPTH value seen. */
2957 int start_quoted = 0; /* Nonzero means starting after a char quote */
2958 Lisp_Object tem;
2959 ptrdiff_t prev_from; /* Keep one character before FROM. */
2960 ptrdiff_t prev_from_byte;
2961 int prev_from_syntax;
2962 int boundary_stop = commentstop == -1;
2963 int nofence;
2964 int found;
2965 ptrdiff_t out_bytepos, out_charpos;
2966 int temp;
2968 prev_from = from;
2969 prev_from_byte = from_byte;
2970 if (from != BEGV)
2971 DEC_BOTH (prev_from, prev_from_byte);
2973 /* Use this macro instead of `from++'. */
2974 #define INC_FROM \
2975 do { prev_from = from; \
2976 prev_from_byte = from_byte; \
2977 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2978 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2979 INC_BOTH (from, from_byte); \
2980 if (from < end) \
2981 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2982 } while (0)
2984 immediate_quit = 1;
2985 QUIT;
2987 if (NILP (oldstate))
2989 depth = 0;
2990 state.instring = -1;
2991 state.incomment = 0;
2992 state.comstyle = 0; /* comment style a by default. */
2993 state.comstr_start = -1; /* no comment/string seen. */
2995 else
2997 tem = Fcar (oldstate);
2998 if (!NILP (tem))
2999 depth = XINT (tem);
3000 else
3001 depth = 0;
3003 oldstate = Fcdr (oldstate);
3004 oldstate = Fcdr (oldstate);
3005 oldstate = Fcdr (oldstate);
3006 tem = Fcar (oldstate);
3007 /* Check whether we are inside string_fence-style string: */
3008 state.instring = (!NILP (tem)
3009 ? (CHARACTERP (tem) ? XFASTINT (tem) : ST_STRING_STYLE)
3010 : -1);
3012 oldstate = Fcdr (oldstate);
3013 tem = Fcar (oldstate);
3014 state.incomment = (!NILP (tem)
3015 ? (INTEGERP (tem) ? XINT (tem) : -1)
3016 : 0);
3018 oldstate = Fcdr (oldstate);
3019 tem = Fcar (oldstate);
3020 start_quoted = !NILP (tem);
3022 /* if the eighth element of the list is nil, we are in comment
3023 style a. If it is non-nil, we are in comment style b */
3024 oldstate = Fcdr (oldstate);
3025 oldstate = Fcdr (oldstate);
3026 tem = Fcar (oldstate);
3027 state.comstyle = (NILP (tem)
3029 : (RANGED_INTEGERP (0, tem, ST_COMMENT_STYLE)
3030 ? XINT (tem)
3031 : ST_COMMENT_STYLE));
3033 oldstate = Fcdr (oldstate);
3034 tem = Fcar (oldstate);
3035 state.comstr_start =
3036 RANGED_INTEGERP (PTRDIFF_MIN, tem, PTRDIFF_MAX) ? XINT (tem) : -1;
3037 oldstate = Fcdr (oldstate);
3038 tem = Fcar (oldstate);
3039 while (!NILP (tem)) /* >= second enclosing sexps. */
3041 Lisp_Object temhd = Fcar (tem);
3042 if (RANGED_INTEGERP (PTRDIFF_MIN, temhd, PTRDIFF_MAX))
3043 curlevel->last = XINT (temhd);
3044 if (++curlevel == endlevel)
3045 curlevel--; /* error ("Nesting too deep for parser"); */
3046 curlevel->prev = -1;
3047 curlevel->last = -1;
3048 tem = Fcdr (tem);
3051 state.quoted = 0;
3052 mindepth = depth;
3054 curlevel->prev = -1;
3055 curlevel->last = -1;
3057 SETUP_SYNTAX_TABLE (prev_from, 1);
3058 temp = FETCH_CHAR (prev_from_byte);
3059 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
3060 UPDATE_SYNTAX_TABLE_FORWARD (from);
3062 /* Enter the loop at a place appropriate for initial state. */
3064 if (state.incomment)
3065 goto startincomment;
3066 if (state.instring >= 0)
3068 nofence = state.instring != ST_STRING_STYLE;
3069 if (start_quoted)
3070 goto startquotedinstring;
3071 goto startinstring;
3073 else if (start_quoted)
3074 goto startquoted;
3076 while (from < end)
3078 int syntax;
3079 INC_FROM;
3080 code = prev_from_syntax & 0xff;
3082 if (from < end
3083 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3084 && (c1 = FETCH_CHAR (from_byte),
3085 syntax = SYNTAX_WITH_FLAGS (c1),
3086 SYNTAX_FLAGS_COMSTART_SECOND (syntax)))
3087 /* Duplicate code to avoid a complex if-expression
3088 which causes trouble for the SGI compiler. */
3090 /* Record the comment style we have entered so that only
3091 the comment-end sequence of the same style actually
3092 terminates the comment section. */
3093 state.comstyle
3094 = SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_from_syntax);
3095 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
3096 comnested = comnested || SYNTAX_FLAGS_COMMENT_NESTED (syntax);
3097 state.incomment = comnested ? 1 : -1;
3098 state.comstr_start = prev_from;
3099 INC_FROM;
3100 code = Scomment;
3102 else if (code == Scomment_fence)
3104 /* Record the comment style we have entered so that only
3105 the comment-end sequence of the same style actually
3106 terminates the comment section. */
3107 state.comstyle = ST_COMMENT_STYLE;
3108 state.incomment = -1;
3109 state.comstr_start = prev_from;
3110 code = Scomment;
3112 else if (code == Scomment)
3114 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax, 0);
3115 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3116 1 : -1);
3117 state.comstr_start = prev_from;
3120 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3121 continue;
3122 switch (SWITCH_ENUM_CAST (code))
3124 case Sescape:
3125 case Scharquote:
3126 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3127 curlevel->last = prev_from;
3128 startquoted:
3129 if (from == end) goto endquoted;
3130 INC_FROM;
3131 goto symstarted;
3132 /* treat following character as a word constituent */
3133 case Sword:
3134 case Ssymbol:
3135 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3136 curlevel->last = prev_from;
3137 symstarted:
3138 while (from < end)
3140 /* Some compilers can't handle this inside the switch. */
3141 temp = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3142 temp = SYNTAX (temp);
3143 switch (temp)
3145 case Scharquote:
3146 case Sescape:
3147 INC_FROM;
3148 if (from == end) goto endquoted;
3149 break;
3150 case Sword:
3151 case Ssymbol:
3152 case Squote:
3153 break;
3154 default:
3155 goto symdone;
3157 INC_FROM;
3159 symdone:
3160 curlevel->prev = curlevel->last;
3161 break;
3163 case Scomment_fence: /* Can't happen because it's handled above. */
3164 case Scomment:
3165 if (commentstop || boundary_stop) goto done;
3166 startincomment:
3167 /* The (from == BEGV) test was to enter the loop in the middle so
3168 that we find a 2-char comment ender even if we start in the
3169 middle of it. We don't want to do that if we're just at the
3170 beginning of the comment (think of (*) ... (*)). */
3171 found = forw_comment (from, from_byte, end,
3172 state.incomment, state.comstyle,
3173 (from == BEGV || from < state.comstr_start + 3)
3174 ? 0 : prev_from_syntax,
3175 &out_charpos, &out_bytepos, &state.incomment);
3176 from = out_charpos; from_byte = out_bytepos;
3177 /* Beware! prev_from and friends are invalid now.
3178 Luckily, the `done' doesn't use them and the INC_FROM
3179 sets them to a sane value without looking at them. */
3180 if (!found) goto done;
3181 INC_FROM;
3182 state.incomment = 0;
3183 state.comstyle = 0; /* reset the comment style */
3184 if (boundary_stop) goto done;
3185 break;
3187 case Sopen:
3188 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3189 depth++;
3190 /* curlevel++->last ran into compiler bug on Apollo */
3191 curlevel->last = prev_from;
3192 if (++curlevel == endlevel)
3193 curlevel--; /* error ("Nesting too deep for parser"); */
3194 curlevel->prev = -1;
3195 curlevel->last = -1;
3196 if (targetdepth == depth) goto done;
3197 break;
3199 case Sclose:
3200 depth--;
3201 if (depth < mindepth)
3202 mindepth = depth;
3203 if (curlevel != levelstart)
3204 curlevel--;
3205 curlevel->prev = curlevel->last;
3206 if (targetdepth == depth) goto done;
3207 break;
3209 case Sstring:
3210 case Sstring_fence:
3211 state.comstr_start = from - 1;
3212 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3213 curlevel->last = prev_from;
3214 state.instring = (code == Sstring
3215 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3216 : ST_STRING_STYLE);
3217 if (boundary_stop) goto done;
3218 startinstring:
3220 nofence = state.instring != ST_STRING_STYLE;
3222 while (1)
3224 int c;
3226 if (from >= end) goto done;
3227 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3228 /* Some compilers can't handle this inside the switch. */
3229 temp = SYNTAX (c);
3231 /* Check TEMP here so that if the char has
3232 a syntax-table property which says it is NOT
3233 a string character, it does not end the string. */
3234 if (nofence && c == state.instring && temp == Sstring)
3235 break;
3237 switch (temp)
3239 case Sstring_fence:
3240 if (!nofence) goto string_end;
3241 break;
3242 case Scharquote:
3243 case Sescape:
3244 INC_FROM;
3245 startquotedinstring:
3246 if (from >= end) goto endquoted;
3248 INC_FROM;
3251 string_end:
3252 state.instring = -1;
3253 curlevel->prev = curlevel->last;
3254 INC_FROM;
3255 if (boundary_stop) goto done;
3256 break;
3258 case Smath:
3259 /* FIXME: We should do something with it. */
3260 break;
3261 default:
3262 /* Ignore whitespace, punctuation, quote, endcomment. */
3263 break;
3266 goto done;
3268 stop: /* Here if stopping before start of sexp. */
3269 from = prev_from; /* We have just fetched the char that starts it; */
3270 goto done; /* but return the position before it. */
3272 endquoted:
3273 state.quoted = 1;
3274 done:
3275 state.depth = depth;
3276 state.mindepth = mindepth;
3277 state.thislevelstart = curlevel->prev;
3278 state.prevlevelstart
3279 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3280 state.location = from;
3281 state.levelstarts = Qnil;
3282 while (curlevel > levelstart)
3283 state.levelstarts = Fcons (make_number ((--curlevel)->last),
3284 state.levelstarts);
3285 immediate_quit = 0;
3287 *stateptr = state;
3290 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3291 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3292 Parsing stops at TO or when certain criteria are met;
3293 point is set to where parsing stops.
3294 If fifth arg OLDSTATE is omitted or nil,
3295 parsing assumes that FROM is the beginning of a function.
3296 Value is a list of elements describing final state of parsing:
3297 0. depth in parens.
3298 1. character address of start of innermost containing list; nil if none.
3299 2. character address of start of last complete sexp terminated.
3300 3. non-nil if inside a string.
3301 (it is the character that will terminate the string,
3302 or t if the string should be terminated by a generic string delimiter.)
3303 4. nil if outside a comment, t if inside a non-nestable comment,
3304 else an integer (the current comment nesting).
3305 5. t if following a quote character.
3306 6. the minimum paren-depth encountered during this scan.
3307 7. style of comment, if any.
3308 8. character address of start of comment or string; nil if not in one.
3309 9. Intermediate data for continuation of parsing (subject to change).
3310 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3311 in parentheses becomes equal to TARGETDEPTH.
3312 Fourth arg STOPBEFORE non-nil means stop when come to
3313 any character that starts a sexp.
3314 Fifth arg OLDSTATE is a list like what this function returns.
3315 It is used to initialize the state of the parse. Elements number 1, 2, 6
3316 are ignored.
3317 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3318 If it is symbol `syntax-table', stop after the start of a comment or a
3319 string, or after end of a comment or a string. */)
3320 (Lisp_Object from, Lisp_Object to, Lisp_Object targetdepth, Lisp_Object stopbefore, Lisp_Object oldstate, Lisp_Object commentstop)
3322 struct lisp_parse_state state;
3323 EMACS_INT target;
3325 if (!NILP (targetdepth))
3327 CHECK_NUMBER (targetdepth);
3328 target = XINT (targetdepth);
3330 else
3331 target = TYPE_MINIMUM (EMACS_INT); /* We won't reach this depth */
3333 validate_region (&from, &to);
3334 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3335 XINT (to),
3336 target, !NILP (stopbefore), oldstate,
3337 (NILP (commentstop)
3338 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3340 SET_PT (state.location);
3342 return Fcons (make_number (state.depth),
3343 Fcons (state.prevlevelstart < 0
3344 ? Qnil : make_number (state.prevlevelstart),
3345 Fcons (state.thislevelstart < 0
3346 ? Qnil : make_number (state.thislevelstart),
3347 Fcons (state.instring >= 0
3348 ? (state.instring == ST_STRING_STYLE
3349 ? Qt : make_number (state.instring)) : Qnil,
3350 Fcons (state.incomment < 0 ? Qt :
3351 (state.incomment == 0 ? Qnil :
3352 make_number (state.incomment)),
3353 Fcons (state.quoted ? Qt : Qnil,
3354 Fcons (make_number (state.mindepth),
3355 Fcons ((state.comstyle
3356 ? (state.comstyle == ST_COMMENT_STYLE
3357 ? Qsyntax_table
3358 : make_number (state.comstyle))
3359 : Qnil),
3360 Fcons (((state.incomment
3361 || (state.instring >= 0))
3362 ? make_number (state.comstr_start)
3363 : Qnil),
3364 Fcons (state.levelstarts, Qnil))))))))));
3367 void
3368 init_syntax_once (void)
3370 register int i, c;
3371 Lisp_Object temp;
3373 /* This has to be done here, before we call Fmake_char_table. */
3374 DEFSYM (Qsyntax_table, "syntax-table");
3376 /* Intern_C_String this now in case it isn't already done.
3377 Setting this variable twice is harmless.
3378 But don't staticpro it here--that is done in alloc.c. */
3379 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
3381 /* Create objects which can be shared among syntax tables. */
3382 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
3383 for (i = 0; i < ASIZE (Vsyntax_code_object); i++)
3384 XVECTOR (Vsyntax_code_object)->contents[i]
3385 = Fcons (make_number (i), Qnil);
3387 /* Now we are ready to set up this property, so we can
3388 create syntax tables. */
3389 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3391 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3393 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3395 /* Control characters should not be whitespace. */
3396 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3397 for (i = 0; i <= ' ' - 1; i++)
3398 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3399 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3401 /* Except that a few really are whitespace. */
3402 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3403 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3404 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3405 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3406 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3407 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3409 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3410 for (i = 'a'; i <= 'z'; i++)
3411 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3412 for (i = 'A'; i <= 'Z'; i++)
3413 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3414 for (i = '0'; i <= '9'; i++)
3415 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3417 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3418 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3420 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3421 Fcons (make_number (Sopen), make_number (')')));
3422 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3423 Fcons (make_number (Sclose), make_number ('(')));
3424 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3425 Fcons (make_number (Sopen), make_number (']')));
3426 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3427 Fcons (make_number (Sclose), make_number ('[')));
3428 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3429 Fcons (make_number (Sopen), make_number ('}')));
3430 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3431 Fcons (make_number (Sclose), make_number ('{')));
3432 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3433 Fcons (make_number ((int) Sstring), Qnil));
3434 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3435 Fcons (make_number ((int) Sescape), Qnil));
3437 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
3438 for (i = 0; i < 10; i++)
3440 c = "_-+*/&|<>="[i];
3441 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3444 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3445 for (i = 0; i < 12; i++)
3447 c = ".,;:?!#@~^'`"[i];
3448 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3451 /* All multibyte characters have syntax `word' by default. */
3452 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3453 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3456 void
3457 syms_of_syntax (void)
3459 DEFSYM (Qsyntax_table_p, "syntax-table-p");
3461 staticpro (&Vsyntax_code_object);
3463 staticpro (&gl_state.object);
3464 staticpro (&gl_state.global_code);
3465 staticpro (&gl_state.current_syntax_table);
3466 staticpro (&gl_state.old_prop);
3468 /* Defined in regex.c */
3469 staticpro (&re_match_object);
3471 DEFSYM (Qscan_error, "scan-error");
3472 Fput (Qscan_error, Qerror_conditions,
3473 pure_cons (Qscan_error, pure_cons (Qerror, Qnil)));
3474 Fput (Qscan_error, Qerror_message,
3475 make_pure_c_string ("Scan error"));
3477 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments,
3478 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3480 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties,
3481 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3482 Otherwise, that text property is simply ignored.
3483 See the info node `(elisp)Syntax Properties' for a description of the
3484 `syntax-table' property. */);
3486 words_include_escapes = 0;
3487 DEFVAR_BOOL ("words-include-escapes", words_include_escapes,
3488 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3490 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol,
3491 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3492 multibyte_syntax_as_symbol = 0;
3494 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3495 open_paren_in_column_0_is_defun_start,
3496 doc: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3497 open_paren_in_column_0_is_defun_start = 1;
3500 DEFVAR_LISP ("find-word-boundary-function-table",
3501 Vfind_word_boundary_function_table,
3502 doc: /*
3503 Char table of functions to search for the word boundary.
3504 Each function is called with two arguments; POS and LIMIT.
3505 POS and LIMIT are character positions in the current buffer.
3507 If POS is less than LIMIT, POS is at the first character of a word,
3508 and the return value of a function is a position after the last
3509 character of that word.
3511 If POS is not less than LIMIT, POS is at the last character of a word,
3512 and the return value of a function is a position at the first
3513 character of that word.
3515 In both cases, LIMIT bounds the search. */);
3516 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3518 defsubr (&Ssyntax_table_p);
3519 defsubr (&Ssyntax_table);
3520 defsubr (&Sstandard_syntax_table);
3521 defsubr (&Scopy_syntax_table);
3522 defsubr (&Sset_syntax_table);
3523 defsubr (&Schar_syntax);
3524 defsubr (&Smatching_paren);
3525 defsubr (&Sstring_to_syntax);
3526 defsubr (&Smodify_syntax_entry);
3527 defsubr (&Sinternal_describe_syntax_value);
3529 defsubr (&Sforward_word);
3531 defsubr (&Sskip_chars_forward);
3532 defsubr (&Sskip_chars_backward);
3533 defsubr (&Sskip_syntax_forward);
3534 defsubr (&Sskip_syntax_backward);
3536 defsubr (&Sforward_comment);
3537 defsubr (&Sscan_lists);
3538 defsubr (&Sscan_sexps);
3539 defsubr (&Sbackward_prefix_chars);
3540 defsubr (&Sparse_partial_sexp);