; Merge from origin/emacs-25
[emacs.git] / src / syntax.c
blob667de402ec4d2645c4a1eb6108083d1ee11d51a9
1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2016 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 <sys/types.h>
25 #include "lisp.h"
26 #include "character.h"
27 #include "buffer.h"
28 #include "regex.h"
29 #include "syntax.h"
30 #include "intervals.h"
31 #include "category.h"
33 /* Make syntax table lookup grant data in gl_state. */
34 #define SYNTAX(c) syntax_property (c, 1)
35 #define SYNTAX_ENTRY(c) syntax_property_entry (c, 1)
36 #define SYNTAX_WITH_FLAGS(c) syntax_property_with_flags (c, 1)
38 /* Eight single-bit flags have the following meanings:
39 1. This character is the first of a two-character comment-start sequence.
40 2. This character is the second of a two-character comment-start sequence.
41 3. This character is the first of a two-character comment-end sequence.
42 4. This character is the second of a two-character comment-end sequence.
43 5. This character is a prefix, for backward-prefix-chars.
44 6. The char is part of a delimiter for comments of style "b".
45 7. This character is part of a nestable comment sequence.
46 8. The char is part of a delimiter for comments of style "c".
47 Note that any two-character sequence whose first character has flag 1
48 and whose second character has flag 2 will be interpreted as a comment start.
50 Bits 6 and 8 discriminate among different comment styles.
51 Languages such as C++ allow two orthogonal syntax start/end pairs
52 and bit 6 determines whether a comment-end or Scommentend
53 ends style a or b. Comment markers can start style a, b, c, or bc.
54 Style a is always the default.
55 For 2-char comment markers, the style b flag is looked up only on the second
56 char of the comment marker and on the first char of the comment ender.
57 For style c (like the nested flag), the flag can be placed on any of
58 the chars. */
60 /* These functions extract specific flags from an integer
61 that holds the syntax code and the flags. */
63 static bool
64 SYNTAX_FLAGS_COMSTART_FIRST (int flags)
66 return (flags >> 16) & 1;
68 static bool
69 SYNTAX_FLAGS_COMSTART_SECOND (int flags)
71 return (flags >> 17) & 1;
73 static bool
74 SYNTAX_FLAGS_COMEND_FIRST (int flags)
76 return (flags >> 18) & 1;
78 static bool
79 SYNTAX_FLAGS_COMEND_SECOND (int flags)
81 return (flags >> 19) & 1;
83 static bool
84 SYNTAX_FLAGS_COMSTARTEND_FIRST (int flags)
86 return (flags & 0x50000) != 0;
88 static bool
89 SYNTAX_FLAGS_PREFIX (int flags)
91 return (flags >> 20) & 1;
93 static bool
94 SYNTAX_FLAGS_COMMENT_STYLEB (int flags)
96 return (flags >> 21) & 1;
98 static bool
99 SYNTAX_FLAGS_COMMENT_STYLEC (int flags)
101 return (flags >> 23) & 1;
103 static int
104 SYNTAX_FLAGS_COMMENT_STYLEC2 (int flags)
106 return (flags >> 22) & 2; /* SYNTAX_FLAGS_COMMENT_STYLEC (flags) * 2 */
108 static bool
109 SYNTAX_FLAGS_COMMENT_NESTED (int flags)
111 return (flags >> 22) & 1;
114 /* FLAGS should be the flags of the main char of the comment marker, e.g.
115 the second for comstart and the first for comend. */
116 static int
117 SYNTAX_FLAGS_COMMENT_STYLE (int flags, int other_flags)
119 return (SYNTAX_FLAGS_COMMENT_STYLEB (flags)
120 | SYNTAX_FLAGS_COMMENT_STYLEC2 (flags)
121 | SYNTAX_FLAGS_COMMENT_STYLEC2 (other_flags));
124 /* Extract a particular flag for a given character. */
126 static bool
127 SYNTAX_COMEND_FIRST (int c)
129 return SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c));
132 /* We use these constants in place for comment-style and
133 string-ender-char to distinguish comments/strings started by
134 comment_fence and string_fence codes. */
136 enum
138 ST_COMMENT_STYLE = 256 + 1,
139 ST_STRING_STYLE = 256 + 2
142 /* This is the internal form of the parse state used in parse-partial-sexp. */
144 struct lisp_parse_state
146 EMACS_INT depth; /* Depth at end of parsing. */
147 int instring; /* -1 if not within string, else desired terminator. */
148 EMACS_INT incomment; /* -1 if in unnestable comment else comment nesting */
149 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
150 bool quoted; /* True if just after an escape char at end of parsing. */
151 EMACS_INT mindepth; /* Minimum depth seen while scanning. */
152 /* Char number of most recent start-of-expression at current level */
153 ptrdiff_t thislevelstart;
154 /* Char number of start of containing expression */
155 ptrdiff_t prevlevelstart;
156 ptrdiff_t location; /* Char number at which parsing stopped. */
157 ptrdiff_t location_byte; /* Corresponding byte position. */
158 ptrdiff_t comstr_start; /* Position of last comment/string starter. */
159 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
160 of levels (starting from outermost). */
161 int prev_syntax; /* Syntax of previous position scanned, when
162 that position (potentially) holds the first char
163 of a 2-char construct, i.e. comment delimiter
164 or Sescape, etc. Smax otherwise. */
167 /* These variables are a cache for finding the start of a defun.
168 find_start_pos is the place for which the defun start was found.
169 find_start_value is the defun start position found for it.
170 find_start_value_byte is the corresponding byte position.
171 find_start_buffer is the buffer it was found in.
172 find_start_begv is the BEGV value when it was found.
173 find_start_modiff is the value of MODIFF when it was found. */
175 static ptrdiff_t find_start_pos;
176 static ptrdiff_t find_start_value;
177 static ptrdiff_t find_start_value_byte;
178 static struct buffer *find_start_buffer;
179 static ptrdiff_t find_start_begv;
180 static EMACS_INT find_start_modiff;
183 static Lisp_Object skip_chars (bool, Lisp_Object, Lisp_Object, bool);
184 static Lisp_Object skip_syntaxes (bool, Lisp_Object, Lisp_Object);
185 static Lisp_Object scan_lists (EMACS_INT, EMACS_INT, EMACS_INT, bool);
186 static void scan_sexps_forward (struct lisp_parse_state *,
187 ptrdiff_t, ptrdiff_t, ptrdiff_t, EMACS_INT,
188 bool, int);
189 static void internalize_parse_state (Lisp_Object, struct lisp_parse_state *);
190 static bool in_classes (int, Lisp_Object);
191 static void parse_sexp_propertize (ptrdiff_t charpos);
193 /* This setter is used only in this file, so it can be private. */
194 static void
195 bset_syntax_table (struct buffer *b, Lisp_Object val)
197 b->syntax_table_ = val;
200 /* Whether the syntax of the character C has the prefix flag set. */
201 bool
202 syntax_prefix_flag_p (int c)
204 return SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c));
207 struct gl_state_s gl_state; /* Global state of syntax parser. */
209 enum { INTERVALS_AT_ONCE = 10 }; /* 1 + max-number of intervals
210 to scan to property-change. */
212 /* Set the syntax entry VAL for char C in table TABLE. */
214 static void
215 SET_RAW_SYNTAX_ENTRY (Lisp_Object table, int c, Lisp_Object val)
217 CHAR_TABLE_SET (table, c, val);
220 /* Set the syntax entry VAL for char-range RANGE in table TABLE.
221 RANGE is a cons (FROM . TO) specifying the range of characters. */
223 static void
224 SET_RAW_SYNTAX_ENTRY_RANGE (Lisp_Object table, Lisp_Object range,
225 Lisp_Object val)
227 Fset_char_table_range (table, range, val);
230 /* Extract the information from the entry for character C
231 in the current syntax table. */
233 static Lisp_Object
234 SYNTAX_MATCH (int c)
236 Lisp_Object ent = SYNTAX_ENTRY (c);
237 return CONSP (ent) ? XCDR (ent) : Qnil;
240 /* This should be called with FROM at the start of forward
241 search, or after the last position of the backward search. It
242 makes sure that the first char is picked up with correct table, so
243 one does not need to call UPDATE_SYNTAX_TABLE immediately after the
244 call.
245 Sign of COUNT gives the direction of the search.
248 static void
249 SETUP_SYNTAX_TABLE (ptrdiff_t from, ptrdiff_t count)
251 SETUP_BUFFER_SYNTAX_TABLE ();
252 gl_state.b_property = BEGV;
253 gl_state.e_property = ZV + 1;
254 gl_state.object = Qnil;
255 gl_state.offset = 0;
256 if (parse_sexp_lookup_properties)
258 if (count > 0)
259 update_syntax_table_forward (from, true, Qnil);
260 else if (from > BEGV)
262 update_syntax_table (from - 1, count, true, Qnil);
263 parse_sexp_propertize (from - 1);
268 /* Same as above, but in OBJECT. If OBJECT is nil, use current buffer.
269 If it is t (which is only used in fast_c_string_match_ignore_case),
270 ignore properties altogether.
272 This is meant for regex.c to use. For buffers, regex.c passes arguments
273 to the UPDATE_SYNTAX_TABLE functions which are relative to BEGV.
274 So if it is a buffer, we set the offset field to BEGV. */
276 void
277 SETUP_SYNTAX_TABLE_FOR_OBJECT (Lisp_Object object,
278 ptrdiff_t from, ptrdiff_t count)
280 SETUP_BUFFER_SYNTAX_TABLE ();
281 gl_state.object = object;
282 if (BUFFERP (gl_state.object))
284 struct buffer *buf = XBUFFER (gl_state.object);
285 gl_state.b_property = 1;
286 gl_state.e_property = BUF_ZV (buf) - BUF_BEGV (buf) + 1;
287 gl_state.offset = BUF_BEGV (buf) - 1;
289 else if (NILP (gl_state.object))
291 gl_state.b_property = 1;
292 gl_state.e_property = ZV - BEGV + 1;
293 gl_state.offset = BEGV - 1;
295 else if (EQ (gl_state.object, Qt))
297 gl_state.b_property = 0;
298 gl_state.e_property = PTRDIFF_MAX;
299 gl_state.offset = 0;
301 else
303 gl_state.b_property = 0;
304 gl_state.e_property = 1 + SCHARS (gl_state.object);
305 gl_state.offset = 0;
307 if (parse_sexp_lookup_properties)
308 update_syntax_table (from + gl_state.offset - (count <= 0),
309 count, 1, gl_state.object);
312 /* Update gl_state to an appropriate interval which contains CHARPOS. The
313 sign of COUNT give the relative position of CHARPOS wrt the previously
314 valid interval. If INIT, only [be]_property fields of gl_state are
315 valid at start, the rest is filled basing on OBJECT.
317 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
318 direction than the intervals - or in an interval. We update the
319 current syntax-table basing on the property of this interval, and
320 update the interval to start further than CHARPOS - or be
321 NULL. We also update lim_property to be the next value of
322 charpos to call this subroutine again - or be before/after the
323 start/end of OBJECT. */
325 void
326 update_syntax_table (ptrdiff_t charpos, EMACS_INT count, bool init,
327 Lisp_Object object)
329 Lisp_Object tmp_table;
330 int cnt = 0;
331 bool invalidate = true;
332 INTERVAL i;
334 if (init)
336 gl_state.old_prop = Qnil;
337 gl_state.start = gl_state.b_property;
338 gl_state.stop = gl_state.e_property;
339 i = interval_of (charpos, object);
340 gl_state.backward_i = gl_state.forward_i = i;
341 invalidate = false;
342 if (!i)
343 return;
344 /* interval_of updates only ->position of the return value, so
345 update the parents manually to speed up update_interval. */
346 while (!NULL_PARENT (i))
348 if (AM_RIGHT_CHILD (i))
349 INTERVAL_PARENT (i)->position = i->position
350 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
351 - TOTAL_LENGTH (INTERVAL_PARENT (i))
352 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
353 else
354 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
355 + TOTAL_LENGTH (i);
356 i = INTERVAL_PARENT (i);
358 i = gl_state.forward_i;
359 gl_state.b_property = i->position - gl_state.offset;
360 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
361 goto update;
363 i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
365 /* We are guaranteed to be called with CHARPOS either in i,
366 or further off. */
367 if (!i)
368 error ("Error in syntax_table logic for to-the-end intervals");
369 else if (charpos < i->position) /* Move left. */
371 if (count > 0)
372 error ("Error in syntax_table logic for intervals <-");
373 /* Update the interval. */
374 i = update_interval (i, charpos);
375 if (INTERVAL_LAST_POS (i) != gl_state.b_property)
377 invalidate = false;
378 gl_state.forward_i = i;
379 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
382 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
384 if (count < 0)
385 error ("Error in syntax_table logic for intervals ->");
386 /* Update the interval. */
387 i = update_interval (i, charpos);
388 if (i->position != gl_state.e_property)
390 invalidate = false;
391 gl_state.backward_i = i;
392 gl_state.b_property = i->position - gl_state.offset;
396 update:
397 tmp_table = textget (i->plist, Qsyntax_table);
399 if (invalidate)
400 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
402 if (invalidate) /* Did not get to adjacent interval. */
403 { /* with the same table => */
404 /* invalidate the old range. */
405 if (count > 0)
407 gl_state.backward_i = i;
408 gl_state.b_property = i->position - gl_state.offset;
410 else
412 gl_state.forward_i = i;
413 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
417 if (!EQ (tmp_table, gl_state.old_prop))
419 gl_state.current_syntax_table = tmp_table;
420 gl_state.old_prop = tmp_table;
421 if (EQ (Fsyntax_table_p (tmp_table), Qt))
423 gl_state.use_global = 0;
425 else if (CONSP (tmp_table))
427 gl_state.use_global = 1;
428 gl_state.global_code = tmp_table;
430 else
432 gl_state.use_global = 0;
433 gl_state.current_syntax_table = BVAR (current_buffer, syntax_table);
437 while (i)
439 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
441 if (count > 0)
443 gl_state.e_property = i->position - gl_state.offset;
444 gl_state.forward_i = i;
446 else
448 gl_state.b_property
449 = i->position + LENGTH (i) - gl_state.offset;
450 gl_state.backward_i = i;
452 return;
454 else if (cnt == INTERVALS_AT_ONCE)
456 if (count > 0)
458 gl_state.e_property
459 = i->position + LENGTH (i) - gl_state.offset
460 /* e_property at EOB is not set to ZV but to ZV+1, so that
461 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
462 having to check eob between the two. */
463 + (next_interval (i) ? 0 : 1);
464 gl_state.forward_i = i;
466 else
468 gl_state.b_property = i->position - gl_state.offset;
469 gl_state.backward_i = i;
471 return;
473 cnt++;
474 i = count > 0 ? next_interval (i) : previous_interval (i);
476 eassert (i == NULL); /* This property goes to the end. */
477 if (count > 0)
479 gl_state.e_property = gl_state.stop;
480 gl_state.forward_i = i;
482 else
483 gl_state.b_property = gl_state.start;
486 static void
487 parse_sexp_propertize (ptrdiff_t charpos)
489 EMACS_INT zv = ZV;
490 if (syntax_propertize__done <= charpos
491 && syntax_propertize__done < zv)
493 EMACS_INT modiffs = CHARS_MODIFF;
494 safe_call1 (Qinternal__syntax_propertize,
495 make_number (min (zv, 1 + charpos)));
496 if (modiffs != CHARS_MODIFF)
497 error ("parse-sexp-propertize-function modified the buffer!");
498 if (syntax_propertize__done <= charpos
499 && syntax_propertize__done < zv)
500 error ("parse-sexp-propertize-function did not move"
501 " syntax-propertize--done");
502 SETUP_SYNTAX_TABLE (charpos, 1);
504 else if (gl_state.e_property > syntax_propertize__done)
506 gl_state.e_property = syntax_propertize__done;
507 gl_state.e_property_truncated = true;
509 else if (gl_state.e_property_truncated
510 && gl_state.e_property < syntax_propertize__done)
511 { /* When moving backward, e_property might be set without resetting
512 e_property_truncated, so the e_property_truncated flag may
513 occasionally be left raised spuriously. This should be rare. */
514 gl_state.e_property_truncated = false;
515 update_syntax_table_forward (charpos, false, Qnil);
519 void
520 update_syntax_table_forward (ptrdiff_t charpos, bool init,
521 Lisp_Object object)
523 if (gl_state.e_property_truncated)
525 eassert (NILP (object));
526 eassert (charpos >= gl_state.e_property);
527 parse_sexp_propertize (charpos);
529 else
531 update_syntax_table (charpos, 1, init, object);
532 if (NILP (object) && gl_state.e_property > syntax_propertize__done)
533 parse_sexp_propertize (charpos);
537 /* Returns true if char at CHARPOS is quoted.
538 Global syntax-table data should be set up already to be good at CHARPOS
539 or after. On return global syntax data is good for lookup at CHARPOS. */
541 static bool
542 char_quoted (ptrdiff_t charpos, ptrdiff_t bytepos)
544 enum syntaxcode code;
545 ptrdiff_t beg = BEGV;
546 bool quoted = 0;
547 ptrdiff_t orig = charpos;
549 while (charpos > beg)
551 int c;
552 DEC_BOTH (charpos, bytepos);
554 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
555 c = FETCH_CHAR_AS_MULTIBYTE (bytepos);
556 code = SYNTAX (c);
557 if (! (code == Scharquote || code == Sescape))
558 break;
560 quoted = !quoted;
563 UPDATE_SYNTAX_TABLE (orig);
564 return quoted;
567 /* Return the bytepos one character before BYTEPOS.
568 We assume that BYTEPOS is not at the start of the buffer. */
570 static ptrdiff_t
571 dec_bytepos (ptrdiff_t bytepos)
573 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
574 return bytepos - 1;
576 DEC_POS (bytepos);
577 return bytepos;
580 /* Return a defun-start position before POS and not too far before.
581 It should be the last one before POS, or nearly the last.
583 When open_paren_in_column_0_is_defun_start is nonzero,
584 only the beginning of the buffer is treated as a defun-start.
586 We record the information about where the scan started
587 and what its result was, so that another call in the same area
588 can return the same value very quickly.
590 There is no promise at which position the global syntax data is
591 valid on return from the subroutine, so the caller should explicitly
592 update the global data. */
594 static ptrdiff_t
595 find_defun_start (ptrdiff_t pos, ptrdiff_t pos_byte)
597 ptrdiff_t opoint = PT, opoint_byte = PT_BYTE;
599 /* Use previous finding, if it's valid and applies to this inquiry. */
600 if (current_buffer == find_start_buffer
601 /* Reuse the defun-start even if POS is a little farther on.
602 POS might be in the next defun, but that's ok.
603 Our value may not be the best possible, but will still be usable. */
604 && pos <= find_start_pos + 1000
605 && pos >= find_start_value
606 && BEGV == find_start_begv
607 && MODIFF == find_start_modiff)
608 return find_start_value;
610 if (!open_paren_in_column_0_is_defun_start)
612 find_start_value = BEGV;
613 find_start_value_byte = BEGV_BYTE;
614 goto found;
617 /* Back up to start of line. */
618 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
620 /* We optimize syntax-table lookup for rare updates. Thus we accept
621 only those `^\s(' which are good in global _and_ text-property
622 syntax-tables. */
623 SETUP_BUFFER_SYNTAX_TABLE ();
624 while (PT > BEGV)
626 int c;
628 /* Open-paren at start of line means we may have found our
629 defun-start. */
630 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
631 if (SYNTAX (c) == Sopen)
633 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
634 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
635 if (SYNTAX (c) == Sopen)
636 break;
637 /* Now fallback to the default value. */
638 SETUP_BUFFER_SYNTAX_TABLE ();
640 /* Move to beg of previous line. */
641 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
644 /* Record what we found, for the next try. */
645 find_start_value = PT;
646 find_start_value_byte = PT_BYTE;
647 TEMP_SET_PT_BOTH (opoint, opoint_byte);
649 found:
650 find_start_buffer = current_buffer;
651 find_start_modiff = MODIFF;
652 find_start_begv = BEGV;
653 find_start_pos = pos;
655 return find_start_value;
658 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
660 static bool
661 prev_char_comend_first (ptrdiff_t pos, ptrdiff_t pos_byte)
663 int c;
664 bool val;
666 DEC_BOTH (pos, pos_byte);
667 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
668 c = FETCH_CHAR (pos_byte);
669 val = SYNTAX_COMEND_FIRST (c);
670 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
671 return val;
674 /* Check whether charpos FROM is at the end of a comment.
675 FROM_BYTE is the bytepos corresponding to FROM.
676 Do not move back before STOP.
678 Return true if we find a comment ending at FROM/FROM_BYTE.
680 If successful, store the charpos of the comment's beginning
681 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
683 Global syntax data remains valid for backward search starting at
684 the returned value (or at FROM, if the search was not successful). */
686 static bool
687 back_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop,
688 bool comnested, int comstyle, ptrdiff_t *charpos_ptr,
689 ptrdiff_t *bytepos_ptr)
691 /* Look back, counting the parity of string-quotes,
692 and recording the comment-starters seen.
693 When we reach a safe place, assume that's not in a string;
694 then step the main scan to the earliest comment-starter seen
695 an even number of string quotes away from the safe place.
697 OFROM[I] is position of the earliest comment-starter seen
698 which is I+2X quotes from the comment-end.
699 PARITY is current parity of quotes from the comment end. */
700 int string_style = -1; /* Presumed outside of any string. */
701 bool string_lossage = 0;
702 /* Not a real lossage: indicates that we have passed a matching comment
703 starter plus a non-matching comment-ender, meaning that any matching
704 comment-starter we might see later could be a false positive (hidden
705 inside another comment).
706 Test case: { a (* b } c (* d *) */
707 bool comment_lossage = 0;
708 ptrdiff_t comment_end = from;
709 ptrdiff_t comment_end_byte = from_byte;
710 ptrdiff_t comstart_pos = 0;
711 ptrdiff_t comstart_byte;
712 /* Place where the containing defun starts,
713 or 0 if we didn't come across it yet. */
714 ptrdiff_t defun_start = 0;
715 ptrdiff_t defun_start_byte = 0;
716 enum syntaxcode code;
717 ptrdiff_t nesting = 1; /* Current comment nesting. */
718 int c;
719 int syntax = 0;
721 /* FIXME: A }} comment-ender style leads to incorrect behavior
722 in the case of {{ c }}} because we ignore the last two chars which are
723 assumed to be comment-enders although they aren't. */
725 /* At beginning of range to scan, we're outside of strings;
726 that determines quote parity to the comment-end. */
727 while (from != stop)
729 ptrdiff_t temp_byte;
730 int prev_syntax;
731 bool com2start, com2end, comstart;
733 /* Move back and examine a character. */
734 DEC_BOTH (from, from_byte);
735 UPDATE_SYNTAX_TABLE_BACKWARD (from);
737 prev_syntax = syntax;
738 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
739 syntax = SYNTAX_WITH_FLAGS (c);
740 code = SYNTAX (c);
742 /* Check for 2-char comment markers. */
743 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
744 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
745 && (comstyle
746 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax, syntax))
747 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
748 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
749 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
750 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
751 comstart = (com2start || code == Scomment);
753 /* Nasty cases with overlapping 2-char comment markers:
754 - snmp-mode: -- c -- foo -- c --
755 --- c --
756 ------ c --
757 - c-mode: *||*
758 |* *|* *|
759 |*| |* |*|
760 /// */
762 /* If a 2-char comment sequence partly overlaps with another,
763 we don't try to be clever. E.g. |*| in C, or }% in modes that
764 have %..\n and %{..}%. */
765 if (from > stop && (com2end || comstart))
767 ptrdiff_t next = from, next_byte = from_byte;
768 int next_c, next_syntax;
769 DEC_BOTH (next, next_byte);
770 UPDATE_SYNTAX_TABLE_BACKWARD (next);
771 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
772 next_syntax = SYNTAX_WITH_FLAGS (next_c);
773 if (((comstart || comnested)
774 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
775 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
776 || ((com2end || comnested)
777 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
778 && (comstyle
779 == SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_syntax))
780 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
781 goto lossage;
782 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
785 if (com2start && comstart_pos == 0)
786 /* We're looking at a comment starter. But it might be a comment
787 ender as well (see snmp-mode). The first time we see one, we
788 need to consider it as a comment starter,
789 and the subsequent times as a comment ender. */
790 com2end = 0;
792 /* Turn a 2-char comment sequences into the appropriate syntax. */
793 if (com2end)
794 code = Sendcomment;
795 else if (com2start)
796 code = Scomment;
797 /* Ignore comment starters of a different style. */
798 else if (code == Scomment
799 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0)
800 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
801 continue;
803 /* Ignore escaped characters, except comment-enders which cannot
804 be escaped. */
805 if ((Vcomment_end_can_be_escaped || code != Sendcomment)
806 && char_quoted (from, from_byte))
807 continue;
809 switch (code)
811 case Sstring_fence:
812 case Scomment_fence:
813 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
814 case Sstring:
815 /* Track parity of quotes. */
816 if (string_style == -1)
817 /* Entering a string. */
818 string_style = c;
819 else if (string_style == c)
820 /* Leaving the string. */
821 string_style = -1;
822 else
823 /* If we have two kinds of string delimiters.
824 There's no way to grok this scanning backwards. */
825 string_lossage = 1;
826 break;
828 case Scomment:
829 /* We've already checked that it is the relevant comstyle. */
830 if (string_style != -1 || comment_lossage || string_lossage)
831 /* There are odd string quotes involved, so let's be careful.
832 Test case in Pascal: " { " a { " } */
833 goto lossage;
835 if (!comnested)
837 /* Record best comment-starter so far. */
838 comstart_pos = from;
839 comstart_byte = from_byte;
841 else if (--nesting <= 0)
842 /* nested comments have to be balanced, so we don't need to
843 keep looking for earlier ones. We use here the same (slightly
844 incorrect) reasoning as below: since it is followed by uniform
845 paired string quotes, this comment-start has to be outside of
846 strings, else the comment-end itself would be inside a string. */
847 goto done;
848 break;
850 case Sendcomment:
851 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == comstyle
852 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
853 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
854 /* This is the same style of comment ender as ours. */
856 if (comnested)
857 nesting++;
858 else
859 /* Anything before that can't count because it would match
860 this comment-ender rather than ours. */
861 from = stop; /* Break out of the loop. */
863 else if (comstart_pos != 0 || c != '\n')
864 /* We're mixing comment styles here, so we'd better be careful.
865 The (comstart_pos != 0 || c != '\n') check is not quite correct
866 (we should just always set comment_lossage), but removing it
867 would imply that any multiline comment in C would go through
868 lossage, which seems overkill.
869 The failure should only happen in the rare cases such as
870 { (* } *) */
871 comment_lossage = 1;
872 break;
874 case Sopen:
875 /* Assume a defun-start point is outside of strings. */
876 if (open_paren_in_column_0_is_defun_start
877 && (from == stop
878 || (temp_byte = dec_bytepos (from_byte),
879 FETCH_CHAR (temp_byte) == '\n')))
881 defun_start = from;
882 defun_start_byte = from_byte;
883 from = stop; /* Break out of the loop. */
885 break;
887 default:
888 break;
892 if (comstart_pos == 0)
894 from = comment_end;
895 from_byte = comment_end_byte;
896 UPDATE_SYNTAX_TABLE_FORWARD (comment_end);
898 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
899 or `done'), then we've found the beginning of the non-nested comment. */
900 else if (1) /* !comnested */
902 from = comstart_pos;
903 from_byte = comstart_byte;
904 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
906 else lossage:
908 struct lisp_parse_state state;
909 bool adjusted = true;
910 /* We had two kinds of string delimiters mixed up
911 together. Decode this going forwards.
912 Scan fwd from a known safe place (beginning-of-defun)
913 to the one in question; this records where we
914 last passed a comment starter. */
915 /* If we did not already find the defun start, find it now. */
916 if (defun_start == 0)
918 defun_start = find_defun_start (comment_end, comment_end_byte);
919 defun_start_byte = find_start_value_byte;
920 adjusted = (defun_start > BEGV);
924 internalize_parse_state (Qnil, &state);
925 scan_sexps_forward (&state,
926 defun_start, defun_start_byte,
927 comment_end, TYPE_MINIMUM (EMACS_INT),
928 0, 0);
929 defun_start = comment_end;
930 if (!adjusted)
932 adjusted = true;
933 find_start_value
934 = CONSP (state.levelstarts) ? XINT (XCAR (state.levelstarts))
935 : state.thislevelstart >= 0 ? state.thislevelstart
936 : find_start_value;
937 find_start_value_byte = CHAR_TO_BYTE (find_start_value);
940 if (state.incomment == (comnested ? 1 : -1)
941 && state.comstyle == comstyle)
942 from = state.comstr_start;
943 else
945 from = comment_end;
946 if (state.incomment)
947 /* If comment_end is inside some other comment, maybe ours
948 is nested, so we need to try again from within the
949 surrounding comment. Example: { a (* " *) */
951 /* FIXME: We should advance by one or two chars. */
952 defun_start = state.comstr_start + 2;
953 defun_start_byte = CHAR_TO_BYTE (defun_start);
956 } while (defun_start < comment_end);
958 from_byte = CHAR_TO_BYTE (from);
959 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
962 done:
963 *charpos_ptr = from;
964 *bytepos_ptr = from_byte;
966 return from != comment_end;
969 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
970 doc: /* Return t if OBJECT is a syntax table.
971 Currently, any char-table counts as a syntax table. */)
972 (Lisp_Object object)
974 if (CHAR_TABLE_P (object)
975 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
976 return Qt;
977 return Qnil;
980 static void
981 check_syntax_table (Lisp_Object obj)
983 CHECK_TYPE (CHAR_TABLE_P (obj) && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table),
984 Qsyntax_table_p, obj);
987 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
988 doc: /* Return the current syntax table.
989 This is the one specified by the current buffer. */)
990 (void)
992 return BVAR (current_buffer, syntax_table);
995 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
996 Sstandard_syntax_table, 0, 0, 0,
997 doc: /* Return the standard syntax table.
998 This is the one used for new buffers. */)
999 (void)
1001 return Vstandard_syntax_table;
1004 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
1005 doc: /* Construct a new syntax table and return it.
1006 It is a copy of the TABLE, which defaults to the standard syntax table. */)
1007 (Lisp_Object table)
1009 Lisp_Object copy;
1011 if (!NILP (table))
1012 check_syntax_table (table);
1013 else
1014 table = Vstandard_syntax_table;
1016 copy = Fcopy_sequence (table);
1018 /* Only the standard syntax table should have a default element.
1019 Other syntax tables should inherit from parents instead. */
1020 set_char_table_defalt (copy, Qnil);
1022 /* Copied syntax tables should all have parents.
1023 If we copied one with no parent, such as the standard syntax table,
1024 use the standard syntax table as the copy's parent. */
1025 if (NILP (XCHAR_TABLE (copy)->parent))
1026 Fset_char_table_parent (copy, Vstandard_syntax_table);
1027 return copy;
1030 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
1031 doc: /* Select a new syntax table for the current buffer.
1032 One argument, a syntax table. */)
1033 (Lisp_Object table)
1035 int idx;
1036 check_syntax_table (table);
1037 bset_syntax_table (current_buffer, table);
1038 /* Indicate that this buffer now has a specified syntax table. */
1039 idx = PER_BUFFER_VAR_IDX (syntax_table);
1040 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
1041 return table;
1044 /* Convert a letter which signifies a syntax code
1045 into the code it signifies.
1046 This is used by modify-syntax-entry, and other things. */
1048 unsigned char const syntax_spec_code[0400] =
1049 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1050 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1051 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1052 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1053 Swhitespace, Scomment_fence, Sstring, 0377, Smath, 0377, 0377, Squote,
1054 Sopen, Sclose, 0377, 0377, 0377, Swhitespace, Spunct, Scharquote,
1055 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1056 0377, 0377, 0377, 0377, Scomment, 0377, Sendcomment, 0377,
1057 Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
1058 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1059 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword,
1060 0377, 0377, 0377, 0377, Sescape, 0377, 0377, Ssymbol,
1061 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
1062 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1063 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword,
1064 0377, 0377, 0377, 0377, Sstring_fence, 0377, 0377, 0377
1067 /* Indexed by syntax code, give the letter that describes it. */
1069 char const syntax_code_spec[16] =
1071 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
1072 '!', '|'
1075 /* Indexed by syntax code, give the object (cons of syntax code and
1076 nil) to be stored in syntax table. Since these objects can be
1077 shared among syntax tables, we generate them in advance. By
1078 sharing objects, the function `describe-syntax' can give a more
1079 compact listing. */
1080 static Lisp_Object Vsyntax_code_object;
1083 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
1084 doc: /* Return the syntax code of CHARACTER, described by a character.
1085 For example, if CHARACTER is a word constituent, the
1086 character `w' (119) is returned.
1087 The characters that correspond to various syntax codes
1088 are listed in the documentation of `modify-syntax-entry'. */)
1089 (Lisp_Object character)
1091 int char_int;
1092 CHECK_CHARACTER (character);
1093 char_int = XINT (character);
1094 SETUP_BUFFER_SYNTAX_TABLE ();
1095 return make_number (syntax_code_spec[SYNTAX (char_int)]);
1098 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
1099 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
1100 (Lisp_Object character)
1102 int char_int;
1103 enum syntaxcode code;
1104 CHECK_CHARACTER (character);
1105 char_int = XINT (character);
1106 SETUP_BUFFER_SYNTAX_TABLE ();
1107 code = SYNTAX (char_int);
1108 if (code == Sopen || code == Sclose)
1109 return SYNTAX_MATCH (char_int);
1110 return Qnil;
1113 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
1114 doc: /* Convert a syntax descriptor STRING into a raw syntax descriptor.
1115 STRING should be a string of the form allowed as argument of
1116 `modify-syntax-entry'. The return value is a raw syntax descriptor: a
1117 cons cell (CODE . MATCHING-CHAR) which can be used, for example, as
1118 the value of a `syntax-table' text property. */)
1119 (Lisp_Object string)
1121 const unsigned char *p;
1122 int val;
1123 Lisp_Object match;
1125 CHECK_STRING (string);
1127 p = SDATA (string);
1128 val = syntax_spec_code[*p++];
1129 if (val == 0377)
1130 error ("Invalid syntax description letter: %c", p[-1]);
1132 if (val == Sinherit)
1133 return Qnil;
1135 if (*p)
1137 int len;
1138 int character = STRING_CHAR_AND_LENGTH (p, len);
1139 XSETINT (match, character);
1140 if (XFASTINT (match) == ' ')
1141 match = Qnil;
1142 p += len;
1144 else
1145 match = Qnil;
1147 while (*p)
1148 switch (*p++)
1150 case '1':
1151 val |= 1 << 16;
1152 break;
1154 case '2':
1155 val |= 1 << 17;
1156 break;
1158 case '3':
1159 val |= 1 << 18;
1160 break;
1162 case '4':
1163 val |= 1 << 19;
1164 break;
1166 case 'p':
1167 val |= 1 << 20;
1168 break;
1170 case 'b':
1171 val |= 1 << 21;
1172 break;
1174 case 'n':
1175 val |= 1 << 22;
1176 break;
1178 case 'c':
1179 val |= 1 << 23;
1180 break;
1183 if (val < ASIZE (Vsyntax_code_object) && NILP (match))
1184 return AREF (Vsyntax_code_object, val);
1185 else
1186 /* Since we can't use a shared object, let's make a new one. */
1187 return Fcons (make_number (val), match);
1190 /* I really don't know why this is interactive
1191 help-form should at least be made useful whilst reading the second arg. */
1192 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
1193 "cSet syntax for character: \nsSet syntax for %s to: ",
1194 doc: /* Set syntax for character CHAR according to string NEWENTRY.
1195 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1196 the current buffer's syntax table.
1197 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1198 in the range MIN to MAX are changed.
1199 The first character of NEWENTRY should be one of the following:
1200 Space or - whitespace syntax. w word constituent.
1201 _ symbol constituent. . punctuation.
1202 ( open-parenthesis. ) close-parenthesis.
1203 " string quote. \\ escape.
1204 $ paired delimiter. \\=' expression quote or prefix operator.
1205 < comment starter. > comment ender.
1206 / character-quote. @ inherit from parent table.
1207 | generic string fence. ! generic comment fence.
1209 Only single-character comment start and end sequences are represented thus.
1210 Two-character sequences are represented as described below.
1211 The second character of NEWENTRY is the matching parenthesis,
1212 used only if the first character is `(' or `)'.
1213 Any additional characters are flags.
1214 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1215 1 means CHAR is the start of a two-char comment start sequence.
1216 2 means CHAR is the second character of such a sequence.
1217 3 means CHAR is the start of a two-char comment end sequence.
1218 4 means CHAR is the second character of such a sequence.
1220 There can be several orthogonal comment sequences. This is to support
1221 language modes such as C++. By default, all comment sequences are of style
1222 a, but you can set the comment sequence style to b (on the second character
1223 of a comment-start, and the first character of a comment-end sequence) and/or
1224 c (on any of its chars) using this flag:
1225 b means CHAR is part of comment sequence b.
1226 c means CHAR is part of comment sequence c.
1227 n means CHAR is part of a nestable comment sequence.
1229 p means CHAR is a prefix character for `backward-prefix-chars';
1230 such characters are treated as whitespace when they occur
1231 between expressions.
1232 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1233 (Lisp_Object c, Lisp_Object newentry, Lisp_Object syntax_table)
1235 if (CONSP (c))
1237 CHECK_CHARACTER_CAR (c);
1238 CHECK_CHARACTER_CDR (c);
1240 else
1241 CHECK_CHARACTER (c);
1243 if (NILP (syntax_table))
1244 syntax_table = BVAR (current_buffer, syntax_table);
1245 else
1246 check_syntax_table (syntax_table);
1248 newentry = Fstring_to_syntax (newentry);
1249 if (CONSP (c))
1250 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1251 else
1252 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
1254 /* We clear the regexp cache, since character classes can now have
1255 different values from those in the compiled regexps.*/
1256 clear_regexp_cache ();
1258 return Qnil;
1261 /* Dump syntax table to buffer in human-readable format */
1263 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1264 Sinternal_describe_syntax_value, 1, 1, 0,
1265 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1266 (Lisp_Object syntax)
1268 int code, syntax_code;
1269 bool start1, start2, end1, end2, prefix, comstyleb, comstylec, comnested;
1270 char str[2];
1271 Lisp_Object first, match_lisp, value = syntax;
1273 if (NILP (value))
1275 insert_string ("default");
1276 return syntax;
1279 if (CHAR_TABLE_P (value))
1281 insert_string ("deeper char-table ...");
1282 return syntax;
1285 if (!CONSP (value))
1287 insert_string ("invalid");
1288 return syntax;
1291 first = XCAR (value);
1292 match_lisp = XCDR (value);
1294 if (!INTEGERP (first) || !(NILP (match_lisp) || CHARACTERP (match_lisp)))
1296 insert_string ("invalid");
1297 return syntax;
1300 syntax_code = XINT (first) & INT_MAX;
1301 code = syntax_code & 0377;
1302 start1 = SYNTAX_FLAGS_COMSTART_FIRST (syntax_code);
1303 start2 = SYNTAX_FLAGS_COMSTART_SECOND (syntax_code);
1304 end1 = SYNTAX_FLAGS_COMEND_FIRST (syntax_code);
1305 end2 = SYNTAX_FLAGS_COMEND_SECOND (syntax_code);
1306 prefix = SYNTAX_FLAGS_PREFIX (syntax_code);
1307 comstyleb = SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code);
1308 comstylec = SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code);
1309 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax_code);
1311 if (Smax <= code)
1313 insert_string ("invalid");
1314 return syntax;
1317 str[0] = syntax_code_spec[code], str[1] = 0;
1318 insert (str, 1);
1320 if (NILP (match_lisp))
1321 insert (" ", 1);
1322 else
1323 insert_char (XINT (match_lisp));
1325 if (start1)
1326 insert ("1", 1);
1327 if (start2)
1328 insert ("2", 1);
1330 if (end1)
1331 insert ("3", 1);
1332 if (end2)
1333 insert ("4", 1);
1335 if (prefix)
1336 insert ("p", 1);
1337 if (comstyleb)
1338 insert ("b", 1);
1339 if (comstylec)
1340 insert ("c", 1);
1341 if (comnested)
1342 insert ("n", 1);
1344 insert_string ("\twhich means: ");
1346 switch (code)
1348 case Swhitespace:
1349 insert_string ("whitespace"); break;
1350 case Spunct:
1351 insert_string ("punctuation"); break;
1352 case Sword:
1353 insert_string ("word"); break;
1354 case Ssymbol:
1355 insert_string ("symbol"); break;
1356 case Sopen:
1357 insert_string ("open"); break;
1358 case Sclose:
1359 insert_string ("close"); break;
1360 case Squote:
1361 insert_string ("prefix"); break;
1362 case Sstring:
1363 insert_string ("string"); break;
1364 case Smath:
1365 insert_string ("math"); break;
1366 case Sescape:
1367 insert_string ("escape"); break;
1368 case Scharquote:
1369 insert_string ("charquote"); break;
1370 case Scomment:
1371 insert_string ("comment"); break;
1372 case Sendcomment:
1373 insert_string ("endcomment"); break;
1374 case Sinherit:
1375 insert_string ("inherit"); break;
1376 case Scomment_fence:
1377 insert_string ("comment fence"); break;
1378 case Sstring_fence:
1379 insert_string ("string fence"); break;
1380 default:
1381 insert_string ("invalid");
1382 return syntax;
1385 if (!NILP (match_lisp))
1387 insert_string (", matches ");
1388 insert_char (XINT (match_lisp));
1391 if (start1)
1392 insert_string (",\n\t is the first character of a comment-start sequence");
1393 if (start2)
1394 insert_string (",\n\t is the second character of a comment-start sequence");
1396 if (end1)
1397 insert_string (",\n\t is the first character of a comment-end sequence");
1398 if (end2)
1399 insert_string (",\n\t is the second character of a comment-end sequence");
1400 if (comstyleb)
1401 insert_string (" (comment style b)");
1402 if (comstylec)
1403 insert_string (" (comment style c)");
1404 if (comnested)
1405 insert_string (" (nestable)");
1407 if (prefix)
1409 AUTO_STRING (prefixdoc,
1410 ",\n\t is a prefix character for `backward-prefix-chars'");
1411 insert1 (Fsubstitute_command_keys (prefixdoc));
1414 return syntax;
1417 /* Return the position across COUNT words from FROM.
1418 If that many words cannot be found before the end of the buffer, return 0.
1419 COUNT negative means scan backward and stop at word beginning. */
1421 ptrdiff_t
1422 scan_words (register ptrdiff_t from, register EMACS_INT count)
1424 register ptrdiff_t beg = BEGV;
1425 register ptrdiff_t end = ZV;
1426 register ptrdiff_t from_byte = CHAR_TO_BYTE (from);
1427 register enum syntaxcode code;
1428 int ch0, ch1;
1429 Lisp_Object func, pos;
1431 immediate_quit = 1;
1432 QUIT;
1434 SETUP_SYNTAX_TABLE (from, count);
1436 while (count > 0)
1438 while (1)
1440 if (from == end)
1442 immediate_quit = 0;
1443 return 0;
1445 UPDATE_SYNTAX_TABLE_FORWARD (from);
1446 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1447 code = SYNTAX (ch0);
1448 INC_BOTH (from, from_byte);
1449 if (words_include_escapes
1450 && (code == Sescape || code == Scharquote))
1451 break;
1452 if (code == Sword)
1453 break;
1455 /* Now CH0 is a character which begins a word and FROM is the
1456 position of the next character. */
1457 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
1458 if (! NILP (Ffboundp (func)))
1460 pos = call2 (func, make_number (from - 1), make_number (end));
1461 if (INTEGERP (pos) && from < XINT (pos) && XINT (pos) <= ZV)
1463 from = XINT (pos);
1464 from_byte = CHAR_TO_BYTE (from);
1467 else
1469 while (1)
1471 if (from == end) break;
1472 UPDATE_SYNTAX_TABLE_FORWARD (from);
1473 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1474 code = SYNTAX (ch1);
1475 if ((code != Sword
1476 && (! words_include_escapes
1477 || (code != Sescape && code != Scharquote)))
1478 || word_boundary_p (ch0, ch1))
1479 break;
1480 INC_BOTH (from, from_byte);
1481 ch0 = ch1;
1484 count--;
1486 while (count < 0)
1488 while (1)
1490 if (from == beg)
1492 immediate_quit = 0;
1493 return 0;
1495 DEC_BOTH (from, from_byte);
1496 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1497 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1498 code = SYNTAX (ch1);
1499 if (words_include_escapes
1500 && (code == Sescape || code == Scharquote))
1501 break;
1502 if (code == Sword)
1503 break;
1505 /* Now CH1 is a character which ends a word and FROM is the
1506 position of it. */
1507 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
1508 if (! NILP (Ffboundp (func)))
1510 pos = call2 (func, make_number (from), make_number (beg));
1511 if (INTEGERP (pos) && BEGV <= XINT (pos) && XINT (pos) < from)
1513 from = XINT (pos);
1514 from_byte = CHAR_TO_BYTE (from);
1517 else
1519 while (1)
1521 if (from == beg)
1522 break;
1523 DEC_BOTH (from, from_byte);
1524 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1525 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1526 code = SYNTAX (ch0);
1527 if ((code != Sword
1528 && (! words_include_escapes
1529 || (code != Sescape && code != Scharquote)))
1530 || word_boundary_p (ch0, ch1))
1532 INC_BOTH (from, from_byte);
1533 break;
1535 ch1 = ch0;
1538 count++;
1541 immediate_quit = 0;
1543 return from;
1546 DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p",
1547 doc: /* Move point forward ARG words (backward if ARG is negative).
1548 If ARG is omitted or nil, move point forward one word.
1549 Normally returns t.
1550 If an edge of the buffer or a field boundary is reached, point is
1551 left there and the function returns nil. Field boundaries are not
1552 noticed if `inhibit-field-text-motion' is non-nil.
1554 The word boundaries are normally determined by the buffer's syntax
1555 table, but `find-word-boundary-function-table', such as set up
1556 by `subword-mode', can change that. If a Lisp program needs to
1557 move by words determined strictly by the syntax table, it should
1558 use `forward-word-strictly' instead. */)
1559 (Lisp_Object arg)
1561 Lisp_Object tmp;
1562 ptrdiff_t orig_val, val;
1564 if (NILP (arg))
1565 XSETFASTINT (arg, 1);
1566 else
1567 CHECK_NUMBER (arg);
1569 val = orig_val = scan_words (PT, XINT (arg));
1570 if (! orig_val)
1571 val = XINT (arg) > 0 ? ZV : BEGV;
1573 /* Avoid jumping out of an input field. */
1574 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1575 Qnil, Qnil, Qnil);
1576 val = XFASTINT (tmp);
1578 SET_PT (val);
1579 return val == orig_val ? Qt : Qnil;
1582 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1583 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1584 STRING is like the inside of a `[...]' in a regular expression
1585 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1586 (but not at the end of a range; quoting is never needed there).
1587 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1588 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1589 Char classes, e.g. `[:alpha:]', are supported.
1591 Returns the distance traveled, either zero or positive. */)
1592 (Lisp_Object string, Lisp_Object lim)
1594 return skip_chars (1, string, lim, 1);
1597 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1598 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1599 See `skip-chars-forward' for details.
1600 Returns the distance traveled, either zero or negative. */)
1601 (Lisp_Object string, Lisp_Object lim)
1603 return skip_chars (0, string, lim, 1);
1606 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1607 doc: /* Move point forward across chars in specified syntax classes.
1608 SYNTAX is a string of syntax code characters.
1609 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1610 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1611 This function returns the distance traveled, either zero or positive. */)
1612 (Lisp_Object syntax, Lisp_Object lim)
1614 return skip_syntaxes (1, syntax, lim);
1617 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1618 doc: /* Move point backward across chars in specified syntax classes.
1619 SYNTAX is a string of syntax code characters.
1620 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1621 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1622 This function returns either zero or a negative number, and the absolute value
1623 of this is the distance traveled. */)
1624 (Lisp_Object syntax, Lisp_Object lim)
1626 return skip_syntaxes (0, syntax, lim);
1629 static Lisp_Object
1630 skip_chars (bool forwardp, Lisp_Object string, Lisp_Object lim,
1631 bool handle_iso_classes)
1633 int c;
1634 char fastmap[0400];
1635 /* Store the ranges of non-ASCII characters. */
1636 int *char_ranges UNINIT;
1637 int n_char_ranges = 0;
1638 bool negate = 0;
1639 ptrdiff_t i, i_byte;
1640 /* True if the current buffer is multibyte and the region contains
1641 non-ASCII chars. */
1642 bool multibyte;
1643 /* True if STRING is multibyte and it contains non-ASCII chars. */
1644 bool string_multibyte;
1645 ptrdiff_t size_byte;
1646 const unsigned char *str;
1647 int len;
1648 Lisp_Object iso_classes;
1649 USE_SAFE_ALLOCA;
1651 CHECK_STRING (string);
1652 iso_classes = Qnil;
1654 if (NILP (lim))
1655 XSETINT (lim, forwardp ? ZV : BEGV);
1656 else
1657 CHECK_NUMBER_COERCE_MARKER (lim);
1659 /* In any case, don't allow scan outside bounds of buffer. */
1660 if (XINT (lim) > ZV)
1661 XSETFASTINT (lim, ZV);
1662 if (XINT (lim) < BEGV)
1663 XSETFASTINT (lim, BEGV);
1665 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1666 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1667 string_multibyte = SBYTES (string) > SCHARS (string);
1669 memset (fastmap, 0, sizeof fastmap);
1671 str = SDATA (string);
1672 size_byte = SBYTES (string);
1674 i_byte = 0;
1675 if (i_byte < size_byte
1676 && SREF (string, 0) == '^')
1678 negate = 1; i_byte++;
1681 /* Find the characters specified and set their elements of fastmap.
1682 Handle backslashes and ranges specially.
1684 If STRING contains non-ASCII characters, setup char_ranges for
1685 them and use fastmap only for their leading codes. */
1687 if (! string_multibyte)
1689 bool string_has_eight_bit = 0;
1691 /* At first setup fastmap. */
1692 while (i_byte < size_byte)
1694 if (handle_iso_classes)
1696 const unsigned char *ch = str + i_byte;
1697 re_wctype_t cc = re_wctype_parse (&ch, size_byte - i_byte);
1698 if (cc == 0)
1699 error ("Invalid ISO C character class");
1700 if (cc != -1)
1702 iso_classes = Fcons (make_number (cc), iso_classes);
1703 i_byte = ch - str;
1704 continue;
1708 c = str[i_byte++];
1710 if (c == '\\')
1712 if (i_byte == size_byte)
1713 break;
1715 c = str[i_byte++];
1717 /* Treat `-' as range character only if another character
1718 follows. */
1719 if (i_byte + 1 < size_byte
1720 && str[i_byte] == '-')
1722 int c2;
1724 /* Skip over the dash. */
1725 i_byte++;
1727 /* Get the end of the range. */
1728 c2 = str[i_byte++];
1729 if (c2 == '\\'
1730 && i_byte < size_byte)
1731 c2 = str[i_byte++];
1733 if (c <= c2)
1735 int lim2 = c2 + 1;
1736 while (c < lim2)
1737 fastmap[c++] = 1;
1738 if (! ASCII_CHAR_P (c2))
1739 string_has_eight_bit = 1;
1742 else
1744 fastmap[c] = 1;
1745 if (! ASCII_CHAR_P (c))
1746 string_has_eight_bit = 1;
1750 /* If the current range is multibyte and STRING contains
1751 eight-bit chars, arrange fastmap and setup char_ranges for
1752 the corresponding multibyte chars. */
1753 if (multibyte && string_has_eight_bit)
1755 char *p1;
1756 char himap[0200 + 1];
1757 memcpy (himap, fastmap + 0200, 0200);
1758 himap[0200] = 0;
1759 memset (fastmap + 0200, 0, 0200);
1760 SAFE_NALLOCA (char_ranges, 2, 128);
1761 i = 0;
1763 while ((p1 = memchr (himap + i, 1, 0200 - i)))
1765 /* Deduce the next range C..C2 from the next clump of 1s
1766 in HIMAP starting with &HIMAP[I]. HIMAP is the high
1767 order half of the old FASTMAP. */
1768 int c2, leading_code;
1769 i = p1 - himap;
1770 c = BYTE8_TO_CHAR (i + 0200);
1771 i += strlen (p1);
1772 c2 = BYTE8_TO_CHAR (i + 0200 - 1);
1774 char_ranges[n_char_ranges++] = c;
1775 char_ranges[n_char_ranges++] = c2;
1776 leading_code = CHAR_LEADING_CODE (c);
1777 memset (fastmap + leading_code, 1,
1778 CHAR_LEADING_CODE (c2) - leading_code + 1);
1782 else /* STRING is multibyte */
1784 SAFE_NALLOCA (char_ranges, 2, SCHARS (string));
1786 while (i_byte < size_byte)
1788 int leading_code = str[i_byte];
1790 if (handle_iso_classes)
1792 const unsigned char *ch = str + i_byte;
1793 re_wctype_t cc = re_wctype_parse (&ch, size_byte - i_byte);
1794 if (cc == 0)
1795 error ("Invalid ISO C character class");
1796 if (cc != -1)
1798 iso_classes = Fcons (make_number (cc), iso_classes);
1799 i_byte = ch - str;
1800 continue;
1804 if (leading_code== '\\')
1806 if (++i_byte == size_byte)
1807 break;
1809 leading_code = str[i_byte];
1811 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1812 i_byte += len;
1815 /* Treat `-' as range character only if another character
1816 follows. */
1817 if (i_byte + 1 < size_byte
1818 && str[i_byte] == '-')
1820 int c2, leading_code2;
1822 /* Skip over the dash. */
1823 i_byte++;
1825 /* Get the end of the range. */
1826 leading_code2 = str[i_byte];
1827 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1828 i_byte += len;
1830 if (c2 == '\\'
1831 && i_byte < size_byte)
1833 leading_code2 = str[i_byte];
1834 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1835 i_byte += len;
1838 if (c > c2)
1839 continue;
1840 if (ASCII_CHAR_P (c))
1842 while (c <= c2 && c < 0x80)
1843 fastmap[c++] = 1;
1844 leading_code = CHAR_LEADING_CODE (c);
1846 if (! ASCII_CHAR_P (c))
1848 int lim2 = leading_code2 + 1;
1849 while (leading_code < lim2)
1850 fastmap[leading_code++] = 1;
1851 if (c <= c2)
1853 char_ranges[n_char_ranges++] = c;
1854 char_ranges[n_char_ranges++] = c2;
1858 else
1860 if (ASCII_CHAR_P (c))
1861 fastmap[c] = 1;
1862 else
1864 fastmap[leading_code] = 1;
1865 char_ranges[n_char_ranges++] = c;
1866 char_ranges[n_char_ranges++] = c;
1871 /* If the current range is unibyte and STRING contains non-ASCII
1872 chars, arrange fastmap for the corresponding unibyte
1873 chars. */
1875 if (! multibyte && n_char_ranges > 0)
1877 memset (fastmap + 0200, 0, 0200);
1878 for (i = 0; i < n_char_ranges; i += 2)
1880 int c1 = char_ranges[i];
1881 int lim2 = char_ranges[i + 1] + 1;
1883 for (; c1 < lim2; c1++)
1885 int b = CHAR_TO_BYTE_SAFE (c1);
1886 if (b >= 0)
1887 fastmap[b] = 1;
1893 /* If ^ was the first character, complement the fastmap. */
1894 if (negate)
1896 if (! multibyte)
1897 for (i = 0; i < sizeof fastmap; i++)
1898 fastmap[i] ^= 1;
1899 else
1901 for (i = 0; i < 0200; i++)
1902 fastmap[i] ^= 1;
1903 /* All non-ASCII chars possibly match. */
1904 for (; i < sizeof fastmap; i++)
1905 fastmap[i] = 1;
1910 ptrdiff_t start_point = PT;
1911 ptrdiff_t pos = PT;
1912 ptrdiff_t pos_byte = PT_BYTE;
1913 unsigned char *p = PT_ADDR, *endp, *stop;
1915 if (forwardp)
1917 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1918 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1920 else
1922 endp = CHAR_POS_ADDR (XINT (lim));
1923 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1926 immediate_quit = 1;
1927 /* This code may look up syntax tables using functions that rely on the
1928 gl_state object. To make sure this object is not out of date,
1929 let's initialize it manually.
1930 We ignore syntax-table text-properties for now, since that's
1931 what we've done in the past. */
1932 SETUP_BUFFER_SYNTAX_TABLE ();
1933 if (forwardp)
1935 if (multibyte)
1936 while (1)
1938 int nbytes;
1940 if (p >= stop)
1942 if (p >= endp)
1943 break;
1944 p = GAP_END_ADDR;
1945 stop = endp;
1947 c = STRING_CHAR_AND_LENGTH (p, nbytes);
1948 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1950 if (negate)
1951 break;
1952 else
1953 goto fwd_ok;
1956 if (! fastmap[*p])
1957 break;
1958 if (! ASCII_CHAR_P (c))
1960 /* As we are looking at a multibyte character, we
1961 must look up the character in the table
1962 CHAR_RANGES. If there's no data in the table,
1963 that character is not what we want to skip. */
1965 /* The following code do the right thing even if
1966 n_char_ranges is zero (i.e. no data in
1967 CHAR_RANGES). */
1968 for (i = 0; i < n_char_ranges; i += 2)
1969 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1970 break;
1971 if (!(negate ^ (i < n_char_ranges)))
1972 break;
1974 fwd_ok:
1975 p += nbytes, pos++, pos_byte += nbytes;
1977 else
1978 while (1)
1980 if (p >= stop)
1982 if (p >= endp)
1983 break;
1984 p = GAP_END_ADDR;
1985 stop = endp;
1988 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1990 if (negate)
1991 break;
1992 else
1993 goto fwd_unibyte_ok;
1996 if (!fastmap[*p])
1997 break;
1998 fwd_unibyte_ok:
1999 p++, pos++, pos_byte++;
2002 else
2004 if (multibyte)
2005 while (1)
2007 unsigned char *prev_p;
2009 if (p <= stop)
2011 if (p <= endp)
2012 break;
2013 p = GPT_ADDR;
2014 stop = endp;
2016 prev_p = p;
2017 while (--p >= stop && ! CHAR_HEAD_P (*p));
2018 c = STRING_CHAR (p);
2020 if (! NILP (iso_classes) && in_classes (c, iso_classes))
2022 if (negate)
2023 break;
2024 else
2025 goto back_ok;
2028 if (! fastmap[*p])
2029 break;
2030 if (! ASCII_CHAR_P (c))
2032 /* See the comment in the previous similar code. */
2033 for (i = 0; i < n_char_ranges; i += 2)
2034 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
2035 break;
2036 if (!(negate ^ (i < n_char_ranges)))
2037 break;
2039 back_ok:
2040 pos--, pos_byte -= prev_p - p;
2042 else
2043 while (1)
2045 if (p <= stop)
2047 if (p <= endp)
2048 break;
2049 p = GPT_ADDR;
2050 stop = endp;
2053 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
2055 if (negate)
2056 break;
2057 else
2058 goto back_unibyte_ok;
2061 if (!fastmap[p[-1]])
2062 break;
2063 back_unibyte_ok:
2064 p--, pos--, pos_byte--;
2068 SET_PT_BOTH (pos, pos_byte);
2069 immediate_quit = 0;
2071 SAFE_FREE ();
2072 return make_number (PT - start_point);
2077 static Lisp_Object
2078 skip_syntaxes (bool forwardp, Lisp_Object string, Lisp_Object lim)
2080 int c;
2081 unsigned char fastmap[0400];
2082 bool negate = 0;
2083 ptrdiff_t i, i_byte;
2084 bool multibyte;
2085 ptrdiff_t size_byte;
2086 unsigned char *str;
2088 CHECK_STRING (string);
2090 if (NILP (lim))
2091 XSETINT (lim, forwardp ? ZV : BEGV);
2092 else
2093 CHECK_NUMBER_COERCE_MARKER (lim);
2095 /* In any case, don't allow scan outside bounds of buffer. */
2096 if (XINT (lim) > ZV)
2097 XSETFASTINT (lim, ZV);
2098 if (XINT (lim) < BEGV)
2099 XSETFASTINT (lim, BEGV);
2101 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
2102 return make_number (0);
2104 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
2105 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
2107 memset (fastmap, 0, sizeof fastmap);
2109 if (SBYTES (string) > SCHARS (string))
2110 /* As this is very rare case (syntax spec is ASCII only), don't
2111 consider efficiency. */
2112 string = string_make_unibyte (string);
2114 str = SDATA (string);
2115 size_byte = SBYTES (string);
2117 i_byte = 0;
2118 if (i_byte < size_byte
2119 && SREF (string, 0) == '^')
2121 negate = 1; i_byte++;
2124 /* Find the syntaxes specified and set their elements of fastmap. */
2126 while (i_byte < size_byte)
2128 c = str[i_byte++];
2129 fastmap[syntax_spec_code[c]] = 1;
2132 /* If ^ was the first character, complement the fastmap. */
2133 if (negate)
2134 for (i = 0; i < sizeof fastmap; i++)
2135 fastmap[i] ^= 1;
2138 ptrdiff_t start_point = PT;
2139 ptrdiff_t pos = PT;
2140 ptrdiff_t pos_byte = PT_BYTE;
2141 unsigned char *p, *endp, *stop;
2143 immediate_quit = 1;
2144 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
2146 if (forwardp)
2148 while (true)
2150 p = BYTE_POS_ADDR (pos_byte);
2151 endp = XINT (lim) == GPT ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
2152 stop = pos < GPT && GPT < XINT (lim) ? GPT_ADDR : endp;
2156 int nbytes;
2158 if (p >= stop)
2160 if (p >= endp)
2161 goto done;
2162 p = GAP_END_ADDR;
2163 stop = endp;
2165 if (multibyte)
2166 c = STRING_CHAR_AND_LENGTH (p, nbytes);
2167 else
2168 c = *p, nbytes = 1;
2169 if (! fastmap[SYNTAX (c)])
2170 goto done;
2171 p += nbytes, pos++, pos_byte += nbytes;
2173 while (!parse_sexp_lookup_properties
2174 || pos < gl_state.e_property);
2176 update_syntax_table_forward (pos + gl_state.offset,
2177 false, gl_state.object);
2180 else
2182 p = BYTE_POS_ADDR (pos_byte);
2183 endp = CHAR_POS_ADDR (XINT (lim));
2184 stop = pos >= GPT && GPT > XINT (lim) ? GAP_END_ADDR : endp;
2186 if (multibyte)
2188 while (1)
2190 unsigned char *prev_p;
2192 if (p <= stop)
2194 if (p <= endp)
2195 break;
2196 p = GPT_ADDR;
2197 stop = endp;
2199 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2200 prev_p = p;
2201 while (--p >= stop && ! CHAR_HEAD_P (*p));
2202 c = STRING_CHAR (p);
2203 if (! fastmap[SYNTAX (c)])
2204 break;
2205 pos--, pos_byte -= prev_p - p;
2208 else
2210 while (1)
2212 if (p <= stop)
2214 if (p <= endp)
2215 break;
2216 p = GPT_ADDR;
2217 stop = endp;
2219 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2220 if (! fastmap[SYNTAX (p[-1])])
2221 break;
2222 p--, pos--, pos_byte--;
2227 done:
2228 SET_PT_BOTH (pos, pos_byte);
2229 immediate_quit = 0;
2231 return make_number (PT - start_point);
2235 /* Return true if character C belongs to one of the ISO classes
2236 in the list ISO_CLASSES. Each class is represented by an
2237 integer which is its type according to re_wctype. */
2239 static bool
2240 in_classes (int c, Lisp_Object iso_classes)
2242 bool fits_class = 0;
2244 while (CONSP (iso_classes))
2246 Lisp_Object elt;
2247 elt = XCAR (iso_classes);
2248 iso_classes = XCDR (iso_classes);
2250 if (re_iswctype (c, XFASTINT (elt)))
2251 fits_class = 1;
2254 return fits_class;
2257 /* Jump over a comment, assuming we are at the beginning of one.
2258 FROM is the current position.
2259 FROM_BYTE is the bytepos corresponding to FROM.
2260 Do not move past STOP (a charpos).
2261 The comment over which we have to jump is of style STYLE
2262 (either SYNTAX_FLAGS_COMMENT_STYLE (foo) or ST_COMMENT_STYLE).
2263 NESTING should be positive to indicate the nesting at the beginning
2264 for nested comments and should be zero or negative else.
2265 ST_COMMENT_STYLE cannot be nested.
2266 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2267 (or 0 If the search cannot start in the middle of a two-character).
2269 If successful, return true and store the charpos of the comment's
2270 end into *CHARPOS_PTR and the corresponding bytepos into
2271 *BYTEPOS_PTR. Else, return false and store the charpos STOP into
2272 *CHARPOS_PTR, the corresponding bytepos into *BYTEPOS_PTR and the
2273 current nesting (as defined for state->incomment) in
2274 *INCOMMENT_PTR. Should the last character scanned in an incomplete
2275 comment be a possible first character of a two character construct,
2276 we store its SYNTAX_WITH_FLAGS into *last_syntax_ptr. Otherwise,
2277 we store Smax into *last_syntax_ptr.
2279 The comment end is the last character of the comment rather than the
2280 character just after the comment.
2282 Global syntax data is assumed to initially be valid for FROM and
2283 remains valid for forward search starting at the returned position. */
2285 static bool
2286 forw_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop,
2287 EMACS_INT nesting, int style, int prev_syntax,
2288 ptrdiff_t *charpos_ptr, ptrdiff_t *bytepos_ptr,
2289 EMACS_INT *incomment_ptr, int *last_syntax_ptr)
2291 register int c, c1;
2292 register enum syntaxcode code;
2293 register int syntax, other_syntax;
2295 if (nesting <= 0) nesting = -1;
2297 /* Enter the loop in the middle so that we find
2298 a 2-char comment ender if we start in the middle of it. */
2299 syntax = prev_syntax;
2300 code = syntax & 0xff;
2301 if (syntax != 0 && from < stop) goto forw_incomment;
2303 while (1)
2305 if (from == stop)
2307 *incomment_ptr = nesting;
2308 *charpos_ptr = from;
2309 *bytepos_ptr = from_byte;
2310 *last_syntax_ptr =
2311 (code == Sescape || code == Scharquote
2312 || SYNTAX_FLAGS_COMEND_FIRST (syntax)
2313 || (nesting > 0
2314 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)))
2315 ? syntax : Smax ;
2316 return 0;
2318 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2319 syntax = SYNTAX_WITH_FLAGS (c);
2320 code = syntax & 0xff;
2321 if (code == Sendcomment
2322 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style
2323 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2324 (nesting > 0 && --nesting == 0) : nesting < 0)
2325 && !(Vcomment_end_can_be_escaped && char_quoted (from, from_byte)))
2326 /* We have encountered a comment end of the same style
2327 as the comment sequence which began this comment
2328 section. */
2329 break;
2330 if (code == Scomment_fence
2331 && style == ST_COMMENT_STYLE)
2332 /* We have encountered a comment end of the same style
2333 as the comment sequence which began this comment
2334 section. */
2335 break;
2336 if (nesting > 0
2337 && code == Scomment
2338 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2339 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style)
2340 /* We have encountered a nested comment of the same style
2341 as the comment sequence which began this comment section. */
2342 nesting++;
2343 INC_BOTH (from, from_byte);
2344 UPDATE_SYNTAX_TABLE_FORWARD (from);
2346 forw_incomment:
2347 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2348 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2349 other_syntax = SYNTAX_WITH_FLAGS (c1),
2350 SYNTAX_FLAGS_COMEND_SECOND (other_syntax))
2351 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, other_syntax) == style
2352 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2353 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax))
2354 ? nesting > 0 : nesting < 0))
2356 syntax = Smax; /* So that "|#" (lisp) can not return
2357 the syntax of "#" in *last_syntax_ptr. */
2358 if (--nesting <= 0)
2359 /* We have encountered a comment end of the same style
2360 as the comment sequence which began this comment section. */
2361 break;
2362 else
2364 INC_BOTH (from, from_byte);
2365 UPDATE_SYNTAX_TABLE_FORWARD (from);
2368 if (nesting > 0
2369 && from < stop
2370 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2371 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2372 other_syntax = SYNTAX_WITH_FLAGS (c1),
2373 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax) == style
2374 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2375 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2376 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax)))
2377 /* We have encountered a nested comment of the same style
2378 as the comment sequence which began this comment section. */
2380 syntax = Smax; /* So that "#|#" isn't also a comment ender. */
2381 INC_BOTH (from, from_byte);
2382 UPDATE_SYNTAX_TABLE_FORWARD (from);
2383 nesting++;
2386 *charpos_ptr = from;
2387 *bytepos_ptr = from_byte;
2388 *last_syntax_ptr = Smax; /* Any syntactic power the last byte had is
2389 used up. */
2390 return 1;
2393 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2394 doc: /*
2395 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2396 Stop scanning if we find something other than a comment or whitespace.
2397 Set point to where scanning stops.
2398 If COUNT comments are found as expected, with nothing except whitespace
2399 between them, return t; otherwise return nil. */)
2400 (Lisp_Object count)
2402 ptrdiff_t from, from_byte, stop;
2403 int c, c1;
2404 enum syntaxcode code;
2405 int comstyle = 0; /* style of comment encountered */
2406 bool comnested = 0; /* whether the comment is nestable or not */
2407 bool found;
2408 EMACS_INT count1;
2409 ptrdiff_t out_charpos, out_bytepos;
2410 EMACS_INT dummy;
2411 int dummy2;
2413 CHECK_NUMBER (count);
2414 count1 = XINT (count);
2415 stop = count1 > 0 ? ZV : BEGV;
2417 immediate_quit = 1;
2418 QUIT;
2420 from = PT;
2421 from_byte = PT_BYTE;
2423 SETUP_SYNTAX_TABLE (from, count1);
2424 while (count1 > 0)
2428 bool comstart_first;
2429 int syntax, other_syntax;
2431 if (from == stop)
2433 SET_PT_BOTH (from, from_byte);
2434 immediate_quit = 0;
2435 return Qnil;
2437 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2438 syntax = SYNTAX_WITH_FLAGS (c);
2439 code = SYNTAX (c);
2440 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2441 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2442 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2443 INC_BOTH (from, from_byte);
2444 UPDATE_SYNTAX_TABLE_FORWARD (from);
2445 if (from < stop && comstart_first
2446 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2447 other_syntax = SYNTAX_WITH_FLAGS (c1),
2448 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)))
2450 /* We have encountered a comment start sequence and we
2451 are ignoring all text inside comments. We must record
2452 the comment style this sequence begins so that later,
2453 only a comment end of the same style actually ends
2454 the comment section. */
2455 code = Scomment;
2456 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2457 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2458 INC_BOTH (from, from_byte);
2459 UPDATE_SYNTAX_TABLE_FORWARD (from);
2462 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2464 if (code == Scomment_fence)
2465 comstyle = ST_COMMENT_STYLE;
2466 else if (code != Scomment)
2468 immediate_quit = 0;
2469 DEC_BOTH (from, from_byte);
2470 SET_PT_BOTH (from, from_byte);
2471 return Qnil;
2473 /* We're at the start of a comment. */
2474 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2475 &out_charpos, &out_bytepos, &dummy, &dummy2);
2476 from = out_charpos; from_byte = out_bytepos;
2477 if (!found)
2479 immediate_quit = 0;
2480 SET_PT_BOTH (from, from_byte);
2481 return Qnil;
2483 INC_BOTH (from, from_byte);
2484 UPDATE_SYNTAX_TABLE_FORWARD (from);
2485 /* We have skipped one comment. */
2486 count1--;
2489 while (count1 < 0)
2491 while (1)
2493 bool quoted;
2494 int syntax;
2496 if (from <= stop)
2498 SET_PT_BOTH (BEGV, BEGV_BYTE);
2499 immediate_quit = 0;
2500 return Qnil;
2503 DEC_BOTH (from, from_byte);
2504 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2505 quoted = char_quoted (from, from_byte);
2506 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2507 syntax = SYNTAX_WITH_FLAGS (c);
2508 code = SYNTAX (c);
2509 comstyle = 0;
2510 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2511 if (code == Sendcomment)
2512 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2513 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2514 && prev_char_comend_first (from, from_byte)
2515 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2517 int other_syntax;
2518 /* We must record the comment style encountered so that
2519 later, we can match only the proper comment begin
2520 sequence of the same style. */
2521 DEC_BOTH (from, from_byte);
2522 code = Sendcomment;
2523 /* Calling char_quoted, above, set up global syntax position
2524 at the new value of FROM. */
2525 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2526 other_syntax = SYNTAX_WITH_FLAGS (c1);
2527 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2528 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2531 if (code == Scomment_fence)
2533 /* Skip until first preceding unquoted comment_fence. */
2534 bool fence_found = 0;
2535 ptrdiff_t ini = from, ini_byte = from_byte;
2537 while (1)
2539 DEC_BOTH (from, from_byte);
2540 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2541 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2542 if (SYNTAX (c) == Scomment_fence
2543 && !char_quoted (from, from_byte))
2545 fence_found = 1;
2546 break;
2548 else if (from == stop)
2549 break;
2551 if (fence_found == 0)
2553 from = ini; /* Set point to ini + 1. */
2554 from_byte = ini_byte;
2555 goto leave;
2557 else
2558 /* We have skipped one comment. */
2559 break;
2561 else if (code == Sendcomment)
2563 found = back_comment (from, from_byte, stop, comnested, comstyle,
2564 &out_charpos, &out_bytepos);
2565 if (!found)
2567 if (c == '\n')
2568 /* This end-of-line is not an end-of-comment.
2569 Treat it like a whitespace.
2570 CC-mode (and maybe others) relies on this behavior. */
2572 else
2574 /* Failure: we should go back to the end of this
2575 not-quite-endcomment. */
2576 if (SYNTAX (c) != code)
2577 /* It was a two-char Sendcomment. */
2578 INC_BOTH (from, from_byte);
2579 goto leave;
2582 else
2584 /* We have skipped one comment. */
2585 from = out_charpos, from_byte = out_bytepos;
2586 break;
2589 else if (code != Swhitespace || quoted)
2591 leave:
2592 immediate_quit = 0;
2593 INC_BOTH (from, from_byte);
2594 SET_PT_BOTH (from, from_byte);
2595 return Qnil;
2599 count1++;
2602 SET_PT_BOTH (from, from_byte);
2603 immediate_quit = 0;
2604 return Qt;
2607 /* Return syntax code of character C if C is an ASCII character
2608 or if MULTIBYTE_SYMBOL_P is false. Otherwise, return Ssymbol. */
2610 static enum syntaxcode
2611 syntax_multibyte (int c, bool multibyte_symbol_p)
2613 return ASCII_CHAR_P (c) || !multibyte_symbol_p ? SYNTAX (c) : Ssymbol;
2616 static Lisp_Object
2617 scan_lists (EMACS_INT from, EMACS_INT count, EMACS_INT depth, bool sexpflag)
2619 Lisp_Object val;
2620 ptrdiff_t stop = count > 0 ? ZV : BEGV;
2621 int c, c1;
2622 int stringterm;
2623 bool quoted;
2624 bool mathexit = 0;
2625 enum syntaxcode code;
2626 EMACS_INT min_depth = depth; /* Err out if depth gets less than this. */
2627 int comstyle = 0; /* Style of comment encountered. */
2628 bool comnested = 0; /* Whether the comment is nestable or not. */
2629 ptrdiff_t temp_pos;
2630 EMACS_INT last_good = from;
2631 bool found;
2632 ptrdiff_t from_byte;
2633 ptrdiff_t out_bytepos, out_charpos;
2634 EMACS_INT dummy;
2635 int dummy2;
2636 bool multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2638 if (depth > 0) min_depth = 0;
2640 if (from > ZV) from = ZV;
2641 if (from < BEGV) from = BEGV;
2643 from_byte = CHAR_TO_BYTE (from);
2645 immediate_quit = 1;
2646 QUIT;
2648 SETUP_SYNTAX_TABLE (from, count);
2649 while (count > 0)
2651 while (from < stop)
2653 bool comstart_first, prefix;
2654 int syntax, other_syntax;
2655 UPDATE_SYNTAX_TABLE_FORWARD (from);
2656 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2657 syntax = SYNTAX_WITH_FLAGS (c);
2658 code = syntax_multibyte (c, multibyte_symbol_p);
2659 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2660 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2661 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2662 prefix = SYNTAX_FLAGS_PREFIX (syntax);
2663 if (depth == min_depth)
2664 last_good = from;
2665 INC_BOTH (from, from_byte);
2666 UPDATE_SYNTAX_TABLE_FORWARD (from);
2667 if (from < stop && comstart_first
2668 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2669 other_syntax = SYNTAX_WITH_FLAGS (c),
2670 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2671 && parse_sexp_ignore_comments)
2673 /* We have encountered a comment start sequence and we
2674 are ignoring all text inside comments. We must record
2675 the comment style this sequence begins so that later,
2676 only a comment end of the same style actually ends
2677 the comment section. */
2678 code = Scomment;
2679 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2680 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2681 INC_BOTH (from, from_byte);
2682 UPDATE_SYNTAX_TABLE_FORWARD (from);
2685 if (prefix)
2686 continue;
2688 switch (code)
2690 case Sescape:
2691 case Scharquote:
2692 if (from == stop)
2693 goto lose;
2694 INC_BOTH (from, from_byte);
2695 /* Treat following character as a word constituent. */
2696 case Sword:
2697 case Ssymbol:
2698 if (depth || !sexpflag) break;
2699 /* This word counts as a sexp; return at end of it. */
2700 while (from < stop)
2702 UPDATE_SYNTAX_TABLE_FORWARD (from);
2704 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2705 switch (syntax_multibyte (c, multibyte_symbol_p))
2707 case Scharquote:
2708 case Sescape:
2709 INC_BOTH (from, from_byte);
2710 if (from == stop)
2711 goto lose;
2712 break;
2713 case Sword:
2714 case Ssymbol:
2715 case Squote:
2716 break;
2717 default:
2718 goto done;
2720 INC_BOTH (from, from_byte);
2722 goto done;
2724 case Scomment_fence:
2725 comstyle = ST_COMMENT_STYLE;
2726 /* FALLTHROUGH */
2727 case Scomment:
2728 if (!parse_sexp_ignore_comments) break;
2729 UPDATE_SYNTAX_TABLE_FORWARD (from);
2730 found = forw_comment (from, from_byte, stop,
2731 comnested, comstyle, 0,
2732 &out_charpos, &out_bytepos, &dummy,
2733 &dummy2);
2734 from = out_charpos, from_byte = out_bytepos;
2735 if (!found)
2737 if (depth == 0)
2738 goto done;
2739 goto lose;
2741 INC_BOTH (from, from_byte);
2742 UPDATE_SYNTAX_TABLE_FORWARD (from);
2743 break;
2745 case Smath:
2746 if (!sexpflag)
2747 break;
2748 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2750 INC_BOTH (from, from_byte);
2752 if (mathexit)
2754 mathexit = 0;
2755 goto close1;
2757 mathexit = 1;
2759 case Sopen:
2760 if (!++depth) goto done;
2761 break;
2763 case Sclose:
2764 close1:
2765 if (!--depth) goto done;
2766 if (depth < min_depth)
2767 xsignal3 (Qscan_error,
2768 build_string ("Containing expression ends prematurely"),
2769 make_number (last_good), make_number (from));
2770 break;
2772 case Sstring:
2773 case Sstring_fence:
2774 temp_pos = dec_bytepos (from_byte);
2775 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2776 while (1)
2778 enum syntaxcode c_code;
2779 if (from >= stop)
2780 goto lose;
2781 UPDATE_SYNTAX_TABLE_FORWARD (from);
2782 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2783 c_code = syntax_multibyte (c, multibyte_symbol_p);
2784 if (code == Sstring
2785 ? c == stringterm && c_code == Sstring
2786 : c_code == Sstring_fence)
2787 break;
2789 if (c_code == Scharquote || c_code == Sescape)
2790 INC_BOTH (from, from_byte);
2791 INC_BOTH (from, from_byte);
2793 INC_BOTH (from, from_byte);
2794 if (!depth && sexpflag) goto done;
2795 break;
2796 default:
2797 /* Ignore whitespace, punctuation, quote, endcomment. */
2798 break;
2802 /* Reached end of buffer. Error if within object, return nil if between */
2803 if (depth)
2804 goto lose;
2806 immediate_quit = 0;
2807 return Qnil;
2809 /* End of object reached */
2810 done:
2811 count--;
2814 while (count < 0)
2816 while (from > stop)
2818 int syntax;
2819 DEC_BOTH (from, from_byte);
2820 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2821 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2822 syntax= SYNTAX_WITH_FLAGS (c);
2823 code = syntax_multibyte (c, multibyte_symbol_p);
2824 if (depth == min_depth)
2825 last_good = from;
2826 comstyle = 0;
2827 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2828 if (code == Sendcomment)
2829 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2830 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2831 && prev_char_comend_first (from, from_byte)
2832 && parse_sexp_ignore_comments)
2834 /* We must record the comment style encountered so that
2835 later, we can match only the proper comment begin
2836 sequence of the same style. */
2837 int c2, other_syntax;
2838 DEC_BOTH (from, from_byte);
2839 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2840 code = Sendcomment;
2841 c2 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2842 other_syntax = SYNTAX_WITH_FLAGS (c2);
2843 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2844 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2847 /* Quoting turns anything except a comment-ender
2848 into a word character. Note that this cannot be true
2849 if we decremented FROM in the if-statement above. */
2850 if (code != Sendcomment && char_quoted (from, from_byte))
2852 DEC_BOTH (from, from_byte);
2853 code = Sword;
2855 else if (SYNTAX_FLAGS_PREFIX (syntax))
2856 continue;
2858 switch (code)
2860 case Sword:
2861 case Ssymbol:
2862 case Sescape:
2863 case Scharquote:
2864 if (depth || !sexpflag) break;
2865 /* This word counts as a sexp; count object finished
2866 after passing it. */
2867 while (from > stop)
2869 temp_pos = from_byte;
2870 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2871 DEC_POS (temp_pos);
2872 else
2873 temp_pos--;
2874 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2875 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2876 /* Don't allow comment-end to be quoted. */
2877 if (syntax_multibyte (c1, multibyte_symbol_p) == Sendcomment)
2878 goto done2;
2879 quoted = char_quoted (from - 1, temp_pos);
2880 if (quoted)
2882 DEC_BOTH (from, from_byte);
2883 temp_pos = dec_bytepos (temp_pos);
2884 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2886 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2887 if (! quoted)
2888 switch (syntax_multibyte (c1, multibyte_symbol_p))
2890 case Sword: case Ssymbol: case Squote: break;
2891 default: goto done2;
2893 DEC_BOTH (from, from_byte);
2895 goto done2;
2897 case Smath:
2898 if (!sexpflag)
2899 break;
2900 if (from > BEGV)
2902 temp_pos = dec_bytepos (from_byte);
2903 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2904 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2905 DEC_BOTH (from, from_byte);
2907 if (mathexit)
2909 mathexit = 0;
2910 goto open2;
2912 mathexit = 1;
2914 case Sclose:
2915 if (!++depth) goto done2;
2916 break;
2918 case Sopen:
2919 open2:
2920 if (!--depth) goto done2;
2921 if (depth < min_depth)
2922 xsignal3 (Qscan_error,
2923 build_string ("Containing expression ends prematurely"),
2924 make_number (last_good), make_number (from));
2925 break;
2927 case Sendcomment:
2928 if (!parse_sexp_ignore_comments)
2929 break;
2930 found = back_comment (from, from_byte, stop, comnested, comstyle,
2931 &out_charpos, &out_bytepos);
2932 /* FIXME: if !found, it really wasn't a comment-end.
2933 For single-char Sendcomment, we can't do much about it apart
2934 from skipping the char.
2935 For 2-char endcomments, we could try again, taking both
2936 chars as separate entities, but it's a lot of trouble
2937 for very little gain, so we don't bother either. -sm */
2938 if (found)
2939 from = out_charpos, from_byte = out_bytepos;
2940 break;
2942 case Scomment_fence:
2943 case Sstring_fence:
2944 while (1)
2946 if (from == stop)
2947 goto lose;
2948 DEC_BOTH (from, from_byte);
2949 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2950 if (!char_quoted (from, from_byte))
2952 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2953 if (syntax_multibyte (c, multibyte_symbol_p) == code)
2954 break;
2957 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2958 break;
2960 case Sstring:
2961 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2962 while (1)
2964 if (from == stop)
2965 goto lose;
2966 DEC_BOTH (from, from_byte);
2967 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2968 if (!char_quoted (from, from_byte))
2970 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2971 if (c == stringterm
2972 && (syntax_multibyte (c, multibyte_symbol_p)
2973 == Sstring))
2974 break;
2977 if (!depth && sexpflag) goto done2;
2978 break;
2979 default:
2980 /* Ignore whitespace, punctuation, quote, endcomment. */
2981 break;
2985 /* Reached start of buffer. Error if within object, return nil if between */
2986 if (depth)
2987 goto lose;
2989 immediate_quit = 0;
2990 return Qnil;
2992 done2:
2993 count++;
2997 immediate_quit = 0;
2998 XSETFASTINT (val, from);
2999 return val;
3001 lose:
3002 xsignal3 (Qscan_error,
3003 build_string ("Unbalanced parentheses"),
3004 make_number (last_good), make_number (from));
3007 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
3008 doc: /* Scan from character number FROM by COUNT lists.
3009 Scan forward if COUNT is positive, backward if COUNT is negative.
3010 Return the character number of the position thus found.
3012 A \"list", in this context, refers to a balanced parenthetical
3013 grouping, as determined by the syntax table.
3015 If DEPTH is nonzero, treat that as the nesting depth of the starting
3016 point (i.e. the starting point is DEPTH parentheses deep). This
3017 function scans over parentheses until the depth goes to zero COUNT
3018 times. Hence, positive DEPTH moves out that number of levels of
3019 parentheses, while negative DEPTH moves to a deeper level.
3021 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3023 If we reach the beginning or end of the accessible part of the buffer
3024 before we have scanned over COUNT lists, return nil if the depth at
3025 that point is zero, and signal a error if the depth is nonzero. */)
3026 (Lisp_Object from, Lisp_Object count, Lisp_Object depth)
3028 CHECK_NUMBER (from);
3029 CHECK_NUMBER (count);
3030 CHECK_NUMBER (depth);
3032 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
3035 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
3036 doc: /* Scan from character number FROM by COUNT balanced expressions.
3037 If COUNT is negative, scan backwards.
3038 Returns the character number of the position thus found.
3040 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3042 If the beginning or end of (the accessible part of) the buffer is reached
3043 in the middle of a parenthetical grouping, an error is signaled.
3044 If the beginning or end is reached between groupings
3045 but before count is used up, nil is returned. */)
3046 (Lisp_Object from, Lisp_Object count)
3048 CHECK_NUMBER (from);
3049 CHECK_NUMBER (count);
3051 return scan_lists (XINT (from), XINT (count), 0, 1);
3054 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
3055 0, 0, 0,
3056 doc: /* Move point backward over any number of chars with prefix syntax.
3057 This includes chars with expression prefix syntax class (\\=') and those with
3058 the prefix syntax flag (p). */)
3059 (void)
3061 ptrdiff_t beg = BEGV;
3062 ptrdiff_t opoint = PT;
3063 ptrdiff_t opoint_byte = PT_BYTE;
3064 ptrdiff_t pos = PT;
3065 ptrdiff_t pos_byte = PT_BYTE;
3066 int c;
3068 if (pos <= beg)
3070 SET_PT_BOTH (opoint, opoint_byte);
3072 return Qnil;
3075 SETUP_SYNTAX_TABLE (pos, -1);
3077 DEC_BOTH (pos, pos_byte);
3079 while (!char_quoted (pos, pos_byte)
3080 /* Previous statement updates syntax table. */
3081 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
3082 || syntax_prefix_flag_p (c)))
3084 opoint = pos;
3085 opoint_byte = pos_byte;
3087 if (pos <= beg)
3088 break;
3089 DEC_BOTH (pos, pos_byte);
3092 SET_PT_BOTH (opoint, opoint_byte);
3094 return Qnil;
3097 /* Parse forward from FROM / FROM_BYTE to END,
3098 assuming that FROM has state STATE,
3099 and return a description of the state of the parse at END.
3100 If STOPBEFORE, stop at the start of an atom.
3101 If COMMENTSTOP is 1, stop at the start of a comment.
3102 If COMMENTSTOP is -1, stop at the start or end of a comment,
3103 after the beginning of a string, or after the end of a string. */
3105 static void
3106 scan_sexps_forward (struct lisp_parse_state *state,
3107 ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t end,
3108 EMACS_INT targetdepth, bool stopbefore,
3109 int commentstop)
3111 enum syntaxcode code;
3112 int c1;
3113 bool comnested;
3114 struct level { ptrdiff_t last, prev; };
3115 struct level levelstart[100];
3116 struct level *curlevel = levelstart;
3117 struct level *endlevel = levelstart + 100;
3118 EMACS_INT depth; /* Paren depth of current scanning location.
3119 level - levelstart equals this except
3120 when the depth becomes negative. */
3121 EMACS_INT mindepth; /* Lowest DEPTH value seen. */
3122 bool start_quoted = 0; /* True means starting after a char quote. */
3123 Lisp_Object tem;
3124 ptrdiff_t prev_from; /* Keep one character before FROM. */
3125 ptrdiff_t prev_from_byte;
3126 int prev_from_syntax, prev_prev_from_syntax;
3127 bool boundary_stop = commentstop == -1;
3128 bool nofence;
3129 bool found;
3130 ptrdiff_t out_bytepos, out_charpos;
3131 int temp;
3133 prev_from = from;
3134 prev_from_byte = from_byte;
3135 if (from != BEGV)
3136 DEC_BOTH (prev_from, prev_from_byte);
3138 /* Use this macro instead of `from++'. */
3139 #define INC_FROM \
3140 do { prev_from = from; \
3141 prev_from_byte = from_byte; \
3142 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
3143 prev_prev_from_syntax = prev_from_syntax; \
3144 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
3145 INC_BOTH (from, from_byte); \
3146 if (from < end) \
3147 UPDATE_SYNTAX_TABLE_FORWARD (from); \
3148 } while (0)
3150 immediate_quit = 1;
3151 QUIT;
3153 depth = state->depth;
3154 start_quoted = state->quoted;
3155 prev_prev_from_syntax = Smax;
3156 prev_from_syntax = state->prev_syntax;
3158 tem = state->levelstarts;
3159 while (!NILP (tem)) /* >= second enclosing sexps. */
3161 Lisp_Object temhd = Fcar (tem);
3162 if (RANGED_INTEGERP (PTRDIFF_MIN, temhd, PTRDIFF_MAX))
3163 curlevel->last = XINT (temhd);
3164 if (++curlevel == endlevel)
3165 curlevel--; /* error ("Nesting too deep for parser"); */
3166 curlevel->prev = -1;
3167 curlevel->last = -1;
3168 tem = Fcdr (tem);
3170 curlevel->prev = -1;
3171 curlevel->last = -1;
3173 state->quoted = 0;
3174 mindepth = depth;
3176 SETUP_SYNTAX_TABLE (from, 1);
3178 /* Enter the loop at a place appropriate for initial state. */
3180 if (state->incomment)
3181 goto startincomment;
3182 if (state->instring >= 0)
3184 nofence = state->instring != ST_STRING_STYLE;
3185 if (start_quoted)
3186 goto startquotedinstring;
3187 goto startinstring;
3189 else if (start_quoted)
3190 goto startquoted;
3192 while (from < end)
3194 int syntax;
3196 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3197 && (c1 = FETCH_CHAR (from_byte),
3198 syntax = SYNTAX_WITH_FLAGS (c1),
3199 SYNTAX_FLAGS_COMSTART_SECOND (syntax)))
3200 /* Duplicate code to avoid a complex if-expression
3201 which causes trouble for the SGI compiler. */
3203 /* Record the comment style we have entered so that only
3204 the comment-end sequence of the same style actually
3205 terminates the comment section. */
3206 state->comstyle
3207 = SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_from_syntax);
3208 comnested = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax)
3209 | SYNTAX_FLAGS_COMMENT_NESTED (syntax));
3210 state->incomment = comnested ? 1 : -1;
3211 state->comstr_start = prev_from;
3212 INC_FROM;
3213 prev_from_syntax = Smax; /* the syntax has already been
3214 "used up". */
3215 code = Scomment;
3217 else
3219 INC_FROM;
3220 code = prev_from_syntax & 0xff;
3221 if (code == Scomment_fence)
3223 /* Record the comment style we have entered so that only
3224 the comment-end sequence of the same style actually
3225 terminates the comment section. */
3226 state->comstyle = ST_COMMENT_STYLE;
3227 state->incomment = -1;
3228 state->comstr_start = prev_from;
3229 code = Scomment;
3231 else if (code == Scomment)
3233 state->comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax, 0);
3234 state->incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3235 1 : -1);
3236 state->comstr_start = prev_from;
3240 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3241 continue;
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 int symchar = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3261 switch (SYNTAX (symchar))
3263 case Scharquote:
3264 case Sescape:
3265 INC_FROM;
3266 if (from == end) goto endquoted;
3267 break;
3268 case Sword:
3269 case Ssymbol:
3270 case Squote:
3271 break;
3272 default:
3273 goto symdone;
3275 INC_FROM;
3277 symdone:
3278 curlevel->prev = curlevel->last;
3279 break;
3281 case Scomment_fence: /* Can't happen because it's handled above. */
3282 case Scomment:
3283 if (commentstop || boundary_stop) goto done;
3284 startincomment:
3285 /* The (from == BEGV) test was to enter the loop in the middle so
3286 that we find a 2-char comment ender even if we start in the
3287 middle of it. We don't want to do that if we're just at the
3288 beginning of the comment (think of (*) ... (*)). */
3289 found = forw_comment (from, from_byte, end,
3290 state->incomment, state->comstyle,
3291 from == BEGV ? 0 : prev_from_syntax,
3292 &out_charpos, &out_bytepos, &state->incomment,
3293 &prev_from_syntax);
3294 from = out_charpos; from_byte = out_bytepos;
3295 /* Beware! prev_from and friends (except prev_from_syntax)
3296 are invalid now. Luckily, the `done' doesn't use them
3297 and the INC_FROM sets them to a sane value without
3298 looking at them. */
3299 if (!found) goto done;
3300 INC_FROM;
3301 state->incomment = 0;
3302 state->comstyle = 0; /* reset the comment style */
3303 prev_from_syntax = Smax; /* For the comment closer */
3304 if (boundary_stop) goto done;
3305 break;
3307 case Sopen:
3308 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3309 depth++;
3310 /* curlevel++->last ran into compiler bug on Apollo */
3311 curlevel->last = prev_from;
3312 if (++curlevel == endlevel)
3313 curlevel--; /* error ("Nesting too deep for parser"); */
3314 curlevel->prev = -1;
3315 curlevel->last = -1;
3316 if (targetdepth == depth) goto done;
3317 break;
3319 case Sclose:
3320 depth--;
3321 if (depth < mindepth)
3322 mindepth = depth;
3323 if (curlevel != levelstart)
3324 curlevel--;
3325 curlevel->prev = curlevel->last;
3326 if (targetdepth == depth) goto done;
3327 break;
3329 case Sstring:
3330 case Sstring_fence:
3331 state->comstr_start = from - 1;
3332 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3333 curlevel->last = prev_from;
3334 state->instring = (code == Sstring
3335 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3336 : ST_STRING_STYLE);
3337 if (boundary_stop) goto done;
3338 startinstring:
3340 nofence = state->instring != ST_STRING_STYLE;
3342 while (1)
3344 int c;
3345 enum syntaxcode c_code;
3347 if (from >= end) goto done;
3348 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3349 c_code = SYNTAX (c);
3351 /* Check C_CODE here so that if the char has
3352 a syntax-table property which says it is NOT
3353 a string character, it does not end the string. */
3354 if (nofence && c == state->instring && c_code == Sstring)
3355 break;
3357 switch (c_code)
3359 case Sstring_fence:
3360 if (!nofence) goto string_end;
3361 break;
3363 case Scharquote:
3364 case Sescape:
3365 INC_FROM;
3366 startquotedinstring:
3367 if (from >= end) goto endquoted;
3368 break;
3370 default:
3371 break;
3373 INC_FROM;
3376 string_end:
3377 state->instring = -1;
3378 curlevel->prev = curlevel->last;
3379 INC_FROM;
3380 if (boundary_stop) goto done;
3381 break;
3383 case Smath:
3384 /* FIXME: We should do something with it. */
3385 break;
3386 default:
3387 /* Ignore whitespace, punctuation, quote, endcomment. */
3388 break;
3391 goto done;
3393 stop: /* Here if stopping before start of sexp. */
3394 from = prev_from; /* We have just fetched the char that starts it; */
3395 from_byte = prev_from_byte;
3396 prev_from_syntax = prev_prev_from_syntax;
3397 goto done; /* but return the position before it. */
3399 endquoted:
3400 state->quoted = 1;
3401 done:
3402 state->depth = depth;
3403 state->mindepth = mindepth;
3404 state->thislevelstart = curlevel->prev;
3405 state->prevlevelstart
3406 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3407 state->location = from;
3408 state->location_byte = from_byte;
3409 state->levelstarts = Qnil;
3410 while (curlevel > levelstart)
3411 state->levelstarts = Fcons (make_number ((--curlevel)->last),
3412 state->levelstarts);
3413 state->prev_syntax = (SYNTAX_FLAGS_COMSTARTEND_FIRST (prev_from_syntax)
3414 || state->quoted) ? prev_from_syntax : Smax;
3415 immediate_quit = 0;
3418 /* Convert a (lisp) parse state to the internal form used in
3419 scan_sexps_forward. */
3420 static void
3421 internalize_parse_state (Lisp_Object external, struct lisp_parse_state *state)
3423 Lisp_Object tem;
3425 if (NILP (external))
3427 state->depth = 0;
3428 state->instring = -1;
3429 state->incomment = 0;
3430 state->quoted = 0;
3431 state->comstyle = 0; /* comment style a by default. */
3432 state->comstr_start = -1; /* no comment/string seen. */
3433 state->levelstarts = Qnil;
3434 state->prev_syntax = Smax;
3436 else
3438 tem = Fcar (external);
3439 if (!NILP (tem))
3440 state->depth = XINT (tem);
3441 else
3442 state->depth = 0;
3444 external = Fcdr (external);
3445 external = Fcdr (external);
3446 external = Fcdr (external);
3447 tem = Fcar (external);
3448 /* Check whether we are inside string_fence-style string: */
3449 state->instring = (!NILP (tem)
3450 ? (CHARACTERP (tem) ? XFASTINT (tem) : ST_STRING_STYLE)
3451 : -1);
3453 external = Fcdr (external);
3454 tem = Fcar (external);
3455 state->incomment = (!NILP (tem)
3456 ? (INTEGERP (tem) ? XINT (tem) : -1)
3457 : 0);
3459 external = Fcdr (external);
3460 tem = Fcar (external);
3461 state->quoted = !NILP (tem);
3463 /* if the eighth element of the list is nil, we are in comment
3464 style a. If it is non-nil, we are in comment style b */
3465 external = Fcdr (external);
3466 external = Fcdr (external);
3467 tem = Fcar (external);
3468 state->comstyle = (NILP (tem)
3470 : (RANGED_INTEGERP (0, tem, ST_COMMENT_STYLE)
3471 ? XINT (tem)
3472 : ST_COMMENT_STYLE));
3474 external = Fcdr (external);
3475 tem = Fcar (external);
3476 state->comstr_start =
3477 RANGED_INTEGERP (PTRDIFF_MIN, tem, PTRDIFF_MAX) ? XINT (tem) : -1;
3478 external = Fcdr (external);
3479 tem = Fcar (external);
3480 state->levelstarts = tem;
3482 external = Fcdr (external);
3483 tem = Fcar (external);
3484 state->prev_syntax = NILP (tem) ? Smax : XINT (tem);
3488 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3489 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3490 Parsing stops at TO or when certain criteria are met;
3491 point is set to where parsing stops.
3492 If fifth arg OLDSTATE is omitted or nil,
3493 parsing assumes that FROM is the beginning of a function.
3495 Value is a list of elements describing final state of parsing:
3496 0. depth in parens.
3497 1. character address of start of innermost containing list; nil if none.
3498 2. character address of start of last complete sexp terminated.
3499 3. non-nil if inside a string.
3500 (it is the character that will terminate the string,
3501 or t if the string should be terminated by a generic string delimiter.)
3502 4. nil if outside a comment, t if inside a non-nestable comment,
3503 else an integer (the current comment nesting).
3504 5. t if following a quote character.
3505 6. the minimum paren-depth encountered during this scan.
3506 7. style of comment, if any.
3507 8. character address of start of comment or string; nil if not in one.
3508 9. List of positions of currently open parens, outermost first.
3509 10. When the last position scanned holds the first character of a
3510 (potential) two character construct, the syntax of that position,
3511 otherwise nil. That construct can be a two character comment
3512 delimiter or an Escaped or Char-quoted character.
3513 11..... Possible further internal information used by `parse-partial-sexp'.
3515 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3516 in parentheses becomes equal to TARGETDEPTH.
3517 Fourth arg STOPBEFORE non-nil means stop when we come to
3518 any character that starts a sexp.
3519 Fifth arg OLDSTATE is a list like what this function returns.
3520 It is used to initialize the state of the parse. Elements number 1, 2, 6
3521 are ignored.
3522 Sixth arg COMMENTSTOP non-nil means stop after the start of a comment.
3523 If it is the symbol `syntax-table', stop after the start of a comment or a
3524 string, or after end of a comment or a string. */)
3525 (Lisp_Object from, Lisp_Object to, Lisp_Object targetdepth,
3526 Lisp_Object stopbefore, Lisp_Object oldstate, Lisp_Object commentstop)
3528 struct lisp_parse_state state;
3529 EMACS_INT target;
3531 if (!NILP (targetdepth))
3533 CHECK_NUMBER (targetdepth);
3534 target = XINT (targetdepth);
3536 else
3537 target = TYPE_MINIMUM (EMACS_INT); /* We won't reach this depth. */
3539 validate_region (&from, &to);
3540 internalize_parse_state (oldstate, &state);
3541 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3542 XINT (to),
3543 target, !NILP (stopbefore),
3544 (NILP (commentstop)
3545 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3547 SET_PT_BOTH (state.location, state.location_byte);
3549 return
3550 Fcons (make_number (state.depth),
3551 Fcons (state.prevlevelstart < 0
3552 ? Qnil : make_number (state.prevlevelstart),
3553 Fcons (state.thislevelstart < 0
3554 ? Qnil : make_number (state.thislevelstart),
3555 Fcons (state.instring >= 0
3556 ? (state.instring == ST_STRING_STYLE
3557 ? Qt : make_number (state.instring)) : Qnil,
3558 Fcons (state.incomment < 0 ? Qt :
3559 (state.incomment == 0 ? Qnil :
3560 make_number (state.incomment)),
3561 Fcons (state.quoted ? Qt : Qnil,
3562 Fcons (make_number (state.mindepth),
3563 Fcons ((state.comstyle
3564 ? (state.comstyle == ST_COMMENT_STYLE
3565 ? Qsyntax_table
3566 : make_number (state.comstyle))
3567 : Qnil),
3568 Fcons (((state.incomment
3569 || (state.instring >= 0))
3570 ? make_number (state.comstr_start)
3571 : Qnil),
3572 Fcons (state.levelstarts,
3573 Fcons (state.prev_syntax == Smax
3574 ? Qnil
3575 : make_number (state.prev_syntax),
3576 Qnil)))))))))));
3579 void
3580 init_syntax_once (void)
3582 register int i, c;
3583 Lisp_Object temp;
3585 /* This has to be done here, before we call Fmake_char_table. */
3586 DEFSYM (Qsyntax_table, "syntax-table");
3588 /* Create objects which can be shared among syntax tables. */
3589 Vsyntax_code_object = make_uninit_vector (Smax);
3590 for (i = 0; i < Smax; i++)
3591 ASET (Vsyntax_code_object, i, Fcons (make_number (i), Qnil));
3593 /* Now we are ready to set up this property, so we can
3594 create syntax tables. */
3595 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3597 temp = AREF (Vsyntax_code_object, Swhitespace);
3599 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3601 /* Control characters should not be whitespace. */
3602 temp = AREF (Vsyntax_code_object, Spunct);
3603 for (i = 0; i <= ' ' - 1; i++)
3604 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3605 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3607 /* Except that a few really are whitespace. */
3608 temp = AREF (Vsyntax_code_object, Swhitespace);
3609 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3610 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3611 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3612 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3613 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3615 temp = AREF (Vsyntax_code_object, Sword);
3616 for (i = 'a'; i <= 'z'; i++)
3617 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3618 for (i = 'A'; i <= 'Z'; i++)
3619 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3620 for (i = '0'; i <= '9'; i++)
3621 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3623 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3624 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3626 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3627 Fcons (make_number (Sopen), make_number (')')));
3628 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3629 Fcons (make_number (Sclose), make_number ('(')));
3630 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3631 Fcons (make_number (Sopen), make_number (']')));
3632 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3633 Fcons (make_number (Sclose), make_number ('[')));
3634 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3635 Fcons (make_number (Sopen), make_number ('}')));
3636 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3637 Fcons (make_number (Sclose), make_number ('{')));
3638 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3639 Fcons (make_number (Sstring), Qnil));
3640 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3641 Fcons (make_number (Sescape), Qnil));
3643 temp = AREF (Vsyntax_code_object, Ssymbol);
3644 for (i = 0; i < 10; i++)
3646 c = "_-+*/&|<>="[i];
3647 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3650 temp = AREF (Vsyntax_code_object, Spunct);
3651 for (i = 0; i < 12; i++)
3653 c = ".,;:?!#@~^'`"[i];
3654 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3657 /* All multibyte characters have syntax `word' by default. */
3658 temp = AREF (Vsyntax_code_object, Sword);
3659 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3662 void
3663 syms_of_syntax (void)
3665 DEFSYM (Qsyntax_table_p, "syntax-table-p");
3667 staticpro (&Vsyntax_code_object);
3669 staticpro (&gl_state.object);
3670 staticpro (&gl_state.global_code);
3671 staticpro (&gl_state.current_syntax_table);
3672 staticpro (&gl_state.old_prop);
3674 /* Defined in regex.c. */
3675 staticpro (&re_match_object);
3677 DEFSYM (Qscan_error, "scan-error");
3678 Fput (Qscan_error, Qerror_conditions,
3679 listn (CONSTYPE_PURE, 2, Qscan_error, Qerror));
3680 Fput (Qscan_error, Qerror_message,
3681 build_pure_c_string ("Scan error"));
3683 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments,
3684 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3686 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties,
3687 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3688 Otherwise, that text property is simply ignored.
3689 See the info node `(elisp)Syntax Properties' for a description of the
3690 `syntax-table' property. */);
3692 DEFVAR_INT ("syntax-propertize--done", syntax_propertize__done,
3693 doc: /* Position up to which syntax-table properties have been set. */);
3694 syntax_propertize__done = -1;
3695 DEFSYM (Qinternal__syntax_propertize, "internal--syntax-propertize");
3696 Fmake_variable_buffer_local (intern ("syntax-propertize--done"));
3698 words_include_escapes = 0;
3699 DEFVAR_BOOL ("words-include-escapes", words_include_escapes,
3700 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3702 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol,
3703 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3704 multibyte_syntax_as_symbol = 0;
3706 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3707 open_paren_in_column_0_is_defun_start,
3708 doc: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3709 open_paren_in_column_0_is_defun_start = 1;
3712 DEFVAR_LISP ("find-word-boundary-function-table",
3713 Vfind_word_boundary_function_table,
3714 doc: /*
3715 Char table of functions to search for the word boundary.
3716 Each function is called with two arguments; POS and LIMIT.
3717 POS and LIMIT are character positions in the current buffer.
3719 If POS is less than LIMIT, POS is at the first character of a word,
3720 and the return value of a function should be a position after the
3721 last character of that word.
3723 If POS is not less than LIMIT, POS is at the last character of a word,
3724 and the return value of a function should be a position at the first
3725 character of that word.
3727 In both cases, LIMIT bounds the search. */);
3728 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3730 DEFVAR_BOOL ("comment-end-can-be-escaped", Vcomment_end_can_be_escaped,
3731 doc: /* Non-nil means an escaped ender inside a comment doesn't end the comment. */);
3732 Vcomment_end_can_be_escaped = 0;
3733 DEFSYM (Qcomment_end_can_be_escaped, "comment-end-can-be-escaped");
3734 Fmake_variable_buffer_local (Qcomment_end_can_be_escaped);
3736 defsubr (&Ssyntax_table_p);
3737 defsubr (&Ssyntax_table);
3738 defsubr (&Sstandard_syntax_table);
3739 defsubr (&Scopy_syntax_table);
3740 defsubr (&Sset_syntax_table);
3741 defsubr (&Schar_syntax);
3742 defsubr (&Smatching_paren);
3743 defsubr (&Sstring_to_syntax);
3744 defsubr (&Smodify_syntax_entry);
3745 defsubr (&Sinternal_describe_syntax_value);
3747 defsubr (&Sforward_word);
3749 defsubr (&Sskip_chars_forward);
3750 defsubr (&Sskip_chars_backward);
3751 defsubr (&Sskip_syntax_forward);
3752 defsubr (&Sskip_syntax_backward);
3754 defsubr (&Sforward_comment);
3755 defsubr (&Sscan_lists);
3756 defsubr (&Sscan_sexps);
3757 defsubr (&Sbackward_prefix_chars);
3758 defsubr (&Sparse_partial_sexp);