Add an undo command to url-cookie-mode
[emacs.git] / src / syntax.c
blobc5a4b03955b305eb91a35fb16b3a76b0844830a6
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 and character script (according to `char-script-table'), but
1577 `find-word-boundary-function-table', such as set up by `subword-mode',
1578 can change that. If a Lisp program needs to move by words determined
1579 strictly by the syntax table, it should use `forward-word-strictly'
1580 instead. See Info node `(elisp) Word Motion' for details. */)
1581 (Lisp_Object arg)
1583 Lisp_Object tmp;
1584 ptrdiff_t orig_val, val;
1586 if (NILP (arg))
1587 XSETFASTINT (arg, 1);
1588 else
1589 CHECK_NUMBER (arg);
1591 val = orig_val = scan_words (PT, XINT (arg));
1592 if (! orig_val)
1593 val = XINT (arg) > 0 ? ZV : BEGV;
1595 /* Avoid jumping out of an input field. */
1596 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1597 Qnil, Qnil, Qnil);
1598 val = XFASTINT (tmp);
1600 SET_PT (val);
1601 return val == orig_val ? Qt : Qnil;
1604 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1605 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1606 STRING is like the inside of a `[...]' in a regular expression
1607 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1608 (but not at the end of a range; quoting is never needed there).
1609 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1610 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1611 Char classes, e.g. `[:alpha:]', are supported.
1613 Returns the distance traveled, either zero or positive. */)
1614 (Lisp_Object string, Lisp_Object lim)
1616 return skip_chars (1, string, lim, 1);
1619 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1620 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1621 See `skip-chars-forward' for details.
1622 Returns the distance traveled, either zero or negative. */)
1623 (Lisp_Object string, Lisp_Object lim)
1625 return skip_chars (0, string, lim, 1);
1628 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1629 doc: /* Move point forward across chars in specified syntax classes.
1630 SYNTAX is a string of syntax code characters.
1631 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1632 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1633 This function returns the distance traveled, either zero or positive. */)
1634 (Lisp_Object syntax, Lisp_Object lim)
1636 return skip_syntaxes (1, syntax, lim);
1639 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1640 doc: /* Move point backward across chars in specified syntax classes.
1641 SYNTAX is a string of syntax code characters.
1642 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1643 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1644 This function returns either zero or a negative number, and the absolute value
1645 of this is the distance traveled. */)
1646 (Lisp_Object syntax, Lisp_Object lim)
1648 return skip_syntaxes (0, syntax, lim);
1651 static Lisp_Object
1652 skip_chars (bool forwardp, Lisp_Object string, Lisp_Object lim,
1653 bool handle_iso_classes)
1655 int c;
1656 char fastmap[0400];
1657 /* Store the ranges of non-ASCII characters. */
1658 int *char_ranges UNINIT;
1659 int n_char_ranges = 0;
1660 bool negate = 0;
1661 ptrdiff_t i, i_byte;
1662 /* True if the current buffer is multibyte and the region contains
1663 non-ASCII chars. */
1664 bool multibyte;
1665 /* True if STRING is multibyte and it contains non-ASCII chars. */
1666 bool string_multibyte;
1667 ptrdiff_t size_byte;
1668 const unsigned char *str;
1669 int len;
1670 Lisp_Object iso_classes;
1671 USE_SAFE_ALLOCA;
1673 CHECK_STRING (string);
1674 iso_classes = Qnil;
1676 if (NILP (lim))
1677 XSETINT (lim, forwardp ? ZV : BEGV);
1678 else
1679 CHECK_NUMBER_COERCE_MARKER (lim);
1681 /* In any case, don't allow scan outside bounds of buffer. */
1682 if (XINT (lim) > ZV)
1683 XSETFASTINT (lim, ZV);
1684 if (XINT (lim) < BEGV)
1685 XSETFASTINT (lim, BEGV);
1687 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1688 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1689 string_multibyte = SBYTES (string) > SCHARS (string);
1691 memset (fastmap, 0, sizeof fastmap);
1693 str = SDATA (string);
1694 size_byte = SBYTES (string);
1696 i_byte = 0;
1697 if (i_byte < size_byte
1698 && SREF (string, 0) == '^')
1700 negate = 1; i_byte++;
1703 /* Find the characters specified and set their elements of fastmap.
1704 Handle backslashes and ranges specially.
1706 If STRING contains non-ASCII characters, setup char_ranges for
1707 them and use fastmap only for their leading codes. */
1709 if (! string_multibyte)
1711 bool string_has_eight_bit = 0;
1713 /* At first setup fastmap. */
1714 while (i_byte < size_byte)
1716 if (handle_iso_classes)
1718 const unsigned char *ch = str + i_byte;
1719 re_wctype_t cc = re_wctype_parse (&ch, size_byte - i_byte);
1720 if (cc == 0)
1721 error ("Invalid ISO C character class");
1722 if (cc != -1)
1724 iso_classes = Fcons (make_number (cc), iso_classes);
1725 i_byte = ch - str;
1726 continue;
1730 c = str[i_byte++];
1732 if (c == '\\')
1734 if (i_byte == size_byte)
1735 break;
1737 c = str[i_byte++];
1739 /* Treat `-' as range character only if another character
1740 follows. */
1741 if (i_byte + 1 < size_byte
1742 && str[i_byte] == '-')
1744 int c2;
1746 /* Skip over the dash. */
1747 i_byte++;
1749 /* Get the end of the range. */
1750 c2 = str[i_byte++];
1751 if (c2 == '\\'
1752 && i_byte < size_byte)
1753 c2 = str[i_byte++];
1755 if (c <= c2)
1757 int lim2 = c2 + 1;
1758 while (c < lim2)
1759 fastmap[c++] = 1;
1760 if (! ASCII_CHAR_P (c2))
1761 string_has_eight_bit = 1;
1764 else
1766 fastmap[c] = 1;
1767 if (! ASCII_CHAR_P (c))
1768 string_has_eight_bit = 1;
1772 /* If the current range is multibyte and STRING contains
1773 eight-bit chars, arrange fastmap and setup char_ranges for
1774 the corresponding multibyte chars. */
1775 if (multibyte && string_has_eight_bit)
1777 char *p1;
1778 char himap[0200 + 1];
1779 memcpy (himap, fastmap + 0200, 0200);
1780 himap[0200] = 0;
1781 memset (fastmap + 0200, 0, 0200);
1782 SAFE_NALLOCA (char_ranges, 2, 128);
1783 i = 0;
1785 while ((p1 = memchr (himap + i, 1, 0200 - i)))
1787 /* Deduce the next range C..C2 from the next clump of 1s
1788 in HIMAP starting with &HIMAP[I]. HIMAP is the high
1789 order half of the old FASTMAP. */
1790 int c2, leading_code;
1791 i = p1 - himap;
1792 c = BYTE8_TO_CHAR (i + 0200);
1793 i += strlen (p1);
1794 c2 = BYTE8_TO_CHAR (i + 0200 - 1);
1796 char_ranges[n_char_ranges++] = c;
1797 char_ranges[n_char_ranges++] = c2;
1798 leading_code = CHAR_LEADING_CODE (c);
1799 memset (fastmap + leading_code, 1,
1800 CHAR_LEADING_CODE (c2) - leading_code + 1);
1804 else /* STRING is multibyte */
1806 SAFE_NALLOCA (char_ranges, 2, SCHARS (string));
1808 while (i_byte < size_byte)
1810 int leading_code = str[i_byte];
1812 if (handle_iso_classes)
1814 const unsigned char *ch = str + i_byte;
1815 re_wctype_t cc = re_wctype_parse (&ch, size_byte - i_byte);
1816 if (cc == 0)
1817 error ("Invalid ISO C character class");
1818 if (cc != -1)
1820 iso_classes = Fcons (make_number (cc), iso_classes);
1821 i_byte = ch - str;
1822 continue;
1826 if (leading_code== '\\')
1828 if (++i_byte == size_byte)
1829 break;
1831 leading_code = str[i_byte];
1833 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1834 i_byte += len;
1837 /* Treat `-' as range character only if another character
1838 follows. */
1839 if (i_byte + 1 < size_byte
1840 && str[i_byte] == '-')
1842 int c2, leading_code2;
1844 /* Skip over the dash. */
1845 i_byte++;
1847 /* Get the end of the range. */
1848 leading_code2 = str[i_byte];
1849 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1850 i_byte += len;
1852 if (c2 == '\\'
1853 && i_byte < size_byte)
1855 leading_code2 = str[i_byte];
1856 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1857 i_byte += len;
1860 if (c > c2)
1861 continue;
1862 if (ASCII_CHAR_P (c))
1864 while (c <= c2 && c < 0x80)
1865 fastmap[c++] = 1;
1866 leading_code = CHAR_LEADING_CODE (c);
1868 if (! ASCII_CHAR_P (c))
1870 int lim2 = leading_code2 + 1;
1871 while (leading_code < lim2)
1872 fastmap[leading_code++] = 1;
1873 if (c <= c2)
1875 char_ranges[n_char_ranges++] = c;
1876 char_ranges[n_char_ranges++] = c2;
1880 else
1882 if (ASCII_CHAR_P (c))
1883 fastmap[c] = 1;
1884 else
1886 fastmap[leading_code] = 1;
1887 char_ranges[n_char_ranges++] = c;
1888 char_ranges[n_char_ranges++] = c;
1893 /* If the current range is unibyte and STRING contains non-ASCII
1894 chars, arrange fastmap for the corresponding unibyte
1895 chars. */
1897 if (! multibyte && n_char_ranges > 0)
1899 memset (fastmap + 0200, 0, 0200);
1900 for (i = 0; i < n_char_ranges; i += 2)
1902 int c1 = char_ranges[i];
1903 int lim2 = char_ranges[i + 1] + 1;
1905 for (; c1 < lim2; c1++)
1907 int b = CHAR_TO_BYTE_SAFE (c1);
1908 if (b >= 0)
1909 fastmap[b] = 1;
1915 /* If ^ was the first character, complement the fastmap. */
1916 if (negate)
1918 if (! multibyte)
1919 for (i = 0; i < sizeof fastmap; i++)
1920 fastmap[i] ^= 1;
1921 else
1923 for (i = 0; i < 0200; i++)
1924 fastmap[i] ^= 1;
1925 /* All non-ASCII chars possibly match. */
1926 for (; i < sizeof fastmap; i++)
1927 fastmap[i] = 1;
1932 ptrdiff_t start_point = PT;
1933 ptrdiff_t pos = PT;
1934 ptrdiff_t pos_byte = PT_BYTE;
1935 unsigned char *p = PT_ADDR, *endp, *stop;
1937 if (forwardp)
1939 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1940 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1942 else
1944 endp = CHAR_POS_ADDR (XINT (lim));
1945 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1948 /* This code may look up syntax tables using functions that rely on the
1949 gl_state object. To make sure this object is not out of date,
1950 let's initialize it manually.
1951 We ignore syntax-table text-properties for now, since that's
1952 what we've done in the past. */
1953 SETUP_BUFFER_SYNTAX_TABLE ();
1954 if (forwardp)
1956 if (multibyte)
1957 while (1)
1959 int nbytes;
1961 if (p >= stop)
1963 if (p >= endp)
1964 break;
1965 p = GAP_END_ADDR;
1966 stop = endp;
1968 c = STRING_CHAR_AND_LENGTH (p, nbytes);
1969 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1971 if (negate)
1972 break;
1973 else
1974 goto fwd_ok;
1977 if (! fastmap[*p])
1978 break;
1979 if (! ASCII_CHAR_P (c))
1981 /* As we are looking at a multibyte character, we
1982 must look up the character in the table
1983 CHAR_RANGES. If there's no data in the table,
1984 that character is not what we want to skip. */
1986 /* The following code do the right thing even if
1987 n_char_ranges is zero (i.e. no data in
1988 CHAR_RANGES). */
1989 for (i = 0; i < n_char_ranges; i += 2)
1990 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1991 break;
1992 if (!(negate ^ (i < n_char_ranges)))
1993 break;
1995 fwd_ok:
1996 p += nbytes, pos++, pos_byte += nbytes;
1997 rarely_quit (pos);
1999 else
2000 while (true)
2002 if (p >= stop)
2004 if (p >= endp)
2005 break;
2006 p = GAP_END_ADDR;
2007 stop = endp;
2010 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
2012 if (negate)
2013 break;
2014 else
2015 goto fwd_unibyte_ok;
2018 if (!fastmap[*p])
2019 break;
2020 fwd_unibyte_ok:
2021 p++, pos++, pos_byte++;
2022 rarely_quit (pos);
2025 else
2027 if (multibyte)
2028 while (true)
2030 if (p <= stop)
2032 if (p <= endp)
2033 break;
2034 p = GPT_ADDR;
2035 stop = endp;
2037 unsigned char *prev_p = p;
2039 p--;
2040 while (stop <= p && ! CHAR_HEAD_P (*p));
2042 c = STRING_CHAR (p);
2044 if (! NILP (iso_classes) && in_classes (c, iso_classes))
2046 if (negate)
2047 break;
2048 else
2049 goto back_ok;
2052 if (! fastmap[*p])
2053 break;
2054 if (! ASCII_CHAR_P (c))
2056 /* See the comment in the previous similar code. */
2057 for (i = 0; i < n_char_ranges; i += 2)
2058 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
2059 break;
2060 if (!(negate ^ (i < n_char_ranges)))
2061 break;
2063 back_ok:
2064 pos--, pos_byte -= prev_p - p;
2065 rarely_quit (pos);
2067 else
2068 while (true)
2070 if (p <= stop)
2072 if (p <= endp)
2073 break;
2074 p = GPT_ADDR;
2075 stop = endp;
2078 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
2080 if (negate)
2081 break;
2082 else
2083 goto back_unibyte_ok;
2086 if (!fastmap[p[-1]])
2087 break;
2088 back_unibyte_ok:
2089 p--, pos--, pos_byte--;
2090 rarely_quit (pos);
2094 SET_PT_BOTH (pos, pos_byte);
2096 SAFE_FREE ();
2097 return make_number (PT - start_point);
2102 static Lisp_Object
2103 skip_syntaxes (bool forwardp, Lisp_Object string, Lisp_Object lim)
2105 int c;
2106 unsigned char fastmap[0400];
2107 bool negate = 0;
2108 ptrdiff_t i, i_byte;
2109 bool multibyte;
2110 ptrdiff_t size_byte;
2111 unsigned char *str;
2113 CHECK_STRING (string);
2115 if (NILP (lim))
2116 XSETINT (lim, forwardp ? ZV : BEGV);
2117 else
2118 CHECK_NUMBER_COERCE_MARKER (lim);
2120 /* In any case, don't allow scan outside bounds of buffer. */
2121 if (XINT (lim) > ZV)
2122 XSETFASTINT (lim, ZV);
2123 if (XINT (lim) < BEGV)
2124 XSETFASTINT (lim, BEGV);
2126 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
2127 return make_number (0);
2129 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
2130 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
2132 memset (fastmap, 0, sizeof fastmap);
2134 if (SBYTES (string) > SCHARS (string))
2135 /* As this is very rare case (syntax spec is ASCII only), don't
2136 consider efficiency. */
2137 string = string_make_unibyte (string);
2139 str = SDATA (string);
2140 size_byte = SBYTES (string);
2142 i_byte = 0;
2143 if (i_byte < size_byte
2144 && SREF (string, 0) == '^')
2146 negate = 1; i_byte++;
2149 /* Find the syntaxes specified and set their elements of fastmap. */
2151 while (i_byte < size_byte)
2153 c = str[i_byte++];
2154 fastmap[syntax_spec_code[c]] = 1;
2157 /* If ^ was the first character, complement the fastmap. */
2158 if (negate)
2159 for (i = 0; i < sizeof fastmap; i++)
2160 fastmap[i] ^= 1;
2163 ptrdiff_t start_point = PT;
2164 ptrdiff_t pos = PT;
2165 ptrdiff_t pos_byte = PT_BYTE;
2166 unsigned char *p, *endp, *stop;
2168 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
2170 if (forwardp)
2172 while (true)
2174 p = BYTE_POS_ADDR (pos_byte);
2175 endp = XINT (lim) == GPT ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
2176 stop = pos < GPT && GPT < XINT (lim) ? GPT_ADDR : endp;
2180 int nbytes;
2182 if (p >= stop)
2184 if (p >= endp)
2185 goto done;
2186 p = GAP_END_ADDR;
2187 stop = endp;
2189 if (multibyte)
2190 c = STRING_CHAR_AND_LENGTH (p, nbytes);
2191 else
2192 c = *p, nbytes = 1;
2193 if (! fastmap[SYNTAX (c)])
2194 goto done;
2195 p += nbytes, pos++, pos_byte += nbytes;
2196 rarely_quit (pos);
2198 while (!parse_sexp_lookup_properties
2199 || pos < gl_state.e_property);
2201 update_syntax_table_forward (pos + gl_state.offset,
2202 false, gl_state.object);
2205 else
2207 p = BYTE_POS_ADDR (pos_byte);
2208 endp = CHAR_POS_ADDR (XINT (lim));
2209 stop = pos >= GPT && GPT > XINT (lim) ? GAP_END_ADDR : endp;
2211 if (multibyte)
2213 while (true)
2215 if (p <= stop)
2217 if (p <= endp)
2218 break;
2219 p = GPT_ADDR;
2220 stop = endp;
2222 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2224 unsigned char *prev_p = p;
2226 p--;
2227 while (stop <= p && ! CHAR_HEAD_P (*p));
2229 c = STRING_CHAR (p);
2230 if (! fastmap[SYNTAX (c)])
2231 break;
2232 pos--, pos_byte -= prev_p - p;
2233 rarely_quit (pos);
2236 else
2238 while (true)
2240 if (p <= stop)
2242 if (p <= endp)
2243 break;
2244 p = GPT_ADDR;
2245 stop = endp;
2247 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2248 if (! fastmap[SYNTAX (p[-1])])
2249 break;
2250 p--, pos--, pos_byte--;
2251 rarely_quit (pos);
2256 done:
2257 SET_PT_BOTH (pos, pos_byte);
2259 return make_number (PT - start_point);
2263 /* Return true if character C belongs to one of the ISO classes
2264 in the list ISO_CLASSES. Each class is represented by an
2265 integer which is its type according to re_wctype. */
2267 static bool
2268 in_classes (int c, Lisp_Object iso_classes)
2270 bool fits_class = 0;
2272 while (CONSP (iso_classes))
2274 Lisp_Object elt;
2275 elt = XCAR (iso_classes);
2276 iso_classes = XCDR (iso_classes);
2278 if (re_iswctype (c, XFASTINT (elt)))
2279 fits_class = 1;
2282 return fits_class;
2285 /* Jump over a comment, assuming we are at the beginning of one.
2286 FROM is the current position.
2287 FROM_BYTE is the bytepos corresponding to FROM.
2288 Do not move past STOP (a charpos).
2289 The comment over which we have to jump is of style STYLE
2290 (either SYNTAX_FLAGS_COMMENT_STYLE (foo) or ST_COMMENT_STYLE).
2291 NESTING should be positive to indicate the nesting at the beginning
2292 for nested comments and should be zero or negative else.
2293 ST_COMMENT_STYLE cannot be nested.
2294 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2295 (or 0 If the search cannot start in the middle of a two-character).
2297 If successful, return true and store the charpos of the comment's
2298 end into *CHARPOS_PTR and the corresponding bytepos into
2299 *BYTEPOS_PTR. Else, return false and store the charpos STOP into
2300 *CHARPOS_PTR, the corresponding bytepos into *BYTEPOS_PTR and the
2301 current nesting (as defined for state->incomment) in
2302 *INCOMMENT_PTR. Should the last character scanned in an incomplete
2303 comment be a possible first character of a two character construct,
2304 we store its SYNTAX_WITH_FLAGS into *last_syntax_ptr. Otherwise,
2305 we store Smax into *last_syntax_ptr.
2307 The comment end is the last character of the comment rather than the
2308 character just after the comment.
2310 Global syntax data is assumed to initially be valid for FROM and
2311 remains valid for forward search starting at the returned position. */
2313 static bool
2314 forw_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop,
2315 EMACS_INT nesting, int style, int prev_syntax,
2316 ptrdiff_t *charpos_ptr, ptrdiff_t *bytepos_ptr,
2317 EMACS_INT *incomment_ptr, int *last_syntax_ptr)
2319 unsigned short int quit_count = 0;
2320 int c, c1;
2321 enum syntaxcode code;
2322 int syntax, other_syntax;
2324 if (nesting <= 0) nesting = -1;
2326 /* Enter the loop in the middle so that we find
2327 a 2-char comment ender if we start in the middle of it. */
2328 syntax = prev_syntax;
2329 code = syntax & 0xff;
2330 if (syntax != 0 && from < stop) goto forw_incomment;
2332 while (1)
2334 if (from == stop)
2336 *incomment_ptr = nesting;
2337 *charpos_ptr = from;
2338 *bytepos_ptr = from_byte;
2339 *last_syntax_ptr =
2340 (code == Sescape || code == Scharquote
2341 || SYNTAX_FLAGS_COMEND_FIRST (syntax)
2342 || (nesting > 0
2343 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)))
2344 ? syntax : Smax ;
2345 return 0;
2347 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2348 syntax = SYNTAX_WITH_FLAGS (c);
2349 code = syntax & 0xff;
2350 if (code == Sendcomment
2351 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style
2352 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2353 (nesting > 0 && --nesting == 0) : nesting < 0)
2354 && !(Vcomment_end_can_be_escaped && char_quoted (from, from_byte)))
2355 /* We have encountered a comment end of the same style
2356 as the comment sequence which began this comment
2357 section. */
2358 break;
2359 if (code == Scomment_fence
2360 && style == ST_COMMENT_STYLE)
2361 /* We have encountered a comment end of the same style
2362 as the comment sequence which began this comment
2363 section. */
2364 break;
2365 if (nesting > 0
2366 && code == Scomment
2367 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2368 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style)
2369 /* We have encountered a nested comment of the same style
2370 as the comment sequence which began this comment section. */
2371 nesting++;
2372 INC_BOTH (from, from_byte);
2373 UPDATE_SYNTAX_TABLE_FORWARD (from);
2375 forw_incomment:
2376 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2377 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2378 other_syntax = SYNTAX_WITH_FLAGS (c1),
2379 SYNTAX_FLAGS_COMEND_SECOND (other_syntax))
2380 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, other_syntax) == style
2381 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2382 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax))
2383 ? nesting > 0 : nesting < 0))
2385 syntax = Smax; /* So that "|#" (lisp) can not return
2386 the syntax of "#" in *last_syntax_ptr. */
2387 if (--nesting <= 0)
2388 /* We have encountered a comment end of the same style
2389 as the comment sequence which began this comment section. */
2390 break;
2391 else
2393 INC_BOTH (from, from_byte);
2394 UPDATE_SYNTAX_TABLE_FORWARD (from);
2397 if (nesting > 0
2398 && from < stop
2399 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2400 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2401 other_syntax = SYNTAX_WITH_FLAGS (c1),
2402 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax) == style
2403 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2404 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2405 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax)))
2406 /* We have encountered a nested comment of the same style
2407 as the comment sequence which began this comment section. */
2409 syntax = Smax; /* So that "#|#" isn't also a comment ender. */
2410 INC_BOTH (from, from_byte);
2411 UPDATE_SYNTAX_TABLE_FORWARD (from);
2412 nesting++;
2415 rarely_quit (++quit_count);
2417 *charpos_ptr = from;
2418 *bytepos_ptr = from_byte;
2419 *last_syntax_ptr = Smax; /* Any syntactic power the last byte had is
2420 used up. */
2421 return 1;
2424 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2425 doc: /*
2426 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2427 Stop scanning if we find something other than a comment or whitespace.
2428 Set point to where scanning stops.
2429 If COUNT comments are found as expected, with nothing except whitespace
2430 between them, return t; otherwise return nil. */)
2431 (Lisp_Object count)
2433 ptrdiff_t from, from_byte, stop;
2434 int c, c1;
2435 enum syntaxcode code;
2436 int comstyle = 0; /* style of comment encountered */
2437 bool comnested = 0; /* whether the comment is nestable or not */
2438 bool found;
2439 EMACS_INT count1;
2440 ptrdiff_t out_charpos, out_bytepos;
2441 EMACS_INT dummy;
2442 int dummy2;
2443 unsigned short int quit_count = 0;
2445 CHECK_NUMBER (count);
2446 count1 = XINT (count);
2447 stop = count1 > 0 ? ZV : BEGV;
2449 from = PT;
2450 from_byte = PT_BYTE;
2452 SETUP_SYNTAX_TABLE (from, count1);
2453 while (count1 > 0)
2457 bool comstart_first;
2458 int syntax, other_syntax;
2460 if (from == stop)
2462 SET_PT_BOTH (from, from_byte);
2463 return Qnil;
2465 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2466 syntax = SYNTAX_WITH_FLAGS (c);
2467 code = SYNTAX (c);
2468 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2469 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2470 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2471 INC_BOTH (from, from_byte);
2472 UPDATE_SYNTAX_TABLE_FORWARD (from);
2473 if (from < stop && comstart_first
2474 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2475 other_syntax = SYNTAX_WITH_FLAGS (c1),
2476 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)))
2478 /* We have encountered a comment start sequence and we
2479 are ignoring all text inside comments. We must record
2480 the comment style this sequence begins so that later,
2481 only a comment end of the same style actually ends
2482 the comment section. */
2483 code = Scomment;
2484 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2485 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2486 INC_BOTH (from, from_byte);
2487 UPDATE_SYNTAX_TABLE_FORWARD (from);
2489 rarely_quit (++quit_count);
2491 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2493 if (code == Scomment_fence)
2494 comstyle = ST_COMMENT_STYLE;
2495 else if (code != Scomment)
2497 DEC_BOTH (from, from_byte);
2498 SET_PT_BOTH (from, from_byte);
2499 return Qnil;
2501 /* We're at the start of a comment. */
2502 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2503 &out_charpos, &out_bytepos, &dummy, &dummy2);
2504 from = out_charpos; from_byte = out_bytepos;
2505 if (!found)
2507 SET_PT_BOTH (from, from_byte);
2508 return Qnil;
2510 INC_BOTH (from, from_byte);
2511 UPDATE_SYNTAX_TABLE_FORWARD (from);
2512 /* We have skipped one comment. */
2513 count1--;
2516 while (count1 < 0)
2518 while (true)
2520 if (from <= stop)
2522 SET_PT_BOTH (BEGV, BEGV_BYTE);
2523 return Qnil;
2526 DEC_BOTH (from, from_byte);
2527 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2528 bool quoted = char_quoted (from, from_byte);
2529 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2530 int syntax = SYNTAX_WITH_FLAGS (c);
2531 code = SYNTAX (c);
2532 comstyle = 0;
2533 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2534 if (code == Sendcomment)
2535 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2536 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2537 && prev_char_comend_first (from, from_byte)
2538 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2540 int other_syntax;
2541 /* We must record the comment style encountered so that
2542 later, we can match only the proper comment begin
2543 sequence of the same style. */
2544 DEC_BOTH (from, from_byte);
2545 code = Sendcomment;
2546 /* Calling char_quoted, above, set up global syntax position
2547 at the new value of FROM. */
2548 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2549 other_syntax = SYNTAX_WITH_FLAGS (c1);
2550 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2551 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2554 if (code == Scomment_fence)
2556 /* Skip until first preceding unquoted comment_fence. */
2557 bool fence_found = 0;
2558 ptrdiff_t ini = from, ini_byte = from_byte;
2560 while (1)
2562 DEC_BOTH (from, from_byte);
2563 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2564 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2565 if (SYNTAX (c) == Scomment_fence
2566 && !char_quoted (from, from_byte))
2568 fence_found = 1;
2569 break;
2571 else if (from == stop)
2572 break;
2573 rarely_quit (++quit_count);
2575 if (fence_found == 0)
2577 from = ini; /* Set point to ini + 1. */
2578 from_byte = ini_byte;
2579 goto leave;
2581 else
2582 /* We have skipped one comment. */
2583 break;
2585 else if (code == Sendcomment)
2587 found = back_comment (from, from_byte, stop, comnested, comstyle,
2588 &out_charpos, &out_bytepos);
2589 if (!found)
2591 if (c == '\n')
2592 /* This end-of-line is not an end-of-comment.
2593 Treat it like a whitespace.
2594 CC-mode (and maybe others) relies on this behavior. */
2596 else
2598 /* Failure: we should go back to the end of this
2599 not-quite-endcomment. */
2600 if (SYNTAX (c) != code)
2601 /* It was a two-char Sendcomment. */
2602 INC_BOTH (from, from_byte);
2603 goto leave;
2606 else
2608 /* We have skipped one comment. */
2609 from = out_charpos, from_byte = out_bytepos;
2610 break;
2613 else if (code != Swhitespace || quoted)
2615 leave:
2616 INC_BOTH (from, from_byte);
2617 SET_PT_BOTH (from, from_byte);
2618 return Qnil;
2621 rarely_quit (++quit_count);
2624 count1++;
2627 SET_PT_BOTH (from, from_byte);
2628 return Qt;
2631 /* Return syntax code of character C if C is an ASCII character
2632 or if MULTIBYTE_SYMBOL_P is false. Otherwise, return Ssymbol. */
2634 static enum syntaxcode
2635 syntax_multibyte (int c, bool multibyte_symbol_p)
2637 return ASCII_CHAR_P (c) || !multibyte_symbol_p ? SYNTAX (c) : Ssymbol;
2640 static Lisp_Object
2641 scan_lists (EMACS_INT from, EMACS_INT count, EMACS_INT depth, bool sexpflag)
2643 Lisp_Object val;
2644 ptrdiff_t stop = count > 0 ? ZV : BEGV;
2645 int c, c1;
2646 int stringterm;
2647 bool quoted;
2648 bool mathexit = 0;
2649 enum syntaxcode code;
2650 EMACS_INT min_depth = depth; /* Err out if depth gets less than this. */
2651 int comstyle = 0; /* Style of comment encountered. */
2652 bool comnested = 0; /* Whether the comment is nestable or not. */
2653 ptrdiff_t temp_pos;
2654 EMACS_INT last_good = from;
2655 bool found;
2656 ptrdiff_t from_byte;
2657 ptrdiff_t out_bytepos, out_charpos;
2658 EMACS_INT dummy;
2659 int dummy2;
2660 bool multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2661 unsigned short int quit_count = 0;
2663 if (depth > 0) min_depth = 0;
2665 if (from > ZV) from = ZV;
2666 if (from < BEGV) from = BEGV;
2668 from_byte = CHAR_TO_BYTE (from);
2670 maybe_quit ();
2672 SETUP_SYNTAX_TABLE (from, count);
2673 while (count > 0)
2675 while (from < stop)
2677 rarely_quit (++quit_count);
2678 bool comstart_first, prefix;
2679 int syntax, other_syntax;
2680 UPDATE_SYNTAX_TABLE_FORWARD (from);
2681 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2682 syntax = SYNTAX_WITH_FLAGS (c);
2683 code = syntax_multibyte (c, multibyte_symbol_p);
2684 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2685 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2686 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2687 prefix = SYNTAX_FLAGS_PREFIX (syntax);
2688 if (depth == min_depth)
2689 last_good = from;
2690 INC_BOTH (from, from_byte);
2691 UPDATE_SYNTAX_TABLE_FORWARD (from);
2692 if (from < stop && comstart_first
2693 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2694 other_syntax = SYNTAX_WITH_FLAGS (c),
2695 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2696 && parse_sexp_ignore_comments)
2698 /* We have encountered a comment start sequence and we
2699 are ignoring all text inside comments. We must record
2700 the comment style this sequence begins so that later,
2701 only a comment end of the same style actually ends
2702 the comment section. */
2703 code = Scomment;
2704 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2705 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2706 INC_BOTH (from, from_byte);
2707 UPDATE_SYNTAX_TABLE_FORWARD (from);
2710 if (prefix)
2711 continue;
2713 switch (code)
2715 case Sescape:
2716 case Scharquote:
2717 if (from == stop)
2718 goto lose;
2719 INC_BOTH (from, from_byte);
2720 /* Treat following character as a word constituent. */
2721 FALLTHROUGH;
2722 case Sword:
2723 case Ssymbol:
2724 if (depth || !sexpflag) break;
2725 /* This word counts as a sexp; return at end of it. */
2726 while (from < stop)
2728 UPDATE_SYNTAX_TABLE_FORWARD (from);
2730 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2731 switch (syntax_multibyte (c, multibyte_symbol_p))
2733 case Scharquote:
2734 case Sescape:
2735 INC_BOTH (from, from_byte);
2736 if (from == stop)
2737 goto lose;
2738 break;
2739 case Sword:
2740 case Ssymbol:
2741 case Squote:
2742 break;
2743 default:
2744 goto done;
2746 INC_BOTH (from, from_byte);
2747 rarely_quit (++quit_count);
2749 goto done;
2751 case Scomment_fence:
2752 comstyle = ST_COMMENT_STYLE;
2753 FALLTHROUGH;
2754 case Scomment:
2755 if (!parse_sexp_ignore_comments) break;
2756 UPDATE_SYNTAX_TABLE_FORWARD (from);
2757 found = forw_comment (from, from_byte, stop,
2758 comnested, comstyle, 0,
2759 &out_charpos, &out_bytepos, &dummy,
2760 &dummy2);
2761 from = out_charpos, from_byte = out_bytepos;
2762 if (!found)
2764 if (depth == 0)
2765 goto done;
2766 goto lose;
2768 INC_BOTH (from, from_byte);
2769 UPDATE_SYNTAX_TABLE_FORWARD (from);
2770 break;
2772 case Smath:
2773 if (!sexpflag)
2774 break;
2775 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2777 INC_BOTH (from, from_byte);
2779 if (mathexit)
2781 mathexit = 0;
2782 goto close1;
2784 mathexit = 1;
2785 FALLTHROUGH;
2786 case Sopen:
2787 if (!++depth) goto done;
2788 break;
2790 case Sclose:
2791 close1:
2792 if (!--depth) goto done;
2793 if (depth < min_depth)
2794 xsignal3 (Qscan_error,
2795 build_string ("Containing expression ends prematurely"),
2796 make_number (last_good), make_number (from));
2797 break;
2799 case Sstring:
2800 case Sstring_fence:
2801 temp_pos = dec_bytepos (from_byte);
2802 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2803 while (1)
2805 enum syntaxcode c_code;
2806 if (from >= stop)
2807 goto lose;
2808 UPDATE_SYNTAX_TABLE_FORWARD (from);
2809 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2810 c_code = syntax_multibyte (c, multibyte_symbol_p);
2811 if (code == Sstring
2812 ? c == stringterm && c_code == Sstring
2813 : c_code == Sstring_fence)
2814 break;
2816 if (c_code == Scharquote || c_code == Sescape)
2817 INC_BOTH (from, from_byte);
2818 INC_BOTH (from, from_byte);
2819 rarely_quit (++quit_count);
2821 INC_BOTH (from, from_byte);
2822 if (!depth && sexpflag) goto done;
2823 break;
2824 default:
2825 /* Ignore whitespace, punctuation, quote, endcomment. */
2826 break;
2830 /* Reached end of buffer. Error if within object, return nil if between */
2831 if (depth)
2832 goto lose;
2834 return Qnil;
2836 /* End of object reached */
2837 done:
2838 count--;
2841 while (count < 0)
2843 while (from > stop)
2845 rarely_quit (++quit_count);
2846 DEC_BOTH (from, from_byte);
2847 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2848 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2849 int syntax = SYNTAX_WITH_FLAGS (c);
2850 code = syntax_multibyte (c, multibyte_symbol_p);
2851 if (depth == min_depth)
2852 last_good = from;
2853 comstyle = 0;
2854 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2855 if (code == Sendcomment)
2856 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2857 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2858 && prev_char_comend_first (from, from_byte)
2859 && parse_sexp_ignore_comments)
2861 /* We must record the comment style encountered so that
2862 later, we can match only the proper comment begin
2863 sequence of the same style. */
2864 int c2, other_syntax;
2865 DEC_BOTH (from, from_byte);
2866 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2867 code = Sendcomment;
2868 c2 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2869 other_syntax = SYNTAX_WITH_FLAGS (c2);
2870 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2871 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2874 /* Quoting turns anything except a comment-ender
2875 into a word character. Note that this cannot be true
2876 if we decremented FROM in the if-statement above. */
2877 if (code != Sendcomment && char_quoted (from, from_byte))
2879 DEC_BOTH (from, from_byte);
2880 code = Sword;
2882 else if (SYNTAX_FLAGS_PREFIX (syntax))
2883 continue;
2885 switch (code)
2887 case Sword:
2888 case Ssymbol:
2889 case Sescape:
2890 case Scharquote:
2891 if (depth || !sexpflag) break;
2892 /* This word counts as a sexp; count object finished
2893 after passing it. */
2894 while (from > stop)
2896 temp_pos = from_byte;
2897 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2898 DEC_POS (temp_pos);
2899 else
2900 temp_pos--;
2901 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2902 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2903 /* Don't allow comment-end to be quoted. */
2904 if (syntax_multibyte (c1, multibyte_symbol_p) == Sendcomment)
2905 goto done2;
2906 quoted = char_quoted (from - 1, temp_pos);
2907 if (quoted)
2909 DEC_BOTH (from, from_byte);
2910 temp_pos = dec_bytepos (temp_pos);
2911 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2913 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2914 if (! quoted)
2915 switch (syntax_multibyte (c1, multibyte_symbol_p))
2917 case Sword: case Ssymbol: case Squote: break;
2918 default: goto done2;
2920 DEC_BOTH (from, from_byte);
2921 rarely_quit (++quit_count);
2923 goto done2;
2925 case Smath:
2926 if (!sexpflag)
2927 break;
2928 if (from > BEGV)
2930 temp_pos = dec_bytepos (from_byte);
2931 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2932 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2933 DEC_BOTH (from, from_byte);
2935 if (mathexit)
2937 mathexit = 0;
2938 goto open2;
2940 mathexit = 1;
2941 FALLTHROUGH;
2942 case Sclose:
2943 if (!++depth) goto done2;
2944 break;
2946 case Sopen:
2947 open2:
2948 if (!--depth) goto done2;
2949 if (depth < min_depth)
2950 xsignal3 (Qscan_error,
2951 build_string ("Containing expression ends prematurely"),
2952 make_number (last_good), make_number (from));
2953 break;
2955 case Sendcomment:
2956 if (!parse_sexp_ignore_comments)
2957 break;
2958 found = back_comment (from, from_byte, stop, comnested, comstyle,
2959 &out_charpos, &out_bytepos);
2960 /* FIXME: if !found, it really wasn't a comment-end.
2961 For single-char Sendcomment, we can't do much about it apart
2962 from skipping the char.
2963 For 2-char endcomments, we could try again, taking both
2964 chars as separate entities, but it's a lot of trouble
2965 for very little gain, so we don't bother either. -sm */
2966 if (found)
2967 from = out_charpos, from_byte = out_bytepos;
2968 break;
2970 case Scomment_fence:
2971 case Sstring_fence:
2972 while (1)
2974 if (from == stop)
2975 goto lose;
2976 DEC_BOTH (from, from_byte);
2977 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2978 if (!char_quoted (from, from_byte))
2980 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2981 if (syntax_multibyte (c, multibyte_symbol_p) == code)
2982 break;
2984 rarely_quit (++quit_count);
2986 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2987 break;
2989 case Sstring:
2990 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2991 while (true)
2993 if (from == stop)
2994 goto lose;
2995 DEC_BOTH (from, from_byte);
2996 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2997 if (!char_quoted (from, from_byte))
2999 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3000 if (c == stringterm
3001 && (syntax_multibyte (c, multibyte_symbol_p)
3002 == Sstring))
3003 break;
3005 rarely_quit (++quit_count);
3007 if (!depth && sexpflag) goto done2;
3008 break;
3009 default:
3010 /* Ignore whitespace, punctuation, quote, endcomment. */
3011 break;
3015 /* Reached start of buffer. Error if within object, return nil if between */
3016 if (depth)
3017 goto lose;
3019 return Qnil;
3021 done2:
3022 count++;
3026 XSETFASTINT (val, from);
3027 return val;
3029 lose:
3030 xsignal3 (Qscan_error,
3031 build_string ("Unbalanced parentheses"),
3032 make_number (last_good), make_number (from));
3035 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
3036 doc: /* Scan from character number FROM by COUNT lists.
3037 Scan forward if COUNT is positive, backward if COUNT is negative.
3038 Return the character number of the position thus found.
3040 A \"list", in this context, refers to a balanced parenthetical
3041 grouping, as determined by the syntax table.
3043 If DEPTH is nonzero, treat that as the nesting depth of the starting
3044 point (i.e. the starting point is DEPTH parentheses deep). This
3045 function scans over parentheses until the depth goes to zero COUNT
3046 times. Hence, positive DEPTH moves out that number of levels of
3047 parentheses, while negative DEPTH moves to a deeper level.
3049 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3051 If we reach the beginning or end of the accessible part of the buffer
3052 before we have scanned over COUNT lists, return nil if the depth at
3053 that point is zero, and signal an error if the depth is nonzero. */)
3054 (Lisp_Object from, Lisp_Object count, Lisp_Object depth)
3056 CHECK_NUMBER (from);
3057 CHECK_NUMBER (count);
3058 CHECK_NUMBER (depth);
3060 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
3063 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
3064 doc: /* Scan from character number FROM by COUNT balanced expressions.
3065 If COUNT is negative, scan backwards.
3066 Returns the character number of the position thus found.
3068 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3070 If the beginning or end of (the accessible part of) the buffer is reached
3071 in the middle of a parenthetical grouping, an error is signaled.
3072 If the beginning or end is reached between groupings
3073 but before count is used up, nil is returned. */)
3074 (Lisp_Object from, Lisp_Object count)
3076 CHECK_NUMBER (from);
3077 CHECK_NUMBER (count);
3079 return scan_lists (XINT (from), XINT (count), 0, 1);
3082 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
3083 0, 0, 0,
3084 doc: /* Move point backward over any number of chars with prefix syntax.
3085 This includes chars with expression prefix syntax class (\\=') and those with
3086 the prefix syntax flag (p). */)
3087 (void)
3089 ptrdiff_t beg = BEGV;
3090 ptrdiff_t opoint = PT;
3091 ptrdiff_t opoint_byte = PT_BYTE;
3092 ptrdiff_t pos = PT;
3093 ptrdiff_t pos_byte = PT_BYTE;
3094 int c;
3096 if (pos <= beg)
3098 SET_PT_BOTH (opoint, opoint_byte);
3100 return Qnil;
3103 SETUP_SYNTAX_TABLE (pos, -1);
3105 DEC_BOTH (pos, pos_byte);
3107 while (!char_quoted (pos, pos_byte)
3108 /* Previous statement updates syntax table. */
3109 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
3110 || syntax_prefix_flag_p (c)))
3112 opoint = pos;
3113 opoint_byte = pos_byte;
3115 if (pos <= beg)
3116 break;
3117 DEC_BOTH (pos, pos_byte);
3118 rarely_quit (pos);
3121 SET_PT_BOTH (opoint, opoint_byte);
3123 return Qnil;
3127 /* If the character at FROM_BYTE is the second part of a 2-character
3128 comment opener based on PREV_FROM_SYNTAX, update STATE and return
3129 true. */
3130 static bool
3131 in_2char_comment_start (struct lisp_parse_state *state,
3132 int prev_from_syntax,
3133 ptrdiff_t prev_from,
3134 ptrdiff_t from_byte)
3136 int c1, syntax;
3137 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3138 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
3139 syntax = SYNTAX_WITH_FLAGS (c1),
3140 SYNTAX_FLAGS_COMSTART_SECOND (syntax)))
3142 /* Record the comment style we have entered so that only
3143 the comment-end sequence of the same style actually
3144 terminates the comment section. */
3145 state->comstyle
3146 = SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_from_syntax);
3147 bool comnested = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax)
3148 | SYNTAX_FLAGS_COMMENT_NESTED (syntax));
3149 state->incomment = comnested ? 1 : -1;
3150 state->comstr_start = prev_from;
3151 return true;
3153 return false;
3156 /* Parse forward from FROM / FROM_BYTE to END,
3157 assuming that FROM has state STATE,
3158 and return a description of the state of the parse at END.
3159 If STOPBEFORE, stop at the start of an atom.
3160 If COMMENTSTOP is 1, stop at the start of a comment.
3161 If COMMENTSTOP is -1, stop at the start or end of a comment,
3162 after the beginning of a string, or after the end of a string. */
3164 static void
3165 scan_sexps_forward (struct lisp_parse_state *state,
3166 ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t end,
3167 EMACS_INT targetdepth, bool stopbefore,
3168 int commentstop)
3170 enum syntaxcode code;
3171 struct level { ptrdiff_t last, prev; };
3172 struct level levelstart[100];
3173 struct level *curlevel = levelstart;
3174 struct level *endlevel = levelstart + 100;
3175 EMACS_INT depth; /* Paren depth of current scanning location.
3176 level - levelstart equals this except
3177 when the depth becomes negative. */
3178 EMACS_INT mindepth; /* Lowest DEPTH value seen. */
3179 bool start_quoted = 0; /* True means starting after a char quote. */
3180 Lisp_Object tem;
3181 ptrdiff_t prev_from; /* Keep one character before FROM. */
3182 ptrdiff_t prev_from_byte;
3183 int prev_from_syntax, prev_prev_from_syntax;
3184 bool boundary_stop = commentstop == -1;
3185 bool nofence;
3186 bool found;
3187 ptrdiff_t out_bytepos, out_charpos;
3188 int temp;
3189 unsigned short int quit_count = 0;
3191 prev_from = from;
3192 prev_from_byte = from_byte;
3193 if (from != BEGV)
3194 DEC_BOTH (prev_from, prev_from_byte);
3196 /* Use this macro instead of `from++'. */
3197 #define INC_FROM \
3198 do { prev_from = from; \
3199 prev_from_byte = from_byte; \
3200 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
3201 prev_prev_from_syntax = prev_from_syntax; \
3202 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
3203 INC_BOTH (from, from_byte); \
3204 if (from < end) \
3205 UPDATE_SYNTAX_TABLE_FORWARD (from); \
3206 } while (0)
3208 maybe_quit ();
3210 depth = state->depth;
3211 start_quoted = state->quoted;
3212 prev_prev_from_syntax = Smax;
3213 prev_from_syntax = state->prev_syntax;
3215 tem = state->levelstarts;
3216 while (!NILP (tem)) /* >= second enclosing sexps. */
3218 Lisp_Object temhd = Fcar (tem);
3219 if (RANGED_INTEGERP (PTRDIFF_MIN, temhd, PTRDIFF_MAX))
3220 curlevel->last = XINT (temhd);
3221 if (++curlevel == endlevel)
3222 curlevel--; /* error ("Nesting too deep for parser"); */
3223 curlevel->prev = -1;
3224 curlevel->last = -1;
3225 tem = Fcdr (tem);
3227 curlevel->prev = -1;
3228 curlevel->last = -1;
3230 state->quoted = 0;
3231 mindepth = depth;
3233 SETUP_SYNTAX_TABLE (from, 1);
3235 /* Enter the loop at a place appropriate for initial state. */
3237 if (state->incomment)
3238 goto startincomment;
3239 if (state->instring >= 0)
3241 nofence = state->instring != ST_STRING_STYLE;
3242 if (start_quoted)
3243 goto startquotedinstring;
3244 goto startinstring;
3246 else if (start_quoted)
3247 goto startquoted;
3248 else if ((from < end)
3249 && (in_2char_comment_start (state, prev_from_syntax,
3250 prev_from, from_byte)))
3252 INC_FROM;
3253 prev_from_syntax = Smax; /* the syntax has already been "used up". */
3254 goto atcomment;
3257 while (from < end)
3259 rarely_quit (++quit_count);
3260 INC_FROM;
3262 if ((from < end)
3263 && (in_2char_comment_start (state, prev_from_syntax,
3264 prev_from, from_byte)))
3266 INC_FROM;
3267 prev_from_syntax = Smax; /* the syntax has already been "used up". */
3268 goto atcomment;
3271 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3272 continue;
3273 code = prev_from_syntax & 0xff;
3274 switch (code)
3276 case Sescape:
3277 case Scharquote:
3278 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3279 curlevel->last = prev_from;
3280 startquoted:
3281 if (from == end) goto endquoted;
3282 INC_FROM;
3283 goto symstarted;
3284 /* treat following character as a word constituent */
3285 case Sword:
3286 case Ssymbol:
3287 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3288 curlevel->last = prev_from;
3289 symstarted:
3290 while (from < end)
3292 if (in_2char_comment_start (state, prev_from_syntax,
3293 prev_from, from_byte))
3295 INC_FROM;
3296 prev_from_syntax = Smax; /* the syntax has already been "used up". */
3297 goto atcomment;
3300 int symchar = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3301 switch (SYNTAX (symchar))
3303 case Scharquote:
3304 case Sescape:
3305 INC_FROM;
3306 if (from == end) goto endquoted;
3307 break;
3308 case Sword:
3309 case Ssymbol:
3310 case Squote:
3311 break;
3312 default:
3313 goto symdone;
3315 INC_FROM;
3316 rarely_quit (++quit_count);
3318 symdone:
3319 curlevel->prev = curlevel->last;
3320 break;
3322 case Scomment_fence:
3323 /* Record the comment style we have entered so that only
3324 the comment-end sequence of the same style actually
3325 terminates the comment section. */
3326 state->comstyle = ST_COMMENT_STYLE;
3327 state->incomment = -1;
3328 state->comstr_start = prev_from;
3329 goto atcomment;
3330 case Scomment:
3331 state->comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax, 0);
3332 state->incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3333 1 : -1);
3334 state->comstr_start = prev_from;
3335 atcomment:
3336 if (commentstop || boundary_stop) goto done;
3337 startincomment:
3338 /* The (from == BEGV) test was to enter the loop in the middle so
3339 that we find a 2-char comment ender even if we start in the
3340 middle of it. We don't want to do that if we're just at the
3341 beginning of the comment (think of (*) ... (*)). */
3342 found = forw_comment (from, from_byte, end,
3343 state->incomment, state->comstyle,
3344 from == BEGV ? 0 : prev_from_syntax,
3345 &out_charpos, &out_bytepos, &state->incomment,
3346 &prev_from_syntax);
3347 from = out_charpos; from_byte = out_bytepos;
3348 /* Beware! prev_from and friends (except prev_from_syntax)
3349 are invalid now. Luckily, the `done' doesn't use them
3350 and the INC_FROM sets them to a sane value without
3351 looking at them. */
3352 if (!found) goto done;
3353 INC_FROM;
3354 state->incomment = 0;
3355 state->comstyle = 0; /* reset the comment style */
3356 prev_from_syntax = Smax; /* For the comment closer */
3357 if (boundary_stop) goto done;
3358 break;
3360 case Sopen:
3361 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3362 depth++;
3363 /* curlevel++->last ran into compiler bug on Apollo */
3364 curlevel->last = prev_from;
3365 if (++curlevel == endlevel)
3366 curlevel--; /* error ("Nesting too deep for parser"); */
3367 curlevel->prev = -1;
3368 curlevel->last = -1;
3369 if (targetdepth == depth) goto done;
3370 break;
3372 case Sclose:
3373 depth--;
3374 if (depth < mindepth)
3375 mindepth = depth;
3376 if (curlevel != levelstart)
3377 curlevel--;
3378 curlevel->prev = curlevel->last;
3379 if (targetdepth == depth) goto done;
3380 break;
3382 case Sstring:
3383 case Sstring_fence:
3384 state->comstr_start = from - 1;
3385 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3386 curlevel->last = prev_from;
3387 state->instring = (code == Sstring
3388 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3389 : ST_STRING_STYLE);
3390 if (boundary_stop) goto done;
3391 startinstring:
3393 nofence = state->instring != ST_STRING_STYLE;
3395 while (1)
3397 int c;
3398 enum syntaxcode c_code;
3400 if (from >= end) goto done;
3401 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3402 c_code = SYNTAX (c);
3404 /* Check C_CODE here so that if the char has
3405 a syntax-table property which says it is NOT
3406 a string character, it does not end the string. */
3407 if (nofence && c == state->instring && c_code == Sstring)
3408 break;
3410 switch (c_code)
3412 case Sstring_fence:
3413 if (!nofence) goto string_end;
3414 break;
3416 case Scharquote:
3417 case Sescape:
3418 INC_FROM;
3419 startquotedinstring:
3420 if (from >= end) goto endquoted;
3421 break;
3423 default:
3424 break;
3426 INC_FROM;
3427 rarely_quit (++quit_count);
3430 string_end:
3431 state->instring = -1;
3432 curlevel->prev = curlevel->last;
3433 INC_FROM;
3434 if (boundary_stop) goto done;
3435 break;
3437 case Smath:
3438 /* FIXME: We should do something with it. */
3439 break;
3440 default:
3441 /* Ignore whitespace, punctuation, quote, endcomment. */
3442 break;
3445 goto done;
3447 stop: /* Here if stopping before start of sexp. */
3448 from = prev_from; /* We have just fetched the char that starts it; */
3449 from_byte = prev_from_byte;
3450 prev_from_syntax = prev_prev_from_syntax;
3451 goto done; /* but return the position before it. */
3453 endquoted:
3454 state->quoted = 1;
3455 done:
3456 state->depth = depth;
3457 state->mindepth = mindepth;
3458 state->thislevelstart = curlevel->prev;
3459 state->prevlevelstart
3460 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3461 state->location = from;
3462 state->location_byte = from_byte;
3463 state->levelstarts = Qnil;
3464 while (curlevel > levelstart)
3465 state->levelstarts = Fcons (make_number ((--curlevel)->last),
3466 state->levelstarts);
3467 state->prev_syntax = (SYNTAX_FLAGS_COMSTARTEND_FIRST (prev_from_syntax)
3468 || state->quoted) ? prev_from_syntax : Smax;
3471 /* Convert a (lisp) parse state to the internal form used in
3472 scan_sexps_forward. */
3473 static void
3474 internalize_parse_state (Lisp_Object external, struct lisp_parse_state *state)
3476 Lisp_Object tem;
3478 if (NILP (external))
3480 state->depth = 0;
3481 state->instring = -1;
3482 state->incomment = 0;
3483 state->quoted = 0;
3484 state->comstyle = 0; /* comment style a by default. */
3485 state->comstr_start = -1; /* no comment/string seen. */
3486 state->levelstarts = Qnil;
3487 state->prev_syntax = Smax;
3489 else
3491 tem = Fcar (external);
3492 if (!NILP (tem))
3493 state->depth = XINT (tem);
3494 else
3495 state->depth = 0;
3497 external = Fcdr (external);
3498 external = Fcdr (external);
3499 external = Fcdr (external);
3500 tem = Fcar (external);
3501 /* Check whether we are inside string_fence-style string: */
3502 state->instring = (!NILP (tem)
3503 ? (CHARACTERP (tem) ? XFASTINT (tem) : ST_STRING_STYLE)
3504 : -1);
3506 external = Fcdr (external);
3507 tem = Fcar (external);
3508 state->incomment = (!NILP (tem)
3509 ? (INTEGERP (tem) ? XINT (tem) : -1)
3510 : 0);
3512 external = Fcdr (external);
3513 tem = Fcar (external);
3514 state->quoted = !NILP (tem);
3516 /* if the eighth element of the list is nil, we are in comment
3517 style a. If it is non-nil, we are in comment style b */
3518 external = Fcdr (external);
3519 external = Fcdr (external);
3520 tem = Fcar (external);
3521 state->comstyle = (NILP (tem)
3523 : (RANGED_INTEGERP (0, tem, ST_COMMENT_STYLE)
3524 ? XINT (tem)
3525 : ST_COMMENT_STYLE));
3527 external = Fcdr (external);
3528 tem = Fcar (external);
3529 state->comstr_start =
3530 RANGED_INTEGERP (PTRDIFF_MIN, tem, PTRDIFF_MAX) ? XINT (tem) : -1;
3531 external = Fcdr (external);
3532 tem = Fcar (external);
3533 state->levelstarts = tem;
3535 external = Fcdr (external);
3536 tem = Fcar (external);
3537 state->prev_syntax = NILP (tem) ? Smax : XINT (tem);
3541 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3542 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3543 Parsing stops at TO or when certain criteria are met;
3544 point is set to where parsing stops.
3545 If fifth arg OLDSTATE is omitted or nil,
3546 parsing assumes that FROM is the beginning of a function.
3548 Value is a list of elements describing final state of parsing:
3549 0. depth in parens.
3550 1. character address of start of innermost containing list; nil if none.
3551 2. character address of start of last complete sexp terminated.
3552 3. non-nil if inside a string.
3553 (it is the character that will terminate the string,
3554 or t if the string should be terminated by a generic string delimiter.)
3555 4. nil if outside a comment, t if inside a non-nestable comment,
3556 else an integer (the current comment nesting).
3557 5. t if following a quote character.
3558 6. the minimum paren-depth encountered during this scan.
3559 7. style of comment, if any.
3560 8. character address of start of comment or string; nil if not in one.
3561 9. List of positions of currently open parens, outermost first.
3562 10. When the last position scanned holds the first character of a
3563 (potential) two character construct, the syntax of that position,
3564 otherwise nil. That construct can be a two character comment
3565 delimiter or an Escaped or Char-quoted character.
3566 11..... Possible further internal information used by `parse-partial-sexp'.
3568 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3569 in parentheses becomes equal to TARGETDEPTH.
3570 Fourth arg STOPBEFORE non-nil means stop when we come to
3571 any character that starts a sexp.
3572 Fifth arg OLDSTATE is a list like what this function returns.
3573 It is used to initialize the state of the parse. Elements number 1, 2, 6
3574 are ignored.
3575 Sixth arg COMMENTSTOP non-nil means stop after the start of a comment.
3576 If it is the symbol `syntax-table', stop after the start of a comment or a
3577 string, or after end of a comment or a string. */)
3578 (Lisp_Object from, Lisp_Object to, Lisp_Object targetdepth,
3579 Lisp_Object stopbefore, Lisp_Object oldstate, Lisp_Object commentstop)
3581 struct lisp_parse_state state;
3582 EMACS_INT target;
3584 if (!NILP (targetdepth))
3586 CHECK_NUMBER (targetdepth);
3587 target = XINT (targetdepth);
3589 else
3590 target = TYPE_MINIMUM (EMACS_INT); /* We won't reach this depth. */
3592 validate_region (&from, &to);
3593 internalize_parse_state (oldstate, &state);
3594 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3595 XINT (to),
3596 target, !NILP (stopbefore),
3597 (NILP (commentstop)
3598 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3600 SET_PT_BOTH (state.location, state.location_byte);
3602 return
3603 Fcons (make_number (state.depth),
3604 Fcons (state.prevlevelstart < 0
3605 ? Qnil : make_number (state.prevlevelstart),
3606 Fcons (state.thislevelstart < 0
3607 ? Qnil : make_number (state.thislevelstart),
3608 Fcons (state.instring >= 0
3609 ? (state.instring == ST_STRING_STYLE
3610 ? Qt : make_number (state.instring)) : Qnil,
3611 Fcons (state.incomment < 0 ? Qt :
3612 (state.incomment == 0 ? Qnil :
3613 make_number (state.incomment)),
3614 Fcons (state.quoted ? Qt : Qnil,
3615 Fcons (make_number (state.mindepth),
3616 Fcons ((state.comstyle
3617 ? (state.comstyle == ST_COMMENT_STYLE
3618 ? Qsyntax_table
3619 : make_number (state.comstyle))
3620 : Qnil),
3621 Fcons (((state.incomment
3622 || (state.instring >= 0))
3623 ? make_number (state.comstr_start)
3624 : Qnil),
3625 Fcons (state.levelstarts,
3626 Fcons (state.prev_syntax == Smax
3627 ? Qnil
3628 : make_number (state.prev_syntax),
3629 Qnil)))))))))));
3632 void
3633 init_syntax_once (void)
3635 register int i, c;
3636 Lisp_Object temp;
3638 /* This has to be done here, before we call Fmake_char_table. */
3639 DEFSYM (Qsyntax_table, "syntax-table");
3641 /* Create objects which can be shared among syntax tables. */
3642 Vsyntax_code_object = make_uninit_vector (Smax);
3643 for (i = 0; i < Smax; i++)
3644 ASET (Vsyntax_code_object, i, Fcons (make_number (i), Qnil));
3646 /* Now we are ready to set up this property, so we can
3647 create syntax tables. */
3648 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3650 temp = AREF (Vsyntax_code_object, Swhitespace);
3652 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3654 /* Control characters should not be whitespace. */
3655 temp = AREF (Vsyntax_code_object, Spunct);
3656 for (i = 0; i <= ' ' - 1; i++)
3657 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3658 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3660 /* Except that a few really are whitespace. */
3661 temp = AREF (Vsyntax_code_object, Swhitespace);
3662 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3663 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3664 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3665 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3666 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3668 temp = AREF (Vsyntax_code_object, Sword);
3669 for (i = 'a'; i <= 'z'; i++)
3670 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3671 for (i = 'A'; i <= 'Z'; i++)
3672 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3673 for (i = '0'; i <= '9'; i++)
3674 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3676 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3677 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3679 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3680 Fcons (make_number (Sopen), make_number (')')));
3681 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3682 Fcons (make_number (Sclose), make_number ('(')));
3683 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3684 Fcons (make_number (Sopen), make_number (']')));
3685 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3686 Fcons (make_number (Sclose), make_number ('[')));
3687 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3688 Fcons (make_number (Sopen), make_number ('}')));
3689 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3690 Fcons (make_number (Sclose), make_number ('{')));
3691 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3692 Fcons (make_number (Sstring), Qnil));
3693 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3694 Fcons (make_number (Sescape), Qnil));
3696 temp = AREF (Vsyntax_code_object, Ssymbol);
3697 for (i = 0; i < 10; i++)
3699 c = "_-+*/&|<>="[i];
3700 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3703 temp = AREF (Vsyntax_code_object, Spunct);
3704 for (i = 0; i < 12; i++)
3706 c = ".,;:?!#@~^'`"[i];
3707 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3710 /* All multibyte characters have syntax `word' by default. */
3711 temp = AREF (Vsyntax_code_object, Sword);
3712 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3715 void
3716 syms_of_syntax (void)
3718 DEFSYM (Qsyntax_table_p, "syntax-table-p");
3719 DEFSYM (Qsyntax_ppss, "syntax-ppss");
3720 DEFVAR_LISP ("comment-use-syntax-ppss",
3721 Vcomment_use_syntax_ppss,
3722 doc: /* Non-nil means `forward-comment' can use `syntax-ppss' internally. */);
3723 Vcomment_use_syntax_ppss = Qt;
3725 staticpro (&Vsyntax_code_object);
3727 staticpro (&gl_state.object);
3728 staticpro (&gl_state.global_code);
3729 staticpro (&gl_state.current_syntax_table);
3730 staticpro (&gl_state.old_prop);
3732 /* Defined in regex.c. */
3733 staticpro (&re_match_object);
3735 DEFSYM (Qscan_error, "scan-error");
3736 Fput (Qscan_error, Qerror_conditions,
3737 listn (CONSTYPE_PURE, 2, Qscan_error, Qerror));
3738 Fput (Qscan_error, Qerror_message,
3739 build_pure_c_string ("Scan error"));
3741 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments,
3742 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3744 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties,
3745 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3746 Otherwise, that text property is simply ignored.
3747 See the info node `(elisp)Syntax Properties' for a description of the
3748 `syntax-table' property. */);
3750 DEFVAR_INT ("syntax-propertize--done", syntax_propertize__done,
3751 doc: /* Position up to which syntax-table properties have been set. */);
3752 syntax_propertize__done = -1;
3753 DEFSYM (Qinternal__syntax_propertize, "internal--syntax-propertize");
3754 Fmake_variable_buffer_local (intern ("syntax-propertize--done"));
3756 words_include_escapes = 0;
3757 DEFVAR_BOOL ("words-include-escapes", words_include_escapes,
3758 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3760 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol,
3761 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3762 multibyte_syntax_as_symbol = 0;
3764 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3765 open_paren_in_column_0_is_defun_start,
3766 doc: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3767 open_paren_in_column_0_is_defun_start = 1;
3770 DEFVAR_LISP ("find-word-boundary-function-table",
3771 Vfind_word_boundary_function_table,
3772 doc: /*
3773 Char table of functions to search for the word boundary.
3774 Each function is called with two arguments; POS and LIMIT.
3775 POS and LIMIT are character positions in the current buffer.
3777 If POS is less than LIMIT, POS is at the first character of a word,
3778 and the return value of a function should be a position after the
3779 last character of that word.
3781 If POS is not less than LIMIT, POS is at the last character of a word,
3782 and the return value of a function should be a position at the first
3783 character of that word.
3785 In both cases, LIMIT bounds the search. */);
3786 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3788 DEFVAR_BOOL ("comment-end-can-be-escaped", Vcomment_end_can_be_escaped,
3789 doc: /* Non-nil means an escaped ender inside a comment doesn't end the comment. */);
3790 Vcomment_end_can_be_escaped = 0;
3791 DEFSYM (Qcomment_end_can_be_escaped, "comment-end-can-be-escaped");
3792 Fmake_variable_buffer_local (Qcomment_end_can_be_escaped);
3794 defsubr (&Ssyntax_table_p);
3795 defsubr (&Ssyntax_table);
3796 defsubr (&Sstandard_syntax_table);
3797 defsubr (&Scopy_syntax_table);
3798 defsubr (&Sset_syntax_table);
3799 defsubr (&Schar_syntax);
3800 defsubr (&Smatching_paren);
3801 defsubr (&Sstring_to_syntax);
3802 defsubr (&Smodify_syntax_entry);
3803 defsubr (&Sinternal_describe_syntax_value);
3805 defsubr (&Sforward_word);
3807 defsubr (&Sskip_chars_forward);
3808 defsubr (&Sskip_chars_backward);
3809 defsubr (&Sskip_syntax_forward);
3810 defsubr (&Sskip_syntax_backward);
3812 defsubr (&Sforward_comment);
3813 defsubr (&Sscan_lists);
3814 defsubr (&Sscan_sexps);
3815 defsubr (&Sbackward_prefix_chars);
3816 defsubr (&Sparse_partial_sexp);