1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2013 Free
3 Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
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 location_byte
; /* Corresponding byte position. */
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 /* This setter is used only in this file, so it can be private. */
156 bset_syntax_table (struct buffer
*b
, Lisp_Object val
)
158 b
->INTERNAL_FIELD (syntax_table
) = val
;
161 /* Whether the syntax of the character C has the prefix flag set. */
162 int syntax_prefix_flag_p (int c
)
164 return SYNTAX_PREFIX (c
);
167 struct gl_state_s gl_state
; /* Global state of syntax parser. */
169 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
170 to scan to property-change. */
172 /* Update gl_state to an appropriate interval which contains CHARPOS. The
173 sign of COUNT give the relative position of CHARPOS wrt the previously
174 valid interval. If INIT, only [be]_property fields of gl_state are
175 valid at start, the rest is filled basing on OBJECT.
177 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
178 direction than the intervals - or in an interval. We update the
179 current syntax-table basing on the property of this interval, and
180 update the interval to start further than CHARPOS - or be
181 NULL. We also update lim_property to be the next value of
182 charpos to call this subroutine again - or be before/after the
183 start/end of OBJECT. */
186 update_syntax_table (ptrdiff_t charpos
, EMACS_INT count
, int init
,
189 Lisp_Object tmp_table
;
196 gl_state
.old_prop
= Qnil
;
197 gl_state
.start
= gl_state
.b_property
;
198 gl_state
.stop
= gl_state
.e_property
;
199 i
= interval_of (charpos
, object
);
200 gl_state
.backward_i
= gl_state
.forward_i
= i
;
204 /* interval_of updates only ->position of the return value, so
205 update the parents manually to speed up update_interval. */
206 while (!NULL_PARENT (i
))
208 if (AM_RIGHT_CHILD (i
))
209 INTERVAL_PARENT (i
)->position
= i
->position
210 - LEFT_TOTAL_LENGTH (i
) + TOTAL_LENGTH (i
) /* right end */
211 - TOTAL_LENGTH (INTERVAL_PARENT (i
))
212 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i
));
214 INTERVAL_PARENT (i
)->position
= i
->position
- LEFT_TOTAL_LENGTH (i
)
216 i
= INTERVAL_PARENT (i
);
218 i
= gl_state
.forward_i
;
219 gl_state
.b_property
= i
->position
- gl_state
.offset
;
220 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
223 i
= count
> 0 ? gl_state
.forward_i
: gl_state
.backward_i
;
225 /* We are guaranteed to be called with CHARPOS either in i,
228 error ("Error in syntax_table logic for to-the-end intervals");
229 else if (charpos
< i
->position
) /* Move left. */
232 error ("Error in syntax_table logic for intervals <-");
233 /* Update the interval. */
234 i
= update_interval (i
, charpos
);
235 if (INTERVAL_LAST_POS (i
) != gl_state
.b_property
)
238 gl_state
.forward_i
= i
;
239 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
242 else if (charpos
>= INTERVAL_LAST_POS (i
)) /* Move right. */
245 error ("Error in syntax_table logic for intervals ->");
246 /* Update the interval. */
247 i
= update_interval (i
, charpos
);
248 if (i
->position
!= gl_state
.e_property
)
251 gl_state
.backward_i
= i
;
252 gl_state
.b_property
= i
->position
- gl_state
.offset
;
257 tmp_table
= textget (i
->plist
, Qsyntax_table
);
260 invalidate
= !EQ (tmp_table
, gl_state
.old_prop
); /* Need to invalidate? */
262 if (invalidate
) /* Did not get to adjacent interval. */
263 { /* with the same table => */
264 /* invalidate the old range. */
267 gl_state
.backward_i
= i
;
268 gl_state
.b_property
= i
->position
- gl_state
.offset
;
272 gl_state
.forward_i
= i
;
273 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
277 if (!EQ (tmp_table
, gl_state
.old_prop
))
279 gl_state
.current_syntax_table
= tmp_table
;
280 gl_state
.old_prop
= tmp_table
;
281 if (EQ (Fsyntax_table_p (tmp_table
), Qt
))
283 gl_state
.use_global
= 0;
285 else if (CONSP (tmp_table
))
287 gl_state
.use_global
= 1;
288 gl_state
.global_code
= tmp_table
;
292 gl_state
.use_global
= 0;
293 gl_state
.current_syntax_table
= BVAR (current_buffer
, syntax_table
);
299 if (cnt
&& !EQ (tmp_table
, textget (i
->plist
, Qsyntax_table
)))
303 gl_state
.e_property
= i
->position
- gl_state
.offset
;
304 gl_state
.forward_i
= i
;
309 = i
->position
+ LENGTH (i
) - gl_state
.offset
;
310 gl_state
.backward_i
= i
;
314 else if (cnt
== INTERVALS_AT_ONCE
)
319 = i
->position
+ LENGTH (i
) - gl_state
.offset
320 /* e_property at EOB is not set to ZV but to ZV+1, so that
321 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
322 having to check eob between the two. */
323 + (next_interval (i
) ? 0 : 1);
324 gl_state
.forward_i
= i
;
328 gl_state
.b_property
= i
->position
- gl_state
.offset
;
329 gl_state
.backward_i
= i
;
334 i
= count
> 0 ? next_interval (i
) : previous_interval (i
);
336 eassert (i
== NULL
); /* This property goes to the end. */
338 gl_state
.e_property
= gl_state
.stop
;
340 gl_state
.b_property
= gl_state
.start
;
343 /* Returns TRUE if char at CHARPOS is quoted.
344 Global syntax-table data should be set up already to be good at CHARPOS
345 or after. On return global syntax data is good for lookup at CHARPOS. */
348 char_quoted (ptrdiff_t charpos
, ptrdiff_t bytepos
)
350 register enum syntaxcode code
;
351 register ptrdiff_t beg
= BEGV
;
352 register int quoted
= 0;
353 ptrdiff_t orig
= charpos
;
355 while (charpos
> beg
)
358 DEC_BOTH (charpos
, bytepos
);
360 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
);
361 c
= FETCH_CHAR_AS_MULTIBYTE (bytepos
);
363 if (! (code
== Scharquote
|| code
== Sescape
))
369 UPDATE_SYNTAX_TABLE (orig
);
373 /* Return the bytepos one character before BYTEPOS.
374 We assume that BYTEPOS is not at the start of the buffer. */
377 dec_bytepos (ptrdiff_t bytepos
)
379 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
386 /* Return a defun-start position before POS and not too far before.
387 It should be the last one before POS, or nearly the last.
389 When open_paren_in_column_0_is_defun_start is nonzero,
390 only the beginning of the buffer is treated as a defun-start.
392 We record the information about where the scan started
393 and what its result was, so that another call in the same area
394 can return the same value very quickly.
396 There is no promise at which position the global syntax data is
397 valid on return from the subroutine, so the caller should explicitly
398 update the global data. */
401 find_defun_start (ptrdiff_t pos
, ptrdiff_t pos_byte
)
403 ptrdiff_t opoint
= PT
, opoint_byte
= PT_BYTE
;
405 if (!open_paren_in_column_0_is_defun_start
)
407 find_start_value
= BEGV
;
408 find_start_value_byte
= BEGV_BYTE
;
409 find_start_buffer
= current_buffer
;
410 find_start_modiff
= MODIFF
;
411 find_start_begv
= BEGV
;
412 find_start_pos
= pos
;
416 /* Use previous finding, if it's valid and applies to this inquiry. */
417 if (current_buffer
== find_start_buffer
418 /* Reuse the defun-start even if POS is a little farther on.
419 POS might be in the next defun, but that's ok.
420 Our value may not be the best possible, but will still be usable. */
421 && pos
<= find_start_pos
+ 1000
422 && pos
>= find_start_value
423 && BEGV
== find_start_begv
424 && MODIFF
== find_start_modiff
)
425 return find_start_value
;
427 /* Back up to start of line. */
428 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
430 /* We optimize syntax-table lookup for rare updates. Thus we accept
431 only those `^\s(' which are good in global _and_ text-property
433 SETUP_BUFFER_SYNTAX_TABLE ();
438 /* Open-paren at start of line means we may have found our
440 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
441 if (SYNTAX (c
) == Sopen
)
443 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
444 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
445 if (SYNTAX (c
) == Sopen
)
447 /* Now fallback to the default value. */
448 SETUP_BUFFER_SYNTAX_TABLE ();
450 /* Move to beg of previous line. */
451 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
454 /* Record what we found, for the next try. */
455 find_start_value
= PT
;
456 find_start_value_byte
= PT_BYTE
;
457 find_start_buffer
= current_buffer
;
458 find_start_modiff
= MODIFF
;
459 find_start_begv
= BEGV
;
460 find_start_pos
= pos
;
462 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
464 return find_start_value
;
467 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
470 prev_char_comend_first (ptrdiff_t pos
, ptrdiff_t pos_byte
)
474 DEC_BOTH (pos
, pos_byte
);
475 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
476 c
= FETCH_CHAR (pos_byte
);
477 val
= SYNTAX_COMEND_FIRST (c
);
478 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
482 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
485 * prev_char_comstart_first (pos, pos_byte)
490 * DEC_BOTH (pos, pos_byte);
491 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
492 * c = FETCH_CHAR (pos_byte);
493 * val = SYNTAX_COMSTART_FIRST (c);
494 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
498 /* Checks whether charpos FROM is at the end of a comment.
499 FROM_BYTE is the bytepos corresponding to FROM.
500 Do not move back before STOP.
502 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
505 If successful, store the charpos of the comment's beginning
506 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
508 Global syntax data remains valid for backward search starting at
509 the returned value (or at FROM, if the search was not successful). */
512 back_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
, int comnested
, int comstyle
, ptrdiff_t *charpos_ptr
, ptrdiff_t *bytepos_ptr
)
514 /* Look back, counting the parity of string-quotes,
515 and recording the comment-starters seen.
516 When we reach a safe place, assume that's not in a string;
517 then step the main scan to the earliest comment-starter seen
518 an even number of string quotes away from the safe place.
520 OFROM[I] is position of the earliest comment-starter seen
521 which is I+2X quotes from the comment-end.
522 PARITY is current parity of quotes from the comment end. */
523 int string_style
= -1; /* Presumed outside of any string. */
524 int string_lossage
= 0;
525 /* Not a real lossage: indicates that we have passed a matching comment
526 starter plus a non-matching comment-ender, meaning that any matching
527 comment-starter we might see later could be a false positive (hidden
528 inside another comment).
529 Test case: { a (* b } c (* d *) */
530 int comment_lossage
= 0;
531 ptrdiff_t comment_end
= from
;
532 ptrdiff_t comment_end_byte
= from_byte
;
533 ptrdiff_t comstart_pos
= 0;
534 ptrdiff_t comstart_byte
IF_LINT (= 0);
535 /* Place where the containing defun starts,
536 or 0 if we didn't come across it yet. */
537 ptrdiff_t defun_start
= 0;
538 ptrdiff_t defun_start_byte
= 0;
539 register enum syntaxcode code
;
540 int nesting
= 1; /* current comment nesting */
544 /* FIXME: A }} comment-ender style leads to incorrect behavior
545 in the case of {{ c }}} because we ignore the last two chars which are
546 assumed to be comment-enders although they aren't. */
548 /* At beginning of range to scan, we're outside of strings;
549 that determines quote parity to the comment-end. */
553 int prev_syntax
, com2start
, com2end
;
556 /* Move back and examine a character. */
557 DEC_BOTH (from
, from_byte
);
558 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
560 prev_syntax
= syntax
;
561 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
562 syntax
= SYNTAX_WITH_FLAGS (c
);
565 /* Check for 2-char comment markers. */
566 com2start
= (SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
567 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax
)
569 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax
, syntax
))
570 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
571 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
);
572 com2end
= (SYNTAX_FLAGS_COMEND_FIRST (syntax
)
573 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax
));
574 comstart
= (com2start
|| code
== Scomment
);
576 /* Nasty cases with overlapping 2-char comment markers:
577 - snmp-mode: -- c -- foo -- c --
585 /* If a 2-char comment sequence partly overlaps with another,
586 we don't try to be clever. E.g. |*| in C, or }% in modes that
587 have %..\n and %{..}%. */
588 if (from
> stop
&& (com2end
|| comstart
))
590 ptrdiff_t next
= from
, next_byte
= from_byte
;
591 int next_c
, next_syntax
;
592 DEC_BOTH (next
, next_byte
);
593 UPDATE_SYNTAX_TABLE_BACKWARD (next
);
594 next_c
= FETCH_CHAR_AS_MULTIBYTE (next_byte
);
595 next_syntax
= SYNTAX_WITH_FLAGS (next_c
);
596 if (((comstart
|| comnested
)
597 && SYNTAX_FLAGS_COMEND_SECOND (syntax
)
598 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax
))
599 || ((com2end
|| comnested
)
600 && SYNTAX_FLAGS_COMSTART_SECOND (syntax
)
602 == SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_syntax
))
603 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax
)))
605 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
608 if (com2start
&& comstart_pos
== 0)
609 /* We're looking at a comment starter. But it might be a comment
610 ender as well (see snmp-mode). The first time we see one, we
611 need to consider it as a comment starter,
612 and the subsequent times as a comment ender. */
615 /* Turn a 2-char comment sequences into the appropriate syntax. */
620 /* Ignore comment starters of a different style. */
621 else if (code
== Scomment
622 && (comstyle
!= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0)
623 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
) != comnested
))
626 /* Ignore escaped characters, except comment-enders. */
627 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
634 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
636 /* Track parity of quotes. */
637 if (string_style
== -1)
638 /* Entering a string. */
640 else if (string_style
== c
)
641 /* Leaving the string. */
644 /* If we have two kinds of string delimiters.
645 There's no way to grok this scanning backwards. */
650 /* We've already checked that it is the relevant comstyle. */
651 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
652 /* There are odd string quotes involved, so let's be careful.
653 Test case in Pascal: " { " a { " } */
658 /* Record best comment-starter so far. */
660 comstart_byte
= from_byte
;
662 else if (--nesting
<= 0)
663 /* nested comments have to be balanced, so we don't need to
664 keep looking for earlier ones. We use here the same (slightly
665 incorrect) reasoning as below: since it is followed by uniform
666 paired string quotes, this comment-start has to be outside of
667 strings, else the comment-end itself would be inside a string. */
672 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == comstyle
673 && ((com2end
&& SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
))
674 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
675 /* This is the same style of comment ender as ours. */
680 /* Anything before that can't count because it would match
681 this comment-ender rather than ours. */
682 from
= stop
; /* Break out of the loop. */
684 else if (comstart_pos
!= 0 || c
!= '\n')
685 /* We're mixing comment styles here, so we'd better be careful.
686 The (comstart_pos != 0 || c != '\n') check is not quite correct
687 (we should just always set comment_lossage), but removing it
688 would imply that any multiline comment in C would go through
689 lossage, which seems overkill.
690 The failure should only happen in the rare cases such as
696 /* Assume a defun-start point is outside of strings. */
697 if (open_paren_in_column_0_is_defun_start
699 || (temp_byte
= dec_bytepos (from_byte
),
700 FETCH_CHAR (temp_byte
) == '\n')))
703 defun_start_byte
= from_byte
;
704 from
= stop
; /* Break out of the loop. */
713 if (comstart_pos
== 0)
716 from_byte
= comment_end_byte
;
717 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
- 1);
719 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
720 or `done'), then we've found the beginning of the non-nested comment. */
721 else if (1) /* !comnested */
724 from_byte
= comstart_byte
;
725 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
729 struct lisp_parse_state state
;
731 /* We had two kinds of string delimiters mixed up
732 together. Decode this going forwards.
733 Scan fwd from a known safe place (beginning-of-defun)
734 to the one in question; this records where we
735 last passed a comment starter. */
736 /* If we did not already find the defun start, find it now. */
737 if (defun_start
== 0)
739 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
740 defun_start_byte
= find_start_value_byte
;
744 scan_sexps_forward (&state
,
745 defun_start
, defun_start_byte
,
746 comment_end
, TYPE_MINIMUM (EMACS_INT
),
748 defun_start
= comment_end
;
749 if (state
.incomment
== (comnested
? 1 : -1)
750 && state
.comstyle
== comstyle
)
751 from
= state
.comstr_start
;
756 /* If comment_end is inside some other comment, maybe ours
757 is nested, so we need to try again from within the
758 surrounding comment. Example: { a (* " *) */
760 /* FIXME: We should advance by one or two chars. */
761 defun_start
= state
.comstr_start
+ 2;
762 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
765 } while (defun_start
< comment_end
);
767 from_byte
= CHAR_TO_BYTE (from
);
768 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
773 *bytepos_ptr
= from_byte
;
775 return (from
== comment_end
) ? -1 : from
;
778 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
779 doc
: /* Return t if OBJECT is a syntax table.
780 Currently, any char-table counts as a syntax table. */)
783 if (CHAR_TABLE_P (object
)
784 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
790 check_syntax_table (Lisp_Object obj
)
792 CHECK_TYPE (CHAR_TABLE_P (obj
) && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
),
793 Qsyntax_table_p
, obj
);
796 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
797 doc
: /* Return the current syntax table.
798 This is the one specified by the current buffer. */)
801 return BVAR (current_buffer
, syntax_table
);
804 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
805 Sstandard_syntax_table
, 0, 0, 0,
806 doc
: /* Return the standard syntax table.
807 This is the one used for new buffers. */)
810 return Vstandard_syntax_table
;
813 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
814 doc
: /* Construct a new syntax table and return it.
815 It is a copy of the TABLE, which defaults to the standard syntax table. */)
821 check_syntax_table (table
);
823 table
= Vstandard_syntax_table
;
825 copy
= Fcopy_sequence (table
);
827 /* Only the standard syntax table should have a default element.
828 Other syntax tables should inherit from parents instead. */
829 set_char_table_defalt (copy
, Qnil
);
831 /* Copied syntax tables should all have parents.
832 If we copied one with no parent, such as the standard syntax table,
833 use the standard syntax table as the copy's parent. */
834 if (NILP (XCHAR_TABLE (copy
)->parent
))
835 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
839 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
840 doc
: /* Select a new syntax table for the current buffer.
841 One argument, a syntax table. */)
845 check_syntax_table (table
);
846 bset_syntax_table (current_buffer
, table
);
847 /* Indicate that this buffer now has a specified syntax table. */
848 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
849 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
853 /* Convert a letter which signifies a syntax code
854 into the code it signifies.
855 This is used by modify-syntax-entry, and other things. */
857 unsigned char syntax_spec_code
[0400] =
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 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
862 (char) Swhitespace
, (char) Scomment_fence
, (char) Sstring
, 0377,
863 (char) Smath
, 0377, 0377, (char) Squote
,
864 (char) Sopen
, (char) Sclose
, 0377, 0377,
865 0377, (char) Swhitespace
, (char) Spunct
, (char) Scharquote
,
866 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
867 0377, 0377, 0377, 0377,
868 (char) Scomment
, 0377, (char) Sendcomment
, 0377,
869 (char) Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
870 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
871 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
872 0377, 0377, 0377, 0377, (char) Sescape
, 0377, 0377, (char) Ssymbol
,
873 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
874 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
875 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
876 0377, 0377, 0377, 0377, (char) Sstring_fence
, 0377, 0377, 0377
879 /* Indexed by syntax code, give the letter that describes it. */
881 char syntax_code_spec
[16] =
883 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
887 /* Indexed by syntax code, give the object (cons of syntax code and
888 nil) to be stored in syntax table. Since these objects can be
889 shared among syntax tables, we generate them in advance. By
890 sharing objects, the function `describe-syntax' can give a more
892 static Lisp_Object Vsyntax_code_object
;
895 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
896 doc
: /* Return the syntax code of CHARACTER, described by a character.
897 For example, if CHARACTER is a word constituent, the
898 character `w' (119) is returned.
899 The characters that correspond to various syntax codes
900 are listed in the documentation of `modify-syntax-entry'. */)
901 (Lisp_Object character
)
904 CHECK_CHARACTER (character
);
905 char_int
= XINT (character
);
906 SETUP_BUFFER_SYNTAX_TABLE ();
907 return make_number (syntax_code_spec
[(int) SYNTAX (char_int
)]);
910 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
911 doc
: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
912 (Lisp_Object character
)
915 CHECK_NUMBER (character
);
916 char_int
= XINT (character
);
917 SETUP_BUFFER_SYNTAX_TABLE ();
918 code
= SYNTAX (char_int
);
919 if (code
== Sopen
|| code
== Sclose
)
920 return SYNTAX_MATCH (char_int
);
924 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
925 doc
: /* Convert a syntax descriptor STRING into a raw syntax descriptor.
926 STRING should be a string of the form allowed as argument of
927 `modify-syntax-entry'. The return value is a raw syntax descriptor: a
928 cons cell \(CODE . MATCHING-CHAR) which can be used, for example, as
929 the value of a `syntax-table' text property. */)
932 register const unsigned char *p
;
933 register enum syntaxcode code
;
937 CHECK_STRING (string
);
940 code
= (enum syntaxcode
) syntax_spec_code
[*p
++];
941 if (((int) code
& 0377) == 0377)
942 error ("Invalid syntax description letter: %c", p
[-1]);
944 if (code
== Sinherit
)
950 int character
= STRING_CHAR_AND_LENGTH (p
, len
);
951 XSETINT (match
, character
);
952 if (XFASTINT (match
) == ' ')
996 if (val
< ASIZE (Vsyntax_code_object
) && NILP (match
))
997 return AREF (Vsyntax_code_object
, val
);
999 /* Since we can't use a shared object, let's make a new one. */
1000 return Fcons (make_number (val
), match
);
1003 /* I really don't know why this is interactive
1004 help-form should at least be made useful whilst reading the second arg. */
1005 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
1006 "cSet syntax for character: \nsSet syntax for %s to: ",
1007 doc
: /* Set syntax for character CHAR according to string NEWENTRY.
1008 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1009 the current buffer's syntax table.
1010 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1011 in the range MIN to MAX are changed.
1012 The first character of NEWENTRY should be one of the following:
1013 Space or - whitespace syntax. w word constituent.
1014 _ symbol constituent. . punctuation.
1015 ( open-parenthesis. ) close-parenthesis.
1016 " string quote. \\ escape.
1017 $ paired delimiter. ' expression quote or prefix operator.
1018 < comment starter. > comment ender.
1019 / character-quote. @ inherit from parent table.
1020 | generic string fence. ! generic comment fence.
1022 Only single-character comment start and end sequences are represented thus.
1023 Two-character sequences are represented as described below.
1024 The second character of NEWENTRY is the matching parenthesis,
1025 used only if the first character is `(' or `)'.
1026 Any additional characters are flags.
1027 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1028 1 means CHAR is the start of a two-char comment start sequence.
1029 2 means CHAR is the second character of such a sequence.
1030 3 means CHAR is the start of a two-char comment end sequence.
1031 4 means CHAR is the second character of such a sequence.
1033 There can be several orthogonal comment sequences. This is to support
1034 language modes such as C++. By default, all comment sequences are of style
1035 a, but you can set the comment sequence style to b (on the second character
1036 of a comment-start, and the first character of a comment-end sequence) and/or
1037 c (on any of its chars) using this flag:
1038 b means CHAR is part of comment sequence b.
1039 c means CHAR is part of comment sequence c.
1040 n means CHAR is part of a nestable comment sequence.
1042 p means CHAR is a prefix character for `backward-prefix-chars';
1043 such characters are treated as whitespace when they occur
1044 between expressions.
1045 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1046 (Lisp_Object c
, Lisp_Object newentry
, Lisp_Object syntax_table
)
1050 CHECK_CHARACTER_CAR (c
);
1051 CHECK_CHARACTER_CDR (c
);
1054 CHECK_CHARACTER (c
);
1056 if (NILP (syntax_table
))
1057 syntax_table
= BVAR (current_buffer
, syntax_table
);
1059 check_syntax_table (syntax_table
);
1061 newentry
= Fstring_to_syntax (newentry
);
1063 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table
, c
, newentry
);
1065 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), newentry
);
1067 /* We clear the regexp cache, since character classes can now have
1068 different values from those in the compiled regexps.*/
1069 clear_regexp_cache ();
1074 /* Dump syntax table to buffer in human-readable format */
1076 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value
,
1077 Sinternal_describe_syntax_value
, 1, 1, 0,
1078 doc
: /* Insert a description of the internal syntax description SYNTAX at point. */)
1079 (Lisp_Object syntax
)
1081 register enum syntaxcode code
;
1083 char desc
, start1
, start2
, end1
, end2
, prefix
,
1084 comstyleb
, comstylec
, comnested
;
1086 Lisp_Object first
, match_lisp
, value
= syntax
;
1090 insert_string ("default");
1094 if (CHAR_TABLE_P (value
))
1096 insert_string ("deeper char-table ...");
1102 insert_string ("invalid");
1106 first
= XCAR (value
);
1107 match_lisp
= XCDR (value
);
1109 if (!INTEGERP (first
) || !(NILP (match_lisp
) || CHARACTERP (match_lisp
)))
1111 insert_string ("invalid");
1115 syntax_code
= XINT (first
) & INT_MAX
;
1116 code
= (enum syntaxcode
) (syntax_code
& 0377);
1117 start1
= SYNTAX_FLAGS_COMSTART_FIRST (syntax_code
);
1118 start2
= SYNTAX_FLAGS_COMSTART_SECOND (syntax_code
);;
1119 end1
= SYNTAX_FLAGS_COMEND_FIRST (syntax_code
);
1120 end2
= SYNTAX_FLAGS_COMEND_SECOND (syntax_code
);
1121 prefix
= SYNTAX_FLAGS_PREFIX (syntax_code
);
1122 comstyleb
= SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code
);
1123 comstylec
= SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code
);
1124 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax_code
);
1126 if ((int) code
< 0 || (int) code
>= (int) Smax
)
1128 insert_string ("invalid");
1131 desc
= syntax_code_spec
[(int) code
];
1133 str
[0] = desc
, str
[1] = 0;
1136 if (NILP (match_lisp
))
1139 insert_char (XINT (match_lisp
));
1160 insert_string ("\twhich means: ");
1165 insert_string ("whitespace"); break;
1167 insert_string ("punctuation"); break;
1169 insert_string ("word"); break;
1171 insert_string ("symbol"); break;
1173 insert_string ("open"); break;
1175 insert_string ("close"); break;
1177 insert_string ("prefix"); break;
1179 insert_string ("string"); break;
1181 insert_string ("math"); break;
1183 insert_string ("escape"); break;
1185 insert_string ("charquote"); break;
1187 insert_string ("comment"); break;
1189 insert_string ("endcomment"); break;
1191 insert_string ("inherit"); break;
1192 case Scomment_fence
:
1193 insert_string ("comment fence"); break;
1195 insert_string ("string fence"); break;
1197 insert_string ("invalid");
1201 if (!NILP (match_lisp
))
1203 insert_string (", matches ");
1204 insert_char (XINT (match_lisp
));
1208 insert_string (",\n\t is the first character of a comment-start sequence");
1210 insert_string (",\n\t is the second character of a comment-start sequence");
1213 insert_string (",\n\t is the first character of a comment-end sequence");
1215 insert_string (",\n\t is the second character of a comment-end sequence");
1217 insert_string (" (comment style b)");
1219 insert_string (" (comment style c)");
1221 insert_string (" (nestable)");
1224 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1229 /* Return the position across COUNT words from FROM.
1230 If that many words cannot be found before the end of the buffer, return 0.
1231 COUNT negative means scan backward and stop at word beginning. */
1234 scan_words (register ptrdiff_t from
, register EMACS_INT count
)
1236 register ptrdiff_t beg
= BEGV
;
1237 register ptrdiff_t end
= ZV
;
1238 register ptrdiff_t from_byte
= CHAR_TO_BYTE (from
);
1239 register enum syntaxcode code
;
1241 Lisp_Object func
, pos
;
1246 SETUP_SYNTAX_TABLE (from
, count
);
1257 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1258 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1259 code
= SYNTAX (ch0
);
1260 INC_BOTH (from
, from_byte
);
1261 if (words_include_escapes
1262 && (code
== Sescape
|| code
== Scharquote
))
1267 /* Now CH0 is a character which begins a word and FROM is the
1268 position of the next character. */
1269 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch0
);
1270 if (! NILP (Ffboundp (func
)))
1272 pos
= call2 (func
, make_number (from
- 1), make_number (end
));
1273 if (INTEGERP (pos
) && from
< XINT (pos
) && XINT (pos
) <= ZV
)
1276 from_byte
= CHAR_TO_BYTE (from
);
1283 if (from
== end
) break;
1284 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1285 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1286 code
= SYNTAX (ch1
);
1288 && (! words_include_escapes
1289 || (code
!= Sescape
&& code
!= Scharquote
)))
1290 || word_boundary_p (ch0
, ch1
))
1292 INC_BOTH (from
, from_byte
);
1307 DEC_BOTH (from
, from_byte
);
1308 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1309 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1310 code
= SYNTAX (ch1
);
1311 if (words_include_escapes
1312 && (code
== Sescape
|| code
== Scharquote
))
1317 /* Now CH1 is a character which ends a word and FROM is the
1319 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch1
);
1320 if (! NILP (Ffboundp (func
)))
1322 pos
= call2 (func
, make_number (from
), make_number (beg
));
1323 if (INTEGERP (pos
) && BEGV
<= XINT (pos
) && XINT (pos
) < from
)
1326 from_byte
= CHAR_TO_BYTE (from
);
1335 DEC_BOTH (from
, from_byte
);
1336 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1337 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1338 code
= SYNTAX (ch0
);
1340 && (! words_include_escapes
1341 || (code
!= Sescape
&& code
!= Scharquote
)))
1342 || word_boundary_p (ch0
, ch1
))
1344 INC_BOTH (from
, from_byte
);
1358 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 0, 1, "^p",
1359 doc
: /* Move point forward ARG words (backward if ARG is negative).
1361 If an edge of the buffer or a field boundary is reached, point is left there
1362 and the function returns nil. Field boundaries are not noticed if
1363 `inhibit-field-text-motion' is non-nil. */)
1367 ptrdiff_t orig_val
, val
;
1370 XSETFASTINT (arg
, 1);
1374 val
= orig_val
= scan_words (PT
, XINT (arg
));
1376 val
= XINT (arg
) > 0 ? ZV
: BEGV
;
1378 /* Avoid jumping out of an input field. */
1379 tmp
= Fconstrain_to_field (make_number (val
), make_number (PT
),
1381 val
= XFASTINT (tmp
);
1384 return val
== orig_val
? Qt
: Qnil
;
1387 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1388 doc
: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1389 STRING is like the inside of a `[...]' in a regular expression
1390 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1391 (but not at the end of a range; quoting is never needed there).
1392 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1393 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1394 Char classes, e.g. `[:alpha:]', are supported.
1396 Returns the distance traveled, either zero or positive. */)
1397 (Lisp_Object string
, Lisp_Object lim
)
1399 return skip_chars (1, string
, lim
, 1);
1402 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1403 doc
: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1404 See `skip-chars-forward' for details.
1405 Returns the distance traveled, either zero or negative. */)
1406 (Lisp_Object string
, Lisp_Object lim
)
1408 return skip_chars (0, string
, lim
, 1);
1411 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1412 doc
: /* Move point forward across chars in specified syntax classes.
1413 SYNTAX is a string of syntax code characters.
1414 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1415 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1416 This function returns the distance traveled, either zero or positive. */)
1417 (Lisp_Object syntax
, Lisp_Object lim
)
1419 return skip_syntaxes (1, syntax
, lim
);
1422 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1423 doc
: /* Move point backward across chars in specified syntax classes.
1424 SYNTAX is a string of syntax code characters.
1425 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1426 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1427 This function returns the distance traveled, either zero or negative. */)
1428 (Lisp_Object syntax
, Lisp_Object lim
)
1430 return skip_syntaxes (0, syntax
, lim
);
1434 skip_chars (int forwardp
, Lisp_Object string
, Lisp_Object lim
, int handle_iso_classes
)
1436 register unsigned int c
;
1437 unsigned char fastmap
[0400];
1438 /* Store the ranges of non-ASCII characters. */
1439 int *char_ranges
IF_LINT (= NULL
);
1440 int n_char_ranges
= 0;
1442 register ptrdiff_t i
, i_byte
;
1443 /* Set to 1 if the current buffer is multibyte and the region
1444 contains non-ASCII chars. */
1446 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1448 int string_multibyte
;
1449 ptrdiff_t size_byte
;
1450 const unsigned char *str
;
1452 Lisp_Object iso_classes
;
1454 CHECK_STRING (string
);
1458 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1460 CHECK_NUMBER_COERCE_MARKER (lim
);
1462 /* In any case, don't allow scan outside bounds of buffer. */
1463 if (XINT (lim
) > ZV
)
1464 XSETFASTINT (lim
, ZV
);
1465 if (XINT (lim
) < BEGV
)
1466 XSETFASTINT (lim
, BEGV
);
1468 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1469 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1470 string_multibyte
= SBYTES (string
) > SCHARS (string
);
1472 memset (fastmap
, 0, sizeof fastmap
);
1474 str
= SDATA (string
);
1475 size_byte
= SBYTES (string
);
1478 if (i_byte
< size_byte
1479 && SREF (string
, 0) == '^')
1481 negate
= 1; i_byte
++;
1484 /* Find the characters specified and set their elements of fastmap.
1485 Handle backslashes and ranges specially.
1487 If STRING contains non-ASCII characters, setup char_ranges for
1488 them and use fastmap only for their leading codes. */
1490 if (! string_multibyte
)
1492 int string_has_eight_bit
= 0;
1494 /* At first setup fastmap. */
1495 while (i_byte
< size_byte
)
1499 if (handle_iso_classes
&& c
== '['
1500 && i_byte
< size_byte
1501 && str
[i_byte
] == ':')
1503 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1504 const unsigned char *class_end
= class_beg
;
1505 const unsigned char *class_limit
= str
+ size_byte
- 2;
1506 /* Leave room for the null. */
1507 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1510 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1511 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1513 while (class_end
< class_limit
1514 && *class_end
>= 'a' && *class_end
<= 'z')
1517 if (class_end
== class_beg
1518 || *class_end
!= ':' || class_end
[1] != ']')
1519 goto not_a_class_name
;
1521 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1522 class_name
[class_end
- class_beg
] = 0;
1524 cc
= re_wctype (class_name
);
1526 error ("Invalid ISO C character class");
1528 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1530 i_byte
= class_end
+ 2 - str
;
1537 if (i_byte
== size_byte
)
1542 /* Treat `-' as range character only if another character
1544 if (i_byte
+ 1 < size_byte
1545 && str
[i_byte
] == '-')
1549 /* Skip over the dash. */
1552 /* Get the end of the range. */
1555 && i_byte
< size_byte
)
1560 unsigned lim2
= c2
+ 1;
1563 if (! ASCII_CHAR_P (c2
))
1564 string_has_eight_bit
= 1;
1570 if (! ASCII_CHAR_P (c
))
1571 string_has_eight_bit
= 1;
1575 /* If the current range is multibyte and STRING contains
1576 eight-bit chars, arrange fastmap and setup char_ranges for
1577 the corresponding multibyte chars. */
1578 if (multibyte
&& string_has_eight_bit
)
1581 char himap
[0200 + 1];
1582 memcpy (himap
, fastmap
+ 0200, 0200);
1584 memset (fastmap
+ 0200, 0, 0200);
1585 char_ranges
= alloca (sizeof *char_ranges
* 128 * 2);
1588 while ((p1
= memchr (himap
+ i
, 1, 0200 - i
)))
1590 /* Deduce the next range C..C2 from the next clump of 1s
1591 in HIMAP starting with &HIMAP[I]. HIMAP is the high
1592 order half of the old FASTMAP. */
1593 int c2
, leading_code
;
1595 c
= BYTE8_TO_CHAR (i
+ 0200);
1597 c2
= BYTE8_TO_CHAR (i
+ 0200 - 1);
1599 char_ranges
[n_char_ranges
++] = c
;
1600 char_ranges
[n_char_ranges
++] = c2
;
1601 leading_code
= CHAR_LEADING_CODE (c
);
1602 memset (fastmap
+ leading_code
, 1,
1603 CHAR_LEADING_CODE (c2
) - leading_code
+ 1);
1607 else /* STRING is multibyte */
1609 char_ranges
= alloca (sizeof *char_ranges
* SCHARS (string
) * 2);
1611 while (i_byte
< size_byte
)
1613 unsigned char leading_code
;
1615 leading_code
= str
[i_byte
];
1616 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1619 if (handle_iso_classes
&& c
== '['
1620 && i_byte
< size_byte
1621 && STRING_CHAR (str
+ i_byte
) == ':')
1623 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1624 const unsigned char *class_end
= class_beg
;
1625 const unsigned char *class_limit
= str
+ size_byte
- 2;
1626 /* Leave room for the null. */
1627 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1630 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1631 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1633 while (class_end
< class_limit
1634 && *class_end
>= 'a' && *class_end
<= 'z')
1637 if (class_end
== class_beg
1638 || *class_end
!= ':' || class_end
[1] != ']')
1639 goto not_a_class_name_multibyte
;
1641 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1642 class_name
[class_end
- class_beg
] = 0;
1644 cc
= re_wctype (class_name
);
1646 error ("Invalid ISO C character class");
1648 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1650 i_byte
= class_end
+ 2 - str
;
1654 not_a_class_name_multibyte
:
1657 if (i_byte
== size_byte
)
1660 leading_code
= str
[i_byte
];
1661 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1664 /* Treat `-' as range character only if another character
1666 if (i_byte
+ 1 < size_byte
1667 && str
[i_byte
] == '-')
1670 unsigned char leading_code2
;
1672 /* Skip over the dash. */
1675 /* Get the end of the range. */
1676 leading_code2
= str
[i_byte
];
1677 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1681 && i_byte
< size_byte
)
1683 leading_code2
= str
[i_byte
];
1684 c2
=STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1690 if (ASCII_CHAR_P (c
))
1692 while (c
<= c2
&& c
< 0x80)
1694 leading_code
= CHAR_LEADING_CODE (c
);
1696 if (! ASCII_CHAR_P (c
))
1698 unsigned lim2
= leading_code2
+ 1;
1699 while (leading_code
< lim2
)
1700 fastmap
[leading_code
++] = 1;
1703 char_ranges
[n_char_ranges
++] = c
;
1704 char_ranges
[n_char_ranges
++] = c2
;
1710 if (ASCII_CHAR_P (c
))
1714 fastmap
[leading_code
] = 1;
1715 char_ranges
[n_char_ranges
++] = c
;
1716 char_ranges
[n_char_ranges
++] = c
;
1721 /* If the current range is unibyte and STRING contains non-ASCII
1722 chars, arrange fastmap for the corresponding unibyte
1725 if (! multibyte
&& n_char_ranges
> 0)
1727 memset (fastmap
+ 0200, 0, 0200);
1728 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1730 int c1
= char_ranges
[i
];
1731 unsigned lim2
= char_ranges
[i
+ 1] + 1;
1733 for (; c1
< lim2
; c1
++)
1735 int b
= CHAR_TO_BYTE_SAFE (c1
);
1743 /* If ^ was the first character, complement the fastmap. */
1747 for (i
= 0; i
< sizeof fastmap
; i
++)
1751 for (i
= 0; i
< 0200; i
++)
1753 /* All non-ASCII chars possibly match. */
1754 for (; i
< sizeof fastmap
; i
++)
1760 ptrdiff_t start_point
= PT
;
1762 ptrdiff_t pos_byte
= PT_BYTE
;
1763 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1767 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1768 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1772 endp
= CHAR_POS_ADDR (XINT (lim
));
1773 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1777 /* This code may look up syntax tables using macros that rely on the
1778 gl_state object. To make sure this object is not out of date,
1779 let's initialize it manually.
1780 We ignore syntax-table text-properties for now, since that's
1781 what we've done in the past. */
1782 SETUP_BUFFER_SYNTAX_TABLE ();
1797 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
1798 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1808 if (! ASCII_CHAR_P (c
))
1810 /* As we are looking at a multibyte character, we
1811 must look up the character in the table
1812 CHAR_RANGES. If there's no data in the table,
1813 that character is not what we want to skip. */
1815 /* The following code do the right thing even if
1816 n_char_ranges is zero (i.e. no data in
1818 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1819 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1821 if (!(negate
^ (i
< n_char_ranges
)))
1825 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
1838 if (!NILP (iso_classes
) && in_classes (*p
, iso_classes
))
1843 goto fwd_unibyte_ok
;
1849 p
++, pos
++, pos_byte
++;
1857 unsigned char *prev_p
;
1867 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
1868 c
= STRING_CHAR (p
);
1870 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1880 if (! ASCII_CHAR_P (c
))
1882 /* See the comment in the previous similar code. */
1883 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1884 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1886 if (!(negate
^ (i
< n_char_ranges
)))
1890 pos
--, pos_byte
-= prev_p
- p
;
1903 if (! NILP (iso_classes
) && in_classes (p
[-1], iso_classes
))
1908 goto back_unibyte_ok
;
1911 if (!fastmap
[p
[-1]])
1914 p
--, pos
--, pos_byte
--;
1918 SET_PT_BOTH (pos
, pos_byte
);
1921 return make_number (PT
- start_point
);
1927 skip_syntaxes (int forwardp
, Lisp_Object string
, Lisp_Object lim
)
1929 register unsigned int c
;
1930 unsigned char fastmap
[0400];
1932 register ptrdiff_t i
, i_byte
;
1934 ptrdiff_t size_byte
;
1937 CHECK_STRING (string
);
1940 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1942 CHECK_NUMBER_COERCE_MARKER (lim
);
1944 /* In any case, don't allow scan outside bounds of buffer. */
1945 if (XINT (lim
) > ZV
)
1946 XSETFASTINT (lim
, ZV
);
1947 if (XINT (lim
) < BEGV
)
1948 XSETFASTINT (lim
, BEGV
);
1950 if (forwardp
? (PT
>= XFASTINT (lim
)) : (PT
<= XFASTINT (lim
)))
1951 return make_number (0);
1953 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1954 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1956 memset (fastmap
, 0, sizeof fastmap
);
1958 if (SBYTES (string
) > SCHARS (string
))
1959 /* As this is very rare case (syntax spec is ASCII only), don't
1960 consider efficiency. */
1961 string
= string_make_unibyte (string
);
1963 str
= SDATA (string
);
1964 size_byte
= SBYTES (string
);
1967 if (i_byte
< size_byte
1968 && SREF (string
, 0) == '^')
1970 negate
= 1; i_byte
++;
1973 /* Find the syntaxes specified and set their elements of fastmap. */
1975 while (i_byte
< size_byte
)
1978 fastmap
[syntax_spec_code
[c
]] = 1;
1981 /* If ^ was the first character, complement the fastmap. */
1983 for (i
= 0; i
< sizeof fastmap
; i
++)
1987 ptrdiff_t start_point
= PT
;
1989 ptrdiff_t pos_byte
= PT_BYTE
;
1990 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1994 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1995 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1999 endp
= CHAR_POS_ADDR (XINT (lim
));
2000 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
2004 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
2020 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
2021 if (! fastmap
[(int) SYNTAX (c
)])
2023 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2024 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2038 if (! fastmap
[(int) SYNTAX (*p
)])
2040 p
++, pos
++, pos_byte
++;
2041 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2051 unsigned char *prev_p
;
2060 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2062 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2063 c
= STRING_CHAR (p
);
2064 if (! fastmap
[(int) SYNTAX (c
)])
2066 pos
--, pos_byte
-= prev_p
- p
;
2080 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2081 if (! fastmap
[(int) SYNTAX (p
[-1])])
2083 p
--, pos
--, pos_byte
--;
2088 SET_PT_BOTH (pos
, pos_byte
);
2091 return make_number (PT
- start_point
);
2095 /* Return 1 if character C belongs to one of the ISO classes
2096 in the list ISO_CLASSES. Each class is represented by an
2097 integer which is its type according to re_wctype. */
2100 in_classes (int c
, Lisp_Object iso_classes
)
2104 while (CONSP (iso_classes
))
2107 elt
= XCAR (iso_classes
);
2108 iso_classes
= XCDR (iso_classes
);
2110 if (re_iswctype (c
, XFASTINT (elt
)))
2117 /* Jump over a comment, assuming we are at the beginning of one.
2118 FROM is the current position.
2119 FROM_BYTE is the bytepos corresponding to FROM.
2120 Do not move past STOP (a charpos).
2121 The comment over which we have to jump is of style STYLE
2122 (either SYNTAX_FLAGS_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2123 NESTING should be positive to indicate the nesting at the beginning
2124 for nested comments and should be zero or negative else.
2125 ST_COMMENT_STYLE cannot be nested.
2126 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2127 (or 0 If the search cannot start in the middle of a two-character).
2129 If successful, return 1 and store the charpos of the comment's end
2130 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2131 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2132 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2133 (as defined for state.incomment) in *INCOMMENT_PTR.
2135 The comment end is the last character of the comment rather than the
2136 character just after the comment.
2138 Global syntax data is assumed to initially be valid for FROM and
2139 remains valid for forward search starting at the returned position. */
2142 forw_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
,
2143 EMACS_INT nesting
, int style
, int prev_syntax
,
2144 ptrdiff_t *charpos_ptr
, ptrdiff_t *bytepos_ptr
,
2145 EMACS_INT
*incomment_ptr
)
2148 register enum syntaxcode code
;
2149 register int syntax
, other_syntax
;
2151 if (nesting
<= 0) nesting
= -1;
2153 /* Enter the loop in the middle so that we find
2154 a 2-char comment ender if we start in the middle of it. */
2155 syntax
= prev_syntax
;
2156 if (syntax
!= 0) goto forw_incomment
;
2162 *incomment_ptr
= nesting
;
2163 *charpos_ptr
= from
;
2164 *bytepos_ptr
= from_byte
;
2167 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2168 syntax
= SYNTAX_WITH_FLAGS (c
);
2169 code
= syntax
& 0xff;
2170 if (code
== Sendcomment
2171 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
2172 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
2173 (nesting
> 0 && --nesting
== 0) : nesting
< 0))
2174 /* we have encountered a comment end of the same style
2175 as the comment sequence which began this comment
2178 if (code
== Scomment_fence
2179 && style
== ST_COMMENT_STYLE
)
2180 /* we have encountered a comment end of the same style
2181 as the comment sequence which began this comment
2186 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
2187 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
)
2188 /* we have encountered a nested comment of the same style
2189 as the comment sequence which began this comment section */
2191 INC_BOTH (from
, from_byte
);
2192 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2195 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2196 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2197 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2198 SYNTAX_FLAGS_COMEND_SECOND (other_syntax
))
2199 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, other_syntax
) == style
2200 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2201 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
))
2202 ? nesting
> 0 : nesting
< 0))
2205 /* we have encountered a comment end of the same style
2206 as the comment sequence which began this comment
2211 INC_BOTH (from
, from_byte
);
2212 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2217 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
2218 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2219 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2220 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
) == style
2221 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2222 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2223 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
)))
2224 /* we have encountered a nested comment of the same style
2225 as the comment sequence which began this comment
2228 INC_BOTH (from
, from_byte
);
2229 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2233 *charpos_ptr
= from
;
2234 *bytepos_ptr
= from_byte
;
2238 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
2240 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2241 Stop scanning if we find something other than a comment or whitespace.
2242 Set point to where scanning stops.
2243 If COUNT comments are found as expected, with nothing except whitespace
2244 between them, return t; otherwise return nil. */)
2247 register ptrdiff_t from
;
2248 ptrdiff_t from_byte
;
2249 register ptrdiff_t stop
;
2251 register enum syntaxcode code
;
2252 int comstyle
= 0; /* style of comment encountered */
2253 int comnested
= 0; /* whether the comment is nestable or not */
2256 ptrdiff_t out_charpos
, out_bytepos
;
2259 CHECK_NUMBER (count
);
2260 count1
= XINT (count
);
2261 stop
= count1
> 0 ? ZV
: BEGV
;
2267 from_byte
= PT_BYTE
;
2269 SETUP_SYNTAX_TABLE (from
, count1
);
2274 int comstart_first
, syntax
, other_syntax
;
2278 SET_PT_BOTH (from
, from_byte
);
2282 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2283 syntax
= SYNTAX_WITH_FLAGS (c
);
2285 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2286 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2287 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2288 INC_BOTH (from
, from_byte
);
2289 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2290 if (from
< stop
&& comstart_first
2291 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2292 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2293 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
)))
2295 /* We have encountered a comment start sequence and we
2296 are ignoring all text inside comments. We must record
2297 the comment style this sequence begins so that later,
2298 only a comment end of the same style actually ends
2299 the comment section. */
2301 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2303 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2304 INC_BOTH (from
, from_byte
);
2305 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2308 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
2310 if (code
== Scomment_fence
)
2311 comstyle
= ST_COMMENT_STYLE
;
2312 else if (code
!= Scomment
)
2315 DEC_BOTH (from
, from_byte
);
2316 SET_PT_BOTH (from
, from_byte
);
2319 /* We're at the start of a comment. */
2320 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
2321 &out_charpos
, &out_bytepos
, &dummy
);
2322 from
= out_charpos
; from_byte
= out_bytepos
;
2326 SET_PT_BOTH (from
, from_byte
);
2329 INC_BOTH (from
, from_byte
);
2330 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2331 /* We have skipped one comment. */
2343 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
2348 DEC_BOTH (from
, from_byte
);
2349 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2350 quoted
= char_quoted (from
, from_byte
);
2351 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2352 syntax
= SYNTAX_WITH_FLAGS (c
);
2355 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2356 if (code
== Sendcomment
)
2357 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2358 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2359 && prev_char_comend_first (from
, from_byte
)
2360 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
2363 /* We must record the comment style encountered so that
2364 later, we can match only the proper comment begin
2365 sequence of the same style. */
2366 DEC_BOTH (from
, from_byte
);
2368 /* Calling char_quoted, above, set up global syntax position
2369 at the new value of FROM. */
2370 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2371 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2372 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2374 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2377 if (code
== Scomment_fence
)
2379 /* Skip until first preceding unquoted comment_fence. */
2380 int fence_found
= 0;
2381 ptrdiff_t ini
= from
, ini_byte
= from_byte
;
2385 DEC_BOTH (from
, from_byte
);
2386 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2387 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2388 if (SYNTAX (c
) == Scomment_fence
2389 && !char_quoted (from
, from_byte
))
2394 else if (from
== stop
)
2397 if (fence_found
== 0)
2399 from
= ini
; /* Set point to ini + 1. */
2400 from_byte
= ini_byte
;
2404 /* We have skipped one comment. */
2407 else if (code
== Sendcomment
)
2409 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2410 &out_charpos
, &out_bytepos
);
2414 /* This end-of-line is not an end-of-comment.
2415 Treat it like a whitespace.
2416 CC-mode (and maybe others) relies on this behavior. */
2420 /* Failure: we should go back to the end of this
2421 not-quite-endcomment. */
2422 if (SYNTAX (c
) != code
)
2423 /* It was a two-char Sendcomment. */
2424 INC_BOTH (from
, from_byte
);
2430 /* We have skipped one comment. */
2431 from
= out_charpos
, from_byte
= out_bytepos
;
2435 else if (code
!= Swhitespace
|| quoted
)
2439 INC_BOTH (from
, from_byte
);
2440 SET_PT_BOTH (from
, from_byte
);
2448 SET_PT_BOTH (from
, from_byte
);
2453 /* Return syntax code of character C if C is an ASCII character
2454 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2456 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2457 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2458 ? SYNTAX (c) : Ssymbol)
2461 scan_lists (register EMACS_INT from
, EMACS_INT count
, EMACS_INT depth
, int sexpflag
)
2464 register ptrdiff_t stop
= count
> 0 ? ZV
: BEGV
;
2469 register enum syntaxcode code
, temp_code
;
2470 EMACS_INT min_depth
= depth
; /* Err out if depth gets less than this. */
2471 int comstyle
= 0; /* style of comment encountered */
2472 int comnested
= 0; /* whether the comment is nestable or not */
2474 EMACS_INT last_good
= from
;
2476 ptrdiff_t from_byte
;
2477 ptrdiff_t out_bytepos
, out_charpos
;
2480 int multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2482 if (depth
> 0) min_depth
= 0;
2484 if (from
> ZV
) from
= ZV
;
2485 if (from
< BEGV
) from
= BEGV
;
2487 from_byte
= CHAR_TO_BYTE (from
);
2492 SETUP_SYNTAX_TABLE (from
, count
);
2497 int comstart_first
, prefix
, syntax
, other_syntax
;
2498 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2499 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2500 syntax
= SYNTAX_WITH_FLAGS (c
);
2501 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2502 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2503 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2504 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2505 prefix
= SYNTAX_FLAGS_PREFIX (syntax
);
2506 if (depth
== min_depth
)
2508 INC_BOTH (from
, from_byte
);
2509 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2510 if (from
< stop
&& comstart_first
2511 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2512 other_syntax
= SYNTAX_WITH_FLAGS (c
),
2513 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2514 && parse_sexp_ignore_comments
)
2516 /* we have encountered a comment start sequence and we
2517 are ignoring all text inside comments. We must record
2518 the comment style this sequence begins so that later,
2519 only a comment end of the same style actually ends
2520 the comment section */
2522 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2524 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2525 INC_BOTH (from
, from_byte
);
2526 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2538 INC_BOTH (from
, from_byte
);
2539 /* treat following character as a word constituent */
2542 if (depth
|| !sexpflag
) break;
2543 /* This word counts as a sexp; return at end of it. */
2546 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2548 /* Some compilers can't handle this inside the switch. */
2549 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2550 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2555 INC_BOTH (from
, from_byte
);
2566 INC_BOTH (from
, from_byte
);
2570 case Scomment_fence
:
2571 comstyle
= ST_COMMENT_STYLE
;
2574 if (!parse_sexp_ignore_comments
) break;
2575 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2576 found
= forw_comment (from
, from_byte
, stop
,
2577 comnested
, comstyle
, 0,
2578 &out_charpos
, &out_bytepos
, &dummy
);
2579 from
= out_charpos
, from_byte
= out_bytepos
;
2586 INC_BOTH (from
, from_byte
);
2587 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2593 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (from_byte
))
2595 INC_BOTH (from
, from_byte
);
2605 if (!++depth
) goto done
;
2610 if (!--depth
) goto done
;
2611 if (depth
< min_depth
)
2612 xsignal3 (Qscan_error
,
2613 build_string ("Containing expression ends prematurely"),
2614 make_number (last_good
), make_number (from
));
2619 temp_pos
= dec_bytepos (from_byte
);
2620 stringterm
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2625 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2626 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2629 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2630 : SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring_fence
)
2633 /* Some compilers can't handle this inside the switch. */
2634 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2639 INC_BOTH (from
, from_byte
);
2641 INC_BOTH (from
, from_byte
);
2643 INC_BOTH (from
, from_byte
);
2644 if (!depth
&& sexpflag
) goto done
;
2647 /* Ignore whitespace, punctuation, quote, endcomment. */
2652 /* Reached end of buffer. Error if within object, return nil if between */
2659 /* End of object reached */
2669 DEC_BOTH (from
, from_byte
);
2670 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2671 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2672 syntax
= SYNTAX_WITH_FLAGS (c
);
2673 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2674 if (depth
== min_depth
)
2677 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2678 if (code
== Sendcomment
)
2679 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2680 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2681 && prev_char_comend_first (from
, from_byte
)
2682 && parse_sexp_ignore_comments
)
2684 /* We must record the comment style encountered so that
2685 later, we can match only the proper comment begin
2686 sequence of the same style. */
2687 int c2
, other_syntax
;
2688 DEC_BOTH (from
, from_byte
);
2689 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2691 c2
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2692 other_syntax
= SYNTAX_WITH_FLAGS (c2
);
2693 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2695 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2698 /* Quoting turns anything except a comment-ender
2699 into a word character. Note that this cannot be true
2700 if we decremented FROM in the if-statement above. */
2701 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2703 DEC_BOTH (from
, from_byte
);
2706 else if (SYNTAX_FLAGS_PREFIX (syntax
))
2715 if (depth
|| !sexpflag
) break;
2716 /* This word counts as a sexp; count object finished
2717 after passing it. */
2720 temp_pos
= from_byte
;
2721 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
2725 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2726 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2727 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2728 /* Don't allow comment-end to be quoted. */
2729 if (temp_code
== Sendcomment
)
2731 quoted
= char_quoted (from
- 1, temp_pos
);
2734 DEC_BOTH (from
, from_byte
);
2735 temp_pos
= dec_bytepos (temp_pos
);
2736 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2738 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2739 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2740 if (! (quoted
|| temp_code
== Sword
2741 || temp_code
== Ssymbol
2742 || temp_code
== Squote
))
2744 DEC_BOTH (from
, from_byte
);
2751 temp_pos
= dec_bytepos (from_byte
);
2752 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2753 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (temp_pos
))
2754 DEC_BOTH (from
, from_byte
);
2763 if (!++depth
) goto done2
;
2768 if (!--depth
) goto done2
;
2769 if (depth
< min_depth
)
2770 xsignal3 (Qscan_error
,
2771 build_string ("Containing expression ends prematurely"),
2772 make_number (last_good
), make_number (from
));
2776 if (!parse_sexp_ignore_comments
)
2778 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2779 &out_charpos
, &out_bytepos
);
2780 /* FIXME: if found == -1, then it really wasn't a comment-end.
2781 For single-char Sendcomment, we can't do much about it apart
2782 from skipping the char.
2783 For 2-char endcomments, we could try again, taking both
2784 chars as separate entities, but it's a lot of trouble
2785 for very little gain, so we don't bother either. -sm */
2787 from
= out_charpos
, from_byte
= out_bytepos
;
2790 case Scomment_fence
:
2796 DEC_BOTH (from
, from_byte
);
2797 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2798 if (!char_quoted (from
, from_byte
)
2799 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2800 SYNTAX_WITH_MULTIBYTE_CHECK (c
) == code
))
2803 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2807 stringterm
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2812 DEC_BOTH (from
, from_byte
);
2813 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2814 if (!char_quoted (from
, from_byte
)
2816 == (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
)))
2817 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2820 if (!depth
&& sexpflag
) goto done2
;
2823 /* Ignore whitespace, punctuation, quote, endcomment. */
2828 /* Reached start of buffer. Error if within object, return nil if between */
2841 XSETFASTINT (val
, from
);
2845 xsignal3 (Qscan_error
,
2846 build_string ("Unbalanced parentheses"),
2847 make_number (last_good
), make_number (from
));
2850 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
2851 doc
: /* Scan from character number FROM by COUNT lists.
2852 Scan forward if COUNT is positive, backward if COUNT is negative.
2853 Return the character number of the position thus found.
2855 A \"list", in this context, refers to a balanced parenthetical
2856 grouping, as determined by the syntax table.
2858 If DEPTH is nonzero, treat that as the nesting depth of the starting
2859 point (i.e. the starting point is DEPTH parentheses deep). This
2860 function scans over parentheses until the depth goes to zero COUNT
2861 times. Hence, positive DEPTH moves out that number of levels of
2862 parentheses, while negative DEPTH moves to a deeper level.
2864 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2866 If we reach the beginning or end of the accessible part of the buffer
2867 before we have scanned over COUNT lists, return nil if the depth at
2868 that point is zero, and signal a error if the depth is nonzero. */)
2869 (Lisp_Object from
, Lisp_Object count
, Lisp_Object depth
)
2871 CHECK_NUMBER (from
);
2872 CHECK_NUMBER (count
);
2873 CHECK_NUMBER (depth
);
2875 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
2878 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
2879 doc
: /* Scan from character number FROM by COUNT balanced expressions.
2880 If COUNT is negative, scan backwards.
2881 Returns the character number of the position thus found.
2883 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2885 If the beginning or end of (the accessible part of) the buffer is reached
2886 in the middle of a parenthetical grouping, an error is signaled.
2887 If the beginning or end is reached between groupings
2888 but before count is used up, nil is returned. */)
2889 (Lisp_Object from
, Lisp_Object count
)
2891 CHECK_NUMBER (from
);
2892 CHECK_NUMBER (count
);
2894 return scan_lists (XINT (from
), XINT (count
), 0, 1);
2897 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
2899 doc
: /* Move point backward over any number of chars with prefix syntax.
2900 This includes chars with "quote" or "prefix" syntax (' or p). */)
2903 ptrdiff_t beg
= BEGV
;
2904 ptrdiff_t opoint
= PT
;
2905 ptrdiff_t opoint_byte
= PT_BYTE
;
2907 ptrdiff_t pos_byte
= PT_BYTE
;
2912 SET_PT_BOTH (opoint
, opoint_byte
);
2917 SETUP_SYNTAX_TABLE (pos
, -1);
2919 DEC_BOTH (pos
, pos_byte
);
2921 while (!char_quoted (pos
, pos_byte
)
2922 /* Previous statement updates syntax table. */
2923 && ((c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
), SYNTAX (c
) == Squote
)
2924 || SYNTAX_PREFIX (c
)))
2927 opoint_byte
= pos_byte
;
2930 DEC_BOTH (pos
, pos_byte
);
2933 SET_PT_BOTH (opoint
, opoint_byte
);
2938 /* Parse forward from FROM / FROM_BYTE to END,
2939 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2940 and return a description of the state of the parse at END.
2941 If STOPBEFORE is nonzero, stop at the start of an atom.
2942 If COMMENTSTOP is 1, stop at the start of a comment.
2943 If COMMENTSTOP is -1, stop at the start or end of a comment,
2944 after the beginning of a string, or after the end of a string. */
2947 scan_sexps_forward (struct lisp_parse_state
*stateptr
,
2948 ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t end
,
2949 EMACS_INT targetdepth
, int stopbefore
,
2950 Lisp_Object oldstate
, int commentstop
)
2952 struct lisp_parse_state state
;
2954 register enum syntaxcode code
;
2957 struct level
{ ptrdiff_t last
, prev
; };
2958 struct level levelstart
[100];
2959 register struct level
*curlevel
= levelstart
;
2960 struct level
*endlevel
= levelstart
+ 100;
2961 register EMACS_INT depth
; /* Paren depth of current scanning location.
2962 level - levelstart equals this except
2963 when the depth becomes negative. */
2964 EMACS_INT mindepth
; /* Lowest DEPTH value seen. */
2965 int start_quoted
= 0; /* Nonzero means starting after a char quote */
2967 ptrdiff_t prev_from
; /* Keep one character before FROM. */
2968 ptrdiff_t prev_from_byte
;
2969 int prev_from_syntax
;
2970 int boundary_stop
= commentstop
== -1;
2973 ptrdiff_t out_bytepos
, out_charpos
;
2977 prev_from_byte
= from_byte
;
2979 DEC_BOTH (prev_from
, prev_from_byte
);
2981 /* Use this macro instead of `from++'. */
2983 do { prev_from = from; \
2984 prev_from_byte = from_byte; \
2985 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2986 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2987 INC_BOTH (from, from_byte); \
2989 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2995 if (NILP (oldstate
))
2998 state
.instring
= -1;
2999 state
.incomment
= 0;
3000 state
.comstyle
= 0; /* comment style a by default. */
3001 state
.comstr_start
= -1; /* no comment/string seen. */
3005 tem
= Fcar (oldstate
);
3011 oldstate
= Fcdr (oldstate
);
3012 oldstate
= Fcdr (oldstate
);
3013 oldstate
= Fcdr (oldstate
);
3014 tem
= Fcar (oldstate
);
3015 /* Check whether we are inside string_fence-style string: */
3016 state
.instring
= (!NILP (tem
)
3017 ? (CHARACTERP (tem
) ? XFASTINT (tem
) : ST_STRING_STYLE
)
3020 oldstate
= Fcdr (oldstate
);
3021 tem
= Fcar (oldstate
);
3022 state
.incomment
= (!NILP (tem
)
3023 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
3026 oldstate
= Fcdr (oldstate
);
3027 tem
= Fcar (oldstate
);
3028 start_quoted
= !NILP (tem
);
3030 /* if the eighth element of the list is nil, we are in comment
3031 style a. If it is non-nil, we are in comment style b */
3032 oldstate
= Fcdr (oldstate
);
3033 oldstate
= Fcdr (oldstate
);
3034 tem
= Fcar (oldstate
);
3035 state
.comstyle
= (NILP (tem
)
3037 : (RANGED_INTEGERP (0, tem
, ST_COMMENT_STYLE
)
3039 : ST_COMMENT_STYLE
));
3041 oldstate
= Fcdr (oldstate
);
3042 tem
= Fcar (oldstate
);
3043 state
.comstr_start
=
3044 RANGED_INTEGERP (PTRDIFF_MIN
, tem
, PTRDIFF_MAX
) ? XINT (tem
) : -1;
3045 oldstate
= Fcdr (oldstate
);
3046 tem
= Fcar (oldstate
);
3047 while (!NILP (tem
)) /* >= second enclosing sexps. */
3049 Lisp_Object temhd
= Fcar (tem
);
3050 if (RANGED_INTEGERP (PTRDIFF_MIN
, temhd
, PTRDIFF_MAX
))
3051 curlevel
->last
= XINT (temhd
);
3052 if (++curlevel
== endlevel
)
3053 curlevel
--; /* error ("Nesting too deep for parser"); */
3054 curlevel
->prev
= -1;
3055 curlevel
->last
= -1;
3062 curlevel
->prev
= -1;
3063 curlevel
->last
= -1;
3065 SETUP_SYNTAX_TABLE (prev_from
, 1);
3066 temp
= FETCH_CHAR (prev_from_byte
);
3067 prev_from_syntax
= SYNTAX_WITH_FLAGS (temp
);
3068 UPDATE_SYNTAX_TABLE_FORWARD (from
);
3070 /* Enter the loop at a place appropriate for initial state. */
3072 if (state
.incomment
)
3073 goto startincomment
;
3074 if (state
.instring
>= 0)
3076 nofence
= state
.instring
!= ST_STRING_STYLE
;
3078 goto startquotedinstring
;
3081 else if (start_quoted
)
3088 code
= prev_from_syntax
& 0xff;
3091 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
)
3092 && (c1
= FETCH_CHAR (from_byte
),
3093 syntax
= SYNTAX_WITH_FLAGS (c1
),
3094 SYNTAX_FLAGS_COMSTART_SECOND (syntax
)))
3095 /* Duplicate code to avoid a complex if-expression
3096 which causes trouble for the SGI compiler. */
3098 /* Record the comment style we have entered so that only
3099 the comment-end sequence of the same style actually
3100 terminates the comment section. */
3102 = SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_from_syntax
);
3103 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
);
3104 comnested
= comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
3105 state
.incomment
= comnested
? 1 : -1;
3106 state
.comstr_start
= prev_from
;
3110 else if (code
== Scomment_fence
)
3112 /* Record the comment style we have entered so that only
3113 the comment-end sequence of the same style actually
3114 terminates the comment section. */
3115 state
.comstyle
= ST_COMMENT_STYLE
;
3116 state
.incomment
= -1;
3117 state
.comstr_start
= prev_from
;
3120 else if (code
== Scomment
)
3122 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
, 0);
3123 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
3125 state
.comstr_start
= prev_from
;
3128 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
3134 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3135 curlevel
->last
= prev_from
;
3137 if (from
== end
) goto endquoted
;
3140 /* treat following character as a word constituent */
3143 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3144 curlevel
->last
= prev_from
;
3148 /* Some compilers can't handle this inside the switch. */
3149 temp
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3150 temp
= SYNTAX (temp
);
3156 if (from
== end
) goto endquoted
;
3168 curlevel
->prev
= curlevel
->last
;
3171 case Scomment_fence
: /* Can't happen because it's handled above. */
3173 if (commentstop
|| boundary_stop
) goto done
;
3175 /* The (from == BEGV) test was to enter the loop in the middle so
3176 that we find a 2-char comment ender even if we start in the
3177 middle of it. We don't want to do that if we're just at the
3178 beginning of the comment (think of (*) ... (*)). */
3179 found
= forw_comment (from
, from_byte
, end
,
3180 state
.incomment
, state
.comstyle
,
3181 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
3182 ? 0 : prev_from_syntax
,
3183 &out_charpos
, &out_bytepos
, &state
.incomment
);
3184 from
= out_charpos
; from_byte
= out_bytepos
;
3185 /* Beware! prev_from and friends are invalid now.
3186 Luckily, the `done' doesn't use them and the INC_FROM
3187 sets them to a sane value without looking at them. */
3188 if (!found
) goto done
;
3190 state
.incomment
= 0;
3191 state
.comstyle
= 0; /* reset the comment style */
3192 if (boundary_stop
) goto done
;
3196 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3198 /* curlevel++->last ran into compiler bug on Apollo */
3199 curlevel
->last
= prev_from
;
3200 if (++curlevel
== endlevel
)
3201 curlevel
--; /* error ("Nesting too deep for parser"); */
3202 curlevel
->prev
= -1;
3203 curlevel
->last
= -1;
3204 if (targetdepth
== depth
) goto done
;
3209 if (depth
< mindepth
)
3211 if (curlevel
!= levelstart
)
3213 curlevel
->prev
= curlevel
->last
;
3214 if (targetdepth
== depth
) goto done
;
3219 state
.comstr_start
= from
- 1;
3220 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3221 curlevel
->last
= prev_from
;
3222 state
.instring
= (code
== Sstring
3223 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte
))
3225 if (boundary_stop
) goto done
;
3228 nofence
= state
.instring
!= ST_STRING_STYLE
;
3234 if (from
>= end
) goto done
;
3235 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3236 /* Some compilers can't handle this inside the switch. */
3239 /* Check TEMP here so that if the char has
3240 a syntax-table property which says it is NOT
3241 a string character, it does not end the string. */
3242 if (nofence
&& c
== state
.instring
&& temp
== Sstring
)
3248 if (!nofence
) goto string_end
;
3253 startquotedinstring
:
3254 if (from
>= end
) goto endquoted
;
3260 state
.instring
= -1;
3261 curlevel
->prev
= curlevel
->last
;
3263 if (boundary_stop
) goto done
;
3267 /* FIXME: We should do something with it. */
3270 /* Ignore whitespace, punctuation, quote, endcomment. */
3276 stop
: /* Here if stopping before start of sexp. */
3277 from
= prev_from
; /* We have just fetched the char that starts it; */
3278 from_byte
= prev_from_byte
;
3279 goto done
; /* but return the position before it. */
3284 state
.depth
= depth
;
3285 state
.mindepth
= mindepth
;
3286 state
.thislevelstart
= curlevel
->prev
;
3287 state
.prevlevelstart
3288 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
3289 state
.location
= from
;
3290 state
.location_byte
= from_byte
;
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
,
3331 Lisp_Object stopbefore
, Lisp_Object oldstate
, Lisp_Object commentstop
)
3333 struct lisp_parse_state state
;
3336 if (!NILP (targetdepth
))
3338 CHECK_NUMBER (targetdepth
);
3339 target
= XINT (targetdepth
);
3342 target
= TYPE_MINIMUM (EMACS_INT
); /* We won't reach this depth */
3344 validate_region (&from
, &to
);
3345 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
3347 target
, !NILP (stopbefore
), oldstate
,
3349 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
3351 SET_PT_BOTH (state
.location
, state
.location_byte
);
3353 return Fcons (make_number (state
.depth
),
3354 Fcons (state
.prevlevelstart
< 0
3355 ? Qnil
: make_number (state
.prevlevelstart
),
3356 Fcons (state
.thislevelstart
< 0
3357 ? Qnil
: make_number (state
.thislevelstart
),
3358 Fcons (state
.instring
>= 0
3359 ? (state
.instring
== ST_STRING_STYLE
3360 ? Qt
: make_number (state
.instring
)) : Qnil
,
3361 Fcons (state
.incomment
< 0 ? Qt
:
3362 (state
.incomment
== 0 ? Qnil
:
3363 make_number (state
.incomment
)),
3364 Fcons (state
.quoted
? Qt
: Qnil
,
3365 Fcons (make_number (state
.mindepth
),
3366 Fcons ((state
.comstyle
3367 ? (state
.comstyle
== ST_COMMENT_STYLE
3369 : make_number (state
.comstyle
))
3371 Fcons (((state
.incomment
3372 || (state
.instring
>= 0))
3373 ? make_number (state
.comstr_start
)
3375 Fcons (state
.levelstarts
, Qnil
))))))))));
3379 init_syntax_once (void)
3384 /* This has to be done here, before we call Fmake_char_table. */
3385 DEFSYM (Qsyntax_table
, "syntax-table");
3387 /* Intern_C_String this now in case it isn't already done.
3388 Setting this variable twice is harmless.
3389 But don't staticpro it here--that is done in alloc.c. */
3390 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
3392 /* Create objects which can be shared among syntax tables. */
3393 Vsyntax_code_object
= make_uninit_vector (Smax
);
3394 for (i
= 0; i
< Smax
; i
++)
3395 ASET (Vsyntax_code_object
, i
, Fcons (make_number (i
), Qnil
));
3397 /* Now we are ready to set up this property, so we can
3398 create syntax tables. */
3399 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
3401 temp
= AREF (Vsyntax_code_object
, (int) Swhitespace
);
3403 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
3405 /* Control characters should not be whitespace. */
3406 temp
= AREF (Vsyntax_code_object
, (int) Spunct
);
3407 for (i
= 0; i
<= ' ' - 1; i
++)
3408 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3409 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 0177, temp
);
3411 /* Except that a few really are whitespace. */
3412 temp
= AREF (Vsyntax_code_object
, (int) Swhitespace
);
3413 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ' ', temp
);
3414 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\t', temp
);
3415 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\n', temp
);
3416 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 015, temp
);
3417 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 014, temp
);
3419 temp
= AREF (Vsyntax_code_object
, (int) Sword
);
3420 for (i
= 'a'; i
<= 'z'; i
++)
3421 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3422 for (i
= 'A'; i
<= 'Z'; i
++)
3423 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3424 for (i
= '0'; i
<= '9'; i
++)
3425 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3427 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
3428 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
3430 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
3431 Fcons (make_number (Sopen
), make_number (')')));
3432 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
3433 Fcons (make_number (Sclose
), make_number ('(')));
3434 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
3435 Fcons (make_number (Sopen
), make_number (']')));
3436 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
3437 Fcons (make_number (Sclose
), make_number ('[')));
3438 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
3439 Fcons (make_number (Sopen
), make_number ('}')));
3440 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
3441 Fcons (make_number (Sclose
), make_number ('{')));
3442 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
3443 Fcons (make_number ((int) Sstring
), Qnil
));
3444 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
3445 Fcons (make_number ((int) Sescape
), Qnil
));
3447 temp
= AREF (Vsyntax_code_object
, (int) Ssymbol
);
3448 for (i
= 0; i
< 10; i
++)
3450 c
= "_-+*/&|<>="[i
];
3451 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3454 temp
= AREF (Vsyntax_code_object
, (int) Spunct
);
3455 for (i
= 0; i
< 12; i
++)
3457 c
= ".,;:?!#@~^'`"[i
];
3458 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3461 /* All multibyte characters have syntax `word' by default. */
3462 temp
= AREF (Vsyntax_code_object
, (int) Sword
);
3463 char_table_set_range (Vstandard_syntax_table
, 0x80, MAX_CHAR
, temp
);
3467 syms_of_syntax (void)
3469 DEFSYM (Qsyntax_table_p
, "syntax-table-p");
3471 staticpro (&Vsyntax_code_object
);
3473 staticpro (&gl_state
.object
);
3474 staticpro (&gl_state
.global_code
);
3475 staticpro (&gl_state
.current_syntax_table
);
3476 staticpro (&gl_state
.old_prop
);
3478 /* Defined in regex.c */
3479 staticpro (&re_match_object
);
3481 DEFSYM (Qscan_error
, "scan-error");
3482 Fput (Qscan_error
, Qerror_conditions
,
3483 listn (CONSTYPE_PURE
, 2, Qscan_error
, Qerror
));
3484 Fput (Qscan_error
, Qerror_message
,
3485 build_pure_c_string ("Scan error"));
3487 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments
,
3488 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3490 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties
,
3491 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3492 Otherwise, that text property is simply ignored.
3493 See the info node `(elisp)Syntax Properties' for a description of the
3494 `syntax-table' property. */);
3496 words_include_escapes
= 0;
3497 DEFVAR_BOOL ("words-include-escapes", words_include_escapes
,
3498 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3500 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol
,
3501 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3502 multibyte_syntax_as_symbol
= 0;
3504 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3505 open_paren_in_column_0_is_defun_start
,
3506 doc
: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3507 open_paren_in_column_0_is_defun_start
= 1;
3510 DEFVAR_LISP ("find-word-boundary-function-table",
3511 Vfind_word_boundary_function_table
,
3513 Char table of functions to search for the word boundary.
3514 Each function is called with two arguments; POS and LIMIT.
3515 POS and LIMIT are character positions in the current buffer.
3517 If POS is less than LIMIT, POS is at the first character of a word,
3518 and the return value of a function is a position after the last
3519 character of that word.
3521 If POS is not less than LIMIT, POS is at the last character of a word,
3522 and the return value of a function is a position at the first
3523 character of that word.
3525 In both cases, LIMIT bounds the search. */);
3526 Vfind_word_boundary_function_table
= Fmake_char_table (Qnil
, Qnil
);
3528 defsubr (&Ssyntax_table_p
);
3529 defsubr (&Ssyntax_table
);
3530 defsubr (&Sstandard_syntax_table
);
3531 defsubr (&Scopy_syntax_table
);
3532 defsubr (&Sset_syntax_table
);
3533 defsubr (&Schar_syntax
);
3534 defsubr (&Smatching_paren
);
3535 defsubr (&Sstring_to_syntax
);
3536 defsubr (&Smodify_syntax_entry
);
3537 defsubr (&Sinternal_describe_syntax_value
);
3539 defsubr (&Sforward_word
);
3541 defsubr (&Sskip_chars_forward
);
3542 defsubr (&Sskip_chars_backward
);
3543 defsubr (&Sskip_syntax_forward
);
3544 defsubr (&Sskip_syntax_backward
);
3546 defsubr (&Sforward_comment
);
3547 defsubr (&Sscan_lists
);
3548 defsubr (&Sscan_sexps
);
3549 defsubr (&Sbackward_prefix_chars
);
3550 defsubr (&Sparse_partial_sexp
);