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/>. */
23 #include <sys/types.h>
27 #include "character.h"
32 /* Make syntax table lookup grant data in gl_state. */
33 #define SYNTAX_ENTRY_VIA_PROPERTY
36 #include "intervals.h"
39 /* Then there are seven single-bit flags that have the following meanings:
40 1. This character is the first of a two-character comment-start sequence.
41 2. This character is the second of a two-character comment-start sequence.
42 3. This character is the first of a two-character comment-end sequence.
43 4. This character is the second of a two-character comment-end sequence.
44 5. This character is a prefix, for backward-prefix-chars.
45 6. The char is part of a delimiter for comments of style "b".
46 7. This character is part of a nestable comment sequence.
47 8. The char is part of a delimiter for comments of style "c".
48 Note that any two-character sequence whose first character has flag 1
49 and whose second character has flag 2 will be interpreted as a comment start.
51 bit 6 and 8 are used to discriminate between different comment styles.
52 Languages such as C++ allow two orthogonal syntax start/end pairs
53 and bit 6 is used to determine whether a comment-end or Scommentend
54 ends style a or b. Comment markers can start style a, b, c, or bc.
55 Style a is always the default.
56 For 2-char comment markers, the style b flag is only looked up on the second
57 char of the comment marker and on the first char of the comment ender.
58 For style c (like to for the nested flag), the flag can be placed on any
62 /* These macros extract specific flags from an integer
63 that holds the syntax code and the flags. */
65 #define SYNTAX_FLAGS_COMSTART_FIRST(flags) (((flags) >> 16) & 1)
67 #define SYNTAX_FLAGS_COMSTART_SECOND(flags) (((flags) >> 17) & 1)
69 #define SYNTAX_FLAGS_COMEND_FIRST(flags) (((flags) >> 18) & 1)
71 #define SYNTAX_FLAGS_COMEND_SECOND(flags) (((flags) >> 19) & 1)
73 #define SYNTAX_FLAGS_PREFIX(flags) (((flags) >> 20) & 1)
75 #define SYNTAX_FLAGS_COMMENT_STYLEB(flags) (((flags) >> 21) & 1)
76 #define SYNTAX_FLAGS_COMMENT_STYLEC(flags) (((flags) >> 22) & 2)
77 /* FLAGS should be the flags of the main char of the comment marker, e.g.
78 the second for comstart and the first for comend. */
79 #define SYNTAX_FLAGS_COMMENT_STYLE(flags, other_flags) \
80 (SYNTAX_FLAGS_COMMENT_STYLEB (flags) \
81 | SYNTAX_FLAGS_COMMENT_STYLEC (flags) \
82 | SYNTAX_FLAGS_COMMENT_STYLEC (other_flags))
84 #define SYNTAX_FLAGS_COMMENT_NESTED(flags) (((flags) >> 22) & 1)
86 /* These macros extract a particular flag for a given character. */
88 #define SYNTAX_COMEND_FIRST(c) \
89 (SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c)))
90 #define SYNTAX_PREFIX(c) (SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c)))
92 /* We use these constants in place for comment-style and
93 string-ender-char to distinguish comments/strings started by
94 comment_fence and string_fence codes. */
96 #define ST_COMMENT_STYLE (256 + 1)
97 #define ST_STRING_STYLE (256 + 2)
99 static Lisp_Object Qsyntax_table_p
;
100 static Lisp_Object Qsyntax_table
, Qscan_error
;
103 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
104 if not compiled with GCC. No need to mark it, since it is used
105 only very temporarily. */
106 Lisp_Object syntax_temp
;
109 /* This is the internal form of the parse state used in parse-partial-sexp. */
111 struct lisp_parse_state
113 EMACS_INT depth
; /* Depth at end of parsing. */
114 int instring
; /* -1 if not within string, else desired terminator. */
115 EMACS_INT incomment
; /* -1 if in unnestable comment else comment nesting */
116 int comstyle
; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
117 int quoted
; /* Nonzero if just after an escape char at end of parsing */
118 EMACS_INT mindepth
; /* Minimum depth seen while scanning. */
119 /* Char number of most recent start-of-expression at current level */
120 ptrdiff_t thislevelstart
;
121 /* Char number of start of containing expression */
122 ptrdiff_t prevlevelstart
;
123 ptrdiff_t location
; /* Char number at which parsing stopped. */
124 ptrdiff_t comstr_start
; /* Position of last comment/string starter. */
125 Lisp_Object levelstarts
; /* Char numbers of starts-of-expression
126 of levels (starting from outermost). */
129 /* These variables are a cache for finding the start of a defun.
130 find_start_pos is the place for which the defun start was found.
131 find_start_value is the defun start position found for it.
132 find_start_value_byte is the corresponding byte position.
133 find_start_buffer is the buffer it was found in.
134 find_start_begv is the BEGV value when it was found.
135 find_start_modiff is the value of MODIFF when it was found. */
137 static ptrdiff_t find_start_pos
;
138 static ptrdiff_t find_start_value
;
139 static ptrdiff_t find_start_value_byte
;
140 static struct buffer
*find_start_buffer
;
141 static ptrdiff_t find_start_begv
;
142 static EMACS_INT find_start_modiff
;
145 static Lisp_Object
skip_chars (int, Lisp_Object
, Lisp_Object
, int);
146 static Lisp_Object
skip_syntaxes (int, Lisp_Object
, Lisp_Object
);
147 static Lisp_Object
scan_lists (EMACS_INT
, EMACS_INT
, EMACS_INT
, int);
148 static void scan_sexps_forward (struct lisp_parse_state
*,
149 ptrdiff_t, ptrdiff_t, ptrdiff_t, EMACS_INT
,
150 int, Lisp_Object
, int);
151 static int in_classes (int, Lisp_Object
);
153 /* This setter is used only in this file, so it can be private. */
155 bset_syntax_table (struct buffer
*b
, Lisp_Object val
)
157 b
->INTERNAL_FIELD (syntax_table
) = val
;
160 /* Whether the syntax of the character C has the prefix flag set. */
161 int syntax_prefix_flag_p (int c
)
163 return SYNTAX_PREFIX (c
);
166 struct gl_state_s gl_state
; /* Global state of syntax parser. */
168 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
169 to scan to property-change. */
171 /* Update gl_state to an appropriate interval which contains CHARPOS. The
172 sign of COUNT give the relative position of CHARPOS wrt the previously
173 valid interval. If INIT, only [be]_property fields of gl_state are
174 valid at start, the rest is filled basing on OBJECT.
176 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
177 direction than the intervals - or in an interval. We update the
178 current syntax-table basing on the property of this interval, and
179 update the interval to start further than CHARPOS - or be
180 NULL. We also update lim_property to be the next value of
181 charpos to call this subroutine again - or be before/after the
182 start/end of OBJECT. */
185 update_syntax_table (ptrdiff_t charpos
, EMACS_INT count
, int init
,
188 Lisp_Object tmp_table
;
195 gl_state
.old_prop
= Qnil
;
196 gl_state
.start
= gl_state
.b_property
;
197 gl_state
.stop
= gl_state
.e_property
;
198 i
= interval_of (charpos
, object
);
199 gl_state
.backward_i
= gl_state
.forward_i
= i
;
203 /* interval_of updates only ->position of the return value, so
204 update the parents manually to speed up update_interval. */
205 while (!NULL_PARENT (i
))
207 if (AM_RIGHT_CHILD (i
))
208 INTERVAL_PARENT (i
)->position
= i
->position
209 - LEFT_TOTAL_LENGTH (i
) + TOTAL_LENGTH (i
) /* right end */
210 - TOTAL_LENGTH (INTERVAL_PARENT (i
))
211 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i
));
213 INTERVAL_PARENT (i
)->position
= i
->position
- LEFT_TOTAL_LENGTH (i
)
215 i
= INTERVAL_PARENT (i
);
217 i
= gl_state
.forward_i
;
218 gl_state
.b_property
= i
->position
- gl_state
.offset
;
219 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
222 i
= count
> 0 ? gl_state
.forward_i
: gl_state
.backward_i
;
224 /* We are guaranteed to be called with CHARPOS either in i,
227 error ("Error in syntax_table logic for to-the-end intervals");
228 else if (charpos
< i
->position
) /* Move left. */
231 error ("Error in syntax_table logic for intervals <-");
232 /* Update the interval. */
233 i
= update_interval (i
, charpos
);
234 if (INTERVAL_LAST_POS (i
) != gl_state
.b_property
)
237 gl_state
.forward_i
= i
;
238 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
241 else if (charpos
>= INTERVAL_LAST_POS (i
)) /* Move right. */
244 error ("Error in syntax_table logic for intervals ->");
245 /* Update the interval. */
246 i
= update_interval (i
, charpos
);
247 if (i
->position
!= gl_state
.e_property
)
250 gl_state
.backward_i
= i
;
251 gl_state
.b_property
= i
->position
- gl_state
.offset
;
256 tmp_table
= textget (i
->plist
, Qsyntax_table
);
259 invalidate
= !EQ (tmp_table
, gl_state
.old_prop
); /* Need to invalidate? */
261 if (invalidate
) /* Did not get to adjacent interval. */
262 { /* with the same table => */
263 /* invalidate the old range. */
266 gl_state
.backward_i
= i
;
267 gl_state
.b_property
= i
->position
- gl_state
.offset
;
271 gl_state
.forward_i
= i
;
272 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
276 if (!EQ (tmp_table
, gl_state
.old_prop
))
278 gl_state
.current_syntax_table
= tmp_table
;
279 gl_state
.old_prop
= tmp_table
;
280 if (EQ (Fsyntax_table_p (tmp_table
), Qt
))
282 gl_state
.use_global
= 0;
284 else if (CONSP (tmp_table
))
286 gl_state
.use_global
= 1;
287 gl_state
.global_code
= tmp_table
;
291 gl_state
.use_global
= 0;
292 gl_state
.current_syntax_table
= BVAR (current_buffer
, syntax_table
);
298 if (cnt
&& !EQ (tmp_table
, textget (i
->plist
, Qsyntax_table
)))
302 gl_state
.e_property
= i
->position
- gl_state
.offset
;
303 gl_state
.forward_i
= i
;
308 = i
->position
+ LENGTH (i
) - gl_state
.offset
;
309 gl_state
.backward_i
= i
;
313 else if (cnt
== INTERVALS_AT_ONCE
)
318 = i
->position
+ LENGTH (i
) - gl_state
.offset
319 /* e_property at EOB is not set to ZV but to ZV+1, so that
320 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
321 having to check eob between the two. */
322 + (next_interval (i
) ? 0 : 1);
323 gl_state
.forward_i
= i
;
327 gl_state
.b_property
= i
->position
- gl_state
.offset
;
328 gl_state
.backward_i
= i
;
333 i
= count
> 0 ? next_interval (i
) : previous_interval (i
);
335 eassert (i
== NULL
); /* This property goes to the end. */
337 gl_state
.e_property
= gl_state
.stop
;
339 gl_state
.b_property
= gl_state
.start
;
342 /* Returns TRUE if char at CHARPOS is quoted.
343 Global syntax-table data should be set up already to be good at CHARPOS
344 or after. On return global syntax data is good for lookup at CHARPOS. */
347 char_quoted (ptrdiff_t charpos
, ptrdiff_t bytepos
)
349 register enum syntaxcode code
;
350 register ptrdiff_t beg
= BEGV
;
351 register int quoted
= 0;
352 ptrdiff_t orig
= charpos
;
354 while (charpos
> beg
)
357 DEC_BOTH (charpos
, bytepos
);
359 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
);
360 c
= FETCH_CHAR_AS_MULTIBYTE (bytepos
);
362 if (! (code
== Scharquote
|| code
== Sescape
))
368 UPDATE_SYNTAX_TABLE (orig
);
372 /* Return the bytepos one character before BYTEPOS.
373 We assume that BYTEPOS is not at the start of the buffer. */
376 dec_bytepos (ptrdiff_t bytepos
)
378 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
385 /* Return a defun-start position before POS and not too far before.
386 It should be the last one before POS, or nearly the last.
388 When open_paren_in_column_0_is_defun_start is nonzero,
389 only the beginning of the buffer is treated as a defun-start.
391 We record the information about where the scan started
392 and what its result was, so that another call in the same area
393 can return the same value very quickly.
395 There is no promise at which position the global syntax data is
396 valid on return from the subroutine, so the caller should explicitly
397 update the global data. */
400 find_defun_start (ptrdiff_t pos
, ptrdiff_t pos_byte
)
402 ptrdiff_t opoint
= PT
, opoint_byte
= PT_BYTE
;
404 if (!open_paren_in_column_0_is_defun_start
)
406 find_start_value
= BEGV
;
407 find_start_value_byte
= BEGV_BYTE
;
408 find_start_buffer
= current_buffer
;
409 find_start_modiff
= MODIFF
;
410 find_start_begv
= BEGV
;
411 find_start_pos
= pos
;
415 /* Use previous finding, if it's valid and applies to this inquiry. */
416 if (current_buffer
== find_start_buffer
417 /* Reuse the defun-start even if POS is a little farther on.
418 POS might be in the next defun, but that's ok.
419 Our value may not be the best possible, but will still be usable. */
420 && pos
<= find_start_pos
+ 1000
421 && pos
>= find_start_value
422 && BEGV
== find_start_begv
423 && MODIFF
== find_start_modiff
)
424 return find_start_value
;
426 /* Back up to start of line. */
427 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
429 /* We optimize syntax-table lookup for rare updates. Thus we accept
430 only those `^\s(' which are good in global _and_ text-property
432 SETUP_BUFFER_SYNTAX_TABLE ();
437 /* Open-paren at start of line means we may have found our
439 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
440 if (SYNTAX (c
) == Sopen
)
442 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
443 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
444 if (SYNTAX (c
) == Sopen
)
446 /* Now fallback to the default value. */
447 SETUP_BUFFER_SYNTAX_TABLE ();
449 /* Move to beg of previous line. */
450 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
453 /* Record what we found, for the next try. */
454 find_start_value
= PT
;
455 find_start_value_byte
= PT_BYTE
;
456 find_start_buffer
= current_buffer
;
457 find_start_modiff
= MODIFF
;
458 find_start_begv
= BEGV
;
459 find_start_pos
= pos
;
461 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
463 return find_start_value
;
466 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
469 prev_char_comend_first (ptrdiff_t pos
, ptrdiff_t pos_byte
)
473 DEC_BOTH (pos
, pos_byte
);
474 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
475 c
= FETCH_CHAR (pos_byte
);
476 val
= SYNTAX_COMEND_FIRST (c
);
477 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
481 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
484 * prev_char_comstart_first (pos, pos_byte)
489 * DEC_BOTH (pos, pos_byte);
490 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
491 * c = FETCH_CHAR (pos_byte);
492 * val = SYNTAX_COMSTART_FIRST (c);
493 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
497 /* Checks whether charpos FROM is at the end of a comment.
498 FROM_BYTE is the bytepos corresponding to FROM.
499 Do not move back before STOP.
501 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
504 If successful, store the charpos of the comment's beginning
505 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
507 Global syntax data remains valid for backward search starting at
508 the returned value (or at FROM, if the search was not successful). */
511 back_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
, int comnested
, int comstyle
, ptrdiff_t *charpos_ptr
, ptrdiff_t *bytepos_ptr
)
513 /* Look back, counting the parity of string-quotes,
514 and recording the comment-starters seen.
515 When we reach a safe place, assume that's not in a string;
516 then step the main scan to the earliest comment-starter seen
517 an even number of string quotes away from the safe place.
519 OFROM[I] is position of the earliest comment-starter seen
520 which is I+2X quotes from the comment-end.
521 PARITY is current parity of quotes from the comment end. */
522 int string_style
= -1; /* Presumed outside of any string. */
523 int string_lossage
= 0;
524 /* Not a real lossage: indicates that we have passed a matching comment
525 starter plus a non-matching comment-ender, meaning that any matching
526 comment-starter we might see later could be a false positive (hidden
527 inside another comment).
528 Test case: { a (* b } c (* d *) */
529 int comment_lossage
= 0;
530 ptrdiff_t comment_end
= from
;
531 ptrdiff_t comment_end_byte
= from_byte
;
532 ptrdiff_t comstart_pos
= 0;
533 ptrdiff_t comstart_byte
IF_LINT (= 0);
534 /* Place where the containing defun starts,
535 or 0 if we didn't come across it yet. */
536 ptrdiff_t defun_start
= 0;
537 ptrdiff_t defun_start_byte
= 0;
538 register enum syntaxcode code
;
539 int nesting
= 1; /* current comment nesting */
543 /* FIXME: A }} comment-ender style leads to incorrect behavior
544 in the case of {{ c }}} because we ignore the last two chars which are
545 assumed to be comment-enders although they aren't. */
547 /* At beginning of range to scan, we're outside of strings;
548 that determines quote parity to the comment-end. */
552 int prev_syntax
, com2start
, com2end
;
555 /* Move back and examine a character. */
556 DEC_BOTH (from
, from_byte
);
557 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
559 prev_syntax
= syntax
;
560 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
561 syntax
= SYNTAX_WITH_FLAGS (c
);
564 /* Check for 2-char comment markers. */
565 com2start
= (SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
566 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax
)
568 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax
, syntax
))
569 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
570 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
);
571 com2end
= (SYNTAX_FLAGS_COMEND_FIRST (syntax
)
572 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax
));
573 comstart
= (com2start
|| code
== Scomment
);
575 /* Nasty cases with overlapping 2-char comment markers:
576 - snmp-mode: -- c -- foo -- c --
584 /* If a 2-char comment sequence partly overlaps with another,
585 we don't try to be clever. E.g. |*| in C, or }% in modes that
586 have %..\n and %{..}%. */
587 if (from
> stop
&& (com2end
|| comstart
))
589 ptrdiff_t next
= from
, next_byte
= from_byte
;
590 int next_c
, next_syntax
;
591 DEC_BOTH (next
, next_byte
);
592 UPDATE_SYNTAX_TABLE_BACKWARD (next
);
593 next_c
= FETCH_CHAR_AS_MULTIBYTE (next_byte
);
594 next_syntax
= SYNTAX_WITH_FLAGS (next_c
);
595 if (((comstart
|| comnested
)
596 && SYNTAX_FLAGS_COMEND_SECOND (syntax
)
597 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax
))
598 || ((com2end
|| comnested
)
599 && SYNTAX_FLAGS_COMSTART_SECOND (syntax
)
601 == SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_syntax
))
602 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax
)))
604 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
607 if (com2start
&& comstart_pos
== 0)
608 /* We're looking at a comment starter. But it might be a comment
609 ender as well (see snmp-mode). The first time we see one, we
610 need to consider it as a comment starter,
611 and the subsequent times as a comment ender. */
614 /* Turn a 2-char comment sequences into the appropriate syntax. */
619 /* Ignore comment starters of a different style. */
620 else if (code
== Scomment
621 && (comstyle
!= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0)
622 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
) != comnested
))
625 /* Ignore escaped characters, except comment-enders. */
626 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
633 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
635 /* Track parity of quotes. */
636 if (string_style
== -1)
637 /* Entering a string. */
639 else if (string_style
== c
)
640 /* Leaving the string. */
643 /* If we have two kinds of string delimiters.
644 There's no way to grok this scanning backwards. */
649 /* We've already checked that it is the relevant comstyle. */
650 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
651 /* There are odd string quotes involved, so let's be careful.
652 Test case in Pascal: " { " a { " } */
657 /* Record best comment-starter so far. */
659 comstart_byte
= from_byte
;
661 else if (--nesting
<= 0)
662 /* nested comments have to be balanced, so we don't need to
663 keep looking for earlier ones. We use here the same (slightly
664 incorrect) reasoning as below: since it is followed by uniform
665 paired string quotes, this comment-start has to be outside of
666 strings, else the comment-end itself would be inside a string. */
671 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == comstyle
672 && ((com2end
&& SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
))
673 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
674 /* This is the same style of comment ender as ours. */
679 /* Anything before that can't count because it would match
680 this comment-ender rather than ours. */
681 from
= stop
; /* Break out of the loop. */
683 else if (comstart_pos
!= 0 || c
!= '\n')
684 /* We're mixing comment styles here, so we'd better be careful.
685 The (comstart_pos != 0 || c != '\n') check is not quite correct
686 (we should just always set comment_lossage), but removing it
687 would imply that any multiline comment in C would go through
688 lossage, which seems overkill.
689 The failure should only happen in the rare cases such as
695 /* Assume a defun-start point is outside of strings. */
696 if (open_paren_in_column_0_is_defun_start
698 || (temp_byte
= dec_bytepos (from_byte
),
699 FETCH_CHAR (temp_byte
) == '\n')))
702 defun_start_byte
= from_byte
;
703 from
= stop
; /* Break out of the loop. */
712 if (comstart_pos
== 0)
715 from_byte
= comment_end_byte
;
716 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
- 1);
718 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
719 or `done'), then we've found the beginning of the non-nested comment. */
720 else if (1) /* !comnested */
723 from_byte
= comstart_byte
;
724 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
728 struct lisp_parse_state state
;
730 /* We had two kinds of string delimiters mixed up
731 together. Decode this going forwards.
732 Scan fwd from a known safe place (beginning-of-defun)
733 to the one in question; this records where we
734 last passed a comment starter. */
735 /* If we did not already find the defun start, find it now. */
736 if (defun_start
== 0)
738 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
739 defun_start_byte
= find_start_value_byte
;
743 scan_sexps_forward (&state
,
744 defun_start
, defun_start_byte
,
745 comment_end
, TYPE_MINIMUM (EMACS_INT
),
747 defun_start
= comment_end
;
748 if (state
.incomment
== (comnested
? 1 : -1)
749 && state
.comstyle
== comstyle
)
750 from
= state
.comstr_start
;
755 /* If comment_end is inside some other comment, maybe ours
756 is nested, so we need to try again from within the
757 surrounding comment. Example: { a (* " *) */
759 /* FIXME: We should advance by one or two chars. */
760 defun_start
= state
.comstr_start
+ 2;
761 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
764 } while (defun_start
< comment_end
);
766 from_byte
= CHAR_TO_BYTE (from
);
767 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
772 *bytepos_ptr
= from_byte
;
774 return (from
== comment_end
) ? -1 : from
;
777 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
778 doc
: /* Return t if OBJECT is a syntax table.
779 Currently, any char-table counts as a syntax table. */)
782 if (CHAR_TABLE_P (object
)
783 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
789 check_syntax_table (Lisp_Object obj
)
791 CHECK_TYPE (CHAR_TABLE_P (obj
) && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
),
792 Qsyntax_table_p
, obj
);
795 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
796 doc
: /* Return the current syntax table.
797 This is the one specified by the current buffer. */)
800 return BVAR (current_buffer
, syntax_table
);
803 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
804 Sstandard_syntax_table
, 0, 0, 0,
805 doc
: /* Return the standard syntax table.
806 This is the one used for new buffers. */)
809 return Vstandard_syntax_table
;
812 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
813 doc
: /* Construct a new syntax table and return it.
814 It is a copy of the TABLE, which defaults to the standard syntax table. */)
820 check_syntax_table (table
);
822 table
= Vstandard_syntax_table
;
824 copy
= Fcopy_sequence (table
);
826 /* Only the standard syntax table should have a default element.
827 Other syntax tables should inherit from parents instead. */
828 set_char_table_defalt (copy
, Qnil
);
830 /* Copied syntax tables should all have parents.
831 If we copied one with no parent, such as the standard syntax table,
832 use the standard syntax table as the copy's parent. */
833 if (NILP (XCHAR_TABLE (copy
)->parent
))
834 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
838 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
839 doc
: /* Select a new syntax table for the current buffer.
840 One argument, a syntax table. */)
844 check_syntax_table (table
);
845 bset_syntax_table (current_buffer
, table
);
846 /* Indicate that this buffer now has a specified syntax table. */
847 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
848 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
852 /* Convert a letter which signifies a syntax code
853 into the code it signifies.
854 This is used by modify-syntax-entry, and other things. */
856 unsigned char syntax_spec_code
[0400] =
857 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
858 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
859 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
860 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
861 (char) Swhitespace
, (char) Scomment_fence
, (char) Sstring
, 0377,
862 (char) Smath
, 0377, 0377, (char) Squote
,
863 (char) Sopen
, (char) Sclose
, 0377, 0377,
864 0377, (char) Swhitespace
, (char) Spunct
, (char) Scharquote
,
865 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
866 0377, 0377, 0377, 0377,
867 (char) Scomment
, 0377, (char) Sendcomment
, 0377,
868 (char) Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
869 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
870 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
871 0377, 0377, 0377, 0377, (char) Sescape
, 0377, 0377, (char) Ssymbol
,
872 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
873 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
874 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
875 0377, 0377, 0377, 0377, (char) Sstring_fence
, 0377, 0377, 0377
878 /* Indexed by syntax code, give the letter that describes it. */
880 char syntax_code_spec
[16] =
882 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
886 /* Indexed by syntax code, give the object (cons of syntax code and
887 nil) to be stored in syntax table. Since these objects can be
888 shared among syntax tables, we generate them in advance. By
889 sharing objects, the function `describe-syntax' can give a more
891 static Lisp_Object Vsyntax_code_object
;
894 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
895 doc
: /* Return the syntax code of CHARACTER, described by a character.
896 For example, if CHARACTER is a word constituent, the
897 character `w' (119) is returned.
898 The characters that correspond to various syntax codes
899 are listed in the documentation of `modify-syntax-entry'. */)
900 (Lisp_Object character
)
903 CHECK_CHARACTER (character
);
904 char_int
= XINT (character
);
905 SETUP_BUFFER_SYNTAX_TABLE ();
906 return make_number (syntax_code_spec
[(int) SYNTAX (char_int
)]);
909 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
910 doc
: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
911 (Lisp_Object character
)
914 CHECK_NUMBER (character
);
915 char_int
= XINT (character
);
916 SETUP_BUFFER_SYNTAX_TABLE ();
917 code
= SYNTAX (char_int
);
918 if (code
== Sopen
|| code
== Sclose
)
919 return SYNTAX_MATCH (char_int
);
923 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
924 doc
: /* Convert a syntax descriptor STRING into a raw syntax descriptor.
925 STRING should be a string of the form allowed as argument of
926 `modify-syntax-entry'. The return value is a raw syntax descriptor: a
927 cons cell \(CODE . MATCHING-CHAR) which can be used, for example, as
928 the value of a `syntax-table' text property. */)
931 register const unsigned char *p
;
932 register enum syntaxcode code
;
936 CHECK_STRING (string
);
939 code
= (enum syntaxcode
) syntax_spec_code
[*p
++];
940 if (((int) code
& 0377) == 0377)
941 error ("Invalid syntax description letter: %c", p
[-1]);
943 if (code
== Sinherit
)
949 int character
= STRING_CHAR_AND_LENGTH (p
, len
);
950 XSETINT (match
, character
);
951 if (XFASTINT (match
) == ' ')
995 if (val
< ASIZE (Vsyntax_code_object
) && NILP (match
))
996 return AREF (Vsyntax_code_object
, val
);
998 /* Since we can't use a shared object, let's make a new one. */
999 return Fcons (make_number (val
), match
);
1002 /* I really don't know why this is interactive
1003 help-form should at least be made useful whilst reading the second arg. */
1004 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
1005 "cSet syntax for character: \nsSet syntax for %s to: ",
1006 doc
: /* Set syntax for character CHAR according to string NEWENTRY.
1007 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1008 the current buffer's syntax table.
1009 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1010 in the range MIN to MAX are changed.
1011 The first character of NEWENTRY should be one of the following:
1012 Space or - whitespace syntax. w word constituent.
1013 _ symbol constituent. . punctuation.
1014 ( open-parenthesis. ) close-parenthesis.
1015 " string quote. \\ escape.
1016 $ paired delimiter. ' expression quote or prefix operator.
1017 < comment starter. > comment ender.
1018 / character-quote. @ inherit from parent table.
1019 | generic string fence. ! generic comment fence.
1021 Only single-character comment start and end sequences are represented thus.
1022 Two-character sequences are represented as described below.
1023 The second character of NEWENTRY is the matching parenthesis,
1024 used only if the first character is `(' or `)'.
1025 Any additional characters are flags.
1026 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1027 1 means CHAR is the start of a two-char comment start sequence.
1028 2 means CHAR is the second character of such a sequence.
1029 3 means CHAR is the start of a two-char comment end sequence.
1030 4 means CHAR is the second character of such a sequence.
1032 There can be several orthogonal comment sequences. This is to support
1033 language modes such as C++. By default, all comment sequences are of style
1034 a, but you can set the comment sequence style to b (on the second character
1035 of a comment-start, and the first character of a comment-end sequence) and/or
1036 c (on any of its chars) using this flag:
1037 b means CHAR is part of comment sequence b.
1038 c means CHAR is part of comment sequence c.
1039 n means CHAR is part of a nestable comment sequence.
1041 p means CHAR is a prefix character for `backward-prefix-chars';
1042 such characters are treated as whitespace when they occur
1043 between expressions.
1044 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1045 (Lisp_Object c
, Lisp_Object newentry
, Lisp_Object syntax_table
)
1049 CHECK_CHARACTER_CAR (c
);
1050 CHECK_CHARACTER_CDR (c
);
1053 CHECK_CHARACTER (c
);
1055 if (NILP (syntax_table
))
1056 syntax_table
= BVAR (current_buffer
, syntax_table
);
1058 check_syntax_table (syntax_table
);
1060 newentry
= Fstring_to_syntax (newentry
);
1062 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table
, c
, newentry
);
1064 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), newentry
);
1066 /* We clear the regexp cache, since character classes can now have
1067 different values from those in the compiled regexps.*/
1068 clear_regexp_cache ();
1073 /* Dump syntax table to buffer in human-readable format */
1075 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value
,
1076 Sinternal_describe_syntax_value
, 1, 1, 0,
1077 doc
: /* Insert a description of the internal syntax description SYNTAX at point. */)
1078 (Lisp_Object syntax
)
1080 register enum syntaxcode code
;
1082 char desc
, start1
, start2
, end1
, end2
, prefix
,
1083 comstyleb
, comstylec
, comnested
;
1085 Lisp_Object first
, match_lisp
, value
= syntax
;
1089 insert_string ("default");
1093 if (CHAR_TABLE_P (value
))
1095 insert_string ("deeper char-table ...");
1101 insert_string ("invalid");
1105 first
= XCAR (value
);
1106 match_lisp
= XCDR (value
);
1108 if (!INTEGERP (first
) || !(NILP (match_lisp
) || CHARACTERP (match_lisp
)))
1110 insert_string ("invalid");
1114 syntax_code
= XINT (first
) & INT_MAX
;
1115 code
= (enum syntaxcode
) (syntax_code
& 0377);
1116 start1
= SYNTAX_FLAGS_COMSTART_FIRST (syntax_code
);
1117 start2
= SYNTAX_FLAGS_COMSTART_SECOND (syntax_code
);;
1118 end1
= SYNTAX_FLAGS_COMEND_FIRST (syntax_code
);
1119 end2
= SYNTAX_FLAGS_COMEND_SECOND (syntax_code
);
1120 prefix
= SYNTAX_FLAGS_PREFIX (syntax_code
);
1121 comstyleb
= SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code
);
1122 comstylec
= SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code
);
1123 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax_code
);
1125 if ((int) code
< 0 || (int) code
>= (int) Smax
)
1127 insert_string ("invalid");
1130 desc
= syntax_code_spec
[(int) code
];
1132 str
[0] = desc
, str
[1] = 0;
1135 if (NILP (match_lisp
))
1138 insert_char (XINT (match_lisp
));
1159 insert_string ("\twhich means: ");
1164 insert_string ("whitespace"); break;
1166 insert_string ("punctuation"); break;
1168 insert_string ("word"); break;
1170 insert_string ("symbol"); break;
1172 insert_string ("open"); break;
1174 insert_string ("close"); break;
1176 insert_string ("prefix"); break;
1178 insert_string ("string"); break;
1180 insert_string ("math"); break;
1182 insert_string ("escape"); break;
1184 insert_string ("charquote"); break;
1186 insert_string ("comment"); break;
1188 insert_string ("endcomment"); break;
1190 insert_string ("inherit"); break;
1191 case Scomment_fence
:
1192 insert_string ("comment fence"); break;
1194 insert_string ("string fence"); break;
1196 insert_string ("invalid");
1200 if (!NILP (match_lisp
))
1202 insert_string (", matches ");
1203 insert_char (XINT (match_lisp
));
1207 insert_string (",\n\t is the first character of a comment-start sequence");
1209 insert_string (",\n\t is the second character of a comment-start sequence");
1212 insert_string (",\n\t is the first character of a comment-end sequence");
1214 insert_string (",\n\t is the second character of a comment-end sequence");
1216 insert_string (" (comment style b)");
1218 insert_string (" (comment style c)");
1220 insert_string (" (nestable)");
1223 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1228 /* Return the position across COUNT words from FROM.
1229 If that many words cannot be found before the end of the buffer, return 0.
1230 COUNT negative means scan backward and stop at word beginning. */
1233 scan_words (register ptrdiff_t from
, register EMACS_INT count
)
1235 register ptrdiff_t beg
= BEGV
;
1236 register ptrdiff_t end
= ZV
;
1237 register ptrdiff_t from_byte
= CHAR_TO_BYTE (from
);
1238 register enum syntaxcode code
;
1240 Lisp_Object func
, pos
;
1245 SETUP_SYNTAX_TABLE (from
, count
);
1256 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1257 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1258 code
= SYNTAX (ch0
);
1259 INC_BOTH (from
, from_byte
);
1260 if (words_include_escapes
1261 && (code
== Sescape
|| code
== Scharquote
))
1266 /* Now CH0 is a character which begins a word and FROM is the
1267 position of the next character. */
1268 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch0
);
1269 if (! NILP (Ffboundp (func
)))
1271 pos
= call2 (func
, make_number (from
- 1), make_number (end
));
1272 if (INTEGERP (pos
) && from
< XINT (pos
) && XINT (pos
) <= ZV
)
1275 from_byte
= CHAR_TO_BYTE (from
);
1282 if (from
== end
) break;
1283 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1284 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1285 code
= SYNTAX (ch1
);
1287 && (! words_include_escapes
1288 || (code
!= Sescape
&& code
!= Scharquote
)))
1289 || word_boundary_p (ch0
, ch1
))
1291 INC_BOTH (from
, from_byte
);
1306 DEC_BOTH (from
, from_byte
);
1307 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1308 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1309 code
= SYNTAX (ch1
);
1310 if (words_include_escapes
1311 && (code
== Sescape
|| code
== Scharquote
))
1316 /* Now CH1 is a character which ends a word and FROM is the
1318 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch1
);
1319 if (! NILP (Ffboundp (func
)))
1321 pos
= call2 (func
, make_number (from
), make_number (beg
));
1322 if (INTEGERP (pos
) && BEGV
<= XINT (pos
) && XINT (pos
) < from
)
1325 from_byte
= CHAR_TO_BYTE (from
);
1334 DEC_BOTH (from
, from_byte
);
1335 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1336 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1337 code
= SYNTAX (ch0
);
1339 && (! words_include_escapes
1340 || (code
!= Sescape
&& code
!= Scharquote
)))
1341 || word_boundary_p (ch0
, ch1
))
1343 INC_BOTH (from
, from_byte
);
1357 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 0, 1, "^p",
1358 doc
: /* Move point forward ARG words (backward if ARG is negative).
1360 If an edge of the buffer or a field boundary is reached, point is left there
1361 and the function returns nil. Field boundaries are not noticed if
1362 `inhibit-field-text-motion' is non-nil. */)
1366 ptrdiff_t orig_val
, val
;
1369 XSETFASTINT (arg
, 1);
1373 val
= orig_val
= scan_words (PT
, XINT (arg
));
1375 val
= XINT (arg
) > 0 ? ZV
: BEGV
;
1377 /* Avoid jumping out of an input field. */
1378 tmp
= Fconstrain_to_field (make_number (val
), make_number (PT
),
1380 val
= XFASTINT (tmp
);
1383 return val
== orig_val
? Qt
: Qnil
;
1386 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1387 doc
: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1388 STRING is like the inside of a `[...]' in a regular expression
1389 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1390 (but not at the end of a range; quoting is never needed there).
1391 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1392 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1393 Char classes, e.g. `[:alpha:]', are supported.
1395 Returns the distance traveled, either zero or positive. */)
1396 (Lisp_Object string
, Lisp_Object lim
)
1398 return skip_chars (1, string
, lim
, 1);
1401 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1402 doc
: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1403 See `skip-chars-forward' for details.
1404 Returns the distance traveled, either zero or negative. */)
1405 (Lisp_Object string
, Lisp_Object lim
)
1407 return skip_chars (0, string
, lim
, 1);
1410 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1411 doc
: /* Move point forward across chars in specified syntax classes.
1412 SYNTAX is a string of syntax code characters.
1413 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1414 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1415 This function returns the distance traveled, either zero or positive. */)
1416 (Lisp_Object syntax
, Lisp_Object lim
)
1418 return skip_syntaxes (1, syntax
, lim
);
1421 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1422 doc
: /* Move point backward across chars in specified syntax classes.
1423 SYNTAX is a string of syntax code characters.
1424 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1425 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1426 This function returns the distance traveled, either zero or negative. */)
1427 (Lisp_Object syntax
, Lisp_Object lim
)
1429 return skip_syntaxes (0, syntax
, lim
);
1433 skip_chars (int forwardp
, Lisp_Object string
, Lisp_Object lim
, int handle_iso_classes
)
1435 register unsigned int c
;
1436 unsigned char fastmap
[0400];
1437 /* Store the ranges of non-ASCII characters. */
1438 int *char_ranges
IF_LINT (= NULL
);
1439 int n_char_ranges
= 0;
1441 register ptrdiff_t i
, i_byte
;
1442 /* Set to 1 if the current buffer is multibyte and the region
1443 contains non-ASCII chars. */
1445 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1447 int string_multibyte
;
1448 ptrdiff_t size_byte
;
1449 const unsigned char *str
;
1451 Lisp_Object iso_classes
;
1453 CHECK_STRING (string
);
1457 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1459 CHECK_NUMBER_COERCE_MARKER (lim
);
1461 /* In any case, don't allow scan outside bounds of buffer. */
1462 if (XINT (lim
) > ZV
)
1463 XSETFASTINT (lim
, ZV
);
1464 if (XINT (lim
) < BEGV
)
1465 XSETFASTINT (lim
, BEGV
);
1467 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1468 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1469 string_multibyte
= SBYTES (string
) > SCHARS (string
);
1471 memset (fastmap
, 0, sizeof fastmap
);
1473 str
= SDATA (string
);
1474 size_byte
= SBYTES (string
);
1477 if (i_byte
< size_byte
1478 && SREF (string
, 0) == '^')
1480 negate
= 1; i_byte
++;
1483 /* Find the characters specified and set their elements of fastmap.
1484 Handle backslashes and ranges specially.
1486 If STRING contains non-ASCII characters, setup char_ranges for
1487 them and use fastmap only for their leading codes. */
1489 if (! string_multibyte
)
1491 int string_has_eight_bit
= 0;
1493 /* At first setup fastmap. */
1494 while (i_byte
< size_byte
)
1498 if (handle_iso_classes
&& c
== '['
1499 && i_byte
< size_byte
1500 && str
[i_byte
] == ':')
1502 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1503 const unsigned char *class_end
= class_beg
;
1504 const unsigned char *class_limit
= str
+ size_byte
- 2;
1505 /* Leave room for the null. */
1506 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1509 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1510 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1512 while (class_end
< class_limit
1513 && *class_end
>= 'a' && *class_end
<= 'z')
1516 if (class_end
== class_beg
1517 || *class_end
!= ':' || class_end
[1] != ']')
1518 goto not_a_class_name
;
1520 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1521 class_name
[class_end
- class_beg
] = 0;
1523 cc
= re_wctype (class_name
);
1525 error ("Invalid ISO C character class");
1527 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1529 i_byte
= class_end
+ 2 - str
;
1536 if (i_byte
== size_byte
)
1541 /* Treat `-' as range character only if another character
1543 if (i_byte
+ 1 < size_byte
1544 && str
[i_byte
] == '-')
1548 /* Skip over the dash. */
1551 /* Get the end of the range. */
1554 && i_byte
< size_byte
)
1559 unsigned lim2
= c2
+ 1;
1562 if (! ASCII_CHAR_P (c2
))
1563 string_has_eight_bit
= 1;
1569 if (! ASCII_CHAR_P (c
))
1570 string_has_eight_bit
= 1;
1574 /* If the current range is multibyte and STRING contains
1575 eight-bit chars, arrange fastmap and setup char_ranges for
1576 the corresponding multibyte chars. */
1577 if (multibyte
&& string_has_eight_bit
)
1579 unsigned char fastmap2
[0400];
1580 int range_start_byte
, range_start_char
;
1582 memcpy (fastmap
+ 0200, fastmap2
+ 0200, 0200);
1583 memset (fastmap
+ 0200, 0, 0200);
1584 /* We are sure that this loop stops. */
1585 for (i
= 0200; ! fastmap2
[i
]; i
++);
1586 c
= BYTE8_TO_CHAR (i
);
1587 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
1588 range_start_byte
= i
;
1589 range_start_char
= c
;
1590 char_ranges
= alloca (sizeof *char_ranges
* 128 * 2);
1591 for (i
= 129; i
< 0400; i
++)
1593 c
= BYTE8_TO_CHAR (i
);
1594 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
1595 if (i
- range_start_byte
!= c
- range_start_char
)
1597 char_ranges
[n_char_ranges
++] = range_start_char
;
1598 char_ranges
[n_char_ranges
++] = ((i
- 1 - range_start_byte
)
1599 + range_start_char
);
1600 range_start_byte
= i
;
1601 range_start_char
= c
;
1604 char_ranges
[n_char_ranges
++] = range_start_char
;
1605 char_ranges
[n_char_ranges
++] = ((i
- 1 - range_start_byte
)
1606 + range_start_char
);
1609 else /* STRING is multibyte */
1611 char_ranges
= alloca (sizeof *char_ranges
* SCHARS (string
) * 2);
1613 while (i_byte
< size_byte
)
1615 unsigned char leading_code
;
1617 leading_code
= str
[i_byte
];
1618 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1621 if (handle_iso_classes
&& c
== '['
1622 && i_byte
< size_byte
1623 && STRING_CHAR (str
+ i_byte
) == ':')
1625 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1626 const unsigned char *class_end
= class_beg
;
1627 const unsigned char *class_limit
= str
+ size_byte
- 2;
1628 /* Leave room for the null. */
1629 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1632 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1633 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1635 while (class_end
< class_limit
1636 && *class_end
>= 'a' && *class_end
<= 'z')
1639 if (class_end
== class_beg
1640 || *class_end
!= ':' || class_end
[1] != ']')
1641 goto not_a_class_name_multibyte
;
1643 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1644 class_name
[class_end
- class_beg
] = 0;
1646 cc
= re_wctype (class_name
);
1648 error ("Invalid ISO C character class");
1650 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1652 i_byte
= class_end
+ 2 - str
;
1656 not_a_class_name_multibyte
:
1659 if (i_byte
== size_byte
)
1662 leading_code
= str
[i_byte
];
1663 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1666 /* Treat `-' as range character only if another character
1668 if (i_byte
+ 1 < size_byte
1669 && str
[i_byte
] == '-')
1672 unsigned char leading_code2
;
1674 /* Skip over the dash. */
1677 /* Get the end of the range. */
1678 leading_code2
= str
[i_byte
];
1679 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1683 && i_byte
< size_byte
)
1685 leading_code2
= str
[i_byte
];
1686 c2
=STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1692 if (ASCII_CHAR_P (c
))
1694 while (c
<= c2
&& c
< 0x80)
1696 leading_code
= CHAR_LEADING_CODE (c
);
1698 if (! ASCII_CHAR_P (c
))
1700 unsigned lim2
= leading_code2
+ 1;
1701 while (leading_code
< lim2
)
1702 fastmap
[leading_code
++] = 1;
1705 char_ranges
[n_char_ranges
++] = c
;
1706 char_ranges
[n_char_ranges
++] = c2
;
1712 if (ASCII_CHAR_P (c
))
1716 fastmap
[leading_code
] = 1;
1717 char_ranges
[n_char_ranges
++] = c
;
1718 char_ranges
[n_char_ranges
++] = c
;
1723 /* If the current range is unibyte and STRING contains non-ASCII
1724 chars, arrange fastmap for the corresponding unibyte
1727 if (! multibyte
&& n_char_ranges
> 0)
1729 memset (fastmap
+ 0200, 0, 0200);
1730 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1732 int c1
= char_ranges
[i
];
1733 unsigned lim2
= char_ranges
[i
+ 1] + 1;
1735 for (; c1
< lim2
; c1
++)
1737 int b
= CHAR_TO_BYTE_SAFE (c1
);
1745 /* If ^ was the first character, complement the fastmap. */
1749 for (i
= 0; i
< sizeof fastmap
; i
++)
1753 for (i
= 0; i
< 0200; i
++)
1755 /* All non-ASCII chars possibly match. */
1756 for (; i
< sizeof fastmap
; i
++)
1762 ptrdiff_t start_point
= PT
;
1764 ptrdiff_t pos_byte
= PT_BYTE
;
1765 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1769 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1770 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1774 endp
= CHAR_POS_ADDR (XINT (lim
));
1775 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1779 /* This code may look up syntax tables using macros that rely on the
1780 gl_state object. To make sure this object is not out of date,
1781 let's initialize it manually.
1782 We ignore syntax-table text-properties for now, since that's
1783 what we've done in the past. */
1784 SETUP_BUFFER_SYNTAX_TABLE ();
1799 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
1800 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1810 if (! ASCII_CHAR_P (c
))
1812 /* As we are looking at a multibyte character, we
1813 must look up the character in the table
1814 CHAR_RANGES. If there's no data in the table,
1815 that character is not what we want to skip. */
1817 /* The following code do the right thing even if
1818 n_char_ranges is zero (i.e. no data in
1820 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1821 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1823 if (!(negate
^ (i
< n_char_ranges
)))
1827 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
1840 if (!NILP (iso_classes
) && in_classes (*p
, iso_classes
))
1845 goto fwd_unibyte_ok
;
1851 p
++, pos
++, pos_byte
++;
1859 unsigned char *prev_p
;
1869 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
1870 c
= STRING_CHAR (p
);
1872 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1882 if (! ASCII_CHAR_P (c
))
1884 /* See the comment in the previous similar code. */
1885 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1886 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1888 if (!(negate
^ (i
< n_char_ranges
)))
1892 pos
--, pos_byte
-= prev_p
- p
;
1905 if (! NILP (iso_classes
) && in_classes (p
[-1], iso_classes
))
1910 goto back_unibyte_ok
;
1913 if (!fastmap
[p
[-1]])
1916 p
--, pos
--, pos_byte
--;
1920 SET_PT_BOTH (pos
, pos_byte
);
1923 return make_number (PT
- start_point
);
1929 skip_syntaxes (int forwardp
, Lisp_Object string
, Lisp_Object lim
)
1931 register unsigned int c
;
1932 unsigned char fastmap
[0400];
1934 register ptrdiff_t i
, i_byte
;
1936 ptrdiff_t size_byte
;
1939 CHECK_STRING (string
);
1942 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1944 CHECK_NUMBER_COERCE_MARKER (lim
);
1946 /* In any case, don't allow scan outside bounds of buffer. */
1947 if (XINT (lim
) > ZV
)
1948 XSETFASTINT (lim
, ZV
);
1949 if (XINT (lim
) < BEGV
)
1950 XSETFASTINT (lim
, BEGV
);
1952 if (forwardp
? (PT
>= XFASTINT (lim
)) : (PT
<= XFASTINT (lim
)))
1953 return make_number (0);
1955 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1956 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1958 memset (fastmap
, 0, sizeof fastmap
);
1960 if (SBYTES (string
) > SCHARS (string
))
1961 /* As this is very rare case (syntax spec is ASCII only), don't
1962 consider efficiency. */
1963 string
= string_make_unibyte (string
);
1965 str
= SDATA (string
);
1966 size_byte
= SBYTES (string
);
1969 if (i_byte
< size_byte
1970 && SREF (string
, 0) == '^')
1972 negate
= 1; i_byte
++;
1975 /* Find the syntaxes specified and set their elements of fastmap. */
1977 while (i_byte
< size_byte
)
1980 fastmap
[syntax_spec_code
[c
]] = 1;
1983 /* If ^ was the first character, complement the fastmap. */
1985 for (i
= 0; i
< sizeof fastmap
; i
++)
1989 ptrdiff_t start_point
= PT
;
1991 ptrdiff_t pos_byte
= PT_BYTE
;
1992 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1996 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1997 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
2001 endp
= CHAR_POS_ADDR (XINT (lim
));
2002 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
2006 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
2022 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
2023 if (! fastmap
[(int) SYNTAX (c
)])
2025 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2026 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2040 if (! fastmap
[(int) SYNTAX (*p
)])
2042 p
++, pos
++, pos_byte
++;
2043 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2053 unsigned char *prev_p
;
2062 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2064 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2065 c
= STRING_CHAR (p
);
2066 if (! fastmap
[(int) SYNTAX (c
)])
2068 pos
--, pos_byte
-= prev_p
- p
;
2082 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2083 if (! fastmap
[(int) SYNTAX (p
[-1])])
2085 p
--, pos
--, pos_byte
--;
2090 SET_PT_BOTH (pos
, pos_byte
);
2093 return make_number (PT
- start_point
);
2097 /* Return 1 if character C belongs to one of the ISO classes
2098 in the list ISO_CLASSES. Each class is represented by an
2099 integer which is its type according to re_wctype. */
2102 in_classes (int c
, Lisp_Object iso_classes
)
2106 while (CONSP (iso_classes
))
2109 elt
= XCAR (iso_classes
);
2110 iso_classes
= XCDR (iso_classes
);
2112 if (re_iswctype (c
, XFASTINT (elt
)))
2119 /* Jump over a comment, assuming we are at the beginning of one.
2120 FROM is the current position.
2121 FROM_BYTE is the bytepos corresponding to FROM.
2122 Do not move past STOP (a charpos).
2123 The comment over which we have to jump is of style STYLE
2124 (either SYNTAX_FLAGS_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2125 NESTING should be positive to indicate the nesting at the beginning
2126 for nested comments and should be zero or negative else.
2127 ST_COMMENT_STYLE cannot be nested.
2128 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2129 (or 0 If the search cannot start in the middle of a two-character).
2131 If successful, return 1 and store the charpos of the comment's end
2132 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2133 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2134 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2135 (as defined for state.incomment) in *INCOMMENT_PTR.
2137 The comment end is the last character of the comment rather than the
2138 character just after the comment.
2140 Global syntax data is assumed to initially be valid for FROM and
2141 remains valid for forward search starting at the returned position. */
2144 forw_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
,
2145 EMACS_INT nesting
, int style
, int prev_syntax
,
2146 ptrdiff_t *charpos_ptr
, ptrdiff_t *bytepos_ptr
,
2147 EMACS_INT
*incomment_ptr
)
2150 register enum syntaxcode code
;
2151 register int syntax
, other_syntax
;
2153 if (nesting
<= 0) nesting
= -1;
2155 /* Enter the loop in the middle so that we find
2156 a 2-char comment ender if we start in the middle of it. */
2157 syntax
= prev_syntax
;
2158 if (syntax
!= 0) goto forw_incomment
;
2164 *incomment_ptr
= nesting
;
2165 *charpos_ptr
= from
;
2166 *bytepos_ptr
= from_byte
;
2169 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2170 syntax
= SYNTAX_WITH_FLAGS (c
);
2171 code
= syntax
& 0xff;
2172 if (code
== Sendcomment
2173 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
2174 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
2175 (nesting
> 0 && --nesting
== 0) : nesting
< 0))
2176 /* we have encountered a comment end of the same style
2177 as the comment sequence which began this comment
2180 if (code
== Scomment_fence
2181 && style
== ST_COMMENT_STYLE
)
2182 /* we have encountered a comment end of the same style
2183 as the comment sequence which began this comment
2188 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
2189 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
)
2190 /* we have encountered a nested comment of the same style
2191 as the comment sequence which began this comment section */
2193 INC_BOTH (from
, from_byte
);
2194 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2197 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2198 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2199 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2200 SYNTAX_FLAGS_COMEND_SECOND (other_syntax
))
2201 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, other_syntax
) == style
2202 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2203 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
))
2204 ? nesting
> 0 : nesting
< 0))
2207 /* we have encountered a comment end of the same style
2208 as the comment sequence which began this comment
2213 INC_BOTH (from
, from_byte
);
2214 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2219 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
2220 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2221 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2222 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
) == style
2223 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2224 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2225 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
)))
2226 /* we have encountered a nested comment of the same style
2227 as the comment sequence which began this comment
2230 INC_BOTH (from
, from_byte
);
2231 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2235 *charpos_ptr
= from
;
2236 *bytepos_ptr
= from_byte
;
2240 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
2242 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2243 Stop scanning if we find something other than a comment or whitespace.
2244 Set point to where scanning stops.
2245 If COUNT comments are found as expected, with nothing except whitespace
2246 between them, return t; otherwise return nil. */)
2249 register ptrdiff_t from
;
2250 ptrdiff_t from_byte
;
2251 register ptrdiff_t stop
;
2253 register enum syntaxcode code
;
2254 int comstyle
= 0; /* style of comment encountered */
2255 int comnested
= 0; /* whether the comment is nestable or not */
2258 ptrdiff_t out_charpos
, out_bytepos
;
2261 CHECK_NUMBER (count
);
2262 count1
= XINT (count
);
2263 stop
= count1
> 0 ? ZV
: BEGV
;
2269 from_byte
= PT_BYTE
;
2271 SETUP_SYNTAX_TABLE (from
, count1
);
2276 int comstart_first
, syntax
, other_syntax
;
2280 SET_PT_BOTH (from
, from_byte
);
2284 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2285 syntax
= SYNTAX_WITH_FLAGS (c
);
2287 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2288 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2289 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2290 INC_BOTH (from
, from_byte
);
2291 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2292 if (from
< stop
&& comstart_first
2293 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2294 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2295 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
)))
2297 /* We have encountered a comment start sequence and we
2298 are ignoring all text inside comments. We must record
2299 the comment style this sequence begins so that later,
2300 only a comment end of the same style actually ends
2301 the comment section. */
2303 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2305 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2306 INC_BOTH (from
, from_byte
);
2307 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2310 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
2312 if (code
== Scomment_fence
)
2313 comstyle
= ST_COMMENT_STYLE
;
2314 else if (code
!= Scomment
)
2317 DEC_BOTH (from
, from_byte
);
2318 SET_PT_BOTH (from
, from_byte
);
2321 /* We're at the start of a comment. */
2322 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
2323 &out_charpos
, &out_bytepos
, &dummy
);
2324 from
= out_charpos
; from_byte
= out_bytepos
;
2328 SET_PT_BOTH (from
, from_byte
);
2331 INC_BOTH (from
, from_byte
);
2332 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2333 /* We have skipped one comment. */
2345 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
2350 DEC_BOTH (from
, from_byte
);
2351 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2352 quoted
= char_quoted (from
, from_byte
);
2353 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2354 syntax
= SYNTAX_WITH_FLAGS (c
);
2357 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2358 if (code
== Sendcomment
)
2359 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2360 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2361 && prev_char_comend_first (from
, from_byte
)
2362 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
2365 /* We must record the comment style encountered so that
2366 later, we can match only the proper comment begin
2367 sequence of the same style. */
2368 DEC_BOTH (from
, from_byte
);
2370 /* Calling char_quoted, above, set up global syntax position
2371 at the new value of FROM. */
2372 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2373 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2374 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2376 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2379 if (code
== Scomment_fence
)
2381 /* Skip until first preceding unquoted comment_fence. */
2382 int fence_found
= 0;
2383 ptrdiff_t ini
= from
, ini_byte
= from_byte
;
2387 DEC_BOTH (from
, from_byte
);
2388 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2389 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2390 if (SYNTAX (c
) == Scomment_fence
2391 && !char_quoted (from
, from_byte
))
2396 else if (from
== stop
)
2399 if (fence_found
== 0)
2401 from
= ini
; /* Set point to ini + 1. */
2402 from_byte
= ini_byte
;
2406 /* We have skipped one comment. */
2409 else if (code
== Sendcomment
)
2411 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2412 &out_charpos
, &out_bytepos
);
2416 /* This end-of-line is not an end-of-comment.
2417 Treat it like a whitespace.
2418 CC-mode (and maybe others) relies on this behavior. */
2422 /* Failure: we should go back to the end of this
2423 not-quite-endcomment. */
2424 if (SYNTAX (c
) != code
)
2425 /* It was a two-char Sendcomment. */
2426 INC_BOTH (from
, from_byte
);
2432 /* We have skipped one comment. */
2433 from
= out_charpos
, from_byte
= out_bytepos
;
2437 else if (code
!= Swhitespace
|| quoted
)
2441 INC_BOTH (from
, from_byte
);
2442 SET_PT_BOTH (from
, from_byte
);
2450 SET_PT_BOTH (from
, from_byte
);
2455 /* Return syntax code of character C if C is an ASCII character
2456 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2458 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2459 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2460 ? SYNTAX (c) : Ssymbol)
2463 scan_lists (register EMACS_INT from
, EMACS_INT count
, EMACS_INT depth
, int sexpflag
)
2466 register ptrdiff_t stop
= count
> 0 ? ZV
: BEGV
;
2471 register enum syntaxcode code
, temp_code
;
2472 EMACS_INT min_depth
= depth
; /* Err out if depth gets less than this. */
2473 int comstyle
= 0; /* style of comment encountered */
2474 int comnested
= 0; /* whether the comment is nestable or not */
2476 EMACS_INT last_good
= from
;
2478 ptrdiff_t from_byte
;
2479 ptrdiff_t out_bytepos
, out_charpos
;
2482 int multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2484 if (depth
> 0) min_depth
= 0;
2486 if (from
> ZV
) from
= ZV
;
2487 if (from
< BEGV
) from
= BEGV
;
2489 from_byte
= CHAR_TO_BYTE (from
);
2494 SETUP_SYNTAX_TABLE (from
, count
);
2499 int comstart_first
, prefix
, syntax
, other_syntax
;
2500 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2501 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2502 syntax
= SYNTAX_WITH_FLAGS (c
);
2503 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2504 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2505 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2506 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2507 prefix
= SYNTAX_FLAGS_PREFIX (syntax
);
2508 if (depth
== min_depth
)
2510 INC_BOTH (from
, from_byte
);
2511 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2512 if (from
< stop
&& comstart_first
2513 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2514 other_syntax
= SYNTAX_WITH_FLAGS (c
),
2515 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2516 && parse_sexp_ignore_comments
)
2518 /* we have encountered a comment start sequence and we
2519 are ignoring all text inside comments. We must record
2520 the comment style this sequence begins so that later,
2521 only a comment end of the same style actually ends
2522 the comment section */
2524 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2526 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2527 INC_BOTH (from
, from_byte
);
2528 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2540 INC_BOTH (from
, from_byte
);
2541 /* treat following character as a word constituent */
2544 if (depth
|| !sexpflag
) break;
2545 /* This word counts as a sexp; return at end of it. */
2548 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2550 /* Some compilers can't handle this inside the switch. */
2551 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2552 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2557 INC_BOTH (from
, from_byte
);
2568 INC_BOTH (from
, from_byte
);
2572 case Scomment_fence
:
2573 comstyle
= ST_COMMENT_STYLE
;
2576 if (!parse_sexp_ignore_comments
) break;
2577 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2578 found
= forw_comment (from
, from_byte
, stop
,
2579 comnested
, comstyle
, 0,
2580 &out_charpos
, &out_bytepos
, &dummy
);
2581 from
= out_charpos
, from_byte
= out_bytepos
;
2588 INC_BOTH (from
, from_byte
);
2589 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2595 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (from_byte
))
2597 INC_BOTH (from
, from_byte
);
2607 if (!++depth
) goto done
;
2612 if (!--depth
) goto done
;
2613 if (depth
< min_depth
)
2614 xsignal3 (Qscan_error
,
2615 build_string ("Containing expression ends prematurely"),
2616 make_number (last_good
), make_number (from
));
2621 temp_pos
= dec_bytepos (from_byte
);
2622 stringterm
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2627 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2628 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2631 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2632 : SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring_fence
)
2635 /* Some compilers can't handle this inside the switch. */
2636 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2641 INC_BOTH (from
, from_byte
);
2643 INC_BOTH (from
, from_byte
);
2645 INC_BOTH (from
, from_byte
);
2646 if (!depth
&& sexpflag
) goto done
;
2649 /* Ignore whitespace, punctuation, quote, endcomment. */
2654 /* Reached end of buffer. Error if within object, return nil if between */
2661 /* End of object reached */
2671 DEC_BOTH (from
, from_byte
);
2672 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2673 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2674 syntax
= SYNTAX_WITH_FLAGS (c
);
2675 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2676 if (depth
== min_depth
)
2679 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2680 if (code
== Sendcomment
)
2681 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2682 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2683 && prev_char_comend_first (from
, from_byte
)
2684 && parse_sexp_ignore_comments
)
2686 /* We must record the comment style encountered so that
2687 later, we can match only the proper comment begin
2688 sequence of the same style. */
2689 int c2
, other_syntax
;
2690 DEC_BOTH (from
, from_byte
);
2691 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2693 c2
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2694 other_syntax
= SYNTAX_WITH_FLAGS (c2
);
2695 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2697 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2700 /* Quoting turns anything except a comment-ender
2701 into a word character. Note that this cannot be true
2702 if we decremented FROM in the if-statement above. */
2703 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2705 DEC_BOTH (from
, from_byte
);
2708 else if (SYNTAX_FLAGS_PREFIX (syntax
))
2717 if (depth
|| !sexpflag
) break;
2718 /* This word counts as a sexp; count object finished
2719 after passing it. */
2722 temp_pos
= from_byte
;
2723 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
2727 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2728 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2729 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2730 /* Don't allow comment-end to be quoted. */
2731 if (temp_code
== Sendcomment
)
2733 quoted
= char_quoted (from
- 1, temp_pos
);
2736 DEC_BOTH (from
, from_byte
);
2737 temp_pos
= dec_bytepos (temp_pos
);
2738 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2740 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2741 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2742 if (! (quoted
|| temp_code
== Sword
2743 || temp_code
== Ssymbol
2744 || temp_code
== Squote
))
2746 DEC_BOTH (from
, from_byte
);
2753 temp_pos
= dec_bytepos (from_byte
);
2754 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2755 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (temp_pos
))
2756 DEC_BOTH (from
, from_byte
);
2765 if (!++depth
) goto done2
;
2770 if (!--depth
) goto done2
;
2771 if (depth
< min_depth
)
2772 xsignal3 (Qscan_error
,
2773 build_string ("Containing expression ends prematurely"),
2774 make_number (last_good
), make_number (from
));
2778 if (!parse_sexp_ignore_comments
)
2780 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2781 &out_charpos
, &out_bytepos
);
2782 /* FIXME: if found == -1, then it really wasn't a comment-end.
2783 For single-char Sendcomment, we can't do much about it apart
2784 from skipping the char.
2785 For 2-char endcomments, we could try again, taking both
2786 chars as separate entities, but it's a lot of trouble
2787 for very little gain, so we don't bother either. -sm */
2789 from
= out_charpos
, from_byte
= out_bytepos
;
2792 case Scomment_fence
:
2798 DEC_BOTH (from
, from_byte
);
2799 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2800 if (!char_quoted (from
, from_byte
)
2801 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2802 SYNTAX_WITH_MULTIBYTE_CHECK (c
) == code
))
2805 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2809 stringterm
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2814 DEC_BOTH (from
, from_byte
);
2815 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2816 if (!char_quoted (from
, from_byte
)
2818 == (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
)))
2819 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2822 if (!depth
&& sexpflag
) goto done2
;
2825 /* Ignore whitespace, punctuation, quote, endcomment. */
2830 /* Reached start of buffer. Error if within object, return nil if between */
2843 XSETFASTINT (val
, from
);
2847 xsignal3 (Qscan_error
,
2848 build_string ("Unbalanced parentheses"),
2849 make_number (last_good
), make_number (from
));
2852 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
2853 doc
: /* Scan from character number FROM by COUNT lists.
2854 Scan forward if COUNT is positive, backward if COUNT is negative.
2855 Return the character number of the position thus found.
2857 A \"list", in this context, refers to a balanced parenthetical
2858 grouping, as determined by the syntax table.
2860 If DEPTH is nonzero, treat that as the nesting depth of the starting
2861 point (i.e. the starting point is DEPTH parentheses deep). This
2862 function scans over parentheses until the depth goes to zero COUNT
2863 times. Hence, positive DEPTH moves out that number of levels of
2864 parentheses, while negative DEPTH moves to a deeper level.
2866 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2868 If we reach the beginning or end of the accessible part of the buffer
2869 before we have scanned over COUNT lists, return nil if the depth at
2870 that point is zero, and signal a error if the depth is nonzero. */)
2871 (Lisp_Object from
, Lisp_Object count
, Lisp_Object depth
)
2873 CHECK_NUMBER (from
);
2874 CHECK_NUMBER (count
);
2875 CHECK_NUMBER (depth
);
2877 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
2880 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
2881 doc
: /* Scan from character number FROM by COUNT balanced expressions.
2882 If COUNT is negative, scan backwards.
2883 Returns the character number of the position thus found.
2885 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2887 If the beginning or end of (the accessible part of) the buffer is reached
2888 in the middle of a parenthetical grouping, an error is signaled.
2889 If the beginning or end is reached between groupings
2890 but before count is used up, nil is returned. */)
2891 (Lisp_Object from
, Lisp_Object count
)
2893 CHECK_NUMBER (from
);
2894 CHECK_NUMBER (count
);
2896 return scan_lists (XINT (from
), XINT (count
), 0, 1);
2899 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
2901 doc
: /* Move point backward over any number of chars with prefix syntax.
2902 This includes chars with "quote" or "prefix" syntax (' or p). */)
2905 ptrdiff_t beg
= BEGV
;
2906 ptrdiff_t opoint
= PT
;
2907 ptrdiff_t opoint_byte
= PT_BYTE
;
2909 ptrdiff_t pos_byte
= PT_BYTE
;
2914 SET_PT_BOTH (opoint
, opoint_byte
);
2919 SETUP_SYNTAX_TABLE (pos
, -1);
2921 DEC_BOTH (pos
, pos_byte
);
2923 while (!char_quoted (pos
, pos_byte
)
2924 /* Previous statement updates syntax table. */
2925 && ((c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
), SYNTAX (c
) == Squote
)
2926 || SYNTAX_PREFIX (c
)))
2929 opoint_byte
= pos_byte
;
2932 DEC_BOTH (pos
, pos_byte
);
2935 SET_PT_BOTH (opoint
, opoint_byte
);
2940 /* Parse forward from FROM / FROM_BYTE to END,
2941 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2942 and return a description of the state of the parse at END.
2943 If STOPBEFORE is nonzero, stop at the start of an atom.
2944 If COMMENTSTOP is 1, stop at the start of a comment.
2945 If COMMENTSTOP is -1, stop at the start or end of a comment,
2946 after the beginning of a string, or after the end of a string. */
2949 scan_sexps_forward (struct lisp_parse_state
*stateptr
,
2950 ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t end
,
2951 EMACS_INT targetdepth
, int stopbefore
,
2952 Lisp_Object oldstate
, int commentstop
)
2954 struct lisp_parse_state state
;
2956 register enum syntaxcode code
;
2959 struct level
{ ptrdiff_t last
, prev
; };
2960 struct level levelstart
[100];
2961 register struct level
*curlevel
= levelstart
;
2962 struct level
*endlevel
= levelstart
+ 100;
2963 register EMACS_INT depth
; /* Paren depth of current scanning location.
2964 level - levelstart equals this except
2965 when the depth becomes negative. */
2966 EMACS_INT mindepth
; /* Lowest DEPTH value seen. */
2967 int start_quoted
= 0; /* Nonzero means starting after a char quote */
2969 ptrdiff_t prev_from
; /* Keep one character before FROM. */
2970 ptrdiff_t prev_from_byte
;
2971 int prev_from_syntax
;
2972 int boundary_stop
= commentstop
== -1;
2975 ptrdiff_t out_bytepos
, out_charpos
;
2979 prev_from_byte
= from_byte
;
2981 DEC_BOTH (prev_from
, prev_from_byte
);
2983 /* Use this macro instead of `from++'. */
2985 do { prev_from = from; \
2986 prev_from_byte = from_byte; \
2987 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2988 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2989 INC_BOTH (from, from_byte); \
2991 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2997 if (NILP (oldstate
))
3000 state
.instring
= -1;
3001 state
.incomment
= 0;
3002 state
.comstyle
= 0; /* comment style a by default. */
3003 state
.comstr_start
= -1; /* no comment/string seen. */
3007 tem
= Fcar (oldstate
);
3013 oldstate
= Fcdr (oldstate
);
3014 oldstate
= Fcdr (oldstate
);
3015 oldstate
= Fcdr (oldstate
);
3016 tem
= Fcar (oldstate
);
3017 /* Check whether we are inside string_fence-style string: */
3018 state
.instring
= (!NILP (tem
)
3019 ? (CHARACTERP (tem
) ? XFASTINT (tem
) : ST_STRING_STYLE
)
3022 oldstate
= Fcdr (oldstate
);
3023 tem
= Fcar (oldstate
);
3024 state
.incomment
= (!NILP (tem
)
3025 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
3028 oldstate
= Fcdr (oldstate
);
3029 tem
= Fcar (oldstate
);
3030 start_quoted
= !NILP (tem
);
3032 /* if the eighth element of the list is nil, we are in comment
3033 style a. If it is non-nil, we are in comment style b */
3034 oldstate
= Fcdr (oldstate
);
3035 oldstate
= Fcdr (oldstate
);
3036 tem
= Fcar (oldstate
);
3037 state
.comstyle
= (NILP (tem
)
3039 : (RANGED_INTEGERP (0, tem
, ST_COMMENT_STYLE
)
3041 : ST_COMMENT_STYLE
));
3043 oldstate
= Fcdr (oldstate
);
3044 tem
= Fcar (oldstate
);
3045 state
.comstr_start
=
3046 RANGED_INTEGERP (PTRDIFF_MIN
, tem
, PTRDIFF_MAX
) ? XINT (tem
) : -1;
3047 oldstate
= Fcdr (oldstate
);
3048 tem
= Fcar (oldstate
);
3049 while (!NILP (tem
)) /* >= second enclosing sexps. */
3051 Lisp_Object temhd
= Fcar (tem
);
3052 if (RANGED_INTEGERP (PTRDIFF_MIN
, temhd
, PTRDIFF_MAX
))
3053 curlevel
->last
= XINT (temhd
);
3054 if (++curlevel
== endlevel
)
3055 curlevel
--; /* error ("Nesting too deep for parser"); */
3056 curlevel
->prev
= -1;
3057 curlevel
->last
= -1;
3064 curlevel
->prev
= -1;
3065 curlevel
->last
= -1;
3067 SETUP_SYNTAX_TABLE (prev_from
, 1);
3068 temp
= FETCH_CHAR (prev_from_byte
);
3069 prev_from_syntax
= SYNTAX_WITH_FLAGS (temp
);
3070 UPDATE_SYNTAX_TABLE_FORWARD (from
);
3072 /* Enter the loop at a place appropriate for initial state. */
3074 if (state
.incomment
)
3075 goto startincomment
;
3076 if (state
.instring
>= 0)
3078 nofence
= state
.instring
!= ST_STRING_STYLE
;
3080 goto startquotedinstring
;
3083 else if (start_quoted
)
3090 code
= prev_from_syntax
& 0xff;
3093 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
)
3094 && (c1
= FETCH_CHAR (from_byte
),
3095 syntax
= SYNTAX_WITH_FLAGS (c1
),
3096 SYNTAX_FLAGS_COMSTART_SECOND (syntax
)))
3097 /* Duplicate code to avoid a complex if-expression
3098 which causes trouble for the SGI compiler. */
3100 /* Record the comment style we have entered so that only
3101 the comment-end sequence of the same style actually
3102 terminates the comment section. */
3104 = SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_from_syntax
);
3105 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
);
3106 comnested
= comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
3107 state
.incomment
= comnested
? 1 : -1;
3108 state
.comstr_start
= prev_from
;
3112 else if (code
== Scomment_fence
)
3114 /* Record the comment style we have entered so that only
3115 the comment-end sequence of the same style actually
3116 terminates the comment section. */
3117 state
.comstyle
= ST_COMMENT_STYLE
;
3118 state
.incomment
= -1;
3119 state
.comstr_start
= prev_from
;
3122 else if (code
== Scomment
)
3124 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
, 0);
3125 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
3127 state
.comstr_start
= prev_from
;
3130 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
3136 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3137 curlevel
->last
= prev_from
;
3139 if (from
== end
) goto endquoted
;
3142 /* treat following character as a word constituent */
3145 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3146 curlevel
->last
= prev_from
;
3150 /* Some compilers can't handle this inside the switch. */
3151 temp
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3152 temp
= SYNTAX (temp
);
3158 if (from
== end
) goto endquoted
;
3170 curlevel
->prev
= curlevel
->last
;
3173 case Scomment_fence
: /* Can't happen because it's handled above. */
3175 if (commentstop
|| boundary_stop
) goto done
;
3177 /* The (from == BEGV) test was to enter the loop in the middle so
3178 that we find a 2-char comment ender even if we start in the
3179 middle of it. We don't want to do that if we're just at the
3180 beginning of the comment (think of (*) ... (*)). */
3181 found
= forw_comment (from
, from_byte
, end
,
3182 state
.incomment
, state
.comstyle
,
3183 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
3184 ? 0 : prev_from_syntax
,
3185 &out_charpos
, &out_bytepos
, &state
.incomment
);
3186 from
= out_charpos
; from_byte
= out_bytepos
;
3187 /* Beware! prev_from and friends are invalid now.
3188 Luckily, the `done' doesn't use them and the INC_FROM
3189 sets them to a sane value without looking at them. */
3190 if (!found
) goto done
;
3192 state
.incomment
= 0;
3193 state
.comstyle
= 0; /* reset the comment style */
3194 if (boundary_stop
) goto done
;
3198 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3200 /* curlevel++->last ran into compiler bug on Apollo */
3201 curlevel
->last
= prev_from
;
3202 if (++curlevel
== endlevel
)
3203 curlevel
--; /* error ("Nesting too deep for parser"); */
3204 curlevel
->prev
= -1;
3205 curlevel
->last
= -1;
3206 if (targetdepth
== depth
) goto done
;
3211 if (depth
< mindepth
)
3213 if (curlevel
!= levelstart
)
3215 curlevel
->prev
= curlevel
->last
;
3216 if (targetdepth
== depth
) goto done
;
3221 state
.comstr_start
= from
- 1;
3222 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3223 curlevel
->last
= prev_from
;
3224 state
.instring
= (code
== Sstring
3225 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte
))
3227 if (boundary_stop
) goto done
;
3230 nofence
= state
.instring
!= ST_STRING_STYLE
;
3236 if (from
>= end
) goto done
;
3237 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3238 /* Some compilers can't handle this inside the switch. */
3241 /* Check TEMP here so that if the char has
3242 a syntax-table property which says it is NOT
3243 a string character, it does not end the string. */
3244 if (nofence
&& c
== state
.instring
&& temp
== Sstring
)
3250 if (!nofence
) goto string_end
;
3255 startquotedinstring
:
3256 if (from
>= end
) goto endquoted
;
3262 state
.instring
= -1;
3263 curlevel
->prev
= curlevel
->last
;
3265 if (boundary_stop
) goto done
;
3269 /* FIXME: We should do something with it. */
3272 /* Ignore whitespace, punctuation, quote, endcomment. */
3278 stop
: /* Here if stopping before start of sexp. */
3279 from
= prev_from
; /* We have just fetched the char that starts it; */
3280 goto done
; /* but return the position before it. */
3285 state
.depth
= depth
;
3286 state
.mindepth
= mindepth
;
3287 state
.thislevelstart
= curlevel
->prev
;
3288 state
.prevlevelstart
3289 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
3290 state
.location
= from
;
3291 state
.levelstarts
= Qnil
;
3292 while (curlevel
> levelstart
)
3293 state
.levelstarts
= Fcons (make_number ((--curlevel
)->last
),
3300 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
3301 doc
: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3302 Parsing stops at TO or when certain criteria are met;
3303 point is set to where parsing stops.
3304 If fifth arg OLDSTATE is omitted or nil,
3305 parsing assumes that FROM is the beginning of a function.
3306 Value is a list of elements describing final state of parsing:
3308 1. character address of start of innermost containing list; nil if none.
3309 2. character address of start of last complete sexp terminated.
3310 3. non-nil if inside a string.
3311 (it is the character that will terminate the string,
3312 or t if the string should be terminated by a generic string delimiter.)
3313 4. nil if outside a comment, t if inside a non-nestable comment,
3314 else an integer (the current comment nesting).
3315 5. t if following a quote character.
3316 6. the minimum paren-depth encountered during this scan.
3317 7. style of comment, if any.
3318 8. character address of start of comment or string; nil if not in one.
3319 9. Intermediate data for continuation of parsing (subject to change).
3320 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3321 in parentheses becomes equal to TARGETDEPTH.
3322 Fourth arg STOPBEFORE non-nil means stop when come to
3323 any character that starts a sexp.
3324 Fifth arg OLDSTATE is a list like what this function returns.
3325 It is used to initialize the state of the parse. Elements number 1, 2, 6
3327 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3328 If it is symbol `syntax-table', stop after the start of a comment or a
3329 string, or after end of a comment or a string. */)
3330 (Lisp_Object from
, Lisp_Object to
, Lisp_Object targetdepth
, Lisp_Object stopbefore
, Lisp_Object oldstate
, Lisp_Object commentstop
)
3332 struct lisp_parse_state state
;
3335 if (!NILP (targetdepth
))
3337 CHECK_NUMBER (targetdepth
);
3338 target
= XINT (targetdepth
);
3341 target
= TYPE_MINIMUM (EMACS_INT
); /* We won't reach this depth */
3343 validate_region (&from
, &to
);
3344 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
3346 target
, !NILP (stopbefore
), oldstate
,
3348 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
3350 SET_PT (state
.location
);
3352 return Fcons (make_number (state
.depth
),
3353 Fcons (state
.prevlevelstart
< 0
3354 ? Qnil
: make_number (state
.prevlevelstart
),
3355 Fcons (state
.thislevelstart
< 0
3356 ? Qnil
: make_number (state
.thislevelstart
),
3357 Fcons (state
.instring
>= 0
3358 ? (state
.instring
== ST_STRING_STYLE
3359 ? Qt
: make_number (state
.instring
)) : Qnil
,
3360 Fcons (state
.incomment
< 0 ? Qt
:
3361 (state
.incomment
== 0 ? Qnil
:
3362 make_number (state
.incomment
)),
3363 Fcons (state
.quoted
? Qt
: Qnil
,
3364 Fcons (make_number (state
.mindepth
),
3365 Fcons ((state
.comstyle
3366 ? (state
.comstyle
== ST_COMMENT_STYLE
3368 : make_number (state
.comstyle
))
3370 Fcons (((state
.incomment
3371 || (state
.instring
>= 0))
3372 ? make_number (state
.comstr_start
)
3374 Fcons (state
.levelstarts
, Qnil
))))))))));
3378 init_syntax_once (void)
3383 /* This has to be done here, before we call Fmake_char_table. */
3384 DEFSYM (Qsyntax_table
, "syntax-table");
3386 /* Intern_C_String this now in case it isn't already done.
3387 Setting this variable twice is harmless.
3388 But don't staticpro it here--that is done in alloc.c. */
3389 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
3391 /* Create objects which can be shared among syntax tables. */
3392 Vsyntax_code_object
= Fmake_vector (make_number (Smax
), Qnil
);
3393 for (i
= 0; i
< ASIZE (Vsyntax_code_object
); i
++)
3394 ASET (Vsyntax_code_object
, i
, Fcons (make_number (i
), Qnil
));
3396 /* Now we are ready to set up this property, so we can
3397 create syntax tables. */
3398 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
3400 temp
= AREF (Vsyntax_code_object
, (int) Swhitespace
);
3402 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
3404 /* Control characters should not be whitespace. */
3405 temp
= AREF (Vsyntax_code_object
, (int) Spunct
);
3406 for (i
= 0; i
<= ' ' - 1; i
++)
3407 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3408 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 0177, temp
);
3410 /* Except that a few really are whitespace. */
3411 temp
= AREF (Vsyntax_code_object
, (int) Swhitespace
);
3412 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ' ', temp
);
3413 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\t', temp
);
3414 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\n', temp
);
3415 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 015, temp
);
3416 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 014, temp
);
3418 temp
= AREF (Vsyntax_code_object
, (int) Sword
);
3419 for (i
= 'a'; i
<= 'z'; i
++)
3420 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3421 for (i
= 'A'; i
<= 'Z'; i
++)
3422 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3423 for (i
= '0'; i
<= '9'; i
++)
3424 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3426 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
3427 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
3429 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
3430 Fcons (make_number (Sopen
), make_number (')')));
3431 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
3432 Fcons (make_number (Sclose
), make_number ('(')));
3433 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
3434 Fcons (make_number (Sopen
), make_number (']')));
3435 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
3436 Fcons (make_number (Sclose
), make_number ('[')));
3437 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
3438 Fcons (make_number (Sopen
), make_number ('}')));
3439 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
3440 Fcons (make_number (Sclose
), make_number ('{')));
3441 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
3442 Fcons (make_number ((int) Sstring
), Qnil
));
3443 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
3444 Fcons (make_number ((int) Sescape
), Qnil
));
3446 temp
= AREF (Vsyntax_code_object
, (int) Ssymbol
);
3447 for (i
= 0; i
< 10; i
++)
3449 c
= "_-+*/&|<>="[i
];
3450 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3453 temp
= AREF (Vsyntax_code_object
, (int) Spunct
);
3454 for (i
= 0; i
< 12; i
++)
3456 c
= ".,;:?!#@~^'`"[i
];
3457 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3460 /* All multibyte characters have syntax `word' by default. */
3461 temp
= AREF (Vsyntax_code_object
, (int) Sword
);
3462 char_table_set_range (Vstandard_syntax_table
, 0x80, MAX_CHAR
, temp
);
3466 syms_of_syntax (void)
3468 DEFSYM (Qsyntax_table_p
, "syntax-table-p");
3470 staticpro (&Vsyntax_code_object
);
3472 staticpro (&gl_state
.object
);
3473 staticpro (&gl_state
.global_code
);
3474 staticpro (&gl_state
.current_syntax_table
);
3475 staticpro (&gl_state
.old_prop
);
3477 /* Defined in regex.c */
3478 staticpro (&re_match_object
);
3480 DEFSYM (Qscan_error
, "scan-error");
3481 Fput (Qscan_error
, Qerror_conditions
,
3482 listn (CONSTYPE_PURE
, 2, Qscan_error
, Qerror
));
3483 Fput (Qscan_error
, Qerror_message
,
3484 build_pure_c_string ("Scan error"));
3486 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments
,
3487 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3489 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties
,
3490 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3491 Otherwise, that text property is simply ignored.
3492 See the info node `(elisp)Syntax Properties' for a description of the
3493 `syntax-table' property. */);
3495 words_include_escapes
= 0;
3496 DEFVAR_BOOL ("words-include-escapes", words_include_escapes
,
3497 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3499 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol
,
3500 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3501 multibyte_syntax_as_symbol
= 0;
3503 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3504 open_paren_in_column_0_is_defun_start
,
3505 doc
: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3506 open_paren_in_column_0_is_defun_start
= 1;
3509 DEFVAR_LISP ("find-word-boundary-function-table",
3510 Vfind_word_boundary_function_table
,
3512 Char table of functions to search for the word boundary.
3513 Each function is called with two arguments; POS and LIMIT.
3514 POS and LIMIT are character positions in the current buffer.
3516 If POS is less than LIMIT, POS is at the first character of a word,
3517 and the return value of a function is a position after the last
3518 character of that word.
3520 If POS is not less than LIMIT, POS is at the last character of a word,
3521 and the return value of a function is a position at the first
3522 character of that word.
3524 In both cases, LIMIT bounds the search. */);
3525 Vfind_word_boundary_function_table
= Fmake_char_table (Qnil
, Qnil
);
3527 defsubr (&Ssyntax_table_p
);
3528 defsubr (&Ssyntax_table
);
3529 defsubr (&Sstandard_syntax_table
);
3530 defsubr (&Scopy_syntax_table
);
3531 defsubr (&Sset_syntax_table
);
3532 defsubr (&Schar_syntax
);
3533 defsubr (&Smatching_paren
);
3534 defsubr (&Sstring_to_syntax
);
3535 defsubr (&Smodify_syntax_entry
);
3536 defsubr (&Sinternal_describe_syntax_value
);
3538 defsubr (&Sforward_word
);
3540 defsubr (&Sskip_chars_forward
);
3541 defsubr (&Sskip_chars_backward
);
3542 defsubr (&Sskip_syntax_forward
);
3543 defsubr (&Sskip_syntax_backward
);
3545 defsubr (&Sforward_comment
);
3546 defsubr (&Sscan_lists
);
3547 defsubr (&Sscan_sexps
);
3548 defsubr (&Sbackward_prefix_chars
);
3549 defsubr (&Sparse_partial_sexp
);