Subject: Restore correct Gnus newsgroup name after sending message
[emacs.git] / src / syntax.c
blobf9e4093765cef97c8eb1384b90b08e10422cfc21
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 <http://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 (!open_paren_in_column_0_is_defun_start)
610 find_start_value = BEGV;
611 find_start_value_byte = BEGV_BYTE;
612 goto found;
615 /* Back up to start of line. */
616 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
618 /* We optimize syntax-table lookup for rare updates. Thus we accept
619 only those `^\s(' which are good in global _and_ text-property
620 syntax-tables. */
621 SETUP_BUFFER_SYNTAX_TABLE ();
622 while (PT > BEGV)
624 int c;
626 /* Open-paren at start of line means we may have found our
627 defun-start. */
628 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
629 if (SYNTAX (c) == Sopen)
631 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
632 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
633 if (SYNTAX (c) == Sopen)
634 break;
635 /* Now fallback to the default value. */
636 SETUP_BUFFER_SYNTAX_TABLE ();
638 /* Move to beg of previous line. */
639 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
642 /* Record what we found, for the next try. */
643 find_start_value = PT;
644 find_start_value_byte = PT_BYTE;
645 TEMP_SET_PT_BOTH (opoint, opoint_byte);
647 found:
648 find_start_buffer = current_buffer;
649 find_start_modiff = MODIFF;
650 find_start_begv = BEGV;
651 find_start_pos = pos;
653 return find_start_value;
656 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
658 static bool
659 prev_char_comend_first (ptrdiff_t pos, ptrdiff_t pos_byte)
661 int c;
662 bool val;
664 DEC_BOTH (pos, pos_byte);
665 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
666 c = FETCH_CHAR (pos_byte);
667 val = SYNTAX_COMEND_FIRST (c);
668 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
669 return val;
672 /* Check whether charpos FROM is at the end of a comment.
673 FROM_BYTE is the bytepos corresponding to FROM.
674 Do not move back before STOP.
676 Return true if we find a comment ending at FROM/FROM_BYTE.
678 If successful, store the charpos of the comment's beginning
679 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
681 Global syntax data remains valid for backward search starting at
682 the returned value (or at FROM, if the search was not successful). */
684 static bool
685 back_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop,
686 bool comnested, int comstyle, ptrdiff_t *charpos_ptr,
687 ptrdiff_t *bytepos_ptr)
689 /* Look back, counting the parity of string-quotes,
690 and recording the comment-starters seen.
691 When we reach a safe place, assume that's not in a string;
692 then step the main scan to the earliest comment-starter seen
693 an even number of string quotes away from the safe place.
695 OFROM[I] is position of the earliest comment-starter seen
696 which is I+2X quotes from the comment-end.
697 PARITY is current parity of quotes from the comment end. */
698 int string_style = -1; /* Presumed outside of any string. */
699 bool string_lossage = 0;
700 /* Not a real lossage: indicates that we have passed a matching comment
701 starter plus a non-matching comment-ender, meaning that any matching
702 comment-starter we might see later could be a false positive (hidden
703 inside another comment).
704 Test case: { a (* b } c (* d *) */
705 bool comment_lossage = 0;
706 ptrdiff_t comment_end = from;
707 ptrdiff_t comment_end_byte = from_byte;
708 ptrdiff_t comstart_pos = 0;
709 ptrdiff_t comstart_byte;
710 /* Place where the containing defun starts,
711 or 0 if we didn't come across it yet. */
712 ptrdiff_t defun_start = 0;
713 ptrdiff_t defun_start_byte = 0;
714 enum syntaxcode code;
715 ptrdiff_t nesting = 1; /* Current comment nesting. */
716 int c;
717 int syntax = 0;
719 /* FIXME: A }} comment-ender style leads to incorrect behavior
720 in the case of {{ c }}} because we ignore the last two chars which are
721 assumed to be comment-enders although they aren't. */
723 /* At beginning of range to scan, we're outside of strings;
724 that determines quote parity to the comment-end. */
725 while (from != stop)
727 ptrdiff_t temp_byte;
728 int prev_syntax;
729 bool com2start, com2end, comstart;
731 /* Move back and examine a character. */
732 DEC_BOTH (from, from_byte);
733 UPDATE_SYNTAX_TABLE_BACKWARD (from);
735 prev_syntax = syntax;
736 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
737 syntax = SYNTAX_WITH_FLAGS (c);
738 code = SYNTAX (c);
740 /* Check for 2-char comment markers. */
741 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
742 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
743 && (comstyle
744 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax, syntax))
745 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
746 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
747 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
748 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
749 comstart = (com2start || code == Scomment);
751 /* Nasty cases with overlapping 2-char comment markers:
752 - snmp-mode: -- c -- foo -- c --
753 --- c --
754 ------ c --
755 - c-mode: *||*
756 |* *|* *|
757 |*| |* |*|
758 /// */
760 /* If a 2-char comment sequence partly overlaps with another,
761 we don't try to be clever. E.g. |*| in C, or }% in modes that
762 have %..\n and %{..}%. */
763 if (from > stop && (com2end || comstart))
765 ptrdiff_t next = from, next_byte = from_byte;
766 int next_c, next_syntax;
767 DEC_BOTH (next, next_byte);
768 UPDATE_SYNTAX_TABLE_BACKWARD (next);
769 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
770 next_syntax = SYNTAX_WITH_FLAGS (next_c);
771 if (((comstart || comnested)
772 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
773 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
774 || ((com2end || comnested)
775 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
776 && (comstyle
777 == SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_syntax))
778 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
779 goto lossage;
780 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
783 if (com2start && comstart_pos == 0)
784 /* We're looking at a comment starter. But it might be a comment
785 ender as well (see snmp-mode). The first time we see one, we
786 need to consider it as a comment starter,
787 and the subsequent times as a comment ender. */
788 com2end = 0;
790 /* Turn a 2-char comment sequences into the appropriate syntax. */
791 if (com2end)
792 code = Sendcomment;
793 else if (com2start)
794 code = Scomment;
795 /* Ignore comment starters of a different style. */
796 else if (code == Scomment
797 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0)
798 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
799 continue;
801 /* Ignore escaped characters, except comment-enders which cannot
802 be escaped. */
803 if ((Vcomment_end_can_be_escaped || code != Sendcomment)
804 && char_quoted (from, from_byte))
805 continue;
807 switch (code)
809 case Sstring_fence:
810 case Scomment_fence:
811 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
812 case Sstring:
813 /* Track parity of quotes. */
814 if (string_style == -1)
815 /* Entering a string. */
816 string_style = c;
817 else if (string_style == c)
818 /* Leaving the string. */
819 string_style = -1;
820 else
821 /* If we have two kinds of string delimiters.
822 There's no way to grok this scanning backwards. */
823 string_lossage = 1;
824 break;
826 case Scomment:
827 /* We've already checked that it is the relevant comstyle. */
828 if (string_style != -1 || comment_lossage || string_lossage)
829 /* There are odd string quotes involved, so let's be careful.
830 Test case in Pascal: " { " a { " } */
831 goto lossage;
833 if (!comnested)
835 /* Record best comment-starter so far. */
836 comstart_pos = from;
837 comstart_byte = from_byte;
839 else if (--nesting <= 0)
840 /* nested comments have to be balanced, so we don't need to
841 keep looking for earlier ones. We use here the same (slightly
842 incorrect) reasoning as below: since it is followed by uniform
843 paired string quotes, this comment-start has to be outside of
844 strings, else the comment-end itself would be inside a string. */
845 goto done;
846 break;
848 case Sendcomment:
849 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == comstyle
850 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
851 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
852 /* This is the same style of comment ender as ours. */
854 if (comnested)
855 nesting++;
856 else
857 /* Anything before that can't count because it would match
858 this comment-ender rather than ours. */
859 from = stop; /* Break out of the loop. */
861 else if (comstart_pos != 0 || c != '\n')
862 /* We're mixing comment styles here, so we'd better be careful.
863 The (comstart_pos != 0 || c != '\n') check is not quite correct
864 (we should just always set comment_lossage), but removing it
865 would imply that any multiline comment in C would go through
866 lossage, which seems overkill.
867 The failure should only happen in the rare cases such as
868 { (* } *) */
869 comment_lossage = 1;
870 break;
872 case Sopen:
873 /* Assume a defun-start point is outside of strings. */
874 if (open_paren_in_column_0_is_defun_start
875 && (from == stop
876 || (temp_byte = dec_bytepos (from_byte),
877 FETCH_CHAR (temp_byte) == '\n')))
879 defun_start = from;
880 defun_start_byte = from_byte;
881 from = stop; /* Break out of the loop. */
883 break;
885 default:
886 break;
890 if (comstart_pos == 0)
892 from = comment_end;
893 from_byte = comment_end_byte;
894 UPDATE_SYNTAX_TABLE_FORWARD (comment_end);
896 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
897 or `done'), then we've found the beginning of the non-nested comment. */
898 else if (1) /* !comnested */
900 from = comstart_pos;
901 from_byte = comstart_byte;
902 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
904 else lossage:
906 struct lisp_parse_state state;
907 bool adjusted = true;
908 /* We had two kinds of string delimiters mixed up
909 together. Decode this going forwards.
910 Scan fwd from a known safe place (beginning-of-defun)
911 to the one in question; this records where we
912 last passed a comment starter. */
913 /* If we did not already find the defun start, find it now. */
914 if (defun_start == 0)
916 defun_start = find_defun_start (comment_end, comment_end_byte);
917 defun_start_byte = find_start_value_byte;
918 adjusted = (defun_start > BEGV);
922 internalize_parse_state (Qnil, &state);
923 scan_sexps_forward (&state,
924 defun_start, defun_start_byte,
925 comment_end, TYPE_MINIMUM (EMACS_INT),
926 0, 0);
927 defun_start = comment_end;
928 if (!adjusted)
930 adjusted = true;
931 find_start_value
932 = CONSP (state.levelstarts) ? XINT (XCAR (state.levelstarts))
933 : state.thislevelstart >= 0 ? state.thislevelstart
934 : find_start_value;
935 find_start_value_byte = CHAR_TO_BYTE (find_start_value);
938 if (state.incomment == (comnested ? 1 : -1)
939 && state.comstyle == comstyle)
940 from = state.comstr_start;
941 else
943 from = comment_end;
944 if (state.incomment)
945 /* If comment_end is inside some other comment, maybe ours
946 is nested, so we need to try again from within the
947 surrounding comment. Example: { a (* " *) */
949 /* FIXME: We should advance by one or two chars. */
950 defun_start = state.comstr_start + 2;
951 defun_start_byte = CHAR_TO_BYTE (defun_start);
954 } while (defun_start < comment_end);
956 from_byte = CHAR_TO_BYTE (from);
957 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
960 done:
961 *charpos_ptr = from;
962 *bytepos_ptr = from_byte;
964 return from != comment_end;
967 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
968 doc: /* Return t if OBJECT is a syntax table.
969 Currently, any char-table counts as a syntax table. */)
970 (Lisp_Object object)
972 if (CHAR_TABLE_P (object)
973 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
974 return Qt;
975 return Qnil;
978 static void
979 check_syntax_table (Lisp_Object obj)
981 CHECK_TYPE (CHAR_TABLE_P (obj) && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table),
982 Qsyntax_table_p, obj);
985 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
986 doc: /* Return the current syntax table.
987 This is the one specified by the current buffer. */)
988 (void)
990 return BVAR (current_buffer, syntax_table);
993 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
994 Sstandard_syntax_table, 0, 0, 0,
995 doc: /* Return the standard syntax table.
996 This is the one used for new buffers. */)
997 (void)
999 return Vstandard_syntax_table;
1002 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
1003 doc: /* Construct a new syntax table and return it.
1004 It is a copy of the TABLE, which defaults to the standard syntax table. */)
1005 (Lisp_Object table)
1007 Lisp_Object copy;
1009 if (!NILP (table))
1010 check_syntax_table (table);
1011 else
1012 table = Vstandard_syntax_table;
1014 copy = Fcopy_sequence (table);
1016 /* Only the standard syntax table should have a default element.
1017 Other syntax tables should inherit from parents instead. */
1018 set_char_table_defalt (copy, Qnil);
1020 /* Copied syntax tables should all have parents.
1021 If we copied one with no parent, such as the standard syntax table,
1022 use the standard syntax table as the copy's parent. */
1023 if (NILP (XCHAR_TABLE (copy)->parent))
1024 Fset_char_table_parent (copy, Vstandard_syntax_table);
1025 return copy;
1028 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
1029 doc: /* Select a new syntax table for the current buffer.
1030 One argument, a syntax table. */)
1031 (Lisp_Object table)
1033 int idx;
1034 check_syntax_table (table);
1035 bset_syntax_table (current_buffer, table);
1036 /* Indicate that this buffer now has a specified syntax table. */
1037 idx = PER_BUFFER_VAR_IDX (syntax_table);
1038 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
1039 return table;
1042 /* Convert a letter which signifies a syntax code
1043 into the code it signifies.
1044 This is used by modify-syntax-entry, and other things. */
1046 unsigned char const syntax_spec_code[0400] =
1047 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1048 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1049 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1050 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1051 Swhitespace, Scomment_fence, Sstring, 0377, Smath, 0377, 0377, Squote,
1052 Sopen, Sclose, 0377, 0377, 0377, Swhitespace, Spunct, Scharquote,
1053 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1054 0377, 0377, 0377, 0377, Scomment, 0377, Sendcomment, 0377,
1055 Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
1056 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1057 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword,
1058 0377, 0377, 0377, 0377, Sescape, 0377, 0377, Ssymbol,
1059 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
1060 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1061 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword,
1062 0377, 0377, 0377, 0377, Sstring_fence, 0377, 0377, 0377
1065 /* Indexed by syntax code, give the letter that describes it. */
1067 char const syntax_code_spec[16] =
1069 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
1070 '!', '|'
1073 /* Indexed by syntax code, give the object (cons of syntax code and
1074 nil) to be stored in syntax table. Since these objects can be
1075 shared among syntax tables, we generate them in advance. By
1076 sharing objects, the function `describe-syntax' can give a more
1077 compact listing. */
1078 static Lisp_Object Vsyntax_code_object;
1081 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
1082 doc: /* Return the syntax code of CHARACTER, described by a character.
1083 For example, if CHARACTER is a word constituent, the
1084 character `w' (119) is returned.
1085 The characters that correspond to various syntax codes
1086 are listed in the documentation of `modify-syntax-entry'. */)
1087 (Lisp_Object character)
1089 int char_int;
1090 CHECK_CHARACTER (character);
1091 char_int = XINT (character);
1092 SETUP_BUFFER_SYNTAX_TABLE ();
1093 return make_number (syntax_code_spec[SYNTAX (char_int)]);
1096 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
1097 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
1098 (Lisp_Object character)
1100 int char_int;
1101 enum syntaxcode code;
1102 CHECK_CHARACTER (character);
1103 char_int = XINT (character);
1104 SETUP_BUFFER_SYNTAX_TABLE ();
1105 code = SYNTAX (char_int);
1106 if (code == Sopen || code == Sclose)
1107 return SYNTAX_MATCH (char_int);
1108 return Qnil;
1111 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
1112 doc: /* Convert a syntax descriptor STRING into a raw syntax descriptor.
1113 STRING should be a string of the form allowed as argument of
1114 `modify-syntax-entry'. The return value is a raw syntax descriptor: a
1115 cons cell (CODE . MATCHING-CHAR) which can be used, for example, as
1116 the value of a `syntax-table' text property. */)
1117 (Lisp_Object string)
1119 const unsigned char *p;
1120 int val;
1121 Lisp_Object match;
1123 CHECK_STRING (string);
1125 p = SDATA (string);
1126 val = syntax_spec_code[*p++];
1127 if (val == 0377)
1128 error ("Invalid syntax description letter: %c", p[-1]);
1130 if (val == Sinherit)
1131 return Qnil;
1133 if (*p)
1135 int len;
1136 int character = STRING_CHAR_AND_LENGTH (p, len);
1137 XSETINT (match, character);
1138 if (XFASTINT (match) == ' ')
1139 match = Qnil;
1140 p += len;
1142 else
1143 match = Qnil;
1145 while (*p)
1146 switch (*p++)
1148 case '1':
1149 val |= 1 << 16;
1150 break;
1152 case '2':
1153 val |= 1 << 17;
1154 break;
1156 case '3':
1157 val |= 1 << 18;
1158 break;
1160 case '4':
1161 val |= 1 << 19;
1162 break;
1164 case 'p':
1165 val |= 1 << 20;
1166 break;
1168 case 'b':
1169 val |= 1 << 21;
1170 break;
1172 case 'n':
1173 val |= 1 << 22;
1174 break;
1176 case 'c':
1177 val |= 1 << 23;
1178 break;
1181 if (val < ASIZE (Vsyntax_code_object) && NILP (match))
1182 return AREF (Vsyntax_code_object, val);
1183 else
1184 /* Since we can't use a shared object, let's make a new one. */
1185 return Fcons (make_number (val), match);
1188 /* I really don't know why this is interactive
1189 help-form should at least be made useful whilst reading the second arg. */
1190 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
1191 "cSet syntax for character: \nsSet syntax for %s to: ",
1192 doc: /* Set syntax for character CHAR according to string NEWENTRY.
1193 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1194 the current buffer's syntax table.
1195 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1196 in the range MIN to MAX are changed.
1197 The first character of NEWENTRY should be one of the following:
1198 Space or - whitespace syntax. w word constituent.
1199 _ symbol constituent. . punctuation.
1200 ( open-parenthesis. ) close-parenthesis.
1201 " string quote. \\ escape.
1202 $ paired delimiter. \\=' expression quote or prefix operator.
1203 < comment starter. > comment ender.
1204 / character-quote. @ inherit from parent table.
1205 | generic string fence. ! generic comment fence.
1207 Only single-character comment start and end sequences are represented thus.
1208 Two-character sequences are represented as described below.
1209 The second character of NEWENTRY is the matching parenthesis,
1210 used only if the first character is `(' or `)'.
1211 Any additional characters are flags.
1212 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1213 1 means CHAR is the start of a two-char comment start sequence.
1214 2 means CHAR is the second character of such a sequence.
1215 3 means CHAR is the start of a two-char comment end sequence.
1216 4 means CHAR is the second character of such a sequence.
1218 There can be several orthogonal comment sequences. This is to support
1219 language modes such as C++. By default, all comment sequences are of style
1220 a, but you can set the comment sequence style to b (on the second character
1221 of a comment-start, and the first character of a comment-end sequence) and/or
1222 c (on any of its chars) using this flag:
1223 b means CHAR is part of comment sequence b.
1224 c means CHAR is part of comment sequence c.
1225 n means CHAR is part of a nestable comment sequence.
1227 p means CHAR is a prefix character for `backward-prefix-chars';
1228 such characters are treated as whitespace when they occur
1229 between expressions.
1230 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1231 (Lisp_Object c, Lisp_Object newentry, Lisp_Object syntax_table)
1233 if (CONSP (c))
1235 CHECK_CHARACTER_CAR (c);
1236 CHECK_CHARACTER_CDR (c);
1238 else
1239 CHECK_CHARACTER (c);
1241 if (NILP (syntax_table))
1242 syntax_table = BVAR (current_buffer, syntax_table);
1243 else
1244 check_syntax_table (syntax_table);
1246 newentry = Fstring_to_syntax (newentry);
1247 if (CONSP (c))
1248 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1249 else
1250 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
1252 /* We clear the regexp cache, since character classes can now have
1253 different values from those in the compiled regexps.*/
1254 clear_regexp_cache ();
1256 return Qnil;
1259 /* Dump syntax table to buffer in human-readable format */
1261 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1262 Sinternal_describe_syntax_value, 1, 1, 0,
1263 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1264 (Lisp_Object syntax)
1266 int code, syntax_code;
1267 bool start1, start2, end1, end2, prefix, comstyleb, comstylec, comnested;
1268 char str[2];
1269 Lisp_Object first, match_lisp, value = syntax;
1271 if (NILP (value))
1273 insert_string ("default");
1274 return syntax;
1277 if (CHAR_TABLE_P (value))
1279 insert_string ("deeper char-table ...");
1280 return syntax;
1283 if (!CONSP (value))
1285 insert_string ("invalid");
1286 return syntax;
1289 first = XCAR (value);
1290 match_lisp = XCDR (value);
1292 if (!INTEGERP (first) || !(NILP (match_lisp) || CHARACTERP (match_lisp)))
1294 insert_string ("invalid");
1295 return syntax;
1298 syntax_code = XINT (first) & INT_MAX;
1299 code = syntax_code & 0377;
1300 start1 = SYNTAX_FLAGS_COMSTART_FIRST (syntax_code);
1301 start2 = SYNTAX_FLAGS_COMSTART_SECOND (syntax_code);
1302 end1 = SYNTAX_FLAGS_COMEND_FIRST (syntax_code);
1303 end2 = SYNTAX_FLAGS_COMEND_SECOND (syntax_code);
1304 prefix = SYNTAX_FLAGS_PREFIX (syntax_code);
1305 comstyleb = SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code);
1306 comstylec = SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code);
1307 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax_code);
1309 if (Smax <= code)
1311 insert_string ("invalid");
1312 return syntax;
1315 str[0] = syntax_code_spec[code], str[1] = 0;
1316 insert (str, 1);
1318 if (NILP (match_lisp))
1319 insert (" ", 1);
1320 else
1321 insert_char (XINT (match_lisp));
1323 if (start1)
1324 insert ("1", 1);
1325 if (start2)
1326 insert ("2", 1);
1328 if (end1)
1329 insert ("3", 1);
1330 if (end2)
1331 insert ("4", 1);
1333 if (prefix)
1334 insert ("p", 1);
1335 if (comstyleb)
1336 insert ("b", 1);
1337 if (comstylec)
1338 insert ("c", 1);
1339 if (comnested)
1340 insert ("n", 1);
1342 insert_string ("\twhich means: ");
1344 switch (code)
1346 case Swhitespace:
1347 insert_string ("whitespace"); break;
1348 case Spunct:
1349 insert_string ("punctuation"); break;
1350 case Sword:
1351 insert_string ("word"); break;
1352 case Ssymbol:
1353 insert_string ("symbol"); break;
1354 case Sopen:
1355 insert_string ("open"); break;
1356 case Sclose:
1357 insert_string ("close"); break;
1358 case Squote:
1359 insert_string ("prefix"); break;
1360 case Sstring:
1361 insert_string ("string"); break;
1362 case Smath:
1363 insert_string ("math"); break;
1364 case Sescape:
1365 insert_string ("escape"); break;
1366 case Scharquote:
1367 insert_string ("charquote"); break;
1368 case Scomment:
1369 insert_string ("comment"); break;
1370 case Sendcomment:
1371 insert_string ("endcomment"); break;
1372 case Sinherit:
1373 insert_string ("inherit"); break;
1374 case Scomment_fence:
1375 insert_string ("comment fence"); break;
1376 case Sstring_fence:
1377 insert_string ("string fence"); break;
1378 default:
1379 insert_string ("invalid");
1380 return syntax;
1383 if (!NILP (match_lisp))
1385 insert_string (", matches ");
1386 insert_char (XINT (match_lisp));
1389 if (start1)
1390 insert_string (",\n\t is the first character of a comment-start sequence");
1391 if (start2)
1392 insert_string (",\n\t is the second character of a comment-start sequence");
1394 if (end1)
1395 insert_string (",\n\t is the first character of a comment-end sequence");
1396 if (end2)
1397 insert_string (",\n\t is the second character of a comment-end sequence");
1398 if (comstyleb)
1399 insert_string (" (comment style b)");
1400 if (comstylec)
1401 insert_string (" (comment style c)");
1402 if (comnested)
1403 insert_string (" (nestable)");
1405 if (prefix)
1407 AUTO_STRING (prefixdoc,
1408 ",\n\t is a prefix character for `backward-prefix-chars'");
1409 insert1 (Fsubstitute_command_keys (prefixdoc));
1412 return syntax;
1415 /* Return the position across COUNT words from FROM.
1416 If that many words cannot be found before the end of the buffer, return 0.
1417 COUNT negative means scan backward and stop at word beginning. */
1419 ptrdiff_t
1420 scan_words (register ptrdiff_t from, register EMACS_INT count)
1422 register ptrdiff_t beg = BEGV;
1423 register ptrdiff_t end = ZV;
1424 register ptrdiff_t from_byte = CHAR_TO_BYTE (from);
1425 register enum syntaxcode code;
1426 int ch0, ch1;
1427 Lisp_Object func, pos;
1429 immediate_quit = true;
1430 maybe_quit ();
1432 SETUP_SYNTAX_TABLE (from, count);
1434 while (count > 0)
1436 while (1)
1438 if (from == end)
1440 immediate_quit = false;
1441 return 0;
1443 UPDATE_SYNTAX_TABLE_FORWARD (from);
1444 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1445 code = SYNTAX (ch0);
1446 INC_BOTH (from, from_byte);
1447 if (words_include_escapes
1448 && (code == Sescape || code == Scharquote))
1449 break;
1450 if (code == Sword)
1451 break;
1453 /* Now CH0 is a character which begins a word and FROM is the
1454 position of the next character. */
1455 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
1456 if (! NILP (Ffboundp (func)))
1458 pos = call2 (func, make_number (from - 1), make_number (end));
1459 if (INTEGERP (pos) && from < XINT (pos) && XINT (pos) <= ZV)
1461 from = XINT (pos);
1462 from_byte = CHAR_TO_BYTE (from);
1465 else
1467 while (1)
1469 if (from == end) break;
1470 UPDATE_SYNTAX_TABLE_FORWARD (from);
1471 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1472 code = SYNTAX (ch1);
1473 if ((code != Sword
1474 && (! words_include_escapes
1475 || (code != Sescape && code != Scharquote)))
1476 || word_boundary_p (ch0, ch1))
1477 break;
1478 INC_BOTH (from, from_byte);
1479 ch0 = ch1;
1482 count--;
1484 while (count < 0)
1486 while (1)
1488 if (from == beg)
1490 immediate_quit = false;
1491 return 0;
1493 DEC_BOTH (from, from_byte);
1494 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1495 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1496 code = SYNTAX (ch1);
1497 if (words_include_escapes
1498 && (code == Sescape || code == Scharquote))
1499 break;
1500 if (code == Sword)
1501 break;
1503 /* Now CH1 is a character which ends a word and FROM is the
1504 position of it. */
1505 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
1506 if (! NILP (Ffboundp (func)))
1508 pos = call2 (func, make_number (from), make_number (beg));
1509 if (INTEGERP (pos) && BEGV <= XINT (pos) && XINT (pos) < from)
1511 from = XINT (pos);
1512 from_byte = CHAR_TO_BYTE (from);
1515 else
1517 while (1)
1519 if (from == beg)
1520 break;
1521 DEC_BOTH (from, from_byte);
1522 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1523 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1524 code = SYNTAX (ch0);
1525 if ((code != Sword
1526 && (! words_include_escapes
1527 || (code != Sescape && code != Scharquote)))
1528 || word_boundary_p (ch0, ch1))
1530 INC_BOTH (from, from_byte);
1531 break;
1533 ch1 = ch0;
1536 count++;
1539 immediate_quit = false;
1541 return from;
1544 DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p",
1545 doc: /* Move point forward ARG words (backward if ARG is negative).
1546 If ARG is omitted or nil, move point forward one word.
1547 Normally returns t.
1548 If an edge of the buffer or a field boundary is reached, point is
1549 left there and the function returns nil. Field boundaries are not
1550 noticed if `inhibit-field-text-motion' is non-nil.
1552 The word boundaries are normally determined by the buffer's syntax
1553 table, but `find-word-boundary-function-table', such as set up
1554 by `subword-mode', can change that. If a Lisp program needs to
1555 move by words determined strictly by the syntax table, it should
1556 use `forward-word-strictly' instead. */)
1557 (Lisp_Object arg)
1559 Lisp_Object tmp;
1560 ptrdiff_t orig_val, val;
1562 if (NILP (arg))
1563 XSETFASTINT (arg, 1);
1564 else
1565 CHECK_NUMBER (arg);
1567 val = orig_val = scan_words (PT, XINT (arg));
1568 if (! orig_val)
1569 val = XINT (arg) > 0 ? ZV : BEGV;
1571 /* Avoid jumping out of an input field. */
1572 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1573 Qnil, Qnil, Qnil);
1574 val = XFASTINT (tmp);
1576 SET_PT (val);
1577 return val == orig_val ? Qt : Qnil;
1580 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1581 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1582 STRING is like the inside of a `[...]' in a regular expression
1583 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1584 (but not at the end of a range; quoting is never needed there).
1585 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1586 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1587 Char classes, e.g. `[:alpha:]', are supported.
1589 Returns the distance traveled, either zero or positive. */)
1590 (Lisp_Object string, Lisp_Object lim)
1592 return skip_chars (1, string, lim, 1);
1595 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1596 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1597 See `skip-chars-forward' for details.
1598 Returns the distance traveled, either zero or negative. */)
1599 (Lisp_Object string, Lisp_Object lim)
1601 return skip_chars (0, string, lim, 1);
1604 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1605 doc: /* Move point forward across chars in specified syntax classes.
1606 SYNTAX is a string of syntax code characters.
1607 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1608 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1609 This function returns the distance traveled, either zero or positive. */)
1610 (Lisp_Object syntax, Lisp_Object lim)
1612 return skip_syntaxes (1, syntax, lim);
1615 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1616 doc: /* Move point backward across chars in specified syntax classes.
1617 SYNTAX is a string of syntax code characters.
1618 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1619 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1620 This function returns either zero or a negative number, and the absolute value
1621 of this is the distance traveled. */)
1622 (Lisp_Object syntax, Lisp_Object lim)
1624 return skip_syntaxes (0, syntax, lim);
1627 static Lisp_Object
1628 skip_chars (bool forwardp, Lisp_Object string, Lisp_Object lim,
1629 bool handle_iso_classes)
1631 int c;
1632 char fastmap[0400];
1633 /* Store the ranges of non-ASCII characters. */
1634 int *char_ranges UNINIT;
1635 int n_char_ranges = 0;
1636 bool negate = 0;
1637 ptrdiff_t i, i_byte;
1638 /* True if the current buffer is multibyte and the region contains
1639 non-ASCII chars. */
1640 bool multibyte;
1641 /* True if STRING is multibyte and it contains non-ASCII chars. */
1642 bool string_multibyte;
1643 ptrdiff_t size_byte;
1644 const unsigned char *str;
1645 int len;
1646 Lisp_Object iso_classes;
1647 USE_SAFE_ALLOCA;
1649 CHECK_STRING (string);
1650 iso_classes = Qnil;
1652 if (NILP (lim))
1653 XSETINT (lim, forwardp ? ZV : BEGV);
1654 else
1655 CHECK_NUMBER_COERCE_MARKER (lim);
1657 /* In any case, don't allow scan outside bounds of buffer. */
1658 if (XINT (lim) > ZV)
1659 XSETFASTINT (lim, ZV);
1660 if (XINT (lim) < BEGV)
1661 XSETFASTINT (lim, BEGV);
1663 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1664 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1665 string_multibyte = SBYTES (string) > SCHARS (string);
1667 memset (fastmap, 0, sizeof fastmap);
1669 str = SDATA (string);
1670 size_byte = SBYTES (string);
1672 i_byte = 0;
1673 if (i_byte < size_byte
1674 && SREF (string, 0) == '^')
1676 negate = 1; i_byte++;
1679 /* Find the characters specified and set their elements of fastmap.
1680 Handle backslashes and ranges specially.
1682 If STRING contains non-ASCII characters, setup char_ranges for
1683 them and use fastmap only for their leading codes. */
1685 if (! string_multibyte)
1687 bool string_has_eight_bit = 0;
1689 /* At first setup fastmap. */
1690 while (i_byte < size_byte)
1692 if (handle_iso_classes)
1694 const unsigned char *ch = str + i_byte;
1695 re_wctype_t cc = re_wctype_parse (&ch, size_byte - i_byte);
1696 if (cc == 0)
1697 error ("Invalid ISO C character class");
1698 if (cc != -1)
1700 iso_classes = Fcons (make_number (cc), iso_classes);
1701 i_byte = ch - str;
1702 continue;
1706 c = str[i_byte++];
1708 if (c == '\\')
1710 if (i_byte == size_byte)
1711 break;
1713 c = str[i_byte++];
1715 /* Treat `-' as range character only if another character
1716 follows. */
1717 if (i_byte + 1 < size_byte
1718 && str[i_byte] == '-')
1720 int c2;
1722 /* Skip over the dash. */
1723 i_byte++;
1725 /* Get the end of the range. */
1726 c2 = str[i_byte++];
1727 if (c2 == '\\'
1728 && i_byte < size_byte)
1729 c2 = str[i_byte++];
1731 if (c <= c2)
1733 int lim2 = c2 + 1;
1734 while (c < lim2)
1735 fastmap[c++] = 1;
1736 if (! ASCII_CHAR_P (c2))
1737 string_has_eight_bit = 1;
1740 else
1742 fastmap[c] = 1;
1743 if (! ASCII_CHAR_P (c))
1744 string_has_eight_bit = 1;
1748 /* If the current range is multibyte and STRING contains
1749 eight-bit chars, arrange fastmap and setup char_ranges for
1750 the corresponding multibyte chars. */
1751 if (multibyte && string_has_eight_bit)
1753 char *p1;
1754 char himap[0200 + 1];
1755 memcpy (himap, fastmap + 0200, 0200);
1756 himap[0200] = 0;
1757 memset (fastmap + 0200, 0, 0200);
1758 SAFE_NALLOCA (char_ranges, 2, 128);
1759 i = 0;
1761 while ((p1 = memchr (himap + i, 1, 0200 - i)))
1763 /* Deduce the next range C..C2 from the next clump of 1s
1764 in HIMAP starting with &HIMAP[I]. HIMAP is the high
1765 order half of the old FASTMAP. */
1766 int c2, leading_code;
1767 i = p1 - himap;
1768 c = BYTE8_TO_CHAR (i + 0200);
1769 i += strlen (p1);
1770 c2 = BYTE8_TO_CHAR (i + 0200 - 1);
1772 char_ranges[n_char_ranges++] = c;
1773 char_ranges[n_char_ranges++] = c2;
1774 leading_code = CHAR_LEADING_CODE (c);
1775 memset (fastmap + leading_code, 1,
1776 CHAR_LEADING_CODE (c2) - leading_code + 1);
1780 else /* STRING is multibyte */
1782 SAFE_NALLOCA (char_ranges, 2, SCHARS (string));
1784 while (i_byte < size_byte)
1786 int leading_code = str[i_byte];
1788 if (handle_iso_classes)
1790 const unsigned char *ch = str + i_byte;
1791 re_wctype_t cc = re_wctype_parse (&ch, size_byte - i_byte);
1792 if (cc == 0)
1793 error ("Invalid ISO C character class");
1794 if (cc != -1)
1796 iso_classes = Fcons (make_number (cc), iso_classes);
1797 i_byte = ch - str;
1798 continue;
1802 if (leading_code== '\\')
1804 if (++i_byte == size_byte)
1805 break;
1807 leading_code = str[i_byte];
1809 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1810 i_byte += len;
1813 /* Treat `-' as range character only if another character
1814 follows. */
1815 if (i_byte + 1 < size_byte
1816 && str[i_byte] == '-')
1818 int c2, leading_code2;
1820 /* Skip over the dash. */
1821 i_byte++;
1823 /* Get the end of the range. */
1824 leading_code2 = str[i_byte];
1825 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1826 i_byte += len;
1828 if (c2 == '\\'
1829 && i_byte < size_byte)
1831 leading_code2 = str[i_byte];
1832 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1833 i_byte += len;
1836 if (c > c2)
1837 continue;
1838 if (ASCII_CHAR_P (c))
1840 while (c <= c2 && c < 0x80)
1841 fastmap[c++] = 1;
1842 leading_code = CHAR_LEADING_CODE (c);
1844 if (! ASCII_CHAR_P (c))
1846 int lim2 = leading_code2 + 1;
1847 while (leading_code < lim2)
1848 fastmap[leading_code++] = 1;
1849 if (c <= c2)
1851 char_ranges[n_char_ranges++] = c;
1852 char_ranges[n_char_ranges++] = c2;
1856 else
1858 if (ASCII_CHAR_P (c))
1859 fastmap[c] = 1;
1860 else
1862 fastmap[leading_code] = 1;
1863 char_ranges[n_char_ranges++] = c;
1864 char_ranges[n_char_ranges++] = c;
1869 /* If the current range is unibyte and STRING contains non-ASCII
1870 chars, arrange fastmap for the corresponding unibyte
1871 chars. */
1873 if (! multibyte && n_char_ranges > 0)
1875 memset (fastmap + 0200, 0, 0200);
1876 for (i = 0; i < n_char_ranges; i += 2)
1878 int c1 = char_ranges[i];
1879 int lim2 = char_ranges[i + 1] + 1;
1881 for (; c1 < lim2; c1++)
1883 int b = CHAR_TO_BYTE_SAFE (c1);
1884 if (b >= 0)
1885 fastmap[b] = 1;
1891 /* If ^ was the first character, complement the fastmap. */
1892 if (negate)
1894 if (! multibyte)
1895 for (i = 0; i < sizeof fastmap; i++)
1896 fastmap[i] ^= 1;
1897 else
1899 for (i = 0; i < 0200; i++)
1900 fastmap[i] ^= 1;
1901 /* All non-ASCII chars possibly match. */
1902 for (; i < sizeof fastmap; i++)
1903 fastmap[i] = 1;
1908 ptrdiff_t start_point = PT;
1909 ptrdiff_t pos = PT;
1910 ptrdiff_t pos_byte = PT_BYTE;
1911 unsigned char *p = PT_ADDR, *endp, *stop;
1913 if (forwardp)
1915 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1916 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1918 else
1920 endp = CHAR_POS_ADDR (XINT (lim));
1921 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1924 immediate_quit = true;
1925 /* This code may look up syntax tables using functions that rely on the
1926 gl_state object. To make sure this object is not out of date,
1927 let's initialize it manually.
1928 We ignore syntax-table text-properties for now, since that's
1929 what we've done in the past. */
1930 SETUP_BUFFER_SYNTAX_TABLE ();
1931 if (forwardp)
1933 if (multibyte)
1934 while (1)
1936 int nbytes;
1938 if (p >= stop)
1940 if (p >= endp)
1941 break;
1942 p = GAP_END_ADDR;
1943 stop = endp;
1945 c = STRING_CHAR_AND_LENGTH (p, nbytes);
1946 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1948 if (negate)
1949 break;
1950 else
1951 goto fwd_ok;
1954 if (! fastmap[*p])
1955 break;
1956 if (! ASCII_CHAR_P (c))
1958 /* As we are looking at a multibyte character, we
1959 must look up the character in the table
1960 CHAR_RANGES. If there's no data in the table,
1961 that character is not what we want to skip. */
1963 /* The following code do the right thing even if
1964 n_char_ranges is zero (i.e. no data in
1965 CHAR_RANGES). */
1966 for (i = 0; i < n_char_ranges; i += 2)
1967 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1968 break;
1969 if (!(negate ^ (i < n_char_ranges)))
1970 break;
1972 fwd_ok:
1973 p += nbytes, pos++, pos_byte += nbytes;
1975 else
1976 while (1)
1978 if (p >= stop)
1980 if (p >= endp)
1981 break;
1982 p = GAP_END_ADDR;
1983 stop = endp;
1986 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1988 if (negate)
1989 break;
1990 else
1991 goto fwd_unibyte_ok;
1994 if (!fastmap[*p])
1995 break;
1996 fwd_unibyte_ok:
1997 p++, pos++, pos_byte++;
2000 else
2002 if (multibyte)
2003 while (1)
2005 unsigned char *prev_p;
2007 if (p <= stop)
2009 if (p <= endp)
2010 break;
2011 p = GPT_ADDR;
2012 stop = endp;
2014 prev_p = p;
2015 while (--p >= stop && ! CHAR_HEAD_P (*p));
2016 c = STRING_CHAR (p);
2018 if (! NILP (iso_classes) && in_classes (c, iso_classes))
2020 if (negate)
2021 break;
2022 else
2023 goto back_ok;
2026 if (! fastmap[*p])
2027 break;
2028 if (! ASCII_CHAR_P (c))
2030 /* See the comment in the previous similar code. */
2031 for (i = 0; i < n_char_ranges; i += 2)
2032 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
2033 break;
2034 if (!(negate ^ (i < n_char_ranges)))
2035 break;
2037 back_ok:
2038 pos--, pos_byte -= prev_p - p;
2040 else
2041 while (1)
2043 if (p <= stop)
2045 if (p <= endp)
2046 break;
2047 p = GPT_ADDR;
2048 stop = endp;
2051 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
2053 if (negate)
2054 break;
2055 else
2056 goto back_unibyte_ok;
2059 if (!fastmap[p[-1]])
2060 break;
2061 back_unibyte_ok:
2062 p--, pos--, pos_byte--;
2066 SET_PT_BOTH (pos, pos_byte);
2067 immediate_quit = false;
2069 SAFE_FREE ();
2070 return make_number (PT - start_point);
2075 static Lisp_Object
2076 skip_syntaxes (bool forwardp, Lisp_Object string, Lisp_Object lim)
2078 int c;
2079 unsigned char fastmap[0400];
2080 bool negate = 0;
2081 ptrdiff_t i, i_byte;
2082 bool multibyte;
2083 ptrdiff_t size_byte;
2084 unsigned char *str;
2086 CHECK_STRING (string);
2088 if (NILP (lim))
2089 XSETINT (lim, forwardp ? ZV : BEGV);
2090 else
2091 CHECK_NUMBER_COERCE_MARKER (lim);
2093 /* In any case, don't allow scan outside bounds of buffer. */
2094 if (XINT (lim) > ZV)
2095 XSETFASTINT (lim, ZV);
2096 if (XINT (lim) < BEGV)
2097 XSETFASTINT (lim, BEGV);
2099 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
2100 return make_number (0);
2102 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
2103 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
2105 memset (fastmap, 0, sizeof fastmap);
2107 if (SBYTES (string) > SCHARS (string))
2108 /* As this is very rare case (syntax spec is ASCII only), don't
2109 consider efficiency. */
2110 string = string_make_unibyte (string);
2112 str = SDATA (string);
2113 size_byte = SBYTES (string);
2115 i_byte = 0;
2116 if (i_byte < size_byte
2117 && SREF (string, 0) == '^')
2119 negate = 1; i_byte++;
2122 /* Find the syntaxes specified and set their elements of fastmap. */
2124 while (i_byte < size_byte)
2126 c = str[i_byte++];
2127 fastmap[syntax_spec_code[c]] = 1;
2130 /* If ^ was the first character, complement the fastmap. */
2131 if (negate)
2132 for (i = 0; i < sizeof fastmap; i++)
2133 fastmap[i] ^= 1;
2136 ptrdiff_t start_point = PT;
2137 ptrdiff_t pos = PT;
2138 ptrdiff_t pos_byte = PT_BYTE;
2139 unsigned char *p, *endp, *stop;
2141 immediate_quit = true;
2142 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
2144 if (forwardp)
2146 while (true)
2148 p = BYTE_POS_ADDR (pos_byte);
2149 endp = XINT (lim) == GPT ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
2150 stop = pos < GPT && GPT < XINT (lim) ? GPT_ADDR : endp;
2154 int nbytes;
2156 if (p >= stop)
2158 if (p >= endp)
2159 goto done;
2160 p = GAP_END_ADDR;
2161 stop = endp;
2163 if (multibyte)
2164 c = STRING_CHAR_AND_LENGTH (p, nbytes);
2165 else
2166 c = *p, nbytes = 1;
2167 if (! fastmap[SYNTAX (c)])
2168 goto done;
2169 p += nbytes, pos++, pos_byte += nbytes;
2171 while (!parse_sexp_lookup_properties
2172 || pos < gl_state.e_property);
2174 update_syntax_table_forward (pos + gl_state.offset,
2175 false, gl_state.object);
2178 else
2180 p = BYTE_POS_ADDR (pos_byte);
2181 endp = CHAR_POS_ADDR (XINT (lim));
2182 stop = pos >= GPT && GPT > XINT (lim) ? GAP_END_ADDR : endp;
2184 if (multibyte)
2186 while (1)
2188 unsigned char *prev_p;
2190 if (p <= stop)
2192 if (p <= endp)
2193 break;
2194 p = GPT_ADDR;
2195 stop = endp;
2197 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2198 prev_p = p;
2199 while (--p >= stop && ! CHAR_HEAD_P (*p));
2200 c = STRING_CHAR (p);
2201 if (! fastmap[SYNTAX (c)])
2202 break;
2203 pos--, pos_byte -= prev_p - p;
2206 else
2208 while (1)
2210 if (p <= stop)
2212 if (p <= endp)
2213 break;
2214 p = GPT_ADDR;
2215 stop = endp;
2217 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2218 if (! fastmap[SYNTAX (p[-1])])
2219 break;
2220 p--, pos--, pos_byte--;
2225 done:
2226 SET_PT_BOTH (pos, pos_byte);
2227 immediate_quit = false;
2229 return make_number (PT - start_point);
2233 /* Return true if character C belongs to one of the ISO classes
2234 in the list ISO_CLASSES. Each class is represented by an
2235 integer which is its type according to re_wctype. */
2237 static bool
2238 in_classes (int c, Lisp_Object iso_classes)
2240 bool fits_class = 0;
2242 while (CONSP (iso_classes))
2244 Lisp_Object elt;
2245 elt = XCAR (iso_classes);
2246 iso_classes = XCDR (iso_classes);
2248 if (re_iswctype (c, XFASTINT (elt)))
2249 fits_class = 1;
2252 return fits_class;
2255 /* Jump over a comment, assuming we are at the beginning of one.
2256 FROM is the current position.
2257 FROM_BYTE is the bytepos corresponding to FROM.
2258 Do not move past STOP (a charpos).
2259 The comment over which we have to jump is of style STYLE
2260 (either SYNTAX_FLAGS_COMMENT_STYLE (foo) or ST_COMMENT_STYLE).
2261 NESTING should be positive to indicate the nesting at the beginning
2262 for nested comments and should be zero or negative else.
2263 ST_COMMENT_STYLE cannot be nested.
2264 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2265 (or 0 If the search cannot start in the middle of a two-character).
2267 If successful, return true and store the charpos of the comment's
2268 end into *CHARPOS_PTR and the corresponding bytepos into
2269 *BYTEPOS_PTR. Else, return false and store the charpos STOP into
2270 *CHARPOS_PTR, the corresponding bytepos into *BYTEPOS_PTR and the
2271 current nesting (as defined for state->incomment) in
2272 *INCOMMENT_PTR. Should the last character scanned in an incomplete
2273 comment be a possible first character of a two character construct,
2274 we store its SYNTAX_WITH_FLAGS into *last_syntax_ptr. Otherwise,
2275 we store Smax into *last_syntax_ptr.
2277 The comment end is the last character of the comment rather than the
2278 character just after the comment.
2280 Global syntax data is assumed to initially be valid for FROM and
2281 remains valid for forward search starting at the returned position. */
2283 static bool
2284 forw_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop,
2285 EMACS_INT nesting, int style, int prev_syntax,
2286 ptrdiff_t *charpos_ptr, ptrdiff_t *bytepos_ptr,
2287 EMACS_INT *incomment_ptr, int *last_syntax_ptr)
2289 register int c, c1;
2290 register enum syntaxcode code;
2291 register int syntax, other_syntax;
2293 if (nesting <= 0) nesting = -1;
2295 /* Enter the loop in the middle so that we find
2296 a 2-char comment ender if we start in the middle of it. */
2297 syntax = prev_syntax;
2298 code = syntax & 0xff;
2299 if (syntax != 0 && from < stop) goto forw_incomment;
2301 while (1)
2303 if (from == stop)
2305 *incomment_ptr = nesting;
2306 *charpos_ptr = from;
2307 *bytepos_ptr = from_byte;
2308 *last_syntax_ptr =
2309 (code == Sescape || code == Scharquote
2310 || SYNTAX_FLAGS_COMEND_FIRST (syntax)
2311 || (nesting > 0
2312 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)))
2313 ? syntax : Smax ;
2314 return 0;
2316 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2317 syntax = SYNTAX_WITH_FLAGS (c);
2318 code = syntax & 0xff;
2319 if (code == Sendcomment
2320 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style
2321 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2322 (nesting > 0 && --nesting == 0) : nesting < 0)
2323 && !(Vcomment_end_can_be_escaped && char_quoted (from, from_byte)))
2324 /* We have encountered a comment end of the same style
2325 as the comment sequence which began this comment
2326 section. */
2327 break;
2328 if (code == Scomment_fence
2329 && style == ST_COMMENT_STYLE)
2330 /* We have encountered a comment end of the same style
2331 as the comment sequence which began this comment
2332 section. */
2333 break;
2334 if (nesting > 0
2335 && code == Scomment
2336 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2337 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style)
2338 /* We have encountered a nested comment of the same style
2339 as the comment sequence which began this comment section. */
2340 nesting++;
2341 INC_BOTH (from, from_byte);
2342 UPDATE_SYNTAX_TABLE_FORWARD (from);
2344 forw_incomment:
2345 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2346 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2347 other_syntax = SYNTAX_WITH_FLAGS (c1),
2348 SYNTAX_FLAGS_COMEND_SECOND (other_syntax))
2349 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, other_syntax) == style
2350 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2351 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax))
2352 ? nesting > 0 : nesting < 0))
2354 syntax = Smax; /* So that "|#" (lisp) can not return
2355 the syntax of "#" in *last_syntax_ptr. */
2356 if (--nesting <= 0)
2357 /* We have encountered a comment end of the same style
2358 as the comment sequence which began this comment section. */
2359 break;
2360 else
2362 INC_BOTH (from, from_byte);
2363 UPDATE_SYNTAX_TABLE_FORWARD (from);
2366 if (nesting > 0
2367 && from < stop
2368 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2369 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2370 other_syntax = SYNTAX_WITH_FLAGS (c1),
2371 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax) == style
2372 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2373 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2374 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax)))
2375 /* We have encountered a nested comment of the same style
2376 as the comment sequence which began this comment section. */
2378 syntax = Smax; /* So that "#|#" isn't also a comment ender. */
2379 INC_BOTH (from, from_byte);
2380 UPDATE_SYNTAX_TABLE_FORWARD (from);
2381 nesting++;
2384 *charpos_ptr = from;
2385 *bytepos_ptr = from_byte;
2386 *last_syntax_ptr = Smax; /* Any syntactic power the last byte had is
2387 used up. */
2388 return 1;
2391 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2392 doc: /*
2393 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2394 Stop scanning if we find something other than a comment or whitespace.
2395 Set point to where scanning stops.
2396 If COUNT comments are found as expected, with nothing except whitespace
2397 between them, return t; otherwise return nil. */)
2398 (Lisp_Object count)
2400 ptrdiff_t from, from_byte, stop;
2401 int c, c1;
2402 enum syntaxcode code;
2403 int comstyle = 0; /* style of comment encountered */
2404 bool comnested = 0; /* whether the comment is nestable or not */
2405 bool found;
2406 EMACS_INT count1;
2407 ptrdiff_t out_charpos, out_bytepos;
2408 EMACS_INT dummy;
2409 int dummy2;
2411 CHECK_NUMBER (count);
2412 count1 = XINT (count);
2413 stop = count1 > 0 ? ZV : BEGV;
2415 immediate_quit = true;
2416 maybe_quit ();
2418 from = PT;
2419 from_byte = PT_BYTE;
2421 SETUP_SYNTAX_TABLE (from, count1);
2422 while (count1 > 0)
2426 bool comstart_first;
2427 int syntax, other_syntax;
2429 if (from == stop)
2431 SET_PT_BOTH (from, from_byte);
2432 immediate_quit = false;
2433 return Qnil;
2435 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2436 syntax = SYNTAX_WITH_FLAGS (c);
2437 code = SYNTAX (c);
2438 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2439 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2440 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2441 INC_BOTH (from, from_byte);
2442 UPDATE_SYNTAX_TABLE_FORWARD (from);
2443 if (from < stop && comstart_first
2444 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2445 other_syntax = SYNTAX_WITH_FLAGS (c1),
2446 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)))
2448 /* We have encountered a comment start sequence and we
2449 are ignoring all text inside comments. We must record
2450 the comment style this sequence begins so that later,
2451 only a comment end of the same style actually ends
2452 the comment section. */
2453 code = Scomment;
2454 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2455 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2456 INC_BOTH (from, from_byte);
2457 UPDATE_SYNTAX_TABLE_FORWARD (from);
2460 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2462 if (code == Scomment_fence)
2463 comstyle = ST_COMMENT_STYLE;
2464 else if (code != Scomment)
2466 immediate_quit = false;
2467 DEC_BOTH (from, from_byte);
2468 SET_PT_BOTH (from, from_byte);
2469 return Qnil;
2471 /* We're at the start of a comment. */
2472 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2473 &out_charpos, &out_bytepos, &dummy, &dummy2);
2474 from = out_charpos; from_byte = out_bytepos;
2475 if (!found)
2477 immediate_quit = false;
2478 SET_PT_BOTH (from, from_byte);
2479 return Qnil;
2481 INC_BOTH (from, from_byte);
2482 UPDATE_SYNTAX_TABLE_FORWARD (from);
2483 /* We have skipped one comment. */
2484 count1--;
2487 while (count1 < 0)
2489 while (1)
2491 bool quoted;
2492 int syntax;
2494 if (from <= stop)
2496 SET_PT_BOTH (BEGV, BEGV_BYTE);
2497 immediate_quit = false;
2498 return Qnil;
2501 DEC_BOTH (from, from_byte);
2502 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2503 quoted = char_quoted (from, from_byte);
2504 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2505 syntax = SYNTAX_WITH_FLAGS (c);
2506 code = SYNTAX (c);
2507 comstyle = 0;
2508 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2509 if (code == Sendcomment)
2510 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2511 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2512 && prev_char_comend_first (from, from_byte)
2513 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2515 int other_syntax;
2516 /* We must record the comment style encountered so that
2517 later, we can match only the proper comment begin
2518 sequence of the same style. */
2519 DEC_BOTH (from, from_byte);
2520 code = Sendcomment;
2521 /* Calling char_quoted, above, set up global syntax position
2522 at the new value of FROM. */
2523 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2524 other_syntax = SYNTAX_WITH_FLAGS (c1);
2525 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2526 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2529 if (code == Scomment_fence)
2531 /* Skip until first preceding unquoted comment_fence. */
2532 bool fence_found = 0;
2533 ptrdiff_t ini = from, ini_byte = from_byte;
2535 while (1)
2537 DEC_BOTH (from, from_byte);
2538 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2539 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2540 if (SYNTAX (c) == Scomment_fence
2541 && !char_quoted (from, from_byte))
2543 fence_found = 1;
2544 break;
2546 else if (from == stop)
2547 break;
2549 if (fence_found == 0)
2551 from = ini; /* Set point to ini + 1. */
2552 from_byte = ini_byte;
2553 goto leave;
2555 else
2556 /* We have skipped one comment. */
2557 break;
2559 else if (code == Sendcomment)
2561 found = back_comment (from, from_byte, stop, comnested, comstyle,
2562 &out_charpos, &out_bytepos);
2563 if (!found)
2565 if (c == '\n')
2566 /* This end-of-line is not an end-of-comment.
2567 Treat it like a whitespace.
2568 CC-mode (and maybe others) relies on this behavior. */
2570 else
2572 /* Failure: we should go back to the end of this
2573 not-quite-endcomment. */
2574 if (SYNTAX (c) != code)
2575 /* It was a two-char Sendcomment. */
2576 INC_BOTH (from, from_byte);
2577 goto leave;
2580 else
2582 /* We have skipped one comment. */
2583 from = out_charpos, from_byte = out_bytepos;
2584 break;
2587 else if (code != Swhitespace || quoted)
2589 leave:
2590 immediate_quit = false;
2591 INC_BOTH (from, from_byte);
2592 SET_PT_BOTH (from, from_byte);
2593 return Qnil;
2597 count1++;
2600 SET_PT_BOTH (from, from_byte);
2601 immediate_quit = false;
2602 return Qt;
2605 /* Return syntax code of character C if C is an ASCII character
2606 or if MULTIBYTE_SYMBOL_P is false. Otherwise, return Ssymbol. */
2608 static enum syntaxcode
2609 syntax_multibyte (int c, bool multibyte_symbol_p)
2611 return ASCII_CHAR_P (c) || !multibyte_symbol_p ? SYNTAX (c) : Ssymbol;
2614 static Lisp_Object
2615 scan_lists (EMACS_INT from, EMACS_INT count, EMACS_INT depth, bool sexpflag)
2617 Lisp_Object val;
2618 ptrdiff_t stop = count > 0 ? ZV : BEGV;
2619 int c, c1;
2620 int stringterm;
2621 bool quoted;
2622 bool mathexit = 0;
2623 enum syntaxcode code;
2624 EMACS_INT min_depth = depth; /* Err out if depth gets less than this. */
2625 int comstyle = 0; /* Style of comment encountered. */
2626 bool comnested = 0; /* Whether the comment is nestable or not. */
2627 ptrdiff_t temp_pos;
2628 EMACS_INT last_good = from;
2629 bool found;
2630 ptrdiff_t from_byte;
2631 ptrdiff_t out_bytepos, out_charpos;
2632 EMACS_INT dummy;
2633 int dummy2;
2634 bool multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2636 if (depth > 0) min_depth = 0;
2638 if (from > ZV) from = ZV;
2639 if (from < BEGV) from = BEGV;
2641 from_byte = CHAR_TO_BYTE (from);
2643 immediate_quit = true;
2644 maybe_quit ();
2646 SETUP_SYNTAX_TABLE (from, count);
2647 while (count > 0)
2649 while (from < stop)
2651 bool comstart_first, prefix;
2652 int syntax, other_syntax;
2653 UPDATE_SYNTAX_TABLE_FORWARD (from);
2654 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2655 syntax = SYNTAX_WITH_FLAGS (c);
2656 code = syntax_multibyte (c, multibyte_symbol_p);
2657 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2658 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2659 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2660 prefix = SYNTAX_FLAGS_PREFIX (syntax);
2661 if (depth == min_depth)
2662 last_good = from;
2663 INC_BOTH (from, from_byte);
2664 UPDATE_SYNTAX_TABLE_FORWARD (from);
2665 if (from < stop && comstart_first
2666 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2667 other_syntax = SYNTAX_WITH_FLAGS (c),
2668 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2669 && parse_sexp_ignore_comments)
2671 /* We have encountered a comment start sequence and we
2672 are ignoring all text inside comments. We must record
2673 the comment style this sequence begins so that later,
2674 only a comment end of the same style actually ends
2675 the comment section. */
2676 code = Scomment;
2677 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2678 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2679 INC_BOTH (from, from_byte);
2680 UPDATE_SYNTAX_TABLE_FORWARD (from);
2683 if (prefix)
2684 continue;
2686 switch (code)
2688 case Sescape:
2689 case Scharquote:
2690 if (from == stop)
2691 goto lose;
2692 INC_BOTH (from, from_byte);
2693 /* Treat following character as a word constituent. */
2694 case Sword:
2695 case Ssymbol:
2696 if (depth || !sexpflag) break;
2697 /* This word counts as a sexp; return at end of it. */
2698 while (from < stop)
2700 UPDATE_SYNTAX_TABLE_FORWARD (from);
2702 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2703 switch (syntax_multibyte (c, multibyte_symbol_p))
2705 case Scharquote:
2706 case Sescape:
2707 INC_BOTH (from, from_byte);
2708 if (from == stop)
2709 goto lose;
2710 break;
2711 case Sword:
2712 case Ssymbol:
2713 case Squote:
2714 break;
2715 default:
2716 goto done;
2718 INC_BOTH (from, from_byte);
2720 goto done;
2722 case Scomment_fence:
2723 comstyle = ST_COMMENT_STYLE;
2724 /* FALLTHROUGH */
2725 case Scomment:
2726 if (!parse_sexp_ignore_comments) break;
2727 UPDATE_SYNTAX_TABLE_FORWARD (from);
2728 found = forw_comment (from, from_byte, stop,
2729 comnested, comstyle, 0,
2730 &out_charpos, &out_bytepos, &dummy,
2731 &dummy2);
2732 from = out_charpos, from_byte = out_bytepos;
2733 if (!found)
2735 if (depth == 0)
2736 goto done;
2737 goto lose;
2739 INC_BOTH (from, from_byte);
2740 UPDATE_SYNTAX_TABLE_FORWARD (from);
2741 break;
2743 case Smath:
2744 if (!sexpflag)
2745 break;
2746 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2748 INC_BOTH (from, from_byte);
2750 if (mathexit)
2752 mathexit = 0;
2753 goto close1;
2755 mathexit = 1;
2757 case Sopen:
2758 if (!++depth) goto done;
2759 break;
2761 case Sclose:
2762 close1:
2763 if (!--depth) goto done;
2764 if (depth < min_depth)
2765 xsignal3 (Qscan_error,
2766 build_string ("Containing expression ends prematurely"),
2767 make_number (last_good), make_number (from));
2768 break;
2770 case Sstring:
2771 case Sstring_fence:
2772 temp_pos = dec_bytepos (from_byte);
2773 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2774 while (1)
2776 enum syntaxcode c_code;
2777 if (from >= stop)
2778 goto lose;
2779 UPDATE_SYNTAX_TABLE_FORWARD (from);
2780 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2781 c_code = syntax_multibyte (c, multibyte_symbol_p);
2782 if (code == Sstring
2783 ? c == stringterm && c_code == Sstring
2784 : c_code == Sstring_fence)
2785 break;
2787 if (c_code == Scharquote || c_code == Sescape)
2788 INC_BOTH (from, from_byte);
2789 INC_BOTH (from, from_byte);
2791 INC_BOTH (from, from_byte);
2792 if (!depth && sexpflag) goto done;
2793 break;
2794 default:
2795 /* Ignore whitespace, punctuation, quote, endcomment. */
2796 break;
2800 /* Reached end of buffer. Error if within object, return nil if between */
2801 if (depth)
2802 goto lose;
2804 immediate_quit = false;
2805 return Qnil;
2807 /* End of object reached */
2808 done:
2809 count--;
2812 while (count < 0)
2814 while (from > stop)
2816 int syntax;
2817 DEC_BOTH (from, from_byte);
2818 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2819 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2820 syntax= SYNTAX_WITH_FLAGS (c);
2821 code = syntax_multibyte (c, multibyte_symbol_p);
2822 if (depth == min_depth)
2823 last_good = from;
2824 comstyle = 0;
2825 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2826 if (code == Sendcomment)
2827 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2828 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2829 && prev_char_comend_first (from, from_byte)
2830 && parse_sexp_ignore_comments)
2832 /* We must record the comment style encountered so that
2833 later, we can match only the proper comment begin
2834 sequence of the same style. */
2835 int c2, other_syntax;
2836 DEC_BOTH (from, from_byte);
2837 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2838 code = Sendcomment;
2839 c2 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2840 other_syntax = SYNTAX_WITH_FLAGS (c2);
2841 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2842 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2845 /* Quoting turns anything except a comment-ender
2846 into a word character. Note that this cannot be true
2847 if we decremented FROM in the if-statement above. */
2848 if (code != Sendcomment && char_quoted (from, from_byte))
2850 DEC_BOTH (from, from_byte);
2851 code = Sword;
2853 else if (SYNTAX_FLAGS_PREFIX (syntax))
2854 continue;
2856 switch (code)
2858 case Sword:
2859 case Ssymbol:
2860 case Sescape:
2861 case Scharquote:
2862 if (depth || !sexpflag) break;
2863 /* This word counts as a sexp; count object finished
2864 after passing it. */
2865 while (from > stop)
2867 temp_pos = from_byte;
2868 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2869 DEC_POS (temp_pos);
2870 else
2871 temp_pos--;
2872 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2873 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2874 /* Don't allow comment-end to be quoted. */
2875 if (syntax_multibyte (c1, multibyte_symbol_p) == Sendcomment)
2876 goto done2;
2877 quoted = char_quoted (from - 1, temp_pos);
2878 if (quoted)
2880 DEC_BOTH (from, from_byte);
2881 temp_pos = dec_bytepos (temp_pos);
2882 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2884 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2885 if (! quoted)
2886 switch (syntax_multibyte (c1, multibyte_symbol_p))
2888 case Sword: case Ssymbol: case Squote: break;
2889 default: goto done2;
2891 DEC_BOTH (from, from_byte);
2893 goto done2;
2895 case Smath:
2896 if (!sexpflag)
2897 break;
2898 if (from > BEGV)
2900 temp_pos = dec_bytepos (from_byte);
2901 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2902 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2903 DEC_BOTH (from, from_byte);
2905 if (mathexit)
2907 mathexit = 0;
2908 goto open2;
2910 mathexit = 1;
2912 case Sclose:
2913 if (!++depth) goto done2;
2914 break;
2916 case Sopen:
2917 open2:
2918 if (!--depth) goto done2;
2919 if (depth < min_depth)
2920 xsignal3 (Qscan_error,
2921 build_string ("Containing expression ends prematurely"),
2922 make_number (last_good), make_number (from));
2923 break;
2925 case Sendcomment:
2926 if (!parse_sexp_ignore_comments)
2927 break;
2928 found = back_comment (from, from_byte, stop, comnested, comstyle,
2929 &out_charpos, &out_bytepos);
2930 /* FIXME: if !found, it really wasn't a comment-end.
2931 For single-char Sendcomment, we can't do much about it apart
2932 from skipping the char.
2933 For 2-char endcomments, we could try again, taking both
2934 chars as separate entities, but it's a lot of trouble
2935 for very little gain, so we don't bother either. -sm */
2936 if (found)
2937 from = out_charpos, from_byte = out_bytepos;
2938 break;
2940 case Scomment_fence:
2941 case Sstring_fence:
2942 while (1)
2944 if (from == stop)
2945 goto lose;
2946 DEC_BOTH (from, from_byte);
2947 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2948 if (!char_quoted (from, from_byte))
2950 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2951 if (syntax_multibyte (c, multibyte_symbol_p) == code)
2952 break;
2955 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2956 break;
2958 case Sstring:
2959 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2960 while (1)
2962 if (from == stop)
2963 goto lose;
2964 DEC_BOTH (from, from_byte);
2965 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2966 if (!char_quoted (from, from_byte))
2968 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2969 if (c == stringterm
2970 && (syntax_multibyte (c, multibyte_symbol_p)
2971 == Sstring))
2972 break;
2975 if (!depth && sexpflag) goto done2;
2976 break;
2977 default:
2978 /* Ignore whitespace, punctuation, quote, endcomment. */
2979 break;
2983 /* Reached start of buffer. Error if within object, return nil if between */
2984 if (depth)
2985 goto lose;
2987 immediate_quit = false;
2988 return Qnil;
2990 done2:
2991 count++;
2995 immediate_quit = false;
2996 XSETFASTINT (val, from);
2997 return val;
2999 lose:
3000 xsignal3 (Qscan_error,
3001 build_string ("Unbalanced parentheses"),
3002 make_number (last_good), make_number (from));
3005 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
3006 doc: /* Scan from character number FROM by COUNT lists.
3007 Scan forward if COUNT is positive, backward if COUNT is negative.
3008 Return the character number of the position thus found.
3010 A \"list", in this context, refers to a balanced parenthetical
3011 grouping, as determined by the syntax table.
3013 If DEPTH is nonzero, treat that as the nesting depth of the starting
3014 point (i.e. the starting point is DEPTH parentheses deep). This
3015 function scans over parentheses until the depth goes to zero COUNT
3016 times. Hence, positive DEPTH moves out that number of levels of
3017 parentheses, while negative DEPTH moves to a deeper level.
3019 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3021 If we reach the beginning or end of the accessible part of the buffer
3022 before we have scanned over COUNT lists, return nil if the depth at
3023 that point is zero, and signal a error if the depth is nonzero. */)
3024 (Lisp_Object from, Lisp_Object count, Lisp_Object depth)
3026 CHECK_NUMBER (from);
3027 CHECK_NUMBER (count);
3028 CHECK_NUMBER (depth);
3030 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
3033 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
3034 doc: /* Scan from character number FROM by COUNT balanced expressions.
3035 If COUNT is negative, scan backwards.
3036 Returns the character number of the position thus found.
3038 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3040 If the beginning or end of (the accessible part of) the buffer is reached
3041 in the middle of a parenthetical grouping, an error is signaled.
3042 If the beginning or end is reached between groupings
3043 but before count is used up, nil is returned. */)
3044 (Lisp_Object from, Lisp_Object count)
3046 CHECK_NUMBER (from);
3047 CHECK_NUMBER (count);
3049 return scan_lists (XINT (from), XINT (count), 0, 1);
3052 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
3053 0, 0, 0,
3054 doc: /* Move point backward over any number of chars with prefix syntax.
3055 This includes chars with expression prefix syntax class (\\=') and those with
3056 the prefix syntax flag (p). */)
3057 (void)
3059 ptrdiff_t beg = BEGV;
3060 ptrdiff_t opoint = PT;
3061 ptrdiff_t opoint_byte = PT_BYTE;
3062 ptrdiff_t pos = PT;
3063 ptrdiff_t pos_byte = PT_BYTE;
3064 int c;
3066 if (pos <= beg)
3068 SET_PT_BOTH (opoint, opoint_byte);
3070 return Qnil;
3073 SETUP_SYNTAX_TABLE (pos, -1);
3075 DEC_BOTH (pos, pos_byte);
3077 while (!char_quoted (pos, pos_byte)
3078 /* Previous statement updates syntax table. */
3079 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
3080 || syntax_prefix_flag_p (c)))
3082 opoint = pos;
3083 opoint_byte = pos_byte;
3085 if (pos <= beg)
3086 break;
3087 DEC_BOTH (pos, pos_byte);
3090 SET_PT_BOTH (opoint, opoint_byte);
3092 return Qnil;
3096 /* If the character at FROM_BYTE is the second part of a 2-character
3097 comment opener based on PREV_FROM_SYNTAX, update STATE and return
3098 true. */
3099 static bool
3100 in_2char_comment_start (struct lisp_parse_state *state,
3101 int prev_from_syntax,
3102 ptrdiff_t prev_from,
3103 ptrdiff_t from_byte)
3105 int c1, syntax;
3106 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3107 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
3108 syntax = SYNTAX_WITH_FLAGS (c1),
3109 SYNTAX_FLAGS_COMSTART_SECOND (syntax)))
3111 /* Record the comment style we have entered so that only
3112 the comment-end sequence of the same style actually
3113 terminates the comment section. */
3114 state->comstyle
3115 = SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_from_syntax);
3116 bool comnested = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax)
3117 | SYNTAX_FLAGS_COMMENT_NESTED (syntax));
3118 state->incomment = comnested ? 1 : -1;
3119 state->comstr_start = prev_from;
3120 return true;
3122 return false;
3125 /* Parse forward from FROM / FROM_BYTE to END,
3126 assuming that FROM has state STATE,
3127 and return a description of the state of the parse at END.
3128 If STOPBEFORE, stop at the start of an atom.
3129 If COMMENTSTOP is 1, stop at the start of a comment.
3130 If COMMENTSTOP is -1, stop at the start or end of a comment,
3131 after the beginning of a string, or after the end of a string. */
3133 static void
3134 scan_sexps_forward (struct lisp_parse_state *state,
3135 ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t end,
3136 EMACS_INT targetdepth, bool stopbefore,
3137 int commentstop)
3139 enum syntaxcode code;
3140 struct level { ptrdiff_t last, prev; };
3141 struct level levelstart[100];
3142 struct level *curlevel = levelstart;
3143 struct level *endlevel = levelstart + 100;
3144 EMACS_INT depth; /* Paren depth of current scanning location.
3145 level - levelstart equals this except
3146 when the depth becomes negative. */
3147 EMACS_INT mindepth; /* Lowest DEPTH value seen. */
3148 bool start_quoted = 0; /* True means starting after a char quote. */
3149 Lisp_Object tem;
3150 ptrdiff_t prev_from; /* Keep one character before FROM. */
3151 ptrdiff_t prev_from_byte;
3152 int prev_from_syntax, prev_prev_from_syntax;
3153 bool boundary_stop = commentstop == -1;
3154 bool nofence;
3155 bool found;
3156 ptrdiff_t out_bytepos, out_charpos;
3157 int temp;
3159 prev_from = from;
3160 prev_from_byte = from_byte;
3161 if (from != BEGV)
3162 DEC_BOTH (prev_from, prev_from_byte);
3164 /* Use this macro instead of `from++'. */
3165 #define INC_FROM \
3166 do { prev_from = from; \
3167 prev_from_byte = from_byte; \
3168 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
3169 prev_prev_from_syntax = prev_from_syntax; \
3170 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
3171 INC_BOTH (from, from_byte); \
3172 if (from < end) \
3173 UPDATE_SYNTAX_TABLE_FORWARD (from); \
3174 } while (0)
3176 immediate_quit = true;
3177 maybe_quit ();
3179 depth = state->depth;
3180 start_quoted = state->quoted;
3181 prev_prev_from_syntax = Smax;
3182 prev_from_syntax = state->prev_syntax;
3184 tem = state->levelstarts;
3185 while (!NILP (tem)) /* >= second enclosing sexps. */
3187 Lisp_Object temhd = Fcar (tem);
3188 if (RANGED_INTEGERP (PTRDIFF_MIN, temhd, PTRDIFF_MAX))
3189 curlevel->last = XINT (temhd);
3190 if (++curlevel == endlevel)
3191 curlevel--; /* error ("Nesting too deep for parser"); */
3192 curlevel->prev = -1;
3193 curlevel->last = -1;
3194 tem = Fcdr (tem);
3196 curlevel->prev = -1;
3197 curlevel->last = -1;
3199 state->quoted = 0;
3200 mindepth = depth;
3202 SETUP_SYNTAX_TABLE (from, 1);
3204 /* Enter the loop at a place appropriate for initial state. */
3206 if (state->incomment)
3207 goto startincomment;
3208 if (state->instring >= 0)
3210 nofence = state->instring != ST_STRING_STYLE;
3211 if (start_quoted)
3212 goto startquotedinstring;
3213 goto startinstring;
3215 else if (start_quoted)
3216 goto startquoted;
3217 else if ((from < end)
3218 && (in_2char_comment_start (state, prev_from_syntax,
3219 prev_from, from_byte)))
3221 INC_FROM;
3222 prev_from_syntax = Smax; /* the syntax has already been "used up". */
3223 goto atcomment;
3226 while (from < end)
3228 INC_FROM;
3230 if ((from < end)
3231 && (in_2char_comment_start (state, prev_from_syntax,
3232 prev_from, from_byte)))
3234 INC_FROM;
3235 prev_from_syntax = Smax; /* the syntax has already been "used up". */
3236 goto atcomment;
3239 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3240 continue;
3241 code = prev_from_syntax & 0xff;
3242 switch (code)
3244 case Sescape:
3245 case Scharquote:
3246 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3247 curlevel->last = prev_from;
3248 startquoted:
3249 if (from == end) goto endquoted;
3250 INC_FROM;
3251 goto symstarted;
3252 /* treat following character as a word constituent */
3253 case Sword:
3254 case Ssymbol:
3255 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3256 curlevel->last = prev_from;
3257 symstarted:
3258 while (from < end)
3260 if (in_2char_comment_start (state, prev_from_syntax,
3261 prev_from, from_byte))
3263 INC_FROM;
3264 prev_from_syntax = Smax; /* the syntax has already been "used up". */
3265 goto atcomment;
3268 int symchar = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3269 switch (SYNTAX (symchar))
3271 case Scharquote:
3272 case Sescape:
3273 INC_FROM;
3274 if (from == end) goto endquoted;
3275 break;
3276 case Sword:
3277 case Ssymbol:
3278 case Squote:
3279 break;
3280 default:
3281 goto symdone;
3283 INC_FROM;
3285 symdone:
3286 curlevel->prev = curlevel->last;
3287 break;
3289 case Scomment_fence:
3290 /* Record the comment style we have entered so that only
3291 the comment-end sequence of the same style actually
3292 terminates the comment section. */
3293 state->comstyle = ST_COMMENT_STYLE;
3294 state->incomment = -1;
3295 state->comstr_start = prev_from;
3296 goto atcomment;
3297 case Scomment:
3298 state->comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax, 0);
3299 state->incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3300 1 : -1);
3301 state->comstr_start = prev_from;
3302 atcomment:
3303 if (commentstop || boundary_stop) goto done;
3304 startincomment:
3305 /* The (from == BEGV) test was to enter the loop in the middle so
3306 that we find a 2-char comment ender even if we start in the
3307 middle of it. We don't want to do that if we're just at the
3308 beginning of the comment (think of (*) ... (*)). */
3309 found = forw_comment (from, from_byte, end,
3310 state->incomment, state->comstyle,
3311 from == BEGV ? 0 : prev_from_syntax,
3312 &out_charpos, &out_bytepos, &state->incomment,
3313 &prev_from_syntax);
3314 from = out_charpos; from_byte = out_bytepos;
3315 /* Beware! prev_from and friends (except prev_from_syntax)
3316 are invalid now. Luckily, the `done' doesn't use them
3317 and the INC_FROM sets them to a sane value without
3318 looking at them. */
3319 if (!found) goto done;
3320 INC_FROM;
3321 state->incomment = 0;
3322 state->comstyle = 0; /* reset the comment style */
3323 prev_from_syntax = Smax; /* For the comment closer */
3324 if (boundary_stop) goto done;
3325 break;
3327 case Sopen:
3328 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3329 depth++;
3330 /* curlevel++->last ran into compiler bug on Apollo */
3331 curlevel->last = prev_from;
3332 if (++curlevel == endlevel)
3333 curlevel--; /* error ("Nesting too deep for parser"); */
3334 curlevel->prev = -1;
3335 curlevel->last = -1;
3336 if (targetdepth == depth) goto done;
3337 break;
3339 case Sclose:
3340 depth--;
3341 if (depth < mindepth)
3342 mindepth = depth;
3343 if (curlevel != levelstart)
3344 curlevel--;
3345 curlevel->prev = curlevel->last;
3346 if (targetdepth == depth) goto done;
3347 break;
3349 case Sstring:
3350 case Sstring_fence:
3351 state->comstr_start = from - 1;
3352 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3353 curlevel->last = prev_from;
3354 state->instring = (code == Sstring
3355 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3356 : ST_STRING_STYLE);
3357 if (boundary_stop) goto done;
3358 startinstring:
3360 nofence = state->instring != ST_STRING_STYLE;
3362 while (1)
3364 int c;
3365 enum syntaxcode c_code;
3367 if (from >= end) goto done;
3368 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3369 c_code = SYNTAX (c);
3371 /* Check C_CODE here so that if the char has
3372 a syntax-table property which says it is NOT
3373 a string character, it does not end the string. */
3374 if (nofence && c == state->instring && c_code == Sstring)
3375 break;
3377 switch (c_code)
3379 case Sstring_fence:
3380 if (!nofence) goto string_end;
3381 break;
3383 case Scharquote:
3384 case Sescape:
3385 INC_FROM;
3386 startquotedinstring:
3387 if (from >= end) goto endquoted;
3388 break;
3390 default:
3391 break;
3393 INC_FROM;
3396 string_end:
3397 state->instring = -1;
3398 curlevel->prev = curlevel->last;
3399 INC_FROM;
3400 if (boundary_stop) goto done;
3401 break;
3403 case Smath:
3404 /* FIXME: We should do something with it. */
3405 break;
3406 default:
3407 /* Ignore whitespace, punctuation, quote, endcomment. */
3408 break;
3411 goto done;
3413 stop: /* Here if stopping before start of sexp. */
3414 from = prev_from; /* We have just fetched the char that starts it; */
3415 from_byte = prev_from_byte;
3416 prev_from_syntax = prev_prev_from_syntax;
3417 goto done; /* but return the position before it. */
3419 endquoted:
3420 state->quoted = 1;
3421 done:
3422 state->depth = depth;
3423 state->mindepth = mindepth;
3424 state->thislevelstart = curlevel->prev;
3425 state->prevlevelstart
3426 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3427 state->location = from;
3428 state->location_byte = from_byte;
3429 state->levelstarts = Qnil;
3430 while (curlevel > levelstart)
3431 state->levelstarts = Fcons (make_number ((--curlevel)->last),
3432 state->levelstarts);
3433 state->prev_syntax = (SYNTAX_FLAGS_COMSTARTEND_FIRST (prev_from_syntax)
3434 || state->quoted) ? prev_from_syntax : Smax;
3435 immediate_quit = false;
3438 /* Convert a (lisp) parse state to the internal form used in
3439 scan_sexps_forward. */
3440 static void
3441 internalize_parse_state (Lisp_Object external, struct lisp_parse_state *state)
3443 Lisp_Object tem;
3445 if (NILP (external))
3447 state->depth = 0;
3448 state->instring = -1;
3449 state->incomment = 0;
3450 state->quoted = 0;
3451 state->comstyle = 0; /* comment style a by default. */
3452 state->comstr_start = -1; /* no comment/string seen. */
3453 state->levelstarts = Qnil;
3454 state->prev_syntax = Smax;
3456 else
3458 tem = Fcar (external);
3459 if (!NILP (tem))
3460 state->depth = XINT (tem);
3461 else
3462 state->depth = 0;
3464 external = Fcdr (external);
3465 external = Fcdr (external);
3466 external = Fcdr (external);
3467 tem = Fcar (external);
3468 /* Check whether we are inside string_fence-style string: */
3469 state->instring = (!NILP (tem)
3470 ? (CHARACTERP (tem) ? XFASTINT (tem) : ST_STRING_STYLE)
3471 : -1);
3473 external = Fcdr (external);
3474 tem = Fcar (external);
3475 state->incomment = (!NILP (tem)
3476 ? (INTEGERP (tem) ? XINT (tem) : -1)
3477 : 0);
3479 external = Fcdr (external);
3480 tem = Fcar (external);
3481 state->quoted = !NILP (tem);
3483 /* if the eighth element of the list is nil, we are in comment
3484 style a. If it is non-nil, we are in comment style b */
3485 external = Fcdr (external);
3486 external = Fcdr (external);
3487 tem = Fcar (external);
3488 state->comstyle = (NILP (tem)
3490 : (RANGED_INTEGERP (0, tem, ST_COMMENT_STYLE)
3491 ? XINT (tem)
3492 : ST_COMMENT_STYLE));
3494 external = Fcdr (external);
3495 tem = Fcar (external);
3496 state->comstr_start =
3497 RANGED_INTEGERP (PTRDIFF_MIN, tem, PTRDIFF_MAX) ? XINT (tem) : -1;
3498 external = Fcdr (external);
3499 tem = Fcar (external);
3500 state->levelstarts = tem;
3502 external = Fcdr (external);
3503 tem = Fcar (external);
3504 state->prev_syntax = NILP (tem) ? Smax : XINT (tem);
3508 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3509 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3510 Parsing stops at TO or when certain criteria are met;
3511 point is set to where parsing stops.
3512 If fifth arg OLDSTATE is omitted or nil,
3513 parsing assumes that FROM is the beginning of a function.
3515 Value is a list of elements describing final state of parsing:
3516 0. depth in parens.
3517 1. character address of start of innermost containing list; nil if none.
3518 2. character address of start of last complete sexp terminated.
3519 3. non-nil if inside a string.
3520 (it is the character that will terminate the string,
3521 or t if the string should be terminated by a generic string delimiter.)
3522 4. nil if outside a comment, t if inside a non-nestable comment,
3523 else an integer (the current comment nesting).
3524 5. t if following a quote character.
3525 6. the minimum paren-depth encountered during this scan.
3526 7. style of comment, if any.
3527 8. character address of start of comment or string; nil if not in one.
3528 9. List of positions of currently open parens, outermost first.
3529 10. When the last position scanned holds the first character of a
3530 (potential) two character construct, the syntax of that position,
3531 otherwise nil. That construct can be a two character comment
3532 delimiter or an Escaped or Char-quoted character.
3533 11..... Possible further internal information used by `parse-partial-sexp'.
3535 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3536 in parentheses becomes equal to TARGETDEPTH.
3537 Fourth arg STOPBEFORE non-nil means stop when we come to
3538 any character that starts a sexp.
3539 Fifth arg OLDSTATE is a list like what this function returns.
3540 It is used to initialize the state of the parse. Elements number 1, 2, 6
3541 are ignored.
3542 Sixth arg COMMENTSTOP non-nil means stop after the start of a comment.
3543 If it is the symbol `syntax-table', stop after the start of a comment or a
3544 string, or after end of a comment or a string. */)
3545 (Lisp_Object from, Lisp_Object to, Lisp_Object targetdepth,
3546 Lisp_Object stopbefore, Lisp_Object oldstate, Lisp_Object commentstop)
3548 struct lisp_parse_state state;
3549 EMACS_INT target;
3551 if (!NILP (targetdepth))
3553 CHECK_NUMBER (targetdepth);
3554 target = XINT (targetdepth);
3556 else
3557 target = TYPE_MINIMUM (EMACS_INT); /* We won't reach this depth. */
3559 validate_region (&from, &to);
3560 internalize_parse_state (oldstate, &state);
3561 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3562 XINT (to),
3563 target, !NILP (stopbefore),
3564 (NILP (commentstop)
3565 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3567 SET_PT_BOTH (state.location, state.location_byte);
3569 return
3570 Fcons (make_number (state.depth),
3571 Fcons (state.prevlevelstart < 0
3572 ? Qnil : make_number (state.prevlevelstart),
3573 Fcons (state.thislevelstart < 0
3574 ? Qnil : make_number (state.thislevelstart),
3575 Fcons (state.instring >= 0
3576 ? (state.instring == ST_STRING_STYLE
3577 ? Qt : make_number (state.instring)) : Qnil,
3578 Fcons (state.incomment < 0 ? Qt :
3579 (state.incomment == 0 ? Qnil :
3580 make_number (state.incomment)),
3581 Fcons (state.quoted ? Qt : Qnil,
3582 Fcons (make_number (state.mindepth),
3583 Fcons ((state.comstyle
3584 ? (state.comstyle == ST_COMMENT_STYLE
3585 ? Qsyntax_table
3586 : make_number (state.comstyle))
3587 : Qnil),
3588 Fcons (((state.incomment
3589 || (state.instring >= 0))
3590 ? make_number (state.comstr_start)
3591 : Qnil),
3592 Fcons (state.levelstarts,
3593 Fcons (state.prev_syntax == Smax
3594 ? Qnil
3595 : make_number (state.prev_syntax),
3596 Qnil)))))))))));
3599 void
3600 init_syntax_once (void)
3602 register int i, c;
3603 Lisp_Object temp;
3605 /* This has to be done here, before we call Fmake_char_table. */
3606 DEFSYM (Qsyntax_table, "syntax-table");
3608 /* Create objects which can be shared among syntax tables. */
3609 Vsyntax_code_object = make_uninit_vector (Smax);
3610 for (i = 0; i < Smax; i++)
3611 ASET (Vsyntax_code_object, i, Fcons (make_number (i), Qnil));
3613 /* Now we are ready to set up this property, so we can
3614 create syntax tables. */
3615 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3617 temp = AREF (Vsyntax_code_object, Swhitespace);
3619 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3621 /* Control characters should not be whitespace. */
3622 temp = AREF (Vsyntax_code_object, Spunct);
3623 for (i = 0; i <= ' ' - 1; i++)
3624 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3625 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3627 /* Except that a few really are whitespace. */
3628 temp = AREF (Vsyntax_code_object, Swhitespace);
3629 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3630 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3631 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3632 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3633 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3635 temp = AREF (Vsyntax_code_object, Sword);
3636 for (i = 'a'; i <= 'z'; i++)
3637 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3638 for (i = 'A'; i <= 'Z'; i++)
3639 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3640 for (i = '0'; i <= '9'; i++)
3641 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3643 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3644 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3646 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3647 Fcons (make_number (Sopen), make_number (')')));
3648 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3649 Fcons (make_number (Sclose), make_number ('(')));
3650 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3651 Fcons (make_number (Sopen), make_number (']')));
3652 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3653 Fcons (make_number (Sclose), make_number ('[')));
3654 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3655 Fcons (make_number (Sopen), make_number ('}')));
3656 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3657 Fcons (make_number (Sclose), make_number ('{')));
3658 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3659 Fcons (make_number (Sstring), Qnil));
3660 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3661 Fcons (make_number (Sescape), Qnil));
3663 temp = AREF (Vsyntax_code_object, Ssymbol);
3664 for (i = 0; i < 10; i++)
3666 c = "_-+*/&|<>="[i];
3667 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3670 temp = AREF (Vsyntax_code_object, Spunct);
3671 for (i = 0; i < 12; i++)
3673 c = ".,;:?!#@~^'`"[i];
3674 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3677 /* All multibyte characters have syntax `word' by default. */
3678 temp = AREF (Vsyntax_code_object, Sword);
3679 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3682 void
3683 syms_of_syntax (void)
3685 DEFSYM (Qsyntax_table_p, "syntax-table-p");
3687 staticpro (&Vsyntax_code_object);
3689 staticpro (&gl_state.object);
3690 staticpro (&gl_state.global_code);
3691 staticpro (&gl_state.current_syntax_table);
3692 staticpro (&gl_state.old_prop);
3694 /* Defined in regex.c. */
3695 staticpro (&re_match_object);
3697 DEFSYM (Qscan_error, "scan-error");
3698 Fput (Qscan_error, Qerror_conditions,
3699 listn (CONSTYPE_PURE, 2, Qscan_error, Qerror));
3700 Fput (Qscan_error, Qerror_message,
3701 build_pure_c_string ("Scan error"));
3703 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments,
3704 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3706 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties,
3707 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3708 Otherwise, that text property is simply ignored.
3709 See the info node `(elisp)Syntax Properties' for a description of the
3710 `syntax-table' property. */);
3712 DEFVAR_INT ("syntax-propertize--done", syntax_propertize__done,
3713 doc: /* Position up to which syntax-table properties have been set. */);
3714 syntax_propertize__done = -1;
3715 DEFSYM (Qinternal__syntax_propertize, "internal--syntax-propertize");
3716 Fmake_variable_buffer_local (intern ("syntax-propertize--done"));
3718 words_include_escapes = 0;
3719 DEFVAR_BOOL ("words-include-escapes", words_include_escapes,
3720 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3722 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol,
3723 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3724 multibyte_syntax_as_symbol = 0;
3726 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3727 open_paren_in_column_0_is_defun_start,
3728 doc: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3729 open_paren_in_column_0_is_defun_start = 1;
3732 DEFVAR_LISP ("find-word-boundary-function-table",
3733 Vfind_word_boundary_function_table,
3734 doc: /*
3735 Char table of functions to search for the word boundary.
3736 Each function is called with two arguments; POS and LIMIT.
3737 POS and LIMIT are character positions in the current buffer.
3739 If POS is less than LIMIT, POS is at the first character of a word,
3740 and the return value of a function should be a position after the
3741 last character of that word.
3743 If POS is not less than LIMIT, POS is at the last character of a word,
3744 and the return value of a function should be a position at the first
3745 character of that word.
3747 In both cases, LIMIT bounds the search. */);
3748 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3750 DEFVAR_BOOL ("comment-end-can-be-escaped", Vcomment_end_can_be_escaped,
3751 doc: /* Non-nil means an escaped ender inside a comment doesn't end the comment. */);
3752 Vcomment_end_can_be_escaped = 0;
3753 DEFSYM (Qcomment_end_can_be_escaped, "comment-end-can-be-escaped");
3754 Fmake_variable_buffer_local (Qcomment_end_can_be_escaped);
3756 defsubr (&Ssyntax_table_p);
3757 defsubr (&Ssyntax_table);
3758 defsubr (&Sstandard_syntax_table);
3759 defsubr (&Scopy_syntax_table);
3760 defsubr (&Sset_syntax_table);
3761 defsubr (&Schar_syntax);
3762 defsubr (&Smatching_paren);
3763 defsubr (&Sstring_to_syntax);
3764 defsubr (&Smodify_syntax_entry);
3765 defsubr (&Sinternal_describe_syntax_value);
3767 defsubr (&Sforward_word);
3769 defsubr (&Sskip_chars_forward);
3770 defsubr (&Sskip_chars_backward);
3771 defsubr (&Sskip_syntax_forward);
3772 defsubr (&Sskip_syntax_backward);
3774 defsubr (&Sforward_comment);
3775 defsubr (&Sscan_lists);
3776 defsubr (&Sscan_sexps);
3777 defsubr (&Sbackward_prefix_chars);
3778 defsubr (&Sparse_partial_sexp);