1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993, 1994, 1995, 1997, 1998, 1999, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
28 #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 Lisp_Object Qsyntax_table_p
, Qsyntax_table
, Qscan_error
;
101 int words_include_escapes
;
102 int parse_sexp_lookup_properties
;
104 /* Nonzero means `scan-sexps' treat all multibyte characters as symbol. */
105 int multibyte_syntax_as_symbol
;
107 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
108 if not compiled with GCC. No need to mark it, since it is used
109 only very temporarily. */
110 Lisp_Object syntax_temp
;
112 /* Non-zero means an open parenthesis in column 0 is always considered
113 to be the start of a defun. Zero means an open parenthesis in
114 column 0 has no special meaning. */
116 int open_paren_in_column_0_is_defun_start
;
118 /* This is the internal form of the parse state used in parse-partial-sexp. */
120 struct lisp_parse_state
122 int depth
; /* Depth at end of parsing. */
123 int instring
; /* -1 if not within string, else desired terminator. */
124 int incomment
; /* -1 if in unnestable comment else comment nesting */
125 int comstyle
; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
126 int quoted
; /* Nonzero if just after an escape char at end of parsing */
127 int mindepth
; /* Minimum depth seen while scanning. */
128 /* Char number of most recent start-of-expression at current level */
129 EMACS_INT thislevelstart
;
130 /* Char number of start of containing expression */
131 EMACS_INT prevlevelstart
;
132 EMACS_INT location
; /* Char number at which parsing stopped. */
133 EMACS_INT comstr_start
; /* Position of last comment/string starter. */
134 Lisp_Object levelstarts
; /* Char numbers of starts-of-expression
135 of levels (starting from outermost). */
138 /* These variables are a cache for finding the start of a defun.
139 find_start_pos is the place for which the defun start was found.
140 find_start_value is the defun start position found for it.
141 find_start_value_byte is the corresponding byte position.
142 find_start_buffer is the buffer it was found in.
143 find_start_begv is the BEGV value when it was found.
144 find_start_modiff is the value of MODIFF when it was found. */
146 static EMACS_INT find_start_pos
;
147 static EMACS_INT find_start_value
;
148 static EMACS_INT find_start_value_byte
;
149 static struct buffer
*find_start_buffer
;
150 static EMACS_INT find_start_begv
;
151 static int find_start_modiff
;
154 static Lisp_Object
skip_chars (int, Lisp_Object
, Lisp_Object
, int);
155 static Lisp_Object
skip_syntaxes (int, Lisp_Object
, Lisp_Object
);
156 static Lisp_Object
scan_lists (EMACS_INT
, EMACS_INT
, EMACS_INT
, int);
157 static void scan_sexps_forward (struct lisp_parse_state
*,
158 EMACS_INT
, EMACS_INT
, EMACS_INT
, int,
159 int, Lisp_Object
, int);
160 static int in_classes (int, Lisp_Object
);
162 /* Whether the syntax of the character C has the prefix flag set. */
163 int syntax_prefix_flag_p (int c
)
165 return SYNTAX_PREFIX (c
);
168 struct gl_state_s gl_state
; /* Global state of syntax parser. */
170 INTERVAL
interval_of (int, Lisp_Object
);
171 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
172 to scan to property-change. */
174 /* Update gl_state to an appropriate interval which contains CHARPOS. The
175 sign of COUNT give the relative position of CHARPOS wrt the previously
176 valid interval. If INIT, only [be]_property fields of gl_state are
177 valid at start, the rest is filled basing on OBJECT.
179 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
180 direction than the intervals - or in an interval. We update the
181 current syntax-table basing on the property of this interval, and
182 update the interval to start further than CHARPOS - or be
183 NULL_INTERVAL. We also update lim_property to be the next value of
184 charpos to call this subroutine again - or be before/after the
185 start/end of OBJECT. */
188 update_syntax_table (int charpos
, int count
, int init
, Lisp_Object object
)
190 Lisp_Object tmp_table
;
191 int cnt
= 0, invalidate
= 1;
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
;
202 if (NULL_INTERVAL_P (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,
227 if (NULL_INTERVAL_P (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
= current_buffer
->syntax_table
;
297 while (!NULL_INTERVAL_P (i
))
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 + (NULL_INTERVAL_P (next_interval (i
)) ? 1 : 0);
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 (NULL_INTERVAL_P (i
)); /* 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 (EMACS_INT charpos
, EMACS_INT bytepos
)
350 register enum syntaxcode code
;
351 register EMACS_INT beg
= BEGV
;
352 register int quoted
= 0;
353 EMACS_INT 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 after BYTEPOS.
374 We assume that BYTEPOS is not at the end of the buffer. */
377 inc_bytepos (EMACS_INT bytepos
)
379 if (NILP (current_buffer
->enable_multibyte_characters
))
386 /* Return the bytepos one character before BYTEPOS.
387 We assume that BYTEPOS is not at the start of the buffer. */
390 dec_bytepos (EMACS_INT bytepos
)
392 if (NILP (current_buffer
->enable_multibyte_characters
))
399 /* Return a defun-start position before POS and not too far before.
400 It should be the last one before POS, or nearly the last.
402 When open_paren_in_column_0_is_defun_start is nonzero,
403 only the beginning of the buffer is treated as a defun-start.
405 We record the information about where the scan started
406 and what its result was, so that another call in the same area
407 can return the same value very quickly.
409 There is no promise at which position the global syntax data is
410 valid on return from the subroutine, so the caller should explicitly
411 update the global data. */
414 find_defun_start (EMACS_INT pos
, EMACS_INT pos_byte
)
416 EMACS_INT opoint
= PT
, opoint_byte
= PT_BYTE
;
418 if (!open_paren_in_column_0_is_defun_start
)
420 find_start_value_byte
= BEGV_BYTE
;
424 /* Use previous finding, if it's valid and applies to this inquiry. */
425 if (current_buffer
== find_start_buffer
426 /* Reuse the defun-start even if POS is a little farther on.
427 POS might be in the next defun, but that's ok.
428 Our value may not be the best possible, but will still be usable. */
429 && pos
<= find_start_pos
+ 1000
430 && pos
>= find_start_value
431 && BEGV
== find_start_begv
432 && MODIFF
== find_start_modiff
)
433 return find_start_value
;
435 /* Back up to start of line. */
436 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
438 /* We optimize syntax-table lookup for rare updates. Thus we accept
439 only those `^\s(' which are good in global _and_ text-property
441 SETUP_BUFFER_SYNTAX_TABLE ();
446 /* Open-paren at start of line means we may have found our
448 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
449 if (SYNTAX (c
) == Sopen
)
451 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
452 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
453 if (SYNTAX (c
) == Sopen
)
455 /* Now fallback to the default value. */
456 SETUP_BUFFER_SYNTAX_TABLE ();
458 /* Move to beg of previous line. */
459 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
462 /* Record what we found, for the next try. */
463 find_start_value
= PT
;
464 find_start_value_byte
= PT_BYTE
;
465 find_start_buffer
= current_buffer
;
466 find_start_modiff
= MODIFF
;
467 find_start_begv
= BEGV
;
468 find_start_pos
= pos
;
470 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
472 return find_start_value
;
475 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
478 prev_char_comend_first (int pos
, int pos_byte
)
482 DEC_BOTH (pos
, pos_byte
);
483 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
484 c
= FETCH_CHAR (pos_byte
);
485 val
= SYNTAX_COMEND_FIRST (c
);
486 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
490 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
493 * prev_char_comstart_first (pos, pos_byte)
498 * DEC_BOTH (pos, pos_byte);
499 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
500 * c = FETCH_CHAR (pos_byte);
501 * val = SYNTAX_COMSTART_FIRST (c);
502 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
506 /* Checks whether charpos FROM is at the end of a comment.
507 FROM_BYTE is the bytepos corresponding to FROM.
508 Do not move back before STOP.
510 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
513 If successful, store the charpos of the comment's beginning
514 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
516 Global syntax data remains valid for backward search starting at
517 the returned value (or at FROM, if the search was not successful). */
520 back_comment (EMACS_INT from
, EMACS_INT from_byte
, EMACS_INT stop
, int comnested
, int comstyle
, EMACS_INT
*charpos_ptr
, EMACS_INT
*bytepos_ptr
)
522 /* Look back, counting the parity of string-quotes,
523 and recording the comment-starters seen.
524 When we reach a safe place, assume that's not in a string;
525 then step the main scan to the earliest comment-starter seen
526 an even number of string quotes away from the safe place.
528 OFROM[I] is position of the earliest comment-starter seen
529 which is I+2X quotes from the comment-end.
530 PARITY is current parity of quotes from the comment end. */
531 int string_style
= -1; /* Presumed outside of any string. */
532 int string_lossage
= 0;
533 /* Not a real lossage: indicates that we have passed a matching comment
534 starter plus a non-matching comment-ender, meaning that any matching
535 comment-starter we might see later could be a false positive (hidden
536 inside another comment).
537 Test case: { a (* b } c (* d *) */
538 int comment_lossage
= 0;
539 EMACS_INT comment_end
= from
;
540 EMACS_INT comment_end_byte
= from_byte
;
541 EMACS_INT comstart_pos
= 0;
542 EMACS_INT comstart_byte
;
543 /* Place where the containing defun starts,
544 or 0 if we didn't come across it yet. */
545 EMACS_INT defun_start
= 0;
546 EMACS_INT defun_start_byte
= 0;
547 register enum syntaxcode code
;
548 int nesting
= 1; /* current comment nesting */
552 /* FIXME: A }} comment-ender style leads to incorrect behavior
553 in the case of {{ c }}} because we ignore the last two chars which are
554 assumed to be comment-enders although they aren't. */
556 /* At beginning of range to scan, we're outside of strings;
557 that determines quote parity to the comment-end. */
560 int temp_byte
, prev_syntax
;
561 int com2start
, com2end
;
564 /* Move back and examine a character. */
565 DEC_BOTH (from
, from_byte
);
566 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
568 prev_syntax
= syntax
;
569 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
570 syntax
= SYNTAX_WITH_FLAGS (c
);
573 /* Check for 2-char comment markers. */
574 com2start
= (SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
575 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax
)
577 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax
, syntax
))
578 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
579 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
);
580 com2end
= (SYNTAX_FLAGS_COMEND_FIRST (syntax
)
581 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax
));
582 comstart
= (com2start
|| code
== Scomment
);
584 /* Nasty cases with overlapping 2-char comment markers:
585 - snmp-mode: -- c -- foo -- c --
593 /* If a 2-char comment sequence partly overlaps with another,
594 we don't try to be clever. E.g. |*| in C, or }% in modes that
595 have %..\n and %{..}%. */
596 if (from
> stop
&& (com2end
|| comstart
))
598 int next
= from
, next_byte
= from_byte
, next_c
, next_syntax
;
599 DEC_BOTH (next
, next_byte
);
600 UPDATE_SYNTAX_TABLE_BACKWARD (next
);
601 next_c
= FETCH_CHAR_AS_MULTIBYTE (next_byte
);
602 next_syntax
= SYNTAX_WITH_FLAGS (next_c
);
603 if (((comstart
|| comnested
)
604 && SYNTAX_FLAGS_COMEND_SECOND (syntax
)
605 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax
))
606 || ((com2end
|| comnested
)
607 && SYNTAX_FLAGS_COMSTART_SECOND (syntax
)
609 == SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_syntax
))
610 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax
)))
612 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
615 if (com2start
&& comstart_pos
== 0)
616 /* We're looking at a comment starter. But it might be a comment
617 ender as well (see snmp-mode). The first time we see one, we
618 need to consider it as a comment starter,
619 and the subsequent times as a comment ender. */
622 /* Turn a 2-char comment sequences into the appropriate syntax. */
627 /* Ignore comment starters of a different style. */
628 else if (code
== Scomment
629 && (comstyle
!= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0)
630 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
) != comnested
))
633 /* Ignore escaped characters, except comment-enders. */
634 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
641 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
643 /* Track parity of quotes. */
644 if (string_style
== -1)
645 /* Entering a string. */
647 else if (string_style
== c
)
648 /* Leaving the string. */
651 /* If we have two kinds of string delimiters.
652 There's no way to grok this scanning backwards. */
657 /* We've already checked that it is the relevant comstyle. */
658 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
659 /* There are odd string quotes involved, so let's be careful.
660 Test case in Pascal: " { " a { " } */
665 /* Record best comment-starter so far. */
667 comstart_byte
= from_byte
;
669 else if (--nesting
<= 0)
670 /* nested comments have to be balanced, so we don't need to
671 keep looking for earlier ones. We use here the same (slightly
672 incorrect) reasoning as below: since it is followed by uniform
673 paired string quotes, this comment-start has to be outside of
674 strings, else the comment-end itself would be inside a string. */
679 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == comstyle
680 && ((com2end
&& SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
))
681 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
682 /* This is the same style of comment ender as ours. */
687 /* Anything before that can't count because it would match
688 this comment-ender rather than ours. */
689 from
= stop
; /* Break out of the loop. */
691 else if (comstart_pos
!= 0 || c
!= '\n')
692 /* We're mixing comment styles here, so we'd better be careful.
693 The (comstart_pos != 0 || c != '\n') check is not quite correct
694 (we should just always set comment_lossage), but removing it
695 would imply that any multiline comment in C would go through
696 lossage, which seems overkill.
697 The failure should only happen in the rare cases such as
703 /* Assume a defun-start point is outside of strings. */
704 if (open_paren_in_column_0_is_defun_start
706 || (temp_byte
= dec_bytepos (from_byte
),
707 FETCH_CHAR (temp_byte
) == '\n')))
710 defun_start_byte
= from_byte
;
711 from
= stop
; /* Break out of the loop. */
720 if (comstart_pos
== 0)
723 from_byte
= comment_end_byte
;
724 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
- 1);
726 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
727 or `done'), then we've found the beginning of the non-nested comment. */
728 else if (1) /* !comnested */
731 from_byte
= comstart_byte
;
732 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
736 struct lisp_parse_state state
;
738 /* We had two kinds of string delimiters mixed up
739 together. Decode this going forwards.
740 Scan fwd from a known safe place (beginning-of-defun)
741 to the one in question; this records where we
742 last passed a comment starter. */
743 /* If we did not already find the defun start, find it now. */
744 if (defun_start
== 0)
746 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
747 defun_start_byte
= find_start_value_byte
;
751 scan_sexps_forward (&state
,
752 defun_start
, defun_start_byte
,
753 comment_end
, -10000, 0, Qnil
, 0);
754 defun_start
= comment_end
;
755 if (state
.incomment
== (comnested
? 1 : -1)
756 && state
.comstyle
== comstyle
)
757 from
= state
.comstr_start
;
762 /* If comment_end is inside some other comment, maybe ours
763 is nested, so we need to try again from within the
764 surrounding comment. Example: { a (* " *) */
766 /* FIXME: We should advance by one or two chars. */
767 defun_start
= state
.comstr_start
+ 2;
768 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
771 } while (defun_start
< comment_end
);
773 from_byte
= CHAR_TO_BYTE (from
);
774 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
779 *bytepos_ptr
= from_byte
;
781 return (from
== comment_end
) ? -1 : from
;
784 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
785 doc
: /* Return t if OBJECT is a syntax table.
786 Currently, any char-table counts as a syntax table. */)
789 if (CHAR_TABLE_P (object
)
790 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
796 check_syntax_table (Lisp_Object obj
)
798 CHECK_TYPE (CHAR_TABLE_P (obj
) && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
),
799 Qsyntax_table_p
, obj
);
802 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
803 doc
: /* Return the current syntax table.
804 This is the one specified by the current buffer. */)
807 return current_buffer
->syntax_table
;
810 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
811 Sstandard_syntax_table
, 0, 0, 0,
812 doc
: /* Return the standard syntax table.
813 This is the one used for new buffers. */)
816 return Vstandard_syntax_table
;
819 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
820 doc
: /* Construct a new syntax table and return it.
821 It is a copy of the TABLE, which defaults to the standard syntax table. */)
827 check_syntax_table (table
);
829 table
= Vstandard_syntax_table
;
831 copy
= Fcopy_sequence (table
);
833 /* Only the standard syntax table should have a default element.
834 Other syntax tables should inherit from parents instead. */
835 XCHAR_TABLE (copy
)->defalt
= Qnil
;
837 /* Copied syntax tables should all have parents.
838 If we copied one with no parent, such as the standard syntax table,
839 use the standard syntax table as the copy's parent. */
840 if (NILP (XCHAR_TABLE (copy
)->parent
))
841 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
845 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
846 doc
: /* Select a new syntax table for the current buffer.
847 One argument, a syntax table. */)
851 check_syntax_table (table
);
852 current_buffer
->syntax_table
= table
;
853 /* Indicate that this buffer now has a specified syntax table. */
854 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
855 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
859 /* Convert a letter which signifies a syntax code
860 into the code it signifies.
861 This is used by modify-syntax-entry, and other things. */
863 unsigned char syntax_spec_code
[0400] =
864 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
865 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
866 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
867 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
868 (char) Swhitespace
, (char) Scomment_fence
, (char) Sstring
, 0377,
869 (char) Smath
, 0377, 0377, (char) Squote
,
870 (char) Sopen
, (char) Sclose
, 0377, 0377,
871 0377, (char) Swhitespace
, (char) Spunct
, (char) Scharquote
,
872 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
873 0377, 0377, 0377, 0377,
874 (char) Scomment
, 0377, (char) Sendcomment
, 0377,
875 (char) Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
876 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
877 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
878 0377, 0377, 0377, 0377, (char) Sescape
, 0377, 0377, (char) Ssymbol
,
879 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
880 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
881 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword
,
882 0377, 0377, 0377, 0377, (char) Sstring_fence
, 0377, 0377, 0377
885 /* Indexed by syntax code, give the letter that describes it. */
887 char syntax_code_spec
[16] =
889 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
893 /* Indexed by syntax code, give the object (cons of syntax code and
894 nil) to be stored in syntax table. Since these objects can be
895 shared among syntax tables, we generate them in advance. By
896 sharing objects, the function `describe-syntax' can give a more
898 static Lisp_Object Vsyntax_code_object
;
901 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
902 doc
: /* Return the syntax code of CHARACTER, described by a character.
903 For example, if CHARACTER is a word constituent, the
904 character `w' (119) is returned.
905 The characters that correspond to various syntax codes
906 are listed in the documentation of `modify-syntax-entry'. */)
907 (Lisp_Object character
)
910 CHECK_CHARACTER (character
);
911 char_int
= XINT (character
);
912 SETUP_BUFFER_SYNTAX_TABLE ();
913 return make_number (syntax_code_spec
[(int) SYNTAX (char_int
)]);
916 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
917 doc
: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
918 (Lisp_Object character
)
921 CHECK_NUMBER (character
);
922 char_int
= XINT (character
);
923 SETUP_BUFFER_SYNTAX_TABLE ();
924 code
= SYNTAX (char_int
);
925 if (code
== Sopen
|| code
== Sclose
)
926 return SYNTAX_MATCH (char_int
);
930 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
931 doc
: /* Convert a syntax specification STRING into syntax cell form.
932 STRING should be a string as it is allowed as argument of
933 `modify-syntax-entry'. Value is the equivalent cons cell
934 \(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
938 register const unsigned char *p
;
939 register enum syntaxcode code
;
943 CHECK_STRING (string
);
946 code
= (enum syntaxcode
) syntax_spec_code
[*p
++];
947 if (((int) code
& 0377) == 0377)
948 error ("Invalid syntax description letter: %c", p
[-1]);
950 if (code
== Sinherit
)
956 int character
= STRING_CHAR_AND_LENGTH (p
, len
);
957 XSETINT (match
, character
);
958 if (XFASTINT (match
) == ' ')
1002 if (val
< XVECTOR (Vsyntax_code_object
)->size
&& NILP (match
))
1003 return XVECTOR (Vsyntax_code_object
)->contents
[val
];
1005 /* Since we can't use a shared object, let's make a new one. */
1006 return Fcons (make_number (val
), match
);
1009 /* I really don't know why this is interactive
1010 help-form should at least be made useful whilst reading the second arg. */
1011 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
1012 "cSet syntax for character: \nsSet syntax for %s to: ",
1013 doc
: /* Set syntax for character CHAR according to string NEWENTRY.
1014 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1015 the current buffer's syntax table.
1016 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1017 in the range MIN to MAX are changed.
1018 The first character of NEWENTRY should be one of the following:
1019 Space or - whitespace syntax. w word constituent.
1020 _ symbol constituent. . punctuation.
1021 ( open-parenthesis. ) close-parenthesis.
1022 " string quote. \\ escape.
1023 $ paired delimiter. ' expression quote or prefix operator.
1024 < comment starter. > comment ender.
1025 / character-quote. @ inherit from `standard-syntax-table'.
1026 | generic string fence. ! generic comment fence.
1028 Only single-character comment start and end sequences are represented thus.
1029 Two-character sequences are represented as described below.
1030 The second character of NEWENTRY is the matching parenthesis,
1031 used only if the first character is `(' or `)'.
1032 Any additional characters are flags.
1033 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1034 1 means CHAR is the start of a two-char comment start sequence.
1035 2 means CHAR is the second character of such a sequence.
1036 3 means CHAR is the start of a two-char comment end sequence.
1037 4 means CHAR is the second character of such a sequence.
1039 There can be several orthogonal comment sequences. This is to support
1040 language modes such as C++. By default, all comment sequences are of style
1041 a, but you can set the comment sequence style to b (on the second character
1042 of a comment-start, and the first character of a comment-end sequence) and/or
1043 c (on any of its chars) using this flag:
1044 b means CHAR is part of comment sequence b.
1045 c means CHAR is part of comment sequence c.
1046 n means CHAR is part of a nestable comment sequence.
1048 p means CHAR is a prefix character for `backward-prefix-chars';
1049 such characters are treated as whitespace when they occur
1050 between expressions.
1051 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1052 (Lisp_Object c
, Lisp_Object newentry
, Lisp_Object syntax_table
)
1056 CHECK_CHARACTER_CAR (c
);
1057 CHECK_CHARACTER_CDR (c
);
1060 CHECK_CHARACTER (c
);
1062 if (NILP (syntax_table
))
1063 syntax_table
= current_buffer
->syntax_table
;
1065 check_syntax_table (syntax_table
);
1067 newentry
= Fstring_to_syntax (newentry
);
1069 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table
, c
, newentry
);
1071 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), newentry
);
1073 /* We clear the regexp cache, since character classes can now have
1074 different values from those in the compiled regexps.*/
1075 clear_regexp_cache ();
1080 /* Dump syntax table to buffer in human-readable format */
1082 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value
,
1083 Sinternal_describe_syntax_value
, 1, 1, 0,
1084 doc
: /* Insert a description of the internal syntax description SYNTAX at point. */)
1085 (Lisp_Object syntax
)
1087 register enum syntaxcode code
;
1089 char desc
, start1
, start2
, end1
, end2
, prefix
,
1090 comstyleb
, comstylec
, comnested
;
1092 Lisp_Object first
, match_lisp
, value
= syntax
;
1096 insert_string ("default");
1100 if (CHAR_TABLE_P (value
))
1102 insert_string ("deeper char-table ...");
1108 insert_string ("invalid");
1112 first
= XCAR (value
);
1113 match_lisp
= XCDR (value
);
1115 if (!INTEGERP (first
) || !(NILP (match_lisp
) || INTEGERP (match_lisp
)))
1117 insert_string ("invalid");
1121 syntax_code
= XINT (first
);
1122 code
= (enum syntaxcode
) (syntax_code
& 0377);
1123 start1
= SYNTAX_FLAGS_COMSTART_FIRST (syntax_code
);
1124 start2
= SYNTAX_FLAGS_COMSTART_SECOND (syntax_code
);;
1125 end1
= SYNTAX_FLAGS_COMEND_FIRST (syntax_code
);
1126 end2
= SYNTAX_FLAGS_COMEND_SECOND (syntax_code
);
1127 prefix
= SYNTAX_FLAGS_PREFIX (syntax_code
);
1128 comstyleb
= SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code
);
1129 comstylec
= SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code
);
1130 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax_code
);
1132 if ((int) code
< 0 || (int) code
>= (int) Smax
)
1134 insert_string ("invalid");
1137 desc
= syntax_code_spec
[(int) code
];
1139 str
[0] = desc
, str
[1] = 0;
1142 if (NILP (match_lisp
))
1145 insert_char (XINT (match_lisp
));
1166 insert_string ("\twhich means: ");
1168 switch (SWITCH_ENUM_CAST (code
))
1171 insert_string ("whitespace"); break;
1173 insert_string ("punctuation"); break;
1175 insert_string ("word"); break;
1177 insert_string ("symbol"); break;
1179 insert_string ("open"); break;
1181 insert_string ("close"); break;
1183 insert_string ("prefix"); break;
1185 insert_string ("string"); break;
1187 insert_string ("math"); break;
1189 insert_string ("escape"); break;
1191 insert_string ("charquote"); break;
1193 insert_string ("comment"); break;
1195 insert_string ("endcomment"); break;
1197 insert_string ("inherit"); break;
1198 case Scomment_fence
:
1199 insert_string ("comment fence"); break;
1201 insert_string ("string fence"); break;
1203 insert_string ("invalid");
1207 if (!NILP (match_lisp
))
1209 insert_string (", matches ");
1210 insert_char (XINT (match_lisp
));
1214 insert_string (",\n\t is the first character of a comment-start sequence");
1216 insert_string (",\n\t is the second character of a comment-start sequence");
1219 insert_string (",\n\t is the first character of a comment-end sequence");
1221 insert_string (",\n\t is the second character of a comment-end sequence");
1223 insert_string (" (comment style b)");
1225 insert_string (" (comment style c)");
1227 insert_string (" (nestable)");
1230 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1235 int parse_sexp_ignore_comments
;
1237 /* Char-table of functions that find the next or previous word
1239 Lisp_Object Vfind_word_boundary_function_table
;
1241 /* Return the position across COUNT words from FROM.
1242 If that many words cannot be found before the end of the buffer, return 0.
1243 COUNT negative means scan backward and stop at word beginning. */
1246 scan_words (register int from
, register int count
)
1248 register int beg
= BEGV
;
1249 register int end
= ZV
;
1250 register int from_byte
= CHAR_TO_BYTE (from
);
1251 register enum syntaxcode code
;
1253 Lisp_Object func
, script
, pos
;
1258 SETUP_SYNTAX_TABLE (from
, count
);
1269 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1270 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1271 code
= SYNTAX (ch0
);
1272 INC_BOTH (from
, from_byte
);
1273 if (words_include_escapes
1274 && (code
== Sescape
|| code
== Scharquote
))
1279 /* Now CH0 is a character which begins a word and FROM is the
1280 position of the next character. */
1281 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch0
);
1282 if (! NILP (Ffboundp (func
)))
1284 pos
= call2 (func
, make_number (from
- 1), make_number (end
));
1285 if (INTEGERP (pos
) && XINT (pos
) > from
)
1288 from_byte
= CHAR_TO_BYTE (from
);
1293 script
= CHAR_TABLE_REF (Vchar_script_table
, ch0
);
1296 if (from
== end
) break;
1297 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1298 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1299 code
= SYNTAX (ch1
);
1301 && (! words_include_escapes
1302 || (code
!= Sescape
&& code
!= Scharquote
)))
1303 || word_boundary_p (ch0
, ch1
))
1305 INC_BOTH (from
, from_byte
);
1320 DEC_BOTH (from
, from_byte
);
1321 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1322 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1323 code
= SYNTAX (ch1
);
1324 if (words_include_escapes
1325 && (code
== Sescape
|| code
== Scharquote
))
1330 /* Now CH1 is a character which ends a word and FROM is the
1332 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch1
);
1333 if (! NILP (Ffboundp (func
)))
1335 pos
= call2 (func
, make_number (from
), make_number (beg
));
1336 if (INTEGERP (pos
) && XINT (pos
) < from
)
1339 from_byte
= CHAR_TO_BYTE (from
);
1344 script
= CHAR_TABLE_REF (Vchar_script_table
, ch1
);
1349 DEC_BOTH (from
, from_byte
);
1350 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1351 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1352 code
= SYNTAX (ch0
);
1354 && (! words_include_escapes
1355 || (code
!= Sescape
&& code
!= Scharquote
)))
1356 || word_boundary_p (ch0
, ch1
))
1358 INC_BOTH (from
, from_byte
);
1372 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 0, 1, "^p",
1373 doc
: /* Move point forward ARG words (backward if ARG is negative).
1375 If an edge of the buffer or a field boundary is reached, point is left there
1376 and the function returns nil. Field boundaries are not noticed if
1377 `inhibit-field-text-motion' is non-nil. */)
1384 XSETFASTINT (arg
, 1);
1388 val
= orig_val
= scan_words (PT
, XINT (arg
));
1390 val
= XINT (arg
) > 0 ? ZV
: BEGV
;
1392 /* Avoid jumping out of an input field. */
1393 tmp
= Fconstrain_to_field (make_number (val
), make_number (PT
),
1395 val
= XFASTINT (tmp
);
1398 return val
== orig_val
? Qt
: Qnil
;
1401 Lisp_Object
skip_chars (int, Lisp_Object
, Lisp_Object
, int);
1403 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1404 doc
: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1405 STRING is like the inside of a `[...]' in a regular expression
1406 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1407 (but not at the end of a range; quoting is never needed there).
1408 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1409 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1410 Char classes, e.g. `[:alpha:]', are supported.
1412 Returns the distance traveled, either zero or positive. */)
1413 (Lisp_Object string
, Lisp_Object lim
)
1415 return skip_chars (1, string
, lim
, 1);
1418 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1419 doc
: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1420 See `skip-chars-forward' for details.
1421 Returns the distance traveled, either zero or negative. */)
1422 (Lisp_Object string
, Lisp_Object lim
)
1424 return skip_chars (0, string
, lim
, 1);
1427 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1428 doc
: /* Move point forward across chars in specified syntax classes.
1429 SYNTAX is a string of syntax code characters.
1430 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1431 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1432 This function returns the distance traveled, either zero or positive. */)
1433 (Lisp_Object syntax
, Lisp_Object lim
)
1435 return skip_syntaxes (1, syntax
, lim
);
1438 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1439 doc
: /* Move point backward across chars in specified syntax classes.
1440 SYNTAX is a string of syntax code characters.
1441 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1442 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1443 This function returns the distance traveled, either zero or negative. */)
1444 (Lisp_Object syntax
, Lisp_Object lim
)
1446 return skip_syntaxes (0, syntax
, lim
);
1450 skip_chars (int forwardp
, Lisp_Object string
, Lisp_Object lim
, int handle_iso_classes
)
1452 register unsigned int c
;
1453 unsigned char fastmap
[0400];
1454 /* Store the ranges of non-ASCII characters. */
1456 int n_char_ranges
= 0;
1458 register int i
, i_byte
;
1459 /* Set to 1 if the current buffer is multibyte and the region
1460 contains non-ASCII chars. */
1462 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1464 int string_multibyte
;
1466 const unsigned char *str
;
1468 Lisp_Object iso_classes
;
1470 CHECK_STRING (string
);
1474 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1476 CHECK_NUMBER_COERCE_MARKER (lim
);
1478 /* In any case, don't allow scan outside bounds of buffer. */
1479 if (XINT (lim
) > ZV
)
1480 XSETFASTINT (lim
, ZV
);
1481 if (XINT (lim
) < BEGV
)
1482 XSETFASTINT (lim
, BEGV
);
1484 multibyte
= (!NILP (current_buffer
->enable_multibyte_characters
)
1485 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1486 string_multibyte
= SBYTES (string
) > SCHARS (string
);
1488 memset (fastmap
, 0, sizeof fastmap
);
1490 str
= SDATA (string
);
1491 size_byte
= SBYTES (string
);
1494 if (i_byte
< size_byte
1495 && SREF (string
, 0) == '^')
1497 negate
= 1; i_byte
++;
1500 /* Find the characters specified and set their elements of fastmap.
1501 Handle backslashes and ranges specially.
1503 If STRING contains non-ASCII characters, setup char_ranges for
1504 them and use fastmap only for their leading codes. */
1506 if (! string_multibyte
)
1508 int string_has_eight_bit
= 0;
1510 /* At first setup fastmap. */
1511 while (i_byte
< size_byte
)
1515 if (handle_iso_classes
&& c
== '['
1516 && i_byte
< size_byte
1517 && str
[i_byte
] == ':')
1519 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1520 const unsigned char *class_end
= class_beg
;
1521 const unsigned char *class_limit
= str
+ size_byte
- 2;
1522 /* Leave room for the null. */
1523 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1526 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1527 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1529 while (class_end
< class_limit
1530 && *class_end
>= 'a' && *class_end
<= 'z')
1533 if (class_end
== class_beg
1534 || *class_end
!= ':' || class_end
[1] != ']')
1535 goto not_a_class_name
;
1537 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1538 class_name
[class_end
- class_beg
] = 0;
1540 cc
= re_wctype (class_name
);
1542 error ("Invalid ISO C character class");
1544 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1546 i_byte
= class_end
+ 2 - str
;
1553 if (i_byte
== size_byte
)
1558 /* Treat `-' as range character only if another character
1560 if (i_byte
+ 1 < size_byte
1561 && str
[i_byte
] == '-')
1565 /* Skip over the dash. */
1568 /* Get the end of the range. */
1571 && i_byte
< size_byte
)
1578 if (! ASCII_CHAR_P (c2
))
1579 string_has_eight_bit
= 1;
1585 if (! ASCII_CHAR_P (c
))
1586 string_has_eight_bit
= 1;
1590 /* If the current range is multibyte and STRING contains
1591 eight-bit chars, arrange fastmap and setup char_ranges for
1592 the corresponding multibyte chars. */
1593 if (multibyte
&& string_has_eight_bit
)
1595 unsigned char fastmap2
[0400];
1596 int range_start_byte
, range_start_char
;
1598 memcpy (fastmap
+ 0200, fastmap2
+ 0200, 0200);
1599 memset (fastmap
+ 0200, 0, 0200);
1600 /* We are sure that this loop stops. */
1601 for (i
= 0200; ! fastmap2
[i
]; i
++);
1602 c
= BYTE8_TO_CHAR (i
);
1603 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
1604 range_start_byte
= i
;
1605 range_start_char
= c
;
1606 char_ranges
= (int *) alloca (sizeof (int) * 128 * 2);
1607 for (i
= 129; i
< 0400; i
++)
1609 c
= BYTE8_TO_CHAR (i
);
1610 fastmap
[CHAR_LEADING_CODE (c
)] = 1;
1611 if (i
- range_start_byte
!= c
- range_start_char
)
1613 char_ranges
[n_char_ranges
++] = range_start_char
;
1614 char_ranges
[n_char_ranges
++] = ((i
- 1 - range_start_byte
)
1615 + range_start_char
);
1616 range_start_byte
= i
;
1617 range_start_char
= c
;
1620 char_ranges
[n_char_ranges
++] = range_start_char
;
1621 char_ranges
[n_char_ranges
++] = ((i
- 1 - range_start_byte
)
1622 + range_start_char
);
1625 else /* STRING is multibyte */
1627 char_ranges
= (int *) alloca (sizeof (int) * SCHARS (string
) * 2);
1629 while (i_byte
< size_byte
)
1631 unsigned char leading_code
;
1633 leading_code
= str
[i_byte
];
1634 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1637 if (handle_iso_classes
&& c
== '['
1638 && i_byte
< size_byte
1639 && STRING_CHAR (str
+ i_byte
) == ':')
1641 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1642 const unsigned char *class_end
= class_beg
;
1643 const unsigned char *class_limit
= str
+ size_byte
- 2;
1644 /* Leave room for the null. */
1645 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1648 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1649 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1651 while (class_end
< class_limit
1652 && *class_end
>= 'a' && *class_end
<= 'z')
1655 if (class_end
== class_beg
1656 || *class_end
!= ':' || class_end
[1] != ']')
1657 goto not_a_class_name_multibyte
;
1659 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1660 class_name
[class_end
- class_beg
] = 0;
1662 cc
= re_wctype (class_name
);
1664 error ("Invalid ISO C character class");
1666 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1668 i_byte
= class_end
+ 2 - str
;
1672 not_a_class_name_multibyte
:
1675 if (i_byte
== size_byte
)
1678 leading_code
= str
[i_byte
];
1679 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1682 /* Treat `-' as range character only if another character
1684 if (i_byte
+ 1 < size_byte
1685 && str
[i_byte
] == '-')
1688 unsigned char leading_code2
;
1690 /* Skip over the dash. */
1693 /* Get the end of the range. */
1694 leading_code2
= str
[i_byte
];
1695 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1699 && i_byte
< size_byte
)
1701 leading_code2
= str
[i_byte
];
1702 c2
=STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1708 if (ASCII_CHAR_P (c
))
1710 while (c
<= c2
&& c
< 0x80)
1712 leading_code
= CHAR_LEADING_CODE (c
);
1714 if (! ASCII_CHAR_P (c
))
1716 while (leading_code
<= leading_code2
)
1717 fastmap
[leading_code
++] = 1;
1720 char_ranges
[n_char_ranges
++] = c
;
1721 char_ranges
[n_char_ranges
++] = c2
;
1727 if (ASCII_CHAR_P (c
))
1731 fastmap
[leading_code
] = 1;
1732 char_ranges
[n_char_ranges
++] = c
;
1733 char_ranges
[n_char_ranges
++] = c
;
1738 /* If the current range is unibyte and STRING contains non-ASCII
1739 chars, arrange fastmap for the corresponding unibyte
1742 if (! multibyte
&& n_char_ranges
> 0)
1744 memset (fastmap
+ 0200, 0, 0200);
1745 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1747 int c1
= char_ranges
[i
];
1748 int c2
= char_ranges
[i
+ 1];
1750 for (; c1
<= c2
; c1
++)
1752 int b
= CHAR_TO_BYTE_SAFE (c1
);
1760 /* If ^ was the first character, complement the fastmap. */
1764 for (i
= 0; i
< sizeof fastmap
; i
++)
1768 for (i
= 0; i
< 0200; i
++)
1770 /* All non-ASCII chars possibly match. */
1771 for (; i
< sizeof fastmap
; i
++)
1777 int start_point
= PT
;
1779 int pos_byte
= PT_BYTE
;
1780 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1784 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1785 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1789 endp
= CHAR_POS_ADDR (XINT (lim
));
1790 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1794 /* This code may look up syntax tables using macros that rely on the
1795 gl_state object. To make sure this object is not out of date,
1796 let's initialize it manually.
1797 We ignore syntax-table text-properties for now, since that's
1798 what we've done in the past. */
1799 SETUP_BUFFER_SYNTAX_TABLE ();
1814 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
1815 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1825 if (! ASCII_CHAR_P (c
))
1827 /* As we are looking at a multibyte character, we
1828 must look up the character in the table
1829 CHAR_RANGES. If there's no data in the table,
1830 that character is not what we want to skip. */
1832 /* The following code do the right thing even if
1833 n_char_ranges is zero (i.e. no data in
1835 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1836 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1838 if (!(negate
^ (i
< n_char_ranges
)))
1842 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
1855 if (!NILP (iso_classes
) && in_classes (*p
, iso_classes
))
1860 goto fwd_unibyte_ok
;
1866 p
++, pos
++, pos_byte
++;
1874 unsigned char *prev_p
;
1884 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
1885 c
= STRING_CHAR (p
);
1887 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1897 if (! ASCII_CHAR_P (c
))
1899 /* See the comment in the previous similar code. */
1900 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1901 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1903 if (!(negate
^ (i
< n_char_ranges
)))
1907 pos
--, pos_byte
-= prev_p
- p
;
1920 if (! NILP (iso_classes
) && in_classes (p
[-1], iso_classes
))
1925 goto back_unibyte_ok
;
1928 if (!fastmap
[p
[-1]])
1931 p
--, pos
--, pos_byte
--;
1935 SET_PT_BOTH (pos
, pos_byte
);
1938 return make_number (PT
- start_point
);
1944 skip_syntaxes (int forwardp
, Lisp_Object string
, Lisp_Object lim
)
1946 register unsigned int c
;
1947 unsigned char fastmap
[0400];
1949 register int i
, i_byte
;
1954 CHECK_STRING (string
);
1957 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1959 CHECK_NUMBER_COERCE_MARKER (lim
);
1961 /* In any case, don't allow scan outside bounds of buffer. */
1962 if (XINT (lim
) > ZV
)
1963 XSETFASTINT (lim
, ZV
);
1964 if (XINT (lim
) < BEGV
)
1965 XSETFASTINT (lim
, BEGV
);
1967 if (forwardp
? (PT
>= XFASTINT (lim
)) : (PT
<= XFASTINT (lim
)))
1968 return make_number (0);
1970 multibyte
= (!NILP (current_buffer
->enable_multibyte_characters
)
1971 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1973 memset (fastmap
, 0, sizeof fastmap
);
1975 if (SBYTES (string
) > SCHARS (string
))
1976 /* As this is very rare case (syntax spec is ASCII only), don't
1977 consider efficiency. */
1978 string
= string_make_unibyte (string
);
1980 str
= SDATA (string
);
1981 size_byte
= SBYTES (string
);
1984 if (i_byte
< size_byte
1985 && SREF (string
, 0) == '^')
1987 negate
= 1; i_byte
++;
1990 /* Find the syntaxes specified and set their elements of fastmap. */
1992 while (i_byte
< size_byte
)
1995 fastmap
[syntax_spec_code
[c
]] = 1;
1998 /* If ^ was the first character, complement the fastmap. */
2000 for (i
= 0; i
< sizeof fastmap
; i
++)
2004 int start_point
= PT
;
2006 int pos_byte
= PT_BYTE
;
2007 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
2011 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
2012 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
2016 endp
= CHAR_POS_ADDR (XINT (lim
));
2017 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
2021 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
2037 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
2038 if (! fastmap
[(int) SYNTAX (c
)])
2040 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2041 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2055 if (! fastmap
[(int) SYNTAX (*p
)])
2057 p
++, pos
++, pos_byte
++;
2058 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2068 unsigned char *prev_p
;
2077 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2079 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2080 c
= STRING_CHAR (p
);
2081 if (! fastmap
[(int) SYNTAX (c
)])
2083 pos
--, pos_byte
-= prev_p
- p
;
2097 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2098 if (! fastmap
[(int) SYNTAX (p
[-1])])
2100 p
--, pos
--, pos_byte
--;
2105 SET_PT_BOTH (pos
, pos_byte
);
2108 return make_number (PT
- start_point
);
2112 /* Return 1 if character C belongs to one of the ISO classes
2113 in the list ISO_CLASSES. Each class is represented by an
2114 integer which is its type according to re_wctype. */
2117 in_classes (int c
, Lisp_Object iso_classes
)
2121 while (CONSP (iso_classes
))
2124 elt
= XCAR (iso_classes
);
2125 iso_classes
= XCDR (iso_classes
);
2127 if (re_iswctype (c
, XFASTINT (elt
)))
2134 /* Jump over a comment, assuming we are at the beginning of one.
2135 FROM is the current position.
2136 FROM_BYTE is the bytepos corresponding to FROM.
2137 Do not move past STOP (a charpos).
2138 The comment over which we have to jump is of style STYLE
2139 (either SYNTAX_FLAGS_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2140 NESTING should be positive to indicate the nesting at the beginning
2141 for nested comments and should be zero or negative else.
2142 ST_COMMENT_STYLE cannot be nested.
2143 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2144 (or 0 If the search cannot start in the middle of a two-character).
2146 If successful, return 1 and store the charpos of the comment's end
2147 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2148 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2149 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2150 (as defined for state.incomment) in *INCOMMENT_PTR.
2152 The comment end is the last character of the comment rather than the
2153 character just after the comment.
2155 Global syntax data is assumed to initially be valid for FROM and
2156 remains valid for forward search starting at the returned position. */
2159 forw_comment (EMACS_INT from
, EMACS_INT from_byte
, EMACS_INT stop
,
2160 int nesting
, int style
, int prev_syntax
,
2161 EMACS_INT
*charpos_ptr
, EMACS_INT
*bytepos_ptr
,
2165 register enum syntaxcode code
;
2166 register int syntax
, other_syntax
;
2168 if (nesting
<= 0) nesting
= -1;
2170 /* Enter the loop in the middle so that we find
2171 a 2-char comment ender if we start in the middle of it. */
2172 syntax
= prev_syntax
;
2173 if (syntax
!= 0) goto forw_incomment
;
2179 *incomment_ptr
= nesting
;
2180 *charpos_ptr
= from
;
2181 *bytepos_ptr
= from_byte
;
2184 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2185 syntax
= SYNTAX_WITH_FLAGS (c
);
2186 code
= syntax
& 0xff;
2187 if (code
== Sendcomment
2188 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
2189 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
2190 (nesting
> 0 && --nesting
== 0) : nesting
< 0))
2191 /* we have encountered a comment end of the same style
2192 as the comment sequence which began this comment
2195 if (code
== Scomment_fence
2196 && style
== ST_COMMENT_STYLE
)
2197 /* we have encountered a comment end of the same style
2198 as the comment sequence which began this comment
2203 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
2204 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
)
2205 /* we have encountered a nested comment of the same style
2206 as the comment sequence which began this comment section */
2208 INC_BOTH (from
, from_byte
);
2209 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2212 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2213 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2214 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2215 SYNTAX_FLAGS_COMEND_SECOND (other_syntax
))
2216 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, other_syntax
) == style
2217 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2218 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
))
2219 ? nesting
> 0 : nesting
< 0))
2222 /* we have encountered a comment end of the same style
2223 as the comment sequence which began this comment
2228 INC_BOTH (from
, from_byte
);
2229 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2234 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
2235 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2236 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2237 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
) == style
2238 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2239 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2240 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
)))
2241 /* we have encountered a nested comment of the same style
2242 as the comment sequence which began this comment
2245 INC_BOTH (from
, from_byte
);
2246 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2250 *charpos_ptr
= from
;
2251 *bytepos_ptr
= from_byte
;
2255 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
2257 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2258 Stop scanning if we find something other than a comment or whitespace.
2259 Set point to where scanning stops.
2260 If COUNT comments are found as expected, with nothing except whitespace
2261 between them, return t; otherwise return nil. */)
2264 register EMACS_INT from
;
2265 EMACS_INT from_byte
;
2266 register EMACS_INT stop
;
2268 register enum syntaxcode code
;
2269 int comstyle
= 0; /* style of comment encountered */
2270 int comnested
= 0; /* whether the comment is nestable or not */
2273 EMACS_INT out_charpos
, out_bytepos
;
2276 CHECK_NUMBER (count
);
2277 count1
= XINT (count
);
2278 stop
= count1
> 0 ? ZV
: BEGV
;
2284 from_byte
= PT_BYTE
;
2286 SETUP_SYNTAX_TABLE (from
, count1
);
2291 int comstart_first
, syntax
, other_syntax
;
2295 SET_PT_BOTH (from
, from_byte
);
2299 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2300 syntax
= SYNTAX_WITH_FLAGS (c
);
2302 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2303 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2304 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2305 INC_BOTH (from
, from_byte
);
2306 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2307 if (from
< stop
&& comstart_first
2308 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2309 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2310 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
)))
2312 /* We have encountered a comment start sequence and we
2313 are ignoring all text inside comments. We must record
2314 the comment style this sequence begins so that later,
2315 only a comment end of the same style actually ends
2316 the comment section. */
2318 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2320 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2321 INC_BOTH (from
, from_byte
);
2322 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2325 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
2327 if (code
== Scomment_fence
)
2328 comstyle
= ST_COMMENT_STYLE
;
2329 else if (code
!= Scomment
)
2332 DEC_BOTH (from
, from_byte
);
2333 SET_PT_BOTH (from
, from_byte
);
2336 /* We're at the start of a comment. */
2337 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
2338 &out_charpos
, &out_bytepos
, &dummy
);
2339 from
= out_charpos
; from_byte
= out_bytepos
;
2343 SET_PT_BOTH (from
, from_byte
);
2346 INC_BOTH (from
, from_byte
);
2347 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2348 /* We have skipped one comment. */
2360 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
2365 DEC_BOTH (from
, from_byte
);
2366 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2367 quoted
= char_quoted (from
, from_byte
);
2368 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2369 syntax
= SYNTAX_WITH_FLAGS (c
);
2372 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2373 if (code
== Sendcomment
)
2374 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2375 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2376 && prev_char_comend_first (from
, from_byte
)
2377 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
2380 /* We must record the comment style encountered so that
2381 later, we can match only the proper comment begin
2382 sequence of the same style. */
2383 DEC_BOTH (from
, from_byte
);
2385 /* Calling char_quoted, above, set up global syntax position
2386 at the new value of FROM. */
2387 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2388 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2389 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2391 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2394 if (code
== Scomment_fence
)
2396 /* Skip until first preceding unquoted comment_fence. */
2397 int found
= 0, ini
= from
, ini_byte
= from_byte
;
2401 DEC_BOTH (from
, from_byte
);
2402 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2403 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2404 if (SYNTAX (c
) == Scomment_fence
2405 && !char_quoted (from
, from_byte
))
2410 else if (from
== stop
)
2415 from
= ini
; /* Set point to ini + 1. */
2416 from_byte
= ini_byte
;
2420 /* We have skipped one comment. */
2423 else if (code
== Sendcomment
)
2425 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2426 &out_charpos
, &out_bytepos
);
2430 /* This end-of-line is not an end-of-comment.
2431 Treat it like a whitespace.
2432 CC-mode (and maybe others) relies on this behavior. */
2436 /* Failure: we should go back to the end of this
2437 not-quite-endcomment. */
2438 if (SYNTAX (c
) != code
)
2439 /* It was a two-char Sendcomment. */
2440 INC_BOTH (from
, from_byte
);
2446 /* We have skipped one comment. */
2447 from
= out_charpos
, from_byte
= out_bytepos
;
2451 else if (code
!= Swhitespace
|| quoted
)
2455 INC_BOTH (from
, from_byte
);
2456 SET_PT_BOTH (from
, from_byte
);
2464 SET_PT_BOTH (from
, from_byte
);
2469 /* Return syntax code of character C if C is an ASCII character
2470 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2472 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2473 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2474 ? SYNTAX (c) : Ssymbol)
2477 scan_lists (register EMACS_INT from
, EMACS_INT count
, EMACS_INT depth
, int sexpflag
)
2480 register EMACS_INT stop
= count
> 0 ? ZV
: BEGV
;
2485 register enum syntaxcode code
, temp_code
;
2486 int min_depth
= depth
; /* Err out if depth gets less than this. */
2487 int comstyle
= 0; /* style of comment encountered */
2488 int comnested
= 0; /* whether the comment is nestable or not */
2490 EMACS_INT last_good
= from
;
2492 EMACS_INT from_byte
;
2493 EMACS_INT out_bytepos
, out_charpos
;
2495 int multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2497 if (depth
> 0) min_depth
= 0;
2499 if (from
> ZV
) from
= ZV
;
2500 if (from
< BEGV
) from
= BEGV
;
2502 from_byte
= CHAR_TO_BYTE (from
);
2507 SETUP_SYNTAX_TABLE (from
, count
);
2512 int comstart_first
, prefix
, syntax
, other_syntax
;
2513 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2514 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2515 syntax
= SYNTAX_WITH_FLAGS (c
);
2516 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2517 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2518 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2519 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2520 prefix
= SYNTAX_FLAGS_PREFIX (syntax
);
2521 if (depth
== min_depth
)
2523 INC_BOTH (from
, from_byte
);
2524 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2525 if (from
< stop
&& comstart_first
2526 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2527 other_syntax
= SYNTAX_WITH_FLAGS (c
),
2528 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2529 && parse_sexp_ignore_comments
)
2531 /* we have encountered a comment start sequence and we
2532 are ignoring all text inside comments. We must record
2533 the comment style this sequence begins so that later,
2534 only a comment end of the same style actually ends
2535 the comment section */
2537 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2539 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2540 INC_BOTH (from
, from_byte
);
2541 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2547 switch (SWITCH_ENUM_CAST (code
))
2553 INC_BOTH (from
, from_byte
);
2554 /* treat following character as a word constituent */
2557 if (depth
|| !sexpflag
) break;
2558 /* This word counts as a sexp; return at end of it. */
2561 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2563 /* Some compilers can't handle this inside the switch. */
2564 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2565 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2570 INC_BOTH (from
, from_byte
);
2581 INC_BOTH (from
, from_byte
);
2585 case Scomment_fence
:
2586 comstyle
= ST_COMMENT_STYLE
;
2589 if (!parse_sexp_ignore_comments
) break;
2590 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2591 found
= forw_comment (from
, from_byte
, stop
,
2592 comnested
, comstyle
, 0,
2593 &out_charpos
, &out_bytepos
, &dummy
);
2594 from
= out_charpos
, from_byte
= out_bytepos
;
2601 INC_BOTH (from
, from_byte
);
2602 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2608 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (from_byte
))
2610 INC_BOTH (from
, from_byte
);
2620 if (!++depth
) goto done
;
2625 if (!--depth
) goto done
;
2626 if (depth
< min_depth
)
2627 xsignal3 (Qscan_error
,
2628 build_string ("Containing expression ends prematurely"),
2629 make_number (last_good
), make_number (from
));
2634 temp_pos
= dec_bytepos (from_byte
);
2635 stringterm
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2640 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2641 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2644 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2645 : SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring_fence
)
2648 /* Some compilers can't handle this inside the switch. */
2649 temp
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2654 INC_BOTH (from
, from_byte
);
2656 INC_BOTH (from
, from_byte
);
2658 INC_BOTH (from
, from_byte
);
2659 if (!depth
&& sexpflag
) goto done
;
2662 /* Ignore whitespace, punctuation, quote, endcomment. */
2667 /* Reached end of buffer. Error if within object, return nil if between */
2674 /* End of object reached */
2684 DEC_BOTH (from
, from_byte
);
2685 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2686 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2687 syntax
= SYNTAX_WITH_FLAGS (c
);
2688 code
= SYNTAX_WITH_MULTIBYTE_CHECK (c
);
2689 if (depth
== min_depth
)
2692 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2693 if (code
== Sendcomment
)
2694 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2695 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2696 && prev_char_comend_first (from
, from_byte
)
2697 && parse_sexp_ignore_comments
)
2699 /* We must record the comment style encountered so that
2700 later, we can match only the proper comment begin
2701 sequence of the same style. */
2702 int c1
, other_syntax
;
2703 DEC_BOTH (from
, from_byte
);
2704 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2706 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2707 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2708 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2710 = comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2713 /* Quoting turns anything except a comment-ender
2714 into a word character. Note that this cannot be true
2715 if we decremented FROM in the if-statement above. */
2716 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2718 DEC_BOTH (from
, from_byte
);
2721 else if (SYNTAX_FLAGS_PREFIX (syntax
))
2724 switch (SWITCH_ENUM_CAST (code
))
2730 if (depth
|| !sexpflag
) break;
2731 /* This word counts as a sexp; count object finished
2732 after passing it. */
2735 temp_pos
= from_byte
;
2736 if (! NILP (current_buffer
->enable_multibyte_characters
))
2740 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2741 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2742 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2743 /* Don't allow comment-end to be quoted. */
2744 if (temp_code
== Sendcomment
)
2746 quoted
= char_quoted (from
- 1, temp_pos
);
2749 DEC_BOTH (from
, from_byte
);
2750 temp_pos
= dec_bytepos (temp_pos
);
2751 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2753 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2754 temp_code
= SYNTAX_WITH_MULTIBYTE_CHECK (c1
);
2755 if (! (quoted
|| temp_code
== Sword
2756 || temp_code
== Ssymbol
2757 || temp_code
== Squote
))
2759 DEC_BOTH (from
, from_byte
);
2766 temp_pos
= dec_bytepos (from_byte
);
2767 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2768 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (temp_pos
))
2769 DEC_BOTH (from
, from_byte
);
2778 if (!++depth
) goto done2
;
2783 if (!--depth
) goto done2
;
2784 if (depth
< min_depth
)
2785 xsignal3 (Qscan_error
,
2786 build_string ("Containing expression ends prematurely"),
2787 make_number (last_good
), make_number (from
));
2791 if (!parse_sexp_ignore_comments
)
2793 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2794 &out_charpos
, &out_bytepos
);
2795 /* FIXME: if found == -1, then it really wasn't a comment-end.
2796 For single-char Sendcomment, we can't do much about it apart
2797 from skipping the char.
2798 For 2-char endcomments, we could try again, taking both
2799 chars as separate entities, but it's a lot of trouble
2800 for very little gain, so we don't bother either. -sm */
2802 from
= out_charpos
, from_byte
= out_bytepos
;
2805 case Scomment_fence
:
2811 DEC_BOTH (from
, from_byte
);
2812 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2813 if (!char_quoted (from
, from_byte
)
2814 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2815 SYNTAX_WITH_MULTIBYTE_CHECK (c
) == code
))
2818 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2822 stringterm
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2827 DEC_BOTH (from
, from_byte
);
2828 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2829 if (!char_quoted (from
, from_byte
)
2831 == (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
)))
2832 && SYNTAX_WITH_MULTIBYTE_CHECK (c
) == Sstring
)
2835 if (!depth
&& sexpflag
) goto done2
;
2838 /* Ignore whitespace, punctuation, quote, endcomment. */
2843 /* Reached start of buffer. Error if within object, return nil if between */
2856 XSETFASTINT (val
, from
);
2860 xsignal3 (Qscan_error
,
2861 build_string ("Unbalanced parentheses"),
2862 make_number (last_good
), make_number (from
));
2865 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
2866 doc
: /* Scan from character number FROM by COUNT lists.
2867 Returns the character number of the position thus found.
2869 If DEPTH is nonzero, paren depth begins counting from that value,
2870 only places where the depth in parentheses becomes zero
2871 are candidates for stopping; COUNT such places are counted.
2872 Thus, a positive value for DEPTH means go out levels.
2874 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2876 If the beginning or end of (the accessible part of) the buffer is reached
2877 and the depth is wrong, an error is signaled.
2878 If the depth is right but the count is not used up, nil is returned. */)
2879 (Lisp_Object from
, Lisp_Object count
, Lisp_Object depth
)
2881 CHECK_NUMBER (from
);
2882 CHECK_NUMBER (count
);
2883 CHECK_NUMBER (depth
);
2885 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
2888 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
2889 doc
: /* Scan from character number FROM by COUNT balanced expressions.
2890 If COUNT is negative, scan backwards.
2891 Returns the character number of the position thus found.
2893 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2895 If the beginning or end of (the accessible part of) the buffer is reached
2896 in the middle of a parenthetical grouping, an error is signaled.
2897 If the beginning or end is reached between groupings
2898 but before count is used up, nil is returned. */)
2899 (Lisp_Object from
, Lisp_Object count
)
2901 CHECK_NUMBER (from
);
2902 CHECK_NUMBER (count
);
2904 return scan_lists (XINT (from
), XINT (count
), 0, 1);
2907 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
2909 doc
: /* Move point backward over any number of chars with prefix syntax.
2910 This includes chars with "quote" or "prefix" syntax (' or p). */)
2915 int opoint_byte
= PT_BYTE
;
2917 int pos_byte
= PT_BYTE
;
2922 SET_PT_BOTH (opoint
, opoint_byte
);
2927 SETUP_SYNTAX_TABLE (pos
, -1);
2929 DEC_BOTH (pos
, pos_byte
);
2931 while (!char_quoted (pos
, pos_byte
)
2932 /* Previous statement updates syntax table. */
2933 && ((c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
), SYNTAX (c
) == Squote
)
2934 || SYNTAX_PREFIX (c
)))
2937 opoint_byte
= pos_byte
;
2940 DEC_BOTH (pos
, pos_byte
);
2943 SET_PT_BOTH (opoint
, opoint_byte
);
2948 /* Parse forward from FROM / FROM_BYTE to END,
2949 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2950 and return a description of the state of the parse at END.
2951 If STOPBEFORE is nonzero, stop at the start of an atom.
2952 If COMMENTSTOP is 1, stop at the start of a comment.
2953 If COMMENTSTOP is -1, stop at the start or end of a comment,
2954 after the beginning of a string, or after the end of a string. */
2957 scan_sexps_forward (struct lisp_parse_state
*stateptr
,
2958 EMACS_INT from
, EMACS_INT from_byte
, EMACS_INT end
,
2959 int targetdepth
, int stopbefore
,
2960 Lisp_Object oldstate
, int commentstop
)
2962 struct lisp_parse_state state
;
2964 register enum syntaxcode code
;
2967 struct level
{ int last
, prev
; };
2968 struct level levelstart
[100];
2969 register struct level
*curlevel
= levelstart
;
2970 struct level
*endlevel
= levelstart
+ 100;
2971 register int depth
; /* Paren depth of current scanning location.
2972 level - levelstart equals this except
2973 when the depth becomes negative. */
2974 int mindepth
; /* Lowest DEPTH value seen. */
2975 int start_quoted
= 0; /* Nonzero means starting after a char quote */
2977 EMACS_INT prev_from
; /* Keep one character before FROM. */
2978 EMACS_INT prev_from_byte
;
2979 int prev_from_syntax
;
2980 int boundary_stop
= commentstop
== -1;
2983 EMACS_INT out_bytepos
, out_charpos
;
2987 prev_from_byte
= from_byte
;
2989 DEC_BOTH (prev_from
, prev_from_byte
);
2991 /* Use this macro instead of `from++'. */
2993 do { prev_from = from; \
2994 prev_from_byte = from_byte; \
2995 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2996 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2997 INC_BOTH (from, from_byte); \
2999 UPDATE_SYNTAX_TABLE_FORWARD (from); \
3005 if (NILP (oldstate
))
3008 state
.instring
= -1;
3009 state
.incomment
= 0;
3010 state
.comstyle
= 0; /* comment style a by default. */
3011 state
.comstr_start
= -1; /* no comment/string seen. */
3015 tem
= Fcar (oldstate
);
3021 oldstate
= Fcdr (oldstate
);
3022 oldstate
= Fcdr (oldstate
);
3023 oldstate
= Fcdr (oldstate
);
3024 tem
= Fcar (oldstate
);
3025 /* Check whether we are inside string_fence-style string: */
3026 state
.instring
= (!NILP (tem
)
3027 ? (INTEGERP (tem
) ? XINT (tem
) : ST_STRING_STYLE
)
3030 oldstate
= Fcdr (oldstate
);
3031 tem
= Fcar (oldstate
);
3032 state
.incomment
= (!NILP (tem
)
3033 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
3036 oldstate
= Fcdr (oldstate
);
3037 tem
= Fcar (oldstate
);
3038 start_quoted
= !NILP (tem
);
3040 /* if the eighth element of the list is nil, we are in comment
3041 style a. If it is non-nil, we are in comment style b */
3042 oldstate
= Fcdr (oldstate
);
3043 oldstate
= Fcdr (oldstate
);
3044 tem
= Fcar (oldstate
);
3045 state
.comstyle
= (NILP (tem
)
3047 : (EQ (tem
, Qsyntax_table
)
3049 : INTEGERP (tem
) ? XINT (tem
) : 1));
3051 oldstate
= Fcdr (oldstate
);
3052 tem
= Fcar (oldstate
);
3053 state
.comstr_start
= NILP (tem
) ? -1 : XINT (tem
) ;
3054 oldstate
= Fcdr (oldstate
);
3055 tem
= Fcar (oldstate
);
3056 while (!NILP (tem
)) /* >= second enclosing sexps. */
3058 /* curlevel++->last ran into compiler bug on Apollo */
3059 curlevel
->last
= XINT (Fcar (tem
));
3060 if (++curlevel
== endlevel
)
3061 curlevel
--; /* error ("Nesting too deep for parser"); */
3062 curlevel
->prev
= -1;
3063 curlevel
->last
= -1;
3070 curlevel
->prev
= -1;
3071 curlevel
->last
= -1;
3073 SETUP_SYNTAX_TABLE (prev_from
, 1);
3074 temp
= FETCH_CHAR (prev_from_byte
);
3075 prev_from_syntax
= SYNTAX_WITH_FLAGS (temp
);
3076 UPDATE_SYNTAX_TABLE_FORWARD (from
);
3078 /* Enter the loop at a place appropriate for initial state. */
3080 if (state
.incomment
)
3081 goto startincomment
;
3082 if (state
.instring
>= 0)
3084 nofence
= state
.instring
!= ST_STRING_STYLE
;
3086 goto startquotedinstring
;
3089 else if (start_quoted
)
3096 code
= prev_from_syntax
& 0xff;
3099 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
)
3100 && (c1
= FETCH_CHAR (from_byte
),
3101 syntax
= SYNTAX_WITH_FLAGS (c1
),
3102 SYNTAX_FLAGS_COMSTART_SECOND (syntax
)))
3103 /* Duplicate code to avoid a complex if-expression
3104 which causes trouble for the SGI compiler. */
3106 /* Record the comment style we have entered so that only
3107 the comment-end sequence of the same style actually
3108 terminates the comment section. */
3110 = SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_from_syntax
);
3111 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
);
3112 comnested
= comnested
|| SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
3113 state
.incomment
= comnested
? 1 : -1;
3114 state
.comstr_start
= prev_from
;
3118 else if (code
== Scomment_fence
)
3120 /* Record the comment style we have entered so that only
3121 the comment-end sequence of the same style actually
3122 terminates the comment section. */
3123 state
.comstyle
= ST_COMMENT_STYLE
;
3124 state
.incomment
= -1;
3125 state
.comstr_start
= prev_from
;
3128 else if (code
== Scomment
)
3130 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
, 0);
3131 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
3133 state
.comstr_start
= prev_from
;
3136 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
3138 switch (SWITCH_ENUM_CAST (code
))
3142 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3143 curlevel
->last
= prev_from
;
3145 if (from
== end
) goto endquoted
;
3148 /* treat following character as a word constituent */
3151 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3152 curlevel
->last
= prev_from
;
3156 /* Some compilers can't handle this inside the switch. */
3157 temp
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3158 temp
= SYNTAX (temp
);
3164 if (from
== end
) goto endquoted
;
3176 curlevel
->prev
= curlevel
->last
;
3179 case Scomment_fence
: /* Can't happen because it's handled above. */
3181 if (commentstop
|| boundary_stop
) goto done
;
3183 /* The (from == BEGV) test was to enter the loop in the middle so
3184 that we find a 2-char comment ender even if we start in the
3185 middle of it. We don't want to do that if we're just at the
3186 beginning of the comment (think of (*) ... (*)). */
3187 found
= forw_comment (from
, from_byte
, end
,
3188 state
.incomment
, state
.comstyle
,
3189 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
3190 ? 0 : prev_from_syntax
,
3191 &out_charpos
, &out_bytepos
, &state
.incomment
);
3192 from
= out_charpos
; from_byte
= out_bytepos
;
3193 /* Beware! prev_from and friends are invalid now.
3194 Luckily, the `done' doesn't use them and the INC_FROM
3195 sets them to a sane value without looking at them. */
3196 if (!found
) goto done
;
3198 state
.incomment
= 0;
3199 state
.comstyle
= 0; /* reset the comment style */
3200 if (boundary_stop
) goto done
;
3204 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3206 /* curlevel++->last ran into compiler bug on Apollo */
3207 curlevel
->last
= prev_from
;
3208 if (++curlevel
== endlevel
)
3209 curlevel
--; /* error ("Nesting too deep for parser"); */
3210 curlevel
->prev
= -1;
3211 curlevel
->last
= -1;
3212 if (targetdepth
== depth
) goto done
;
3217 if (depth
< mindepth
)
3219 if (curlevel
!= levelstart
)
3221 curlevel
->prev
= curlevel
->last
;
3222 if (targetdepth
== depth
) goto done
;
3227 state
.comstr_start
= from
- 1;
3228 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3229 curlevel
->last
= prev_from
;
3230 state
.instring
= (code
== Sstring
3231 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte
))
3233 if (boundary_stop
) goto done
;
3236 nofence
= state
.instring
!= ST_STRING_STYLE
;
3242 if (from
>= end
) goto done
;
3243 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3244 /* Some compilers can't handle this inside the switch. */
3247 /* Check TEMP here so that if the char has
3248 a syntax-table property which says it is NOT
3249 a string character, it does not end the string. */
3250 if (nofence
&& c
== state
.instring
&& temp
== Sstring
)
3256 if (!nofence
) goto string_end
;
3261 startquotedinstring
:
3262 if (from
>= end
) goto endquoted
;
3268 state
.instring
= -1;
3269 curlevel
->prev
= curlevel
->last
;
3271 if (boundary_stop
) goto done
;
3275 /* FIXME: We should do something with it. */
3278 /* Ignore whitespace, punctuation, quote, endcomment. */
3284 stop
: /* Here if stopping before start of sexp. */
3285 from
= prev_from
; /* We have just fetched the char that starts it; */
3286 goto done
; /* but return the position before it. */
3291 state
.depth
= depth
;
3292 state
.mindepth
= mindepth
;
3293 state
.thislevelstart
= curlevel
->prev
;
3294 state
.prevlevelstart
3295 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
3296 state
.location
= from
;
3297 state
.levelstarts
= Qnil
;
3298 while (--curlevel
>= levelstart
)
3299 state
.levelstarts
= Fcons (make_number (curlevel
->last
),
3306 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
3307 doc
: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3308 Parsing stops at TO or when certain criteria are met;
3309 point is set to where parsing stops.
3310 If fifth arg OLDSTATE is omitted or nil,
3311 parsing assumes that FROM is the beginning of a function.
3312 Value is a list of elements describing final state of parsing:
3314 1. character address of start of innermost containing list; nil if none.
3315 2. character address of start of last complete sexp terminated.
3316 3. non-nil if inside a string.
3317 (it is the character that will terminate the string,
3318 or t if the string should be terminated by a generic string delimiter.)
3319 4. nil if outside a comment, t if inside a non-nestable comment,
3320 else an integer (the current comment nesting).
3321 5. t if following a quote character.
3322 6. the minimum paren-depth encountered during this scan.
3323 7. style of comment, if any.
3324 8. character address of start of comment or string; nil if not in one.
3325 9. Intermediate data for continuation of parsing (subject to change).
3326 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3327 in parentheses becomes equal to TARGETDEPTH.
3328 Fourth arg STOPBEFORE non-nil means stop when come to
3329 any character that starts a sexp.
3330 Fifth arg OLDSTATE is a list like what this function returns.
3331 It is used to initialize the state of the parse. Elements number 1, 2, 6
3333 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3334 If it is symbol `syntax-table', stop after the start of a comment or a
3335 string, or after end of a comment or a string. */)
3336 (Lisp_Object from
, Lisp_Object to
, Lisp_Object targetdepth
, Lisp_Object stopbefore
, Lisp_Object oldstate
, Lisp_Object commentstop
)
3338 struct lisp_parse_state state
;
3341 if (!NILP (targetdepth
))
3343 CHECK_NUMBER (targetdepth
);
3344 target
= XINT (targetdepth
);
3347 target
= -100000; /* We won't reach this depth */
3349 validate_region (&from
, &to
);
3350 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
3352 target
, !NILP (stopbefore
), oldstate
,
3354 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
3356 SET_PT (state
.location
);
3358 return Fcons (make_number (state
.depth
),
3359 Fcons (state
.prevlevelstart
< 0
3360 ? Qnil
: make_number (state
.prevlevelstart
),
3361 Fcons (state
.thislevelstart
< 0
3362 ? Qnil
: make_number (state
.thislevelstart
),
3363 Fcons (state
.instring
>= 0
3364 ? (state
.instring
== ST_STRING_STYLE
3365 ? Qt
: make_number (state
.instring
)) : Qnil
,
3366 Fcons (state
.incomment
< 0 ? Qt
:
3367 (state
.incomment
== 0 ? Qnil
:
3368 make_number (state
.incomment
)),
3369 Fcons (state
.quoted
? Qt
: Qnil
,
3370 Fcons (make_number (state
.mindepth
),
3371 Fcons ((state
.comstyle
3372 ? (state
.comstyle
== ST_COMMENT_STYLE
3374 : make_number (state
.comstyle
))
3376 Fcons (((state
.incomment
3377 || (state
.instring
>= 0))
3378 ? make_number (state
.comstr_start
)
3380 Fcons (state
.levelstarts
, Qnil
))))))))));
3384 init_syntax_once (void)
3389 /* This has to be done here, before we call Fmake_char_table. */
3390 Qsyntax_table
= intern_c_string ("syntax-table");
3391 staticpro (&Qsyntax_table
);
3393 /* Intern_C_String this now in case it isn't already done.
3394 Setting this variable twice is harmless.
3395 But don't staticpro it here--that is done in alloc.c. */
3396 Qchar_table_extra_slots
= intern_c_string ("char-table-extra-slots");
3398 /* Create objects which can be shared among syntax tables. */
3399 Vsyntax_code_object
= Fmake_vector (make_number (Smax
), Qnil
);
3400 for (i
= 0; i
< XVECTOR (Vsyntax_code_object
)->size
; i
++)
3401 XVECTOR (Vsyntax_code_object
)->contents
[i
]
3402 = Fcons (make_number (i
), Qnil
);
3404 /* Now we are ready to set up this property, so we can
3405 create syntax tables. */
3406 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
3408 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Swhitespace
];
3410 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
3412 /* Control characters should not be whitespace. */
3413 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Spunct
];
3414 for (i
= 0; i
<= ' ' - 1; i
++)
3415 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3416 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 0177, temp
);
3418 /* Except that a few really are whitespace. */
3419 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Swhitespace
];
3420 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ' ', temp
);
3421 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\t', temp
);
3422 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\n', temp
);
3423 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 015, temp
);
3424 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 014, temp
);
3426 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
3427 for (i
= 'a'; i
<= 'z'; i
++)
3428 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3429 for (i
= 'A'; i
<= 'Z'; i
++)
3430 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3431 for (i
= '0'; i
<= '9'; i
++)
3432 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3434 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
3435 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
3437 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
3438 Fcons (make_number (Sopen
), make_number (')')));
3439 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
3440 Fcons (make_number (Sclose
), make_number ('(')));
3441 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
3442 Fcons (make_number (Sopen
), make_number (']')));
3443 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
3444 Fcons (make_number (Sclose
), make_number ('[')));
3445 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
3446 Fcons (make_number (Sopen
), make_number ('}')));
3447 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
3448 Fcons (make_number (Sclose
), make_number ('{')));
3449 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
3450 Fcons (make_number ((int) Sstring
), Qnil
));
3451 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
3452 Fcons (make_number ((int) Sescape
), Qnil
));
3454 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Ssymbol
];
3455 for (i
= 0; i
< 10; i
++)
3457 c
= "_-+*/&|<>="[i
];
3458 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3461 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Spunct
];
3462 for (i
= 0; i
< 12; i
++)
3464 c
= ".,;:?!#@~^'`"[i
];
3465 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3468 /* All multibyte characters have syntax `word' by default. */
3469 temp
= XVECTOR (Vsyntax_code_object
)->contents
[(int) Sword
];
3470 char_table_set_range (Vstandard_syntax_table
, 0x80, MAX_CHAR
, temp
);
3474 syms_of_syntax (void)
3476 Qsyntax_table_p
= intern_c_string ("syntax-table-p");
3477 staticpro (&Qsyntax_table_p
);
3479 staticpro (&Vsyntax_code_object
);
3481 staticpro (&gl_state
.object
);
3482 staticpro (&gl_state
.global_code
);
3483 staticpro (&gl_state
.current_syntax_table
);
3484 staticpro (&gl_state
.old_prop
);
3486 /* Defined in regex.c */
3487 staticpro (&re_match_object
);
3489 Qscan_error
= intern_c_string ("scan-error");
3490 staticpro (&Qscan_error
);
3491 Fput (Qscan_error
, Qerror_conditions
,
3492 pure_cons (Qscan_error
, pure_cons (Qerror
, Qnil
)));
3493 Fput (Qscan_error
, Qerror_message
,
3494 make_pure_c_string ("Scan error"));
3496 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments
,
3497 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3499 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties
,
3500 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3501 Otherwise, that text property is simply ignored.
3502 See the info node `(elisp)Syntax Properties' for a description of the
3503 `syntax-table' property. */);
3505 words_include_escapes
= 0;
3506 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes
,
3507 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3509 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol
,
3510 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3511 multibyte_syntax_as_symbol
= 0;
3513 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3514 &open_paren_in_column_0_is_defun_start
,
3515 doc
: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3516 open_paren_in_column_0_is_defun_start
= 1;
3519 DEFVAR_LISP ("find-word-boundary-function-table",
3520 &Vfind_word_boundary_function_table
,
3522 Char table of functions to search for the word boundary.
3523 Each function is called with two arguments; POS and LIMIT.
3524 POS and LIMIT are character positions in the current buffer.
3526 If POS is less than LIMIT, POS is at the first character of a word,
3527 and the return value of a function is a position after the last
3528 character of that word.
3530 If POS is not less than LIMIT, POS is at the last character of a word,
3531 and the return value of a function is a position at the first
3532 character of that word.
3534 In both cases, LIMIT bounds the search. */);
3535 Vfind_word_boundary_function_table
= Fmake_char_table (Qnil
, Qnil
);
3537 defsubr (&Ssyntax_table_p
);
3538 defsubr (&Ssyntax_table
);
3539 defsubr (&Sstandard_syntax_table
);
3540 defsubr (&Scopy_syntax_table
);
3541 defsubr (&Sset_syntax_table
);
3542 defsubr (&Schar_syntax
);
3543 defsubr (&Smatching_paren
);
3544 defsubr (&Sstring_to_syntax
);
3545 defsubr (&Smodify_syntax_entry
);
3546 defsubr (&Sinternal_describe_syntax_value
);
3548 defsubr (&Sforward_word
);
3550 defsubr (&Sskip_chars_forward
);
3551 defsubr (&Sskip_chars_backward
);
3552 defsubr (&Sskip_syntax_forward
);
3553 defsubr (&Sskip_syntax_backward
);
3555 defsubr (&Sforward_comment
);
3556 defsubr (&Sscan_lists
);
3557 defsubr (&Sscan_sexps
);
3558 defsubr (&Sbackward_prefix_chars
);
3559 defsubr (&Sparse_partial_sexp
);
3562 /* arch-tag: 3e297b9f-088e-4b64-8f4c-fb0b3443e412
3563 (do not change this comment) */