* lisp/net/ange-ftp.el: Use lexical-binding
[emacs.git] / src / syntax.c
blob91c46f7a738ae2208b1ab9db8c8b4e36bcd23931
1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2017 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'. */)
1112 (Lisp_Object character)
1114 int char_int;
1115 CHECK_CHARACTER (character);
1116 char_int = XINT (character);
1117 SETUP_BUFFER_SYNTAX_TABLE ();
1118 return make_number (syntax_code_spec[SYNTAX (char_int)]);
1121 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
1122 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
1123 (Lisp_Object character)
1125 int char_int;
1126 enum syntaxcode code;
1127 CHECK_CHARACTER (character);
1128 char_int = XINT (character);
1129 SETUP_BUFFER_SYNTAX_TABLE ();
1130 code = SYNTAX (char_int);
1131 if (code == Sopen || code == Sclose)
1132 return SYNTAX_MATCH (char_int);
1133 return Qnil;
1136 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
1137 doc: /* Convert a syntax descriptor STRING into a raw syntax descriptor.
1138 STRING should be a string of the form allowed as argument of
1139 `modify-syntax-entry'. The return value is a raw syntax descriptor: a
1140 cons cell (CODE . MATCHING-CHAR) which can be used, for example, as
1141 the value of a `syntax-table' text property. */)
1142 (Lisp_Object string)
1144 const unsigned char *p;
1145 int val;
1146 Lisp_Object match;
1148 CHECK_STRING (string);
1150 p = SDATA (string);
1151 val = syntax_spec_code[*p++];
1152 if (val == 0377)
1153 error ("Invalid syntax description letter: %c", p[-1]);
1155 if (val == Sinherit)
1156 return Qnil;
1158 if (*p)
1160 int len;
1161 int character = STRING_CHAR_AND_LENGTH (p, len);
1162 XSETINT (match, character);
1163 if (XFASTINT (match) == ' ')
1164 match = Qnil;
1165 p += len;
1167 else
1168 match = Qnil;
1170 while (*p)
1171 switch (*p++)
1173 case '1':
1174 val |= 1 << 16;
1175 break;
1177 case '2':
1178 val |= 1 << 17;
1179 break;
1181 case '3':
1182 val |= 1 << 18;
1183 break;
1185 case '4':
1186 val |= 1 << 19;
1187 break;
1189 case 'p':
1190 val |= 1 << 20;
1191 break;
1193 case 'b':
1194 val |= 1 << 21;
1195 break;
1197 case 'n':
1198 val |= 1 << 22;
1199 break;
1201 case 'c':
1202 val |= 1 << 23;
1203 break;
1206 if (val < ASIZE (Vsyntax_code_object) && NILP (match))
1207 return AREF (Vsyntax_code_object, val);
1208 else
1209 /* Since we can't use a shared object, let's make a new one. */
1210 return Fcons (make_number (val), match);
1213 /* I really don't know why this is interactive
1214 help-form should at least be made useful whilst reading the second arg. */
1215 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
1216 "cSet syntax for character: \nsSet syntax for %s to: ",
1217 doc: /* Set syntax for character CHAR according to string NEWENTRY.
1218 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1219 the current buffer's syntax table.
1220 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1221 in the range MIN to MAX are changed.
1222 The first character of NEWENTRY should be one of the following:
1223 Space or - whitespace syntax. w word constituent.
1224 _ symbol constituent. . punctuation.
1225 ( open-parenthesis. ) close-parenthesis.
1226 " string quote. \\ escape.
1227 $ paired delimiter. \\=' expression quote or prefix operator.
1228 < comment starter. > comment ender.
1229 / character-quote. @ inherit from parent table.
1230 | generic string fence. ! generic comment fence.
1232 Only single-character comment start and end sequences are represented thus.
1233 Two-character sequences are represented as described below.
1234 The second character of NEWENTRY is the matching parenthesis,
1235 used only if the first character is `(' or `)'.
1236 Any additional characters are flags.
1237 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1238 1 means CHAR is the start of a two-char comment start sequence.
1239 2 means CHAR is the second character of such a sequence.
1240 3 means CHAR is the start of a two-char comment end sequence.
1241 4 means CHAR is the second character of such a sequence.
1243 There can be several orthogonal comment sequences. This is to support
1244 language modes such as C++. By default, all comment sequences are of style
1245 a, but you can set the comment sequence style to b (on the second character
1246 of a comment-start, and the first character of a comment-end sequence) and/or
1247 c (on any of its chars) using this flag:
1248 b means CHAR is part of comment sequence b.
1249 c means CHAR is part of comment sequence c.
1250 n means CHAR is part of a nestable comment sequence.
1252 p means CHAR is a prefix character for `backward-prefix-chars';
1253 such characters are treated as whitespace when they occur
1254 between expressions.
1255 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1256 (Lisp_Object c, Lisp_Object newentry, Lisp_Object syntax_table)
1258 if (CONSP (c))
1260 CHECK_CHARACTER_CAR (c);
1261 CHECK_CHARACTER_CDR (c);
1263 else
1264 CHECK_CHARACTER (c);
1266 if (NILP (syntax_table))
1267 syntax_table = BVAR (current_buffer, syntax_table);
1268 else
1269 check_syntax_table (syntax_table);
1271 newentry = Fstring_to_syntax (newentry);
1272 if (CONSP (c))
1273 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1274 else
1275 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
1277 /* We clear the regexp cache, since character classes can now have
1278 different values from those in the compiled regexps.*/
1279 clear_regexp_cache ();
1281 return Qnil;
1284 /* Dump syntax table to buffer in human-readable format */
1286 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1287 Sinternal_describe_syntax_value, 1, 1, 0,
1288 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1289 (Lisp_Object syntax)
1291 int code, syntax_code;
1292 bool start1, start2, end1, end2, prefix, comstyleb, comstylec, comnested;
1293 char str[2];
1294 Lisp_Object first, match_lisp, value = syntax;
1296 if (NILP (value))
1298 insert_string ("default");
1299 return syntax;
1302 if (CHAR_TABLE_P (value))
1304 insert_string ("deeper char-table ...");
1305 return syntax;
1308 if (!CONSP (value))
1310 insert_string ("invalid");
1311 return syntax;
1314 first = XCAR (value);
1315 match_lisp = XCDR (value);
1317 if (!INTEGERP (first) || !(NILP (match_lisp) || CHARACTERP (match_lisp)))
1319 insert_string ("invalid");
1320 return syntax;
1323 syntax_code = XINT (first) & INT_MAX;
1324 code = syntax_code & 0377;
1325 start1 = SYNTAX_FLAGS_COMSTART_FIRST (syntax_code);
1326 start2 = SYNTAX_FLAGS_COMSTART_SECOND (syntax_code);
1327 end1 = SYNTAX_FLAGS_COMEND_FIRST (syntax_code);
1328 end2 = SYNTAX_FLAGS_COMEND_SECOND (syntax_code);
1329 prefix = SYNTAX_FLAGS_PREFIX (syntax_code);
1330 comstyleb = SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code);
1331 comstylec = SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code);
1332 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax_code);
1334 if (Smax <= code)
1336 insert_string ("invalid");
1337 return syntax;
1340 str[0] = syntax_code_spec[code], str[1] = 0;
1341 insert (str, 1);
1343 if (NILP (match_lisp))
1344 insert (" ", 1);
1345 else
1346 insert_char (XINT (match_lisp));
1348 if (start1)
1349 insert ("1", 1);
1350 if (start2)
1351 insert ("2", 1);
1353 if (end1)
1354 insert ("3", 1);
1355 if (end2)
1356 insert ("4", 1);
1358 if (prefix)
1359 insert ("p", 1);
1360 if (comstyleb)
1361 insert ("b", 1);
1362 if (comstylec)
1363 insert ("c", 1);
1364 if (comnested)
1365 insert ("n", 1);
1367 insert_string ("\twhich means: ");
1369 switch (code)
1371 case Swhitespace:
1372 insert_string ("whitespace"); break;
1373 case Spunct:
1374 insert_string ("punctuation"); break;
1375 case Sword:
1376 insert_string ("word"); break;
1377 case Ssymbol:
1378 insert_string ("symbol"); break;
1379 case Sopen:
1380 insert_string ("open"); break;
1381 case Sclose:
1382 insert_string ("close"); break;
1383 case Squote:
1384 insert_string ("prefix"); break;
1385 case Sstring:
1386 insert_string ("string"); break;
1387 case Smath:
1388 insert_string ("math"); break;
1389 case Sescape:
1390 insert_string ("escape"); break;
1391 case Scharquote:
1392 insert_string ("charquote"); break;
1393 case Scomment:
1394 insert_string ("comment"); break;
1395 case Sendcomment:
1396 insert_string ("endcomment"); break;
1397 case Sinherit:
1398 insert_string ("inherit"); break;
1399 case Scomment_fence:
1400 insert_string ("comment fence"); break;
1401 case Sstring_fence:
1402 insert_string ("string fence"); break;
1403 default:
1404 insert_string ("invalid");
1405 return syntax;
1408 if (!NILP (match_lisp))
1410 insert_string (", matches ");
1411 insert_char (XINT (match_lisp));
1414 if (start1)
1415 insert_string (",\n\t is the first character of a comment-start sequence");
1416 if (start2)
1417 insert_string (",\n\t is the second character of a comment-start sequence");
1419 if (end1)
1420 insert_string (",\n\t is the first character of a comment-end sequence");
1421 if (end2)
1422 insert_string (",\n\t is the second character of a comment-end sequence");
1423 if (comstyleb)
1424 insert_string (" (comment style b)");
1425 if (comstylec)
1426 insert_string (" (comment style c)");
1427 if (comnested)
1428 insert_string (" (nestable)");
1430 if (prefix)
1432 AUTO_STRING (prefixdoc,
1433 ",\n\t is a prefix character for `backward-prefix-chars'");
1434 insert1 (Fsubstitute_command_keys (prefixdoc));
1437 return syntax;
1440 /* Return the position across COUNT words from FROM.
1441 If that many words cannot be found before the end of the buffer, return 0.
1442 COUNT negative means scan backward and stop at word beginning. */
1444 ptrdiff_t
1445 scan_words (ptrdiff_t from, EMACS_INT count)
1447 ptrdiff_t beg = BEGV;
1448 ptrdiff_t end = ZV;
1449 ptrdiff_t from_byte = CHAR_TO_BYTE (from);
1450 enum syntaxcode code;
1451 int ch0, ch1;
1452 Lisp_Object func, pos;
1454 SETUP_SYNTAX_TABLE (from, count);
1456 while (count > 0)
1458 while (true)
1460 if (from == end)
1461 return 0;
1462 UPDATE_SYNTAX_TABLE_FORWARD (from);
1463 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1464 code = SYNTAX (ch0);
1465 INC_BOTH (from, from_byte);
1466 if (words_include_escapes
1467 && (code == Sescape || code == Scharquote))
1468 break;
1469 if (code == Sword)
1470 break;
1471 rarely_quit (from);
1473 /* Now CH0 is a character which begins a word and FROM is the
1474 position of the next character. */
1475 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
1476 if (! NILP (Ffboundp (func)))
1478 pos = call2 (func, make_number (from - 1), make_number (end));
1479 if (INTEGERP (pos) && from < XINT (pos) && XINT (pos) <= ZV)
1481 from = XINT (pos);
1482 from_byte = CHAR_TO_BYTE (from);
1485 else
1487 while (1)
1489 if (from == end) break;
1490 UPDATE_SYNTAX_TABLE_FORWARD (from);
1491 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1492 code = SYNTAX (ch1);
1493 if ((code != Sword
1494 && (! words_include_escapes
1495 || (code != Sescape && code != Scharquote)))
1496 || word_boundary_p (ch0, ch1))
1497 break;
1498 INC_BOTH (from, from_byte);
1499 ch0 = ch1;
1500 rarely_quit (from);
1503 count--;
1505 while (count < 0)
1507 while (true)
1509 if (from == beg)
1510 return 0;
1511 DEC_BOTH (from, from_byte);
1512 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1513 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1514 code = SYNTAX (ch1);
1515 if (words_include_escapes
1516 && (code == Sescape || code == Scharquote))
1517 break;
1518 if (code == Sword)
1519 break;
1520 rarely_quit (from);
1522 /* Now CH1 is a character which ends a word and FROM is the
1523 position of it. */
1524 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
1525 if (! NILP (Ffboundp (func)))
1527 pos = call2 (func, make_number (from), make_number (beg));
1528 if (INTEGERP (pos) && BEGV <= XINT (pos) && XINT (pos) < from)
1530 from = XINT (pos);
1531 from_byte = CHAR_TO_BYTE (from);
1534 else
1536 while (1)
1538 if (from == beg)
1539 break;
1540 DEC_BOTH (from, from_byte);
1541 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1542 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1543 code = SYNTAX (ch0);
1544 if ((code != Sword
1545 && (! words_include_escapes
1546 || (code != Sescape && code != Scharquote)))
1547 || word_boundary_p (ch0, ch1))
1549 INC_BOTH (from, from_byte);
1550 break;
1552 ch1 = ch0;
1553 rarely_quit (from);
1556 count++;
1559 return from;
1562 DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p",
1563 doc: /* Move point forward ARG words (backward if ARG is negative).
1564 If ARG is omitted or nil, move point forward one word.
1565 Normally returns t.
1566 If an edge of the buffer or a field boundary is reached, point is
1567 left there and the function returns nil. Field boundaries are not
1568 noticed if `inhibit-field-text-motion' is non-nil.
1570 The word boundaries are normally determined by the buffer's syntax
1571 table, but `find-word-boundary-function-table', such as set up
1572 by `subword-mode', can change that. If a Lisp program needs to
1573 move by words determined strictly by the syntax table, it should
1574 use `forward-word-strictly' instead. */)
1575 (Lisp_Object arg)
1577 Lisp_Object tmp;
1578 ptrdiff_t orig_val, val;
1580 if (NILP (arg))
1581 XSETFASTINT (arg, 1);
1582 else
1583 CHECK_NUMBER (arg);
1585 val = orig_val = scan_words (PT, XINT (arg));
1586 if (! orig_val)
1587 val = XINT (arg) > 0 ? ZV : BEGV;
1589 /* Avoid jumping out of an input field. */
1590 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1591 Qnil, Qnil, Qnil);
1592 val = XFASTINT (tmp);
1594 SET_PT (val);
1595 return val == orig_val ? Qt : Qnil;
1598 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1599 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1600 STRING is like the inside of a `[...]' in a regular expression
1601 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1602 (but not at the end of a range; quoting is never needed there).
1603 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1604 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1605 Char classes, e.g. `[:alpha:]', are supported.
1607 Returns the distance traveled, either zero or positive. */)
1608 (Lisp_Object string, Lisp_Object lim)
1610 return skip_chars (1, string, lim, 1);
1613 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1614 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1615 See `skip-chars-forward' for details.
1616 Returns the distance traveled, either zero or negative. */)
1617 (Lisp_Object string, Lisp_Object lim)
1619 return skip_chars (0, string, lim, 1);
1622 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1623 doc: /* Move point forward across chars in specified syntax classes.
1624 SYNTAX is a string of syntax code characters.
1625 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1626 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1627 This function returns the distance traveled, either zero or positive. */)
1628 (Lisp_Object syntax, Lisp_Object lim)
1630 return skip_syntaxes (1, syntax, lim);
1633 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1634 doc: /* Move point backward across chars in specified syntax classes.
1635 SYNTAX is a string of syntax code characters.
1636 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1637 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1638 This function returns either zero or a negative number, and the absolute value
1639 of this is the distance traveled. */)
1640 (Lisp_Object syntax, Lisp_Object lim)
1642 return skip_syntaxes (0, syntax, lim);
1645 static Lisp_Object
1646 skip_chars (bool forwardp, Lisp_Object string, Lisp_Object lim,
1647 bool handle_iso_classes)
1649 int c;
1650 char fastmap[0400];
1651 /* Store the ranges of non-ASCII characters. */
1652 int *char_ranges UNINIT;
1653 int n_char_ranges = 0;
1654 bool negate = 0;
1655 ptrdiff_t i, i_byte;
1656 /* True if the current buffer is multibyte and the region contains
1657 non-ASCII chars. */
1658 bool multibyte;
1659 /* True if STRING is multibyte and it contains non-ASCII chars. */
1660 bool string_multibyte;
1661 ptrdiff_t size_byte;
1662 const unsigned char *str;
1663 int len;
1664 Lisp_Object iso_classes;
1665 USE_SAFE_ALLOCA;
1667 CHECK_STRING (string);
1668 iso_classes = Qnil;
1670 if (NILP (lim))
1671 XSETINT (lim, forwardp ? ZV : BEGV);
1672 else
1673 CHECK_NUMBER_COERCE_MARKER (lim);
1675 /* In any case, don't allow scan outside bounds of buffer. */
1676 if (XINT (lim) > ZV)
1677 XSETFASTINT (lim, ZV);
1678 if (XINT (lim) < BEGV)
1679 XSETFASTINT (lim, BEGV);
1681 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1682 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1683 string_multibyte = SBYTES (string) > SCHARS (string);
1685 memset (fastmap, 0, sizeof fastmap);
1687 str = SDATA (string);
1688 size_byte = SBYTES (string);
1690 i_byte = 0;
1691 if (i_byte < size_byte
1692 && SREF (string, 0) == '^')
1694 negate = 1; i_byte++;
1697 /* Find the characters specified and set their elements of fastmap.
1698 Handle backslashes and ranges specially.
1700 If STRING contains non-ASCII characters, setup char_ranges for
1701 them and use fastmap only for their leading codes. */
1703 if (! string_multibyte)
1705 bool string_has_eight_bit = 0;
1707 /* At first setup fastmap. */
1708 while (i_byte < size_byte)
1710 if (handle_iso_classes)
1712 const unsigned char *ch = str + i_byte;
1713 re_wctype_t cc = re_wctype_parse (&ch, size_byte - i_byte);
1714 if (cc == 0)
1715 error ("Invalid ISO C character class");
1716 if (cc != -1)
1718 iso_classes = Fcons (make_number (cc), iso_classes);
1719 i_byte = ch - str;
1720 continue;
1724 c = str[i_byte++];
1726 if (c == '\\')
1728 if (i_byte == size_byte)
1729 break;
1731 c = str[i_byte++];
1733 /* Treat `-' as range character only if another character
1734 follows. */
1735 if (i_byte + 1 < size_byte
1736 && str[i_byte] == '-')
1738 int c2;
1740 /* Skip over the dash. */
1741 i_byte++;
1743 /* Get the end of the range. */
1744 c2 = str[i_byte++];
1745 if (c2 == '\\'
1746 && i_byte < size_byte)
1747 c2 = str[i_byte++];
1749 if (c <= c2)
1751 int lim2 = c2 + 1;
1752 while (c < lim2)
1753 fastmap[c++] = 1;
1754 if (! ASCII_CHAR_P (c2))
1755 string_has_eight_bit = 1;
1758 else
1760 fastmap[c] = 1;
1761 if (! ASCII_CHAR_P (c))
1762 string_has_eight_bit = 1;
1766 /* If the current range is multibyte and STRING contains
1767 eight-bit chars, arrange fastmap and setup char_ranges for
1768 the corresponding multibyte chars. */
1769 if (multibyte && string_has_eight_bit)
1771 char *p1;
1772 char himap[0200 + 1];
1773 memcpy (himap, fastmap + 0200, 0200);
1774 himap[0200] = 0;
1775 memset (fastmap + 0200, 0, 0200);
1776 SAFE_NALLOCA (char_ranges, 2, 128);
1777 i = 0;
1779 while ((p1 = memchr (himap + i, 1, 0200 - i)))
1781 /* Deduce the next range C..C2 from the next clump of 1s
1782 in HIMAP starting with &HIMAP[I]. HIMAP is the high
1783 order half of the old FASTMAP. */
1784 int c2, leading_code;
1785 i = p1 - himap;
1786 c = BYTE8_TO_CHAR (i + 0200);
1787 i += strlen (p1);
1788 c2 = BYTE8_TO_CHAR (i + 0200 - 1);
1790 char_ranges[n_char_ranges++] = c;
1791 char_ranges[n_char_ranges++] = c2;
1792 leading_code = CHAR_LEADING_CODE (c);
1793 memset (fastmap + leading_code, 1,
1794 CHAR_LEADING_CODE (c2) - leading_code + 1);
1798 else /* STRING is multibyte */
1800 SAFE_NALLOCA (char_ranges, 2, SCHARS (string));
1802 while (i_byte < size_byte)
1804 int leading_code = str[i_byte];
1806 if (handle_iso_classes)
1808 const unsigned char *ch = str + i_byte;
1809 re_wctype_t cc = re_wctype_parse (&ch, size_byte - i_byte);
1810 if (cc == 0)
1811 error ("Invalid ISO C character class");
1812 if (cc != -1)
1814 iso_classes = Fcons (make_number (cc), iso_classes);
1815 i_byte = ch - str;
1816 continue;
1820 if (leading_code== '\\')
1822 if (++i_byte == size_byte)
1823 break;
1825 leading_code = str[i_byte];
1827 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1828 i_byte += len;
1831 /* Treat `-' as range character only if another character
1832 follows. */
1833 if (i_byte + 1 < size_byte
1834 && str[i_byte] == '-')
1836 int c2, leading_code2;
1838 /* Skip over the dash. */
1839 i_byte++;
1841 /* Get the end of the range. */
1842 leading_code2 = str[i_byte];
1843 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1844 i_byte += len;
1846 if (c2 == '\\'
1847 && i_byte < size_byte)
1849 leading_code2 = str[i_byte];
1850 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1851 i_byte += len;
1854 if (c > c2)
1855 continue;
1856 if (ASCII_CHAR_P (c))
1858 while (c <= c2 && c < 0x80)
1859 fastmap[c++] = 1;
1860 leading_code = CHAR_LEADING_CODE (c);
1862 if (! ASCII_CHAR_P (c))
1864 int lim2 = leading_code2 + 1;
1865 while (leading_code < lim2)
1866 fastmap[leading_code++] = 1;
1867 if (c <= c2)
1869 char_ranges[n_char_ranges++] = c;
1870 char_ranges[n_char_ranges++] = c2;
1874 else
1876 if (ASCII_CHAR_P (c))
1877 fastmap[c] = 1;
1878 else
1880 fastmap[leading_code] = 1;
1881 char_ranges[n_char_ranges++] = c;
1882 char_ranges[n_char_ranges++] = c;
1887 /* If the current range is unibyte and STRING contains non-ASCII
1888 chars, arrange fastmap for the corresponding unibyte
1889 chars. */
1891 if (! multibyte && n_char_ranges > 0)
1893 memset (fastmap + 0200, 0, 0200);
1894 for (i = 0; i < n_char_ranges; i += 2)
1896 int c1 = char_ranges[i];
1897 int lim2 = char_ranges[i + 1] + 1;
1899 for (; c1 < lim2; c1++)
1901 int b = CHAR_TO_BYTE_SAFE (c1);
1902 if (b >= 0)
1903 fastmap[b] = 1;
1909 /* If ^ was the first character, complement the fastmap. */
1910 if (negate)
1912 if (! multibyte)
1913 for (i = 0; i < sizeof fastmap; i++)
1914 fastmap[i] ^= 1;
1915 else
1917 for (i = 0; i < 0200; i++)
1918 fastmap[i] ^= 1;
1919 /* All non-ASCII chars possibly match. */
1920 for (; i < sizeof fastmap; i++)
1921 fastmap[i] = 1;
1926 ptrdiff_t start_point = PT;
1927 ptrdiff_t pos = PT;
1928 ptrdiff_t pos_byte = PT_BYTE;
1929 unsigned char *p = PT_ADDR, *endp, *stop;
1931 if (forwardp)
1933 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1934 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1936 else
1938 endp = CHAR_POS_ADDR (XINT (lim));
1939 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1942 /* This code may look up syntax tables using functions that rely on the
1943 gl_state object. To make sure this object is not out of date,
1944 let's initialize it manually.
1945 We ignore syntax-table text-properties for now, since that's
1946 what we've done in the past. */
1947 SETUP_BUFFER_SYNTAX_TABLE ();
1948 if (forwardp)
1950 if (multibyte)
1951 while (1)
1953 int nbytes;
1955 if (p >= stop)
1957 if (p >= endp)
1958 break;
1959 p = GAP_END_ADDR;
1960 stop = endp;
1962 c = STRING_CHAR_AND_LENGTH (p, nbytes);
1963 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1965 if (negate)
1966 break;
1967 else
1968 goto fwd_ok;
1971 if (! fastmap[*p])
1972 break;
1973 if (! ASCII_CHAR_P (c))
1975 /* As we are looking at a multibyte character, we
1976 must look up the character in the table
1977 CHAR_RANGES. If there's no data in the table,
1978 that character is not what we want to skip. */
1980 /* The following code do the right thing even if
1981 n_char_ranges is zero (i.e. no data in
1982 CHAR_RANGES). */
1983 for (i = 0; i < n_char_ranges; i += 2)
1984 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1985 break;
1986 if (!(negate ^ (i < n_char_ranges)))
1987 break;
1989 fwd_ok:
1990 p += nbytes, pos++, pos_byte += nbytes;
1991 rarely_quit (pos);
1993 else
1994 while (true)
1996 if (p >= stop)
1998 if (p >= endp)
1999 break;
2000 p = GAP_END_ADDR;
2001 stop = endp;
2004 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
2006 if (negate)
2007 break;
2008 else
2009 goto fwd_unibyte_ok;
2012 if (!fastmap[*p])
2013 break;
2014 fwd_unibyte_ok:
2015 p++, pos++, pos_byte++;
2016 rarely_quit (pos);
2019 else
2021 if (multibyte)
2022 while (true)
2024 if (p <= stop)
2026 if (p <= endp)
2027 break;
2028 p = GPT_ADDR;
2029 stop = endp;
2031 unsigned char *prev_p = p;
2033 p--;
2034 while (stop <= p && ! CHAR_HEAD_P (*p));
2036 c = STRING_CHAR (p);
2038 if (! NILP (iso_classes) && in_classes (c, iso_classes))
2040 if (negate)
2041 break;
2042 else
2043 goto back_ok;
2046 if (! fastmap[*p])
2047 break;
2048 if (! ASCII_CHAR_P (c))
2050 /* See the comment in the previous similar code. */
2051 for (i = 0; i < n_char_ranges; i += 2)
2052 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
2053 break;
2054 if (!(negate ^ (i < n_char_ranges)))
2055 break;
2057 back_ok:
2058 pos--, pos_byte -= prev_p - p;
2059 rarely_quit (pos);
2061 else
2062 while (true)
2064 if (p <= stop)
2066 if (p <= endp)
2067 break;
2068 p = GPT_ADDR;
2069 stop = endp;
2072 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
2074 if (negate)
2075 break;
2076 else
2077 goto back_unibyte_ok;
2080 if (!fastmap[p[-1]])
2081 break;
2082 back_unibyte_ok:
2083 p--, pos--, pos_byte--;
2084 rarely_quit (pos);
2088 SET_PT_BOTH (pos, pos_byte);
2090 SAFE_FREE ();
2091 return make_number (PT - start_point);
2096 static Lisp_Object
2097 skip_syntaxes (bool forwardp, Lisp_Object string, Lisp_Object lim)
2099 int c;
2100 unsigned char fastmap[0400];
2101 bool negate = 0;
2102 ptrdiff_t i, i_byte;
2103 bool multibyte;
2104 ptrdiff_t size_byte;
2105 unsigned char *str;
2107 CHECK_STRING (string);
2109 if (NILP (lim))
2110 XSETINT (lim, forwardp ? ZV : BEGV);
2111 else
2112 CHECK_NUMBER_COERCE_MARKER (lim);
2114 /* In any case, don't allow scan outside bounds of buffer. */
2115 if (XINT (lim) > ZV)
2116 XSETFASTINT (lim, ZV);
2117 if (XINT (lim) < BEGV)
2118 XSETFASTINT (lim, BEGV);
2120 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
2121 return make_number (0);
2123 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
2124 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
2126 memset (fastmap, 0, sizeof fastmap);
2128 if (SBYTES (string) > SCHARS (string))
2129 /* As this is very rare case (syntax spec is ASCII only), don't
2130 consider efficiency. */
2131 string = string_make_unibyte (string);
2133 str = SDATA (string);
2134 size_byte = SBYTES (string);
2136 i_byte = 0;
2137 if (i_byte < size_byte
2138 && SREF (string, 0) == '^')
2140 negate = 1; i_byte++;
2143 /* Find the syntaxes specified and set their elements of fastmap. */
2145 while (i_byte < size_byte)
2147 c = str[i_byte++];
2148 fastmap[syntax_spec_code[c]] = 1;
2151 /* If ^ was the first character, complement the fastmap. */
2152 if (negate)
2153 for (i = 0; i < sizeof fastmap; i++)
2154 fastmap[i] ^= 1;
2157 ptrdiff_t start_point = PT;
2158 ptrdiff_t pos = PT;
2159 ptrdiff_t pos_byte = PT_BYTE;
2160 unsigned char *p, *endp, *stop;
2162 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
2164 if (forwardp)
2166 while (true)
2168 p = BYTE_POS_ADDR (pos_byte);
2169 endp = XINT (lim) == GPT ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
2170 stop = pos < GPT && GPT < XINT (lim) ? GPT_ADDR : endp;
2174 int nbytes;
2176 if (p >= stop)
2178 if (p >= endp)
2179 goto done;
2180 p = GAP_END_ADDR;
2181 stop = endp;
2183 if (multibyte)
2184 c = STRING_CHAR_AND_LENGTH (p, nbytes);
2185 else
2186 c = *p, nbytes = 1;
2187 if (! fastmap[SYNTAX (c)])
2188 goto done;
2189 p += nbytes, pos++, pos_byte += nbytes;
2190 rarely_quit (pos);
2192 while (!parse_sexp_lookup_properties
2193 || pos < gl_state.e_property);
2195 update_syntax_table_forward (pos + gl_state.offset,
2196 false, gl_state.object);
2199 else
2201 p = BYTE_POS_ADDR (pos_byte);
2202 endp = CHAR_POS_ADDR (XINT (lim));
2203 stop = pos >= GPT && GPT > XINT (lim) ? GAP_END_ADDR : endp;
2205 if (multibyte)
2207 while (true)
2209 if (p <= stop)
2211 if (p <= endp)
2212 break;
2213 p = GPT_ADDR;
2214 stop = endp;
2216 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2218 unsigned char *prev_p = p;
2220 p--;
2221 while (stop <= p && ! CHAR_HEAD_P (*p));
2223 c = STRING_CHAR (p);
2224 if (! fastmap[SYNTAX (c)])
2225 break;
2226 pos--, pos_byte -= prev_p - p;
2227 rarely_quit (pos);
2230 else
2232 while (true)
2234 if (p <= stop)
2236 if (p <= endp)
2237 break;
2238 p = GPT_ADDR;
2239 stop = endp;
2241 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2242 if (! fastmap[SYNTAX (p[-1])])
2243 break;
2244 p--, pos--, pos_byte--;
2245 rarely_quit (pos);
2250 done:
2251 SET_PT_BOTH (pos, pos_byte);
2253 return make_number (PT - start_point);
2257 /* Return true if character C belongs to one of the ISO classes
2258 in the list ISO_CLASSES. Each class is represented by an
2259 integer which is its type according to re_wctype. */
2261 static bool
2262 in_classes (int c, Lisp_Object iso_classes)
2264 bool fits_class = 0;
2266 while (CONSP (iso_classes))
2268 Lisp_Object elt;
2269 elt = XCAR (iso_classes);
2270 iso_classes = XCDR (iso_classes);
2272 if (re_iswctype (c, XFASTINT (elt)))
2273 fits_class = 1;
2276 return fits_class;
2279 /* Jump over a comment, assuming we are at the beginning of one.
2280 FROM is the current position.
2281 FROM_BYTE is the bytepos corresponding to FROM.
2282 Do not move past STOP (a charpos).
2283 The comment over which we have to jump is of style STYLE
2284 (either SYNTAX_FLAGS_COMMENT_STYLE (foo) or ST_COMMENT_STYLE).
2285 NESTING should be positive to indicate the nesting at the beginning
2286 for nested comments and should be zero or negative else.
2287 ST_COMMENT_STYLE cannot be nested.
2288 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2289 (or 0 If the search cannot start in the middle of a two-character).
2291 If successful, return true and store the charpos of the comment's
2292 end into *CHARPOS_PTR and the corresponding bytepos into
2293 *BYTEPOS_PTR. Else, return false and store the charpos STOP into
2294 *CHARPOS_PTR, the corresponding bytepos into *BYTEPOS_PTR and the
2295 current nesting (as defined for state->incomment) in
2296 *INCOMMENT_PTR. Should the last character scanned in an incomplete
2297 comment be a possible first character of a two character construct,
2298 we store its SYNTAX_WITH_FLAGS into *last_syntax_ptr. Otherwise,
2299 we store Smax into *last_syntax_ptr.
2301 The comment end is the last character of the comment rather than the
2302 character just after the comment.
2304 Global syntax data is assumed to initially be valid for FROM and
2305 remains valid for forward search starting at the returned position. */
2307 static bool
2308 forw_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop,
2309 EMACS_INT nesting, int style, int prev_syntax,
2310 ptrdiff_t *charpos_ptr, ptrdiff_t *bytepos_ptr,
2311 EMACS_INT *incomment_ptr, int *last_syntax_ptr)
2313 unsigned short int quit_count = 0;
2314 int c, c1;
2315 enum syntaxcode code;
2316 int syntax, other_syntax;
2318 if (nesting <= 0) nesting = -1;
2320 /* Enter the loop in the middle so that we find
2321 a 2-char comment ender if we start in the middle of it. */
2322 syntax = prev_syntax;
2323 code = syntax & 0xff;
2324 if (syntax != 0 && from < stop) goto forw_incomment;
2326 while (1)
2328 if (from == stop)
2330 *incomment_ptr = nesting;
2331 *charpos_ptr = from;
2332 *bytepos_ptr = from_byte;
2333 *last_syntax_ptr =
2334 (code == Sescape || code == Scharquote
2335 || SYNTAX_FLAGS_COMEND_FIRST (syntax)
2336 || (nesting > 0
2337 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)))
2338 ? syntax : Smax ;
2339 return 0;
2341 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2342 syntax = SYNTAX_WITH_FLAGS (c);
2343 code = syntax & 0xff;
2344 if (code == Sendcomment
2345 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style
2346 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2347 (nesting > 0 && --nesting == 0) : nesting < 0)
2348 && !(Vcomment_end_can_be_escaped && char_quoted (from, from_byte)))
2349 /* We have encountered a comment end of the same style
2350 as the comment sequence which began this comment
2351 section. */
2352 break;
2353 if (code == Scomment_fence
2354 && style == ST_COMMENT_STYLE)
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 (nesting > 0
2360 && code == Scomment
2361 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2362 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style)
2363 /* We have encountered a nested comment of the same style
2364 as the comment sequence which began this comment section. */
2365 nesting++;
2366 INC_BOTH (from, from_byte);
2367 UPDATE_SYNTAX_TABLE_FORWARD (from);
2369 forw_incomment:
2370 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2371 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2372 other_syntax = SYNTAX_WITH_FLAGS (c1),
2373 SYNTAX_FLAGS_COMEND_SECOND (other_syntax))
2374 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, other_syntax) == style
2375 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2376 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax))
2377 ? nesting > 0 : nesting < 0))
2379 syntax = Smax; /* So that "|#" (lisp) can not return
2380 the syntax of "#" in *last_syntax_ptr. */
2381 if (--nesting <= 0)
2382 /* We have encountered a comment end of the same style
2383 as the comment sequence which began this comment section. */
2384 break;
2385 else
2387 INC_BOTH (from, from_byte);
2388 UPDATE_SYNTAX_TABLE_FORWARD (from);
2391 if (nesting > 0
2392 && from < stop
2393 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2394 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2395 other_syntax = SYNTAX_WITH_FLAGS (c1),
2396 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax) == style
2397 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2398 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2399 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax)))
2400 /* We have encountered a nested comment of the same style
2401 as the comment sequence which began this comment section. */
2403 syntax = Smax; /* So that "#|#" isn't also a comment ender. */
2404 INC_BOTH (from, from_byte);
2405 UPDATE_SYNTAX_TABLE_FORWARD (from);
2406 nesting++;
2409 rarely_quit (++quit_count);
2411 *charpos_ptr = from;
2412 *bytepos_ptr = from_byte;
2413 *last_syntax_ptr = Smax; /* Any syntactic power the last byte had is
2414 used up. */
2415 return 1;
2418 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2419 doc: /*
2420 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2421 Stop scanning if we find something other than a comment or whitespace.
2422 Set point to where scanning stops.
2423 If COUNT comments are found as expected, with nothing except whitespace
2424 between them, return t; otherwise return nil. */)
2425 (Lisp_Object count)
2427 ptrdiff_t from, from_byte, stop;
2428 int c, c1;
2429 enum syntaxcode code;
2430 int comstyle = 0; /* style of comment encountered */
2431 bool comnested = 0; /* whether the comment is nestable or not */
2432 bool found;
2433 EMACS_INT count1;
2434 ptrdiff_t out_charpos, out_bytepos;
2435 EMACS_INT dummy;
2436 int dummy2;
2437 unsigned short int quit_count = 0;
2439 CHECK_NUMBER (count);
2440 count1 = XINT (count);
2441 stop = count1 > 0 ? ZV : BEGV;
2443 from = PT;
2444 from_byte = PT_BYTE;
2446 SETUP_SYNTAX_TABLE (from, count1);
2447 while (count1 > 0)
2451 bool comstart_first;
2452 int syntax, other_syntax;
2454 if (from == stop)
2456 SET_PT_BOTH (from, from_byte);
2457 return Qnil;
2459 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2460 syntax = SYNTAX_WITH_FLAGS (c);
2461 code = SYNTAX (c);
2462 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2463 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2464 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2465 INC_BOTH (from, from_byte);
2466 UPDATE_SYNTAX_TABLE_FORWARD (from);
2467 if (from < stop && comstart_first
2468 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2469 other_syntax = SYNTAX_WITH_FLAGS (c1),
2470 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)))
2472 /* We have encountered a comment start sequence and we
2473 are ignoring all text inside comments. We must record
2474 the comment style this sequence begins so that later,
2475 only a comment end of the same style actually ends
2476 the comment section. */
2477 code = Scomment;
2478 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2479 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2480 INC_BOTH (from, from_byte);
2481 UPDATE_SYNTAX_TABLE_FORWARD (from);
2483 rarely_quit (++quit_count);
2485 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2487 if (code == Scomment_fence)
2488 comstyle = ST_COMMENT_STYLE;
2489 else if (code != Scomment)
2491 DEC_BOTH (from, from_byte);
2492 SET_PT_BOTH (from, from_byte);
2493 return Qnil;
2495 /* We're at the start of a comment. */
2496 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2497 &out_charpos, &out_bytepos, &dummy, &dummy2);
2498 from = out_charpos; from_byte = out_bytepos;
2499 if (!found)
2501 SET_PT_BOTH (from, from_byte);
2502 return Qnil;
2504 INC_BOTH (from, from_byte);
2505 UPDATE_SYNTAX_TABLE_FORWARD (from);
2506 /* We have skipped one comment. */
2507 count1--;
2510 while (count1 < 0)
2512 while (true)
2514 if (from <= stop)
2516 SET_PT_BOTH (BEGV, BEGV_BYTE);
2517 return Qnil;
2520 DEC_BOTH (from, from_byte);
2521 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2522 bool quoted = char_quoted (from, from_byte);
2523 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2524 int syntax = SYNTAX_WITH_FLAGS (c);
2525 code = SYNTAX (c);
2526 comstyle = 0;
2527 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2528 if (code == Sendcomment)
2529 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2530 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2531 && prev_char_comend_first (from, from_byte)
2532 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2534 int other_syntax;
2535 /* We must record the comment style encountered so that
2536 later, we can match only the proper comment begin
2537 sequence of the same style. */
2538 DEC_BOTH (from, from_byte);
2539 code = Sendcomment;
2540 /* Calling char_quoted, above, set up global syntax position
2541 at the new value of FROM. */
2542 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2543 other_syntax = SYNTAX_WITH_FLAGS (c1);
2544 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2545 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2548 if (code == Scomment_fence)
2550 /* Skip until first preceding unquoted comment_fence. */
2551 bool fence_found = 0;
2552 ptrdiff_t ini = from, ini_byte = from_byte;
2554 while (1)
2556 DEC_BOTH (from, from_byte);
2557 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2558 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2559 if (SYNTAX (c) == Scomment_fence
2560 && !char_quoted (from, from_byte))
2562 fence_found = 1;
2563 break;
2565 else if (from == stop)
2566 break;
2567 rarely_quit (++quit_count);
2569 if (fence_found == 0)
2571 from = ini; /* Set point to ini + 1. */
2572 from_byte = ini_byte;
2573 goto leave;
2575 else
2576 /* We have skipped one comment. */
2577 break;
2579 else if (code == Sendcomment)
2581 found = back_comment (from, from_byte, stop, comnested, comstyle,
2582 &out_charpos, &out_bytepos);
2583 if (!found)
2585 if (c == '\n')
2586 /* This end-of-line is not an end-of-comment.
2587 Treat it like a whitespace.
2588 CC-mode (and maybe others) relies on this behavior. */
2590 else
2592 /* Failure: we should go back to the end of this
2593 not-quite-endcomment. */
2594 if (SYNTAX (c) != code)
2595 /* It was a two-char Sendcomment. */
2596 INC_BOTH (from, from_byte);
2597 goto leave;
2600 else
2602 /* We have skipped one comment. */
2603 from = out_charpos, from_byte = out_bytepos;
2604 break;
2607 else if (code != Swhitespace || quoted)
2609 leave:
2610 INC_BOTH (from, from_byte);
2611 SET_PT_BOTH (from, from_byte);
2612 return Qnil;
2615 rarely_quit (++quit_count);
2618 count1++;
2621 SET_PT_BOTH (from, from_byte);
2622 return Qt;
2625 /* Return syntax code of character C if C is an ASCII character
2626 or if MULTIBYTE_SYMBOL_P is false. Otherwise, return Ssymbol. */
2628 static enum syntaxcode
2629 syntax_multibyte (int c, bool multibyte_symbol_p)
2631 return ASCII_CHAR_P (c) || !multibyte_symbol_p ? SYNTAX (c) : Ssymbol;
2634 static Lisp_Object
2635 scan_lists (EMACS_INT from, EMACS_INT count, EMACS_INT depth, bool sexpflag)
2637 Lisp_Object val;
2638 ptrdiff_t stop = count > 0 ? ZV : BEGV;
2639 int c, c1;
2640 int stringterm;
2641 bool quoted;
2642 bool mathexit = 0;
2643 enum syntaxcode code;
2644 EMACS_INT min_depth = depth; /* Err out if depth gets less than this. */
2645 int comstyle = 0; /* Style of comment encountered. */
2646 bool comnested = 0; /* Whether the comment is nestable or not. */
2647 ptrdiff_t temp_pos;
2648 EMACS_INT last_good = from;
2649 bool found;
2650 ptrdiff_t from_byte;
2651 ptrdiff_t out_bytepos, out_charpos;
2652 EMACS_INT dummy;
2653 int dummy2;
2654 bool multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2655 unsigned short int quit_count = 0;
2657 if (depth > 0) min_depth = 0;
2659 if (from > ZV) from = ZV;
2660 if (from < BEGV) from = BEGV;
2662 from_byte = CHAR_TO_BYTE (from);
2664 maybe_quit ();
2666 SETUP_SYNTAX_TABLE (from, count);
2667 while (count > 0)
2669 while (from < stop)
2671 rarely_quit (++quit_count);
2672 bool comstart_first, prefix;
2673 int syntax, other_syntax;
2674 UPDATE_SYNTAX_TABLE_FORWARD (from);
2675 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2676 syntax = SYNTAX_WITH_FLAGS (c);
2677 code = syntax_multibyte (c, multibyte_symbol_p);
2678 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2679 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2680 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2681 prefix = SYNTAX_FLAGS_PREFIX (syntax);
2682 if (depth == min_depth)
2683 last_good = from;
2684 INC_BOTH (from, from_byte);
2685 UPDATE_SYNTAX_TABLE_FORWARD (from);
2686 if (from < stop && comstart_first
2687 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2688 other_syntax = SYNTAX_WITH_FLAGS (c),
2689 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2690 && parse_sexp_ignore_comments)
2692 /* We have encountered a comment start sequence and we
2693 are ignoring all text inside comments. We must record
2694 the comment style this sequence begins so that later,
2695 only a comment end of the same style actually ends
2696 the comment section. */
2697 code = Scomment;
2698 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2699 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2700 INC_BOTH (from, from_byte);
2701 UPDATE_SYNTAX_TABLE_FORWARD (from);
2704 if (prefix)
2705 continue;
2707 switch (code)
2709 case Sescape:
2710 case Scharquote:
2711 if (from == stop)
2712 goto lose;
2713 INC_BOTH (from, from_byte);
2714 /* Treat following character as a word constituent. */
2715 FALLTHROUGH;
2716 case Sword:
2717 case Ssymbol:
2718 if (depth || !sexpflag) break;
2719 /* This word counts as a sexp; return at end of it. */
2720 while (from < stop)
2722 UPDATE_SYNTAX_TABLE_FORWARD (from);
2724 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2725 switch (syntax_multibyte (c, multibyte_symbol_p))
2727 case Scharquote:
2728 case Sescape:
2729 INC_BOTH (from, from_byte);
2730 if (from == stop)
2731 goto lose;
2732 break;
2733 case Sword:
2734 case Ssymbol:
2735 case Squote:
2736 break;
2737 default:
2738 goto done;
2740 INC_BOTH (from, from_byte);
2741 rarely_quit (++quit_count);
2743 goto done;
2745 case Scomment_fence:
2746 comstyle = ST_COMMENT_STYLE;
2747 FALLTHROUGH;
2748 case Scomment:
2749 if (!parse_sexp_ignore_comments) break;
2750 UPDATE_SYNTAX_TABLE_FORWARD (from);
2751 found = forw_comment (from, from_byte, stop,
2752 comnested, comstyle, 0,
2753 &out_charpos, &out_bytepos, &dummy,
2754 &dummy2);
2755 from = out_charpos, from_byte = out_bytepos;
2756 if (!found)
2758 if (depth == 0)
2759 goto done;
2760 goto lose;
2762 INC_BOTH (from, from_byte);
2763 UPDATE_SYNTAX_TABLE_FORWARD (from);
2764 break;
2766 case Smath:
2767 if (!sexpflag)
2768 break;
2769 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2771 INC_BOTH (from, from_byte);
2773 if (mathexit)
2775 mathexit = 0;
2776 goto close1;
2778 mathexit = 1;
2779 FALLTHROUGH;
2780 case Sopen:
2781 if (!++depth) goto done;
2782 break;
2784 case Sclose:
2785 close1:
2786 if (!--depth) goto done;
2787 if (depth < min_depth)
2788 xsignal3 (Qscan_error,
2789 build_string ("Containing expression ends prematurely"),
2790 make_number (last_good), make_number (from));
2791 break;
2793 case Sstring:
2794 case Sstring_fence:
2795 temp_pos = dec_bytepos (from_byte);
2796 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2797 while (1)
2799 enum syntaxcode c_code;
2800 if (from >= stop)
2801 goto lose;
2802 UPDATE_SYNTAX_TABLE_FORWARD (from);
2803 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2804 c_code = syntax_multibyte (c, multibyte_symbol_p);
2805 if (code == Sstring
2806 ? c == stringterm && c_code == Sstring
2807 : c_code == Sstring_fence)
2808 break;
2810 if (c_code == Scharquote || c_code == Sescape)
2811 INC_BOTH (from, from_byte);
2812 INC_BOTH (from, from_byte);
2813 rarely_quit (++quit_count);
2815 INC_BOTH (from, from_byte);
2816 if (!depth && sexpflag) goto done;
2817 break;
2818 default:
2819 /* Ignore whitespace, punctuation, quote, endcomment. */
2820 break;
2824 /* Reached end of buffer. Error if within object, return nil if between */
2825 if (depth)
2826 goto lose;
2828 return Qnil;
2830 /* End of object reached */
2831 done:
2832 count--;
2835 while (count < 0)
2837 while (from > stop)
2839 rarely_quit (++quit_count);
2840 DEC_BOTH (from, from_byte);
2841 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2842 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2843 int syntax = SYNTAX_WITH_FLAGS (c);
2844 code = syntax_multibyte (c, multibyte_symbol_p);
2845 if (depth == min_depth)
2846 last_good = from;
2847 comstyle = 0;
2848 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2849 if (code == Sendcomment)
2850 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2851 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2852 && prev_char_comend_first (from, from_byte)
2853 && parse_sexp_ignore_comments)
2855 /* We must record the comment style encountered so that
2856 later, we can match only the proper comment begin
2857 sequence of the same style. */
2858 int c2, other_syntax;
2859 DEC_BOTH (from, from_byte);
2860 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2861 code = Sendcomment;
2862 c2 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2863 other_syntax = SYNTAX_WITH_FLAGS (c2);
2864 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2865 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2868 /* Quoting turns anything except a comment-ender
2869 into a word character. Note that this cannot be true
2870 if we decremented FROM in the if-statement above. */
2871 if (code != Sendcomment && char_quoted (from, from_byte))
2873 DEC_BOTH (from, from_byte);
2874 code = Sword;
2876 else if (SYNTAX_FLAGS_PREFIX (syntax))
2877 continue;
2879 switch (code)
2881 case Sword:
2882 case Ssymbol:
2883 case Sescape:
2884 case Scharquote:
2885 if (depth || !sexpflag) break;
2886 /* This word counts as a sexp; count object finished
2887 after passing it. */
2888 while (from > stop)
2890 temp_pos = from_byte;
2891 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2892 DEC_POS (temp_pos);
2893 else
2894 temp_pos--;
2895 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2896 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2897 /* Don't allow comment-end to be quoted. */
2898 if (syntax_multibyte (c1, multibyte_symbol_p) == Sendcomment)
2899 goto done2;
2900 quoted = char_quoted (from - 1, temp_pos);
2901 if (quoted)
2903 DEC_BOTH (from, from_byte);
2904 temp_pos = dec_bytepos (temp_pos);
2905 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2907 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2908 if (! quoted)
2909 switch (syntax_multibyte (c1, multibyte_symbol_p))
2911 case Sword: case Ssymbol: case Squote: break;
2912 default: goto done2;
2914 DEC_BOTH (from, from_byte);
2915 rarely_quit (++quit_count);
2917 goto done2;
2919 case Smath:
2920 if (!sexpflag)
2921 break;
2922 if (from > BEGV)
2924 temp_pos = dec_bytepos (from_byte);
2925 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2926 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2927 DEC_BOTH (from, from_byte);
2929 if (mathexit)
2931 mathexit = 0;
2932 goto open2;
2934 mathexit = 1;
2935 FALLTHROUGH;
2936 case Sclose:
2937 if (!++depth) goto done2;
2938 break;
2940 case Sopen:
2941 open2:
2942 if (!--depth) goto done2;
2943 if (depth < min_depth)
2944 xsignal3 (Qscan_error,
2945 build_string ("Containing expression ends prematurely"),
2946 make_number (last_good), make_number (from));
2947 break;
2949 case Sendcomment:
2950 if (!parse_sexp_ignore_comments)
2951 break;
2952 found = back_comment (from, from_byte, stop, comnested, comstyle,
2953 &out_charpos, &out_bytepos);
2954 /* FIXME: if !found, it really wasn't a comment-end.
2955 For single-char Sendcomment, we can't do much about it apart
2956 from skipping the char.
2957 For 2-char endcomments, we could try again, taking both
2958 chars as separate entities, but it's a lot of trouble
2959 for very little gain, so we don't bother either. -sm */
2960 if (found)
2961 from = out_charpos, from_byte = out_bytepos;
2962 break;
2964 case Scomment_fence:
2965 case Sstring_fence:
2966 while (1)
2968 if (from == stop)
2969 goto lose;
2970 DEC_BOTH (from, from_byte);
2971 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2972 if (!char_quoted (from, from_byte))
2974 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2975 if (syntax_multibyte (c, multibyte_symbol_p) == code)
2976 break;
2978 rarely_quit (++quit_count);
2980 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2981 break;
2983 case Sstring:
2984 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2985 while (true)
2987 if (from == stop)
2988 goto lose;
2989 DEC_BOTH (from, from_byte);
2990 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2991 if (!char_quoted (from, from_byte))
2993 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2994 if (c == stringterm
2995 && (syntax_multibyte (c, multibyte_symbol_p)
2996 == Sstring))
2997 break;
2999 rarely_quit (++quit_count);
3001 if (!depth && sexpflag) goto done2;
3002 break;
3003 default:
3004 /* Ignore whitespace, punctuation, quote, endcomment. */
3005 break;
3009 /* Reached start of buffer. Error if within object, return nil if between */
3010 if (depth)
3011 goto lose;
3013 return Qnil;
3015 done2:
3016 count++;
3020 XSETFASTINT (val, from);
3021 return val;
3023 lose:
3024 xsignal3 (Qscan_error,
3025 build_string ("Unbalanced parentheses"),
3026 make_number (last_good), make_number (from));
3029 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
3030 doc: /* Scan from character number FROM by COUNT lists.
3031 Scan forward if COUNT is positive, backward if COUNT is negative.
3032 Return the character number of the position thus found.
3034 A \"list", in this context, refers to a balanced parenthetical
3035 grouping, as determined by the syntax table.
3037 If DEPTH is nonzero, treat that as the nesting depth of the starting
3038 point (i.e. the starting point is DEPTH parentheses deep). This
3039 function scans over parentheses until the depth goes to zero COUNT
3040 times. Hence, positive DEPTH moves out that number of levels of
3041 parentheses, while negative DEPTH moves to a deeper level.
3043 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3045 If we reach the beginning or end of the accessible part of the buffer
3046 before we have scanned over COUNT lists, return nil if the depth at
3047 that point is zero, and signal a error if the depth is nonzero. */)
3048 (Lisp_Object from, Lisp_Object count, Lisp_Object depth)
3050 CHECK_NUMBER (from);
3051 CHECK_NUMBER (count);
3052 CHECK_NUMBER (depth);
3054 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
3057 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
3058 doc: /* Scan from character number FROM by COUNT balanced expressions.
3059 If COUNT is negative, scan backwards.
3060 Returns the character number of the position thus found.
3062 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3064 If the beginning or end of (the accessible part of) the buffer is reached
3065 in the middle of a parenthetical grouping, an error is signaled.
3066 If the beginning or end is reached between groupings
3067 but before count is used up, nil is returned. */)
3068 (Lisp_Object from, Lisp_Object count)
3070 CHECK_NUMBER (from);
3071 CHECK_NUMBER (count);
3073 return scan_lists (XINT (from), XINT (count), 0, 1);
3076 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
3077 0, 0, 0,
3078 doc: /* Move point backward over any number of chars with prefix syntax.
3079 This includes chars with expression prefix syntax class (\\=') and those with
3080 the prefix syntax flag (p). */)
3081 (void)
3083 ptrdiff_t beg = BEGV;
3084 ptrdiff_t opoint = PT;
3085 ptrdiff_t opoint_byte = PT_BYTE;
3086 ptrdiff_t pos = PT;
3087 ptrdiff_t pos_byte = PT_BYTE;
3088 int c;
3090 if (pos <= beg)
3092 SET_PT_BOTH (opoint, opoint_byte);
3094 return Qnil;
3097 SETUP_SYNTAX_TABLE (pos, -1);
3099 DEC_BOTH (pos, pos_byte);
3101 while (!char_quoted (pos, pos_byte)
3102 /* Previous statement updates syntax table. */
3103 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
3104 || syntax_prefix_flag_p (c)))
3106 opoint = pos;
3107 opoint_byte = pos_byte;
3109 if (pos <= beg)
3110 break;
3111 DEC_BOTH (pos, pos_byte);
3112 rarely_quit (pos);
3115 SET_PT_BOTH (opoint, opoint_byte);
3117 return Qnil;
3121 /* If the character at FROM_BYTE is the second part of a 2-character
3122 comment opener based on PREV_FROM_SYNTAX, update STATE and return
3123 true. */
3124 static bool
3125 in_2char_comment_start (struct lisp_parse_state *state,
3126 int prev_from_syntax,
3127 ptrdiff_t prev_from,
3128 ptrdiff_t from_byte)
3130 int c1, syntax;
3131 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3132 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
3133 syntax = SYNTAX_WITH_FLAGS (c1),
3134 SYNTAX_FLAGS_COMSTART_SECOND (syntax)))
3136 /* Record the comment style we have entered so that only
3137 the comment-end sequence of the same style actually
3138 terminates the comment section. */
3139 state->comstyle
3140 = SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_from_syntax);
3141 bool comnested = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax)
3142 | SYNTAX_FLAGS_COMMENT_NESTED (syntax));
3143 state->incomment = comnested ? 1 : -1;
3144 state->comstr_start = prev_from;
3145 return true;
3147 return false;
3150 /* Parse forward from FROM / FROM_BYTE to END,
3151 assuming that FROM has state STATE,
3152 and return a description of the state of the parse at END.
3153 If STOPBEFORE, stop at the start of an atom.
3154 If COMMENTSTOP is 1, stop at the start of a comment.
3155 If COMMENTSTOP is -1, stop at the start or end of a comment,
3156 after the beginning of a string, or after the end of a string. */
3158 static void
3159 scan_sexps_forward (struct lisp_parse_state *state,
3160 ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t end,
3161 EMACS_INT targetdepth, bool stopbefore,
3162 int commentstop)
3164 enum syntaxcode code;
3165 struct level { ptrdiff_t last, prev; };
3166 struct level levelstart[100];
3167 struct level *curlevel = levelstart;
3168 struct level *endlevel = levelstart + 100;
3169 EMACS_INT depth; /* Paren depth of current scanning location.
3170 level - levelstart equals this except
3171 when the depth becomes negative. */
3172 EMACS_INT mindepth; /* Lowest DEPTH value seen. */
3173 bool start_quoted = 0; /* True means starting after a char quote. */
3174 Lisp_Object tem;
3175 ptrdiff_t prev_from; /* Keep one character before FROM. */
3176 ptrdiff_t prev_from_byte;
3177 int prev_from_syntax, prev_prev_from_syntax;
3178 bool boundary_stop = commentstop == -1;
3179 bool nofence;
3180 bool found;
3181 ptrdiff_t out_bytepos, out_charpos;
3182 int temp;
3183 unsigned short int quit_count = 0;
3185 prev_from = from;
3186 prev_from_byte = from_byte;
3187 if (from != BEGV)
3188 DEC_BOTH (prev_from, prev_from_byte);
3190 /* Use this macro instead of `from++'. */
3191 #define INC_FROM \
3192 do { prev_from = from; \
3193 prev_from_byte = from_byte; \
3194 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
3195 prev_prev_from_syntax = prev_from_syntax; \
3196 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
3197 INC_BOTH (from, from_byte); \
3198 if (from < end) \
3199 UPDATE_SYNTAX_TABLE_FORWARD (from); \
3200 } while (0)
3202 maybe_quit ();
3204 depth = state->depth;
3205 start_quoted = state->quoted;
3206 prev_prev_from_syntax = Smax;
3207 prev_from_syntax = state->prev_syntax;
3209 tem = state->levelstarts;
3210 while (!NILP (tem)) /* >= second enclosing sexps. */
3212 Lisp_Object temhd = Fcar (tem);
3213 if (RANGED_INTEGERP (PTRDIFF_MIN, temhd, PTRDIFF_MAX))
3214 curlevel->last = XINT (temhd);
3215 if (++curlevel == endlevel)
3216 curlevel--; /* error ("Nesting too deep for parser"); */
3217 curlevel->prev = -1;
3218 curlevel->last = -1;
3219 tem = Fcdr (tem);
3221 curlevel->prev = -1;
3222 curlevel->last = -1;
3224 state->quoted = 0;
3225 mindepth = depth;
3227 SETUP_SYNTAX_TABLE (from, 1);
3229 /* Enter the loop at a place appropriate for initial state. */
3231 if (state->incomment)
3232 goto startincomment;
3233 if (state->instring >= 0)
3235 nofence = state->instring != ST_STRING_STYLE;
3236 if (start_quoted)
3237 goto startquotedinstring;
3238 goto startinstring;
3240 else if (start_quoted)
3241 goto startquoted;
3242 else if ((from < end)
3243 && (in_2char_comment_start (state, prev_from_syntax,
3244 prev_from, from_byte)))
3246 INC_FROM;
3247 prev_from_syntax = Smax; /* the syntax has already been "used up". */
3248 goto atcomment;
3251 while (from < end)
3253 rarely_quit (++quit_count);
3254 INC_FROM;
3256 if ((from < end)
3257 && (in_2char_comment_start (state, prev_from_syntax,
3258 prev_from, from_byte)))
3260 INC_FROM;
3261 prev_from_syntax = Smax; /* the syntax has already been "used up". */
3262 goto atcomment;
3265 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3266 continue;
3267 code = prev_from_syntax & 0xff;
3268 switch (code)
3270 case Sescape:
3271 case Scharquote:
3272 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3273 curlevel->last = prev_from;
3274 startquoted:
3275 if (from == end) goto endquoted;
3276 INC_FROM;
3277 goto symstarted;
3278 /* treat following character as a word constituent */
3279 case Sword:
3280 case Ssymbol:
3281 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3282 curlevel->last = prev_from;
3283 symstarted:
3284 while (from < end)
3286 if (in_2char_comment_start (state, prev_from_syntax,
3287 prev_from, from_byte))
3289 INC_FROM;
3290 prev_from_syntax = Smax; /* the syntax has already been "used up". */
3291 goto atcomment;
3294 int symchar = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3295 switch (SYNTAX (symchar))
3297 case Scharquote:
3298 case Sescape:
3299 INC_FROM;
3300 if (from == end) goto endquoted;
3301 break;
3302 case Sword:
3303 case Ssymbol:
3304 case Squote:
3305 break;
3306 default:
3307 goto symdone;
3309 INC_FROM;
3310 rarely_quit (++quit_count);
3312 symdone:
3313 curlevel->prev = curlevel->last;
3314 break;
3316 case Scomment_fence:
3317 /* Record the comment style we have entered so that only
3318 the comment-end sequence of the same style actually
3319 terminates the comment section. */
3320 state->comstyle = ST_COMMENT_STYLE;
3321 state->incomment = -1;
3322 state->comstr_start = prev_from;
3323 goto atcomment;
3324 case Scomment:
3325 state->comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax, 0);
3326 state->incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3327 1 : -1);
3328 state->comstr_start = prev_from;
3329 atcomment:
3330 if (commentstop || boundary_stop) goto done;
3331 startincomment:
3332 /* The (from == BEGV) test was to enter the loop in the middle so
3333 that we find a 2-char comment ender even if we start in the
3334 middle of it. We don't want to do that if we're just at the
3335 beginning of the comment (think of (*) ... (*)). */
3336 found = forw_comment (from, from_byte, end,
3337 state->incomment, state->comstyle,
3338 from == BEGV ? 0 : prev_from_syntax,
3339 &out_charpos, &out_bytepos, &state->incomment,
3340 &prev_from_syntax);
3341 from = out_charpos; from_byte = out_bytepos;
3342 /* Beware! prev_from and friends (except prev_from_syntax)
3343 are invalid now. Luckily, the `done' doesn't use them
3344 and the INC_FROM sets them to a sane value without
3345 looking at them. */
3346 if (!found) goto done;
3347 INC_FROM;
3348 state->incomment = 0;
3349 state->comstyle = 0; /* reset the comment style */
3350 prev_from_syntax = Smax; /* For the comment closer */
3351 if (boundary_stop) goto done;
3352 break;
3354 case Sopen:
3355 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3356 depth++;
3357 /* curlevel++->last ran into compiler bug on Apollo */
3358 curlevel->last = prev_from;
3359 if (++curlevel == endlevel)
3360 curlevel--; /* error ("Nesting too deep for parser"); */
3361 curlevel->prev = -1;
3362 curlevel->last = -1;
3363 if (targetdepth == depth) goto done;
3364 break;
3366 case Sclose:
3367 depth--;
3368 if (depth < mindepth)
3369 mindepth = depth;
3370 if (curlevel != levelstart)
3371 curlevel--;
3372 curlevel->prev = curlevel->last;
3373 if (targetdepth == depth) goto done;
3374 break;
3376 case Sstring:
3377 case Sstring_fence:
3378 state->comstr_start = from - 1;
3379 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3380 curlevel->last = prev_from;
3381 state->instring = (code == Sstring
3382 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3383 : ST_STRING_STYLE);
3384 if (boundary_stop) goto done;
3385 startinstring:
3387 nofence = state->instring != ST_STRING_STYLE;
3389 while (1)
3391 int c;
3392 enum syntaxcode c_code;
3394 if (from >= end) goto done;
3395 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3396 c_code = SYNTAX (c);
3398 /* Check C_CODE here so that if the char has
3399 a syntax-table property which says it is NOT
3400 a string character, it does not end the string. */
3401 if (nofence && c == state->instring && c_code == Sstring)
3402 break;
3404 switch (c_code)
3406 case Sstring_fence:
3407 if (!nofence) goto string_end;
3408 break;
3410 case Scharquote:
3411 case Sescape:
3412 INC_FROM;
3413 startquotedinstring:
3414 if (from >= end) goto endquoted;
3415 break;
3417 default:
3418 break;
3420 INC_FROM;
3421 rarely_quit (++quit_count);
3424 string_end:
3425 state->instring = -1;
3426 curlevel->prev = curlevel->last;
3427 INC_FROM;
3428 if (boundary_stop) goto done;
3429 break;
3431 case Smath:
3432 /* FIXME: We should do something with it. */
3433 break;
3434 default:
3435 /* Ignore whitespace, punctuation, quote, endcomment. */
3436 break;
3439 goto done;
3441 stop: /* Here if stopping before start of sexp. */
3442 from = prev_from; /* We have just fetched the char that starts it; */
3443 from_byte = prev_from_byte;
3444 prev_from_syntax = prev_prev_from_syntax;
3445 goto done; /* but return the position before it. */
3447 endquoted:
3448 state->quoted = 1;
3449 done:
3450 state->depth = depth;
3451 state->mindepth = mindepth;
3452 state->thislevelstart = curlevel->prev;
3453 state->prevlevelstart
3454 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3455 state->location = from;
3456 state->location_byte = from_byte;
3457 state->levelstarts = Qnil;
3458 while (curlevel > levelstart)
3459 state->levelstarts = Fcons (make_number ((--curlevel)->last),
3460 state->levelstarts);
3461 state->prev_syntax = (SYNTAX_FLAGS_COMSTARTEND_FIRST (prev_from_syntax)
3462 || state->quoted) ? prev_from_syntax : Smax;
3465 /* Convert a (lisp) parse state to the internal form used in
3466 scan_sexps_forward. */
3467 static void
3468 internalize_parse_state (Lisp_Object external, struct lisp_parse_state *state)
3470 Lisp_Object tem;
3472 if (NILP (external))
3474 state->depth = 0;
3475 state->instring = -1;
3476 state->incomment = 0;
3477 state->quoted = 0;
3478 state->comstyle = 0; /* comment style a by default. */
3479 state->comstr_start = -1; /* no comment/string seen. */
3480 state->levelstarts = Qnil;
3481 state->prev_syntax = Smax;
3483 else
3485 tem = Fcar (external);
3486 if (!NILP (tem))
3487 state->depth = XINT (tem);
3488 else
3489 state->depth = 0;
3491 external = Fcdr (external);
3492 external = Fcdr (external);
3493 external = Fcdr (external);
3494 tem = Fcar (external);
3495 /* Check whether we are inside string_fence-style string: */
3496 state->instring = (!NILP (tem)
3497 ? (CHARACTERP (tem) ? XFASTINT (tem) : ST_STRING_STYLE)
3498 : -1);
3500 external = Fcdr (external);
3501 tem = Fcar (external);
3502 state->incomment = (!NILP (tem)
3503 ? (INTEGERP (tem) ? XINT (tem) : -1)
3504 : 0);
3506 external = Fcdr (external);
3507 tem = Fcar (external);
3508 state->quoted = !NILP (tem);
3510 /* if the eighth element of the list is nil, we are in comment
3511 style a. If it is non-nil, we are in comment style b */
3512 external = Fcdr (external);
3513 external = Fcdr (external);
3514 tem = Fcar (external);
3515 state->comstyle = (NILP (tem)
3517 : (RANGED_INTEGERP (0, tem, ST_COMMENT_STYLE)
3518 ? XINT (tem)
3519 : ST_COMMENT_STYLE));
3521 external = Fcdr (external);
3522 tem = Fcar (external);
3523 state->comstr_start =
3524 RANGED_INTEGERP (PTRDIFF_MIN, tem, PTRDIFF_MAX) ? XINT (tem) : -1;
3525 external = Fcdr (external);
3526 tem = Fcar (external);
3527 state->levelstarts = tem;
3529 external = Fcdr (external);
3530 tem = Fcar (external);
3531 state->prev_syntax = NILP (tem) ? Smax : XINT (tem);
3535 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3536 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3537 Parsing stops at TO or when certain criteria are met;
3538 point is set to where parsing stops.
3539 If fifth arg OLDSTATE is omitted or nil,
3540 parsing assumes that FROM is the beginning of a function.
3542 Value is a list of elements describing final state of parsing:
3543 0. depth in parens.
3544 1. character address of start of innermost containing list; nil if none.
3545 2. character address of start of last complete sexp terminated.
3546 3. non-nil if inside a string.
3547 (it is the character that will terminate the string,
3548 or t if the string should be terminated by a generic string delimiter.)
3549 4. nil if outside a comment, t if inside a non-nestable comment,
3550 else an integer (the current comment nesting).
3551 5. t if following a quote character.
3552 6. the minimum paren-depth encountered during this scan.
3553 7. style of comment, if any.
3554 8. character address of start of comment or string; nil if not in one.
3555 9. List of positions of currently open parens, outermost first.
3556 10. When the last position scanned holds the first character of a
3557 (potential) two character construct, the syntax of that position,
3558 otherwise nil. That construct can be a two character comment
3559 delimiter or an Escaped or Char-quoted character.
3560 11..... Possible further internal information used by `parse-partial-sexp'.
3562 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3563 in parentheses becomes equal to TARGETDEPTH.
3564 Fourth arg STOPBEFORE non-nil means stop when we come to
3565 any character that starts a sexp.
3566 Fifth arg OLDSTATE is a list like what this function returns.
3567 It is used to initialize the state of the parse. Elements number 1, 2, 6
3568 are ignored.
3569 Sixth arg COMMENTSTOP non-nil means stop after the start of a comment.
3570 If it is the symbol `syntax-table', stop after the start of a comment or a
3571 string, or after end of a comment or a string. */)
3572 (Lisp_Object from, Lisp_Object to, Lisp_Object targetdepth,
3573 Lisp_Object stopbefore, Lisp_Object oldstate, Lisp_Object commentstop)
3575 struct lisp_parse_state state;
3576 EMACS_INT target;
3578 if (!NILP (targetdepth))
3580 CHECK_NUMBER (targetdepth);
3581 target = XINT (targetdepth);
3583 else
3584 target = TYPE_MINIMUM (EMACS_INT); /* We won't reach this depth. */
3586 validate_region (&from, &to);
3587 internalize_parse_state (oldstate, &state);
3588 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3589 XINT (to),
3590 target, !NILP (stopbefore),
3591 (NILP (commentstop)
3592 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3594 SET_PT_BOTH (state.location, state.location_byte);
3596 return
3597 Fcons (make_number (state.depth),
3598 Fcons (state.prevlevelstart < 0
3599 ? Qnil : make_number (state.prevlevelstart),
3600 Fcons (state.thislevelstart < 0
3601 ? Qnil : make_number (state.thislevelstart),
3602 Fcons (state.instring >= 0
3603 ? (state.instring == ST_STRING_STYLE
3604 ? Qt : make_number (state.instring)) : Qnil,
3605 Fcons (state.incomment < 0 ? Qt :
3606 (state.incomment == 0 ? Qnil :
3607 make_number (state.incomment)),
3608 Fcons (state.quoted ? Qt : Qnil,
3609 Fcons (make_number (state.mindepth),
3610 Fcons ((state.comstyle
3611 ? (state.comstyle == ST_COMMENT_STYLE
3612 ? Qsyntax_table
3613 : make_number (state.comstyle))
3614 : Qnil),
3615 Fcons (((state.incomment
3616 || (state.instring >= 0))
3617 ? make_number (state.comstr_start)
3618 : Qnil),
3619 Fcons (state.levelstarts,
3620 Fcons (state.prev_syntax == Smax
3621 ? Qnil
3622 : make_number (state.prev_syntax),
3623 Qnil)))))))))));
3626 void
3627 init_syntax_once (void)
3629 register int i, c;
3630 Lisp_Object temp;
3632 /* This has to be done here, before we call Fmake_char_table. */
3633 DEFSYM (Qsyntax_table, "syntax-table");
3635 /* Create objects which can be shared among syntax tables. */
3636 Vsyntax_code_object = make_uninit_vector (Smax);
3637 for (i = 0; i < Smax; i++)
3638 ASET (Vsyntax_code_object, i, Fcons (make_number (i), Qnil));
3640 /* Now we are ready to set up this property, so we can
3641 create syntax tables. */
3642 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3644 temp = AREF (Vsyntax_code_object, Swhitespace);
3646 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3648 /* Control characters should not be whitespace. */
3649 temp = AREF (Vsyntax_code_object, Spunct);
3650 for (i = 0; i <= ' ' - 1; i++)
3651 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3652 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3654 /* Except that a few really are whitespace. */
3655 temp = AREF (Vsyntax_code_object, Swhitespace);
3656 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3657 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3658 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3659 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3660 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3662 temp = AREF (Vsyntax_code_object, Sword);
3663 for (i = 'a'; i <= 'z'; i++)
3664 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3665 for (i = 'A'; i <= 'Z'; i++)
3666 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3667 for (i = '0'; i <= '9'; i++)
3668 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3670 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3671 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3673 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3674 Fcons (make_number (Sopen), make_number (')')));
3675 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3676 Fcons (make_number (Sclose), make_number ('(')));
3677 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3678 Fcons (make_number (Sopen), make_number (']')));
3679 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3680 Fcons (make_number (Sclose), make_number ('[')));
3681 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3682 Fcons (make_number (Sopen), make_number ('}')));
3683 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3684 Fcons (make_number (Sclose), make_number ('{')));
3685 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3686 Fcons (make_number (Sstring), Qnil));
3687 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3688 Fcons (make_number (Sescape), Qnil));
3690 temp = AREF (Vsyntax_code_object, Ssymbol);
3691 for (i = 0; i < 10; i++)
3693 c = "_-+*/&|<>="[i];
3694 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3697 temp = AREF (Vsyntax_code_object, Spunct);
3698 for (i = 0; i < 12; i++)
3700 c = ".,;:?!#@~^'`"[i];
3701 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3704 /* All multibyte characters have syntax `word' by default. */
3705 temp = AREF (Vsyntax_code_object, Sword);
3706 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3709 void
3710 syms_of_syntax (void)
3712 DEFSYM (Qsyntax_table_p, "syntax-table-p");
3713 DEFSYM (Qsyntax_ppss, "syntax-ppss");
3714 DEFVAR_LISP ("comment-use-syntax-ppss",
3715 Vcomment_use_syntax_ppss,
3716 doc: /* Non-nil means `forward-comment' can use `syntax-ppss' internally. */);
3717 Vcomment_use_syntax_ppss = Qt;
3719 staticpro (&Vsyntax_code_object);
3721 staticpro (&gl_state.object);
3722 staticpro (&gl_state.global_code);
3723 staticpro (&gl_state.current_syntax_table);
3724 staticpro (&gl_state.old_prop);
3726 /* Defined in regex.c. */
3727 staticpro (&re_match_object);
3729 DEFSYM (Qscan_error, "scan-error");
3730 Fput (Qscan_error, Qerror_conditions,
3731 listn (CONSTYPE_PURE, 2, Qscan_error, Qerror));
3732 Fput (Qscan_error, Qerror_message,
3733 build_pure_c_string ("Scan error"));
3735 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments,
3736 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3738 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties,
3739 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3740 Otherwise, that text property is simply ignored.
3741 See the info node `(elisp)Syntax Properties' for a description of the
3742 `syntax-table' property. */);
3744 DEFVAR_INT ("syntax-propertize--done", syntax_propertize__done,
3745 doc: /* Position up to which syntax-table properties have been set. */);
3746 syntax_propertize__done = -1;
3747 DEFSYM (Qinternal__syntax_propertize, "internal--syntax-propertize");
3748 Fmake_variable_buffer_local (intern ("syntax-propertize--done"));
3750 words_include_escapes = 0;
3751 DEFVAR_BOOL ("words-include-escapes", words_include_escapes,
3752 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3754 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol,
3755 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3756 multibyte_syntax_as_symbol = 0;
3758 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3759 open_paren_in_column_0_is_defun_start,
3760 doc: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3761 open_paren_in_column_0_is_defun_start = 1;
3764 DEFVAR_LISP ("find-word-boundary-function-table",
3765 Vfind_word_boundary_function_table,
3766 doc: /*
3767 Char table of functions to search for the word boundary.
3768 Each function is called with two arguments; POS and LIMIT.
3769 POS and LIMIT are character positions in the current buffer.
3771 If POS is less than LIMIT, POS is at the first character of a word,
3772 and the return value of a function should be a position after the
3773 last character of that word.
3775 If POS is not less than LIMIT, POS is at the last character of a word,
3776 and the return value of a function should be a position at the first
3777 character of that word.
3779 In both cases, LIMIT bounds the search. */);
3780 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3782 DEFVAR_BOOL ("comment-end-can-be-escaped", Vcomment_end_can_be_escaped,
3783 doc: /* Non-nil means an escaped ender inside a comment doesn't end the comment. */);
3784 Vcomment_end_can_be_escaped = 0;
3785 DEFSYM (Qcomment_end_can_be_escaped, "comment-end-can-be-escaped");
3786 Fmake_variable_buffer_local (Qcomment_end_can_be_escaped);
3788 defsubr (&Ssyntax_table_p);
3789 defsubr (&Ssyntax_table);
3790 defsubr (&Sstandard_syntax_table);
3791 defsubr (&Scopy_syntax_table);
3792 defsubr (&Sset_syntax_table);
3793 defsubr (&Schar_syntax);
3794 defsubr (&Smatching_paren);
3795 defsubr (&Sstring_to_syntax);
3796 defsubr (&Smodify_syntax_entry);
3797 defsubr (&Sinternal_describe_syntax_value);
3799 defsubr (&Sforward_word);
3801 defsubr (&Sskip_chars_forward);
3802 defsubr (&Sskip_chars_backward);
3803 defsubr (&Sskip_syntax_forward);
3804 defsubr (&Sskip_syntax_backward);
3806 defsubr (&Sforward_comment);
3807 defsubr (&Sscan_lists);
3808 defsubr (&Sscan_sexps);
3809 defsubr (&Sbackward_prefix_chars);
3810 defsubr (&Sparse_partial_sexp);