* lisp/progmodes/vhdl-mode.el: Doc grammar fixes.
[emacs.git] / src / syntax.c
blob5e697d350ff0e2f87db1cf20d429b574cb086d2c
1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2014 Free
3 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 <sys/types.h>
25 #include "lisp.h"
26 #include "commands.h"
27 #include "character.h"
28 #include "buffer.h"
29 #include "keymap.h"
30 #include "regex.h"
32 #include "syntax.h"
33 #include "intervals.h"
34 #include "category.h"
36 /* Make syntax table lookup grant data in gl_state. */
37 #define SYNTAX(c) syntax_property (c, 1)
38 #define SYNTAX_ENTRY(c) syntax_property_entry (c, 1)
39 #define SYNTAX_WITH_FLAGS(c) syntax_property_with_flags (c, 1)
41 /* Eight single-bit flags have the following meanings:
42 1. This character is the first of a two-character comment-start sequence.
43 2. This character is the second of a two-character comment-start sequence.
44 3. This character is the first of a two-character comment-end sequence.
45 4. This character is the second of a two-character comment-end sequence.
46 5. This character is a prefix, for backward-prefix-chars.
47 6. The char is part of a delimiter for comments of style "b".
48 7. This character is part of a nestable comment sequence.
49 8. The char is part of a delimiter for comments of style "c".
50 Note that any two-character sequence whose first character has flag 1
51 and whose second character has flag 2 will be interpreted as a comment start.
53 Bits 6 and 8 discriminate among different comment styles.
54 Languages such as C++ allow two orthogonal syntax start/end pairs
55 and bit 6 determines whether a comment-end or Scommentend
56 ends style a or b. Comment markers can start style a, b, c, or bc.
57 Style a is always the default.
58 For 2-char comment markers, the style b flag is looked up only on the second
59 char of the comment marker and on the first char of the comment ender.
60 For style c (like the nested flag), the flag can be placed on any of
61 the chars. */
63 /* These functions extract specific flags from an integer
64 that holds the syntax code and the flags. */
66 static bool
67 SYNTAX_FLAGS_COMSTART_FIRST (int flags)
69 return (flags >> 16) & 1;
71 static bool
72 SYNTAX_FLAGS_COMSTART_SECOND (int flags)
74 return (flags >> 17) & 1;
76 static bool
77 SYNTAX_FLAGS_COMEND_FIRST (int flags)
79 return (flags >> 18) & 1;
81 static bool
82 SYNTAX_FLAGS_COMEND_SECOND (int flags)
84 return (flags >> 19) & 1;
86 static bool
87 SYNTAX_FLAGS_PREFIX (int flags)
89 return (flags >> 20) & 1;
91 static bool
92 SYNTAX_FLAGS_COMMENT_STYLEB (int flags)
94 return (flags >> 21) & 1;
96 static bool
97 SYNTAX_FLAGS_COMMENT_STYLEC (int flags)
99 return (flags >> 23) & 1;
101 static int
102 SYNTAX_FLAGS_COMMENT_STYLEC2 (int flags)
104 return (flags >> 22) & 2; /* SYNTAX_FLAGS_COMMENT_STYLEC (flags) * 2 */
106 static bool
107 SYNTAX_FLAGS_COMMENT_NESTED (int flags)
109 return (flags >> 22) & 1;
112 /* FLAGS should be the flags of the main char of the comment marker, e.g.
113 the second for comstart and the first for comend. */
114 static int
115 SYNTAX_FLAGS_COMMENT_STYLE (int flags, int other_flags)
117 return (SYNTAX_FLAGS_COMMENT_STYLEB (flags)
118 | SYNTAX_FLAGS_COMMENT_STYLEC2 (flags)
119 | SYNTAX_FLAGS_COMMENT_STYLEC2 (other_flags));
122 /* Extract a particular flag for a given character. */
124 static bool
125 SYNTAX_COMEND_FIRST (int c)
127 return SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c));
130 /* We use these constants in place for comment-style and
131 string-ender-char to distinguish comments/strings started by
132 comment_fence and string_fence codes. */
134 enum
136 ST_COMMENT_STYLE = 256 + 1,
137 ST_STRING_STYLE = 256 + 2
140 static Lisp_Object Qsyntax_table_p;
141 static Lisp_Object Qsyntax_table, Qscan_error;
143 /* This is the internal form of the parse state used in parse-partial-sexp. */
145 struct lisp_parse_state
147 EMACS_INT depth; /* Depth at end of parsing. */
148 int instring; /* -1 if not within string, else desired terminator. */
149 EMACS_INT incomment; /* -1 if in unnestable comment else comment nesting */
150 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
151 bool quoted; /* True if just after an escape char at end of parsing. */
152 EMACS_INT mindepth; /* Minimum depth seen while scanning. */
153 /* Char number of most recent start-of-expression at current level */
154 ptrdiff_t thislevelstart;
155 /* Char number of start of containing expression */
156 ptrdiff_t prevlevelstart;
157 ptrdiff_t location; /* Char number at which parsing stopped. */
158 ptrdiff_t location_byte; /* Corresponding byte position. */
159 ptrdiff_t comstr_start; /* Position of last comment/string starter. */
160 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
161 of levels (starting from outermost). */
164 /* These variables are a cache for finding the start of a defun.
165 find_start_pos is the place for which the defun start was found.
166 find_start_value is the defun start position found for it.
167 find_start_value_byte is the corresponding byte position.
168 find_start_buffer is the buffer it was found in.
169 find_start_begv is the BEGV value when it was found.
170 find_start_modiff is the value of MODIFF when it was found. */
172 static ptrdiff_t find_start_pos;
173 static ptrdiff_t find_start_value;
174 static ptrdiff_t find_start_value_byte;
175 static struct buffer *find_start_buffer;
176 static ptrdiff_t find_start_begv;
177 static EMACS_INT find_start_modiff;
180 static Lisp_Object skip_chars (bool, Lisp_Object, Lisp_Object, bool);
181 static Lisp_Object skip_syntaxes (bool, Lisp_Object, Lisp_Object);
182 static Lisp_Object scan_lists (EMACS_INT, EMACS_INT, EMACS_INT, bool);
183 static void scan_sexps_forward (struct lisp_parse_state *,
184 ptrdiff_t, ptrdiff_t, ptrdiff_t, EMACS_INT,
185 bool, Lisp_Object, int);
186 static bool in_classes (int, Lisp_Object);
188 /* This setter is used only in this file, so it can be private. */
189 static void
190 bset_syntax_table (struct buffer *b, Lisp_Object val)
192 b->INTERNAL_FIELD (syntax_table) = val;
195 /* Whether the syntax of the character C has the prefix flag set. */
196 bool
197 syntax_prefix_flag_p (int c)
199 return SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c));
202 struct gl_state_s gl_state; /* Global state of syntax parser. */
204 enum { INTERVALS_AT_ONCE = 10 }; /* 1 + max-number of intervals
205 to scan to property-change. */
207 /* Set the syntax entry VAL for char C in table TABLE. */
209 static void
210 SET_RAW_SYNTAX_ENTRY (Lisp_Object table, int c, Lisp_Object val)
212 CHAR_TABLE_SET (table, c, val);
215 /* Set the syntax entry VAL for char-range RANGE in table TABLE.
216 RANGE is a cons (FROM . TO) specifying the range of characters. */
218 static void
219 SET_RAW_SYNTAX_ENTRY_RANGE (Lisp_Object table, Lisp_Object range,
220 Lisp_Object val)
222 Fset_char_table_range (table, range, val);
225 /* Extract the information from the entry for character C
226 in the current syntax table. */
228 static Lisp_Object
229 SYNTAX_MATCH (int c)
231 Lisp_Object ent = SYNTAX_ENTRY (c);
232 return CONSP (ent) ? XCDR (ent) : Qnil;
235 /* This should be called with FROM at the start of forward
236 search, or after the last position of the backward search. It
237 makes sure that the first char is picked up with correct table, so
238 one does not need to call UPDATE_SYNTAX_TABLE immediately after the
239 call.
240 Sign of COUNT gives the direction of the search.
243 static void
244 SETUP_SYNTAX_TABLE (ptrdiff_t from, ptrdiff_t count)
246 SETUP_BUFFER_SYNTAX_TABLE ();
247 gl_state.b_property = BEGV;
248 gl_state.e_property = ZV + 1;
249 gl_state.object = Qnil;
250 gl_state.offset = 0;
251 if (parse_sexp_lookup_properties)
252 if (count > 0 || from > BEGV)
253 update_syntax_table (count > 0 ? from : from - 1, count, 1, Qnil);
256 /* Same as above, but in OBJECT. If OBJECT is nil, use current buffer.
257 If it is t (which is only used in fast_c_string_match_ignore_case),
258 ignore properties altogether.
260 This is meant for regex.c to use. For buffers, regex.c passes arguments
261 to the UPDATE_SYNTAX_TABLE functions which are relative to BEGV.
262 So if it is a buffer, we set the offset field to BEGV. */
264 void
265 SETUP_SYNTAX_TABLE_FOR_OBJECT (Lisp_Object object,
266 ptrdiff_t from, ptrdiff_t count)
268 SETUP_BUFFER_SYNTAX_TABLE ();
269 gl_state.object = object;
270 if (BUFFERP (gl_state.object))
272 struct buffer *buf = XBUFFER (gl_state.object);
273 gl_state.b_property = 1;
274 gl_state.e_property = BUF_ZV (buf) - BUF_BEGV (buf) + 1;
275 gl_state.offset = BUF_BEGV (buf) - 1;
277 else if (NILP (gl_state.object))
279 gl_state.b_property = 1;
280 gl_state.e_property = ZV - BEGV + 1;
281 gl_state.offset = BEGV - 1;
283 else if (EQ (gl_state.object, Qt))
285 gl_state.b_property = 0;
286 gl_state.e_property = PTRDIFF_MAX;
287 gl_state.offset = 0;
289 else
291 gl_state.b_property = 0;
292 gl_state.e_property = 1 + SCHARS (gl_state.object);
293 gl_state.offset = 0;
295 if (parse_sexp_lookup_properties)
296 update_syntax_table (from + gl_state.offset - (count <= 0),
297 count, 1, gl_state.object);
300 /* Update gl_state to an appropriate interval which contains CHARPOS. The
301 sign of COUNT give the relative position of CHARPOS wrt the previously
302 valid interval. If INIT, only [be]_property fields of gl_state are
303 valid at start, the rest is filled basing on OBJECT.
305 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
306 direction than the intervals - or in an interval. We update the
307 current syntax-table basing on the property of this interval, and
308 update the interval to start further than CHARPOS - or be
309 NULL. We also update lim_property to be the next value of
310 charpos to call this subroutine again - or be before/after the
311 start/end of OBJECT. */
313 void
314 update_syntax_table (ptrdiff_t charpos, EMACS_INT count, bool init,
315 Lisp_Object object)
317 Lisp_Object tmp_table;
318 int cnt = 0;
319 bool invalidate = 1;
320 INTERVAL i;
322 if (init)
324 gl_state.old_prop = Qnil;
325 gl_state.start = gl_state.b_property;
326 gl_state.stop = gl_state.e_property;
327 i = interval_of (charpos, object);
328 gl_state.backward_i = gl_state.forward_i = i;
329 invalidate = 0;
330 if (!i)
331 return;
332 /* interval_of updates only ->position of the return value, so
333 update the parents manually to speed up update_interval. */
334 while (!NULL_PARENT (i))
336 if (AM_RIGHT_CHILD (i))
337 INTERVAL_PARENT (i)->position = i->position
338 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
339 - TOTAL_LENGTH (INTERVAL_PARENT (i))
340 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
341 else
342 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
343 + TOTAL_LENGTH (i);
344 i = INTERVAL_PARENT (i);
346 i = gl_state.forward_i;
347 gl_state.b_property = i->position - gl_state.offset;
348 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
349 goto update;
351 i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
353 /* We are guaranteed to be called with CHARPOS either in i,
354 or further off. */
355 if (!i)
356 error ("Error in syntax_table logic for to-the-end intervals");
357 else if (charpos < i->position) /* Move left. */
359 if (count > 0)
360 error ("Error in syntax_table logic for intervals <-");
361 /* Update the interval. */
362 i = update_interval (i, charpos);
363 if (INTERVAL_LAST_POS (i) != gl_state.b_property)
365 invalidate = 0;
366 gl_state.forward_i = i;
367 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
370 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
372 if (count < 0)
373 error ("Error in syntax_table logic for intervals ->");
374 /* Update the interval. */
375 i = update_interval (i, charpos);
376 if (i->position != gl_state.e_property)
378 invalidate = 0;
379 gl_state.backward_i = i;
380 gl_state.b_property = i->position - gl_state.offset;
384 update:
385 tmp_table = textget (i->plist, Qsyntax_table);
387 if (invalidate)
388 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
390 if (invalidate) /* Did not get to adjacent interval. */
391 { /* with the same table => */
392 /* invalidate the old range. */
393 if (count > 0)
395 gl_state.backward_i = i;
396 gl_state.b_property = i->position - gl_state.offset;
398 else
400 gl_state.forward_i = i;
401 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
405 if (!EQ (tmp_table, gl_state.old_prop))
407 gl_state.current_syntax_table = tmp_table;
408 gl_state.old_prop = tmp_table;
409 if (EQ (Fsyntax_table_p (tmp_table), Qt))
411 gl_state.use_global = 0;
413 else if (CONSP (tmp_table))
415 gl_state.use_global = 1;
416 gl_state.global_code = tmp_table;
418 else
420 gl_state.use_global = 0;
421 gl_state.current_syntax_table = BVAR (current_buffer, syntax_table);
425 while (i)
427 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
429 if (count > 0)
431 gl_state.e_property = i->position - gl_state.offset;
432 gl_state.forward_i = i;
434 else
436 gl_state.b_property
437 = i->position + LENGTH (i) - gl_state.offset;
438 gl_state.backward_i = i;
440 return;
442 else if (cnt == INTERVALS_AT_ONCE)
444 if (count > 0)
446 gl_state.e_property
447 = i->position + LENGTH (i) - gl_state.offset
448 /* e_property at EOB is not set to ZV but to ZV+1, so that
449 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
450 having to check eob between the two. */
451 + (next_interval (i) ? 0 : 1);
452 gl_state.forward_i = i;
454 else
456 gl_state.b_property = i->position - gl_state.offset;
457 gl_state.backward_i = i;
459 return;
461 cnt++;
462 i = count > 0 ? next_interval (i) : previous_interval (i);
464 eassert (i == NULL); /* This property goes to the end. */
465 if (count > 0)
466 gl_state.e_property = gl_state.stop;
467 else
468 gl_state.b_property = gl_state.start;
471 /* Returns true if char at CHARPOS is quoted.
472 Global syntax-table data should be set up already to be good at CHARPOS
473 or after. On return global syntax data is good for lookup at CHARPOS. */
475 static bool
476 char_quoted (ptrdiff_t charpos, ptrdiff_t bytepos)
478 enum syntaxcode code;
479 ptrdiff_t beg = BEGV;
480 bool quoted = 0;
481 ptrdiff_t orig = charpos;
483 while (charpos > beg)
485 int c;
486 DEC_BOTH (charpos, bytepos);
488 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
489 c = FETCH_CHAR_AS_MULTIBYTE (bytepos);
490 code = SYNTAX (c);
491 if (! (code == Scharquote || code == Sescape))
492 break;
494 quoted = !quoted;
497 UPDATE_SYNTAX_TABLE (orig);
498 return quoted;
501 /* Return the bytepos one character before BYTEPOS.
502 We assume that BYTEPOS is not at the start of the buffer. */
504 static ptrdiff_t
505 dec_bytepos (ptrdiff_t bytepos)
507 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
508 return bytepos - 1;
510 DEC_POS (bytepos);
511 return bytepos;
514 /* Return a defun-start position before POS and not too far before.
515 It should be the last one before POS, or nearly the last.
517 When open_paren_in_column_0_is_defun_start is nonzero,
518 only the beginning of the buffer is treated as a defun-start.
520 We record the information about where the scan started
521 and what its result was, so that another call in the same area
522 can return the same value very quickly.
524 There is no promise at which position the global syntax data is
525 valid on return from the subroutine, so the caller should explicitly
526 update the global data. */
528 static ptrdiff_t
529 find_defun_start (ptrdiff_t pos, ptrdiff_t pos_byte)
531 ptrdiff_t opoint = PT, opoint_byte = PT_BYTE;
533 /* Use previous finding, if it's valid and applies to this inquiry. */
534 if (current_buffer == find_start_buffer
535 /* Reuse the defun-start even if POS is a little farther on.
536 POS might be in the next defun, but that's ok.
537 Our value may not be the best possible, but will still be usable. */
538 && pos <= find_start_pos + 1000
539 && pos >= find_start_value
540 && BEGV == find_start_begv
541 && MODIFF == find_start_modiff)
542 return find_start_value;
544 if (!open_paren_in_column_0_is_defun_start)
546 find_start_value = BEGV;
547 find_start_value_byte = BEGV_BYTE;
548 goto found;
551 /* Back up to start of line. */
552 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
554 /* We optimize syntax-table lookup for rare updates. Thus we accept
555 only those `^\s(' which are good in global _and_ text-property
556 syntax-tables. */
557 SETUP_BUFFER_SYNTAX_TABLE ();
558 while (PT > BEGV)
560 int c;
562 /* Open-paren at start of line means we may have found our
563 defun-start. */
564 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
565 if (SYNTAX (c) == Sopen)
567 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
568 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
569 if (SYNTAX (c) == Sopen)
570 break;
571 /* Now fallback to the default value. */
572 SETUP_BUFFER_SYNTAX_TABLE ();
574 /* Move to beg of previous line. */
575 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
578 /* Record what we found, for the next try. */
579 find_start_value = PT;
580 find_start_value_byte = PT_BYTE;
581 TEMP_SET_PT_BOTH (opoint, opoint_byte);
583 found:
584 find_start_buffer = current_buffer;
585 find_start_modiff = MODIFF;
586 find_start_begv = BEGV;
587 find_start_pos = pos;
589 return find_start_value;
592 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
594 static bool
595 prev_char_comend_first (ptrdiff_t pos, ptrdiff_t pos_byte)
597 int c;
598 bool val;
600 DEC_BOTH (pos, pos_byte);
601 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
602 c = FETCH_CHAR (pos_byte);
603 val = SYNTAX_COMEND_FIRST (c);
604 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
605 return val;
608 /* Check whether charpos FROM is at the end of a comment.
609 FROM_BYTE is the bytepos corresponding to FROM.
610 Do not move back before STOP.
612 Return true if we find a comment ending at FROM/FROM_BYTE.
614 If successful, store the charpos of the comment's beginning
615 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
617 Global syntax data remains valid for backward search starting at
618 the returned value (or at FROM, if the search was not successful). */
620 static bool
621 back_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop,
622 bool comnested, int comstyle, ptrdiff_t *charpos_ptr,
623 ptrdiff_t *bytepos_ptr)
625 /* Look back, counting the parity of string-quotes,
626 and recording the comment-starters seen.
627 When we reach a safe place, assume that's not in a string;
628 then step the main scan to the earliest comment-starter seen
629 an even number of string quotes away from the safe place.
631 OFROM[I] is position of the earliest comment-starter seen
632 which is I+2X quotes from the comment-end.
633 PARITY is current parity of quotes from the comment end. */
634 int string_style = -1; /* Presumed outside of any string. */
635 bool string_lossage = 0;
636 /* Not a real lossage: indicates that we have passed a matching comment
637 starter plus a non-matching comment-ender, meaning that any matching
638 comment-starter we might see later could be a false positive (hidden
639 inside another comment).
640 Test case: { a (* b } c (* d *) */
641 bool comment_lossage = 0;
642 ptrdiff_t comment_end = from;
643 ptrdiff_t comment_end_byte = from_byte;
644 ptrdiff_t comstart_pos = 0;
645 ptrdiff_t comstart_byte IF_LINT (= 0);
646 /* Place where the containing defun starts,
647 or 0 if we didn't come across it yet. */
648 ptrdiff_t defun_start = 0;
649 ptrdiff_t defun_start_byte = 0;
650 enum syntaxcode code;
651 ptrdiff_t nesting = 1; /* current comment nesting */
652 int c;
653 int syntax = 0;
655 /* FIXME: A }} comment-ender style leads to incorrect behavior
656 in the case of {{ c }}} because we ignore the last two chars which are
657 assumed to be comment-enders although they aren't. */
659 /* At beginning of range to scan, we're outside of strings;
660 that determines quote parity to the comment-end. */
661 while (from != stop)
663 ptrdiff_t temp_byte;
664 int prev_syntax;
665 bool com2start, com2end, comstart;
667 /* Move back and examine a character. */
668 DEC_BOTH (from, from_byte);
669 UPDATE_SYNTAX_TABLE_BACKWARD (from);
671 prev_syntax = syntax;
672 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
673 syntax = SYNTAX_WITH_FLAGS (c);
674 code = SYNTAX (c);
676 /* Check for 2-char comment markers. */
677 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
678 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
679 && (comstyle
680 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax, syntax))
681 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
682 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
683 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
684 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
685 comstart = (com2start || code == Scomment);
687 /* Nasty cases with overlapping 2-char comment markers:
688 - snmp-mode: -- c -- foo -- c --
689 --- c --
690 ------ c --
691 - c-mode: *||*
692 |* *|* *|
693 |*| |* |*|
694 /// */
696 /* If a 2-char comment sequence partly overlaps with another,
697 we don't try to be clever. E.g. |*| in C, or }% in modes that
698 have %..\n and %{..}%. */
699 if (from > stop && (com2end || comstart))
701 ptrdiff_t next = from, next_byte = from_byte;
702 int next_c, next_syntax;
703 DEC_BOTH (next, next_byte);
704 UPDATE_SYNTAX_TABLE_BACKWARD (next);
705 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
706 next_syntax = SYNTAX_WITH_FLAGS (next_c);
707 if (((comstart || comnested)
708 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
709 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
710 || ((com2end || comnested)
711 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
712 && (comstyle
713 == SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_syntax))
714 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
715 goto lossage;
716 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
719 if (com2start && comstart_pos == 0)
720 /* We're looking at a comment starter. But it might be a comment
721 ender as well (see snmp-mode). The first time we see one, we
722 need to consider it as a comment starter,
723 and the subsequent times as a comment ender. */
724 com2end = 0;
726 /* Turn a 2-char comment sequences into the appropriate syntax. */
727 if (com2end)
728 code = Sendcomment;
729 else if (com2start)
730 code = Scomment;
731 /* Ignore comment starters of a different style. */
732 else if (code == Scomment
733 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0)
734 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
735 continue;
737 /* Ignore escaped characters, except comment-enders. */
738 if (code != Sendcomment && char_quoted (from, from_byte))
739 continue;
741 switch (code)
743 case Sstring_fence:
744 case Scomment_fence:
745 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
746 case Sstring:
747 /* Track parity of quotes. */
748 if (string_style == -1)
749 /* Entering a string. */
750 string_style = c;
751 else if (string_style == c)
752 /* Leaving the string. */
753 string_style = -1;
754 else
755 /* If we have two kinds of string delimiters.
756 There's no way to grok this scanning backwards. */
757 string_lossage = 1;
758 break;
760 case Scomment:
761 /* We've already checked that it is the relevant comstyle. */
762 if (string_style != -1 || comment_lossage || string_lossage)
763 /* There are odd string quotes involved, so let's be careful.
764 Test case in Pascal: " { " a { " } */
765 goto lossage;
767 if (!comnested)
769 /* Record best comment-starter so far. */
770 comstart_pos = from;
771 comstart_byte = from_byte;
773 else if (--nesting <= 0)
774 /* nested comments have to be balanced, so we don't need to
775 keep looking for earlier ones. We use here the same (slightly
776 incorrect) reasoning as below: since it is followed by uniform
777 paired string quotes, this comment-start has to be outside of
778 strings, else the comment-end itself would be inside a string. */
779 goto done;
780 break;
782 case Sendcomment:
783 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == comstyle
784 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
785 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
786 /* This is the same style of comment ender as ours. */
788 if (comnested)
789 nesting++;
790 else
791 /* Anything before that can't count because it would match
792 this comment-ender rather than ours. */
793 from = stop; /* Break out of the loop. */
795 else if (comstart_pos != 0 || c != '\n')
796 /* We're mixing comment styles here, so we'd better be careful.
797 The (comstart_pos != 0 || c != '\n') check is not quite correct
798 (we should just always set comment_lossage), but removing it
799 would imply that any multiline comment in C would go through
800 lossage, which seems overkill.
801 The failure should only happen in the rare cases such as
802 { (* } *) */
803 comment_lossage = 1;
804 break;
806 case Sopen:
807 /* Assume a defun-start point is outside of strings. */
808 if (open_paren_in_column_0_is_defun_start
809 && (from == stop
810 || (temp_byte = dec_bytepos (from_byte),
811 FETCH_CHAR (temp_byte) == '\n')))
813 defun_start = from;
814 defun_start_byte = from_byte;
815 from = stop; /* Break out of the loop. */
817 break;
819 default:
820 break;
824 if (comstart_pos == 0)
826 from = comment_end;
827 from_byte = comment_end_byte;
828 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
830 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
831 or `done'), then we've found the beginning of the non-nested comment. */
832 else if (1) /* !comnested */
834 from = comstart_pos;
835 from_byte = comstart_byte;
836 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
838 else
840 struct lisp_parse_state state;
841 bool adjusted;
842 lossage:
843 adjusted = true;
844 /* We had two kinds of string delimiters mixed up
845 together. Decode this going forwards.
846 Scan fwd from a known safe place (beginning-of-defun)
847 to the one in question; this records where we
848 last passed a comment starter. */
849 /* If we did not already find the defun start, find it now. */
850 if (defun_start == 0)
852 defun_start = find_defun_start (comment_end, comment_end_byte);
853 defun_start_byte = find_start_value_byte;
854 adjusted = (defun_start > BEGV);
858 scan_sexps_forward (&state,
859 defun_start, defun_start_byte,
860 comment_end, TYPE_MINIMUM (EMACS_INT),
861 0, Qnil, 0);
862 defun_start = comment_end;
863 if (!adjusted)
865 adjusted = true;
866 find_start_value
867 = CONSP (state.levelstarts) ? XINT (XCAR (state.levelstarts))
868 : state.thislevelstart >= 0 ? state.thislevelstart
869 : find_start_value;
870 find_start_value_byte = CHAR_TO_BYTE (find_start_value);
873 if (state.incomment == (comnested ? 1 : -1)
874 && state.comstyle == comstyle)
875 from = state.comstr_start;
876 else
878 from = comment_end;
879 if (state.incomment)
880 /* If comment_end is inside some other comment, maybe ours
881 is nested, so we need to try again from within the
882 surrounding comment. Example: { a (* " *) */
884 /* FIXME: We should advance by one or two chars. */
885 defun_start = state.comstr_start + 2;
886 defun_start_byte = CHAR_TO_BYTE (defun_start);
889 } while (defun_start < comment_end);
891 from_byte = CHAR_TO_BYTE (from);
892 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
895 done:
896 *charpos_ptr = from;
897 *bytepos_ptr = from_byte;
899 return from != comment_end;
902 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
903 doc: /* Return t if OBJECT is a syntax table.
904 Currently, any char-table counts as a syntax table. */)
905 (Lisp_Object object)
907 if (CHAR_TABLE_P (object)
908 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
909 return Qt;
910 return Qnil;
913 static void
914 check_syntax_table (Lisp_Object obj)
916 CHECK_TYPE (CHAR_TABLE_P (obj) && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table),
917 Qsyntax_table_p, obj);
920 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
921 doc: /* Return the current syntax table.
922 This is the one specified by the current buffer. */)
923 (void)
925 return BVAR (current_buffer, syntax_table);
928 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
929 Sstandard_syntax_table, 0, 0, 0,
930 doc: /* Return the standard syntax table.
931 This is the one used for new buffers. */)
932 (void)
934 return Vstandard_syntax_table;
937 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
938 doc: /* Construct a new syntax table and return it.
939 It is a copy of the TABLE, which defaults to the standard syntax table. */)
940 (Lisp_Object table)
942 Lisp_Object copy;
944 if (!NILP (table))
945 check_syntax_table (table);
946 else
947 table = Vstandard_syntax_table;
949 copy = Fcopy_sequence (table);
951 /* Only the standard syntax table should have a default element.
952 Other syntax tables should inherit from parents instead. */
953 set_char_table_defalt (copy, Qnil);
955 /* Copied syntax tables should all have parents.
956 If we copied one with no parent, such as the standard syntax table,
957 use the standard syntax table as the copy's parent. */
958 if (NILP (XCHAR_TABLE (copy)->parent))
959 Fset_char_table_parent (copy, Vstandard_syntax_table);
960 return copy;
963 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
964 doc: /* Select a new syntax table for the current buffer.
965 One argument, a syntax table. */)
966 (Lisp_Object table)
968 int idx;
969 check_syntax_table (table);
970 bset_syntax_table (current_buffer, table);
971 /* Indicate that this buffer now has a specified syntax table. */
972 idx = PER_BUFFER_VAR_IDX (syntax_table);
973 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
974 return table;
977 /* Convert a letter which signifies a syntax code
978 into the code it signifies.
979 This is used by modify-syntax-entry, and other things. */
981 unsigned char const syntax_spec_code[0400] =
982 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
983 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
984 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
985 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
986 Swhitespace, Scomment_fence, Sstring, 0377, Smath, 0377, 0377, Squote,
987 Sopen, Sclose, 0377, 0377, 0377, Swhitespace, Spunct, Scharquote,
988 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
989 0377, 0377, 0377, 0377, Scomment, 0377, Sendcomment, 0377,
990 Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
991 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
992 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword,
993 0377, 0377, 0377, 0377, Sescape, 0377, 0377, Ssymbol,
994 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
995 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
996 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword,
997 0377, 0377, 0377, 0377, Sstring_fence, 0377, 0377, 0377
1000 /* Indexed by syntax code, give the letter that describes it. */
1002 char const syntax_code_spec[16] =
1004 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
1005 '!', '|'
1008 /* Indexed by syntax code, give the object (cons of syntax code and
1009 nil) to be stored in syntax table. Since these objects can be
1010 shared among syntax tables, we generate them in advance. By
1011 sharing objects, the function `describe-syntax' can give a more
1012 compact listing. */
1013 static Lisp_Object Vsyntax_code_object;
1016 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
1017 doc: /* Return the syntax code of CHARACTER, described by a character.
1018 For example, if CHARACTER is a word constituent, the
1019 character `w' (119) is returned.
1020 The characters that correspond to various syntax codes
1021 are listed in the documentation of `modify-syntax-entry'. */)
1022 (Lisp_Object character)
1024 int char_int;
1025 CHECK_CHARACTER (character);
1026 char_int = XINT (character);
1027 SETUP_BUFFER_SYNTAX_TABLE ();
1028 return make_number (syntax_code_spec[SYNTAX (char_int)]);
1031 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
1032 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
1033 (Lisp_Object character)
1035 int char_int;
1036 enum syntaxcode code;
1037 CHECK_CHARACTER (character);
1038 char_int = XINT (character);
1039 SETUP_BUFFER_SYNTAX_TABLE ();
1040 code = SYNTAX (char_int);
1041 if (code == Sopen || code == Sclose)
1042 return SYNTAX_MATCH (char_int);
1043 return Qnil;
1046 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
1047 doc: /* Convert a syntax descriptor STRING into a raw syntax descriptor.
1048 STRING should be a string of the form allowed as argument of
1049 `modify-syntax-entry'. The return value is a raw syntax descriptor: a
1050 cons cell \(CODE . MATCHING-CHAR) which can be used, for example, as
1051 the value of a `syntax-table' text property. */)
1052 (Lisp_Object string)
1054 const unsigned char *p;
1055 int val;
1056 Lisp_Object match;
1058 CHECK_STRING (string);
1060 p = SDATA (string);
1061 val = syntax_spec_code[*p++];
1062 if (val == 0377)
1063 error ("Invalid syntax description letter: %c", p[-1]);
1065 if (val == Sinherit)
1066 return Qnil;
1068 if (*p)
1070 int len;
1071 int character = STRING_CHAR_AND_LENGTH (p, len);
1072 XSETINT (match, character);
1073 if (XFASTINT (match) == ' ')
1074 match = Qnil;
1075 p += len;
1077 else
1078 match = Qnil;
1080 while (*p)
1081 switch (*p++)
1083 case '1':
1084 val |= 1 << 16;
1085 break;
1087 case '2':
1088 val |= 1 << 17;
1089 break;
1091 case '3':
1092 val |= 1 << 18;
1093 break;
1095 case '4':
1096 val |= 1 << 19;
1097 break;
1099 case 'p':
1100 val |= 1 << 20;
1101 break;
1103 case 'b':
1104 val |= 1 << 21;
1105 break;
1107 case 'n':
1108 val |= 1 << 22;
1109 break;
1111 case 'c':
1112 val |= 1 << 23;
1113 break;
1116 if (val < ASIZE (Vsyntax_code_object) && NILP (match))
1117 return AREF (Vsyntax_code_object, val);
1118 else
1119 /* Since we can't use a shared object, let's make a new one. */
1120 return Fcons (make_number (val), match);
1123 /* I really don't know why this is interactive
1124 help-form should at least be made useful whilst reading the second arg. */
1125 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
1126 "cSet syntax for character: \nsSet syntax for %s to: ",
1127 doc: /* Set syntax for character CHAR according to string NEWENTRY.
1128 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1129 the current buffer's syntax table.
1130 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1131 in the range MIN to MAX are changed.
1132 The first character of NEWENTRY should be one of the following:
1133 Space or - whitespace syntax. w word constituent.
1134 _ symbol constituent. . punctuation.
1135 ( open-parenthesis. ) close-parenthesis.
1136 " string quote. \\ escape.
1137 $ paired delimiter. ' expression quote or prefix operator.
1138 < comment starter. > comment ender.
1139 / character-quote. @ inherit from parent table.
1140 | generic string fence. ! generic comment fence.
1142 Only single-character comment start and end sequences are represented thus.
1143 Two-character sequences are represented as described below.
1144 The second character of NEWENTRY is the matching parenthesis,
1145 used only if the first character is `(' or `)'.
1146 Any additional characters are flags.
1147 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1148 1 means CHAR is the start of a two-char comment start sequence.
1149 2 means CHAR is the second character of such a sequence.
1150 3 means CHAR is the start of a two-char comment end sequence.
1151 4 means CHAR is the second character of such a sequence.
1153 There can be several orthogonal comment sequences. This is to support
1154 language modes such as C++. By default, all comment sequences are of style
1155 a, but you can set the comment sequence style to b (on the second character
1156 of a comment-start, and the first character of a comment-end sequence) and/or
1157 c (on any of its chars) using this flag:
1158 b means CHAR is part of comment sequence b.
1159 c means CHAR is part of comment sequence c.
1160 n means CHAR is part of a nestable comment sequence.
1162 p means CHAR is a prefix character for `backward-prefix-chars';
1163 such characters are treated as whitespace when they occur
1164 between expressions.
1165 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1166 (Lisp_Object c, Lisp_Object newentry, Lisp_Object syntax_table)
1168 if (CONSP (c))
1170 CHECK_CHARACTER_CAR (c);
1171 CHECK_CHARACTER_CDR (c);
1173 else
1174 CHECK_CHARACTER (c);
1176 if (NILP (syntax_table))
1177 syntax_table = BVAR (current_buffer, syntax_table);
1178 else
1179 check_syntax_table (syntax_table);
1181 newentry = Fstring_to_syntax (newentry);
1182 if (CONSP (c))
1183 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1184 else
1185 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
1187 /* We clear the regexp cache, since character classes can now have
1188 different values from those in the compiled regexps.*/
1189 clear_regexp_cache ();
1191 return Qnil;
1194 /* Dump syntax table to buffer in human-readable format */
1196 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1197 Sinternal_describe_syntax_value, 1, 1, 0,
1198 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1199 (Lisp_Object syntax)
1201 int code, syntax_code;
1202 bool start1, start2, end1, end2, prefix, comstyleb, comstylec, comnested;
1203 char str[2];
1204 Lisp_Object first, match_lisp, value = syntax;
1206 if (NILP (value))
1208 insert_string ("default");
1209 return syntax;
1212 if (CHAR_TABLE_P (value))
1214 insert_string ("deeper char-table ...");
1215 return syntax;
1218 if (!CONSP (value))
1220 insert_string ("invalid");
1221 return syntax;
1224 first = XCAR (value);
1225 match_lisp = XCDR (value);
1227 if (!INTEGERP (first) || !(NILP (match_lisp) || CHARACTERP (match_lisp)))
1229 insert_string ("invalid");
1230 return syntax;
1233 syntax_code = XINT (first) & INT_MAX;
1234 code = syntax_code & 0377;
1235 start1 = SYNTAX_FLAGS_COMSTART_FIRST (syntax_code);
1236 start2 = SYNTAX_FLAGS_COMSTART_SECOND (syntax_code);;
1237 end1 = SYNTAX_FLAGS_COMEND_FIRST (syntax_code);
1238 end2 = SYNTAX_FLAGS_COMEND_SECOND (syntax_code);
1239 prefix = SYNTAX_FLAGS_PREFIX (syntax_code);
1240 comstyleb = SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code);
1241 comstylec = SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code);
1242 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax_code);
1244 if (Smax <= code)
1246 insert_string ("invalid");
1247 return syntax;
1250 str[0] = syntax_code_spec[code], str[1] = 0;
1251 insert (str, 1);
1253 if (NILP (match_lisp))
1254 insert (" ", 1);
1255 else
1256 insert_char (XINT (match_lisp));
1258 if (start1)
1259 insert ("1", 1);
1260 if (start2)
1261 insert ("2", 1);
1263 if (end1)
1264 insert ("3", 1);
1265 if (end2)
1266 insert ("4", 1);
1268 if (prefix)
1269 insert ("p", 1);
1270 if (comstyleb)
1271 insert ("b", 1);
1272 if (comstylec)
1273 insert ("c", 1);
1274 if (comnested)
1275 insert ("n", 1);
1277 insert_string ("\twhich means: ");
1279 switch (code)
1281 case Swhitespace:
1282 insert_string ("whitespace"); break;
1283 case Spunct:
1284 insert_string ("punctuation"); break;
1285 case Sword:
1286 insert_string ("word"); break;
1287 case Ssymbol:
1288 insert_string ("symbol"); break;
1289 case Sopen:
1290 insert_string ("open"); break;
1291 case Sclose:
1292 insert_string ("close"); break;
1293 case Squote:
1294 insert_string ("prefix"); break;
1295 case Sstring:
1296 insert_string ("string"); break;
1297 case Smath:
1298 insert_string ("math"); break;
1299 case Sescape:
1300 insert_string ("escape"); break;
1301 case Scharquote:
1302 insert_string ("charquote"); break;
1303 case Scomment:
1304 insert_string ("comment"); break;
1305 case Sendcomment:
1306 insert_string ("endcomment"); break;
1307 case Sinherit:
1308 insert_string ("inherit"); break;
1309 case Scomment_fence:
1310 insert_string ("comment fence"); break;
1311 case Sstring_fence:
1312 insert_string ("string fence"); break;
1313 default:
1314 insert_string ("invalid");
1315 return syntax;
1318 if (!NILP (match_lisp))
1320 insert_string (", matches ");
1321 insert_char (XINT (match_lisp));
1324 if (start1)
1325 insert_string (",\n\t is the first character of a comment-start sequence");
1326 if (start2)
1327 insert_string (",\n\t is the second character of a comment-start sequence");
1329 if (end1)
1330 insert_string (",\n\t is the first character of a comment-end sequence");
1331 if (end2)
1332 insert_string (",\n\t is the second character of a comment-end sequence");
1333 if (comstyleb)
1334 insert_string (" (comment style b)");
1335 if (comstylec)
1336 insert_string (" (comment style c)");
1337 if (comnested)
1338 insert_string (" (nestable)");
1340 if (prefix)
1341 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1343 return syntax;
1346 /* Return the position across COUNT words from FROM.
1347 If that many words cannot be found before the end of the buffer, return 0.
1348 COUNT negative means scan backward and stop at word beginning. */
1350 ptrdiff_t
1351 scan_words (register ptrdiff_t from, register EMACS_INT count)
1353 register ptrdiff_t beg = BEGV;
1354 register ptrdiff_t end = ZV;
1355 register ptrdiff_t from_byte = CHAR_TO_BYTE (from);
1356 register enum syntaxcode code;
1357 int ch0, ch1;
1358 Lisp_Object func, pos;
1360 immediate_quit = 1;
1361 QUIT;
1363 SETUP_SYNTAX_TABLE (from, count);
1365 while (count > 0)
1367 while (1)
1369 if (from == end)
1371 immediate_quit = 0;
1372 return 0;
1374 UPDATE_SYNTAX_TABLE_FORWARD (from);
1375 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1376 code = SYNTAX (ch0);
1377 INC_BOTH (from, from_byte);
1378 if (words_include_escapes
1379 && (code == Sescape || code == Scharquote))
1380 break;
1381 if (code == Sword)
1382 break;
1384 /* Now CH0 is a character which begins a word and FROM is the
1385 position of the next character. */
1386 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
1387 if (! NILP (Ffboundp (func)))
1389 pos = call2 (func, make_number (from - 1), make_number (end));
1390 if (INTEGERP (pos) && from < XINT (pos) && XINT (pos) <= ZV)
1392 from = XINT (pos);
1393 from_byte = CHAR_TO_BYTE (from);
1396 else
1398 while (1)
1400 if (from == end) break;
1401 UPDATE_SYNTAX_TABLE_FORWARD (from);
1402 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1403 code = SYNTAX (ch1);
1404 if ((code != Sword
1405 && (! words_include_escapes
1406 || (code != Sescape && code != Scharquote)))
1407 || word_boundary_p (ch0, ch1))
1408 break;
1409 INC_BOTH (from, from_byte);
1410 ch0 = ch1;
1413 count--;
1415 while (count < 0)
1417 while (1)
1419 if (from == beg)
1421 immediate_quit = 0;
1422 return 0;
1424 DEC_BOTH (from, from_byte);
1425 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1426 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1427 code = SYNTAX (ch1);
1428 if (words_include_escapes
1429 && (code == Sescape || code == Scharquote))
1430 break;
1431 if (code == Sword)
1432 break;
1434 /* Now CH1 is a character which ends a word and FROM is the
1435 position of it. */
1436 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
1437 if (! NILP (Ffboundp (func)))
1439 pos = call2 (func, make_number (from), make_number (beg));
1440 if (INTEGERP (pos) && BEGV <= XINT (pos) && XINT (pos) < from)
1442 from = XINT (pos);
1443 from_byte = CHAR_TO_BYTE (from);
1446 else
1448 while (1)
1450 if (from == beg)
1451 break;
1452 DEC_BOTH (from, from_byte);
1453 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1454 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1455 code = SYNTAX (ch0);
1456 if ((code != Sword
1457 && (! words_include_escapes
1458 || (code != Sescape && code != Scharquote)))
1459 || word_boundary_p (ch0, ch1))
1461 INC_BOTH (from, from_byte);
1462 break;
1464 ch1 = ch0;
1467 count++;
1470 immediate_quit = 0;
1472 return from;
1475 DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p",
1476 doc: /* Move point forward ARG words (backward if ARG is negative).
1477 If ARG is omitted or nil, move point forward one word.
1478 Normally returns t.
1479 If an edge of the buffer or a field boundary is reached, point is left there
1480 and the function returns nil. Field boundaries are not noticed if
1481 `inhibit-field-text-motion' is non-nil. */)
1482 (Lisp_Object arg)
1484 Lisp_Object tmp;
1485 ptrdiff_t orig_val, val;
1487 if (NILP (arg))
1488 XSETFASTINT (arg, 1);
1489 else
1490 CHECK_NUMBER (arg);
1492 val = orig_val = scan_words (PT, XINT (arg));
1493 if (! orig_val)
1494 val = XINT (arg) > 0 ? ZV : BEGV;
1496 /* Avoid jumping out of an input field. */
1497 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1498 Qnil, Qnil, Qnil);
1499 val = XFASTINT (tmp);
1501 SET_PT (val);
1502 return val == orig_val ? Qt : Qnil;
1505 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1506 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1507 STRING is like the inside of a `[...]' in a regular expression
1508 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1509 (but not at the end of a range; quoting is never needed there).
1510 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1511 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1512 Char classes, e.g. `[:alpha:]', are supported.
1514 Returns the distance traveled, either zero or positive. */)
1515 (Lisp_Object string, Lisp_Object lim)
1517 return skip_chars (1, string, lim, 1);
1520 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1521 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1522 See `skip-chars-forward' for details.
1523 Returns the distance traveled, either zero or negative. */)
1524 (Lisp_Object string, Lisp_Object lim)
1526 return skip_chars (0, string, lim, 1);
1529 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1530 doc: /* Move point forward across chars in specified syntax classes.
1531 SYNTAX is a string of syntax code characters.
1532 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1533 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1534 This function returns the distance traveled, either zero or positive. */)
1535 (Lisp_Object syntax, Lisp_Object lim)
1537 return skip_syntaxes (1, syntax, lim);
1540 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1541 doc: /* Move point backward across chars in specified syntax classes.
1542 SYNTAX is a string of syntax code characters.
1543 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1544 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1545 This function returns either zero or a negative number, and the absolute value
1546 of this is the distance traveled. */)
1547 (Lisp_Object syntax, Lisp_Object lim)
1549 return skip_syntaxes (0, syntax, lim);
1552 static Lisp_Object
1553 skip_chars (bool forwardp, Lisp_Object string, Lisp_Object lim,
1554 bool handle_iso_classes)
1556 int c;
1557 char fastmap[0400];
1558 /* Store the ranges of non-ASCII characters. */
1559 int *char_ranges IF_LINT (= NULL);
1560 int n_char_ranges = 0;
1561 bool negate = 0;
1562 ptrdiff_t i, i_byte;
1563 /* True if the current buffer is multibyte and the region contains
1564 non-ASCII chars. */
1565 bool multibyte;
1566 /* True if STRING is multibyte and it contains non-ASCII chars. */
1567 bool string_multibyte;
1568 ptrdiff_t size_byte;
1569 const unsigned char *str;
1570 int len;
1571 Lisp_Object iso_classes;
1573 CHECK_STRING (string);
1574 iso_classes = Qnil;
1576 if (NILP (lim))
1577 XSETINT (lim, forwardp ? ZV : BEGV);
1578 else
1579 CHECK_NUMBER_COERCE_MARKER (lim);
1581 /* In any case, don't allow scan outside bounds of buffer. */
1582 if (XINT (lim) > ZV)
1583 XSETFASTINT (lim, ZV);
1584 if (XINT (lim) < BEGV)
1585 XSETFASTINT (lim, BEGV);
1587 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1588 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1589 string_multibyte = SBYTES (string) > SCHARS (string);
1591 memset (fastmap, 0, sizeof fastmap);
1593 str = SDATA (string);
1594 size_byte = SBYTES (string);
1596 i_byte = 0;
1597 if (i_byte < size_byte
1598 && SREF (string, 0) == '^')
1600 negate = 1; i_byte++;
1603 /* Find the characters specified and set their elements of fastmap.
1604 Handle backslashes and ranges specially.
1606 If STRING contains non-ASCII characters, setup char_ranges for
1607 them and use fastmap only for their leading codes. */
1609 if (! string_multibyte)
1611 bool string_has_eight_bit = 0;
1613 /* At first setup fastmap. */
1614 while (i_byte < size_byte)
1616 c = str[i_byte++];
1618 if (handle_iso_classes && c == '['
1619 && i_byte < size_byte
1620 && str[i_byte] == ':')
1622 const unsigned char *class_beg = str + i_byte + 1;
1623 const unsigned char *class_end = class_beg;
1624 const unsigned char *class_limit = str + size_byte - 2;
1625 /* Leave room for the null. */
1626 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1627 re_wctype_t cc;
1629 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1630 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1632 while (class_end < class_limit
1633 && *class_end >= 'a' && *class_end <= 'z')
1634 class_end++;
1636 if (class_end == class_beg
1637 || *class_end != ':' || class_end[1] != ']')
1638 goto not_a_class_name;
1640 memcpy (class_name, class_beg, class_end - class_beg);
1641 class_name[class_end - class_beg] = 0;
1643 cc = re_wctype (class_name);
1644 if (cc == 0)
1645 error ("Invalid ISO C character class");
1647 iso_classes = Fcons (make_number (cc), iso_classes);
1649 i_byte = class_end + 2 - str;
1650 continue;
1653 not_a_class_name:
1654 if (c == '\\')
1656 if (i_byte == size_byte)
1657 break;
1659 c = str[i_byte++];
1661 /* Treat `-' as range character only if another character
1662 follows. */
1663 if (i_byte + 1 < size_byte
1664 && str[i_byte] == '-')
1666 int c2;
1668 /* Skip over the dash. */
1669 i_byte++;
1671 /* Get the end of the range. */
1672 c2 = str[i_byte++];
1673 if (c2 == '\\'
1674 && i_byte < size_byte)
1675 c2 = str[i_byte++];
1677 if (c <= c2)
1679 int lim2 = c2 + 1;
1680 while (c < lim2)
1681 fastmap[c++] = 1;
1682 if (! ASCII_CHAR_P (c2))
1683 string_has_eight_bit = 1;
1686 else
1688 fastmap[c] = 1;
1689 if (! ASCII_CHAR_P (c))
1690 string_has_eight_bit = 1;
1694 /* If the current range is multibyte and STRING contains
1695 eight-bit chars, arrange fastmap and setup char_ranges for
1696 the corresponding multibyte chars. */
1697 if (multibyte && string_has_eight_bit)
1699 char *p1;
1700 char himap[0200 + 1];
1701 memcpy (himap, fastmap + 0200, 0200);
1702 himap[0200] = 0;
1703 memset (fastmap + 0200, 0, 0200);
1704 char_ranges = alloca (sizeof *char_ranges * 128 * 2);
1705 i = 0;
1707 while ((p1 = memchr (himap + i, 1, 0200 - i)))
1709 /* Deduce the next range C..C2 from the next clump of 1s
1710 in HIMAP starting with &HIMAP[I]. HIMAP is the high
1711 order half of the old FASTMAP. */
1712 int c2, leading_code;
1713 i = p1 - himap;
1714 c = BYTE8_TO_CHAR (i + 0200);
1715 i += strlen (p1);
1716 c2 = BYTE8_TO_CHAR (i + 0200 - 1);
1718 char_ranges[n_char_ranges++] = c;
1719 char_ranges[n_char_ranges++] = c2;
1720 leading_code = CHAR_LEADING_CODE (c);
1721 memset (fastmap + leading_code, 1,
1722 CHAR_LEADING_CODE (c2) - leading_code + 1);
1726 else /* STRING is multibyte */
1728 char_ranges = alloca (sizeof *char_ranges * SCHARS (string) * 2);
1730 while (i_byte < size_byte)
1732 int leading_code = str[i_byte];
1733 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1734 i_byte += len;
1736 if (handle_iso_classes && c == '['
1737 && i_byte < size_byte
1738 && STRING_CHAR (str + i_byte) == ':')
1740 const unsigned char *class_beg = str + i_byte + 1;
1741 const unsigned char *class_end = class_beg;
1742 const unsigned char *class_limit = str + size_byte - 2;
1743 /* Leave room for the null. */
1744 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1745 re_wctype_t cc;
1747 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1748 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1750 while (class_end < class_limit
1751 && *class_end >= 'a' && *class_end <= 'z')
1752 class_end++;
1754 if (class_end == class_beg
1755 || *class_end != ':' || class_end[1] != ']')
1756 goto not_a_class_name_multibyte;
1758 memcpy (class_name, class_beg, class_end - class_beg);
1759 class_name[class_end - class_beg] = 0;
1761 cc = re_wctype (class_name);
1762 if (cc == 0)
1763 error ("Invalid ISO C character class");
1765 iso_classes = Fcons (make_number (cc), iso_classes);
1767 i_byte = class_end + 2 - str;
1768 continue;
1771 not_a_class_name_multibyte:
1772 if (c == '\\')
1774 if (i_byte == size_byte)
1775 break;
1777 leading_code = str[i_byte];
1778 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1779 i_byte += len;
1781 /* Treat `-' as range character only if another character
1782 follows. */
1783 if (i_byte + 1 < size_byte
1784 && str[i_byte] == '-')
1786 int c2, leading_code2;
1788 /* Skip over the dash. */
1789 i_byte++;
1791 /* Get the end of the range. */
1792 leading_code2 = str[i_byte];
1793 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1794 i_byte += len;
1796 if (c2 == '\\'
1797 && i_byte < size_byte)
1799 leading_code2 = str[i_byte];
1800 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1801 i_byte += len;
1804 if (c > c2)
1805 continue;
1806 if (ASCII_CHAR_P (c))
1808 while (c <= c2 && c < 0x80)
1809 fastmap[c++] = 1;
1810 leading_code = CHAR_LEADING_CODE (c);
1812 if (! ASCII_CHAR_P (c))
1814 int lim2 = leading_code2 + 1;
1815 while (leading_code < lim2)
1816 fastmap[leading_code++] = 1;
1817 if (c <= c2)
1819 char_ranges[n_char_ranges++] = c;
1820 char_ranges[n_char_ranges++] = c2;
1824 else
1826 if (ASCII_CHAR_P (c))
1827 fastmap[c] = 1;
1828 else
1830 fastmap[leading_code] = 1;
1831 char_ranges[n_char_ranges++] = c;
1832 char_ranges[n_char_ranges++] = c;
1837 /* If the current range is unibyte and STRING contains non-ASCII
1838 chars, arrange fastmap for the corresponding unibyte
1839 chars. */
1841 if (! multibyte && n_char_ranges > 0)
1843 memset (fastmap + 0200, 0, 0200);
1844 for (i = 0; i < n_char_ranges; i += 2)
1846 int c1 = char_ranges[i];
1847 int lim2 = char_ranges[i + 1] + 1;
1849 for (; c1 < lim2; c1++)
1851 int b = CHAR_TO_BYTE_SAFE (c1);
1852 if (b >= 0)
1853 fastmap[b] = 1;
1859 /* If ^ was the first character, complement the fastmap. */
1860 if (negate)
1862 if (! multibyte)
1863 for (i = 0; i < sizeof fastmap; i++)
1864 fastmap[i] ^= 1;
1865 else
1867 for (i = 0; i < 0200; i++)
1868 fastmap[i] ^= 1;
1869 /* All non-ASCII chars possibly match. */
1870 for (; i < sizeof fastmap; i++)
1871 fastmap[i] = 1;
1876 ptrdiff_t start_point = PT;
1877 ptrdiff_t pos = PT;
1878 ptrdiff_t pos_byte = PT_BYTE;
1879 unsigned char *p = PT_ADDR, *endp, *stop;
1881 if (forwardp)
1883 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1884 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1886 else
1888 endp = CHAR_POS_ADDR (XINT (lim));
1889 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1892 immediate_quit = 1;
1893 /* This code may look up syntax tables using functions that rely on the
1894 gl_state object. To make sure this object is not out of date,
1895 let's initialize it manually.
1896 We ignore syntax-table text-properties for now, since that's
1897 what we've done in the past. */
1898 SETUP_BUFFER_SYNTAX_TABLE ();
1899 if (forwardp)
1901 if (multibyte)
1902 while (1)
1904 int nbytes;
1906 if (p >= stop)
1908 if (p >= endp)
1909 break;
1910 p = GAP_END_ADDR;
1911 stop = endp;
1913 c = STRING_CHAR_AND_LENGTH (p, nbytes);
1914 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1916 if (negate)
1917 break;
1918 else
1919 goto fwd_ok;
1922 if (! fastmap[*p])
1923 break;
1924 if (! ASCII_CHAR_P (c))
1926 /* As we are looking at a multibyte character, we
1927 must look up the character in the table
1928 CHAR_RANGES. If there's no data in the table,
1929 that character is not what we want to skip. */
1931 /* The following code do the right thing even if
1932 n_char_ranges is zero (i.e. no data in
1933 CHAR_RANGES). */
1934 for (i = 0; i < n_char_ranges; i += 2)
1935 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1936 break;
1937 if (!(negate ^ (i < n_char_ranges)))
1938 break;
1940 fwd_ok:
1941 p += nbytes, pos++, pos_byte += nbytes;
1943 else
1944 while (1)
1946 if (p >= stop)
1948 if (p >= endp)
1949 break;
1950 p = GAP_END_ADDR;
1951 stop = endp;
1954 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1956 if (negate)
1957 break;
1958 else
1959 goto fwd_unibyte_ok;
1962 if (!fastmap[*p])
1963 break;
1964 fwd_unibyte_ok:
1965 p++, pos++, pos_byte++;
1968 else
1970 if (multibyte)
1971 while (1)
1973 unsigned char *prev_p;
1975 if (p <= stop)
1977 if (p <= endp)
1978 break;
1979 p = GPT_ADDR;
1980 stop = endp;
1982 prev_p = p;
1983 while (--p >= stop && ! CHAR_HEAD_P (*p));
1984 c = STRING_CHAR (p);
1986 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1988 if (negate)
1989 break;
1990 else
1991 goto back_ok;
1994 if (! fastmap[*p])
1995 break;
1996 if (! ASCII_CHAR_P (c))
1998 /* See the comment in the previous similar code. */
1999 for (i = 0; i < n_char_ranges; i += 2)
2000 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
2001 break;
2002 if (!(negate ^ (i < n_char_ranges)))
2003 break;
2005 back_ok:
2006 pos--, pos_byte -= prev_p - p;
2008 else
2009 while (1)
2011 if (p <= stop)
2013 if (p <= endp)
2014 break;
2015 p = GPT_ADDR;
2016 stop = endp;
2019 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
2021 if (negate)
2022 break;
2023 else
2024 goto back_unibyte_ok;
2027 if (!fastmap[p[-1]])
2028 break;
2029 back_unibyte_ok:
2030 p--, pos--, pos_byte--;
2034 SET_PT_BOTH (pos, pos_byte);
2035 immediate_quit = 0;
2037 return make_number (PT - start_point);
2042 static Lisp_Object
2043 skip_syntaxes (bool forwardp, Lisp_Object string, Lisp_Object lim)
2045 int c;
2046 unsigned char fastmap[0400];
2047 bool negate = 0;
2048 ptrdiff_t i, i_byte;
2049 bool multibyte;
2050 ptrdiff_t size_byte;
2051 unsigned char *str;
2053 CHECK_STRING (string);
2055 if (NILP (lim))
2056 XSETINT (lim, forwardp ? ZV : BEGV);
2057 else
2058 CHECK_NUMBER_COERCE_MARKER (lim);
2060 /* In any case, don't allow scan outside bounds of buffer. */
2061 if (XINT (lim) > ZV)
2062 XSETFASTINT (lim, ZV);
2063 if (XINT (lim) < BEGV)
2064 XSETFASTINT (lim, BEGV);
2066 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
2067 return make_number (0);
2069 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
2070 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
2072 memset (fastmap, 0, sizeof fastmap);
2074 if (SBYTES (string) > SCHARS (string))
2075 /* As this is very rare case (syntax spec is ASCII only), don't
2076 consider efficiency. */
2077 string = string_make_unibyte (string);
2079 str = SDATA (string);
2080 size_byte = SBYTES (string);
2082 i_byte = 0;
2083 if (i_byte < size_byte
2084 && SREF (string, 0) == '^')
2086 negate = 1; i_byte++;
2089 /* Find the syntaxes specified and set their elements of fastmap. */
2091 while (i_byte < size_byte)
2093 c = str[i_byte++];
2094 fastmap[syntax_spec_code[c]] = 1;
2097 /* If ^ was the first character, complement the fastmap. */
2098 if (negate)
2099 for (i = 0; i < sizeof fastmap; i++)
2100 fastmap[i] ^= 1;
2103 ptrdiff_t start_point = PT;
2104 ptrdiff_t pos = PT;
2105 ptrdiff_t pos_byte = PT_BYTE;
2106 unsigned char *p = PT_ADDR, *endp, *stop;
2108 if (forwardp)
2110 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
2111 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
2113 else
2115 endp = CHAR_POS_ADDR (XINT (lim));
2116 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
2119 immediate_quit = 1;
2120 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
2121 if (forwardp)
2123 if (multibyte)
2125 while (1)
2127 int nbytes;
2129 if (p >= stop)
2131 if (p >= endp)
2132 break;
2133 p = GAP_END_ADDR;
2134 stop = endp;
2136 c = STRING_CHAR_AND_LENGTH (p, nbytes);
2137 if (! fastmap[SYNTAX (c)])
2138 break;
2139 p += nbytes, pos++, pos_byte += nbytes;
2140 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2143 else
2145 while (1)
2147 if (p >= stop)
2149 if (p >= endp)
2150 break;
2151 p = GAP_END_ADDR;
2152 stop = endp;
2154 if (! fastmap[SYNTAX (*p)])
2155 break;
2156 p++, pos++, pos_byte++;
2157 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2161 else
2163 if (multibyte)
2165 while (1)
2167 unsigned char *prev_p;
2169 if (p <= stop)
2171 if (p <= endp)
2172 break;
2173 p = GPT_ADDR;
2174 stop = endp;
2176 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2177 prev_p = p;
2178 while (--p >= stop && ! CHAR_HEAD_P (*p));
2179 c = STRING_CHAR (p);
2180 if (! fastmap[SYNTAX (c)])
2181 break;
2182 pos--, pos_byte -= prev_p - p;
2185 else
2187 while (1)
2189 if (p <= stop)
2191 if (p <= endp)
2192 break;
2193 p = GPT_ADDR;
2194 stop = endp;
2196 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2197 if (! fastmap[SYNTAX (p[-1])])
2198 break;
2199 p--, pos--, pos_byte--;
2204 SET_PT_BOTH (pos, pos_byte);
2205 immediate_quit = 0;
2207 return make_number (PT - start_point);
2211 /* Return true if character C belongs to one of the ISO classes
2212 in the list ISO_CLASSES. Each class is represented by an
2213 integer which is its type according to re_wctype. */
2215 static bool
2216 in_classes (int c, Lisp_Object iso_classes)
2218 bool fits_class = 0;
2220 while (CONSP (iso_classes))
2222 Lisp_Object elt;
2223 elt = XCAR (iso_classes);
2224 iso_classes = XCDR (iso_classes);
2226 if (re_iswctype (c, XFASTINT (elt)))
2227 fits_class = 1;
2230 return fits_class;
2233 /* Jump over a comment, assuming we are at the beginning of one.
2234 FROM is the current position.
2235 FROM_BYTE is the bytepos corresponding to FROM.
2236 Do not move past STOP (a charpos).
2237 The comment over which we have to jump is of style STYLE
2238 (either SYNTAX_FLAGS_COMMENT_STYLE (foo) or ST_COMMENT_STYLE).
2239 NESTING should be positive to indicate the nesting at the beginning
2240 for nested comments and should be zero or negative else.
2241 ST_COMMENT_STYLE cannot be nested.
2242 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2243 (or 0 If the search cannot start in the middle of a two-character).
2245 If successful, return true and store the charpos of the comment's end
2246 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2247 Else, return false and store the charpos STOP into *CHARPOS_PTR, the
2248 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2249 (as defined for state.incomment) in *INCOMMENT_PTR.
2251 The comment end is the last character of the comment rather than the
2252 character just after the comment.
2254 Global syntax data is assumed to initially be valid for FROM and
2255 remains valid for forward search starting at the returned position. */
2257 static bool
2258 forw_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop,
2259 EMACS_INT nesting, int style, int prev_syntax,
2260 ptrdiff_t *charpos_ptr, ptrdiff_t *bytepos_ptr,
2261 EMACS_INT *incomment_ptr)
2263 register int c, c1;
2264 register enum syntaxcode code;
2265 register int syntax, other_syntax;
2267 if (nesting <= 0) nesting = -1;
2269 /* Enter the loop in the middle so that we find
2270 a 2-char comment ender if we start in the middle of it. */
2271 syntax = prev_syntax;
2272 if (syntax != 0) goto forw_incomment;
2274 while (1)
2276 if (from == stop)
2278 *incomment_ptr = nesting;
2279 *charpos_ptr = from;
2280 *bytepos_ptr = from_byte;
2281 return 0;
2283 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2284 syntax = SYNTAX_WITH_FLAGS (c);
2285 code = syntax & 0xff;
2286 if (code == Sendcomment
2287 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style
2288 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2289 (nesting > 0 && --nesting == 0) : nesting < 0))
2290 /* we have encountered a comment end of the same style
2291 as the comment sequence which began this comment
2292 section */
2293 break;
2294 if (code == Scomment_fence
2295 && style == ST_COMMENT_STYLE)
2296 /* we have encountered a comment end of the same style
2297 as the comment sequence which began this comment
2298 section. */
2299 break;
2300 if (nesting > 0
2301 && code == Scomment
2302 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2303 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style)
2304 /* we have encountered a nested comment of the same style
2305 as the comment sequence which began this comment section */
2306 nesting++;
2307 INC_BOTH (from, from_byte);
2308 UPDATE_SYNTAX_TABLE_FORWARD (from);
2310 forw_incomment:
2311 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2312 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2313 other_syntax = SYNTAX_WITH_FLAGS (c1),
2314 SYNTAX_FLAGS_COMEND_SECOND (other_syntax))
2315 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, other_syntax) == style
2316 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2317 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax))
2318 ? nesting > 0 : nesting < 0))
2320 if (--nesting <= 0)
2321 /* we have encountered a comment end of the same style
2322 as the comment sequence which began this comment
2323 section */
2324 break;
2325 else
2327 INC_BOTH (from, from_byte);
2328 UPDATE_SYNTAX_TABLE_FORWARD (from);
2331 if (nesting > 0
2332 && from < stop
2333 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2334 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2335 other_syntax = SYNTAX_WITH_FLAGS (c1),
2336 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax) == style
2337 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2338 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2339 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax)))
2340 /* we have encountered a nested comment of the same style
2341 as the comment sequence which began this comment
2342 section */
2344 INC_BOTH (from, from_byte);
2345 UPDATE_SYNTAX_TABLE_FORWARD (from);
2346 nesting++;
2349 *charpos_ptr = from;
2350 *bytepos_ptr = from_byte;
2351 return 1;
2354 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2355 doc: /*
2356 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2357 Stop scanning if we find something other than a comment or whitespace.
2358 Set point to where scanning stops.
2359 If COUNT comments are found as expected, with nothing except whitespace
2360 between them, return t; otherwise return nil. */)
2361 (Lisp_Object count)
2363 ptrdiff_t from, from_byte, stop;
2364 int c, c1;
2365 enum syntaxcode code;
2366 int comstyle = 0; /* style of comment encountered */
2367 bool comnested = 0; /* whether the comment is nestable or not */
2368 bool found;
2369 EMACS_INT count1;
2370 ptrdiff_t out_charpos, out_bytepos;
2371 EMACS_INT dummy;
2373 CHECK_NUMBER (count);
2374 count1 = XINT (count);
2375 stop = count1 > 0 ? ZV : BEGV;
2377 immediate_quit = 1;
2378 QUIT;
2380 from = PT;
2381 from_byte = PT_BYTE;
2383 SETUP_SYNTAX_TABLE (from, count1);
2384 while (count1 > 0)
2388 bool comstart_first;
2389 int syntax, other_syntax;
2391 if (from == stop)
2393 SET_PT_BOTH (from, from_byte);
2394 immediate_quit = 0;
2395 return Qnil;
2397 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2398 syntax = SYNTAX_WITH_FLAGS (c);
2399 code = SYNTAX (c);
2400 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2401 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2402 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2403 INC_BOTH (from, from_byte);
2404 UPDATE_SYNTAX_TABLE_FORWARD (from);
2405 if (from < stop && comstart_first
2406 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2407 other_syntax = SYNTAX_WITH_FLAGS (c1),
2408 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)))
2410 /* We have encountered a comment start sequence and we
2411 are ignoring all text inside comments. We must record
2412 the comment style this sequence begins so that later,
2413 only a comment end of the same style actually ends
2414 the comment section. */
2415 code = Scomment;
2416 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2417 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2418 INC_BOTH (from, from_byte);
2419 UPDATE_SYNTAX_TABLE_FORWARD (from);
2422 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2424 if (code == Scomment_fence)
2425 comstyle = ST_COMMENT_STYLE;
2426 else if (code != Scomment)
2428 immediate_quit = 0;
2429 DEC_BOTH (from, from_byte);
2430 SET_PT_BOTH (from, from_byte);
2431 return Qnil;
2433 /* We're at the start of a comment. */
2434 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2435 &out_charpos, &out_bytepos, &dummy);
2436 from = out_charpos; from_byte = out_bytepos;
2437 if (!found)
2439 immediate_quit = 0;
2440 SET_PT_BOTH (from, from_byte);
2441 return Qnil;
2443 INC_BOTH (from, from_byte);
2444 UPDATE_SYNTAX_TABLE_FORWARD (from);
2445 /* We have skipped one comment. */
2446 count1--;
2449 while (count1 < 0)
2451 while (1)
2453 bool quoted;
2454 int syntax;
2456 if (from <= stop)
2458 SET_PT_BOTH (BEGV, BEGV_BYTE);
2459 immediate_quit = 0;
2460 return Qnil;
2463 DEC_BOTH (from, from_byte);
2464 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2465 quoted = char_quoted (from, from_byte);
2466 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2467 syntax = SYNTAX_WITH_FLAGS (c);
2468 code = SYNTAX (c);
2469 comstyle = 0;
2470 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2471 if (code == Sendcomment)
2472 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2473 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2474 && prev_char_comend_first (from, from_byte)
2475 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2477 int other_syntax;
2478 /* We must record the comment style encountered so that
2479 later, we can match only the proper comment begin
2480 sequence of the same style. */
2481 DEC_BOTH (from, from_byte);
2482 code = Sendcomment;
2483 /* Calling char_quoted, above, set up global syntax position
2484 at the new value of FROM. */
2485 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2486 other_syntax = SYNTAX_WITH_FLAGS (c1);
2487 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2488 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2491 if (code == Scomment_fence)
2493 /* Skip until first preceding unquoted comment_fence. */
2494 bool fence_found = 0;
2495 ptrdiff_t ini = from, ini_byte = from_byte;
2497 while (1)
2499 DEC_BOTH (from, from_byte);
2500 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2501 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2502 if (SYNTAX (c) == Scomment_fence
2503 && !char_quoted (from, from_byte))
2505 fence_found = 1;
2506 break;
2508 else if (from == stop)
2509 break;
2511 if (fence_found == 0)
2513 from = ini; /* Set point to ini + 1. */
2514 from_byte = ini_byte;
2515 goto leave;
2517 else
2518 /* We have skipped one comment. */
2519 break;
2521 else if (code == Sendcomment)
2523 found = back_comment (from, from_byte, stop, comnested, comstyle,
2524 &out_charpos, &out_bytepos);
2525 if (!found)
2527 if (c == '\n')
2528 /* This end-of-line is not an end-of-comment.
2529 Treat it like a whitespace.
2530 CC-mode (and maybe others) relies on this behavior. */
2532 else
2534 /* Failure: we should go back to the end of this
2535 not-quite-endcomment. */
2536 if (SYNTAX (c) != code)
2537 /* It was a two-char Sendcomment. */
2538 INC_BOTH (from, from_byte);
2539 goto leave;
2542 else
2544 /* We have skipped one comment. */
2545 from = out_charpos, from_byte = out_bytepos;
2546 break;
2549 else if (code != Swhitespace || quoted)
2551 leave:
2552 immediate_quit = 0;
2553 INC_BOTH (from, from_byte);
2554 SET_PT_BOTH (from, from_byte);
2555 return Qnil;
2559 count1++;
2562 SET_PT_BOTH (from, from_byte);
2563 immediate_quit = 0;
2564 return Qt;
2567 /* Return syntax code of character C if C is an ASCII character
2568 or if MULTIBYTE_SYMBOL_P is false. Otherwise, return Ssymbol. */
2570 static enum syntaxcode
2571 syntax_multibyte (int c, bool multibyte_symbol_p)
2573 return ASCII_CHAR_P (c) || !multibyte_symbol_p ? SYNTAX (c) : Ssymbol;
2576 static Lisp_Object
2577 scan_lists (EMACS_INT from, EMACS_INT count, EMACS_INT depth, bool sexpflag)
2579 Lisp_Object val;
2580 ptrdiff_t stop = count > 0 ? ZV : BEGV;
2581 int c, c1;
2582 int stringterm;
2583 bool quoted;
2584 bool mathexit = 0;
2585 enum syntaxcode code;
2586 EMACS_INT min_depth = depth; /* Err out if depth gets less than this. */
2587 int comstyle = 0; /* style of comment encountered */
2588 bool comnested = 0; /* whether the comment is nestable or not */
2589 ptrdiff_t temp_pos;
2590 EMACS_INT last_good = from;
2591 bool found;
2592 ptrdiff_t from_byte;
2593 ptrdiff_t out_bytepos, out_charpos;
2594 EMACS_INT dummy;
2595 bool multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2597 if (depth > 0) min_depth = 0;
2599 if (from > ZV) from = ZV;
2600 if (from < BEGV) from = BEGV;
2602 from_byte = CHAR_TO_BYTE (from);
2604 immediate_quit = 1;
2605 QUIT;
2607 SETUP_SYNTAX_TABLE (from, count);
2608 while (count > 0)
2610 while (from < stop)
2612 bool comstart_first, prefix;
2613 int syntax, other_syntax;
2614 UPDATE_SYNTAX_TABLE_FORWARD (from);
2615 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2616 syntax = SYNTAX_WITH_FLAGS (c);
2617 code = syntax_multibyte (c, multibyte_symbol_p);
2618 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2619 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2620 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2621 prefix = SYNTAX_FLAGS_PREFIX (syntax);
2622 if (depth == min_depth)
2623 last_good = from;
2624 INC_BOTH (from, from_byte);
2625 UPDATE_SYNTAX_TABLE_FORWARD (from);
2626 if (from < stop && comstart_first
2627 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2628 other_syntax = SYNTAX_WITH_FLAGS (c),
2629 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2630 && parse_sexp_ignore_comments)
2632 /* we have encountered a comment start sequence and we
2633 are ignoring all text inside comments. We must record
2634 the comment style this sequence begins so that later,
2635 only a comment end of the same style actually ends
2636 the comment section */
2637 code = Scomment;
2638 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2639 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2640 INC_BOTH (from, from_byte);
2641 UPDATE_SYNTAX_TABLE_FORWARD (from);
2644 if (prefix)
2645 continue;
2647 switch (code)
2649 case Sescape:
2650 case Scharquote:
2651 if (from == stop)
2652 goto lose;
2653 INC_BOTH (from, from_byte);
2654 /* treat following character as a word constituent */
2655 case Sword:
2656 case Ssymbol:
2657 if (depth || !sexpflag) break;
2658 /* This word counts as a sexp; return at end of it. */
2659 while (from < stop)
2661 UPDATE_SYNTAX_TABLE_FORWARD (from);
2663 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2664 switch (syntax_multibyte (c, multibyte_symbol_p))
2666 case Scharquote:
2667 case Sescape:
2668 INC_BOTH (from, from_byte);
2669 if (from == stop)
2670 goto lose;
2671 break;
2672 case Sword:
2673 case Ssymbol:
2674 case Squote:
2675 break;
2676 default:
2677 goto done;
2679 INC_BOTH (from, from_byte);
2681 goto done;
2683 case Scomment_fence:
2684 comstyle = ST_COMMENT_STYLE;
2685 /* FALLTHROUGH */
2686 case Scomment:
2687 if (!parse_sexp_ignore_comments) break;
2688 UPDATE_SYNTAX_TABLE_FORWARD (from);
2689 found = forw_comment (from, from_byte, stop,
2690 comnested, comstyle, 0,
2691 &out_charpos, &out_bytepos, &dummy);
2692 from = out_charpos, from_byte = out_bytepos;
2693 if (!found)
2695 if (depth == 0)
2696 goto done;
2697 goto lose;
2699 INC_BOTH (from, from_byte);
2700 UPDATE_SYNTAX_TABLE_FORWARD (from);
2701 break;
2703 case Smath:
2704 if (!sexpflag)
2705 break;
2706 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2708 INC_BOTH (from, from_byte);
2710 if (mathexit)
2712 mathexit = 0;
2713 goto close1;
2715 mathexit = 1;
2717 case Sopen:
2718 if (!++depth) goto done;
2719 break;
2721 case Sclose:
2722 close1:
2723 if (!--depth) goto done;
2724 if (depth < min_depth)
2725 xsignal3 (Qscan_error,
2726 build_string ("Containing expression ends prematurely"),
2727 make_number (last_good), make_number (from));
2728 break;
2730 case Sstring:
2731 case Sstring_fence:
2732 temp_pos = dec_bytepos (from_byte);
2733 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2734 while (1)
2736 enum syntaxcode c_code;
2737 if (from >= stop)
2738 goto lose;
2739 UPDATE_SYNTAX_TABLE_FORWARD (from);
2740 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2741 c_code = syntax_multibyte (c, multibyte_symbol_p);
2742 if (code == Sstring
2743 ? c == stringterm && c_code == Sstring
2744 : c_code == Sstring_fence)
2745 break;
2747 switch (c_code)
2749 case Scharquote:
2750 case Sescape:
2751 INC_BOTH (from, from_byte);
2753 INC_BOTH (from, from_byte);
2755 INC_BOTH (from, from_byte);
2756 if (!depth && sexpflag) goto done;
2757 break;
2758 default:
2759 /* Ignore whitespace, punctuation, quote, endcomment. */
2760 break;
2764 /* Reached end of buffer. Error if within object, return nil if between */
2765 if (depth)
2766 goto lose;
2768 immediate_quit = 0;
2769 return Qnil;
2771 /* End of object reached */
2772 done:
2773 count--;
2776 while (count < 0)
2778 while (from > stop)
2780 int syntax;
2781 DEC_BOTH (from, from_byte);
2782 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2783 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2784 syntax= SYNTAX_WITH_FLAGS (c);
2785 code = syntax_multibyte (c, multibyte_symbol_p);
2786 if (depth == min_depth)
2787 last_good = from;
2788 comstyle = 0;
2789 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2790 if (code == Sendcomment)
2791 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2792 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2793 && prev_char_comend_first (from, from_byte)
2794 && parse_sexp_ignore_comments)
2796 /* We must record the comment style encountered so that
2797 later, we can match only the proper comment begin
2798 sequence of the same style. */
2799 int c2, other_syntax;
2800 DEC_BOTH (from, from_byte);
2801 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2802 code = Sendcomment;
2803 c2 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2804 other_syntax = SYNTAX_WITH_FLAGS (c2);
2805 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2806 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2809 /* Quoting turns anything except a comment-ender
2810 into a word character. Note that this cannot be true
2811 if we decremented FROM in the if-statement above. */
2812 if (code != Sendcomment && char_quoted (from, from_byte))
2814 DEC_BOTH (from, from_byte);
2815 code = Sword;
2817 else if (SYNTAX_FLAGS_PREFIX (syntax))
2818 continue;
2820 switch (code)
2822 case Sword:
2823 case Ssymbol:
2824 case Sescape:
2825 case Scharquote:
2826 if (depth || !sexpflag) break;
2827 /* This word counts as a sexp; count object finished
2828 after passing it. */
2829 while (from > stop)
2831 temp_pos = from_byte;
2832 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2833 DEC_POS (temp_pos);
2834 else
2835 temp_pos--;
2836 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2837 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2838 /* Don't allow comment-end to be quoted. */
2839 if (syntax_multibyte (c1, multibyte_symbol_p) == Sendcomment)
2840 goto done2;
2841 quoted = char_quoted (from - 1, temp_pos);
2842 if (quoted)
2844 DEC_BOTH (from, from_byte);
2845 temp_pos = dec_bytepos (temp_pos);
2846 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2848 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2849 if (! quoted)
2850 switch (syntax_multibyte (c1, multibyte_symbol_p))
2852 case Sword: case Ssymbol: case Squote: break;
2853 default: goto done2;
2855 DEC_BOTH (from, from_byte);
2857 goto done2;
2859 case Smath:
2860 if (!sexpflag)
2861 break;
2862 if (from > BEGV)
2864 temp_pos = dec_bytepos (from_byte);
2865 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2866 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2867 DEC_BOTH (from, from_byte);
2869 if (mathexit)
2871 mathexit = 0;
2872 goto open2;
2874 mathexit = 1;
2876 case Sclose:
2877 if (!++depth) goto done2;
2878 break;
2880 case Sopen:
2881 open2:
2882 if (!--depth) goto done2;
2883 if (depth < min_depth)
2884 xsignal3 (Qscan_error,
2885 build_string ("Containing expression ends prematurely"),
2886 make_number (last_good), make_number (from));
2887 break;
2889 case Sendcomment:
2890 if (!parse_sexp_ignore_comments)
2891 break;
2892 found = back_comment (from, from_byte, stop, comnested, comstyle,
2893 &out_charpos, &out_bytepos);
2894 /* FIXME: if !found, it really wasn't a comment-end.
2895 For single-char Sendcomment, we can't do much about it apart
2896 from skipping the char.
2897 For 2-char endcomments, we could try again, taking both
2898 chars as separate entities, but it's a lot of trouble
2899 for very little gain, so we don't bother either. -sm */
2900 if (found)
2901 from = out_charpos, from_byte = out_bytepos;
2902 break;
2904 case Scomment_fence:
2905 case Sstring_fence:
2906 while (1)
2908 if (from == stop)
2909 goto lose;
2910 DEC_BOTH (from, from_byte);
2911 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2912 if (!char_quoted (from, from_byte))
2914 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2915 if (syntax_multibyte (c, multibyte_symbol_p) == code)
2916 break;
2919 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2920 break;
2922 case Sstring:
2923 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2924 while (1)
2926 if (from == stop)
2927 goto lose;
2928 DEC_BOTH (from, from_byte);
2929 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2930 if (!char_quoted (from, from_byte))
2932 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2933 if (c == stringterm
2934 && (syntax_multibyte (c, multibyte_symbol_p)
2935 == Sstring))
2936 break;
2939 if (!depth && sexpflag) goto done2;
2940 break;
2941 default:
2942 /* Ignore whitespace, punctuation, quote, endcomment. */
2943 break;
2947 /* Reached start of buffer. Error if within object, return nil if between */
2948 if (depth)
2949 goto lose;
2951 immediate_quit = 0;
2952 return Qnil;
2954 done2:
2955 count++;
2959 immediate_quit = 0;
2960 XSETFASTINT (val, from);
2961 return val;
2963 lose:
2964 xsignal3 (Qscan_error,
2965 build_string ("Unbalanced parentheses"),
2966 make_number (last_good), make_number (from));
2969 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2970 doc: /* Scan from character number FROM by COUNT lists.
2971 Scan forward if COUNT is positive, backward if COUNT is negative.
2972 Return the character number of the position thus found.
2974 A \"list", in this context, refers to a balanced parenthetical
2975 grouping, as determined by the syntax table.
2977 If DEPTH is nonzero, treat that as the nesting depth of the starting
2978 point (i.e. the starting point is DEPTH parentheses deep). This
2979 function scans over parentheses until the depth goes to zero COUNT
2980 times. Hence, positive DEPTH moves out that number of levels of
2981 parentheses, while negative DEPTH moves to a deeper level.
2983 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2985 If we reach the beginning or end of the accessible part of the buffer
2986 before we have scanned over COUNT lists, return nil if the depth at
2987 that point is zero, and signal a error if the depth is nonzero. */)
2988 (Lisp_Object from, Lisp_Object count, Lisp_Object depth)
2990 CHECK_NUMBER (from);
2991 CHECK_NUMBER (count);
2992 CHECK_NUMBER (depth);
2994 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2997 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2998 doc: /* Scan from character number FROM by COUNT balanced expressions.
2999 If COUNT is negative, scan backwards.
3000 Returns the character number of the position thus found.
3002 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3004 If the beginning or end of (the accessible part of) the buffer is reached
3005 in the middle of a parenthetical grouping, an error is signaled.
3006 If the beginning or end is reached between groupings
3007 but before count is used up, nil is returned. */)
3008 (Lisp_Object from, Lisp_Object count)
3010 CHECK_NUMBER (from);
3011 CHECK_NUMBER (count);
3013 return scan_lists (XINT (from), XINT (count), 0, 1);
3016 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
3017 0, 0, 0,
3018 doc: /* Move point backward over any number of chars with prefix syntax.
3019 This includes chars with "quote" or "prefix" syntax (' or p). */)
3020 (void)
3022 ptrdiff_t beg = BEGV;
3023 ptrdiff_t opoint = PT;
3024 ptrdiff_t opoint_byte = PT_BYTE;
3025 ptrdiff_t pos = PT;
3026 ptrdiff_t pos_byte = PT_BYTE;
3027 int c;
3029 if (pos <= beg)
3031 SET_PT_BOTH (opoint, opoint_byte);
3033 return Qnil;
3036 SETUP_SYNTAX_TABLE (pos, -1);
3038 DEC_BOTH (pos, pos_byte);
3040 while (!char_quoted (pos, pos_byte)
3041 /* Previous statement updates syntax table. */
3042 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
3043 || syntax_prefix_flag_p (c)))
3045 opoint = pos;
3046 opoint_byte = pos_byte;
3048 if (pos + 1 > beg)
3049 DEC_BOTH (pos, pos_byte);
3052 SET_PT_BOTH (opoint, opoint_byte);
3054 return Qnil;
3057 /* Parse forward from FROM / FROM_BYTE to END,
3058 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
3059 and return a description of the state of the parse at END.
3060 If STOPBEFORE, stop at the start of an atom.
3061 If COMMENTSTOP is 1, stop at the start of a comment.
3062 If COMMENTSTOP is -1, stop at the start or end of a comment,
3063 after the beginning of a string, or after the end of a string. */
3065 static void
3066 scan_sexps_forward (struct lisp_parse_state *stateptr,
3067 ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t end,
3068 EMACS_INT targetdepth, bool stopbefore,
3069 Lisp_Object oldstate, int commentstop)
3071 struct lisp_parse_state state;
3072 enum syntaxcode code;
3073 int c1;
3074 bool comnested;
3075 struct level { ptrdiff_t last, prev; };
3076 struct level levelstart[100];
3077 struct level *curlevel = levelstart;
3078 struct level *endlevel = levelstart + 100;
3079 EMACS_INT depth; /* Paren depth of current scanning location.
3080 level - levelstart equals this except
3081 when the depth becomes negative. */
3082 EMACS_INT mindepth; /* Lowest DEPTH value seen. */
3083 bool start_quoted = 0; /* True means starting after a char quote. */
3084 Lisp_Object tem;
3085 ptrdiff_t prev_from; /* Keep one character before FROM. */
3086 ptrdiff_t prev_from_byte;
3087 int prev_from_syntax;
3088 bool boundary_stop = commentstop == -1;
3089 bool nofence;
3090 bool found;
3091 ptrdiff_t out_bytepos, out_charpos;
3092 int temp;
3094 prev_from = from;
3095 prev_from_byte = from_byte;
3096 if (from != BEGV)
3097 DEC_BOTH (prev_from, prev_from_byte);
3099 /* Use this macro instead of `from++'. */
3100 #define INC_FROM \
3101 do { prev_from = from; \
3102 prev_from_byte = from_byte; \
3103 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
3104 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
3105 INC_BOTH (from, from_byte); \
3106 if (from < end) \
3107 UPDATE_SYNTAX_TABLE_FORWARD (from); \
3108 } while (0)
3110 immediate_quit = 1;
3111 QUIT;
3113 if (NILP (oldstate))
3115 depth = 0;
3116 state.instring = -1;
3117 state.incomment = 0;
3118 state.comstyle = 0; /* comment style a by default. */
3119 state.comstr_start = -1; /* no comment/string seen. */
3121 else
3123 tem = Fcar (oldstate);
3124 if (!NILP (tem))
3125 depth = XINT (tem);
3126 else
3127 depth = 0;
3129 oldstate = Fcdr (oldstate);
3130 oldstate = Fcdr (oldstate);
3131 oldstate = Fcdr (oldstate);
3132 tem = Fcar (oldstate);
3133 /* Check whether we are inside string_fence-style string: */
3134 state.instring = (!NILP (tem)
3135 ? (CHARACTERP (tem) ? XFASTINT (tem) : ST_STRING_STYLE)
3136 : -1);
3138 oldstate = Fcdr (oldstate);
3139 tem = Fcar (oldstate);
3140 state.incomment = (!NILP (tem)
3141 ? (INTEGERP (tem) ? XINT (tem) : -1)
3142 : 0);
3144 oldstate = Fcdr (oldstate);
3145 tem = Fcar (oldstate);
3146 start_quoted = !NILP (tem);
3148 /* if the eighth element of the list is nil, we are in comment
3149 style a. If it is non-nil, we are in comment style b */
3150 oldstate = Fcdr (oldstate);
3151 oldstate = Fcdr (oldstate);
3152 tem = Fcar (oldstate);
3153 state.comstyle = (NILP (tem)
3155 : (RANGED_INTEGERP (0, tem, ST_COMMENT_STYLE)
3156 ? XINT (tem)
3157 : ST_COMMENT_STYLE));
3159 oldstate = Fcdr (oldstate);
3160 tem = Fcar (oldstate);
3161 state.comstr_start =
3162 RANGED_INTEGERP (PTRDIFF_MIN, tem, PTRDIFF_MAX) ? XINT (tem) : -1;
3163 oldstate = Fcdr (oldstate);
3164 tem = Fcar (oldstate);
3165 while (!NILP (tem)) /* >= second enclosing sexps. */
3167 Lisp_Object temhd = Fcar (tem);
3168 if (RANGED_INTEGERP (PTRDIFF_MIN, temhd, PTRDIFF_MAX))
3169 curlevel->last = XINT (temhd);
3170 if (++curlevel == endlevel)
3171 curlevel--; /* error ("Nesting too deep for parser"); */
3172 curlevel->prev = -1;
3173 curlevel->last = -1;
3174 tem = Fcdr (tem);
3177 state.quoted = 0;
3178 mindepth = depth;
3180 curlevel->prev = -1;
3181 curlevel->last = -1;
3183 SETUP_SYNTAX_TABLE (prev_from, 1);
3184 temp = FETCH_CHAR (prev_from_byte);
3185 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
3186 UPDATE_SYNTAX_TABLE_FORWARD (from);
3188 /* Enter the loop at a place appropriate for initial state. */
3190 if (state.incomment)
3191 goto startincomment;
3192 if (state.instring >= 0)
3194 nofence = state.instring != ST_STRING_STYLE;
3195 if (start_quoted)
3196 goto startquotedinstring;
3197 goto startinstring;
3199 else if (start_quoted)
3200 goto startquoted;
3202 while (from < end)
3204 int syntax;
3205 INC_FROM;
3206 code = prev_from_syntax & 0xff;
3208 if (from < end
3209 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3210 && (c1 = FETCH_CHAR (from_byte),
3211 syntax = SYNTAX_WITH_FLAGS (c1),
3212 SYNTAX_FLAGS_COMSTART_SECOND (syntax)))
3213 /* Duplicate code to avoid a complex if-expression
3214 which causes trouble for the SGI compiler. */
3216 /* Record the comment style we have entered so that only
3217 the comment-end sequence of the same style actually
3218 terminates the comment section. */
3219 state.comstyle
3220 = SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_from_syntax);
3221 comnested = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax)
3222 | SYNTAX_FLAGS_COMMENT_NESTED (syntax));
3223 state.incomment = comnested ? 1 : -1;
3224 state.comstr_start = prev_from;
3225 INC_FROM;
3226 code = Scomment;
3228 else if (code == Scomment_fence)
3230 /* Record the comment style we have entered so that only
3231 the comment-end sequence of the same style actually
3232 terminates the comment section. */
3233 state.comstyle = ST_COMMENT_STYLE;
3234 state.incomment = -1;
3235 state.comstr_start = prev_from;
3236 code = Scomment;
3238 else if (code == Scomment)
3240 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax, 0);
3241 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3242 1 : -1);
3243 state.comstr_start = prev_from;
3246 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3247 continue;
3248 switch (code)
3250 case Sescape:
3251 case Scharquote:
3252 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3253 curlevel->last = prev_from;
3254 startquoted:
3255 if (from == end) goto endquoted;
3256 INC_FROM;
3257 goto symstarted;
3258 /* treat following character as a word constituent */
3259 case Sword:
3260 case Ssymbol:
3261 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3262 curlevel->last = prev_from;
3263 symstarted:
3264 while (from < end)
3266 int symchar = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3267 switch (SYNTAX (symchar))
3269 case Scharquote:
3270 case Sescape:
3271 INC_FROM;
3272 if (from == end) goto endquoted;
3273 break;
3274 case Sword:
3275 case Ssymbol:
3276 case Squote:
3277 break;
3278 default:
3279 goto symdone;
3281 INC_FROM;
3283 symdone:
3284 curlevel->prev = curlevel->last;
3285 break;
3287 case Scomment_fence: /* Can't happen because it's handled above. */
3288 case Scomment:
3289 if (commentstop || boundary_stop) goto done;
3290 startincomment:
3291 /* The (from == BEGV) test was to enter the loop in the middle so
3292 that we find a 2-char comment ender even if we start in the
3293 middle of it. We don't want to do that if we're just at the
3294 beginning of the comment (think of (*) ... (*)). */
3295 found = forw_comment (from, from_byte, end,
3296 state.incomment, state.comstyle,
3297 (from == BEGV || from < state.comstr_start + 3)
3298 ? 0 : prev_from_syntax,
3299 &out_charpos, &out_bytepos, &state.incomment);
3300 from = out_charpos; from_byte = out_bytepos;
3301 /* Beware! prev_from and friends are invalid now.
3302 Luckily, the `done' doesn't use them and the INC_FROM
3303 sets them to a sane value without looking at them. */
3304 if (!found) goto done;
3305 INC_FROM;
3306 state.incomment = 0;
3307 state.comstyle = 0; /* reset the comment style */
3308 if (boundary_stop) goto done;
3309 break;
3311 case Sopen:
3312 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3313 depth++;
3314 /* curlevel++->last ran into compiler bug on Apollo */
3315 curlevel->last = prev_from;
3316 if (++curlevel == endlevel)
3317 curlevel--; /* error ("Nesting too deep for parser"); */
3318 curlevel->prev = -1;
3319 curlevel->last = -1;
3320 if (targetdepth == depth) goto done;
3321 break;
3323 case Sclose:
3324 depth--;
3325 if (depth < mindepth)
3326 mindepth = depth;
3327 if (curlevel != levelstart)
3328 curlevel--;
3329 curlevel->prev = curlevel->last;
3330 if (targetdepth == depth) goto done;
3331 break;
3333 case Sstring:
3334 case Sstring_fence:
3335 state.comstr_start = from - 1;
3336 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3337 curlevel->last = prev_from;
3338 state.instring = (code == Sstring
3339 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3340 : ST_STRING_STYLE);
3341 if (boundary_stop) goto done;
3342 startinstring:
3344 nofence = state.instring != ST_STRING_STYLE;
3346 while (1)
3348 int c;
3349 enum syntaxcode c_code;
3351 if (from >= end) goto done;
3352 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3353 c_code = SYNTAX (c);
3355 /* Check C_CODE here so that if the char has
3356 a syntax-table property which says it is NOT
3357 a string character, it does not end the string. */
3358 if (nofence && c == state.instring && c_code == Sstring)
3359 break;
3361 switch (c_code)
3363 case Sstring_fence:
3364 if (!nofence) goto string_end;
3365 break;
3366 case Scharquote:
3367 case Sescape:
3368 INC_FROM;
3369 startquotedinstring:
3370 if (from >= end) goto endquoted;
3372 INC_FROM;
3375 string_end:
3376 state.instring = -1;
3377 curlevel->prev = curlevel->last;
3378 INC_FROM;
3379 if (boundary_stop) goto done;
3380 break;
3382 case Smath:
3383 /* FIXME: We should do something with it. */
3384 break;
3385 default:
3386 /* Ignore whitespace, punctuation, quote, endcomment. */
3387 break;
3390 goto done;
3392 stop: /* Here if stopping before start of sexp. */
3393 from = prev_from; /* We have just fetched the char that starts it; */
3394 from_byte = prev_from_byte;
3395 goto done; /* but return the position before it. */
3397 endquoted:
3398 state.quoted = 1;
3399 done:
3400 state.depth = depth;
3401 state.mindepth = mindepth;
3402 state.thislevelstart = curlevel->prev;
3403 state.prevlevelstart
3404 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3405 state.location = from;
3406 state.location_byte = from_byte;
3407 state.levelstarts = Qnil;
3408 while (curlevel > levelstart)
3409 state.levelstarts = Fcons (make_number ((--curlevel)->last),
3410 state.levelstarts);
3411 immediate_quit = 0;
3413 *stateptr = state;
3416 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3417 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3418 Parsing stops at TO or when certain criteria are met;
3419 point is set to where parsing stops.
3420 If fifth arg OLDSTATE is omitted or nil,
3421 parsing assumes that FROM is the beginning of a function.
3422 Value is a list of elements describing final state of parsing:
3423 0. depth in parens.
3424 1. character address of start of innermost containing list; nil if none.
3425 2. character address of start of last complete sexp terminated.
3426 3. non-nil if inside a string.
3427 (it is the character that will terminate the string,
3428 or t if the string should be terminated by a generic string delimiter.)
3429 4. nil if outside a comment, t if inside a non-nestable comment,
3430 else an integer (the current comment nesting).
3431 5. t if following a quote character.
3432 6. the minimum paren-depth encountered during this scan.
3433 7. style of comment, if any.
3434 8. character address of start of comment or string; nil if not in one.
3435 9. Intermediate data for continuation of parsing (subject to change).
3436 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3437 in parentheses becomes equal to TARGETDEPTH.
3438 Fourth arg STOPBEFORE non-nil means stop when come to
3439 any character that starts a sexp.
3440 Fifth arg OLDSTATE is a list like what this function returns.
3441 It is used to initialize the state of the parse. Elements number 1, 2, 6
3442 are ignored.
3443 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3444 If it is symbol `syntax-table', stop after the start of a comment or a
3445 string, or after end of a comment or a string. */)
3446 (Lisp_Object from, Lisp_Object to, Lisp_Object targetdepth,
3447 Lisp_Object stopbefore, Lisp_Object oldstate, Lisp_Object commentstop)
3449 struct lisp_parse_state state;
3450 EMACS_INT target;
3452 if (!NILP (targetdepth))
3454 CHECK_NUMBER (targetdepth);
3455 target = XINT (targetdepth);
3457 else
3458 target = TYPE_MINIMUM (EMACS_INT); /* We won't reach this depth */
3460 validate_region (&from, &to);
3461 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3462 XINT (to),
3463 target, !NILP (stopbefore), oldstate,
3464 (NILP (commentstop)
3465 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3467 SET_PT_BOTH (state.location, state.location_byte);
3469 return Fcons (make_number (state.depth),
3470 Fcons (state.prevlevelstart < 0
3471 ? Qnil : make_number (state.prevlevelstart),
3472 Fcons (state.thislevelstart < 0
3473 ? Qnil : make_number (state.thislevelstart),
3474 Fcons (state.instring >= 0
3475 ? (state.instring == ST_STRING_STYLE
3476 ? Qt : make_number (state.instring)) : Qnil,
3477 Fcons (state.incomment < 0 ? Qt :
3478 (state.incomment == 0 ? Qnil :
3479 make_number (state.incomment)),
3480 Fcons (state.quoted ? Qt : Qnil,
3481 Fcons (make_number (state.mindepth),
3482 Fcons ((state.comstyle
3483 ? (state.comstyle == ST_COMMENT_STYLE
3484 ? Qsyntax_table
3485 : make_number (state.comstyle))
3486 : Qnil),
3487 Fcons (((state.incomment
3488 || (state.instring >= 0))
3489 ? make_number (state.comstr_start)
3490 : Qnil),
3491 Fcons (state.levelstarts, Qnil))))))))));
3494 void
3495 init_syntax_once (void)
3497 register int i, c;
3498 Lisp_Object temp;
3500 /* This has to be done here, before we call Fmake_char_table. */
3501 DEFSYM (Qsyntax_table, "syntax-table");
3503 /* This variable is DEFSYMed in alloc.c and not initialized yet, so
3504 intern it here. NOTE: you must guarantee that init_syntax_once
3505 is called before all other users of this variable. */
3506 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
3508 /* Create objects which can be shared among syntax tables. */
3509 Vsyntax_code_object = make_uninit_vector (Smax);
3510 for (i = 0; i < Smax; i++)
3511 ASET (Vsyntax_code_object, i, Fcons (make_number (i), Qnil));
3513 /* Now we are ready to set up this property, so we can
3514 create syntax tables. */
3515 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3517 temp = AREF (Vsyntax_code_object, Swhitespace);
3519 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3521 /* Control characters should not be whitespace. */
3522 temp = AREF (Vsyntax_code_object, Spunct);
3523 for (i = 0; i <= ' ' - 1; i++)
3524 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3525 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3527 /* Except that a few really are whitespace. */
3528 temp = AREF (Vsyntax_code_object, Swhitespace);
3529 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3530 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3531 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3532 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3533 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3535 temp = AREF (Vsyntax_code_object, Sword);
3536 for (i = 'a'; i <= 'z'; i++)
3537 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3538 for (i = 'A'; i <= 'Z'; i++)
3539 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3540 for (i = '0'; i <= '9'; i++)
3541 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3543 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3544 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3546 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3547 Fcons (make_number (Sopen), make_number (')')));
3548 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3549 Fcons (make_number (Sclose), make_number ('(')));
3550 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3551 Fcons (make_number (Sopen), make_number (']')));
3552 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3553 Fcons (make_number (Sclose), make_number ('[')));
3554 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3555 Fcons (make_number (Sopen), make_number ('}')));
3556 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3557 Fcons (make_number (Sclose), make_number ('{')));
3558 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3559 Fcons (make_number (Sstring), Qnil));
3560 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3561 Fcons (make_number (Sescape), Qnil));
3563 temp = AREF (Vsyntax_code_object, Ssymbol);
3564 for (i = 0; i < 10; i++)
3566 c = "_-+*/&|<>="[i];
3567 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3570 temp = AREF (Vsyntax_code_object, Spunct);
3571 for (i = 0; i < 12; i++)
3573 c = ".,;:?!#@~^'`"[i];
3574 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3577 /* All multibyte characters have syntax `word' by default. */
3578 temp = AREF (Vsyntax_code_object, Sword);
3579 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3582 void
3583 syms_of_syntax (void)
3585 DEFSYM (Qsyntax_table_p, "syntax-table-p");
3587 staticpro (&Vsyntax_code_object);
3589 staticpro (&gl_state.object);
3590 staticpro (&gl_state.global_code);
3591 staticpro (&gl_state.current_syntax_table);
3592 staticpro (&gl_state.old_prop);
3594 /* Defined in regex.c */
3595 staticpro (&re_match_object);
3597 DEFSYM (Qscan_error, "scan-error");
3598 Fput (Qscan_error, Qerror_conditions,
3599 listn (CONSTYPE_PURE, 2, Qscan_error, Qerror));
3600 Fput (Qscan_error, Qerror_message,
3601 build_pure_c_string ("Scan error"));
3603 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments,
3604 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3606 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties,
3607 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3608 Otherwise, that text property is simply ignored.
3609 See the info node `(elisp)Syntax Properties' for a description of the
3610 `syntax-table' property. */);
3612 words_include_escapes = 0;
3613 DEFVAR_BOOL ("words-include-escapes", words_include_escapes,
3614 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3616 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol,
3617 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3618 multibyte_syntax_as_symbol = 0;
3620 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3621 open_paren_in_column_0_is_defun_start,
3622 doc: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3623 open_paren_in_column_0_is_defun_start = 1;
3626 DEFVAR_LISP ("find-word-boundary-function-table",
3627 Vfind_word_boundary_function_table,
3628 doc: /*
3629 Char table of functions to search for the word boundary.
3630 Each function is called with two arguments; POS and LIMIT.
3631 POS and LIMIT are character positions in the current buffer.
3633 If POS is less than LIMIT, POS is at the first character of a word,
3634 and the return value of a function is a position after the last
3635 character of that word.
3637 If POS is not less than LIMIT, POS is at the last character of a word,
3638 and the return value of a function is a position at the first
3639 character of that word.
3641 In both cases, LIMIT bounds the search. */);
3642 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3644 defsubr (&Ssyntax_table_p);
3645 defsubr (&Ssyntax_table);
3646 defsubr (&Sstandard_syntax_table);
3647 defsubr (&Scopy_syntax_table);
3648 defsubr (&Sset_syntax_table);
3649 defsubr (&Schar_syntax);
3650 defsubr (&Smatching_paren);
3651 defsubr (&Sstring_to_syntax);
3652 defsubr (&Smodify_syntax_entry);
3653 defsubr (&Sinternal_describe_syntax_value);
3655 defsubr (&Sforward_word);
3657 defsubr (&Sskip_chars_forward);
3658 defsubr (&Sskip_chars_backward);
3659 defsubr (&Sskip_syntax_forward);
3660 defsubr (&Sskip_syntax_backward);
3662 defsubr (&Sforward_comment);
3663 defsubr (&Sscan_lists);
3664 defsubr (&Sscan_sexps);
3665 defsubr (&Sbackward_prefix_chars);
3666 defsubr (&Sparse_partial_sexp);