Introduce a new comment style "c" flag.
[emacs.git] / src / syntax.c
blobf0a7dca42dc40c8bcc3903788c8b6f654896353a
1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993, 1994, 1995, 1997, 1998, 1999, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include <ctype.h>
24 #include <setjmp.h>
25 #include "lisp.h"
26 #include "commands.h"
27 #include "buffer.h"
28 #include "character.h"
29 #include "keymap.h"
30 #include "regex.h"
32 /* Make syntax table lookup grant data in gl_state. */
33 #define SYNTAX_ENTRY_VIA_PROPERTY
35 #include "syntax.h"
36 #include "intervals.h"
37 #include "category.h"
39 /* Then there are seven single-bit flags that have the following meanings:
40 1. This character is the first of a two-character comment-start sequence.
41 2. This character is the second of a two-character comment-start sequence.
42 3. This character is the first of a two-character comment-end sequence.
43 4. This character is the second of a two-character comment-end sequence.
44 5. This character is a prefix, for backward-prefix-chars.
45 6. The char is part of a delimiter for comments of style "b".
46 7. This character is part of a nestable comment sequence.
47 8. The char is part of a delimiter for comments of style "c".
48 Note that any two-character sequence whose first character has flag 1
49 and whose second character has flag 2 will be interpreted as a comment start.
51 bit 6 and 8 are used to discriminate between different comment styles.
52 Languages such as C++ allow two orthogonal syntax start/end pairs
53 and bit 6 is used to determine whether a comment-end or Scommentend
54 ends style a or b. Comment markers can start style a, b, c, or bc.
55 Style a is always the default.
56 For 2-char comment markers, the style b flag is only looked up on the second
57 char of the comment marker and on the first char of the comment ender.
58 For style c (like to for the nested flag), the flag can be placed on any
59 one of the chars.
62 /* These macros extract specific flags from an integer
63 that holds the syntax code and the flags. */
65 #define SYNTAX_FLAGS_COMSTART_FIRST(flags) (((flags) >> 16) & 1)
67 #define SYNTAX_FLAGS_COMSTART_SECOND(flags) (((flags) >> 17) & 1)
69 #define SYNTAX_FLAGS_COMEND_FIRST(flags) (((flags) >> 18) & 1)
71 #define SYNTAX_FLAGS_COMEND_SECOND(flags) (((flags) >> 19) & 1)
73 #define SYNTAX_FLAGS_PREFIX(flags) (((flags) >> 20) & 1)
75 #define SYNTAX_FLAGS_COMMENT_STYLEB(flags) (((flags) >> 21) & 1)
76 #define SYNTAX_FLAGS_COMMENT_STYLEC(flags) (((flags) >> 22) & 2)
77 /* FLAGS should be the flags of the main char of the comment marker, e.g.
78 the second for comstart and the first for comend. */
79 #define SYNTAX_FLAGS_COMMENT_STYLE(flags, other_flags) \
80 (SYNTAX_FLAGS_COMMENT_STYLEB (flags) \
81 | SYNTAX_FLAGS_COMMENT_STYLEC (flags) \
82 | SYNTAX_FLAGS_COMMENT_STYLEC (other_flags))
84 #define SYNTAX_FLAGS_COMMENT_NESTED(flags) (((flags) >> 22) & 1)
86 /* These macros extract a particular flag for a given character. */
88 #define SYNTAX_COMEND_FIRST(c) \
89 (SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c)))
90 #define SYNTAX_PREFIX(c) (SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c)))
92 /* We use these constants in place for comment-style and
93 string-ender-char to distinguish comments/strings started by
94 comment_fence and string_fence codes. */
96 #define ST_COMMENT_STYLE (256 + 1)
97 #define ST_STRING_STYLE (256 + 2)
99 Lisp_Object Qsyntax_table_p, Qsyntax_table, Qscan_error;
101 int words_include_escapes;
102 int parse_sexp_lookup_properties;
104 /* Nonzero means `scan-sexps' treat all multibyte characters as symbol. */
105 int multibyte_syntax_as_symbol;
107 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
108 if not compiled with GCC. No need to mark it, since it is used
109 only very temporarily. */
110 Lisp_Object syntax_temp;
112 /* Non-zero means an open parenthesis in column 0 is always considered
113 to be the start of a defun. Zero means an open parenthesis in
114 column 0 has no special meaning. */
116 int open_paren_in_column_0_is_defun_start;
118 /* This is the internal form of the parse state used in parse-partial-sexp. */
120 struct lisp_parse_state
122 int depth; /* Depth at end of parsing. */
123 int instring; /* -1 if not within string, else desired terminator. */
124 int incomment; /* -1 if in unnestable comment else comment nesting */
125 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
126 int quoted; /* Nonzero if just after an escape char at end of parsing */
127 int mindepth; /* Minimum depth seen while scanning. */
128 /* Char number of most recent start-of-expression at current level */
129 EMACS_INT thislevelstart;
130 /* Char number of start of containing expression */
131 EMACS_INT prevlevelstart;
132 EMACS_INT location; /* Char number at which parsing stopped. */
133 EMACS_INT comstr_start; /* Position of last comment/string starter. */
134 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
135 of levels (starting from outermost). */
138 /* These variables are a cache for finding the start of a defun.
139 find_start_pos is the place for which the defun start was found.
140 find_start_value is the defun start position found for it.
141 find_start_value_byte is the corresponding byte position.
142 find_start_buffer is the buffer it was found in.
143 find_start_begv is the BEGV value when it was found.
144 find_start_modiff is the value of MODIFF when it was found. */
146 static EMACS_INT find_start_pos;
147 static EMACS_INT find_start_value;
148 static EMACS_INT find_start_value_byte;
149 static struct buffer *find_start_buffer;
150 static EMACS_INT find_start_begv;
151 static int find_start_modiff;
154 static Lisp_Object skip_chars (int, Lisp_Object, Lisp_Object, int);
155 static Lisp_Object skip_syntaxes (int, Lisp_Object, Lisp_Object);
156 static Lisp_Object scan_lists (EMACS_INT, EMACS_INT, EMACS_INT, int);
157 static void scan_sexps_forward (struct lisp_parse_state *,
158 EMACS_INT, EMACS_INT, EMACS_INT, int,
159 int, Lisp_Object, int);
160 static int in_classes (int, Lisp_Object);
162 /* Whether the syntax of the character C has the prefix flag set. */
163 int syntax_prefix_flag_p (int c)
165 return SYNTAX_PREFIX (c);
168 struct gl_state_s gl_state; /* Global state of syntax parser. */
170 INTERVAL interval_of (int, Lisp_Object);
171 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
172 to scan to property-change. */
174 /* Update gl_state to an appropriate interval which contains CHARPOS. The
175 sign of COUNT give the relative position of CHARPOS wrt the previously
176 valid interval. If INIT, only [be]_property fields of gl_state are
177 valid at start, the rest is filled basing on OBJECT.
179 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
180 direction than the intervals - or in an interval. We update the
181 current syntax-table basing on the property of this interval, and
182 update the interval to start further than CHARPOS - or be
183 NULL_INTERVAL. We also update lim_property to be the next value of
184 charpos to call this subroutine again - or be before/after the
185 start/end of OBJECT. */
187 void
188 update_syntax_table (int charpos, int count, int init, Lisp_Object object)
190 Lisp_Object tmp_table;
191 int cnt = 0, invalidate = 1;
192 INTERVAL i;
194 if (init)
196 gl_state.old_prop = Qnil;
197 gl_state.start = gl_state.b_property;
198 gl_state.stop = gl_state.e_property;
199 i = interval_of (charpos, object);
200 gl_state.backward_i = gl_state.forward_i = i;
201 invalidate = 0;
202 if (NULL_INTERVAL_P (i))
203 return;
204 /* interval_of updates only ->position of the return value, so
205 update the parents manually to speed up update_interval. */
206 while (!NULL_PARENT (i))
208 if (AM_RIGHT_CHILD (i))
209 INTERVAL_PARENT (i)->position = i->position
210 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
211 - TOTAL_LENGTH (INTERVAL_PARENT (i))
212 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
213 else
214 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
215 + TOTAL_LENGTH (i);
216 i = INTERVAL_PARENT (i);
218 i = gl_state.forward_i;
219 gl_state.b_property = i->position - gl_state.offset;
220 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
221 goto update;
223 i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
225 /* We are guaranteed to be called with CHARPOS either in i,
226 or further off. */
227 if (NULL_INTERVAL_P (i))
228 error ("Error in syntax_table logic for to-the-end intervals");
229 else if (charpos < i->position) /* Move left. */
231 if (count > 0)
232 error ("Error in syntax_table logic for intervals <-");
233 /* Update the interval. */
234 i = update_interval (i, charpos);
235 if (INTERVAL_LAST_POS (i) != gl_state.b_property)
237 invalidate = 0;
238 gl_state.forward_i = i;
239 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
242 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
244 if (count < 0)
245 error ("Error in syntax_table logic for intervals ->");
246 /* Update the interval. */
247 i = update_interval (i, charpos);
248 if (i->position != gl_state.e_property)
250 invalidate = 0;
251 gl_state.backward_i = i;
252 gl_state.b_property = i->position - gl_state.offset;
256 update:
257 tmp_table = textget (i->plist, Qsyntax_table);
259 if (invalidate)
260 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
262 if (invalidate) /* Did not get to adjacent interval. */
263 { /* with the same table => */
264 /* invalidate the old range. */
265 if (count > 0)
267 gl_state.backward_i = i;
268 gl_state.b_property = i->position - gl_state.offset;
270 else
272 gl_state.forward_i = i;
273 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
277 if (!EQ (tmp_table, gl_state.old_prop))
279 gl_state.current_syntax_table = tmp_table;
280 gl_state.old_prop = tmp_table;
281 if (EQ (Fsyntax_table_p (tmp_table), Qt))
283 gl_state.use_global = 0;
285 else if (CONSP (tmp_table))
287 gl_state.use_global = 1;
288 gl_state.global_code = tmp_table;
290 else
292 gl_state.use_global = 0;
293 gl_state.current_syntax_table = current_buffer->syntax_table;
297 while (!NULL_INTERVAL_P (i))
299 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
301 if (count > 0)
303 gl_state.e_property = i->position - gl_state.offset;
304 gl_state.forward_i = i;
306 else
308 gl_state.b_property
309 = i->position + LENGTH (i) - gl_state.offset;
310 gl_state.backward_i = i;
312 return;
314 else if (cnt == INTERVALS_AT_ONCE)
316 if (count > 0)
318 gl_state.e_property
319 = i->position + LENGTH (i) - gl_state.offset
320 /* e_property at EOB is not set to ZV but to ZV+1, so that
321 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
322 having to check eob between the two. */
323 + (NULL_INTERVAL_P (next_interval (i)) ? 1 : 0);
324 gl_state.forward_i = i;
326 else
328 gl_state.b_property = i->position - gl_state.offset;
329 gl_state.backward_i = i;
331 return;
333 cnt++;
334 i = count > 0 ? next_interval (i) : previous_interval (i);
336 eassert (NULL_INTERVAL_P (i)); /* This property goes to the end. */
337 if (count > 0)
338 gl_state.e_property = gl_state.stop;
339 else
340 gl_state.b_property = gl_state.start;
343 /* Returns TRUE if char at CHARPOS is quoted.
344 Global syntax-table data should be set up already to be good at CHARPOS
345 or after. On return global syntax data is good for lookup at CHARPOS. */
347 static int
348 char_quoted (EMACS_INT charpos, EMACS_INT bytepos)
350 register enum syntaxcode code;
351 register EMACS_INT beg = BEGV;
352 register int quoted = 0;
353 EMACS_INT orig = charpos;
355 while (charpos > beg)
357 int c;
358 DEC_BOTH (charpos, bytepos);
360 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
361 c = FETCH_CHAR_AS_MULTIBYTE (bytepos);
362 code = SYNTAX (c);
363 if (! (code == Scharquote || code == Sescape))
364 break;
366 quoted = !quoted;
369 UPDATE_SYNTAX_TABLE (orig);
370 return quoted;
373 /* Return the bytepos one character after BYTEPOS.
374 We assume that BYTEPOS is not at the end of the buffer. */
376 INLINE EMACS_INT
377 inc_bytepos (EMACS_INT bytepos)
379 if (NILP (current_buffer->enable_multibyte_characters))
380 return bytepos + 1;
382 INC_POS (bytepos);
383 return bytepos;
386 /* Return the bytepos one character before BYTEPOS.
387 We assume that BYTEPOS is not at the start of the buffer. */
389 INLINE EMACS_INT
390 dec_bytepos (EMACS_INT bytepos)
392 if (NILP (current_buffer->enable_multibyte_characters))
393 return bytepos - 1;
395 DEC_POS (bytepos);
396 return bytepos;
399 /* Return a defun-start position before POS and not too far before.
400 It should be the last one before POS, or nearly the last.
402 When open_paren_in_column_0_is_defun_start is nonzero,
403 only the beginning of the buffer is treated as a defun-start.
405 We record the information about where the scan started
406 and what its result was, so that another call in the same area
407 can return the same value very quickly.
409 There is no promise at which position the global syntax data is
410 valid on return from the subroutine, so the caller should explicitly
411 update the global data. */
413 static EMACS_INT
414 find_defun_start (EMACS_INT pos, EMACS_INT pos_byte)
416 EMACS_INT opoint = PT, opoint_byte = PT_BYTE;
418 if (!open_paren_in_column_0_is_defun_start)
420 find_start_value_byte = BEGV_BYTE;
421 return BEGV;
424 /* Use previous finding, if it's valid and applies to this inquiry. */
425 if (current_buffer == find_start_buffer
426 /* Reuse the defun-start even if POS is a little farther on.
427 POS might be in the next defun, but that's ok.
428 Our value may not be the best possible, but will still be usable. */
429 && pos <= find_start_pos + 1000
430 && pos >= find_start_value
431 && BEGV == find_start_begv
432 && MODIFF == find_start_modiff)
433 return find_start_value;
435 /* Back up to start of line. */
436 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
438 /* We optimize syntax-table lookup for rare updates. Thus we accept
439 only those `^\s(' which are good in global _and_ text-property
440 syntax-tables. */
441 SETUP_BUFFER_SYNTAX_TABLE ();
442 while (PT > BEGV)
444 int c;
446 /* Open-paren at start of line means we may have found our
447 defun-start. */
448 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
449 if (SYNTAX (c) == Sopen)
451 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
452 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
453 if (SYNTAX (c) == Sopen)
454 break;
455 /* Now fallback to the default value. */
456 SETUP_BUFFER_SYNTAX_TABLE ();
458 /* Move to beg of previous line. */
459 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
462 /* Record what we found, for the next try. */
463 find_start_value = PT;
464 find_start_value_byte = PT_BYTE;
465 find_start_buffer = current_buffer;
466 find_start_modiff = MODIFF;
467 find_start_begv = BEGV;
468 find_start_pos = pos;
470 TEMP_SET_PT_BOTH (opoint, opoint_byte);
472 return find_start_value;
475 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
477 static int
478 prev_char_comend_first (int pos, int pos_byte)
480 int c, val;
482 DEC_BOTH (pos, pos_byte);
483 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
484 c = FETCH_CHAR (pos_byte);
485 val = SYNTAX_COMEND_FIRST (c);
486 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
487 return val;
490 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
492 /* static int
493 * prev_char_comstart_first (pos, pos_byte)
494 * int pos, pos_byte;
496 * int c, val;
498 * DEC_BOTH (pos, pos_byte);
499 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
500 * c = FETCH_CHAR (pos_byte);
501 * val = SYNTAX_COMSTART_FIRST (c);
502 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
503 * return val;
504 * } */
506 /* Checks whether charpos FROM is at the end of a comment.
507 FROM_BYTE is the bytepos corresponding to FROM.
508 Do not move back before STOP.
510 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
511 return -1 otherwise.
513 If successful, store the charpos of the comment's beginning
514 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
516 Global syntax data remains valid for backward search starting at
517 the returned value (or at FROM, if the search was not successful). */
519 static int
520 back_comment (EMACS_INT from, EMACS_INT from_byte, EMACS_INT stop, int comnested, int comstyle, EMACS_INT *charpos_ptr, EMACS_INT *bytepos_ptr)
522 /* Look back, counting the parity of string-quotes,
523 and recording the comment-starters seen.
524 When we reach a safe place, assume that's not in a string;
525 then step the main scan to the earliest comment-starter seen
526 an even number of string quotes away from the safe place.
528 OFROM[I] is position of the earliest comment-starter seen
529 which is I+2X quotes from the comment-end.
530 PARITY is current parity of quotes from the comment end. */
531 int string_style = -1; /* Presumed outside of any string. */
532 int string_lossage = 0;
533 /* Not a real lossage: indicates that we have passed a matching comment
534 starter plus a non-matching comment-ender, meaning that any matching
535 comment-starter we might see later could be a false positive (hidden
536 inside another comment).
537 Test case: { a (* b } c (* d *) */
538 int comment_lossage = 0;
539 EMACS_INT comment_end = from;
540 EMACS_INT comment_end_byte = from_byte;
541 EMACS_INT comstart_pos = 0;
542 EMACS_INT comstart_byte;
543 /* Place where the containing defun starts,
544 or 0 if we didn't come across it yet. */
545 EMACS_INT defun_start = 0;
546 EMACS_INT defun_start_byte = 0;
547 register enum syntaxcode code;
548 int nesting = 1; /* current comment nesting */
549 int c;
550 int syntax = 0;
552 /* FIXME: A }} comment-ender style leads to incorrect behavior
553 in the case of {{ c }}} because we ignore the last two chars which are
554 assumed to be comment-enders although they aren't. */
556 /* At beginning of range to scan, we're outside of strings;
557 that determines quote parity to the comment-end. */
558 while (from != stop)
560 int temp_byte, prev_syntax;
561 int com2start, com2end;
563 /* Move back and examine a character. */
564 DEC_BOTH (from, from_byte);
565 UPDATE_SYNTAX_TABLE_BACKWARD (from);
567 prev_syntax = syntax;
568 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
569 syntax = SYNTAX_WITH_FLAGS (c);
570 code = SYNTAX (c);
572 /* Check for 2-char comment markers. */
573 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
574 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
575 && (comstyle
576 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax, syntax))
577 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
578 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
579 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
580 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
582 /* Nasty cases with overlapping 2-char comment markers:
583 - snmp-mode: -- c -- foo -- c --
584 --- c --
585 ------ c --
586 - c-mode: *||*
587 |* *|* *|
588 |*| |* |*|
589 /// */
591 /* If a 2-char comment sequence partly overlaps with another,
592 we don't try to be clever. */
593 if (from > stop && (com2end || com2start))
595 int next = from, next_byte = from_byte, next_c, next_syntax;
596 DEC_BOTH (next, next_byte);
597 UPDATE_SYNTAX_TABLE_BACKWARD (next);
598 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
599 next_syntax = SYNTAX_WITH_FLAGS (next_c);
600 if (((com2start || comnested)
601 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
602 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
603 || ((com2end || comnested)
604 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
605 && (comstyle
606 == SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_syntax))
607 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
608 goto lossage;
609 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
612 if (com2start && comstart_pos == 0)
613 /* We're looking at a comment starter. But it might be a comment
614 ender as well (see snmp-mode). The first time we see one, we
615 need to consider it as a comment starter,
616 and the subsequent times as a comment ender. */
617 com2end = 0;
619 /* Turn a 2-char comment sequences into the appropriate syntax. */
620 if (com2end)
621 code = Sendcomment;
622 else if (com2start)
623 code = Scomment;
624 /* Ignore comment starters of a different style. */
625 else if (code == Scomment
626 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0)
627 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
628 continue;
630 /* Ignore escaped characters, except comment-enders. */
631 if (code != Sendcomment && char_quoted (from, from_byte))
632 continue;
634 switch (code)
636 case Sstring_fence:
637 case Scomment_fence:
638 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
639 case Sstring:
640 /* Track parity of quotes. */
641 if (string_style == -1)
642 /* Entering a string. */
643 string_style = c;
644 else if (string_style == c)
645 /* Leaving the string. */
646 string_style = -1;
647 else
648 /* If we have two kinds of string delimiters.
649 There's no way to grok this scanning backwards. */
650 string_lossage = 1;
651 break;
653 case Scomment:
654 /* We've already checked that it is the relevant comstyle. */
655 if (string_style != -1 || comment_lossage || string_lossage)
656 /* There are odd string quotes involved, so let's be careful.
657 Test case in Pascal: " { " a { " } */
658 goto lossage;
660 if (!comnested)
662 /* Record best comment-starter so far. */
663 comstart_pos = from;
664 comstart_byte = from_byte;
666 else if (--nesting <= 0)
667 /* nested comments have to be balanced, so we don't need to
668 keep looking for earlier ones. We use here the same (slightly
669 incorrect) reasoning as below: since it is followed by uniform
670 paired string quotes, this comment-start has to be outside of
671 strings, else the comment-end itself would be inside a string. */
672 goto done;
673 break;
675 case Sendcomment:
676 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == comstyle
677 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
678 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
679 /* This is the same style of comment ender as ours. */
681 if (comnested)
682 nesting++;
683 else
684 /* Anything before that can't count because it would match
685 this comment-ender rather than ours. */
686 from = stop; /* Break out of the loop. */
688 else if (comstart_pos != 0 || c != '\n')
689 /* We're mixing comment styles here, so we'd better be careful.
690 The (comstart_pos != 0 || c != '\n') check is not quite correct
691 (we should just always set comment_lossage), but removing it
692 would imply that any multiline comment in C would go through
693 lossage, which seems overkill.
694 The failure should only happen in the rare cases such as
695 { (* } *) */
696 comment_lossage = 1;
697 break;
699 case Sopen:
700 /* Assume a defun-start point is outside of strings. */
701 if (open_paren_in_column_0_is_defun_start
702 && (from == stop
703 || (temp_byte = dec_bytepos (from_byte),
704 FETCH_CHAR (temp_byte) == '\n')))
706 defun_start = from;
707 defun_start_byte = from_byte;
708 from = stop; /* Break out of the loop. */
710 break;
712 default:
713 break;
717 if (comstart_pos == 0)
719 from = comment_end;
720 from_byte = comment_end_byte;
721 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
723 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
724 or `done'), then we've found the beginning of the non-nested comment. */
725 else if (1) /* !comnested */
727 from = comstart_pos;
728 from_byte = comstart_byte;
729 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
731 else
733 struct lisp_parse_state state;
734 lossage:
735 /* We had two kinds of string delimiters mixed up
736 together. Decode this going forwards.
737 Scan fwd from a known safe place (beginning-of-defun)
738 to the one in question; this records where we
739 last passed a comment starter. */
740 /* If we did not already find the defun start, find it now. */
741 if (defun_start == 0)
743 defun_start = find_defun_start (comment_end, comment_end_byte);
744 defun_start_byte = find_start_value_byte;
748 scan_sexps_forward (&state,
749 defun_start, defun_start_byte,
750 comment_end, -10000, 0, Qnil, 0);
751 defun_start = comment_end;
752 if (state.incomment == (comnested ? 1 : -1)
753 && state.comstyle == comstyle)
754 from = state.comstr_start;
755 else
757 from = comment_end;
758 if (state.incomment)
759 /* If comment_end is inside some other comment, maybe ours
760 is nested, so we need to try again from within the
761 surrounding comment. Example: { a (* " *) */
763 /* FIXME: We should advance by one or two chars. */
764 defun_start = state.comstr_start + 2;
765 defun_start_byte = CHAR_TO_BYTE (defun_start);
768 } while (defun_start < comment_end);
770 from_byte = CHAR_TO_BYTE (from);
771 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
774 done:
775 *charpos_ptr = from;
776 *bytepos_ptr = from_byte;
778 return (from == comment_end) ? -1 : from;
781 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
782 doc: /* Return t if OBJECT is a syntax table.
783 Currently, any char-table counts as a syntax table. */)
784 (Lisp_Object object)
786 if (CHAR_TABLE_P (object)
787 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
788 return Qt;
789 return Qnil;
792 static void
793 check_syntax_table (Lisp_Object obj)
795 CHECK_TYPE (CHAR_TABLE_P (obj) && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table),
796 Qsyntax_table_p, obj);
799 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
800 doc: /* Return the current syntax table.
801 This is the one specified by the current buffer. */)
802 (void)
804 return current_buffer->syntax_table;
807 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
808 Sstandard_syntax_table, 0, 0, 0,
809 doc: /* Return the standard syntax table.
810 This is the one used for new buffers. */)
811 (void)
813 return Vstandard_syntax_table;
816 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
817 doc: /* Construct a new syntax table and return it.
818 It is a copy of the TABLE, which defaults to the standard syntax table. */)
819 (Lisp_Object table)
821 Lisp_Object copy;
823 if (!NILP (table))
824 check_syntax_table (table);
825 else
826 table = Vstandard_syntax_table;
828 copy = Fcopy_sequence (table);
830 /* Only the standard syntax table should have a default element.
831 Other syntax tables should inherit from parents instead. */
832 XCHAR_TABLE (copy)->defalt = Qnil;
834 /* Copied syntax tables should all have parents.
835 If we copied one with no parent, such as the standard syntax table,
836 use the standard syntax table as the copy's parent. */
837 if (NILP (XCHAR_TABLE (copy)->parent))
838 Fset_char_table_parent (copy, Vstandard_syntax_table);
839 return copy;
842 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
843 doc: /* Select a new syntax table for the current buffer.
844 One argument, a syntax table. */)
845 (Lisp_Object table)
847 int idx;
848 check_syntax_table (table);
849 current_buffer->syntax_table = table;
850 /* Indicate that this buffer now has a specified syntax table. */
851 idx = PER_BUFFER_VAR_IDX (syntax_table);
852 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
853 return table;
856 /* Convert a letter which signifies a syntax code
857 into the code it signifies.
858 This is used by modify-syntax-entry, and other things. */
860 unsigned char syntax_spec_code[0400] =
861 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
862 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
863 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
864 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
865 (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377,
866 (char) Smath, 0377, 0377, (char) Squote,
867 (char) Sopen, (char) Sclose, 0377, 0377,
868 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
869 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
870 0377, 0377, 0377, 0377,
871 (char) Scomment, 0377, (char) Sendcomment, 0377,
872 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
873 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
874 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
875 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
876 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
877 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
878 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
879 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377
882 /* Indexed by syntax code, give the letter that describes it. */
884 char syntax_code_spec[16] =
886 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
887 '!', '|'
890 /* Indexed by syntax code, give the object (cons of syntax code and
891 nil) to be stored in syntax table. Since these objects can be
892 shared among syntax tables, we generate them in advance. By
893 sharing objects, the function `describe-syntax' can give a more
894 compact listing. */
895 static Lisp_Object Vsyntax_code_object;
898 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
899 doc: /* Return the syntax code of CHARACTER, described by a character.
900 For example, if CHARACTER is a word constituent, the
901 character `w' (119) is returned.
902 The characters that correspond to various syntax codes
903 are listed in the documentation of `modify-syntax-entry'. */)
904 (Lisp_Object character)
906 int char_int;
907 CHECK_CHARACTER (character);
908 char_int = XINT (character);
909 SETUP_BUFFER_SYNTAX_TABLE ();
910 return make_number (syntax_code_spec[(int) SYNTAX (char_int)]);
913 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
914 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
915 (Lisp_Object character)
917 int char_int, code;
918 CHECK_NUMBER (character);
919 char_int = XINT (character);
920 SETUP_BUFFER_SYNTAX_TABLE ();
921 code = SYNTAX (char_int);
922 if (code == Sopen || code == Sclose)
923 return SYNTAX_MATCH (char_int);
924 return Qnil;
927 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
928 doc: /* Convert a syntax specification STRING into syntax cell form.
929 STRING should be a string as it is allowed as argument of
930 `modify-syntax-entry'. Value is the equivalent cons cell
931 \(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
932 text property. */)
933 (Lisp_Object string)
935 register const unsigned char *p;
936 register enum syntaxcode code;
937 int val;
938 Lisp_Object match;
940 CHECK_STRING (string);
942 p = SDATA (string);
943 code = (enum syntaxcode) syntax_spec_code[*p++];
944 if (((int) code & 0377) == 0377)
945 error ("Invalid syntax description letter: %c", p[-1]);
947 if (code == Sinherit)
948 return Qnil;
950 if (*p)
952 int len;
953 int character = STRING_CHAR_AND_LENGTH (p, len);
954 XSETINT (match, character);
955 if (XFASTINT (match) == ' ')
956 match = Qnil;
957 p += len;
959 else
960 match = Qnil;
962 val = (int) code;
963 while (*p)
964 switch (*p++)
966 case '1':
967 val |= 1 << 16;
968 break;
970 case '2':
971 val |= 1 << 17;
972 break;
974 case '3':
975 val |= 1 << 18;
976 break;
978 case '4':
979 val |= 1 << 19;
980 break;
982 case 'p':
983 val |= 1 << 20;
984 break;
986 case 'b':
987 val |= 1 << 21;
988 break;
990 case 'n':
991 val |= 1 << 22;
992 break;
994 case 'c':
995 val |= 1 << 23;
996 break;
999 if (val < XVECTOR (Vsyntax_code_object)->size && NILP (match))
1000 return XVECTOR (Vsyntax_code_object)->contents[val];
1001 else
1002 /* Since we can't use a shared object, let's make a new one. */
1003 return Fcons (make_number (val), match);
1006 /* I really don't know why this is interactive
1007 help-form should at least be made useful whilst reading the second arg. */
1008 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
1009 "cSet syntax for character: \nsSet syntax for %s to: ",
1010 doc: /* Set syntax for character CHAR according to string NEWENTRY.
1011 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1012 the current buffer's syntax table.
1013 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1014 in the range MIN to MAX are changed.
1015 The first character of NEWENTRY should be one of the following:
1016 Space or - whitespace syntax. w word constituent.
1017 _ symbol constituent. . punctuation.
1018 ( open-parenthesis. ) close-parenthesis.
1019 " string quote. \\ escape.
1020 $ paired delimiter. ' expression quote or prefix operator.
1021 < comment starter. > comment ender.
1022 / character-quote. @ inherit from `standard-syntax-table'.
1023 | generic string fence. ! generic comment fence.
1025 Only single-character comment start and end sequences are represented thus.
1026 Two-character sequences are represented as described below.
1027 The second character of NEWENTRY is the matching parenthesis,
1028 used only if the first character is `(' or `)'.
1029 Any additional characters are flags.
1030 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1031 1 means CHAR is the start of a two-char comment start sequence.
1032 2 means CHAR is the second character of such a sequence.
1033 3 means CHAR is the start of a two-char comment end sequence.
1034 4 means CHAR is the second character of such a sequence.
1036 There can be several orthogonal comment sequences. This is to support
1037 language modes such as C++. By default, all comment sequences are of style
1038 a, but you can set the comment sequence style to b (on the second character
1039 of a comment-start, and the first character of a comment-end sequence) and/or
1040 c (on any of its chars) using this flag:
1041 b means CHAR is part of comment sequence b.
1042 c means CHAR is part of comment sequence c.
1043 n means CHAR is part of a nestable comment sequence.
1045 p means CHAR is a prefix character for `backward-prefix-chars';
1046 such characters are treated as whitespace when they occur
1047 between expressions.
1048 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1049 (Lisp_Object c, Lisp_Object newentry, Lisp_Object syntax_table)
1051 if (CONSP (c))
1053 CHECK_CHARACTER_CAR (c);
1054 CHECK_CHARACTER_CDR (c);
1056 else
1057 CHECK_CHARACTER (c);
1059 if (NILP (syntax_table))
1060 syntax_table = current_buffer->syntax_table;
1061 else
1062 check_syntax_table (syntax_table);
1064 newentry = Fstring_to_syntax (newentry);
1065 if (CONSP (c))
1066 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1067 else
1068 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
1070 /* We clear the regexp cache, since character classes can now have
1071 different values from those in the compiled regexps.*/
1072 clear_regexp_cache ();
1074 return Qnil;
1077 /* Dump syntax table to buffer in human-readable format */
1079 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1080 Sinternal_describe_syntax_value, 1, 1, 0,
1081 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1082 (Lisp_Object syntax)
1084 register enum syntaxcode code;
1085 int syntax_code;
1086 char desc, start1, start2, end1, end2, prefix,
1087 comstyleb, comstylec, comnested;
1088 char str[2];
1089 Lisp_Object first, match_lisp, value = syntax;
1091 if (NILP (value))
1093 insert_string ("default");
1094 return syntax;
1097 if (CHAR_TABLE_P (value))
1099 insert_string ("deeper char-table ...");
1100 return syntax;
1103 if (!CONSP (value))
1105 insert_string ("invalid");
1106 return syntax;
1109 first = XCAR (value);
1110 match_lisp = XCDR (value);
1112 if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp)))
1114 insert_string ("invalid");
1115 return syntax;
1118 syntax_code = XINT (first);
1119 code = (enum syntaxcode) (syntax_code & 0377);
1120 start1 = SYNTAX_FLAGS_COMSTART_FIRST (syntax_code);
1121 start2 = SYNTAX_FLAGS_COMSTART_SECOND (syntax_code);;
1122 end1 = SYNTAX_FLAGS_COMEND_FIRST (syntax_code);
1123 end2 = SYNTAX_FLAGS_COMEND_SECOND (syntax_code);
1124 prefix = SYNTAX_FLAGS_PREFIX (syntax_code);
1125 comstyleb = SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code);
1126 comstylec = SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code);
1127 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax_code);
1129 if ((int) code < 0 || (int) code >= (int) Smax)
1131 insert_string ("invalid");
1132 return syntax;
1134 desc = syntax_code_spec[(int) code];
1136 str[0] = desc, str[1] = 0;
1137 insert (str, 1);
1139 if (NILP (match_lisp))
1140 insert (" ", 1);
1141 else
1142 insert_char (XINT (match_lisp));
1144 if (start1)
1145 insert ("1", 1);
1146 if (start2)
1147 insert ("2", 1);
1149 if (end1)
1150 insert ("3", 1);
1151 if (end2)
1152 insert ("4", 1);
1154 if (prefix)
1155 insert ("p", 1);
1156 if (comstyleb)
1157 insert ("b", 1);
1158 if (comstylec)
1159 insert ("c", 1);
1160 if (comnested)
1161 insert ("n", 1);
1163 insert_string ("\twhich means: ");
1165 switch (SWITCH_ENUM_CAST (code))
1167 case Swhitespace:
1168 insert_string ("whitespace"); break;
1169 case Spunct:
1170 insert_string ("punctuation"); break;
1171 case Sword:
1172 insert_string ("word"); break;
1173 case Ssymbol:
1174 insert_string ("symbol"); break;
1175 case Sopen:
1176 insert_string ("open"); break;
1177 case Sclose:
1178 insert_string ("close"); break;
1179 case Squote:
1180 insert_string ("prefix"); break;
1181 case Sstring:
1182 insert_string ("string"); break;
1183 case Smath:
1184 insert_string ("math"); break;
1185 case Sescape:
1186 insert_string ("escape"); break;
1187 case Scharquote:
1188 insert_string ("charquote"); break;
1189 case Scomment:
1190 insert_string ("comment"); break;
1191 case Sendcomment:
1192 insert_string ("endcomment"); break;
1193 case Sinherit:
1194 insert_string ("inherit"); break;
1195 case Scomment_fence:
1196 insert_string ("comment fence"); break;
1197 case Sstring_fence:
1198 insert_string ("string fence"); break;
1199 default:
1200 insert_string ("invalid");
1201 return syntax;
1204 if (!NILP (match_lisp))
1206 insert_string (", matches ");
1207 insert_char (XINT (match_lisp));
1210 if (start1)
1211 insert_string (",\n\t is the first character of a comment-start sequence");
1212 if (start2)
1213 insert_string (",\n\t is the second character of a comment-start sequence");
1215 if (end1)
1216 insert_string (",\n\t is the first character of a comment-end sequence");
1217 if (end2)
1218 insert_string (",\n\t is the second character of a comment-end sequence");
1219 if (comstyleb)
1220 insert_string (" (comment style b)");
1221 if (comstylec)
1222 insert_string (" (comment style c)");
1223 if (comnested)
1224 insert_string (" (nestable)");
1226 if (prefix)
1227 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1229 return syntax;
1232 int parse_sexp_ignore_comments;
1234 /* Char-table of functions that find the next or previous word
1235 boundary. */
1236 Lisp_Object Vfind_word_boundary_function_table;
1238 /* Return the position across COUNT words from FROM.
1239 If that many words cannot be found before the end of the buffer, return 0.
1240 COUNT negative means scan backward and stop at word beginning. */
1243 scan_words (register int from, register int count)
1245 register int beg = BEGV;
1246 register int end = ZV;
1247 register int from_byte = CHAR_TO_BYTE (from);
1248 register enum syntaxcode code;
1249 int ch0, ch1;
1250 Lisp_Object func, script, pos;
1252 immediate_quit = 1;
1253 QUIT;
1255 SETUP_SYNTAX_TABLE (from, count);
1257 while (count > 0)
1259 while (1)
1261 if (from == end)
1263 immediate_quit = 0;
1264 return 0;
1266 UPDATE_SYNTAX_TABLE_FORWARD (from);
1267 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1268 code = SYNTAX (ch0);
1269 INC_BOTH (from, from_byte);
1270 if (words_include_escapes
1271 && (code == Sescape || code == Scharquote))
1272 break;
1273 if (code == Sword)
1274 break;
1276 /* Now CH0 is a character which begins a word and FROM is the
1277 position of the next character. */
1278 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
1279 if (! NILP (Ffboundp (func)))
1281 pos = call2 (func, make_number (from - 1), make_number (end));
1282 if (INTEGERP (pos) && XINT (pos) > from)
1284 from = XINT (pos);
1285 from_byte = CHAR_TO_BYTE (from);
1288 else
1290 script = CHAR_TABLE_REF (Vchar_script_table, ch0);
1291 while (1)
1293 if (from == end) break;
1294 UPDATE_SYNTAX_TABLE_FORWARD (from);
1295 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1296 code = SYNTAX (ch1);
1297 if ((code != Sword
1298 && (! words_include_escapes
1299 || (code != Sescape && code != Scharquote)))
1300 || word_boundary_p (ch0, ch1))
1301 break;
1302 INC_BOTH (from, from_byte);
1303 ch0 = ch1;
1306 count--;
1308 while (count < 0)
1310 while (1)
1312 if (from == beg)
1314 immediate_quit = 0;
1315 return 0;
1317 DEC_BOTH (from, from_byte);
1318 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1319 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1320 code = SYNTAX (ch1);
1321 if (words_include_escapes
1322 && (code == Sescape || code == Scharquote))
1323 break;
1324 if (code == Sword)
1325 break;
1327 /* Now CH1 is a character which ends a word and FROM is the
1328 position of it. */
1329 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
1330 if (! NILP (Ffboundp (func)))
1332 pos = call2 (func, make_number (from), make_number (beg));
1333 if (INTEGERP (pos) && XINT (pos) < from)
1335 from = XINT (pos);
1336 from_byte = CHAR_TO_BYTE (from);
1339 else
1341 script = CHAR_TABLE_REF (Vchar_script_table, ch1);
1342 while (1)
1344 if (from == beg)
1345 break;
1346 DEC_BOTH (from, from_byte);
1347 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1348 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1349 code = SYNTAX (ch0);
1350 if ((code != Sword
1351 && (! words_include_escapes
1352 || (code != Sescape && code != Scharquote)))
1353 || word_boundary_p (ch0, ch1))
1355 INC_BOTH (from, from_byte);
1356 break;
1358 ch1 = ch0;
1361 count++;
1364 immediate_quit = 0;
1366 return from;
1369 DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p",
1370 doc: /* Move point forward ARG words (backward if ARG is negative).
1371 Normally returns t.
1372 If an edge of the buffer or a field boundary is reached, point is left there
1373 and the function returns nil. Field boundaries are not noticed if
1374 `inhibit-field-text-motion' is non-nil. */)
1375 (Lisp_Object arg)
1377 Lisp_Object tmp;
1378 int orig_val, val;
1380 if (NILP (arg))
1381 XSETFASTINT (arg, 1);
1382 else
1383 CHECK_NUMBER (arg);
1385 val = orig_val = scan_words (PT, XINT (arg));
1386 if (! orig_val)
1387 val = XINT (arg) > 0 ? ZV : BEGV;
1389 /* Avoid jumping out of an input field. */
1390 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1391 Qt, Qnil, Qnil);
1392 val = XFASTINT (tmp);
1394 SET_PT (val);
1395 return val == orig_val ? Qt : Qnil;
1398 Lisp_Object skip_chars (int, Lisp_Object, Lisp_Object, int);
1400 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1401 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1402 STRING is like the inside of a `[...]' in a regular expression
1403 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1404 (but not at the end of a range; quoting is never needed there).
1405 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1406 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1407 Char classes, e.g. `[:alpha:]', are supported.
1409 Returns the distance traveled, either zero or positive. */)
1410 (Lisp_Object string, Lisp_Object lim)
1412 return skip_chars (1, string, lim, 1);
1415 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1416 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1417 See `skip-chars-forward' for details.
1418 Returns the distance traveled, either zero or negative. */)
1419 (Lisp_Object string, Lisp_Object lim)
1421 return skip_chars (0, string, lim, 1);
1424 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1425 doc: /* Move point forward across chars in specified syntax classes.
1426 SYNTAX is a string of syntax code characters.
1427 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1428 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1429 This function returns the distance traveled, either zero or positive. */)
1430 (Lisp_Object syntax, Lisp_Object lim)
1432 return skip_syntaxes (1, syntax, lim);
1435 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1436 doc: /* Move point backward across chars in specified syntax classes.
1437 SYNTAX is a string of syntax code characters.
1438 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1439 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1440 This function returns the distance traveled, either zero or negative. */)
1441 (Lisp_Object syntax, Lisp_Object lim)
1443 return skip_syntaxes (0, syntax, lim);
1446 static Lisp_Object
1447 skip_chars (int forwardp, Lisp_Object string, Lisp_Object lim, int handle_iso_classes)
1449 register unsigned int c;
1450 unsigned char fastmap[0400];
1451 /* Store the ranges of non-ASCII characters. */
1452 int *char_ranges;
1453 int n_char_ranges = 0;
1454 int negate = 0;
1455 register int i, i_byte;
1456 /* Set to 1 if the current buffer is multibyte and the region
1457 contains non-ASCII chars. */
1458 int multibyte;
1459 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1460 chars. */
1461 int string_multibyte;
1462 int size_byte;
1463 const unsigned char *str;
1464 int len;
1465 Lisp_Object iso_classes;
1467 CHECK_STRING (string);
1468 iso_classes = Qnil;
1470 if (NILP (lim))
1471 XSETINT (lim, forwardp ? ZV : BEGV);
1472 else
1473 CHECK_NUMBER_COERCE_MARKER (lim);
1475 /* In any case, don't allow scan outside bounds of buffer. */
1476 if (XINT (lim) > ZV)
1477 XSETFASTINT (lim, ZV);
1478 if (XINT (lim) < BEGV)
1479 XSETFASTINT (lim, BEGV);
1481 multibyte = (!NILP (current_buffer->enable_multibyte_characters)
1482 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1483 string_multibyte = SBYTES (string) > SCHARS (string);
1485 memset (fastmap, 0, sizeof fastmap);
1487 str = SDATA (string);
1488 size_byte = SBYTES (string);
1490 i_byte = 0;
1491 if (i_byte < size_byte
1492 && SREF (string, 0) == '^')
1494 negate = 1; i_byte++;
1497 /* Find the characters specified and set their elements of fastmap.
1498 Handle backslashes and ranges specially.
1500 If STRING contains non-ASCII characters, setup char_ranges for
1501 them and use fastmap only for their leading codes. */
1503 if (! string_multibyte)
1505 int string_has_eight_bit = 0;
1507 /* At first setup fastmap. */
1508 while (i_byte < size_byte)
1510 c = str[i_byte++];
1512 if (handle_iso_classes && c == '['
1513 && i_byte < size_byte
1514 && str[i_byte] == ':')
1516 const unsigned char *class_beg = str + i_byte + 1;
1517 const unsigned char *class_end = class_beg;
1518 const unsigned char *class_limit = str + size_byte - 2;
1519 /* Leave room for the null. */
1520 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1521 re_wctype_t cc;
1523 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1524 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1526 while (class_end < class_limit
1527 && *class_end >= 'a' && *class_end <= 'z')
1528 class_end++;
1530 if (class_end == class_beg
1531 || *class_end != ':' || class_end[1] != ']')
1532 goto not_a_class_name;
1534 memcpy (class_name, class_beg, class_end - class_beg);
1535 class_name[class_end - class_beg] = 0;
1537 cc = re_wctype (class_name);
1538 if (cc == 0)
1539 error ("Invalid ISO C character class");
1541 iso_classes = Fcons (make_number (cc), iso_classes);
1543 i_byte = class_end + 2 - str;
1544 continue;
1547 not_a_class_name:
1548 if (c == '\\')
1550 if (i_byte == size_byte)
1551 break;
1553 c = str[i_byte++];
1555 /* Treat `-' as range character only if another character
1556 follows. */
1557 if (i_byte + 1 < size_byte
1558 && str[i_byte] == '-')
1560 unsigned int c2;
1562 /* Skip over the dash. */
1563 i_byte++;
1565 /* Get the end of the range. */
1566 c2 = str[i_byte++];
1567 if (c2 == '\\'
1568 && i_byte < size_byte)
1569 c2 = str[i_byte++];
1571 if (c <= c2)
1573 while (c <= c2)
1574 fastmap[c++] = 1;
1575 if (! ASCII_CHAR_P (c2))
1576 string_has_eight_bit = 1;
1579 else
1581 fastmap[c] = 1;
1582 if (! ASCII_CHAR_P (c))
1583 string_has_eight_bit = 1;
1587 /* If the current range is multibyte and STRING contains
1588 eight-bit chars, arrange fastmap and setup char_ranges for
1589 the corresponding multibyte chars. */
1590 if (multibyte && string_has_eight_bit)
1592 unsigned char fastmap2[0400];
1593 int range_start_byte, range_start_char;
1595 memcpy (fastmap + 0200, fastmap2 + 0200, 0200);
1596 memset (fastmap + 0200, 0, 0200);
1597 /* We are sure that this loop stops. */
1598 for (i = 0200; ! fastmap2[i]; i++);
1599 c = BYTE8_TO_CHAR (i);
1600 fastmap[CHAR_LEADING_CODE (c)] = 1;
1601 range_start_byte = i;
1602 range_start_char = c;
1603 char_ranges = (int *) alloca (sizeof (int) * 128 * 2);
1604 for (i = 129; i < 0400; i++)
1606 c = BYTE8_TO_CHAR (i);
1607 fastmap[CHAR_LEADING_CODE (c)] = 1;
1608 if (i - range_start_byte != c - range_start_char)
1610 char_ranges[n_char_ranges++] = range_start_char;
1611 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1612 + range_start_char);
1613 range_start_byte = i;
1614 range_start_char = c;
1617 char_ranges[n_char_ranges++] = range_start_char;
1618 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1619 + range_start_char);
1622 else /* STRING is multibyte */
1624 char_ranges = (int *) alloca (sizeof (int) * SCHARS (string) * 2);
1626 while (i_byte < size_byte)
1628 unsigned char leading_code;
1630 leading_code = str[i_byte];
1631 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1632 i_byte += len;
1634 if (handle_iso_classes && c == '['
1635 && i_byte < size_byte
1636 && STRING_CHAR (str + i_byte) == ':')
1638 const unsigned char *class_beg = str + i_byte + 1;
1639 const unsigned char *class_end = class_beg;
1640 const unsigned char *class_limit = str + size_byte - 2;
1641 /* Leave room for the null. */
1642 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1643 re_wctype_t cc;
1645 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1646 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1648 while (class_end < class_limit
1649 && *class_end >= 'a' && *class_end <= 'z')
1650 class_end++;
1652 if (class_end == class_beg
1653 || *class_end != ':' || class_end[1] != ']')
1654 goto not_a_class_name_multibyte;
1656 memcpy (class_name, class_beg, class_end - class_beg);
1657 class_name[class_end - class_beg] = 0;
1659 cc = re_wctype (class_name);
1660 if (cc == 0)
1661 error ("Invalid ISO C character class");
1663 iso_classes = Fcons (make_number (cc), iso_classes);
1665 i_byte = class_end + 2 - str;
1666 continue;
1669 not_a_class_name_multibyte:
1670 if (c == '\\')
1672 if (i_byte == size_byte)
1673 break;
1675 leading_code = str[i_byte];
1676 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1677 i_byte += len;
1679 /* Treat `-' as range character only if another character
1680 follows. */
1681 if (i_byte + 1 < size_byte
1682 && str[i_byte] == '-')
1684 unsigned int c2;
1685 unsigned char leading_code2;
1687 /* Skip over the dash. */
1688 i_byte++;
1690 /* Get the end of the range. */
1691 leading_code2 = str[i_byte];
1692 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1693 i_byte += len;
1695 if (c2 == '\\'
1696 && i_byte < size_byte)
1698 leading_code2 = str[i_byte];
1699 c2 =STRING_CHAR_AND_LENGTH (str + i_byte, len);
1700 i_byte += len;
1703 if (c > c2)
1704 continue;
1705 if (ASCII_CHAR_P (c))
1707 while (c <= c2 && c < 0x80)
1708 fastmap[c++] = 1;
1709 leading_code = CHAR_LEADING_CODE (c);
1711 if (! ASCII_CHAR_P (c))
1713 while (leading_code <= leading_code2)
1714 fastmap[leading_code++] = 1;
1715 if (c <= c2)
1717 char_ranges[n_char_ranges++] = c;
1718 char_ranges[n_char_ranges++] = c2;
1722 else
1724 if (ASCII_CHAR_P (c))
1725 fastmap[c] = 1;
1726 else
1728 fastmap[leading_code] = 1;
1729 char_ranges[n_char_ranges++] = c;
1730 char_ranges[n_char_ranges++] = c;
1735 /* If the current range is unibyte and STRING contains non-ASCII
1736 chars, arrange fastmap for the corresponding unibyte
1737 chars. */
1739 if (! multibyte && n_char_ranges > 0)
1741 memset (fastmap + 0200, 0, 0200);
1742 for (i = 0; i < n_char_ranges; i += 2)
1744 int c1 = char_ranges[i];
1745 int c2 = char_ranges[i + 1];
1747 for (; c1 <= c2; c1++)
1749 int b = CHAR_TO_BYTE_SAFE (c1);
1750 if (b >= 0)
1751 fastmap[b] = 1;
1757 /* If ^ was the first character, complement the fastmap. */
1758 if (negate)
1760 if (! multibyte)
1761 for (i = 0; i < sizeof fastmap; i++)
1762 fastmap[i] ^= 1;
1763 else
1765 for (i = 0; i < 0200; i++)
1766 fastmap[i] ^= 1;
1767 /* All non-ASCII chars possibly match. */
1768 for (; i < sizeof fastmap; i++)
1769 fastmap[i] = 1;
1774 int start_point = PT;
1775 int pos = PT;
1776 int pos_byte = PT_BYTE;
1777 unsigned char *p = PT_ADDR, *endp, *stop;
1779 if (forwardp)
1781 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1782 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1784 else
1786 endp = CHAR_POS_ADDR (XINT (lim));
1787 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1790 immediate_quit = 1;
1791 /* This code may look up syntax tables using macros that rely on the
1792 gl_state object. To make sure this object is not out of date,
1793 let's initialize it manually.
1794 We ignore syntax-table text-properties for now, since that's
1795 what we've done in the past. */
1796 SETUP_BUFFER_SYNTAX_TABLE ();
1797 if (forwardp)
1799 if (multibyte)
1800 while (1)
1802 int nbytes;
1804 if (p >= stop)
1806 if (p >= endp)
1807 break;
1808 p = GAP_END_ADDR;
1809 stop = endp;
1811 c = STRING_CHAR_AND_LENGTH (p, nbytes);
1812 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1814 if (negate)
1815 break;
1816 else
1817 goto fwd_ok;
1820 if (! fastmap[*p])
1821 break;
1822 if (! ASCII_CHAR_P (c))
1824 /* As we are looking at a multibyte character, we
1825 must look up the character in the table
1826 CHAR_RANGES. If there's no data in the table,
1827 that character is not what we want to skip. */
1829 /* The following code do the right thing even if
1830 n_char_ranges is zero (i.e. no data in
1831 CHAR_RANGES). */
1832 for (i = 0; i < n_char_ranges; i += 2)
1833 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1834 break;
1835 if (!(negate ^ (i < n_char_ranges)))
1836 break;
1838 fwd_ok:
1839 p += nbytes, pos++, pos_byte += nbytes;
1841 else
1842 while (1)
1844 if (p >= stop)
1846 if (p >= endp)
1847 break;
1848 p = GAP_END_ADDR;
1849 stop = endp;
1852 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1854 if (negate)
1855 break;
1856 else
1857 goto fwd_unibyte_ok;
1860 if (!fastmap[*p])
1861 break;
1862 fwd_unibyte_ok:
1863 p++, pos++, pos_byte++;
1866 else
1868 if (multibyte)
1869 while (1)
1871 unsigned char *prev_p;
1873 if (p <= stop)
1875 if (p <= endp)
1876 break;
1877 p = GPT_ADDR;
1878 stop = endp;
1880 prev_p = p;
1881 while (--p >= stop && ! CHAR_HEAD_P (*p));
1882 c = STRING_CHAR (p);
1884 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1886 if (negate)
1887 break;
1888 else
1889 goto back_ok;
1892 if (! fastmap[*p])
1893 break;
1894 if (! ASCII_CHAR_P (c))
1896 /* See the comment in the previous similar code. */
1897 for (i = 0; i < n_char_ranges; i += 2)
1898 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1899 break;
1900 if (!(negate ^ (i < n_char_ranges)))
1901 break;
1903 back_ok:
1904 pos--, pos_byte -= prev_p - p;
1906 else
1907 while (1)
1909 if (p <= stop)
1911 if (p <= endp)
1912 break;
1913 p = GPT_ADDR;
1914 stop = endp;
1917 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
1919 if (negate)
1920 break;
1921 else
1922 goto back_unibyte_ok;
1925 if (!fastmap[p[-1]])
1926 break;
1927 back_unibyte_ok:
1928 p--, pos--, pos_byte--;
1932 SET_PT_BOTH (pos, pos_byte);
1933 immediate_quit = 0;
1935 return make_number (PT - start_point);
1940 static Lisp_Object
1941 skip_syntaxes (int forwardp, Lisp_Object string, Lisp_Object lim)
1943 register unsigned int c;
1944 unsigned char fastmap[0400];
1945 int negate = 0;
1946 register int i, i_byte;
1947 int multibyte;
1948 int size_byte;
1949 unsigned char *str;
1951 CHECK_STRING (string);
1953 if (NILP (lim))
1954 XSETINT (lim, forwardp ? ZV : BEGV);
1955 else
1956 CHECK_NUMBER_COERCE_MARKER (lim);
1958 /* In any case, don't allow scan outside bounds of buffer. */
1959 if (XINT (lim) > ZV)
1960 XSETFASTINT (lim, ZV);
1961 if (XINT (lim) < BEGV)
1962 XSETFASTINT (lim, BEGV);
1964 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
1965 return make_number (0);
1967 multibyte = (!NILP (current_buffer->enable_multibyte_characters)
1968 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1970 memset (fastmap, 0, sizeof fastmap);
1972 if (SBYTES (string) > SCHARS (string))
1973 /* As this is very rare case (syntax spec is ASCII only), don't
1974 consider efficiency. */
1975 string = string_make_unibyte (string);
1977 str = SDATA (string);
1978 size_byte = SBYTES (string);
1980 i_byte = 0;
1981 if (i_byte < size_byte
1982 && SREF (string, 0) == '^')
1984 negate = 1; i_byte++;
1987 /* Find the syntaxes specified and set their elements of fastmap. */
1989 while (i_byte < size_byte)
1991 c = str[i_byte++];
1992 fastmap[syntax_spec_code[c]] = 1;
1995 /* If ^ was the first character, complement the fastmap. */
1996 if (negate)
1997 for (i = 0; i < sizeof fastmap; i++)
1998 fastmap[i] ^= 1;
2001 int start_point = PT;
2002 int pos = PT;
2003 int pos_byte = PT_BYTE;
2004 unsigned char *p = PT_ADDR, *endp, *stop;
2006 if (forwardp)
2008 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
2009 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
2011 else
2013 endp = CHAR_POS_ADDR (XINT (lim));
2014 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
2017 immediate_quit = 1;
2018 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
2019 if (forwardp)
2021 if (multibyte)
2023 while (1)
2025 int nbytes;
2027 if (p >= stop)
2029 if (p >= endp)
2030 break;
2031 p = GAP_END_ADDR;
2032 stop = endp;
2034 c = STRING_CHAR_AND_LENGTH (p, nbytes);
2035 if (! fastmap[(int) SYNTAX (c)])
2036 break;
2037 p += nbytes, pos++, pos_byte += nbytes;
2038 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2041 else
2043 while (1)
2045 if (p >= stop)
2047 if (p >= endp)
2048 break;
2049 p = GAP_END_ADDR;
2050 stop = endp;
2052 if (! fastmap[(int) SYNTAX (*p)])
2053 break;
2054 p++, pos++, pos_byte++;
2055 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2059 else
2061 if (multibyte)
2063 while (1)
2065 unsigned char *prev_p;
2067 if (p <= stop)
2069 if (p <= endp)
2070 break;
2071 p = GPT_ADDR;
2072 stop = endp;
2074 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2075 prev_p = p;
2076 while (--p >= stop && ! CHAR_HEAD_P (*p));
2077 c = STRING_CHAR (p);
2078 if (! fastmap[(int) SYNTAX (c)])
2079 break;
2080 pos--, pos_byte -= prev_p - p;
2083 else
2085 while (1)
2087 if (p <= stop)
2089 if (p <= endp)
2090 break;
2091 p = GPT_ADDR;
2092 stop = endp;
2094 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2095 if (! fastmap[(int) SYNTAX (p[-1])])
2096 break;
2097 p--, pos--, pos_byte--;
2102 SET_PT_BOTH (pos, pos_byte);
2103 immediate_quit = 0;
2105 return make_number (PT - start_point);
2109 /* Return 1 if character C belongs to one of the ISO classes
2110 in the list ISO_CLASSES. Each class is represented by an
2111 integer which is its type according to re_wctype. */
2113 static int
2114 in_classes (int c, Lisp_Object iso_classes)
2116 int fits_class = 0;
2118 while (CONSP (iso_classes))
2120 Lisp_Object elt;
2121 elt = XCAR (iso_classes);
2122 iso_classes = XCDR (iso_classes);
2124 if (re_iswctype (c, XFASTINT (elt)))
2125 fits_class = 1;
2128 return fits_class;
2131 /* Jump over a comment, assuming we are at the beginning of one.
2132 FROM is the current position.
2133 FROM_BYTE is the bytepos corresponding to FROM.
2134 Do not move past STOP (a charpos).
2135 The comment over which we have to jump is of style STYLE
2136 (either SYNTAX_FLAGS_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2137 NESTING should be positive to indicate the nesting at the beginning
2138 for nested comments and should be zero or negative else.
2139 ST_COMMENT_STYLE cannot be nested.
2140 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2141 (or 0 If the search cannot start in the middle of a two-character).
2143 If successful, return 1 and store the charpos of the comment's end
2144 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2145 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2146 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2147 (as defined for state.incomment) in *INCOMMENT_PTR.
2149 The comment end is the last character of the comment rather than the
2150 character just after the comment.
2152 Global syntax data is assumed to initially be valid for FROM and
2153 remains valid for forward search starting at the returned position. */
2155 static int
2156 forw_comment (EMACS_INT from, EMACS_INT from_byte, EMACS_INT stop,
2157 int nesting, int style, int prev_syntax,
2158 EMACS_INT *charpos_ptr, EMACS_INT *bytepos_ptr,
2159 int *incomment_ptr)
2161 register int c, c1;
2162 register enum syntaxcode code;
2163 register int syntax, other_syntax;
2165 if (nesting <= 0) nesting = -1;
2167 /* Enter the loop in the middle so that we find
2168 a 2-char comment ender if we start in the middle of it. */
2169 syntax = prev_syntax;
2170 if (syntax != 0) goto forw_incomment;
2172 while (1)
2174 if (from == stop)
2176 *incomment_ptr = nesting;
2177 *charpos_ptr = from;
2178 *bytepos_ptr = from_byte;
2179 return 0;
2181 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2182 syntax = SYNTAX_WITH_FLAGS (c);
2183 code = syntax & 0xff;
2184 if (code == Sendcomment
2185 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style
2186 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2187 (nesting > 0 && --nesting == 0) : nesting < 0))
2188 /* we have encountered a comment end of the same style
2189 as the comment sequence which began this comment
2190 section */
2191 break;
2192 if (code == Scomment_fence
2193 && style == ST_COMMENT_STYLE)
2194 /* we have encountered a comment end of the same style
2195 as the comment sequence which began this comment
2196 section. */
2197 break;
2198 if (nesting > 0
2199 && code == Scomment
2200 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2201 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style)
2202 /* we have encountered a nested comment of the same style
2203 as the comment sequence which began this comment section */
2204 nesting++;
2205 INC_BOTH (from, from_byte);
2206 UPDATE_SYNTAX_TABLE_FORWARD (from);
2208 forw_incomment:
2209 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2210 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2211 other_syntax = SYNTAX_WITH_FLAGS (c1),
2212 SYNTAX_FLAGS_COMEND_SECOND (other_syntax))
2213 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, other_syntax) == style
2214 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2215 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax))
2216 ? nesting > 0 : nesting < 0))
2218 if (--nesting <= 0)
2219 /* we have encountered a comment end of the same style
2220 as the comment sequence which began this comment
2221 section */
2222 break;
2223 else
2225 INC_BOTH (from, from_byte);
2226 UPDATE_SYNTAX_TABLE_FORWARD (from);
2229 if (nesting > 0
2230 && from < stop
2231 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2232 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2233 other_syntax = SYNTAX_WITH_FLAGS (c1),
2234 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax) == style
2235 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2236 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2237 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax)))
2238 /* we have encountered a nested comment of the same style
2239 as the comment sequence which began this comment
2240 section */
2242 INC_BOTH (from, from_byte);
2243 UPDATE_SYNTAX_TABLE_FORWARD (from);
2244 nesting++;
2247 *charpos_ptr = from;
2248 *bytepos_ptr = from_byte;
2249 return 1;
2252 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2253 doc: /*
2254 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2255 Stop scanning if we find something other than a comment or whitespace.
2256 Set point to where scanning stops.
2257 If COUNT comments are found as expected, with nothing except whitespace
2258 between them, return t; otherwise return nil. */)
2259 (Lisp_Object count)
2261 register EMACS_INT from;
2262 EMACS_INT from_byte;
2263 register EMACS_INT stop;
2264 register int c, c1;
2265 register enum syntaxcode code;
2266 int comstyle = 0; /* style of comment encountered */
2267 int comnested = 0; /* whether the comment is nestable or not */
2268 int found;
2269 EMACS_INT count1;
2270 EMACS_INT out_charpos, out_bytepos;
2271 int dummy;
2273 CHECK_NUMBER (count);
2274 count1 = XINT (count);
2275 stop = count1 > 0 ? ZV : BEGV;
2277 immediate_quit = 1;
2278 QUIT;
2280 from = PT;
2281 from_byte = PT_BYTE;
2283 SETUP_SYNTAX_TABLE (from, count1);
2284 while (count1 > 0)
2288 int comstart_first, syntax, other_syntax;
2290 if (from == stop)
2292 SET_PT_BOTH (from, from_byte);
2293 immediate_quit = 0;
2294 return Qnil;
2296 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2297 syntax = SYNTAX_WITH_FLAGS (c);
2298 code = SYNTAX (c);
2299 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2300 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2301 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2302 INC_BOTH (from, from_byte);
2303 UPDATE_SYNTAX_TABLE_FORWARD (from);
2304 if (from < stop && comstart_first
2305 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2306 other_syntax = SYNTAX_WITH_FLAGS (c1),
2307 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)))
2309 /* We have encountered a comment start sequence and we
2310 are ignoring all text inside comments. We must record
2311 the comment style this sequence begins so that later,
2312 only a comment end of the same style actually ends
2313 the comment section. */
2314 code = Scomment;
2315 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2316 comnested
2317 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2318 INC_BOTH (from, from_byte);
2319 UPDATE_SYNTAX_TABLE_FORWARD (from);
2322 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2324 if (code == Scomment_fence)
2325 comstyle = ST_COMMENT_STYLE;
2326 else if (code != Scomment)
2328 immediate_quit = 0;
2329 DEC_BOTH (from, from_byte);
2330 SET_PT_BOTH (from, from_byte);
2331 return Qnil;
2333 /* We're at the start of a comment. */
2334 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2335 &out_charpos, &out_bytepos, &dummy);
2336 from = out_charpos; from_byte = out_bytepos;
2337 if (!found)
2339 immediate_quit = 0;
2340 SET_PT_BOTH (from, from_byte);
2341 return Qnil;
2343 INC_BOTH (from, from_byte);
2344 UPDATE_SYNTAX_TABLE_FORWARD (from);
2345 /* We have skipped one comment. */
2346 count1--;
2349 while (count1 < 0)
2351 while (1)
2353 int quoted, syntax;
2355 if (from <= stop)
2357 SET_PT_BOTH (BEGV, BEGV_BYTE);
2358 immediate_quit = 0;
2359 return Qnil;
2362 DEC_BOTH (from, from_byte);
2363 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2364 quoted = char_quoted (from, from_byte);
2365 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2366 syntax = SYNTAX_WITH_FLAGS (c);
2367 code = SYNTAX (c);
2368 comstyle = 0;
2369 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2370 if (code == Sendcomment)
2371 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2372 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2373 && prev_char_comend_first (from, from_byte)
2374 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2376 int other_syntax;
2377 /* We must record the comment style encountered so that
2378 later, we can match only the proper comment begin
2379 sequence of the same style. */
2380 DEC_BOTH (from, from_byte);
2381 code = Sendcomment;
2382 /* Calling char_quoted, above, set up global syntax position
2383 at the new value of FROM. */
2384 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2385 other_syntax = SYNTAX_WITH_FLAGS (c1);
2386 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2387 comnested
2388 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2391 if (code == Scomment_fence)
2393 /* Skip until first preceding unquoted comment_fence. */
2394 int found = 0, ini = from, ini_byte = from_byte;
2396 while (1)
2398 DEC_BOTH (from, from_byte);
2399 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2400 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2401 if (SYNTAX (c) == Scomment_fence
2402 && !char_quoted (from, from_byte))
2404 found = 1;
2405 break;
2407 else if (from == stop)
2408 break;
2410 if (found == 0)
2412 from = ini; /* Set point to ini + 1. */
2413 from_byte = ini_byte;
2414 goto leave;
2416 else
2417 /* We have skipped one comment. */
2418 break;
2420 else if (code == Sendcomment)
2422 found = back_comment (from, from_byte, stop, comnested, comstyle,
2423 &out_charpos, &out_bytepos);
2424 if (found == -1)
2426 if (c == '\n')
2427 /* This end-of-line is not an end-of-comment.
2428 Treat it like a whitespace.
2429 CC-mode (and maybe others) relies on this behavior. */
2431 else
2433 /* Failure: we should go back to the end of this
2434 not-quite-endcomment. */
2435 if (SYNTAX (c) != code)
2436 /* It was a two-char Sendcomment. */
2437 INC_BOTH (from, from_byte);
2438 goto leave;
2441 else
2443 /* We have skipped one comment. */
2444 from = out_charpos, from_byte = out_bytepos;
2445 break;
2448 else if (code != Swhitespace || quoted)
2450 leave:
2451 immediate_quit = 0;
2452 INC_BOTH (from, from_byte);
2453 SET_PT_BOTH (from, from_byte);
2454 return Qnil;
2458 count1++;
2461 SET_PT_BOTH (from, from_byte);
2462 immediate_quit = 0;
2463 return Qt;
2466 /* Return syntax code of character C if C is an ASCII character
2467 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2469 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2470 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2471 ? SYNTAX (c) : Ssymbol)
2473 static Lisp_Object
2474 scan_lists (register EMACS_INT from, EMACS_INT count, EMACS_INT depth, int sexpflag)
2476 Lisp_Object val;
2477 register EMACS_INT stop = count > 0 ? ZV : BEGV;
2478 register int c, c1;
2479 int stringterm;
2480 int quoted;
2481 int mathexit = 0;
2482 register enum syntaxcode code, temp_code;
2483 int min_depth = depth; /* Err out if depth gets less than this. */
2484 int comstyle = 0; /* style of comment encountered */
2485 int comnested = 0; /* whether the comment is nestable or not */
2486 EMACS_INT temp_pos;
2487 EMACS_INT last_good = from;
2488 int found;
2489 EMACS_INT from_byte;
2490 EMACS_INT out_bytepos, out_charpos;
2491 int temp, dummy;
2492 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2494 if (depth > 0) min_depth = 0;
2496 if (from > ZV) from = ZV;
2497 if (from < BEGV) from = BEGV;
2499 from_byte = CHAR_TO_BYTE (from);
2501 immediate_quit = 1;
2502 QUIT;
2504 SETUP_SYNTAX_TABLE (from, count);
2505 while (count > 0)
2507 while (from < stop)
2509 int comstart_first, prefix, syntax, other_syntax;
2510 UPDATE_SYNTAX_TABLE_FORWARD (from);
2511 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2512 syntax = SYNTAX_WITH_FLAGS (c);
2513 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2514 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2515 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2516 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2517 prefix = SYNTAX_FLAGS_PREFIX (syntax);
2518 if (depth == min_depth)
2519 last_good = from;
2520 INC_BOTH (from, from_byte);
2521 UPDATE_SYNTAX_TABLE_FORWARD (from);
2522 if (from < stop && comstart_first
2523 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2524 other_syntax = SYNTAX_WITH_FLAGS (c),
2525 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2526 && parse_sexp_ignore_comments)
2528 /* we have encountered a comment start sequence and we
2529 are ignoring all text inside comments. We must record
2530 the comment style this sequence begins so that later,
2531 only a comment end of the same style actually ends
2532 the comment section */
2533 code = Scomment;
2534 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2535 comnested
2536 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2537 INC_BOTH (from, from_byte);
2538 UPDATE_SYNTAX_TABLE_FORWARD (from);
2541 if (prefix)
2542 continue;
2544 switch (SWITCH_ENUM_CAST (code))
2546 case Sescape:
2547 case Scharquote:
2548 if (from == stop)
2549 goto lose;
2550 INC_BOTH (from, from_byte);
2551 /* treat following character as a word constituent */
2552 case Sword:
2553 case Ssymbol:
2554 if (depth || !sexpflag) break;
2555 /* This word counts as a sexp; return at end of it. */
2556 while (from < stop)
2558 UPDATE_SYNTAX_TABLE_FORWARD (from);
2560 /* Some compilers can't handle this inside the switch. */
2561 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2562 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2563 switch (temp)
2565 case Scharquote:
2566 case Sescape:
2567 INC_BOTH (from, from_byte);
2568 if (from == stop)
2569 goto lose;
2570 break;
2571 case Sword:
2572 case Ssymbol:
2573 case Squote:
2574 break;
2575 default:
2576 goto done;
2578 INC_BOTH (from, from_byte);
2580 goto done;
2582 case Scomment_fence:
2583 comstyle = ST_COMMENT_STYLE;
2584 /* FALLTHROUGH */
2585 case Scomment:
2586 if (!parse_sexp_ignore_comments) break;
2587 UPDATE_SYNTAX_TABLE_FORWARD (from);
2588 found = forw_comment (from, from_byte, stop,
2589 comnested, comstyle, 0,
2590 &out_charpos, &out_bytepos, &dummy);
2591 from = out_charpos, from_byte = out_bytepos;
2592 if (!found)
2594 if (depth == 0)
2595 goto done;
2596 goto lose;
2598 INC_BOTH (from, from_byte);
2599 UPDATE_SYNTAX_TABLE_FORWARD (from);
2600 break;
2602 case Smath:
2603 if (!sexpflag)
2604 break;
2605 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2607 INC_BOTH (from, from_byte);
2609 if (mathexit)
2611 mathexit = 0;
2612 goto close1;
2614 mathexit = 1;
2616 case Sopen:
2617 if (!++depth) goto done;
2618 break;
2620 case Sclose:
2621 close1:
2622 if (!--depth) goto done;
2623 if (depth < min_depth)
2624 xsignal3 (Qscan_error,
2625 build_string ("Containing expression ends prematurely"),
2626 make_number (last_good), make_number (from));
2627 break;
2629 case Sstring:
2630 case Sstring_fence:
2631 temp_pos = dec_bytepos (from_byte);
2632 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2633 while (1)
2635 if (from >= stop)
2636 goto lose;
2637 UPDATE_SYNTAX_TABLE_FORWARD (from);
2638 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2639 if (code == Sstring
2640 ? (c == stringterm
2641 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2642 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
2643 break;
2645 /* Some compilers can't handle this inside the switch. */
2646 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2647 switch (temp)
2649 case Scharquote:
2650 case Sescape:
2651 INC_BOTH (from, from_byte);
2653 INC_BOTH (from, from_byte);
2655 INC_BOTH (from, from_byte);
2656 if (!depth && sexpflag) goto done;
2657 break;
2658 default:
2659 /* Ignore whitespace, punctuation, quote, endcomment. */
2660 break;
2664 /* Reached end of buffer. Error if within object, return nil if between */
2665 if (depth)
2666 goto lose;
2668 immediate_quit = 0;
2669 return Qnil;
2671 /* End of object reached */
2672 done:
2673 count--;
2676 while (count < 0)
2678 while (from > stop)
2680 int syntax;
2681 DEC_BOTH (from, from_byte);
2682 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2683 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2684 syntax= SYNTAX_WITH_FLAGS (c);
2685 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2686 if (depth == min_depth)
2687 last_good = from;
2688 comstyle = 0;
2689 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2690 if (code == Sendcomment)
2691 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2692 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2693 && prev_char_comend_first (from, from_byte)
2694 && parse_sexp_ignore_comments)
2696 /* We must record the comment style encountered so that
2697 later, we can match only the proper comment begin
2698 sequence of the same style. */
2699 int c1, other_syntax;
2700 DEC_BOTH (from, from_byte);
2701 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2702 code = Sendcomment;
2703 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2704 other_syntax = SYNTAX_WITH_FLAGS (c1);
2705 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2706 comnested
2707 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2710 /* Quoting turns anything except a comment-ender
2711 into a word character. Note that this cannot be true
2712 if we decremented FROM in the if-statement above. */
2713 if (code != Sendcomment && char_quoted (from, from_byte))
2715 DEC_BOTH (from, from_byte);
2716 code = Sword;
2718 else if (SYNTAX_FLAGS_PREFIX (syntax))
2719 continue;
2721 switch (SWITCH_ENUM_CAST (code))
2723 case Sword:
2724 case Ssymbol:
2725 case Sescape:
2726 case Scharquote:
2727 if (depth || !sexpflag) break;
2728 /* This word counts as a sexp; count object finished
2729 after passing it. */
2730 while (from > stop)
2732 temp_pos = from_byte;
2733 if (! NILP (current_buffer->enable_multibyte_characters))
2734 DEC_POS (temp_pos);
2735 else
2736 temp_pos--;
2737 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2738 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2739 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2740 /* Don't allow comment-end to be quoted. */
2741 if (temp_code == Sendcomment)
2742 goto done2;
2743 quoted = char_quoted (from - 1, temp_pos);
2744 if (quoted)
2746 DEC_BOTH (from, from_byte);
2747 temp_pos = dec_bytepos (temp_pos);
2748 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2750 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2751 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2752 if (! (quoted || temp_code == Sword
2753 || temp_code == Ssymbol
2754 || temp_code == Squote))
2755 goto done2;
2756 DEC_BOTH (from, from_byte);
2758 goto done2;
2760 case Smath:
2761 if (!sexpflag)
2762 break;
2763 temp_pos = dec_bytepos (from_byte);
2764 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2765 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2766 DEC_BOTH (from, from_byte);
2767 if (mathexit)
2769 mathexit = 0;
2770 goto open2;
2772 mathexit = 1;
2774 case Sclose:
2775 if (!++depth) goto done2;
2776 break;
2778 case Sopen:
2779 open2:
2780 if (!--depth) goto done2;
2781 if (depth < min_depth)
2782 xsignal3 (Qscan_error,
2783 build_string ("Containing expression ends prematurely"),
2784 make_number (last_good), make_number (from));
2785 break;
2787 case Sendcomment:
2788 if (!parse_sexp_ignore_comments)
2789 break;
2790 found = back_comment (from, from_byte, stop, comnested, comstyle,
2791 &out_charpos, &out_bytepos);
2792 /* FIXME: if found == -1, then it really wasn't a comment-end.
2793 For single-char Sendcomment, we can't do much about it apart
2794 from skipping the char.
2795 For 2-char endcomments, we could try again, taking both
2796 chars as separate entities, but it's a lot of trouble
2797 for very little gain, so we don't bother either. -sm */
2798 if (found != -1)
2799 from = out_charpos, from_byte = out_bytepos;
2800 break;
2802 case Scomment_fence:
2803 case Sstring_fence:
2804 while (1)
2806 if (from == stop)
2807 goto lose;
2808 DEC_BOTH (from, from_byte);
2809 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2810 if (!char_quoted (from, from_byte)
2811 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2812 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
2813 break;
2815 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2816 break;
2818 case Sstring:
2819 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2820 while (1)
2822 if (from == stop)
2823 goto lose;
2824 DEC_BOTH (from, from_byte);
2825 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2826 if (!char_quoted (from, from_byte)
2827 && (stringterm
2828 == (c = FETCH_CHAR_AS_MULTIBYTE (from_byte)))
2829 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2830 break;
2832 if (!depth && sexpflag) goto done2;
2833 break;
2834 default:
2835 /* Ignore whitespace, punctuation, quote, endcomment. */
2836 break;
2840 /* Reached start of buffer. Error if within object, return nil if between */
2841 if (depth)
2842 goto lose;
2844 immediate_quit = 0;
2845 return Qnil;
2847 done2:
2848 count++;
2852 immediate_quit = 0;
2853 XSETFASTINT (val, from);
2854 return val;
2856 lose:
2857 xsignal3 (Qscan_error,
2858 build_string ("Unbalanced parentheses"),
2859 make_number (last_good), make_number (from));
2862 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2863 doc: /* Scan from character number FROM by COUNT lists.
2864 Returns the character number of the position thus found.
2866 If DEPTH is nonzero, paren depth begins counting from that value,
2867 only places where the depth in parentheses becomes zero
2868 are candidates for stopping; COUNT such places are counted.
2869 Thus, a positive value for DEPTH means go out levels.
2871 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2873 If the beginning or end of (the accessible part of) the buffer is reached
2874 and the depth is wrong, an error is signaled.
2875 If the depth is right but the count is not used up, nil is returned. */)
2876 (Lisp_Object from, Lisp_Object count, Lisp_Object depth)
2878 CHECK_NUMBER (from);
2879 CHECK_NUMBER (count);
2880 CHECK_NUMBER (depth);
2882 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2885 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2886 doc: /* Scan from character number FROM by COUNT balanced expressions.
2887 If COUNT is negative, scan backwards.
2888 Returns the character number of the position thus found.
2890 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2892 If the beginning or end of (the accessible part of) the buffer is reached
2893 in the middle of a parenthetical grouping, an error is signaled.
2894 If the beginning or end is reached between groupings
2895 but before count is used up, nil is returned. */)
2896 (Lisp_Object from, Lisp_Object count)
2898 CHECK_NUMBER (from);
2899 CHECK_NUMBER (count);
2901 return scan_lists (XINT (from), XINT (count), 0, 1);
2904 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2905 0, 0, 0,
2906 doc: /* Move point backward over any number of chars with prefix syntax.
2907 This includes chars with "quote" or "prefix" syntax (' or p). */)
2908 (void)
2910 int beg = BEGV;
2911 int opoint = PT;
2912 int opoint_byte = PT_BYTE;
2913 int pos = PT;
2914 int pos_byte = PT_BYTE;
2915 int c;
2917 if (pos <= beg)
2919 SET_PT_BOTH (opoint, opoint_byte);
2921 return Qnil;
2924 SETUP_SYNTAX_TABLE (pos, -1);
2926 DEC_BOTH (pos, pos_byte);
2928 while (!char_quoted (pos, pos_byte)
2929 /* Previous statement updates syntax table. */
2930 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
2931 || SYNTAX_PREFIX (c)))
2933 opoint = pos;
2934 opoint_byte = pos_byte;
2936 if (pos + 1 > beg)
2937 DEC_BOTH (pos, pos_byte);
2940 SET_PT_BOTH (opoint, opoint_byte);
2942 return Qnil;
2945 /* Parse forward from FROM / FROM_BYTE to END,
2946 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2947 and return a description of the state of the parse at END.
2948 If STOPBEFORE is nonzero, stop at the start of an atom.
2949 If COMMENTSTOP is 1, stop at the start of a comment.
2950 If COMMENTSTOP is -1, stop at the start or end of a comment,
2951 after the beginning of a string, or after the end of a string. */
2953 static void
2954 scan_sexps_forward (struct lisp_parse_state *stateptr,
2955 EMACS_INT from, EMACS_INT from_byte, EMACS_INT end,
2956 int targetdepth, int stopbefore,
2957 Lisp_Object oldstate, int commentstop)
2959 struct lisp_parse_state state;
2961 register enum syntaxcode code;
2962 int c1;
2963 int comnested;
2964 struct level { int last, prev; };
2965 struct level levelstart[100];
2966 register struct level *curlevel = levelstart;
2967 struct level *endlevel = levelstart + 100;
2968 register int depth; /* Paren depth of current scanning location.
2969 level - levelstart equals this except
2970 when the depth becomes negative. */
2971 int mindepth; /* Lowest DEPTH value seen. */
2972 int start_quoted = 0; /* Nonzero means starting after a char quote */
2973 Lisp_Object tem;
2974 EMACS_INT prev_from; /* Keep one character before FROM. */
2975 EMACS_INT prev_from_byte;
2976 int prev_from_syntax;
2977 int boundary_stop = commentstop == -1;
2978 int nofence;
2979 int found;
2980 EMACS_INT out_bytepos, out_charpos;
2981 int temp;
2983 prev_from = from;
2984 prev_from_byte = from_byte;
2985 if (from != BEGV)
2986 DEC_BOTH (prev_from, prev_from_byte);
2988 /* Use this macro instead of `from++'. */
2989 #define INC_FROM \
2990 do { prev_from = from; \
2991 prev_from_byte = from_byte; \
2992 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2993 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2994 INC_BOTH (from, from_byte); \
2995 if (from < end) \
2996 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2997 } while (0)
2999 immediate_quit = 1;
3000 QUIT;
3002 if (NILP (oldstate))
3004 depth = 0;
3005 state.instring = -1;
3006 state.incomment = 0;
3007 state.comstyle = 0; /* comment style a by default. */
3008 state.comstr_start = -1; /* no comment/string seen. */
3010 else
3012 tem = Fcar (oldstate);
3013 if (!NILP (tem))
3014 depth = XINT (tem);
3015 else
3016 depth = 0;
3018 oldstate = Fcdr (oldstate);
3019 oldstate = Fcdr (oldstate);
3020 oldstate = Fcdr (oldstate);
3021 tem = Fcar (oldstate);
3022 /* Check whether we are inside string_fence-style string: */
3023 state.instring = (!NILP (tem)
3024 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
3025 : -1);
3027 oldstate = Fcdr (oldstate);
3028 tem = Fcar (oldstate);
3029 state.incomment = (!NILP (tem)
3030 ? (INTEGERP (tem) ? XINT (tem) : -1)
3031 : 0);
3033 oldstate = Fcdr (oldstate);
3034 tem = Fcar (oldstate);
3035 start_quoted = !NILP (tem);
3037 /* if the eighth element of the list is nil, we are in comment
3038 style a. If it is non-nil, we are in comment style b */
3039 oldstate = Fcdr (oldstate);
3040 oldstate = Fcdr (oldstate);
3041 tem = Fcar (oldstate);
3042 state.comstyle = (NILP (tem)
3044 : (EQ (tem, Qsyntax_table)
3045 ? ST_COMMENT_STYLE
3046 : INTEGERP (tem) ? XINT (tem) : 1));
3048 oldstate = Fcdr (oldstate);
3049 tem = Fcar (oldstate);
3050 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
3051 oldstate = Fcdr (oldstate);
3052 tem = Fcar (oldstate);
3053 while (!NILP (tem)) /* >= second enclosing sexps. */
3055 /* curlevel++->last ran into compiler bug on Apollo */
3056 curlevel->last = XINT (Fcar (tem));
3057 if (++curlevel == endlevel)
3058 curlevel--; /* error ("Nesting too deep for parser"); */
3059 curlevel->prev = -1;
3060 curlevel->last = -1;
3061 tem = Fcdr (tem);
3064 state.quoted = 0;
3065 mindepth = depth;
3067 curlevel->prev = -1;
3068 curlevel->last = -1;
3070 SETUP_SYNTAX_TABLE (prev_from, 1);
3071 temp = FETCH_CHAR (prev_from_byte);
3072 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
3073 UPDATE_SYNTAX_TABLE_FORWARD (from);
3075 /* Enter the loop at a place appropriate for initial state. */
3077 if (state.incomment)
3078 goto startincomment;
3079 if (state.instring >= 0)
3081 nofence = state.instring != ST_STRING_STYLE;
3082 if (start_quoted)
3083 goto startquotedinstring;
3084 goto startinstring;
3086 else if (start_quoted)
3087 goto startquoted;
3089 while (from < end)
3091 int syntax;
3092 INC_FROM;
3093 code = prev_from_syntax & 0xff;
3095 if (from < end
3096 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3097 && (c1 = FETCH_CHAR (from_byte),
3098 syntax = SYNTAX_WITH_FLAGS (c1),
3099 SYNTAX_FLAGS_COMSTART_SECOND (syntax)))
3100 /* Duplicate code to avoid a complex if-expression
3101 which causes trouble for the SGI compiler. */
3103 /* Record the comment style we have entered so that only
3104 the comment-end sequence of the same style actually
3105 terminates the comment section. */
3106 state.comstyle
3107 = SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_from_syntax);
3108 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
3109 comnested = comnested || SYNTAX_FLAGS_COMMENT_NESTED (syntax);
3110 state.incomment = comnested ? 1 : -1;
3111 state.comstr_start = prev_from;
3112 INC_FROM;
3113 code = Scomment;
3115 else if (code == Scomment_fence)
3117 /* Record the comment style we have entered so that only
3118 the comment-end sequence of the same style actually
3119 terminates the comment section. */
3120 state.comstyle = ST_COMMENT_STYLE;
3121 state.incomment = -1;
3122 state.comstr_start = prev_from;
3123 code = Scomment;
3125 else if (code == Scomment)
3127 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax, 0);
3128 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3129 1 : -1);
3130 state.comstr_start = prev_from;
3133 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3134 continue;
3135 switch (SWITCH_ENUM_CAST (code))
3137 case Sescape:
3138 case Scharquote:
3139 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3140 curlevel->last = prev_from;
3141 startquoted:
3142 if (from == end) goto endquoted;
3143 INC_FROM;
3144 goto symstarted;
3145 /* treat following character as a word constituent */
3146 case Sword:
3147 case Ssymbol:
3148 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3149 curlevel->last = prev_from;
3150 symstarted:
3151 while (from < end)
3153 /* Some compilers can't handle this inside the switch. */
3154 temp = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3155 temp = SYNTAX (temp);
3156 switch (temp)
3158 case Scharquote:
3159 case Sescape:
3160 INC_FROM;
3161 if (from == end) goto endquoted;
3162 break;
3163 case Sword:
3164 case Ssymbol:
3165 case Squote:
3166 break;
3167 default:
3168 goto symdone;
3170 INC_FROM;
3172 symdone:
3173 curlevel->prev = curlevel->last;
3174 break;
3176 case Scomment_fence: /* Can't happen because it's handled above. */
3177 case Scomment:
3178 if (commentstop || boundary_stop) goto done;
3179 startincomment:
3180 /* The (from == BEGV) test was to enter the loop in the middle so
3181 that we find a 2-char comment ender even if we start in the
3182 middle of it. We don't want to do that if we're just at the
3183 beginning of the comment (think of (*) ... (*)). */
3184 found = forw_comment (from, from_byte, end,
3185 state.incomment, state.comstyle,
3186 (from == BEGV || from < state.comstr_start + 3)
3187 ? 0 : prev_from_syntax,
3188 &out_charpos, &out_bytepos, &state.incomment);
3189 from = out_charpos; from_byte = out_bytepos;
3190 /* Beware! prev_from and friends are invalid now.
3191 Luckily, the `done' doesn't use them and the INC_FROM
3192 sets them to a sane value without looking at them. */
3193 if (!found) goto done;
3194 INC_FROM;
3195 state.incomment = 0;
3196 state.comstyle = 0; /* reset the comment style */
3197 if (boundary_stop) goto done;
3198 break;
3200 case Sopen:
3201 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3202 depth++;
3203 /* curlevel++->last ran into compiler bug on Apollo */
3204 curlevel->last = prev_from;
3205 if (++curlevel == endlevel)
3206 curlevel--; /* error ("Nesting too deep for parser"); */
3207 curlevel->prev = -1;
3208 curlevel->last = -1;
3209 if (targetdepth == depth) goto done;
3210 break;
3212 case Sclose:
3213 depth--;
3214 if (depth < mindepth)
3215 mindepth = depth;
3216 if (curlevel != levelstart)
3217 curlevel--;
3218 curlevel->prev = curlevel->last;
3219 if (targetdepth == depth) goto done;
3220 break;
3222 case Sstring:
3223 case Sstring_fence:
3224 state.comstr_start = from - 1;
3225 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3226 curlevel->last = prev_from;
3227 state.instring = (code == Sstring
3228 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3229 : ST_STRING_STYLE);
3230 if (boundary_stop) goto done;
3231 startinstring:
3233 nofence = state.instring != ST_STRING_STYLE;
3235 while (1)
3237 int c;
3239 if (from >= end) goto done;
3240 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3241 /* Some compilers can't handle this inside the switch. */
3242 temp = SYNTAX (c);
3244 /* Check TEMP here so that if the char has
3245 a syntax-table property which says it is NOT
3246 a string character, it does not end the string. */
3247 if (nofence && c == state.instring && temp == Sstring)
3248 break;
3250 switch (temp)
3252 case Sstring_fence:
3253 if (!nofence) goto string_end;
3254 break;
3255 case Scharquote:
3256 case Sescape:
3257 INC_FROM;
3258 startquotedinstring:
3259 if (from >= end) goto endquoted;
3261 INC_FROM;
3264 string_end:
3265 state.instring = -1;
3266 curlevel->prev = curlevel->last;
3267 INC_FROM;
3268 if (boundary_stop) goto done;
3269 break;
3271 case Smath:
3272 /* FIXME: We should do something with it. */
3273 break;
3274 default:
3275 /* Ignore whitespace, punctuation, quote, endcomment. */
3276 break;
3279 goto done;
3281 stop: /* Here if stopping before start of sexp. */
3282 from = prev_from; /* We have just fetched the char that starts it; */
3283 goto done; /* but return the position before it. */
3285 endquoted:
3286 state.quoted = 1;
3287 done:
3288 state.depth = depth;
3289 state.mindepth = mindepth;
3290 state.thislevelstart = curlevel->prev;
3291 state.prevlevelstart
3292 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3293 state.location = from;
3294 state.levelstarts = Qnil;
3295 while (--curlevel >= levelstart)
3296 state.levelstarts = Fcons (make_number (curlevel->last),
3297 state.levelstarts);
3298 immediate_quit = 0;
3300 *stateptr = state;
3303 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3304 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3305 Parsing stops at TO or when certain criteria are met;
3306 point is set to where parsing stops.
3307 If fifth arg OLDSTATE is omitted or nil,
3308 parsing assumes that FROM is the beginning of a function.
3309 Value is a list of elements describing final state of parsing:
3310 0. depth in parens.
3311 1. character address of start of innermost containing list; nil if none.
3312 2. character address of start of last complete sexp terminated.
3313 3. non-nil if inside a string.
3314 (it is the character that will terminate the string,
3315 or t if the string should be terminated by a generic string delimiter.)
3316 4. nil if outside a comment, t if inside a non-nestable comment,
3317 else an integer (the current comment nesting).
3318 5. t if following a quote character.
3319 6. the minimum paren-depth encountered during this scan.
3320 7. style of comment, if any.
3321 8. character address of start of comment or string; nil if not in one.
3322 9. Intermediate data for continuation of parsing (subject to change).
3323 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3324 in parentheses becomes equal to TARGETDEPTH.
3325 Fourth arg STOPBEFORE non-nil means stop when come to
3326 any character that starts a sexp.
3327 Fifth arg OLDSTATE is a list like what this function returns.
3328 It is used to initialize the state of the parse. Elements number 1, 2, 6
3329 and 8 are ignored.
3330 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3331 If it is symbol `syntax-table', stop after the start of a comment or a
3332 string, or after end of a comment or a string. */)
3333 (Lisp_Object from, Lisp_Object to, Lisp_Object targetdepth, Lisp_Object stopbefore, Lisp_Object oldstate, Lisp_Object commentstop)
3335 struct lisp_parse_state state;
3336 int target;
3338 if (!NILP (targetdepth))
3340 CHECK_NUMBER (targetdepth);
3341 target = XINT (targetdepth);
3343 else
3344 target = -100000; /* We won't reach this depth */
3346 validate_region (&from, &to);
3347 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3348 XINT (to),
3349 target, !NILP (stopbefore), oldstate,
3350 (NILP (commentstop)
3351 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3353 SET_PT (state.location);
3355 return Fcons (make_number (state.depth),
3356 Fcons (state.prevlevelstart < 0
3357 ? Qnil : make_number (state.prevlevelstart),
3358 Fcons (state.thislevelstart < 0
3359 ? Qnil : make_number (state.thislevelstart),
3360 Fcons (state.instring >= 0
3361 ? (state.instring == ST_STRING_STYLE
3362 ? Qt : make_number (state.instring)) : Qnil,
3363 Fcons (state.incomment < 0 ? Qt :
3364 (state.incomment == 0 ? Qnil :
3365 make_number (state.incomment)),
3366 Fcons (state.quoted ? Qt : Qnil,
3367 Fcons (make_number (state.mindepth),
3368 Fcons ((state.comstyle
3369 ? (state.comstyle == ST_COMMENT_STYLE
3370 ? Qsyntax_table
3371 : make_number (state.comstyle))
3372 : Qnil),
3373 Fcons (((state.incomment
3374 || (state.instring >= 0))
3375 ? make_number (state.comstr_start)
3376 : Qnil),
3377 Fcons (state.levelstarts, Qnil))))))))));
3380 void
3381 init_syntax_once (void)
3383 register int i, c;
3384 Lisp_Object temp;
3386 /* This has to be done here, before we call Fmake_char_table. */
3387 Qsyntax_table = intern_c_string ("syntax-table");
3388 staticpro (&Qsyntax_table);
3390 /* Intern_C_String this now in case it isn't already done.
3391 Setting this variable twice is harmless.
3392 But don't staticpro it here--that is done in alloc.c. */
3393 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
3395 /* Create objects which can be shared among syntax tables. */
3396 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
3397 for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++)
3398 XVECTOR (Vsyntax_code_object)->contents[i]
3399 = Fcons (make_number (i), Qnil);
3401 /* Now we are ready to set up this property, so we can
3402 create syntax tables. */
3403 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3405 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3407 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3409 /* Control characters should not be whitespace. */
3410 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3411 for (i = 0; i <= ' ' - 1; i++)
3412 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3413 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3415 /* Except that a few really are whitespace. */
3416 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3417 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3418 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3419 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3420 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3421 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3423 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3424 for (i = 'a'; i <= 'z'; i++)
3425 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3426 for (i = 'A'; i <= 'Z'; i++)
3427 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3428 for (i = '0'; i <= '9'; i++)
3429 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3431 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3432 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3434 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3435 Fcons (make_number (Sopen), make_number (')')));
3436 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3437 Fcons (make_number (Sclose), make_number ('(')));
3438 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3439 Fcons (make_number (Sopen), make_number (']')));
3440 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3441 Fcons (make_number (Sclose), make_number ('[')));
3442 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3443 Fcons (make_number (Sopen), make_number ('}')));
3444 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3445 Fcons (make_number (Sclose), make_number ('{')));
3446 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3447 Fcons (make_number ((int) Sstring), Qnil));
3448 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3449 Fcons (make_number ((int) Sescape), Qnil));
3451 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
3452 for (i = 0; i < 10; i++)
3454 c = "_-+*/&|<>="[i];
3455 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3458 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3459 for (i = 0; i < 12; i++)
3461 c = ".,;:?!#@~^'`"[i];
3462 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3465 /* All multibyte characters have syntax `word' by default. */
3466 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3467 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3470 void
3471 syms_of_syntax (void)
3473 Qsyntax_table_p = intern_c_string ("syntax-table-p");
3474 staticpro (&Qsyntax_table_p);
3476 staticpro (&Vsyntax_code_object);
3478 staticpro (&gl_state.object);
3479 staticpro (&gl_state.global_code);
3480 staticpro (&gl_state.current_syntax_table);
3481 staticpro (&gl_state.old_prop);
3483 /* Defined in regex.c */
3484 staticpro (&re_match_object);
3486 Qscan_error = intern_c_string ("scan-error");
3487 staticpro (&Qscan_error);
3488 Fput (Qscan_error, Qerror_conditions,
3489 pure_cons (Qscan_error, pure_cons (Qerror, Qnil)));
3490 Fput (Qscan_error, Qerror_message,
3491 make_pure_c_string ("Scan error"));
3493 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
3494 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3496 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties,
3497 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3498 Otherwise, that text property is simply ignored.
3499 See the info node `(elisp)Syntax Properties' for a description of the
3500 `syntax-table' property. */);
3502 words_include_escapes = 0;
3503 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
3504 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3506 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol,
3507 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3508 multibyte_syntax_as_symbol = 0;
3510 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3511 &open_paren_in_column_0_is_defun_start,
3512 doc: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3513 open_paren_in_column_0_is_defun_start = 1;
3516 DEFVAR_LISP ("find-word-boundary-function-table",
3517 &Vfind_word_boundary_function_table,
3518 doc: /*
3519 Char table of functions to search for the word boundary.
3520 Each function is called with two arguments; POS and LIMIT.
3521 POS and LIMIT are character positions in the current buffer.
3523 If POS is less than LIMIT, POS is at the first character of a word,
3524 and the return value of a function is a position after the last
3525 character of that word.
3527 If POS is not less than LIMIT, POS is at the last character of a word,
3528 and the return value of a function is a position at the first
3529 character of that word.
3531 In both cases, LIMIT bounds the search. */);
3532 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3534 defsubr (&Ssyntax_table_p);
3535 defsubr (&Ssyntax_table);
3536 defsubr (&Sstandard_syntax_table);
3537 defsubr (&Scopy_syntax_table);
3538 defsubr (&Sset_syntax_table);
3539 defsubr (&Schar_syntax);
3540 defsubr (&Smatching_paren);
3541 defsubr (&Sstring_to_syntax);
3542 defsubr (&Smodify_syntax_entry);
3543 defsubr (&Sinternal_describe_syntax_value);
3545 defsubr (&Sforward_word);
3547 defsubr (&Sskip_chars_forward);
3548 defsubr (&Sskip_chars_backward);
3549 defsubr (&Sskip_syntax_forward);
3550 defsubr (&Sskip_syntax_backward);
3552 defsubr (&Sforward_comment);
3553 defsubr (&Sscan_lists);
3554 defsubr (&Sscan_sexps);
3555 defsubr (&Sbackward_prefix_chars);
3556 defsubr (&Sparse_partial_sexp);
3559 /* arch-tag: 3e297b9f-088e-4b64-8f4c-fb0b3443e412
3560 (do not change this comment) */