* buffer.c (struct sortstr.priority): Now EMACS_INT, not int.
[emacs.git] / src / syntax.c
blob82103cfa3d4460c6da3810291407a5f1da8afb38
1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2011
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 #include <config.h>
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 int depth; /* Depth at end of parsing. */
115 int instring; /* -1 if not within string, else desired terminator. */
116 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 int mindepth; /* Minimum depth seen while scanning. */
120 /* Char number of most recent start-of-expression at current level */
121 EMACS_INT thislevelstart;
122 /* Char number of start of containing expression */
123 EMACS_INT prevlevelstart;
124 EMACS_INT location; /* Char number at which parsing stopped. */
125 EMACS_INT 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 EMACS_INT find_start_pos;
139 static EMACS_INT find_start_value;
140 static EMACS_INT find_start_value_byte;
141 static struct buffer *find_start_buffer;
142 static EMACS_INT find_start_begv;
143 static 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 EMACS_INT, EMACS_INT, EMACS_INT, 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 (EMACS_INT 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 (EMACS_INT charpos, EMACS_INT bytepos)
344 register enum syntaxcode code;
345 register EMACS_INT beg = BEGV;
346 register int quoted = 0;
347 EMACS_INT 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 EMACS_INT
371 dec_bytepos (EMACS_INT 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 EMACS_INT
395 find_defun_start (EMACS_INT pos, EMACS_INT pos_byte)
397 EMACS_INT opoint = PT, opoint_byte = PT_BYTE;
399 if (!open_paren_in_column_0_is_defun_start)
401 find_start_value_byte = BEGV_BYTE;
402 return BEGV;
405 /* Use previous finding, if it's valid and applies to this inquiry. */
406 if (current_buffer == find_start_buffer
407 /* Reuse the defun-start even if POS is a little farther on.
408 POS might be in the next defun, but that's ok.
409 Our value may not be the best possible, but will still be usable. */
410 && pos <= find_start_pos + 1000
411 && pos >= find_start_value
412 && BEGV == find_start_begv
413 && MODIFF == find_start_modiff)
414 return find_start_value;
416 /* Back up to start of line. */
417 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
419 /* We optimize syntax-table lookup for rare updates. Thus we accept
420 only those `^\s(' which are good in global _and_ text-property
421 syntax-tables. */
422 SETUP_BUFFER_SYNTAX_TABLE ();
423 while (PT > BEGV)
425 int c;
427 /* Open-paren at start of line means we may have found our
428 defun-start. */
429 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
430 if (SYNTAX (c) == Sopen)
432 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
433 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
434 if (SYNTAX (c) == Sopen)
435 break;
436 /* Now fallback to the default value. */
437 SETUP_BUFFER_SYNTAX_TABLE ();
439 /* Move to beg of previous line. */
440 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
443 /* Record what we found, for the next try. */
444 find_start_value = PT;
445 find_start_value_byte = PT_BYTE;
446 find_start_buffer = current_buffer;
447 find_start_modiff = MODIFF;
448 find_start_begv = BEGV;
449 find_start_pos = pos;
451 TEMP_SET_PT_BOTH (opoint, opoint_byte);
453 return find_start_value;
456 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
458 static int
459 prev_char_comend_first (EMACS_INT pos, EMACS_INT pos_byte)
461 int c, val;
463 DEC_BOTH (pos, pos_byte);
464 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
465 c = FETCH_CHAR (pos_byte);
466 val = SYNTAX_COMEND_FIRST (c);
467 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
468 return val;
471 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
473 /* static int
474 * prev_char_comstart_first (pos, pos_byte)
475 * int pos, pos_byte;
477 * int c, val;
479 * DEC_BOTH (pos, pos_byte);
480 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
481 * c = FETCH_CHAR (pos_byte);
482 * val = SYNTAX_COMSTART_FIRST (c);
483 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
484 * return val;
485 * } */
487 /* Checks whether charpos FROM is at the end of a comment.
488 FROM_BYTE is the bytepos corresponding to FROM.
489 Do not move back before STOP.
491 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
492 return -1 otherwise.
494 If successful, store the charpos of the comment's beginning
495 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
497 Global syntax data remains valid for backward search starting at
498 the returned value (or at FROM, if the search was not successful). */
500 static int
501 back_comment (EMACS_INT from, EMACS_INT from_byte, EMACS_INT stop, int comnested, int comstyle, EMACS_INT *charpos_ptr, EMACS_INT *bytepos_ptr)
503 /* Look back, counting the parity of string-quotes,
504 and recording the comment-starters seen.
505 When we reach a safe place, assume that's not in a string;
506 then step the main scan to the earliest comment-starter seen
507 an even number of string quotes away from the safe place.
509 OFROM[I] is position of the earliest comment-starter seen
510 which is I+2X quotes from the comment-end.
511 PARITY is current parity of quotes from the comment end. */
512 int string_style = -1; /* Presumed outside of any string. */
513 int string_lossage = 0;
514 /* Not a real lossage: indicates that we have passed a matching comment
515 starter plus a non-matching comment-ender, meaning that any matching
516 comment-starter we might see later could be a false positive (hidden
517 inside another comment).
518 Test case: { a (* b } c (* d *) */
519 int comment_lossage = 0;
520 EMACS_INT comment_end = from;
521 EMACS_INT comment_end_byte = from_byte;
522 EMACS_INT comstart_pos = 0;
523 EMACS_INT comstart_byte IF_LINT (= 0);
524 /* Place where the containing defun starts,
525 or 0 if we didn't come across it yet. */
526 EMACS_INT defun_start = 0;
527 EMACS_INT defun_start_byte = 0;
528 register enum syntaxcode code;
529 int nesting = 1; /* current comment nesting */
530 int c;
531 int syntax = 0;
533 /* FIXME: A }} comment-ender style leads to incorrect behavior
534 in the case of {{ c }}} because we ignore the last two chars which are
535 assumed to be comment-enders although they aren't. */
537 /* At beginning of range to scan, we're outside of strings;
538 that determines quote parity to the comment-end. */
539 while (from != stop)
541 EMACS_INT temp_byte;
542 int prev_syntax, com2start, com2end;
543 int comstart;
545 /* Move back and examine a character. */
546 DEC_BOTH (from, from_byte);
547 UPDATE_SYNTAX_TABLE_BACKWARD (from);
549 prev_syntax = syntax;
550 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
551 syntax = SYNTAX_WITH_FLAGS (c);
552 code = SYNTAX (c);
554 /* Check for 2-char comment markers. */
555 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
556 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
557 && (comstyle
558 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax, syntax))
559 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
560 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
561 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
562 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
563 comstart = (com2start || code == Scomment);
565 /* Nasty cases with overlapping 2-char comment markers:
566 - snmp-mode: -- c -- foo -- c --
567 --- c --
568 ------ c --
569 - c-mode: *||*
570 |* *|* *|
571 |*| |* |*|
572 /// */
574 /* If a 2-char comment sequence partly overlaps with another,
575 we don't try to be clever. E.g. |*| in C, or }% in modes that
576 have %..\n and %{..}%. */
577 if (from > stop && (com2end || comstart))
579 EMACS_INT next = from, next_byte = from_byte;
580 int next_c, next_syntax;
581 DEC_BOTH (next, next_byte);
582 UPDATE_SYNTAX_TABLE_BACKWARD (next);
583 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
584 next_syntax = SYNTAX_WITH_FLAGS (next_c);
585 if (((comstart || comnested)
586 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
587 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
588 || ((com2end || comnested)
589 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
590 && (comstyle
591 == SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_syntax))
592 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
593 goto lossage;
594 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
597 if (com2start && comstart_pos == 0)
598 /* We're looking at a comment starter. But it might be a comment
599 ender as well (see snmp-mode). The first time we see one, we
600 need to consider it as a comment starter,
601 and the subsequent times as a comment ender. */
602 com2end = 0;
604 /* Turn a 2-char comment sequences into the appropriate syntax. */
605 if (com2end)
606 code = Sendcomment;
607 else if (com2start)
608 code = Scomment;
609 /* Ignore comment starters of a different style. */
610 else if (code == Scomment
611 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0)
612 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
613 continue;
615 /* Ignore escaped characters, except comment-enders. */
616 if (code != Sendcomment && char_quoted (from, from_byte))
617 continue;
619 switch (code)
621 case Sstring_fence:
622 case Scomment_fence:
623 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
624 case Sstring:
625 /* Track parity of quotes. */
626 if (string_style == -1)
627 /* Entering a string. */
628 string_style = c;
629 else if (string_style == c)
630 /* Leaving the string. */
631 string_style = -1;
632 else
633 /* If we have two kinds of string delimiters.
634 There's no way to grok this scanning backwards. */
635 string_lossage = 1;
636 break;
638 case Scomment:
639 /* We've already checked that it is the relevant comstyle. */
640 if (string_style != -1 || comment_lossage || string_lossage)
641 /* There are odd string quotes involved, so let's be careful.
642 Test case in Pascal: " { " a { " } */
643 goto lossage;
645 if (!comnested)
647 /* Record best comment-starter so far. */
648 comstart_pos = from;
649 comstart_byte = from_byte;
651 else if (--nesting <= 0)
652 /* nested comments have to be balanced, so we don't need to
653 keep looking for earlier ones. We use here the same (slightly
654 incorrect) reasoning as below: since it is followed by uniform
655 paired string quotes, this comment-start has to be outside of
656 strings, else the comment-end itself would be inside a string. */
657 goto done;
658 break;
660 case Sendcomment:
661 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == comstyle
662 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
663 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
664 /* This is the same style of comment ender as ours. */
666 if (comnested)
667 nesting++;
668 else
669 /* Anything before that can't count because it would match
670 this comment-ender rather than ours. */
671 from = stop; /* Break out of the loop. */
673 else if (comstart_pos != 0 || c != '\n')
674 /* We're mixing comment styles here, so we'd better be careful.
675 The (comstart_pos != 0 || c != '\n') check is not quite correct
676 (we should just always set comment_lossage), but removing it
677 would imply that any multiline comment in C would go through
678 lossage, which seems overkill.
679 The failure should only happen in the rare cases such as
680 { (* } *) */
681 comment_lossage = 1;
682 break;
684 case Sopen:
685 /* Assume a defun-start point is outside of strings. */
686 if (open_paren_in_column_0_is_defun_start
687 && (from == stop
688 || (temp_byte = dec_bytepos (from_byte),
689 FETCH_CHAR (temp_byte) == '\n')))
691 defun_start = from;
692 defun_start_byte = from_byte;
693 from = stop; /* Break out of the loop. */
695 break;
697 default:
698 break;
702 if (comstart_pos == 0)
704 from = comment_end;
705 from_byte = comment_end_byte;
706 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
708 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
709 or `done'), then we've found the beginning of the non-nested comment. */
710 else if (1) /* !comnested */
712 from = comstart_pos;
713 from_byte = comstart_byte;
714 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
716 else
718 struct lisp_parse_state state;
719 lossage:
720 /* We had two kinds of string delimiters mixed up
721 together. Decode this going forwards.
722 Scan fwd from a known safe place (beginning-of-defun)
723 to the one in question; this records where we
724 last passed a comment starter. */
725 /* If we did not already find the defun start, find it now. */
726 if (defun_start == 0)
728 defun_start = find_defun_start (comment_end, comment_end_byte);
729 defun_start_byte = find_start_value_byte;
733 scan_sexps_forward (&state,
734 defun_start, defun_start_byte,
735 comment_end, -10000, 0, Qnil, 0);
736 defun_start = comment_end;
737 if (state.incomment == (comnested ? 1 : -1)
738 && state.comstyle == comstyle)
739 from = state.comstr_start;
740 else
742 from = comment_end;
743 if (state.incomment)
744 /* If comment_end is inside some other comment, maybe ours
745 is nested, so we need to try again from within the
746 surrounding comment. Example: { a (* " *) */
748 /* FIXME: We should advance by one or two chars. */
749 defun_start = state.comstr_start + 2;
750 defun_start_byte = CHAR_TO_BYTE (defun_start);
753 } while (defun_start < comment_end);
755 from_byte = CHAR_TO_BYTE (from);
756 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
759 done:
760 *charpos_ptr = from;
761 *bytepos_ptr = from_byte;
763 return (from == comment_end) ? -1 : from;
766 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
767 doc: /* Return t if OBJECT is a syntax table.
768 Currently, any char-table counts as a syntax table. */)
769 (Lisp_Object object)
771 if (CHAR_TABLE_P (object)
772 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
773 return Qt;
774 return Qnil;
777 static void
778 check_syntax_table (Lisp_Object obj)
780 CHECK_TYPE (CHAR_TABLE_P (obj) && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table),
781 Qsyntax_table_p, obj);
784 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
785 doc: /* Return the current syntax table.
786 This is the one specified by the current buffer. */)
787 (void)
789 return BVAR (current_buffer, syntax_table);
792 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
793 Sstandard_syntax_table, 0, 0, 0,
794 doc: /* Return the standard syntax table.
795 This is the one used for new buffers. */)
796 (void)
798 return Vstandard_syntax_table;
801 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
802 doc: /* Construct a new syntax table and return it.
803 It is a copy of the TABLE, which defaults to the standard syntax table. */)
804 (Lisp_Object table)
806 Lisp_Object copy;
808 if (!NILP (table))
809 check_syntax_table (table);
810 else
811 table = Vstandard_syntax_table;
813 copy = Fcopy_sequence (table);
815 /* Only the standard syntax table should have a default element.
816 Other syntax tables should inherit from parents instead. */
817 XCHAR_TABLE (copy)->defalt = Qnil;
819 /* Copied syntax tables should all have parents.
820 If we copied one with no parent, such as the standard syntax table,
821 use the standard syntax table as the copy's parent. */
822 if (NILP (XCHAR_TABLE (copy)->parent))
823 Fset_char_table_parent (copy, Vstandard_syntax_table);
824 return copy;
827 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
828 doc: /* Select a new syntax table for the current buffer.
829 One argument, a syntax table. */)
830 (Lisp_Object table)
832 int idx;
833 check_syntax_table (table);
834 BVAR (current_buffer, syntax_table) = table;
835 /* Indicate that this buffer now has a specified syntax table. */
836 idx = PER_BUFFER_VAR_IDX (syntax_table);
837 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
838 return table;
841 /* Convert a letter which signifies a syntax code
842 into the code it signifies.
843 This is used by modify-syntax-entry, and other things. */
845 unsigned char syntax_spec_code[0400] =
846 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
847 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
848 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
849 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
850 (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377,
851 (char) Smath, 0377, 0377, (char) Squote,
852 (char) Sopen, (char) Sclose, 0377, 0377,
853 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
854 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
855 0377, 0377, 0377, 0377,
856 (char) Scomment, 0377, (char) Sendcomment, 0377,
857 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
858 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
859 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
860 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
861 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
862 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
863 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
864 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377
867 /* Indexed by syntax code, give the letter that describes it. */
869 char syntax_code_spec[16] =
871 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
872 '!', '|'
875 /* Indexed by syntax code, give the object (cons of syntax code and
876 nil) to be stored in syntax table. Since these objects can be
877 shared among syntax tables, we generate them in advance. By
878 sharing objects, the function `describe-syntax' can give a more
879 compact listing. */
880 static Lisp_Object Vsyntax_code_object;
883 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
884 doc: /* Return the syntax code of CHARACTER, described by a character.
885 For example, if CHARACTER is a word constituent, the
886 character `w' (119) is returned.
887 The characters that correspond to various syntax codes
888 are listed in the documentation of `modify-syntax-entry'. */)
889 (Lisp_Object character)
891 int char_int;
892 CHECK_CHARACTER (character);
893 char_int = XINT (character);
894 SETUP_BUFFER_SYNTAX_TABLE ();
895 return make_number (syntax_code_spec[(int) SYNTAX (char_int)]);
898 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
899 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
900 (Lisp_Object character)
902 int char_int, code;
903 CHECK_NUMBER (character);
904 char_int = XINT (character);
905 SETUP_BUFFER_SYNTAX_TABLE ();
906 code = SYNTAX (char_int);
907 if (code == Sopen || code == Sclose)
908 return SYNTAX_MATCH (char_int);
909 return Qnil;
912 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
913 doc: /* Convert a syntax specification STRING into syntax cell form.
914 STRING should be a string as it is allowed as argument of
915 `modify-syntax-entry'. Value is the equivalent cons cell
916 \(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
917 text property. */)
918 (Lisp_Object string)
920 register const unsigned char *p;
921 register enum syntaxcode code;
922 int val;
923 Lisp_Object match;
925 CHECK_STRING (string);
927 p = SDATA (string);
928 code = (enum syntaxcode) syntax_spec_code[*p++];
929 if (((int) code & 0377) == 0377)
930 error ("Invalid syntax description letter: %c", p[-1]);
932 if (code == Sinherit)
933 return Qnil;
935 if (*p)
937 int len;
938 int character = STRING_CHAR_AND_LENGTH (p, len);
939 XSETINT (match, character);
940 if (XFASTINT (match) == ' ')
941 match = Qnil;
942 p += len;
944 else
945 match = Qnil;
947 val = (int) code;
948 while (*p)
949 switch (*p++)
951 case '1':
952 val |= 1 << 16;
953 break;
955 case '2':
956 val |= 1 << 17;
957 break;
959 case '3':
960 val |= 1 << 18;
961 break;
963 case '4':
964 val |= 1 << 19;
965 break;
967 case 'p':
968 val |= 1 << 20;
969 break;
971 case 'b':
972 val |= 1 << 21;
973 break;
975 case 'n':
976 val |= 1 << 22;
977 break;
979 case 'c':
980 val |= 1 << 23;
981 break;
984 if (val < ASIZE (Vsyntax_code_object) && NILP (match))
985 return XVECTOR (Vsyntax_code_object)->contents[val];
986 else
987 /* Since we can't use a shared object, let's make a new one. */
988 return Fcons (make_number (val), match);
991 /* I really don't know why this is interactive
992 help-form should at least be made useful whilst reading the second arg. */
993 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
994 "cSet syntax for character: \nsSet syntax for %s to: ",
995 doc: /* Set syntax for character CHAR according to string NEWENTRY.
996 The syntax is changed only for table SYNTAX-TABLE, which defaults to
997 the current buffer's syntax table.
998 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
999 in the range MIN to MAX are changed.
1000 The first character of NEWENTRY should be one of the following:
1001 Space or - whitespace syntax. w word constituent.
1002 _ symbol constituent. . punctuation.
1003 ( open-parenthesis. ) close-parenthesis.
1004 " string quote. \\ escape.
1005 $ paired delimiter. ' expression quote or prefix operator.
1006 < comment starter. > comment ender.
1007 / character-quote. @ inherit from `standard-syntax-table'.
1008 | generic string fence. ! generic comment fence.
1010 Only single-character comment start and end sequences are represented thus.
1011 Two-character sequences are represented as described below.
1012 The second character of NEWENTRY is the matching parenthesis,
1013 used only if the first character is `(' or `)'.
1014 Any additional characters are flags.
1015 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1016 1 means CHAR is the start of a two-char comment start sequence.
1017 2 means CHAR is the second character of such a sequence.
1018 3 means CHAR is the start of a two-char comment end sequence.
1019 4 means CHAR is the second character of such a sequence.
1021 There can be several orthogonal comment sequences. This is to support
1022 language modes such as C++. By default, all comment sequences are of style
1023 a, but you can set the comment sequence style to b (on the second character
1024 of a comment-start, and the first character of a comment-end sequence) and/or
1025 c (on any of its chars) using this flag:
1026 b means CHAR is part of comment sequence b.
1027 c means CHAR is part of comment sequence c.
1028 n means CHAR is part of a nestable comment sequence.
1030 p means CHAR is a prefix character for `backward-prefix-chars';
1031 such characters are treated as whitespace when they occur
1032 between expressions.
1033 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1034 (Lisp_Object c, Lisp_Object newentry, Lisp_Object syntax_table)
1036 if (CONSP (c))
1038 CHECK_CHARACTER_CAR (c);
1039 CHECK_CHARACTER_CDR (c);
1041 else
1042 CHECK_CHARACTER (c);
1044 if (NILP (syntax_table))
1045 syntax_table = BVAR (current_buffer, syntax_table);
1046 else
1047 check_syntax_table (syntax_table);
1049 newentry = Fstring_to_syntax (newentry);
1050 if (CONSP (c))
1051 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1052 else
1053 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
1055 /* We clear the regexp cache, since character classes can now have
1056 different values from those in the compiled regexps.*/
1057 clear_regexp_cache ();
1059 return Qnil;
1062 /* Dump syntax table to buffer in human-readable format */
1064 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1065 Sinternal_describe_syntax_value, 1, 1, 0,
1066 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1067 (Lisp_Object syntax)
1069 register enum syntaxcode code;
1070 int syntax_code;
1071 char desc, start1, start2, end1, end2, prefix,
1072 comstyleb, comstylec, comnested;
1073 char str[2];
1074 Lisp_Object first, match_lisp, value = syntax;
1076 if (NILP (value))
1078 insert_string ("default");
1079 return syntax;
1082 if (CHAR_TABLE_P (value))
1084 insert_string ("deeper char-table ...");
1085 return syntax;
1088 if (!CONSP (value))
1090 insert_string ("invalid");
1091 return syntax;
1094 first = XCAR (value);
1095 match_lisp = XCDR (value);
1097 if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp)))
1099 insert_string ("invalid");
1100 return syntax;
1103 syntax_code = XINT (first);
1104 code = (enum syntaxcode) (syntax_code & 0377);
1105 start1 = SYNTAX_FLAGS_COMSTART_FIRST (syntax_code);
1106 start2 = SYNTAX_FLAGS_COMSTART_SECOND (syntax_code);;
1107 end1 = SYNTAX_FLAGS_COMEND_FIRST (syntax_code);
1108 end2 = SYNTAX_FLAGS_COMEND_SECOND (syntax_code);
1109 prefix = SYNTAX_FLAGS_PREFIX (syntax_code);
1110 comstyleb = SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code);
1111 comstylec = SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code);
1112 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax_code);
1114 if ((int) code < 0 || (int) code >= (int) Smax)
1116 insert_string ("invalid");
1117 return syntax;
1119 desc = syntax_code_spec[(int) code];
1121 str[0] = desc, str[1] = 0;
1122 insert (str, 1);
1124 if (NILP (match_lisp))
1125 insert (" ", 1);
1126 else
1127 insert_char (XINT (match_lisp));
1129 if (start1)
1130 insert ("1", 1);
1131 if (start2)
1132 insert ("2", 1);
1134 if (end1)
1135 insert ("3", 1);
1136 if (end2)
1137 insert ("4", 1);
1139 if (prefix)
1140 insert ("p", 1);
1141 if (comstyleb)
1142 insert ("b", 1);
1143 if (comstylec)
1144 insert ("c", 1);
1145 if (comnested)
1146 insert ("n", 1);
1148 insert_string ("\twhich means: ");
1150 switch (SWITCH_ENUM_CAST (code))
1152 case Swhitespace:
1153 insert_string ("whitespace"); break;
1154 case Spunct:
1155 insert_string ("punctuation"); break;
1156 case Sword:
1157 insert_string ("word"); break;
1158 case Ssymbol:
1159 insert_string ("symbol"); break;
1160 case Sopen:
1161 insert_string ("open"); break;
1162 case Sclose:
1163 insert_string ("close"); break;
1164 case Squote:
1165 insert_string ("prefix"); break;
1166 case Sstring:
1167 insert_string ("string"); break;
1168 case Smath:
1169 insert_string ("math"); break;
1170 case Sescape:
1171 insert_string ("escape"); break;
1172 case Scharquote:
1173 insert_string ("charquote"); break;
1174 case Scomment:
1175 insert_string ("comment"); break;
1176 case Sendcomment:
1177 insert_string ("endcomment"); break;
1178 case Sinherit:
1179 insert_string ("inherit"); break;
1180 case Scomment_fence:
1181 insert_string ("comment fence"); break;
1182 case Sstring_fence:
1183 insert_string ("string fence"); break;
1184 default:
1185 insert_string ("invalid");
1186 return syntax;
1189 if (!NILP (match_lisp))
1191 insert_string (", matches ");
1192 insert_char (XINT (match_lisp));
1195 if (start1)
1196 insert_string (",\n\t is the first character of a comment-start sequence");
1197 if (start2)
1198 insert_string (",\n\t is the second character of a comment-start sequence");
1200 if (end1)
1201 insert_string (",\n\t is the first character of a comment-end sequence");
1202 if (end2)
1203 insert_string (",\n\t is the second character of a comment-end sequence");
1204 if (comstyleb)
1205 insert_string (" (comment style b)");
1206 if (comstylec)
1207 insert_string (" (comment style c)");
1208 if (comnested)
1209 insert_string (" (nestable)");
1211 if (prefix)
1212 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1214 return syntax;
1217 /* Return the position across COUNT words from FROM.
1218 If that many words cannot be found before the end of the buffer, return 0.
1219 COUNT negative means scan backward and stop at word beginning. */
1221 EMACS_INT
1222 scan_words (register EMACS_INT from, register EMACS_INT count)
1224 register EMACS_INT beg = BEGV;
1225 register EMACS_INT end = ZV;
1226 register EMACS_INT from_byte = CHAR_TO_BYTE (from);
1227 register enum syntaxcode code;
1228 int ch0, ch1;
1229 Lisp_Object func, pos;
1231 immediate_quit = 1;
1232 QUIT;
1234 SETUP_SYNTAX_TABLE (from, count);
1236 while (count > 0)
1238 while (1)
1240 if (from == end)
1242 immediate_quit = 0;
1243 return 0;
1245 UPDATE_SYNTAX_TABLE_FORWARD (from);
1246 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1247 code = SYNTAX (ch0);
1248 INC_BOTH (from, from_byte);
1249 if (words_include_escapes
1250 && (code == Sescape || code == Scharquote))
1251 break;
1252 if (code == Sword)
1253 break;
1255 /* Now CH0 is a character which begins a word and FROM is the
1256 position of the next character. */
1257 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
1258 if (! NILP (Ffboundp (func)))
1260 pos = call2 (func, make_number (from - 1), make_number (end));
1261 if (INTEGERP (pos) && XINT (pos) > from)
1263 from = XINT (pos);
1264 from_byte = CHAR_TO_BYTE (from);
1267 else
1269 while (1)
1271 if (from == end) break;
1272 UPDATE_SYNTAX_TABLE_FORWARD (from);
1273 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1274 code = SYNTAX (ch1);
1275 if ((code != Sword
1276 && (! words_include_escapes
1277 || (code != Sescape && code != Scharquote)))
1278 || word_boundary_p (ch0, ch1))
1279 break;
1280 INC_BOTH (from, from_byte);
1281 ch0 = ch1;
1284 count--;
1286 while (count < 0)
1288 while (1)
1290 if (from == beg)
1292 immediate_quit = 0;
1293 return 0;
1295 DEC_BOTH (from, from_byte);
1296 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1297 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1298 code = SYNTAX (ch1);
1299 if (words_include_escapes
1300 && (code == Sescape || code == Scharquote))
1301 break;
1302 if (code == Sword)
1303 break;
1305 /* Now CH1 is a character which ends a word and FROM is the
1306 position of it. */
1307 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
1308 if (! NILP (Ffboundp (func)))
1310 pos = call2 (func, make_number (from), make_number (beg));
1311 if (INTEGERP (pos) && XINT (pos) < from)
1313 from = XINT (pos);
1314 from_byte = CHAR_TO_BYTE (from);
1317 else
1319 while (1)
1321 if (from == beg)
1322 break;
1323 DEC_BOTH (from, from_byte);
1324 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1325 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1326 code = SYNTAX (ch0);
1327 if ((code != Sword
1328 && (! words_include_escapes
1329 || (code != Sescape && code != Scharquote)))
1330 || word_boundary_p (ch0, ch1))
1332 INC_BOTH (from, from_byte);
1333 break;
1335 ch1 = ch0;
1338 count++;
1341 immediate_quit = 0;
1343 return from;
1346 DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p",
1347 doc: /* Move point forward ARG words (backward if ARG is negative).
1348 Normally returns t.
1349 If an edge of the buffer or a field boundary is reached, point is left there
1350 and the function returns nil. Field boundaries are not noticed if
1351 `inhibit-field-text-motion' is non-nil. */)
1352 (Lisp_Object arg)
1354 Lisp_Object tmp;
1355 int orig_val, val;
1357 if (NILP (arg))
1358 XSETFASTINT (arg, 1);
1359 else
1360 CHECK_NUMBER (arg);
1362 val = orig_val = scan_words (PT, XINT (arg));
1363 if (! orig_val)
1364 val = XINT (arg) > 0 ? ZV : BEGV;
1366 /* Avoid jumping out of an input field. */
1367 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1368 Qt, Qnil, Qnil);
1369 val = XFASTINT (tmp);
1371 SET_PT (val);
1372 return val == orig_val ? Qt : Qnil;
1375 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1376 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1377 STRING is like the inside of a `[...]' in a regular expression
1378 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1379 (but not at the end of a range; quoting is never needed there).
1380 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1381 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1382 Char classes, e.g. `[:alpha:]', are supported.
1384 Returns the distance traveled, either zero or positive. */)
1385 (Lisp_Object string, Lisp_Object lim)
1387 return skip_chars (1, string, lim, 1);
1390 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1391 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1392 See `skip-chars-forward' for details.
1393 Returns the distance traveled, either zero or negative. */)
1394 (Lisp_Object string, Lisp_Object lim)
1396 return skip_chars (0, string, lim, 1);
1399 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1400 doc: /* Move point forward across chars in specified syntax classes.
1401 SYNTAX is a string of syntax code characters.
1402 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1403 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1404 This function returns the distance traveled, either zero or positive. */)
1405 (Lisp_Object syntax, Lisp_Object lim)
1407 return skip_syntaxes (1, syntax, lim);
1410 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1411 doc: /* Move point backward across chars in specified syntax classes.
1412 SYNTAX is a string of syntax code characters.
1413 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1414 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1415 This function returns the distance traveled, either zero or negative. */)
1416 (Lisp_Object syntax, Lisp_Object lim)
1418 return skip_syntaxes (0, syntax, lim);
1421 static Lisp_Object
1422 skip_chars (int forwardp, Lisp_Object string, Lisp_Object lim, int handle_iso_classes)
1424 register unsigned int c;
1425 unsigned char fastmap[0400];
1426 /* Store the ranges of non-ASCII characters. */
1427 int *char_ranges IF_LINT (= NULL);
1428 int n_char_ranges = 0;
1429 int negate = 0;
1430 register EMACS_INT i, i_byte;
1431 /* Set to 1 if the current buffer is multibyte and the region
1432 contains non-ASCII chars. */
1433 int multibyte;
1434 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1435 chars. */
1436 int string_multibyte;
1437 EMACS_INT size_byte;
1438 const unsigned char *str;
1439 int len;
1440 Lisp_Object iso_classes;
1442 CHECK_STRING (string);
1443 iso_classes = Qnil;
1445 if (NILP (lim))
1446 XSETINT (lim, forwardp ? ZV : BEGV);
1447 else
1448 CHECK_NUMBER_COERCE_MARKER (lim);
1450 /* In any case, don't allow scan outside bounds of buffer. */
1451 if (XINT (lim) > ZV)
1452 XSETFASTINT (lim, ZV);
1453 if (XINT (lim) < BEGV)
1454 XSETFASTINT (lim, BEGV);
1456 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1457 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1458 string_multibyte = SBYTES (string) > SCHARS (string);
1460 memset (fastmap, 0, sizeof fastmap);
1462 str = SDATA (string);
1463 size_byte = SBYTES (string);
1465 i_byte = 0;
1466 if (i_byte < size_byte
1467 && SREF (string, 0) == '^')
1469 negate = 1; i_byte++;
1472 /* Find the characters specified and set their elements of fastmap.
1473 Handle backslashes and ranges specially.
1475 If STRING contains non-ASCII characters, setup char_ranges for
1476 them and use fastmap only for their leading codes. */
1478 if (! string_multibyte)
1480 int string_has_eight_bit = 0;
1482 /* At first setup fastmap. */
1483 while (i_byte < size_byte)
1485 c = str[i_byte++];
1487 if (handle_iso_classes && c == '['
1488 && i_byte < size_byte
1489 && str[i_byte] == ':')
1491 const unsigned char *class_beg = str + i_byte + 1;
1492 const unsigned char *class_end = class_beg;
1493 const unsigned char *class_limit = str + size_byte - 2;
1494 /* Leave room for the null. */
1495 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1496 re_wctype_t cc;
1498 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1499 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1501 while (class_end < class_limit
1502 && *class_end >= 'a' && *class_end <= 'z')
1503 class_end++;
1505 if (class_end == class_beg
1506 || *class_end != ':' || class_end[1] != ']')
1507 goto not_a_class_name;
1509 memcpy (class_name, class_beg, class_end - class_beg);
1510 class_name[class_end - class_beg] = 0;
1512 cc = re_wctype (class_name);
1513 if (cc == 0)
1514 error ("Invalid ISO C character class");
1516 iso_classes = Fcons (make_number (cc), iso_classes);
1518 i_byte = class_end + 2 - str;
1519 continue;
1522 not_a_class_name:
1523 if (c == '\\')
1525 if (i_byte == size_byte)
1526 break;
1528 c = str[i_byte++];
1530 /* Treat `-' as range character only if another character
1531 follows. */
1532 if (i_byte + 1 < size_byte
1533 && str[i_byte] == '-')
1535 unsigned int c2;
1537 /* Skip over the dash. */
1538 i_byte++;
1540 /* Get the end of the range. */
1541 c2 = str[i_byte++];
1542 if (c2 == '\\'
1543 && i_byte < size_byte)
1544 c2 = str[i_byte++];
1546 if (c <= c2)
1548 unsigned lim2 = c2 + 1;
1549 while (c < lim2)
1550 fastmap[c++] = 1;
1551 if (! ASCII_CHAR_P (c2))
1552 string_has_eight_bit = 1;
1555 else
1557 fastmap[c] = 1;
1558 if (! ASCII_CHAR_P (c))
1559 string_has_eight_bit = 1;
1563 /* If the current range is multibyte and STRING contains
1564 eight-bit chars, arrange fastmap and setup char_ranges for
1565 the corresponding multibyte chars. */
1566 if (multibyte && string_has_eight_bit)
1568 unsigned char fastmap2[0400];
1569 int range_start_byte, range_start_char;
1571 memcpy (fastmap + 0200, fastmap2 + 0200, 0200);
1572 memset (fastmap + 0200, 0, 0200);
1573 /* We are sure that this loop stops. */
1574 for (i = 0200; ! fastmap2[i]; i++);
1575 c = BYTE8_TO_CHAR (i);
1576 fastmap[CHAR_LEADING_CODE (c)] = 1;
1577 range_start_byte = i;
1578 range_start_char = c;
1579 char_ranges = (int *) alloca (sizeof (int) * 128 * 2);
1580 for (i = 129; i < 0400; i++)
1582 c = BYTE8_TO_CHAR (i);
1583 fastmap[CHAR_LEADING_CODE (c)] = 1;
1584 if (i - range_start_byte != c - range_start_char)
1586 char_ranges[n_char_ranges++] = range_start_char;
1587 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1588 + range_start_char);
1589 range_start_byte = i;
1590 range_start_char = c;
1593 char_ranges[n_char_ranges++] = range_start_char;
1594 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1595 + range_start_char);
1598 else /* STRING is multibyte */
1600 char_ranges = (int *) alloca (sizeof (int) * SCHARS (string) * 2);
1602 while (i_byte < size_byte)
1604 unsigned char leading_code;
1606 leading_code = str[i_byte];
1607 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1608 i_byte += len;
1610 if (handle_iso_classes && c == '['
1611 && i_byte < size_byte
1612 && STRING_CHAR (str + i_byte) == ':')
1614 const unsigned char *class_beg = str + i_byte + 1;
1615 const unsigned char *class_end = class_beg;
1616 const unsigned char *class_limit = str + size_byte - 2;
1617 /* Leave room for the null. */
1618 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1619 re_wctype_t cc;
1621 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1622 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1624 while (class_end < class_limit
1625 && *class_end >= 'a' && *class_end <= 'z')
1626 class_end++;
1628 if (class_end == class_beg
1629 || *class_end != ':' || class_end[1] != ']')
1630 goto not_a_class_name_multibyte;
1632 memcpy (class_name, class_beg, class_end - class_beg);
1633 class_name[class_end - class_beg] = 0;
1635 cc = re_wctype (class_name);
1636 if (cc == 0)
1637 error ("Invalid ISO C character class");
1639 iso_classes = Fcons (make_number (cc), iso_classes);
1641 i_byte = class_end + 2 - str;
1642 continue;
1645 not_a_class_name_multibyte:
1646 if (c == '\\')
1648 if (i_byte == size_byte)
1649 break;
1651 leading_code = str[i_byte];
1652 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1653 i_byte += len;
1655 /* Treat `-' as range character only if another character
1656 follows. */
1657 if (i_byte + 1 < size_byte
1658 && str[i_byte] == '-')
1660 unsigned int c2;
1661 unsigned char leading_code2;
1663 /* Skip over the dash. */
1664 i_byte++;
1666 /* Get the end of the range. */
1667 leading_code2 = str[i_byte];
1668 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1669 i_byte += len;
1671 if (c2 == '\\'
1672 && i_byte < size_byte)
1674 leading_code2 = str[i_byte];
1675 c2 =STRING_CHAR_AND_LENGTH (str + i_byte, len);
1676 i_byte += len;
1679 if (c > c2)
1680 continue;
1681 if (ASCII_CHAR_P (c))
1683 while (c <= c2 && c < 0x80)
1684 fastmap[c++] = 1;
1685 leading_code = CHAR_LEADING_CODE (c);
1687 if (! ASCII_CHAR_P (c))
1689 unsigned lim2 = leading_code2 + 1;
1690 while (leading_code < lim2)
1691 fastmap[leading_code++] = 1;
1692 if (c <= c2)
1694 char_ranges[n_char_ranges++] = c;
1695 char_ranges[n_char_ranges++] = c2;
1699 else
1701 if (ASCII_CHAR_P (c))
1702 fastmap[c] = 1;
1703 else
1705 fastmap[leading_code] = 1;
1706 char_ranges[n_char_ranges++] = c;
1707 char_ranges[n_char_ranges++] = c;
1712 /* If the current range is unibyte and STRING contains non-ASCII
1713 chars, arrange fastmap for the corresponding unibyte
1714 chars. */
1716 if (! multibyte && n_char_ranges > 0)
1718 memset (fastmap + 0200, 0, 0200);
1719 for (i = 0; i < n_char_ranges; i += 2)
1721 int c1 = char_ranges[i];
1722 unsigned lim2 = char_ranges[i + 1] + 1;
1724 for (; c1 < lim2; c1++)
1726 int b = CHAR_TO_BYTE_SAFE (c1);
1727 if (b >= 0)
1728 fastmap[b] = 1;
1734 /* If ^ was the first character, complement the fastmap. */
1735 if (negate)
1737 if (! multibyte)
1738 for (i = 0; i < sizeof fastmap; i++)
1739 fastmap[i] ^= 1;
1740 else
1742 for (i = 0; i < 0200; i++)
1743 fastmap[i] ^= 1;
1744 /* All non-ASCII chars possibly match. */
1745 for (; i < sizeof fastmap; i++)
1746 fastmap[i] = 1;
1751 EMACS_INT start_point = PT;
1752 EMACS_INT pos = PT;
1753 EMACS_INT pos_byte = PT_BYTE;
1754 unsigned char *p = PT_ADDR, *endp, *stop;
1756 if (forwardp)
1758 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1759 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1761 else
1763 endp = CHAR_POS_ADDR (XINT (lim));
1764 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1767 immediate_quit = 1;
1768 /* This code may look up syntax tables using macros that rely on the
1769 gl_state object. To make sure this object is not out of date,
1770 let's initialize it manually.
1771 We ignore syntax-table text-properties for now, since that's
1772 what we've done in the past. */
1773 SETUP_BUFFER_SYNTAX_TABLE ();
1774 if (forwardp)
1776 if (multibyte)
1777 while (1)
1779 int nbytes;
1781 if (p >= stop)
1783 if (p >= endp)
1784 break;
1785 p = GAP_END_ADDR;
1786 stop = endp;
1788 c = STRING_CHAR_AND_LENGTH (p, nbytes);
1789 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1791 if (negate)
1792 break;
1793 else
1794 goto fwd_ok;
1797 if (! fastmap[*p])
1798 break;
1799 if (! ASCII_CHAR_P (c))
1801 /* As we are looking at a multibyte character, we
1802 must look up the character in the table
1803 CHAR_RANGES. If there's no data in the table,
1804 that character is not what we want to skip. */
1806 /* The following code do the right thing even if
1807 n_char_ranges is zero (i.e. no data in
1808 CHAR_RANGES). */
1809 for (i = 0; i < n_char_ranges; i += 2)
1810 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1811 break;
1812 if (!(negate ^ (i < n_char_ranges)))
1813 break;
1815 fwd_ok:
1816 p += nbytes, pos++, pos_byte += nbytes;
1818 else
1819 while (1)
1821 if (p >= stop)
1823 if (p >= endp)
1824 break;
1825 p = GAP_END_ADDR;
1826 stop = endp;
1829 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1831 if (negate)
1832 break;
1833 else
1834 goto fwd_unibyte_ok;
1837 if (!fastmap[*p])
1838 break;
1839 fwd_unibyte_ok:
1840 p++, pos++, pos_byte++;
1843 else
1845 if (multibyte)
1846 while (1)
1848 unsigned char *prev_p;
1850 if (p <= stop)
1852 if (p <= endp)
1853 break;
1854 p = GPT_ADDR;
1855 stop = endp;
1857 prev_p = p;
1858 while (--p >= stop && ! CHAR_HEAD_P (*p));
1859 c = STRING_CHAR (p);
1861 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1863 if (negate)
1864 break;
1865 else
1866 goto back_ok;
1869 if (! fastmap[*p])
1870 break;
1871 if (! ASCII_CHAR_P (c))
1873 /* See the comment in the previous similar code. */
1874 for (i = 0; i < n_char_ranges; i += 2)
1875 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1876 break;
1877 if (!(negate ^ (i < n_char_ranges)))
1878 break;
1880 back_ok:
1881 pos--, pos_byte -= prev_p - p;
1883 else
1884 while (1)
1886 if (p <= stop)
1888 if (p <= endp)
1889 break;
1890 p = GPT_ADDR;
1891 stop = endp;
1894 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
1896 if (negate)
1897 break;
1898 else
1899 goto back_unibyte_ok;
1902 if (!fastmap[p[-1]])
1903 break;
1904 back_unibyte_ok:
1905 p--, pos--, pos_byte--;
1909 SET_PT_BOTH (pos, pos_byte);
1910 immediate_quit = 0;
1912 return make_number (PT - start_point);
1917 static Lisp_Object
1918 skip_syntaxes (int forwardp, Lisp_Object string, Lisp_Object lim)
1920 register unsigned int c;
1921 unsigned char fastmap[0400];
1922 int negate = 0;
1923 register EMACS_INT i, i_byte;
1924 int multibyte;
1925 EMACS_INT size_byte;
1926 unsigned char *str;
1928 CHECK_STRING (string);
1930 if (NILP (lim))
1931 XSETINT (lim, forwardp ? ZV : BEGV);
1932 else
1933 CHECK_NUMBER_COERCE_MARKER (lim);
1935 /* In any case, don't allow scan outside bounds of buffer. */
1936 if (XINT (lim) > ZV)
1937 XSETFASTINT (lim, ZV);
1938 if (XINT (lim) < BEGV)
1939 XSETFASTINT (lim, BEGV);
1941 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
1942 return make_number (0);
1944 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1945 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1947 memset (fastmap, 0, sizeof fastmap);
1949 if (SBYTES (string) > SCHARS (string))
1950 /* As this is very rare case (syntax spec is ASCII only), don't
1951 consider efficiency. */
1952 string = string_make_unibyte (string);
1954 str = SDATA (string);
1955 size_byte = SBYTES (string);
1957 i_byte = 0;
1958 if (i_byte < size_byte
1959 && SREF (string, 0) == '^')
1961 negate = 1; i_byte++;
1964 /* Find the syntaxes specified and set their elements of fastmap. */
1966 while (i_byte < size_byte)
1968 c = str[i_byte++];
1969 fastmap[syntax_spec_code[c]] = 1;
1972 /* If ^ was the first character, complement the fastmap. */
1973 if (negate)
1974 for (i = 0; i < sizeof fastmap; i++)
1975 fastmap[i] ^= 1;
1978 EMACS_INT start_point = PT;
1979 EMACS_INT pos = PT;
1980 EMACS_INT pos_byte = PT_BYTE;
1981 unsigned char *p = PT_ADDR, *endp, *stop;
1983 if (forwardp)
1985 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1986 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1988 else
1990 endp = CHAR_POS_ADDR (XINT (lim));
1991 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1994 immediate_quit = 1;
1995 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
1996 if (forwardp)
1998 if (multibyte)
2000 while (1)
2002 int nbytes;
2004 if (p >= stop)
2006 if (p >= endp)
2007 break;
2008 p = GAP_END_ADDR;
2009 stop = endp;
2011 c = STRING_CHAR_AND_LENGTH (p, nbytes);
2012 if (! fastmap[(int) SYNTAX (c)])
2013 break;
2014 p += nbytes, pos++, pos_byte += nbytes;
2015 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2018 else
2020 while (1)
2022 if (p >= stop)
2024 if (p >= endp)
2025 break;
2026 p = GAP_END_ADDR;
2027 stop = endp;
2029 if (! fastmap[(int) SYNTAX (*p)])
2030 break;
2031 p++, pos++, pos_byte++;
2032 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2036 else
2038 if (multibyte)
2040 while (1)
2042 unsigned char *prev_p;
2044 if (p <= stop)
2046 if (p <= endp)
2047 break;
2048 p = GPT_ADDR;
2049 stop = endp;
2051 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2052 prev_p = p;
2053 while (--p >= stop && ! CHAR_HEAD_P (*p));
2054 c = STRING_CHAR (p);
2055 if (! fastmap[(int) SYNTAX (c)])
2056 break;
2057 pos--, pos_byte -= prev_p - p;
2060 else
2062 while (1)
2064 if (p <= stop)
2066 if (p <= endp)
2067 break;
2068 p = GPT_ADDR;
2069 stop = endp;
2071 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2072 if (! fastmap[(int) SYNTAX (p[-1])])
2073 break;
2074 p--, pos--, pos_byte--;
2079 SET_PT_BOTH (pos, pos_byte);
2080 immediate_quit = 0;
2082 return make_number (PT - start_point);
2086 /* Return 1 if character C belongs to one of the ISO classes
2087 in the list ISO_CLASSES. Each class is represented by an
2088 integer which is its type according to re_wctype. */
2090 static int
2091 in_classes (int c, Lisp_Object iso_classes)
2093 int fits_class = 0;
2095 while (CONSP (iso_classes))
2097 Lisp_Object elt;
2098 elt = XCAR (iso_classes);
2099 iso_classes = XCDR (iso_classes);
2101 if (re_iswctype (c, XFASTINT (elt)))
2102 fits_class = 1;
2105 return fits_class;
2108 /* Jump over a comment, assuming we are at the beginning of one.
2109 FROM is the current position.
2110 FROM_BYTE is the bytepos corresponding to FROM.
2111 Do not move past STOP (a charpos).
2112 The comment over which we have to jump is of style STYLE
2113 (either SYNTAX_FLAGS_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2114 NESTING should be positive to indicate the nesting at the beginning
2115 for nested comments and should be zero or negative else.
2116 ST_COMMENT_STYLE cannot be nested.
2117 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2118 (or 0 If the search cannot start in the middle of a two-character).
2120 If successful, return 1 and store the charpos of the comment's end
2121 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2122 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2123 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2124 (as defined for state.incomment) in *INCOMMENT_PTR.
2126 The comment end is the last character of the comment rather than the
2127 character just after the comment.
2129 Global syntax data is assumed to initially be valid for FROM and
2130 remains valid for forward search starting at the returned position. */
2132 static int
2133 forw_comment (EMACS_INT from, EMACS_INT from_byte, EMACS_INT stop,
2134 int nesting, int style, int prev_syntax,
2135 EMACS_INT *charpos_ptr, EMACS_INT *bytepos_ptr,
2136 int *incomment_ptr)
2138 register int c, c1;
2139 register enum syntaxcode code;
2140 register int syntax, other_syntax;
2142 if (nesting <= 0) nesting = -1;
2144 /* Enter the loop in the middle so that we find
2145 a 2-char comment ender if we start in the middle of it. */
2146 syntax = prev_syntax;
2147 if (syntax != 0) goto forw_incomment;
2149 while (1)
2151 if (from == stop)
2153 *incomment_ptr = nesting;
2154 *charpos_ptr = from;
2155 *bytepos_ptr = from_byte;
2156 return 0;
2158 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2159 syntax = SYNTAX_WITH_FLAGS (c);
2160 code = syntax & 0xff;
2161 if (code == Sendcomment
2162 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style
2163 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2164 (nesting > 0 && --nesting == 0) : nesting < 0))
2165 /* we have encountered a comment end of the same style
2166 as the comment sequence which began this comment
2167 section */
2168 break;
2169 if (code == Scomment_fence
2170 && style == ST_COMMENT_STYLE)
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 (nesting > 0
2176 && code == Scomment
2177 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2178 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style)
2179 /* we have encountered a nested comment of the same style
2180 as the comment sequence which began this comment section */
2181 nesting++;
2182 INC_BOTH (from, from_byte);
2183 UPDATE_SYNTAX_TABLE_FORWARD (from);
2185 forw_incomment:
2186 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2187 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2188 other_syntax = SYNTAX_WITH_FLAGS (c1),
2189 SYNTAX_FLAGS_COMEND_SECOND (other_syntax))
2190 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, other_syntax) == style
2191 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2192 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax))
2193 ? nesting > 0 : nesting < 0))
2195 if (--nesting <= 0)
2196 /* we have encountered a comment end of the same style
2197 as the comment sequence which began this comment
2198 section */
2199 break;
2200 else
2202 INC_BOTH (from, from_byte);
2203 UPDATE_SYNTAX_TABLE_FORWARD (from);
2206 if (nesting > 0
2207 && from < stop
2208 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2209 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2210 other_syntax = SYNTAX_WITH_FLAGS (c1),
2211 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax) == style
2212 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2213 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2214 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax)))
2215 /* we have encountered a nested comment of the same style
2216 as the comment sequence which began this comment
2217 section */
2219 INC_BOTH (from, from_byte);
2220 UPDATE_SYNTAX_TABLE_FORWARD (from);
2221 nesting++;
2224 *charpos_ptr = from;
2225 *bytepos_ptr = from_byte;
2226 return 1;
2229 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2230 doc: /*
2231 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2232 Stop scanning if we find something other than a comment or whitespace.
2233 Set point to where scanning stops.
2234 If COUNT comments are found as expected, with nothing except whitespace
2235 between them, return t; otherwise return nil. */)
2236 (Lisp_Object count)
2238 register EMACS_INT from;
2239 EMACS_INT from_byte;
2240 register EMACS_INT stop;
2241 register int c, c1;
2242 register enum syntaxcode code;
2243 int comstyle = 0; /* style of comment encountered */
2244 int comnested = 0; /* whether the comment is nestable or not */
2245 int found;
2246 EMACS_INT count1;
2247 EMACS_INT out_charpos, out_bytepos;
2248 int dummy;
2250 CHECK_NUMBER (count);
2251 count1 = XINT (count);
2252 stop = count1 > 0 ? ZV : BEGV;
2254 immediate_quit = 1;
2255 QUIT;
2257 from = PT;
2258 from_byte = PT_BYTE;
2260 SETUP_SYNTAX_TABLE (from, count1);
2261 while (count1 > 0)
2265 int comstart_first, syntax, other_syntax;
2267 if (from == stop)
2269 SET_PT_BOTH (from, from_byte);
2270 immediate_quit = 0;
2271 return Qnil;
2273 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2274 syntax = SYNTAX_WITH_FLAGS (c);
2275 code = SYNTAX (c);
2276 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2277 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2278 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2279 INC_BOTH (from, from_byte);
2280 UPDATE_SYNTAX_TABLE_FORWARD (from);
2281 if (from < stop && comstart_first
2282 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2283 other_syntax = SYNTAX_WITH_FLAGS (c1),
2284 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)))
2286 /* We have encountered a comment start sequence and we
2287 are ignoring all text inside comments. We must record
2288 the comment style this sequence begins so that later,
2289 only a comment end of the same style actually ends
2290 the comment section. */
2291 code = Scomment;
2292 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2293 comnested
2294 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2295 INC_BOTH (from, from_byte);
2296 UPDATE_SYNTAX_TABLE_FORWARD (from);
2299 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2301 if (code == Scomment_fence)
2302 comstyle = ST_COMMENT_STYLE;
2303 else if (code != Scomment)
2305 immediate_quit = 0;
2306 DEC_BOTH (from, from_byte);
2307 SET_PT_BOTH (from, from_byte);
2308 return Qnil;
2310 /* We're at the start of a comment. */
2311 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2312 &out_charpos, &out_bytepos, &dummy);
2313 from = out_charpos; from_byte = out_bytepos;
2314 if (!found)
2316 immediate_quit = 0;
2317 SET_PT_BOTH (from, from_byte);
2318 return Qnil;
2320 INC_BOTH (from, from_byte);
2321 UPDATE_SYNTAX_TABLE_FORWARD (from);
2322 /* We have skipped one comment. */
2323 count1--;
2326 while (count1 < 0)
2328 while (1)
2330 int quoted, syntax;
2332 if (from <= stop)
2334 SET_PT_BOTH (BEGV, BEGV_BYTE);
2335 immediate_quit = 0;
2336 return Qnil;
2339 DEC_BOTH (from, from_byte);
2340 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2341 quoted = char_quoted (from, from_byte);
2342 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2343 syntax = SYNTAX_WITH_FLAGS (c);
2344 code = SYNTAX (c);
2345 comstyle = 0;
2346 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2347 if (code == Sendcomment)
2348 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2349 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2350 && prev_char_comend_first (from, from_byte)
2351 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2353 int other_syntax;
2354 /* We must record the comment style encountered so that
2355 later, we can match only the proper comment begin
2356 sequence of the same style. */
2357 DEC_BOTH (from, from_byte);
2358 code = Sendcomment;
2359 /* Calling char_quoted, above, set up global syntax position
2360 at the new value of FROM. */
2361 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2362 other_syntax = SYNTAX_WITH_FLAGS (c1);
2363 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2364 comnested
2365 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2368 if (code == Scomment_fence)
2370 /* Skip until first preceding unquoted comment_fence. */
2371 int fence_found = 0;
2372 EMACS_INT ini = from, ini_byte = from_byte;
2374 while (1)
2376 DEC_BOTH (from, from_byte);
2377 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2378 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2379 if (SYNTAX (c) == Scomment_fence
2380 && !char_quoted (from, from_byte))
2382 fence_found = 1;
2383 break;
2385 else if (from == stop)
2386 break;
2388 if (fence_found == 0)
2390 from = ini; /* Set point to ini + 1. */
2391 from_byte = ini_byte;
2392 goto leave;
2394 else
2395 /* We have skipped one comment. */
2396 break;
2398 else if (code == Sendcomment)
2400 found = back_comment (from, from_byte, stop, comnested, comstyle,
2401 &out_charpos, &out_bytepos);
2402 if (found == -1)
2404 if (c == '\n')
2405 /* This end-of-line is not an end-of-comment.
2406 Treat it like a whitespace.
2407 CC-mode (and maybe others) relies on this behavior. */
2409 else
2411 /* Failure: we should go back to the end of this
2412 not-quite-endcomment. */
2413 if (SYNTAX (c) != code)
2414 /* It was a two-char Sendcomment. */
2415 INC_BOTH (from, from_byte);
2416 goto leave;
2419 else
2421 /* We have skipped one comment. */
2422 from = out_charpos, from_byte = out_bytepos;
2423 break;
2426 else if (code != Swhitespace || quoted)
2428 leave:
2429 immediate_quit = 0;
2430 INC_BOTH (from, from_byte);
2431 SET_PT_BOTH (from, from_byte);
2432 return Qnil;
2436 count1++;
2439 SET_PT_BOTH (from, from_byte);
2440 immediate_quit = 0;
2441 return Qt;
2444 /* Return syntax code of character C if C is an ASCII character
2445 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2447 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2448 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2449 ? SYNTAX (c) : Ssymbol)
2451 static Lisp_Object
2452 scan_lists (register EMACS_INT from, EMACS_INT count, EMACS_INT depth, int sexpflag)
2454 Lisp_Object val;
2455 register EMACS_INT stop = count > 0 ? ZV : BEGV;
2456 register int c, c1;
2457 int stringterm;
2458 int quoted;
2459 int mathexit = 0;
2460 register enum syntaxcode code, temp_code;
2461 int min_depth = depth; /* Err out if depth gets less than this. */
2462 int comstyle = 0; /* style of comment encountered */
2463 int comnested = 0; /* whether the comment is nestable or not */
2464 EMACS_INT temp_pos;
2465 EMACS_INT last_good = from;
2466 int found;
2467 EMACS_INT from_byte;
2468 EMACS_INT out_bytepos, out_charpos;
2469 int temp, dummy;
2470 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2472 if (depth > 0) min_depth = 0;
2474 if (from > ZV) from = ZV;
2475 if (from < BEGV) from = BEGV;
2477 from_byte = CHAR_TO_BYTE (from);
2479 immediate_quit = 1;
2480 QUIT;
2482 SETUP_SYNTAX_TABLE (from, count);
2483 while (count > 0)
2485 while (from < stop)
2487 int comstart_first, prefix, syntax, other_syntax;
2488 UPDATE_SYNTAX_TABLE_FORWARD (from);
2489 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2490 syntax = SYNTAX_WITH_FLAGS (c);
2491 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2492 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2493 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2494 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2495 prefix = SYNTAX_FLAGS_PREFIX (syntax);
2496 if (depth == min_depth)
2497 last_good = from;
2498 INC_BOTH (from, from_byte);
2499 UPDATE_SYNTAX_TABLE_FORWARD (from);
2500 if (from < stop && comstart_first
2501 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2502 other_syntax = SYNTAX_WITH_FLAGS (c),
2503 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2504 && parse_sexp_ignore_comments)
2506 /* we have encountered a comment start sequence and we
2507 are ignoring all text inside comments. We must record
2508 the comment style this sequence begins so that later,
2509 only a comment end of the same style actually ends
2510 the comment section */
2511 code = Scomment;
2512 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2513 comnested
2514 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2515 INC_BOTH (from, from_byte);
2516 UPDATE_SYNTAX_TABLE_FORWARD (from);
2519 if (prefix)
2520 continue;
2522 switch (SWITCH_ENUM_CAST (code))
2524 case Sescape:
2525 case Scharquote:
2526 if (from == stop)
2527 goto lose;
2528 INC_BOTH (from, from_byte);
2529 /* treat following character as a word constituent */
2530 case Sword:
2531 case Ssymbol:
2532 if (depth || !sexpflag) break;
2533 /* This word counts as a sexp; return at end of it. */
2534 while (from < stop)
2536 UPDATE_SYNTAX_TABLE_FORWARD (from);
2538 /* Some compilers can't handle this inside the switch. */
2539 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2540 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2541 switch (temp)
2543 case Scharquote:
2544 case Sescape:
2545 INC_BOTH (from, from_byte);
2546 if (from == stop)
2547 goto lose;
2548 break;
2549 case Sword:
2550 case Ssymbol:
2551 case Squote:
2552 break;
2553 default:
2554 goto done;
2556 INC_BOTH (from, from_byte);
2558 goto done;
2560 case Scomment_fence:
2561 comstyle = ST_COMMENT_STYLE;
2562 /* FALLTHROUGH */
2563 case Scomment:
2564 if (!parse_sexp_ignore_comments) break;
2565 UPDATE_SYNTAX_TABLE_FORWARD (from);
2566 found = forw_comment (from, from_byte, stop,
2567 comnested, comstyle, 0,
2568 &out_charpos, &out_bytepos, &dummy);
2569 from = out_charpos, from_byte = out_bytepos;
2570 if (!found)
2572 if (depth == 0)
2573 goto done;
2574 goto lose;
2576 INC_BOTH (from, from_byte);
2577 UPDATE_SYNTAX_TABLE_FORWARD (from);
2578 break;
2580 case Smath:
2581 if (!sexpflag)
2582 break;
2583 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2585 INC_BOTH (from, from_byte);
2587 if (mathexit)
2589 mathexit = 0;
2590 goto close1;
2592 mathexit = 1;
2594 case Sopen:
2595 if (!++depth) goto done;
2596 break;
2598 case Sclose:
2599 close1:
2600 if (!--depth) goto done;
2601 if (depth < min_depth)
2602 xsignal3 (Qscan_error,
2603 build_string ("Containing expression ends prematurely"),
2604 make_number (last_good), make_number (from));
2605 break;
2607 case Sstring:
2608 case Sstring_fence:
2609 temp_pos = dec_bytepos (from_byte);
2610 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2611 while (1)
2613 if (from >= stop)
2614 goto lose;
2615 UPDATE_SYNTAX_TABLE_FORWARD (from);
2616 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2617 if (code == Sstring
2618 ? (c == stringterm
2619 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2620 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
2621 break;
2623 /* Some compilers can't handle this inside the switch. */
2624 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2625 switch (temp)
2627 case Scharquote:
2628 case Sescape:
2629 INC_BOTH (from, from_byte);
2631 INC_BOTH (from, from_byte);
2633 INC_BOTH (from, from_byte);
2634 if (!depth && sexpflag) goto done;
2635 break;
2636 default:
2637 /* Ignore whitespace, punctuation, quote, endcomment. */
2638 break;
2642 /* Reached end of buffer. Error if within object, return nil if between */
2643 if (depth)
2644 goto lose;
2646 immediate_quit = 0;
2647 return Qnil;
2649 /* End of object reached */
2650 done:
2651 count--;
2654 while (count < 0)
2656 while (from > stop)
2658 int syntax;
2659 DEC_BOTH (from, from_byte);
2660 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2661 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2662 syntax= SYNTAX_WITH_FLAGS (c);
2663 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2664 if (depth == min_depth)
2665 last_good = from;
2666 comstyle = 0;
2667 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2668 if (code == Sendcomment)
2669 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2670 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2671 && prev_char_comend_first (from, from_byte)
2672 && parse_sexp_ignore_comments)
2674 /* We must record the comment style encountered so that
2675 later, we can match only the proper comment begin
2676 sequence of the same style. */
2677 int c2, other_syntax;
2678 DEC_BOTH (from, from_byte);
2679 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2680 code = Sendcomment;
2681 c2 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2682 other_syntax = SYNTAX_WITH_FLAGS (c2);
2683 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2684 comnested
2685 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2688 /* Quoting turns anything except a comment-ender
2689 into a word character. Note that this cannot be true
2690 if we decremented FROM in the if-statement above. */
2691 if (code != Sendcomment && char_quoted (from, from_byte))
2693 DEC_BOTH (from, from_byte);
2694 code = Sword;
2696 else if (SYNTAX_FLAGS_PREFIX (syntax))
2697 continue;
2699 switch (SWITCH_ENUM_CAST (code))
2701 case Sword:
2702 case Ssymbol:
2703 case Sescape:
2704 case Scharquote:
2705 if (depth || !sexpflag) break;
2706 /* This word counts as a sexp; count object finished
2707 after passing it. */
2708 while (from > stop)
2710 temp_pos = from_byte;
2711 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2712 DEC_POS (temp_pos);
2713 else
2714 temp_pos--;
2715 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2716 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2717 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2718 /* Don't allow comment-end to be quoted. */
2719 if (temp_code == Sendcomment)
2720 goto done2;
2721 quoted = char_quoted (from - 1, temp_pos);
2722 if (quoted)
2724 DEC_BOTH (from, from_byte);
2725 temp_pos = dec_bytepos (temp_pos);
2726 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2728 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2729 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2730 if (! (quoted || temp_code == Sword
2731 || temp_code == Ssymbol
2732 || temp_code == Squote))
2733 goto done2;
2734 DEC_BOTH (from, from_byte);
2736 goto done2;
2738 case Smath:
2739 if (!sexpflag)
2740 break;
2741 temp_pos = dec_bytepos (from_byte);
2742 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2743 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2744 DEC_BOTH (from, from_byte);
2745 if (mathexit)
2747 mathexit = 0;
2748 goto open2;
2750 mathexit = 1;
2752 case Sclose:
2753 if (!++depth) goto done2;
2754 break;
2756 case Sopen:
2757 open2:
2758 if (!--depth) goto done2;
2759 if (depth < min_depth)
2760 xsignal3 (Qscan_error,
2761 build_string ("Containing expression ends prematurely"),
2762 make_number (last_good), make_number (from));
2763 break;
2765 case Sendcomment:
2766 if (!parse_sexp_ignore_comments)
2767 break;
2768 found = back_comment (from, from_byte, stop, comnested, comstyle,
2769 &out_charpos, &out_bytepos);
2770 /* FIXME: if found == -1, then it really wasn't a comment-end.
2771 For single-char Sendcomment, we can't do much about it apart
2772 from skipping the char.
2773 For 2-char endcomments, we could try again, taking both
2774 chars as separate entities, but it's a lot of trouble
2775 for very little gain, so we don't bother either. -sm */
2776 if (found != -1)
2777 from = out_charpos, from_byte = out_bytepos;
2778 break;
2780 case Scomment_fence:
2781 case Sstring_fence:
2782 while (1)
2784 if (from == stop)
2785 goto lose;
2786 DEC_BOTH (from, from_byte);
2787 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2788 if (!char_quoted (from, from_byte)
2789 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2790 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
2791 break;
2793 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2794 break;
2796 case Sstring:
2797 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2798 while (1)
2800 if (from == stop)
2801 goto lose;
2802 DEC_BOTH (from, from_byte);
2803 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2804 if (!char_quoted (from, from_byte)
2805 && (stringterm
2806 == (c = FETCH_CHAR_AS_MULTIBYTE (from_byte)))
2807 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2808 break;
2810 if (!depth && sexpflag) goto done2;
2811 break;
2812 default:
2813 /* Ignore whitespace, punctuation, quote, endcomment. */
2814 break;
2818 /* Reached start of buffer. Error if within object, return nil if between */
2819 if (depth)
2820 goto lose;
2822 immediate_quit = 0;
2823 return Qnil;
2825 done2:
2826 count++;
2830 immediate_quit = 0;
2831 XSETFASTINT (val, from);
2832 return val;
2834 lose:
2835 xsignal3 (Qscan_error,
2836 build_string ("Unbalanced parentheses"),
2837 make_number (last_good), make_number (from));
2840 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2841 doc: /* Scan from character number FROM by COUNT lists.
2842 Returns the character number of the position thus found.
2844 If DEPTH is nonzero, paren depth begins counting from that value,
2845 only places where the depth in parentheses becomes zero
2846 are candidates for stopping; COUNT such places are counted.
2847 Thus, a positive value for DEPTH means go out levels.
2849 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2851 If the beginning or end of (the accessible part of) the buffer is reached
2852 and the depth is wrong, an error is signaled.
2853 If the depth is right but the count is not used up, nil is returned. */)
2854 (Lisp_Object from, Lisp_Object count, Lisp_Object depth)
2856 CHECK_NUMBER (from);
2857 CHECK_NUMBER (count);
2858 CHECK_NUMBER (depth);
2860 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2863 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2864 doc: /* Scan from character number FROM by COUNT balanced expressions.
2865 If COUNT is negative, scan backwards.
2866 Returns the character number of the position thus found.
2868 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2870 If the beginning or end of (the accessible part of) the buffer is reached
2871 in the middle of a parenthetical grouping, an error is signaled.
2872 If the beginning or end is reached between groupings
2873 but before count is used up, nil is returned. */)
2874 (Lisp_Object from, Lisp_Object count)
2876 CHECK_NUMBER (from);
2877 CHECK_NUMBER (count);
2879 return scan_lists (XINT (from), XINT (count), 0, 1);
2882 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2883 0, 0, 0,
2884 doc: /* Move point backward over any number of chars with prefix syntax.
2885 This includes chars with "quote" or "prefix" syntax (' or p). */)
2886 (void)
2888 EMACS_INT beg = BEGV;
2889 EMACS_INT opoint = PT;
2890 EMACS_INT opoint_byte = PT_BYTE;
2891 EMACS_INT pos = PT;
2892 EMACS_INT pos_byte = PT_BYTE;
2893 int c;
2895 if (pos <= beg)
2897 SET_PT_BOTH (opoint, opoint_byte);
2899 return Qnil;
2902 SETUP_SYNTAX_TABLE (pos, -1);
2904 DEC_BOTH (pos, pos_byte);
2906 while (!char_quoted (pos, pos_byte)
2907 /* Previous statement updates syntax table. */
2908 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
2909 || SYNTAX_PREFIX (c)))
2911 opoint = pos;
2912 opoint_byte = pos_byte;
2914 if (pos + 1 > beg)
2915 DEC_BOTH (pos, pos_byte);
2918 SET_PT_BOTH (opoint, opoint_byte);
2920 return Qnil;
2923 /* Parse forward from FROM / FROM_BYTE to END,
2924 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2925 and return a description of the state of the parse at END.
2926 If STOPBEFORE is nonzero, stop at the start of an atom.
2927 If COMMENTSTOP is 1, stop at the start of a comment.
2928 If COMMENTSTOP is -1, stop at the start or end of a comment,
2929 after the beginning of a string, or after the end of a string. */
2931 static void
2932 scan_sexps_forward (struct lisp_parse_state *stateptr,
2933 EMACS_INT from, EMACS_INT from_byte, EMACS_INT end,
2934 int targetdepth, int stopbefore,
2935 Lisp_Object oldstate, int commentstop)
2937 struct lisp_parse_state state;
2939 register enum syntaxcode code;
2940 int c1;
2941 int comnested;
2942 struct level { int last, prev; };
2943 struct level levelstart[100];
2944 register struct level *curlevel = levelstart;
2945 struct level *endlevel = levelstart + 100;
2946 register int depth; /* Paren depth of current scanning location.
2947 level - levelstart equals this except
2948 when the depth becomes negative. */
2949 int mindepth; /* Lowest DEPTH value seen. */
2950 int start_quoted = 0; /* Nonzero means starting after a char quote */
2951 Lisp_Object tem;
2952 EMACS_INT prev_from; /* Keep one character before FROM. */
2953 EMACS_INT prev_from_byte;
2954 int prev_from_syntax;
2955 int boundary_stop = commentstop == -1;
2956 int nofence;
2957 int found;
2958 EMACS_INT out_bytepos, out_charpos;
2959 int temp;
2961 prev_from = from;
2962 prev_from_byte = from_byte;
2963 if (from != BEGV)
2964 DEC_BOTH (prev_from, prev_from_byte);
2966 /* Use this macro instead of `from++'. */
2967 #define INC_FROM \
2968 do { prev_from = from; \
2969 prev_from_byte = from_byte; \
2970 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2971 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2972 INC_BOTH (from, from_byte); \
2973 if (from < end) \
2974 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2975 } while (0)
2977 immediate_quit = 1;
2978 QUIT;
2980 if (NILP (oldstate))
2982 depth = 0;
2983 state.instring = -1;
2984 state.incomment = 0;
2985 state.comstyle = 0; /* comment style a by default. */
2986 state.comstr_start = -1; /* no comment/string seen. */
2988 else
2990 tem = Fcar (oldstate);
2991 if (!NILP (tem))
2992 depth = XINT (tem);
2993 else
2994 depth = 0;
2996 oldstate = Fcdr (oldstate);
2997 oldstate = Fcdr (oldstate);
2998 oldstate = Fcdr (oldstate);
2999 tem = Fcar (oldstate);
3000 /* Check whether we are inside string_fence-style string: */
3001 state.instring = (!NILP (tem)
3002 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
3003 : -1);
3005 oldstate = Fcdr (oldstate);
3006 tem = Fcar (oldstate);
3007 state.incomment = (!NILP (tem)
3008 ? (INTEGERP (tem) ? XINT (tem) : -1)
3009 : 0);
3011 oldstate = Fcdr (oldstate);
3012 tem = Fcar (oldstate);
3013 start_quoted = !NILP (tem);
3015 /* if the eighth element of the list is nil, we are in comment
3016 style a. If it is non-nil, we are in comment style b */
3017 oldstate = Fcdr (oldstate);
3018 oldstate = Fcdr (oldstate);
3019 tem = Fcar (oldstate);
3020 state.comstyle = (NILP (tem)
3022 : (EQ (tem, Qsyntax_table)
3023 ? ST_COMMENT_STYLE
3024 : INTEGERP (tem) ? XINT (tem) : 1));
3026 oldstate = Fcdr (oldstate);
3027 tem = Fcar (oldstate);
3028 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
3029 oldstate = Fcdr (oldstate);
3030 tem = Fcar (oldstate);
3031 while (!NILP (tem)) /* >= second enclosing sexps. */
3033 /* curlevel++->last ran into compiler bug on Apollo */
3034 curlevel->last = XINT (Fcar (tem));
3035 if (++curlevel == endlevel)
3036 curlevel--; /* error ("Nesting too deep for parser"); */
3037 curlevel->prev = -1;
3038 curlevel->last = -1;
3039 tem = Fcdr (tem);
3042 state.quoted = 0;
3043 mindepth = depth;
3045 curlevel->prev = -1;
3046 curlevel->last = -1;
3048 SETUP_SYNTAX_TABLE (prev_from, 1);
3049 temp = FETCH_CHAR (prev_from_byte);
3050 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
3051 UPDATE_SYNTAX_TABLE_FORWARD (from);
3053 /* Enter the loop at a place appropriate for initial state. */
3055 if (state.incomment)
3056 goto startincomment;
3057 if (state.instring >= 0)
3059 nofence = state.instring != ST_STRING_STYLE;
3060 if (start_quoted)
3061 goto startquotedinstring;
3062 goto startinstring;
3064 else if (start_quoted)
3065 goto startquoted;
3067 while (from < end)
3069 int syntax;
3070 INC_FROM;
3071 code = prev_from_syntax & 0xff;
3073 if (from < end
3074 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3075 && (c1 = FETCH_CHAR (from_byte),
3076 syntax = SYNTAX_WITH_FLAGS (c1),
3077 SYNTAX_FLAGS_COMSTART_SECOND (syntax)))
3078 /* Duplicate code to avoid a complex if-expression
3079 which causes trouble for the SGI compiler. */
3081 /* Record the comment style we have entered so that only
3082 the comment-end sequence of the same style actually
3083 terminates the comment section. */
3084 state.comstyle
3085 = SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_from_syntax);
3086 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
3087 comnested = comnested || SYNTAX_FLAGS_COMMENT_NESTED (syntax);
3088 state.incomment = comnested ? 1 : -1;
3089 state.comstr_start = prev_from;
3090 INC_FROM;
3091 code = Scomment;
3093 else if (code == Scomment_fence)
3095 /* Record the comment style we have entered so that only
3096 the comment-end sequence of the same style actually
3097 terminates the comment section. */
3098 state.comstyle = ST_COMMENT_STYLE;
3099 state.incomment = -1;
3100 state.comstr_start = prev_from;
3101 code = Scomment;
3103 else if (code == Scomment)
3105 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax, 0);
3106 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3107 1 : -1);
3108 state.comstr_start = prev_from;
3111 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3112 continue;
3113 switch (SWITCH_ENUM_CAST (code))
3115 case Sescape:
3116 case Scharquote:
3117 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3118 curlevel->last = prev_from;
3119 startquoted:
3120 if (from == end) goto endquoted;
3121 INC_FROM;
3122 goto symstarted;
3123 /* treat following character as a word constituent */
3124 case Sword:
3125 case Ssymbol:
3126 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3127 curlevel->last = prev_from;
3128 symstarted:
3129 while (from < end)
3131 /* Some compilers can't handle this inside the switch. */
3132 temp = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3133 temp = SYNTAX (temp);
3134 switch (temp)
3136 case Scharquote:
3137 case Sescape:
3138 INC_FROM;
3139 if (from == end) goto endquoted;
3140 break;
3141 case Sword:
3142 case Ssymbol:
3143 case Squote:
3144 break;
3145 default:
3146 goto symdone;
3148 INC_FROM;
3150 symdone:
3151 curlevel->prev = curlevel->last;
3152 break;
3154 case Scomment_fence: /* Can't happen because it's handled above. */
3155 case Scomment:
3156 if (commentstop || boundary_stop) goto done;
3157 startincomment:
3158 /* The (from == BEGV) test was to enter the loop in the middle so
3159 that we find a 2-char comment ender even if we start in the
3160 middle of it. We don't want to do that if we're just at the
3161 beginning of the comment (think of (*) ... (*)). */
3162 found = forw_comment (from, from_byte, end,
3163 state.incomment, state.comstyle,
3164 (from == BEGV || from < state.comstr_start + 3)
3165 ? 0 : prev_from_syntax,
3166 &out_charpos, &out_bytepos, &state.incomment);
3167 from = out_charpos; from_byte = out_bytepos;
3168 /* Beware! prev_from and friends are invalid now.
3169 Luckily, the `done' doesn't use them and the INC_FROM
3170 sets them to a sane value without looking at them. */
3171 if (!found) goto done;
3172 INC_FROM;
3173 state.incomment = 0;
3174 state.comstyle = 0; /* reset the comment style */
3175 if (boundary_stop) goto done;
3176 break;
3178 case Sopen:
3179 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3180 depth++;
3181 /* curlevel++->last ran into compiler bug on Apollo */
3182 curlevel->last = prev_from;
3183 if (++curlevel == endlevel)
3184 curlevel--; /* error ("Nesting too deep for parser"); */
3185 curlevel->prev = -1;
3186 curlevel->last = -1;
3187 if (targetdepth == depth) goto done;
3188 break;
3190 case Sclose:
3191 depth--;
3192 if (depth < mindepth)
3193 mindepth = depth;
3194 if (curlevel != levelstart)
3195 curlevel--;
3196 curlevel->prev = curlevel->last;
3197 if (targetdepth == depth) goto done;
3198 break;
3200 case Sstring:
3201 case Sstring_fence:
3202 state.comstr_start = from - 1;
3203 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3204 curlevel->last = prev_from;
3205 state.instring = (code == Sstring
3206 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3207 : ST_STRING_STYLE);
3208 if (boundary_stop) goto done;
3209 startinstring:
3211 nofence = state.instring != ST_STRING_STYLE;
3213 while (1)
3215 int c;
3217 if (from >= end) goto done;
3218 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3219 /* Some compilers can't handle this inside the switch. */
3220 temp = SYNTAX (c);
3222 /* Check TEMP here so that if the char has
3223 a syntax-table property which says it is NOT
3224 a string character, it does not end the string. */
3225 if (nofence && c == state.instring && temp == Sstring)
3226 break;
3228 switch (temp)
3230 case Sstring_fence:
3231 if (!nofence) goto string_end;
3232 break;
3233 case Scharquote:
3234 case Sescape:
3235 INC_FROM;
3236 startquotedinstring:
3237 if (from >= end) goto endquoted;
3239 INC_FROM;
3242 string_end:
3243 state.instring = -1;
3244 curlevel->prev = curlevel->last;
3245 INC_FROM;
3246 if (boundary_stop) goto done;
3247 break;
3249 case Smath:
3250 /* FIXME: We should do something with it. */
3251 break;
3252 default:
3253 /* Ignore whitespace, punctuation, quote, endcomment. */
3254 break;
3257 goto done;
3259 stop: /* Here if stopping before start of sexp. */
3260 from = prev_from; /* We have just fetched the char that starts it; */
3261 goto done; /* but return the position before it. */
3263 endquoted:
3264 state.quoted = 1;
3265 done:
3266 state.depth = depth;
3267 state.mindepth = mindepth;
3268 state.thislevelstart = curlevel->prev;
3269 state.prevlevelstart
3270 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3271 state.location = from;
3272 state.levelstarts = Qnil;
3273 while (curlevel > levelstart)
3274 state.levelstarts = Fcons (make_number ((--curlevel)->last),
3275 state.levelstarts);
3276 immediate_quit = 0;
3278 *stateptr = state;
3281 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3282 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3283 Parsing stops at TO or when certain criteria are met;
3284 point is set to where parsing stops.
3285 If fifth arg OLDSTATE is omitted or nil,
3286 parsing assumes that FROM is the beginning of a function.
3287 Value is a list of elements describing final state of parsing:
3288 0. depth in parens.
3289 1. character address of start of innermost containing list; nil if none.
3290 2. character address of start of last complete sexp terminated.
3291 3. non-nil if inside a string.
3292 (it is the character that will terminate the string,
3293 or t if the string should be terminated by a generic string delimiter.)
3294 4. nil if outside a comment, t if inside a non-nestable comment,
3295 else an integer (the current comment nesting).
3296 5. t if following a quote character.
3297 6. the minimum paren-depth encountered during this scan.
3298 7. style of comment, if any.
3299 8. character address of start of comment or string; nil if not in one.
3300 9. Intermediate data for continuation of parsing (subject to change).
3301 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3302 in parentheses becomes equal to TARGETDEPTH.
3303 Fourth arg STOPBEFORE non-nil means stop when come to
3304 any character that starts a sexp.
3305 Fifth arg OLDSTATE is a list like what this function returns.
3306 It is used to initialize the state of the parse. Elements number 1, 2, 6
3307 and 8 are ignored.
3308 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3309 If it is symbol `syntax-table', stop after the start of a comment or a
3310 string, or after end of a comment or a string. */)
3311 (Lisp_Object from, Lisp_Object to, Lisp_Object targetdepth, Lisp_Object stopbefore, Lisp_Object oldstate, Lisp_Object commentstop)
3313 struct lisp_parse_state state;
3314 int target;
3316 if (!NILP (targetdepth))
3318 CHECK_NUMBER (targetdepth);
3319 target = XINT (targetdepth);
3321 else
3322 target = -100000; /* We won't reach this depth */
3324 validate_region (&from, &to);
3325 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3326 XINT (to),
3327 target, !NILP (stopbefore), oldstate,
3328 (NILP (commentstop)
3329 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3331 SET_PT (state.location);
3333 return Fcons (make_number (state.depth),
3334 Fcons (state.prevlevelstart < 0
3335 ? Qnil : make_number (state.prevlevelstart),
3336 Fcons (state.thislevelstart < 0
3337 ? Qnil : make_number (state.thislevelstart),
3338 Fcons (state.instring >= 0
3339 ? (state.instring == ST_STRING_STYLE
3340 ? Qt : make_number (state.instring)) : Qnil,
3341 Fcons (state.incomment < 0 ? Qt :
3342 (state.incomment == 0 ? Qnil :
3343 make_number (state.incomment)),
3344 Fcons (state.quoted ? Qt : Qnil,
3345 Fcons (make_number (state.mindepth),
3346 Fcons ((state.comstyle
3347 ? (state.comstyle == ST_COMMENT_STYLE
3348 ? Qsyntax_table
3349 : make_number (state.comstyle))
3350 : Qnil),
3351 Fcons (((state.incomment
3352 || (state.instring >= 0))
3353 ? make_number (state.comstr_start)
3354 : Qnil),
3355 Fcons (state.levelstarts, Qnil))))))))));
3358 void
3359 init_syntax_once (void)
3361 register int i, c;
3362 Lisp_Object temp;
3364 /* This has to be done here, before we call Fmake_char_table. */
3365 Qsyntax_table = intern_c_string ("syntax-table");
3366 staticpro (&Qsyntax_table);
3368 /* Intern_C_String this now in case it isn't already done.
3369 Setting this variable twice is harmless.
3370 But don't staticpro it here--that is done in alloc.c. */
3371 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
3373 /* Create objects which can be shared among syntax tables. */
3374 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
3375 for (i = 0; i < ASIZE (Vsyntax_code_object); i++)
3376 XVECTOR (Vsyntax_code_object)->contents[i]
3377 = Fcons (make_number (i), Qnil);
3379 /* Now we are ready to set up this property, so we can
3380 create syntax tables. */
3381 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3383 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3385 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3387 /* Control characters should not be whitespace. */
3388 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3389 for (i = 0; i <= ' ' - 1; i++)
3390 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3391 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3393 /* Except that a few really are whitespace. */
3394 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3395 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3396 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3397 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3398 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3399 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3401 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3402 for (i = 'a'; i <= 'z'; i++)
3403 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3404 for (i = 'A'; i <= 'Z'; i++)
3405 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3406 for (i = '0'; i <= '9'; i++)
3407 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3409 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3410 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3412 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3413 Fcons (make_number (Sopen), make_number (')')));
3414 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3415 Fcons (make_number (Sclose), make_number ('(')));
3416 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3417 Fcons (make_number (Sopen), make_number (']')));
3418 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3419 Fcons (make_number (Sclose), make_number ('[')));
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 ((int) Sstring), Qnil));
3426 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3427 Fcons (make_number ((int) Sescape), Qnil));
3429 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
3430 for (i = 0; i < 10; i++)
3432 c = "_-+*/&|<>="[i];
3433 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3436 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3437 for (i = 0; i < 12; i++)
3439 c = ".,;:?!#@~^'`"[i];
3440 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3443 /* All multibyte characters have syntax `word' by default. */
3444 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3445 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3448 void
3449 syms_of_syntax (void)
3451 Qsyntax_table_p = intern_c_string ("syntax-table-p");
3452 staticpro (&Qsyntax_table_p);
3454 staticpro (&Vsyntax_code_object);
3456 staticpro (&gl_state.object);
3457 staticpro (&gl_state.global_code);
3458 staticpro (&gl_state.current_syntax_table);
3459 staticpro (&gl_state.old_prop);
3461 /* Defined in regex.c */
3462 staticpro (&re_match_object);
3464 Qscan_error = intern_c_string ("scan-error");
3465 staticpro (&Qscan_error);
3466 Fput (Qscan_error, Qerror_conditions,
3467 pure_cons (Qscan_error, pure_cons (Qerror, Qnil)));
3468 Fput (Qscan_error, Qerror_message,
3469 make_pure_c_string ("Scan error"));
3471 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments,
3472 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3474 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties,
3475 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3476 Otherwise, that text property is simply ignored.
3477 See the info node `(elisp)Syntax Properties' for a description of the
3478 `syntax-table' property. */);
3480 words_include_escapes = 0;
3481 DEFVAR_BOOL ("words-include-escapes", words_include_escapes,
3482 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3484 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol,
3485 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3486 multibyte_syntax_as_symbol = 0;
3488 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3489 open_paren_in_column_0_is_defun_start,
3490 doc: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3491 open_paren_in_column_0_is_defun_start = 1;
3494 DEFVAR_LISP ("find-word-boundary-function-table",
3495 Vfind_word_boundary_function_table,
3496 doc: /*
3497 Char table of functions to search for the word boundary.
3498 Each function is called with two arguments; POS and LIMIT.
3499 POS and LIMIT are character positions in the current buffer.
3501 If POS is less than LIMIT, POS is at the first character of a word,
3502 and the return value of a function is a position after the last
3503 character of that word.
3505 If POS is not less than LIMIT, POS is at the last character of a word,
3506 and the return value of a function is a position at the first
3507 character of that word.
3509 In both cases, LIMIT bounds the search. */);
3510 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3512 defsubr (&Ssyntax_table_p);
3513 defsubr (&Ssyntax_table);
3514 defsubr (&Sstandard_syntax_table);
3515 defsubr (&Scopy_syntax_table);
3516 defsubr (&Sset_syntax_table);
3517 defsubr (&Schar_syntax);
3518 defsubr (&Smatching_paren);
3519 defsubr (&Sstring_to_syntax);
3520 defsubr (&Smodify_syntax_entry);
3521 defsubr (&Sinternal_describe_syntax_value);
3523 defsubr (&Sforward_word);
3525 defsubr (&Sskip_chars_forward);
3526 defsubr (&Sskip_chars_backward);
3527 defsubr (&Sskip_syntax_forward);
3528 defsubr (&Sskip_syntax_backward);
3530 defsubr (&Sforward_comment);
3531 defsubr (&Sscan_lists);
3532 defsubr (&Sscan_sexps);
3533 defsubr (&Sbackward_prefix_chars);
3534 defsubr (&Sparse_partial_sexp);