1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2015 Free
3 Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
23 #include <sys/types.h>
27 #include "character.h"
33 #include "intervals.h"
36 /* Make syntax table lookup grant data in gl_state. */
37 #define SYNTAX(c) syntax_property (c, 1)
38 #define SYNTAX_ENTRY(c) syntax_property_entry (c, 1)
39 #define SYNTAX_WITH_FLAGS(c) syntax_property_with_flags (c, 1)
41 /* Eight single-bit flags have the following meanings:
42 1. This character is the first of a two-character comment-start sequence.
43 2. This character is the second of a two-character comment-start sequence.
44 3. This character is the first of a two-character comment-end sequence.
45 4. This character is the second of a two-character comment-end sequence.
46 5. This character is a prefix, for backward-prefix-chars.
47 6. The char is part of a delimiter for comments of style "b".
48 7. This character is part of a nestable comment sequence.
49 8. The char is part of a delimiter for comments of style "c".
50 Note that any two-character sequence whose first character has flag 1
51 and whose second character has flag 2 will be interpreted as a comment start.
53 Bits 6 and 8 discriminate among different comment styles.
54 Languages such as C++ allow two orthogonal syntax start/end pairs
55 and bit 6 determines whether a comment-end or Scommentend
56 ends style a or b. Comment markers can start style a, b, c, or bc.
57 Style a is always the default.
58 For 2-char comment markers, the style b flag is looked up only on the second
59 char of the comment marker and on the first char of the comment ender.
60 For style c (like the nested flag), the flag can be placed on any of
63 /* These functions extract specific flags from an integer
64 that holds the syntax code and the flags. */
67 SYNTAX_FLAGS_COMSTART_FIRST (int flags
)
69 return (flags
>> 16) & 1;
72 SYNTAX_FLAGS_COMSTART_SECOND (int flags
)
74 return (flags
>> 17) & 1;
77 SYNTAX_FLAGS_COMEND_FIRST (int flags
)
79 return (flags
>> 18) & 1;
82 SYNTAX_FLAGS_COMEND_SECOND (int flags
)
84 return (flags
>> 19) & 1;
87 SYNTAX_FLAGS_PREFIX (int flags
)
89 return (flags
>> 20) & 1;
92 SYNTAX_FLAGS_COMMENT_STYLEB (int flags
)
94 return (flags
>> 21) & 1;
97 SYNTAX_FLAGS_COMMENT_STYLEC (int flags
)
99 return (flags
>> 23) & 1;
102 SYNTAX_FLAGS_COMMENT_STYLEC2 (int flags
)
104 return (flags
>> 22) & 2; /* SYNTAX_FLAGS_COMMENT_STYLEC (flags) * 2 */
107 SYNTAX_FLAGS_COMMENT_NESTED (int flags
)
109 return (flags
>> 22) & 1;
112 /* FLAGS should be the flags of the main char of the comment marker, e.g.
113 the second for comstart and the first for comend. */
115 SYNTAX_FLAGS_COMMENT_STYLE (int flags
, int other_flags
)
117 return (SYNTAX_FLAGS_COMMENT_STYLEB (flags
)
118 | SYNTAX_FLAGS_COMMENT_STYLEC2 (flags
)
119 | SYNTAX_FLAGS_COMMENT_STYLEC2 (other_flags
));
122 /* Extract a particular flag for a given character. */
125 SYNTAX_COMEND_FIRST (int c
)
127 return SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c
));
130 /* We use these constants in place for comment-style and
131 string-ender-char to distinguish comments/strings started by
132 comment_fence and string_fence codes. */
136 ST_COMMENT_STYLE
= 256 + 1,
137 ST_STRING_STYLE
= 256 + 2
140 /* This is the internal form of the parse state used in parse-partial-sexp. */
142 struct lisp_parse_state
144 EMACS_INT depth
; /* Depth at end of parsing. */
145 int instring
; /* -1 if not within string, else desired terminator. */
146 EMACS_INT incomment
; /* -1 if in unnestable comment else comment nesting */
147 int comstyle
; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
148 bool quoted
; /* True if just after an escape char at end of parsing. */
149 EMACS_INT mindepth
; /* Minimum depth seen while scanning. */
150 /* Char number of most recent start-of-expression at current level */
151 ptrdiff_t thislevelstart
;
152 /* Char number of start of containing expression */
153 ptrdiff_t prevlevelstart
;
154 ptrdiff_t location
; /* Char number at which parsing stopped. */
155 ptrdiff_t location_byte
; /* Corresponding byte position. */
156 ptrdiff_t comstr_start
; /* Position of last comment/string starter. */
157 Lisp_Object levelstarts
; /* Char numbers of starts-of-expression
158 of levels (starting from outermost). */
161 /* These variables are a cache for finding the start of a defun.
162 find_start_pos is the place for which the defun start was found.
163 find_start_value is the defun start position found for it.
164 find_start_value_byte is the corresponding byte position.
165 find_start_buffer is the buffer it was found in.
166 find_start_begv is the BEGV value when it was found.
167 find_start_modiff is the value of MODIFF when it was found. */
169 static ptrdiff_t find_start_pos
;
170 static ptrdiff_t find_start_value
;
171 static ptrdiff_t find_start_value_byte
;
172 static struct buffer
*find_start_buffer
;
173 static ptrdiff_t find_start_begv
;
174 static EMACS_INT find_start_modiff
;
177 static Lisp_Object
skip_chars (bool, Lisp_Object
, Lisp_Object
, bool);
178 static Lisp_Object
skip_syntaxes (bool, Lisp_Object
, Lisp_Object
);
179 static Lisp_Object
scan_lists (EMACS_INT
, EMACS_INT
, EMACS_INT
, bool);
180 static void scan_sexps_forward (struct lisp_parse_state
*,
181 ptrdiff_t, ptrdiff_t, ptrdiff_t, EMACS_INT
,
182 bool, Lisp_Object
, int);
183 static bool in_classes (int, Lisp_Object
);
184 static void parse_sexp_propertize (ptrdiff_t charpos
);
186 /* This setter is used only in this file, so it can be private. */
188 bset_syntax_table (struct buffer
*b
, Lisp_Object val
)
190 b
->syntax_table_
= val
;
193 /* Whether the syntax of the character C has the prefix flag set. */
195 syntax_prefix_flag_p (int c
)
197 return SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c
));
200 struct gl_state_s gl_state
; /* Global state of syntax parser. */
202 enum { INTERVALS_AT_ONCE
= 10 }; /* 1 + max-number of intervals
203 to scan to property-change. */
205 /* Set the syntax entry VAL for char C in table TABLE. */
208 SET_RAW_SYNTAX_ENTRY (Lisp_Object table
, int c
, Lisp_Object val
)
210 CHAR_TABLE_SET (table
, c
, val
);
213 /* Set the syntax entry VAL for char-range RANGE in table TABLE.
214 RANGE is a cons (FROM . TO) specifying the range of characters. */
217 SET_RAW_SYNTAX_ENTRY_RANGE (Lisp_Object table
, Lisp_Object range
,
220 Fset_char_table_range (table
, range
, val
);
223 /* Extract the information from the entry for character C
224 in the current syntax table. */
229 Lisp_Object ent
= SYNTAX_ENTRY (c
);
230 return CONSP (ent
) ? XCDR (ent
) : Qnil
;
233 /* This should be called with FROM at the start of forward
234 search, or after the last position of the backward search. It
235 makes sure that the first char is picked up with correct table, so
236 one does not need to call UPDATE_SYNTAX_TABLE immediately after the
238 Sign of COUNT gives the direction of the search.
242 SETUP_SYNTAX_TABLE (ptrdiff_t from
, ptrdiff_t count
)
244 SETUP_BUFFER_SYNTAX_TABLE ();
245 gl_state
.b_property
= BEGV
;
246 gl_state
.e_property
= ZV
+ 1;
247 gl_state
.object
= Qnil
;
249 if (parse_sexp_lookup_properties
)
252 update_syntax_table_forward (from
, true, Qnil
);
253 else if (from
> BEGV
)
255 update_syntax_table (from
- 1, count
, true, Qnil
);
256 parse_sexp_propertize (from
- 1);
261 /* Same as above, but in OBJECT. If OBJECT is nil, use current buffer.
262 If it is t (which is only used in fast_c_string_match_ignore_case),
263 ignore properties altogether.
265 This is meant for regex.c to use. For buffers, regex.c passes arguments
266 to the UPDATE_SYNTAX_TABLE functions which are relative to BEGV.
267 So if it is a buffer, we set the offset field to BEGV. */
270 SETUP_SYNTAX_TABLE_FOR_OBJECT (Lisp_Object object
,
271 ptrdiff_t from
, ptrdiff_t count
)
273 SETUP_BUFFER_SYNTAX_TABLE ();
274 gl_state
.object
= object
;
275 if (BUFFERP (gl_state
.object
))
277 struct buffer
*buf
= XBUFFER (gl_state
.object
);
278 gl_state
.b_property
= 1;
279 gl_state
.e_property
= BUF_ZV (buf
) - BUF_BEGV (buf
) + 1;
280 gl_state
.offset
= BUF_BEGV (buf
) - 1;
282 else if (NILP (gl_state
.object
))
284 gl_state
.b_property
= 1;
285 gl_state
.e_property
= ZV
- BEGV
+ 1;
286 gl_state
.offset
= BEGV
- 1;
288 else if (EQ (gl_state
.object
, Qt
))
290 gl_state
.b_property
= 0;
291 gl_state
.e_property
= PTRDIFF_MAX
;
296 gl_state
.b_property
= 0;
297 gl_state
.e_property
= 1 + SCHARS (gl_state
.object
);
300 if (parse_sexp_lookup_properties
)
301 update_syntax_table (from
+ gl_state
.offset
- (count
<= 0),
302 count
, 1, gl_state
.object
);
305 /* Update gl_state to an appropriate interval which contains CHARPOS. The
306 sign of COUNT give the relative position of CHARPOS wrt the previously
307 valid interval. If INIT, only [be]_property fields of gl_state are
308 valid at start, the rest is filled basing on OBJECT.
310 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
311 direction than the intervals - or in an interval. We update the
312 current syntax-table basing on the property of this interval, and
313 update the interval to start further than CHARPOS - or be
314 NULL. We also update lim_property to be the next value of
315 charpos to call this subroutine again - or be before/after the
316 start/end of OBJECT. */
319 update_syntax_table (ptrdiff_t charpos
, EMACS_INT count
, bool init
,
322 Lisp_Object tmp_table
;
324 bool invalidate
= true;
329 gl_state
.old_prop
= Qnil
;
330 gl_state
.start
= gl_state
.b_property
;
331 gl_state
.stop
= gl_state
.e_property
;
332 i
= interval_of (charpos
, object
);
333 gl_state
.backward_i
= gl_state
.forward_i
= i
;
337 /* interval_of updates only ->position of the return value, so
338 update the parents manually to speed up update_interval. */
339 while (!NULL_PARENT (i
))
341 if (AM_RIGHT_CHILD (i
))
342 INTERVAL_PARENT (i
)->position
= i
->position
343 - LEFT_TOTAL_LENGTH (i
) + TOTAL_LENGTH (i
) /* right end */
344 - TOTAL_LENGTH (INTERVAL_PARENT (i
))
345 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i
));
347 INTERVAL_PARENT (i
)->position
= i
->position
- LEFT_TOTAL_LENGTH (i
)
349 i
= INTERVAL_PARENT (i
);
351 i
= gl_state
.forward_i
;
352 gl_state
.b_property
= i
->position
- gl_state
.offset
;
353 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
356 i
= count
> 0 ? gl_state
.forward_i
: gl_state
.backward_i
;
358 /* We are guaranteed to be called with CHARPOS either in i,
361 error ("Error in syntax_table logic for to-the-end intervals");
362 else if (charpos
< i
->position
) /* Move left. */
365 error ("Error in syntax_table logic for intervals <-");
366 /* Update the interval. */
367 i
= update_interval (i
, charpos
);
368 if (INTERVAL_LAST_POS (i
) != gl_state
.b_property
)
371 gl_state
.forward_i
= i
;
372 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
375 else if (charpos
>= INTERVAL_LAST_POS (i
)) /* Move right. */
378 error ("Error in syntax_table logic for intervals ->");
379 /* Update the interval. */
380 i
= update_interval (i
, charpos
);
381 if (i
->position
!= gl_state
.e_property
)
384 gl_state
.backward_i
= i
;
385 gl_state
.b_property
= i
->position
- gl_state
.offset
;
390 tmp_table
= textget (i
->plist
, Qsyntax_table
);
393 invalidate
= !EQ (tmp_table
, gl_state
.old_prop
); /* Need to invalidate? */
395 if (invalidate
) /* Did not get to adjacent interval. */
396 { /* with the same table => */
397 /* invalidate the old range. */
400 gl_state
.backward_i
= i
;
401 gl_state
.b_property
= i
->position
- gl_state
.offset
;
405 gl_state
.forward_i
= i
;
406 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
410 if (!EQ (tmp_table
, gl_state
.old_prop
))
412 gl_state
.current_syntax_table
= tmp_table
;
413 gl_state
.old_prop
= tmp_table
;
414 if (EQ (Fsyntax_table_p (tmp_table
), Qt
))
416 gl_state
.use_global
= 0;
418 else if (CONSP (tmp_table
))
420 gl_state
.use_global
= 1;
421 gl_state
.global_code
= tmp_table
;
425 gl_state
.use_global
= 0;
426 gl_state
.current_syntax_table
= BVAR (current_buffer
, syntax_table
);
432 if (cnt
&& !EQ (tmp_table
, textget (i
->plist
, Qsyntax_table
)))
436 gl_state
.e_property
= i
->position
- gl_state
.offset
;
437 gl_state
.forward_i
= i
;
442 = i
->position
+ LENGTH (i
) - gl_state
.offset
;
443 gl_state
.backward_i
= i
;
447 else if (cnt
== INTERVALS_AT_ONCE
)
452 = i
->position
+ LENGTH (i
) - gl_state
.offset
453 /* e_property at EOB is not set to ZV but to ZV+1, so that
454 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
455 having to check eob between the two. */
456 + (next_interval (i
) ? 0 : 1);
457 gl_state
.forward_i
= i
;
461 gl_state
.b_property
= i
->position
- gl_state
.offset
;
462 gl_state
.backward_i
= i
;
467 i
= count
> 0 ? next_interval (i
) : previous_interval (i
);
469 eassert (i
== NULL
); /* This property goes to the end. */
472 gl_state
.e_property
= gl_state
.stop
;
473 gl_state
.forward_i
= i
;
476 gl_state
.b_property
= gl_state
.start
;
480 parse_sexp_propertize (ptrdiff_t charpos
)
483 if (syntax_propertize__done
<= charpos
484 && syntax_propertize__done
< zv
)
486 EMACS_INT modiffs
= CHARS_MODIFF
;
487 safe_call1 (Qinternal__syntax_propertize
,
488 make_number (min (zv
, 1 + charpos
)));
489 if (modiffs
!= CHARS_MODIFF
)
490 error ("parse-sexp-propertize-function modified the buffer!");
491 if (syntax_propertize__done
<= charpos
492 && syntax_propertize__done
< zv
)
493 error ("parse-sexp-propertize-function did not move"
494 " syntax-propertize--done");
495 SETUP_SYNTAX_TABLE (charpos
, 1);
497 else if (gl_state
.e_property
> syntax_propertize__done
)
499 gl_state
.e_property
= syntax_propertize__done
;
500 gl_state
.e_property_truncated
= true;
505 update_syntax_table_forward (ptrdiff_t charpos
, bool init
,
508 if (gl_state
.e_property_truncated
)
510 eassert (NILP (object
));
511 eassert (charpos
>= gl_state
.e_property
);
512 eassert (charpos
>= syntax_propertize__done
);
513 parse_sexp_propertize (charpos
);
517 update_syntax_table (charpos
, 1, init
, object
);
518 if (gl_state
.e_property
> syntax_propertize__done
520 parse_sexp_propertize (charpos
);
524 /* Returns true if char at CHARPOS is quoted.
525 Global syntax-table data should be set up already to be good at CHARPOS
526 or after. On return global syntax data is good for lookup at CHARPOS. */
529 char_quoted (ptrdiff_t charpos
, ptrdiff_t bytepos
)
531 enum syntaxcode code
;
532 ptrdiff_t beg
= BEGV
;
534 ptrdiff_t orig
= charpos
;
536 while (charpos
> beg
)
539 DEC_BOTH (charpos
, bytepos
);
541 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
);
542 c
= FETCH_CHAR_AS_MULTIBYTE (bytepos
);
544 if (! (code
== Scharquote
|| code
== Sescape
))
550 UPDATE_SYNTAX_TABLE (orig
);
554 /* Return the bytepos one character before BYTEPOS.
555 We assume that BYTEPOS is not at the start of the buffer. */
558 dec_bytepos (ptrdiff_t bytepos
)
560 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
567 /* Return a defun-start position before POS and not too far before.
568 It should be the last one before POS, or nearly the last.
570 When open_paren_in_column_0_is_defun_start is nonzero,
571 only the beginning of the buffer is treated as a defun-start.
573 We record the information about where the scan started
574 and what its result was, so that another call in the same area
575 can return the same value very quickly.
577 There is no promise at which position the global syntax data is
578 valid on return from the subroutine, so the caller should explicitly
579 update the global data. */
582 find_defun_start (ptrdiff_t pos
, ptrdiff_t pos_byte
)
584 ptrdiff_t opoint
= PT
, opoint_byte
= PT_BYTE
;
586 /* Use previous finding, if it's valid and applies to this inquiry. */
587 if (current_buffer
== find_start_buffer
588 /* Reuse the defun-start even if POS is a little farther on.
589 POS might be in the next defun, but that's ok.
590 Our value may not be the best possible, but will still be usable. */
591 && pos
<= find_start_pos
+ 1000
592 && pos
>= find_start_value
593 && BEGV
== find_start_begv
594 && MODIFF
== find_start_modiff
)
595 return find_start_value
;
597 if (!open_paren_in_column_0_is_defun_start
)
599 find_start_value
= BEGV
;
600 find_start_value_byte
= BEGV_BYTE
;
604 /* Back up to start of line. */
605 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
607 /* We optimize syntax-table lookup for rare updates. Thus we accept
608 only those `^\s(' which are good in global _and_ text-property
610 SETUP_BUFFER_SYNTAX_TABLE ();
615 /* Open-paren at start of line means we may have found our
617 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
618 if (SYNTAX (c
) == Sopen
)
620 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
621 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
622 if (SYNTAX (c
) == Sopen
)
624 /* Now fallback to the default value. */
625 SETUP_BUFFER_SYNTAX_TABLE ();
627 /* Move to beg of previous line. */
628 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
631 /* Record what we found, for the next try. */
632 find_start_value
= PT
;
633 find_start_value_byte
= PT_BYTE
;
634 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
637 find_start_buffer
= current_buffer
;
638 find_start_modiff
= MODIFF
;
639 find_start_begv
= BEGV
;
640 find_start_pos
= pos
;
642 return find_start_value
;
645 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
648 prev_char_comend_first (ptrdiff_t pos
, ptrdiff_t pos_byte
)
653 DEC_BOTH (pos
, pos_byte
);
654 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
655 c
= FETCH_CHAR (pos_byte
);
656 val
= SYNTAX_COMEND_FIRST (c
);
657 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
661 /* Check whether charpos FROM is at the end of a comment.
662 FROM_BYTE is the bytepos corresponding to FROM.
663 Do not move back before STOP.
665 Return true if we find a comment ending at FROM/FROM_BYTE.
667 If successful, store the charpos of the comment's beginning
668 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
670 Global syntax data remains valid for backward search starting at
671 the returned value (or at FROM, if the search was not successful). */
674 back_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
,
675 bool comnested
, int comstyle
, ptrdiff_t *charpos_ptr
,
676 ptrdiff_t *bytepos_ptr
)
678 /* Look back, counting the parity of string-quotes,
679 and recording the comment-starters seen.
680 When we reach a safe place, assume that's not in a string;
681 then step the main scan to the earliest comment-starter seen
682 an even number of string quotes away from the safe place.
684 OFROM[I] is position of the earliest comment-starter seen
685 which is I+2X quotes from the comment-end.
686 PARITY is current parity of quotes from the comment end. */
687 int string_style
= -1; /* Presumed outside of any string. */
688 bool string_lossage
= 0;
689 /* Not a real lossage: indicates that we have passed a matching comment
690 starter plus a non-matching comment-ender, meaning that any matching
691 comment-starter we might see later could be a false positive (hidden
692 inside another comment).
693 Test case: { a (* b } c (* d *) */
694 bool comment_lossage
= 0;
695 ptrdiff_t comment_end
= from
;
696 ptrdiff_t comment_end_byte
= from_byte
;
697 ptrdiff_t comstart_pos
= 0;
698 ptrdiff_t comstart_byte
IF_LINT (= 0);
699 /* Place where the containing defun starts,
700 or 0 if we didn't come across it yet. */
701 ptrdiff_t defun_start
= 0;
702 ptrdiff_t defun_start_byte
= 0;
703 enum syntaxcode code
;
704 ptrdiff_t nesting
= 1; /* Current comment nesting. */
708 /* FIXME: A }} comment-ender style leads to incorrect behavior
709 in the case of {{ c }}} because we ignore the last two chars which are
710 assumed to be comment-enders although they aren't. */
712 /* At beginning of range to scan, we're outside of strings;
713 that determines quote parity to the comment-end. */
718 bool com2start
, com2end
, comstart
;
720 /* Move back and examine a character. */
721 DEC_BOTH (from
, from_byte
);
722 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
724 prev_syntax
= syntax
;
725 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
726 syntax
= SYNTAX_WITH_FLAGS (c
);
729 /* Check for 2-char comment markers. */
730 com2start
= (SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
731 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax
)
733 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax
, syntax
))
734 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
735 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
);
736 com2end
= (SYNTAX_FLAGS_COMEND_FIRST (syntax
)
737 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax
));
738 comstart
= (com2start
|| code
== Scomment
);
740 /* Nasty cases with overlapping 2-char comment markers:
741 - snmp-mode: -- c -- foo -- c --
749 /* If a 2-char comment sequence partly overlaps with another,
750 we don't try to be clever. E.g. |*| in C, or }% in modes that
751 have %..\n and %{..}%. */
752 if (from
> stop
&& (com2end
|| comstart
))
754 ptrdiff_t next
= from
, next_byte
= from_byte
;
755 int next_c
, next_syntax
;
756 DEC_BOTH (next
, next_byte
);
757 UPDATE_SYNTAX_TABLE_BACKWARD (next
);
758 next_c
= FETCH_CHAR_AS_MULTIBYTE (next_byte
);
759 next_syntax
= SYNTAX_WITH_FLAGS (next_c
);
760 if (((comstart
|| comnested
)
761 && SYNTAX_FLAGS_COMEND_SECOND (syntax
)
762 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax
))
763 || ((com2end
|| comnested
)
764 && SYNTAX_FLAGS_COMSTART_SECOND (syntax
)
766 == SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_syntax
))
767 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax
)))
769 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
772 if (com2start
&& comstart_pos
== 0)
773 /* We're looking at a comment starter. But it might be a comment
774 ender as well (see snmp-mode). The first time we see one, we
775 need to consider it as a comment starter,
776 and the subsequent times as a comment ender. */
779 /* Turn a 2-char comment sequences into the appropriate syntax. */
784 /* Ignore comment starters of a different style. */
785 else if (code
== Scomment
786 && (comstyle
!= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0)
787 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
) != comnested
))
790 /* Ignore escaped characters, except comment-enders. */
791 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
798 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
800 /* Track parity of quotes. */
801 if (string_style
== -1)
802 /* Entering a string. */
804 else if (string_style
== c
)
805 /* Leaving the string. */
808 /* If we have two kinds of string delimiters.
809 There's no way to grok this scanning backwards. */
814 /* We've already checked that it is the relevant comstyle. */
815 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
816 /* There are odd string quotes involved, so let's be careful.
817 Test case in Pascal: " { " a { " } */
822 /* Record best comment-starter so far. */
824 comstart_byte
= from_byte
;
826 else if (--nesting
<= 0)
827 /* nested comments have to be balanced, so we don't need to
828 keep looking for earlier ones. We use here the same (slightly
829 incorrect) reasoning as below: since it is followed by uniform
830 paired string quotes, this comment-start has to be outside of
831 strings, else the comment-end itself would be inside a string. */
836 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == comstyle
837 && ((com2end
&& SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
))
838 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
839 /* This is the same style of comment ender as ours. */
844 /* Anything before that can't count because it would match
845 this comment-ender rather than ours. */
846 from
= stop
; /* Break out of the loop. */
848 else if (comstart_pos
!= 0 || c
!= '\n')
849 /* We're mixing comment styles here, so we'd better be careful.
850 The (comstart_pos != 0 || c != '\n') check is not quite correct
851 (we should just always set comment_lossage), but removing it
852 would imply that any multiline comment in C would go through
853 lossage, which seems overkill.
854 The failure should only happen in the rare cases such as
860 /* Assume a defun-start point is outside of strings. */
861 if (open_paren_in_column_0_is_defun_start
863 || (temp_byte
= dec_bytepos (from_byte
),
864 FETCH_CHAR (temp_byte
) == '\n')))
867 defun_start_byte
= from_byte
;
868 from
= stop
; /* Break out of the loop. */
877 if (comstart_pos
== 0)
880 from_byte
= comment_end_byte
;
881 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
);
883 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
884 or `done'), then we've found the beginning of the non-nested comment. */
885 else if (1) /* !comnested */
888 from_byte
= comstart_byte
;
889 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
893 struct lisp_parse_state state
;
894 bool adjusted
= true;
895 /* We had two kinds of string delimiters mixed up
896 together. Decode this going forwards.
897 Scan fwd from a known safe place (beginning-of-defun)
898 to the one in question; this records where we
899 last passed a comment starter. */
900 /* If we did not already find the defun start, find it now. */
901 if (defun_start
== 0)
903 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
904 defun_start_byte
= find_start_value_byte
;
905 adjusted
= (defun_start
> BEGV
);
909 scan_sexps_forward (&state
,
910 defun_start
, defun_start_byte
,
911 comment_end
, TYPE_MINIMUM (EMACS_INT
),
913 defun_start
= comment_end
;
918 = CONSP (state
.levelstarts
) ? XINT (XCAR (state
.levelstarts
))
919 : state
.thislevelstart
>= 0 ? state
.thislevelstart
921 find_start_value_byte
= CHAR_TO_BYTE (find_start_value
);
924 if (state
.incomment
== (comnested
? 1 : -1)
925 && state
.comstyle
== comstyle
)
926 from
= state
.comstr_start
;
931 /* If comment_end is inside some other comment, maybe ours
932 is nested, so we need to try again from within the
933 surrounding comment. Example: { a (* " *) */
935 /* FIXME: We should advance by one or two chars. */
936 defun_start
= state
.comstr_start
+ 2;
937 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
940 } while (defun_start
< comment_end
);
942 from_byte
= CHAR_TO_BYTE (from
);
943 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
948 *bytepos_ptr
= from_byte
;
950 return from
!= comment_end
;
953 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
954 doc
: /* Return t if OBJECT is a syntax table.
955 Currently, any char-table counts as a syntax table. */)
958 if (CHAR_TABLE_P (object
)
959 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
965 check_syntax_table (Lisp_Object obj
)
967 CHECK_TYPE (CHAR_TABLE_P (obj
) && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
),
968 Qsyntax_table_p
, obj
);
971 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
972 doc
: /* Return the current syntax table.
973 This is the one specified by the current buffer. */)
976 return BVAR (current_buffer
, syntax_table
);
979 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
980 Sstandard_syntax_table
, 0, 0, 0,
981 doc
: /* Return the standard syntax table.
982 This is the one used for new buffers. */)
985 return Vstandard_syntax_table
;
988 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
989 doc
: /* Construct a new syntax table and return it.
990 It is a copy of the TABLE, which defaults to the standard syntax table. */)
996 check_syntax_table (table
);
998 table
= Vstandard_syntax_table
;
1000 copy
= Fcopy_sequence (table
);
1002 /* Only the standard syntax table should have a default element.
1003 Other syntax tables should inherit from parents instead. */
1004 set_char_table_defalt (copy
, Qnil
);
1006 /* Copied syntax tables should all have parents.
1007 If we copied one with no parent, such as the standard syntax table,
1008 use the standard syntax table as the copy's parent. */
1009 if (NILP (XCHAR_TABLE (copy
)->parent
))
1010 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
1014 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
1015 doc
: /* Select a new syntax table for the current buffer.
1016 One argument, a syntax table. */)
1020 check_syntax_table (table
);
1021 bset_syntax_table (current_buffer
, table
);
1022 /* Indicate that this buffer now has a specified syntax table. */
1023 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
1024 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
1028 /* Convert a letter which signifies a syntax code
1029 into the code it signifies.
1030 This is used by modify-syntax-entry, and other things. */
1032 unsigned char const syntax_spec_code
[0400] =
1033 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1034 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1035 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1036 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1037 Swhitespace
, Scomment_fence
, Sstring
, 0377, Smath
, 0377, 0377, Squote
,
1038 Sopen
, Sclose
, 0377, 0377, 0377, Swhitespace
, Spunct
, Scharquote
,
1039 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1040 0377, 0377, 0377, 0377, Scomment
, 0377, Sendcomment
, 0377,
1041 Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
1042 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1043 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword
,
1044 0377, 0377, 0377, 0377, Sescape
, 0377, 0377, Ssymbol
,
1045 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
1046 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1047 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword
,
1048 0377, 0377, 0377, 0377, Sstring_fence
, 0377, 0377, 0377
1051 /* Indexed by syntax code, give the letter that describes it. */
1053 char const syntax_code_spec
[16] =
1055 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
1059 /* Indexed by syntax code, give the object (cons of syntax code and
1060 nil) to be stored in syntax table. Since these objects can be
1061 shared among syntax tables, we generate them in advance. By
1062 sharing objects, the function `describe-syntax' can give a more
1064 static Lisp_Object Vsyntax_code_object
;
1067 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
1068 doc
: /* Return the syntax code of CHARACTER, described by a character.
1069 For example, if CHARACTER is a word constituent, the
1070 character `w' (119) is returned.
1071 The characters that correspond to various syntax codes
1072 are listed in the documentation of `modify-syntax-entry'. */)
1073 (Lisp_Object character
)
1076 CHECK_CHARACTER (character
);
1077 char_int
= XINT (character
);
1078 SETUP_BUFFER_SYNTAX_TABLE ();
1079 return make_number (syntax_code_spec
[SYNTAX (char_int
)]);
1082 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
1083 doc
: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
1084 (Lisp_Object character
)
1087 enum syntaxcode code
;
1088 CHECK_CHARACTER (character
);
1089 char_int
= XINT (character
);
1090 SETUP_BUFFER_SYNTAX_TABLE ();
1091 code
= SYNTAX (char_int
);
1092 if (code
== Sopen
|| code
== Sclose
)
1093 return SYNTAX_MATCH (char_int
);
1097 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
1098 doc
: /* Convert a syntax descriptor STRING into a raw syntax descriptor.
1099 STRING should be a string of the form allowed as argument of
1100 `modify-syntax-entry'. The return value is a raw syntax descriptor: a
1101 cons cell \(CODE . MATCHING-CHAR) which can be used, for example, as
1102 the value of a `syntax-table' text property. */)
1103 (Lisp_Object string
)
1105 const unsigned char *p
;
1109 CHECK_STRING (string
);
1112 val
= syntax_spec_code
[*p
++];
1114 error ("Invalid syntax description letter: %c", p
[-1]);
1116 if (val
== Sinherit
)
1122 int character
= STRING_CHAR_AND_LENGTH (p
, len
);
1123 XSETINT (match
, character
);
1124 if (XFASTINT (match
) == ' ')
1167 if (val
< ASIZE (Vsyntax_code_object
) && NILP (match
))
1168 return AREF (Vsyntax_code_object
, val
);
1170 /* Since we can't use a shared object, let's make a new one. */
1171 return Fcons (make_number (val
), match
);
1174 /* I really don't know why this is interactive
1175 help-form should at least be made useful whilst reading the second arg. */
1176 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
1177 "cSet syntax for character: \nsSet syntax for %s to: ",
1178 doc
: /* Set syntax for character CHAR according to string NEWENTRY.
1179 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1180 the current buffer's syntax table.
1181 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1182 in the range MIN to MAX are changed.
1183 The first character of NEWENTRY should be one of the following:
1184 Space or - whitespace syntax. w word constituent.
1185 _ symbol constituent. . punctuation.
1186 ( open-parenthesis. ) close-parenthesis.
1187 " string quote. \\ escape.
1188 $ paired delimiter. \\=' expression quote or prefix operator.
1189 < comment starter. > comment ender.
1190 / character-quote. @ inherit from parent table.
1191 | generic string fence. ! generic comment fence.
1193 Only single-character comment start and end sequences are represented thus.
1194 Two-character sequences are represented as described below.
1195 The second character of NEWENTRY is the matching parenthesis,
1196 used only if the first character is `(' or `)'.
1197 Any additional characters are flags.
1198 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1199 1 means CHAR is the start of a two-char comment start sequence.
1200 2 means CHAR is the second character of such a sequence.
1201 3 means CHAR is the start of a two-char comment end sequence.
1202 4 means CHAR is the second character of such a sequence.
1204 There can be several orthogonal comment sequences. This is to support
1205 language modes such as C++. By default, all comment sequences are of style
1206 a, but you can set the comment sequence style to b (on the second character
1207 of a comment-start, and the first character of a comment-end sequence) and/or
1208 c (on any of its chars) using this flag:
1209 b means CHAR is part of comment sequence b.
1210 c means CHAR is part of comment sequence c.
1211 n means CHAR is part of a nestable comment sequence.
1213 p means CHAR is a prefix character for `backward-prefix-chars';
1214 such characters are treated as whitespace when they occur
1215 between expressions.
1216 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1217 (Lisp_Object c
, Lisp_Object newentry
, Lisp_Object syntax_table
)
1221 CHECK_CHARACTER_CAR (c
);
1222 CHECK_CHARACTER_CDR (c
);
1225 CHECK_CHARACTER (c
);
1227 if (NILP (syntax_table
))
1228 syntax_table
= BVAR (current_buffer
, syntax_table
);
1230 check_syntax_table (syntax_table
);
1232 newentry
= Fstring_to_syntax (newentry
);
1234 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table
, c
, newentry
);
1236 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), newentry
);
1238 /* We clear the regexp cache, since character classes can now have
1239 different values from those in the compiled regexps.*/
1240 clear_regexp_cache ();
1245 /* Dump syntax table to buffer in human-readable format */
1247 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value
,
1248 Sinternal_describe_syntax_value
, 1, 1, 0,
1249 doc
: /* Insert a description of the internal syntax description SYNTAX at point. */)
1250 (Lisp_Object syntax
)
1252 int code
, syntax_code
;
1253 bool start1
, start2
, end1
, end2
, prefix
, comstyleb
, comstylec
, comnested
;
1255 Lisp_Object first
, match_lisp
, value
= syntax
;
1259 insert_string ("default");
1263 if (CHAR_TABLE_P (value
))
1265 insert_string ("deeper char-table ...");
1271 insert_string ("invalid");
1275 first
= XCAR (value
);
1276 match_lisp
= XCDR (value
);
1278 if (!INTEGERP (first
) || !(NILP (match_lisp
) || CHARACTERP (match_lisp
)))
1280 insert_string ("invalid");
1284 syntax_code
= XINT (first
) & INT_MAX
;
1285 code
= syntax_code
& 0377;
1286 start1
= SYNTAX_FLAGS_COMSTART_FIRST (syntax_code
);
1287 start2
= SYNTAX_FLAGS_COMSTART_SECOND (syntax_code
);
1288 end1
= SYNTAX_FLAGS_COMEND_FIRST (syntax_code
);
1289 end2
= SYNTAX_FLAGS_COMEND_SECOND (syntax_code
);
1290 prefix
= SYNTAX_FLAGS_PREFIX (syntax_code
);
1291 comstyleb
= SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code
);
1292 comstylec
= SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code
);
1293 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax_code
);
1297 insert_string ("invalid");
1301 str
[0] = syntax_code_spec
[code
], str
[1] = 0;
1304 if (NILP (match_lisp
))
1307 insert_char (XINT (match_lisp
));
1328 insert_string ("\twhich means: ");
1333 insert_string ("whitespace"); break;
1335 insert_string ("punctuation"); break;
1337 insert_string ("word"); break;
1339 insert_string ("symbol"); break;
1341 insert_string ("open"); break;
1343 insert_string ("close"); break;
1345 insert_string ("prefix"); break;
1347 insert_string ("string"); break;
1349 insert_string ("math"); break;
1351 insert_string ("escape"); break;
1353 insert_string ("charquote"); break;
1355 insert_string ("comment"); break;
1357 insert_string ("endcomment"); break;
1359 insert_string ("inherit"); break;
1360 case Scomment_fence
:
1361 insert_string ("comment fence"); break;
1363 insert_string ("string fence"); break;
1365 insert_string ("invalid");
1369 if (!NILP (match_lisp
))
1371 insert_string (", matches ");
1372 insert_char (XINT (match_lisp
));
1376 insert_string (",\n\t is the first character of a comment-start sequence");
1378 insert_string (",\n\t is the second character of a comment-start sequence");
1381 insert_string (",\n\t is the first character of a comment-end sequence");
1383 insert_string (",\n\t is the second character of a comment-end sequence");
1385 insert_string (" (comment style b)");
1387 insert_string (" (comment style c)");
1389 insert_string (" (nestable)");
1393 AUTO_STRING (prefixdoc
,
1394 ",\n\t is a prefix character for `backward-prefix-chars'");
1395 insert1 (Fsubstitute_command_keys (prefixdoc
));
1401 /* Return the position across COUNT words from FROM.
1402 If that many words cannot be found before the end of the buffer, return 0.
1403 COUNT negative means scan backward and stop at word beginning. */
1406 scan_words (register ptrdiff_t from
, register EMACS_INT count
)
1408 register ptrdiff_t beg
= BEGV
;
1409 register ptrdiff_t end
= ZV
;
1410 register ptrdiff_t from_byte
= CHAR_TO_BYTE (from
);
1411 register enum syntaxcode code
;
1413 Lisp_Object func
, pos
;
1418 SETUP_SYNTAX_TABLE (from
, count
);
1429 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1430 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1431 code
= SYNTAX (ch0
);
1432 INC_BOTH (from
, from_byte
);
1433 if (words_include_escapes
1434 && (code
== Sescape
|| code
== Scharquote
))
1439 /* Now CH0 is a character which begins a word and FROM is the
1440 position of the next character. */
1441 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch0
);
1442 if (! NILP (Ffboundp (func
)))
1444 pos
= call2 (func
, make_number (from
- 1), make_number (end
));
1445 if (INTEGERP (pos
) && from
< XINT (pos
) && XINT (pos
) <= ZV
)
1448 from_byte
= CHAR_TO_BYTE (from
);
1455 if (from
== end
) break;
1456 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1457 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1458 code
= SYNTAX (ch1
);
1460 && (! words_include_escapes
1461 || (code
!= Sescape
&& code
!= Scharquote
)))
1462 || word_boundary_p (ch0
, ch1
))
1464 INC_BOTH (from
, from_byte
);
1479 DEC_BOTH (from
, from_byte
);
1480 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1481 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1482 code
= SYNTAX (ch1
);
1483 if (words_include_escapes
1484 && (code
== Sescape
|| code
== Scharquote
))
1489 /* Now CH1 is a character which ends a word and FROM is the
1491 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch1
);
1492 if (! NILP (Ffboundp (func
)))
1494 pos
= call2 (func
, make_number (from
), make_number (beg
));
1495 if (INTEGERP (pos
) && BEGV
<= XINT (pos
) && XINT (pos
) < from
)
1498 from_byte
= CHAR_TO_BYTE (from
);
1507 DEC_BOTH (from
, from_byte
);
1508 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1509 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1510 code
= SYNTAX (ch0
);
1512 && (! words_include_escapes
1513 || (code
!= Sescape
&& code
!= Scharquote
)))
1514 || word_boundary_p (ch0
, ch1
))
1516 INC_BOTH (from
, from_byte
);
1530 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 0, 1, "^p",
1531 doc
: /* Move point forward ARG words (backward if ARG is negative).
1532 If ARG is omitted or nil, move point forward one word.
1534 If an edge of the buffer or a field boundary is reached, point is left there
1535 and the function returns nil. Field boundaries are not noticed if
1536 `inhibit-field-text-motion' is non-nil. */)
1540 ptrdiff_t orig_val
, val
;
1543 XSETFASTINT (arg
, 1);
1547 val
= orig_val
= scan_words (PT
, XINT (arg
));
1549 val
= XINT (arg
) > 0 ? ZV
: BEGV
;
1551 /* Avoid jumping out of an input field. */
1552 tmp
= Fconstrain_to_field (make_number (val
), make_number (PT
),
1554 val
= XFASTINT (tmp
);
1557 return val
== orig_val
? Qt
: Qnil
;
1560 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1561 doc
: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1562 STRING is like the inside of a `[...]' in a regular expression
1563 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1564 (but not at the end of a range; quoting is never needed there).
1565 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1566 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1567 Char classes, e.g. `[:alpha:]', are supported.
1569 Returns the distance traveled, either zero or positive. */)
1570 (Lisp_Object string
, Lisp_Object lim
)
1572 return skip_chars (1, string
, lim
, 1);
1575 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1576 doc
: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1577 See `skip-chars-forward' for details.
1578 Returns the distance traveled, either zero or negative. */)
1579 (Lisp_Object string
, Lisp_Object lim
)
1581 return skip_chars (0, string
, lim
, 1);
1584 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1585 doc
: /* Move point forward across chars in specified syntax classes.
1586 SYNTAX is a string of syntax code characters.
1587 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1588 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1589 This function returns the distance traveled, either zero or positive. */)
1590 (Lisp_Object syntax
, Lisp_Object lim
)
1592 return skip_syntaxes (1, syntax
, lim
);
1595 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1596 doc
: /* Move point backward across chars in specified syntax classes.
1597 SYNTAX is a string of syntax code characters.
1598 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1599 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1600 This function returns either zero or a negative number, and the absolute value
1601 of this is the distance traveled. */)
1602 (Lisp_Object syntax
, Lisp_Object lim
)
1604 return skip_syntaxes (0, syntax
, lim
);
1608 skip_chars (bool forwardp
, Lisp_Object string
, Lisp_Object lim
,
1609 bool handle_iso_classes
)
1613 /* Store the ranges of non-ASCII characters. */
1614 int *char_ranges
IF_LINT (= NULL
);
1615 int n_char_ranges
= 0;
1617 ptrdiff_t i
, i_byte
;
1618 /* True if the current buffer is multibyte and the region contains
1621 /* True if STRING is multibyte and it contains non-ASCII chars. */
1622 bool string_multibyte
;
1623 ptrdiff_t size_byte
;
1624 const unsigned char *str
;
1626 Lisp_Object iso_classes
;
1629 CHECK_STRING (string
);
1633 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1635 CHECK_NUMBER_COERCE_MARKER (lim
);
1637 /* In any case, don't allow scan outside bounds of buffer. */
1638 if (XINT (lim
) > ZV
)
1639 XSETFASTINT (lim
, ZV
);
1640 if (XINT (lim
) < BEGV
)
1641 XSETFASTINT (lim
, BEGV
);
1643 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1644 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1645 string_multibyte
= SBYTES (string
) > SCHARS (string
);
1647 memset (fastmap
, 0, sizeof fastmap
);
1649 str
= SDATA (string
);
1650 size_byte
= SBYTES (string
);
1653 if (i_byte
< size_byte
1654 && SREF (string
, 0) == '^')
1656 negate
= 1; i_byte
++;
1659 /* Find the characters specified and set their elements of fastmap.
1660 Handle backslashes and ranges specially.
1662 If STRING contains non-ASCII characters, setup char_ranges for
1663 them and use fastmap only for their leading codes. */
1665 if (! string_multibyte
)
1667 bool string_has_eight_bit
= 0;
1669 /* At first setup fastmap. */
1670 while (i_byte
< size_byte
)
1674 if (handle_iso_classes
&& c
== '['
1675 && i_byte
< size_byte
1676 && str
[i_byte
] == ':')
1678 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1679 const unsigned char *class_end
= class_beg
;
1680 const unsigned char *class_limit
= str
+ size_byte
- 2;
1681 /* Leave room for the null. */
1682 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1685 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1686 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1688 while (class_end
< class_limit
1689 && *class_end
>= 'a' && *class_end
<= 'z')
1692 if (class_end
== class_beg
1693 || *class_end
!= ':' || class_end
[1] != ']')
1694 goto not_a_class_name
;
1696 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1697 class_name
[class_end
- class_beg
] = 0;
1699 cc
= re_wctype (class_name
);
1701 error ("Invalid ISO C character class");
1703 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1705 i_byte
= class_end
+ 2 - str
;
1712 if (i_byte
== size_byte
)
1717 /* Treat `-' as range character only if another character
1719 if (i_byte
+ 1 < size_byte
1720 && str
[i_byte
] == '-')
1724 /* Skip over the dash. */
1727 /* Get the end of the range. */
1730 && i_byte
< size_byte
)
1738 if (! ASCII_CHAR_P (c2
))
1739 string_has_eight_bit
= 1;
1745 if (! ASCII_CHAR_P (c
))
1746 string_has_eight_bit
= 1;
1750 /* If the current range is multibyte and STRING contains
1751 eight-bit chars, arrange fastmap and setup char_ranges for
1752 the corresponding multibyte chars. */
1753 if (multibyte
&& string_has_eight_bit
)
1756 char himap
[0200 + 1];
1757 memcpy (himap
, fastmap
+ 0200, 0200);
1759 memset (fastmap
+ 0200, 0, 0200);
1760 SAFE_NALLOCA (char_ranges
, 2, 128);
1763 while ((p1
= memchr (himap
+ i
, 1, 0200 - i
)))
1765 /* Deduce the next range C..C2 from the next clump of 1s
1766 in HIMAP starting with &HIMAP[I]. HIMAP is the high
1767 order half of the old FASTMAP. */
1768 int c2
, leading_code
;
1770 c
= BYTE8_TO_CHAR (i
+ 0200);
1772 c2
= BYTE8_TO_CHAR (i
+ 0200 - 1);
1774 char_ranges
[n_char_ranges
++] = c
;
1775 char_ranges
[n_char_ranges
++] = c2
;
1776 leading_code
= CHAR_LEADING_CODE (c
);
1777 memset (fastmap
+ leading_code
, 1,
1778 CHAR_LEADING_CODE (c2
) - leading_code
+ 1);
1782 else /* STRING is multibyte */
1784 SAFE_NALLOCA (char_ranges
, 2, SCHARS (string
));
1786 while (i_byte
< size_byte
)
1788 int leading_code
= str
[i_byte
];
1789 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1792 if (handle_iso_classes
&& c
== '['
1793 && i_byte
< size_byte
1794 && STRING_CHAR (str
+ i_byte
) == ':')
1796 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1797 const unsigned char *class_end
= class_beg
;
1798 const unsigned char *class_limit
= str
+ size_byte
- 2;
1799 /* Leave room for the null. */
1800 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1803 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1804 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1806 while (class_end
< class_limit
1807 && *class_end
>= 'a' && *class_end
<= 'z')
1810 if (class_end
== class_beg
1811 || *class_end
!= ':' || class_end
[1] != ']')
1812 goto not_a_class_name_multibyte
;
1814 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1815 class_name
[class_end
- class_beg
] = 0;
1817 cc
= re_wctype (class_name
);
1819 error ("Invalid ISO C character class");
1821 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1823 i_byte
= class_end
+ 2 - str
;
1827 not_a_class_name_multibyte
:
1830 if (i_byte
== size_byte
)
1833 leading_code
= str
[i_byte
];
1834 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1837 /* Treat `-' as range character only if another character
1839 if (i_byte
+ 1 < size_byte
1840 && str
[i_byte
] == '-')
1842 int c2
, leading_code2
;
1844 /* Skip over the dash. */
1847 /* Get the end of the range. */
1848 leading_code2
= str
[i_byte
];
1849 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1853 && i_byte
< size_byte
)
1855 leading_code2
= str
[i_byte
];
1856 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1862 if (ASCII_CHAR_P (c
))
1864 while (c
<= c2
&& c
< 0x80)
1866 leading_code
= CHAR_LEADING_CODE (c
);
1868 if (! ASCII_CHAR_P (c
))
1870 int lim2
= leading_code2
+ 1;
1871 while (leading_code
< lim2
)
1872 fastmap
[leading_code
++] = 1;
1875 char_ranges
[n_char_ranges
++] = c
;
1876 char_ranges
[n_char_ranges
++] = c2
;
1882 if (ASCII_CHAR_P (c
))
1886 fastmap
[leading_code
] = 1;
1887 char_ranges
[n_char_ranges
++] = c
;
1888 char_ranges
[n_char_ranges
++] = c
;
1893 /* If the current range is unibyte and STRING contains non-ASCII
1894 chars, arrange fastmap for the corresponding unibyte
1897 if (! multibyte
&& n_char_ranges
> 0)
1899 memset (fastmap
+ 0200, 0, 0200);
1900 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1902 int c1
= char_ranges
[i
];
1903 int lim2
= char_ranges
[i
+ 1] + 1;
1905 for (; c1
< lim2
; c1
++)
1907 int b
= CHAR_TO_BYTE_SAFE (c1
);
1915 /* If ^ was the first character, complement the fastmap. */
1919 for (i
= 0; i
< sizeof fastmap
; i
++)
1923 for (i
= 0; i
< 0200; i
++)
1925 /* All non-ASCII chars possibly match. */
1926 for (; i
< sizeof fastmap
; i
++)
1932 ptrdiff_t start_point
= PT
;
1934 ptrdiff_t pos_byte
= PT_BYTE
;
1935 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1939 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1940 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1944 endp
= CHAR_POS_ADDR (XINT (lim
));
1945 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1949 /* This code may look up syntax tables using functions that rely on the
1950 gl_state object. To make sure this object is not out of date,
1951 let's initialize it manually.
1952 We ignore syntax-table text-properties for now, since that's
1953 what we've done in the past. */
1954 SETUP_BUFFER_SYNTAX_TABLE ();
1969 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
1970 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1980 if (! ASCII_CHAR_P (c
))
1982 /* As we are looking at a multibyte character, we
1983 must look up the character in the table
1984 CHAR_RANGES. If there's no data in the table,
1985 that character is not what we want to skip. */
1987 /* The following code do the right thing even if
1988 n_char_ranges is zero (i.e. no data in
1990 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1991 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1993 if (!(negate
^ (i
< n_char_ranges
)))
1997 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2010 if (!NILP (iso_classes
) && in_classes (*p
, iso_classes
))
2015 goto fwd_unibyte_ok
;
2021 p
++, pos
++, pos_byte
++;
2029 unsigned char *prev_p
;
2039 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2040 c
= STRING_CHAR (p
);
2042 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
2052 if (! ASCII_CHAR_P (c
))
2054 /* See the comment in the previous similar code. */
2055 for (i
= 0; i
< n_char_ranges
; i
+= 2)
2056 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
2058 if (!(negate
^ (i
< n_char_ranges
)))
2062 pos
--, pos_byte
-= prev_p
- p
;
2075 if (! NILP (iso_classes
) && in_classes (p
[-1], iso_classes
))
2080 goto back_unibyte_ok
;
2083 if (!fastmap
[p
[-1]])
2086 p
--, pos
--, pos_byte
--;
2090 SET_PT_BOTH (pos
, pos_byte
);
2094 return make_number (PT
- start_point
);
2100 skip_syntaxes (bool forwardp
, Lisp_Object string
, Lisp_Object lim
)
2103 unsigned char fastmap
[0400];
2105 ptrdiff_t i
, i_byte
;
2107 ptrdiff_t size_byte
;
2110 CHECK_STRING (string
);
2113 XSETINT (lim
, forwardp
? ZV
: BEGV
);
2115 CHECK_NUMBER_COERCE_MARKER (lim
);
2117 /* In any case, don't allow scan outside bounds of buffer. */
2118 if (XINT (lim
) > ZV
)
2119 XSETFASTINT (lim
, ZV
);
2120 if (XINT (lim
) < BEGV
)
2121 XSETFASTINT (lim
, BEGV
);
2123 if (forwardp
? (PT
>= XFASTINT (lim
)) : (PT
<= XFASTINT (lim
)))
2124 return make_number (0);
2126 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
2127 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
2129 memset (fastmap
, 0, sizeof fastmap
);
2131 if (SBYTES (string
) > SCHARS (string
))
2132 /* As this is very rare case (syntax spec is ASCII only), don't
2133 consider efficiency. */
2134 string
= string_make_unibyte (string
);
2136 str
= SDATA (string
);
2137 size_byte
= SBYTES (string
);
2140 if (i_byte
< size_byte
2141 && SREF (string
, 0) == '^')
2143 negate
= 1; i_byte
++;
2146 /* Find the syntaxes specified and set their elements of fastmap. */
2148 while (i_byte
< size_byte
)
2151 fastmap
[syntax_spec_code
[c
]] = 1;
2154 /* If ^ was the first character, complement the fastmap. */
2156 for (i
= 0; i
< sizeof fastmap
; i
++)
2160 ptrdiff_t start_point
= PT
;
2162 ptrdiff_t pos_byte
= PT_BYTE
;
2163 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
2167 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
2168 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
2172 endp
= CHAR_POS_ADDR (XINT (lim
));
2173 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
2177 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
2193 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
2194 if (! fastmap
[SYNTAX (c
)])
2196 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2197 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2211 if (! fastmap
[SYNTAX (*p
)])
2213 p
++, pos
++, pos_byte
++;
2214 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2224 unsigned char *prev_p
;
2233 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2235 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2236 c
= STRING_CHAR (p
);
2237 if (! fastmap
[SYNTAX (c
)])
2239 pos
--, pos_byte
-= prev_p
- p
;
2253 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2254 if (! fastmap
[SYNTAX (p
[-1])])
2256 p
--, pos
--, pos_byte
--;
2261 SET_PT_BOTH (pos
, pos_byte
);
2264 return make_number (PT
- start_point
);
2268 /* Return true if character C belongs to one of the ISO classes
2269 in the list ISO_CLASSES. Each class is represented by an
2270 integer which is its type according to re_wctype. */
2273 in_classes (int c
, Lisp_Object iso_classes
)
2275 bool fits_class
= 0;
2277 while (CONSP (iso_classes
))
2280 elt
= XCAR (iso_classes
);
2281 iso_classes
= XCDR (iso_classes
);
2283 if (re_iswctype (c
, XFASTINT (elt
)))
2290 /* Jump over a comment, assuming we are at the beginning of one.
2291 FROM is the current position.
2292 FROM_BYTE is the bytepos corresponding to FROM.
2293 Do not move past STOP (a charpos).
2294 The comment over which we have to jump is of style STYLE
2295 (either SYNTAX_FLAGS_COMMENT_STYLE (foo) or ST_COMMENT_STYLE).
2296 NESTING should be positive to indicate the nesting at the beginning
2297 for nested comments and should be zero or negative else.
2298 ST_COMMENT_STYLE cannot be nested.
2299 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2300 (or 0 If the search cannot start in the middle of a two-character).
2302 If successful, return true and store the charpos of the comment's end
2303 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2304 Else, return false and store the charpos STOP into *CHARPOS_PTR, the
2305 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2306 (as defined for state.incomment) in *INCOMMENT_PTR.
2308 The comment end is the last character of the comment rather than the
2309 character just after the comment.
2311 Global syntax data is assumed to initially be valid for FROM and
2312 remains valid for forward search starting at the returned position. */
2315 forw_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
,
2316 EMACS_INT nesting
, int style
, int prev_syntax
,
2317 ptrdiff_t *charpos_ptr
, ptrdiff_t *bytepos_ptr
,
2318 EMACS_INT
*incomment_ptr
)
2321 register enum syntaxcode code
;
2322 register int syntax
, other_syntax
;
2324 if (nesting
<= 0) nesting
= -1;
2326 /* Enter the loop in the middle so that we find
2327 a 2-char comment ender if we start in the middle of it. */
2328 syntax
= prev_syntax
;
2329 if (syntax
!= 0) goto forw_incomment
;
2335 *incomment_ptr
= nesting
;
2336 *charpos_ptr
= from
;
2337 *bytepos_ptr
= from_byte
;
2340 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2341 syntax
= SYNTAX_WITH_FLAGS (c
);
2342 code
= syntax
& 0xff;
2343 if (code
== Sendcomment
2344 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
2345 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
2346 (nesting
> 0 && --nesting
== 0) : nesting
< 0))
2347 /* We have encountered a comment end of the same style
2348 as the comment sequence which began this comment
2351 if (code
== Scomment_fence
2352 && style
== ST_COMMENT_STYLE
)
2353 /* We have encountered a comment end of the same style
2354 as the comment sequence which began this comment
2359 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
2360 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
)
2361 /* We have encountered a nested comment of the same style
2362 as the comment sequence which began this comment section. */
2364 INC_BOTH (from
, from_byte
);
2365 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2368 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2369 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2370 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2371 SYNTAX_FLAGS_COMEND_SECOND (other_syntax
))
2372 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, other_syntax
) == style
2373 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2374 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
))
2375 ? nesting
> 0 : nesting
< 0))
2378 /* We have encountered a comment end of the same style
2379 as the comment sequence which began this comment section. */
2383 INC_BOTH (from
, from_byte
);
2384 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2389 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
2390 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2391 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2392 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
) == style
2393 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2394 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2395 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
)))
2396 /* We have encountered a nested comment of the same style
2397 as the comment sequence which began this comment section. */
2399 INC_BOTH (from
, from_byte
);
2400 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2404 *charpos_ptr
= from
;
2405 *bytepos_ptr
= from_byte
;
2409 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
2411 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2412 Stop scanning if we find something other than a comment or whitespace.
2413 Set point to where scanning stops.
2414 If COUNT comments are found as expected, with nothing except whitespace
2415 between them, return t; otherwise return nil. */)
2418 ptrdiff_t from
, from_byte
, stop
;
2420 enum syntaxcode code
;
2421 int comstyle
= 0; /* style of comment encountered */
2422 bool comnested
= 0; /* whether the comment is nestable or not */
2425 ptrdiff_t out_charpos
, out_bytepos
;
2428 CHECK_NUMBER (count
);
2429 count1
= XINT (count
);
2430 stop
= count1
> 0 ? ZV
: BEGV
;
2436 from_byte
= PT_BYTE
;
2438 SETUP_SYNTAX_TABLE (from
, count1
);
2443 bool comstart_first
;
2444 int syntax
, other_syntax
;
2448 SET_PT_BOTH (from
, from_byte
);
2452 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2453 syntax
= SYNTAX_WITH_FLAGS (c
);
2455 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2456 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2457 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2458 INC_BOTH (from
, from_byte
);
2459 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2460 if (from
< stop
&& comstart_first
2461 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2462 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2463 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
)))
2465 /* We have encountered a comment start sequence and we
2466 are ignoring all text inside comments. We must record
2467 the comment style this sequence begins so that later,
2468 only a comment end of the same style actually ends
2469 the comment section. */
2471 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2472 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2473 INC_BOTH (from
, from_byte
);
2474 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2477 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
2479 if (code
== Scomment_fence
)
2480 comstyle
= ST_COMMENT_STYLE
;
2481 else if (code
!= Scomment
)
2484 DEC_BOTH (from
, from_byte
);
2485 SET_PT_BOTH (from
, from_byte
);
2488 /* We're at the start of a comment. */
2489 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
2490 &out_charpos
, &out_bytepos
, &dummy
);
2491 from
= out_charpos
; from_byte
= out_bytepos
;
2495 SET_PT_BOTH (from
, from_byte
);
2498 INC_BOTH (from
, from_byte
);
2499 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2500 /* We have skipped one comment. */
2513 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
2518 DEC_BOTH (from
, from_byte
);
2519 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2520 quoted
= char_quoted (from
, from_byte
);
2521 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2522 syntax
= SYNTAX_WITH_FLAGS (c
);
2525 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2526 if (code
== Sendcomment
)
2527 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2528 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2529 && prev_char_comend_first (from
, from_byte
)
2530 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
2533 /* We must record the comment style encountered so that
2534 later, we can match only the proper comment begin
2535 sequence of the same style. */
2536 DEC_BOTH (from
, from_byte
);
2538 /* Calling char_quoted, above, set up global syntax position
2539 at the new value of FROM. */
2540 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2541 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2542 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2543 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2546 if (code
== Scomment_fence
)
2548 /* Skip until first preceding unquoted comment_fence. */
2549 bool fence_found
= 0;
2550 ptrdiff_t ini
= from
, ini_byte
= from_byte
;
2554 DEC_BOTH (from
, from_byte
);
2555 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2556 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2557 if (SYNTAX (c
) == Scomment_fence
2558 && !char_quoted (from
, from_byte
))
2563 else if (from
== stop
)
2566 if (fence_found
== 0)
2568 from
= ini
; /* Set point to ini + 1. */
2569 from_byte
= ini_byte
;
2573 /* We have skipped one comment. */
2576 else if (code
== Sendcomment
)
2578 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2579 &out_charpos
, &out_bytepos
);
2583 /* This end-of-line is not an end-of-comment.
2584 Treat it like a whitespace.
2585 CC-mode (and maybe others) relies on this behavior. */
2589 /* Failure: we should go back to the end of this
2590 not-quite-endcomment. */
2591 if (SYNTAX (c
) != code
)
2592 /* It was a two-char Sendcomment. */
2593 INC_BOTH (from
, from_byte
);
2599 /* We have skipped one comment. */
2600 from
= out_charpos
, from_byte
= out_bytepos
;
2604 else if (code
!= Swhitespace
|| quoted
)
2608 INC_BOTH (from
, from_byte
);
2609 SET_PT_BOTH (from
, from_byte
);
2617 SET_PT_BOTH (from
, from_byte
);
2622 /* Return syntax code of character C if C is an ASCII character
2623 or if MULTIBYTE_SYMBOL_P is false. Otherwise, return Ssymbol. */
2625 static enum syntaxcode
2626 syntax_multibyte (int c
, bool multibyte_symbol_p
)
2628 return ASCII_CHAR_P (c
) || !multibyte_symbol_p
? SYNTAX (c
) : Ssymbol
;
2632 scan_lists (EMACS_INT from
, EMACS_INT count
, EMACS_INT depth
, bool sexpflag
)
2635 ptrdiff_t stop
= count
> 0 ? ZV
: BEGV
;
2640 enum syntaxcode code
;
2641 EMACS_INT min_depth
= depth
; /* Err out if depth gets less than this. */
2642 int comstyle
= 0; /* Style of comment encountered. */
2643 bool comnested
= 0; /* Whether the comment is nestable or not. */
2645 EMACS_INT last_good
= from
;
2647 ptrdiff_t from_byte
;
2648 ptrdiff_t out_bytepos
, out_charpos
;
2650 bool multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2652 if (depth
> 0) min_depth
= 0;
2654 if (from
> ZV
) from
= ZV
;
2655 if (from
< BEGV
) from
= BEGV
;
2657 from_byte
= CHAR_TO_BYTE (from
);
2662 SETUP_SYNTAX_TABLE (from
, count
);
2667 bool comstart_first
, prefix
;
2668 int syntax
, other_syntax
;
2669 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2670 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2671 syntax
= SYNTAX_WITH_FLAGS (c
);
2672 code
= syntax_multibyte (c
, multibyte_symbol_p
);
2673 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2674 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2675 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2676 prefix
= SYNTAX_FLAGS_PREFIX (syntax
);
2677 if (depth
== min_depth
)
2679 INC_BOTH (from
, from_byte
);
2680 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2681 if (from
< stop
&& comstart_first
2682 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2683 other_syntax
= SYNTAX_WITH_FLAGS (c
),
2684 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2685 && parse_sexp_ignore_comments
)
2687 /* We have encountered a comment start sequence and we
2688 are ignoring all text inside comments. We must record
2689 the comment style this sequence begins so that later,
2690 only a comment end of the same style actually ends
2691 the comment section. */
2693 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2694 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2695 INC_BOTH (from
, from_byte
);
2696 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2708 INC_BOTH (from
, from_byte
);
2709 /* Treat following character as a word constituent. */
2712 if (depth
|| !sexpflag
) break;
2713 /* This word counts as a sexp; return at end of it. */
2716 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2718 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2719 switch (syntax_multibyte (c
, multibyte_symbol_p
))
2723 INC_BOTH (from
, from_byte
);
2734 INC_BOTH (from
, from_byte
);
2738 case Scomment_fence
:
2739 comstyle
= ST_COMMENT_STYLE
;
2742 if (!parse_sexp_ignore_comments
) break;
2743 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2744 found
= forw_comment (from
, from_byte
, stop
,
2745 comnested
, comstyle
, 0,
2746 &out_charpos
, &out_bytepos
, &dummy
);
2747 from
= out_charpos
, from_byte
= out_bytepos
;
2754 INC_BOTH (from
, from_byte
);
2755 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2761 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (from_byte
))
2763 INC_BOTH (from
, from_byte
);
2773 if (!++depth
) goto done
;
2778 if (!--depth
) goto done
;
2779 if (depth
< min_depth
)
2780 xsignal3 (Qscan_error
,
2781 build_string ("Containing expression ends prematurely"),
2782 make_number (last_good
), make_number (from
));
2787 temp_pos
= dec_bytepos (from_byte
);
2788 stringterm
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2791 enum syntaxcode c_code
;
2794 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2795 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2796 c_code
= syntax_multibyte (c
, multibyte_symbol_p
);
2798 ? c
== stringterm
&& c_code
== Sstring
2799 : c_code
== Sstring_fence
)
2806 INC_BOTH (from
, from_byte
);
2808 INC_BOTH (from
, from_byte
);
2810 INC_BOTH (from
, from_byte
);
2811 if (!depth
&& sexpflag
) goto done
;
2814 /* Ignore whitespace, punctuation, quote, endcomment. */
2819 /* Reached end of buffer. Error if within object, return nil if between */
2826 /* End of object reached */
2836 DEC_BOTH (from
, from_byte
);
2837 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2838 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2839 syntax
= SYNTAX_WITH_FLAGS (c
);
2840 code
= syntax_multibyte (c
, multibyte_symbol_p
);
2841 if (depth
== min_depth
)
2844 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2845 if (code
== Sendcomment
)
2846 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2847 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2848 && prev_char_comend_first (from
, from_byte
)
2849 && parse_sexp_ignore_comments
)
2851 /* We must record the comment style encountered so that
2852 later, we can match only the proper comment begin
2853 sequence of the same style. */
2854 int c2
, other_syntax
;
2855 DEC_BOTH (from
, from_byte
);
2856 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2858 c2
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2859 other_syntax
= SYNTAX_WITH_FLAGS (c2
);
2860 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2861 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2864 /* Quoting turns anything except a comment-ender
2865 into a word character. Note that this cannot be true
2866 if we decremented FROM in the if-statement above. */
2867 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2869 DEC_BOTH (from
, from_byte
);
2872 else if (SYNTAX_FLAGS_PREFIX (syntax
))
2881 if (depth
|| !sexpflag
) break;
2882 /* This word counts as a sexp; count object finished
2883 after passing it. */
2886 temp_pos
= from_byte
;
2887 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
2891 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2892 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2893 /* Don't allow comment-end to be quoted. */
2894 if (syntax_multibyte (c1
, multibyte_symbol_p
) == Sendcomment
)
2896 quoted
= char_quoted (from
- 1, temp_pos
);
2899 DEC_BOTH (from
, from_byte
);
2900 temp_pos
= dec_bytepos (temp_pos
);
2901 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2903 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2905 switch (syntax_multibyte (c1
, multibyte_symbol_p
))
2907 case Sword
: case Ssymbol
: case Squote
: break;
2908 default: goto done2
;
2910 DEC_BOTH (from
, from_byte
);
2919 temp_pos
= dec_bytepos (from_byte
);
2920 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2921 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (temp_pos
))
2922 DEC_BOTH (from
, from_byte
);
2932 if (!++depth
) goto done2
;
2937 if (!--depth
) goto done2
;
2938 if (depth
< min_depth
)
2939 xsignal3 (Qscan_error
,
2940 build_string ("Containing expression ends prematurely"),
2941 make_number (last_good
), make_number (from
));
2945 if (!parse_sexp_ignore_comments
)
2947 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2948 &out_charpos
, &out_bytepos
);
2949 /* FIXME: if !found, it really wasn't a comment-end.
2950 For single-char Sendcomment, we can't do much about it apart
2951 from skipping the char.
2952 For 2-char endcomments, we could try again, taking both
2953 chars as separate entities, but it's a lot of trouble
2954 for very little gain, so we don't bother either. -sm */
2956 from
= out_charpos
, from_byte
= out_bytepos
;
2959 case Scomment_fence
:
2965 DEC_BOTH (from
, from_byte
);
2966 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2967 if (!char_quoted (from
, from_byte
))
2969 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2970 if (syntax_multibyte (c
, multibyte_symbol_p
) == code
)
2974 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2978 stringterm
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2983 DEC_BOTH (from
, from_byte
);
2984 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2985 if (!char_quoted (from
, from_byte
))
2987 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2989 && (syntax_multibyte (c
, multibyte_symbol_p
)
2994 if (!depth
&& sexpflag
) goto done2
;
2997 /* Ignore whitespace, punctuation, quote, endcomment. */
3002 /* Reached start of buffer. Error if within object, return nil if between */
3015 XSETFASTINT (val
, from
);
3019 xsignal3 (Qscan_error
,
3020 build_string ("Unbalanced parentheses"),
3021 make_number (last_good
), make_number (from
));
3024 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
3025 doc
: /* Scan from character number FROM by COUNT lists.
3026 Scan forward if COUNT is positive, backward if COUNT is negative.
3027 Return the character number of the position thus found.
3029 A \"list", in this context, refers to a balanced parenthetical
3030 grouping, as determined by the syntax table.
3032 If DEPTH is nonzero, treat that as the nesting depth of the starting
3033 point (i.e. the starting point is DEPTH parentheses deep). This
3034 function scans over parentheses until the depth goes to zero COUNT
3035 times. Hence, positive DEPTH moves out that number of levels of
3036 parentheses, while negative DEPTH moves to a deeper level.
3038 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3040 If we reach the beginning or end of the accessible part of the buffer
3041 before we have scanned over COUNT lists, return nil if the depth at
3042 that point is zero, and signal a error if the depth is nonzero. */)
3043 (Lisp_Object from
, Lisp_Object count
, Lisp_Object depth
)
3045 CHECK_NUMBER (from
);
3046 CHECK_NUMBER (count
);
3047 CHECK_NUMBER (depth
);
3049 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
3052 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
3053 doc
: /* Scan from character number FROM by COUNT balanced expressions.
3054 If COUNT is negative, scan backwards.
3055 Returns the character number of the position thus found.
3057 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3059 If the beginning or end of (the accessible part of) the buffer is reached
3060 in the middle of a parenthetical grouping, an error is signaled.
3061 If the beginning or end is reached between groupings
3062 but before count is used up, nil is returned. */)
3063 (Lisp_Object from
, Lisp_Object count
)
3065 CHECK_NUMBER (from
);
3066 CHECK_NUMBER (count
);
3068 return scan_lists (XINT (from
), XINT (count
), 0, 1);
3071 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
3073 doc
: /* Move point backward over any number of chars with prefix syntax.
3074 This includes chars with expression prefix syntax class (') and those with
3075 the prefix syntax flag (p). */)
3078 ptrdiff_t beg
= BEGV
;
3079 ptrdiff_t opoint
= PT
;
3080 ptrdiff_t opoint_byte
= PT_BYTE
;
3082 ptrdiff_t pos_byte
= PT_BYTE
;
3087 SET_PT_BOTH (opoint
, opoint_byte
);
3092 SETUP_SYNTAX_TABLE (pos
, -1);
3094 DEC_BOTH (pos
, pos_byte
);
3096 while (!char_quoted (pos
, pos_byte
)
3097 /* Previous statement updates syntax table. */
3098 && ((c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
), SYNTAX (c
) == Squote
)
3099 || syntax_prefix_flag_p (c
)))
3102 opoint_byte
= pos_byte
;
3105 DEC_BOTH (pos
, pos_byte
);
3108 SET_PT_BOTH (opoint
, opoint_byte
);
3113 /* Parse forward from FROM / FROM_BYTE to END,
3114 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
3115 and return a description of the state of the parse at END.
3116 If STOPBEFORE, stop at the start of an atom.
3117 If COMMENTSTOP is 1, stop at the start of a comment.
3118 If COMMENTSTOP is -1, stop at the start or end of a comment,
3119 after the beginning of a string, or after the end of a string. */
3122 scan_sexps_forward (struct lisp_parse_state
*stateptr
,
3123 ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t end
,
3124 EMACS_INT targetdepth
, bool stopbefore
,
3125 Lisp_Object oldstate
, int commentstop
)
3127 struct lisp_parse_state state
;
3128 enum syntaxcode code
;
3131 struct level
{ ptrdiff_t last
, prev
; };
3132 struct level levelstart
[100];
3133 struct level
*curlevel
= levelstart
;
3134 struct level
*endlevel
= levelstart
+ 100;
3135 EMACS_INT depth
; /* Paren depth of current scanning location.
3136 level - levelstart equals this except
3137 when the depth becomes negative. */
3138 EMACS_INT mindepth
; /* Lowest DEPTH value seen. */
3139 bool start_quoted
= 0; /* True means starting after a char quote. */
3141 ptrdiff_t prev_from
; /* Keep one character before FROM. */
3142 ptrdiff_t prev_from_byte
;
3143 int prev_from_syntax
;
3144 bool boundary_stop
= commentstop
== -1;
3147 ptrdiff_t out_bytepos
, out_charpos
;
3151 prev_from_byte
= from_byte
;
3153 DEC_BOTH (prev_from
, prev_from_byte
);
3155 /* Use this macro instead of `from++'. */
3157 do { prev_from = from; \
3158 prev_from_byte = from_byte; \
3159 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
3160 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
3161 INC_BOTH (from, from_byte); \
3163 UPDATE_SYNTAX_TABLE_FORWARD (from); \
3169 if (NILP (oldstate
))
3172 state
.instring
= -1;
3173 state
.incomment
= 0;
3174 state
.comstyle
= 0; /* comment style a by default. */
3175 state
.comstr_start
= -1; /* no comment/string seen. */
3179 tem
= Fcar (oldstate
);
3185 oldstate
= Fcdr (oldstate
);
3186 oldstate
= Fcdr (oldstate
);
3187 oldstate
= Fcdr (oldstate
);
3188 tem
= Fcar (oldstate
);
3189 /* Check whether we are inside string_fence-style string: */
3190 state
.instring
= (!NILP (tem
)
3191 ? (CHARACTERP (tem
) ? XFASTINT (tem
) : ST_STRING_STYLE
)
3194 oldstate
= Fcdr (oldstate
);
3195 tem
= Fcar (oldstate
);
3196 state
.incomment
= (!NILP (tem
)
3197 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
3200 oldstate
= Fcdr (oldstate
);
3201 tem
= Fcar (oldstate
);
3202 start_quoted
= !NILP (tem
);
3204 /* if the eighth element of the list is nil, we are in comment
3205 style a. If it is non-nil, we are in comment style b */
3206 oldstate
= Fcdr (oldstate
);
3207 oldstate
= Fcdr (oldstate
);
3208 tem
= Fcar (oldstate
);
3209 state
.comstyle
= (NILP (tem
)
3211 : (RANGED_INTEGERP (0, tem
, ST_COMMENT_STYLE
)
3213 : ST_COMMENT_STYLE
));
3215 oldstate
= Fcdr (oldstate
);
3216 tem
= Fcar (oldstate
);
3217 state
.comstr_start
=
3218 RANGED_INTEGERP (PTRDIFF_MIN
, tem
, PTRDIFF_MAX
) ? XINT (tem
) : -1;
3219 oldstate
= Fcdr (oldstate
);
3220 tem
= Fcar (oldstate
);
3221 while (!NILP (tem
)) /* >= second enclosing sexps. */
3223 Lisp_Object temhd
= Fcar (tem
);
3224 if (RANGED_INTEGERP (PTRDIFF_MIN
, temhd
, PTRDIFF_MAX
))
3225 curlevel
->last
= XINT (temhd
);
3226 if (++curlevel
== endlevel
)
3227 curlevel
--; /* error ("Nesting too deep for parser"); */
3228 curlevel
->prev
= -1;
3229 curlevel
->last
= -1;
3236 curlevel
->prev
= -1;
3237 curlevel
->last
= -1;
3239 SETUP_SYNTAX_TABLE (prev_from
, 1);
3240 temp
= FETCH_CHAR (prev_from_byte
);
3241 prev_from_syntax
= SYNTAX_WITH_FLAGS (temp
);
3242 UPDATE_SYNTAX_TABLE_FORWARD (from
);
3244 /* Enter the loop at a place appropriate for initial state. */
3246 if (state
.incomment
)
3247 goto startincomment
;
3248 if (state
.instring
>= 0)
3250 nofence
= state
.instring
!= ST_STRING_STYLE
;
3252 goto startquotedinstring
;
3255 else if (start_quoted
)
3262 code
= prev_from_syntax
& 0xff;
3265 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
)
3266 && (c1
= FETCH_CHAR (from_byte
),
3267 syntax
= SYNTAX_WITH_FLAGS (c1
),
3268 SYNTAX_FLAGS_COMSTART_SECOND (syntax
)))
3269 /* Duplicate code to avoid a complex if-expression
3270 which causes trouble for the SGI compiler. */
3272 /* Record the comment style we have entered so that only
3273 the comment-end sequence of the same style actually
3274 terminates the comment section. */
3276 = SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_from_syntax
);
3277 comnested
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
)
3278 | SYNTAX_FLAGS_COMMENT_NESTED (syntax
));
3279 state
.incomment
= comnested
? 1 : -1;
3280 state
.comstr_start
= prev_from
;
3284 else if (code
== Scomment_fence
)
3286 /* Record the comment style we have entered so that only
3287 the comment-end sequence of the same style actually
3288 terminates the comment section. */
3289 state
.comstyle
= ST_COMMENT_STYLE
;
3290 state
.incomment
= -1;
3291 state
.comstr_start
= prev_from
;
3294 else if (code
== Scomment
)
3296 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
, 0);
3297 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
3299 state
.comstr_start
= prev_from
;
3302 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
3308 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3309 curlevel
->last
= prev_from
;
3311 if (from
== end
) goto endquoted
;
3314 /* treat following character as a word constituent */
3317 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3318 curlevel
->last
= prev_from
;
3322 int symchar
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3323 switch (SYNTAX (symchar
))
3328 if (from
== end
) goto endquoted
;
3340 curlevel
->prev
= curlevel
->last
;
3343 case Scomment_fence
: /* Can't happen because it's handled above. */
3345 if (commentstop
|| boundary_stop
) goto done
;
3347 /* The (from == BEGV) test was to enter the loop in the middle so
3348 that we find a 2-char comment ender even if we start in the
3349 middle of it. We don't want to do that if we're just at the
3350 beginning of the comment (think of (*) ... (*)). */
3351 found
= forw_comment (from
, from_byte
, end
,
3352 state
.incomment
, state
.comstyle
,
3353 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
3354 ? 0 : prev_from_syntax
,
3355 &out_charpos
, &out_bytepos
, &state
.incomment
);
3356 from
= out_charpos
; from_byte
= out_bytepos
;
3357 /* Beware! prev_from and friends are invalid now.
3358 Luckily, the `done' doesn't use them and the INC_FROM
3359 sets them to a sane value without looking at them. */
3360 if (!found
) goto done
;
3362 state
.incomment
= 0;
3363 state
.comstyle
= 0; /* reset the comment style */
3364 if (boundary_stop
) goto done
;
3368 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3370 /* curlevel++->last ran into compiler bug on Apollo */
3371 curlevel
->last
= prev_from
;
3372 if (++curlevel
== endlevel
)
3373 curlevel
--; /* error ("Nesting too deep for parser"); */
3374 curlevel
->prev
= -1;
3375 curlevel
->last
= -1;
3376 if (targetdepth
== depth
) goto done
;
3381 if (depth
< mindepth
)
3383 if (curlevel
!= levelstart
)
3385 curlevel
->prev
= curlevel
->last
;
3386 if (targetdepth
== depth
) goto done
;
3391 state
.comstr_start
= from
- 1;
3392 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3393 curlevel
->last
= prev_from
;
3394 state
.instring
= (code
== Sstring
3395 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte
))
3397 if (boundary_stop
) goto done
;
3400 nofence
= state
.instring
!= ST_STRING_STYLE
;
3405 enum syntaxcode c_code
;
3407 if (from
>= end
) goto done
;
3408 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3409 c_code
= SYNTAX (c
);
3411 /* Check C_CODE here so that if the char has
3412 a syntax-table property which says it is NOT
3413 a string character, it does not end the string. */
3414 if (nofence
&& c
== state
.instring
&& c_code
== Sstring
)
3420 if (!nofence
) goto string_end
;
3425 startquotedinstring
:
3426 if (from
>= end
) goto endquoted
;
3432 state
.instring
= -1;
3433 curlevel
->prev
= curlevel
->last
;
3435 if (boundary_stop
) goto done
;
3439 /* FIXME: We should do something with it. */
3442 /* Ignore whitespace, punctuation, quote, endcomment. */
3448 stop
: /* Here if stopping before start of sexp. */
3449 from
= prev_from
; /* We have just fetched the char that starts it; */
3450 from_byte
= prev_from_byte
;
3451 goto done
; /* but return the position before it. */
3456 state
.depth
= depth
;
3457 state
.mindepth
= mindepth
;
3458 state
.thislevelstart
= curlevel
->prev
;
3459 state
.prevlevelstart
3460 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
3461 state
.location
= from
;
3462 state
.location_byte
= from_byte
;
3463 state
.levelstarts
= Qnil
;
3464 while (curlevel
> levelstart
)
3465 state
.levelstarts
= Fcons (make_number ((--curlevel
)->last
),
3472 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
3473 doc
: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3474 Parsing stops at TO or when certain criteria are met;
3475 point is set to where parsing stops.
3476 If fifth arg OLDSTATE is omitted or nil,
3477 parsing assumes that FROM is the beginning of a function.
3478 Value is a list of elements describing final state of parsing:
3480 1. character address of start of innermost containing list; nil if none.
3481 2. character address of start of last complete sexp terminated.
3482 3. non-nil if inside a string.
3483 (it is the character that will terminate the string,
3484 or t if the string should be terminated by a generic string delimiter.)
3485 4. nil if outside a comment, t if inside a non-nestable comment,
3486 else an integer (the current comment nesting).
3487 5. t if following a quote character.
3488 6. the minimum paren-depth encountered during this scan.
3489 7. style of comment, if any.
3490 8. character address of start of comment or string; nil if not in one.
3491 9. Intermediate data for continuation of parsing (subject to change).
3492 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3493 in parentheses becomes equal to TARGETDEPTH.
3494 Fourth arg STOPBEFORE non-nil means stop when come to
3495 any character that starts a sexp.
3496 Fifth arg OLDSTATE is a list like what this function returns.
3497 It is used to initialize the state of the parse. Elements number 1, 2, 6
3499 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3500 If it is symbol `syntax-table', stop after the start of a comment or a
3501 string, or after end of a comment or a string. */)
3502 (Lisp_Object from
, Lisp_Object to
, Lisp_Object targetdepth
,
3503 Lisp_Object stopbefore
, Lisp_Object oldstate
, Lisp_Object commentstop
)
3505 struct lisp_parse_state state
;
3508 if (!NILP (targetdepth
))
3510 CHECK_NUMBER (targetdepth
);
3511 target
= XINT (targetdepth
);
3514 target
= TYPE_MINIMUM (EMACS_INT
); /* We won't reach this depth. */
3516 validate_region (&from
, &to
);
3517 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
3519 target
, !NILP (stopbefore
), oldstate
,
3521 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
3523 SET_PT_BOTH (state
.location
, state
.location_byte
);
3525 return Fcons (make_number (state
.depth
),
3526 Fcons (state
.prevlevelstart
< 0
3527 ? Qnil
: make_number (state
.prevlevelstart
),
3528 Fcons (state
.thislevelstart
< 0
3529 ? Qnil
: make_number (state
.thislevelstart
),
3530 Fcons (state
.instring
>= 0
3531 ? (state
.instring
== ST_STRING_STYLE
3532 ? Qt
: make_number (state
.instring
)) : Qnil
,
3533 Fcons (state
.incomment
< 0 ? Qt
:
3534 (state
.incomment
== 0 ? Qnil
:
3535 make_number (state
.incomment
)),
3536 Fcons (state
.quoted
? Qt
: Qnil
,
3537 Fcons (make_number (state
.mindepth
),
3538 Fcons ((state
.comstyle
3539 ? (state
.comstyle
== ST_COMMENT_STYLE
3541 : make_number (state
.comstyle
))
3543 Fcons (((state
.incomment
3544 || (state
.instring
>= 0))
3545 ? make_number (state
.comstr_start
)
3547 Fcons (state
.levelstarts
, Qnil
))))))))));
3551 init_syntax_once (void)
3556 /* This has to be done here, before we call Fmake_char_table. */
3557 DEFSYM (Qsyntax_table
, "syntax-table");
3559 /* Create objects which can be shared among syntax tables. */
3560 Vsyntax_code_object
= make_uninit_vector (Smax
);
3561 for (i
= 0; i
< Smax
; i
++)
3562 ASET (Vsyntax_code_object
, i
, Fcons (make_number (i
), Qnil
));
3564 /* Now we are ready to set up this property, so we can
3565 create syntax tables. */
3566 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
3568 temp
= AREF (Vsyntax_code_object
, Swhitespace
);
3570 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
3572 /* Control characters should not be whitespace. */
3573 temp
= AREF (Vsyntax_code_object
, Spunct
);
3574 for (i
= 0; i
<= ' ' - 1; i
++)
3575 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3576 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 0177, temp
);
3578 /* Except that a few really are whitespace. */
3579 temp
= AREF (Vsyntax_code_object
, Swhitespace
);
3580 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ' ', temp
);
3581 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\t', temp
);
3582 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\n', temp
);
3583 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 015, temp
);
3584 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 014, temp
);
3586 temp
= AREF (Vsyntax_code_object
, Sword
);
3587 for (i
= 'a'; i
<= 'z'; i
++)
3588 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3589 for (i
= 'A'; i
<= 'Z'; i
++)
3590 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3591 for (i
= '0'; i
<= '9'; i
++)
3592 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3594 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
3595 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
3597 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
3598 Fcons (make_number (Sopen
), make_number (')')));
3599 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
3600 Fcons (make_number (Sclose
), make_number ('(')));
3601 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
3602 Fcons (make_number (Sopen
), make_number (']')));
3603 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
3604 Fcons (make_number (Sclose
), make_number ('[')));
3605 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
3606 Fcons (make_number (Sopen
), make_number ('}')));
3607 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
3608 Fcons (make_number (Sclose
), make_number ('{')));
3609 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
3610 Fcons (make_number (Sstring
), Qnil
));
3611 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
3612 Fcons (make_number (Sescape
), Qnil
));
3614 temp
= AREF (Vsyntax_code_object
, Ssymbol
);
3615 for (i
= 0; i
< 10; i
++)
3617 c
= "_-+*/&|<>="[i
];
3618 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3621 temp
= AREF (Vsyntax_code_object
, Spunct
);
3622 for (i
= 0; i
< 12; i
++)
3624 c
= ".,;:?!#@~^'`"[i
];
3625 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3628 /* All multibyte characters have syntax `word' by default. */
3629 temp
= AREF (Vsyntax_code_object
, Sword
);
3630 char_table_set_range (Vstandard_syntax_table
, 0x80, MAX_CHAR
, temp
);
3634 syms_of_syntax (void)
3636 DEFSYM (Qsyntax_table_p
, "syntax-table-p");
3638 staticpro (&Vsyntax_code_object
);
3640 staticpro (&gl_state
.object
);
3641 staticpro (&gl_state
.global_code
);
3642 staticpro (&gl_state
.current_syntax_table
);
3643 staticpro (&gl_state
.old_prop
);
3645 /* Defined in regex.c. */
3646 staticpro (&re_match_object
);
3648 DEFSYM (Qscan_error
, "scan-error");
3649 Fput (Qscan_error
, Qerror_conditions
,
3650 listn (CONSTYPE_PURE
, 2, Qscan_error
, Qerror
));
3651 Fput (Qscan_error
, Qerror_message
,
3652 build_pure_c_string ("Scan error"));
3654 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments
,
3655 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3657 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties
,
3658 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3659 Otherwise, that text property is simply ignored.
3660 See the info node `(elisp)Syntax Properties' for a description of the
3661 `syntax-table' property. */);
3663 DEFVAR_INT ("syntax-propertize--done", syntax_propertize__done
,
3664 doc
: /* Position up to which syntax-table properties have been set. */);
3665 syntax_propertize__done
= -1;
3666 DEFSYM (Qinternal__syntax_propertize
, "internal--syntax-propertize");
3668 words_include_escapes
= 0;
3669 DEFVAR_BOOL ("words-include-escapes", words_include_escapes
,
3670 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3672 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol
,
3673 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3674 multibyte_syntax_as_symbol
= 0;
3676 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3677 open_paren_in_column_0_is_defun_start
,
3678 doc
: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3679 open_paren_in_column_0_is_defun_start
= 1;
3682 DEFVAR_LISP ("find-word-boundary-function-table",
3683 Vfind_word_boundary_function_table
,
3685 Char table of functions to search for the word boundary.
3686 Each function is called with two arguments; POS and LIMIT.
3687 POS and LIMIT are character positions in the current buffer.
3689 If POS is less than LIMIT, POS is at the first character of a word,
3690 and the return value of a function is a position after the last
3691 character of that word.
3693 If POS is not less than LIMIT, POS is at the last character of a word,
3694 and the return value of a function is a position at the first
3695 character of that word.
3697 In both cases, LIMIT bounds the search. */);
3698 Vfind_word_boundary_function_table
= Fmake_char_table (Qnil
, Qnil
);
3700 defsubr (&Ssyntax_table_p
);
3701 defsubr (&Ssyntax_table
);
3702 defsubr (&Sstandard_syntax_table
);
3703 defsubr (&Scopy_syntax_table
);
3704 defsubr (&Sset_syntax_table
);
3705 defsubr (&Schar_syntax
);
3706 defsubr (&Smatching_paren
);
3707 defsubr (&Sstring_to_syntax
);
3708 defsubr (&Smodify_syntax_entry
);
3709 defsubr (&Sinternal_describe_syntax_value
);
3711 defsubr (&Sforward_word
);
3713 defsubr (&Sskip_chars_forward
);
3714 defsubr (&Sskip_chars_backward
);
3715 defsubr (&Sskip_syntax_forward
);
3716 defsubr (&Sskip_syntax_backward
);
3718 defsubr (&Sforward_comment
);
3719 defsubr (&Sscan_lists
);
3720 defsubr (&Sscan_sexps
);
3721 defsubr (&Sbackward_prefix_chars
);
3722 defsubr (&Sparse_partial_sexp
);