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