1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2011
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/>. */
27 #include "character.h"
31 /* Make syntax table lookup grant data in gl_state. */
32 #define SYNTAX_ENTRY_VIA_PROPERTY
35 #include "intervals.h"
38 /* Then there are seven single-bit flags that have the following meanings:
39 1. This character is the first of a two-character comment-start sequence.
40 2. This character is the second of a two-character comment-start sequence.
41 3. This character is the first of a two-character comment-end sequence.
42 4. This character is the second of a two-character comment-end sequence.
43 5. This character is a prefix, for backward-prefix-chars.
44 6. The char is part of a delimiter for comments of style "b".
45 7. This character is part of a nestable comment sequence.
46 8. The char is part of a delimiter for comments of style "c".
47 Note that any two-character sequence whose first character has flag 1
48 and whose second character has flag 2 will be interpreted as a comment start.
50 bit 6 and 8 are used to discriminate between different comment styles.
51 Languages such as C++ allow two orthogonal syntax start/end pairs
52 and bit 6 is used to determine whether a comment-end or Scommentend
53 ends style a or b. Comment markers can start style a, b, c, or bc.
54 Style a is always the default.
55 For 2-char comment markers, the style b flag is only looked up on the second
56 char of the comment marker and on the first char of the comment ender.
57 For style c (like to for the nested flag), the flag can be placed on any
61 /* These macros extract specific flags from an integer
62 that holds the syntax code and the flags. */
64 #define SYNTAX_FLAGS_COMSTART_FIRST(flags) (((flags) >> 16) & 1)
66 #define SYNTAX_FLAGS_COMSTART_SECOND(flags) (((flags) >> 17) & 1)
68 #define SYNTAX_FLAGS_COMEND_FIRST(flags) (((flags) >> 18) & 1)
70 #define SYNTAX_FLAGS_COMEND_SECOND(flags) (((flags) >> 19) & 1)
72 #define SYNTAX_FLAGS_PREFIX(flags) (((flags) >> 20) & 1)
74 #define SYNTAX_FLAGS_COMMENT_STYLEB(flags) (((flags) >> 21) & 1)
75 #define SYNTAX_FLAGS_COMMENT_STYLEC(flags) (((flags) >> 22) & 2)
76 /* FLAGS should be the flags of the main char of the comment marker, e.g.
77 the second for comstart and the first for comend. */
78 #define SYNTAX_FLAGS_COMMENT_STYLE(flags, other_flags) \
79 (SYNTAX_FLAGS_COMMENT_STYLEB (flags) \
80 | SYNTAX_FLAGS_COMMENT_STYLEC (flags) \
81 | SYNTAX_FLAGS_COMMENT_STYLEC (other_flags))
83 #define SYNTAX_FLAGS_COMMENT_NESTED(flags) (((flags) >> 22) & 1)
85 /* These macros extract a particular flag for a given character. */
87 #define SYNTAX_COMEND_FIRST(c) \
88 (SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c)))
89 #define SYNTAX_PREFIX(c) (SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c)))
91 /* We use these constants in place for comment-style and
92 string-ender-char to distinguish comments/strings started by
93 comment_fence and string_fence codes. */
95 #define ST_COMMENT_STYLE (256 + 1)
96 #define ST_STRING_STYLE (256 + 2)
98 Lisp_Object Qsyntax_table_p
, Qsyntax_table
, Qscan_error
;
100 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
101 if not compiled with GCC. No need to mark it, since it is used
102 only very temporarily. */
103 Lisp_Object syntax_temp
;
105 /* This is the internal form of the parse state used in parse-partial-sexp. */
107 struct lisp_parse_state
109 int depth
; /* Depth at end of parsing. */
110 int instring
; /* -1 if not within string, else desired terminator. */
111 int incomment
; /* -1 if in unnestable comment else comment nesting */
112 int comstyle
; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
113 int quoted
; /* Nonzero if just after an escape char at end of parsing */
114 int mindepth
; /* Minimum depth seen while scanning. */
115 /* Char number of most recent start-of-expression at current level */
116 EMACS_INT thislevelstart
;
117 /* Char number of start of containing expression */
118 EMACS_INT prevlevelstart
;
119 EMACS_INT location
; /* Char number at which parsing stopped. */
120 EMACS_INT comstr_start
; /* Position of last comment/string starter. */
121 Lisp_Object levelstarts
; /* Char numbers of starts-of-expression
122 of levels (starting from outermost). */
125 /* These variables are a cache for finding the start of a defun.
126 find_start_pos is the place for which the defun start was found.
127 find_start_value is the defun start position found for it.
128 find_start_value_byte is the corresponding byte position.
129 find_start_buffer is the buffer it was found in.
130 find_start_begv is the BEGV value when it was found.
131 find_start_modiff is the value of MODIFF when it was found. */
133 static EMACS_INT find_start_pos
;
134 static EMACS_INT find_start_value
;
135 static EMACS_INT find_start_value_byte
;
136 static struct buffer
*find_start_buffer
;
137 static EMACS_INT find_start_begv
;
138 static int find_start_modiff
;
141 static Lisp_Object
skip_chars (int, Lisp_Object
, Lisp_Object
, int);
142 static Lisp_Object
skip_syntaxes (int, Lisp_Object
, Lisp_Object
);
143 static Lisp_Object
scan_lists (EMACS_INT
, EMACS_INT
, EMACS_INT
, int);
144 static void scan_sexps_forward (struct lisp_parse_state
*,
145 EMACS_INT
, EMACS_INT
, EMACS_INT
, int,
146 int, Lisp_Object
, int);
147 static int in_classes (int, Lisp_Object
);
149 /* Whether the syntax of the character C has the prefix flag set. */
150 int syntax_prefix_flag_p (int c
)
152 return SYNTAX_PREFIX (c
);
155 struct gl_state_s gl_state
; /* Global state of syntax parser. */
157 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
158 to scan to property-change. */
160 /* Update gl_state to an appropriate interval which contains CHARPOS. The
161 sign of COUNT give the relative position of CHARPOS wrt the previously
162 valid interval. If INIT, only [be]_property fields of gl_state are
163 valid at start, the rest is filled basing on OBJECT.
165 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
166 direction than the intervals - or in an interval. We update the
167 current syntax-table basing on the property of this interval, and
168 update the interval to start further than CHARPOS - or be
169 NULL_INTERVAL. We also update lim_property to be the next value of
170 charpos to call this subroutine again - or be before/after the
171 start/end of OBJECT. */
174 update_syntax_table (EMACS_INT charpos
, int count
, int init
,
177 Lisp_Object tmp_table
;
184 gl_state
.old_prop
= Qnil
;
185 gl_state
.start
= gl_state
.b_property
;
186 gl_state
.stop
= gl_state
.e_property
;
187 i
= interval_of (charpos
, object
);
188 gl_state
.backward_i
= gl_state
.forward_i
= i
;
190 if (NULL_INTERVAL_P (i
))
192 /* interval_of updates only ->position of the return value, so
193 update the parents manually to speed up update_interval. */
194 while (!NULL_PARENT (i
))
196 if (AM_RIGHT_CHILD (i
))
197 INTERVAL_PARENT (i
)->position
= i
->position
198 - LEFT_TOTAL_LENGTH (i
) + TOTAL_LENGTH (i
) /* right end */
199 - TOTAL_LENGTH (INTERVAL_PARENT (i
))
200 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i
));
202 INTERVAL_PARENT (i
)->position
= i
->position
- LEFT_TOTAL_LENGTH (i
)
204 i
= INTERVAL_PARENT (i
);
206 i
= gl_state
.forward_i
;
207 gl_state
.b_property
= i
->position
- gl_state
.offset
;
208 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
211 i
= count
> 0 ? gl_state
.forward_i
: gl_state
.backward_i
;
213 /* We are guaranteed to be called with CHARPOS either in i,
215 if (NULL_INTERVAL_P (i
))
216 error ("Error in syntax_table logic for to-the-end intervals");
217 else if (charpos
< i
->position
) /* Move left. */
220 error ("Error in syntax_table logic for intervals <-");
221 /* Update the interval. */
222 i
= update_interval (i
, charpos
);
223 if (INTERVAL_LAST_POS (i
) != gl_state
.b_property
)
226 gl_state
.forward_i
= i
;
227 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
230 else if (charpos
>= INTERVAL_LAST_POS (i
)) /* Move right. */
233 error ("Error in syntax_table logic for intervals ->");
234 /* Update the interval. */
235 i
= update_interval (i
, charpos
);
236 if (i
->position
!= gl_state
.e_property
)
239 gl_state
.backward_i
= i
;
240 gl_state
.b_property
= i
->position
- gl_state
.offset
;
245 tmp_table
= textget (i
->plist
, Qsyntax_table
);
248 invalidate
= !EQ (tmp_table
, gl_state
.old_prop
); /* Need to invalidate? */
250 if (invalidate
) /* Did not get to adjacent interval. */
251 { /* with the same table => */
252 /* invalidate the old range. */
255 gl_state
.backward_i
= i
;
256 gl_state
.b_property
= i
->position
- gl_state
.offset
;
260 gl_state
.forward_i
= i
;
261 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
265 if (!EQ (tmp_table
, gl_state
.old_prop
))
267 gl_state
.current_syntax_table
= tmp_table
;
268 gl_state
.old_prop
= tmp_table
;
269 if (EQ (Fsyntax_table_p (tmp_table
), Qt
))
271 gl_state
.use_global
= 0;
273 else if (CONSP (tmp_table
))
275 gl_state
.use_global
= 1;
276 gl_state
.global_code
= tmp_table
;
280 gl_state
.use_global
= 0;
281 gl_state
.current_syntax_table
= BVAR (current_buffer
, syntax_table
);
285 while (!NULL_INTERVAL_P (i
))
287 if (cnt
&& !EQ (tmp_table
, textget (i
->plist
, Qsyntax_table
)))
291 gl_state
.e_property
= i
->position
- gl_state
.offset
;
292 gl_state
.forward_i
= i
;
297 = i
->position
+ LENGTH (i
) - gl_state
.offset
;
298 gl_state
.backward_i
= i
;
302 else if (cnt
== INTERVALS_AT_ONCE
)
307 = i
->position
+ LENGTH (i
) - gl_state
.offset
308 /* e_property at EOB is not set to ZV but to ZV+1, so that
309 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
310 having to check eob between the two. */
311 + (NULL_INTERVAL_P (next_interval (i
)) ? 1 : 0);
312 gl_state
.forward_i
= i
;
316 gl_state
.b_property
= i
->position
- gl_state
.offset
;
317 gl_state
.backward_i
= i
;
322 i
= count
> 0 ? next_interval (i
) : previous_interval (i
);
324 eassert (NULL_INTERVAL_P (i
)); /* This property goes to the end. */
326 gl_state
.e_property
= gl_state
.stop
;
328 gl_state
.b_property
= gl_state
.start
;
331 /* Returns TRUE if char at CHARPOS is quoted.
332 Global syntax-table data should be set up already to be good at CHARPOS
333 or after. On return global syntax data is good for lookup at CHARPOS. */
336 char_quoted (EMACS_INT charpos
, EMACS_INT bytepos
)
338 register enum syntaxcode code
;
339 register EMACS_INT beg
= BEGV
;
340 register int quoted
= 0;
341 EMACS_INT orig
= charpos
;
343 while (charpos
> beg
)
346 DEC_BOTH (charpos
, bytepos
);
348 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
);
349 c
= FETCH_CHAR_AS_MULTIBYTE (bytepos
);
351 if (! (code
== Scharquote
|| code
== Sescape
))
357 UPDATE_SYNTAX_TABLE (orig
);
361 /* Return the bytepos one character before BYTEPOS.
362 We assume that BYTEPOS is not at the start of the buffer. */
364 static INLINE EMACS_INT
365 dec_bytepos (EMACS_INT bytepos
)
367 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
374 /* Return a defun-start position before POS and not too far before.
375 It should be the last one before POS, or nearly the last.
377 When open_paren_in_column_0_is_defun_start is nonzero,
378 only the beginning of the buffer is treated as a defun-start.
380 We record the information about where the scan started
381 and what its result was, so that another call in the same area
382 can return the same value very quickly.
384 There is no promise at which position the global syntax data is
385 valid on return from the subroutine, so the caller should explicitly
386 update the global data. */
389 find_defun_start (EMACS_INT pos
, EMACS_INT pos_byte
)
391 EMACS_INT opoint
= PT
, opoint_byte
= PT_BYTE
;
393 if (!open_paren_in_column_0_is_defun_start
)
395 find_start_value_byte
= BEGV_BYTE
;
399 /* Use previous finding, if it's valid and applies to this inquiry. */
400 if (current_buffer
== find_start_buffer
401 /* Reuse the defun-start even if POS is a little farther on.
402 POS might be in the next defun, but that's ok.
403 Our value may not be the best possible, but will still be usable. */
404 && pos
<= find_start_pos
+ 1000
405 && pos
>= find_start_value
406 && BEGV
== find_start_begv
407 && MODIFF
== find_start_modiff
)
408 return find_start_value
;
410 /* Back up to start of line. */
411 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
413 /* We optimize syntax-table lookup for rare updates. Thus we accept
414 only those `^\s(' which are good in global _and_ text-property
416 SETUP_BUFFER_SYNTAX_TABLE ();
421 /* Open-paren at start of line means we may have found our
423 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
424 if (SYNTAX (c
) == Sopen
)
426 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
427 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
428 if (SYNTAX (c
) == Sopen
)
430 /* Now fallback to the default value. */
431 SETUP_BUFFER_SYNTAX_TABLE ();
433 /* Move to beg of previous line. */
434 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
437 /* Record what we found, for the next try. */
438 find_start_value
= PT
;
439 find_start_value_byte
= PT_BYTE
;
440 find_start_buffer
= current_buffer
;
441 find_start_modiff
= MODIFF
;
442 find_start_begv
= BEGV
;
443 find_start_pos
= pos
;
445 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
447 return find_start_value
;
450 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
453 prev_char_comend_first (EMACS_INT pos
, EMACS_INT pos_byte
)
457 DEC_BOTH (pos
, pos_byte
);
458 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
459 c
= FETCH_CHAR (pos_byte
);
460 val
= SYNTAX_COMEND_FIRST (c
);
461 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
465 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
468 * prev_char_comstart_first (pos, pos_byte)
473 * DEC_BOTH (pos, pos_byte);
474 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
475 * c = FETCH_CHAR (pos_byte);
476 * val = SYNTAX_COMSTART_FIRST (c);
477 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
481 /* Checks whether charpos FROM is at the end of a comment.
482 FROM_BYTE is the bytepos corresponding to FROM.
483 Do not move back before STOP.
485 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
488 If successful, store the charpos of the comment's beginning
489 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
491 Global syntax data remains valid for backward search starting at
492 the returned value (or at FROM, if the search was not successful). */
495 back_comment (EMACS_INT from
, EMACS_INT from_byte
, EMACS_INT stop
, int comnested
, int comstyle
, EMACS_INT
*charpos_ptr
, EMACS_INT
*bytepos_ptr
)
497 /* Look back, counting the parity of string-quotes,
498 and recording the comment-starters seen.
499 When we reach a safe place, assume that's not in a string;
500 then step the main scan to the earliest comment-starter seen
501 an even number of string quotes away from the safe place.
503 OFROM[I] is position of the earliest comment-starter seen
504 which is I+2X quotes from the comment-end.
505 PARITY is current parity of quotes from the comment end. */
506 int string_style
= -1; /* Presumed outside of any string. */
507 int string_lossage
= 0;
508 /* Not a real lossage: indicates that we have passed a matching comment
509 starter plus a non-matching comment-ender, meaning that any matching
510 comment-starter we might see later could be a false positive (hidden
511 inside another comment).
512 Test case: { a (* b } c (* d *) */
513 int comment_lossage
= 0;
514 EMACS_INT comment_end
= from
;
515 EMACS_INT comment_end_byte
= from_byte
;
516 EMACS_INT comstart_pos
= 0;
517 EMACS_INT comstart_byte
IF_LINT (= 0);
518 /* Place where the containing defun starts,
519 or 0 if we didn't come across it yet. */
520 EMACS_INT defun_start
= 0;
521 EMACS_INT defun_start_byte
= 0;
522 register enum syntaxcode code
;
523 int nesting
= 1; /* current comment nesting */
527 /* FIXME: A }} comment-ender style leads to incorrect behavior
528 in the case of {{ c }}} because we ignore the last two chars which are
529 assumed to be comment-enders although they aren't. */
531 /* At beginning of range to scan, we're outside of strings;
532 that determines quote parity to the comment-end. */
536 int prev_syntax
, com2start
, com2end
;
539 /* Move back and examine a character. */
540 DEC_BOTH (from
, from_byte
);
541 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
543 prev_syntax
= syntax
;
544 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
545 syntax
= SYNTAX_WITH_FLAGS (c
);
548 /* Check for 2-char comment markers. */
549 com2start
= (SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
550 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax
)
552 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax
, syntax
))
553 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
554 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
);
555 com2end
= (SYNTAX_FLAGS_COMEND_FIRST (syntax
)
556 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax
));
557 comstart
= (com2start
|| code
== Scomment
);
559 /* Nasty cases with overlapping 2-char comment markers:
560 - snmp-mode: -- c -- foo -- c --
568 /* If a 2-char comment sequence partly overlaps with another,
569 we don't try to be clever. E.g. |*| in C, or }% in modes that
570 have %..\n and %{..}%. */
571 if (from
> stop
&& (com2end
|| comstart
))
573 EMACS_INT next
= from
, next_byte
= from_byte
;
574 int next_c
, next_syntax
;
575 DEC_BOTH (next
, next_byte
);
576 UPDATE_SYNTAX_TABLE_BACKWARD (next
);
577 next_c
= FETCH_CHAR_AS_MULTIBYTE (next_byte
);
578 next_syntax
= SYNTAX_WITH_FLAGS (next_c
);
579 if (((comstart
|| comnested
)
580 && SYNTAX_FLAGS_COMEND_SECOND (syntax
)
581 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax
))
582 || ((com2end
|| comnested
)
583 && SYNTAX_FLAGS_COMSTART_SECOND (syntax
)
585 == SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_syntax
))
586 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax
)))
588 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
591 if (com2start
&& comstart_pos
== 0)
592 /* We're looking at a comment starter. But it might be a comment
593 ender as well (see snmp-mode). The first time we see one, we
594 need to consider it as a comment starter,
595 and the subsequent times as a comment ender. */
598 /* Turn a 2-char comment sequences into the appropriate syntax. */
603 /* Ignore comment starters of a different style. */
604 else if (code
== Scomment
605 && (comstyle
!= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0)
606 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
) != comnested
))
609 /* Ignore escaped characters, except comment-enders. */
610 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
617 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
619 /* Track parity of quotes. */
620 if (string_style
== -1)
621 /* Entering a string. */
623 else if (string_style
== c
)
624 /* Leaving the string. */
627 /* If we have two kinds of string delimiters.
628 There's no way to grok this scanning backwards. */
633 /* We've already checked that it is the relevant comstyle. */
634 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
635 /* There are odd string quotes involved, so let's be careful.
636 Test case in Pascal: " { " a { " } */
641 /* Record best comment-starter so far. */
643 comstart_byte
= from_byte
;
645 else if (--nesting
<= 0)
646 /* nested comments have to be balanced, so we don't need to
647 keep looking for earlier ones. We use here the same (slightly
648 incorrect) reasoning as below: since it is followed by uniform
649 paired string quotes, this comment-start has to be outside of
650 strings, else the comment-end itself would be inside a string. */
655 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == comstyle
656 && ((com2end
&& SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
))
657 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
658 /* This is the same style of comment ender as ours. */
663 /* Anything before that can't count because it would match
664 this comment-ender rather than ours. */
665 from
= stop
; /* Break out of the loop. */
667 else if (comstart_pos
!= 0 || c
!= '\n')
668 /* We're mixing comment styles here, so we'd better be careful.
669 The (comstart_pos != 0 || c != '\n') check is not quite correct
670 (we should just always set comment_lossage), but removing it
671 would imply that any multiline comment in C would go through
672 lossage, which seems overkill.
673 The failure should only happen in the rare cases such as
679 /* Assume a defun-start point is outside of strings. */
680 if (open_paren_in_column_0_is_defun_start
682 || (temp_byte
= dec_bytepos (from_byte
),
683 FETCH_CHAR (temp_byte
) == '\n')))
686 defun_start_byte
= from_byte
;
687 from
= stop
; /* Break out of the loop. */
696 if (comstart_pos
== 0)
699 from_byte
= comment_end_byte
;
700 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
- 1);
702 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
703 or `done'), then we've found the beginning of the non-nested comment. */
704 else if (1) /* !comnested */
707 from_byte
= comstart_byte
;
708 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
712 struct lisp_parse_state state
;
714 /* We had two kinds of string delimiters mixed up
715 together. Decode this going forwards.
716 Scan fwd from a known safe place (beginning-of-defun)
717 to the one in question; this records where we
718 last passed a comment starter. */
719 /* If we did not already find the defun start, find it now. */
720 if (defun_start
== 0)
722 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
723 defun_start_byte
= find_start_value_byte
;
727 scan_sexps_forward (&state
,
728 defun_start
, defun_start_byte
,
729 comment_end
, -10000, 0, Qnil
, 0);
730 defun_start
= comment_end
;
731 if (state
.incomment
== (comnested
? 1 : -1)
732 && state
.comstyle
== comstyle
)
733 from
= state
.comstr_start
;
738 /* If comment_end is inside some other comment, maybe ours
739 is nested, so we need to try again from within the
740 surrounding comment. Example: { a (* " *) */
742 /* FIXME: We should advance by one or two chars. */
743 defun_start
= state
.comstr_start
+ 2;
744 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
747 } while (defun_start
< comment_end
);
749 from_byte
= CHAR_TO_BYTE (from
);
750 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
755 *bytepos_ptr
= from_byte
;
757 return (from
== comment_end
) ? -1 : from
;
760 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
761 doc
: /* Return t if OBJECT is a syntax table.
762 Currently, any char-table counts as a syntax table. */)
765 if (CHAR_TABLE_P (object
)
766 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
772 check_syntax_table (Lisp_Object obj
)
774 CHECK_TYPE (CHAR_TABLE_P (obj
) && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
),
775 Qsyntax_table_p
, obj
);
778 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
779 doc
: /* Return the current syntax table.
780 This is the one specified by the current buffer. */)
783 return BVAR (current_buffer
, syntax_table
);
786 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
787 Sstandard_syntax_table
, 0, 0, 0,
788 doc
: /* Return the standard syntax table.
789 This is the one used for new buffers. */)
792 return Vstandard_syntax_table
;
795 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
796 doc
: /* Construct a new syntax table and return it.
797 It is a copy of the TABLE, which defaults to the standard syntax table. */)
803 check_syntax_table (table
);
805 table
= Vstandard_syntax_table
;
807 copy
= Fcopy_sequence (table
);
809 /* Only the standard syntax table should have a default element.
810 Other syntax tables should inherit from parents instead. */
811 XCHAR_TABLE (copy
)->defalt
= Qnil
;
813 /* Copied syntax tables should all have parents.
814 If we copied one with no parent, such as the standard syntax table,
815 use the standard syntax table as the copy's parent. */
816 if (NILP (XCHAR_TABLE (copy
)->parent
))
817 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
821 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
822 doc
: /* Select a new syntax table for the current buffer.
823 One argument, a syntax table. */)
827 check_syntax_table (table
);
828 BVAR (current_buffer
, syntax_table
) = table
;
829 /* Indicate that this buffer now has a specified syntax table. */
830 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
831 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
835 /* Convert a letter which signifies a syntax code
836 into the code it signifies.
837 This is used by modify-syntax-entry, and other things. */
839 unsigned char syntax_spec_code
[0400] =
840 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
841 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
842 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
843 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
844 (char) Swhitespace
, (char) Scomment_fence
, (char) Sstring
, 0377,
845 (char) Smath
, 0377, 0377, (char) Squote
,
846 (char) Sopen
, (char) Sclose
, 0377, 0377,
847 0377, (char) Swhitespace
, (char) Spunct
, (char) Scharquote
,
848 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
849 0377, 0377, 0377, 0377,
850 (char) Scomment
, 0377, (char) Sendcomment
, 0377,
851 (char) Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
852 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
853 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
854 0377, 0377, 0377, 0377, (char) Sescape
, 0377, 0377, (char) Ssymbol
,
855 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
856 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
857 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
858 0377, 0377, 0377, 0377, (char) Sstring_fence
, 0377, 0377, 0377
861 /* Indexed by syntax code, give the letter that describes it. */
863 char syntax_code_spec
[16] =
865 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
869 /* Indexed by syntax code, give the object (cons of syntax code and
870 nil) to be stored in syntax table. Since these objects can be
871 shared among syntax tables, we generate them in advance. By
872 sharing objects, the function `describe-syntax' can give a more
874 static Lisp_Object Vsyntax_code_object
;
877 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
878 doc
: /* Return the syntax code of CHARACTER, described by a character.
879 For example, if CHARACTER is a word constituent, the
880 character `w' (119) is returned.
881 The characters that correspond to various syntax codes
882 are listed in the documentation of `modify-syntax-entry'. */)
883 (Lisp_Object character
)
886 CHECK_CHARACTER (character
);
887 char_int
= XINT (character
);
888 SETUP_BUFFER_SYNTAX_TABLE ();
889 return make_number (syntax_code_spec
[(int) SYNTAX (char_int
)]);
892 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
893 doc
: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
894 (Lisp_Object character
)
897 CHECK_NUMBER (character
);
898 char_int
= XINT (character
);
899 SETUP_BUFFER_SYNTAX_TABLE ();
900 code
= SYNTAX (char_int
);
901 if (code
== Sopen
|| code
== Sclose
)
902 return SYNTAX_MATCH (char_int
);
906 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
907 doc
: /* Convert a syntax specification STRING into syntax cell form.
908 STRING should be a string as it is allowed as argument of
909 `modify-syntax-entry'. Value is the equivalent cons cell
910 \(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
914 register const unsigned char *p
;
915 register enum syntaxcode code
;
919 CHECK_STRING (string
);
922 code
= (enum syntaxcode
) syntax_spec_code
[*p
++];
923 if (((int) code
& 0377) == 0377)
924 error ("Invalid syntax description letter: %c", p
[-1]);
926 if (code
== Sinherit
)
932 int character
= STRING_CHAR_AND_LENGTH (p
, len
);
933 XSETINT (match
, character
);
934 if (XFASTINT (match
) == ' ')
978 if (val
< XVECTOR (Vsyntax_code_object
)->size
&& NILP (match
))
979 return XVECTOR (Vsyntax_code_object
)->contents
[val
];
981 /* Since we can't use a shared object, let's make a new one. */
982 return Fcons (make_number (val
), match
);
985 /* I really don't know why this is interactive
986 help-form should at least be made useful whilst reading the second arg. */
987 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
988 "cSet syntax for character: \nsSet syntax for %s to: ",
989 doc
: /* Set syntax for character CHAR according to string NEWENTRY.
990 The syntax is changed only for table SYNTAX-TABLE, which defaults to
991 the current buffer's syntax table.
992 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
993 in the range MIN to MAX are changed.
994 The first character of NEWENTRY should be one of the following:
995 Space or - whitespace syntax. w word constituent.
996 _ symbol constituent. . punctuation.
997 ( open-parenthesis. ) close-parenthesis.
998 " string quote. \\ escape.
999 $ paired delimiter. ' expression quote or prefix operator.
1000 < comment starter. > comment ender.
1001 / character-quote. @ inherit from `standard-syntax-table'.
1002 | generic string fence. ! generic comment fence.
1004 Only single-character comment start and end sequences are represented thus.
1005 Two-character sequences are represented as described below.
1006 The second character of NEWENTRY is the matching parenthesis,
1007 used only if the first character is `(' or `)'.
1008 Any additional characters are flags.
1009 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1010 1 means CHAR is the start of a two-char comment start sequence.
1011 2 means CHAR is the second character of such a sequence.
1012 3 means CHAR is the start of a two-char comment end sequence.
1013 4 means CHAR is the second character of such a sequence.
1015 There can be several orthogonal comment sequences. This is to support
1016 language modes such as C++. By default, all comment sequences are of style
1017 a, but you can set the comment sequence style to b (on the second character
1018 of a comment-start, and the first character of a comment-end sequence) and/or
1019 c (on any of its chars) using this flag:
1020 b means CHAR is part of comment sequence b.
1021 c means CHAR is part of comment sequence c.
1022 n means CHAR is part of a nestable comment sequence.
1024 p means CHAR is a prefix character for `backward-prefix-chars';
1025 such characters are treated as whitespace when they occur
1026 between expressions.
1027 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1028 (Lisp_Object c
, Lisp_Object newentry
, Lisp_Object syntax_table
)
1032 CHECK_CHARACTER_CAR (c
);
1033 CHECK_CHARACTER_CDR (c
);
1036 CHECK_CHARACTER (c
);
1038 if (NILP (syntax_table
))
1039 syntax_table
= BVAR (current_buffer
, syntax_table
);
1041 check_syntax_table (syntax_table
);
1043 newentry
= Fstring_to_syntax (newentry
);
1045 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table
, c
, newentry
);
1047 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), newentry
);
1049 /* We clear the regexp cache, since character classes can now have
1050 different values from those in the compiled regexps.*/
1051 clear_regexp_cache ();
1056 /* Dump syntax table to buffer in human-readable format */
1058 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value
,
1059 Sinternal_describe_syntax_value
, 1, 1, 0,
1060 doc
: /* Insert a description of the internal syntax description SYNTAX at point. */)
1061 (Lisp_Object syntax
)
1063 register enum syntaxcode code
;
1065 char desc
, start1
, start2
, end1
, end2
, prefix
,
1066 comstyleb
, comstylec
, comnested
;
1068 Lisp_Object first
, match_lisp
, value
= syntax
;
1072 insert_string ("default");
1076 if (CHAR_TABLE_P (value
))
1078 insert_string ("deeper char-table ...");
1084 insert_string ("invalid");
1088 first
= XCAR (value
);
1089 match_lisp
= XCDR (value
);
1091 if (!INTEGERP (first
) || !(NILP (match_lisp
) || INTEGERP (match_lisp
)))
1093 insert_string ("invalid");
1097 syntax_code
= XINT (first
);
1098 code
= (enum syntaxcode
) (syntax_code
& 0377);
1099 start1
= SYNTAX_FLAGS_COMSTART_FIRST (syntax_code
);
1100 start2
= SYNTAX_FLAGS_COMSTART_SECOND (syntax_code
);;
1101 end1
= SYNTAX_FLAGS_COMEND_FIRST (syntax_code
);
1102 end2
= SYNTAX_FLAGS_COMEND_SECOND (syntax_code
);
1103 prefix
= SYNTAX_FLAGS_PREFIX (syntax_code
);
1104 comstyleb
= SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code
);
1105 comstylec
= SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code
);
1106 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax_code
);
1108 if ((int) code
< 0 || (int) code
>= (int) Smax
)
1110 insert_string ("invalid");
1113 desc
= syntax_code_spec
[(int) code
];
1115 str
[0] = desc
, str
[1] = 0;
1118 if (NILP (match_lisp
))
1121 insert_char (XINT (match_lisp
));
1142 insert_string ("\twhich means: ");
1144 switch (SWITCH_ENUM_CAST (code
))
1147 insert_string ("whitespace"); break;
1149 insert_string ("punctuation"); break;
1151 insert_string ("word"); break;
1153 insert_string ("symbol"); break;
1155 insert_string ("open"); break;
1157 insert_string ("close"); break;
1159 insert_string ("prefix"); break;
1161 insert_string ("string"); break;
1163 insert_string ("math"); break;
1165 insert_string ("escape"); break;
1167 insert_string ("charquote"); break;
1169 insert_string ("comment"); break;
1171 insert_string ("endcomment"); break;
1173 insert_string ("inherit"); break;
1174 case Scomment_fence
:
1175 insert_string ("comment fence"); break;
1177 insert_string ("string fence"); break;
1179 insert_string ("invalid");
1183 if (!NILP (match_lisp
))
1185 insert_string (", matches ");
1186 insert_char (XINT (match_lisp
));
1190 insert_string (",\n\t is the first character of a comment-start sequence");
1192 insert_string (",\n\t is the second character of a comment-start sequence");
1195 insert_string (",\n\t is the first character of a comment-end sequence");
1197 insert_string (",\n\t is the second character of a comment-end sequence");
1199 insert_string (" (comment style b)");
1201 insert_string (" (comment style c)");
1203 insert_string (" (nestable)");
1206 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1211 /* Return the position across COUNT words from FROM.
1212 If that many words cannot be found before the end of the buffer, return 0.
1213 COUNT negative means scan backward and stop at word beginning. */
1216 scan_words (register EMACS_INT from
, register EMACS_INT count
)
1218 register EMACS_INT beg
= BEGV
;
1219 register EMACS_INT end
= ZV
;
1220 register EMACS_INT from_byte
= CHAR_TO_BYTE (from
);
1221 register enum syntaxcode code
;
1223 Lisp_Object func
, pos
;
1228 SETUP_SYNTAX_TABLE (from
, count
);
1239 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1240 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1241 code
= SYNTAX (ch0
);
1242 INC_BOTH (from
, from_byte
);
1243 if (words_include_escapes
1244 && (code
== Sescape
|| code
== Scharquote
))
1249 /* Now CH0 is a character which begins a word and FROM is the
1250 position of the next character. */
1251 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch0
);
1252 if (! NILP (Ffboundp (func
)))
1254 pos
= call2 (func
, make_number (from
- 1), make_number (end
));
1255 if (INTEGERP (pos
) && XINT (pos
) > from
)
1258 from_byte
= CHAR_TO_BYTE (from
);
1265 if (from
== end
) break;
1266 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1267 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1268 code
= SYNTAX (ch1
);
1270 && (! words_include_escapes
1271 || (code
!= Sescape
&& code
!= Scharquote
)))
1272 || word_boundary_p (ch0
, ch1
))
1274 INC_BOTH (from
, from_byte
);
1289 DEC_BOTH (from
, from_byte
);
1290 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1291 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1292 code
= SYNTAX (ch1
);
1293 if (words_include_escapes
1294 && (code
== Sescape
|| code
== Scharquote
))
1299 /* Now CH1 is a character which ends a word and FROM is the
1301 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch1
);
1302 if (! NILP (Ffboundp (func
)))
1304 pos
= call2 (func
, make_number (from
), make_number (beg
));
1305 if (INTEGERP (pos
) && XINT (pos
) < from
)
1308 from_byte
= CHAR_TO_BYTE (from
);
1317 DEC_BOTH (from
, from_byte
);
1318 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1319 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1320 code
= SYNTAX (ch0
);
1322 && (! words_include_escapes
1323 || (code
!= Sescape
&& code
!= Scharquote
)))
1324 || word_boundary_p (ch0
, ch1
))
1326 INC_BOTH (from
, from_byte
);
1340 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 0, 1, "^p",
1341 doc
: /* Move point forward ARG words (backward if ARG is negative).
1343 If an edge of the buffer or a field boundary is reached, point is left there
1344 and the function returns nil. Field boundaries are not noticed if
1345 `inhibit-field-text-motion' is non-nil. */)
1352 XSETFASTINT (arg
, 1);
1356 val
= orig_val
= scan_words (PT
, XINT (arg
));
1358 val
= XINT (arg
) > 0 ? ZV
: BEGV
;
1360 /* Avoid jumping out of an input field. */
1361 tmp
= Fconstrain_to_field (make_number (val
), make_number (PT
),
1363 val
= XFASTINT (tmp
);
1366 return val
== orig_val
? Qt
: Qnil
;
1369 Lisp_Object
skip_chars (int, Lisp_Object
, Lisp_Object
, int);
1371 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1372 doc
: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1373 STRING is like the inside of a `[...]' in a regular expression
1374 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1375 (but not at the end of a range; quoting is never needed there).
1376 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1377 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1378 Char classes, e.g. `[:alpha:]', are supported.
1380 Returns the distance traveled, either zero or positive. */)
1381 (Lisp_Object string
, Lisp_Object lim
)
1383 return skip_chars (1, string
, lim
, 1);
1386 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1387 doc
: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1388 See `skip-chars-forward' for details.
1389 Returns the distance traveled, either zero or negative. */)
1390 (Lisp_Object string
, Lisp_Object lim
)
1392 return skip_chars (0, string
, lim
, 1);
1395 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1396 doc
: /* Move point forward across chars in specified syntax classes.
1397 SYNTAX is a string of syntax code characters.
1398 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1399 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1400 This function returns the distance traveled, either zero or positive. */)
1401 (Lisp_Object syntax
, Lisp_Object lim
)
1403 return skip_syntaxes (1, syntax
, lim
);
1406 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1407 doc
: /* Move point backward across chars in specified syntax classes.
1408 SYNTAX is a string of syntax code characters.
1409 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1410 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1411 This function returns the distance traveled, either zero or negative. */)
1412 (Lisp_Object syntax
, Lisp_Object lim
)
1414 return skip_syntaxes (0, syntax
, lim
);
1418 skip_chars (int forwardp
, Lisp_Object string
, Lisp_Object lim
, int handle_iso_classes
)
1420 register unsigned int c
;
1421 unsigned char fastmap
[0400];
1422 /* Store the ranges of non-ASCII characters. */
1423 int *char_ranges
IF_LINT (= NULL
);
1424 int n_char_ranges
= 0;
1426 register EMACS_INT i
, i_byte
;
1427 /* Set to 1 if the current buffer is multibyte and the region
1428 contains non-ASCII chars. */
1430 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1432 int string_multibyte
;
1433 EMACS_INT size_byte
;
1434 const unsigned char *str
;
1436 Lisp_Object iso_classes
;
1438 CHECK_STRING (string
);
1442 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1444 CHECK_NUMBER_COERCE_MARKER (lim
);
1446 /* In any case, don't allow scan outside bounds of buffer. */
1447 if (XINT (lim
) > ZV
)
1448 XSETFASTINT (lim
, ZV
);
1449 if (XINT (lim
) < BEGV
)
1450 XSETFASTINT (lim
, BEGV
);
1452 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1453 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1454 string_multibyte
= SBYTES (string
) > SCHARS (string
);
1456 memset (fastmap
, 0, sizeof fastmap
);
1458 str
= SDATA (string
);
1459 size_byte
= SBYTES (string
);
1462 if (i_byte
< size_byte
1463 && SREF (string
, 0) == '^')
1465 negate
= 1; i_byte
++;
1468 /* Find the characters specified and set their elements of fastmap.
1469 Handle backslashes and ranges specially.
1471 If STRING contains non-ASCII characters, setup char_ranges for
1472 them and use fastmap only for their leading codes. */
1474 if (! string_multibyte
)
1476 int string_has_eight_bit
= 0;
1478 /* At first setup fastmap. */
1479 while (i_byte
< size_byte
)
1483 if (handle_iso_classes
&& c
== '['
1484 && i_byte
< size_byte
1485 && str
[i_byte
] == ':')
1487 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1488 const unsigned char *class_end
= class_beg
;
1489 const unsigned char *class_limit
= str
+ size_byte
- 2;
1490 /* Leave room for the null. */
1491 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1494 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1495 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1497 while (class_end
< class_limit
1498 && *class_end
>= 'a' && *class_end
<= 'z')
1501 if (class_end
== class_beg
1502 || *class_end
!= ':' || class_end
[1] != ']')
1503 goto not_a_class_name
;
1505 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1506 class_name
[class_end
- class_beg
] = 0;
1508 cc
= re_wctype (class_name
);
1510 error ("Invalid ISO C character class");
1512 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1514 i_byte
= class_end
+ 2 - str
;
1521 if (i_byte
== size_byte
)
1526 /* Treat `-' as range character only if another character
1528 if (i_byte
+ 1 < size_byte
1529 && str
[i_byte
] == '-')
1533 /* Skip over the dash. */
1536 /* Get the end of the range. */
1539 && i_byte
< size_byte
)
1544 unsigned lim2
= c2
+ 1;
1547 if (! ASCII_CHAR_P (c2
))
1548 string_has_eight_bit
= 1;
1554 if (! ASCII_CHAR_P (c
))
1555 string_has_eight_bit
= 1;
1559 /* If the current range is multibyte and STRING contains
1560 eight-bit chars, arrange fastmap and setup char_ranges for
1561 the corresponding multibyte chars. */
1562 if (multibyte
&& string_has_eight_bit
)
1564 unsigned char fastmap2
[0400];
1565 int range_start_byte
, range_start_char
;
1567 memcpy (fastmap
+ 0200, fastmap2
+ 0200, 0200);
1568 memset (fastmap
+ 0200, 0, 0200);
1569 /* We are sure that this loop stops. */
1570 for (i
= 0200; ! fastmap2
[i
]; i
++);
1571 c
= BYTE8_TO_CHAR (i
);
1572 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
1573 range_start_byte
= i
;
1574 range_start_char
= c
;
1575 char_ranges
= (int *) alloca (sizeof (int) * 128 * 2);
1576 for (i
= 129; i
< 0400; i
++)
1578 c
= BYTE8_TO_CHAR (i
);
1579 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
1580 if (i
- range_start_byte
!= c
- range_start_char
)
1582 char_ranges
[n_char_ranges
++] = range_start_char
;
1583 char_ranges
[n_char_ranges
++] = ((i
- 1 - range_start_byte
)
1584 + range_start_char
);
1585 range_start_byte
= i
;
1586 range_start_char
= c
;
1589 char_ranges
[n_char_ranges
++] = range_start_char
;
1590 char_ranges
[n_char_ranges
++] = ((i
- 1 - range_start_byte
)
1591 + range_start_char
);
1594 else /* STRING is multibyte */
1596 char_ranges
= (int *) alloca (sizeof (int) * SCHARS (string
) * 2);
1598 while (i_byte
< size_byte
)
1600 unsigned char leading_code
;
1602 leading_code
= str
[i_byte
];
1603 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1606 if (handle_iso_classes
&& c
== '['
1607 && i_byte
< size_byte
1608 && STRING_CHAR (str
+ i_byte
) == ':')
1610 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1611 const unsigned char *class_end
= class_beg
;
1612 const unsigned char *class_limit
= str
+ size_byte
- 2;
1613 /* Leave room for the null. */
1614 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1617 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1618 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1620 while (class_end
< class_limit
1621 && *class_end
>= 'a' && *class_end
<= 'z')
1624 if (class_end
== class_beg
1625 || *class_end
!= ':' || class_end
[1] != ']')
1626 goto not_a_class_name_multibyte
;
1628 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1629 class_name
[class_end
- class_beg
] = 0;
1631 cc
= re_wctype (class_name
);
1633 error ("Invalid ISO C character class");
1635 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1637 i_byte
= class_end
+ 2 - str
;
1641 not_a_class_name_multibyte
:
1644 if (i_byte
== size_byte
)
1647 leading_code
= str
[i_byte
];
1648 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1651 /* Treat `-' as range character only if another character
1653 if (i_byte
+ 1 < size_byte
1654 && str
[i_byte
] == '-')
1657 unsigned char leading_code2
;
1659 /* Skip over the dash. */
1662 /* Get the end of the range. */
1663 leading_code2
= str
[i_byte
];
1664 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1668 && i_byte
< size_byte
)
1670 leading_code2
= str
[i_byte
];
1671 c2
=STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1677 if (ASCII_CHAR_P (c
))
1679 while (c
<= c2
&& c
< 0x80)
1681 leading_code
= CHAR_LEADING_CODE (c
);
1683 if (! ASCII_CHAR_P (c
))
1685 unsigned lim2
= leading_code2
+ 1;
1686 while (leading_code
< lim2
)
1687 fastmap
[leading_code
++] = 1;
1690 char_ranges
[n_char_ranges
++] = c
;
1691 char_ranges
[n_char_ranges
++] = c2
;
1697 if (ASCII_CHAR_P (c
))
1701 fastmap
[leading_code
] = 1;
1702 char_ranges
[n_char_ranges
++] = c
;
1703 char_ranges
[n_char_ranges
++] = c
;
1708 /* If the current range is unibyte and STRING contains non-ASCII
1709 chars, arrange fastmap for the corresponding unibyte
1712 if (! multibyte
&& n_char_ranges
> 0)
1714 memset (fastmap
+ 0200, 0, 0200);
1715 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1717 int c1
= char_ranges
[i
];
1718 unsigned lim2
= char_ranges
[i
+ 1] + 1;
1720 for (; c1
< lim2
; c1
++)
1722 int b
= CHAR_TO_BYTE_SAFE (c1
);
1730 /* If ^ was the first character, complement the fastmap. */
1734 for (i
= 0; i
< sizeof fastmap
; i
++)
1738 for (i
= 0; i
< 0200; i
++)
1740 /* All non-ASCII chars possibly match. */
1741 for (; i
< sizeof fastmap
; i
++)
1747 EMACS_INT start_point
= PT
;
1749 EMACS_INT pos_byte
= PT_BYTE
;
1750 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1754 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1755 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1759 endp
= CHAR_POS_ADDR (XINT (lim
));
1760 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1764 /* This code may look up syntax tables using macros that rely on the
1765 gl_state object. To make sure this object is not out of date,
1766 let's initialize it manually.
1767 We ignore syntax-table text-properties for now, since that's
1768 what we've done in the past. */
1769 SETUP_BUFFER_SYNTAX_TABLE ();
1784 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
1785 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1795 if (! ASCII_CHAR_P (c
))
1797 /* As we are looking at a multibyte character, we
1798 must look up the character in the table
1799 CHAR_RANGES. If there's no data in the table,
1800 that character is not what we want to skip. */
1802 /* The following code do the right thing even if
1803 n_char_ranges is zero (i.e. no data in
1805 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1806 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1808 if (!(negate
^ (i
< n_char_ranges
)))
1812 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
1825 if (!NILP (iso_classes
) && in_classes (*p
, iso_classes
))
1830 goto fwd_unibyte_ok
;
1836 p
++, pos
++, pos_byte
++;
1844 unsigned char *prev_p
;
1854 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
1855 c
= STRING_CHAR (p
);
1857 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1867 if (! ASCII_CHAR_P (c
))
1869 /* See the comment in the previous similar code. */
1870 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1871 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1873 if (!(negate
^ (i
< n_char_ranges
)))
1877 pos
--, pos_byte
-= prev_p
- p
;
1890 if (! NILP (iso_classes
) && in_classes (p
[-1], iso_classes
))
1895 goto back_unibyte_ok
;
1898 if (!fastmap
[p
[-1]])
1901 p
--, pos
--, pos_byte
--;
1905 SET_PT_BOTH (pos
, pos_byte
);
1908 return make_number (PT
- start_point
);
1914 skip_syntaxes (int forwardp
, Lisp_Object string
, Lisp_Object lim
)
1916 register unsigned int c
;
1917 unsigned char fastmap
[0400];
1919 register EMACS_INT i
, i_byte
;
1921 EMACS_INT size_byte
;
1924 CHECK_STRING (string
);
1927 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1929 CHECK_NUMBER_COERCE_MARKER (lim
);
1931 /* In any case, don't allow scan outside bounds of buffer. */
1932 if (XINT (lim
) > ZV
)
1933 XSETFASTINT (lim
, ZV
);
1934 if (XINT (lim
) < BEGV
)
1935 XSETFASTINT (lim
, BEGV
);
1937 if (forwardp
? (PT
>= XFASTINT (lim
)) : (PT
<= XFASTINT (lim
)))
1938 return make_number (0);
1940 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1941 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1943 memset (fastmap
, 0, sizeof fastmap
);
1945 if (SBYTES (string
) > SCHARS (string
))
1946 /* As this is very rare case (syntax spec is ASCII only), don't
1947 consider efficiency. */
1948 string
= string_make_unibyte (string
);
1950 str
= SDATA (string
);
1951 size_byte
= SBYTES (string
);
1954 if (i_byte
< size_byte
1955 && SREF (string
, 0) == '^')
1957 negate
= 1; i_byte
++;
1960 /* Find the syntaxes specified and set their elements of fastmap. */
1962 while (i_byte
< size_byte
)
1965 fastmap
[syntax_spec_code
[c
]] = 1;
1968 /* If ^ was the first character, complement the fastmap. */
1970 for (i
= 0; i
< sizeof fastmap
; i
++)
1974 EMACS_INT start_point
= PT
;
1976 EMACS_INT pos_byte
= PT_BYTE
;
1977 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1981 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1982 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1986 endp
= CHAR_POS_ADDR (XINT (lim
));
1987 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1991 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
2007 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
2008 if (! fastmap
[(int) SYNTAX (c
)])
2010 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2011 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2025 if (! fastmap
[(int) SYNTAX (*p
)])
2027 p
++, pos
++, pos_byte
++;
2028 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2038 unsigned char *prev_p
;
2047 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2049 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2050 c
= STRING_CHAR (p
);
2051 if (! fastmap
[(int) SYNTAX (c
)])
2053 pos
--, pos_byte
-= prev_p
- p
;
2067 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2068 if (! fastmap
[(int) SYNTAX (p
[-1])])
2070 p
--, pos
--, pos_byte
--;
2075 SET_PT_BOTH (pos
, pos_byte
);
2078 return make_number (PT
- start_point
);
2082 /* Return 1 if character C belongs to one of the ISO classes
2083 in the list ISO_CLASSES. Each class is represented by an
2084 integer which is its type according to re_wctype. */
2087 in_classes (int c
, Lisp_Object iso_classes
)
2091 while (CONSP (iso_classes
))
2094 elt
= XCAR (iso_classes
);
2095 iso_classes
= XCDR (iso_classes
);
2097 if (re_iswctype (c
, XFASTINT (elt
)))
2104 /* Jump over a comment, assuming we are at the beginning of one.
2105 FROM is the current position.
2106 FROM_BYTE is the bytepos corresponding to FROM.
2107 Do not move past STOP (a charpos).
2108 The comment over which we have to jump is of style STYLE
2109 (either SYNTAX_FLAGS_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2110 NESTING should be positive to indicate the nesting at the beginning
2111 for nested comments and should be zero or negative else.
2112 ST_COMMENT_STYLE cannot be nested.
2113 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2114 (or 0 If the search cannot start in the middle of a two-character).
2116 If successful, return 1 and store the charpos of the comment's end
2117 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2118 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2119 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2120 (as defined for state.incomment) in *INCOMMENT_PTR.
2122 The comment end is the last character of the comment rather than the
2123 character just after the comment.
2125 Global syntax data is assumed to initially be valid for FROM and
2126 remains valid for forward search starting at the returned position. */
2129 forw_comment (EMACS_INT from
, EMACS_INT from_byte
, EMACS_INT stop
,
2130 int nesting
, int style
, int prev_syntax
,
2131 EMACS_INT
*charpos_ptr
, EMACS_INT
*bytepos_ptr
,
2135 register enum syntaxcode code
;
2136 register int syntax
, other_syntax
;
2138 if (nesting
<= 0) nesting
= -1;
2140 /* Enter the loop in the middle so that we find
2141 a 2-char comment ender if we start in the middle of it. */
2142 syntax
= prev_syntax
;
2143 if (syntax
!= 0) goto forw_incomment
;
2149 *incomment_ptr
= nesting
;
2150 *charpos_ptr
= from
;
2151 *bytepos_ptr
= from_byte
;
2154 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2155 syntax
= SYNTAX_WITH_FLAGS (c
);
2156 code
= syntax
& 0xff;
2157 if (code
== Sendcomment
2158 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
2159 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
2160 (nesting
> 0 && --nesting
== 0) : nesting
< 0))
2161 /* we have encountered a comment end of the same style
2162 as the comment sequence which began this comment
2165 if (code
== Scomment_fence
2166 && style
== ST_COMMENT_STYLE
)
2167 /* we have encountered a comment end of the same style
2168 as the comment sequence which began this comment
2173 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
2174 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
)
2175 /* we have encountered a nested comment of the same style
2176 as the comment sequence which began this comment section */
2178 INC_BOTH (from
, from_byte
);
2179 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2182 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2183 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2184 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2185 SYNTAX_FLAGS_COMEND_SECOND (other_syntax
))
2186 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, other_syntax
) == style
2187 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2188 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
))
2189 ? nesting
> 0 : nesting
< 0))
2192 /* we have encountered a comment end of the same style
2193 as the comment sequence which began this comment
2198 INC_BOTH (from
, from_byte
);
2199 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2204 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
2205 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2206 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2207 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
) == style
2208 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2209 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2210 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
)))
2211 /* we have encountered a nested comment of the same style
2212 as the comment sequence which began this comment
2215 INC_BOTH (from
, from_byte
);
2216 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2220 *charpos_ptr
= from
;
2221 *bytepos_ptr
= from_byte
;
2225 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
2227 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2228 Stop scanning if we find something other than a comment or whitespace.
2229 Set point to where scanning stops.
2230 If COUNT comments are found as expected, with nothing except whitespace
2231 between them, return t; otherwise return nil. */)
2234 register EMACS_INT from
;
2235 EMACS_INT from_byte
;
2236 register EMACS_INT stop
;
2238 register enum syntaxcode code
;
2239 int comstyle
= 0; /* style of comment encountered */
2240 int comnested
= 0; /* whether the comment is nestable or not */
2243 EMACS_INT out_charpos
, out_bytepos
;
2246 CHECK_NUMBER (count
);
2247 count1
= XINT (count
);
2248 stop
= count1
> 0 ? ZV
: BEGV
;
2254 from_byte
= PT_BYTE
;
2256 SETUP_SYNTAX_TABLE (from
, count1
);
2261 int comstart_first
, syntax
, other_syntax
;
2265 SET_PT_BOTH (from
, from_byte
);
2269 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2270 syntax
= SYNTAX_WITH_FLAGS (c
);
2272 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2273 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2274 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2275 INC_BOTH (from
, from_byte
);
2276 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2277 if (from
< stop
&& comstart_first
2278 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2279 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2280 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
)))
2282 /* We have encountered a comment start sequence and we
2283 are ignoring all text inside comments. We must record
2284 the comment style this sequence begins so that later,
2285 only a comment end of the same style actually ends
2286 the comment section. */
2288 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2290 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2291 INC_BOTH (from
, from_byte
);
2292 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2295 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
2297 if (code
== Scomment_fence
)
2298 comstyle
= ST_COMMENT_STYLE
;
2299 else if (code
!= Scomment
)
2302 DEC_BOTH (from
, from_byte
);
2303 SET_PT_BOTH (from
, from_byte
);
2306 /* We're at the start of a comment. */
2307 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
2308 &out_charpos
, &out_bytepos
, &dummy
);
2309 from
= out_charpos
; from_byte
= out_bytepos
;
2313 SET_PT_BOTH (from
, from_byte
);
2316 INC_BOTH (from
, from_byte
);
2317 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2318 /* We have skipped one comment. */
2330 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
2335 DEC_BOTH (from
, from_byte
);
2336 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2337 quoted
= char_quoted (from
, from_byte
);
2338 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2339 syntax
= SYNTAX_WITH_FLAGS (c
);
2342 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2343 if (code
== Sendcomment
)
2344 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2345 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2346 && prev_char_comend_first (from
, from_byte
)
2347 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
2350 /* We must record the comment style encountered so that
2351 later, we can match only the proper comment begin
2352 sequence of the same style. */
2353 DEC_BOTH (from
, from_byte
);
2355 /* Calling char_quoted, above, set up global syntax position
2356 at the new value of FROM. */
2357 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2358 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2359 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2361 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2364 if (code
== Scomment_fence
)
2366 /* Skip until first preceding unquoted comment_fence. */
2367 int fence_found
= 0;
2368 EMACS_INT ini
= from
, ini_byte
= from_byte
;
2372 DEC_BOTH (from
, from_byte
);
2373 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2374 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2375 if (SYNTAX (c
) == Scomment_fence
2376 && !char_quoted (from
, from_byte
))
2381 else if (from
== stop
)
2384 if (fence_found
== 0)
2386 from
= ini
; /* Set point to ini + 1. */
2387 from_byte
= ini_byte
;
2391 /* We have skipped one comment. */
2394 else if (code
== Sendcomment
)
2396 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2397 &out_charpos
, &out_bytepos
);
2401 /* This end-of-line is not an end-of-comment.
2402 Treat it like a whitespace.
2403 CC-mode (and maybe others) relies on this behavior. */
2407 /* Failure: we should go back to the end of this
2408 not-quite-endcomment. */
2409 if (SYNTAX (c
) != code
)
2410 /* It was a two-char Sendcomment. */
2411 INC_BOTH (from
, from_byte
);
2417 /* We have skipped one comment. */
2418 from
= out_charpos
, from_byte
= out_bytepos
;
2422 else if (code
!= Swhitespace
|| quoted
)
2426 INC_BOTH (from
, from_byte
);
2427 SET_PT_BOTH (from
, from_byte
);
2435 SET_PT_BOTH (from
, from_byte
);
2440 /* Return syntax code of character C if C is an ASCII character
2441 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2443 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2444 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2445 ? SYNTAX (c) : Ssymbol)
2448 scan_lists (register EMACS_INT from
, EMACS_INT count
, EMACS_INT depth
, int sexpflag
)
2451 register EMACS_INT stop
= count
> 0 ? ZV
: BEGV
;
2456 register enum syntaxcode code
, temp_code
;
2457 int min_depth
= depth
; /* Err out if depth gets less than this. */
2458 int comstyle
= 0; /* style of comment encountered */
2459 int comnested
= 0; /* whether the comment is nestable or not */
2461 EMACS_INT last_good
= from
;
2463 EMACS_INT from_byte
;
2464 EMACS_INT out_bytepos
, out_charpos
;
2466 int multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2468 if (depth
> 0) min_depth
= 0;
2470 if (from
> ZV
) from
= ZV
;
2471 if (from
< BEGV
) from
= BEGV
;
2473 from_byte
= CHAR_TO_BYTE (from
);
2478 SETUP_SYNTAX_TABLE (from
, count
);
2483 int comstart_first
, prefix
, syntax
, other_syntax
;
2484 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2485 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2486 syntax
= SYNTAX_WITH_FLAGS (c
);
2487 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2488 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2489 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2490 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2491 prefix
= SYNTAX_FLAGS_PREFIX (syntax
);
2492 if (depth
== min_depth
)
2494 INC_BOTH (from
, from_byte
);
2495 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2496 if (from
< stop
&& comstart_first
2497 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2498 other_syntax
= SYNTAX_WITH_FLAGS (c
),
2499 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2500 && parse_sexp_ignore_comments
)
2502 /* we have encountered a comment start sequence and we
2503 are ignoring all text inside comments. We must record
2504 the comment style this sequence begins so that later,
2505 only a comment end of the same style actually ends
2506 the comment section */
2508 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2510 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2511 INC_BOTH (from
, from_byte
);
2512 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2518 switch (SWITCH_ENUM_CAST (code
))
2524 INC_BOTH (from
, from_byte
);
2525 /* treat following character as a word constituent */
2528 if (depth
|| !sexpflag
) break;
2529 /* This word counts as a sexp; return at end of it. */
2532 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2534 /* Some compilers can't handle this inside the switch. */
2535 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2536 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2541 INC_BOTH (from
, from_byte
);
2552 INC_BOTH (from
, from_byte
);
2556 case Scomment_fence
:
2557 comstyle
= ST_COMMENT_STYLE
;
2560 if (!parse_sexp_ignore_comments
) break;
2561 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2562 found
= forw_comment (from
, from_byte
, stop
,
2563 comnested
, comstyle
, 0,
2564 &out_charpos
, &out_bytepos
, &dummy
);
2565 from
= out_charpos
, from_byte
= out_bytepos
;
2572 INC_BOTH (from
, from_byte
);
2573 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2579 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (from_byte
))
2581 INC_BOTH (from
, from_byte
);
2591 if (!++depth
) goto done
;
2596 if (!--depth
) goto done
;
2597 if (depth
< min_depth
)
2598 xsignal3 (Qscan_error
,
2599 build_string ("Containing expression ends prematurely"),
2600 make_number (last_good
), make_number (from
));
2605 temp_pos
= dec_bytepos (from_byte
);
2606 stringterm
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2611 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2612 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2615 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2616 : SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring_fence
)
2619 /* Some compilers can't handle this inside the switch. */
2620 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2625 INC_BOTH (from
, from_byte
);
2627 INC_BOTH (from
, from_byte
);
2629 INC_BOTH (from
, from_byte
);
2630 if (!depth
&& sexpflag
) goto done
;
2633 /* Ignore whitespace, punctuation, quote, endcomment. */
2638 /* Reached end of buffer. Error if within object, return nil if between */
2645 /* End of object reached */
2655 DEC_BOTH (from
, from_byte
);
2656 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2657 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2658 syntax
= SYNTAX_WITH_FLAGS (c
);
2659 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2660 if (depth
== min_depth
)
2663 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2664 if (code
== Sendcomment
)
2665 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2666 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2667 && prev_char_comend_first (from
, from_byte
)
2668 && parse_sexp_ignore_comments
)
2670 /* We must record the comment style encountered so that
2671 later, we can match only the proper comment begin
2672 sequence of the same style. */
2673 int c2
, other_syntax
;
2674 DEC_BOTH (from
, from_byte
);
2675 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2677 c2
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2678 other_syntax
= SYNTAX_WITH_FLAGS (c2
);
2679 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2681 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2684 /* Quoting turns anything except a comment-ender
2685 into a word character. Note that this cannot be true
2686 if we decremented FROM in the if-statement above. */
2687 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2689 DEC_BOTH (from
, from_byte
);
2692 else if (SYNTAX_FLAGS_PREFIX (syntax
))
2695 switch (SWITCH_ENUM_CAST (code
))
2701 if (depth
|| !sexpflag
) break;
2702 /* This word counts as a sexp; count object finished
2703 after passing it. */
2706 temp_pos
= from_byte
;
2707 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
2711 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2712 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2713 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2714 /* Don't allow comment-end to be quoted. */
2715 if (temp_code
== Sendcomment
)
2717 quoted
= char_quoted (from
- 1, temp_pos
);
2720 DEC_BOTH (from
, from_byte
);
2721 temp_pos
= dec_bytepos (temp_pos
);
2722 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2724 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2725 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2726 if (! (quoted
|| temp_code
== Sword
2727 || temp_code
== Ssymbol
2728 || temp_code
== Squote
))
2730 DEC_BOTH (from
, from_byte
);
2737 temp_pos
= dec_bytepos (from_byte
);
2738 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2739 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (temp_pos
))
2740 DEC_BOTH (from
, from_byte
);
2749 if (!++depth
) goto done2
;
2754 if (!--depth
) goto done2
;
2755 if (depth
< min_depth
)
2756 xsignal3 (Qscan_error
,
2757 build_string ("Containing expression ends prematurely"),
2758 make_number (last_good
), make_number (from
));
2762 if (!parse_sexp_ignore_comments
)
2764 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2765 &out_charpos
, &out_bytepos
);
2766 /* FIXME: if found == -1, then it really wasn't a comment-end.
2767 For single-char Sendcomment, we can't do much about it apart
2768 from skipping the char.
2769 For 2-char endcomments, we could try again, taking both
2770 chars as separate entities, but it's a lot of trouble
2771 for very little gain, so we don't bother either. -sm */
2773 from
= out_charpos
, from_byte
= out_bytepos
;
2776 case Scomment_fence
:
2782 DEC_BOTH (from
, from_byte
);
2783 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2784 if (!char_quoted (from
, from_byte
)
2785 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2786 SYNTAX_WITH_MULTIBYTE_CHECK (c
) == code
))
2789 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2793 stringterm
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2798 DEC_BOTH (from
, from_byte
);
2799 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2800 if (!char_quoted (from
, from_byte
)
2802 == (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
)))
2803 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2806 if (!depth
&& sexpflag
) goto done2
;
2809 /* Ignore whitespace, punctuation, quote, endcomment. */
2814 /* Reached start of buffer. Error if within object, return nil if between */
2827 XSETFASTINT (val
, from
);
2831 xsignal3 (Qscan_error
,
2832 build_string ("Unbalanced parentheses"),
2833 make_number (last_good
), make_number (from
));
2836 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
2837 doc
: /* Scan from character number FROM by COUNT lists.
2838 Returns the character number of the position thus found.
2840 If DEPTH is nonzero, paren depth begins counting from that value,
2841 only places where the depth in parentheses becomes zero
2842 are candidates for stopping; COUNT such places are counted.
2843 Thus, a positive value for DEPTH means go out levels.
2845 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2847 If the beginning or end of (the accessible part of) the buffer is reached
2848 and the depth is wrong, an error is signaled.
2849 If the depth is right but the count is not used up, nil is returned. */)
2850 (Lisp_Object from
, Lisp_Object count
, Lisp_Object depth
)
2852 CHECK_NUMBER (from
);
2853 CHECK_NUMBER (count
);
2854 CHECK_NUMBER (depth
);
2856 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
2859 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
2860 doc
: /* Scan from character number FROM by COUNT balanced expressions.
2861 If COUNT is negative, scan backwards.
2862 Returns the character number of the position thus found.
2864 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2866 If the beginning or end of (the accessible part of) the buffer is reached
2867 in the middle of a parenthetical grouping, an error is signaled.
2868 If the beginning or end is reached between groupings
2869 but before count is used up, nil is returned. */)
2870 (Lisp_Object from
, Lisp_Object count
)
2872 CHECK_NUMBER (from
);
2873 CHECK_NUMBER (count
);
2875 return scan_lists (XINT (from
), XINT (count
), 0, 1);
2878 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
2880 doc
: /* Move point backward over any number of chars with prefix syntax.
2881 This includes chars with "quote" or "prefix" syntax (' or p). */)
2884 EMACS_INT beg
= BEGV
;
2885 EMACS_INT opoint
= PT
;
2886 EMACS_INT opoint_byte
= PT_BYTE
;
2888 EMACS_INT pos_byte
= PT_BYTE
;
2893 SET_PT_BOTH (opoint
, opoint_byte
);
2898 SETUP_SYNTAX_TABLE (pos
, -1);
2900 DEC_BOTH (pos
, pos_byte
);
2902 while (!char_quoted (pos
, pos_byte
)
2903 /* Previous statement updates syntax table. */
2904 && ((c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
), SYNTAX (c
) == Squote
)
2905 || SYNTAX_PREFIX (c
)))
2908 opoint_byte
= pos_byte
;
2911 DEC_BOTH (pos
, pos_byte
);
2914 SET_PT_BOTH (opoint
, opoint_byte
);
2919 /* Parse forward from FROM / FROM_BYTE to END,
2920 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2921 and return a description of the state of the parse at END.
2922 If STOPBEFORE is nonzero, stop at the start of an atom.
2923 If COMMENTSTOP is 1, stop at the start of a comment.
2924 If COMMENTSTOP is -1, stop at the start or end of a comment,
2925 after the beginning of a string, or after the end of a string. */
2928 scan_sexps_forward (struct lisp_parse_state
*stateptr
,
2929 EMACS_INT from
, EMACS_INT from_byte
, EMACS_INT end
,
2930 int targetdepth
, int stopbefore
,
2931 Lisp_Object oldstate
, int commentstop
)
2933 struct lisp_parse_state state
;
2935 register enum syntaxcode code
;
2938 struct level
{ int last
, prev
; };
2939 struct level levelstart
[100];
2940 register struct level
*curlevel
= levelstart
;
2941 struct level
*endlevel
= levelstart
+ 100;
2942 register int depth
; /* Paren depth of current scanning location.
2943 level - levelstart equals this except
2944 when the depth becomes negative. */
2945 int mindepth
; /* Lowest DEPTH value seen. */
2946 int start_quoted
= 0; /* Nonzero means starting after a char quote */
2948 EMACS_INT prev_from
; /* Keep one character before FROM. */
2949 EMACS_INT prev_from_byte
;
2950 int prev_from_syntax
;
2951 int boundary_stop
= commentstop
== -1;
2954 EMACS_INT out_bytepos
, out_charpos
;
2958 prev_from_byte
= from_byte
;
2960 DEC_BOTH (prev_from
, prev_from_byte
);
2962 /* Use this macro instead of `from++'. */
2964 do { prev_from = from; \
2965 prev_from_byte = from_byte; \
2966 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2967 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2968 INC_BOTH (from, from_byte); \
2970 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2976 if (NILP (oldstate
))
2979 state
.instring
= -1;
2980 state
.incomment
= 0;
2981 state
.comstyle
= 0; /* comment style a by default. */
2982 state
.comstr_start
= -1; /* no comment/string seen. */
2986 tem
= Fcar (oldstate
);
2992 oldstate
= Fcdr (oldstate
);
2993 oldstate
= Fcdr (oldstate
);
2994 oldstate
= Fcdr (oldstate
);
2995 tem
= Fcar (oldstate
);
2996 /* Check whether we are inside string_fence-style string: */
2997 state
.instring
= (!NILP (tem
)
2998 ? (INTEGERP (tem
) ? XINT (tem
) : ST_STRING_STYLE
)
3001 oldstate
= Fcdr (oldstate
);
3002 tem
= Fcar (oldstate
);
3003 state
.incomment
= (!NILP (tem
)
3004 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
3007 oldstate
= Fcdr (oldstate
);
3008 tem
= Fcar (oldstate
);
3009 start_quoted
= !NILP (tem
);
3011 /* if the eighth element of the list is nil, we are in comment
3012 style a. If it is non-nil, we are in comment style b */
3013 oldstate
= Fcdr (oldstate
);
3014 oldstate
= Fcdr (oldstate
);
3015 tem
= Fcar (oldstate
);
3016 state
.comstyle
= (NILP (tem
)
3018 : (EQ (tem
, Qsyntax_table
)
3020 : INTEGERP (tem
) ? XINT (tem
) : 1));
3022 oldstate
= Fcdr (oldstate
);
3023 tem
= Fcar (oldstate
);
3024 state
.comstr_start
= NILP (tem
) ? -1 : XINT (tem
) ;
3025 oldstate
= Fcdr (oldstate
);
3026 tem
= Fcar (oldstate
);
3027 while (!NILP (tem
)) /* >= second enclosing sexps. */
3029 /* curlevel++->last ran into compiler bug on Apollo */
3030 curlevel
->last
= XINT (Fcar (tem
));
3031 if (++curlevel
== endlevel
)
3032 curlevel
--; /* error ("Nesting too deep for parser"); */
3033 curlevel
->prev
= -1;
3034 curlevel
->last
= -1;
3041 curlevel
->prev
= -1;
3042 curlevel
->last
= -1;
3044 SETUP_SYNTAX_TABLE (prev_from
, 1);
3045 temp
= FETCH_CHAR (prev_from_byte
);
3046 prev_from_syntax
= SYNTAX_WITH_FLAGS (temp
);
3047 UPDATE_SYNTAX_TABLE_FORWARD (from
);
3049 /* Enter the loop at a place appropriate for initial state. */
3051 if (state
.incomment
)
3052 goto startincomment
;
3053 if (state
.instring
>= 0)
3055 nofence
= state
.instring
!= ST_STRING_STYLE
;
3057 goto startquotedinstring
;
3060 else if (start_quoted
)
3067 code
= prev_from_syntax
& 0xff;
3070 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
)
3071 && (c1
= FETCH_CHAR (from_byte
),
3072 syntax
= SYNTAX_WITH_FLAGS (c1
),
3073 SYNTAX_FLAGS_COMSTART_SECOND (syntax
)))
3074 /* Duplicate code to avoid a complex if-expression
3075 which causes trouble for the SGI compiler. */
3077 /* Record the comment style we have entered so that only
3078 the comment-end sequence of the same style actually
3079 terminates the comment section. */
3081 = SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_from_syntax
);
3082 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
);
3083 comnested
= comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
3084 state
.incomment
= comnested
? 1 : -1;
3085 state
.comstr_start
= prev_from
;
3089 else if (code
== Scomment_fence
)
3091 /* Record the comment style we have entered so that only
3092 the comment-end sequence of the same style actually
3093 terminates the comment section. */
3094 state
.comstyle
= ST_COMMENT_STYLE
;
3095 state
.incomment
= -1;
3096 state
.comstr_start
= prev_from
;
3099 else if (code
== Scomment
)
3101 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
, 0);
3102 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
3104 state
.comstr_start
= prev_from
;
3107 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
3109 switch (SWITCH_ENUM_CAST (code
))
3113 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3114 curlevel
->last
= prev_from
;
3116 if (from
== end
) goto endquoted
;
3119 /* treat following character as a word constituent */
3122 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3123 curlevel
->last
= prev_from
;
3127 /* Some compilers can't handle this inside the switch. */
3128 temp
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3129 temp
= SYNTAX (temp
);
3135 if (from
== end
) goto endquoted
;
3147 curlevel
->prev
= curlevel
->last
;
3150 case Scomment_fence
: /* Can't happen because it's handled above. */
3152 if (commentstop
|| boundary_stop
) goto done
;
3154 /* The (from == BEGV) test was to enter the loop in the middle so
3155 that we find a 2-char comment ender even if we start in the
3156 middle of it. We don't want to do that if we're just at the
3157 beginning of the comment (think of (*) ... (*)). */
3158 found
= forw_comment (from
, from_byte
, end
,
3159 state
.incomment
, state
.comstyle
,
3160 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
3161 ? 0 : prev_from_syntax
,
3162 &out_charpos
, &out_bytepos
, &state
.incomment
);
3163 from
= out_charpos
; from_byte
= out_bytepos
;
3164 /* Beware! prev_from and friends are invalid now.
3165 Luckily, the `done' doesn't use them and the INC_FROM
3166 sets them to a sane value without looking at them. */
3167 if (!found
) goto done
;
3169 state
.incomment
= 0;
3170 state
.comstyle
= 0; /* reset the comment style */
3171 if (boundary_stop
) goto done
;
3175 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3177 /* curlevel++->last ran into compiler bug on Apollo */
3178 curlevel
->last
= prev_from
;
3179 if (++curlevel
== endlevel
)
3180 curlevel
--; /* error ("Nesting too deep for parser"); */
3181 curlevel
->prev
= -1;
3182 curlevel
->last
= -1;
3183 if (targetdepth
== depth
) goto done
;
3188 if (depth
< mindepth
)
3190 if (curlevel
!= levelstart
)
3192 curlevel
->prev
= curlevel
->last
;
3193 if (targetdepth
== depth
) goto done
;
3198 state
.comstr_start
= from
- 1;
3199 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3200 curlevel
->last
= prev_from
;
3201 state
.instring
= (code
== Sstring
3202 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte
))
3204 if (boundary_stop
) goto done
;
3207 nofence
= state
.instring
!= ST_STRING_STYLE
;
3213 if (from
>= end
) goto done
;
3214 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3215 /* Some compilers can't handle this inside the switch. */
3218 /* Check TEMP here so that if the char has
3219 a syntax-table property which says it is NOT
3220 a string character, it does not end the string. */
3221 if (nofence
&& c
== state
.instring
&& temp
== Sstring
)
3227 if (!nofence
) goto string_end
;
3232 startquotedinstring
:
3233 if (from
>= end
) goto endquoted
;
3239 state
.instring
= -1;
3240 curlevel
->prev
= curlevel
->last
;
3242 if (boundary_stop
) goto done
;
3246 /* FIXME: We should do something with it. */
3249 /* Ignore whitespace, punctuation, quote, endcomment. */
3255 stop
: /* Here if stopping before start of sexp. */
3256 from
= prev_from
; /* We have just fetched the char that starts it; */
3257 goto done
; /* but return the position before it. */
3262 state
.depth
= depth
;
3263 state
.mindepth
= mindepth
;
3264 state
.thislevelstart
= curlevel
->prev
;
3265 state
.prevlevelstart
3266 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
3267 state
.location
= from
;
3268 state
.levelstarts
= Qnil
;
3269 while (curlevel
> levelstart
)
3270 state
.levelstarts
= Fcons (make_number ((--curlevel
)->last
),
3277 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
3278 doc
: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3279 Parsing stops at TO or when certain criteria are met;
3280 point is set to where parsing stops.
3281 If fifth arg OLDSTATE is omitted or nil,
3282 parsing assumes that FROM is the beginning of a function.
3283 Value is a list of elements describing final state of parsing:
3285 1. character address of start of innermost containing list; nil if none.
3286 2. character address of start of last complete sexp terminated.
3287 3. non-nil if inside a string.
3288 (it is the character that will terminate the string,
3289 or t if the string should be terminated by a generic string delimiter.)
3290 4. nil if outside a comment, t if inside a non-nestable comment,
3291 else an integer (the current comment nesting).
3292 5. t if following a quote character.
3293 6. the minimum paren-depth encountered during this scan.
3294 7. style of comment, if any.
3295 8. character address of start of comment or string; nil if not in one.
3296 9. Intermediate data for continuation of parsing (subject to change).
3297 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3298 in parentheses becomes equal to TARGETDEPTH.
3299 Fourth arg STOPBEFORE non-nil means stop when come to
3300 any character that starts a sexp.
3301 Fifth arg OLDSTATE is a list like what this function returns.
3302 It is used to initialize the state of the parse. Elements number 1, 2, 6
3304 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3305 If it is symbol `syntax-table', stop after the start of a comment or a
3306 string, or after end of a comment or a string. */)
3307 (Lisp_Object from
, Lisp_Object to
, Lisp_Object targetdepth
, Lisp_Object stopbefore
, Lisp_Object oldstate
, Lisp_Object commentstop
)
3309 struct lisp_parse_state state
;
3312 if (!NILP (targetdepth
))
3314 CHECK_NUMBER (targetdepth
);
3315 target
= XINT (targetdepth
);
3318 target
= -100000; /* We won't reach this depth */
3320 validate_region (&from
, &to
);
3321 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
3323 target
, !NILP (stopbefore
), oldstate
,
3325 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
3327 SET_PT (state
.location
);
3329 return Fcons (make_number (state
.depth
),
3330 Fcons (state
.prevlevelstart
< 0
3331 ? Qnil
: make_number (state
.prevlevelstart
),
3332 Fcons (state
.thislevelstart
< 0
3333 ? Qnil
: make_number (state
.thislevelstart
),
3334 Fcons (state
.instring
>= 0
3335 ? (state
.instring
== ST_STRING_STYLE
3336 ? Qt
: make_number (state
.instring
)) : Qnil
,
3337 Fcons (state
.incomment
< 0 ? Qt
:
3338 (state
.incomment
== 0 ? Qnil
:
3339 make_number (state
.incomment
)),
3340 Fcons (state
.quoted
? Qt
: Qnil
,
3341 Fcons (make_number (state
.mindepth
),
3342 Fcons ((state
.comstyle
3343 ? (state
.comstyle
== ST_COMMENT_STYLE
3345 : make_number (state
.comstyle
))
3347 Fcons (((state
.incomment
3348 || (state
.instring
>= 0))
3349 ? make_number (state
.comstr_start
)
3351 Fcons (state
.levelstarts
, Qnil
))))))))));
3355 init_syntax_once (void)
3360 /* This has to be done here, before we call Fmake_char_table. */
3361 Qsyntax_table
= intern_c_string ("syntax-table");
3362 staticpro (&Qsyntax_table
);
3364 /* Intern_C_String this now in case it isn't already done.
3365 Setting this variable twice is harmless.
3366 But don't staticpro it here--that is done in alloc.c. */
3367 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
3369 /* Create objects which can be shared among syntax tables. */
3370 Vsyntax_code_object
= Fmake_vector (make_number (Smax
), Qnil
);
3371 for (i
= 0; i
< XVECTOR (Vsyntax_code_object
)->size
; i
++)
3372 XVECTOR (Vsyntax_code_object
)->contents
[i
]
3373 = Fcons (make_number (i
), Qnil
);
3375 /* Now we are ready to set up this property, so we can
3376 create syntax tables. */
3377 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
3379 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Swhitespace
];
3381 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
3383 /* Control characters should not be whitespace. */
3384 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Spunct
];
3385 for (i
= 0; i
<= ' ' - 1; i
++)
3386 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3387 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 0177, temp
);
3389 /* Except that a few really are whitespace. */
3390 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Swhitespace
];
3391 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ' ', temp
);
3392 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\t', temp
);
3393 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\n', temp
);
3394 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 015, temp
);
3395 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 014, temp
);
3397 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
3398 for (i
= 'a'; i
<= 'z'; i
++)
3399 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3400 for (i
= 'A'; i
<= 'Z'; i
++)
3401 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3402 for (i
= '0'; i
<= '9'; i
++)
3403 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3405 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
3406 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
3408 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
3409 Fcons (make_number (Sopen
), make_number (')')));
3410 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
3411 Fcons (make_number (Sclose
), make_number ('(')));
3412 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
3413 Fcons (make_number (Sopen
), make_number (']')));
3414 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
3415 Fcons (make_number (Sclose
), make_number ('[')));
3416 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
3417 Fcons (make_number (Sopen
), make_number ('}')));
3418 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
3419 Fcons (make_number (Sclose
), make_number ('{')));
3420 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
3421 Fcons (make_number ((int) Sstring
), Qnil
));
3422 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
3423 Fcons (make_number ((int) Sescape
), Qnil
));
3425 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Ssymbol
];
3426 for (i
= 0; i
< 10; i
++)
3428 c
= "_-+*/&|<>="[i
];
3429 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3432 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Spunct
];
3433 for (i
= 0; i
< 12; i
++)
3435 c
= ".,;:?!#@~^'`"[i
];
3436 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3439 /* All multibyte characters have syntax `word' by default. */
3440 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
3441 char_table_set_range (Vstandard_syntax_table
, 0x80, MAX_CHAR
, temp
);
3445 syms_of_syntax (void)
3447 Qsyntax_table_p
= intern_c_string ("syntax-table-p");
3448 staticpro (&Qsyntax_table_p
);
3450 staticpro (&Vsyntax_code_object
);
3452 staticpro (&gl_state
.object
);
3453 staticpro (&gl_state
.global_code
);
3454 staticpro (&gl_state
.current_syntax_table
);
3455 staticpro (&gl_state
.old_prop
);
3457 /* Defined in regex.c */
3458 staticpro (&re_match_object
);
3460 Qscan_error
= intern_c_string ("scan-error");
3461 staticpro (&Qscan_error
);
3462 Fput (Qscan_error
, Qerror_conditions
,
3463 pure_cons (Qscan_error
, pure_cons (Qerror
, Qnil
)));
3464 Fput (Qscan_error
, Qerror_message
,
3465 make_pure_c_string ("Scan error"));
3467 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments
,
3468 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3470 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties
,
3471 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3472 Otherwise, that text property is simply ignored.
3473 See the info node `(elisp)Syntax Properties' for a description of the
3474 `syntax-table' property. */);
3476 words_include_escapes
= 0;
3477 DEFVAR_BOOL ("words-include-escapes", words_include_escapes
,
3478 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3480 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol
,
3481 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3482 multibyte_syntax_as_symbol
= 0;
3484 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3485 open_paren_in_column_0_is_defun_start
,
3486 doc
: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3487 open_paren_in_column_0_is_defun_start
= 1;
3490 DEFVAR_LISP ("find-word-boundary-function-table",
3491 Vfind_word_boundary_function_table
,
3493 Char table of functions to search for the word boundary.
3494 Each function is called with two arguments; POS and LIMIT.
3495 POS and LIMIT are character positions in the current buffer.
3497 If POS is less than LIMIT, POS is at the first character of a word,
3498 and the return value of a function is a position after the last
3499 character of that word.
3501 If POS is not less than LIMIT, POS is at the last character of a word,
3502 and the return value of a function is a position at the first
3503 character of that word.
3505 In both cases, LIMIT bounds the search. */);
3506 Vfind_word_boundary_function_table
= Fmake_char_table (Qnil
, Qnil
);
3508 defsubr (&Ssyntax_table_p
);
3509 defsubr (&Ssyntax_table
);
3510 defsubr (&Sstandard_syntax_table
);
3511 defsubr (&Scopy_syntax_table
);
3512 defsubr (&Sset_syntax_table
);
3513 defsubr (&Schar_syntax
);
3514 defsubr (&Smatching_paren
);
3515 defsubr (&Sstring_to_syntax
);
3516 defsubr (&Smodify_syntax_entry
);
3517 defsubr (&Sinternal_describe_syntax_value
);
3519 defsubr (&Sforward_word
);
3521 defsubr (&Sskip_chars_forward
);
3522 defsubr (&Sskip_chars_backward
);
3523 defsubr (&Sskip_syntax_forward
);
3524 defsubr (&Sskip_syntax_backward
);
3526 defsubr (&Sforward_comment
);
3527 defsubr (&Sscan_lists
);
3528 defsubr (&Sscan_sexps
);
3529 defsubr (&Sbackward_prefix_chars
);
3530 defsubr (&Sparse_partial_sexp
);