Merge from trunk.
[emacs.git] / src / syntax.c
blob3e51099794bc3550bc85b4dad7a12457fdccf618
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 = BEGV;
402 find_start_value_byte = BEGV_BYTE;
403 find_start_buffer = current_buffer;
404 find_start_modiff = MODIFF;
405 find_start_begv = BEGV;
406 find_start_pos = pos;
407 return BEGV;
410 /* Use previous finding, if it's valid and applies to this inquiry. */
411 if (current_buffer == find_start_buffer
412 /* Reuse the defun-start even if POS is a little farther on.
413 POS might be in the next defun, but that's ok.
414 Our value may not be the best possible, but will still be usable. */
415 && pos <= find_start_pos + 1000
416 && pos >= find_start_value
417 && BEGV == find_start_begv
418 && MODIFF == find_start_modiff)
419 return find_start_value;
421 /* Back up to start of line. */
422 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
424 /* We optimize syntax-table lookup for rare updates. Thus we accept
425 only those `^\s(' which are good in global _and_ text-property
426 syntax-tables. */
427 SETUP_BUFFER_SYNTAX_TABLE ();
428 while (PT > BEGV)
430 int c;
432 /* Open-paren at start of line means we may have found our
433 defun-start. */
434 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
435 if (SYNTAX (c) == Sopen)
437 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
438 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
439 if (SYNTAX (c) == Sopen)
440 break;
441 /* Now fallback to the default value. */
442 SETUP_BUFFER_SYNTAX_TABLE ();
444 /* Move to beg of previous line. */
445 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
448 /* Record what we found, for the next try. */
449 find_start_value = PT;
450 find_start_value_byte = PT_BYTE;
451 find_start_buffer = current_buffer;
452 find_start_modiff = MODIFF;
453 find_start_begv = BEGV;
454 find_start_pos = pos;
456 TEMP_SET_PT_BOTH (opoint, opoint_byte);
458 return find_start_value;
461 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
463 static int
464 prev_char_comend_first (EMACS_INT pos, EMACS_INT pos_byte)
466 int c, val;
468 DEC_BOTH (pos, pos_byte);
469 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
470 c = FETCH_CHAR (pos_byte);
471 val = SYNTAX_COMEND_FIRST (c);
472 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
473 return val;
476 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
478 /* static int
479 * prev_char_comstart_first (pos, pos_byte)
480 * int pos, pos_byte;
482 * int c, val;
484 * DEC_BOTH (pos, pos_byte);
485 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
486 * c = FETCH_CHAR (pos_byte);
487 * val = SYNTAX_COMSTART_FIRST (c);
488 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
489 * return val;
490 * } */
492 /* Checks whether charpos FROM is at the end of a comment.
493 FROM_BYTE is the bytepos corresponding to FROM.
494 Do not move back before STOP.
496 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
497 return -1 otherwise.
499 If successful, store the charpos of the comment's beginning
500 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
502 Global syntax data remains valid for backward search starting at
503 the returned value (or at FROM, if the search was not successful). */
505 static int
506 back_comment (EMACS_INT from, EMACS_INT from_byte, EMACS_INT stop, int comnested, int comstyle, EMACS_INT *charpos_ptr, EMACS_INT *bytepos_ptr)
508 /* Look back, counting the parity of string-quotes,
509 and recording the comment-starters seen.
510 When we reach a safe place, assume that's not in a string;
511 then step the main scan to the earliest comment-starter seen
512 an even number of string quotes away from the safe place.
514 OFROM[I] is position of the earliest comment-starter seen
515 which is I+2X quotes from the comment-end.
516 PARITY is current parity of quotes from the comment end. */
517 int string_style = -1; /* Presumed outside of any string. */
518 int string_lossage = 0;
519 /* Not a real lossage: indicates that we have passed a matching comment
520 starter plus a non-matching comment-ender, meaning that any matching
521 comment-starter we might see later could be a false positive (hidden
522 inside another comment).
523 Test case: { a (* b } c (* d *) */
524 int comment_lossage = 0;
525 EMACS_INT comment_end = from;
526 EMACS_INT comment_end_byte = from_byte;
527 EMACS_INT comstart_pos = 0;
528 EMACS_INT comstart_byte IF_LINT (= 0);
529 /* Place where the containing defun starts,
530 or 0 if we didn't come across it yet. */
531 EMACS_INT defun_start = 0;
532 EMACS_INT defun_start_byte = 0;
533 register enum syntaxcode code;
534 int nesting = 1; /* current comment nesting */
535 int c;
536 int syntax = 0;
538 /* FIXME: A }} comment-ender style leads to incorrect behavior
539 in the case of {{ c }}} because we ignore the last two chars which are
540 assumed to be comment-enders although they aren't. */
542 /* At beginning of range to scan, we're outside of strings;
543 that determines quote parity to the comment-end. */
544 while (from != stop)
546 EMACS_INT temp_byte;
547 int prev_syntax, com2start, com2end;
548 int comstart;
550 /* Move back and examine a character. */
551 DEC_BOTH (from, from_byte);
552 UPDATE_SYNTAX_TABLE_BACKWARD (from);
554 prev_syntax = syntax;
555 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
556 syntax = SYNTAX_WITH_FLAGS (c);
557 code = SYNTAX (c);
559 /* Check for 2-char comment markers. */
560 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
561 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
562 && (comstyle
563 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax, syntax))
564 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
565 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
566 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
567 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
568 comstart = (com2start || code == Scomment);
570 /* Nasty cases with overlapping 2-char comment markers:
571 - snmp-mode: -- c -- foo -- c --
572 --- c --
573 ------ c --
574 - c-mode: *||*
575 |* *|* *|
576 |*| |* |*|
577 /// */
579 /* If a 2-char comment sequence partly overlaps with another,
580 we don't try to be clever. E.g. |*| in C, or }% in modes that
581 have %..\n and %{..}%. */
582 if (from > stop && (com2end || comstart))
584 EMACS_INT next = from, next_byte = from_byte;
585 int next_c, next_syntax;
586 DEC_BOTH (next, next_byte);
587 UPDATE_SYNTAX_TABLE_BACKWARD (next);
588 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
589 next_syntax = SYNTAX_WITH_FLAGS (next_c);
590 if (((comstart || comnested)
591 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
592 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
593 || ((com2end || comnested)
594 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
595 && (comstyle
596 == SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_syntax))
597 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
598 goto lossage;
599 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
602 if (com2start && comstart_pos == 0)
603 /* We're looking at a comment starter. But it might be a comment
604 ender as well (see snmp-mode). The first time we see one, we
605 need to consider it as a comment starter,
606 and the subsequent times as a comment ender. */
607 com2end = 0;
609 /* Turn a 2-char comment sequences into the appropriate syntax. */
610 if (com2end)
611 code = Sendcomment;
612 else if (com2start)
613 code = Scomment;
614 /* Ignore comment starters of a different style. */
615 else if (code == Scomment
616 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0)
617 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
618 continue;
620 /* Ignore escaped characters, except comment-enders. */
621 if (code != Sendcomment && char_quoted (from, from_byte))
622 continue;
624 switch (code)
626 case Sstring_fence:
627 case Scomment_fence:
628 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
629 case Sstring:
630 /* Track parity of quotes. */
631 if (string_style == -1)
632 /* Entering a string. */
633 string_style = c;
634 else if (string_style == c)
635 /* Leaving the string. */
636 string_style = -1;
637 else
638 /* If we have two kinds of string delimiters.
639 There's no way to grok this scanning backwards. */
640 string_lossage = 1;
641 break;
643 case Scomment:
644 /* We've already checked that it is the relevant comstyle. */
645 if (string_style != -1 || comment_lossage || string_lossage)
646 /* There are odd string quotes involved, so let's be careful.
647 Test case in Pascal: " { " a { " } */
648 goto lossage;
650 if (!comnested)
652 /* Record best comment-starter so far. */
653 comstart_pos = from;
654 comstart_byte = from_byte;
656 else if (--nesting <= 0)
657 /* nested comments have to be balanced, so we don't need to
658 keep looking for earlier ones. We use here the same (slightly
659 incorrect) reasoning as below: since it is followed by uniform
660 paired string quotes, this comment-start has to be outside of
661 strings, else the comment-end itself would be inside a string. */
662 goto done;
663 break;
665 case Sendcomment:
666 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == comstyle
667 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
668 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
669 /* This is the same style of comment ender as ours. */
671 if (comnested)
672 nesting++;
673 else
674 /* Anything before that can't count because it would match
675 this comment-ender rather than ours. */
676 from = stop; /* Break out of the loop. */
678 else if (comstart_pos != 0 || c != '\n')
679 /* We're mixing comment styles here, so we'd better be careful.
680 The (comstart_pos != 0 || c != '\n') check is not quite correct
681 (we should just always set comment_lossage), but removing it
682 would imply that any multiline comment in C would go through
683 lossage, which seems overkill.
684 The failure should only happen in the rare cases such as
685 { (* } *) */
686 comment_lossage = 1;
687 break;
689 case Sopen:
690 /* Assume a defun-start point is outside of strings. */
691 if (open_paren_in_column_0_is_defun_start
692 && (from == stop
693 || (temp_byte = dec_bytepos (from_byte),
694 FETCH_CHAR (temp_byte) == '\n')))
696 defun_start = from;
697 defun_start_byte = from_byte;
698 from = stop; /* Break out of the loop. */
700 break;
702 default:
703 break;
707 if (comstart_pos == 0)
709 from = comment_end;
710 from_byte = comment_end_byte;
711 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
713 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
714 or `done'), then we've found the beginning of the non-nested comment. */
715 else if (1) /* !comnested */
717 from = comstart_pos;
718 from_byte = comstart_byte;
719 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
721 else
723 struct lisp_parse_state state;
724 lossage:
725 /* We had two kinds of string delimiters mixed up
726 together. Decode this going forwards.
727 Scan fwd from a known safe place (beginning-of-defun)
728 to the one in question; this records where we
729 last passed a comment starter. */
730 /* If we did not already find the defun start, find it now. */
731 if (defun_start == 0)
733 defun_start = find_defun_start (comment_end, comment_end_byte);
734 defun_start_byte = find_start_value_byte;
738 scan_sexps_forward (&state,
739 defun_start, defun_start_byte,
740 comment_end, -10000, 0, Qnil, 0);
741 defun_start = comment_end;
742 if (state.incomment == (comnested ? 1 : -1)
743 && state.comstyle == comstyle)
744 from = state.comstr_start;
745 else
747 from = comment_end;
748 if (state.incomment)
749 /* If comment_end is inside some other comment, maybe ours
750 is nested, so we need to try again from within the
751 surrounding comment. Example: { a (* " *) */
753 /* FIXME: We should advance by one or two chars. */
754 defun_start = state.comstr_start + 2;
755 defun_start_byte = CHAR_TO_BYTE (defun_start);
758 } while (defun_start < comment_end);
760 from_byte = CHAR_TO_BYTE (from);
761 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
764 done:
765 *charpos_ptr = from;
766 *bytepos_ptr = from_byte;
768 return (from == comment_end) ? -1 : from;
771 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
772 doc: /* Return t if OBJECT is a syntax table.
773 Currently, any char-table counts as a syntax table. */)
774 (Lisp_Object object)
776 if (CHAR_TABLE_P (object)
777 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
778 return Qt;
779 return Qnil;
782 static void
783 check_syntax_table (Lisp_Object obj)
785 CHECK_TYPE (CHAR_TABLE_P (obj) && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table),
786 Qsyntax_table_p, obj);
789 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
790 doc: /* Return the current syntax table.
791 This is the one specified by the current buffer. */)
792 (void)
794 return BVAR (current_buffer, syntax_table);
797 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
798 Sstandard_syntax_table, 0, 0, 0,
799 doc: /* Return the standard syntax table.
800 This is the one used for new buffers. */)
801 (void)
803 return Vstandard_syntax_table;
806 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
807 doc: /* Construct a new syntax table and return it.
808 It is a copy of the TABLE, which defaults to the standard syntax table. */)
809 (Lisp_Object table)
811 Lisp_Object copy;
813 if (!NILP (table))
814 check_syntax_table (table);
815 else
816 table = Vstandard_syntax_table;
818 copy = Fcopy_sequence (table);
820 /* Only the standard syntax table should have a default element.
821 Other syntax tables should inherit from parents instead. */
822 XCHAR_TABLE (copy)->defalt = Qnil;
824 /* Copied syntax tables should all have parents.
825 If we copied one with no parent, such as the standard syntax table,
826 use the standard syntax table as the copy's parent. */
827 if (NILP (XCHAR_TABLE (copy)->parent))
828 Fset_char_table_parent (copy, Vstandard_syntax_table);
829 return copy;
832 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
833 doc: /* Select a new syntax table for the current buffer.
834 One argument, a syntax table. */)
835 (Lisp_Object table)
837 int idx;
838 check_syntax_table (table);
839 BVAR (current_buffer, syntax_table) = table;
840 /* Indicate that this buffer now has a specified syntax table. */
841 idx = PER_BUFFER_VAR_IDX (syntax_table);
842 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
843 return table;
846 /* Convert a letter which signifies a syntax code
847 into the code it signifies.
848 This is used by modify-syntax-entry, and other things. */
850 unsigned char syntax_spec_code[0400] =
851 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
852 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
853 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
854 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
855 (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377,
856 (char) Smath, 0377, 0377, (char) Squote,
857 (char) Sopen, (char) Sclose, 0377, 0377,
858 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
859 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
860 0377, 0377, 0377, 0377,
861 (char) Scomment, 0377, (char) Sendcomment, 0377,
862 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
863 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
864 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
865 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
866 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
867 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
868 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
869 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377
872 /* Indexed by syntax code, give the letter that describes it. */
874 char syntax_code_spec[16] =
876 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
877 '!', '|'
880 /* Indexed by syntax code, give the object (cons of syntax code and
881 nil) to be stored in syntax table. Since these objects can be
882 shared among syntax tables, we generate them in advance. By
883 sharing objects, the function `describe-syntax' can give a more
884 compact listing. */
885 static Lisp_Object Vsyntax_code_object;
888 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
889 doc: /* Return the syntax code of CHARACTER, described by a character.
890 For example, if CHARACTER is a word constituent, the
891 character `w' (119) is returned.
892 The characters that correspond to various syntax codes
893 are listed in the documentation of `modify-syntax-entry'. */)
894 (Lisp_Object character)
896 int char_int;
897 CHECK_CHARACTER (character);
898 char_int = XINT (character);
899 SETUP_BUFFER_SYNTAX_TABLE ();
900 return make_number (syntax_code_spec[(int) SYNTAX (char_int)]);
903 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
904 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
905 (Lisp_Object character)
907 int char_int, code;
908 CHECK_NUMBER (character);
909 char_int = XINT (character);
910 SETUP_BUFFER_SYNTAX_TABLE ();
911 code = SYNTAX (char_int);
912 if (code == Sopen || code == Sclose)
913 return SYNTAX_MATCH (char_int);
914 return Qnil;
917 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
918 doc: /* Convert a syntax specification STRING into syntax cell form.
919 STRING should be a string as it is allowed as argument of
920 `modify-syntax-entry'. Value is the equivalent cons cell
921 \(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
922 text property. */)
923 (Lisp_Object string)
925 register const unsigned char *p;
926 register enum syntaxcode code;
927 int val;
928 Lisp_Object match;
930 CHECK_STRING (string);
932 p = SDATA (string);
933 code = (enum syntaxcode) syntax_spec_code[*p++];
934 if (((int) code & 0377) == 0377)
935 error ("Invalid syntax description letter: %c", p[-1]);
937 if (code == Sinherit)
938 return Qnil;
940 if (*p)
942 int len;
943 int character = STRING_CHAR_AND_LENGTH (p, len);
944 XSETINT (match, character);
945 if (XFASTINT (match) == ' ')
946 match = Qnil;
947 p += len;
949 else
950 match = Qnil;
952 val = (int) code;
953 while (*p)
954 switch (*p++)
956 case '1':
957 val |= 1 << 16;
958 break;
960 case '2':
961 val |= 1 << 17;
962 break;
964 case '3':
965 val |= 1 << 18;
966 break;
968 case '4':
969 val |= 1 << 19;
970 break;
972 case 'p':
973 val |= 1 << 20;
974 break;
976 case 'b':
977 val |= 1 << 21;
978 break;
980 case 'n':
981 val |= 1 << 22;
982 break;
984 case 'c':
985 val |= 1 << 23;
986 break;
989 if (val < ASIZE (Vsyntax_code_object) && NILP (match))
990 return XVECTOR (Vsyntax_code_object)->contents[val];
991 else
992 /* Since we can't use a shared object, let's make a new one. */
993 return Fcons (make_number (val), match);
996 /* I really don't know why this is interactive
997 help-form should at least be made useful whilst reading the second arg. */
998 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
999 "cSet syntax for character: \nsSet syntax for %s to: ",
1000 doc: /* Set syntax for character CHAR according to string NEWENTRY.
1001 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1002 the current buffer's syntax table.
1003 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1004 in the range MIN to MAX are changed.
1005 The first character of NEWENTRY should be one of the following:
1006 Space or - whitespace syntax. w word constituent.
1007 _ symbol constituent. . punctuation.
1008 ( open-parenthesis. ) close-parenthesis.
1009 " string quote. \\ escape.
1010 $ paired delimiter. ' expression quote or prefix operator.
1011 < comment starter. > comment ender.
1012 / character-quote. @ inherit from `standard-syntax-table'.
1013 | generic string fence. ! generic comment fence.
1015 Only single-character comment start and end sequences are represented thus.
1016 Two-character sequences are represented as described below.
1017 The second character of NEWENTRY is the matching parenthesis,
1018 used only if the first character is `(' or `)'.
1019 Any additional characters are flags.
1020 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1021 1 means CHAR is the start of a two-char comment start sequence.
1022 2 means CHAR is the second character of such a sequence.
1023 3 means CHAR is the start of a two-char comment end sequence.
1024 4 means CHAR is the second character of such a sequence.
1026 There can be several orthogonal comment sequences. This is to support
1027 language modes such as C++. By default, all comment sequences are of style
1028 a, but you can set the comment sequence style to b (on the second character
1029 of a comment-start, and the first character of a comment-end sequence) and/or
1030 c (on any of its chars) using this flag:
1031 b means CHAR is part of comment sequence b.
1032 c means CHAR is part of comment sequence c.
1033 n means CHAR is part of a nestable comment sequence.
1035 p means CHAR is a prefix character for `backward-prefix-chars';
1036 such characters are treated as whitespace when they occur
1037 between expressions.
1038 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1039 (Lisp_Object c, Lisp_Object newentry, Lisp_Object syntax_table)
1041 if (CONSP (c))
1043 CHECK_CHARACTER_CAR (c);
1044 CHECK_CHARACTER_CDR (c);
1046 else
1047 CHECK_CHARACTER (c);
1049 if (NILP (syntax_table))
1050 syntax_table = BVAR (current_buffer, syntax_table);
1051 else
1052 check_syntax_table (syntax_table);
1054 newentry = Fstring_to_syntax (newentry);
1055 if (CONSP (c))
1056 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1057 else
1058 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
1060 /* We clear the regexp cache, since character classes can now have
1061 different values from those in the compiled regexps.*/
1062 clear_regexp_cache ();
1064 return Qnil;
1067 /* Dump syntax table to buffer in human-readable format */
1069 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1070 Sinternal_describe_syntax_value, 1, 1, 0,
1071 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1072 (Lisp_Object syntax)
1074 register enum syntaxcode code;
1075 int syntax_code;
1076 char desc, start1, start2, end1, end2, prefix,
1077 comstyleb, comstylec, comnested;
1078 char str[2];
1079 Lisp_Object first, match_lisp, value = syntax;
1081 if (NILP (value))
1083 insert_string ("default");
1084 return syntax;
1087 if (CHAR_TABLE_P (value))
1089 insert_string ("deeper char-table ...");
1090 return syntax;
1093 if (!CONSP (value))
1095 insert_string ("invalid");
1096 return syntax;
1099 first = XCAR (value);
1100 match_lisp = XCDR (value);
1102 if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp)))
1104 insert_string ("invalid");
1105 return syntax;
1108 syntax_code = XINT (first);
1109 code = (enum syntaxcode) (syntax_code & 0377);
1110 start1 = SYNTAX_FLAGS_COMSTART_FIRST (syntax_code);
1111 start2 = SYNTAX_FLAGS_COMSTART_SECOND (syntax_code);;
1112 end1 = SYNTAX_FLAGS_COMEND_FIRST (syntax_code);
1113 end2 = SYNTAX_FLAGS_COMEND_SECOND (syntax_code);
1114 prefix = SYNTAX_FLAGS_PREFIX (syntax_code);
1115 comstyleb = SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code);
1116 comstylec = SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code);
1117 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax_code);
1119 if ((int) code < 0 || (int) code >= (int) Smax)
1121 insert_string ("invalid");
1122 return syntax;
1124 desc = syntax_code_spec[(int) code];
1126 str[0] = desc, str[1] = 0;
1127 insert (str, 1);
1129 if (NILP (match_lisp))
1130 insert (" ", 1);
1131 else
1132 insert_char (XINT (match_lisp));
1134 if (start1)
1135 insert ("1", 1);
1136 if (start2)
1137 insert ("2", 1);
1139 if (end1)
1140 insert ("3", 1);
1141 if (end2)
1142 insert ("4", 1);
1144 if (prefix)
1145 insert ("p", 1);
1146 if (comstyleb)
1147 insert ("b", 1);
1148 if (comstylec)
1149 insert ("c", 1);
1150 if (comnested)
1151 insert ("n", 1);
1153 insert_string ("\twhich means: ");
1155 switch (SWITCH_ENUM_CAST (code))
1157 case Swhitespace:
1158 insert_string ("whitespace"); break;
1159 case Spunct:
1160 insert_string ("punctuation"); break;
1161 case Sword:
1162 insert_string ("word"); break;
1163 case Ssymbol:
1164 insert_string ("symbol"); break;
1165 case Sopen:
1166 insert_string ("open"); break;
1167 case Sclose:
1168 insert_string ("close"); break;
1169 case Squote:
1170 insert_string ("prefix"); break;
1171 case Sstring:
1172 insert_string ("string"); break;
1173 case Smath:
1174 insert_string ("math"); break;
1175 case Sescape:
1176 insert_string ("escape"); break;
1177 case Scharquote:
1178 insert_string ("charquote"); break;
1179 case Scomment:
1180 insert_string ("comment"); break;
1181 case Sendcomment:
1182 insert_string ("endcomment"); break;
1183 case Sinherit:
1184 insert_string ("inherit"); break;
1185 case Scomment_fence:
1186 insert_string ("comment fence"); break;
1187 case Sstring_fence:
1188 insert_string ("string fence"); break;
1189 default:
1190 insert_string ("invalid");
1191 return syntax;
1194 if (!NILP (match_lisp))
1196 insert_string (", matches ");
1197 insert_char (XINT (match_lisp));
1200 if (start1)
1201 insert_string (",\n\t is the first character of a comment-start sequence");
1202 if (start2)
1203 insert_string (",\n\t is the second character of a comment-start sequence");
1205 if (end1)
1206 insert_string (",\n\t is the first character of a comment-end sequence");
1207 if (end2)
1208 insert_string (",\n\t is the second character of a comment-end sequence");
1209 if (comstyleb)
1210 insert_string (" (comment style b)");
1211 if (comstylec)
1212 insert_string (" (comment style c)");
1213 if (comnested)
1214 insert_string (" (nestable)");
1216 if (prefix)
1217 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1219 return syntax;
1222 /* Return the position across COUNT words from FROM.
1223 If that many words cannot be found before the end of the buffer, return 0.
1224 COUNT negative means scan backward and stop at word beginning. */
1226 EMACS_INT
1227 scan_words (register EMACS_INT from, register EMACS_INT count)
1229 register EMACS_INT beg = BEGV;
1230 register EMACS_INT end = ZV;
1231 register EMACS_INT from_byte = CHAR_TO_BYTE (from);
1232 register enum syntaxcode code;
1233 int ch0, ch1;
1234 Lisp_Object func, pos;
1236 immediate_quit = 1;
1237 QUIT;
1239 SETUP_SYNTAX_TABLE (from, count);
1241 while (count > 0)
1243 while (1)
1245 if (from == end)
1247 immediate_quit = 0;
1248 return 0;
1250 UPDATE_SYNTAX_TABLE_FORWARD (from);
1251 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1252 code = SYNTAX (ch0);
1253 INC_BOTH (from, from_byte);
1254 if (words_include_escapes
1255 && (code == Sescape || code == Scharquote))
1256 break;
1257 if (code == Sword)
1258 break;
1260 /* Now CH0 is a character which begins a word and FROM is the
1261 position of the next character. */
1262 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
1263 if (! NILP (Ffboundp (func)))
1265 pos = call2 (func, make_number (from - 1), make_number (end));
1266 if (INTEGERP (pos) && XINT (pos) > from)
1268 from = XINT (pos);
1269 from_byte = CHAR_TO_BYTE (from);
1272 else
1274 while (1)
1276 if (from == end) break;
1277 UPDATE_SYNTAX_TABLE_FORWARD (from);
1278 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1279 code = SYNTAX (ch1);
1280 if ((code != Sword
1281 && (! words_include_escapes
1282 || (code != Sescape && code != Scharquote)))
1283 || word_boundary_p (ch0, ch1))
1284 break;
1285 INC_BOTH (from, from_byte);
1286 ch0 = ch1;
1289 count--;
1291 while (count < 0)
1293 while (1)
1295 if (from == beg)
1297 immediate_quit = 0;
1298 return 0;
1300 DEC_BOTH (from, from_byte);
1301 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1302 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1303 code = SYNTAX (ch1);
1304 if (words_include_escapes
1305 && (code == Sescape || code == Scharquote))
1306 break;
1307 if (code == Sword)
1308 break;
1310 /* Now CH1 is a character which ends a word and FROM is the
1311 position of it. */
1312 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
1313 if (! NILP (Ffboundp (func)))
1315 pos = call2 (func, make_number (from), make_number (beg));
1316 if (INTEGERP (pos) && XINT (pos) < from)
1318 from = XINT (pos);
1319 from_byte = CHAR_TO_BYTE (from);
1322 else
1324 while (1)
1326 if (from == beg)
1327 break;
1328 DEC_BOTH (from, from_byte);
1329 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1330 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1331 code = SYNTAX (ch0);
1332 if ((code != Sword
1333 && (! words_include_escapes
1334 || (code != Sescape && code != Scharquote)))
1335 || word_boundary_p (ch0, ch1))
1337 INC_BOTH (from, from_byte);
1338 break;
1340 ch1 = ch0;
1343 count++;
1346 immediate_quit = 0;
1348 return from;
1351 DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p",
1352 doc: /* Move point forward ARG words (backward if ARG is negative).
1353 Normally returns t.
1354 If an edge of the buffer or a field boundary is reached, point is left there
1355 and the function returns nil. Field boundaries are not noticed if
1356 `inhibit-field-text-motion' is non-nil. */)
1357 (Lisp_Object arg)
1359 Lisp_Object tmp;
1360 int orig_val, val;
1362 if (NILP (arg))
1363 XSETFASTINT (arg, 1);
1364 else
1365 CHECK_NUMBER (arg);
1367 val = orig_val = scan_words (PT, XINT (arg));
1368 if (! orig_val)
1369 val = XINT (arg) > 0 ? ZV : BEGV;
1371 /* Avoid jumping out of an input field. */
1372 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1373 Qt, Qnil, Qnil);
1374 val = XFASTINT (tmp);
1376 SET_PT (val);
1377 return val == orig_val ? Qt : Qnil;
1380 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1381 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1382 STRING is like the inside of a `[...]' in a regular expression
1383 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1384 (but not at the end of a range; quoting is never needed there).
1385 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1386 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1387 Char classes, e.g. `[:alpha:]', are supported.
1389 Returns the distance traveled, either zero or positive. */)
1390 (Lisp_Object string, Lisp_Object lim)
1392 return skip_chars (1, string, lim, 1);
1395 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1396 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1397 See `skip-chars-forward' for details.
1398 Returns the distance traveled, either zero or negative. */)
1399 (Lisp_Object string, Lisp_Object lim)
1401 return skip_chars (0, string, lim, 1);
1404 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1405 doc: /* Move point forward across chars in specified syntax classes.
1406 SYNTAX is a string of syntax code characters.
1407 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1408 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1409 This function returns the distance traveled, either zero or positive. */)
1410 (Lisp_Object syntax, Lisp_Object lim)
1412 return skip_syntaxes (1, syntax, lim);
1415 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1416 doc: /* Move point backward across chars in specified syntax classes.
1417 SYNTAX is a string of syntax code characters.
1418 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1419 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1420 This function returns the distance traveled, either zero or negative. */)
1421 (Lisp_Object syntax, Lisp_Object lim)
1423 return skip_syntaxes (0, syntax, lim);
1426 static Lisp_Object
1427 skip_chars (int forwardp, Lisp_Object string, Lisp_Object lim, int handle_iso_classes)
1429 register unsigned int c;
1430 unsigned char fastmap[0400];
1431 /* Store the ranges of non-ASCII characters. */
1432 int *char_ranges IF_LINT (= NULL);
1433 int n_char_ranges = 0;
1434 int negate = 0;
1435 register EMACS_INT i, i_byte;
1436 /* Set to 1 if the current buffer is multibyte and the region
1437 contains non-ASCII chars. */
1438 int multibyte;
1439 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1440 chars. */
1441 int string_multibyte;
1442 EMACS_INT size_byte;
1443 const unsigned char *str;
1444 int len;
1445 Lisp_Object iso_classes;
1447 CHECK_STRING (string);
1448 iso_classes = Qnil;
1450 if (NILP (lim))
1451 XSETINT (lim, forwardp ? ZV : BEGV);
1452 else
1453 CHECK_NUMBER_COERCE_MARKER (lim);
1455 /* In any case, don't allow scan outside bounds of buffer. */
1456 if (XINT (lim) > ZV)
1457 XSETFASTINT (lim, ZV);
1458 if (XINT (lim) < BEGV)
1459 XSETFASTINT (lim, BEGV);
1461 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1462 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1463 string_multibyte = SBYTES (string) > SCHARS (string);
1465 memset (fastmap, 0, sizeof fastmap);
1467 str = SDATA (string);
1468 size_byte = SBYTES (string);
1470 i_byte = 0;
1471 if (i_byte < size_byte
1472 && SREF (string, 0) == '^')
1474 negate = 1; i_byte++;
1477 /* Find the characters specified and set their elements of fastmap.
1478 Handle backslashes and ranges specially.
1480 If STRING contains non-ASCII characters, setup char_ranges for
1481 them and use fastmap only for their leading codes. */
1483 if (! string_multibyte)
1485 int string_has_eight_bit = 0;
1487 /* At first setup fastmap. */
1488 while (i_byte < size_byte)
1490 c = str[i_byte++];
1492 if (handle_iso_classes && c == '['
1493 && i_byte < size_byte
1494 && str[i_byte] == ':')
1496 const unsigned char *class_beg = str + i_byte + 1;
1497 const unsigned char *class_end = class_beg;
1498 const unsigned char *class_limit = str + size_byte - 2;
1499 /* Leave room for the null. */
1500 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1501 re_wctype_t cc;
1503 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1504 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1506 while (class_end < class_limit
1507 && *class_end >= 'a' && *class_end <= 'z')
1508 class_end++;
1510 if (class_end == class_beg
1511 || *class_end != ':' || class_end[1] != ']')
1512 goto not_a_class_name;
1514 memcpy (class_name, class_beg, class_end - class_beg);
1515 class_name[class_end - class_beg] = 0;
1517 cc = re_wctype (class_name);
1518 if (cc == 0)
1519 error ("Invalid ISO C character class");
1521 iso_classes = Fcons (make_number (cc), iso_classes);
1523 i_byte = class_end + 2 - str;
1524 continue;
1527 not_a_class_name:
1528 if (c == '\\')
1530 if (i_byte == size_byte)
1531 break;
1533 c = str[i_byte++];
1535 /* Treat `-' as range character only if another character
1536 follows. */
1537 if (i_byte + 1 < size_byte
1538 && str[i_byte] == '-')
1540 unsigned int c2;
1542 /* Skip over the dash. */
1543 i_byte++;
1545 /* Get the end of the range. */
1546 c2 = str[i_byte++];
1547 if (c2 == '\\'
1548 && i_byte < size_byte)
1549 c2 = str[i_byte++];
1551 if (c <= c2)
1553 unsigned lim2 = c2 + 1;
1554 while (c < lim2)
1555 fastmap[c++] = 1;
1556 if (! ASCII_CHAR_P (c2))
1557 string_has_eight_bit = 1;
1560 else
1562 fastmap[c] = 1;
1563 if (! ASCII_CHAR_P (c))
1564 string_has_eight_bit = 1;
1568 /* If the current range is multibyte and STRING contains
1569 eight-bit chars, arrange fastmap and setup char_ranges for
1570 the corresponding multibyte chars. */
1571 if (multibyte && string_has_eight_bit)
1573 unsigned char fastmap2[0400];
1574 int range_start_byte, range_start_char;
1576 memcpy (fastmap + 0200, fastmap2 + 0200, 0200);
1577 memset (fastmap + 0200, 0, 0200);
1578 /* We are sure that this loop stops. */
1579 for (i = 0200; ! fastmap2[i]; i++);
1580 c = BYTE8_TO_CHAR (i);
1581 fastmap[CHAR_LEADING_CODE (c)] = 1;
1582 range_start_byte = i;
1583 range_start_char = c;
1584 char_ranges = (int *) alloca (sizeof (int) * 128 * 2);
1585 for (i = 129; i < 0400; i++)
1587 c = BYTE8_TO_CHAR (i);
1588 fastmap[CHAR_LEADING_CODE (c)] = 1;
1589 if (i - range_start_byte != c - range_start_char)
1591 char_ranges[n_char_ranges++] = range_start_char;
1592 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1593 + range_start_char);
1594 range_start_byte = i;
1595 range_start_char = c;
1598 char_ranges[n_char_ranges++] = range_start_char;
1599 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1600 + range_start_char);
1603 else /* STRING is multibyte */
1605 char_ranges = (int *) alloca (sizeof (int) * SCHARS (string) * 2);
1607 while (i_byte < size_byte)
1609 unsigned char leading_code;
1611 leading_code = str[i_byte];
1612 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1613 i_byte += len;
1615 if (handle_iso_classes && c == '['
1616 && i_byte < size_byte
1617 && STRING_CHAR (str + i_byte) == ':')
1619 const unsigned char *class_beg = str + i_byte + 1;
1620 const unsigned char *class_end = class_beg;
1621 const unsigned char *class_limit = str + size_byte - 2;
1622 /* Leave room for the null. */
1623 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1624 re_wctype_t cc;
1626 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1627 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1629 while (class_end < class_limit
1630 && *class_end >= 'a' && *class_end <= 'z')
1631 class_end++;
1633 if (class_end == class_beg
1634 || *class_end != ':' || class_end[1] != ']')
1635 goto not_a_class_name_multibyte;
1637 memcpy (class_name, class_beg, class_end - class_beg);
1638 class_name[class_end - class_beg] = 0;
1640 cc = re_wctype (class_name);
1641 if (cc == 0)
1642 error ("Invalid ISO C character class");
1644 iso_classes = Fcons (make_number (cc), iso_classes);
1646 i_byte = class_end + 2 - str;
1647 continue;
1650 not_a_class_name_multibyte:
1651 if (c == '\\')
1653 if (i_byte == size_byte)
1654 break;
1656 leading_code = str[i_byte];
1657 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1658 i_byte += len;
1660 /* Treat `-' as range character only if another character
1661 follows. */
1662 if (i_byte + 1 < size_byte
1663 && str[i_byte] == '-')
1665 unsigned int c2;
1666 unsigned char leading_code2;
1668 /* Skip over the dash. */
1669 i_byte++;
1671 /* Get the end of the range. */
1672 leading_code2 = str[i_byte];
1673 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1674 i_byte += len;
1676 if (c2 == '\\'
1677 && i_byte < size_byte)
1679 leading_code2 = str[i_byte];
1680 c2 =STRING_CHAR_AND_LENGTH (str + i_byte, len);
1681 i_byte += len;
1684 if (c > c2)
1685 continue;
1686 if (ASCII_CHAR_P (c))
1688 while (c <= c2 && c < 0x80)
1689 fastmap[c++] = 1;
1690 leading_code = CHAR_LEADING_CODE (c);
1692 if (! ASCII_CHAR_P (c))
1694 unsigned lim2 = leading_code2 + 1;
1695 while (leading_code < lim2)
1696 fastmap[leading_code++] = 1;
1697 if (c <= c2)
1699 char_ranges[n_char_ranges++] = c;
1700 char_ranges[n_char_ranges++] = c2;
1704 else
1706 if (ASCII_CHAR_P (c))
1707 fastmap[c] = 1;
1708 else
1710 fastmap[leading_code] = 1;
1711 char_ranges[n_char_ranges++] = c;
1712 char_ranges[n_char_ranges++] = c;
1717 /* If the current range is unibyte and STRING contains non-ASCII
1718 chars, arrange fastmap for the corresponding unibyte
1719 chars. */
1721 if (! multibyte && n_char_ranges > 0)
1723 memset (fastmap + 0200, 0, 0200);
1724 for (i = 0; i < n_char_ranges; i += 2)
1726 int c1 = char_ranges[i];
1727 unsigned lim2 = char_ranges[i + 1] + 1;
1729 for (; c1 < lim2; c1++)
1731 int b = CHAR_TO_BYTE_SAFE (c1);
1732 if (b >= 0)
1733 fastmap[b] = 1;
1739 /* If ^ was the first character, complement the fastmap. */
1740 if (negate)
1742 if (! multibyte)
1743 for (i = 0; i < sizeof fastmap; i++)
1744 fastmap[i] ^= 1;
1745 else
1747 for (i = 0; i < 0200; i++)
1748 fastmap[i] ^= 1;
1749 /* All non-ASCII chars possibly match. */
1750 for (; i < sizeof fastmap; i++)
1751 fastmap[i] = 1;
1756 EMACS_INT start_point = PT;
1757 EMACS_INT pos = PT;
1758 EMACS_INT pos_byte = PT_BYTE;
1759 unsigned char *p = PT_ADDR, *endp, *stop;
1761 if (forwardp)
1763 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1764 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1766 else
1768 endp = CHAR_POS_ADDR (XINT (lim));
1769 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1772 immediate_quit = 1;
1773 /* This code may look up syntax tables using macros that rely on the
1774 gl_state object. To make sure this object is not out of date,
1775 let's initialize it manually.
1776 We ignore syntax-table text-properties for now, since that's
1777 what we've done in the past. */
1778 SETUP_BUFFER_SYNTAX_TABLE ();
1779 if (forwardp)
1781 if (multibyte)
1782 while (1)
1784 int nbytes;
1786 if (p >= stop)
1788 if (p >= endp)
1789 break;
1790 p = GAP_END_ADDR;
1791 stop = endp;
1793 c = STRING_CHAR_AND_LENGTH (p, nbytes);
1794 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1796 if (negate)
1797 break;
1798 else
1799 goto fwd_ok;
1802 if (! fastmap[*p])
1803 break;
1804 if (! ASCII_CHAR_P (c))
1806 /* As we are looking at a multibyte character, we
1807 must look up the character in the table
1808 CHAR_RANGES. If there's no data in the table,
1809 that character is not what we want to skip. */
1811 /* The following code do the right thing even if
1812 n_char_ranges is zero (i.e. no data in
1813 CHAR_RANGES). */
1814 for (i = 0; i < n_char_ranges; i += 2)
1815 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1816 break;
1817 if (!(negate ^ (i < n_char_ranges)))
1818 break;
1820 fwd_ok:
1821 p += nbytes, pos++, pos_byte += nbytes;
1823 else
1824 while (1)
1826 if (p >= stop)
1828 if (p >= endp)
1829 break;
1830 p = GAP_END_ADDR;
1831 stop = endp;
1834 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1836 if (negate)
1837 break;
1838 else
1839 goto fwd_unibyte_ok;
1842 if (!fastmap[*p])
1843 break;
1844 fwd_unibyte_ok:
1845 p++, pos++, pos_byte++;
1848 else
1850 if (multibyte)
1851 while (1)
1853 unsigned char *prev_p;
1855 if (p <= stop)
1857 if (p <= endp)
1858 break;
1859 p = GPT_ADDR;
1860 stop = endp;
1862 prev_p = p;
1863 while (--p >= stop && ! CHAR_HEAD_P (*p));
1864 c = STRING_CHAR (p);
1866 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1868 if (negate)
1869 break;
1870 else
1871 goto back_ok;
1874 if (! fastmap[*p])
1875 break;
1876 if (! ASCII_CHAR_P (c))
1878 /* See the comment in the previous similar code. */
1879 for (i = 0; i < n_char_ranges; i += 2)
1880 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1881 break;
1882 if (!(negate ^ (i < n_char_ranges)))
1883 break;
1885 back_ok:
1886 pos--, pos_byte -= prev_p - p;
1888 else
1889 while (1)
1891 if (p <= stop)
1893 if (p <= endp)
1894 break;
1895 p = GPT_ADDR;
1896 stop = endp;
1899 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
1901 if (negate)
1902 break;
1903 else
1904 goto back_unibyte_ok;
1907 if (!fastmap[p[-1]])
1908 break;
1909 back_unibyte_ok:
1910 p--, pos--, pos_byte--;
1914 SET_PT_BOTH (pos, pos_byte);
1915 immediate_quit = 0;
1917 return make_number (PT - start_point);
1922 static Lisp_Object
1923 skip_syntaxes (int forwardp, Lisp_Object string, Lisp_Object lim)
1925 register unsigned int c;
1926 unsigned char fastmap[0400];
1927 int negate = 0;
1928 register EMACS_INT i, i_byte;
1929 int multibyte;
1930 EMACS_INT size_byte;
1931 unsigned char *str;
1933 CHECK_STRING (string);
1935 if (NILP (lim))
1936 XSETINT (lim, forwardp ? ZV : BEGV);
1937 else
1938 CHECK_NUMBER_COERCE_MARKER (lim);
1940 /* In any case, don't allow scan outside bounds of buffer. */
1941 if (XINT (lim) > ZV)
1942 XSETFASTINT (lim, ZV);
1943 if (XINT (lim) < BEGV)
1944 XSETFASTINT (lim, BEGV);
1946 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
1947 return make_number (0);
1949 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1950 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1952 memset (fastmap, 0, sizeof fastmap);
1954 if (SBYTES (string) > SCHARS (string))
1955 /* As this is very rare case (syntax spec is ASCII only), don't
1956 consider efficiency. */
1957 string = string_make_unibyte (string);
1959 str = SDATA (string);
1960 size_byte = SBYTES (string);
1962 i_byte = 0;
1963 if (i_byte < size_byte
1964 && SREF (string, 0) == '^')
1966 negate = 1; i_byte++;
1969 /* Find the syntaxes specified and set their elements of fastmap. */
1971 while (i_byte < size_byte)
1973 c = str[i_byte++];
1974 fastmap[syntax_spec_code[c]] = 1;
1977 /* If ^ was the first character, complement the fastmap. */
1978 if (negate)
1979 for (i = 0; i < sizeof fastmap; i++)
1980 fastmap[i] ^= 1;
1983 EMACS_INT start_point = PT;
1984 EMACS_INT pos = PT;
1985 EMACS_INT pos_byte = PT_BYTE;
1986 unsigned char *p = PT_ADDR, *endp, *stop;
1988 if (forwardp)
1990 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1991 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1993 else
1995 endp = CHAR_POS_ADDR (XINT (lim));
1996 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1999 immediate_quit = 1;
2000 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
2001 if (forwardp)
2003 if (multibyte)
2005 while (1)
2007 int nbytes;
2009 if (p >= stop)
2011 if (p >= endp)
2012 break;
2013 p = GAP_END_ADDR;
2014 stop = endp;
2016 c = STRING_CHAR_AND_LENGTH (p, nbytes);
2017 if (! fastmap[(int) SYNTAX (c)])
2018 break;
2019 p += nbytes, pos++, pos_byte += nbytes;
2020 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2023 else
2025 while (1)
2027 if (p >= stop)
2029 if (p >= endp)
2030 break;
2031 p = GAP_END_ADDR;
2032 stop = endp;
2034 if (! fastmap[(int) SYNTAX (*p)])
2035 break;
2036 p++, pos++, pos_byte++;
2037 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2041 else
2043 if (multibyte)
2045 while (1)
2047 unsigned char *prev_p;
2049 if (p <= stop)
2051 if (p <= endp)
2052 break;
2053 p = GPT_ADDR;
2054 stop = endp;
2056 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2057 prev_p = p;
2058 while (--p >= stop && ! CHAR_HEAD_P (*p));
2059 c = STRING_CHAR (p);
2060 if (! fastmap[(int) SYNTAX (c)])
2061 break;
2062 pos--, pos_byte -= prev_p - p;
2065 else
2067 while (1)
2069 if (p <= stop)
2071 if (p <= endp)
2072 break;
2073 p = GPT_ADDR;
2074 stop = endp;
2076 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2077 if (! fastmap[(int) SYNTAX (p[-1])])
2078 break;
2079 p--, pos--, pos_byte--;
2084 SET_PT_BOTH (pos, pos_byte);
2085 immediate_quit = 0;
2087 return make_number (PT - start_point);
2091 /* Return 1 if character C belongs to one of the ISO classes
2092 in the list ISO_CLASSES. Each class is represented by an
2093 integer which is its type according to re_wctype. */
2095 static int
2096 in_classes (int c, Lisp_Object iso_classes)
2098 int fits_class = 0;
2100 while (CONSP (iso_classes))
2102 Lisp_Object elt;
2103 elt = XCAR (iso_classes);
2104 iso_classes = XCDR (iso_classes);
2106 if (re_iswctype (c, XFASTINT (elt)))
2107 fits_class = 1;
2110 return fits_class;
2113 /* Jump over a comment, assuming we are at the beginning of one.
2114 FROM is the current position.
2115 FROM_BYTE is the bytepos corresponding to FROM.
2116 Do not move past STOP (a charpos).
2117 The comment over which we have to jump is of style STYLE
2118 (either SYNTAX_FLAGS_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2119 NESTING should be positive to indicate the nesting at the beginning
2120 for nested comments and should be zero or negative else.
2121 ST_COMMENT_STYLE cannot be nested.
2122 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2123 (or 0 If the search cannot start in the middle of a two-character).
2125 If successful, return 1 and store the charpos of the comment's end
2126 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2127 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2128 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2129 (as defined for state.incomment) in *INCOMMENT_PTR.
2131 The comment end is the last character of the comment rather than the
2132 character just after the comment.
2134 Global syntax data is assumed to initially be valid for FROM and
2135 remains valid for forward search starting at the returned position. */
2137 static int
2138 forw_comment (EMACS_INT from, EMACS_INT from_byte, EMACS_INT stop,
2139 int nesting, int style, int prev_syntax,
2140 EMACS_INT *charpos_ptr, EMACS_INT *bytepos_ptr,
2141 int *incomment_ptr)
2143 register int c, c1;
2144 register enum syntaxcode code;
2145 register int syntax, other_syntax;
2147 if (nesting <= 0) nesting = -1;
2149 /* Enter the loop in the middle so that we find
2150 a 2-char comment ender if we start in the middle of it. */
2151 syntax = prev_syntax;
2152 if (syntax != 0) goto forw_incomment;
2154 while (1)
2156 if (from == stop)
2158 *incomment_ptr = nesting;
2159 *charpos_ptr = from;
2160 *bytepos_ptr = from_byte;
2161 return 0;
2163 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2164 syntax = SYNTAX_WITH_FLAGS (c);
2165 code = syntax & 0xff;
2166 if (code == Sendcomment
2167 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style
2168 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2169 (nesting > 0 && --nesting == 0) : nesting < 0))
2170 /* we have encountered a comment end of the same style
2171 as the comment sequence which began this comment
2172 section */
2173 break;
2174 if (code == Scomment_fence
2175 && style == ST_COMMENT_STYLE)
2176 /* we have encountered a comment end of the same style
2177 as the comment sequence which began this comment
2178 section. */
2179 break;
2180 if (nesting > 0
2181 && code == Scomment
2182 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2183 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style)
2184 /* we have encountered a nested comment of the same style
2185 as the comment sequence which began this comment section */
2186 nesting++;
2187 INC_BOTH (from, from_byte);
2188 UPDATE_SYNTAX_TABLE_FORWARD (from);
2190 forw_incomment:
2191 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2192 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2193 other_syntax = SYNTAX_WITH_FLAGS (c1),
2194 SYNTAX_FLAGS_COMEND_SECOND (other_syntax))
2195 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, other_syntax) == style
2196 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2197 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax))
2198 ? nesting > 0 : nesting < 0))
2200 if (--nesting <= 0)
2201 /* we have encountered a comment end of the same style
2202 as the comment sequence which began this comment
2203 section */
2204 break;
2205 else
2207 INC_BOTH (from, from_byte);
2208 UPDATE_SYNTAX_TABLE_FORWARD (from);
2211 if (nesting > 0
2212 && from < stop
2213 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2214 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2215 other_syntax = SYNTAX_WITH_FLAGS (c1),
2216 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax) == style
2217 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2218 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2219 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax)))
2220 /* we have encountered a nested comment of the same style
2221 as the comment sequence which began this comment
2222 section */
2224 INC_BOTH (from, from_byte);
2225 UPDATE_SYNTAX_TABLE_FORWARD (from);
2226 nesting++;
2229 *charpos_ptr = from;
2230 *bytepos_ptr = from_byte;
2231 return 1;
2234 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2235 doc: /*
2236 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2237 Stop scanning if we find something other than a comment or whitespace.
2238 Set point to where scanning stops.
2239 If COUNT comments are found as expected, with nothing except whitespace
2240 between them, return t; otherwise return nil. */)
2241 (Lisp_Object count)
2243 register EMACS_INT from;
2244 EMACS_INT from_byte;
2245 register EMACS_INT stop;
2246 register int c, c1;
2247 register enum syntaxcode code;
2248 int comstyle = 0; /* style of comment encountered */
2249 int comnested = 0; /* whether the comment is nestable or not */
2250 int found;
2251 EMACS_INT count1;
2252 EMACS_INT out_charpos, out_bytepos;
2253 int dummy;
2255 CHECK_NUMBER (count);
2256 count1 = XINT (count);
2257 stop = count1 > 0 ? ZV : BEGV;
2259 immediate_quit = 1;
2260 QUIT;
2262 from = PT;
2263 from_byte = PT_BYTE;
2265 SETUP_SYNTAX_TABLE (from, count1);
2266 while (count1 > 0)
2270 int comstart_first, syntax, other_syntax;
2272 if (from == stop)
2274 SET_PT_BOTH (from, from_byte);
2275 immediate_quit = 0;
2276 return Qnil;
2278 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2279 syntax = SYNTAX_WITH_FLAGS (c);
2280 code = SYNTAX (c);
2281 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2282 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2283 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2284 INC_BOTH (from, from_byte);
2285 UPDATE_SYNTAX_TABLE_FORWARD (from);
2286 if (from < stop && comstart_first
2287 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2288 other_syntax = SYNTAX_WITH_FLAGS (c1),
2289 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)))
2291 /* We have encountered a comment start sequence and we
2292 are ignoring all text inside comments. We must record
2293 the comment style this sequence begins so that later,
2294 only a comment end of the same style actually ends
2295 the comment section. */
2296 code = Scomment;
2297 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2298 comnested
2299 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2300 INC_BOTH (from, from_byte);
2301 UPDATE_SYNTAX_TABLE_FORWARD (from);
2304 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2306 if (code == Scomment_fence)
2307 comstyle = ST_COMMENT_STYLE;
2308 else if (code != Scomment)
2310 immediate_quit = 0;
2311 DEC_BOTH (from, from_byte);
2312 SET_PT_BOTH (from, from_byte);
2313 return Qnil;
2315 /* We're at the start of a comment. */
2316 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2317 &out_charpos, &out_bytepos, &dummy);
2318 from = out_charpos; from_byte = out_bytepos;
2319 if (!found)
2321 immediate_quit = 0;
2322 SET_PT_BOTH (from, from_byte);
2323 return Qnil;
2325 INC_BOTH (from, from_byte);
2326 UPDATE_SYNTAX_TABLE_FORWARD (from);
2327 /* We have skipped one comment. */
2328 count1--;
2331 while (count1 < 0)
2333 while (1)
2335 int quoted, syntax;
2337 if (from <= stop)
2339 SET_PT_BOTH (BEGV, BEGV_BYTE);
2340 immediate_quit = 0;
2341 return Qnil;
2344 DEC_BOTH (from, from_byte);
2345 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2346 quoted = char_quoted (from, from_byte);
2347 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2348 syntax = SYNTAX_WITH_FLAGS (c);
2349 code = SYNTAX (c);
2350 comstyle = 0;
2351 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2352 if (code == Sendcomment)
2353 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2354 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2355 && prev_char_comend_first (from, from_byte)
2356 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2358 int other_syntax;
2359 /* We must record the comment style encountered so that
2360 later, we can match only the proper comment begin
2361 sequence of the same style. */
2362 DEC_BOTH (from, from_byte);
2363 code = Sendcomment;
2364 /* Calling char_quoted, above, set up global syntax position
2365 at the new value of FROM. */
2366 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2367 other_syntax = SYNTAX_WITH_FLAGS (c1);
2368 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2369 comnested
2370 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2373 if (code == Scomment_fence)
2375 /* Skip until first preceding unquoted comment_fence. */
2376 int fence_found = 0;
2377 EMACS_INT ini = from, ini_byte = from_byte;
2379 while (1)
2381 DEC_BOTH (from, from_byte);
2382 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2383 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2384 if (SYNTAX (c) == Scomment_fence
2385 && !char_quoted (from, from_byte))
2387 fence_found = 1;
2388 break;
2390 else if (from == stop)
2391 break;
2393 if (fence_found == 0)
2395 from = ini; /* Set point to ini + 1. */
2396 from_byte = ini_byte;
2397 goto leave;
2399 else
2400 /* We have skipped one comment. */
2401 break;
2403 else if (code == Sendcomment)
2405 found = back_comment (from, from_byte, stop, comnested, comstyle,
2406 &out_charpos, &out_bytepos);
2407 if (found == -1)
2409 if (c == '\n')
2410 /* This end-of-line is not an end-of-comment.
2411 Treat it like a whitespace.
2412 CC-mode (and maybe others) relies on this behavior. */
2414 else
2416 /* Failure: we should go back to the end of this
2417 not-quite-endcomment. */
2418 if (SYNTAX (c) != code)
2419 /* It was a two-char Sendcomment. */
2420 INC_BOTH (from, from_byte);
2421 goto leave;
2424 else
2426 /* We have skipped one comment. */
2427 from = out_charpos, from_byte = out_bytepos;
2428 break;
2431 else if (code != Swhitespace || quoted)
2433 leave:
2434 immediate_quit = 0;
2435 INC_BOTH (from, from_byte);
2436 SET_PT_BOTH (from, from_byte);
2437 return Qnil;
2441 count1++;
2444 SET_PT_BOTH (from, from_byte);
2445 immediate_quit = 0;
2446 return Qt;
2449 /* Return syntax code of character C if C is an ASCII character
2450 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2452 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2453 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2454 ? SYNTAX (c) : Ssymbol)
2456 static Lisp_Object
2457 scan_lists (register EMACS_INT from, EMACS_INT count, EMACS_INT depth, int sexpflag)
2459 Lisp_Object val;
2460 register EMACS_INT stop = count > 0 ? ZV : BEGV;
2461 register int c, c1;
2462 int stringterm;
2463 int quoted;
2464 int mathexit = 0;
2465 register enum syntaxcode code, temp_code;
2466 int min_depth = depth; /* Err out if depth gets less than this. */
2467 int comstyle = 0; /* style of comment encountered */
2468 int comnested = 0; /* whether the comment is nestable or not */
2469 EMACS_INT temp_pos;
2470 EMACS_INT last_good = from;
2471 int found;
2472 EMACS_INT from_byte;
2473 EMACS_INT out_bytepos, out_charpos;
2474 int temp, dummy;
2475 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2477 if (depth > 0) min_depth = 0;
2479 if (from > ZV) from = ZV;
2480 if (from < BEGV) from = BEGV;
2482 from_byte = CHAR_TO_BYTE (from);
2484 immediate_quit = 1;
2485 QUIT;
2487 SETUP_SYNTAX_TABLE (from, count);
2488 while (count > 0)
2490 while (from < stop)
2492 int comstart_first, prefix, syntax, other_syntax;
2493 UPDATE_SYNTAX_TABLE_FORWARD (from);
2494 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2495 syntax = SYNTAX_WITH_FLAGS (c);
2496 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2497 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2498 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2499 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2500 prefix = SYNTAX_FLAGS_PREFIX (syntax);
2501 if (depth == min_depth)
2502 last_good = from;
2503 INC_BOTH (from, from_byte);
2504 UPDATE_SYNTAX_TABLE_FORWARD (from);
2505 if (from < stop && comstart_first
2506 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2507 other_syntax = SYNTAX_WITH_FLAGS (c),
2508 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2509 && parse_sexp_ignore_comments)
2511 /* we have encountered a comment start sequence and we
2512 are ignoring all text inside comments. We must record
2513 the comment style this sequence begins so that later,
2514 only a comment end of the same style actually ends
2515 the comment section */
2516 code = Scomment;
2517 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2518 comnested
2519 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2520 INC_BOTH (from, from_byte);
2521 UPDATE_SYNTAX_TABLE_FORWARD (from);
2524 if (prefix)
2525 continue;
2527 switch (SWITCH_ENUM_CAST (code))
2529 case Sescape:
2530 case Scharquote:
2531 if (from == stop)
2532 goto lose;
2533 INC_BOTH (from, from_byte);
2534 /* treat following character as a word constituent */
2535 case Sword:
2536 case Ssymbol:
2537 if (depth || !sexpflag) break;
2538 /* This word counts as a sexp; return at end of it. */
2539 while (from < stop)
2541 UPDATE_SYNTAX_TABLE_FORWARD (from);
2543 /* Some compilers can't handle this inside the switch. */
2544 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2545 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2546 switch (temp)
2548 case Scharquote:
2549 case Sescape:
2550 INC_BOTH (from, from_byte);
2551 if (from == stop)
2552 goto lose;
2553 break;
2554 case Sword:
2555 case Ssymbol:
2556 case Squote:
2557 break;
2558 default:
2559 goto done;
2561 INC_BOTH (from, from_byte);
2563 goto done;
2565 case Scomment_fence:
2566 comstyle = ST_COMMENT_STYLE;
2567 /* FALLTHROUGH */
2568 case Scomment:
2569 if (!parse_sexp_ignore_comments) break;
2570 UPDATE_SYNTAX_TABLE_FORWARD (from);
2571 found = forw_comment (from, from_byte, stop,
2572 comnested, comstyle, 0,
2573 &out_charpos, &out_bytepos, &dummy);
2574 from = out_charpos, from_byte = out_bytepos;
2575 if (!found)
2577 if (depth == 0)
2578 goto done;
2579 goto lose;
2581 INC_BOTH (from, from_byte);
2582 UPDATE_SYNTAX_TABLE_FORWARD (from);
2583 break;
2585 case Smath:
2586 if (!sexpflag)
2587 break;
2588 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2590 INC_BOTH (from, from_byte);
2592 if (mathexit)
2594 mathexit = 0;
2595 goto close1;
2597 mathexit = 1;
2599 case Sopen:
2600 if (!++depth) goto done;
2601 break;
2603 case Sclose:
2604 close1:
2605 if (!--depth) goto done;
2606 if (depth < min_depth)
2607 xsignal3 (Qscan_error,
2608 build_string ("Containing expression ends prematurely"),
2609 make_number (last_good), make_number (from));
2610 break;
2612 case Sstring:
2613 case Sstring_fence:
2614 temp_pos = dec_bytepos (from_byte);
2615 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2616 while (1)
2618 if (from >= stop)
2619 goto lose;
2620 UPDATE_SYNTAX_TABLE_FORWARD (from);
2621 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2622 if (code == Sstring
2623 ? (c == stringterm
2624 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2625 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
2626 break;
2628 /* Some compilers can't handle this inside the switch. */
2629 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2630 switch (temp)
2632 case Scharquote:
2633 case Sescape:
2634 INC_BOTH (from, from_byte);
2636 INC_BOTH (from, from_byte);
2638 INC_BOTH (from, from_byte);
2639 if (!depth && sexpflag) goto done;
2640 break;
2641 default:
2642 /* Ignore whitespace, punctuation, quote, endcomment. */
2643 break;
2647 /* Reached end of buffer. Error if within object, return nil if between */
2648 if (depth)
2649 goto lose;
2651 immediate_quit = 0;
2652 return Qnil;
2654 /* End of object reached */
2655 done:
2656 count--;
2659 while (count < 0)
2661 while (from > stop)
2663 int syntax;
2664 DEC_BOTH (from, from_byte);
2665 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2666 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2667 syntax= SYNTAX_WITH_FLAGS (c);
2668 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2669 if (depth == min_depth)
2670 last_good = from;
2671 comstyle = 0;
2672 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2673 if (code == Sendcomment)
2674 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2675 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2676 && prev_char_comend_first (from, from_byte)
2677 && parse_sexp_ignore_comments)
2679 /* We must record the comment style encountered so that
2680 later, we can match only the proper comment begin
2681 sequence of the same style. */
2682 int c2, other_syntax;
2683 DEC_BOTH (from, from_byte);
2684 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2685 code = Sendcomment;
2686 c2 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2687 other_syntax = SYNTAX_WITH_FLAGS (c2);
2688 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2689 comnested
2690 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2693 /* Quoting turns anything except a comment-ender
2694 into a word character. Note that this cannot be true
2695 if we decremented FROM in the if-statement above. */
2696 if (code != Sendcomment && char_quoted (from, from_byte))
2698 DEC_BOTH (from, from_byte);
2699 code = Sword;
2701 else if (SYNTAX_FLAGS_PREFIX (syntax))
2702 continue;
2704 switch (SWITCH_ENUM_CAST (code))
2706 case Sword:
2707 case Ssymbol:
2708 case Sescape:
2709 case Scharquote:
2710 if (depth || !sexpflag) break;
2711 /* This word counts as a sexp; count object finished
2712 after passing it. */
2713 while (from > stop)
2715 temp_pos = from_byte;
2716 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2717 DEC_POS (temp_pos);
2718 else
2719 temp_pos--;
2720 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2721 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2722 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2723 /* Don't allow comment-end to be quoted. */
2724 if (temp_code == Sendcomment)
2725 goto done2;
2726 quoted = char_quoted (from - 1, temp_pos);
2727 if (quoted)
2729 DEC_BOTH (from, from_byte);
2730 temp_pos = dec_bytepos (temp_pos);
2731 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2733 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2734 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2735 if (! (quoted || temp_code == Sword
2736 || temp_code == Ssymbol
2737 || temp_code == Squote))
2738 goto done2;
2739 DEC_BOTH (from, from_byte);
2741 goto done2;
2743 case Smath:
2744 if (!sexpflag)
2745 break;
2746 temp_pos = dec_bytepos (from_byte);
2747 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2748 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2749 DEC_BOTH (from, from_byte);
2750 if (mathexit)
2752 mathexit = 0;
2753 goto open2;
2755 mathexit = 1;
2757 case Sclose:
2758 if (!++depth) goto done2;
2759 break;
2761 case Sopen:
2762 open2:
2763 if (!--depth) goto done2;
2764 if (depth < min_depth)
2765 xsignal3 (Qscan_error,
2766 build_string ("Containing expression ends prematurely"),
2767 make_number (last_good), make_number (from));
2768 break;
2770 case Sendcomment:
2771 if (!parse_sexp_ignore_comments)
2772 break;
2773 found = back_comment (from, from_byte, stop, comnested, comstyle,
2774 &out_charpos, &out_bytepos);
2775 /* FIXME: if found == -1, then it really wasn't a comment-end.
2776 For single-char Sendcomment, we can't do much about it apart
2777 from skipping the char.
2778 For 2-char endcomments, we could try again, taking both
2779 chars as separate entities, but it's a lot of trouble
2780 for very little gain, so we don't bother either. -sm */
2781 if (found != -1)
2782 from = out_charpos, from_byte = out_bytepos;
2783 break;
2785 case Scomment_fence:
2786 case Sstring_fence:
2787 while (1)
2789 if (from == stop)
2790 goto lose;
2791 DEC_BOTH (from, from_byte);
2792 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2793 if (!char_quoted (from, from_byte)
2794 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2795 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
2796 break;
2798 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2799 break;
2801 case Sstring:
2802 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2803 while (1)
2805 if (from == stop)
2806 goto lose;
2807 DEC_BOTH (from, from_byte);
2808 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2809 if (!char_quoted (from, from_byte)
2810 && (stringterm
2811 == (c = FETCH_CHAR_AS_MULTIBYTE (from_byte)))
2812 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2813 break;
2815 if (!depth && sexpflag) goto done2;
2816 break;
2817 default:
2818 /* Ignore whitespace, punctuation, quote, endcomment. */
2819 break;
2823 /* Reached start of buffer. Error if within object, return nil if between */
2824 if (depth)
2825 goto lose;
2827 immediate_quit = 0;
2828 return Qnil;
2830 done2:
2831 count++;
2835 immediate_quit = 0;
2836 XSETFASTINT (val, from);
2837 return val;
2839 lose:
2840 xsignal3 (Qscan_error,
2841 build_string ("Unbalanced parentheses"),
2842 make_number (last_good), make_number (from));
2845 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2846 doc: /* Scan from character number FROM by COUNT lists.
2847 Returns the character number of the position thus found.
2849 If DEPTH is nonzero, paren depth begins counting from that value,
2850 only places where the depth in parentheses becomes zero
2851 are candidates for stopping; COUNT such places are counted.
2852 Thus, a positive value for DEPTH means go out levels.
2854 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2856 If the beginning or end of (the accessible part of) the buffer is reached
2857 and the depth is wrong, an error is signaled.
2858 If the depth is right but the count is not used up, nil is returned. */)
2859 (Lisp_Object from, Lisp_Object count, Lisp_Object depth)
2861 CHECK_NUMBER (from);
2862 CHECK_NUMBER (count);
2863 CHECK_NUMBER (depth);
2865 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2868 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2869 doc: /* Scan from character number FROM by COUNT balanced expressions.
2870 If COUNT is negative, scan backwards.
2871 Returns the character number of the position thus found.
2873 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2875 If the beginning or end of (the accessible part of) the buffer is reached
2876 in the middle of a parenthetical grouping, an error is signaled.
2877 If the beginning or end is reached between groupings
2878 but before count is used up, nil is returned. */)
2879 (Lisp_Object from, Lisp_Object count)
2881 CHECK_NUMBER (from);
2882 CHECK_NUMBER (count);
2884 return scan_lists (XINT (from), XINT (count), 0, 1);
2887 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2888 0, 0, 0,
2889 doc: /* Move point backward over any number of chars with prefix syntax.
2890 This includes chars with "quote" or "prefix" syntax (' or p). */)
2891 (void)
2893 EMACS_INT beg = BEGV;
2894 EMACS_INT opoint = PT;
2895 EMACS_INT opoint_byte = PT_BYTE;
2896 EMACS_INT pos = PT;
2897 EMACS_INT pos_byte = PT_BYTE;
2898 int c;
2900 if (pos <= beg)
2902 SET_PT_BOTH (opoint, opoint_byte);
2904 return Qnil;
2907 SETUP_SYNTAX_TABLE (pos, -1);
2909 DEC_BOTH (pos, pos_byte);
2911 while (!char_quoted (pos, pos_byte)
2912 /* Previous statement updates syntax table. */
2913 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
2914 || SYNTAX_PREFIX (c)))
2916 opoint = pos;
2917 opoint_byte = pos_byte;
2919 if (pos + 1 > beg)
2920 DEC_BOTH (pos, pos_byte);
2923 SET_PT_BOTH (opoint, opoint_byte);
2925 return Qnil;
2928 /* Parse forward from FROM / FROM_BYTE to END,
2929 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2930 and return a description of the state of the parse at END.
2931 If STOPBEFORE is nonzero, stop at the start of an atom.
2932 If COMMENTSTOP is 1, stop at the start of a comment.
2933 If COMMENTSTOP is -1, stop at the start or end of a comment,
2934 after the beginning of a string, or after the end of a string. */
2936 static void
2937 scan_sexps_forward (struct lisp_parse_state *stateptr,
2938 EMACS_INT from, EMACS_INT from_byte, EMACS_INT end,
2939 int targetdepth, int stopbefore,
2940 Lisp_Object oldstate, int commentstop)
2942 struct lisp_parse_state state;
2944 register enum syntaxcode code;
2945 int c1;
2946 int comnested;
2947 struct level { int last, prev; };
2948 struct level levelstart[100];
2949 register struct level *curlevel = levelstart;
2950 struct level *endlevel = levelstart + 100;
2951 register int depth; /* Paren depth of current scanning location.
2952 level - levelstart equals this except
2953 when the depth becomes negative. */
2954 int mindepth; /* Lowest DEPTH value seen. */
2955 int start_quoted = 0; /* Nonzero means starting after a char quote */
2956 Lisp_Object tem;
2957 EMACS_INT prev_from; /* Keep one character before FROM. */
2958 EMACS_INT prev_from_byte;
2959 int prev_from_syntax;
2960 int boundary_stop = commentstop == -1;
2961 int nofence;
2962 int found;
2963 EMACS_INT out_bytepos, out_charpos;
2964 int temp;
2966 prev_from = from;
2967 prev_from_byte = from_byte;
2968 if (from != BEGV)
2969 DEC_BOTH (prev_from, prev_from_byte);
2971 /* Use this macro instead of `from++'. */
2972 #define INC_FROM \
2973 do { prev_from = from; \
2974 prev_from_byte = from_byte; \
2975 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2976 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2977 INC_BOTH (from, from_byte); \
2978 if (from < end) \
2979 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2980 } while (0)
2982 immediate_quit = 1;
2983 QUIT;
2985 if (NILP (oldstate))
2987 depth = 0;
2988 state.instring = -1;
2989 state.incomment = 0;
2990 state.comstyle = 0; /* comment style a by default. */
2991 state.comstr_start = -1; /* no comment/string seen. */
2993 else
2995 tem = Fcar (oldstate);
2996 if (!NILP (tem))
2997 depth = XINT (tem);
2998 else
2999 depth = 0;
3001 oldstate = Fcdr (oldstate);
3002 oldstate = Fcdr (oldstate);
3003 oldstate = Fcdr (oldstate);
3004 tem = Fcar (oldstate);
3005 /* Check whether we are inside string_fence-style string: */
3006 state.instring = (!NILP (tem)
3007 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
3008 : -1);
3010 oldstate = Fcdr (oldstate);
3011 tem = Fcar (oldstate);
3012 state.incomment = (!NILP (tem)
3013 ? (INTEGERP (tem) ? XINT (tem) : -1)
3014 : 0);
3016 oldstate = Fcdr (oldstate);
3017 tem = Fcar (oldstate);
3018 start_quoted = !NILP (tem);
3020 /* if the eighth element of the list is nil, we are in comment
3021 style a. If it is non-nil, we are in comment style b */
3022 oldstate = Fcdr (oldstate);
3023 oldstate = Fcdr (oldstate);
3024 tem = Fcar (oldstate);
3025 state.comstyle = (NILP (tem)
3027 : (EQ (tem, Qsyntax_table)
3028 ? ST_COMMENT_STYLE
3029 : INTEGERP (tem) ? XINT (tem) : 1));
3031 oldstate = Fcdr (oldstate);
3032 tem = Fcar (oldstate);
3033 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
3034 oldstate = Fcdr (oldstate);
3035 tem = Fcar (oldstate);
3036 while (!NILP (tem)) /* >= second enclosing sexps. */
3038 /* curlevel++->last ran into compiler bug on Apollo */
3039 curlevel->last = XINT (Fcar (tem));
3040 if (++curlevel == endlevel)
3041 curlevel--; /* error ("Nesting too deep for parser"); */
3042 curlevel->prev = -1;
3043 curlevel->last = -1;
3044 tem = Fcdr (tem);
3047 state.quoted = 0;
3048 mindepth = depth;
3050 curlevel->prev = -1;
3051 curlevel->last = -1;
3053 SETUP_SYNTAX_TABLE (prev_from, 1);
3054 temp = FETCH_CHAR (prev_from_byte);
3055 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
3056 UPDATE_SYNTAX_TABLE_FORWARD (from);
3058 /* Enter the loop at a place appropriate for initial state. */
3060 if (state.incomment)
3061 goto startincomment;
3062 if (state.instring >= 0)
3064 nofence = state.instring != ST_STRING_STYLE;
3065 if (start_quoted)
3066 goto startquotedinstring;
3067 goto startinstring;
3069 else if (start_quoted)
3070 goto startquoted;
3072 while (from < end)
3074 int syntax;
3075 INC_FROM;
3076 code = prev_from_syntax & 0xff;
3078 if (from < end
3079 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3080 && (c1 = FETCH_CHAR (from_byte),
3081 syntax = SYNTAX_WITH_FLAGS (c1),
3082 SYNTAX_FLAGS_COMSTART_SECOND (syntax)))
3083 /* Duplicate code to avoid a complex if-expression
3084 which causes trouble for the SGI compiler. */
3086 /* Record the comment style we have entered so that only
3087 the comment-end sequence of the same style actually
3088 terminates the comment section. */
3089 state.comstyle
3090 = SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_from_syntax);
3091 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
3092 comnested = comnested || SYNTAX_FLAGS_COMMENT_NESTED (syntax);
3093 state.incomment = comnested ? 1 : -1;
3094 state.comstr_start = prev_from;
3095 INC_FROM;
3096 code = Scomment;
3098 else if (code == Scomment_fence)
3100 /* Record the comment style we have entered so that only
3101 the comment-end sequence of the same style actually
3102 terminates the comment section. */
3103 state.comstyle = ST_COMMENT_STYLE;
3104 state.incomment = -1;
3105 state.comstr_start = prev_from;
3106 code = Scomment;
3108 else if (code == Scomment)
3110 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax, 0);
3111 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3112 1 : -1);
3113 state.comstr_start = prev_from;
3116 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3117 continue;
3118 switch (SWITCH_ENUM_CAST (code))
3120 case Sescape:
3121 case Scharquote:
3122 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3123 curlevel->last = prev_from;
3124 startquoted:
3125 if (from == end) goto endquoted;
3126 INC_FROM;
3127 goto symstarted;
3128 /* treat following character as a word constituent */
3129 case Sword:
3130 case Ssymbol:
3131 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3132 curlevel->last = prev_from;
3133 symstarted:
3134 while (from < end)
3136 /* Some compilers can't handle this inside the switch. */
3137 temp = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3138 temp = SYNTAX (temp);
3139 switch (temp)
3141 case Scharquote:
3142 case Sescape:
3143 INC_FROM;
3144 if (from == end) goto endquoted;
3145 break;
3146 case Sword:
3147 case Ssymbol:
3148 case Squote:
3149 break;
3150 default:
3151 goto symdone;
3153 INC_FROM;
3155 symdone:
3156 curlevel->prev = curlevel->last;
3157 break;
3159 case Scomment_fence: /* Can't happen because it's handled above. */
3160 case Scomment:
3161 if (commentstop || boundary_stop) goto done;
3162 startincomment:
3163 /* The (from == BEGV) test was to enter the loop in the middle so
3164 that we find a 2-char comment ender even if we start in the
3165 middle of it. We don't want to do that if we're just at the
3166 beginning of the comment (think of (*) ... (*)). */
3167 found = forw_comment (from, from_byte, end,
3168 state.incomment, state.comstyle,
3169 (from == BEGV || from < state.comstr_start + 3)
3170 ? 0 : prev_from_syntax,
3171 &out_charpos, &out_bytepos, &state.incomment);
3172 from = out_charpos; from_byte = out_bytepos;
3173 /* Beware! prev_from and friends are invalid now.
3174 Luckily, the `done' doesn't use them and the INC_FROM
3175 sets them to a sane value without looking at them. */
3176 if (!found) goto done;
3177 INC_FROM;
3178 state.incomment = 0;
3179 state.comstyle = 0; /* reset the comment style */
3180 if (boundary_stop) goto done;
3181 break;
3183 case Sopen:
3184 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3185 depth++;
3186 /* curlevel++->last ran into compiler bug on Apollo */
3187 curlevel->last = prev_from;
3188 if (++curlevel == endlevel)
3189 curlevel--; /* error ("Nesting too deep for parser"); */
3190 curlevel->prev = -1;
3191 curlevel->last = -1;
3192 if (targetdepth == depth) goto done;
3193 break;
3195 case Sclose:
3196 depth--;
3197 if (depth < mindepth)
3198 mindepth = depth;
3199 if (curlevel != levelstart)
3200 curlevel--;
3201 curlevel->prev = curlevel->last;
3202 if (targetdepth == depth) goto done;
3203 break;
3205 case Sstring:
3206 case Sstring_fence:
3207 state.comstr_start = from - 1;
3208 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3209 curlevel->last = prev_from;
3210 state.instring = (code == Sstring
3211 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3212 : ST_STRING_STYLE);
3213 if (boundary_stop) goto done;
3214 startinstring:
3216 nofence = state.instring != ST_STRING_STYLE;
3218 while (1)
3220 int c;
3222 if (from >= end) goto done;
3223 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3224 /* Some compilers can't handle this inside the switch. */
3225 temp = SYNTAX (c);
3227 /* Check TEMP here so that if the char has
3228 a syntax-table property which says it is NOT
3229 a string character, it does not end the string. */
3230 if (nofence && c == state.instring && temp == Sstring)
3231 break;
3233 switch (temp)
3235 case Sstring_fence:
3236 if (!nofence) goto string_end;
3237 break;
3238 case Scharquote:
3239 case Sescape:
3240 INC_FROM;
3241 startquotedinstring:
3242 if (from >= end) goto endquoted;
3244 INC_FROM;
3247 string_end:
3248 state.instring = -1;
3249 curlevel->prev = curlevel->last;
3250 INC_FROM;
3251 if (boundary_stop) goto done;
3252 break;
3254 case Smath:
3255 /* FIXME: We should do something with it. */
3256 break;
3257 default:
3258 /* Ignore whitespace, punctuation, quote, endcomment. */
3259 break;
3262 goto done;
3264 stop: /* Here if stopping before start of sexp. */
3265 from = prev_from; /* We have just fetched the char that starts it; */
3266 goto done; /* but return the position before it. */
3268 endquoted:
3269 state.quoted = 1;
3270 done:
3271 state.depth = depth;
3272 state.mindepth = mindepth;
3273 state.thislevelstart = curlevel->prev;
3274 state.prevlevelstart
3275 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3276 state.location = from;
3277 state.levelstarts = Qnil;
3278 while (curlevel > levelstart)
3279 state.levelstarts = Fcons (make_number ((--curlevel)->last),
3280 state.levelstarts);
3281 immediate_quit = 0;
3283 *stateptr = state;
3286 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3287 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3288 Parsing stops at TO or when certain criteria are met;
3289 point is set to where parsing stops.
3290 If fifth arg OLDSTATE is omitted or nil,
3291 parsing assumes that FROM is the beginning of a function.
3292 Value is a list of elements describing final state of parsing:
3293 0. depth in parens.
3294 1. character address of start of innermost containing list; nil if none.
3295 2. character address of start of last complete sexp terminated.
3296 3. non-nil if inside a string.
3297 (it is the character that will terminate the string,
3298 or t if the string should be terminated by a generic string delimiter.)
3299 4. nil if outside a comment, t if inside a non-nestable comment,
3300 else an integer (the current comment nesting).
3301 5. t if following a quote character.
3302 6. the minimum paren-depth encountered during this scan.
3303 7. style of comment, if any.
3304 8. character address of start of comment or string; nil if not in one.
3305 9. Intermediate data for continuation of parsing (subject to change).
3306 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3307 in parentheses becomes equal to TARGETDEPTH.
3308 Fourth arg STOPBEFORE non-nil means stop when come to
3309 any character that starts a sexp.
3310 Fifth arg OLDSTATE is a list like what this function returns.
3311 It is used to initialize the state of the parse. Elements number 1, 2, 6
3312 and 8 are ignored.
3313 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3314 If it is symbol `syntax-table', stop after the start of a comment or a
3315 string, or after end of a comment or a string. */)
3316 (Lisp_Object from, Lisp_Object to, Lisp_Object targetdepth, Lisp_Object stopbefore, Lisp_Object oldstate, Lisp_Object commentstop)
3318 struct lisp_parse_state state;
3319 int target;
3321 if (!NILP (targetdepth))
3323 CHECK_NUMBER (targetdepth);
3324 target = XINT (targetdepth);
3326 else
3327 target = -100000; /* We won't reach this depth */
3329 validate_region (&from, &to);
3330 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3331 XINT (to),
3332 target, !NILP (stopbefore), oldstate,
3333 (NILP (commentstop)
3334 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3336 SET_PT (state.location);
3338 return Fcons (make_number (state.depth),
3339 Fcons (state.prevlevelstart < 0
3340 ? Qnil : make_number (state.prevlevelstart),
3341 Fcons (state.thislevelstart < 0
3342 ? Qnil : make_number (state.thislevelstart),
3343 Fcons (state.instring >= 0
3344 ? (state.instring == ST_STRING_STYLE
3345 ? Qt : make_number (state.instring)) : Qnil,
3346 Fcons (state.incomment < 0 ? Qt :
3347 (state.incomment == 0 ? Qnil :
3348 make_number (state.incomment)),
3349 Fcons (state.quoted ? Qt : Qnil,
3350 Fcons (make_number (state.mindepth),
3351 Fcons ((state.comstyle
3352 ? (state.comstyle == ST_COMMENT_STYLE
3353 ? Qsyntax_table
3354 : make_number (state.comstyle))
3355 : Qnil),
3356 Fcons (((state.incomment
3357 || (state.instring >= 0))
3358 ? make_number (state.comstr_start)
3359 : Qnil),
3360 Fcons (state.levelstarts, Qnil))))))))));
3363 void
3364 init_syntax_once (void)
3366 register int i, c;
3367 Lisp_Object temp;
3369 /* This has to be done here, before we call Fmake_char_table. */
3370 DEFSYM (Qsyntax_table, "syntax-table");
3372 /* Intern_C_String this now in case it isn't already done.
3373 Setting this variable twice is harmless.
3374 But don't staticpro it here--that is done in alloc.c. */
3375 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
3377 /* Create objects which can be shared among syntax tables. */
3378 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
3379 for (i = 0; i < ASIZE (Vsyntax_code_object); i++)
3380 XVECTOR (Vsyntax_code_object)->contents[i]
3381 = Fcons (make_number (i), Qnil);
3383 /* Now we are ready to set up this property, so we can
3384 create syntax tables. */
3385 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3387 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3389 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3391 /* Control characters should not be whitespace. */
3392 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3393 for (i = 0; i <= ' ' - 1; i++)
3394 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3395 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3397 /* Except that a few really are whitespace. */
3398 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3399 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3400 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3401 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3402 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3403 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3405 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3406 for (i = 'a'; i <= 'z'; i++)
3407 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3408 for (i = 'A'; i <= 'Z'; i++)
3409 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3410 for (i = '0'; i <= '9'; i++)
3411 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3413 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3414 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
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 (Sopen), make_number ('}')));
3426 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3427 Fcons (make_number (Sclose), make_number ('{')));
3428 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3429 Fcons (make_number ((int) Sstring), Qnil));
3430 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3431 Fcons (make_number ((int) Sescape), Qnil));
3433 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
3434 for (i = 0; i < 10; i++)
3436 c = "_-+*/&|<>="[i];
3437 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3440 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3441 for (i = 0; i < 12; i++)
3443 c = ".,;:?!#@~^'`"[i];
3444 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3447 /* All multibyte characters have syntax `word' by default. */
3448 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3449 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3452 void
3453 syms_of_syntax (void)
3455 DEFSYM (Qsyntax_table_p, "syntax-table-p");
3457 staticpro (&Vsyntax_code_object);
3459 staticpro (&gl_state.object);
3460 staticpro (&gl_state.global_code);
3461 staticpro (&gl_state.current_syntax_table);
3462 staticpro (&gl_state.old_prop);
3464 /* Defined in regex.c */
3465 staticpro (&re_match_object);
3467 DEFSYM (Qscan_error, "scan-error");
3468 Fput (Qscan_error, Qerror_conditions,
3469 pure_cons (Qscan_error, pure_cons (Qerror, Qnil)));
3470 Fput (Qscan_error, Qerror_message,
3471 make_pure_c_string ("Scan error"));
3473 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments,
3474 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3476 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties,
3477 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3478 Otherwise, that text property is simply ignored.
3479 See the info node `(elisp)Syntax Properties' for a description of the
3480 `syntax-table' property. */);
3482 words_include_escapes = 0;
3483 DEFVAR_BOOL ("words-include-escapes", words_include_escapes,
3484 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3486 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol,
3487 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3488 multibyte_syntax_as_symbol = 0;
3490 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3491 open_paren_in_column_0_is_defun_start,
3492 doc: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3493 open_paren_in_column_0_is_defun_start = 1;
3496 DEFVAR_LISP ("find-word-boundary-function-table",
3497 Vfind_word_boundary_function_table,
3498 doc: /*
3499 Char table of functions to search for the word boundary.
3500 Each function is called with two arguments; POS and LIMIT.
3501 POS and LIMIT are character positions in the current buffer.
3503 If POS is less than LIMIT, POS is at the first character of a word,
3504 and the return value of a function is a position after the last
3505 character of that word.
3507 If POS is not less than LIMIT, POS is at the last character of a word,
3508 and the return value of a function is a position at the first
3509 character of that word.
3511 In both cases, LIMIT bounds the search. */);
3512 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3514 defsubr (&Ssyntax_table_p);
3515 defsubr (&Ssyntax_table);
3516 defsubr (&Sstandard_syntax_table);
3517 defsubr (&Scopy_syntax_table);
3518 defsubr (&Sset_syntax_table);
3519 defsubr (&Schar_syntax);
3520 defsubr (&Smatching_paren);
3521 defsubr (&Sstring_to_syntax);
3522 defsubr (&Smodify_syntax_entry);
3523 defsubr (&Sinternal_describe_syntax_value);
3525 defsubr (&Sforward_word);
3527 defsubr (&Sskip_chars_forward);
3528 defsubr (&Sskip_chars_backward);
3529 defsubr (&Sskip_syntax_forward);
3530 defsubr (&Sskip_syntax_backward);
3532 defsubr (&Sforward_comment);
3533 defsubr (&Sscan_lists);
3534 defsubr (&Sscan_sexps);
3535 defsubr (&Sbackward_prefix_chars);
3536 defsubr (&Sparse_partial_sexp);