Implement inhibit-bidi-mirroring; improve biditest.el.
[emacs.git] / src / syntax.c
blob4166ee211c76eadad95dc1b15ce70c53a6278131
1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2014 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 static Lisp_Object Qsyntax_table_p;
141 static Lisp_Object Qsyntax_table, Qscan_error;
143 /* This is the internal form of the parse state used in parse-partial-sexp. */
145 struct lisp_parse_state
147 EMACS_INT depth; /* Depth at end of parsing. */
148 int instring; /* -1 if not within string, else desired terminator. */
149 EMACS_INT incomment; /* -1 if in unnestable comment else comment nesting */
150 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
151 bool quoted; /* True if just after an escape char at end of parsing. */
152 EMACS_INT mindepth; /* Minimum depth seen while scanning. */
153 /* Char number of most recent start-of-expression at current level */
154 ptrdiff_t thislevelstart;
155 /* Char number of start of containing expression */
156 ptrdiff_t prevlevelstart;
157 ptrdiff_t location; /* Char number at which parsing stopped. */
158 ptrdiff_t location_byte; /* Corresponding byte position. */
159 ptrdiff_t comstr_start; /* Position of last comment/string starter. */
160 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
161 of levels (starting from outermost). */
164 /* These variables are a cache for finding the start of a defun.
165 find_start_pos is the place for which the defun start was found.
166 find_start_value is the defun start position found for it.
167 find_start_value_byte is the corresponding byte position.
168 find_start_buffer is the buffer it was found in.
169 find_start_begv is the BEGV value when it was found.
170 find_start_modiff is the value of MODIFF when it was found. */
172 static ptrdiff_t find_start_pos;
173 static ptrdiff_t find_start_value;
174 static ptrdiff_t find_start_value_byte;
175 static struct buffer *find_start_buffer;
176 static ptrdiff_t find_start_begv;
177 static EMACS_INT find_start_modiff;
180 static Lisp_Object skip_chars (bool, Lisp_Object, Lisp_Object, bool);
181 static Lisp_Object skip_syntaxes (bool, Lisp_Object, Lisp_Object);
182 static Lisp_Object scan_lists (EMACS_INT, EMACS_INT, EMACS_INT, bool);
183 static void scan_sexps_forward (struct lisp_parse_state *,
184 ptrdiff_t, ptrdiff_t, ptrdiff_t, EMACS_INT,
185 bool, Lisp_Object, int);
186 static bool in_classes (int, Lisp_Object);
188 /* This setter is used only in this file, so it can be private. */
189 static void
190 bset_syntax_table (struct buffer *b, Lisp_Object val)
192 b->INTERNAL_FIELD (syntax_table) = val;
195 /* Whether the syntax of the character C has the prefix flag set. */
196 bool
197 syntax_prefix_flag_p (int c)
199 return SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c));
202 struct gl_state_s gl_state; /* Global state of syntax parser. */
204 enum { INTERVALS_AT_ONCE = 10 }; /* 1 + max-number of intervals
205 to scan to property-change. */
207 /* Set the syntax entry VAL for char C in table TABLE. */
209 static void
210 SET_RAW_SYNTAX_ENTRY (Lisp_Object table, int c, Lisp_Object val)
212 CHAR_TABLE_SET (table, c, val);
215 /* Set the syntax entry VAL for char-range RANGE in table TABLE.
216 RANGE is a cons (FROM . TO) specifying the range of characters. */
218 static void
219 SET_RAW_SYNTAX_ENTRY_RANGE (Lisp_Object table, Lisp_Object range,
220 Lisp_Object val)
222 Fset_char_table_range (table, range, val);
225 /* Extract the information from the entry for character C
226 in the current syntax table. */
228 static Lisp_Object
229 SYNTAX_MATCH (int c)
231 Lisp_Object ent = SYNTAX_ENTRY (c);
232 return CONSP (ent) ? XCDR (ent) : Qnil;
235 /* This should be called with FROM at the start of forward
236 search, or after the last position of the backward search. It
237 makes sure that the first char is picked up with correct table, so
238 one does not need to call UPDATE_SYNTAX_TABLE immediately after the
239 call.
240 Sign of COUNT gives the direction of the search.
243 static void
244 SETUP_SYNTAX_TABLE (ptrdiff_t from, ptrdiff_t count)
246 SETUP_BUFFER_SYNTAX_TABLE ();
247 gl_state.b_property = BEGV;
248 gl_state.e_property = ZV + 1;
249 gl_state.object = Qnil;
250 gl_state.offset = 0;
251 if (parse_sexp_lookup_properties)
252 if (count > 0 || from > BEGV)
253 update_syntax_table (count > 0 ? from : from - 1, count, 1, Qnil);
256 /* Same as above, but in OBJECT. If OBJECT is nil, use current buffer.
257 If it is t (which is only used in fast_c_string_match_ignore_case),
258 ignore properties altogether.
260 This is meant for regex.c to use. For buffers, regex.c passes arguments
261 to the UPDATE_SYNTAX_TABLE functions which are relative to BEGV.
262 So if it is a buffer, we set the offset field to BEGV. */
264 void
265 SETUP_SYNTAX_TABLE_FOR_OBJECT (Lisp_Object object,
266 ptrdiff_t from, ptrdiff_t count)
268 SETUP_BUFFER_SYNTAX_TABLE ();
269 gl_state.object = object;
270 if (BUFFERP (gl_state.object))
272 struct buffer *buf = XBUFFER (gl_state.object);
273 gl_state.b_property = 1;
274 gl_state.e_property = BUF_ZV (buf) - BUF_BEGV (buf) + 1;
275 gl_state.offset = BUF_BEGV (buf) - 1;
277 else if (NILP (gl_state.object))
279 gl_state.b_property = 1;
280 gl_state.e_property = ZV - BEGV + 1;
281 gl_state.offset = BEGV - 1;
283 else if (EQ (gl_state.object, Qt))
285 gl_state.b_property = 0;
286 gl_state.e_property = PTRDIFF_MAX;
287 gl_state.offset = 0;
289 else
291 gl_state.b_property = 0;
292 gl_state.e_property = 1 + SCHARS (gl_state.object);
293 gl_state.offset = 0;
295 if (parse_sexp_lookup_properties)
296 update_syntax_table (from + gl_state.offset - (count <= 0),
297 count, 1, gl_state.object);
300 /* Update gl_state to an appropriate interval which contains CHARPOS. The
301 sign of COUNT give the relative position of CHARPOS wrt the previously
302 valid interval. If INIT, only [be]_property fields of gl_state are
303 valid at start, the rest is filled basing on OBJECT.
305 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
306 direction than the intervals - or in an interval. We update the
307 current syntax-table basing on the property of this interval, and
308 update the interval to start further than CHARPOS - or be
309 NULL. We also update lim_property to be the next value of
310 charpos to call this subroutine again - or be before/after the
311 start/end of OBJECT. */
313 void
314 update_syntax_table (ptrdiff_t charpos, EMACS_INT count, bool init,
315 Lisp_Object object)
317 Lisp_Object tmp_table;
318 int cnt = 0;
319 bool invalidate = 1;
320 INTERVAL i;
322 if (init)
324 gl_state.old_prop = Qnil;
325 gl_state.start = gl_state.b_property;
326 gl_state.stop = gl_state.e_property;
327 i = interval_of (charpos, object);
328 gl_state.backward_i = gl_state.forward_i = i;
329 invalidate = 0;
330 if (!i)
331 return;
332 /* interval_of updates only ->position of the return value, so
333 update the parents manually to speed up update_interval. */
334 while (!NULL_PARENT (i))
336 if (AM_RIGHT_CHILD (i))
337 INTERVAL_PARENT (i)->position = i->position
338 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
339 - TOTAL_LENGTH (INTERVAL_PARENT (i))
340 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
341 else
342 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
343 + TOTAL_LENGTH (i);
344 i = INTERVAL_PARENT (i);
346 i = gl_state.forward_i;
347 gl_state.b_property = i->position - gl_state.offset;
348 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
349 goto update;
351 i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
353 /* We are guaranteed to be called with CHARPOS either in i,
354 or further off. */
355 if (!i)
356 error ("Error in syntax_table logic for to-the-end intervals");
357 else if (charpos < i->position) /* Move left. */
359 if (count > 0)
360 error ("Error in syntax_table logic for intervals <-");
361 /* Update the interval. */
362 i = update_interval (i, charpos);
363 if (INTERVAL_LAST_POS (i) != gl_state.b_property)
365 invalidate = 0;
366 gl_state.forward_i = i;
367 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
370 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
372 if (count < 0)
373 error ("Error in syntax_table logic for intervals ->");
374 /* Update the interval. */
375 i = update_interval (i, charpos);
376 if (i->position != gl_state.e_property)
378 invalidate = 0;
379 gl_state.backward_i = i;
380 gl_state.b_property = i->position - gl_state.offset;
384 update:
385 tmp_table = textget (i->plist, Qsyntax_table);
387 if (invalidate)
388 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
390 if (invalidate) /* Did not get to adjacent interval. */
391 { /* with the same table => */
392 /* invalidate the old range. */
393 if (count > 0)
395 gl_state.backward_i = i;
396 gl_state.b_property = i->position - gl_state.offset;
398 else
400 gl_state.forward_i = i;
401 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
405 if (!EQ (tmp_table, gl_state.old_prop))
407 gl_state.current_syntax_table = tmp_table;
408 gl_state.old_prop = tmp_table;
409 if (EQ (Fsyntax_table_p (tmp_table), Qt))
411 gl_state.use_global = 0;
413 else if (CONSP (tmp_table))
415 gl_state.use_global = 1;
416 gl_state.global_code = tmp_table;
418 else
420 gl_state.use_global = 0;
421 gl_state.current_syntax_table = BVAR (current_buffer, syntax_table);
425 while (i)
427 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
429 if (count > 0)
431 gl_state.e_property = i->position - gl_state.offset;
432 gl_state.forward_i = i;
434 else
436 gl_state.b_property
437 = i->position + LENGTH (i) - gl_state.offset;
438 gl_state.backward_i = i;
440 return;
442 else if (cnt == INTERVALS_AT_ONCE)
444 if (count > 0)
446 gl_state.e_property
447 = i->position + LENGTH (i) - gl_state.offset
448 /* e_property at EOB is not set to ZV but to ZV+1, so that
449 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
450 having to check eob between the two. */
451 + (next_interval (i) ? 0 : 1);
452 gl_state.forward_i = i;
454 else
456 gl_state.b_property = i->position - gl_state.offset;
457 gl_state.backward_i = i;
459 return;
461 cnt++;
462 i = count > 0 ? next_interval (i) : previous_interval (i);
464 eassert (i == NULL); /* This property goes to the end. */
465 if (count > 0)
466 gl_state.e_property = gl_state.stop;
467 else
468 gl_state.b_property = gl_state.start;
471 /* Returns true if char at CHARPOS is quoted.
472 Global syntax-table data should be set up already to be good at CHARPOS
473 or after. On return global syntax data is good for lookup at CHARPOS. */
475 static bool
476 char_quoted (ptrdiff_t charpos, ptrdiff_t bytepos)
478 enum syntaxcode code;
479 ptrdiff_t beg = BEGV;
480 bool quoted = 0;
481 ptrdiff_t orig = charpos;
483 while (charpos > beg)
485 int c;
486 DEC_BOTH (charpos, bytepos);
488 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
489 c = FETCH_CHAR_AS_MULTIBYTE (bytepos);
490 code = SYNTAX (c);
491 if (! (code == Scharquote || code == Sescape))
492 break;
494 quoted = !quoted;
497 UPDATE_SYNTAX_TABLE (orig);
498 return quoted;
501 /* Return the bytepos one character before BYTEPOS.
502 We assume that BYTEPOS is not at the start of the buffer. */
504 static ptrdiff_t
505 dec_bytepos (ptrdiff_t bytepos)
507 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
508 return bytepos - 1;
510 DEC_POS (bytepos);
511 return bytepos;
514 /* Return a defun-start position before POS and not too far before.
515 It should be the last one before POS, or nearly the last.
517 When open_paren_in_column_0_is_defun_start is nonzero,
518 only the beginning of the buffer is treated as a defun-start.
520 We record the information about where the scan started
521 and what its result was, so that another call in the same area
522 can return the same value very quickly.
524 There is no promise at which position the global syntax data is
525 valid on return from the subroutine, so the caller should explicitly
526 update the global data. */
528 static ptrdiff_t
529 find_defun_start (ptrdiff_t pos, ptrdiff_t pos_byte)
531 ptrdiff_t opoint = PT, opoint_byte = PT_BYTE;
533 /* Use previous finding, if it's valid and applies to this inquiry. */
534 if (current_buffer == find_start_buffer
535 /* Reuse the defun-start even if POS is a little farther on.
536 POS might be in the next defun, but that's ok.
537 Our value may not be the best possible, but will still be usable. */
538 && pos <= find_start_pos + 1000
539 && pos >= find_start_value
540 && BEGV == find_start_begv
541 && MODIFF == find_start_modiff)
542 return find_start_value;
544 if (!open_paren_in_column_0_is_defun_start)
546 find_start_value = BEGV;
547 find_start_value_byte = BEGV_BYTE;
548 goto found;
551 /* Back up to start of line. */
552 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
554 /* We optimize syntax-table lookup for rare updates. Thus we accept
555 only those `^\s(' which are good in global _and_ text-property
556 syntax-tables. */
557 SETUP_BUFFER_SYNTAX_TABLE ();
558 while (PT > BEGV)
560 int c;
562 /* Open-paren at start of line means we may have found our
563 defun-start. */
564 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
565 if (SYNTAX (c) == Sopen)
567 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
568 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
569 if (SYNTAX (c) == Sopen)
570 break;
571 /* Now fallback to the default value. */
572 SETUP_BUFFER_SYNTAX_TABLE ();
574 /* Move to beg of previous line. */
575 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
578 /* Record what we found, for the next try. */
579 find_start_value = PT;
580 find_start_value_byte = PT_BYTE;
581 TEMP_SET_PT_BOTH (opoint, opoint_byte);
583 found:
584 find_start_buffer = current_buffer;
585 find_start_modiff = MODIFF;
586 find_start_begv = BEGV;
587 find_start_pos = pos;
589 return find_start_value;
592 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
594 static bool
595 prev_char_comend_first (ptrdiff_t pos, ptrdiff_t pos_byte)
597 int c;
598 bool val;
600 DEC_BOTH (pos, pos_byte);
601 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
602 c = FETCH_CHAR (pos_byte);
603 val = SYNTAX_COMEND_FIRST (c);
604 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
605 return val;
608 /* Check whether charpos FROM is at the end of a comment.
609 FROM_BYTE is the bytepos corresponding to FROM.
610 Do not move back before STOP.
612 Return true if we find a comment ending at FROM/FROM_BYTE.
614 If successful, store the charpos of the comment's beginning
615 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
617 Global syntax data remains valid for backward search starting at
618 the returned value (or at FROM, if the search was not successful). */
620 static bool
621 back_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop,
622 bool comnested, int comstyle, ptrdiff_t *charpos_ptr,
623 ptrdiff_t *bytepos_ptr)
625 /* Look back, counting the parity of string-quotes,
626 and recording the comment-starters seen.
627 When we reach a safe place, assume that's not in a string;
628 then step the main scan to the earliest comment-starter seen
629 an even number of string quotes away from the safe place.
631 OFROM[I] is position of the earliest comment-starter seen
632 which is I+2X quotes from the comment-end.
633 PARITY is current parity of quotes from the comment end. */
634 int string_style = -1; /* Presumed outside of any string. */
635 bool string_lossage = 0;
636 /* Not a real lossage: indicates that we have passed a matching comment
637 starter plus a non-matching comment-ender, meaning that any matching
638 comment-starter we might see later could be a false positive (hidden
639 inside another comment).
640 Test case: { a (* b } c (* d *) */
641 bool comment_lossage = 0;
642 ptrdiff_t comment_end = from;
643 ptrdiff_t comment_end_byte = from_byte;
644 ptrdiff_t comstart_pos = 0;
645 ptrdiff_t comstart_byte IF_LINT (= 0);
646 /* Place where the containing defun starts,
647 or 0 if we didn't come across it yet. */
648 ptrdiff_t defun_start = 0;
649 ptrdiff_t defun_start_byte = 0;
650 enum syntaxcode code;
651 ptrdiff_t nesting = 1; /* current comment nesting */
652 int c;
653 int syntax = 0;
655 /* FIXME: A }} comment-ender style leads to incorrect behavior
656 in the case of {{ c }}} because we ignore the last two chars which are
657 assumed to be comment-enders although they aren't. */
659 /* At beginning of range to scan, we're outside of strings;
660 that determines quote parity to the comment-end. */
661 while (from != stop)
663 ptrdiff_t temp_byte;
664 int prev_syntax;
665 bool com2start, com2end, comstart;
667 /* Move back and examine a character. */
668 DEC_BOTH (from, from_byte);
669 UPDATE_SYNTAX_TABLE_BACKWARD (from);
671 prev_syntax = syntax;
672 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
673 syntax = SYNTAX_WITH_FLAGS (c);
674 code = SYNTAX (c);
676 /* Check for 2-char comment markers. */
677 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
678 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
679 && (comstyle
680 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax, syntax))
681 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
682 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
683 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
684 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
685 comstart = (com2start || code == Scomment);
687 /* Nasty cases with overlapping 2-char comment markers:
688 - snmp-mode: -- c -- foo -- c --
689 --- c --
690 ------ c --
691 - c-mode: *||*
692 |* *|* *|
693 |*| |* |*|
694 /// */
696 /* If a 2-char comment sequence partly overlaps with another,
697 we don't try to be clever. E.g. |*| in C, or }% in modes that
698 have %..\n and %{..}%. */
699 if (from > stop && (com2end || comstart))
701 ptrdiff_t next = from, next_byte = from_byte;
702 int next_c, next_syntax;
703 DEC_BOTH (next, next_byte);
704 UPDATE_SYNTAX_TABLE_BACKWARD (next);
705 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
706 next_syntax = SYNTAX_WITH_FLAGS (next_c);
707 if (((comstart || comnested)
708 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
709 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
710 || ((com2end || comnested)
711 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
712 && (comstyle
713 == SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_syntax))
714 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
715 goto lossage;
716 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
719 if (com2start && comstart_pos == 0)
720 /* We're looking at a comment starter. But it might be a comment
721 ender as well (see snmp-mode). The first time we see one, we
722 need to consider it as a comment starter,
723 and the subsequent times as a comment ender. */
724 com2end = 0;
726 /* Turn a 2-char comment sequences into the appropriate syntax. */
727 if (com2end)
728 code = Sendcomment;
729 else if (com2start)
730 code = Scomment;
731 /* Ignore comment starters of a different style. */
732 else if (code == Scomment
733 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0)
734 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
735 continue;
737 /* Ignore escaped characters, except comment-enders. */
738 if (code != Sendcomment && char_quoted (from, from_byte))
739 continue;
741 switch (code)
743 case Sstring_fence:
744 case Scomment_fence:
745 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
746 case Sstring:
747 /* Track parity of quotes. */
748 if (string_style == -1)
749 /* Entering a string. */
750 string_style = c;
751 else if (string_style == c)
752 /* Leaving the string. */
753 string_style = -1;
754 else
755 /* If we have two kinds of string delimiters.
756 There's no way to grok this scanning backwards. */
757 string_lossage = 1;
758 break;
760 case Scomment:
761 /* We've already checked that it is the relevant comstyle. */
762 if (string_style != -1 || comment_lossage || string_lossage)
763 /* There are odd string quotes involved, so let's be careful.
764 Test case in Pascal: " { " a { " } */
765 goto lossage;
767 if (!comnested)
769 /* Record best comment-starter so far. */
770 comstart_pos = from;
771 comstart_byte = from_byte;
773 else if (--nesting <= 0)
774 /* nested comments have to be balanced, so we don't need to
775 keep looking for earlier ones. We use here the same (slightly
776 incorrect) reasoning as below: since it is followed by uniform
777 paired string quotes, this comment-start has to be outside of
778 strings, else the comment-end itself would be inside a string. */
779 goto done;
780 break;
782 case Sendcomment:
783 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == comstyle
784 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
785 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
786 /* This is the same style of comment ender as ours. */
788 if (comnested)
789 nesting++;
790 else
791 /* Anything before that can't count because it would match
792 this comment-ender rather than ours. */
793 from = stop; /* Break out of the loop. */
795 else if (comstart_pos != 0 || c != '\n')
796 /* We're mixing comment styles here, so we'd better be careful.
797 The (comstart_pos != 0 || c != '\n') check is not quite correct
798 (we should just always set comment_lossage), but removing it
799 would imply that any multiline comment in C would go through
800 lossage, which seems overkill.
801 The failure should only happen in the rare cases such as
802 { (* } *) */
803 comment_lossage = 1;
804 break;
806 case Sopen:
807 /* Assume a defun-start point is outside of strings. */
808 if (open_paren_in_column_0_is_defun_start
809 && (from == stop
810 || (temp_byte = dec_bytepos (from_byte),
811 FETCH_CHAR (temp_byte) == '\n')))
813 defun_start = from;
814 defun_start_byte = from_byte;
815 from = stop; /* Break out of the loop. */
817 break;
819 default:
820 break;
824 if (comstart_pos == 0)
826 from = comment_end;
827 from_byte = comment_end_byte;
828 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
830 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
831 or `done'), then we've found the beginning of the non-nested comment. */
832 else if (1) /* !comnested */
834 from = comstart_pos;
835 from_byte = comstart_byte;
836 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
838 else lossage:
840 struct lisp_parse_state state;
841 bool adjusted = true;
842 /* We had two kinds of string delimiters mixed up
843 together. Decode this going forwards.
844 Scan fwd from a known safe place (beginning-of-defun)
845 to the one in question; this records where we
846 last passed a comment starter. */
847 /* If we did not already find the defun start, find it now. */
848 if (defun_start == 0)
850 defun_start = find_defun_start (comment_end, comment_end_byte);
851 defun_start_byte = find_start_value_byte;
852 adjusted = (defun_start > BEGV);
856 scan_sexps_forward (&state,
857 defun_start, defun_start_byte,
858 comment_end, TYPE_MINIMUM (EMACS_INT),
859 0, Qnil, 0);
860 defun_start = comment_end;
861 if (!adjusted)
863 adjusted = true;
864 find_start_value
865 = CONSP (state.levelstarts) ? XINT (XCAR (state.levelstarts))
866 : state.thislevelstart >= 0 ? state.thislevelstart
867 : find_start_value;
868 find_start_value_byte = CHAR_TO_BYTE (find_start_value);
871 if (state.incomment == (comnested ? 1 : -1)
872 && state.comstyle == comstyle)
873 from = state.comstr_start;
874 else
876 from = comment_end;
877 if (state.incomment)
878 /* If comment_end is inside some other comment, maybe ours
879 is nested, so we need to try again from within the
880 surrounding comment. Example: { a (* " *) */
882 /* FIXME: We should advance by one or two chars. */
883 defun_start = state.comstr_start + 2;
884 defun_start_byte = CHAR_TO_BYTE (defun_start);
887 } while (defun_start < comment_end);
889 from_byte = CHAR_TO_BYTE (from);
890 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
893 done:
894 *charpos_ptr = from;
895 *bytepos_ptr = from_byte;
897 return from != comment_end;
900 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
901 doc: /* Return t if OBJECT is a syntax table.
902 Currently, any char-table counts as a syntax table. */)
903 (Lisp_Object object)
905 if (CHAR_TABLE_P (object)
906 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
907 return Qt;
908 return Qnil;
911 static void
912 check_syntax_table (Lisp_Object obj)
914 CHECK_TYPE (CHAR_TABLE_P (obj) && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table),
915 Qsyntax_table_p, obj);
918 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
919 doc: /* Return the current syntax table.
920 This is the one specified by the current buffer. */)
921 (void)
923 return BVAR (current_buffer, syntax_table);
926 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
927 Sstandard_syntax_table, 0, 0, 0,
928 doc: /* Return the standard syntax table.
929 This is the one used for new buffers. */)
930 (void)
932 return Vstandard_syntax_table;
935 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
936 doc: /* Construct a new syntax table and return it.
937 It is a copy of the TABLE, which defaults to the standard syntax table. */)
938 (Lisp_Object table)
940 Lisp_Object copy;
942 if (!NILP (table))
943 check_syntax_table (table);
944 else
945 table = Vstandard_syntax_table;
947 copy = Fcopy_sequence (table);
949 /* Only the standard syntax table should have a default element.
950 Other syntax tables should inherit from parents instead. */
951 set_char_table_defalt (copy, Qnil);
953 /* Copied syntax tables should all have parents.
954 If we copied one with no parent, such as the standard syntax table,
955 use the standard syntax table as the copy's parent. */
956 if (NILP (XCHAR_TABLE (copy)->parent))
957 Fset_char_table_parent (copy, Vstandard_syntax_table);
958 return copy;
961 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
962 doc: /* Select a new syntax table for the current buffer.
963 One argument, a syntax table. */)
964 (Lisp_Object table)
966 int idx;
967 check_syntax_table (table);
968 bset_syntax_table (current_buffer, table);
969 /* Indicate that this buffer now has a specified syntax table. */
970 idx = PER_BUFFER_VAR_IDX (syntax_table);
971 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
972 return table;
975 /* Convert a letter which signifies a syntax code
976 into the code it signifies.
977 This is used by modify-syntax-entry, and other things. */
979 unsigned char const syntax_spec_code[0400] =
980 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
981 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
982 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
983 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
984 Swhitespace, Scomment_fence, Sstring, 0377, Smath, 0377, 0377, Squote,
985 Sopen, Sclose, 0377, 0377, 0377, Swhitespace, Spunct, Scharquote,
986 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
987 0377, 0377, 0377, 0377, Scomment, 0377, Sendcomment, 0377,
988 Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
989 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
990 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword,
991 0377, 0377, 0377, 0377, Sescape, 0377, 0377, Ssymbol,
992 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
993 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
994 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword,
995 0377, 0377, 0377, 0377, Sstring_fence, 0377, 0377, 0377
998 /* Indexed by syntax code, give the letter that describes it. */
1000 char const syntax_code_spec[16] =
1002 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
1003 '!', '|'
1006 /* Indexed by syntax code, give the object (cons of syntax code and
1007 nil) to be stored in syntax table. Since these objects can be
1008 shared among syntax tables, we generate them in advance. By
1009 sharing objects, the function `describe-syntax' can give a more
1010 compact listing. */
1011 static Lisp_Object Vsyntax_code_object;
1014 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
1015 doc: /* Return the syntax code of CHARACTER, described by a character.
1016 For example, if CHARACTER is a word constituent, the
1017 character `w' (119) is returned.
1018 The characters that correspond to various syntax codes
1019 are listed in the documentation of `modify-syntax-entry'. */)
1020 (Lisp_Object character)
1022 int char_int;
1023 CHECK_CHARACTER (character);
1024 char_int = XINT (character);
1025 SETUP_BUFFER_SYNTAX_TABLE ();
1026 return make_number (syntax_code_spec[SYNTAX (char_int)]);
1029 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
1030 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
1031 (Lisp_Object character)
1033 int char_int;
1034 enum syntaxcode code;
1035 CHECK_CHARACTER (character);
1036 char_int = XINT (character);
1037 SETUP_BUFFER_SYNTAX_TABLE ();
1038 code = SYNTAX (char_int);
1039 if (code == Sopen || code == Sclose)
1040 return SYNTAX_MATCH (char_int);
1041 return Qnil;
1044 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
1045 doc: /* Convert a syntax descriptor STRING into a raw syntax descriptor.
1046 STRING should be a string of the form allowed as argument of
1047 `modify-syntax-entry'. The return value is a raw syntax descriptor: a
1048 cons cell \(CODE . MATCHING-CHAR) which can be used, for example, as
1049 the value of a `syntax-table' text property. */)
1050 (Lisp_Object string)
1052 const unsigned char *p;
1053 int val;
1054 Lisp_Object match;
1056 CHECK_STRING (string);
1058 p = SDATA (string);
1059 val = syntax_spec_code[*p++];
1060 if (val == 0377)
1061 error ("Invalid syntax description letter: %c", p[-1]);
1063 if (val == Sinherit)
1064 return Qnil;
1066 if (*p)
1068 int len;
1069 int character = STRING_CHAR_AND_LENGTH (p, len);
1070 XSETINT (match, character);
1071 if (XFASTINT (match) == ' ')
1072 match = Qnil;
1073 p += len;
1075 else
1076 match = Qnil;
1078 while (*p)
1079 switch (*p++)
1081 case '1':
1082 val |= 1 << 16;
1083 break;
1085 case '2':
1086 val |= 1 << 17;
1087 break;
1089 case '3':
1090 val |= 1 << 18;
1091 break;
1093 case '4':
1094 val |= 1 << 19;
1095 break;
1097 case 'p':
1098 val |= 1 << 20;
1099 break;
1101 case 'b':
1102 val |= 1 << 21;
1103 break;
1105 case 'n':
1106 val |= 1 << 22;
1107 break;
1109 case 'c':
1110 val |= 1 << 23;
1111 break;
1114 if (val < ASIZE (Vsyntax_code_object) && NILP (match))
1115 return AREF (Vsyntax_code_object, val);
1116 else
1117 /* Since we can't use a shared object, let's make a new one. */
1118 return Fcons (make_number (val), match);
1121 /* I really don't know why this is interactive
1122 help-form should at least be made useful whilst reading the second arg. */
1123 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
1124 "cSet syntax for character: \nsSet syntax for %s to: ",
1125 doc: /* Set syntax for character CHAR according to string NEWENTRY.
1126 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1127 the current buffer's syntax table.
1128 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1129 in the range MIN to MAX are changed.
1130 The first character of NEWENTRY should be one of the following:
1131 Space or - whitespace syntax. w word constituent.
1132 _ symbol constituent. . punctuation.
1133 ( open-parenthesis. ) close-parenthesis.
1134 " string quote. \\ escape.
1135 $ paired delimiter. ' expression quote or prefix operator.
1136 < comment starter. > comment ender.
1137 / character-quote. @ inherit from parent table.
1138 | generic string fence. ! generic comment fence.
1140 Only single-character comment start and end sequences are represented thus.
1141 Two-character sequences are represented as described below.
1142 The second character of NEWENTRY is the matching parenthesis,
1143 used only if the first character is `(' or `)'.
1144 Any additional characters are flags.
1145 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1146 1 means CHAR is the start of a two-char comment start sequence.
1147 2 means CHAR is the second character of such a sequence.
1148 3 means CHAR is the start of a two-char comment end sequence.
1149 4 means CHAR is the second character of such a sequence.
1151 There can be several orthogonal comment sequences. This is to support
1152 language modes such as C++. By default, all comment sequences are of style
1153 a, but you can set the comment sequence style to b (on the second character
1154 of a comment-start, and the first character of a comment-end sequence) and/or
1155 c (on any of its chars) using this flag:
1156 b means CHAR is part of comment sequence b.
1157 c means CHAR is part of comment sequence c.
1158 n means CHAR is part of a nestable comment sequence.
1160 p means CHAR is a prefix character for `backward-prefix-chars';
1161 such characters are treated as whitespace when they occur
1162 between expressions.
1163 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1164 (Lisp_Object c, Lisp_Object newentry, Lisp_Object syntax_table)
1166 if (CONSP (c))
1168 CHECK_CHARACTER_CAR (c);
1169 CHECK_CHARACTER_CDR (c);
1171 else
1172 CHECK_CHARACTER (c);
1174 if (NILP (syntax_table))
1175 syntax_table = BVAR (current_buffer, syntax_table);
1176 else
1177 check_syntax_table (syntax_table);
1179 newentry = Fstring_to_syntax (newentry);
1180 if (CONSP (c))
1181 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1182 else
1183 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
1185 /* We clear the regexp cache, since character classes can now have
1186 different values from those in the compiled regexps.*/
1187 clear_regexp_cache ();
1189 return Qnil;
1192 /* Dump syntax table to buffer in human-readable format */
1194 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1195 Sinternal_describe_syntax_value, 1, 1, 0,
1196 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1197 (Lisp_Object syntax)
1199 int code, syntax_code;
1200 bool start1, start2, end1, end2, prefix, comstyleb, comstylec, comnested;
1201 char str[2];
1202 Lisp_Object first, match_lisp, value = syntax;
1204 if (NILP (value))
1206 insert_string ("default");
1207 return syntax;
1210 if (CHAR_TABLE_P (value))
1212 insert_string ("deeper char-table ...");
1213 return syntax;
1216 if (!CONSP (value))
1218 insert_string ("invalid");
1219 return syntax;
1222 first = XCAR (value);
1223 match_lisp = XCDR (value);
1225 if (!INTEGERP (first) || !(NILP (match_lisp) || CHARACTERP (match_lisp)))
1227 insert_string ("invalid");
1228 return syntax;
1231 syntax_code = XINT (first) & INT_MAX;
1232 code = syntax_code & 0377;
1233 start1 = SYNTAX_FLAGS_COMSTART_FIRST (syntax_code);
1234 start2 = SYNTAX_FLAGS_COMSTART_SECOND (syntax_code);;
1235 end1 = SYNTAX_FLAGS_COMEND_FIRST (syntax_code);
1236 end2 = SYNTAX_FLAGS_COMEND_SECOND (syntax_code);
1237 prefix = SYNTAX_FLAGS_PREFIX (syntax_code);
1238 comstyleb = SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code);
1239 comstylec = SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code);
1240 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax_code);
1242 if (Smax <= code)
1244 insert_string ("invalid");
1245 return syntax;
1248 str[0] = syntax_code_spec[code], str[1] = 0;
1249 insert (str, 1);
1251 if (NILP (match_lisp))
1252 insert (" ", 1);
1253 else
1254 insert_char (XINT (match_lisp));
1256 if (start1)
1257 insert ("1", 1);
1258 if (start2)
1259 insert ("2", 1);
1261 if (end1)
1262 insert ("3", 1);
1263 if (end2)
1264 insert ("4", 1);
1266 if (prefix)
1267 insert ("p", 1);
1268 if (comstyleb)
1269 insert ("b", 1);
1270 if (comstylec)
1271 insert ("c", 1);
1272 if (comnested)
1273 insert ("n", 1);
1275 insert_string ("\twhich means: ");
1277 switch (code)
1279 case Swhitespace:
1280 insert_string ("whitespace"); break;
1281 case Spunct:
1282 insert_string ("punctuation"); break;
1283 case Sword:
1284 insert_string ("word"); break;
1285 case Ssymbol:
1286 insert_string ("symbol"); break;
1287 case Sopen:
1288 insert_string ("open"); break;
1289 case Sclose:
1290 insert_string ("close"); break;
1291 case Squote:
1292 insert_string ("prefix"); break;
1293 case Sstring:
1294 insert_string ("string"); break;
1295 case Smath:
1296 insert_string ("math"); break;
1297 case Sescape:
1298 insert_string ("escape"); break;
1299 case Scharquote:
1300 insert_string ("charquote"); break;
1301 case Scomment:
1302 insert_string ("comment"); break;
1303 case Sendcomment:
1304 insert_string ("endcomment"); break;
1305 case Sinherit:
1306 insert_string ("inherit"); break;
1307 case Scomment_fence:
1308 insert_string ("comment fence"); break;
1309 case Sstring_fence:
1310 insert_string ("string fence"); break;
1311 default:
1312 insert_string ("invalid");
1313 return syntax;
1316 if (!NILP (match_lisp))
1318 insert_string (", matches ");
1319 insert_char (XINT (match_lisp));
1322 if (start1)
1323 insert_string (",\n\t is the first character of a comment-start sequence");
1324 if (start2)
1325 insert_string (",\n\t is the second character of a comment-start sequence");
1327 if (end1)
1328 insert_string (",\n\t is the first character of a comment-end sequence");
1329 if (end2)
1330 insert_string (",\n\t is the second character of a comment-end sequence");
1331 if (comstyleb)
1332 insert_string (" (comment style b)");
1333 if (comstylec)
1334 insert_string (" (comment style c)");
1335 if (comnested)
1336 insert_string (" (nestable)");
1338 if (prefix)
1339 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1341 return syntax;
1344 /* Return the position across COUNT words from FROM.
1345 If that many words cannot be found before the end of the buffer, return 0.
1346 COUNT negative means scan backward and stop at word beginning. */
1348 ptrdiff_t
1349 scan_words (register ptrdiff_t from, register EMACS_INT count)
1351 register ptrdiff_t beg = BEGV;
1352 register ptrdiff_t end = ZV;
1353 register ptrdiff_t from_byte = CHAR_TO_BYTE (from);
1354 register enum syntaxcode code;
1355 int ch0, ch1;
1356 Lisp_Object func, pos;
1358 immediate_quit = 1;
1359 QUIT;
1361 SETUP_SYNTAX_TABLE (from, count);
1363 while (count > 0)
1365 while (1)
1367 if (from == end)
1369 immediate_quit = 0;
1370 return 0;
1372 UPDATE_SYNTAX_TABLE_FORWARD (from);
1373 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1374 code = SYNTAX (ch0);
1375 INC_BOTH (from, from_byte);
1376 if (words_include_escapes
1377 && (code == Sescape || code == Scharquote))
1378 break;
1379 if (code == Sword)
1380 break;
1382 /* Now CH0 is a character which begins a word and FROM is the
1383 position of the next character. */
1384 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
1385 if (! NILP (Ffboundp (func)))
1387 pos = call2 (func, make_number (from - 1), make_number (end));
1388 if (INTEGERP (pos) && from < XINT (pos) && XINT (pos) <= ZV)
1390 from = XINT (pos);
1391 from_byte = CHAR_TO_BYTE (from);
1394 else
1396 while (1)
1398 if (from == end) break;
1399 UPDATE_SYNTAX_TABLE_FORWARD (from);
1400 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1401 code = SYNTAX (ch1);
1402 if ((code != Sword
1403 && (! words_include_escapes
1404 || (code != Sescape && code != Scharquote)))
1405 || word_boundary_p (ch0, ch1))
1406 break;
1407 INC_BOTH (from, from_byte);
1408 ch0 = ch1;
1411 count--;
1413 while (count < 0)
1415 while (1)
1417 if (from == beg)
1419 immediate_quit = 0;
1420 return 0;
1422 DEC_BOTH (from, from_byte);
1423 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1424 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1425 code = SYNTAX (ch1);
1426 if (words_include_escapes
1427 && (code == Sescape || code == Scharquote))
1428 break;
1429 if (code == Sword)
1430 break;
1432 /* Now CH1 is a character which ends a word and FROM is the
1433 position of it. */
1434 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
1435 if (! NILP (Ffboundp (func)))
1437 pos = call2 (func, make_number (from), make_number (beg));
1438 if (INTEGERP (pos) && BEGV <= XINT (pos) && XINT (pos) < from)
1440 from = XINT (pos);
1441 from_byte = CHAR_TO_BYTE (from);
1444 else
1446 while (1)
1448 if (from == beg)
1449 break;
1450 DEC_BOTH (from, from_byte);
1451 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1452 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1453 code = SYNTAX (ch0);
1454 if ((code != Sword
1455 && (! words_include_escapes
1456 || (code != Sescape && code != Scharquote)))
1457 || word_boundary_p (ch0, ch1))
1459 INC_BOTH (from, from_byte);
1460 break;
1462 ch1 = ch0;
1465 count++;
1468 immediate_quit = 0;
1470 return from;
1473 DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p",
1474 doc: /* Move point forward ARG words (backward if ARG is negative).
1475 If ARG is omitted or nil, move point forward one word.
1476 Normally returns t.
1477 If an edge of the buffer or a field boundary is reached, point is left there
1478 and the function returns nil. Field boundaries are not noticed if
1479 `inhibit-field-text-motion' is non-nil. */)
1480 (Lisp_Object arg)
1482 Lisp_Object tmp;
1483 ptrdiff_t orig_val, val;
1485 if (NILP (arg))
1486 XSETFASTINT (arg, 1);
1487 else
1488 CHECK_NUMBER (arg);
1490 val = orig_val = scan_words (PT, XINT (arg));
1491 if (! orig_val)
1492 val = XINT (arg) > 0 ? ZV : BEGV;
1494 /* Avoid jumping out of an input field. */
1495 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1496 Qnil, Qnil, Qnil);
1497 val = XFASTINT (tmp);
1499 SET_PT (val);
1500 return val == orig_val ? Qt : Qnil;
1503 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1504 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1505 STRING is like the inside of a `[...]' in a regular expression
1506 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1507 (but not at the end of a range; quoting is never needed there).
1508 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1509 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1510 Char classes, e.g. `[:alpha:]', are supported.
1512 Returns the distance traveled, either zero or positive. */)
1513 (Lisp_Object string, Lisp_Object lim)
1515 return skip_chars (1, string, lim, 1);
1518 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1519 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1520 See `skip-chars-forward' for details.
1521 Returns the distance traveled, either zero or negative. */)
1522 (Lisp_Object string, Lisp_Object lim)
1524 return skip_chars (0, string, lim, 1);
1527 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1528 doc: /* Move point forward across chars in specified syntax classes.
1529 SYNTAX is a string of syntax code characters.
1530 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1531 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1532 This function returns the distance traveled, either zero or positive. */)
1533 (Lisp_Object syntax, Lisp_Object lim)
1535 return skip_syntaxes (1, syntax, lim);
1538 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1539 doc: /* Move point backward across chars in specified syntax classes.
1540 SYNTAX is a string of syntax code characters.
1541 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1542 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1543 This function returns either zero or a negative number, and the absolute value
1544 of this is the distance traveled. */)
1545 (Lisp_Object syntax, Lisp_Object lim)
1547 return skip_syntaxes (0, syntax, lim);
1550 static Lisp_Object
1551 skip_chars (bool forwardp, Lisp_Object string, Lisp_Object lim,
1552 bool handle_iso_classes)
1554 int c;
1555 char fastmap[0400];
1556 /* Store the ranges of non-ASCII characters. */
1557 int *char_ranges IF_LINT (= NULL);
1558 int n_char_ranges = 0;
1559 bool negate = 0;
1560 ptrdiff_t i, i_byte;
1561 /* True if the current buffer is multibyte and the region contains
1562 non-ASCII chars. */
1563 bool multibyte;
1564 /* True if STRING is multibyte and it contains non-ASCII chars. */
1565 bool string_multibyte;
1566 ptrdiff_t size_byte;
1567 const unsigned char *str;
1568 int len;
1569 Lisp_Object iso_classes;
1571 CHECK_STRING (string);
1572 iso_classes = Qnil;
1574 if (NILP (lim))
1575 XSETINT (lim, forwardp ? ZV : BEGV);
1576 else
1577 CHECK_NUMBER_COERCE_MARKER (lim);
1579 /* In any case, don't allow scan outside bounds of buffer. */
1580 if (XINT (lim) > ZV)
1581 XSETFASTINT (lim, ZV);
1582 if (XINT (lim) < BEGV)
1583 XSETFASTINT (lim, BEGV);
1585 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1586 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1587 string_multibyte = SBYTES (string) > SCHARS (string);
1589 memset (fastmap, 0, sizeof fastmap);
1591 str = SDATA (string);
1592 size_byte = SBYTES (string);
1594 i_byte = 0;
1595 if (i_byte < size_byte
1596 && SREF (string, 0) == '^')
1598 negate = 1; i_byte++;
1601 /* Find the characters specified and set their elements of fastmap.
1602 Handle backslashes and ranges specially.
1604 If STRING contains non-ASCII characters, setup char_ranges for
1605 them and use fastmap only for their leading codes. */
1607 if (! string_multibyte)
1609 bool string_has_eight_bit = 0;
1611 /* At first setup fastmap. */
1612 while (i_byte < size_byte)
1614 c = str[i_byte++];
1616 if (handle_iso_classes && c == '['
1617 && i_byte < size_byte
1618 && str[i_byte] == ':')
1620 const unsigned char *class_beg = str + i_byte + 1;
1621 const unsigned char *class_end = class_beg;
1622 const unsigned char *class_limit = str + size_byte - 2;
1623 /* Leave room for the null. */
1624 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1625 re_wctype_t cc;
1627 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1628 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1630 while (class_end < class_limit
1631 && *class_end >= 'a' && *class_end <= 'z')
1632 class_end++;
1634 if (class_end == class_beg
1635 || *class_end != ':' || class_end[1] != ']')
1636 goto not_a_class_name;
1638 memcpy (class_name, class_beg, class_end - class_beg);
1639 class_name[class_end - class_beg] = 0;
1641 cc = re_wctype (class_name);
1642 if (cc == 0)
1643 error ("Invalid ISO C character class");
1645 iso_classes = Fcons (make_number (cc), iso_classes);
1647 i_byte = class_end + 2 - str;
1648 continue;
1651 not_a_class_name:
1652 if (c == '\\')
1654 if (i_byte == size_byte)
1655 break;
1657 c = str[i_byte++];
1659 /* Treat `-' as range character only if another character
1660 follows. */
1661 if (i_byte + 1 < size_byte
1662 && str[i_byte] == '-')
1664 int c2;
1666 /* Skip over the dash. */
1667 i_byte++;
1669 /* Get the end of the range. */
1670 c2 = str[i_byte++];
1671 if (c2 == '\\'
1672 && i_byte < size_byte)
1673 c2 = str[i_byte++];
1675 if (c <= c2)
1677 int lim2 = c2 + 1;
1678 while (c < lim2)
1679 fastmap[c++] = 1;
1680 if (! ASCII_CHAR_P (c2))
1681 string_has_eight_bit = 1;
1684 else
1686 fastmap[c] = 1;
1687 if (! ASCII_CHAR_P (c))
1688 string_has_eight_bit = 1;
1692 /* If the current range is multibyte and STRING contains
1693 eight-bit chars, arrange fastmap and setup char_ranges for
1694 the corresponding multibyte chars. */
1695 if (multibyte && string_has_eight_bit)
1697 char *p1;
1698 char himap[0200 + 1];
1699 memcpy (himap, fastmap + 0200, 0200);
1700 himap[0200] = 0;
1701 memset (fastmap + 0200, 0, 0200);
1702 char_ranges = alloca (sizeof *char_ranges * 128 * 2);
1703 i = 0;
1705 while ((p1 = memchr (himap + i, 1, 0200 - i)))
1707 /* Deduce the next range C..C2 from the next clump of 1s
1708 in HIMAP starting with &HIMAP[I]. HIMAP is the high
1709 order half of the old FASTMAP. */
1710 int c2, leading_code;
1711 i = p1 - himap;
1712 c = BYTE8_TO_CHAR (i + 0200);
1713 i += strlen (p1);
1714 c2 = BYTE8_TO_CHAR (i + 0200 - 1);
1716 char_ranges[n_char_ranges++] = c;
1717 char_ranges[n_char_ranges++] = c2;
1718 leading_code = CHAR_LEADING_CODE (c);
1719 memset (fastmap + leading_code, 1,
1720 CHAR_LEADING_CODE (c2) - leading_code + 1);
1724 else /* STRING is multibyte */
1726 char_ranges = alloca (sizeof *char_ranges * SCHARS (string) * 2);
1728 while (i_byte < size_byte)
1730 int leading_code = str[i_byte];
1731 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1732 i_byte += len;
1734 if (handle_iso_classes && c == '['
1735 && i_byte < size_byte
1736 && STRING_CHAR (str + i_byte) == ':')
1738 const unsigned char *class_beg = str + i_byte + 1;
1739 const unsigned char *class_end = class_beg;
1740 const unsigned char *class_limit = str + size_byte - 2;
1741 /* Leave room for the null. */
1742 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1743 re_wctype_t cc;
1745 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1746 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1748 while (class_end < class_limit
1749 && *class_end >= 'a' && *class_end <= 'z')
1750 class_end++;
1752 if (class_end == class_beg
1753 || *class_end != ':' || class_end[1] != ']')
1754 goto not_a_class_name_multibyte;
1756 memcpy (class_name, class_beg, class_end - class_beg);
1757 class_name[class_end - class_beg] = 0;
1759 cc = re_wctype (class_name);
1760 if (cc == 0)
1761 error ("Invalid ISO C character class");
1763 iso_classes = Fcons (make_number (cc), iso_classes);
1765 i_byte = class_end + 2 - str;
1766 continue;
1769 not_a_class_name_multibyte:
1770 if (c == '\\')
1772 if (i_byte == size_byte)
1773 break;
1775 leading_code = str[i_byte];
1776 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1777 i_byte += len;
1779 /* Treat `-' as range character only if another character
1780 follows. */
1781 if (i_byte + 1 < size_byte
1782 && str[i_byte] == '-')
1784 int c2, leading_code2;
1786 /* Skip over the dash. */
1787 i_byte++;
1789 /* Get the end of the range. */
1790 leading_code2 = str[i_byte];
1791 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1792 i_byte += len;
1794 if (c2 == '\\'
1795 && i_byte < size_byte)
1797 leading_code2 = str[i_byte];
1798 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1799 i_byte += len;
1802 if (c > c2)
1803 continue;
1804 if (ASCII_CHAR_P (c))
1806 while (c <= c2 && c < 0x80)
1807 fastmap[c++] = 1;
1808 leading_code = CHAR_LEADING_CODE (c);
1810 if (! ASCII_CHAR_P (c))
1812 int lim2 = leading_code2 + 1;
1813 while (leading_code < lim2)
1814 fastmap[leading_code++] = 1;
1815 if (c <= c2)
1817 char_ranges[n_char_ranges++] = c;
1818 char_ranges[n_char_ranges++] = c2;
1822 else
1824 if (ASCII_CHAR_P (c))
1825 fastmap[c] = 1;
1826 else
1828 fastmap[leading_code] = 1;
1829 char_ranges[n_char_ranges++] = c;
1830 char_ranges[n_char_ranges++] = c;
1835 /* If the current range is unibyte and STRING contains non-ASCII
1836 chars, arrange fastmap for the corresponding unibyte
1837 chars. */
1839 if (! multibyte && n_char_ranges > 0)
1841 memset (fastmap + 0200, 0, 0200);
1842 for (i = 0; i < n_char_ranges; i += 2)
1844 int c1 = char_ranges[i];
1845 int lim2 = char_ranges[i + 1] + 1;
1847 for (; c1 < lim2; c1++)
1849 int b = CHAR_TO_BYTE_SAFE (c1);
1850 if (b >= 0)
1851 fastmap[b] = 1;
1857 /* If ^ was the first character, complement the fastmap. */
1858 if (negate)
1860 if (! multibyte)
1861 for (i = 0; i < sizeof fastmap; i++)
1862 fastmap[i] ^= 1;
1863 else
1865 for (i = 0; i < 0200; i++)
1866 fastmap[i] ^= 1;
1867 /* All non-ASCII chars possibly match. */
1868 for (; i < sizeof fastmap; i++)
1869 fastmap[i] = 1;
1874 ptrdiff_t start_point = PT;
1875 ptrdiff_t pos = PT;
1876 ptrdiff_t pos_byte = PT_BYTE;
1877 unsigned char *p = PT_ADDR, *endp, *stop;
1879 if (forwardp)
1881 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1882 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1884 else
1886 endp = CHAR_POS_ADDR (XINT (lim));
1887 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1890 immediate_quit = 1;
1891 /* This code may look up syntax tables using functions that rely on the
1892 gl_state object. To make sure this object is not out of date,
1893 let's initialize it manually.
1894 We ignore syntax-table text-properties for now, since that's
1895 what we've done in the past. */
1896 SETUP_BUFFER_SYNTAX_TABLE ();
1897 if (forwardp)
1899 if (multibyte)
1900 while (1)
1902 int nbytes;
1904 if (p >= stop)
1906 if (p >= endp)
1907 break;
1908 p = GAP_END_ADDR;
1909 stop = endp;
1911 c = STRING_CHAR_AND_LENGTH (p, nbytes);
1912 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1914 if (negate)
1915 break;
1916 else
1917 goto fwd_ok;
1920 if (! fastmap[*p])
1921 break;
1922 if (! ASCII_CHAR_P (c))
1924 /* As we are looking at a multibyte character, we
1925 must look up the character in the table
1926 CHAR_RANGES. If there's no data in the table,
1927 that character is not what we want to skip. */
1929 /* The following code do the right thing even if
1930 n_char_ranges is zero (i.e. no data in
1931 CHAR_RANGES). */
1932 for (i = 0; i < n_char_ranges; i += 2)
1933 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1934 break;
1935 if (!(negate ^ (i < n_char_ranges)))
1936 break;
1938 fwd_ok:
1939 p += nbytes, pos++, pos_byte += nbytes;
1941 else
1942 while (1)
1944 if (p >= stop)
1946 if (p >= endp)
1947 break;
1948 p = GAP_END_ADDR;
1949 stop = endp;
1952 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1954 if (negate)
1955 break;
1956 else
1957 goto fwd_unibyte_ok;
1960 if (!fastmap[*p])
1961 break;
1962 fwd_unibyte_ok:
1963 p++, pos++, pos_byte++;
1966 else
1968 if (multibyte)
1969 while (1)
1971 unsigned char *prev_p;
1973 if (p <= stop)
1975 if (p <= endp)
1976 break;
1977 p = GPT_ADDR;
1978 stop = endp;
1980 prev_p = p;
1981 while (--p >= stop && ! CHAR_HEAD_P (*p));
1982 c = STRING_CHAR (p);
1984 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1986 if (negate)
1987 break;
1988 else
1989 goto back_ok;
1992 if (! fastmap[*p])
1993 break;
1994 if (! ASCII_CHAR_P (c))
1996 /* See the comment in the previous similar code. */
1997 for (i = 0; i < n_char_ranges; i += 2)
1998 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1999 break;
2000 if (!(negate ^ (i < n_char_ranges)))
2001 break;
2003 back_ok:
2004 pos--, pos_byte -= prev_p - p;
2006 else
2007 while (1)
2009 if (p <= stop)
2011 if (p <= endp)
2012 break;
2013 p = GPT_ADDR;
2014 stop = endp;
2017 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
2019 if (negate)
2020 break;
2021 else
2022 goto back_unibyte_ok;
2025 if (!fastmap[p[-1]])
2026 break;
2027 back_unibyte_ok:
2028 p--, pos--, pos_byte--;
2032 SET_PT_BOTH (pos, pos_byte);
2033 immediate_quit = 0;
2035 return make_number (PT - start_point);
2040 static Lisp_Object
2041 skip_syntaxes (bool forwardp, Lisp_Object string, Lisp_Object lim)
2043 int c;
2044 unsigned char fastmap[0400];
2045 bool negate = 0;
2046 ptrdiff_t i, i_byte;
2047 bool multibyte;
2048 ptrdiff_t size_byte;
2049 unsigned char *str;
2051 CHECK_STRING (string);
2053 if (NILP (lim))
2054 XSETINT (lim, forwardp ? ZV : BEGV);
2055 else
2056 CHECK_NUMBER_COERCE_MARKER (lim);
2058 /* In any case, don't allow scan outside bounds of buffer. */
2059 if (XINT (lim) > ZV)
2060 XSETFASTINT (lim, ZV);
2061 if (XINT (lim) < BEGV)
2062 XSETFASTINT (lim, BEGV);
2064 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
2065 return make_number (0);
2067 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
2068 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
2070 memset (fastmap, 0, sizeof fastmap);
2072 if (SBYTES (string) > SCHARS (string))
2073 /* As this is very rare case (syntax spec is ASCII only), don't
2074 consider efficiency. */
2075 string = string_make_unibyte (string);
2077 str = SDATA (string);
2078 size_byte = SBYTES (string);
2080 i_byte = 0;
2081 if (i_byte < size_byte
2082 && SREF (string, 0) == '^')
2084 negate = 1; i_byte++;
2087 /* Find the syntaxes specified and set their elements of fastmap. */
2089 while (i_byte < size_byte)
2091 c = str[i_byte++];
2092 fastmap[syntax_spec_code[c]] = 1;
2095 /* If ^ was the first character, complement the fastmap. */
2096 if (negate)
2097 for (i = 0; i < sizeof fastmap; i++)
2098 fastmap[i] ^= 1;
2101 ptrdiff_t start_point = PT;
2102 ptrdiff_t pos = PT;
2103 ptrdiff_t pos_byte = PT_BYTE;
2104 unsigned char *p = PT_ADDR, *endp, *stop;
2106 if (forwardp)
2108 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
2109 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
2111 else
2113 endp = CHAR_POS_ADDR (XINT (lim));
2114 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
2117 immediate_quit = 1;
2118 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
2119 if (forwardp)
2121 if (multibyte)
2123 while (1)
2125 int nbytes;
2127 if (p >= stop)
2129 if (p >= endp)
2130 break;
2131 p = GAP_END_ADDR;
2132 stop = endp;
2134 c = STRING_CHAR_AND_LENGTH (p, nbytes);
2135 if (! fastmap[SYNTAX (c)])
2136 break;
2137 p += nbytes, pos++, pos_byte += nbytes;
2138 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2141 else
2143 while (1)
2145 if (p >= stop)
2147 if (p >= endp)
2148 break;
2149 p = GAP_END_ADDR;
2150 stop = endp;
2152 if (! fastmap[SYNTAX (*p)])
2153 break;
2154 p++, pos++, pos_byte++;
2155 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2159 else
2161 if (multibyte)
2163 while (1)
2165 unsigned char *prev_p;
2167 if (p <= stop)
2169 if (p <= endp)
2170 break;
2171 p = GPT_ADDR;
2172 stop = endp;
2174 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2175 prev_p = p;
2176 while (--p >= stop && ! CHAR_HEAD_P (*p));
2177 c = STRING_CHAR (p);
2178 if (! fastmap[SYNTAX (c)])
2179 break;
2180 pos--, pos_byte -= prev_p - p;
2183 else
2185 while (1)
2187 if (p <= stop)
2189 if (p <= endp)
2190 break;
2191 p = GPT_ADDR;
2192 stop = endp;
2194 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2195 if (! fastmap[SYNTAX (p[-1])])
2196 break;
2197 p--, pos--, pos_byte--;
2202 SET_PT_BOTH (pos, pos_byte);
2203 immediate_quit = 0;
2205 return make_number (PT - start_point);
2209 /* Return true if character C belongs to one of the ISO classes
2210 in the list ISO_CLASSES. Each class is represented by an
2211 integer which is its type according to re_wctype. */
2213 static bool
2214 in_classes (int c, Lisp_Object iso_classes)
2216 bool fits_class = 0;
2218 while (CONSP (iso_classes))
2220 Lisp_Object elt;
2221 elt = XCAR (iso_classes);
2222 iso_classes = XCDR (iso_classes);
2224 if (re_iswctype (c, XFASTINT (elt)))
2225 fits_class = 1;
2228 return fits_class;
2231 /* Jump over a comment, assuming we are at the beginning of one.
2232 FROM is the current position.
2233 FROM_BYTE is the bytepos corresponding to FROM.
2234 Do not move past STOP (a charpos).
2235 The comment over which we have to jump is of style STYLE
2236 (either SYNTAX_FLAGS_COMMENT_STYLE (foo) or ST_COMMENT_STYLE).
2237 NESTING should be positive to indicate the nesting at the beginning
2238 for nested comments and should be zero or negative else.
2239 ST_COMMENT_STYLE cannot be nested.
2240 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2241 (or 0 If the search cannot start in the middle of a two-character).
2243 If successful, return true and store the charpos of the comment's end
2244 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2245 Else, return false and store the charpos STOP into *CHARPOS_PTR, the
2246 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2247 (as defined for state.incomment) in *INCOMMENT_PTR.
2249 The comment end is the last character of the comment rather than the
2250 character just after the comment.
2252 Global syntax data is assumed to initially be valid for FROM and
2253 remains valid for forward search starting at the returned position. */
2255 static bool
2256 forw_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop,
2257 EMACS_INT nesting, int style, int prev_syntax,
2258 ptrdiff_t *charpos_ptr, ptrdiff_t *bytepos_ptr,
2259 EMACS_INT *incomment_ptr)
2261 register int c, c1;
2262 register enum syntaxcode code;
2263 register int syntax, other_syntax;
2265 if (nesting <= 0) nesting = -1;
2267 /* Enter the loop in the middle so that we find
2268 a 2-char comment ender if we start in the middle of it. */
2269 syntax = prev_syntax;
2270 if (syntax != 0) goto forw_incomment;
2272 while (1)
2274 if (from == stop)
2276 *incomment_ptr = nesting;
2277 *charpos_ptr = from;
2278 *bytepos_ptr = from_byte;
2279 return 0;
2281 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2282 syntax = SYNTAX_WITH_FLAGS (c);
2283 code = syntax & 0xff;
2284 if (code == Sendcomment
2285 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style
2286 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2287 (nesting > 0 && --nesting == 0) : nesting < 0))
2288 /* we have encountered a comment end of the same style
2289 as the comment sequence which began this comment
2290 section */
2291 break;
2292 if (code == Scomment_fence
2293 && style == ST_COMMENT_STYLE)
2294 /* we have encountered a comment end of the same style
2295 as the comment sequence which began this comment
2296 section. */
2297 break;
2298 if (nesting > 0
2299 && code == Scomment
2300 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2301 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style)
2302 /* we have encountered a nested comment of the same style
2303 as the comment sequence which began this comment section */
2304 nesting++;
2305 INC_BOTH (from, from_byte);
2306 UPDATE_SYNTAX_TABLE_FORWARD (from);
2308 forw_incomment:
2309 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2310 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2311 other_syntax = SYNTAX_WITH_FLAGS (c1),
2312 SYNTAX_FLAGS_COMEND_SECOND (other_syntax))
2313 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, other_syntax) == style
2314 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2315 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax))
2316 ? nesting > 0 : nesting < 0))
2318 if (--nesting <= 0)
2319 /* we have encountered a comment end of the same style
2320 as the comment sequence which began this comment
2321 section */
2322 break;
2323 else
2325 INC_BOTH (from, from_byte);
2326 UPDATE_SYNTAX_TABLE_FORWARD (from);
2329 if (nesting > 0
2330 && from < stop
2331 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2332 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2333 other_syntax = SYNTAX_WITH_FLAGS (c1),
2334 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax) == style
2335 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2336 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2337 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax)))
2338 /* we have encountered a nested comment of the same style
2339 as the comment sequence which began this comment
2340 section */
2342 INC_BOTH (from, from_byte);
2343 UPDATE_SYNTAX_TABLE_FORWARD (from);
2344 nesting++;
2347 *charpos_ptr = from;
2348 *bytepos_ptr = from_byte;
2349 return 1;
2352 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2353 doc: /*
2354 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2355 Stop scanning if we find something other than a comment or whitespace.
2356 Set point to where scanning stops.
2357 If COUNT comments are found as expected, with nothing except whitespace
2358 between them, return t; otherwise return nil. */)
2359 (Lisp_Object count)
2361 ptrdiff_t from, from_byte, stop;
2362 int c, c1;
2363 enum syntaxcode code;
2364 int comstyle = 0; /* style of comment encountered */
2365 bool comnested = 0; /* whether the comment is nestable or not */
2366 bool found;
2367 EMACS_INT count1;
2368 ptrdiff_t out_charpos, out_bytepos;
2369 EMACS_INT dummy;
2371 CHECK_NUMBER (count);
2372 count1 = XINT (count);
2373 stop = count1 > 0 ? ZV : BEGV;
2375 immediate_quit = 1;
2376 QUIT;
2378 from = PT;
2379 from_byte = PT_BYTE;
2381 SETUP_SYNTAX_TABLE (from, count1);
2382 while (count1 > 0)
2386 bool comstart_first;
2387 int syntax, other_syntax;
2389 if (from == stop)
2391 SET_PT_BOTH (from, from_byte);
2392 immediate_quit = 0;
2393 return Qnil;
2395 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2396 syntax = SYNTAX_WITH_FLAGS (c);
2397 code = SYNTAX (c);
2398 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2399 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2400 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2401 INC_BOTH (from, from_byte);
2402 UPDATE_SYNTAX_TABLE_FORWARD (from);
2403 if (from < stop && comstart_first
2404 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2405 other_syntax = SYNTAX_WITH_FLAGS (c1),
2406 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)))
2408 /* We have encountered a comment start sequence and we
2409 are ignoring all text inside comments. We must record
2410 the comment style this sequence begins so that later,
2411 only a comment end of the same style actually ends
2412 the comment section. */
2413 code = Scomment;
2414 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2415 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2416 INC_BOTH (from, from_byte);
2417 UPDATE_SYNTAX_TABLE_FORWARD (from);
2420 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2422 if (code == Scomment_fence)
2423 comstyle = ST_COMMENT_STYLE;
2424 else if (code != Scomment)
2426 immediate_quit = 0;
2427 DEC_BOTH (from, from_byte);
2428 SET_PT_BOTH (from, from_byte);
2429 return Qnil;
2431 /* We're at the start of a comment. */
2432 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2433 &out_charpos, &out_bytepos, &dummy);
2434 from = out_charpos; from_byte = out_bytepos;
2435 if (!found)
2437 immediate_quit = 0;
2438 SET_PT_BOTH (from, from_byte);
2439 return Qnil;
2441 INC_BOTH (from, from_byte);
2442 UPDATE_SYNTAX_TABLE_FORWARD (from);
2443 /* We have skipped one comment. */
2444 count1--;
2447 while (count1 < 0)
2449 while (1)
2451 bool quoted;
2452 int syntax;
2454 if (from <= stop)
2456 SET_PT_BOTH (BEGV, BEGV_BYTE);
2457 immediate_quit = 0;
2458 return Qnil;
2461 DEC_BOTH (from, from_byte);
2462 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2463 quoted = char_quoted (from, from_byte);
2464 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2465 syntax = SYNTAX_WITH_FLAGS (c);
2466 code = SYNTAX (c);
2467 comstyle = 0;
2468 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2469 if (code == Sendcomment)
2470 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2471 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2472 && prev_char_comend_first (from, from_byte)
2473 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2475 int other_syntax;
2476 /* We must record the comment style encountered so that
2477 later, we can match only the proper comment begin
2478 sequence of the same style. */
2479 DEC_BOTH (from, from_byte);
2480 code = Sendcomment;
2481 /* Calling char_quoted, above, set up global syntax position
2482 at the new value of FROM. */
2483 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2484 other_syntax = SYNTAX_WITH_FLAGS (c1);
2485 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2486 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2489 if (code == Scomment_fence)
2491 /* Skip until first preceding unquoted comment_fence. */
2492 bool fence_found = 0;
2493 ptrdiff_t ini = from, ini_byte = from_byte;
2495 while (1)
2497 DEC_BOTH (from, from_byte);
2498 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2499 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2500 if (SYNTAX (c) == Scomment_fence
2501 && !char_quoted (from, from_byte))
2503 fence_found = 1;
2504 break;
2506 else if (from == stop)
2507 break;
2509 if (fence_found == 0)
2511 from = ini; /* Set point to ini + 1. */
2512 from_byte = ini_byte;
2513 goto leave;
2515 else
2516 /* We have skipped one comment. */
2517 break;
2519 else if (code == Sendcomment)
2521 found = back_comment (from, from_byte, stop, comnested, comstyle,
2522 &out_charpos, &out_bytepos);
2523 if (!found)
2525 if (c == '\n')
2526 /* This end-of-line is not an end-of-comment.
2527 Treat it like a whitespace.
2528 CC-mode (and maybe others) relies on this behavior. */
2530 else
2532 /* Failure: we should go back to the end of this
2533 not-quite-endcomment. */
2534 if (SYNTAX (c) != code)
2535 /* It was a two-char Sendcomment. */
2536 INC_BOTH (from, from_byte);
2537 goto leave;
2540 else
2542 /* We have skipped one comment. */
2543 from = out_charpos, from_byte = out_bytepos;
2544 break;
2547 else if (code != Swhitespace || quoted)
2549 leave:
2550 immediate_quit = 0;
2551 INC_BOTH (from, from_byte);
2552 SET_PT_BOTH (from, from_byte);
2553 return Qnil;
2557 count1++;
2560 SET_PT_BOTH (from, from_byte);
2561 immediate_quit = 0;
2562 return Qt;
2565 /* Return syntax code of character C if C is an ASCII character
2566 or if MULTIBYTE_SYMBOL_P is false. Otherwise, return Ssymbol. */
2568 static enum syntaxcode
2569 syntax_multibyte (int c, bool multibyte_symbol_p)
2571 return ASCII_CHAR_P (c) || !multibyte_symbol_p ? SYNTAX (c) : Ssymbol;
2574 static Lisp_Object
2575 scan_lists (EMACS_INT from, EMACS_INT count, EMACS_INT depth, bool sexpflag)
2577 Lisp_Object val;
2578 ptrdiff_t stop = count > 0 ? ZV : BEGV;
2579 int c, c1;
2580 int stringterm;
2581 bool quoted;
2582 bool mathexit = 0;
2583 enum syntaxcode code;
2584 EMACS_INT min_depth = depth; /* Err out if depth gets less than this. */
2585 int comstyle = 0; /* style of comment encountered */
2586 bool comnested = 0; /* whether the comment is nestable or not */
2587 ptrdiff_t temp_pos;
2588 EMACS_INT last_good = from;
2589 bool found;
2590 ptrdiff_t from_byte;
2591 ptrdiff_t out_bytepos, out_charpos;
2592 EMACS_INT dummy;
2593 bool multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2595 if (depth > 0) min_depth = 0;
2597 if (from > ZV) from = ZV;
2598 if (from < BEGV) from = BEGV;
2600 from_byte = CHAR_TO_BYTE (from);
2602 immediate_quit = 1;
2603 QUIT;
2605 SETUP_SYNTAX_TABLE (from, count);
2606 while (count > 0)
2608 while (from < stop)
2610 bool comstart_first, prefix;
2611 int syntax, other_syntax;
2612 UPDATE_SYNTAX_TABLE_FORWARD (from);
2613 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2614 syntax = SYNTAX_WITH_FLAGS (c);
2615 code = syntax_multibyte (c, multibyte_symbol_p);
2616 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2617 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2618 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2619 prefix = SYNTAX_FLAGS_PREFIX (syntax);
2620 if (depth == min_depth)
2621 last_good = from;
2622 INC_BOTH (from, from_byte);
2623 UPDATE_SYNTAX_TABLE_FORWARD (from);
2624 if (from < stop && comstart_first
2625 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2626 other_syntax = SYNTAX_WITH_FLAGS (c),
2627 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
2628 && parse_sexp_ignore_comments)
2630 /* we have encountered a comment start sequence and we
2631 are ignoring all text inside comments. We must record
2632 the comment style this sequence begins so that later,
2633 only a comment end of the same style actually ends
2634 the comment section */
2635 code = Scomment;
2636 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2637 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2638 INC_BOTH (from, from_byte);
2639 UPDATE_SYNTAX_TABLE_FORWARD (from);
2642 if (prefix)
2643 continue;
2645 switch (code)
2647 case Sescape:
2648 case Scharquote:
2649 if (from == stop)
2650 goto lose;
2651 INC_BOTH (from, from_byte);
2652 /* treat following character as a word constituent */
2653 case Sword:
2654 case Ssymbol:
2655 if (depth || !sexpflag) break;
2656 /* This word counts as a sexp; return at end of it. */
2657 while (from < stop)
2659 UPDATE_SYNTAX_TABLE_FORWARD (from);
2661 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2662 switch (syntax_multibyte (c, multibyte_symbol_p))
2664 case Scharquote:
2665 case Sescape:
2666 INC_BOTH (from, from_byte);
2667 if (from == stop)
2668 goto lose;
2669 break;
2670 case Sword:
2671 case Ssymbol:
2672 case Squote:
2673 break;
2674 default:
2675 goto done;
2677 INC_BOTH (from, from_byte);
2679 goto done;
2681 case Scomment_fence:
2682 comstyle = ST_COMMENT_STYLE;
2683 /* FALLTHROUGH */
2684 case Scomment:
2685 if (!parse_sexp_ignore_comments) break;
2686 UPDATE_SYNTAX_TABLE_FORWARD (from);
2687 found = forw_comment (from, from_byte, stop,
2688 comnested, comstyle, 0,
2689 &out_charpos, &out_bytepos, &dummy);
2690 from = out_charpos, from_byte = out_bytepos;
2691 if (!found)
2693 if (depth == 0)
2694 goto done;
2695 goto lose;
2697 INC_BOTH (from, from_byte);
2698 UPDATE_SYNTAX_TABLE_FORWARD (from);
2699 break;
2701 case Smath:
2702 if (!sexpflag)
2703 break;
2704 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2706 INC_BOTH (from, from_byte);
2708 if (mathexit)
2710 mathexit = 0;
2711 goto close1;
2713 mathexit = 1;
2715 case Sopen:
2716 if (!++depth) goto done;
2717 break;
2719 case Sclose:
2720 close1:
2721 if (!--depth) goto done;
2722 if (depth < min_depth)
2723 xsignal3 (Qscan_error,
2724 build_string ("Containing expression ends prematurely"),
2725 make_number (last_good), make_number (from));
2726 break;
2728 case Sstring:
2729 case Sstring_fence:
2730 temp_pos = dec_bytepos (from_byte);
2731 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2732 while (1)
2734 enum syntaxcode c_code;
2735 if (from >= stop)
2736 goto lose;
2737 UPDATE_SYNTAX_TABLE_FORWARD (from);
2738 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2739 c_code = syntax_multibyte (c, multibyte_symbol_p);
2740 if (code == Sstring
2741 ? c == stringterm && c_code == Sstring
2742 : c_code == Sstring_fence)
2743 break;
2745 switch (c_code)
2747 case Scharquote:
2748 case Sescape:
2749 INC_BOTH (from, from_byte);
2751 INC_BOTH (from, from_byte);
2753 INC_BOTH (from, from_byte);
2754 if (!depth && sexpflag) goto done;
2755 break;
2756 default:
2757 /* Ignore whitespace, punctuation, quote, endcomment. */
2758 break;
2762 /* Reached end of buffer. Error if within object, return nil if between */
2763 if (depth)
2764 goto lose;
2766 immediate_quit = 0;
2767 return Qnil;
2769 /* End of object reached */
2770 done:
2771 count--;
2774 while (count < 0)
2776 while (from > stop)
2778 int syntax;
2779 DEC_BOTH (from, from_byte);
2780 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2781 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2782 syntax= SYNTAX_WITH_FLAGS (c);
2783 code = syntax_multibyte (c, multibyte_symbol_p);
2784 if (depth == min_depth)
2785 last_good = from;
2786 comstyle = 0;
2787 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2788 if (code == Sendcomment)
2789 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2790 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
2791 && prev_char_comend_first (from, from_byte)
2792 && parse_sexp_ignore_comments)
2794 /* We must record the comment style encountered so that
2795 later, we can match only the proper comment begin
2796 sequence of the same style. */
2797 int c2, other_syntax;
2798 DEC_BOTH (from, from_byte);
2799 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2800 code = Sendcomment;
2801 c2 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2802 other_syntax = SYNTAX_WITH_FLAGS (c2);
2803 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2804 comnested |= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
2807 /* Quoting turns anything except a comment-ender
2808 into a word character. Note that this cannot be true
2809 if we decremented FROM in the if-statement above. */
2810 if (code != Sendcomment && char_quoted (from, from_byte))
2812 DEC_BOTH (from, from_byte);
2813 code = Sword;
2815 else if (SYNTAX_FLAGS_PREFIX (syntax))
2816 continue;
2818 switch (code)
2820 case Sword:
2821 case Ssymbol:
2822 case Sescape:
2823 case Scharquote:
2824 if (depth || !sexpflag) break;
2825 /* This word counts as a sexp; count object finished
2826 after passing it. */
2827 while (from > stop)
2829 temp_pos = from_byte;
2830 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
2831 DEC_POS (temp_pos);
2832 else
2833 temp_pos--;
2834 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2835 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2836 /* Don't allow comment-end to be quoted. */
2837 if (syntax_multibyte (c1, multibyte_symbol_p) == Sendcomment)
2838 goto done2;
2839 quoted = char_quoted (from - 1, temp_pos);
2840 if (quoted)
2842 DEC_BOTH (from, from_byte);
2843 temp_pos = dec_bytepos (temp_pos);
2844 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2846 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2847 if (! quoted)
2848 switch (syntax_multibyte (c1, multibyte_symbol_p))
2850 case Sword: case Ssymbol: case Squote: break;
2851 default: goto done2;
2853 DEC_BOTH (from, from_byte);
2855 goto done2;
2857 case Smath:
2858 if (!sexpflag)
2859 break;
2860 temp_pos = dec_bytepos (from_byte);
2861 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2862 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2863 DEC_BOTH (from, from_byte);
2864 if (mathexit)
2866 mathexit = 0;
2867 goto open2;
2869 mathexit = 1;
2871 case Sclose:
2872 if (!++depth) goto done2;
2873 break;
2875 case Sopen:
2876 open2:
2877 if (!--depth) goto done2;
2878 if (depth < min_depth)
2879 xsignal3 (Qscan_error,
2880 build_string ("Containing expression ends prematurely"),
2881 make_number (last_good), make_number (from));
2882 break;
2884 case Sendcomment:
2885 if (!parse_sexp_ignore_comments)
2886 break;
2887 found = back_comment (from, from_byte, stop, comnested, comstyle,
2888 &out_charpos, &out_bytepos);
2889 /* FIXME: if !found, it really wasn't a comment-end.
2890 For single-char Sendcomment, we can't do much about it apart
2891 from skipping the char.
2892 For 2-char endcomments, we could try again, taking both
2893 chars as separate entities, but it's a lot of trouble
2894 for very little gain, so we don't bother either. -sm */
2895 if (found)
2896 from = out_charpos, from_byte = out_bytepos;
2897 break;
2899 case Scomment_fence:
2900 case Sstring_fence:
2901 while (1)
2903 if (from == stop)
2904 goto lose;
2905 DEC_BOTH (from, from_byte);
2906 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2907 if (!char_quoted (from, from_byte))
2909 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2910 if (syntax_multibyte (c, multibyte_symbol_p) == code)
2911 break;
2914 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2915 break;
2917 case Sstring:
2918 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2919 while (1)
2921 if (from == stop)
2922 goto lose;
2923 DEC_BOTH (from, from_byte);
2924 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2925 if (!char_quoted (from, from_byte))
2927 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2928 if (c == stringterm
2929 && (syntax_multibyte (c, multibyte_symbol_p)
2930 == Sstring))
2931 break;
2934 if (!depth && sexpflag) goto done2;
2935 break;
2936 default:
2937 /* Ignore whitespace, punctuation, quote, endcomment. */
2938 break;
2942 /* Reached start of buffer. Error if within object, return nil if between */
2943 if (depth)
2944 goto lose;
2946 immediate_quit = 0;
2947 return Qnil;
2949 done2:
2950 count++;
2954 immediate_quit = 0;
2955 XSETFASTINT (val, from);
2956 return val;
2958 lose:
2959 xsignal3 (Qscan_error,
2960 build_string ("Unbalanced parentheses"),
2961 make_number (last_good), make_number (from));
2964 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2965 doc: /* Scan from character number FROM by COUNT lists.
2966 Scan forward if COUNT is positive, backward if COUNT is negative.
2967 Return the character number of the position thus found.
2969 A \"list", in this context, refers to a balanced parenthetical
2970 grouping, as determined by the syntax table.
2972 If DEPTH is nonzero, treat that as the nesting depth of the starting
2973 point (i.e. the starting point is DEPTH parentheses deep). This
2974 function scans over parentheses until the depth goes to zero COUNT
2975 times. Hence, positive DEPTH moves out that number of levels of
2976 parentheses, while negative DEPTH moves to a deeper level.
2978 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2980 If we reach the beginning or end of the accessible part of the buffer
2981 before we have scanned over COUNT lists, return nil if the depth at
2982 that point is zero, and signal a error if the depth is nonzero. */)
2983 (Lisp_Object from, Lisp_Object count, Lisp_Object depth)
2985 CHECK_NUMBER (from);
2986 CHECK_NUMBER (count);
2987 CHECK_NUMBER (depth);
2989 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2992 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2993 doc: /* Scan from character number FROM by COUNT balanced expressions.
2994 If COUNT is negative, scan backwards.
2995 Returns the character number of the position thus found.
2997 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2999 If the beginning or end of (the accessible part of) the buffer is reached
3000 in the middle of a parenthetical grouping, an error is signaled.
3001 If the beginning or end is reached between groupings
3002 but before count is used up, nil is returned. */)
3003 (Lisp_Object from, Lisp_Object count)
3005 CHECK_NUMBER (from);
3006 CHECK_NUMBER (count);
3008 return scan_lists (XINT (from), XINT (count), 0, 1);
3011 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
3012 0, 0, 0,
3013 doc: /* Move point backward over any number of chars with prefix syntax.
3014 This includes chars with "quote" or "prefix" syntax (' or p). */)
3015 (void)
3017 ptrdiff_t beg = BEGV;
3018 ptrdiff_t opoint = PT;
3019 ptrdiff_t opoint_byte = PT_BYTE;
3020 ptrdiff_t pos = PT;
3021 ptrdiff_t pos_byte = PT_BYTE;
3022 int c;
3024 if (pos <= beg)
3026 SET_PT_BOTH (opoint, opoint_byte);
3028 return Qnil;
3031 SETUP_SYNTAX_TABLE (pos, -1);
3033 DEC_BOTH (pos, pos_byte);
3035 while (!char_quoted (pos, pos_byte)
3036 /* Previous statement updates syntax table. */
3037 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
3038 || syntax_prefix_flag_p (c)))
3040 opoint = pos;
3041 opoint_byte = pos_byte;
3043 if (pos + 1 > beg)
3044 DEC_BOTH (pos, pos_byte);
3047 SET_PT_BOTH (opoint, opoint_byte);
3049 return Qnil;
3052 /* Parse forward from FROM / FROM_BYTE to END,
3053 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
3054 and return a description of the state of the parse at END.
3055 If STOPBEFORE, stop at the start of an atom.
3056 If COMMENTSTOP is 1, stop at the start of a comment.
3057 If COMMENTSTOP is -1, stop at the start or end of a comment,
3058 after the beginning of a string, or after the end of a string. */
3060 static void
3061 scan_sexps_forward (struct lisp_parse_state *stateptr,
3062 ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t end,
3063 EMACS_INT targetdepth, bool stopbefore,
3064 Lisp_Object oldstate, int commentstop)
3066 struct lisp_parse_state state;
3067 enum syntaxcode code;
3068 int c1;
3069 bool comnested;
3070 struct level { ptrdiff_t last, prev; };
3071 struct level levelstart[100];
3072 struct level *curlevel = levelstart;
3073 struct level *endlevel = levelstart + 100;
3074 EMACS_INT depth; /* Paren depth of current scanning location.
3075 level - levelstart equals this except
3076 when the depth becomes negative. */
3077 EMACS_INT mindepth; /* Lowest DEPTH value seen. */
3078 bool start_quoted = 0; /* True means starting after a char quote. */
3079 Lisp_Object tem;
3080 ptrdiff_t prev_from; /* Keep one character before FROM. */
3081 ptrdiff_t prev_from_byte;
3082 int prev_from_syntax;
3083 bool boundary_stop = commentstop == -1;
3084 bool nofence;
3085 bool found;
3086 ptrdiff_t out_bytepos, out_charpos;
3087 int temp;
3089 prev_from = from;
3090 prev_from_byte = from_byte;
3091 if (from != BEGV)
3092 DEC_BOTH (prev_from, prev_from_byte);
3094 /* Use this macro instead of `from++'. */
3095 #define INC_FROM \
3096 do { prev_from = from; \
3097 prev_from_byte = from_byte; \
3098 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
3099 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
3100 INC_BOTH (from, from_byte); \
3101 if (from < end) \
3102 UPDATE_SYNTAX_TABLE_FORWARD (from); \
3103 } while (0)
3105 immediate_quit = 1;
3106 QUIT;
3108 if (NILP (oldstate))
3110 depth = 0;
3111 state.instring = -1;
3112 state.incomment = 0;
3113 state.comstyle = 0; /* comment style a by default. */
3114 state.comstr_start = -1; /* no comment/string seen. */
3116 else
3118 tem = Fcar (oldstate);
3119 if (!NILP (tem))
3120 depth = XINT (tem);
3121 else
3122 depth = 0;
3124 oldstate = Fcdr (oldstate);
3125 oldstate = Fcdr (oldstate);
3126 oldstate = Fcdr (oldstate);
3127 tem = Fcar (oldstate);
3128 /* Check whether we are inside string_fence-style string: */
3129 state.instring = (!NILP (tem)
3130 ? (CHARACTERP (tem) ? XFASTINT (tem) : ST_STRING_STYLE)
3131 : -1);
3133 oldstate = Fcdr (oldstate);
3134 tem = Fcar (oldstate);
3135 state.incomment = (!NILP (tem)
3136 ? (INTEGERP (tem) ? XINT (tem) : -1)
3137 : 0);
3139 oldstate = Fcdr (oldstate);
3140 tem = Fcar (oldstate);
3141 start_quoted = !NILP (tem);
3143 /* if the eighth element of the list is nil, we are in comment
3144 style a. If it is non-nil, we are in comment style b */
3145 oldstate = Fcdr (oldstate);
3146 oldstate = Fcdr (oldstate);
3147 tem = Fcar (oldstate);
3148 state.comstyle = (NILP (tem)
3150 : (RANGED_INTEGERP (0, tem, ST_COMMENT_STYLE)
3151 ? XINT (tem)
3152 : ST_COMMENT_STYLE));
3154 oldstate = Fcdr (oldstate);
3155 tem = Fcar (oldstate);
3156 state.comstr_start =
3157 RANGED_INTEGERP (PTRDIFF_MIN, tem, PTRDIFF_MAX) ? XINT (tem) : -1;
3158 oldstate = Fcdr (oldstate);
3159 tem = Fcar (oldstate);
3160 while (!NILP (tem)) /* >= second enclosing sexps. */
3162 Lisp_Object temhd = Fcar (tem);
3163 if (RANGED_INTEGERP (PTRDIFF_MIN, temhd, PTRDIFF_MAX))
3164 curlevel->last = XINT (temhd);
3165 if (++curlevel == endlevel)
3166 curlevel--; /* error ("Nesting too deep for parser"); */
3167 curlevel->prev = -1;
3168 curlevel->last = -1;
3169 tem = Fcdr (tem);
3172 state.quoted = 0;
3173 mindepth = depth;
3175 curlevel->prev = -1;
3176 curlevel->last = -1;
3178 SETUP_SYNTAX_TABLE (prev_from, 1);
3179 temp = FETCH_CHAR (prev_from_byte);
3180 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
3181 UPDATE_SYNTAX_TABLE_FORWARD (from);
3183 /* Enter the loop at a place appropriate for initial state. */
3185 if (state.incomment)
3186 goto startincomment;
3187 if (state.instring >= 0)
3189 nofence = state.instring != ST_STRING_STYLE;
3190 if (start_quoted)
3191 goto startquotedinstring;
3192 goto startinstring;
3194 else if (start_quoted)
3195 goto startquoted;
3197 while (from < end)
3199 int syntax;
3200 INC_FROM;
3201 code = prev_from_syntax & 0xff;
3203 if (from < end
3204 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3205 && (c1 = FETCH_CHAR (from_byte),
3206 syntax = SYNTAX_WITH_FLAGS (c1),
3207 SYNTAX_FLAGS_COMSTART_SECOND (syntax)))
3208 /* Duplicate code to avoid a complex if-expression
3209 which causes trouble for the SGI compiler. */
3211 /* Record the comment style we have entered so that only
3212 the comment-end sequence of the same style actually
3213 terminates the comment section. */
3214 state.comstyle
3215 = SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_from_syntax);
3216 comnested = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax)
3217 | SYNTAX_FLAGS_COMMENT_NESTED (syntax));
3218 state.incomment = comnested ? 1 : -1;
3219 state.comstr_start = prev_from;
3220 INC_FROM;
3221 code = Scomment;
3223 else if (code == Scomment_fence)
3225 /* Record the comment style we have entered so that only
3226 the comment-end sequence of the same style actually
3227 terminates the comment section. */
3228 state.comstyle = ST_COMMENT_STYLE;
3229 state.incomment = -1;
3230 state.comstr_start = prev_from;
3231 code = Scomment;
3233 else if (code == Scomment)
3235 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax, 0);
3236 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3237 1 : -1);
3238 state.comstr_start = prev_from;
3241 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3242 continue;
3243 switch (code)
3245 case Sescape:
3246 case Scharquote:
3247 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3248 curlevel->last = prev_from;
3249 startquoted:
3250 if (from == end) goto endquoted;
3251 INC_FROM;
3252 goto symstarted;
3253 /* treat following character as a word constituent */
3254 case Sword:
3255 case Ssymbol:
3256 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3257 curlevel->last = prev_from;
3258 symstarted:
3259 while (from < end)
3261 int symchar = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3262 switch (SYNTAX (symchar))
3264 case Scharquote:
3265 case Sescape:
3266 INC_FROM;
3267 if (from == end) goto endquoted;
3268 break;
3269 case Sword:
3270 case Ssymbol:
3271 case Squote:
3272 break;
3273 default:
3274 goto symdone;
3276 INC_FROM;
3278 symdone:
3279 curlevel->prev = curlevel->last;
3280 break;
3282 case Scomment_fence: /* Can't happen because it's handled above. */
3283 case Scomment:
3284 if (commentstop || boundary_stop) goto done;
3285 startincomment:
3286 /* The (from == BEGV) test was to enter the loop in the middle so
3287 that we find a 2-char comment ender even if we start in the
3288 middle of it. We don't want to do that if we're just at the
3289 beginning of the comment (think of (*) ... (*)). */
3290 found = forw_comment (from, from_byte, end,
3291 state.incomment, state.comstyle,
3292 (from == BEGV || from < state.comstr_start + 3)
3293 ? 0 : prev_from_syntax,
3294 &out_charpos, &out_bytepos, &state.incomment);
3295 from = out_charpos; from_byte = out_bytepos;
3296 /* Beware! prev_from and friends are invalid now.
3297 Luckily, the `done' doesn't use them and the INC_FROM
3298 sets them to a sane value without looking at them. */
3299 if (!found) goto done;
3300 INC_FROM;
3301 state.incomment = 0;
3302 state.comstyle = 0; /* reset the comment style */
3303 if (boundary_stop) goto done;
3304 break;
3306 case Sopen:
3307 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3308 depth++;
3309 /* curlevel++->last ran into compiler bug on Apollo */
3310 curlevel->last = prev_from;
3311 if (++curlevel == endlevel)
3312 curlevel--; /* error ("Nesting too deep for parser"); */
3313 curlevel->prev = -1;
3314 curlevel->last = -1;
3315 if (targetdepth == depth) goto done;
3316 break;
3318 case Sclose:
3319 depth--;
3320 if (depth < mindepth)
3321 mindepth = depth;
3322 if (curlevel != levelstart)
3323 curlevel--;
3324 curlevel->prev = curlevel->last;
3325 if (targetdepth == depth) goto done;
3326 break;
3328 case Sstring:
3329 case Sstring_fence:
3330 state.comstr_start = from - 1;
3331 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3332 curlevel->last = prev_from;
3333 state.instring = (code == Sstring
3334 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3335 : ST_STRING_STYLE);
3336 if (boundary_stop) goto done;
3337 startinstring:
3339 nofence = state.instring != ST_STRING_STYLE;
3341 while (1)
3343 int c;
3344 enum syntaxcode c_code;
3346 if (from >= end) goto done;
3347 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3348 c_code = SYNTAX (c);
3350 /* Check C_CODE here so that if the char has
3351 a syntax-table property which says it is NOT
3352 a string character, it does not end the string. */
3353 if (nofence && c == state.instring && c_code == Sstring)
3354 break;
3356 switch (c_code)
3358 case Sstring_fence:
3359 if (!nofence) goto string_end;
3360 break;
3361 case Scharquote:
3362 case Sescape:
3363 INC_FROM;
3364 startquotedinstring:
3365 if (from >= end) goto endquoted;
3367 INC_FROM;
3370 string_end:
3371 state.instring = -1;
3372 curlevel->prev = curlevel->last;
3373 INC_FROM;
3374 if (boundary_stop) goto done;
3375 break;
3377 case Smath:
3378 /* FIXME: We should do something with it. */
3379 break;
3380 default:
3381 /* Ignore whitespace, punctuation, quote, endcomment. */
3382 break;
3385 goto done;
3387 stop: /* Here if stopping before start of sexp. */
3388 from = prev_from; /* We have just fetched the char that starts it; */
3389 from_byte = prev_from_byte;
3390 goto done; /* but return the position before it. */
3392 endquoted:
3393 state.quoted = 1;
3394 done:
3395 state.depth = depth;
3396 state.mindepth = mindepth;
3397 state.thislevelstart = curlevel->prev;
3398 state.prevlevelstart
3399 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3400 state.location = from;
3401 state.location_byte = from_byte;
3402 state.levelstarts = Qnil;
3403 while (curlevel > levelstart)
3404 state.levelstarts = Fcons (make_number ((--curlevel)->last),
3405 state.levelstarts);
3406 immediate_quit = 0;
3408 *stateptr = state;
3411 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3412 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3413 Parsing stops at TO or when certain criteria are met;
3414 point is set to where parsing stops.
3415 If fifth arg OLDSTATE is omitted or nil,
3416 parsing assumes that FROM is the beginning of a function.
3417 Value is a list of elements describing final state of parsing:
3418 0. depth in parens.
3419 1. character address of start of innermost containing list; nil if none.
3420 2. character address of start of last complete sexp terminated.
3421 3. non-nil if inside a string.
3422 (it is the character that will terminate the string,
3423 or t if the string should be terminated by a generic string delimiter.)
3424 4. nil if outside a comment, t if inside a non-nestable comment,
3425 else an integer (the current comment nesting).
3426 5. t if following a quote character.
3427 6. the minimum paren-depth encountered during this scan.
3428 7. style of comment, if any.
3429 8. character address of start of comment or string; nil if not in one.
3430 9. Intermediate data for continuation of parsing (subject to change).
3431 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3432 in parentheses becomes equal to TARGETDEPTH.
3433 Fourth arg STOPBEFORE non-nil means stop when come to
3434 any character that starts a sexp.
3435 Fifth arg OLDSTATE is a list like what this function returns.
3436 It is used to initialize the state of the parse. Elements number 1, 2, 6
3437 are ignored.
3438 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3439 If it is symbol `syntax-table', stop after the start of a comment or a
3440 string, or after end of a comment or a string. */)
3441 (Lisp_Object from, Lisp_Object to, Lisp_Object targetdepth,
3442 Lisp_Object stopbefore, Lisp_Object oldstate, Lisp_Object commentstop)
3444 struct lisp_parse_state state;
3445 EMACS_INT target;
3447 if (!NILP (targetdepth))
3449 CHECK_NUMBER (targetdepth);
3450 target = XINT (targetdepth);
3452 else
3453 target = TYPE_MINIMUM (EMACS_INT); /* We won't reach this depth */
3455 validate_region (&from, &to);
3456 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3457 XINT (to),
3458 target, !NILP (stopbefore), oldstate,
3459 (NILP (commentstop)
3460 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3462 SET_PT_BOTH (state.location, state.location_byte);
3464 return Fcons (make_number (state.depth),
3465 Fcons (state.prevlevelstart < 0
3466 ? Qnil : make_number (state.prevlevelstart),
3467 Fcons (state.thislevelstart < 0
3468 ? Qnil : make_number (state.thislevelstart),
3469 Fcons (state.instring >= 0
3470 ? (state.instring == ST_STRING_STYLE
3471 ? Qt : make_number (state.instring)) : Qnil,
3472 Fcons (state.incomment < 0 ? Qt :
3473 (state.incomment == 0 ? Qnil :
3474 make_number (state.incomment)),
3475 Fcons (state.quoted ? Qt : Qnil,
3476 Fcons (make_number (state.mindepth),
3477 Fcons ((state.comstyle
3478 ? (state.comstyle == ST_COMMENT_STYLE
3479 ? Qsyntax_table
3480 : make_number (state.comstyle))
3481 : Qnil),
3482 Fcons (((state.incomment
3483 || (state.instring >= 0))
3484 ? make_number (state.comstr_start)
3485 : Qnil),
3486 Fcons (state.levelstarts, Qnil))))))))));
3489 void
3490 init_syntax_once (void)
3492 register int i, c;
3493 Lisp_Object temp;
3495 /* This has to be done here, before we call Fmake_char_table. */
3496 DEFSYM (Qsyntax_table, "syntax-table");
3498 /* This variable is DEFSYMed in alloc.c and not initialized yet, so
3499 intern it here. NOTE: you must guarantee that init_syntax_once
3500 is called before all other users of this variable. */
3501 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
3503 /* Create objects which can be shared among syntax tables. */
3504 Vsyntax_code_object = make_uninit_vector (Smax);
3505 for (i = 0; i < Smax; i++)
3506 ASET (Vsyntax_code_object, i, Fcons (make_number (i), Qnil));
3508 /* Now we are ready to set up this property, so we can
3509 create syntax tables. */
3510 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3512 temp = AREF (Vsyntax_code_object, Swhitespace);
3514 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3516 /* Control characters should not be whitespace. */
3517 temp = AREF (Vsyntax_code_object, Spunct);
3518 for (i = 0; i <= ' ' - 1; i++)
3519 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3520 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3522 /* Except that a few really are whitespace. */
3523 temp = AREF (Vsyntax_code_object, Swhitespace);
3524 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3525 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3526 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3527 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3528 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3530 temp = AREF (Vsyntax_code_object, Sword);
3531 for (i = 'a'; i <= 'z'; i++)
3532 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3533 for (i = 'A'; i <= 'Z'; i++)
3534 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3535 for (i = '0'; i <= '9'; i++)
3536 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3538 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3539 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3541 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3542 Fcons (make_number (Sopen), make_number (')')));
3543 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3544 Fcons (make_number (Sclose), make_number ('(')));
3545 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3546 Fcons (make_number (Sopen), make_number (']')));
3547 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3548 Fcons (make_number (Sclose), make_number ('[')));
3549 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3550 Fcons (make_number (Sopen), make_number ('}')));
3551 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3552 Fcons (make_number (Sclose), make_number ('{')));
3553 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3554 Fcons (make_number (Sstring), Qnil));
3555 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3556 Fcons (make_number (Sescape), Qnil));
3558 temp = AREF (Vsyntax_code_object, Ssymbol);
3559 for (i = 0; i < 10; i++)
3561 c = "_-+*/&|<>="[i];
3562 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3565 temp = AREF (Vsyntax_code_object, Spunct);
3566 for (i = 0; i < 12; i++)
3568 c = ".,;:?!#@~^'`"[i];
3569 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3572 /* All multibyte characters have syntax `word' by default. */
3573 temp = AREF (Vsyntax_code_object, Sword);
3574 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3577 void
3578 syms_of_syntax (void)
3580 DEFSYM (Qsyntax_table_p, "syntax-table-p");
3582 staticpro (&Vsyntax_code_object);
3584 staticpro (&gl_state.object);
3585 staticpro (&gl_state.global_code);
3586 staticpro (&gl_state.current_syntax_table);
3587 staticpro (&gl_state.old_prop);
3589 /* Defined in regex.c */
3590 staticpro (&re_match_object);
3592 DEFSYM (Qscan_error, "scan-error");
3593 Fput (Qscan_error, Qerror_conditions,
3594 listn (CONSTYPE_PURE, 2, Qscan_error, Qerror));
3595 Fput (Qscan_error, Qerror_message,
3596 build_pure_c_string ("Scan error"));
3598 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments,
3599 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3601 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties,
3602 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3603 Otherwise, that text property is simply ignored.
3604 See the info node `(elisp)Syntax Properties' for a description of the
3605 `syntax-table' property. */);
3607 words_include_escapes = 0;
3608 DEFVAR_BOOL ("words-include-escapes", words_include_escapes,
3609 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3611 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol,
3612 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3613 multibyte_syntax_as_symbol = 0;
3615 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3616 open_paren_in_column_0_is_defun_start,
3617 doc: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3618 open_paren_in_column_0_is_defun_start = 1;
3621 DEFVAR_LISP ("find-word-boundary-function-table",
3622 Vfind_word_boundary_function_table,
3623 doc: /*
3624 Char table of functions to search for the word boundary.
3625 Each function is called with two arguments; POS and LIMIT.
3626 POS and LIMIT are character positions in the current buffer.
3628 If POS is less than LIMIT, POS is at the first character of a word,
3629 and the return value of a function is a position after the last
3630 character of that word.
3632 If POS is not less than LIMIT, POS is at the last character of a word,
3633 and the return value of a function is a position at the first
3634 character of that word.
3636 In both cases, LIMIT bounds the search. */);
3637 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3639 defsubr (&Ssyntax_table_p);
3640 defsubr (&Ssyntax_table);
3641 defsubr (&Sstandard_syntax_table);
3642 defsubr (&Scopy_syntax_table);
3643 defsubr (&Sset_syntax_table);
3644 defsubr (&Schar_syntax);
3645 defsubr (&Smatching_paren);
3646 defsubr (&Sstring_to_syntax);
3647 defsubr (&Smodify_syntax_entry);
3648 defsubr (&Sinternal_describe_syntax_value);
3650 defsubr (&Sforward_word);
3652 defsubr (&Sskip_chars_forward);
3653 defsubr (&Sskip_chars_backward);
3654 defsubr (&Sskip_syntax_forward);
3655 defsubr (&Sskip_syntax_backward);
3657 defsubr (&Sforward_comment);
3658 defsubr (&Sscan_lists);
3659 defsubr (&Sscan_sexps);
3660 defsubr (&Sbackward_prefix_chars);
3661 defsubr (&Sparse_partial_sexp);