* test/lisp/mouse-tests.el: Fix tests broken by mouse.el change
[emacs.git] / src / syntax.c
blob52cec23cd7e73c9c168047bfb2541157c938f98e
1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2018 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 (at
10 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 <https://www.gnu.org/licenses/>. */
21 #include <config.h>
23 #include "lisp.h"
24 #include "character.h"
25 #include "buffer.h"
26 #include "regex.h"
27 #include "syntax.h"
28 #include "intervals.h"
29 #include "category.h"
31 /* Make syntax table lookup grant data in gl_state. */
32 #define SYNTAX(c) syntax_property (c, 1)
33 #define SYNTAX_ENTRY(c) syntax_property_entry (c, 1)
34 #define SYNTAX_WITH_FLAGS(c) syntax_property_with_flags (c, 1)
36 /* Eight single-bit flags have the following meanings:
37 1. This character is the first of a two-character comment-start sequence.
38 2. This character is the second of a two-character comment-start sequence.
39 3. This character is the first of a two-character comment-end sequence.
40 4. This character is the second of a two-character comment-end sequence.
41 5. This character is a prefix, for backward-prefix-chars.
42 6. The char is part of a delimiter for comments of style "b".
43 7. This character is part of a nestable comment sequence.
44 8. The char is part of a delimiter for comments of style "c".
45 Note that any two-character sequence whose first character has flag 1
46 and whose second character has flag 2 will be interpreted as a comment start.
48 Bits 6 and 8 discriminate among different comment styles.
49 Languages such as C++ allow two orthogonal syntax start/end pairs
50 and bit 6 determines whether a comment-end or Scommentend
51 ends style a or b. Comment markers can start style a, b, c, or bc.
52 Style a is always the default.
53 For 2-char comment markers, the style b flag is looked up only on the second
54 char of the comment marker and on the first char of the comment ender.
55 For style c (like the nested flag), the flag can be placed on any of
56 the chars. */
58 /* These functions extract specific flags from an integer
59 that holds the syntax code and the flags. */
61 static bool
62 SYNTAX_FLAGS_COMSTART_FIRST (int flags)
64 return (flags >> 16) & 1;
66 static bool
67 SYNTAX_FLAGS_COMSTART_SECOND (int flags)
69 return (flags >> 17) & 1;
71 static bool
72 SYNTAX_FLAGS_COMEND_FIRST (int flags)
74 return (flags >> 18) & 1;
76 static bool
77 SYNTAX_FLAGS_COMEND_SECOND (int flags)
79 return (flags >> 19) & 1;
81 static bool
82 SYNTAX_FLAGS_COMSTARTEND_FIRST (int flags)
84 return (flags & 0x50000) != 0;
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 /* This is the internal form of the parse state used in parse-partial-sexp. */
142 struct lisp_parse_state
144 EMACS_INT depth; /* Depth at end of parsing. */
145 int instring; /* -1 if not within string, else desired terminator. */
146 EMACS_INT incomment; /* -1 if in unnestable comment else comment nesting */
147 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
148 bool quoted; /* True if just after an escape char at end of parsing. */
149 EMACS_INT mindepth; /* Minimum depth seen while scanning. */
150 /* Char number of most recent start-of-expression at current level */
151 ptrdiff_t thislevelstart;
152 /* Char number of start of containing expression */
153 ptrdiff_t prevlevelstart;
154 ptrdiff_t location; /* Char number at which parsing stopped. */
155 ptrdiff_t location_byte; /* Corresponding byte position. */
156 ptrdiff_t comstr_start; /* Position of last comment/string starter. */
157 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
158 of levels (starting from outermost). */
159 int prev_syntax; /* Syntax of previous position scanned, when
160 that position (potentially) holds the first char
161 of a 2-char construct, i.e. comment delimiter
162 or Sescape, etc. Smax otherwise. */
165 /* These variables are a cache for finding the start of a defun.
166 find_start_pos is the place for which the defun start was found.
167 find_start_value is the defun start position found for it.
168 find_start_value_byte is the corresponding byte position.
169 find_start_buffer is the buffer it was found in.
170 find_start_begv is the BEGV value when it was found.
171 find_start_modiff is the value of MODIFF when it was found. */
173 static ptrdiff_t find_start_pos;
174 static ptrdiff_t find_start_value;
175 static ptrdiff_t find_start_value_byte;
176 static struct buffer *find_start_buffer;
177 static ptrdiff_t find_start_begv;
178 static EMACS_INT find_start_modiff;
181 static Lisp_Object skip_chars (bool, Lisp_Object, Lisp_Object, bool);
182 static Lisp_Object skip_syntaxes (bool, Lisp_Object, Lisp_Object);
183 static Lisp_Object scan_lists (EMACS_INT, EMACS_INT, EMACS_INT, bool);
184 static void scan_sexps_forward (struct lisp_parse_state *,
185 ptrdiff_t, ptrdiff_t, ptrdiff_t, EMACS_INT,
186 bool, int);
187 static void internalize_parse_state (Lisp_Object, struct lisp_parse_state *);
188 static bool in_classes (int, Lisp_Object);
189 static void parse_sexp_propertize (ptrdiff_t charpos);
191 /* This setter is used only in this file, so it can be private. */
192 static void
193 bset_syntax_table (struct buffer *b, Lisp_Object val)
195 b->syntax_table_ = val;
198 /* Whether the syntax of the character C has the prefix flag set. */
199 bool
200 syntax_prefix_flag_p (int c)
202 return SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c));
205 struct gl_state_s gl_state; /* Global state of syntax parser. */
207 enum { INTERVALS_AT_ONCE = 10 }; /* 1 + max-number of intervals
208 to scan to property-change. */
210 /* Set the syntax entry VAL for char C in table TABLE. */
212 static void
213 SET_RAW_SYNTAX_ENTRY (Lisp_Object table, int c, Lisp_Object val)
215 CHAR_TABLE_SET (table, c, val);
218 /* Set the syntax entry VAL for char-range RANGE in table TABLE.
219 RANGE is a cons (FROM . TO) specifying the range of characters. */
221 static void
222 SET_RAW_SYNTAX_ENTRY_RANGE (Lisp_Object table, Lisp_Object range,
223 Lisp_Object val)
225 Fset_char_table_range (table, range, val);
228 /* Extract the information from the entry for character C
229 in the current syntax table. */
231 static Lisp_Object
232 SYNTAX_MATCH (int c)
234 Lisp_Object ent = SYNTAX_ENTRY (c);
235 return CONSP (ent) ? XCDR (ent) : Qnil;
238 /* This should be called with FROM at the start of forward
239 search, or after the last position of the backward search. It
240 makes sure that the first char is picked up with correct table, so
241 one does not need to call UPDATE_SYNTAX_TABLE immediately after the
242 call.
243 Sign of COUNT gives the direction of the search.
246 static void
247 SETUP_SYNTAX_TABLE (ptrdiff_t from, ptrdiff_t count)
249 SETUP_BUFFER_SYNTAX_TABLE ();
250 gl_state.b_property = BEGV;
251 gl_state.e_property = ZV + 1;
252 gl_state.object = Qnil;
253 gl_state.offset = 0;
254 if (parse_sexp_lookup_properties)
256 if (count > 0)
257 update_syntax_table_forward (from, true, Qnil);
258 else if (from > BEGV)
260 update_syntax_table (from - 1, count, true, Qnil);
261 parse_sexp_propertize (from - 1);
266 /* Same as above, but in OBJECT. If OBJECT is nil, use current buffer.
267 If it is t (which is only used in fast_c_string_match_ignore_case),
268 ignore properties altogether.
270 This is meant for regex.c to use. For buffers, regex.c passes arguments
271 to the UPDATE_SYNTAX_TABLE functions which are relative to BEGV.
272 So if it is a buffer, we set the offset field to BEGV. */
274 void
275 SETUP_SYNTAX_TABLE_FOR_OBJECT (Lisp_Object object,
276 ptrdiff_t from, ptrdiff_t count)
278 SETUP_BUFFER_SYNTAX_TABLE ();
279 gl_state.object = object;
280 if (BUFFERP (gl_state.object))
282 struct buffer *buf = XBUFFER (gl_state.object);
283 gl_state.b_property = 1;
284 gl_state.e_property = BUF_ZV (buf) - BUF_BEGV (buf) + 1;
285 gl_state.offset = BUF_BEGV (buf) - 1;
287 else if (NILP (gl_state.object))
289 gl_state.b_property = 1;
290 gl_state.e_property = ZV - BEGV + 1;
291 gl_state.offset = BEGV - 1;
293 else if (EQ (gl_state.object, Qt))
295 gl_state.b_property = 0;
296 gl_state.e_property = PTRDIFF_MAX;
297 gl_state.offset = 0;
299 else
301 gl_state.b_property = 0;
302 gl_state.e_property = 1 + SCHARS (gl_state.object);
303 gl_state.offset = 0;
305 if (parse_sexp_lookup_properties)
306 update_syntax_table (from + gl_state.offset - (count <= 0),
307 count, 1, gl_state.object);
310 /* Update gl_state to an appropriate interval which contains CHARPOS. The
311 sign of COUNT give the relative position of CHARPOS wrt the previously
312 valid interval. If INIT, only [be]_property fields of gl_state are
313 valid at start, the rest is filled basing on OBJECT.
315 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
316 direction than the intervals - or in an interval. We update the
317 current syntax-table basing on the property of this interval, and
318 update the interval to start further than CHARPOS - or be
319 NULL. We also update lim_property to be the next value of
320 charpos to call this subroutine again - or be before/after the
321 start/end of OBJECT. */
323 void
324 update_syntax_table (ptrdiff_t charpos, EMACS_INT count, bool init,
325 Lisp_Object object)
327 Lisp_Object tmp_table;
328 int cnt = 0;
329 bool invalidate = true;
330 INTERVAL i;
332 if (init)
334 gl_state.old_prop = Qnil;
335 gl_state.start = gl_state.b_property;
336 gl_state.stop = gl_state.e_property;
337 i = interval_of (charpos, object);
338 gl_state.backward_i = gl_state.forward_i = i;
339 invalidate = false;
340 if (!i)
341 return;
342 /* interval_of updates only ->position of the return value, so
343 update the parents manually to speed up update_interval. */
344 while (!NULL_PARENT (i))
346 if (AM_RIGHT_CHILD (i))
347 INTERVAL_PARENT (i)->position = i->position
348 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
349 - TOTAL_LENGTH (INTERVAL_PARENT (i))
350 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
351 else
352 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
353 + TOTAL_LENGTH (i);
354 i = INTERVAL_PARENT (i);
356 i = gl_state.forward_i;
357 gl_state.b_property = i->position - gl_state.offset;
358 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
359 goto update;
361 i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
363 /* We are guaranteed to be called with CHARPOS either in i,
364 or further off. */
365 if (!i)
366 error ("Error in syntax_table logic for to-the-end intervals");
367 else if (charpos < i->position) /* Move left. */
369 if (count > 0)
370 error ("Error in syntax_table logic for intervals <-");
371 /* Update the interval. */
372 i = update_interval (i, charpos);
373 if (INTERVAL_LAST_POS (i) != gl_state.b_property)
375 invalidate = false;
376 gl_state.forward_i = i;
377 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
380 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
382 if (count < 0)
383 error ("Error in syntax_table logic for intervals ->");
384 /* Update the interval. */
385 i = update_interval (i, charpos);
386 if (i->position != gl_state.e_property)
388 invalidate = false;
389 gl_state.backward_i = i;
390 gl_state.b_property = i->position - gl_state.offset;
394 update:
395 tmp_table = textget (i->plist, Qsyntax_table);
397 if (invalidate)
398 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
400 if (invalidate) /* Did not get to adjacent interval. */
401 { /* with the same table => */
402 /* invalidate the old range. */
403 if (count > 0)
405 gl_state.backward_i = i;
406 gl_state.b_property = i->position - gl_state.offset;
408 else
410 gl_state.forward_i = i;
411 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
415 if (!EQ (tmp_table, gl_state.old_prop))
417 gl_state.current_syntax_table = tmp_table;
418 gl_state.old_prop = tmp_table;
419 if (EQ (Fsyntax_table_p (tmp_table), Qt))
421 gl_state.use_global = 0;
423 else if (CONSP (tmp_table))
425 gl_state.use_global = 1;
426 gl_state.global_code = tmp_table;
428 else
430 gl_state.use_global = 0;
431 gl_state.current_syntax_table = BVAR (current_buffer, syntax_table);
435 while (i)
437 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
439 if (count > 0)
441 gl_state.e_property = i->position - gl_state.offset;
442 gl_state.forward_i = i;
444 else
446 gl_state.b_property
447 = i->position + LENGTH (i) - gl_state.offset;
448 gl_state.backward_i = i;
450 return;
452 else if (cnt == INTERVALS_AT_ONCE)
454 if (count > 0)
456 gl_state.e_property
457 = i->position + LENGTH (i) - gl_state.offset
458 /* e_property at EOB is not set to ZV but to ZV+1, so that
459 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
460 having to check eob between the two. */
461 + (next_interval (i) ? 0 : 1);
462 gl_state.forward_i = i;
464 else
466 gl_state.b_property = i->position - gl_state.offset;
467 gl_state.backward_i = i;
469 return;
471 cnt++;
472 i = count > 0 ? next_interval (i) : previous_interval (i);
474 eassert (i == NULL); /* This property goes to the end. */
475 if (count > 0)
477 gl_state.e_property = gl_state.stop;
478 gl_state.forward_i = i;
480 else
481 gl_state.b_property = gl_state.start;
484 static void
485 parse_sexp_propertize (ptrdiff_t charpos)
487 EMACS_INT zv = ZV;
488 if (syntax_propertize__done <= charpos
489 && syntax_propertize__done < zv)
491 EMACS_INT modiffs = CHARS_MODIFF;
492 safe_call1 (Qinternal__syntax_propertize,
493 make_number (min (zv, 1 + charpos)));
494 if (modiffs != CHARS_MODIFF)
495 error ("parse-sexp-propertize-function modified the buffer!");
496 if (syntax_propertize__done <= charpos
497 && syntax_propertize__done < zv)
498 error ("parse-sexp-propertize-function did not move"
499 " syntax-propertize--done");
500 SETUP_SYNTAX_TABLE (charpos, 1);
502 else if (gl_state.e_property > syntax_propertize__done)
504 gl_state.e_property = syntax_propertize__done;
505 gl_state.e_property_truncated = true;
507 else if (gl_state.e_property_truncated
508 && gl_state.e_property < syntax_propertize__done)
509 { /* When moving backward, e_property might be set without resetting
510 e_property_truncated, so the e_property_truncated flag may
511 occasionally be left raised spuriously. This should be rare. */
512 gl_state.e_property_truncated = false;
513 update_syntax_table_forward (charpos, false, Qnil);
517 void
518 update_syntax_table_forward (ptrdiff_t charpos, bool init,
519 Lisp_Object object)
521 if (gl_state.e_property_truncated)
523 eassert (NILP (object));
524 eassert (charpos >= gl_state.e_property);
525 parse_sexp_propertize (charpos);
527 else
529 update_syntax_table (charpos, 1, init, object);
530 if (NILP (object) && gl_state.e_property > syntax_propertize__done)
531 parse_sexp_propertize (charpos);
535 /* Returns true if char at CHARPOS is quoted.
536 Global syntax-table data should be set up already to be good at CHARPOS
537 or after. On return global syntax data is good for lookup at CHARPOS. */
539 static bool
540 char_quoted (ptrdiff_t charpos, ptrdiff_t bytepos)
542 enum syntaxcode code;
543 ptrdiff_t beg = BEGV;
544 bool quoted = 0;
545 ptrdiff_t orig = charpos;
547 while (charpos > beg)
549 int c;
550 DEC_BOTH (charpos, bytepos);
552 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
553 c = FETCH_CHAR_AS_MULTIBYTE (bytepos);
554 code = SYNTAX (c);
555 if (! (code == Scharquote || code == Sescape))
556 break;
558 quoted = !quoted;
561 UPDATE_SYNTAX_TABLE (orig);
562 return quoted;
565 /* Return the bytepos one character before BYTEPOS.
566 We assume that BYTEPOS is not at the start of the buffer. */
568 static ptrdiff_t
569 dec_bytepos (ptrdiff_t bytepos)
571 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
572 return bytepos - 1;
574 DEC_POS (bytepos);
575 return bytepos;
578 /* Return a defun-start position before POS and not too far before.
579 It should be the last one before POS, or nearly the last.
581 When open_paren_in_column_0_is_defun_start is nonzero,
582 only the beginning of the buffer is treated as a defun-start.
584 We record the information about where the scan started
585 and what its result was, so that another call in the same area
586 can return the same value very quickly.
588 There is no promise at which position the global syntax data is
589 valid on return from the subroutine, so the caller should explicitly
590 update the global data. */
592 static ptrdiff_t
593 find_defun_start (ptrdiff_t pos, ptrdiff_t pos_byte)
595 ptrdiff_t opoint = PT, opoint_byte = PT_BYTE;
597 /* Use previous finding, if it's valid and applies to this inquiry. */
598 if (current_buffer == find_start_buffer
599 /* Reuse the defun-start even if POS is a little farther on.
600 POS might be in the next defun, but that's ok.
601 Our value may not be the best possible, but will still be usable. */
602 && pos <= find_start_pos + 1000
603 && pos >= find_start_value
604 && BEGV == find_start_begv
605 && MODIFF == find_start_modiff)
606 return find_start_value;
608 if (!NILP (Vcomment_use_syntax_ppss))
610 EMACS_INT modiffs = CHARS_MODIFF;
611 Lisp_Object ppss = call1 (Qsyntax_ppss, make_number (pos));
612 if (modiffs != CHARS_MODIFF)
613 error ("syntax-ppss modified the buffer!");
614 TEMP_SET_PT_BOTH (opoint, opoint_byte);
615 Lisp_Object boc = Fnth (make_number (8), ppss);
616 if (NUMBERP (boc))
618 find_start_value = XINT (boc);
619 find_start_value_byte = CHAR_TO_BYTE (find_start_value);
621 else
623 find_start_value = pos;
624 find_start_value_byte = pos_byte;
626 goto found;
628 if (!open_paren_in_column_0_is_defun_start)
630 find_start_value = BEGV;
631 find_start_value_byte = BEGV_BYTE;
632 goto found;
635 /* Back up to start of line. */
636 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
638 /* We optimize syntax-table lookup for rare updates. Thus we accept
639 only those `^\s(' which are good in global _and_ text-property
640 syntax-tables. */
641 SETUP_BUFFER_SYNTAX_TABLE ();
642 while (PT > BEGV)
644 /* Open-paren at start of line means we may have found our
645 defun-start. */
646 int c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
647 if (SYNTAX (c) == Sopen)
649 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
650 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
651 if (SYNTAX (c) == Sopen)
652 break;
653 /* Now fallback to the default value. */
654 SETUP_BUFFER_SYNTAX_TABLE ();
656 /* Move to beg of previous line. */
657 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
660 /* Record what we found, for the next try. */
661 find_start_value = PT;
662 find_start_value_byte = PT_BYTE;
663 TEMP_SET_PT_BOTH (opoint, opoint_byte);
665 found:
666 find_start_buffer = current_buffer;
667 find_start_modiff = MODIFF;
668 find_start_begv = BEGV;
669 find_start_pos = pos;
671 return find_start_value;
674 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
676 static bool
677 prev_char_comend_first (ptrdiff_t pos, ptrdiff_t pos_byte)
679 int c;
680 bool val;
682 DEC_BOTH (pos, pos_byte);
683 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
684 c = FETCH_CHAR (pos_byte);
685 val = SYNTAX_COMEND_FIRST (c);
686 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
687 return val;
690 /* Check whether charpos FROM is at the end of a comment.
691 FROM_BYTE is the bytepos corresponding to FROM.
692 Do not move back before STOP.
694 Return true if we find a comment ending at FROM/FROM_BYTE.
696 If successful, store the charpos of the comment's beginning
697 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
699 Global syntax data remains valid for backward search starting at
700 the returned value (or at FROM, if the search was not successful). */
702 static bool
703 back_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop,
704 bool comnested, int comstyle, ptrdiff_t *charpos_ptr,
705 ptrdiff_t *bytepos_ptr)
707 /* Look back, counting the parity of string-quotes,
708 and recording the comment-starters seen.
709 When we reach a safe place, assume that's not in a string;
710 then step the main scan to the earliest comment-starter seen
711 an even number of string quotes away from the safe place.
713 OFROM[I] is position of the earliest comment-starter seen
714 which is I+2X quotes from the comment-end.
715 PARITY is current parity of quotes from the comment end. */
716 int string_style = -1; /* Presumed outside of any string. */
717 bool string_lossage = 0;
718 /* Not a real lossage: indicates that we have passed a matching comment
719 starter plus a non-matching comment-ender, meaning that any matching
720 comment-starter we might see later could be a false positive (hidden
721 inside another comment).
722 Test case: { a (* b } c (* d *) */
723 bool comment_lossage = 0;
724 ptrdiff_t comment_end = from;
725 ptrdiff_t comment_end_byte = from_byte;
726 ptrdiff_t comstart_pos = 0;
727 ptrdiff_t comstart_byte;
728 /* Place where the containing defun starts,
729 or 0 if we didn't come across it yet. */
730 ptrdiff_t defun_start = 0;
731 ptrdiff_t defun_start_byte = 0;
732 enum syntaxcode code;
733 ptrdiff_t nesting = 1; /* Current comment nesting. */
734 int c;
735 int syntax = 0;
736 unsigned short int quit_count = 0;
738 /* FIXME: A }} comment-ender style leads to incorrect behavior
739 in the case of {{ c }}} because we ignore the last two chars which are
740 assumed to be comment-enders although they aren't. */
742 /* At beginning of range to scan, we're outside of strings;
743 that determines quote parity to the comment-end. */
744 while (from != stop)
746 rarely_quit (++quit_count);
748 ptrdiff_t temp_byte;
749 int prev_syntax;
750 bool com2start, com2end, comstart;
752 /* Move back and examine a character. */
753 DEC_BOTH (from, from_byte);
754 UPDATE_SYNTAX_TABLE_BACKWARD (from);
756 prev_syntax = syntax;
757 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
758 syntax = SYNTAX_WITH_FLAGS (c);
759 code = SYNTAX (c);
761 /* Check for 2-char comment markers. */
762 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
763 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
764 && (comstyle
765 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax, syntax))
766 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
767 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
768 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
769 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
770 comstart = (com2start || code == Scomment);
772 /* Nasty cases with overlapping 2-char comment markers:
773 - snmp-mode: -- c -- foo -- c --
774 --- c --
775 ------ c --
776 - c-mode: *||*
777 |* *|* *|
778 |*| |* |*|
779 /// */
781 /* If a 2-char comment sequence partly overlaps with another,
782 we don't try to be clever. E.g. |*| in C, or }% in modes that
783 have %..\n and %{..}%. */
784 if (from > stop && (com2end || comstart))
786 ptrdiff_t next = from, next_byte = from_byte;
787 int next_c, next_syntax;
788 DEC_BOTH (next, next_byte);
789 UPDATE_SYNTAX_TABLE_BACKWARD (next);
790 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
791 next_syntax = SYNTAX_WITH_FLAGS (next_c);
792 if (((comstart || comnested)
793 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
794 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
795 || ((com2end || comnested)
796 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
797 && (comstyle
798 == SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_syntax))
799 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
800 goto lossage;
801 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
804 if (com2start && comstart_pos == 0)
805 /* We're looking at a comment starter. But it might be a comment
806 ender as well (see snmp-mode). The first time we see one, we
807 need to consider it as a comment starter,
808 and the subsequent times as a comment ender. */
809 com2end = 0;
811 /* Turn a 2-char comment sequences into the appropriate syntax. */
812 if (com2end)
813 code = Sendcomment;
814 else if (com2start)
815 code = Scomment;
816 /* Ignore comment starters of a different style. */
817 else if (code == Scomment
818 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0)
819 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
820 continue;
822 /* Ignore escaped characters, except comment-enders which cannot
823 be escaped. */
824 if ((Vcomment_end_can_be_escaped || code != Sendcomment)
825 && char_quoted (from, from_byte))
826 continue;
828 switch (code)
830 case Sstring_fence:
831 case Scomment_fence:
832 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
833 FALLTHROUGH;
834 case Sstring:
835 /* Track parity of quotes. */
836 if (string_style == -1)
837 /* Entering a string. */
838 string_style = c;
839 else if (string_style == c)
840 /* Leaving the string. */
841 string_style = -1;
842 else
843 /* If we have two kinds of string delimiters.
844 There's no way to grok this scanning backwards. */
845 string_lossage = 1;
846 break;
848 case Scomment:
849 /* We've already checked that it is the relevant comstyle. */
850 if (string_style != -1 || comment_lossage || string_lossage)
851 /* There are odd string quotes involved, so let's be careful.
852 Test case in Pascal: " { " a { " } */
853 goto lossage;
855 if (!comnested)
857 /* Record best comment-starter so far. */
858 comstart_pos = from;
859 comstart_byte = from_byte;
861 else if (--nesting <= 0)
862 /* nested comments have to be balanced, so we don't need to
863 keep looking for earlier ones. We use here the same (slightly
864 incorrect) reasoning as below: since it is followed by uniform
865 paired string quotes, this comment-start has to be outside of
866 strings, else the comment-end itself would be inside a string. */
867 goto done;
868 break;
870 case Sendcomment:
871 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == comstyle
872 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
873 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
874 /* This is the same style of comment ender as ours. */
876 if (comnested)
877 nesting++;
878 else
879 /* Anything before that can't count because it would match
880 this comment-ender rather than ours. */
881 from = stop; /* Break out of the loop. */
883 else if (comstart_pos != 0 || c != '\n')
884 /* We're mixing comment styles here, so we'd better be careful.
885 The (comstart_pos != 0 || c != '\n') check is not quite correct
886 (we should just always set comment_lossage), but removing it
887 would imply that any multiline comment in C would go through
888 lossage, which seems overkill.
889 The failure should only happen in the rare cases such as
890 { (* } *) */
891 comment_lossage = 1;
892 break;
894 case Sopen:
895 /* Assume a defun-start point is outside of strings. */
896 if (open_paren_in_column_0_is_defun_start
897 && NILP (Vcomment_use_syntax_ppss)
898 && (from == stop
899 || (temp_byte = dec_bytepos (from_byte),
900 FETCH_CHAR (temp_byte) == '\n')))
902 defun_start = from;
903 defun_start_byte = from_byte;
904 from = stop; /* Break out of the loop. */
906 break;
908 default:
909 break;
913 if (comstart_pos == 0)
915 from = comment_end;
916 from_byte = comment_end_byte;
917 UPDATE_SYNTAX_TABLE_FORWARD (comment_end);
919 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
920 or `done'), then we've found the beginning of the non-nested comment. */
921 else if (1) /* !comnested */
923 from = comstart_pos;
924 from_byte = comstart_byte;
925 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
927 else lossage:
929 struct lisp_parse_state state;
930 bool adjusted = true;
931 /* We had two kinds of string delimiters mixed up
932 together. Decode this going forwards.
933 Scan fwd from a known safe place (beginning-of-defun)
934 to the one in question; this records where we
935 last passed a comment starter. */
936 /* If we did not already find the defun start, find it now. */
937 if (defun_start == 0)
939 defun_start = find_defun_start (comment_end, comment_end_byte);
940 defun_start_byte = find_start_value_byte;
941 adjusted = (defun_start > BEGV);
945 internalize_parse_state (Qnil, &state);
946 scan_sexps_forward (&state,
947 defun_start, defun_start_byte,
948 comment_end, TYPE_MINIMUM (EMACS_INT),
949 0, 0);
950 defun_start = comment_end;
951 if (!adjusted)
953 adjusted = true;
954 find_start_value
955 = CONSP (state.levelstarts) ? XINT (XCAR (state.levelstarts))
956 : state.thislevelstart >= 0 ? state.thislevelstart
957 : find_start_value;
958 find_start_value_byte = CHAR_TO_BYTE (find_start_value);
961 if (state.incomment == (comnested ? 1 : -1)
962 && state.comstyle == comstyle)
963 from = state.comstr_start;
964 else
966 from = comment_end;
967 if (state.incomment)
968 /* If comment_end is inside some other comment, maybe ours
969 is nested, so we need to try again from within the
970 surrounding comment. Example: { a (* " *) */
972 /* FIXME: We should advance by one or two chars. */
973 defun_start = state.comstr_start + 2;
974 defun_start_byte = CHAR_TO_BYTE (defun_start);
977 rarely_quit (++quit_count);
979 while (defun_start < comment_end);
981 from_byte = CHAR_TO_BYTE (from);
982 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
985 done:
986 *charpos_ptr = from;
987 *bytepos_ptr = from_byte;
989 return from != comment_end;
992 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
993 doc: /* Return t if OBJECT is a syntax table.
994 Currently, any char-table counts as a syntax table. */)
995 (Lisp_Object object)
997 if (CHAR_TABLE_P (object)
998 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
999 return Qt;
1000 return Qnil;
1003 static void
1004 check_syntax_table (Lisp_Object obj)
1006 CHECK_TYPE (CHAR_TABLE_P (obj) && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table),
1007 Qsyntax_table_p, obj);
1010 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
1011 doc: /* Return the current syntax table.
1012 This is the one specified by the current buffer. */)
1013 (void)
1015 return BVAR (current_buffer, syntax_table);
1018 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
1019 Sstandard_syntax_table, 0, 0, 0,
1020 doc: /* Return the standard syntax table.
1021 This is the one used for new buffers. */)
1022 (void)
1024 return Vstandard_syntax_table;
1027 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
1028 doc: /* Construct a new syntax table and return it.
1029 It is a copy of the TABLE, which defaults to the standard syntax table. */)
1030 (Lisp_Object table)
1032 Lisp_Object copy;
1034 if (!NILP (table))
1035 check_syntax_table (table);
1036 else
1037 table = Vstandard_syntax_table;
1039 copy = Fcopy_sequence (table);
1041 /* Only the standard syntax table should have a default element.
1042 Other syntax tables should inherit from parents instead. */
1043 set_char_table_defalt (copy, Qnil);
1045 /* Copied syntax tables should all have parents.
1046 If we copied one with no parent, such as the standard syntax table,
1047 use the standard syntax table as the copy's parent. */
1048 if (NILP (XCHAR_TABLE (copy)->parent))
1049 Fset_char_table_parent (copy, Vstandard_syntax_table);
1050 return copy;
1053 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
1054 doc: /* Select a new syntax table for the current buffer.
1055 One argument, a syntax table. */)
1056 (Lisp_Object table)
1058 int idx;
1059 check_syntax_table (table);
1060 bset_syntax_table (current_buffer, table);
1061 /* Indicate that this buffer now has a specified syntax table. */
1062 idx = PER_BUFFER_VAR_IDX (syntax_table);
1063 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
1064 return table;
1067 /* Convert a letter which signifies a syntax code
1068 into the code it signifies.
1069 This is used by modify-syntax-entry, and other things. */
1071 unsigned char const syntax_spec_code[0400] =
1072 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1073 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1074 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1075 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1076 Swhitespace, Scomment_fence, Sstring, 0377, Smath, 0377, 0377, Squote,
1077 Sopen, Sclose, 0377, 0377, 0377, Swhitespace, Spunct, Scharquote,
1078 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1079 0377, 0377, 0377, 0377, Scomment, 0377, Sendcomment, 0377,
1080 Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
1081 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1082 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword,
1083 0377, 0377, 0377, 0377, Sescape, 0377, 0377, Ssymbol,
1084 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
1085 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1086 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword,
1087 0377, 0377, 0377, 0377, Sstring_fence, 0377, 0377, 0377
1090 /* Indexed by syntax code, give the letter that describes it. */
1092 char const syntax_code_spec[16] =
1094 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
1095 '!', '|'
1098 /* Indexed by syntax code, give the object (cons of syntax code and
1099 nil) to be stored in syntax table. Since these objects can be
1100 shared among syntax tables, we generate them in advance. By
1101 sharing objects, the function `describe-syntax' can give a more
1102 compact listing. */
1103 static Lisp_Object Vsyntax_code_object;
1106 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
1107 doc: /* Return the syntax code of CHARACTER, described by a character.
1108 For example, if CHARACTER is a word constituent, the
1109 character `w' (119) is returned.
1110 The characters that correspond to various syntax codes
1111 are listed in the documentation of `modify-syntax-entry'.
1113 If you're trying to determine the syntax of characters in the buffer,
1114 this is probably the wrong function to use, because it can't take
1115 `syntax-table' text properties into account. Consider using
1116 `syntax-after' instead. */)
1117 (Lisp_Object character)
1119 int char_int;
1120 CHECK_CHARACTER (character);
1121 char_int = XINT (character);
1122 SETUP_BUFFER_SYNTAX_TABLE ();
1123 return make_number (syntax_code_spec[SYNTAX (char_int)]);
1126 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
1127 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
1128 (Lisp_Object character)
1130 int char_int;
1131 enum syntaxcode code;
1132 CHECK_CHARACTER (character);
1133 char_int = XINT (character);
1134 SETUP_BUFFER_SYNTAX_TABLE ();
1135 code = SYNTAX (char_int);
1136 if (code == Sopen || code == Sclose)
1137 return SYNTAX_MATCH (char_int);
1138 return Qnil;
1141 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
1142 doc: /* Convert a syntax descriptor STRING into a raw syntax descriptor.
1143 STRING should be a string of the form allowed as argument of
1144 `modify-syntax-entry'. The return value is a raw syntax descriptor: a
1145 cons cell (CODE . MATCHING-CHAR) which can be used, for example, as
1146 the value of a `syntax-table' text property. */)
1147 (Lisp_Object string)
1149 const unsigned char *p;
1150 int val;
1151 Lisp_Object match;
1153 CHECK_STRING (string);
1155 p = SDATA (string);
1156 val = syntax_spec_code[*p++];
1157 if (val == 0377)
1158 error ("Invalid syntax description letter: %c", p[-1]);
1160 if (val == Sinherit)
1161 return Qnil;
1163 if (*p)
1165 int len;
1166 int character = STRING_CHAR_AND_LENGTH (p, len);
1167 XSETINT (match, character);
1168 if (XFASTINT (match) == ' ')
1169 match = Qnil;
1170 p += len;
1172 else
1173 match = Qnil;
1175 while (*p)
1176 switch (*p++)
1178 case '1':
1179 val |= 1 << 16;
1180 break;
1182 case '2':
1183 val |= 1 << 17;
1184 break;
1186 case '3':
1187 val |= 1 << 18;
1188 break;
1190 case '4':
1191 val |= 1 << 19;
1192 break;
1194 case 'p':
1195 val |= 1 << 20;
1196 break;
1198 case 'b':
1199 val |= 1 << 21;
1200 break;
1202 case 'n':
1203 val |= 1 << 22;
1204 break;
1206 case 'c':
1207 val |= 1 << 23;
1208 break;
1211 if (val < ASIZE (Vsyntax_code_object) && NILP (match))
1212 return AREF (Vsyntax_code_object, val);
1213 else
1214 /* Since we can't use a shared object, let's make a new one. */
1215 return Fcons (make_number (val), match);
1218 /* I really don't know why this is interactive
1219 help-form should at least be made useful whilst reading the second arg. */
1220 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
1221 "cSet syntax for character: \nsSet syntax for %s to: ",
1222 doc: /* Set syntax for character CHAR according to string NEWENTRY.
1223 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1224 the current buffer's syntax table.
1225 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1226 in the range MIN to MAX are changed.
1227 The first character of NEWENTRY should be one of the following:
1228 Space or - whitespace syntax. w word constituent.
1229 _ symbol constituent. . punctuation.
1230 ( open-parenthesis. ) close-parenthesis.
1231 " string quote. \\ escape.
1232 $ paired delimiter. \\=' expression quote or prefix operator.
1233 < comment starter. > comment ender.
1234 / character-quote. @ inherit from parent table.
1235 | generic string fence. ! generic comment fence.
1237 Only single-character comment start and end sequences are represented thus.
1238 Two-character sequences are represented as described below.
1239 The second character of NEWENTRY is the matching parenthesis,
1240 used only if the first character is `(' or `)'.
1241 Any additional characters are flags.
1242 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1243 1 means CHAR is the start of a two-char comment start sequence.
1244 2 means CHAR is the second character of such a sequence.
1245 3 means CHAR is the start of a two-char comment end sequence.
1246 4 means CHAR is the second character of such a sequence.
1248 There can be several orthogonal comment sequences. This is to support
1249 language modes such as C++. By default, all comment sequences are of style
1250 a, but you can set the comment sequence style to b (on the second character
1251 of a comment-start, and the first character of a comment-end sequence) and/or
1252 c (on any of its chars) using this flag:
1253 b means CHAR is part of comment sequence b.
1254 c means CHAR is part of comment sequence c.
1255 n means CHAR is part of a nestable comment sequence.
1257 p means CHAR is a prefix character for `backward-prefix-chars';
1258 such characters are treated as whitespace when they occur
1259 between expressions.
1260 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1261 (Lisp_Object c, Lisp_Object newentry, Lisp_Object syntax_table)
1263 if (CONSP (c))
1265 CHECK_CHARACTER_CAR (c);
1266 CHECK_CHARACTER_CDR (c);
1268 else
1269 CHECK_CHARACTER (c);
1271 if (NILP (syntax_table))
1272 syntax_table = BVAR (current_buffer, syntax_table);
1273 else
1274 check_syntax_table (syntax_table);
1276 newentry = Fstring_to_syntax (newentry);
1277 if (CONSP (c))
1278 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1279 else
1280 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
1282 /* We clear the regexp cache, since character classes can now have
1283 different values from those in the compiled regexps.*/
1284 clear_regexp_cache ();
1286 return Qnil;
1289 /* Dump syntax table to buffer in human-readable format */
1291 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1292 Sinternal_describe_syntax_value, 1, 1, 0,
1293 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1294 (Lisp_Object syntax)
1296 int code, syntax_code;
1297 bool start1, start2, end1, end2, prefix, comstyleb, comstylec, comnested;
1298 char str[2];
1299 Lisp_Object first, match_lisp, value = syntax;
1301 if (NILP (value))
1303 insert_string ("default");
1304 return syntax;
1307 if (CHAR_TABLE_P (value))
1309 insert_string ("deeper char-table ...");
1310 return syntax;
1313 if (!CONSP (value))
1315 insert_string ("invalid");
1316 return syntax;
1319 first = XCAR (value);
1320 match_lisp = XCDR (value);
1322 if (!INTEGERP (first) || !(NILP (match_lisp) || CHARACTERP (match_lisp)))
1324 insert_string ("invalid");
1325 return syntax;
1328 syntax_code = XINT (first) & INT_MAX;
1329 code = syntax_code & 0377;
1330 start1 = SYNTAX_FLAGS_COMSTART_FIRST (syntax_code);
1331 start2 = SYNTAX_FLAGS_COMSTART_SECOND (syntax_code);
1332 end1 = SYNTAX_FLAGS_COMEND_FIRST (syntax_code);
1333 end2 = SYNTAX_FLAGS_COMEND_SECOND (syntax_code);
1334 prefix = SYNTAX_FLAGS_PREFIX (syntax_code);
1335 comstyleb = SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code);
1336 comstylec = SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code);
1337 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax_code);
1339 if (Smax <= code)
1341 insert_string ("invalid");
1342 return syntax;
1345 str[0] = syntax_code_spec[code], str[1] = 0;
1346 insert (str, 1);
1348 if (NILP (match_lisp))
1349 insert (" ", 1);
1350 else
1351 insert_char (XINT (match_lisp));
1353 if (start1)
1354 insert ("1", 1);
1355 if (start2)
1356 insert ("2", 1);
1358 if (end1)
1359 insert ("3", 1);
1360 if (end2)
1361 insert ("4", 1);
1363 if (prefix)
1364 insert ("p", 1);
1365 if (comstyleb)
1366 insert ("b", 1);
1367 if (comstylec)
1368 insert ("c", 1);
1369 if (comnested)
1370 insert ("n", 1);
1372 insert_string ("\twhich means: ");
1374 switch (code)
1376 case Swhitespace:
1377 insert_string ("whitespace"); break;
1378 case Spunct:
1379 insert_string ("punctuation"); break;
1380 case Sword:
1381 insert_string ("word"); break;
1382 case Ssymbol:
1383 insert_string ("symbol"); break;
1384 case Sopen:
1385 insert_string ("open"); break;
1386 case Sclose:
1387 insert_string ("close"); break;
1388 case Squote:
1389 insert_string ("prefix"); break;
1390 case Sstring:
1391 insert_string ("string"); break;
1392 case Smath:
1393 insert_string ("math"); break;
1394 case Sescape:
1395 insert_string ("escape"); break;
1396 case Scharquote:
1397 insert_string ("charquote"); break;
1398 case Scomment:
1399 insert_string ("comment"); break;
1400 case Sendcomment:
1401 insert_string ("endcomment"); break;
1402 case Sinherit:
1403 insert_string ("inherit"); break;
1404 case Scomment_fence:
1405 insert_string ("comment fence"); break;
1406 case Sstring_fence:
1407 insert_string ("string fence"); break;
1408 default:
1409 insert_string ("invalid");
1410 return syntax;
1413 if (!NILP (match_lisp))
1415 insert_string (", matches ");
1416 insert_char (XINT (match_lisp));
1419 if (start1)
1420 insert_string (",\n\t is the first character of a comment-start sequence");
1421 if (start2)
1422 insert_string (",\n\t is the second character of a comment-start sequence");
1424 if (end1)
1425 insert_string (",\n\t is the first character of a comment-end sequence");
1426 if (end2)
1427 insert_string (",\n\t is the second character of a comment-end sequence");
1428 if (comstyleb)
1429 insert_string (" (comment style b)");
1430 if (comstylec)
1431 insert_string (" (comment style c)");
1432 if (comnested)
1433 insert_string (" (nestable)");
1435 if (prefix)
1437 AUTO_STRING (prefixdoc,
1438 ",\n\t is a prefix character for `backward-prefix-chars'");
1439 insert1 (Fsubstitute_command_keys (prefixdoc));
1442 return syntax;
1445 /* Return the position across COUNT words from FROM.
1446 If that many words cannot be found before the end of the buffer, return 0.
1447 COUNT negative means scan backward and stop at word beginning. */
1449 ptrdiff_t
1450 scan_words (ptrdiff_t from, EMACS_INT count)
1452 ptrdiff_t beg = BEGV;
1453 ptrdiff_t end = ZV;
1454 ptrdiff_t from_byte = CHAR_TO_BYTE (from);
1455 enum syntaxcode code;
1456 int ch0, ch1;
1457 Lisp_Object func, pos;
1459 SETUP_SYNTAX_TABLE (from, count);
1461 while (count > 0)
1463 while (true)
1465 if (from == end)
1466 return 0;
1467 UPDATE_SYNTAX_TABLE_FORWARD (from);
1468 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1469 code = SYNTAX (ch0);
1470 INC_BOTH (from, from_byte);
1471 if (words_include_escapes
1472 && (code == Sescape || code == Scharquote))
1473 break;
1474 if (code == Sword)
1475 break;
1476 rarely_quit (from);
1478 /* Now CH0 is a character which begins a word and FROM is the
1479 position of the next character. */
1480 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
1481 if (! NILP (Ffboundp (func)))
1483 pos = call2 (func, make_number (from - 1), make_number (end));
1484 if (INTEGERP (pos) && from < XINT (pos) && XINT (pos) <= ZV)
1486 from = XINT (pos);
1487 from_byte = CHAR_TO_BYTE (from);
1490 else
1492 while (1)
1494 if (from == end) break;
1495 UPDATE_SYNTAX_TABLE_FORWARD (from);
1496 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1497 code = SYNTAX (ch1);
1498 if ((code != Sword
1499 && (! words_include_escapes
1500 || (code != Sescape && code != Scharquote)))
1501 || word_boundary_p (ch0, ch1))
1502 break;
1503 INC_BOTH (from, from_byte);
1504 ch0 = ch1;
1505 rarely_quit (from);
1508 count--;
1510 while (count < 0)
1512 while (true)
1514 if (from == beg)
1515 return 0;
1516 DEC_BOTH (from, from_byte);
1517 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1518 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1519 code = SYNTAX (ch1);
1520 if (words_include_escapes
1521 && (code == Sescape || code == Scharquote))
1522 break;
1523 if (code == Sword)
1524 break;
1525 rarely_quit (from);
1527 /* Now CH1 is a character which ends a word and FROM is the
1528 position of it. */
1529 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
1530 if (! NILP (Ffboundp (func)))
1532 pos = call2 (func, make_number (from), make_number (beg));
1533 if (INTEGERP (pos) && BEGV <= XINT (pos) && XINT (pos) < from)
1535 from = XINT (pos);
1536 from_byte = CHAR_TO_BYTE (from);
1539 else
1541 while (1)
1543 if (from == beg)
1544 break;
1545 DEC_BOTH (from, from_byte);
1546 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1547 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1548 code = SYNTAX (ch0);
1549 if ((code != Sword
1550 && (! words_include_escapes
1551 || (code != Sescape && code != Scharquote)))
1552 || word_boundary_p (ch0, ch1))
1554 INC_BOTH (from, from_byte);
1555 break;
1557 ch1 = ch0;
1558 rarely_quit (from);
1561 count++;
1564 return from;
1567 DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p",
1568 doc: /* Move point forward ARG words (backward if ARG is negative).
1569 If ARG is omitted or nil, move point forward one word.
1570 Normally returns t.
1571 If an edge of the buffer or a field boundary is reached, point is
1572 left there and the function returns nil. Field boundaries are not
1573 noticed if `inhibit-field-text-motion' is non-nil.
1575 The word boundaries are normally determined by the buffer's syntax
1576 table, but `find-word-boundary-function-table', such as set up
1577 by `subword-mode', can change that. If a Lisp program needs to
1578 move by words determined strictly by the syntax table, it should
1579 use `forward-word-strictly' instead. */)
1580 (Lisp_Object arg)
1582 Lisp_Object tmp;
1583 ptrdiff_t orig_val, val;
1585 if (NILP (arg))
1586 XSETFASTINT (arg, 1);
1587 else
1588 CHECK_NUMBER (arg);
1590 val = orig_val = scan_words (PT, XINT (arg));
1591 if (! orig_val)
1592 val = XINT (arg) > 0 ? ZV : BEGV;
1594 /* Avoid jumping out of an input field. */
1595 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1596 Qnil, Qnil, Qnil);
1597 val = XFASTINT (tmp);
1599 SET_PT (val);
1600 return val == orig_val ? Qt : Qnil;
1603 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1604 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1605 STRING is like the inside of a `[...]' in a regular expression
1606 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1607 (but not at the end of a range; quoting is never needed there).
1608 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1609 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1610 Char classes, e.g. `[:alpha:]', are supported.
1612 Returns the distance traveled, either zero or positive. */)
1613 (Lisp_Object string, Lisp_Object lim)
1615 return skip_chars (1, string, lim, 1);
1618 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1619 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1620 See `skip-chars-forward' for details.
1621 Returns the distance traveled, either zero or negative. */)
1622 (Lisp_Object string, Lisp_Object lim)
1624 return skip_chars (0, string, lim, 1);
1627 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1628 doc: /* Move point forward across chars in specified syntax classes.
1629 SYNTAX is a string of syntax code characters.
1630 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1631 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1632 This function returns the distance traveled, either zero or positive. */)
1633 (Lisp_Object syntax, Lisp_Object lim)
1635 return skip_syntaxes (1, syntax, lim);
1638 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1639 doc: /* Move point backward across chars in specified syntax classes.
1640 SYNTAX is a string of syntax code characters.
1641 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1642 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1643 This function returns either zero or a negative number, and the absolute value
1644 of this is the distance traveled. */)
1645 (Lisp_Object syntax, Lisp_Object lim)
1647 return skip_syntaxes (0, syntax, lim);
1650 static Lisp_Object
1651 skip_chars (bool forwardp, Lisp_Object string, Lisp_Object lim,
1652 bool handle_iso_classes)
1654 int c;
1655 char fastmap[0400];
1656 /* Store the ranges of non-ASCII characters. */
1657 int *char_ranges UNINIT;
1658 int n_char_ranges = 0;
1659 bool negate = 0;
1660 ptrdiff_t i, i_byte;
1661 /* True if the current buffer is multibyte and the region contains
1662 non-ASCII chars. */
1663 bool multibyte;
1664 /* True if STRING is multibyte and it contains non-ASCII chars. */
1665 bool string_multibyte;
1666 ptrdiff_t size_byte;
1667 const unsigned char *str;
1668 int len;
1669 Lisp_Object iso_classes;
1670 USE_SAFE_ALLOCA;
1672 CHECK_STRING (string);
1673 iso_classes = Qnil;
1675 if (NILP (lim))
1676 XSETINT (lim, forwardp ? ZV : BEGV);
1677 else
1678 CHECK_NUMBER_COERCE_MARKER (lim);
1680 /* In any case, don't allow scan outside bounds of buffer. */
1681 if (XINT (lim) > ZV)
1682 XSETFASTINT (lim, ZV);
1683 if (XINT (lim) < BEGV)
1684 XSETFASTINT (lim, BEGV);
1686 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1687 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1688 string_multibyte = SBYTES (string) > SCHARS (string);
1690 memset (fastmap, 0, sizeof fastmap);
1692 str = SDATA (string);
1693 size_byte = SBYTES (string);
1695 i_byte = 0;
1696 if (i_byte < size_byte
1697 && SREF (string, 0) == '^')
1699 negate = 1; i_byte++;
1702 /* Find the characters specified and set their elements of fastmap.
1703 Handle backslashes and ranges specially.
1705 If STRING contains non-ASCII characters, setup char_ranges for
1706 them and use fastmap only for their leading codes. */
1708 if (! string_multibyte)
1710 bool string_has_eight_bit = 0;
1712 /* At first setup fastmap. */
1713 while (i_byte < size_byte)
1715 if (handle_iso_classes)
1717 const unsigned char *ch = str + i_byte;
1718 re_wctype_t cc = re_wctype_parse (&ch, size_byte - i_byte);
1719 if (cc == 0)
1720 error ("Invalid ISO C character class");
1721 if (cc != -1)
1723 iso_classes = Fcons (make_number (cc), iso_classes);
1724 i_byte = ch - str;
1725 continue;
1729 c = str[i_byte++];
1731 if (c == '\\')
1733 if (i_byte == size_byte)
1734 break;
1736 c = str[i_byte++];
1738 /* Treat `-' as range character only if another character
1739 follows. */
1740 if (i_byte + 1 < size_byte
1741 && str[i_byte] == '-')
1743 int c2;
1745 /* Skip over the dash. */
1746 i_byte++;
1748 /* Get the end of the range. */
1749 c2 = str[i_byte++];
1750 if (c2 == '\\'
1751 && i_byte < size_byte)
1752 c2 = str[i_byte++];
1754 if (c <= c2)
1756 int lim2 = c2 + 1;
1757 while (c < lim2)
1758 fastmap[c++] = 1;
1759 if (! ASCII_CHAR_P (c2))
1760 string_has_eight_bit = 1;
1763 else
1765 fastmap[c] = 1;
1766 if (! ASCII_CHAR_P (c))
1767 string_has_eight_bit = 1;
1771 /* If the current range is multibyte and STRING contains
1772 eight-bit chars, arrange fastmap and setup char_ranges for
1773 the corresponding multibyte chars. */
1774 if (multibyte && string_has_eight_bit)
1776 char *p1;
1777 char himap[0200 + 1];
1778 memcpy (himap, fastmap + 0200, 0200);
1779 himap[0200] = 0;
1780 memset (fastmap + 0200, 0, 0200);
1781 SAFE_NALLOCA (char_ranges, 2, 128);
1782 i = 0;
1784 while ((p1 = memchr (himap + i, 1, 0200 - i)))
1786 /* Deduce the next range C..C2 from the next clump of 1s
1787 in HIMAP starting with &HIMAP[I]. HIMAP is the high
1788 order half of the old FASTMAP. */
1789 int c2, leading_code;
1790 i = p1 - himap;
1791 c = BYTE8_TO_CHAR (i + 0200);
1792 i += strlen (p1);
1793 c2 = BYTE8_TO_CHAR (i + 0200 - 1);
1795 char_ranges[n_char_ranges++] = c;
1796 char_ranges[n_char_ranges++] = c2;
1797 leading_code = CHAR_LEADING_CODE (c);
1798 memset (fastmap + leading_code, 1,
1799 CHAR_LEADING_CODE (c2) - leading_code + 1);
1803 else /* STRING is multibyte */
1805 SAFE_NALLOCA (char_ranges, 2, SCHARS (string));
1807 while (i_byte < size_byte)
1809 int leading_code = str[i_byte];
1811 if (handle_iso_classes)
1813 const unsigned char *ch = str + i_byte;
1814 re_wctype_t cc = re_wctype_parse (&ch, size_byte - i_byte);
1815 if (cc == 0)
1816 error ("Invalid ISO C character class");
1817 if (cc != -1)
1819 iso_classes = Fcons (make_number (cc), iso_classes);
1820 i_byte = ch - str;
1821 continue;
1825 if (leading_code== '\\')
1827 if (++i_byte == size_byte)
1828 break;
1830 leading_code = str[i_byte];
1832 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1833 i_byte += len;
1836 /* Treat `-' as range character only if another character
1837 follows. */
1838 if (i_byte + 1 < size_byte
1839 && str[i_byte] == '-')
1841 int c2, leading_code2;
1843 /* Skip over the dash. */
1844 i_byte++;
1846 /* Get the end of the range. */
1847 leading_code2 = str[i_byte];
1848 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1849 i_byte += len;
1851 if (c2 == '\\'
1852 && i_byte < size_byte)
1854 leading_code2 = str[i_byte];
1855 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1856 i_byte += len;
1859 if (c > c2)
1860 continue;
1861 if (ASCII_CHAR_P (c))
1863 while (c <= c2 && c < 0x80)
1864 fastmap[c++] = 1;
1865 leading_code = CHAR_LEADING_CODE (c);
1867 if (! ASCII_CHAR_P (c))
1869 int lim2 = leading_code2 + 1;
1870 while (leading_code < lim2)
1871 fastmap[leading_code++] = 1;
1872 if (c <= c2)
1874 char_ranges[n_char_ranges++] = c;
1875 char_ranges[n_char_ranges++] = c2;
1879 else
1881 if (ASCII_CHAR_P (c))
1882 fastmap[c] = 1;
1883 else
1885 fastmap[leading_code] = 1;
1886 char_ranges[n_char_ranges++] = c;
1887 char_ranges[n_char_ranges++] = c;
1892 /* If the current range is unibyte and STRING contains non-ASCII
1893 chars, arrange fastmap for the corresponding unibyte
1894 chars. */
1896 if (! multibyte && n_char_ranges > 0)
1898 memset (fastmap + 0200, 0, 0200);
1899 for (i = 0; i < n_char_ranges; i += 2)
1901 int c1 = char_ranges[i];
1902 int lim2 = char_ranges[i + 1] + 1;
1904 for (; c1 < lim2; c1++)
1906 int b = CHAR_TO_BYTE_SAFE (c1);
1907 if (b >= 0)
1908 fastmap[b] = 1;
1914 /* If ^ was the first character, complement the fastmap. */
1915 if (negate)
1917 if (! multibyte)
1918 for (i = 0; i < sizeof fastmap; i++)
1919 fastmap[i] ^= 1;
1920 else
1922 for (i = 0; i < 0200; i++)
1923 fastmap[i] ^= 1;
1924 /* All non-ASCII chars possibly match. */
1925 for (; i < sizeof fastmap; i++)
1926 fastmap[i] = 1;
1931 ptrdiff_t start_point = PT;
1932 ptrdiff_t pos = PT;
1933 ptrdiff_t pos_byte = PT_BYTE;
1934 unsigned char *p = PT_ADDR, *endp, *stop;
1936 if (forwardp)
1938 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1939 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1941 else
1943 endp = CHAR_POS_ADDR (XINT (lim));
1944 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1947 /* This code may look up syntax tables using functions that rely on the
1948 gl_state object. To make sure this object is not out of date,
1949 let's initialize it manually.
1950 We ignore syntax-table text-properties for now, since that's
1951 what we've done in the past. */
1952 SETUP_BUFFER_SYNTAX_TABLE ();
1953 if (forwardp)
1955 if (multibyte)
1956 while (1)
1958 int nbytes;
1960 if (p >= stop)
1962 if (p >= endp)
1963 break;
1964 p = GAP_END_ADDR;
1965 stop = endp;
1967 c = STRING_CHAR_AND_LENGTH (p, nbytes);
1968 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1970 if (negate)
1971 break;
1972 else
1973 goto fwd_ok;
1976 if (! fastmap[*p])
1977 break;
1978 if (! ASCII_CHAR_P (c))
1980 /* As we are looking at a multibyte character, we
1981 must look up the character in the table
1982 CHAR_RANGES. If there's no data in the table,
1983 that character is not what we want to skip. */
1985 /* The following code do the right thing even if
1986 n_char_ranges is zero (i.e. no data in
1987 CHAR_RANGES). */
1988 for (i = 0; i < n_char_ranges; i += 2)
1989 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1990 break;
1991 if (!(negate ^ (i < n_char_ranges)))
1992 break;
1994 fwd_ok:
1995 p += nbytes, pos++, pos_byte += nbytes;
1996 rarely_quit (pos);
1998 else
1999 while (true)
2001 if (p >= stop)
2003 if (p >= endp)
2004 break;
2005 p = GAP_END_ADDR;
2006 stop = endp;
2009 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
2011 if (negate)
2012 break;
2013 else
2014 goto fwd_unibyte_ok;
2017 if (!fastmap[*p])
2018 break;
2019 fwd_unibyte_ok:
2020 p++, pos++, pos_byte++;
2021 rarely_quit (pos);
2024 else
2026 if (multibyte)
2027 while (true)
2029 if (p <= stop)
2031 if (p <= endp)
2032 break;
2033 p = GPT_ADDR;
2034 stop = endp;
2036 unsigned char *prev_p = p;
2038 p--;
2039 while (stop <= p && ! CHAR_HEAD_P (*p));
2041 c = STRING_CHAR (p);
2043 if (! NILP (iso_classes) && in_classes (c, iso_classes))
2045 if (negate)
2046 break;
2047 else
2048 goto back_ok;
2051 if (! fastmap[*p])
2052 break;
2053 if (! ASCII_CHAR_P (c))
2055 /* See the comment in the previous similar code. */
2056 for (i = 0; i < n_char_ranges; i += 2)
2057 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
2058 break;
2059 if (!(negate ^ (i < n_char_ranges)))
2060 break;
2062 back_ok:
2063 pos--, pos_byte -= prev_p - p;
2064 rarely_quit (pos);
2066 else
2067 while (true)
2069 if (p <= stop)
2071 if (p <= endp)
2072 break;
2073 p = GPT_ADDR;
2074 stop = endp;
2077 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
2079 if (negate)
2080 break;
2081 else
2082 goto back_unibyte_ok;
2085 if (!fastmap[p[-1]])
2086 break;
2087 back_unibyte_ok:
2088 p--, pos--, pos_byte--;
2089 rarely_quit (pos);
2093 SET_PT_BOTH (pos, pos_byte);
2095 SAFE_FREE ();
2096 return make_number (PT - start_point);
2101 static Lisp_Object
2102 skip_syntaxes (bool forwardp, Lisp_Object string, Lisp_Object lim)
2104 int c;
2105 unsigned char fastmap[0400];
2106 bool negate = 0;
2107 ptrdiff_t i, i_byte;
2108 bool multibyte;
2109 ptrdiff_t size_byte;
2110 unsigned char *str;
2112 CHECK_STRING (string);
2114 if (NILP (lim))
2115 XSETINT (lim, forwardp ? ZV : BEGV);
2116 else
2117 CHECK_NUMBER_COERCE_MARKER (lim);
2119 /* In any case, don't allow scan outside bounds of buffer. */
2120 if (XINT (lim) > ZV)
2121 XSETFASTINT (lim, ZV);
2122 if (XINT (lim) < BEGV)
2123 XSETFASTINT (lim, BEGV);
2125 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
2126 return make_number (0);
2128 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
2129 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
2131 memset (fastmap, 0, sizeof fastmap);
2133 if (SBYTES (string) > SCHARS (string))
2134 /* As this is very rare case (syntax spec is ASCII only), don't
2135 consider efficiency. */
2136 string = string_make_unibyte (string);
2138 str = SDATA (string);
2139 size_byte = SBYTES (string);
2141 i_byte = 0;
2142 if (i_byte < size_byte
2143 && SREF (string, 0) == '^')
2145 negate = 1; i_byte++;
2148 /* Find the syntaxes specified and set their elements of fastmap. */
2150 while (i_byte < size_byte)
2152 c = str[i_byte++];
2153 fastmap[syntax_spec_code[c]] = 1;
2156 /* If ^ was the first character, complement the fastmap. */
2157 if (negate)
2158 for (i = 0; i < sizeof fastmap; i++)
2159 fastmap[i] ^= 1;
2162 ptrdiff_t start_point = PT;
2163 ptrdiff_t pos = PT;
2164 ptrdiff_t pos_byte = PT_BYTE;
2165 unsigned char *p, *endp, *stop;
2167 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
2169 if (forwardp)
2171 while (true)
2173 p = BYTE_POS_ADDR (pos_byte);
2174 endp = XINT (lim) == GPT ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
2175 stop = pos < GPT && GPT < XINT (lim) ? GPT_ADDR : endp;
2179 int nbytes;
2181 if (p >= stop)
2183 if (p >= endp)
2184 goto done;
2185 p = GAP_END_ADDR;
2186 stop = endp;
2188 if (multibyte)
2189 c = STRING_CHAR_AND_LENGTH (p, nbytes);
2190 else
2191 c = *p, nbytes = 1;
2192 if (! fastmap[SYNTAX (c)])
2193 goto done;
2194 p += nbytes, pos++, pos_byte += nbytes;
2195 rarely_quit (pos);
2197 while (!parse_sexp_lookup_properties
2198 || pos < gl_state.e_property);
2200 update_syntax_table_forward (pos + gl_state.offset,
2201 false, gl_state.object);
2204 else
2206 p = BYTE_POS_ADDR (pos_byte);
2207 endp = CHAR_POS_ADDR (XINT (lim));
2208 stop = pos >= GPT && GPT > XINT (lim) ? GAP_END_ADDR : endp;
2210 if (multibyte)
2212 while (true)
2214 if (p <= stop)
2216 if (p <= endp)
2217 break;
2218 p = GPT_ADDR;
2219 stop = endp;
2221 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2223 unsigned char *prev_p = p;
2225 p--;
2226 while (stop <= p && ! CHAR_HEAD_P (*p));
2228 c = STRING_CHAR (p);
2229 if (! fastmap[SYNTAX (c)])
2230 break;
2231 pos--, pos_byte -= prev_p - p;
2232 rarely_quit (pos);
2235 else
2237 while (true)
2239 if (p <= stop)
2241 if (p <= endp)
2242 break;
2243 p = GPT_ADDR;
2244 stop = endp;
2246 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2247 if (! fastmap[SYNTAX (p[-1])])
2248 break;
2249 p--, pos--, pos_byte--;
2250 rarely_quit (pos);
2255 done:
2256 SET_PT_BOTH (pos, pos_byte);
2258 return make_number (PT - start_point);
2262 /* Return true if character C belongs to one of the ISO classes
2263 in the list ISO_CLASSES. Each class is represented by an
2264 integer which is its type according to re_wctype. */
2266 static bool
2267 in_classes (int c, Lisp_Object iso_classes)
2269 bool fits_class = 0;
2271 while (CONSP (iso_classes))
2273 Lisp_Object elt;
2274 elt = XCAR (iso_classes);
2275 iso_classes = XCDR (iso_classes);
2277 if (re_iswctype (c, XFASTINT (elt)))
2278 fits_class = 1;
2281 return fits_class;
2284 /* Jump over a comment, assuming we are at the beginning of one.
2285 FROM is the current position.
2286 FROM_BYTE is the bytepos corresponding to FROM.
2287 Do not move past STOP (a charpos).
2288 The comment over which we have to jump is of style STYLE
2289 (either SYNTAX_FLAGS_COMMENT_STYLE (foo) or ST_COMMENT_STYLE).
2290 NESTING should be positive to indicate the nesting at the beginning
2291 for nested comments and should be zero or negative else.
2292 ST_COMMENT_STYLE cannot be nested.
2293 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2294 (or 0 If the search cannot start in the middle of a two-character).
2296 If successful, return true and store the charpos of the comment's
2297 end into *CHARPOS_PTR and the corresponding bytepos into
2298 *BYTEPOS_PTR. Else, return false and store the charpos STOP into
2299 *CHARPOS_PTR, the corresponding bytepos into *BYTEPOS_PTR and the
2300 current nesting (as defined for state->incomment) in
2301 *INCOMMENT_PTR. Should the last character scanned in an incomplete
2302 comment be a possible first character of a two character construct,
2303 we store its SYNTAX_WITH_FLAGS into *last_syntax_ptr. Otherwise,
2304 we store Smax into *last_syntax_ptr.
2306 The comment end is the last character of the comment rather than the
2307 character just after the comment.
2309 Global syntax data is assumed to initially be valid for FROM and
2310 remains valid for forward search starting at the returned position. */
2312 static bool
2313 forw_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop,
2314 EMACS_INT nesting, int style, int prev_syntax,
2315 ptrdiff_t *charpos_ptr, ptrdiff_t *bytepos_ptr,
2316 EMACS_INT *incomment_ptr, int *last_syntax_ptr)
2318 unsigned short int quit_count = 0;
2319 int c, c1;
2320 enum syntaxcode code;
2321 int syntax, other_syntax;
2323 if (nesting <= 0) nesting = -1;
2325 /* Enter the loop in the middle so that we find
2326 a 2-char comment ender if we start in the middle of it. */
2327 syntax = prev_syntax;
2328 code = syntax & 0xff;
2329 if (syntax != 0 && from < stop) goto forw_incomment;
2331 while (1)
2333 if (from == stop)
2335 *incomment_ptr = nesting;
2336 *charpos_ptr = from;
2337 *bytepos_ptr = from_byte;
2338 *last_syntax_ptr =
2339 (code == Sescape || code == Scharquote
2340 || SYNTAX_FLAGS_COMEND_FIRST (syntax)
2341 || (nesting > 0
2342 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)))
2343 ? syntax : Smax ;
2344 return 0;
2346 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2347 syntax = SYNTAX_WITH_FLAGS (c);
2348 code = syntax & 0xff;
2349 if (code == Sendcomment
2350 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style
2351 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2352 (nesting > 0 && --nesting == 0) : nesting < 0)
2353 && !(Vcomment_end_can_be_escaped && char_quoted (from, from_byte)))
2354 /* We have encountered a comment end of the same style
2355 as the comment sequence which began this comment
2356 section. */
2357 break;
2358 if (code == Scomment_fence
2359 && style == ST_COMMENT_STYLE)
2360 /* We have encountered a comment end of the same style
2361 as the comment sequence which began this comment
2362 section. */
2363 break;
2364 if (nesting > 0
2365 && code == Scomment
2366 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2367 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style)
2368 /* We have encountered a nested comment of the same style
2369 as the comment sequence which began this comment section. */
2370 nesting++;
2371 INC_BOTH (from, from_byte);
2372 UPDATE_SYNTAX_TABLE_FORWARD (from);
2374 forw_incomment:
2375 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2376 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2377 other_syntax = SYNTAX_WITH_FLAGS (c1),
2378 SYNTAX_FLAGS_COMEND_SECOND (other_syntax))
2379 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, other_syntax) == style
2380 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2381 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax))
2382 ? nesting > 0 : nesting < 0))
2384 syntax = Smax; /* So that "|#" (lisp) can not return
2385 the syntax of "#" in *last_syntax_ptr. */
2386 if (--nesting <= 0)
2387 /* We have encountered a comment end of the same style
2388 as the comment sequence which began this comment section. */
2389 break;
2390 else
2392 INC_BOTH (from, from_byte);
2393 UPDATE_SYNTAX_TABLE_FORWARD (from);
2396 if (nesting > 0
2397 && from < stop
2398 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2399 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2400 other_syntax = SYNTAX_WITH_FLAGS (c1),
2401 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax) == style
2402 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2403 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2404 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax)))
2405 /* We have encountered a nested comment of the same style
2406 as the comment sequence which began this comment section. */
2408 syntax = Smax; /* So that "#|#" isn't also a comment ender. */
2409 INC_BOTH (from, from_byte);
2410 UPDATE_SYNTAX_TABLE_FORWARD (from);
2411 nesting++;
2414 rarely_quit (++quit_count);
2416 *charpos_ptr = from;
2417 *bytepos_ptr = from_byte;
2418 *last_syntax_ptr = Smax; /* Any syntactic power the last byte had is
2419 used up. */
2420 return 1;
2423 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2424 doc: /*
2425 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2426 Stop scanning if we find something other than a comment or whitespace.
2427 Set point to where scanning stops.
2428 If COUNT comments are found as expected, with nothing except whitespace
2429 between them, return t; otherwise return nil. */)
2430 (Lisp_Object count)
2432 ptrdiff_t from, from_byte, stop;
2433 int c, c1;
2434 enum syntaxcode code;
2435 int comstyle = 0; /* style of comment encountered */
2436 bool comnested = 0; /* whether the comment is nestable or not */
2437 bool found;
2438 EMACS_INT count1;
2439 ptrdiff_t out_charpos, out_bytepos;
2440 EMACS_INT dummy;
2441 int dummy2;
2442 unsigned short int quit_count = 0;
2444 CHECK_NUMBER (count);
2445 count1 = XINT (count);
2446 stop = count1 > 0 ? ZV : BEGV;
2448 from = PT;
2449 from_byte = PT_BYTE;
2451 SETUP_SYNTAX_TABLE (from, count1);
2452 while (count1 > 0)
2456 bool comstart_first;
2457 int syntax, other_syntax;
2459 if (from == stop)
2461 SET_PT_BOTH (from, from_byte);
2462 return Qnil;
2464 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2465 syntax = SYNTAX_WITH_FLAGS (c);
2466 code = SYNTAX (c);
2467 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2468 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2469 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2470 INC_BOTH (from, from_byte);
2471 UPDATE_SYNTAX_TABLE_FORWARD (from);
2472 if (from < stop && comstart_first
2473 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2474 other_syntax = SYNTAX_WITH_FLAGS (c1),
2475 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)))
2477 /* We have encountered a comment start sequence and we
2478 are ignoring all text inside comments. We must record
2479 the comment style this sequence begins so that later,
2480 only a comment end of the same style actually ends
2481 the comment section. */
2482 code = Scomment;
2483 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2484 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2485 INC_BOTH (from, from_byte);
2486 UPDATE_SYNTAX_TABLE_FORWARD (from);
2488 rarely_quit (++quit_count);
2490 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2492 if (code == Scomment_fence)
2493 comstyle = ST_COMMENT_STYLE;
2494 else if (code != Scomment)
2496 DEC_BOTH (from, from_byte);
2497 SET_PT_BOTH (from, from_byte);
2498 return Qnil;
2500 /* We're at the start of a comment. */
2501 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2502 &out_charpos, &out_bytepos, &dummy, &dummy2);
2503 from = out_charpos; from_byte = out_bytepos;
2504 if (!found)
2506 SET_PT_BOTH (from, from_byte);
2507 return Qnil;
2509 INC_BOTH (from, from_byte);
2510 UPDATE_SYNTAX_TABLE_FORWARD (from);
2511 /* We have skipped one comment. */
2512 count1--;
2515 while (count1 < 0)
2517 while (true)
2519 if (from <= stop)
2521 SET_PT_BOTH (BEGV, BEGV_BYTE);
2522 return Qnil;
2525 DEC_BOTH (from, from_byte);
2526 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2527 bool quoted = char_quoted (from, from_byte);
2528 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2529 int syntax = SYNTAX_WITH_FLAGS (c);
2530 code = SYNTAX (c);
2531 comstyle = 0;
2532 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2533 if (code == Sendcomment)
2534 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2535 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2536 && prev_char_comend_first (from, from_byte)
2537 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2539 int other_syntax;
2540 /* We must record the comment style encountered so that
2541 later, we can match only the proper comment begin
2542 sequence of the same style. */
2543 DEC_BOTH (from, from_byte);
2544 code = Sendcomment;
2545 /* Calling char_quoted, above, set up global syntax position
2546 at the new value of FROM. */
2547 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2548 other_syntax = SYNTAX_WITH_FLAGS (c1);
2549 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2550 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2553 if (code == Scomment_fence)
2555 /* Skip until first preceding unquoted comment_fence. */
2556 bool fence_found = 0;
2557 ptrdiff_t ini = from, ini_byte = from_byte;
2559 while (1)
2561 DEC_BOTH (from, from_byte);
2562 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2563 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2564 if (SYNTAX (c) == Scomment_fence
2565 && !char_quoted (from, from_byte))
2567 fence_found = 1;
2568 break;
2570 else if (from == stop)
2571 break;
2572 rarely_quit (++quit_count);
2574 if (fence_found == 0)
2576 from = ini; /* Set point to ini + 1. */
2577 from_byte = ini_byte;
2578 goto leave;
2580 else
2581 /* We have skipped one comment. */
2582 break;
2584 else if (code == Sendcomment)
2586 found = back_comment (from, from_byte, stop, comnested, comstyle,
2587 &out_charpos, &out_bytepos);
2588 if (!found)
2590 if (c == '\n')
2591 /* This end-of-line is not an end-of-comment.
2592 Treat it like a whitespace.
2593 CC-mode (and maybe others) relies on this behavior. */
2595 else
2597 /* Failure: we should go back to the end of this
2598 not-quite-endcomment. */
2599 if (SYNTAX (c) != code)
2600 /* It was a two-char Sendcomment. */
2601 INC_BOTH (from, from_byte);
2602 goto leave;
2605 else
2607 /* We have skipped one comment. */
2608 from = out_charpos, from_byte = out_bytepos;
2609 break;
2612 else if (code != Swhitespace || quoted)
2614 leave:
2615 INC_BOTH (from, from_byte);
2616 SET_PT_BOTH (from, from_byte);
2617 return Qnil;
2620 rarely_quit (++quit_count);
2623 count1++;
2626 SET_PT_BOTH (from, from_byte);
2627 return Qt;
2630 /* Return syntax code of character C if C is an ASCII character
2631 or if MULTIBYTE_SYMBOL_P is false. Otherwise, return Ssymbol. */
2633 static enum syntaxcode
2634 syntax_multibyte (int c, bool multibyte_symbol_p)
2636 return ASCII_CHAR_P (c) || !multibyte_symbol_p ? SYNTAX (c) : Ssymbol;
2639 static Lisp_Object
2640 scan_lists (EMACS_INT from, EMACS_INT count, EMACS_INT depth, bool sexpflag)
2642 Lisp_Object val;
2643 ptrdiff_t stop = count > 0 ? ZV : BEGV;
2644 int c, c1;
2645 int stringterm;
2646 bool quoted;
2647 bool mathexit = 0;
2648 enum syntaxcode code;
2649 EMACS_INT min_depth = depth; /* Err out if depth gets less than this. */
2650 int comstyle = 0; /* Style of comment encountered. */
2651 bool comnested = 0; /* Whether the comment is nestable or not. */
2652 ptrdiff_t temp_pos;
2653 EMACS_INT last_good = from;
2654 bool found;
2655 ptrdiff_t from_byte;
2656 ptrdiff_t out_bytepos, out_charpos;
2657 EMACS_INT dummy;
2658 int dummy2;
2659 bool multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2660 unsigned short int quit_count = 0;
2662 if (depth > 0) min_depth = 0;
2664 if (from > ZV) from = ZV;
2665 if (from < BEGV) from = BEGV;
2667 from_byte = CHAR_TO_BYTE (from);
2669 maybe_quit ();
2671 SETUP_SYNTAX_TABLE (from, count);
2672 while (count > 0)
2674 while (from < stop)
2676 rarely_quit (++quit_count);
2677 bool comstart_first, prefix;
2678 int syntax, other_syntax;
2679 UPDATE_SYNTAX_TABLE_FORWARD (from);
2680 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2681 syntax = SYNTAX_WITH_FLAGS (c);
2682 code = syntax_multibyte (c, multibyte_symbol_p);
2683 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2684 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2685 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2686 prefix = SYNTAX_FLAGS_PREFIX (syntax);
2687 if (depth == min_depth)
2688 last_good = from;
2689 INC_BOTH (from, from_byte);
2690 UPDATE_SYNTAX_TABLE_FORWARD (from);
2691 if (from < stop && comstart_first
2692 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2693 other_syntax = SYNTAX_WITH_FLAGS (c),
2694 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2695 && parse_sexp_ignore_comments)
2697 /* We have encountered a comment start sequence and we
2698 are ignoring all text inside comments. We must record
2699 the comment style this sequence begins so that later,
2700 only a comment end of the same style actually ends
2701 the comment section. */
2702 code = Scomment;
2703 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2704 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2705 INC_BOTH (from, from_byte);
2706 UPDATE_SYNTAX_TABLE_FORWARD (from);
2709 if (prefix)
2710 continue;
2712 switch (code)
2714 case Sescape:
2715 case Scharquote:
2716 if (from == stop)
2717 goto lose;
2718 INC_BOTH (from, from_byte);
2719 /* Treat following character as a word constituent. */
2720 FALLTHROUGH;
2721 case Sword:
2722 case Ssymbol:
2723 if (depth || !sexpflag) break;
2724 /* This word counts as a sexp; return at end of it. */
2725 while (from < stop)
2727 UPDATE_SYNTAX_TABLE_FORWARD (from);
2729 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2730 switch (syntax_multibyte (c, multibyte_symbol_p))
2732 case Scharquote:
2733 case Sescape:
2734 INC_BOTH (from, from_byte);
2735 if (from == stop)
2736 goto lose;
2737 break;
2738 case Sword:
2739 case Ssymbol:
2740 case Squote:
2741 break;
2742 default:
2743 goto done;
2745 INC_BOTH (from, from_byte);
2746 rarely_quit (++quit_count);
2748 goto done;
2750 case Scomment_fence:
2751 comstyle = ST_COMMENT_STYLE;
2752 FALLTHROUGH;
2753 case Scomment:
2754 if (!parse_sexp_ignore_comments) break;
2755 UPDATE_SYNTAX_TABLE_FORWARD (from);
2756 found = forw_comment (from, from_byte, stop,
2757 comnested, comstyle, 0,
2758 &out_charpos, &out_bytepos, &dummy,
2759 &dummy2);
2760 from = out_charpos, from_byte = out_bytepos;
2761 if (!found)
2763 if (depth == 0)
2764 goto done;
2765 goto lose;
2767 INC_BOTH (from, from_byte);
2768 UPDATE_SYNTAX_TABLE_FORWARD (from);
2769 break;
2771 case Smath:
2772 if (!sexpflag)
2773 break;
2774 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2776 INC_BOTH (from, from_byte);
2778 if (mathexit)
2780 mathexit = 0;
2781 goto close1;
2783 mathexit = 1;
2784 FALLTHROUGH;
2785 case Sopen:
2786 if (!++depth) goto done;
2787 break;
2789 case Sclose:
2790 close1:
2791 if (!--depth) goto done;
2792 if (depth < min_depth)
2793 xsignal3 (Qscan_error,
2794 build_string ("Containing expression ends prematurely"),
2795 make_number (last_good), make_number (from));
2796 break;
2798 case Sstring:
2799 case Sstring_fence:
2800 temp_pos = dec_bytepos (from_byte);
2801 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2802 while (1)
2804 enum syntaxcode c_code;
2805 if (from >= stop)
2806 goto lose;
2807 UPDATE_SYNTAX_TABLE_FORWARD (from);
2808 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2809 c_code = syntax_multibyte (c, multibyte_symbol_p);
2810 if (code == Sstring
2811 ? c == stringterm && c_code == Sstring
2812 : c_code == Sstring_fence)
2813 break;
2815 if (c_code == Scharquote || c_code == Sescape)
2816 INC_BOTH (from, from_byte);
2817 INC_BOTH (from, from_byte);
2818 rarely_quit (++quit_count);
2820 INC_BOTH (from, from_byte);
2821 if (!depth && sexpflag) goto done;
2822 break;
2823 default:
2824 /* Ignore whitespace, punctuation, quote, endcomment. */
2825 break;
2829 /* Reached end of buffer. Error if within object, return nil if between */
2830 if (depth)
2831 goto lose;
2833 return Qnil;
2835 /* End of object reached */
2836 done:
2837 count--;
2840 while (count < 0)
2842 while (from > stop)
2844 rarely_quit (++quit_count);
2845 DEC_BOTH (from, from_byte);
2846 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2847 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2848 int syntax = SYNTAX_WITH_FLAGS (c);
2849 code = syntax_multibyte (c, multibyte_symbol_p);
2850 if (depth == min_depth)
2851 last_good = from;
2852 comstyle = 0;
2853 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2854 if (code == Sendcomment)
2855 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2856 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2857 && prev_char_comend_first (from, from_byte)
2858 && parse_sexp_ignore_comments)
2860 /* We must record the comment style encountered so that
2861 later, we can match only the proper comment begin
2862 sequence of the same style. */
2863 int c2, other_syntax;
2864 DEC_BOTH (from, from_byte);
2865 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2866 code = Sendcomment;
2867 c2 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2868 other_syntax = SYNTAX_WITH_FLAGS (c2);
2869 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2870 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2873 /* Quoting turns anything except a comment-ender
2874 into a word character. Note that this cannot be true
2875 if we decremented FROM in the if-statement above. */
2876 if (code != Sendcomment && char_quoted (from, from_byte))
2878 DEC_BOTH (from, from_byte);
2879 code = Sword;
2881 else if (SYNTAX_FLAGS_PREFIX (syntax))
2882 continue;
2884 switch (code)
2886 case Sword:
2887 case Ssymbol:
2888 case Sescape:
2889 case Scharquote:
2890 if (depth || !sexpflag) break;
2891 /* This word counts as a sexp; count object finished
2892 after passing it. */
2893 while (from > stop)
2895 temp_pos = from_byte;
2896 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2897 DEC_POS (temp_pos);
2898 else
2899 temp_pos--;
2900 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2901 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2902 /* Don't allow comment-end to be quoted. */
2903 if (syntax_multibyte (c1, multibyte_symbol_p) == Sendcomment)
2904 goto done2;
2905 quoted = char_quoted (from - 1, temp_pos);
2906 if (quoted)
2908 DEC_BOTH (from, from_byte);
2909 temp_pos = dec_bytepos (temp_pos);
2910 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2912 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2913 if (! quoted)
2914 switch (syntax_multibyte (c1, multibyte_symbol_p))
2916 case Sword: case Ssymbol: case Squote: break;
2917 default: goto done2;
2919 DEC_BOTH (from, from_byte);
2920 rarely_quit (++quit_count);
2922 goto done2;
2924 case Smath:
2925 if (!sexpflag)
2926 break;
2927 if (from > BEGV)
2929 temp_pos = dec_bytepos (from_byte);
2930 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2931 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2932 DEC_BOTH (from, from_byte);
2934 if (mathexit)
2936 mathexit = 0;
2937 goto open2;
2939 mathexit = 1;
2940 FALLTHROUGH;
2941 case Sclose:
2942 if (!++depth) goto done2;
2943 break;
2945 case Sopen:
2946 open2:
2947 if (!--depth) goto done2;
2948 if (depth < min_depth)
2949 xsignal3 (Qscan_error,
2950 build_string ("Containing expression ends prematurely"),
2951 make_number (last_good), make_number (from));
2952 break;
2954 case Sendcomment:
2955 if (!parse_sexp_ignore_comments)
2956 break;
2957 found = back_comment (from, from_byte, stop, comnested, comstyle,
2958 &out_charpos, &out_bytepos);
2959 /* FIXME: if !found, it really wasn't a comment-end.
2960 For single-char Sendcomment, we can't do much about it apart
2961 from skipping the char.
2962 For 2-char endcomments, we could try again, taking both
2963 chars as separate entities, but it's a lot of trouble
2964 for very little gain, so we don't bother either. -sm */
2965 if (found)
2966 from = out_charpos, from_byte = out_bytepos;
2967 break;
2969 case Scomment_fence:
2970 case Sstring_fence:
2971 while (1)
2973 if (from == stop)
2974 goto lose;
2975 DEC_BOTH (from, from_byte);
2976 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2977 if (!char_quoted (from, from_byte))
2979 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2980 if (syntax_multibyte (c, multibyte_symbol_p) == code)
2981 break;
2983 rarely_quit (++quit_count);
2985 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2986 break;
2988 case Sstring:
2989 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2990 while (true)
2992 if (from == stop)
2993 goto lose;
2994 DEC_BOTH (from, from_byte);
2995 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2996 if (!char_quoted (from, from_byte))
2998 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2999 if (c == stringterm
3000 && (syntax_multibyte (c, multibyte_symbol_p)
3001 == Sstring))
3002 break;
3004 rarely_quit (++quit_count);
3006 if (!depth && sexpflag) goto done2;
3007 break;
3008 default:
3009 /* Ignore whitespace, punctuation, quote, endcomment. */
3010 break;
3014 /* Reached start of buffer. Error if within object, return nil if between */
3015 if (depth)
3016 goto lose;
3018 return Qnil;
3020 done2:
3021 count++;
3025 XSETFASTINT (val, from);
3026 return val;
3028 lose:
3029 xsignal3 (Qscan_error,
3030 build_string ("Unbalanced parentheses"),
3031 make_number (last_good), make_number (from));
3034 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
3035 doc: /* Scan from character number FROM by COUNT lists.
3036 Scan forward if COUNT is positive, backward if COUNT is negative.
3037 Return the character number of the position thus found.
3039 A \"list", in this context, refers to a balanced parenthetical
3040 grouping, as determined by the syntax table.
3042 If DEPTH is nonzero, treat that as the nesting depth of the starting
3043 point (i.e. the starting point is DEPTH parentheses deep). This
3044 function scans over parentheses until the depth goes to zero COUNT
3045 times. Hence, positive DEPTH moves out that number of levels of
3046 parentheses, while negative DEPTH moves to a deeper level.
3048 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3050 If we reach the beginning or end of the accessible part of the buffer
3051 before we have scanned over COUNT lists, return nil if the depth at
3052 that point is zero, and signal a error if the depth is nonzero. */)
3053 (Lisp_Object from, Lisp_Object count, Lisp_Object depth)
3055 CHECK_NUMBER (from);
3056 CHECK_NUMBER (count);
3057 CHECK_NUMBER (depth);
3059 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
3062 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
3063 doc: /* Scan from character number FROM by COUNT balanced expressions.
3064 If COUNT is negative, scan backwards.
3065 Returns the character number of the position thus found.
3067 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3069 If the beginning or end of (the accessible part of) the buffer is reached
3070 in the middle of a parenthetical grouping, an error is signaled.
3071 If the beginning or end is reached between groupings
3072 but before count is used up, nil is returned. */)
3073 (Lisp_Object from, Lisp_Object count)
3075 CHECK_NUMBER (from);
3076 CHECK_NUMBER (count);
3078 return scan_lists (XINT (from), XINT (count), 0, 1);
3081 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
3082 0, 0, 0,
3083 doc: /* Move point backward over any number of chars with prefix syntax.
3084 This includes chars with expression prefix syntax class (\\=') and those with
3085 the prefix syntax flag (p). */)
3086 (void)
3088 ptrdiff_t beg = BEGV;
3089 ptrdiff_t opoint = PT;
3090 ptrdiff_t opoint_byte = PT_BYTE;
3091 ptrdiff_t pos = PT;
3092 ptrdiff_t pos_byte = PT_BYTE;
3093 int c;
3095 if (pos <= beg)
3097 SET_PT_BOTH (opoint, opoint_byte);
3099 return Qnil;
3102 SETUP_SYNTAX_TABLE (pos, -1);
3104 DEC_BOTH (pos, pos_byte);
3106 while (!char_quoted (pos, pos_byte)
3107 /* Previous statement updates syntax table. */
3108 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
3109 || syntax_prefix_flag_p (c)))
3111 opoint = pos;
3112 opoint_byte = pos_byte;
3114 if (pos <= beg)
3115 break;
3116 DEC_BOTH (pos, pos_byte);
3117 rarely_quit (pos);
3120 SET_PT_BOTH (opoint, opoint_byte);
3122 return Qnil;
3126 /* If the character at FROM_BYTE is the second part of a 2-character
3127 comment opener based on PREV_FROM_SYNTAX, update STATE and return
3128 true. */
3129 static bool
3130 in_2char_comment_start (struct lisp_parse_state *state,
3131 int prev_from_syntax,
3132 ptrdiff_t prev_from,
3133 ptrdiff_t from_byte)
3135 int c1, syntax;
3136 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3137 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
3138 syntax = SYNTAX_WITH_FLAGS (c1),
3139 SYNTAX_FLAGS_COMSTART_SECOND (syntax)))
3141 /* Record the comment style we have entered so that only
3142 the comment-end sequence of the same style actually
3143 terminates the comment section. */
3144 state->comstyle
3145 = SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_from_syntax);
3146 bool comnested = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax)
3147 | SYNTAX_FLAGS_COMMENT_NESTED (syntax));
3148 state->incomment = comnested ? 1 : -1;
3149 state->comstr_start = prev_from;
3150 return true;
3152 return false;
3155 /* Parse forward from FROM / FROM_BYTE to END,
3156 assuming that FROM has state STATE,
3157 and return a description of the state of the parse at END.
3158 If STOPBEFORE, stop at the start of an atom.
3159 If COMMENTSTOP is 1, stop at the start of a comment.
3160 If COMMENTSTOP is -1, stop at the start or end of a comment,
3161 after the beginning of a string, or after the end of a string. */
3163 static void
3164 scan_sexps_forward (struct lisp_parse_state *state,
3165 ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t end,
3166 EMACS_INT targetdepth, bool stopbefore,
3167 int commentstop)
3169 enum syntaxcode code;
3170 struct level { ptrdiff_t last, prev; };
3171 struct level levelstart[100];
3172 struct level *curlevel = levelstart;
3173 struct level *endlevel = levelstart + 100;
3174 EMACS_INT depth; /* Paren depth of current scanning location.
3175 level - levelstart equals this except
3176 when the depth becomes negative. */
3177 EMACS_INT mindepth; /* Lowest DEPTH value seen. */
3178 bool start_quoted = 0; /* True means starting after a char quote. */
3179 Lisp_Object tem;
3180 ptrdiff_t prev_from; /* Keep one character before FROM. */
3181 ptrdiff_t prev_from_byte;
3182 int prev_from_syntax, prev_prev_from_syntax;
3183 bool boundary_stop = commentstop == -1;
3184 bool nofence;
3185 bool found;
3186 ptrdiff_t out_bytepos, out_charpos;
3187 int temp;
3188 unsigned short int quit_count = 0;
3190 prev_from = from;
3191 prev_from_byte = from_byte;
3192 if (from != BEGV)
3193 DEC_BOTH (prev_from, prev_from_byte);
3195 /* Use this macro instead of `from++'. */
3196 #define INC_FROM \
3197 do { prev_from = from; \
3198 prev_from_byte = from_byte; \
3199 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
3200 prev_prev_from_syntax = prev_from_syntax; \
3201 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
3202 INC_BOTH (from, from_byte); \
3203 if (from < end) \
3204 UPDATE_SYNTAX_TABLE_FORWARD (from); \
3205 } while (0)
3207 maybe_quit ();
3209 depth = state->depth;
3210 start_quoted = state->quoted;
3211 prev_prev_from_syntax = Smax;
3212 prev_from_syntax = state->prev_syntax;
3214 tem = state->levelstarts;
3215 while (!NILP (tem)) /* >= second enclosing sexps. */
3217 Lisp_Object temhd = Fcar (tem);
3218 if (RANGED_INTEGERP (PTRDIFF_MIN, temhd, PTRDIFF_MAX))
3219 curlevel->last = XINT (temhd);
3220 if (++curlevel == endlevel)
3221 curlevel--; /* error ("Nesting too deep for parser"); */
3222 curlevel->prev = -1;
3223 curlevel->last = -1;
3224 tem = Fcdr (tem);
3226 curlevel->prev = -1;
3227 curlevel->last = -1;
3229 state->quoted = 0;
3230 mindepth = depth;
3232 SETUP_SYNTAX_TABLE (from, 1);
3234 /* Enter the loop at a place appropriate for initial state. */
3236 if (state->incomment)
3237 goto startincomment;
3238 if (state->instring >= 0)
3240 nofence = state->instring != ST_STRING_STYLE;
3241 if (start_quoted)
3242 goto startquotedinstring;
3243 goto startinstring;
3245 else if (start_quoted)
3246 goto startquoted;
3247 else if ((from < end)
3248 && (in_2char_comment_start (state, prev_from_syntax,
3249 prev_from, from_byte)))
3251 INC_FROM;
3252 prev_from_syntax = Smax; /* the syntax has already been "used up". */
3253 goto atcomment;
3256 while (from < end)
3258 rarely_quit (++quit_count);
3259 INC_FROM;
3261 if ((from < end)
3262 && (in_2char_comment_start (state, prev_from_syntax,
3263 prev_from, from_byte)))
3265 INC_FROM;
3266 prev_from_syntax = Smax; /* the syntax has already been "used up". */
3267 goto atcomment;
3270 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3271 continue;
3272 code = prev_from_syntax & 0xff;
3273 switch (code)
3275 case Sescape:
3276 case Scharquote:
3277 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3278 curlevel->last = prev_from;
3279 startquoted:
3280 if (from == end) goto endquoted;
3281 INC_FROM;
3282 goto symstarted;
3283 /* treat following character as a word constituent */
3284 case Sword:
3285 case Ssymbol:
3286 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3287 curlevel->last = prev_from;
3288 symstarted:
3289 while (from < end)
3291 if (in_2char_comment_start (state, prev_from_syntax,
3292 prev_from, from_byte))
3294 INC_FROM;
3295 prev_from_syntax = Smax; /* the syntax has already been "used up". */
3296 goto atcomment;
3299 int symchar = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3300 switch (SYNTAX (symchar))
3302 case Scharquote:
3303 case Sescape:
3304 INC_FROM;
3305 if (from == end) goto endquoted;
3306 break;
3307 case Sword:
3308 case Ssymbol:
3309 case Squote:
3310 break;
3311 default:
3312 goto symdone;
3314 INC_FROM;
3315 rarely_quit (++quit_count);
3317 symdone:
3318 curlevel->prev = curlevel->last;
3319 break;
3321 case Scomment_fence:
3322 /* Record the comment style we have entered so that only
3323 the comment-end sequence of the same style actually
3324 terminates the comment section. */
3325 state->comstyle = ST_COMMENT_STYLE;
3326 state->incomment = -1;
3327 state->comstr_start = prev_from;
3328 goto atcomment;
3329 case Scomment:
3330 state->comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax, 0);
3331 state->incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3332 1 : -1);
3333 state->comstr_start = prev_from;
3334 atcomment:
3335 if (commentstop || boundary_stop) goto done;
3336 startincomment:
3337 /* The (from == BEGV) test was to enter the loop in the middle so
3338 that we find a 2-char comment ender even if we start in the
3339 middle of it. We don't want to do that if we're just at the
3340 beginning of the comment (think of (*) ... (*)). */
3341 found = forw_comment (from, from_byte, end,
3342 state->incomment, state->comstyle,
3343 from == BEGV ? 0 : prev_from_syntax,
3344 &out_charpos, &out_bytepos, &state->incomment,
3345 &prev_from_syntax);
3346 from = out_charpos; from_byte = out_bytepos;
3347 /* Beware! prev_from and friends (except prev_from_syntax)
3348 are invalid now. Luckily, the `done' doesn't use them
3349 and the INC_FROM sets them to a sane value without
3350 looking at them. */
3351 if (!found) goto done;
3352 INC_FROM;
3353 state->incomment = 0;
3354 state->comstyle = 0; /* reset the comment style */
3355 prev_from_syntax = Smax; /* For the comment closer */
3356 if (boundary_stop) goto done;
3357 break;
3359 case Sopen:
3360 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3361 depth++;
3362 /* curlevel++->last ran into compiler bug on Apollo */
3363 curlevel->last = prev_from;
3364 if (++curlevel == endlevel)
3365 curlevel--; /* error ("Nesting too deep for parser"); */
3366 curlevel->prev = -1;
3367 curlevel->last = -1;
3368 if (targetdepth == depth) goto done;
3369 break;
3371 case Sclose:
3372 depth--;
3373 if (depth < mindepth)
3374 mindepth = depth;
3375 if (curlevel != levelstart)
3376 curlevel--;
3377 curlevel->prev = curlevel->last;
3378 if (targetdepth == depth) goto done;
3379 break;
3381 case Sstring:
3382 case Sstring_fence:
3383 state->comstr_start = from - 1;
3384 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3385 curlevel->last = prev_from;
3386 state->instring = (code == Sstring
3387 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3388 : ST_STRING_STYLE);
3389 if (boundary_stop) goto done;
3390 startinstring:
3392 nofence = state->instring != ST_STRING_STYLE;
3394 while (1)
3396 int c;
3397 enum syntaxcode c_code;
3399 if (from >= end) goto done;
3400 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3401 c_code = SYNTAX (c);
3403 /* Check C_CODE here so that if the char has
3404 a syntax-table property which says it is NOT
3405 a string character, it does not end the string. */
3406 if (nofence && c == state->instring && c_code == Sstring)
3407 break;
3409 switch (c_code)
3411 case Sstring_fence:
3412 if (!nofence) goto string_end;
3413 break;
3415 case Scharquote:
3416 case Sescape:
3417 INC_FROM;
3418 startquotedinstring:
3419 if (from >= end) goto endquoted;
3420 break;
3422 default:
3423 break;
3425 INC_FROM;
3426 rarely_quit (++quit_count);
3429 string_end:
3430 state->instring = -1;
3431 curlevel->prev = curlevel->last;
3432 INC_FROM;
3433 if (boundary_stop) goto done;
3434 break;
3436 case Smath:
3437 /* FIXME: We should do something with it. */
3438 break;
3439 default:
3440 /* Ignore whitespace, punctuation, quote, endcomment. */
3441 break;
3444 goto done;
3446 stop: /* Here if stopping before start of sexp. */
3447 from = prev_from; /* We have just fetched the char that starts it; */
3448 from_byte = prev_from_byte;
3449 prev_from_syntax = prev_prev_from_syntax;
3450 goto done; /* but return the position before it. */
3452 endquoted:
3453 state->quoted = 1;
3454 done:
3455 state->depth = depth;
3456 state->mindepth = mindepth;
3457 state->thislevelstart = curlevel->prev;
3458 state->prevlevelstart
3459 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3460 state->location = from;
3461 state->location_byte = from_byte;
3462 state->levelstarts = Qnil;
3463 while (curlevel > levelstart)
3464 state->levelstarts = Fcons (make_number ((--curlevel)->last),
3465 state->levelstarts);
3466 state->prev_syntax = (SYNTAX_FLAGS_COMSTARTEND_FIRST (prev_from_syntax)
3467 || state->quoted) ? prev_from_syntax : Smax;
3470 /* Convert a (lisp) parse state to the internal form used in
3471 scan_sexps_forward. */
3472 static void
3473 internalize_parse_state (Lisp_Object external, struct lisp_parse_state *state)
3475 Lisp_Object tem;
3477 if (NILP (external))
3479 state->depth = 0;
3480 state->instring = -1;
3481 state->incomment = 0;
3482 state->quoted = 0;
3483 state->comstyle = 0; /* comment style a by default. */
3484 state->comstr_start = -1; /* no comment/string seen. */
3485 state->levelstarts = Qnil;
3486 state->prev_syntax = Smax;
3488 else
3490 tem = Fcar (external);
3491 if (!NILP (tem))
3492 state->depth = XINT (tem);
3493 else
3494 state->depth = 0;
3496 external = Fcdr (external);
3497 external = Fcdr (external);
3498 external = Fcdr (external);
3499 tem = Fcar (external);
3500 /* Check whether we are inside string_fence-style string: */
3501 state->instring = (!NILP (tem)
3502 ? (CHARACTERP (tem) ? XFASTINT (tem) : ST_STRING_STYLE)
3503 : -1);
3505 external = Fcdr (external);
3506 tem = Fcar (external);
3507 state->incomment = (!NILP (tem)
3508 ? (INTEGERP (tem) ? XINT (tem) : -1)
3509 : 0);
3511 external = Fcdr (external);
3512 tem = Fcar (external);
3513 state->quoted = !NILP (tem);
3515 /* if the eighth element of the list is nil, we are in comment
3516 style a. If it is non-nil, we are in comment style b */
3517 external = Fcdr (external);
3518 external = Fcdr (external);
3519 tem = Fcar (external);
3520 state->comstyle = (NILP (tem)
3522 : (RANGED_INTEGERP (0, tem, ST_COMMENT_STYLE)
3523 ? XINT (tem)
3524 : ST_COMMENT_STYLE));
3526 external = Fcdr (external);
3527 tem = Fcar (external);
3528 state->comstr_start =
3529 RANGED_INTEGERP (PTRDIFF_MIN, tem, PTRDIFF_MAX) ? XINT (tem) : -1;
3530 external = Fcdr (external);
3531 tem = Fcar (external);
3532 state->levelstarts = tem;
3534 external = Fcdr (external);
3535 tem = Fcar (external);
3536 state->prev_syntax = NILP (tem) ? Smax : XINT (tem);
3540 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3541 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3542 Parsing stops at TO or when certain criteria are met;
3543 point is set to where parsing stops.
3544 If fifth arg OLDSTATE is omitted or nil,
3545 parsing assumes that FROM is the beginning of a function.
3547 Value is a list of elements describing final state of parsing:
3548 0. depth in parens.
3549 1. character address of start of innermost containing list; nil if none.
3550 2. character address of start of last complete sexp terminated.
3551 3. non-nil if inside a string.
3552 (it is the character that will terminate the string,
3553 or t if the string should be terminated by a generic string delimiter.)
3554 4. nil if outside a comment, t if inside a non-nestable comment,
3555 else an integer (the current comment nesting).
3556 5. t if following a quote character.
3557 6. the minimum paren-depth encountered during this scan.
3558 7. style of comment, if any.
3559 8. character address of start of comment or string; nil if not in one.
3560 9. List of positions of currently open parens, outermost first.
3561 10. When the last position scanned holds the first character of a
3562 (potential) two character construct, the syntax of that position,
3563 otherwise nil. That construct can be a two character comment
3564 delimiter or an Escaped or Char-quoted character.
3565 11..... Possible further internal information used by `parse-partial-sexp'.
3567 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3568 in parentheses becomes equal to TARGETDEPTH.
3569 Fourth arg STOPBEFORE non-nil means stop when we come to
3570 any character that starts a sexp.
3571 Fifth arg OLDSTATE is a list like what this function returns.
3572 It is used to initialize the state of the parse. Elements number 1, 2, 6
3573 are ignored.
3574 Sixth arg COMMENTSTOP non-nil means stop after the start of a comment.
3575 If it is the symbol `syntax-table', stop after the start of a comment or a
3576 string, or after end of a comment or a string. */)
3577 (Lisp_Object from, Lisp_Object to, Lisp_Object targetdepth,
3578 Lisp_Object stopbefore, Lisp_Object oldstate, Lisp_Object commentstop)
3580 struct lisp_parse_state state;
3581 EMACS_INT target;
3583 if (!NILP (targetdepth))
3585 CHECK_NUMBER (targetdepth);
3586 target = XINT (targetdepth);
3588 else
3589 target = TYPE_MINIMUM (EMACS_INT); /* We won't reach this depth. */
3591 validate_region (&from, &to);
3592 internalize_parse_state (oldstate, &state);
3593 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3594 XINT (to),
3595 target, !NILP (stopbefore),
3596 (NILP (commentstop)
3597 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3599 SET_PT_BOTH (state.location, state.location_byte);
3601 return
3602 Fcons (make_number (state.depth),
3603 Fcons (state.prevlevelstart < 0
3604 ? Qnil : make_number (state.prevlevelstart),
3605 Fcons (state.thislevelstart < 0
3606 ? Qnil : make_number (state.thislevelstart),
3607 Fcons (state.instring >= 0
3608 ? (state.instring == ST_STRING_STYLE
3609 ? Qt : make_number (state.instring)) : Qnil,
3610 Fcons (state.incomment < 0 ? Qt :
3611 (state.incomment == 0 ? Qnil :
3612 make_number (state.incomment)),
3613 Fcons (state.quoted ? Qt : Qnil,
3614 Fcons (make_number (state.mindepth),
3615 Fcons ((state.comstyle
3616 ? (state.comstyle == ST_COMMENT_STYLE
3617 ? Qsyntax_table
3618 : make_number (state.comstyle))
3619 : Qnil),
3620 Fcons (((state.incomment
3621 || (state.instring >= 0))
3622 ? make_number (state.comstr_start)
3623 : Qnil),
3624 Fcons (state.levelstarts,
3625 Fcons (state.prev_syntax == Smax
3626 ? Qnil
3627 : make_number (state.prev_syntax),
3628 Qnil)))))))))));
3631 void
3632 init_syntax_once (void)
3634 register int i, c;
3635 Lisp_Object temp;
3637 /* This has to be done here, before we call Fmake_char_table. */
3638 DEFSYM (Qsyntax_table, "syntax-table");
3640 /* Create objects which can be shared among syntax tables. */
3641 Vsyntax_code_object = make_uninit_vector (Smax);
3642 for (i = 0; i < Smax; i++)
3643 ASET (Vsyntax_code_object, i, Fcons (make_number (i), Qnil));
3645 /* Now we are ready to set up this property, so we can
3646 create syntax tables. */
3647 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3649 temp = AREF (Vsyntax_code_object, Swhitespace);
3651 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3653 /* Control characters should not be whitespace. */
3654 temp = AREF (Vsyntax_code_object, Spunct);
3655 for (i = 0; i <= ' ' - 1; i++)
3656 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3657 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3659 /* Except that a few really are whitespace. */
3660 temp = AREF (Vsyntax_code_object, Swhitespace);
3661 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3662 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3663 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3664 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3665 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3667 temp = AREF (Vsyntax_code_object, Sword);
3668 for (i = 'a'; i <= 'z'; i++)
3669 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3670 for (i = 'A'; i <= 'Z'; i++)
3671 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3672 for (i = '0'; i <= '9'; i++)
3673 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3675 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3676 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3678 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3679 Fcons (make_number (Sopen), make_number (')')));
3680 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3681 Fcons (make_number (Sclose), make_number ('(')));
3682 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3683 Fcons (make_number (Sopen), make_number (']')));
3684 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3685 Fcons (make_number (Sclose), make_number ('[')));
3686 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3687 Fcons (make_number (Sopen), make_number ('}')));
3688 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3689 Fcons (make_number (Sclose), make_number ('{')));
3690 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3691 Fcons (make_number (Sstring), Qnil));
3692 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3693 Fcons (make_number (Sescape), Qnil));
3695 temp = AREF (Vsyntax_code_object, Ssymbol);
3696 for (i = 0; i < 10; i++)
3698 c = "_-+*/&|<>="[i];
3699 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3702 temp = AREF (Vsyntax_code_object, Spunct);
3703 for (i = 0; i < 12; i++)
3705 c = ".,;:?!#@~^'`"[i];
3706 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3709 /* All multibyte characters have syntax `word' by default. */
3710 temp = AREF (Vsyntax_code_object, Sword);
3711 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3714 void
3715 syms_of_syntax (void)
3717 DEFSYM (Qsyntax_table_p, "syntax-table-p");
3718 DEFSYM (Qsyntax_ppss, "syntax-ppss");
3719 DEFVAR_LISP ("comment-use-syntax-ppss",
3720 Vcomment_use_syntax_ppss,
3721 doc: /* Non-nil means `forward-comment' can use `syntax-ppss' internally. */);
3722 Vcomment_use_syntax_ppss = Qt;
3724 staticpro (&Vsyntax_code_object);
3726 staticpro (&gl_state.object);
3727 staticpro (&gl_state.global_code);
3728 staticpro (&gl_state.current_syntax_table);
3729 staticpro (&gl_state.old_prop);
3731 /* Defined in regex.c. */
3732 staticpro (&re_match_object);
3734 DEFSYM (Qscan_error, "scan-error");
3735 Fput (Qscan_error, Qerror_conditions,
3736 listn (CONSTYPE_PURE, 2, Qscan_error, Qerror));
3737 Fput (Qscan_error, Qerror_message,
3738 build_pure_c_string ("Scan error"));
3740 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments,
3741 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3743 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties,
3744 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3745 Otherwise, that text property is simply ignored.
3746 See the info node `(elisp)Syntax Properties' for a description of the
3747 `syntax-table' property. */);
3749 DEFVAR_INT ("syntax-propertize--done", syntax_propertize__done,
3750 doc: /* Position up to which syntax-table properties have been set. */);
3751 syntax_propertize__done = -1;
3752 DEFSYM (Qinternal__syntax_propertize, "internal--syntax-propertize");
3753 Fmake_variable_buffer_local (intern ("syntax-propertize--done"));
3755 words_include_escapes = 0;
3756 DEFVAR_BOOL ("words-include-escapes", words_include_escapes,
3757 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3759 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol,
3760 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3761 multibyte_syntax_as_symbol = 0;
3763 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3764 open_paren_in_column_0_is_defun_start,
3765 doc: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3766 open_paren_in_column_0_is_defun_start = 1;
3769 DEFVAR_LISP ("find-word-boundary-function-table",
3770 Vfind_word_boundary_function_table,
3771 doc: /*
3772 Char table of functions to search for the word boundary.
3773 Each function is called with two arguments; POS and LIMIT.
3774 POS and LIMIT are character positions in the current buffer.
3776 If POS is less than LIMIT, POS is at the first character of a word,
3777 and the return value of a function should be a position after the
3778 last character of that word.
3780 If POS is not less than LIMIT, POS is at the last character of a word,
3781 and the return value of a function should be a position at the first
3782 character of that word.
3784 In both cases, LIMIT bounds the search. */);
3785 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3787 DEFVAR_BOOL ("comment-end-can-be-escaped", Vcomment_end_can_be_escaped,
3788 doc: /* Non-nil means an escaped ender inside a comment doesn't end the comment. */);
3789 Vcomment_end_can_be_escaped = 0;
3790 DEFSYM (Qcomment_end_can_be_escaped, "comment-end-can-be-escaped");
3791 Fmake_variable_buffer_local (Qcomment_end_can_be_escaped);
3793 defsubr (&Ssyntax_table_p);
3794 defsubr (&Ssyntax_table);
3795 defsubr (&Sstandard_syntax_table);
3796 defsubr (&Scopy_syntax_table);
3797 defsubr (&Sset_syntax_table);
3798 defsubr (&Schar_syntax);
3799 defsubr (&Smatching_paren);
3800 defsubr (&Sstring_to_syntax);
3801 defsubr (&Smodify_syntax_entry);
3802 defsubr (&Sinternal_describe_syntax_value);
3804 defsubr (&Sforward_word);
3806 defsubr (&Sskip_chars_forward);
3807 defsubr (&Sskip_chars_backward);
3808 defsubr (&Sskip_syntax_forward);
3809 defsubr (&Sskip_syntax_backward);
3811 defsubr (&Sforward_comment);
3812 defsubr (&Sscan_lists);
3813 defsubr (&Sscan_sexps);
3814 defsubr (&Sbackward_prefix_chars);
3815 defsubr (&Sparse_partial_sexp);