* lisp/emacs-lisp/package.el (package--with-work-buffer-async):
[emacs.git] / src / syntax.c
blob0d8b08c01b6d688b8fb25c5cce141376c07c4974
1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2015 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
10 (at 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 "commands.h"
27 #include "character.h"
28 #include "buffer.h"
29 #include "keymap.h"
30 #include "regex.h"
32 #include "syntax.h"
33 #include "intervals.h"
34 #include "category.h"
36 /* Make syntax table lookup grant data in gl_state. */
37 #define SYNTAX(c) syntax_property (c, 1)
38 #define SYNTAX_ENTRY(c) syntax_property_entry (c, 1)
39 #define SYNTAX_WITH_FLAGS(c) syntax_property_with_flags (c, 1)
41 /* Eight single-bit flags have the following meanings:
42 1. This character is the first of a two-character comment-start sequence.
43 2. This character is the second of a two-character comment-start sequence.
44 3. This character is the first of a two-character comment-end sequence.
45 4. This character is the second of a two-character comment-end sequence.
46 5. This character is a prefix, for backward-prefix-chars.
47 6. The char is part of a delimiter for comments of style "b".
48 7. This character is part of a nestable comment sequence.
49 8. The char is part of a delimiter for comments of style "c".
50 Note that any two-character sequence whose first character has flag 1
51 and whose second character has flag 2 will be interpreted as a comment start.
53 Bits 6 and 8 discriminate among different comment styles.
54 Languages such as C++ allow two orthogonal syntax start/end pairs
55 and bit 6 determines whether a comment-end or Scommentend
56 ends style a or b. Comment markers can start style a, b, c, or bc.
57 Style a is always the default.
58 For 2-char comment markers, the style b flag is looked up only on the second
59 char of the comment marker and on the first char of the comment ender.
60 For style c (like the nested flag), the flag can be placed on any of
61 the chars. */
63 /* These functions extract specific flags from an integer
64 that holds the syntax code and the flags. */
66 static bool
67 SYNTAX_FLAGS_COMSTART_FIRST (int flags)
69 return (flags >> 16) & 1;
71 static bool
72 SYNTAX_FLAGS_COMSTART_SECOND (int flags)
74 return (flags >> 17) & 1;
76 static bool
77 SYNTAX_FLAGS_COMEND_FIRST (int flags)
79 return (flags >> 18) & 1;
81 static bool
82 SYNTAX_FLAGS_COMEND_SECOND (int flags)
84 return (flags >> 19) & 1;
86 static bool
87 SYNTAX_FLAGS_PREFIX (int flags)
89 return (flags >> 20) & 1;
91 static bool
92 SYNTAX_FLAGS_COMMENT_STYLEB (int flags)
94 return (flags >> 21) & 1;
96 static bool
97 SYNTAX_FLAGS_COMMENT_STYLEC (int flags)
99 return (flags >> 23) & 1;
101 static int
102 SYNTAX_FLAGS_COMMENT_STYLEC2 (int flags)
104 return (flags >> 22) & 2; /* SYNTAX_FLAGS_COMMENT_STYLEC (flags) * 2 */
106 static bool
107 SYNTAX_FLAGS_COMMENT_NESTED (int flags)
109 return (flags >> 22) & 1;
112 /* FLAGS should be the flags of the main char of the comment marker, e.g.
113 the second for comstart and the first for comend. */
114 static int
115 SYNTAX_FLAGS_COMMENT_STYLE (int flags, int other_flags)
117 return (SYNTAX_FLAGS_COMMENT_STYLEB (flags)
118 | SYNTAX_FLAGS_COMMENT_STYLEC2 (flags)
119 | SYNTAX_FLAGS_COMMENT_STYLEC2 (other_flags));
122 /* Extract a particular flag for a given character. */
124 static bool
125 SYNTAX_COMEND_FIRST (int c)
127 return SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c));
130 /* We use these constants in place for comment-style and
131 string-ender-char to distinguish comments/strings started by
132 comment_fence and string_fence codes. */
134 enum
136 ST_COMMENT_STYLE = 256 + 1,
137 ST_STRING_STYLE = 256 + 2
140 /* This is the internal form of the parse state used in parse-partial-sexp. */
142 struct lisp_parse_state
144 EMACS_INT depth; /* Depth at end of parsing. */
145 int instring; /* -1 if not within string, else desired terminator. */
146 EMACS_INT incomment; /* -1 if in unnestable comment else comment nesting */
147 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
148 bool quoted; /* True if just after an escape char at end of parsing. */
149 EMACS_INT mindepth; /* Minimum depth seen while scanning. */
150 /* Char number of most recent start-of-expression at current level */
151 ptrdiff_t thislevelstart;
152 /* Char number of start of containing expression */
153 ptrdiff_t prevlevelstart;
154 ptrdiff_t location; /* Char number at which parsing stopped. */
155 ptrdiff_t location_byte; /* Corresponding byte position. */
156 ptrdiff_t comstr_start; /* Position of last comment/string starter. */
157 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
158 of levels (starting from outermost). */
161 /* These variables are a cache for finding the start of a defun.
162 find_start_pos is the place for which the defun start was found.
163 find_start_value is the defun start position found for it.
164 find_start_value_byte is the corresponding byte position.
165 find_start_buffer is the buffer it was found in.
166 find_start_begv is the BEGV value when it was found.
167 find_start_modiff is the value of MODIFF when it was found. */
169 static ptrdiff_t find_start_pos;
170 static ptrdiff_t find_start_value;
171 static ptrdiff_t find_start_value_byte;
172 static struct buffer *find_start_buffer;
173 static ptrdiff_t find_start_begv;
174 static EMACS_INT find_start_modiff;
177 static Lisp_Object skip_chars (bool, Lisp_Object, Lisp_Object, bool);
178 static Lisp_Object skip_syntaxes (bool, Lisp_Object, Lisp_Object);
179 static Lisp_Object scan_lists (EMACS_INT, EMACS_INT, EMACS_INT, bool);
180 static void scan_sexps_forward (struct lisp_parse_state *,
181 ptrdiff_t, ptrdiff_t, ptrdiff_t, EMACS_INT,
182 bool, Lisp_Object, int);
183 static bool in_classes (int, Lisp_Object);
185 /* This setter is used only in this file, so it can be private. */
186 static void
187 bset_syntax_table (struct buffer *b, Lisp_Object val)
189 b->syntax_table_ = val;
192 /* Whether the syntax of the character C has the prefix flag set. */
193 bool
194 syntax_prefix_flag_p (int c)
196 return SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c));
199 struct gl_state_s gl_state; /* Global state of syntax parser. */
201 enum { INTERVALS_AT_ONCE = 10 }; /* 1 + max-number of intervals
202 to scan to property-change. */
204 /* Set the syntax entry VAL for char C in table TABLE. */
206 static void
207 SET_RAW_SYNTAX_ENTRY (Lisp_Object table, int c, Lisp_Object val)
209 CHAR_TABLE_SET (table, c, val);
212 /* Set the syntax entry VAL for char-range RANGE in table TABLE.
213 RANGE is a cons (FROM . TO) specifying the range of characters. */
215 static void
216 SET_RAW_SYNTAX_ENTRY_RANGE (Lisp_Object table, Lisp_Object range,
217 Lisp_Object val)
219 Fset_char_table_range (table, range, val);
222 /* Extract the information from the entry for character C
223 in the current syntax table. */
225 static Lisp_Object
226 SYNTAX_MATCH (int c)
228 Lisp_Object ent = SYNTAX_ENTRY (c);
229 return CONSP (ent) ? XCDR (ent) : Qnil;
232 /* This should be called with FROM at the start of forward
233 search, or after the last position of the backward search. It
234 makes sure that the first char is picked up with correct table, so
235 one does not need to call UPDATE_SYNTAX_TABLE immediately after the
236 call.
237 Sign of COUNT gives the direction of the search.
240 static void
241 SETUP_SYNTAX_TABLE (ptrdiff_t from, ptrdiff_t count)
243 SETUP_BUFFER_SYNTAX_TABLE ();
244 gl_state.b_property = BEGV;
245 gl_state.e_property = ZV + 1;
246 gl_state.object = Qnil;
247 gl_state.offset = 0;
248 if (parse_sexp_lookup_properties)
249 if (count > 0 || from > BEGV)
250 update_syntax_table (count > 0 ? from : from - 1, count, 1, Qnil);
253 /* Same as above, but in OBJECT. If OBJECT is nil, use current buffer.
254 If it is t (which is only used in fast_c_string_match_ignore_case),
255 ignore properties altogether.
257 This is meant for regex.c to use. For buffers, regex.c passes arguments
258 to the UPDATE_SYNTAX_TABLE functions which are relative to BEGV.
259 So if it is a buffer, we set the offset field to BEGV. */
261 void
262 SETUP_SYNTAX_TABLE_FOR_OBJECT (Lisp_Object object,
263 ptrdiff_t from, ptrdiff_t count)
265 SETUP_BUFFER_SYNTAX_TABLE ();
266 gl_state.object = object;
267 if (BUFFERP (gl_state.object))
269 struct buffer *buf = XBUFFER (gl_state.object);
270 gl_state.b_property = 1;
271 gl_state.e_property = BUF_ZV (buf) - BUF_BEGV (buf) + 1;
272 gl_state.offset = BUF_BEGV (buf) - 1;
274 else if (NILP (gl_state.object))
276 gl_state.b_property = 1;
277 gl_state.e_property = ZV - BEGV + 1;
278 gl_state.offset = BEGV - 1;
280 else if (EQ (gl_state.object, Qt))
282 gl_state.b_property = 0;
283 gl_state.e_property = PTRDIFF_MAX;
284 gl_state.offset = 0;
286 else
288 gl_state.b_property = 0;
289 gl_state.e_property = 1 + SCHARS (gl_state.object);
290 gl_state.offset = 0;
292 if (parse_sexp_lookup_properties)
293 update_syntax_table (from + gl_state.offset - (count <= 0),
294 count, 1, gl_state.object);
297 /* Update gl_state to an appropriate interval which contains CHARPOS. The
298 sign of COUNT give the relative position of CHARPOS wrt the previously
299 valid interval. If INIT, only [be]_property fields of gl_state are
300 valid at start, the rest is filled basing on OBJECT.
302 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
303 direction than the intervals - or in an interval. We update the
304 current syntax-table basing on the property of this interval, and
305 update the interval to start further than CHARPOS - or be
306 NULL. We also update lim_property to be the next value of
307 charpos to call this subroutine again - or be before/after the
308 start/end of OBJECT. */
310 void
311 update_syntax_table (ptrdiff_t charpos, EMACS_INT count, bool init,
312 Lisp_Object object)
314 Lisp_Object tmp_table;
315 int cnt = 0;
316 bool invalidate = 1;
317 INTERVAL i;
319 if (init)
321 gl_state.old_prop = Qnil;
322 gl_state.start = gl_state.b_property;
323 gl_state.stop = gl_state.e_property;
324 i = interval_of (charpos, object);
325 gl_state.backward_i = gl_state.forward_i = i;
326 invalidate = 0;
327 if (!i)
328 return;
329 /* interval_of updates only ->position of the return value, so
330 update the parents manually to speed up update_interval. */
331 while (!NULL_PARENT (i))
333 if (AM_RIGHT_CHILD (i))
334 INTERVAL_PARENT (i)->position = i->position
335 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
336 - TOTAL_LENGTH (INTERVAL_PARENT (i))
337 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
338 else
339 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
340 + TOTAL_LENGTH (i);
341 i = INTERVAL_PARENT (i);
343 i = gl_state.forward_i;
344 gl_state.b_property = i->position - gl_state.offset;
345 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
346 goto update;
348 i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
350 /* We are guaranteed to be called with CHARPOS either in i,
351 or further off. */
352 if (!i)
353 error ("Error in syntax_table logic for to-the-end intervals");
354 else if (charpos < i->position) /* Move left. */
356 if (count > 0)
357 error ("Error in syntax_table logic for intervals <-");
358 /* Update the interval. */
359 i = update_interval (i, charpos);
360 if (INTERVAL_LAST_POS (i) != gl_state.b_property)
362 invalidate = 0;
363 gl_state.forward_i = i;
364 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
367 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
369 if (count < 0)
370 error ("Error in syntax_table logic for intervals ->");
371 /* Update the interval. */
372 i = update_interval (i, charpos);
373 if (i->position != gl_state.e_property)
375 invalidate = 0;
376 gl_state.backward_i = i;
377 gl_state.b_property = i->position - gl_state.offset;
381 update:
382 tmp_table = textget (i->plist, Qsyntax_table);
384 if (invalidate)
385 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
387 if (invalidate) /* Did not get to adjacent interval. */
388 { /* with the same table => */
389 /* invalidate the old range. */
390 if (count > 0)
392 gl_state.backward_i = i;
393 gl_state.b_property = i->position - gl_state.offset;
395 else
397 gl_state.forward_i = i;
398 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
402 if (!EQ (tmp_table, gl_state.old_prop))
404 gl_state.current_syntax_table = tmp_table;
405 gl_state.old_prop = tmp_table;
406 if (EQ (Fsyntax_table_p (tmp_table), Qt))
408 gl_state.use_global = 0;
410 else if (CONSP (tmp_table))
412 gl_state.use_global = 1;
413 gl_state.global_code = tmp_table;
415 else
417 gl_state.use_global = 0;
418 gl_state.current_syntax_table = BVAR (current_buffer, syntax_table);
422 while (i)
424 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
426 if (count > 0)
428 gl_state.e_property = i->position - gl_state.offset;
429 gl_state.forward_i = i;
431 else
433 gl_state.b_property
434 = i->position + LENGTH (i) - gl_state.offset;
435 gl_state.backward_i = i;
437 return;
439 else if (cnt == INTERVALS_AT_ONCE)
441 if (count > 0)
443 gl_state.e_property
444 = i->position + LENGTH (i) - gl_state.offset
445 /* e_property at EOB is not set to ZV but to ZV+1, so that
446 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
447 having to check eob between the two. */
448 + (next_interval (i) ? 0 : 1);
449 gl_state.forward_i = i;
451 else
453 gl_state.b_property = i->position - gl_state.offset;
454 gl_state.backward_i = i;
456 return;
458 cnt++;
459 i = count > 0 ? next_interval (i) : previous_interval (i);
461 eassert (i == NULL); /* This property goes to the end. */
462 if (count > 0)
463 gl_state.e_property = gl_state.stop;
464 else
465 gl_state.b_property = gl_state.start;
468 /* Returns true if char at CHARPOS is quoted.
469 Global syntax-table data should be set up already to be good at CHARPOS
470 or after. On return global syntax data is good for lookup at CHARPOS. */
472 static bool
473 char_quoted (ptrdiff_t charpos, ptrdiff_t bytepos)
475 enum syntaxcode code;
476 ptrdiff_t beg = BEGV;
477 bool quoted = 0;
478 ptrdiff_t orig = charpos;
480 while (charpos > beg)
482 int c;
483 DEC_BOTH (charpos, bytepos);
485 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
486 c = FETCH_CHAR_AS_MULTIBYTE (bytepos);
487 code = SYNTAX (c);
488 if (! (code == Scharquote || code == Sescape))
489 break;
491 quoted = !quoted;
494 UPDATE_SYNTAX_TABLE (orig);
495 return quoted;
498 /* Return the bytepos one character before BYTEPOS.
499 We assume that BYTEPOS is not at the start of the buffer. */
501 static ptrdiff_t
502 dec_bytepos (ptrdiff_t bytepos)
504 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
505 return bytepos - 1;
507 DEC_POS (bytepos);
508 return bytepos;
511 /* Return a defun-start position before POS and not too far before.
512 It should be the last one before POS, or nearly the last.
514 When open_paren_in_column_0_is_defun_start is nonzero,
515 only the beginning of the buffer is treated as a defun-start.
517 We record the information about where the scan started
518 and what its result was, so that another call in the same area
519 can return the same value very quickly.
521 There is no promise at which position the global syntax data is
522 valid on return from the subroutine, so the caller should explicitly
523 update the global data. */
525 static ptrdiff_t
526 find_defun_start (ptrdiff_t pos, ptrdiff_t pos_byte)
528 ptrdiff_t opoint = PT, opoint_byte = PT_BYTE;
530 /* Use previous finding, if it's valid and applies to this inquiry. */
531 if (current_buffer == find_start_buffer
532 /* Reuse the defun-start even if POS is a little farther on.
533 POS might be in the next defun, but that's ok.
534 Our value may not be the best possible, but will still be usable. */
535 && pos <= find_start_pos + 1000
536 && pos >= find_start_value
537 && BEGV == find_start_begv
538 && MODIFF == find_start_modiff)
539 return find_start_value;
541 if (!open_paren_in_column_0_is_defun_start)
543 find_start_value = BEGV;
544 find_start_value_byte = BEGV_BYTE;
545 goto found;
548 /* Back up to start of line. */
549 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
551 /* We optimize syntax-table lookup for rare updates. Thus we accept
552 only those `^\s(' which are good in global _and_ text-property
553 syntax-tables. */
554 SETUP_BUFFER_SYNTAX_TABLE ();
555 while (PT > BEGV)
557 int c;
559 /* Open-paren at start of line means we may have found our
560 defun-start. */
561 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
562 if (SYNTAX (c) == Sopen)
564 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
565 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
566 if (SYNTAX (c) == Sopen)
567 break;
568 /* Now fallback to the default value. */
569 SETUP_BUFFER_SYNTAX_TABLE ();
571 /* Move to beg of previous line. */
572 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
575 /* Record what we found, for the next try. */
576 find_start_value = PT;
577 find_start_value_byte = PT_BYTE;
578 TEMP_SET_PT_BOTH (opoint, opoint_byte);
580 found:
581 find_start_buffer = current_buffer;
582 find_start_modiff = MODIFF;
583 find_start_begv = BEGV;
584 find_start_pos = pos;
586 return find_start_value;
589 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
591 static bool
592 prev_char_comend_first (ptrdiff_t pos, ptrdiff_t pos_byte)
594 int c;
595 bool val;
597 DEC_BOTH (pos, pos_byte);
598 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
599 c = FETCH_CHAR (pos_byte);
600 val = SYNTAX_COMEND_FIRST (c);
601 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
602 return val;
605 /* Check whether charpos FROM is at the end of a comment.
606 FROM_BYTE is the bytepos corresponding to FROM.
607 Do not move back before STOP.
609 Return true if we find a comment ending at FROM/FROM_BYTE.
611 If successful, store the charpos of the comment's beginning
612 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
614 Global syntax data remains valid for backward search starting at
615 the returned value (or at FROM, if the search was not successful). */
617 static bool
618 back_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop,
619 bool comnested, int comstyle, ptrdiff_t *charpos_ptr,
620 ptrdiff_t *bytepos_ptr)
622 /* Look back, counting the parity of string-quotes,
623 and recording the comment-starters seen.
624 When we reach a safe place, assume that's not in a string;
625 then step the main scan to the earliest comment-starter seen
626 an even number of string quotes away from the safe place.
628 OFROM[I] is position of the earliest comment-starter seen
629 which is I+2X quotes from the comment-end.
630 PARITY is current parity of quotes from the comment end. */
631 int string_style = -1; /* Presumed outside of any string. */
632 bool string_lossage = 0;
633 /* Not a real lossage: indicates that we have passed a matching comment
634 starter plus a non-matching comment-ender, meaning that any matching
635 comment-starter we might see later could be a false positive (hidden
636 inside another comment).
637 Test case: { a (* b } c (* d *) */
638 bool comment_lossage = 0;
639 ptrdiff_t comment_end = from;
640 ptrdiff_t comment_end_byte = from_byte;
641 ptrdiff_t comstart_pos = 0;
642 ptrdiff_t comstart_byte IF_LINT (= 0);
643 /* Place where the containing defun starts,
644 or 0 if we didn't come across it yet. */
645 ptrdiff_t defun_start = 0;
646 ptrdiff_t defun_start_byte = 0;
647 enum syntaxcode code;
648 ptrdiff_t nesting = 1; /* current comment nesting */
649 int c;
650 int syntax = 0;
652 /* FIXME: A }} comment-ender style leads to incorrect behavior
653 in the case of {{ c }}} because we ignore the last two chars which are
654 assumed to be comment-enders although they aren't. */
656 /* At beginning of range to scan, we're outside of strings;
657 that determines quote parity to the comment-end. */
658 while (from != stop)
660 ptrdiff_t temp_byte;
661 int prev_syntax;
662 bool com2start, com2end, comstart;
664 /* Move back and examine a character. */
665 DEC_BOTH (from, from_byte);
666 UPDATE_SYNTAX_TABLE_BACKWARD (from);
668 prev_syntax = syntax;
669 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
670 syntax = SYNTAX_WITH_FLAGS (c);
671 code = SYNTAX (c);
673 /* Check for 2-char comment markers. */
674 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
675 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
676 && (comstyle
677 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax, syntax))
678 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
679 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
680 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
681 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
682 comstart = (com2start || code == Scomment);
684 /* Nasty cases with overlapping 2-char comment markers:
685 - snmp-mode: -- c -- foo -- c --
686 --- c --
687 ------ c --
688 - c-mode: *||*
689 |* *|* *|
690 |*| |* |*|
691 /// */
693 /* If a 2-char comment sequence partly overlaps with another,
694 we don't try to be clever. E.g. |*| in C, or }% in modes that
695 have %..\n and %{..}%. */
696 if (from > stop && (com2end || comstart))
698 ptrdiff_t next = from, next_byte = from_byte;
699 int next_c, next_syntax;
700 DEC_BOTH (next, next_byte);
701 UPDATE_SYNTAX_TABLE_BACKWARD (next);
702 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
703 next_syntax = SYNTAX_WITH_FLAGS (next_c);
704 if (((comstart || comnested)
705 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
706 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
707 || ((com2end || comnested)
708 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
709 && (comstyle
710 == SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_syntax))
711 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
712 goto lossage;
713 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
716 if (com2start && comstart_pos == 0)
717 /* We're looking at a comment starter. But it might be a comment
718 ender as well (see snmp-mode). The first time we see one, we
719 need to consider it as a comment starter,
720 and the subsequent times as a comment ender. */
721 com2end = 0;
723 /* Turn a 2-char comment sequences into the appropriate syntax. */
724 if (com2end)
725 code = Sendcomment;
726 else if (com2start)
727 code = Scomment;
728 /* Ignore comment starters of a different style. */
729 else if (code == Scomment
730 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0)
731 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
732 continue;
734 /* Ignore escaped characters, except comment-enders. */
735 if (code != Sendcomment && char_quoted (from, from_byte))
736 continue;
738 switch (code)
740 case Sstring_fence:
741 case Scomment_fence:
742 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
743 case Sstring:
744 /* Track parity of quotes. */
745 if (string_style == -1)
746 /* Entering a string. */
747 string_style = c;
748 else if (string_style == c)
749 /* Leaving the string. */
750 string_style = -1;
751 else
752 /* If we have two kinds of string delimiters.
753 There's no way to grok this scanning backwards. */
754 string_lossage = 1;
755 break;
757 case Scomment:
758 /* We've already checked that it is the relevant comstyle. */
759 if (string_style != -1 || comment_lossage || string_lossage)
760 /* There are odd string quotes involved, so let's be careful.
761 Test case in Pascal: " { " a { " } */
762 goto lossage;
764 if (!comnested)
766 /* Record best comment-starter so far. */
767 comstart_pos = from;
768 comstart_byte = from_byte;
770 else if (--nesting <= 0)
771 /* nested comments have to be balanced, so we don't need to
772 keep looking for earlier ones. We use here the same (slightly
773 incorrect) reasoning as below: since it is followed by uniform
774 paired string quotes, this comment-start has to be outside of
775 strings, else the comment-end itself would be inside a string. */
776 goto done;
777 break;
779 case Sendcomment:
780 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == comstyle
781 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
782 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
783 /* This is the same style of comment ender as ours. */
785 if (comnested)
786 nesting++;
787 else
788 /* Anything before that can't count because it would match
789 this comment-ender rather than ours. */
790 from = stop; /* Break out of the loop. */
792 else if (comstart_pos != 0 || c != '\n')
793 /* We're mixing comment styles here, so we'd better be careful.
794 The (comstart_pos != 0 || c != '\n') check is not quite correct
795 (we should just always set comment_lossage), but removing it
796 would imply that any multiline comment in C would go through
797 lossage, which seems overkill.
798 The failure should only happen in the rare cases such as
799 { (* } *) */
800 comment_lossage = 1;
801 break;
803 case Sopen:
804 /* Assume a defun-start point is outside of strings. */
805 if (open_paren_in_column_0_is_defun_start
806 && (from == stop
807 || (temp_byte = dec_bytepos (from_byte),
808 FETCH_CHAR (temp_byte) == '\n')))
810 defun_start = from;
811 defun_start_byte = from_byte;
812 from = stop; /* Break out of the loop. */
814 break;
816 default:
817 break;
821 if (comstart_pos == 0)
823 from = comment_end;
824 from_byte = comment_end_byte;
825 UPDATE_SYNTAX_TABLE_FORWARD (comment_end);
827 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
828 or `done'), then we've found the beginning of the non-nested comment. */
829 else if (1) /* !comnested */
831 from = comstart_pos;
832 from_byte = comstart_byte;
833 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
835 else lossage:
837 struct lisp_parse_state state;
838 bool adjusted = true;
839 /* We had two kinds of string delimiters mixed up
840 together. Decode this going forwards.
841 Scan fwd from a known safe place (beginning-of-defun)
842 to the one in question; this records where we
843 last passed a comment starter. */
844 /* If we did not already find the defun start, find it now. */
845 if (defun_start == 0)
847 defun_start = find_defun_start (comment_end, comment_end_byte);
848 defun_start_byte = find_start_value_byte;
849 adjusted = (defun_start > BEGV);
853 scan_sexps_forward (&state,
854 defun_start, defun_start_byte,
855 comment_end, TYPE_MINIMUM (EMACS_INT),
856 0, Qnil, 0);
857 defun_start = comment_end;
858 if (!adjusted)
860 adjusted = true;
861 find_start_value
862 = CONSP (state.levelstarts) ? XINT (XCAR (state.levelstarts))
863 : state.thislevelstart >= 0 ? state.thislevelstart
864 : find_start_value;
865 find_start_value_byte = CHAR_TO_BYTE (find_start_value);
868 if (state.incomment == (comnested ? 1 : -1)
869 && state.comstyle == comstyle)
870 from = state.comstr_start;
871 else
873 from = comment_end;
874 if (state.incomment)
875 /* If comment_end is inside some other comment, maybe ours
876 is nested, so we need to try again from within the
877 surrounding comment. Example: { a (* " *) */
879 /* FIXME: We should advance by one or two chars. */
880 defun_start = state.comstr_start + 2;
881 defun_start_byte = CHAR_TO_BYTE (defun_start);
884 } while (defun_start < comment_end);
886 from_byte = CHAR_TO_BYTE (from);
887 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
890 done:
891 *charpos_ptr = from;
892 *bytepos_ptr = from_byte;
894 return from != comment_end;
897 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
898 doc: /* Return t if OBJECT is a syntax table.
899 Currently, any char-table counts as a syntax table. */)
900 (Lisp_Object object)
902 if (CHAR_TABLE_P (object)
903 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
904 return Qt;
905 return Qnil;
908 static void
909 check_syntax_table (Lisp_Object obj)
911 CHECK_TYPE (CHAR_TABLE_P (obj) && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table),
912 Qsyntax_table_p, obj);
915 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
916 doc: /* Return the current syntax table.
917 This is the one specified by the current buffer. */)
918 (void)
920 return BVAR (current_buffer, syntax_table);
923 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
924 Sstandard_syntax_table, 0, 0, 0,
925 doc: /* Return the standard syntax table.
926 This is the one used for new buffers. */)
927 (void)
929 return Vstandard_syntax_table;
932 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
933 doc: /* Construct a new syntax table and return it.
934 It is a copy of the TABLE, which defaults to the standard syntax table. */)
935 (Lisp_Object table)
937 Lisp_Object copy;
939 if (!NILP (table))
940 check_syntax_table (table);
941 else
942 table = Vstandard_syntax_table;
944 copy = Fcopy_sequence (table);
946 /* Only the standard syntax table should have a default element.
947 Other syntax tables should inherit from parents instead. */
948 set_char_table_defalt (copy, Qnil);
950 /* Copied syntax tables should all have parents.
951 If we copied one with no parent, such as the standard syntax table,
952 use the standard syntax table as the copy's parent. */
953 if (NILP (XCHAR_TABLE (copy)->parent))
954 Fset_char_table_parent (copy, Vstandard_syntax_table);
955 return copy;
958 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
959 doc: /* Select a new syntax table for the current buffer.
960 One argument, a syntax table. */)
961 (Lisp_Object table)
963 int idx;
964 check_syntax_table (table);
965 bset_syntax_table (current_buffer, table);
966 /* Indicate that this buffer now has a specified syntax table. */
967 idx = PER_BUFFER_VAR_IDX (syntax_table);
968 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
969 return table;
972 /* Convert a letter which signifies a syntax code
973 into the code it signifies.
974 This is used by modify-syntax-entry, and other things. */
976 unsigned char const syntax_spec_code[0400] =
977 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
978 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
979 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
980 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
981 Swhitespace, Scomment_fence, Sstring, 0377, Smath, 0377, 0377, Squote,
982 Sopen, Sclose, 0377, 0377, 0377, Swhitespace, Spunct, Scharquote,
983 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
984 0377, 0377, 0377, 0377, Scomment, 0377, Sendcomment, 0377,
985 Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
986 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
987 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword,
988 0377, 0377, 0377, 0377, Sescape, 0377, 0377, Ssymbol,
989 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
990 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
991 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword,
992 0377, 0377, 0377, 0377, Sstring_fence, 0377, 0377, 0377
995 /* Indexed by syntax code, give the letter that describes it. */
997 char const syntax_code_spec[16] =
999 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
1000 '!', '|'
1003 /* Indexed by syntax code, give the object (cons of syntax code and
1004 nil) to be stored in syntax table. Since these objects can be
1005 shared among syntax tables, we generate them in advance. By
1006 sharing objects, the function `describe-syntax' can give a more
1007 compact listing. */
1008 static Lisp_Object Vsyntax_code_object;
1011 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
1012 doc: /* Return the syntax code of CHARACTER, described by a character.
1013 For example, if CHARACTER is a word constituent, the
1014 character `w' (119) is returned.
1015 The characters that correspond to various syntax codes
1016 are listed in the documentation of `modify-syntax-entry'. */)
1017 (Lisp_Object character)
1019 int char_int;
1020 CHECK_CHARACTER (character);
1021 char_int = XINT (character);
1022 SETUP_BUFFER_SYNTAX_TABLE ();
1023 return make_number (syntax_code_spec[SYNTAX (char_int)]);
1026 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
1027 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
1028 (Lisp_Object character)
1030 int char_int;
1031 enum syntaxcode code;
1032 CHECK_CHARACTER (character);
1033 char_int = XINT (character);
1034 SETUP_BUFFER_SYNTAX_TABLE ();
1035 code = SYNTAX (char_int);
1036 if (code == Sopen || code == Sclose)
1037 return SYNTAX_MATCH (char_int);
1038 return Qnil;
1041 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
1042 doc: /* Convert a syntax descriptor STRING into a raw syntax descriptor.
1043 STRING should be a string of the form allowed as argument of
1044 `modify-syntax-entry'. The return value is a raw syntax descriptor: a
1045 cons cell \(CODE . MATCHING-CHAR) which can be used, for example, as
1046 the value of a `syntax-table' text property. */)
1047 (Lisp_Object string)
1049 const unsigned char *p;
1050 int val;
1051 Lisp_Object match;
1053 CHECK_STRING (string);
1055 p = SDATA (string);
1056 val = syntax_spec_code[*p++];
1057 if (val == 0377)
1058 error ("Invalid syntax description letter: %c", p[-1]);
1060 if (val == Sinherit)
1061 return Qnil;
1063 if (*p)
1065 int len;
1066 int character = STRING_CHAR_AND_LENGTH (p, len);
1067 XSETINT (match, character);
1068 if (XFASTINT (match) == ' ')
1069 match = Qnil;
1070 p += len;
1072 else
1073 match = Qnil;
1075 while (*p)
1076 switch (*p++)
1078 case '1':
1079 val |= 1 << 16;
1080 break;
1082 case '2':
1083 val |= 1 << 17;
1084 break;
1086 case '3':
1087 val |= 1 << 18;
1088 break;
1090 case '4':
1091 val |= 1 << 19;
1092 break;
1094 case 'p':
1095 val |= 1 << 20;
1096 break;
1098 case 'b':
1099 val |= 1 << 21;
1100 break;
1102 case 'n':
1103 val |= 1 << 22;
1104 break;
1106 case 'c':
1107 val |= 1 << 23;
1108 break;
1111 if (val < ASIZE (Vsyntax_code_object) && NILP (match))
1112 return AREF (Vsyntax_code_object, val);
1113 else
1114 /* Since we can't use a shared object, let's make a new one. */
1115 return Fcons (make_number (val), match);
1118 /* I really don't know why this is interactive
1119 help-form should at least be made useful whilst reading the second arg. */
1120 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
1121 "cSet syntax for character: \nsSet syntax for %s to: ",
1122 doc: /* Set syntax for character CHAR according to string NEWENTRY.
1123 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1124 the current buffer's syntax table.
1125 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1126 in the range MIN to MAX are changed.
1127 The first character of NEWENTRY should be one of the following:
1128 Space or - whitespace syntax. w word constituent.
1129 _ symbol constituent. . punctuation.
1130 ( open-parenthesis. ) close-parenthesis.
1131 " string quote. \\ escape.
1132 $ paired delimiter. ' expression quote or prefix operator.
1133 < comment starter. > comment ender.
1134 / character-quote. @ inherit from parent table.
1135 | generic string fence. ! generic comment fence.
1137 Only single-character comment start and end sequences are represented thus.
1138 Two-character sequences are represented as described below.
1139 The second character of NEWENTRY is the matching parenthesis,
1140 used only if the first character is `(' or `)'.
1141 Any additional characters are flags.
1142 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1143 1 means CHAR is the start of a two-char comment start sequence.
1144 2 means CHAR is the second character of such a sequence.
1145 3 means CHAR is the start of a two-char comment end sequence.
1146 4 means CHAR is the second character of such a sequence.
1148 There can be several orthogonal comment sequences. This is to support
1149 language modes such as C++. By default, all comment sequences are of style
1150 a, but you can set the comment sequence style to b (on the second character
1151 of a comment-start, and the first character of a comment-end sequence) and/or
1152 c (on any of its chars) using this flag:
1153 b means CHAR is part of comment sequence b.
1154 c means CHAR is part of comment sequence c.
1155 n means CHAR is part of a nestable comment sequence.
1157 p means CHAR is a prefix character for `backward-prefix-chars';
1158 such characters are treated as whitespace when they occur
1159 between expressions.
1160 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1161 (Lisp_Object c, Lisp_Object newentry, Lisp_Object syntax_table)
1163 if (CONSP (c))
1165 CHECK_CHARACTER_CAR (c);
1166 CHECK_CHARACTER_CDR (c);
1168 else
1169 CHECK_CHARACTER (c);
1171 if (NILP (syntax_table))
1172 syntax_table = BVAR (current_buffer, syntax_table);
1173 else
1174 check_syntax_table (syntax_table);
1176 newentry = Fstring_to_syntax (newentry);
1177 if (CONSP (c))
1178 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1179 else
1180 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
1182 /* We clear the regexp cache, since character classes can now have
1183 different values from those in the compiled regexps.*/
1184 clear_regexp_cache ();
1186 return Qnil;
1189 /* Dump syntax table to buffer in human-readable format */
1191 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1192 Sinternal_describe_syntax_value, 1, 1, 0,
1193 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1194 (Lisp_Object syntax)
1196 int code, syntax_code;
1197 bool start1, start2, end1, end2, prefix, comstyleb, comstylec, comnested;
1198 char str[2];
1199 Lisp_Object first, match_lisp, value = syntax;
1201 if (NILP (value))
1203 insert_string ("default");
1204 return syntax;
1207 if (CHAR_TABLE_P (value))
1209 insert_string ("deeper char-table ...");
1210 return syntax;
1213 if (!CONSP (value))
1215 insert_string ("invalid");
1216 return syntax;
1219 first = XCAR (value);
1220 match_lisp = XCDR (value);
1222 if (!INTEGERP (first) || !(NILP (match_lisp) || CHARACTERP (match_lisp)))
1224 insert_string ("invalid");
1225 return syntax;
1228 syntax_code = XINT (first) & INT_MAX;
1229 code = syntax_code & 0377;
1230 start1 = SYNTAX_FLAGS_COMSTART_FIRST (syntax_code);
1231 start2 = SYNTAX_FLAGS_COMSTART_SECOND (syntax_code);
1232 end1 = SYNTAX_FLAGS_COMEND_FIRST (syntax_code);
1233 end2 = SYNTAX_FLAGS_COMEND_SECOND (syntax_code);
1234 prefix = SYNTAX_FLAGS_PREFIX (syntax_code);
1235 comstyleb = SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code);
1236 comstylec = SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code);
1237 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax_code);
1239 if (Smax <= code)
1241 insert_string ("invalid");
1242 return syntax;
1245 str[0] = syntax_code_spec[code], str[1] = 0;
1246 insert (str, 1);
1248 if (NILP (match_lisp))
1249 insert (" ", 1);
1250 else
1251 insert_char (XINT (match_lisp));
1253 if (start1)
1254 insert ("1", 1);
1255 if (start2)
1256 insert ("2", 1);
1258 if (end1)
1259 insert ("3", 1);
1260 if (end2)
1261 insert ("4", 1);
1263 if (prefix)
1264 insert ("p", 1);
1265 if (comstyleb)
1266 insert ("b", 1);
1267 if (comstylec)
1268 insert ("c", 1);
1269 if (comnested)
1270 insert ("n", 1);
1272 insert_string ("\twhich means: ");
1274 switch (code)
1276 case Swhitespace:
1277 insert_string ("whitespace"); break;
1278 case Spunct:
1279 insert_string ("punctuation"); break;
1280 case Sword:
1281 insert_string ("word"); break;
1282 case Ssymbol:
1283 insert_string ("symbol"); break;
1284 case Sopen:
1285 insert_string ("open"); break;
1286 case Sclose:
1287 insert_string ("close"); break;
1288 case Squote:
1289 insert_string ("prefix"); break;
1290 case Sstring:
1291 insert_string ("string"); break;
1292 case Smath:
1293 insert_string ("math"); break;
1294 case Sescape:
1295 insert_string ("escape"); break;
1296 case Scharquote:
1297 insert_string ("charquote"); break;
1298 case Scomment:
1299 insert_string ("comment"); break;
1300 case Sendcomment:
1301 insert_string ("endcomment"); break;
1302 case Sinherit:
1303 insert_string ("inherit"); break;
1304 case Scomment_fence:
1305 insert_string ("comment fence"); break;
1306 case Sstring_fence:
1307 insert_string ("string fence"); break;
1308 default:
1309 insert_string ("invalid");
1310 return syntax;
1313 if (!NILP (match_lisp))
1315 insert_string (", matches ");
1316 insert_char (XINT (match_lisp));
1319 if (start1)
1320 insert_string (",\n\t is the first character of a comment-start sequence");
1321 if (start2)
1322 insert_string (",\n\t is the second character of a comment-start sequence");
1324 if (end1)
1325 insert_string (",\n\t is the first character of a comment-end sequence");
1326 if (end2)
1327 insert_string (",\n\t is the second character of a comment-end sequence");
1328 if (comstyleb)
1329 insert_string (" (comment style b)");
1330 if (comstylec)
1331 insert_string (" (comment style c)");
1332 if (comnested)
1333 insert_string (" (nestable)");
1335 if (prefix)
1336 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1338 return syntax;
1341 /* Return the position across COUNT words from FROM.
1342 If that many words cannot be found before the end of the buffer, return 0.
1343 COUNT negative means scan backward and stop at word beginning. */
1345 ptrdiff_t
1346 scan_words (register ptrdiff_t from, register EMACS_INT count)
1348 register ptrdiff_t beg = BEGV;
1349 register ptrdiff_t end = ZV;
1350 register ptrdiff_t from_byte = CHAR_TO_BYTE (from);
1351 register enum syntaxcode code;
1352 int ch0, ch1;
1353 Lisp_Object func, pos;
1355 immediate_quit = 1;
1356 QUIT;
1358 SETUP_SYNTAX_TABLE (from, count);
1360 while (count > 0)
1362 while (1)
1364 if (from == end)
1366 immediate_quit = 0;
1367 return 0;
1369 UPDATE_SYNTAX_TABLE_FORWARD (from);
1370 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1371 code = SYNTAX (ch0);
1372 INC_BOTH (from, from_byte);
1373 if (words_include_escapes
1374 && (code == Sescape || code == Scharquote))
1375 break;
1376 if (code == Sword)
1377 break;
1379 /* Now CH0 is a character which begins a word and FROM is the
1380 position of the next character. */
1381 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
1382 if (! NILP (Ffboundp (func)))
1384 pos = call2 (func, make_number (from - 1), make_number (end));
1385 if (INTEGERP (pos) && from < XINT (pos) && XINT (pos) <= ZV)
1387 from = XINT (pos);
1388 from_byte = CHAR_TO_BYTE (from);
1391 else
1393 while (1)
1395 if (from == end) break;
1396 UPDATE_SYNTAX_TABLE_FORWARD (from);
1397 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1398 code = SYNTAX (ch1);
1399 if ((code != Sword
1400 && (! words_include_escapes
1401 || (code != Sescape && code != Scharquote)))
1402 || word_boundary_p (ch0, ch1))
1403 break;
1404 INC_BOTH (from, from_byte);
1405 ch0 = ch1;
1408 count--;
1410 while (count < 0)
1412 while (1)
1414 if (from == beg)
1416 immediate_quit = 0;
1417 return 0;
1419 DEC_BOTH (from, from_byte);
1420 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1421 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1422 code = SYNTAX (ch1);
1423 if (words_include_escapes
1424 && (code == Sescape || code == Scharquote))
1425 break;
1426 if (code == Sword)
1427 break;
1429 /* Now CH1 is a character which ends a word and FROM is the
1430 position of it. */
1431 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
1432 if (! NILP (Ffboundp (func)))
1434 pos = call2 (func, make_number (from), make_number (beg));
1435 if (INTEGERP (pos) && BEGV <= XINT (pos) && XINT (pos) < from)
1437 from = XINT (pos);
1438 from_byte = CHAR_TO_BYTE (from);
1441 else
1443 while (1)
1445 if (from == beg)
1446 break;
1447 DEC_BOTH (from, from_byte);
1448 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1449 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1450 code = SYNTAX (ch0);
1451 if ((code != Sword
1452 && (! words_include_escapes
1453 || (code != Sescape && code != Scharquote)))
1454 || word_boundary_p (ch0, ch1))
1456 INC_BOTH (from, from_byte);
1457 break;
1459 ch1 = ch0;
1462 count++;
1465 immediate_quit = 0;
1467 return from;
1470 DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p",
1471 doc: /* Move point forward ARG words (backward if ARG is negative).
1472 If ARG is omitted or nil, move point forward one word.
1473 Normally returns t.
1474 If an edge of the buffer or a field boundary is reached, point is left there
1475 and the function returns nil. Field boundaries are not noticed if
1476 `inhibit-field-text-motion' is non-nil. */)
1477 (Lisp_Object arg)
1479 Lisp_Object tmp;
1480 ptrdiff_t orig_val, val;
1482 if (NILP (arg))
1483 XSETFASTINT (arg, 1);
1484 else
1485 CHECK_NUMBER (arg);
1487 val = orig_val = scan_words (PT, XINT (arg));
1488 if (! orig_val)
1489 val = XINT (arg) > 0 ? ZV : BEGV;
1491 /* Avoid jumping out of an input field. */
1492 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1493 Qnil, Qnil, Qnil);
1494 val = XFASTINT (tmp);
1496 SET_PT (val);
1497 return val == orig_val ? Qt : Qnil;
1500 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1501 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1502 STRING is like the inside of a `[...]' in a regular expression
1503 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1504 (but not at the end of a range; quoting is never needed there).
1505 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1506 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1507 Char classes, e.g. `[:alpha:]', are supported.
1509 Returns the distance traveled, either zero or positive. */)
1510 (Lisp_Object string, Lisp_Object lim)
1512 return skip_chars (1, string, lim, 1);
1515 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1516 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1517 See `skip-chars-forward' for details.
1518 Returns the distance traveled, either zero or negative. */)
1519 (Lisp_Object string, Lisp_Object lim)
1521 return skip_chars (0, string, lim, 1);
1524 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1525 doc: /* Move point forward across chars in specified syntax classes.
1526 SYNTAX is a string of syntax code characters.
1527 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1528 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1529 This function returns the distance traveled, either zero or positive. */)
1530 (Lisp_Object syntax, Lisp_Object lim)
1532 return skip_syntaxes (1, syntax, lim);
1535 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1536 doc: /* Move point backward across chars in specified syntax classes.
1537 SYNTAX is a string of syntax code characters.
1538 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1539 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1540 This function returns either zero or a negative number, and the absolute value
1541 of this is the distance traveled. */)
1542 (Lisp_Object syntax, Lisp_Object lim)
1544 return skip_syntaxes (0, syntax, lim);
1547 static Lisp_Object
1548 skip_chars (bool forwardp, Lisp_Object string, Lisp_Object lim,
1549 bool handle_iso_classes)
1551 int c;
1552 char fastmap[0400];
1553 /* Store the ranges of non-ASCII characters. */
1554 int *char_ranges IF_LINT (= NULL);
1555 int n_char_ranges = 0;
1556 bool negate = 0;
1557 ptrdiff_t i, i_byte;
1558 /* True if the current buffer is multibyte and the region contains
1559 non-ASCII chars. */
1560 bool multibyte;
1561 /* True if STRING is multibyte and it contains non-ASCII chars. */
1562 bool string_multibyte;
1563 ptrdiff_t size_byte;
1564 const unsigned char *str;
1565 int len;
1566 Lisp_Object iso_classes;
1567 USE_SAFE_ALLOCA;
1569 CHECK_STRING (string);
1570 iso_classes = Qnil;
1572 if (NILP (lim))
1573 XSETINT (lim, forwardp ? ZV : BEGV);
1574 else
1575 CHECK_NUMBER_COERCE_MARKER (lim);
1577 /* In any case, don't allow scan outside bounds of buffer. */
1578 if (XINT (lim) > ZV)
1579 XSETFASTINT (lim, ZV);
1580 if (XINT (lim) < BEGV)
1581 XSETFASTINT (lim, BEGV);
1583 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1584 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1585 string_multibyte = SBYTES (string) > SCHARS (string);
1587 memset (fastmap, 0, sizeof fastmap);
1589 str = SDATA (string);
1590 size_byte = SBYTES (string);
1592 i_byte = 0;
1593 if (i_byte < size_byte
1594 && SREF (string, 0) == '^')
1596 negate = 1; i_byte++;
1599 /* Find the characters specified and set their elements of fastmap.
1600 Handle backslashes and ranges specially.
1602 If STRING contains non-ASCII characters, setup char_ranges for
1603 them and use fastmap only for their leading codes. */
1605 if (! string_multibyte)
1607 bool string_has_eight_bit = 0;
1609 /* At first setup fastmap. */
1610 while (i_byte < size_byte)
1612 c = str[i_byte++];
1614 if (handle_iso_classes && c == '['
1615 && i_byte < size_byte
1616 && str[i_byte] == ':')
1618 const unsigned char *class_beg = str + i_byte + 1;
1619 const unsigned char *class_end = class_beg;
1620 const unsigned char *class_limit = str + size_byte - 2;
1621 /* Leave room for the null. */
1622 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1623 re_wctype_t cc;
1625 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1626 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1628 while (class_end < class_limit
1629 && *class_end >= 'a' && *class_end <= 'z')
1630 class_end++;
1632 if (class_end == class_beg
1633 || *class_end != ':' || class_end[1] != ']')
1634 goto not_a_class_name;
1636 memcpy (class_name, class_beg, class_end - class_beg);
1637 class_name[class_end - class_beg] = 0;
1639 cc = re_wctype (class_name);
1640 if (cc == 0)
1641 error ("Invalid ISO C character class");
1643 iso_classes = Fcons (make_number (cc), iso_classes);
1645 i_byte = class_end + 2 - str;
1646 continue;
1649 not_a_class_name:
1650 if (c == '\\')
1652 if (i_byte == size_byte)
1653 break;
1655 c = str[i_byte++];
1657 /* Treat `-' as range character only if another character
1658 follows. */
1659 if (i_byte + 1 < size_byte
1660 && str[i_byte] == '-')
1662 int c2;
1664 /* Skip over the dash. */
1665 i_byte++;
1667 /* Get the end of the range. */
1668 c2 = str[i_byte++];
1669 if (c2 == '\\'
1670 && i_byte < size_byte)
1671 c2 = str[i_byte++];
1673 if (c <= c2)
1675 int lim2 = c2 + 1;
1676 while (c < lim2)
1677 fastmap[c++] = 1;
1678 if (! ASCII_CHAR_P (c2))
1679 string_has_eight_bit = 1;
1682 else
1684 fastmap[c] = 1;
1685 if (! ASCII_CHAR_P (c))
1686 string_has_eight_bit = 1;
1690 /* If the current range is multibyte and STRING contains
1691 eight-bit chars, arrange fastmap and setup char_ranges for
1692 the corresponding multibyte chars. */
1693 if (multibyte && string_has_eight_bit)
1695 char *p1;
1696 char himap[0200 + 1];
1697 memcpy (himap, fastmap + 0200, 0200);
1698 himap[0200] = 0;
1699 memset (fastmap + 0200, 0, 0200);
1700 SAFE_NALLOCA (char_ranges, 2, 128);
1701 i = 0;
1703 while ((p1 = memchr (himap + i, 1, 0200 - i)))
1705 /* Deduce the next range C..C2 from the next clump of 1s
1706 in HIMAP starting with &HIMAP[I]. HIMAP is the high
1707 order half of the old FASTMAP. */
1708 int c2, leading_code;
1709 i = p1 - himap;
1710 c = BYTE8_TO_CHAR (i + 0200);
1711 i += strlen (p1);
1712 c2 = BYTE8_TO_CHAR (i + 0200 - 1);
1714 char_ranges[n_char_ranges++] = c;
1715 char_ranges[n_char_ranges++] = c2;
1716 leading_code = CHAR_LEADING_CODE (c);
1717 memset (fastmap + leading_code, 1,
1718 CHAR_LEADING_CODE (c2) - leading_code + 1);
1722 else /* STRING is multibyte */
1724 SAFE_NALLOCA (char_ranges, 2, SCHARS (string));
1726 while (i_byte < size_byte)
1728 int leading_code = str[i_byte];
1729 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1730 i_byte += len;
1732 if (handle_iso_classes && c == '['
1733 && i_byte < size_byte
1734 && STRING_CHAR (str + i_byte) == ':')
1736 const unsigned char *class_beg = str + i_byte + 1;
1737 const unsigned char *class_end = class_beg;
1738 const unsigned char *class_limit = str + size_byte - 2;
1739 /* Leave room for the null. */
1740 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1741 re_wctype_t cc;
1743 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1744 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1746 while (class_end < class_limit
1747 && *class_end >= 'a' && *class_end <= 'z')
1748 class_end++;
1750 if (class_end == class_beg
1751 || *class_end != ':' || class_end[1] != ']')
1752 goto not_a_class_name_multibyte;
1754 memcpy (class_name, class_beg, class_end - class_beg);
1755 class_name[class_end - class_beg] = 0;
1757 cc = re_wctype (class_name);
1758 if (cc == 0)
1759 error ("Invalid ISO C character class");
1761 iso_classes = Fcons (make_number (cc), iso_classes);
1763 i_byte = class_end + 2 - str;
1764 continue;
1767 not_a_class_name_multibyte:
1768 if (c == '\\')
1770 if (i_byte == size_byte)
1771 break;
1773 leading_code = str[i_byte];
1774 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1775 i_byte += len;
1777 /* Treat `-' as range character only if another character
1778 follows. */
1779 if (i_byte + 1 < size_byte
1780 && str[i_byte] == '-')
1782 int c2, leading_code2;
1784 /* Skip over the dash. */
1785 i_byte++;
1787 /* Get the end of the range. */
1788 leading_code2 = str[i_byte];
1789 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1790 i_byte += len;
1792 if (c2 == '\\'
1793 && i_byte < size_byte)
1795 leading_code2 = str[i_byte];
1796 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1797 i_byte += len;
1800 if (c > c2)
1801 continue;
1802 if (ASCII_CHAR_P (c))
1804 while (c <= c2 && c < 0x80)
1805 fastmap[c++] = 1;
1806 leading_code = CHAR_LEADING_CODE (c);
1808 if (! ASCII_CHAR_P (c))
1810 int lim2 = leading_code2 + 1;
1811 while (leading_code < lim2)
1812 fastmap[leading_code++] = 1;
1813 if (c <= c2)
1815 char_ranges[n_char_ranges++] = c;
1816 char_ranges[n_char_ranges++] = c2;
1820 else
1822 if (ASCII_CHAR_P (c))
1823 fastmap[c] = 1;
1824 else
1826 fastmap[leading_code] = 1;
1827 char_ranges[n_char_ranges++] = c;
1828 char_ranges[n_char_ranges++] = c;
1833 /* If the current range is unibyte and STRING contains non-ASCII
1834 chars, arrange fastmap for the corresponding unibyte
1835 chars. */
1837 if (! multibyte && n_char_ranges > 0)
1839 memset (fastmap + 0200, 0, 0200);
1840 for (i = 0; i < n_char_ranges; i += 2)
1842 int c1 = char_ranges[i];
1843 int lim2 = char_ranges[i + 1] + 1;
1845 for (; c1 < lim2; c1++)
1847 int b = CHAR_TO_BYTE_SAFE (c1);
1848 if (b >= 0)
1849 fastmap[b] = 1;
1855 /* If ^ was the first character, complement the fastmap. */
1856 if (negate)
1858 if (! multibyte)
1859 for (i = 0; i < sizeof fastmap; i++)
1860 fastmap[i] ^= 1;
1861 else
1863 for (i = 0; i < 0200; i++)
1864 fastmap[i] ^= 1;
1865 /* All non-ASCII chars possibly match. */
1866 for (; i < sizeof fastmap; i++)
1867 fastmap[i] = 1;
1872 ptrdiff_t start_point = PT;
1873 ptrdiff_t pos = PT;
1874 ptrdiff_t pos_byte = PT_BYTE;
1875 unsigned char *p = PT_ADDR, *endp, *stop;
1877 if (forwardp)
1879 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1880 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1882 else
1884 endp = CHAR_POS_ADDR (XINT (lim));
1885 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1888 immediate_quit = 1;
1889 /* This code may look up syntax tables using functions that rely on the
1890 gl_state object. To make sure this object is not out of date,
1891 let's initialize it manually.
1892 We ignore syntax-table text-properties for now, since that's
1893 what we've done in the past. */
1894 SETUP_BUFFER_SYNTAX_TABLE ();
1895 if (forwardp)
1897 if (multibyte)
1898 while (1)
1900 int nbytes;
1902 if (p >= stop)
1904 if (p >= endp)
1905 break;
1906 p = GAP_END_ADDR;
1907 stop = endp;
1909 c = STRING_CHAR_AND_LENGTH (p, nbytes);
1910 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1912 if (negate)
1913 break;
1914 else
1915 goto fwd_ok;
1918 if (! fastmap[*p])
1919 break;
1920 if (! ASCII_CHAR_P (c))
1922 /* As we are looking at a multibyte character, we
1923 must look up the character in the table
1924 CHAR_RANGES. If there's no data in the table,
1925 that character is not what we want to skip. */
1927 /* The following code do the right thing even if
1928 n_char_ranges is zero (i.e. no data in
1929 CHAR_RANGES). */
1930 for (i = 0; i < n_char_ranges; i += 2)
1931 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1932 break;
1933 if (!(negate ^ (i < n_char_ranges)))
1934 break;
1936 fwd_ok:
1937 p += nbytes, pos++, pos_byte += nbytes;
1939 else
1940 while (1)
1942 if (p >= stop)
1944 if (p >= endp)
1945 break;
1946 p = GAP_END_ADDR;
1947 stop = endp;
1950 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1952 if (negate)
1953 break;
1954 else
1955 goto fwd_unibyte_ok;
1958 if (!fastmap[*p])
1959 break;
1960 fwd_unibyte_ok:
1961 p++, pos++, pos_byte++;
1964 else
1966 if (multibyte)
1967 while (1)
1969 unsigned char *prev_p;
1971 if (p <= stop)
1973 if (p <= endp)
1974 break;
1975 p = GPT_ADDR;
1976 stop = endp;
1978 prev_p = p;
1979 while (--p >= stop && ! CHAR_HEAD_P (*p));
1980 c = STRING_CHAR (p);
1982 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1984 if (negate)
1985 break;
1986 else
1987 goto back_ok;
1990 if (! fastmap[*p])
1991 break;
1992 if (! ASCII_CHAR_P (c))
1994 /* See the comment in the previous similar code. */
1995 for (i = 0; i < n_char_ranges; i += 2)
1996 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1997 break;
1998 if (!(negate ^ (i < n_char_ranges)))
1999 break;
2001 back_ok:
2002 pos--, pos_byte -= prev_p - p;
2004 else
2005 while (1)
2007 if (p <= stop)
2009 if (p <= endp)
2010 break;
2011 p = GPT_ADDR;
2012 stop = endp;
2015 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
2017 if (negate)
2018 break;
2019 else
2020 goto back_unibyte_ok;
2023 if (!fastmap[p[-1]])
2024 break;
2025 back_unibyte_ok:
2026 p--, pos--, pos_byte--;
2030 SET_PT_BOTH (pos, pos_byte);
2031 immediate_quit = 0;
2033 SAFE_FREE ();
2034 return make_number (PT - start_point);
2039 static Lisp_Object
2040 skip_syntaxes (bool forwardp, Lisp_Object string, Lisp_Object lim)
2042 int c;
2043 unsigned char fastmap[0400];
2044 bool negate = 0;
2045 ptrdiff_t i, i_byte;
2046 bool multibyte;
2047 ptrdiff_t size_byte;
2048 unsigned char *str;
2050 CHECK_STRING (string);
2052 if (NILP (lim))
2053 XSETINT (lim, forwardp ? ZV : BEGV);
2054 else
2055 CHECK_NUMBER_COERCE_MARKER (lim);
2057 /* In any case, don't allow scan outside bounds of buffer. */
2058 if (XINT (lim) > ZV)
2059 XSETFASTINT (lim, ZV);
2060 if (XINT (lim) < BEGV)
2061 XSETFASTINT (lim, BEGV);
2063 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
2064 return make_number (0);
2066 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
2067 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
2069 memset (fastmap, 0, sizeof fastmap);
2071 if (SBYTES (string) > SCHARS (string))
2072 /* As this is very rare case (syntax spec is ASCII only), don't
2073 consider efficiency. */
2074 string = string_make_unibyte (string);
2076 str = SDATA (string);
2077 size_byte = SBYTES (string);
2079 i_byte = 0;
2080 if (i_byte < size_byte
2081 && SREF (string, 0) == '^')
2083 negate = 1; i_byte++;
2086 /* Find the syntaxes specified and set their elements of fastmap. */
2088 while (i_byte < size_byte)
2090 c = str[i_byte++];
2091 fastmap[syntax_spec_code[c]] = 1;
2094 /* If ^ was the first character, complement the fastmap. */
2095 if (negate)
2096 for (i = 0; i < sizeof fastmap; i++)
2097 fastmap[i] ^= 1;
2100 ptrdiff_t start_point = PT;
2101 ptrdiff_t pos = PT;
2102 ptrdiff_t pos_byte = PT_BYTE;
2103 unsigned char *p = PT_ADDR, *endp, *stop;
2105 if (forwardp)
2107 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
2108 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
2110 else
2112 endp = CHAR_POS_ADDR (XINT (lim));
2113 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
2116 immediate_quit = 1;
2117 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
2118 if (forwardp)
2120 if (multibyte)
2122 while (1)
2124 int nbytes;
2126 if (p >= stop)
2128 if (p >= endp)
2129 break;
2130 p = GAP_END_ADDR;
2131 stop = endp;
2133 c = STRING_CHAR_AND_LENGTH (p, nbytes);
2134 if (! fastmap[SYNTAX (c)])
2135 break;
2136 p += nbytes, pos++, pos_byte += nbytes;
2137 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2140 else
2142 while (1)
2144 if (p >= stop)
2146 if (p >= endp)
2147 break;
2148 p = GAP_END_ADDR;
2149 stop = endp;
2151 if (! fastmap[SYNTAX (*p)])
2152 break;
2153 p++, pos++, pos_byte++;
2154 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2158 else
2160 if (multibyte)
2162 while (1)
2164 unsigned char *prev_p;
2166 if (p <= stop)
2168 if (p <= endp)
2169 break;
2170 p = GPT_ADDR;
2171 stop = endp;
2173 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2174 prev_p = p;
2175 while (--p >= stop && ! CHAR_HEAD_P (*p));
2176 c = STRING_CHAR (p);
2177 if (! fastmap[SYNTAX (c)])
2178 break;
2179 pos--, pos_byte -= prev_p - p;
2182 else
2184 while (1)
2186 if (p <= stop)
2188 if (p <= endp)
2189 break;
2190 p = GPT_ADDR;
2191 stop = endp;
2193 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2194 if (! fastmap[SYNTAX (p[-1])])
2195 break;
2196 p--, pos--, pos_byte--;
2201 SET_PT_BOTH (pos, pos_byte);
2202 immediate_quit = 0;
2204 return make_number (PT - start_point);
2208 /* Return true if character C belongs to one of the ISO classes
2209 in the list ISO_CLASSES. Each class is represented by an
2210 integer which is its type according to re_wctype. */
2212 static bool
2213 in_classes (int c, Lisp_Object iso_classes)
2215 bool fits_class = 0;
2217 while (CONSP (iso_classes))
2219 Lisp_Object elt;
2220 elt = XCAR (iso_classes);
2221 iso_classes = XCDR (iso_classes);
2223 if (re_iswctype (c, XFASTINT (elt)))
2224 fits_class = 1;
2227 return fits_class;
2230 /* Jump over a comment, assuming we are at the beginning of one.
2231 FROM is the current position.
2232 FROM_BYTE is the bytepos corresponding to FROM.
2233 Do not move past STOP (a charpos).
2234 The comment over which we have to jump is of style STYLE
2235 (either SYNTAX_FLAGS_COMMENT_STYLE (foo) or ST_COMMENT_STYLE).
2236 NESTING should be positive to indicate the nesting at the beginning
2237 for nested comments and should be zero or negative else.
2238 ST_COMMENT_STYLE cannot be nested.
2239 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2240 (or 0 If the search cannot start in the middle of a two-character).
2242 If successful, return true and store the charpos of the comment's end
2243 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2244 Else, return false and store the charpos STOP into *CHARPOS_PTR, the
2245 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2246 (as defined for state.incomment) in *INCOMMENT_PTR.
2248 The comment end is the last character of the comment rather than the
2249 character just after the comment.
2251 Global syntax data is assumed to initially be valid for FROM and
2252 remains valid for forward search starting at the returned position. */
2254 static bool
2255 forw_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop,
2256 EMACS_INT nesting, int style, int prev_syntax,
2257 ptrdiff_t *charpos_ptr, ptrdiff_t *bytepos_ptr,
2258 EMACS_INT *incomment_ptr)
2260 register int c, c1;
2261 register enum syntaxcode code;
2262 register int syntax, other_syntax;
2264 if (nesting <= 0) nesting = -1;
2266 /* Enter the loop in the middle so that we find
2267 a 2-char comment ender if we start in the middle of it. */
2268 syntax = prev_syntax;
2269 if (syntax != 0) goto forw_incomment;
2271 while (1)
2273 if (from == stop)
2275 *incomment_ptr = nesting;
2276 *charpos_ptr = from;
2277 *bytepos_ptr = from_byte;
2278 return 0;
2280 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2281 syntax = SYNTAX_WITH_FLAGS (c);
2282 code = syntax & 0xff;
2283 if (code == Sendcomment
2284 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style
2285 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2286 (nesting > 0 && --nesting == 0) : nesting < 0))
2287 /* we have encountered a comment end of the same style
2288 as the comment sequence which began this comment
2289 section */
2290 break;
2291 if (code == Scomment_fence
2292 && style == ST_COMMENT_STYLE)
2293 /* we have encountered a comment end of the same style
2294 as the comment sequence which began this comment
2295 section. */
2296 break;
2297 if (nesting > 0
2298 && code == Scomment
2299 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2300 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style)
2301 /* we have encountered a nested comment of the same style
2302 as the comment sequence which began this comment section */
2303 nesting++;
2304 INC_BOTH (from, from_byte);
2305 UPDATE_SYNTAX_TABLE_FORWARD (from);
2307 forw_incomment:
2308 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2309 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2310 other_syntax = SYNTAX_WITH_FLAGS (c1),
2311 SYNTAX_FLAGS_COMEND_SECOND (other_syntax))
2312 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, other_syntax) == style
2313 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2314 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax))
2315 ? nesting > 0 : nesting < 0))
2317 if (--nesting <= 0)
2318 /* we have encountered a comment end of the same style
2319 as the comment sequence which began this comment
2320 section */
2321 break;
2322 else
2324 INC_BOTH (from, from_byte);
2325 UPDATE_SYNTAX_TABLE_FORWARD (from);
2328 if (nesting > 0
2329 && from < stop
2330 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2331 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2332 other_syntax = SYNTAX_WITH_FLAGS (c1),
2333 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax) == style
2334 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2335 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2336 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax)))
2337 /* we have encountered a nested comment of the same style
2338 as the comment sequence which began this comment
2339 section */
2341 INC_BOTH (from, from_byte);
2342 UPDATE_SYNTAX_TABLE_FORWARD (from);
2343 nesting++;
2346 *charpos_ptr = from;
2347 *bytepos_ptr = from_byte;
2348 return 1;
2351 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2352 doc: /*
2353 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2354 Stop scanning if we find something other than a comment or whitespace.
2355 Set point to where scanning stops.
2356 If COUNT comments are found as expected, with nothing except whitespace
2357 between them, return t; otherwise return nil. */)
2358 (Lisp_Object count)
2360 ptrdiff_t from, from_byte, stop;
2361 int c, c1;
2362 enum syntaxcode code;
2363 int comstyle = 0; /* style of comment encountered */
2364 bool comnested = 0; /* whether the comment is nestable or not */
2365 bool found;
2366 EMACS_INT count1;
2367 ptrdiff_t out_charpos, out_bytepos;
2368 EMACS_INT dummy;
2370 CHECK_NUMBER (count);
2371 count1 = XINT (count);
2372 stop = count1 > 0 ? ZV : BEGV;
2374 immediate_quit = 1;
2375 QUIT;
2377 from = PT;
2378 from_byte = PT_BYTE;
2380 SETUP_SYNTAX_TABLE (from, count1);
2381 while (count1 > 0)
2385 bool comstart_first;
2386 int syntax, other_syntax;
2388 if (from == stop)
2390 SET_PT_BOTH (from, from_byte);
2391 immediate_quit = 0;
2392 return Qnil;
2394 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2395 syntax = SYNTAX_WITH_FLAGS (c);
2396 code = SYNTAX (c);
2397 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2398 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2399 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2400 INC_BOTH (from, from_byte);
2401 UPDATE_SYNTAX_TABLE_FORWARD (from);
2402 if (from < stop && comstart_first
2403 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2404 other_syntax = SYNTAX_WITH_FLAGS (c1),
2405 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)))
2407 /* We have encountered a comment start sequence and we
2408 are ignoring all text inside comments. We must record
2409 the comment style this sequence begins so that later,
2410 only a comment end of the same style actually ends
2411 the comment section. */
2412 code = Scomment;
2413 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2414 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2415 INC_BOTH (from, from_byte);
2416 UPDATE_SYNTAX_TABLE_FORWARD (from);
2419 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2421 if (code == Scomment_fence)
2422 comstyle = ST_COMMENT_STYLE;
2423 else if (code != Scomment)
2425 immediate_quit = 0;
2426 DEC_BOTH (from, from_byte);
2427 SET_PT_BOTH (from, from_byte);
2428 return Qnil;
2430 /* We're at the start of a comment. */
2431 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2432 &out_charpos, &out_bytepos, &dummy);
2433 from = out_charpos; from_byte = out_bytepos;
2434 if (!found)
2436 immediate_quit = 0;
2437 SET_PT_BOTH (from, from_byte);
2438 return Qnil;
2440 INC_BOTH (from, from_byte);
2441 UPDATE_SYNTAX_TABLE_FORWARD (from);
2442 /* We have skipped one comment. */
2443 count1--;
2446 while (count1 < 0)
2448 while (1)
2450 bool quoted;
2451 int syntax;
2453 if (from <= stop)
2455 SET_PT_BOTH (BEGV, BEGV_BYTE);
2456 immediate_quit = 0;
2457 return Qnil;
2460 DEC_BOTH (from, from_byte);
2461 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2462 quoted = char_quoted (from, from_byte);
2463 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2464 syntax = SYNTAX_WITH_FLAGS (c);
2465 code = SYNTAX (c);
2466 comstyle = 0;
2467 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2468 if (code == Sendcomment)
2469 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2470 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2471 && prev_char_comend_first (from, from_byte)
2472 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2474 int other_syntax;
2475 /* We must record the comment style encountered so that
2476 later, we can match only the proper comment begin
2477 sequence of the same style. */
2478 DEC_BOTH (from, from_byte);
2479 code = Sendcomment;
2480 /* Calling char_quoted, above, set up global syntax position
2481 at the new value of FROM. */
2482 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2483 other_syntax = SYNTAX_WITH_FLAGS (c1);
2484 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2485 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2488 if (code == Scomment_fence)
2490 /* Skip until first preceding unquoted comment_fence. */
2491 bool fence_found = 0;
2492 ptrdiff_t ini = from, ini_byte = from_byte;
2494 while (1)
2496 DEC_BOTH (from, from_byte);
2497 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2498 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2499 if (SYNTAX (c) == Scomment_fence
2500 && !char_quoted (from, from_byte))
2502 fence_found = 1;
2503 break;
2505 else if (from == stop)
2506 break;
2508 if (fence_found == 0)
2510 from = ini; /* Set point to ini + 1. */
2511 from_byte = ini_byte;
2512 goto leave;
2514 else
2515 /* We have skipped one comment. */
2516 break;
2518 else if (code == Sendcomment)
2520 found = back_comment (from, from_byte, stop, comnested, comstyle,
2521 &out_charpos, &out_bytepos);
2522 if (!found)
2524 if (c == '\n')
2525 /* This end-of-line is not an end-of-comment.
2526 Treat it like a whitespace.
2527 CC-mode (and maybe others) relies on this behavior. */
2529 else
2531 /* Failure: we should go back to the end of this
2532 not-quite-endcomment. */
2533 if (SYNTAX (c) != code)
2534 /* It was a two-char Sendcomment. */
2535 INC_BOTH (from, from_byte);
2536 goto leave;
2539 else
2541 /* We have skipped one comment. */
2542 from = out_charpos, from_byte = out_bytepos;
2543 break;
2546 else if (code != Swhitespace || quoted)
2548 leave:
2549 immediate_quit = 0;
2550 INC_BOTH (from, from_byte);
2551 SET_PT_BOTH (from, from_byte);
2552 return Qnil;
2556 count1++;
2559 SET_PT_BOTH (from, from_byte);
2560 immediate_quit = 0;
2561 return Qt;
2564 /* Return syntax code of character C if C is an ASCII character
2565 or if MULTIBYTE_SYMBOL_P is false. Otherwise, return Ssymbol. */
2567 static enum syntaxcode
2568 syntax_multibyte (int c, bool multibyte_symbol_p)
2570 return ASCII_CHAR_P (c) || !multibyte_symbol_p ? SYNTAX (c) : Ssymbol;
2573 static Lisp_Object
2574 scan_lists (EMACS_INT from, EMACS_INT count, EMACS_INT depth, bool sexpflag)
2576 Lisp_Object val;
2577 ptrdiff_t stop = count > 0 ? ZV : BEGV;
2578 int c, c1;
2579 int stringterm;
2580 bool quoted;
2581 bool mathexit = 0;
2582 enum syntaxcode code;
2583 EMACS_INT min_depth = depth; /* Err out if depth gets less than this. */
2584 int comstyle = 0; /* style of comment encountered */
2585 bool comnested = 0; /* whether the comment is nestable or not */
2586 ptrdiff_t temp_pos;
2587 EMACS_INT last_good = from;
2588 bool found;
2589 ptrdiff_t from_byte;
2590 ptrdiff_t out_bytepos, out_charpos;
2591 EMACS_INT dummy;
2592 bool multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2594 if (depth > 0) min_depth = 0;
2596 if (from > ZV) from = ZV;
2597 if (from < BEGV) from = BEGV;
2599 from_byte = CHAR_TO_BYTE (from);
2601 immediate_quit = 1;
2602 QUIT;
2604 SETUP_SYNTAX_TABLE (from, count);
2605 while (count > 0)
2607 while (from < stop)
2609 bool comstart_first, prefix;
2610 int syntax, other_syntax;
2611 UPDATE_SYNTAX_TABLE_FORWARD (from);
2612 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2613 syntax = SYNTAX_WITH_FLAGS (c);
2614 code = syntax_multibyte (c, multibyte_symbol_p);
2615 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2616 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2617 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2618 prefix = SYNTAX_FLAGS_PREFIX (syntax);
2619 if (depth == min_depth)
2620 last_good = from;
2621 INC_BOTH (from, from_byte);
2622 UPDATE_SYNTAX_TABLE_FORWARD (from);
2623 if (from < stop && comstart_first
2624 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2625 other_syntax = SYNTAX_WITH_FLAGS (c),
2626 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2627 && parse_sexp_ignore_comments)
2629 /* we have encountered a comment start sequence and we
2630 are ignoring all text inside comments. We must record
2631 the comment style this sequence begins so that later,
2632 only a comment end of the same style actually ends
2633 the comment section */
2634 code = Scomment;
2635 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2636 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2637 INC_BOTH (from, from_byte);
2638 UPDATE_SYNTAX_TABLE_FORWARD (from);
2641 if (prefix)
2642 continue;
2644 switch (code)
2646 case Sescape:
2647 case Scharquote:
2648 if (from == stop)
2649 goto lose;
2650 INC_BOTH (from, from_byte);
2651 /* treat following character as a word constituent */
2652 case Sword:
2653 case Ssymbol:
2654 if (depth || !sexpflag) break;
2655 /* This word counts as a sexp; return at end of it. */
2656 while (from < stop)
2658 UPDATE_SYNTAX_TABLE_FORWARD (from);
2660 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2661 switch (syntax_multibyte (c, multibyte_symbol_p))
2663 case Scharquote:
2664 case Sescape:
2665 INC_BOTH (from, from_byte);
2666 if (from == stop)
2667 goto lose;
2668 break;
2669 case Sword:
2670 case Ssymbol:
2671 case Squote:
2672 break;
2673 default:
2674 goto done;
2676 INC_BOTH (from, from_byte);
2678 goto done;
2680 case Scomment_fence:
2681 comstyle = ST_COMMENT_STYLE;
2682 /* FALLTHROUGH */
2683 case Scomment:
2684 if (!parse_sexp_ignore_comments) break;
2685 UPDATE_SYNTAX_TABLE_FORWARD (from);
2686 found = forw_comment (from, from_byte, stop,
2687 comnested, comstyle, 0,
2688 &out_charpos, &out_bytepos, &dummy);
2689 from = out_charpos, from_byte = out_bytepos;
2690 if (!found)
2692 if (depth == 0)
2693 goto done;
2694 goto lose;
2696 INC_BOTH (from, from_byte);
2697 UPDATE_SYNTAX_TABLE_FORWARD (from);
2698 break;
2700 case Smath:
2701 if (!sexpflag)
2702 break;
2703 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2705 INC_BOTH (from, from_byte);
2707 if (mathexit)
2709 mathexit = 0;
2710 goto close1;
2712 mathexit = 1;
2714 case Sopen:
2715 if (!++depth) goto done;
2716 break;
2718 case Sclose:
2719 close1:
2720 if (!--depth) goto done;
2721 if (depth < min_depth)
2722 xsignal3 (Qscan_error,
2723 build_string ("Containing expression ends prematurely"),
2724 make_number (last_good), make_number (from));
2725 break;
2727 case Sstring:
2728 case Sstring_fence:
2729 temp_pos = dec_bytepos (from_byte);
2730 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2731 while (1)
2733 enum syntaxcode c_code;
2734 if (from >= stop)
2735 goto lose;
2736 UPDATE_SYNTAX_TABLE_FORWARD (from);
2737 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2738 c_code = syntax_multibyte (c, multibyte_symbol_p);
2739 if (code == Sstring
2740 ? c == stringterm && c_code == Sstring
2741 : c_code == Sstring_fence)
2742 break;
2744 switch (c_code)
2746 case Scharquote:
2747 case Sescape:
2748 INC_BOTH (from, from_byte);
2750 INC_BOTH (from, from_byte);
2752 INC_BOTH (from, from_byte);
2753 if (!depth && sexpflag) goto done;
2754 break;
2755 default:
2756 /* Ignore whitespace, punctuation, quote, endcomment. */
2757 break;
2761 /* Reached end of buffer. Error if within object, return nil if between */
2762 if (depth)
2763 goto lose;
2765 immediate_quit = 0;
2766 return Qnil;
2768 /* End of object reached */
2769 done:
2770 count--;
2773 while (count < 0)
2775 while (from > stop)
2777 int syntax;
2778 DEC_BOTH (from, from_byte);
2779 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2780 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2781 syntax= SYNTAX_WITH_FLAGS (c);
2782 code = syntax_multibyte (c, multibyte_symbol_p);
2783 if (depth == min_depth)
2784 last_good = from;
2785 comstyle = 0;
2786 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2787 if (code == Sendcomment)
2788 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2789 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2790 && prev_char_comend_first (from, from_byte)
2791 && parse_sexp_ignore_comments)
2793 /* We must record the comment style encountered so that
2794 later, we can match only the proper comment begin
2795 sequence of the same style. */
2796 int c2, other_syntax;
2797 DEC_BOTH (from, from_byte);
2798 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2799 code = Sendcomment;
2800 c2 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2801 other_syntax = SYNTAX_WITH_FLAGS (c2);
2802 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2803 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2806 /* Quoting turns anything except a comment-ender
2807 into a word character. Note that this cannot be true
2808 if we decremented FROM in the if-statement above. */
2809 if (code != Sendcomment && char_quoted (from, from_byte))
2811 DEC_BOTH (from, from_byte);
2812 code = Sword;
2814 else if (SYNTAX_FLAGS_PREFIX (syntax))
2815 continue;
2817 switch (code)
2819 case Sword:
2820 case Ssymbol:
2821 case Sescape:
2822 case Scharquote:
2823 if (depth || !sexpflag) break;
2824 /* This word counts as a sexp; count object finished
2825 after passing it. */
2826 while (from > stop)
2828 temp_pos = from_byte;
2829 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2830 DEC_POS (temp_pos);
2831 else
2832 temp_pos--;
2833 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2834 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2835 /* Don't allow comment-end to be quoted. */
2836 if (syntax_multibyte (c1, multibyte_symbol_p) == Sendcomment)
2837 goto done2;
2838 quoted = char_quoted (from - 1, temp_pos);
2839 if (quoted)
2841 DEC_BOTH (from, from_byte);
2842 temp_pos = dec_bytepos (temp_pos);
2843 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2845 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2846 if (! quoted)
2847 switch (syntax_multibyte (c1, multibyte_symbol_p))
2849 case Sword: case Ssymbol: case Squote: break;
2850 default: goto done2;
2852 DEC_BOTH (from, from_byte);
2854 goto done2;
2856 case Smath:
2857 if (!sexpflag)
2858 break;
2859 if (from > BEGV)
2861 temp_pos = dec_bytepos (from_byte);
2862 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2863 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2864 DEC_BOTH (from, from_byte);
2866 if (mathexit)
2868 mathexit = 0;
2869 goto open2;
2871 mathexit = 1;
2873 case Sclose:
2874 if (!++depth) goto done2;
2875 break;
2877 case Sopen:
2878 open2:
2879 if (!--depth) goto done2;
2880 if (depth < min_depth)
2881 xsignal3 (Qscan_error,
2882 build_string ("Containing expression ends prematurely"),
2883 make_number (last_good), make_number (from));
2884 break;
2886 case Sendcomment:
2887 if (!parse_sexp_ignore_comments)
2888 break;
2889 found = back_comment (from, from_byte, stop, comnested, comstyle,
2890 &out_charpos, &out_bytepos);
2891 /* FIXME: if !found, it really wasn't a comment-end.
2892 For single-char Sendcomment, we can't do much about it apart
2893 from skipping the char.
2894 For 2-char endcomments, we could try again, taking both
2895 chars as separate entities, but it's a lot of trouble
2896 for very little gain, so we don't bother either. -sm */
2897 if (found)
2898 from = out_charpos, from_byte = out_bytepos;
2899 break;
2901 case Scomment_fence:
2902 case Sstring_fence:
2903 while (1)
2905 if (from == stop)
2906 goto lose;
2907 DEC_BOTH (from, from_byte);
2908 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2909 if (!char_quoted (from, from_byte))
2911 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2912 if (syntax_multibyte (c, multibyte_symbol_p) == code)
2913 break;
2916 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2917 break;
2919 case Sstring:
2920 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2921 while (1)
2923 if (from == stop)
2924 goto lose;
2925 DEC_BOTH (from, from_byte);
2926 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2927 if (!char_quoted (from, from_byte))
2929 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2930 if (c == stringterm
2931 && (syntax_multibyte (c, multibyte_symbol_p)
2932 == Sstring))
2933 break;
2936 if (!depth && sexpflag) goto done2;
2937 break;
2938 default:
2939 /* Ignore whitespace, punctuation, quote, endcomment. */
2940 break;
2944 /* Reached start of buffer. Error if within object, return nil if between */
2945 if (depth)
2946 goto lose;
2948 immediate_quit = 0;
2949 return Qnil;
2951 done2:
2952 count++;
2956 immediate_quit = 0;
2957 XSETFASTINT (val, from);
2958 return val;
2960 lose:
2961 xsignal3 (Qscan_error,
2962 build_string ("Unbalanced parentheses"),
2963 make_number (last_good), make_number (from));
2966 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2967 doc: /* Scan from character number FROM by COUNT lists.
2968 Scan forward if COUNT is positive, backward if COUNT is negative.
2969 Return the character number of the position thus found.
2971 A \"list", in this context, refers to a balanced parenthetical
2972 grouping, as determined by the syntax table.
2974 If DEPTH is nonzero, treat that as the nesting depth of the starting
2975 point (i.e. the starting point is DEPTH parentheses deep). This
2976 function scans over parentheses until the depth goes to zero COUNT
2977 times. Hence, positive DEPTH moves out that number of levels of
2978 parentheses, while negative DEPTH moves to a deeper level.
2980 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2982 If we reach the beginning or end of the accessible part of the buffer
2983 before we have scanned over COUNT lists, return nil if the depth at
2984 that point is zero, and signal a error if the depth is nonzero. */)
2985 (Lisp_Object from, Lisp_Object count, Lisp_Object depth)
2987 CHECK_NUMBER (from);
2988 CHECK_NUMBER (count);
2989 CHECK_NUMBER (depth);
2991 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2994 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2995 doc: /* Scan from character number FROM by COUNT balanced expressions.
2996 If COUNT is negative, scan backwards.
2997 Returns the character number of the position thus found.
2999 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3001 If the beginning or end of (the accessible part of) the buffer is reached
3002 in the middle of a parenthetical grouping, an error is signaled.
3003 If the beginning or end is reached between groupings
3004 but before count is used up, nil is returned. */)
3005 (Lisp_Object from, Lisp_Object count)
3007 CHECK_NUMBER (from);
3008 CHECK_NUMBER (count);
3010 return scan_lists (XINT (from), XINT (count), 0, 1);
3013 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
3014 0, 0, 0,
3015 doc: /* Move point backward over any number of chars with prefix syntax.
3016 This includes chars with expression prefix syntax class (') and those with
3017 the prefix syntax flag (p). */)
3018 (void)
3020 ptrdiff_t beg = BEGV;
3021 ptrdiff_t opoint = PT;
3022 ptrdiff_t opoint_byte = PT_BYTE;
3023 ptrdiff_t pos = PT;
3024 ptrdiff_t pos_byte = PT_BYTE;
3025 int c;
3027 if (pos <= beg)
3029 SET_PT_BOTH (opoint, opoint_byte);
3031 return Qnil;
3034 SETUP_SYNTAX_TABLE (pos, -1);
3036 DEC_BOTH (pos, pos_byte);
3038 while (!char_quoted (pos, pos_byte)
3039 /* Previous statement updates syntax table. */
3040 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
3041 || syntax_prefix_flag_p (c)))
3043 opoint = pos;
3044 opoint_byte = pos_byte;
3046 if (pos + 1 > beg)
3047 DEC_BOTH (pos, pos_byte);
3050 SET_PT_BOTH (opoint, opoint_byte);
3052 return Qnil;
3055 /* Parse forward from FROM / FROM_BYTE to END,
3056 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
3057 and return a description of the state of the parse at END.
3058 If STOPBEFORE, stop at the start of an atom.
3059 If COMMENTSTOP is 1, stop at the start of a comment.
3060 If COMMENTSTOP is -1, stop at the start or end of a comment,
3061 after the beginning of a string, or after the end of a string. */
3063 static void
3064 scan_sexps_forward (struct lisp_parse_state *stateptr,
3065 ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t end,
3066 EMACS_INT targetdepth, bool stopbefore,
3067 Lisp_Object oldstate, int commentstop)
3069 struct lisp_parse_state state;
3070 enum syntaxcode code;
3071 int c1;
3072 bool comnested;
3073 struct level { ptrdiff_t last, prev; };
3074 struct level levelstart[100];
3075 struct level *curlevel = levelstart;
3076 struct level *endlevel = levelstart + 100;
3077 EMACS_INT depth; /* Paren depth of current scanning location.
3078 level - levelstart equals this except
3079 when the depth becomes negative. */
3080 EMACS_INT mindepth; /* Lowest DEPTH value seen. */
3081 bool start_quoted = 0; /* True means starting after a char quote. */
3082 Lisp_Object tem;
3083 ptrdiff_t prev_from; /* Keep one character before FROM. */
3084 ptrdiff_t prev_from_byte;
3085 int prev_from_syntax;
3086 bool boundary_stop = commentstop == -1;
3087 bool nofence;
3088 bool found;
3089 ptrdiff_t out_bytepos, out_charpos;
3090 int temp;
3092 prev_from = from;
3093 prev_from_byte = from_byte;
3094 if (from != BEGV)
3095 DEC_BOTH (prev_from, prev_from_byte);
3097 /* Use this macro instead of `from++'. */
3098 #define INC_FROM \
3099 do { prev_from = from; \
3100 prev_from_byte = from_byte; \
3101 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
3102 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
3103 INC_BOTH (from, from_byte); \
3104 if (from < end) \
3105 UPDATE_SYNTAX_TABLE_FORWARD (from); \
3106 } while (0)
3108 immediate_quit = 1;
3109 QUIT;
3111 if (NILP (oldstate))
3113 depth = 0;
3114 state.instring = -1;
3115 state.incomment = 0;
3116 state.comstyle = 0; /* comment style a by default. */
3117 state.comstr_start = -1; /* no comment/string seen. */
3119 else
3121 tem = Fcar (oldstate);
3122 if (!NILP (tem))
3123 depth = XINT (tem);
3124 else
3125 depth = 0;
3127 oldstate = Fcdr (oldstate);
3128 oldstate = Fcdr (oldstate);
3129 oldstate = Fcdr (oldstate);
3130 tem = Fcar (oldstate);
3131 /* Check whether we are inside string_fence-style string: */
3132 state.instring = (!NILP (tem)
3133 ? (CHARACTERP (tem) ? XFASTINT (tem) : ST_STRING_STYLE)
3134 : -1);
3136 oldstate = Fcdr (oldstate);
3137 tem = Fcar (oldstate);
3138 state.incomment = (!NILP (tem)
3139 ? (INTEGERP (tem) ? XINT (tem) : -1)
3140 : 0);
3142 oldstate = Fcdr (oldstate);
3143 tem = Fcar (oldstate);
3144 start_quoted = !NILP (tem);
3146 /* if the eighth element of the list is nil, we are in comment
3147 style a. If it is non-nil, we are in comment style b */
3148 oldstate = Fcdr (oldstate);
3149 oldstate = Fcdr (oldstate);
3150 tem = Fcar (oldstate);
3151 state.comstyle = (NILP (tem)
3153 : (RANGED_INTEGERP (0, tem, ST_COMMENT_STYLE)
3154 ? XINT (tem)
3155 : ST_COMMENT_STYLE));
3157 oldstate = Fcdr (oldstate);
3158 tem = Fcar (oldstate);
3159 state.comstr_start =
3160 RANGED_INTEGERP (PTRDIFF_MIN, tem, PTRDIFF_MAX) ? XINT (tem) : -1;
3161 oldstate = Fcdr (oldstate);
3162 tem = Fcar (oldstate);
3163 while (!NILP (tem)) /* >= second enclosing sexps. */
3165 Lisp_Object temhd = Fcar (tem);
3166 if (RANGED_INTEGERP (PTRDIFF_MIN, temhd, PTRDIFF_MAX))
3167 curlevel->last = XINT (temhd);
3168 if (++curlevel == endlevel)
3169 curlevel--; /* error ("Nesting too deep for parser"); */
3170 curlevel->prev = -1;
3171 curlevel->last = -1;
3172 tem = Fcdr (tem);
3175 state.quoted = 0;
3176 mindepth = depth;
3178 curlevel->prev = -1;
3179 curlevel->last = -1;
3181 SETUP_SYNTAX_TABLE (prev_from, 1);
3182 temp = FETCH_CHAR (prev_from_byte);
3183 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
3184 UPDATE_SYNTAX_TABLE_FORWARD (from);
3186 /* Enter the loop at a place appropriate for initial state. */
3188 if (state.incomment)
3189 goto startincomment;
3190 if (state.instring >= 0)
3192 nofence = state.instring != ST_STRING_STYLE;
3193 if (start_quoted)
3194 goto startquotedinstring;
3195 goto startinstring;
3197 else if (start_quoted)
3198 goto startquoted;
3200 while (from < end)
3202 int syntax;
3203 INC_FROM;
3204 code = prev_from_syntax & 0xff;
3206 if (from < end
3207 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3208 && (c1 = FETCH_CHAR (from_byte),
3209 syntax = SYNTAX_WITH_FLAGS (c1),
3210 SYNTAX_FLAGS_COMSTART_SECOND (syntax)))
3211 /* Duplicate code to avoid a complex if-expression
3212 which causes trouble for the SGI compiler. */
3214 /* Record the comment style we have entered so that only
3215 the comment-end sequence of the same style actually
3216 terminates the comment section. */
3217 state.comstyle
3218 = SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_from_syntax);
3219 comnested = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax)
3220 | SYNTAX_FLAGS_COMMENT_NESTED (syntax));
3221 state.incomment = comnested ? 1 : -1;
3222 state.comstr_start = prev_from;
3223 INC_FROM;
3224 code = Scomment;
3226 else if (code == Scomment_fence)
3228 /* Record the comment style we have entered so that only
3229 the comment-end sequence of the same style actually
3230 terminates the comment section. */
3231 state.comstyle = ST_COMMENT_STYLE;
3232 state.incomment = -1;
3233 state.comstr_start = prev_from;
3234 code = Scomment;
3236 else if (code == Scomment)
3238 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax, 0);
3239 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3240 1 : -1);
3241 state.comstr_start = prev_from;
3244 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3245 continue;
3246 switch (code)
3248 case Sescape:
3249 case Scharquote:
3250 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3251 curlevel->last = prev_from;
3252 startquoted:
3253 if (from == end) goto endquoted;
3254 INC_FROM;
3255 goto symstarted;
3256 /* treat following character as a word constituent */
3257 case Sword:
3258 case Ssymbol:
3259 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3260 curlevel->last = prev_from;
3261 symstarted:
3262 while (from < end)
3264 int symchar = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3265 switch (SYNTAX (symchar))
3267 case Scharquote:
3268 case Sescape:
3269 INC_FROM;
3270 if (from == end) goto endquoted;
3271 break;
3272 case Sword:
3273 case Ssymbol:
3274 case Squote:
3275 break;
3276 default:
3277 goto symdone;
3279 INC_FROM;
3281 symdone:
3282 curlevel->prev = curlevel->last;
3283 break;
3285 case Scomment_fence: /* Can't happen because it's handled above. */
3286 case Scomment:
3287 if (commentstop || boundary_stop) goto done;
3288 startincomment:
3289 /* The (from == BEGV) test was to enter the loop in the middle so
3290 that we find a 2-char comment ender even if we start in the
3291 middle of it. We don't want to do that if we're just at the
3292 beginning of the comment (think of (*) ... (*)). */
3293 found = forw_comment (from, from_byte, end,
3294 state.incomment, state.comstyle,
3295 (from == BEGV || from < state.comstr_start + 3)
3296 ? 0 : prev_from_syntax,
3297 &out_charpos, &out_bytepos, &state.incomment);
3298 from = out_charpos; from_byte = out_bytepos;
3299 /* Beware! prev_from and friends are invalid now.
3300 Luckily, the `done' doesn't use them and the INC_FROM
3301 sets them to a sane value without looking at them. */
3302 if (!found) goto done;
3303 INC_FROM;
3304 state.incomment = 0;
3305 state.comstyle = 0; /* reset the comment style */
3306 if (boundary_stop) goto done;
3307 break;
3309 case Sopen:
3310 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3311 depth++;
3312 /* curlevel++->last ran into compiler bug on Apollo */
3313 curlevel->last = prev_from;
3314 if (++curlevel == endlevel)
3315 curlevel--; /* error ("Nesting too deep for parser"); */
3316 curlevel->prev = -1;
3317 curlevel->last = -1;
3318 if (targetdepth == depth) goto done;
3319 break;
3321 case Sclose:
3322 depth--;
3323 if (depth < mindepth)
3324 mindepth = depth;
3325 if (curlevel != levelstart)
3326 curlevel--;
3327 curlevel->prev = curlevel->last;
3328 if (targetdepth == depth) goto done;
3329 break;
3331 case Sstring:
3332 case Sstring_fence:
3333 state.comstr_start = from - 1;
3334 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3335 curlevel->last = prev_from;
3336 state.instring = (code == Sstring
3337 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3338 : ST_STRING_STYLE);
3339 if (boundary_stop) goto done;
3340 startinstring:
3342 nofence = state.instring != ST_STRING_STYLE;
3344 while (1)
3346 int c;
3347 enum syntaxcode c_code;
3349 if (from >= end) goto done;
3350 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3351 c_code = SYNTAX (c);
3353 /* Check C_CODE here so that if the char has
3354 a syntax-table property which says it is NOT
3355 a string character, it does not end the string. */
3356 if (nofence && c == state.instring && c_code == Sstring)
3357 break;
3359 switch (c_code)
3361 case Sstring_fence:
3362 if (!nofence) goto string_end;
3363 break;
3364 case Scharquote:
3365 case Sescape:
3366 INC_FROM;
3367 startquotedinstring:
3368 if (from >= end) goto endquoted;
3370 INC_FROM;
3373 string_end:
3374 state.instring = -1;
3375 curlevel->prev = curlevel->last;
3376 INC_FROM;
3377 if (boundary_stop) goto done;
3378 break;
3380 case Smath:
3381 /* FIXME: We should do something with it. */
3382 break;
3383 default:
3384 /* Ignore whitespace, punctuation, quote, endcomment. */
3385 break;
3388 goto done;
3390 stop: /* Here if stopping before start of sexp. */
3391 from = prev_from; /* We have just fetched the char that starts it; */
3392 from_byte = prev_from_byte;
3393 goto done; /* but return the position before it. */
3395 endquoted:
3396 state.quoted = 1;
3397 done:
3398 state.depth = depth;
3399 state.mindepth = mindepth;
3400 state.thislevelstart = curlevel->prev;
3401 state.prevlevelstart
3402 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3403 state.location = from;
3404 state.location_byte = from_byte;
3405 state.levelstarts = Qnil;
3406 while (curlevel > levelstart)
3407 state.levelstarts = Fcons (make_number ((--curlevel)->last),
3408 state.levelstarts);
3409 immediate_quit = 0;
3411 *stateptr = state;
3414 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3415 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3416 Parsing stops at TO or when certain criteria are met;
3417 point is set to where parsing stops.
3418 If fifth arg OLDSTATE is omitted or nil,
3419 parsing assumes that FROM is the beginning of a function.
3420 Value is a list of elements describing final state of parsing:
3421 0. depth in parens.
3422 1. character address of start of innermost containing list; nil if none.
3423 2. character address of start of last complete sexp terminated.
3424 3. non-nil if inside a string.
3425 (it is the character that will terminate the string,
3426 or t if the string should be terminated by a generic string delimiter.)
3427 4. nil if outside a comment, t if inside a non-nestable comment,
3428 else an integer (the current comment nesting).
3429 5. t if following a quote character.
3430 6. the minimum paren-depth encountered during this scan.
3431 7. style of comment, if any.
3432 8. character address of start of comment or string; nil if not in one.
3433 9. Intermediate data for continuation of parsing (subject to change).
3434 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3435 in parentheses becomes equal to TARGETDEPTH.
3436 Fourth arg STOPBEFORE non-nil means stop when come to
3437 any character that starts a sexp.
3438 Fifth arg OLDSTATE is a list like what this function returns.
3439 It is used to initialize the state of the parse. Elements number 1, 2, 6
3440 are ignored.
3441 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3442 If it is symbol `syntax-table', stop after the start of a comment or a
3443 string, or after end of a comment or a string. */)
3444 (Lisp_Object from, Lisp_Object to, Lisp_Object targetdepth,
3445 Lisp_Object stopbefore, Lisp_Object oldstate, Lisp_Object commentstop)
3447 struct lisp_parse_state state;
3448 EMACS_INT target;
3450 if (!NILP (targetdepth))
3452 CHECK_NUMBER (targetdepth);
3453 target = XINT (targetdepth);
3455 else
3456 target = TYPE_MINIMUM (EMACS_INT); /* We won't reach this depth */
3458 validate_region (&from, &to);
3459 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3460 XINT (to),
3461 target, !NILP (stopbefore), oldstate,
3462 (NILP (commentstop)
3463 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3465 SET_PT_BOTH (state.location, state.location_byte);
3467 return Fcons (make_number (state.depth),
3468 Fcons (state.prevlevelstart < 0
3469 ? Qnil : make_number (state.prevlevelstart),
3470 Fcons (state.thislevelstart < 0
3471 ? Qnil : make_number (state.thislevelstart),
3472 Fcons (state.instring >= 0
3473 ? (state.instring == ST_STRING_STYLE
3474 ? Qt : make_number (state.instring)) : Qnil,
3475 Fcons (state.incomment < 0 ? Qt :
3476 (state.incomment == 0 ? Qnil :
3477 make_number (state.incomment)),
3478 Fcons (state.quoted ? Qt : Qnil,
3479 Fcons (make_number (state.mindepth),
3480 Fcons ((state.comstyle
3481 ? (state.comstyle == ST_COMMENT_STYLE
3482 ? Qsyntax_table
3483 : make_number (state.comstyle))
3484 : Qnil),
3485 Fcons (((state.incomment
3486 || (state.instring >= 0))
3487 ? make_number (state.comstr_start)
3488 : Qnil),
3489 Fcons (state.levelstarts, Qnil))))))))));
3492 void
3493 init_syntax_once (void)
3495 register int i, c;
3496 Lisp_Object temp;
3498 /* This has to be done here, before we call Fmake_char_table. */
3499 DEFSYM (Qsyntax_table, "syntax-table");
3501 /* Create objects which can be shared among syntax tables. */
3502 Vsyntax_code_object = make_uninit_vector (Smax);
3503 for (i = 0; i < Smax; i++)
3504 ASET (Vsyntax_code_object, i, Fcons (make_number (i), Qnil));
3506 /* Now we are ready to set up this property, so we can
3507 create syntax tables. */
3508 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3510 temp = AREF (Vsyntax_code_object, Swhitespace);
3512 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3514 /* Control characters should not be whitespace. */
3515 temp = AREF (Vsyntax_code_object, Spunct);
3516 for (i = 0; i <= ' ' - 1; i++)
3517 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3518 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3520 /* Except that a few really are whitespace. */
3521 temp = AREF (Vsyntax_code_object, Swhitespace);
3522 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3523 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3524 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3525 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3526 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3528 temp = AREF (Vsyntax_code_object, Sword);
3529 for (i = 'a'; i <= 'z'; i++)
3530 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3531 for (i = 'A'; i <= 'Z'; i++)
3532 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3533 for (i = '0'; i <= '9'; i++)
3534 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3536 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3537 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3539 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3540 Fcons (make_number (Sopen), make_number (')')));
3541 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3542 Fcons (make_number (Sclose), make_number ('(')));
3543 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3544 Fcons (make_number (Sopen), make_number (']')));
3545 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3546 Fcons (make_number (Sclose), make_number ('[')));
3547 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3548 Fcons (make_number (Sopen), make_number ('}')));
3549 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3550 Fcons (make_number (Sclose), make_number ('{')));
3551 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3552 Fcons (make_number (Sstring), Qnil));
3553 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3554 Fcons (make_number (Sescape), Qnil));
3556 temp = AREF (Vsyntax_code_object, Ssymbol);
3557 for (i = 0; i < 10; i++)
3559 c = "_-+*/&|<>="[i];
3560 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3563 temp = AREF (Vsyntax_code_object, Spunct);
3564 for (i = 0; i < 12; i++)
3566 c = ".,;:?!#@~^'`"[i];
3567 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3570 /* All multibyte characters have syntax `word' by default. */
3571 temp = AREF (Vsyntax_code_object, Sword);
3572 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3575 void
3576 syms_of_syntax (void)
3578 DEFSYM (Qsyntax_table_p, "syntax-table-p");
3580 staticpro (&Vsyntax_code_object);
3582 staticpro (&gl_state.object);
3583 staticpro (&gl_state.global_code);
3584 staticpro (&gl_state.current_syntax_table);
3585 staticpro (&gl_state.old_prop);
3587 /* Defined in regex.c */
3588 staticpro (&re_match_object);
3590 DEFSYM (Qscan_error, "scan-error");
3591 Fput (Qscan_error, Qerror_conditions,
3592 listn (CONSTYPE_PURE, 2, Qscan_error, Qerror));
3593 Fput (Qscan_error, Qerror_message,
3594 build_pure_c_string ("Scan error"));
3596 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments,
3597 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3599 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties,
3600 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3601 Otherwise, that text property is simply ignored.
3602 See the info node `(elisp)Syntax Properties' for a description of the
3603 `syntax-table' property. */);
3605 words_include_escapes = 0;
3606 DEFVAR_BOOL ("words-include-escapes", words_include_escapes,
3607 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3609 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol,
3610 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3611 multibyte_syntax_as_symbol = 0;
3613 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3614 open_paren_in_column_0_is_defun_start,
3615 doc: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3616 open_paren_in_column_0_is_defun_start = 1;
3619 DEFVAR_LISP ("find-word-boundary-function-table",
3620 Vfind_word_boundary_function_table,
3621 doc: /*
3622 Char table of functions to search for the word boundary.
3623 Each function is called with two arguments; POS and LIMIT.
3624 POS and LIMIT are character positions in the current buffer.
3626 If POS is less than LIMIT, POS is at the first character of a word,
3627 and the return value of a function is a position after the last
3628 character of that word.
3630 If POS is not less than LIMIT, POS is at the last character of a word,
3631 and the return value of a function is a position at the first
3632 character of that word.
3634 In both cases, LIMIT bounds the search. */);
3635 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3637 defsubr (&Ssyntax_table_p);
3638 defsubr (&Ssyntax_table);
3639 defsubr (&Sstandard_syntax_table);
3640 defsubr (&Scopy_syntax_table);
3641 defsubr (&Sset_syntax_table);
3642 defsubr (&Schar_syntax);
3643 defsubr (&Smatching_paren);
3644 defsubr (&Sstring_to_syntax);
3645 defsubr (&Smodify_syntax_entry);
3646 defsubr (&Sinternal_describe_syntax_value);
3648 defsubr (&Sforward_word);
3650 defsubr (&Sskip_chars_forward);
3651 defsubr (&Sskip_chars_backward);
3652 defsubr (&Sskip_syntax_forward);
3653 defsubr (&Sskip_syntax_backward);
3655 defsubr (&Sforward_comment);
3656 defsubr (&Sscan_lists);
3657 defsubr (&Sscan_sexps);
3658 defsubr (&Sbackward_prefix_chars);
3659 defsubr (&Sparse_partial_sexp);