1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2012
3 Free 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/>. */
24 #include <sys/types.h>
28 #include "character.h"
33 /* Make syntax table lookup grant data in gl_state. */
34 #define SYNTAX_ENTRY_VIA_PROPERTY
37 #include "intervals.h"
40 /* Then there are seven single-bit flags that have the following meanings:
41 1. This character is the first of a two-character comment-start sequence.
42 2. This character is the second of a two-character comment-start sequence.
43 3. This character is the first of a two-character comment-end sequence.
44 4. This character is the second of a two-character comment-end sequence.
45 5. This character is a prefix, for backward-prefix-chars.
46 6. The char is part of a delimiter for comments of style "b".
47 7. This character is part of a nestable comment sequence.
48 8. The char is part of a delimiter for comments of style "c".
49 Note that any two-character sequence whose first character has flag 1
50 and whose second character has flag 2 will be interpreted as a comment start.
52 bit 6 and 8 are used to discriminate between different comment styles.
53 Languages such as C++ allow two orthogonal syntax start/end pairs
54 and bit 6 is used to determine whether a comment-end or Scommentend
55 ends style a or b. Comment markers can start style a, b, c, or bc.
56 Style a is always the default.
57 For 2-char comment markers, the style b flag is only looked up on the second
58 char of the comment marker and on the first char of the comment ender.
59 For style c (like to for the nested flag), the flag can be placed on any
63 /* These macros extract specific flags from an integer
64 that holds the syntax code and the flags. */
66 #define SYNTAX_FLAGS_COMSTART_FIRST(flags) (((flags) >> 16) & 1)
68 #define SYNTAX_FLAGS_COMSTART_SECOND(flags) (((flags) >> 17) & 1)
70 #define SYNTAX_FLAGS_COMEND_FIRST(flags) (((flags) >> 18) & 1)
72 #define SYNTAX_FLAGS_COMEND_SECOND(flags) (((flags) >> 19) & 1)
74 #define SYNTAX_FLAGS_PREFIX(flags) (((flags) >> 20) & 1)
76 #define SYNTAX_FLAGS_COMMENT_STYLEB(flags) (((flags) >> 21) & 1)
77 #define SYNTAX_FLAGS_COMMENT_STYLEC(flags) (((flags) >> 22) & 2)
78 /* FLAGS should be the flags of the main char of the comment marker, e.g.
79 the second for comstart and the first for comend. */
80 #define SYNTAX_FLAGS_COMMENT_STYLE(flags, other_flags) \
81 (SYNTAX_FLAGS_COMMENT_STYLEB (flags) \
82 | SYNTAX_FLAGS_COMMENT_STYLEC (flags) \
83 | SYNTAX_FLAGS_COMMENT_STYLEC (other_flags))
85 #define SYNTAX_FLAGS_COMMENT_NESTED(flags) (((flags) >> 22) & 1)
87 /* These macros extract a particular flag for a given character. */
89 #define SYNTAX_COMEND_FIRST(c) \
90 (SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c)))
91 #define SYNTAX_PREFIX(c) (SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c)))
93 /* We use these constants in place for comment-style and
94 string-ender-char to distinguish comments/strings started by
95 comment_fence and string_fence codes. */
97 #define ST_COMMENT_STYLE (256 + 1)
98 #define ST_STRING_STYLE (256 + 2)
100 static Lisp_Object Qsyntax_table_p
;
101 static Lisp_Object Qsyntax_table
, Qscan_error
;
104 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
105 if not compiled with GCC. No need to mark it, since it is used
106 only very temporarily. */
107 Lisp_Object syntax_temp
;
110 /* This is the internal form of the parse state used in parse-partial-sexp. */
112 struct lisp_parse_state
114 EMACS_INT depth
; /* Depth at end of parsing. */
115 int instring
; /* -1 if not within string, else desired terminator. */
116 EMACS_INT incomment
; /* -1 if in unnestable comment else comment nesting */
117 int comstyle
; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
118 int quoted
; /* Nonzero if just after an escape char at end of parsing */
119 EMACS_INT mindepth
; /* Minimum depth seen while scanning. */
120 /* Char number of most recent start-of-expression at current level */
121 ptrdiff_t thislevelstart
;
122 /* Char number of start of containing expression */
123 ptrdiff_t prevlevelstart
;
124 ptrdiff_t location
; /* Char number at which parsing stopped. */
125 ptrdiff_t comstr_start
; /* Position of last comment/string starter. */
126 Lisp_Object levelstarts
; /* Char numbers of starts-of-expression
127 of levels (starting from outermost). */
130 /* These variables are a cache for finding the start of a defun.
131 find_start_pos is the place for which the defun start was found.
132 find_start_value is the defun start position found for it.
133 find_start_value_byte is the corresponding byte position.
134 find_start_buffer is the buffer it was found in.
135 find_start_begv is the BEGV value when it was found.
136 find_start_modiff is the value of MODIFF when it was found. */
138 static ptrdiff_t find_start_pos
;
139 static ptrdiff_t find_start_value
;
140 static ptrdiff_t find_start_value_byte
;
141 static struct buffer
*find_start_buffer
;
142 static ptrdiff_t find_start_begv
;
143 static EMACS_INT find_start_modiff
;
146 static Lisp_Object
skip_chars (int, Lisp_Object
, Lisp_Object
, int);
147 static Lisp_Object
skip_syntaxes (int, Lisp_Object
, Lisp_Object
);
148 static Lisp_Object
scan_lists (EMACS_INT
, EMACS_INT
, EMACS_INT
, int);
149 static void scan_sexps_forward (struct lisp_parse_state
*,
150 ptrdiff_t, ptrdiff_t, ptrdiff_t, EMACS_INT
,
151 int, Lisp_Object
, int);
152 static int in_classes (int, Lisp_Object
);
154 /* Whether the syntax of the character C has the prefix flag set. */
155 int syntax_prefix_flag_p (int c
)
157 return SYNTAX_PREFIX (c
);
160 struct gl_state_s gl_state
; /* Global state of syntax parser. */
162 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
163 to scan to property-change. */
165 /* Update gl_state to an appropriate interval which contains CHARPOS. The
166 sign of COUNT give the relative position of CHARPOS wrt the previously
167 valid interval. If INIT, only [be]_property fields of gl_state are
168 valid at start, the rest is filled basing on OBJECT.
170 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
171 direction than the intervals - or in an interval. We update the
172 current syntax-table basing on the property of this interval, and
173 update the interval to start further than CHARPOS - or be
174 NULL_INTERVAL. We also update lim_property to be the next value of
175 charpos to call this subroutine again - or be before/after the
176 start/end of OBJECT. */
179 update_syntax_table (ptrdiff_t charpos
, EMACS_INT count
, int init
,
182 Lisp_Object tmp_table
;
189 gl_state
.old_prop
= Qnil
;
190 gl_state
.start
= gl_state
.b_property
;
191 gl_state
.stop
= gl_state
.e_property
;
192 i
= interval_of (charpos
, object
);
193 gl_state
.backward_i
= gl_state
.forward_i
= i
;
195 if (NULL_INTERVAL_P (i
))
197 /* interval_of updates only ->position of the return value, so
198 update the parents manually to speed up update_interval. */
199 while (!NULL_PARENT (i
))
201 if (AM_RIGHT_CHILD (i
))
202 INTERVAL_PARENT (i
)->position
= i
->position
203 - LEFT_TOTAL_LENGTH (i
) + TOTAL_LENGTH (i
) /* right end */
204 - TOTAL_LENGTH (INTERVAL_PARENT (i
))
205 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i
));
207 INTERVAL_PARENT (i
)->position
= i
->position
- LEFT_TOTAL_LENGTH (i
)
209 i
= INTERVAL_PARENT (i
);
211 i
= gl_state
.forward_i
;
212 gl_state
.b_property
= i
->position
- gl_state
.offset
;
213 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
216 i
= count
> 0 ? gl_state
.forward_i
: gl_state
.backward_i
;
218 /* We are guaranteed to be called with CHARPOS either in i,
220 if (NULL_INTERVAL_P (i
))
221 error ("Error in syntax_table logic for to-the-end intervals");
222 else if (charpos
< i
->position
) /* Move left. */
225 error ("Error in syntax_table logic for intervals <-");
226 /* Update the interval. */
227 i
= update_interval (i
, charpos
);
228 if (INTERVAL_LAST_POS (i
) != gl_state
.b_property
)
231 gl_state
.forward_i
= i
;
232 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
235 else if (charpos
>= INTERVAL_LAST_POS (i
)) /* Move right. */
238 error ("Error in syntax_table logic for intervals ->");
239 /* Update the interval. */
240 i
= update_interval (i
, charpos
);
241 if (i
->position
!= gl_state
.e_property
)
244 gl_state
.backward_i
= i
;
245 gl_state
.b_property
= i
->position
- gl_state
.offset
;
250 tmp_table
= textget (i
->plist
, Qsyntax_table
);
253 invalidate
= !EQ (tmp_table
, gl_state
.old_prop
); /* Need to invalidate? */
255 if (invalidate
) /* Did not get to adjacent interval. */
256 { /* with the same table => */
257 /* invalidate the old range. */
260 gl_state
.backward_i
= i
;
261 gl_state
.b_property
= i
->position
- gl_state
.offset
;
265 gl_state
.forward_i
= i
;
266 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
270 if (!EQ (tmp_table
, gl_state
.old_prop
))
272 gl_state
.current_syntax_table
= tmp_table
;
273 gl_state
.old_prop
= tmp_table
;
274 if (EQ (Fsyntax_table_p (tmp_table
), Qt
))
276 gl_state
.use_global
= 0;
278 else if (CONSP (tmp_table
))
280 gl_state
.use_global
= 1;
281 gl_state
.global_code
= tmp_table
;
285 gl_state
.use_global
= 0;
286 gl_state
.current_syntax_table
= BVAR (current_buffer
, syntax_table
);
290 while (!NULL_INTERVAL_P (i
))
292 if (cnt
&& !EQ (tmp_table
, textget (i
->plist
, Qsyntax_table
)))
296 gl_state
.e_property
= i
->position
- gl_state
.offset
;
297 gl_state
.forward_i
= i
;
302 = i
->position
+ LENGTH (i
) - gl_state
.offset
;
303 gl_state
.backward_i
= i
;
307 else if (cnt
== INTERVALS_AT_ONCE
)
312 = i
->position
+ LENGTH (i
) - gl_state
.offset
313 /* e_property at EOB is not set to ZV but to ZV+1, so that
314 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
315 having to check eob between the two. */
316 + (NULL_INTERVAL_P (next_interval (i
)) ? 1 : 0);
317 gl_state
.forward_i
= i
;
321 gl_state
.b_property
= i
->position
- gl_state
.offset
;
322 gl_state
.backward_i
= i
;
327 i
= count
> 0 ? next_interval (i
) : previous_interval (i
);
329 eassert (NULL_INTERVAL_P (i
)); /* This property goes to the end. */
331 gl_state
.e_property
= gl_state
.stop
;
333 gl_state
.b_property
= gl_state
.start
;
336 /* Returns TRUE if char at CHARPOS is quoted.
337 Global syntax-table data should be set up already to be good at CHARPOS
338 or after. On return global syntax data is good for lookup at CHARPOS. */
341 char_quoted (ptrdiff_t charpos
, ptrdiff_t bytepos
)
343 register enum syntaxcode code
;
344 register ptrdiff_t beg
= BEGV
;
345 register int quoted
= 0;
346 ptrdiff_t orig
= charpos
;
348 while (charpos
> beg
)
351 DEC_BOTH (charpos
, bytepos
);
353 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
);
354 c
= FETCH_CHAR_AS_MULTIBYTE (bytepos
);
356 if (! (code
== Scharquote
|| code
== Sescape
))
362 UPDATE_SYNTAX_TABLE (orig
);
366 /* Return the bytepos one character before BYTEPOS.
367 We assume that BYTEPOS is not at the start of the buffer. */
369 static inline ptrdiff_t
370 dec_bytepos (ptrdiff_t bytepos
)
372 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
379 /* Return a defun-start position before POS and not too far before.
380 It should be the last one before POS, or nearly the last.
382 When open_paren_in_column_0_is_defun_start is nonzero,
383 only the beginning of the buffer is treated as a defun-start.
385 We record the information about where the scan started
386 and what its result was, so that another call in the same area
387 can return the same value very quickly.
389 There is no promise at which position the global syntax data is
390 valid on return from the subroutine, so the caller should explicitly
391 update the global data. */
394 find_defun_start (ptrdiff_t pos
, ptrdiff_t pos_byte
)
396 ptrdiff_t opoint
= PT
, opoint_byte
= PT_BYTE
;
398 if (!open_paren_in_column_0_is_defun_start
)
400 find_start_value
= BEGV
;
401 find_start_value_byte
= BEGV_BYTE
;
402 find_start_buffer
= current_buffer
;
403 find_start_modiff
= MODIFF
;
404 find_start_begv
= BEGV
;
405 find_start_pos
= pos
;
409 /* Use previous finding, if it's valid and applies to this inquiry. */
410 if (current_buffer
== find_start_buffer
411 /* Reuse the defun-start even if POS is a little farther on.
412 POS might be in the next defun, but that's ok.
413 Our value may not be the best possible, but will still be usable. */
414 && pos
<= find_start_pos
+ 1000
415 && pos
>= find_start_value
416 && BEGV
== find_start_begv
417 && MODIFF
== find_start_modiff
)
418 return find_start_value
;
420 /* Back up to start of line. */
421 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
423 /* We optimize syntax-table lookup for rare updates. Thus we accept
424 only those `^\s(' which are good in global _and_ text-property
426 SETUP_BUFFER_SYNTAX_TABLE ();
431 /* Open-paren at start of line means we may have found our
433 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
434 if (SYNTAX (c
) == Sopen
)
436 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
437 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
438 if (SYNTAX (c
) == Sopen
)
440 /* Now fallback to the default value. */
441 SETUP_BUFFER_SYNTAX_TABLE ();
443 /* Move to beg of previous line. */
444 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
447 /* Record what we found, for the next try. */
448 find_start_value
= PT
;
449 find_start_value_byte
= PT_BYTE
;
450 find_start_buffer
= current_buffer
;
451 find_start_modiff
= MODIFF
;
452 find_start_begv
= BEGV
;
453 find_start_pos
= pos
;
455 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
457 return find_start_value
;
460 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
463 prev_char_comend_first (ptrdiff_t pos
, ptrdiff_t pos_byte
)
467 DEC_BOTH (pos
, pos_byte
);
468 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
469 c
= FETCH_CHAR (pos_byte
);
470 val
= SYNTAX_COMEND_FIRST (c
);
471 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
475 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
478 * prev_char_comstart_first (pos, pos_byte)
483 * DEC_BOTH (pos, pos_byte);
484 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
485 * c = FETCH_CHAR (pos_byte);
486 * val = SYNTAX_COMSTART_FIRST (c);
487 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
491 /* Checks whether charpos FROM is at the end of a comment.
492 FROM_BYTE is the bytepos corresponding to FROM.
493 Do not move back before STOP.
495 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
498 If successful, store the charpos of the comment's beginning
499 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
501 Global syntax data remains valid for backward search starting at
502 the returned value (or at FROM, if the search was not successful). */
505 back_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
, int comnested
, int comstyle
, ptrdiff_t *charpos_ptr
, ptrdiff_t *bytepos_ptr
)
507 /* Look back, counting the parity of string-quotes,
508 and recording the comment-starters seen.
509 When we reach a safe place, assume that's not in a string;
510 then step the main scan to the earliest comment-starter seen
511 an even number of string quotes away from the safe place.
513 OFROM[I] is position of the earliest comment-starter seen
514 which is I+2X quotes from the comment-end.
515 PARITY is current parity of quotes from the comment end. */
516 int string_style
= -1; /* Presumed outside of any string. */
517 int string_lossage
= 0;
518 /* Not a real lossage: indicates that we have passed a matching comment
519 starter plus a non-matching comment-ender, meaning that any matching
520 comment-starter we might see later could be a false positive (hidden
521 inside another comment).
522 Test case: { a (* b } c (* d *) */
523 int comment_lossage
= 0;
524 ptrdiff_t comment_end
= from
;
525 ptrdiff_t comment_end_byte
= from_byte
;
526 ptrdiff_t comstart_pos
= 0;
527 ptrdiff_t comstart_byte
IF_LINT (= 0);
528 /* Place where the containing defun starts,
529 or 0 if we didn't come across it yet. */
530 ptrdiff_t defun_start
= 0;
531 ptrdiff_t defun_start_byte
= 0;
532 register enum syntaxcode code
;
533 int nesting
= 1; /* current comment nesting */
537 /* FIXME: A }} comment-ender style leads to incorrect behavior
538 in the case of {{ c }}} because we ignore the last two chars which are
539 assumed to be comment-enders although they aren't. */
541 /* At beginning of range to scan, we're outside of strings;
542 that determines quote parity to the comment-end. */
546 int prev_syntax
, com2start
, com2end
;
549 /* Move back and examine a character. */
550 DEC_BOTH (from
, from_byte
);
551 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
553 prev_syntax
= syntax
;
554 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
555 syntax
= SYNTAX_WITH_FLAGS (c
);
558 /* Check for 2-char comment markers. */
559 com2start
= (SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
560 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax
)
562 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax
, syntax
))
563 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
564 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
);
565 com2end
= (SYNTAX_FLAGS_COMEND_FIRST (syntax
)
566 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax
));
567 comstart
= (com2start
|| code
== Scomment
);
569 /* Nasty cases with overlapping 2-char comment markers:
570 - snmp-mode: -- c -- foo -- c --
578 /* If a 2-char comment sequence partly overlaps with another,
579 we don't try to be clever. E.g. |*| in C, or }% in modes that
580 have %..\n and %{..}%. */
581 if (from
> stop
&& (com2end
|| comstart
))
583 ptrdiff_t next
= from
, next_byte
= from_byte
;
584 int next_c
, next_syntax
;
585 DEC_BOTH (next
, next_byte
);
586 UPDATE_SYNTAX_TABLE_BACKWARD (next
);
587 next_c
= FETCH_CHAR_AS_MULTIBYTE (next_byte
);
588 next_syntax
= SYNTAX_WITH_FLAGS (next_c
);
589 if (((comstart
|| comnested
)
590 && SYNTAX_FLAGS_COMEND_SECOND (syntax
)
591 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax
))
592 || ((com2end
|| comnested
)
593 && SYNTAX_FLAGS_COMSTART_SECOND (syntax
)
595 == SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_syntax
))
596 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax
)))
598 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
601 if (com2start
&& comstart_pos
== 0)
602 /* We're looking at a comment starter. But it might be a comment
603 ender as well (see snmp-mode). The first time we see one, we
604 need to consider it as a comment starter,
605 and the subsequent times as a comment ender. */
608 /* Turn a 2-char comment sequences into the appropriate syntax. */
613 /* Ignore comment starters of a different style. */
614 else if (code
== Scomment
615 && (comstyle
!= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0)
616 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
) != comnested
))
619 /* Ignore escaped characters, except comment-enders. */
620 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
627 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
629 /* Track parity of quotes. */
630 if (string_style
== -1)
631 /* Entering a string. */
633 else if (string_style
== c
)
634 /* Leaving the string. */
637 /* If we have two kinds of string delimiters.
638 There's no way to grok this scanning backwards. */
643 /* We've already checked that it is the relevant comstyle. */
644 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
645 /* There are odd string quotes involved, so let's be careful.
646 Test case in Pascal: " { " a { " } */
651 /* Record best comment-starter so far. */
653 comstart_byte
= from_byte
;
655 else if (--nesting
<= 0)
656 /* nested comments have to be balanced, so we don't need to
657 keep looking for earlier ones. We use here the same (slightly
658 incorrect) reasoning as below: since it is followed by uniform
659 paired string quotes, this comment-start has to be outside of
660 strings, else the comment-end itself would be inside a string. */
665 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == comstyle
666 && ((com2end
&& SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
))
667 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
668 /* This is the same style of comment ender as ours. */
673 /* Anything before that can't count because it would match
674 this comment-ender rather than ours. */
675 from
= stop
; /* Break out of the loop. */
677 else if (comstart_pos
!= 0 || c
!= '\n')
678 /* We're mixing comment styles here, so we'd better be careful.
679 The (comstart_pos != 0 || c != '\n') check is not quite correct
680 (we should just always set comment_lossage), but removing it
681 would imply that any multiline comment in C would go through
682 lossage, which seems overkill.
683 The failure should only happen in the rare cases such as
689 /* Assume a defun-start point is outside of strings. */
690 if (open_paren_in_column_0_is_defun_start
692 || (temp_byte
= dec_bytepos (from_byte
),
693 FETCH_CHAR (temp_byte
) == '\n')))
696 defun_start_byte
= from_byte
;
697 from
= stop
; /* Break out of the loop. */
706 if (comstart_pos
== 0)
709 from_byte
= comment_end_byte
;
710 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
- 1);
712 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
713 or `done'), then we've found the beginning of the non-nested comment. */
714 else if (1) /* !comnested */
717 from_byte
= comstart_byte
;
718 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
722 struct lisp_parse_state state
;
724 /* We had two kinds of string delimiters mixed up
725 together. Decode this going forwards.
726 Scan fwd from a known safe place (beginning-of-defun)
727 to the one in question; this records where we
728 last passed a comment starter. */
729 /* If we did not already find the defun start, find it now. */
730 if (defun_start
== 0)
732 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
733 defun_start_byte
= find_start_value_byte
;
737 scan_sexps_forward (&state
,
738 defun_start
, defun_start_byte
,
739 comment_end
, TYPE_MINIMUM (EMACS_INT
),
741 defun_start
= comment_end
;
742 if (state
.incomment
== (comnested
? 1 : -1)
743 && state
.comstyle
== comstyle
)
744 from
= state
.comstr_start
;
749 /* If comment_end is inside some other comment, maybe ours
750 is nested, so we need to try again from within the
751 surrounding comment. Example: { a (* " *) */
753 /* FIXME: We should advance by one or two chars. */
754 defun_start
= state
.comstr_start
+ 2;
755 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
758 } while (defun_start
< comment_end
);
760 from_byte
= CHAR_TO_BYTE (from
);
761 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
766 *bytepos_ptr
= from_byte
;
768 return (from
== comment_end
) ? -1 : from
;
771 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
772 doc
: /* Return t if OBJECT is a syntax table.
773 Currently, any char-table counts as a syntax table. */)
776 if (CHAR_TABLE_P (object
)
777 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
783 check_syntax_table (Lisp_Object obj
)
785 CHECK_TYPE (CHAR_TABLE_P (obj
) && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
),
786 Qsyntax_table_p
, obj
);
789 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
790 doc
: /* Return the current syntax table.
791 This is the one specified by the current buffer. */)
794 return BVAR (current_buffer
, syntax_table
);
797 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
798 Sstandard_syntax_table
, 0, 0, 0,
799 doc
: /* Return the standard syntax table.
800 This is the one used for new buffers. */)
803 return Vstandard_syntax_table
;
806 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
807 doc
: /* Construct a new syntax table and return it.
808 It is a copy of the TABLE, which defaults to the standard syntax table. */)
814 check_syntax_table (table
);
816 table
= Vstandard_syntax_table
;
818 copy
= Fcopy_sequence (table
);
820 /* Only the standard syntax table should have a default element.
821 Other syntax tables should inherit from parents instead. */
822 XCHAR_TABLE (copy
)->defalt
= Qnil
;
824 /* Copied syntax tables should all have parents.
825 If we copied one with no parent, such as the standard syntax table,
826 use the standard syntax table as the copy's parent. */
827 if (NILP (XCHAR_TABLE (copy
)->parent
))
828 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
832 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
833 doc
: /* Select a new syntax table for the current buffer.
834 One argument, a syntax table. */)
838 check_syntax_table (table
);
839 BVAR (current_buffer
, syntax_table
) = table
;
840 /* Indicate that this buffer now has a specified syntax table. */
841 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
842 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
846 /* Convert a letter which signifies a syntax code
847 into the code it signifies.
848 This is used by modify-syntax-entry, and other things. */
850 unsigned char syntax_spec_code
[0400] =
851 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
852 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
853 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
854 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
855 (char) Swhitespace
, (char) Scomment_fence
, (char) Sstring
, 0377,
856 (char) Smath
, 0377, 0377, (char) Squote
,
857 (char) Sopen
, (char) Sclose
, 0377, 0377,
858 0377, (char) Swhitespace
, (char) Spunct
, (char) Scharquote
,
859 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
860 0377, 0377, 0377, 0377,
861 (char) Scomment
, 0377, (char) Sendcomment
, 0377,
862 (char) Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
863 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
864 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
865 0377, 0377, 0377, 0377, (char) Sescape
, 0377, 0377, (char) Ssymbol
,
866 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
867 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
868 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
869 0377, 0377, 0377, 0377, (char) Sstring_fence
, 0377, 0377, 0377
872 /* Indexed by syntax code, give the letter that describes it. */
874 char syntax_code_spec
[16] =
876 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
880 /* Indexed by syntax code, give the object (cons of syntax code and
881 nil) to be stored in syntax table. Since these objects can be
882 shared among syntax tables, we generate them in advance. By
883 sharing objects, the function `describe-syntax' can give a more
885 static Lisp_Object Vsyntax_code_object
;
888 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
889 doc
: /* Return the syntax code of CHARACTER, described by a character.
890 For example, if CHARACTER is a word constituent, the
891 character `w' (119) is returned.
892 The characters that correspond to various syntax codes
893 are listed in the documentation of `modify-syntax-entry'. */)
894 (Lisp_Object character
)
897 CHECK_CHARACTER (character
);
898 char_int
= XINT (character
);
899 SETUP_BUFFER_SYNTAX_TABLE ();
900 return make_number (syntax_code_spec
[(int) SYNTAX (char_int
)]);
903 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
904 doc
: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
905 (Lisp_Object character
)
908 CHECK_NUMBER (character
);
909 char_int
= XINT (character
);
910 SETUP_BUFFER_SYNTAX_TABLE ();
911 code
= SYNTAX (char_int
);
912 if (code
== Sopen
|| code
== Sclose
)
913 return SYNTAX_MATCH (char_int
);
917 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
918 doc
: /* Convert a syntax specification STRING into syntax cell form.
919 STRING should be a string as it is allowed as argument of
920 `modify-syntax-entry'. Value is the equivalent cons cell
921 \(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
925 register const unsigned char *p
;
926 register enum syntaxcode code
;
930 CHECK_STRING (string
);
933 code
= (enum syntaxcode
) syntax_spec_code
[*p
++];
934 if (((int) code
& 0377) == 0377)
935 error ("Invalid syntax description letter: %c", p
[-1]);
937 if (code
== Sinherit
)
943 int character
= STRING_CHAR_AND_LENGTH (p
, len
);
944 XSETINT (match
, character
);
945 if (XFASTINT (match
) == ' ')
989 if (val
< ASIZE (Vsyntax_code_object
) && NILP (match
))
990 return AREF (Vsyntax_code_object
, val
);
992 /* Since we can't use a shared object, let's make a new one. */
993 return Fcons (make_number (val
), match
);
996 /* I really don't know why this is interactive
997 help-form should at least be made useful whilst reading the second arg. */
998 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
999 "cSet syntax for character: \nsSet syntax for %s to: ",
1000 doc
: /* Set syntax for character CHAR according to string NEWENTRY.
1001 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1002 the current buffer's syntax table.
1003 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1004 in the range MIN to MAX are changed.
1005 The first character of NEWENTRY should be one of the following:
1006 Space or - whitespace syntax. w word constituent.
1007 _ symbol constituent. . punctuation.
1008 ( open-parenthesis. ) close-parenthesis.
1009 " string quote. \\ escape.
1010 $ paired delimiter. ' expression quote or prefix operator.
1011 < comment starter. > comment ender.
1012 / character-quote. @ inherit from `standard-syntax-table'.
1013 | generic string fence. ! generic comment fence.
1015 Only single-character comment start and end sequences are represented thus.
1016 Two-character sequences are represented as described below.
1017 The second character of NEWENTRY is the matching parenthesis,
1018 used only if the first character is `(' or `)'.
1019 Any additional characters are flags.
1020 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1021 1 means CHAR is the start of a two-char comment start sequence.
1022 2 means CHAR is the second character of such a sequence.
1023 3 means CHAR is the start of a two-char comment end sequence.
1024 4 means CHAR is the second character of such a sequence.
1026 There can be several orthogonal comment sequences. This is to support
1027 language modes such as C++. By default, all comment sequences are of style
1028 a, but you can set the comment sequence style to b (on the second character
1029 of a comment-start, and the first character of a comment-end sequence) and/or
1030 c (on any of its chars) using this flag:
1031 b means CHAR is part of comment sequence b.
1032 c means CHAR is part of comment sequence c.
1033 n means CHAR is part of a nestable comment sequence.
1035 p means CHAR is a prefix character for `backward-prefix-chars';
1036 such characters are treated as whitespace when they occur
1037 between expressions.
1038 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1039 (Lisp_Object c
, Lisp_Object newentry
, Lisp_Object syntax_table
)
1043 CHECK_CHARACTER_CAR (c
);
1044 CHECK_CHARACTER_CDR (c
);
1047 CHECK_CHARACTER (c
);
1049 if (NILP (syntax_table
))
1050 syntax_table
= BVAR (current_buffer
, syntax_table
);
1052 check_syntax_table (syntax_table
);
1054 newentry
= Fstring_to_syntax (newentry
);
1056 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table
, c
, newentry
);
1058 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), newentry
);
1060 /* We clear the regexp cache, since character classes can now have
1061 different values from those in the compiled regexps.*/
1062 clear_regexp_cache ();
1067 /* Dump syntax table to buffer in human-readable format */
1069 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value
,
1070 Sinternal_describe_syntax_value
, 1, 1, 0,
1071 doc
: /* Insert a description of the internal syntax description SYNTAX at point. */)
1072 (Lisp_Object syntax
)
1074 register enum syntaxcode code
;
1076 char desc
, start1
, start2
, end1
, end2
, prefix
,
1077 comstyleb
, comstylec
, comnested
;
1079 Lisp_Object first
, match_lisp
, value
= syntax
;
1083 insert_string ("default");
1087 if (CHAR_TABLE_P (value
))
1089 insert_string ("deeper char-table ...");
1095 insert_string ("invalid");
1099 first
= XCAR (value
);
1100 match_lisp
= XCDR (value
);
1102 if (!INTEGERP (first
) || !(NILP (match_lisp
) || CHARACTERP (match_lisp
)))
1104 insert_string ("invalid");
1108 syntax_code
= XINT (first
) & INT_MAX
;
1109 code
= (enum syntaxcode
) (syntax_code
& 0377);
1110 start1
= SYNTAX_FLAGS_COMSTART_FIRST (syntax_code
);
1111 start2
= SYNTAX_FLAGS_COMSTART_SECOND (syntax_code
);;
1112 end1
= SYNTAX_FLAGS_COMEND_FIRST (syntax_code
);
1113 end2
= SYNTAX_FLAGS_COMEND_SECOND (syntax_code
);
1114 prefix
= SYNTAX_FLAGS_PREFIX (syntax_code
);
1115 comstyleb
= SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code
);
1116 comstylec
= SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code
);
1117 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax_code
);
1119 if ((int) code
< 0 || (int) code
>= (int) Smax
)
1121 insert_string ("invalid");
1124 desc
= syntax_code_spec
[(int) code
];
1126 str
[0] = desc
, str
[1] = 0;
1129 if (NILP (match_lisp
))
1132 insert_char (XINT (match_lisp
));
1153 insert_string ("\twhich means: ");
1155 switch (SWITCH_ENUM_CAST (code
))
1158 insert_string ("whitespace"); break;
1160 insert_string ("punctuation"); break;
1162 insert_string ("word"); break;
1164 insert_string ("symbol"); break;
1166 insert_string ("open"); break;
1168 insert_string ("close"); break;
1170 insert_string ("prefix"); break;
1172 insert_string ("string"); break;
1174 insert_string ("math"); break;
1176 insert_string ("escape"); break;
1178 insert_string ("charquote"); break;
1180 insert_string ("comment"); break;
1182 insert_string ("endcomment"); break;
1184 insert_string ("inherit"); break;
1185 case Scomment_fence
:
1186 insert_string ("comment fence"); break;
1188 insert_string ("string fence"); break;
1190 insert_string ("invalid");
1194 if (!NILP (match_lisp
))
1196 insert_string (", matches ");
1197 insert_char (XINT (match_lisp
));
1201 insert_string (",\n\t is the first character of a comment-start sequence");
1203 insert_string (",\n\t is the second character of a comment-start sequence");
1206 insert_string (",\n\t is the first character of a comment-end sequence");
1208 insert_string (",\n\t is the second character of a comment-end sequence");
1210 insert_string (" (comment style b)");
1212 insert_string (" (comment style c)");
1214 insert_string (" (nestable)");
1217 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1222 /* Return the position across COUNT words from FROM.
1223 If that many words cannot be found before the end of the buffer, return 0.
1224 COUNT negative means scan backward and stop at word beginning. */
1227 scan_words (register ptrdiff_t from
, register EMACS_INT count
)
1229 register ptrdiff_t beg
= BEGV
;
1230 register ptrdiff_t end
= ZV
;
1231 register ptrdiff_t from_byte
= CHAR_TO_BYTE (from
);
1232 register enum syntaxcode code
;
1234 Lisp_Object func
, pos
;
1239 SETUP_SYNTAX_TABLE (from
, count
);
1250 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1251 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1252 code
= SYNTAX (ch0
);
1253 INC_BOTH (from
, from_byte
);
1254 if (words_include_escapes
1255 && (code
== Sescape
|| code
== Scharquote
))
1260 /* Now CH0 is a character which begins a word and FROM is the
1261 position of the next character. */
1262 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch0
);
1263 if (! NILP (Ffboundp (func
)))
1265 pos
= call2 (func
, make_number (from
- 1), make_number (end
));
1266 if (INTEGERP (pos
) && from
< XINT (pos
) && XINT (pos
) <= ZV
)
1269 from_byte
= CHAR_TO_BYTE (from
);
1276 if (from
== end
) break;
1277 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1278 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1279 code
= SYNTAX (ch1
);
1281 && (! words_include_escapes
1282 || (code
!= Sescape
&& code
!= Scharquote
)))
1283 || word_boundary_p (ch0
, ch1
))
1285 INC_BOTH (from
, from_byte
);
1300 DEC_BOTH (from
, from_byte
);
1301 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1302 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1303 code
= SYNTAX (ch1
);
1304 if (words_include_escapes
1305 && (code
== Sescape
|| code
== Scharquote
))
1310 /* Now CH1 is a character which ends a word and FROM is the
1312 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch1
);
1313 if (! NILP (Ffboundp (func
)))
1315 pos
= call2 (func
, make_number (from
), make_number (beg
));
1316 if (INTEGERP (pos
) && BEGV
<= XINT (pos
) && XINT (pos
) < from
)
1319 from_byte
= CHAR_TO_BYTE (from
);
1328 DEC_BOTH (from
, from_byte
);
1329 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1330 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1331 code
= SYNTAX (ch0
);
1333 && (! words_include_escapes
1334 || (code
!= Sescape
&& code
!= Scharquote
)))
1335 || word_boundary_p (ch0
, ch1
))
1337 INC_BOTH (from
, from_byte
);
1351 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 0, 1, "^p",
1352 doc
: /* Move point forward ARG words (backward if ARG is negative).
1354 If an edge of the buffer or a field boundary is reached, point is left there
1355 and the function returns nil. Field boundaries are not noticed if
1356 `inhibit-field-text-motion' is non-nil. */)
1360 ptrdiff_t orig_val
, val
;
1363 XSETFASTINT (arg
, 1);
1367 val
= orig_val
= scan_words (PT
, XINT (arg
));
1369 val
= XINT (arg
) > 0 ? ZV
: BEGV
;
1371 /* Avoid jumping out of an input field. */
1372 tmp
= Fconstrain_to_field (make_number (val
), make_number (PT
),
1374 val
= XFASTINT (tmp
);
1377 return val
== orig_val
? Qt
: Qnil
;
1380 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1381 doc
: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1382 STRING is like the inside of a `[...]' in a regular expression
1383 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1384 (but not at the end of a range; quoting is never needed there).
1385 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1386 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1387 Char classes, e.g. `[:alpha:]', are supported.
1389 Returns the distance traveled, either zero or positive. */)
1390 (Lisp_Object string
, Lisp_Object lim
)
1392 return skip_chars (1, string
, lim
, 1);
1395 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1396 doc
: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1397 See `skip-chars-forward' for details.
1398 Returns the distance traveled, either zero or negative. */)
1399 (Lisp_Object string
, Lisp_Object lim
)
1401 return skip_chars (0, string
, lim
, 1);
1404 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1405 doc
: /* Move point forward across chars in specified syntax classes.
1406 SYNTAX is a string of syntax code characters.
1407 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1408 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1409 This function returns the distance traveled, either zero or positive. */)
1410 (Lisp_Object syntax
, Lisp_Object lim
)
1412 return skip_syntaxes (1, syntax
, lim
);
1415 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1416 doc
: /* Move point backward across chars in specified syntax classes.
1417 SYNTAX is a string of syntax code characters.
1418 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1419 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1420 This function returns the distance traveled, either zero or negative. */)
1421 (Lisp_Object syntax
, Lisp_Object lim
)
1423 return skip_syntaxes (0, syntax
, lim
);
1427 skip_chars (int forwardp
, Lisp_Object string
, Lisp_Object lim
, int handle_iso_classes
)
1429 register unsigned int c
;
1430 unsigned char fastmap
[0400];
1431 /* Store the ranges of non-ASCII characters. */
1432 int *char_ranges
IF_LINT (= NULL
);
1433 int n_char_ranges
= 0;
1435 register ptrdiff_t i
, i_byte
;
1436 /* Set to 1 if the current buffer is multibyte and the region
1437 contains non-ASCII chars. */
1439 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1441 int string_multibyte
;
1442 ptrdiff_t size_byte
;
1443 const unsigned char *str
;
1445 Lisp_Object iso_classes
;
1447 CHECK_STRING (string
);
1451 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1453 CHECK_NUMBER_COERCE_MARKER (lim
);
1455 /* In any case, don't allow scan outside bounds of buffer. */
1456 if (XINT (lim
) > ZV
)
1457 XSETFASTINT (lim
, ZV
);
1458 if (XINT (lim
) < BEGV
)
1459 XSETFASTINT (lim
, BEGV
);
1461 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1462 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1463 string_multibyte
= SBYTES (string
) > SCHARS (string
);
1465 memset (fastmap
, 0, sizeof fastmap
);
1467 str
= SDATA (string
);
1468 size_byte
= SBYTES (string
);
1471 if (i_byte
< size_byte
1472 && SREF (string
, 0) == '^')
1474 negate
= 1; i_byte
++;
1477 /* Find the characters specified and set their elements of fastmap.
1478 Handle backslashes and ranges specially.
1480 If STRING contains non-ASCII characters, setup char_ranges for
1481 them and use fastmap only for their leading codes. */
1483 if (! string_multibyte
)
1485 int string_has_eight_bit
= 0;
1487 /* At first setup fastmap. */
1488 while (i_byte
< size_byte
)
1492 if (handle_iso_classes
&& c
== '['
1493 && i_byte
< size_byte
1494 && str
[i_byte
] == ':')
1496 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1497 const unsigned char *class_end
= class_beg
;
1498 const unsigned char *class_limit
= str
+ size_byte
- 2;
1499 /* Leave room for the null. */
1500 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1503 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1504 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1506 while (class_end
< class_limit
1507 && *class_end
>= 'a' && *class_end
<= 'z')
1510 if (class_end
== class_beg
1511 || *class_end
!= ':' || class_end
[1] != ']')
1512 goto not_a_class_name
;
1514 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1515 class_name
[class_end
- class_beg
] = 0;
1517 cc
= re_wctype (class_name
);
1519 error ("Invalid ISO C character class");
1521 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1523 i_byte
= class_end
+ 2 - str
;
1530 if (i_byte
== size_byte
)
1535 /* Treat `-' as range character only if another character
1537 if (i_byte
+ 1 < size_byte
1538 && str
[i_byte
] == '-')
1542 /* Skip over the dash. */
1545 /* Get the end of the range. */
1548 && i_byte
< size_byte
)
1553 unsigned lim2
= c2
+ 1;
1556 if (! ASCII_CHAR_P (c2
))
1557 string_has_eight_bit
= 1;
1563 if (! ASCII_CHAR_P (c
))
1564 string_has_eight_bit
= 1;
1568 /* If the current range is multibyte and STRING contains
1569 eight-bit chars, arrange fastmap and setup char_ranges for
1570 the corresponding multibyte chars. */
1571 if (multibyte
&& string_has_eight_bit
)
1573 unsigned char fastmap2
[0400];
1574 int range_start_byte
, range_start_char
;
1576 memcpy (fastmap
+ 0200, fastmap2
+ 0200, 0200);
1577 memset (fastmap
+ 0200, 0, 0200);
1578 /* We are sure that this loop stops. */
1579 for (i
= 0200; ! fastmap2
[i
]; i
++);
1580 c
= BYTE8_TO_CHAR (i
);
1581 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
1582 range_start_byte
= i
;
1583 range_start_char
= c
;
1584 char_ranges
= alloca (sizeof *char_ranges
* 128 * 2);
1585 for (i
= 129; i
< 0400; i
++)
1587 c
= BYTE8_TO_CHAR (i
);
1588 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
1589 if (i
- range_start_byte
!= c
- range_start_char
)
1591 char_ranges
[n_char_ranges
++] = range_start_char
;
1592 char_ranges
[n_char_ranges
++] = ((i
- 1 - range_start_byte
)
1593 + range_start_char
);
1594 range_start_byte
= i
;
1595 range_start_char
= c
;
1598 char_ranges
[n_char_ranges
++] = range_start_char
;
1599 char_ranges
[n_char_ranges
++] = ((i
- 1 - range_start_byte
)
1600 + range_start_char
);
1603 else /* STRING is multibyte */
1605 char_ranges
= alloca (sizeof *char_ranges
* SCHARS (string
) * 2);
1607 while (i_byte
< size_byte
)
1609 unsigned char leading_code
;
1611 leading_code
= str
[i_byte
];
1612 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1615 if (handle_iso_classes
&& c
== '['
1616 && i_byte
< size_byte
1617 && STRING_CHAR (str
+ i_byte
) == ':')
1619 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1620 const unsigned char *class_end
= class_beg
;
1621 const unsigned char *class_limit
= str
+ size_byte
- 2;
1622 /* Leave room for the null. */
1623 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1626 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1627 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1629 while (class_end
< class_limit
1630 && *class_end
>= 'a' && *class_end
<= 'z')
1633 if (class_end
== class_beg
1634 || *class_end
!= ':' || class_end
[1] != ']')
1635 goto not_a_class_name_multibyte
;
1637 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1638 class_name
[class_end
- class_beg
] = 0;
1640 cc
= re_wctype (class_name
);
1642 error ("Invalid ISO C character class");
1644 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1646 i_byte
= class_end
+ 2 - str
;
1650 not_a_class_name_multibyte
:
1653 if (i_byte
== size_byte
)
1656 leading_code
= str
[i_byte
];
1657 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1660 /* Treat `-' as range character only if another character
1662 if (i_byte
+ 1 < size_byte
1663 && str
[i_byte
] == '-')
1666 unsigned char leading_code2
;
1668 /* Skip over the dash. */
1671 /* Get the end of the range. */
1672 leading_code2
= str
[i_byte
];
1673 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1677 && i_byte
< size_byte
)
1679 leading_code2
= str
[i_byte
];
1680 c2
=STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1686 if (ASCII_CHAR_P (c
))
1688 while (c
<= c2
&& c
< 0x80)
1690 leading_code
= CHAR_LEADING_CODE (c
);
1692 if (! ASCII_CHAR_P (c
))
1694 unsigned lim2
= leading_code2
+ 1;
1695 while (leading_code
< lim2
)
1696 fastmap
[leading_code
++] = 1;
1699 char_ranges
[n_char_ranges
++] = c
;
1700 char_ranges
[n_char_ranges
++] = c2
;
1706 if (ASCII_CHAR_P (c
))
1710 fastmap
[leading_code
] = 1;
1711 char_ranges
[n_char_ranges
++] = c
;
1712 char_ranges
[n_char_ranges
++] = c
;
1717 /* If the current range is unibyte and STRING contains non-ASCII
1718 chars, arrange fastmap for the corresponding unibyte
1721 if (! multibyte
&& n_char_ranges
> 0)
1723 memset (fastmap
+ 0200, 0, 0200);
1724 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1726 int c1
= char_ranges
[i
];
1727 unsigned lim2
= char_ranges
[i
+ 1] + 1;
1729 for (; c1
< lim2
; c1
++)
1731 int b
= CHAR_TO_BYTE_SAFE (c1
);
1739 /* If ^ was the first character, complement the fastmap. */
1743 for (i
= 0; i
< sizeof fastmap
; i
++)
1747 for (i
= 0; i
< 0200; i
++)
1749 /* All non-ASCII chars possibly match. */
1750 for (; i
< sizeof fastmap
; i
++)
1756 ptrdiff_t start_point
= PT
;
1758 ptrdiff_t pos_byte
= PT_BYTE
;
1759 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1763 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1764 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1768 endp
= CHAR_POS_ADDR (XINT (lim
));
1769 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1773 /* This code may look up syntax tables using macros that rely on the
1774 gl_state object. To make sure this object is not out of date,
1775 let's initialize it manually.
1776 We ignore syntax-table text-properties for now, since that's
1777 what we've done in the past. */
1778 SETUP_BUFFER_SYNTAX_TABLE ();
1793 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
1794 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1804 if (! ASCII_CHAR_P (c
))
1806 /* As we are looking at a multibyte character, we
1807 must look up the character in the table
1808 CHAR_RANGES. If there's no data in the table,
1809 that character is not what we want to skip. */
1811 /* The following code do the right thing even if
1812 n_char_ranges is zero (i.e. no data in
1814 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1815 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1817 if (!(negate
^ (i
< n_char_ranges
)))
1821 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
1834 if (!NILP (iso_classes
) && in_classes (*p
, iso_classes
))
1839 goto fwd_unibyte_ok
;
1845 p
++, pos
++, pos_byte
++;
1853 unsigned char *prev_p
;
1863 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
1864 c
= STRING_CHAR (p
);
1866 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1876 if (! ASCII_CHAR_P (c
))
1878 /* See the comment in the previous similar code. */
1879 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1880 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1882 if (!(negate
^ (i
< n_char_ranges
)))
1886 pos
--, pos_byte
-= prev_p
- p
;
1899 if (! NILP (iso_classes
) && in_classes (p
[-1], iso_classes
))
1904 goto back_unibyte_ok
;
1907 if (!fastmap
[p
[-1]])
1910 p
--, pos
--, pos_byte
--;
1914 SET_PT_BOTH (pos
, pos_byte
);
1917 return make_number (PT
- start_point
);
1923 skip_syntaxes (int forwardp
, Lisp_Object string
, Lisp_Object lim
)
1925 register unsigned int c
;
1926 unsigned char fastmap
[0400];
1928 register ptrdiff_t i
, i_byte
;
1930 ptrdiff_t size_byte
;
1933 CHECK_STRING (string
);
1936 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1938 CHECK_NUMBER_COERCE_MARKER (lim
);
1940 /* In any case, don't allow scan outside bounds of buffer. */
1941 if (XINT (lim
) > ZV
)
1942 XSETFASTINT (lim
, ZV
);
1943 if (XINT (lim
) < BEGV
)
1944 XSETFASTINT (lim
, BEGV
);
1946 if (forwardp
? (PT
>= XFASTINT (lim
)) : (PT
<= XFASTINT (lim
)))
1947 return make_number (0);
1949 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1950 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1952 memset (fastmap
, 0, sizeof fastmap
);
1954 if (SBYTES (string
) > SCHARS (string
))
1955 /* As this is very rare case (syntax spec is ASCII only), don't
1956 consider efficiency. */
1957 string
= string_make_unibyte (string
);
1959 str
= SDATA (string
);
1960 size_byte
= SBYTES (string
);
1963 if (i_byte
< size_byte
1964 && SREF (string
, 0) == '^')
1966 negate
= 1; i_byte
++;
1969 /* Find the syntaxes specified and set their elements of fastmap. */
1971 while (i_byte
< size_byte
)
1974 fastmap
[syntax_spec_code
[c
]] = 1;
1977 /* If ^ was the first character, complement the fastmap. */
1979 for (i
= 0; i
< sizeof fastmap
; i
++)
1983 ptrdiff_t start_point
= PT
;
1985 ptrdiff_t pos_byte
= PT_BYTE
;
1986 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1990 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1991 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1995 endp
= CHAR_POS_ADDR (XINT (lim
));
1996 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
2000 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
2016 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
2017 if (! fastmap
[(int) SYNTAX (c
)])
2019 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2020 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2034 if (! fastmap
[(int) SYNTAX (*p
)])
2036 p
++, pos
++, pos_byte
++;
2037 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2047 unsigned char *prev_p
;
2056 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2058 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2059 c
= STRING_CHAR (p
);
2060 if (! fastmap
[(int) SYNTAX (c
)])
2062 pos
--, pos_byte
-= prev_p
- p
;
2076 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2077 if (! fastmap
[(int) SYNTAX (p
[-1])])
2079 p
--, pos
--, pos_byte
--;
2084 SET_PT_BOTH (pos
, pos_byte
);
2087 return make_number (PT
- start_point
);
2091 /* Return 1 if character C belongs to one of the ISO classes
2092 in the list ISO_CLASSES. Each class is represented by an
2093 integer which is its type according to re_wctype. */
2096 in_classes (int c
, Lisp_Object iso_classes
)
2100 while (CONSP (iso_classes
))
2103 elt
= XCAR (iso_classes
);
2104 iso_classes
= XCDR (iso_classes
);
2106 if (re_iswctype (c
, XFASTINT (elt
)))
2113 /* Jump over a comment, assuming we are at the beginning of one.
2114 FROM is the current position.
2115 FROM_BYTE is the bytepos corresponding to FROM.
2116 Do not move past STOP (a charpos).
2117 The comment over which we have to jump is of style STYLE
2118 (either SYNTAX_FLAGS_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2119 NESTING should be positive to indicate the nesting at the beginning
2120 for nested comments and should be zero or negative else.
2121 ST_COMMENT_STYLE cannot be nested.
2122 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2123 (or 0 If the search cannot start in the middle of a two-character).
2125 If successful, return 1 and store the charpos of the comment's end
2126 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2127 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2128 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2129 (as defined for state.incomment) in *INCOMMENT_PTR.
2131 The comment end is the last character of the comment rather than the
2132 character just after the comment.
2134 Global syntax data is assumed to initially be valid for FROM and
2135 remains valid for forward search starting at the returned position. */
2138 forw_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
,
2139 EMACS_INT nesting
, int style
, int prev_syntax
,
2140 ptrdiff_t *charpos_ptr
, ptrdiff_t *bytepos_ptr
,
2141 EMACS_INT
*incomment_ptr
)
2144 register enum syntaxcode code
;
2145 register int syntax
, other_syntax
;
2147 if (nesting
<= 0) nesting
= -1;
2149 /* Enter the loop in the middle so that we find
2150 a 2-char comment ender if we start in the middle of it. */
2151 syntax
= prev_syntax
;
2152 if (syntax
!= 0) goto forw_incomment
;
2158 *incomment_ptr
= nesting
;
2159 *charpos_ptr
= from
;
2160 *bytepos_ptr
= from_byte
;
2163 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2164 syntax
= SYNTAX_WITH_FLAGS (c
);
2165 code
= syntax
& 0xff;
2166 if (code
== Sendcomment
2167 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
2168 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
2169 (nesting
> 0 && --nesting
== 0) : nesting
< 0))
2170 /* we have encountered a comment end of the same style
2171 as the comment sequence which began this comment
2174 if (code
== Scomment_fence
2175 && style
== ST_COMMENT_STYLE
)
2176 /* we have encountered a comment end of the same style
2177 as the comment sequence which began this comment
2182 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
2183 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
)
2184 /* we have encountered a nested comment of the same style
2185 as the comment sequence which began this comment section */
2187 INC_BOTH (from
, from_byte
);
2188 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2191 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2192 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2193 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2194 SYNTAX_FLAGS_COMEND_SECOND (other_syntax
))
2195 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, other_syntax
) == style
2196 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2197 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
))
2198 ? nesting
> 0 : nesting
< 0))
2201 /* we have encountered a comment end of the same style
2202 as the comment sequence which began this comment
2207 INC_BOTH (from
, from_byte
);
2208 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2213 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
2214 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2215 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2216 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
) == style
2217 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2218 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2219 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
)))
2220 /* we have encountered a nested comment of the same style
2221 as the comment sequence which began this comment
2224 INC_BOTH (from
, from_byte
);
2225 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2229 *charpos_ptr
= from
;
2230 *bytepos_ptr
= from_byte
;
2234 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
2236 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2237 Stop scanning if we find something other than a comment or whitespace.
2238 Set point to where scanning stops.
2239 If COUNT comments are found as expected, with nothing except whitespace
2240 between them, return t; otherwise return nil. */)
2243 register ptrdiff_t from
;
2244 ptrdiff_t from_byte
;
2245 register ptrdiff_t stop
;
2247 register enum syntaxcode code
;
2248 int comstyle
= 0; /* style of comment encountered */
2249 int comnested
= 0; /* whether the comment is nestable or not */
2252 ptrdiff_t out_charpos
, out_bytepos
;
2255 CHECK_NUMBER (count
);
2256 count1
= XINT (count
);
2257 stop
= count1
> 0 ? ZV
: BEGV
;
2263 from_byte
= PT_BYTE
;
2265 SETUP_SYNTAX_TABLE (from
, count1
);
2270 int comstart_first
, syntax
, other_syntax
;
2274 SET_PT_BOTH (from
, from_byte
);
2278 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2279 syntax
= SYNTAX_WITH_FLAGS (c
);
2281 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2282 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2283 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2284 INC_BOTH (from
, from_byte
);
2285 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2286 if (from
< stop
&& comstart_first
2287 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2288 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2289 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
)))
2291 /* We have encountered a comment start sequence and we
2292 are ignoring all text inside comments. We must record
2293 the comment style this sequence begins so that later,
2294 only a comment end of the same style actually ends
2295 the comment section. */
2297 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2299 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2300 INC_BOTH (from
, from_byte
);
2301 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2304 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
2306 if (code
== Scomment_fence
)
2307 comstyle
= ST_COMMENT_STYLE
;
2308 else if (code
!= Scomment
)
2311 DEC_BOTH (from
, from_byte
);
2312 SET_PT_BOTH (from
, from_byte
);
2315 /* We're at the start of a comment. */
2316 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
2317 &out_charpos
, &out_bytepos
, &dummy
);
2318 from
= out_charpos
; from_byte
= out_bytepos
;
2322 SET_PT_BOTH (from
, from_byte
);
2325 INC_BOTH (from
, from_byte
);
2326 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2327 /* We have skipped one comment. */
2339 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
2344 DEC_BOTH (from
, from_byte
);
2345 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2346 quoted
= char_quoted (from
, from_byte
);
2347 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2348 syntax
= SYNTAX_WITH_FLAGS (c
);
2351 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2352 if (code
== Sendcomment
)
2353 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2354 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2355 && prev_char_comend_first (from
, from_byte
)
2356 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
2359 /* We must record the comment style encountered so that
2360 later, we can match only the proper comment begin
2361 sequence of the same style. */
2362 DEC_BOTH (from
, from_byte
);
2364 /* Calling char_quoted, above, set up global syntax position
2365 at the new value of FROM. */
2366 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2367 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2368 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2370 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2373 if (code
== Scomment_fence
)
2375 /* Skip until first preceding unquoted comment_fence. */
2376 int fence_found
= 0;
2377 ptrdiff_t ini
= from
, ini_byte
= from_byte
;
2381 DEC_BOTH (from
, from_byte
);
2382 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2383 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2384 if (SYNTAX (c
) == Scomment_fence
2385 && !char_quoted (from
, from_byte
))
2390 else if (from
== stop
)
2393 if (fence_found
== 0)
2395 from
= ini
; /* Set point to ini + 1. */
2396 from_byte
= ini_byte
;
2400 /* We have skipped one comment. */
2403 else if (code
== Sendcomment
)
2405 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2406 &out_charpos
, &out_bytepos
);
2410 /* This end-of-line is not an end-of-comment.
2411 Treat it like a whitespace.
2412 CC-mode (and maybe others) relies on this behavior. */
2416 /* Failure: we should go back to the end of this
2417 not-quite-endcomment. */
2418 if (SYNTAX (c
) != code
)
2419 /* It was a two-char Sendcomment. */
2420 INC_BOTH (from
, from_byte
);
2426 /* We have skipped one comment. */
2427 from
= out_charpos
, from_byte
= out_bytepos
;
2431 else if (code
!= Swhitespace
|| quoted
)
2435 INC_BOTH (from
, from_byte
);
2436 SET_PT_BOTH (from
, from_byte
);
2444 SET_PT_BOTH (from
, from_byte
);
2449 /* Return syntax code of character C if C is an ASCII character
2450 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2452 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2453 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2454 ? SYNTAX (c) : Ssymbol)
2457 scan_lists (register EMACS_INT from
, EMACS_INT count
, EMACS_INT depth
, int sexpflag
)
2460 register ptrdiff_t stop
= count
> 0 ? ZV
: BEGV
;
2465 register enum syntaxcode code
, temp_code
;
2466 EMACS_INT min_depth
= depth
; /* Err out if depth gets less than this. */
2467 int comstyle
= 0; /* style of comment encountered */
2468 int comnested
= 0; /* whether the comment is nestable or not */
2470 EMACS_INT last_good
= from
;
2472 ptrdiff_t from_byte
;
2473 ptrdiff_t out_bytepos
, out_charpos
;
2476 int multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2478 if (depth
> 0) min_depth
= 0;
2480 if (from
> ZV
) from
= ZV
;
2481 if (from
< BEGV
) from
= BEGV
;
2483 from_byte
= CHAR_TO_BYTE (from
);
2488 SETUP_SYNTAX_TABLE (from
, count
);
2493 int comstart_first
, prefix
, syntax
, other_syntax
;
2494 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2495 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2496 syntax
= SYNTAX_WITH_FLAGS (c
);
2497 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2498 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2499 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2500 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2501 prefix
= SYNTAX_FLAGS_PREFIX (syntax
);
2502 if (depth
== min_depth
)
2504 INC_BOTH (from
, from_byte
);
2505 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2506 if (from
< stop
&& comstart_first
2507 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2508 other_syntax
= SYNTAX_WITH_FLAGS (c
),
2509 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2510 && parse_sexp_ignore_comments
)
2512 /* we have encountered a comment start sequence and we
2513 are ignoring all text inside comments. We must record
2514 the comment style this sequence begins so that later,
2515 only a comment end of the same style actually ends
2516 the comment section */
2518 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2520 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2521 INC_BOTH (from
, from_byte
);
2522 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2528 switch (SWITCH_ENUM_CAST (code
))
2534 INC_BOTH (from
, from_byte
);
2535 /* treat following character as a word constituent */
2538 if (depth
|| !sexpflag
) break;
2539 /* This word counts as a sexp; return at end of it. */
2542 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2544 /* Some compilers can't handle this inside the switch. */
2545 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2546 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2551 INC_BOTH (from
, from_byte
);
2562 INC_BOTH (from
, from_byte
);
2566 case Scomment_fence
:
2567 comstyle
= ST_COMMENT_STYLE
;
2570 if (!parse_sexp_ignore_comments
) break;
2571 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2572 found
= forw_comment (from
, from_byte
, stop
,
2573 comnested
, comstyle
, 0,
2574 &out_charpos
, &out_bytepos
, &dummy
);
2575 from
= out_charpos
, from_byte
= out_bytepos
;
2582 INC_BOTH (from
, from_byte
);
2583 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2589 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (from_byte
))
2591 INC_BOTH (from
, from_byte
);
2601 if (!++depth
) goto done
;
2606 if (!--depth
) goto done
;
2607 if (depth
< min_depth
)
2608 xsignal3 (Qscan_error
,
2609 build_string ("Containing expression ends prematurely"),
2610 make_number (last_good
), make_number (from
));
2615 temp_pos
= dec_bytepos (from_byte
);
2616 stringterm
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2621 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2622 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2625 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2626 : SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring_fence
)
2629 /* Some compilers can't handle this inside the switch. */
2630 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2635 INC_BOTH (from
, from_byte
);
2637 INC_BOTH (from
, from_byte
);
2639 INC_BOTH (from
, from_byte
);
2640 if (!depth
&& sexpflag
) goto done
;
2643 /* Ignore whitespace, punctuation, quote, endcomment. */
2648 /* Reached end of buffer. Error if within object, return nil if between */
2655 /* End of object reached */
2665 DEC_BOTH (from
, from_byte
);
2666 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2667 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2668 syntax
= SYNTAX_WITH_FLAGS (c
);
2669 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2670 if (depth
== min_depth
)
2673 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2674 if (code
== Sendcomment
)
2675 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2676 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2677 && prev_char_comend_first (from
, from_byte
)
2678 && parse_sexp_ignore_comments
)
2680 /* We must record the comment style encountered so that
2681 later, we can match only the proper comment begin
2682 sequence of the same style. */
2683 int c2
, other_syntax
;
2684 DEC_BOTH (from
, from_byte
);
2685 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2687 c2
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2688 other_syntax
= SYNTAX_WITH_FLAGS (c2
);
2689 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2691 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2694 /* Quoting turns anything except a comment-ender
2695 into a word character. Note that this cannot be true
2696 if we decremented FROM in the if-statement above. */
2697 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2699 DEC_BOTH (from
, from_byte
);
2702 else if (SYNTAX_FLAGS_PREFIX (syntax
))
2705 switch (SWITCH_ENUM_CAST (code
))
2711 if (depth
|| !sexpflag
) break;
2712 /* This word counts as a sexp; count object finished
2713 after passing it. */
2716 temp_pos
= from_byte
;
2717 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
2721 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2722 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2723 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2724 /* Don't allow comment-end to be quoted. */
2725 if (temp_code
== Sendcomment
)
2727 quoted
= char_quoted (from
- 1, temp_pos
);
2730 DEC_BOTH (from
, from_byte
);
2731 temp_pos
= dec_bytepos (temp_pos
);
2732 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2734 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2735 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2736 if (! (quoted
|| temp_code
== Sword
2737 || temp_code
== Ssymbol
2738 || temp_code
== Squote
))
2740 DEC_BOTH (from
, from_byte
);
2747 temp_pos
= dec_bytepos (from_byte
);
2748 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2749 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (temp_pos
))
2750 DEC_BOTH (from
, from_byte
);
2759 if (!++depth
) goto done2
;
2764 if (!--depth
) goto done2
;
2765 if (depth
< min_depth
)
2766 xsignal3 (Qscan_error
,
2767 build_string ("Containing expression ends prematurely"),
2768 make_number (last_good
), make_number (from
));
2772 if (!parse_sexp_ignore_comments
)
2774 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2775 &out_charpos
, &out_bytepos
);
2776 /* FIXME: if found == -1, then it really wasn't a comment-end.
2777 For single-char Sendcomment, we can't do much about it apart
2778 from skipping the char.
2779 For 2-char endcomments, we could try again, taking both
2780 chars as separate entities, but it's a lot of trouble
2781 for very little gain, so we don't bother either. -sm */
2783 from
= out_charpos
, from_byte
= out_bytepos
;
2786 case Scomment_fence
:
2792 DEC_BOTH (from
, from_byte
);
2793 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2794 if (!char_quoted (from
, from_byte
)
2795 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2796 SYNTAX_WITH_MULTIBYTE_CHECK (c
) == code
))
2799 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2803 stringterm
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2808 DEC_BOTH (from
, from_byte
);
2809 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2810 if (!char_quoted (from
, from_byte
)
2812 == (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
)))
2813 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2816 if (!depth
&& sexpflag
) goto done2
;
2819 /* Ignore whitespace, punctuation, quote, endcomment. */
2824 /* Reached start of buffer. Error if within object, return nil if between */
2837 XSETFASTINT (val
, from
);
2841 xsignal3 (Qscan_error
,
2842 build_string ("Unbalanced parentheses"),
2843 make_number (last_good
), make_number (from
));
2846 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
2847 doc
: /* Scan from character number FROM by COUNT lists.
2848 Scan forward if COUNT is positive, backward if COUNT is negative.
2849 Return the character number of the position thus found.
2851 A \"list", in this context, refers to a balanced parenthetical
2852 grouping, as determined by the syntax table.
2854 If DEPTH is nonzero, treat that as the nesting depth of the starting
2855 point (i.e. the starting point is DEPTH parentheses deep). This
2856 function scans over parentheses until the depth goes to zero COUNT
2857 times. Hence, positive DEPTH moves out that number of levels of
2858 parentheses, while negative DEPTH moves to a deeper level.
2860 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2862 If we reach the beginning or end of the accessible part of the buffer
2863 before we have scanned over COUNT lists, return nil if the depth at
2864 that point is zero, and signal a error if the depth is nonzero. */)
2865 (Lisp_Object from
, Lisp_Object count
, Lisp_Object depth
)
2867 CHECK_NUMBER (from
);
2868 CHECK_NUMBER (count
);
2869 CHECK_NUMBER (depth
);
2871 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
2874 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
2875 doc
: /* Scan from character number FROM by COUNT balanced expressions.
2876 If COUNT is negative, scan backwards.
2877 Returns the character number of the position thus found.
2879 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2881 If the beginning or end of (the accessible part of) the buffer is reached
2882 in the middle of a parenthetical grouping, an error is signaled.
2883 If the beginning or end is reached between groupings
2884 but before count is used up, nil is returned. */)
2885 (Lisp_Object from
, Lisp_Object count
)
2887 CHECK_NUMBER (from
);
2888 CHECK_NUMBER (count
);
2890 return scan_lists (XINT (from
), XINT (count
), 0, 1);
2893 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
2895 doc
: /* Move point backward over any number of chars with prefix syntax.
2896 This includes chars with "quote" or "prefix" syntax (' or p). */)
2899 ptrdiff_t beg
= BEGV
;
2900 ptrdiff_t opoint
= PT
;
2901 ptrdiff_t opoint_byte
= PT_BYTE
;
2903 ptrdiff_t pos_byte
= PT_BYTE
;
2908 SET_PT_BOTH (opoint
, opoint_byte
);
2913 SETUP_SYNTAX_TABLE (pos
, -1);
2915 DEC_BOTH (pos
, pos_byte
);
2917 while (!char_quoted (pos
, pos_byte
)
2918 /* Previous statement updates syntax table. */
2919 && ((c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
), SYNTAX (c
) == Squote
)
2920 || SYNTAX_PREFIX (c
)))
2923 opoint_byte
= pos_byte
;
2926 DEC_BOTH (pos
, pos_byte
);
2929 SET_PT_BOTH (opoint
, opoint_byte
);
2934 /* Parse forward from FROM / FROM_BYTE to END,
2935 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2936 and return a description of the state of the parse at END.
2937 If STOPBEFORE is nonzero, stop at the start of an atom.
2938 If COMMENTSTOP is 1, stop at the start of a comment.
2939 If COMMENTSTOP is -1, stop at the start or end of a comment,
2940 after the beginning of a string, or after the end of a string. */
2943 scan_sexps_forward (struct lisp_parse_state
*stateptr
,
2944 ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t end
,
2945 EMACS_INT targetdepth
, int stopbefore
,
2946 Lisp_Object oldstate
, int commentstop
)
2948 struct lisp_parse_state state
;
2950 register enum syntaxcode code
;
2953 struct level
{ ptrdiff_t last
, prev
; };
2954 struct level levelstart
[100];
2955 register struct level
*curlevel
= levelstart
;
2956 struct level
*endlevel
= levelstart
+ 100;
2957 register EMACS_INT depth
; /* Paren depth of current scanning location.
2958 level - levelstart equals this except
2959 when the depth becomes negative. */
2960 EMACS_INT mindepth
; /* Lowest DEPTH value seen. */
2961 int start_quoted
= 0; /* Nonzero means starting after a char quote */
2963 ptrdiff_t prev_from
; /* Keep one character before FROM. */
2964 ptrdiff_t prev_from_byte
;
2965 int prev_from_syntax
;
2966 int boundary_stop
= commentstop
== -1;
2969 ptrdiff_t out_bytepos
, out_charpos
;
2973 prev_from_byte
= from_byte
;
2975 DEC_BOTH (prev_from
, prev_from_byte
);
2977 /* Use this macro instead of `from++'. */
2979 do { prev_from = from; \
2980 prev_from_byte = from_byte; \
2981 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2982 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2983 INC_BOTH (from, from_byte); \
2985 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2991 if (NILP (oldstate
))
2994 state
.instring
= -1;
2995 state
.incomment
= 0;
2996 state
.comstyle
= 0; /* comment style a by default. */
2997 state
.comstr_start
= -1; /* no comment/string seen. */
3001 tem
= Fcar (oldstate
);
3007 oldstate
= Fcdr (oldstate
);
3008 oldstate
= Fcdr (oldstate
);
3009 oldstate
= Fcdr (oldstate
);
3010 tem
= Fcar (oldstate
);
3011 /* Check whether we are inside string_fence-style string: */
3012 state
.instring
= (!NILP (tem
)
3013 ? (CHARACTERP (tem
) ? XFASTINT (tem
) : ST_STRING_STYLE
)
3016 oldstate
= Fcdr (oldstate
);
3017 tem
= Fcar (oldstate
);
3018 state
.incomment
= (!NILP (tem
)
3019 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
3022 oldstate
= Fcdr (oldstate
);
3023 tem
= Fcar (oldstate
);
3024 start_quoted
= !NILP (tem
);
3026 /* if the eighth element of the list is nil, we are in comment
3027 style a. If it is non-nil, we are in comment style b */
3028 oldstate
= Fcdr (oldstate
);
3029 oldstate
= Fcdr (oldstate
);
3030 tem
= Fcar (oldstate
);
3031 state
.comstyle
= (NILP (tem
)
3033 : (RANGED_INTEGERP (0, tem
, ST_COMMENT_STYLE
)
3035 : ST_COMMENT_STYLE
));
3037 oldstate
= Fcdr (oldstate
);
3038 tem
= Fcar (oldstate
);
3039 state
.comstr_start
=
3040 RANGED_INTEGERP (PTRDIFF_MIN
, tem
, PTRDIFF_MAX
) ? XINT (tem
) : -1;
3041 oldstate
= Fcdr (oldstate
);
3042 tem
= Fcar (oldstate
);
3043 while (!NILP (tem
)) /* >= second enclosing sexps. */
3045 Lisp_Object temhd
= Fcar (tem
);
3046 if (RANGED_INTEGERP (PTRDIFF_MIN
, temhd
, PTRDIFF_MAX
))
3047 curlevel
->last
= XINT (temhd
);
3048 if (++curlevel
== endlevel
)
3049 curlevel
--; /* error ("Nesting too deep for parser"); */
3050 curlevel
->prev
= -1;
3051 curlevel
->last
= -1;
3058 curlevel
->prev
= -1;
3059 curlevel
->last
= -1;
3061 SETUP_SYNTAX_TABLE (prev_from
, 1);
3062 temp
= FETCH_CHAR (prev_from_byte
);
3063 prev_from_syntax
= SYNTAX_WITH_FLAGS (temp
);
3064 UPDATE_SYNTAX_TABLE_FORWARD (from
);
3066 /* Enter the loop at a place appropriate for initial state. */
3068 if (state
.incomment
)
3069 goto startincomment
;
3070 if (state
.instring
>= 0)
3072 nofence
= state
.instring
!= ST_STRING_STYLE
;
3074 goto startquotedinstring
;
3077 else if (start_quoted
)
3084 code
= prev_from_syntax
& 0xff;
3087 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
)
3088 && (c1
= FETCH_CHAR (from_byte
),
3089 syntax
= SYNTAX_WITH_FLAGS (c1
),
3090 SYNTAX_FLAGS_COMSTART_SECOND (syntax
)))
3091 /* Duplicate code to avoid a complex if-expression
3092 which causes trouble for the SGI compiler. */
3094 /* Record the comment style we have entered so that only
3095 the comment-end sequence of the same style actually
3096 terminates the comment section. */
3098 = SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_from_syntax
);
3099 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
);
3100 comnested
= comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
3101 state
.incomment
= comnested
? 1 : -1;
3102 state
.comstr_start
= prev_from
;
3106 else if (code
== Scomment_fence
)
3108 /* Record the comment style we have entered so that only
3109 the comment-end sequence of the same style actually
3110 terminates the comment section. */
3111 state
.comstyle
= ST_COMMENT_STYLE
;
3112 state
.incomment
= -1;
3113 state
.comstr_start
= prev_from
;
3116 else if (code
== Scomment
)
3118 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
, 0);
3119 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
3121 state
.comstr_start
= prev_from
;
3124 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
3126 switch (SWITCH_ENUM_CAST (code
))
3130 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3131 curlevel
->last
= prev_from
;
3133 if (from
== end
) goto endquoted
;
3136 /* treat following character as a word constituent */
3139 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3140 curlevel
->last
= prev_from
;
3144 /* Some compilers can't handle this inside the switch. */
3145 temp
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3146 temp
= SYNTAX (temp
);
3152 if (from
== end
) goto endquoted
;
3164 curlevel
->prev
= curlevel
->last
;
3167 case Scomment_fence
: /* Can't happen because it's handled above. */
3169 if (commentstop
|| boundary_stop
) goto done
;
3171 /* The (from == BEGV) test was to enter the loop in the middle so
3172 that we find a 2-char comment ender even if we start in the
3173 middle of it. We don't want to do that if we're just at the
3174 beginning of the comment (think of (*) ... (*)). */
3175 found
= forw_comment (from
, from_byte
, end
,
3176 state
.incomment
, state
.comstyle
,
3177 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
3178 ? 0 : prev_from_syntax
,
3179 &out_charpos
, &out_bytepos
, &state
.incomment
);
3180 from
= out_charpos
; from_byte
= out_bytepos
;
3181 /* Beware! prev_from and friends are invalid now.
3182 Luckily, the `done' doesn't use them and the INC_FROM
3183 sets them to a sane value without looking at them. */
3184 if (!found
) goto done
;
3186 state
.incomment
= 0;
3187 state
.comstyle
= 0; /* reset the comment style */
3188 if (boundary_stop
) goto done
;
3192 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3194 /* curlevel++->last ran into compiler bug on Apollo */
3195 curlevel
->last
= prev_from
;
3196 if (++curlevel
== endlevel
)
3197 curlevel
--; /* error ("Nesting too deep for parser"); */
3198 curlevel
->prev
= -1;
3199 curlevel
->last
= -1;
3200 if (targetdepth
== depth
) goto done
;
3205 if (depth
< mindepth
)
3207 if (curlevel
!= levelstart
)
3209 curlevel
->prev
= curlevel
->last
;
3210 if (targetdepth
== depth
) goto done
;
3215 state
.comstr_start
= from
- 1;
3216 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3217 curlevel
->last
= prev_from
;
3218 state
.instring
= (code
== Sstring
3219 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte
))
3221 if (boundary_stop
) goto done
;
3224 nofence
= state
.instring
!= ST_STRING_STYLE
;
3230 if (from
>= end
) goto done
;
3231 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3232 /* Some compilers can't handle this inside the switch. */
3235 /* Check TEMP here so that if the char has
3236 a syntax-table property which says it is NOT
3237 a string character, it does not end the string. */
3238 if (nofence
&& c
== state
.instring
&& temp
== Sstring
)
3244 if (!nofence
) goto string_end
;
3249 startquotedinstring
:
3250 if (from
>= end
) goto endquoted
;
3256 state
.instring
= -1;
3257 curlevel
->prev
= curlevel
->last
;
3259 if (boundary_stop
) goto done
;
3263 /* FIXME: We should do something with it. */
3266 /* Ignore whitespace, punctuation, quote, endcomment. */
3272 stop
: /* Here if stopping before start of sexp. */
3273 from
= prev_from
; /* We have just fetched the char that starts it; */
3274 goto done
; /* but return the position before it. */
3279 state
.depth
= depth
;
3280 state
.mindepth
= mindepth
;
3281 state
.thislevelstart
= curlevel
->prev
;
3282 state
.prevlevelstart
3283 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
3284 state
.location
= from
;
3285 state
.levelstarts
= Qnil
;
3286 while (curlevel
> levelstart
)
3287 state
.levelstarts
= Fcons (make_number ((--curlevel
)->last
),
3294 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
3295 doc
: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3296 Parsing stops at TO or when certain criteria are met;
3297 point is set to where parsing stops.
3298 If fifth arg OLDSTATE is omitted or nil,
3299 parsing assumes that FROM is the beginning of a function.
3300 Value is a list of elements describing final state of parsing:
3302 1. character address of start of innermost containing list; nil if none.
3303 2. character address of start of last complete sexp terminated.
3304 3. non-nil if inside a string.
3305 (it is the character that will terminate the string,
3306 or t if the string should be terminated by a generic string delimiter.)
3307 4. nil if outside a comment, t if inside a non-nestable comment,
3308 else an integer (the current comment nesting).
3309 5. t if following a quote character.
3310 6. the minimum paren-depth encountered during this scan.
3311 7. style of comment, if any.
3312 8. character address of start of comment or string; nil if not in one.
3313 9. Intermediate data for continuation of parsing (subject to change).
3314 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3315 in parentheses becomes equal to TARGETDEPTH.
3316 Fourth arg STOPBEFORE non-nil means stop when come to
3317 any character that starts a sexp.
3318 Fifth arg OLDSTATE is a list like what this function returns.
3319 It is used to initialize the state of the parse. Elements number 1, 2, 6
3321 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3322 If it is symbol `syntax-table', stop after the start of a comment or a
3323 string, or after end of a comment or a string. */)
3324 (Lisp_Object from
, Lisp_Object to
, Lisp_Object targetdepth
, Lisp_Object stopbefore
, Lisp_Object oldstate
, Lisp_Object commentstop
)
3326 struct lisp_parse_state state
;
3329 if (!NILP (targetdepth
))
3331 CHECK_NUMBER (targetdepth
);
3332 target
= XINT (targetdepth
);
3335 target
= TYPE_MINIMUM (EMACS_INT
); /* We won't reach this depth */
3337 validate_region (&from
, &to
);
3338 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
3340 target
, !NILP (stopbefore
), oldstate
,
3342 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
3344 SET_PT (state
.location
);
3346 return Fcons (make_number (state
.depth
),
3347 Fcons (state
.prevlevelstart
< 0
3348 ? Qnil
: make_number (state
.prevlevelstart
),
3349 Fcons (state
.thislevelstart
< 0
3350 ? Qnil
: make_number (state
.thislevelstart
),
3351 Fcons (state
.instring
>= 0
3352 ? (state
.instring
== ST_STRING_STYLE
3353 ? Qt
: make_number (state
.instring
)) : Qnil
,
3354 Fcons (state
.incomment
< 0 ? Qt
:
3355 (state
.incomment
== 0 ? Qnil
:
3356 make_number (state
.incomment
)),
3357 Fcons (state
.quoted
? Qt
: Qnil
,
3358 Fcons (make_number (state
.mindepth
),
3359 Fcons ((state
.comstyle
3360 ? (state
.comstyle
== ST_COMMENT_STYLE
3362 : make_number (state
.comstyle
))
3364 Fcons (((state
.incomment
3365 || (state
.instring
>= 0))
3366 ? make_number (state
.comstr_start
)
3368 Fcons (state
.levelstarts
, Qnil
))))))))));
3372 init_syntax_once (void)
3377 /* This has to be done here, before we call Fmake_char_table. */
3378 DEFSYM (Qsyntax_table
, "syntax-table");
3380 /* Intern_C_String this now in case it isn't already done.
3381 Setting this variable twice is harmless.
3382 But don't staticpro it here--that is done in alloc.c. */
3383 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
3385 /* Create objects which can be shared among syntax tables. */
3386 Vsyntax_code_object
= Fmake_vector (make_number (Smax
), Qnil
);
3387 for (i
= 0; i
< ASIZE (Vsyntax_code_object
); i
++)
3388 ASET (Vsyntax_code_object
, i
, Fcons (make_number (i
), Qnil
));
3390 /* Now we are ready to set up this property, so we can
3391 create syntax tables. */
3392 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
3394 temp
= AREF (Vsyntax_code_object
, (int) Swhitespace
);
3396 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
3398 /* Control characters should not be whitespace. */
3399 temp
= AREF (Vsyntax_code_object
, (int) Spunct
);
3400 for (i
= 0; i
<= ' ' - 1; i
++)
3401 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3402 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 0177, temp
);
3404 /* Except that a few really are whitespace. */
3405 temp
= AREF (Vsyntax_code_object
, (int) Swhitespace
);
3406 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ' ', temp
);
3407 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\t', temp
);
3408 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\n', temp
);
3409 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 015, temp
);
3410 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 014, temp
);
3412 temp
= AREF (Vsyntax_code_object
, (int) Sword
);
3413 for (i
= 'a'; i
<= 'z'; i
++)
3414 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3415 for (i
= 'A'; i
<= 'Z'; i
++)
3416 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3417 for (i
= '0'; i
<= '9'; i
++)
3418 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3420 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
3421 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
3423 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
3424 Fcons (make_number (Sopen
), make_number (')')));
3425 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
3426 Fcons (make_number (Sclose
), make_number ('(')));
3427 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
3428 Fcons (make_number (Sopen
), make_number (']')));
3429 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
3430 Fcons (make_number (Sclose
), make_number ('[')));
3431 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
3432 Fcons (make_number (Sopen
), make_number ('}')));
3433 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
3434 Fcons (make_number (Sclose
), make_number ('{')));
3435 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
3436 Fcons (make_number ((int) Sstring
), Qnil
));
3437 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
3438 Fcons (make_number ((int) Sescape
), Qnil
));
3440 temp
= AREF (Vsyntax_code_object
, (int) Ssymbol
);
3441 for (i
= 0; i
< 10; i
++)
3443 c
= "_-+*/&|<>="[i
];
3444 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3447 temp
= AREF (Vsyntax_code_object
, (int) Spunct
);
3448 for (i
= 0; i
< 12; i
++)
3450 c
= ".,;:?!#@~^'`"[i
];
3451 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3454 /* All multibyte characters have syntax `word' by default. */
3455 temp
= AREF (Vsyntax_code_object
, (int) Sword
);
3456 char_table_set_range (Vstandard_syntax_table
, 0x80, MAX_CHAR
, temp
);
3460 syms_of_syntax (void)
3462 DEFSYM (Qsyntax_table_p
, "syntax-table-p");
3464 staticpro (&Vsyntax_code_object
);
3466 staticpro (&gl_state
.object
);
3467 staticpro (&gl_state
.global_code
);
3468 staticpro (&gl_state
.current_syntax_table
);
3469 staticpro (&gl_state
.old_prop
);
3471 /* Defined in regex.c */
3472 staticpro (&re_match_object
);
3474 DEFSYM (Qscan_error
, "scan-error");
3475 Fput (Qscan_error
, Qerror_conditions
,
3476 pure_cons (Qscan_error
, pure_cons (Qerror
, Qnil
)));
3477 Fput (Qscan_error
, Qerror_message
,
3478 build_pure_c_string ("Scan error"));
3480 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments
,
3481 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3483 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties
,
3484 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3485 Otherwise, that text property is simply ignored.
3486 See the info node `(elisp)Syntax Properties' for a description of the
3487 `syntax-table' property. */);
3489 words_include_escapes
= 0;
3490 DEFVAR_BOOL ("words-include-escapes", words_include_escapes
,
3491 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3493 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol
,
3494 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3495 multibyte_syntax_as_symbol
= 0;
3497 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3498 open_paren_in_column_0_is_defun_start
,
3499 doc
: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3500 open_paren_in_column_0_is_defun_start
= 1;
3503 DEFVAR_LISP ("find-word-boundary-function-table",
3504 Vfind_word_boundary_function_table
,
3506 Char table of functions to search for the word boundary.
3507 Each function is called with two arguments; POS and LIMIT.
3508 POS and LIMIT are character positions in the current buffer.
3510 If POS is less than LIMIT, POS is at the first character of a word,
3511 and the return value of a function is a position after the last
3512 character of that word.
3514 If POS is not less than LIMIT, POS is at the last character of a word,
3515 and the return value of a function is a position at the first
3516 character of that word.
3518 In both cases, LIMIT bounds the search. */);
3519 Vfind_word_boundary_function_table
= Fmake_char_table (Qnil
, Qnil
);
3521 defsubr (&Ssyntax_table_p
);
3522 defsubr (&Ssyntax_table
);
3523 defsubr (&Sstandard_syntax_table
);
3524 defsubr (&Scopy_syntax_table
);
3525 defsubr (&Sset_syntax_table
);
3526 defsubr (&Schar_syntax
);
3527 defsubr (&Smatching_paren
);
3528 defsubr (&Sstring_to_syntax
);
3529 defsubr (&Smodify_syntax_entry
);
3530 defsubr (&Sinternal_describe_syntax_value
);
3532 defsubr (&Sforward_word
);
3534 defsubr (&Sskip_chars_forward
);
3535 defsubr (&Sskip_chars_backward
);
3536 defsubr (&Sskip_syntax_forward
);
3537 defsubr (&Sskip_syntax_backward
);
3539 defsubr (&Sforward_comment
);
3540 defsubr (&Sscan_lists
);
3541 defsubr (&Sscan_sexps
);
3542 defsubr (&Sbackward_prefix_chars
);
3543 defsubr (&Sparse_partial_sexp
);