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>
26 #include "character.h"
30 #include "intervals.h"
33 /* Make syntax table lookup grant data in gl_state. */
34 #define SYNTAX(c) syntax_property (c, 1)
35 #define SYNTAX_ENTRY(c) syntax_property_entry (c, 1)
36 #define SYNTAX_WITH_FLAGS(c) syntax_property_with_flags (c, 1)
38 /* Eight single-bit flags have the following meanings:
39 1. This character is the first of a two-character comment-start sequence.
40 2. This character is the second of a two-character comment-start sequence.
41 3. This character is the first of a two-character comment-end sequence.
42 4. This character is the second of a two-character comment-end sequence.
43 5. This character is a prefix, for backward-prefix-chars.
44 6. The char is part of a delimiter for comments of style "b".
45 7. This character is part of a nestable comment sequence.
46 8. The char is part of a delimiter for comments of style "c".
47 Note that any two-character sequence whose first character has flag 1
48 and whose second character has flag 2 will be interpreted as a comment start.
50 Bits 6 and 8 discriminate among different comment styles.
51 Languages such as C++ allow two orthogonal syntax start/end pairs
52 and bit 6 determines whether a comment-end or Scommentend
53 ends style a or b. Comment markers can start style a, b, c, or bc.
54 Style a is always the default.
55 For 2-char comment markers, the style b flag is looked up only on the second
56 char of the comment marker and on the first char of the comment ender.
57 For style c (like the nested flag), the flag can be placed on any of
60 /* These functions extract specific flags from an integer
61 that holds the syntax code and the flags. */
64 SYNTAX_FLAGS_COMSTART_FIRST (int flags
)
66 return (flags
>> 16) & 1;
69 SYNTAX_FLAGS_COMSTART_SECOND (int flags
)
71 return (flags
>> 17) & 1;
74 SYNTAX_FLAGS_COMEND_FIRST (int flags
)
76 return (flags
>> 18) & 1;
79 SYNTAX_FLAGS_COMEND_SECOND (int flags
)
81 return (flags
>> 19) & 1;
84 SYNTAX_FLAGS_PREFIX (int flags
)
86 return (flags
>> 20) & 1;
89 SYNTAX_FLAGS_COMMENT_STYLEB (int flags
)
91 return (flags
>> 21) & 1;
94 SYNTAX_FLAGS_COMMENT_STYLEC (int flags
)
96 return (flags
>> 23) & 1;
99 SYNTAX_FLAGS_COMMENT_STYLEC2 (int flags
)
101 return (flags
>> 22) & 2; /* SYNTAX_FLAGS_COMMENT_STYLEC (flags) * 2 */
104 SYNTAX_FLAGS_COMMENT_NESTED (int flags
)
106 return (flags
>> 22) & 1;
109 /* FLAGS should be the flags of the main char of the comment marker, e.g.
110 the second for comstart and the first for comend. */
112 SYNTAX_FLAGS_COMMENT_STYLE (int flags
, int other_flags
)
114 return (SYNTAX_FLAGS_COMMENT_STYLEB (flags
)
115 | SYNTAX_FLAGS_COMMENT_STYLEC2 (flags
)
116 | SYNTAX_FLAGS_COMMENT_STYLEC2 (other_flags
));
119 /* Extract a particular flag for a given character. */
122 SYNTAX_COMEND_FIRST (int c
)
124 return SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c
));
127 /* We use these constants in place for comment-style and
128 string-ender-char to distinguish comments/strings started by
129 comment_fence and string_fence codes. */
133 ST_COMMENT_STYLE
= 256 + 1,
134 ST_STRING_STYLE
= 256 + 2
137 /* This is the internal form of the parse state used in parse-partial-sexp. */
139 struct lisp_parse_state
141 EMACS_INT depth
; /* Depth at end of parsing. */
142 int instring
; /* -1 if not within string, else desired terminator. */
143 EMACS_INT incomment
; /* -1 if in unnestable comment else comment nesting */
144 int comstyle
; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
145 bool quoted
; /* True if just after an escape char at end of parsing. */
146 EMACS_INT mindepth
; /* Minimum depth seen while scanning. */
147 /* Char number of most recent start-of-expression at current level */
148 ptrdiff_t thislevelstart
;
149 /* Char number of start of containing expression */
150 ptrdiff_t prevlevelstart
;
151 ptrdiff_t location
; /* Char number at which parsing stopped. */
152 ptrdiff_t location_byte
; /* Corresponding byte position. */
153 ptrdiff_t comstr_start
; /* Position of last comment/string starter. */
154 Lisp_Object levelstarts
; /* Char numbers of starts-of-expression
155 of levels (starting from outermost). */
158 /* These variables are a cache for finding the start of a defun.
159 find_start_pos is the place for which the defun start was found.
160 find_start_value is the defun start position found for it.
161 find_start_value_byte is the corresponding byte position.
162 find_start_buffer is the buffer it was found in.
163 find_start_begv is the BEGV value when it was found.
164 find_start_modiff is the value of MODIFF when it was found. */
166 static ptrdiff_t find_start_pos
;
167 static ptrdiff_t find_start_value
;
168 static ptrdiff_t find_start_value_byte
;
169 static struct buffer
*find_start_buffer
;
170 static ptrdiff_t find_start_begv
;
171 static EMACS_INT find_start_modiff
;
174 static Lisp_Object
skip_chars (bool, Lisp_Object
, Lisp_Object
, bool);
175 static Lisp_Object
skip_syntaxes (bool, Lisp_Object
, Lisp_Object
);
176 static Lisp_Object
scan_lists (EMACS_INT
, EMACS_INT
, EMACS_INT
, bool);
177 static void scan_sexps_forward (struct lisp_parse_state
*,
178 ptrdiff_t, ptrdiff_t, ptrdiff_t, EMACS_INT
,
179 bool, Lisp_Object
, int);
180 static bool in_classes (int, Lisp_Object
);
181 static void parse_sexp_propertize (ptrdiff_t charpos
);
183 /* This setter is used only in this file, so it can be private. */
185 bset_syntax_table (struct buffer
*b
, Lisp_Object val
)
187 b
->syntax_table_
= val
;
190 /* Whether the syntax of the character C has the prefix flag set. */
192 syntax_prefix_flag_p (int c
)
194 return SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c
));
197 struct gl_state_s gl_state
; /* Global state of syntax parser. */
199 enum { INTERVALS_AT_ONCE
= 10 }; /* 1 + max-number of intervals
200 to scan to property-change. */
202 /* Set the syntax entry VAL for char C in table TABLE. */
205 SET_RAW_SYNTAX_ENTRY (Lisp_Object table
, int c
, Lisp_Object val
)
207 CHAR_TABLE_SET (table
, c
, val
);
210 /* Set the syntax entry VAL for char-range RANGE in table TABLE.
211 RANGE is a cons (FROM . TO) specifying the range of characters. */
214 SET_RAW_SYNTAX_ENTRY_RANGE (Lisp_Object table
, Lisp_Object range
,
217 Fset_char_table_range (table
, range
, val
);
220 /* Extract the information from the entry for character C
221 in the current syntax table. */
226 Lisp_Object ent
= SYNTAX_ENTRY (c
);
227 return CONSP (ent
) ? XCDR (ent
) : Qnil
;
230 /* This should be called with FROM at the start of forward
231 search, or after the last position of the backward search. It
232 makes sure that the first char is picked up with correct table, so
233 one does not need to call UPDATE_SYNTAX_TABLE immediately after the
235 Sign of COUNT gives the direction of the search.
239 SETUP_SYNTAX_TABLE (ptrdiff_t from
, ptrdiff_t count
)
241 SETUP_BUFFER_SYNTAX_TABLE ();
242 gl_state
.b_property
= BEGV
;
243 gl_state
.e_property
= ZV
+ 1;
244 gl_state
.object
= Qnil
;
246 if (parse_sexp_lookup_properties
)
249 update_syntax_table_forward (from
, true, Qnil
);
250 else if (from
> BEGV
)
252 update_syntax_table (from
- 1, count
, true, Qnil
);
253 parse_sexp_propertize (from
- 1);
258 /* Same as above, but in OBJECT. If OBJECT is nil, use current buffer.
259 If it is t (which is only used in fast_c_string_match_ignore_case),
260 ignore properties altogether.
262 This is meant for regex.c to use. For buffers, regex.c passes arguments
263 to the UPDATE_SYNTAX_TABLE functions which are relative to BEGV.
264 So if it is a buffer, we set the offset field to BEGV. */
267 SETUP_SYNTAX_TABLE_FOR_OBJECT (Lisp_Object object
,
268 ptrdiff_t from
, ptrdiff_t count
)
270 SETUP_BUFFER_SYNTAX_TABLE ();
271 gl_state
.object
= object
;
272 if (BUFFERP (gl_state
.object
))
274 struct buffer
*buf
= XBUFFER (gl_state
.object
);
275 gl_state
.b_property
= 1;
276 gl_state
.e_property
= BUF_ZV (buf
) - BUF_BEGV (buf
) + 1;
277 gl_state
.offset
= BUF_BEGV (buf
) - 1;
279 else if (NILP (gl_state
.object
))
281 gl_state
.b_property
= 1;
282 gl_state
.e_property
= ZV
- BEGV
+ 1;
283 gl_state
.offset
= BEGV
- 1;
285 else if (EQ (gl_state
.object
, Qt
))
287 gl_state
.b_property
= 0;
288 gl_state
.e_property
= PTRDIFF_MAX
;
293 gl_state
.b_property
= 0;
294 gl_state
.e_property
= 1 + SCHARS (gl_state
.object
);
297 if (parse_sexp_lookup_properties
)
298 update_syntax_table (from
+ gl_state
.offset
- (count
<= 0),
299 count
, 1, gl_state
.object
);
302 /* Update gl_state to an appropriate interval which contains CHARPOS. The
303 sign of COUNT give the relative position of CHARPOS wrt the previously
304 valid interval. If INIT, only [be]_property fields of gl_state are
305 valid at start, the rest is filled basing on OBJECT.
307 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
308 direction than the intervals - or in an interval. We update the
309 current syntax-table basing on the property of this interval, and
310 update the interval to start further than CHARPOS - or be
311 NULL. We also update lim_property to be the next value of
312 charpos to call this subroutine again - or be before/after the
313 start/end of OBJECT. */
316 update_syntax_table (ptrdiff_t charpos
, EMACS_INT count
, bool init
,
319 Lisp_Object tmp_table
;
321 bool invalidate
= true;
326 gl_state
.old_prop
= Qnil
;
327 gl_state
.start
= gl_state
.b_property
;
328 gl_state
.stop
= gl_state
.e_property
;
329 i
= interval_of (charpos
, object
);
330 gl_state
.backward_i
= gl_state
.forward_i
= i
;
334 /* interval_of updates only ->position of the return value, so
335 update the parents manually to speed up update_interval. */
336 while (!NULL_PARENT (i
))
338 if (AM_RIGHT_CHILD (i
))
339 INTERVAL_PARENT (i
)->position
= i
->position
340 - LEFT_TOTAL_LENGTH (i
) + TOTAL_LENGTH (i
) /* right end */
341 - TOTAL_LENGTH (INTERVAL_PARENT (i
))
342 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i
));
344 INTERVAL_PARENT (i
)->position
= i
->position
- LEFT_TOTAL_LENGTH (i
)
346 i
= INTERVAL_PARENT (i
);
348 i
= gl_state
.forward_i
;
349 gl_state
.b_property
= i
->position
- gl_state
.offset
;
350 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
353 i
= count
> 0 ? gl_state
.forward_i
: gl_state
.backward_i
;
355 /* We are guaranteed to be called with CHARPOS either in i,
358 error ("Error in syntax_table logic for to-the-end intervals");
359 else if (charpos
< i
->position
) /* Move left. */
362 error ("Error in syntax_table logic for intervals <-");
363 /* Update the interval. */
364 i
= update_interval (i
, charpos
);
365 if (INTERVAL_LAST_POS (i
) != gl_state
.b_property
)
368 gl_state
.forward_i
= i
;
369 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
372 else if (charpos
>= INTERVAL_LAST_POS (i
)) /* Move right. */
375 error ("Error in syntax_table logic for intervals ->");
376 /* Update the interval. */
377 i
= update_interval (i
, charpos
);
378 if (i
->position
!= gl_state
.e_property
)
381 gl_state
.backward_i
= i
;
382 gl_state
.b_property
= i
->position
- gl_state
.offset
;
387 tmp_table
= textget (i
->plist
, Qsyntax_table
);
390 invalidate
= !EQ (tmp_table
, gl_state
.old_prop
); /* Need to invalidate? */
392 if (invalidate
) /* Did not get to adjacent interval. */
393 { /* with the same table => */
394 /* invalidate the old range. */
397 gl_state
.backward_i
= i
;
398 gl_state
.b_property
= i
->position
- gl_state
.offset
;
402 gl_state
.forward_i
= i
;
403 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
407 if (!EQ (tmp_table
, gl_state
.old_prop
))
409 gl_state
.current_syntax_table
= tmp_table
;
410 gl_state
.old_prop
= tmp_table
;
411 if (EQ (Fsyntax_table_p (tmp_table
), Qt
))
413 gl_state
.use_global
= 0;
415 else if (CONSP (tmp_table
))
417 gl_state
.use_global
= 1;
418 gl_state
.global_code
= tmp_table
;
422 gl_state
.use_global
= 0;
423 gl_state
.current_syntax_table
= BVAR (current_buffer
, syntax_table
);
429 if (cnt
&& !EQ (tmp_table
, textget (i
->plist
, Qsyntax_table
)))
433 gl_state
.e_property
= i
->position
- gl_state
.offset
;
434 gl_state
.forward_i
= i
;
439 = i
->position
+ LENGTH (i
) - gl_state
.offset
;
440 gl_state
.backward_i
= i
;
444 else if (cnt
== INTERVALS_AT_ONCE
)
449 = i
->position
+ LENGTH (i
) - gl_state
.offset
450 /* e_property at EOB is not set to ZV but to ZV+1, so that
451 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
452 having to check eob between the two. */
453 + (next_interval (i
) ? 0 : 1);
454 gl_state
.forward_i
= i
;
458 gl_state
.b_property
= i
->position
- gl_state
.offset
;
459 gl_state
.backward_i
= i
;
464 i
= count
> 0 ? next_interval (i
) : previous_interval (i
);
466 eassert (i
== NULL
); /* This property goes to the end. */
469 gl_state
.e_property
= gl_state
.stop
;
470 gl_state
.forward_i
= i
;
473 gl_state
.b_property
= gl_state
.start
;
477 parse_sexp_propertize (ptrdiff_t charpos
)
480 if (syntax_propertize__done
<= charpos
481 && syntax_propertize__done
< zv
)
483 EMACS_INT modiffs
= CHARS_MODIFF
;
484 safe_call1 (Qinternal__syntax_propertize
,
485 make_number (min (zv
, 1 + charpos
)));
486 if (modiffs
!= CHARS_MODIFF
)
487 error ("parse-sexp-propertize-function modified the buffer!");
488 if (syntax_propertize__done
<= charpos
489 && syntax_propertize__done
< zv
)
490 error ("parse-sexp-propertize-function did not move"
491 " syntax-propertize--done");
492 SETUP_SYNTAX_TABLE (charpos
, 1);
494 else if (gl_state
.e_property
> syntax_propertize__done
)
496 gl_state
.e_property
= syntax_propertize__done
;
497 gl_state
.e_property_truncated
= true;
499 else if (gl_state
.e_property_truncated
500 && gl_state
.e_property
< syntax_propertize__done
)
501 { /* When moving backward, e_property might be set without resetting
502 e_property_truncated, so the e_property_truncated flag may
503 occasionally be left raised spuriously. This should be rare. */
504 gl_state
.e_property_truncated
= false;
505 update_syntax_table_forward (charpos
, false, Qnil
);
510 update_syntax_table_forward (ptrdiff_t charpos
, bool init
,
513 if (gl_state
.e_property_truncated
)
515 eassert (NILP (object
));
516 eassert (charpos
>= gl_state
.e_property
);
517 parse_sexp_propertize (charpos
);
521 update_syntax_table (charpos
, 1, init
, object
);
522 if (NILP (object
) && gl_state
.e_property
> syntax_propertize__done
)
523 parse_sexp_propertize (charpos
);
527 /* Returns true if char at CHARPOS is quoted.
528 Global syntax-table data should be set up already to be good at CHARPOS
529 or after. On return global syntax data is good for lookup at CHARPOS. */
532 char_quoted (ptrdiff_t charpos
, ptrdiff_t bytepos
)
534 enum syntaxcode code
;
535 ptrdiff_t beg
= BEGV
;
537 ptrdiff_t orig
= charpos
;
539 while (charpos
> beg
)
542 DEC_BOTH (charpos
, bytepos
);
544 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
);
545 c
= FETCH_CHAR_AS_MULTIBYTE (bytepos
);
547 if (! (code
== Scharquote
|| code
== Sescape
))
553 UPDATE_SYNTAX_TABLE (orig
);
557 /* Return the bytepos one character before BYTEPOS.
558 We assume that BYTEPOS is not at the start of the buffer. */
561 dec_bytepos (ptrdiff_t bytepos
)
563 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
570 /* Return a defun-start position before POS and not too far before.
571 It should be the last one before POS, or nearly the last.
573 When open_paren_in_column_0_is_defun_start is nonzero,
574 only the beginning of the buffer is treated as a defun-start.
576 We record the information about where the scan started
577 and what its result was, so that another call in the same area
578 can return the same value very quickly.
580 There is no promise at which position the global syntax data is
581 valid on return from the subroutine, so the caller should explicitly
582 update the global data. */
585 find_defun_start (ptrdiff_t pos
, ptrdiff_t pos_byte
)
587 ptrdiff_t opoint
= PT
, opoint_byte
= PT_BYTE
;
589 /* Use previous finding, if it's valid and applies to this inquiry. */
590 if (current_buffer
== find_start_buffer
591 /* Reuse the defun-start even if POS is a little farther on.
592 POS might be in the next defun, but that's ok.
593 Our value may not be the best possible, but will still be usable. */
594 && pos
<= find_start_pos
+ 1000
595 && pos
>= find_start_value
596 && BEGV
== find_start_begv
597 && MODIFF
== find_start_modiff
)
598 return find_start_value
;
600 if (!open_paren_in_column_0_is_defun_start
)
602 find_start_value
= BEGV
;
603 find_start_value_byte
= BEGV_BYTE
;
607 /* Back up to start of line. */
608 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
610 /* We optimize syntax-table lookup for rare updates. Thus we accept
611 only those `^\s(' which are good in global _and_ text-property
613 SETUP_BUFFER_SYNTAX_TABLE ();
618 /* Open-paren at start of line means we may have found our
620 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
621 if (SYNTAX (c
) == Sopen
)
623 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
624 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
625 if (SYNTAX (c
) == Sopen
)
627 /* Now fallback to the default value. */
628 SETUP_BUFFER_SYNTAX_TABLE ();
630 /* Move to beg of previous line. */
631 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
634 /* Record what we found, for the next try. */
635 find_start_value
= PT
;
636 find_start_value_byte
= PT_BYTE
;
637 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
640 find_start_buffer
= current_buffer
;
641 find_start_modiff
= MODIFF
;
642 find_start_begv
= BEGV
;
643 find_start_pos
= pos
;
645 return find_start_value
;
648 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
651 prev_char_comend_first (ptrdiff_t pos
, ptrdiff_t pos_byte
)
656 DEC_BOTH (pos
, pos_byte
);
657 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
658 c
= FETCH_CHAR (pos_byte
);
659 val
= SYNTAX_COMEND_FIRST (c
);
660 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
664 /* Check whether charpos FROM is at the end of a comment.
665 FROM_BYTE is the bytepos corresponding to FROM.
666 Do not move back before STOP.
668 Return true if we find a comment ending at FROM/FROM_BYTE.
670 If successful, store the charpos of the comment's beginning
671 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
673 Global syntax data remains valid for backward search starting at
674 the returned value (or at FROM, if the search was not successful). */
677 back_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
,
678 bool comnested
, int comstyle
, ptrdiff_t *charpos_ptr
,
679 ptrdiff_t *bytepos_ptr
)
681 /* Look back, counting the parity of string-quotes,
682 and recording the comment-starters seen.
683 When we reach a safe place, assume that's not in a string;
684 then step the main scan to the earliest comment-starter seen
685 an even number of string quotes away from the safe place.
687 OFROM[I] is position of the earliest comment-starter seen
688 which is I+2X quotes from the comment-end.
689 PARITY is current parity of quotes from the comment end. */
690 int string_style
= -1; /* Presumed outside of any string. */
691 bool string_lossage
= 0;
692 /* Not a real lossage: indicates that we have passed a matching comment
693 starter plus a non-matching comment-ender, meaning that any matching
694 comment-starter we might see later could be a false positive (hidden
695 inside another comment).
696 Test case: { a (* b } c (* d *) */
697 bool comment_lossage
= 0;
698 ptrdiff_t comment_end
= from
;
699 ptrdiff_t comment_end_byte
= from_byte
;
700 ptrdiff_t comstart_pos
= 0;
701 ptrdiff_t comstart_byte
IF_LINT (= 0);
702 /* Place where the containing defun starts,
703 or 0 if we didn't come across it yet. */
704 ptrdiff_t defun_start
= 0;
705 ptrdiff_t defun_start_byte
= 0;
706 enum syntaxcode code
;
707 ptrdiff_t nesting
= 1; /* Current comment nesting. */
711 /* FIXME: A }} comment-ender style leads to incorrect behavior
712 in the case of {{ c }}} because we ignore the last two chars which are
713 assumed to be comment-enders although they aren't. */
715 /* At beginning of range to scan, we're outside of strings;
716 that determines quote parity to the comment-end. */
721 bool com2start
, com2end
, comstart
;
723 /* Move back and examine a character. */
724 DEC_BOTH (from
, from_byte
);
725 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
727 prev_syntax
= syntax
;
728 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
729 syntax
= SYNTAX_WITH_FLAGS (c
);
732 /* Check for 2-char comment markers. */
733 com2start
= (SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
734 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax
)
736 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax
, syntax
))
737 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
738 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
);
739 com2end
= (SYNTAX_FLAGS_COMEND_FIRST (syntax
)
740 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax
));
741 comstart
= (com2start
|| code
== Scomment
);
743 /* Nasty cases with overlapping 2-char comment markers:
744 - snmp-mode: -- c -- foo -- c --
752 /* If a 2-char comment sequence partly overlaps with another,
753 we don't try to be clever. E.g. |*| in C, or }% in modes that
754 have %..\n and %{..}%. */
755 if (from
> stop
&& (com2end
|| comstart
))
757 ptrdiff_t next
= from
, next_byte
= from_byte
;
758 int next_c
, next_syntax
;
759 DEC_BOTH (next
, next_byte
);
760 UPDATE_SYNTAX_TABLE_BACKWARD (next
);
761 next_c
= FETCH_CHAR_AS_MULTIBYTE (next_byte
);
762 next_syntax
= SYNTAX_WITH_FLAGS (next_c
);
763 if (((comstart
|| comnested
)
764 && SYNTAX_FLAGS_COMEND_SECOND (syntax
)
765 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax
))
766 || ((com2end
|| comnested
)
767 && SYNTAX_FLAGS_COMSTART_SECOND (syntax
)
769 == SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_syntax
))
770 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax
)))
772 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
775 if (com2start
&& comstart_pos
== 0)
776 /* We're looking at a comment starter. But it might be a comment
777 ender as well (see snmp-mode). The first time we see one, we
778 need to consider it as a comment starter,
779 and the subsequent times as a comment ender. */
782 /* Turn a 2-char comment sequences into the appropriate syntax. */
787 /* Ignore comment starters of a different style. */
788 else if (code
== Scomment
789 && (comstyle
!= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0)
790 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
) != comnested
))
793 /* Ignore escaped characters, except comment-enders. */
794 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
801 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
803 /* Track parity of quotes. */
804 if (string_style
== -1)
805 /* Entering a string. */
807 else if (string_style
== c
)
808 /* Leaving the string. */
811 /* If we have two kinds of string delimiters.
812 There's no way to grok this scanning backwards. */
817 /* We've already checked that it is the relevant comstyle. */
818 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
819 /* There are odd string quotes involved, so let's be careful.
820 Test case in Pascal: " { " a { " } */
825 /* Record best comment-starter so far. */
827 comstart_byte
= from_byte
;
829 else if (--nesting
<= 0)
830 /* nested comments have to be balanced, so we don't need to
831 keep looking for earlier ones. We use here the same (slightly
832 incorrect) reasoning as below: since it is followed by uniform
833 paired string quotes, this comment-start has to be outside of
834 strings, else the comment-end itself would be inside a string. */
839 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == comstyle
840 && ((com2end
&& SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
))
841 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
842 /* This is the same style of comment ender as ours. */
847 /* Anything before that can't count because it would match
848 this comment-ender rather than ours. */
849 from
= stop
; /* Break out of the loop. */
851 else if (comstart_pos
!= 0 || c
!= '\n')
852 /* We're mixing comment styles here, so we'd better be careful.
853 The (comstart_pos != 0 || c != '\n') check is not quite correct
854 (we should just always set comment_lossage), but removing it
855 would imply that any multiline comment in C would go through
856 lossage, which seems overkill.
857 The failure should only happen in the rare cases such as
863 /* Assume a defun-start point is outside of strings. */
864 if (open_paren_in_column_0_is_defun_start
866 || (temp_byte
= dec_bytepos (from_byte
),
867 FETCH_CHAR (temp_byte
) == '\n')))
870 defun_start_byte
= from_byte
;
871 from
= stop
; /* Break out of the loop. */
880 if (comstart_pos
== 0)
883 from_byte
= comment_end_byte
;
884 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
);
886 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
887 or `done'), then we've found the beginning of the non-nested comment. */
888 else if (1) /* !comnested */
891 from_byte
= comstart_byte
;
892 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
896 struct lisp_parse_state state
;
897 bool adjusted
= true;
898 /* We had two kinds of string delimiters mixed up
899 together. Decode this going forwards.
900 Scan fwd from a known safe place (beginning-of-defun)
901 to the one in question; this records where we
902 last passed a comment starter. */
903 /* If we did not already find the defun start, find it now. */
904 if (defun_start
== 0)
906 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
907 defun_start_byte
= find_start_value_byte
;
908 adjusted
= (defun_start
> BEGV
);
912 scan_sexps_forward (&state
,
913 defun_start
, defun_start_byte
,
914 comment_end
, TYPE_MINIMUM (EMACS_INT
),
916 defun_start
= comment_end
;
921 = CONSP (state
.levelstarts
) ? XINT (XCAR (state
.levelstarts
))
922 : state
.thislevelstart
>= 0 ? state
.thislevelstart
924 find_start_value_byte
= CHAR_TO_BYTE (find_start_value
);
927 if (state
.incomment
== (comnested
? 1 : -1)
928 && state
.comstyle
== comstyle
)
929 from
= state
.comstr_start
;
934 /* If comment_end is inside some other comment, maybe ours
935 is nested, so we need to try again from within the
936 surrounding comment. Example: { a (* " *) */
938 /* FIXME: We should advance by one or two chars. */
939 defun_start
= state
.comstr_start
+ 2;
940 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
943 } while (defun_start
< comment_end
);
945 from_byte
= CHAR_TO_BYTE (from
);
946 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
951 *bytepos_ptr
= from_byte
;
953 return from
!= comment_end
;
956 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
957 doc
: /* Return t if OBJECT is a syntax table.
958 Currently, any char-table counts as a syntax table. */)
961 if (CHAR_TABLE_P (object
)
962 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
968 check_syntax_table (Lisp_Object obj
)
970 CHECK_TYPE (CHAR_TABLE_P (obj
) && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
),
971 Qsyntax_table_p
, obj
);
974 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
975 doc
: /* Return the current syntax table.
976 This is the one specified by the current buffer. */)
979 return BVAR (current_buffer
, syntax_table
);
982 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
983 Sstandard_syntax_table
, 0, 0, 0,
984 doc
: /* Return the standard syntax table.
985 This is the one used for new buffers. */)
988 return Vstandard_syntax_table
;
991 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
992 doc
: /* Construct a new syntax table and return it.
993 It is a copy of the TABLE, which defaults to the standard syntax table. */)
999 check_syntax_table (table
);
1001 table
= Vstandard_syntax_table
;
1003 copy
= Fcopy_sequence (table
);
1005 /* Only the standard syntax table should have a default element.
1006 Other syntax tables should inherit from parents instead. */
1007 set_char_table_defalt (copy
, Qnil
);
1009 /* Copied syntax tables should all have parents.
1010 If we copied one with no parent, such as the standard syntax table,
1011 use the standard syntax table as the copy's parent. */
1012 if (NILP (XCHAR_TABLE (copy
)->parent
))
1013 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
1017 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
1018 doc
: /* Select a new syntax table for the current buffer.
1019 One argument, a syntax table. */)
1023 check_syntax_table (table
);
1024 bset_syntax_table (current_buffer
, table
);
1025 /* Indicate that this buffer now has a specified syntax table. */
1026 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
1027 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
1031 /* Convert a letter which signifies a syntax code
1032 into the code it signifies.
1033 This is used by modify-syntax-entry, and other things. */
1035 unsigned char const syntax_spec_code
[0400] =
1036 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1037 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1038 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1039 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1040 Swhitespace
, Scomment_fence
, Sstring
, 0377, Smath
, 0377, 0377, Squote
,
1041 Sopen
, Sclose
, 0377, 0377, 0377, Swhitespace
, Spunct
, Scharquote
,
1042 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1043 0377, 0377, 0377, 0377, Scomment
, 0377, Sendcomment
, 0377,
1044 Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
1045 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1046 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword
,
1047 0377, 0377, 0377, 0377, Sescape
, 0377, 0377, Ssymbol
,
1048 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
1049 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1050 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword
,
1051 0377, 0377, 0377, 0377, Sstring_fence
, 0377, 0377, 0377
1054 /* Indexed by syntax code, give the letter that describes it. */
1056 char const syntax_code_spec
[16] =
1058 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
1062 /* Indexed by syntax code, give the object (cons of syntax code and
1063 nil) to be stored in syntax table. Since these objects can be
1064 shared among syntax tables, we generate them in advance. By
1065 sharing objects, the function `describe-syntax' can give a more
1067 static Lisp_Object Vsyntax_code_object
;
1070 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
1071 doc
: /* Return the syntax code of CHARACTER, described by a character.
1072 For example, if CHARACTER is a word constituent, the
1073 character `w' (119) is returned.
1074 The characters that correspond to various syntax codes
1075 are listed in the documentation of `modify-syntax-entry'. */)
1076 (Lisp_Object character
)
1079 CHECK_CHARACTER (character
);
1080 char_int
= XINT (character
);
1081 SETUP_BUFFER_SYNTAX_TABLE ();
1082 return make_number (syntax_code_spec
[SYNTAX (char_int
)]);
1085 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
1086 doc
: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
1087 (Lisp_Object character
)
1090 enum syntaxcode code
;
1091 CHECK_CHARACTER (character
);
1092 char_int
= XINT (character
);
1093 SETUP_BUFFER_SYNTAX_TABLE ();
1094 code
= SYNTAX (char_int
);
1095 if (code
== Sopen
|| code
== Sclose
)
1096 return SYNTAX_MATCH (char_int
);
1100 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
1101 doc
: /* Convert a syntax descriptor STRING into a raw syntax descriptor.
1102 STRING should be a string of the form allowed as argument of
1103 `modify-syntax-entry'. The return value is a raw syntax descriptor: a
1104 cons cell (CODE . MATCHING-CHAR) which can be used, for example, as
1105 the value of a `syntax-table' text property. */)
1106 (Lisp_Object string
)
1108 const unsigned char *p
;
1112 CHECK_STRING (string
);
1115 val
= syntax_spec_code
[*p
++];
1117 error ("Invalid syntax description letter: %c", p
[-1]);
1119 if (val
== Sinherit
)
1125 int character
= STRING_CHAR_AND_LENGTH (p
, len
);
1126 XSETINT (match
, character
);
1127 if (XFASTINT (match
) == ' ')
1170 if (val
< ASIZE (Vsyntax_code_object
) && NILP (match
))
1171 return AREF (Vsyntax_code_object
, val
);
1173 /* Since we can't use a shared object, let's make a new one. */
1174 return Fcons (make_number (val
), match
);
1177 /* I really don't know why this is interactive
1178 help-form should at least be made useful whilst reading the second arg. */
1179 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
1180 "cSet syntax for character: \nsSet syntax for %s to: ",
1181 doc
: /* Set syntax for character CHAR according to string NEWENTRY.
1182 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1183 the current buffer's syntax table.
1184 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1185 in the range MIN to MAX are changed.
1186 The first character of NEWENTRY should be one of the following:
1187 Space or - whitespace syntax. w word constituent.
1188 _ symbol constituent. . punctuation.
1189 ( open-parenthesis. ) close-parenthesis.
1190 " string quote. \\ escape.
1191 $ paired delimiter. \\=' expression quote or prefix operator.
1192 < comment starter. > comment ender.
1193 / character-quote. @ inherit from parent table.
1194 | generic string fence. ! generic comment fence.
1196 Only single-character comment start and end sequences are represented thus.
1197 Two-character sequences are represented as described below.
1198 The second character of NEWENTRY is the matching parenthesis,
1199 used only if the first character is `(' or `)'.
1200 Any additional characters are flags.
1201 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1202 1 means CHAR is the start of a two-char comment start sequence.
1203 2 means CHAR is the second character of such a sequence.
1204 3 means CHAR is the start of a two-char comment end sequence.
1205 4 means CHAR is the second character of such a sequence.
1207 There can be several orthogonal comment sequences. This is to support
1208 language modes such as C++. By default, all comment sequences are of style
1209 a, but you can set the comment sequence style to b (on the second character
1210 of a comment-start, and the first character of a comment-end sequence) and/or
1211 c (on any of its chars) using this flag:
1212 b means CHAR is part of comment sequence b.
1213 c means CHAR is part of comment sequence c.
1214 n means CHAR is part of a nestable comment sequence.
1216 p means CHAR is a prefix character for `backward-prefix-chars';
1217 such characters are treated as whitespace when they occur
1218 between expressions.
1219 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1220 (Lisp_Object c
, Lisp_Object newentry
, Lisp_Object syntax_table
)
1224 CHECK_CHARACTER_CAR (c
);
1225 CHECK_CHARACTER_CDR (c
);
1228 CHECK_CHARACTER (c
);
1230 if (NILP (syntax_table
))
1231 syntax_table
= BVAR (current_buffer
, syntax_table
);
1233 check_syntax_table (syntax_table
);
1235 newentry
= Fstring_to_syntax (newentry
);
1237 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table
, c
, newentry
);
1239 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), newentry
);
1241 /* We clear the regexp cache, since character classes can now have
1242 different values from those in the compiled regexps.*/
1243 clear_regexp_cache ();
1248 /* Dump syntax table to buffer in human-readable format */
1250 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value
,
1251 Sinternal_describe_syntax_value
, 1, 1, 0,
1252 doc
: /* Insert a description of the internal syntax description SYNTAX at point. */)
1253 (Lisp_Object syntax
)
1255 int code
, syntax_code
;
1256 bool start1
, start2
, end1
, end2
, prefix
, comstyleb
, comstylec
, comnested
;
1258 Lisp_Object first
, match_lisp
, value
= syntax
;
1262 insert_string ("default");
1266 if (CHAR_TABLE_P (value
))
1268 insert_string ("deeper char-table ...");
1274 insert_string ("invalid");
1278 first
= XCAR (value
);
1279 match_lisp
= XCDR (value
);
1281 if (!INTEGERP (first
) || !(NILP (match_lisp
) || CHARACTERP (match_lisp
)))
1283 insert_string ("invalid");
1287 syntax_code
= XINT (first
) & INT_MAX
;
1288 code
= syntax_code
& 0377;
1289 start1
= SYNTAX_FLAGS_COMSTART_FIRST (syntax_code
);
1290 start2
= SYNTAX_FLAGS_COMSTART_SECOND (syntax_code
);
1291 end1
= SYNTAX_FLAGS_COMEND_FIRST (syntax_code
);
1292 end2
= SYNTAX_FLAGS_COMEND_SECOND (syntax_code
);
1293 prefix
= SYNTAX_FLAGS_PREFIX (syntax_code
);
1294 comstyleb
= SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code
);
1295 comstylec
= SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code
);
1296 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax_code
);
1300 insert_string ("invalid");
1304 str
[0] = syntax_code_spec
[code
], str
[1] = 0;
1307 if (NILP (match_lisp
))
1310 insert_char (XINT (match_lisp
));
1331 insert_string ("\twhich means: ");
1336 insert_string ("whitespace"); break;
1338 insert_string ("punctuation"); break;
1340 insert_string ("word"); break;
1342 insert_string ("symbol"); break;
1344 insert_string ("open"); break;
1346 insert_string ("close"); break;
1348 insert_string ("prefix"); break;
1350 insert_string ("string"); break;
1352 insert_string ("math"); break;
1354 insert_string ("escape"); break;
1356 insert_string ("charquote"); break;
1358 insert_string ("comment"); break;
1360 insert_string ("endcomment"); break;
1362 insert_string ("inherit"); break;
1363 case Scomment_fence
:
1364 insert_string ("comment fence"); break;
1366 insert_string ("string fence"); break;
1368 insert_string ("invalid");
1372 if (!NILP (match_lisp
))
1374 insert_string (", matches ");
1375 insert_char (XINT (match_lisp
));
1379 insert_string (",\n\t is the first character of a comment-start sequence");
1381 insert_string (",\n\t is the second character of a comment-start sequence");
1384 insert_string (",\n\t is the first character of a comment-end sequence");
1386 insert_string (",\n\t is the second character of a comment-end sequence");
1388 insert_string (" (comment style b)");
1390 insert_string (" (comment style c)");
1392 insert_string (" (nestable)");
1396 AUTO_STRING (prefixdoc
,
1397 ",\n\t is a prefix character for `backward-prefix-chars'");
1398 insert1 (Fsubstitute_command_keys (prefixdoc
));
1404 /* Return the position across COUNT words from FROM.
1405 If that many words cannot be found before the end of the buffer, return 0.
1406 COUNT negative means scan backward and stop at word beginning. */
1409 scan_words (register ptrdiff_t from
, register EMACS_INT count
)
1411 register ptrdiff_t beg
= BEGV
;
1412 register ptrdiff_t end
= ZV
;
1413 register ptrdiff_t from_byte
= CHAR_TO_BYTE (from
);
1414 register enum syntaxcode code
;
1416 Lisp_Object func
, pos
;
1421 SETUP_SYNTAX_TABLE (from
, count
);
1432 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1433 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1434 code
= SYNTAX (ch0
);
1435 INC_BOTH (from
, from_byte
);
1436 if (words_include_escapes
1437 && (code
== Sescape
|| code
== Scharquote
))
1442 /* Now CH0 is a character which begins a word and FROM is the
1443 position of the next character. */
1444 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch0
);
1445 if (! NILP (Ffboundp (func
)))
1447 pos
= call2 (func
, make_number (from
- 1), make_number (end
));
1448 if (INTEGERP (pos
) && from
< XINT (pos
) && XINT (pos
) <= ZV
)
1451 from_byte
= CHAR_TO_BYTE (from
);
1458 if (from
== end
) break;
1459 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1460 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1461 code
= SYNTAX (ch1
);
1463 && (! words_include_escapes
1464 || (code
!= Sescape
&& code
!= Scharquote
)))
1465 || word_boundary_p (ch0
, ch1
))
1467 INC_BOTH (from
, from_byte
);
1482 DEC_BOTH (from
, from_byte
);
1483 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1484 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1485 code
= SYNTAX (ch1
);
1486 if (words_include_escapes
1487 && (code
== Sescape
|| code
== Scharquote
))
1492 /* Now CH1 is a character which ends a word and FROM is the
1494 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch1
);
1495 if (! NILP (Ffboundp (func
)))
1497 pos
= call2 (func
, make_number (from
), make_number (beg
));
1498 if (INTEGERP (pos
) && BEGV
<= XINT (pos
) && XINT (pos
) < from
)
1501 from_byte
= CHAR_TO_BYTE (from
);
1510 DEC_BOTH (from
, from_byte
);
1511 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1512 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1513 code
= SYNTAX (ch0
);
1515 && (! words_include_escapes
1516 || (code
!= Sescape
&& code
!= Scharquote
)))
1517 || word_boundary_p (ch0
, ch1
))
1519 INC_BOTH (from
, from_byte
);
1533 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 0, 1, "^p",
1534 doc
: /* Move point forward ARG words (backward if ARG is negative).
1535 If ARG is omitted or nil, move point forward one word.
1537 If an edge of the buffer or a field boundary is reached, point is left there
1538 and the function returns nil. Field boundaries are not noticed if
1539 `inhibit-field-text-motion' is non-nil. */)
1543 ptrdiff_t orig_val
, val
;
1546 XSETFASTINT (arg
, 1);
1550 val
= orig_val
= scan_words (PT
, XINT (arg
));
1552 val
= XINT (arg
) > 0 ? ZV
: BEGV
;
1554 /* Avoid jumping out of an input field. */
1555 tmp
= Fconstrain_to_field (make_number (val
), make_number (PT
),
1557 val
= XFASTINT (tmp
);
1560 return val
== orig_val
? Qt
: Qnil
;
1563 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1564 doc
: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1565 STRING is like the inside of a `[...]' in a regular expression
1566 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1567 (but not at the end of a range; quoting is never needed there).
1568 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1569 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1570 Char classes, e.g. `[:alpha:]', are supported.
1572 Returns the distance traveled, either zero or positive. */)
1573 (Lisp_Object string
, Lisp_Object lim
)
1575 return skip_chars (1, string
, lim
, 1);
1578 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1579 doc
: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1580 See `skip-chars-forward' for details.
1581 Returns the distance traveled, either zero or negative. */)
1582 (Lisp_Object string
, Lisp_Object lim
)
1584 return skip_chars (0, string
, lim
, 1);
1587 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1588 doc
: /* Move point forward across chars in specified syntax classes.
1589 SYNTAX is a string of syntax code characters.
1590 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1591 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1592 This function returns the distance traveled, either zero or positive. */)
1593 (Lisp_Object syntax
, Lisp_Object lim
)
1595 return skip_syntaxes (1, syntax
, lim
);
1598 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1599 doc
: /* Move point backward across chars in specified syntax classes.
1600 SYNTAX is a string of syntax code characters.
1601 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1602 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1603 This function returns either zero or a negative number, and the absolute value
1604 of this is the distance traveled. */)
1605 (Lisp_Object syntax
, Lisp_Object lim
)
1607 return skip_syntaxes (0, syntax
, lim
);
1611 skip_chars (bool forwardp
, Lisp_Object string
, Lisp_Object lim
,
1612 bool handle_iso_classes
)
1616 /* Store the ranges of non-ASCII characters. */
1617 int *char_ranges
IF_LINT (= NULL
);
1618 int n_char_ranges
= 0;
1620 ptrdiff_t i
, i_byte
;
1621 /* True if the current buffer is multibyte and the region contains
1624 /* True if STRING is multibyte and it contains non-ASCII chars. */
1625 bool string_multibyte
;
1626 ptrdiff_t size_byte
;
1627 const unsigned char *str
;
1629 Lisp_Object iso_classes
;
1632 CHECK_STRING (string
);
1636 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1638 CHECK_NUMBER_COERCE_MARKER (lim
);
1640 /* In any case, don't allow scan outside bounds of buffer. */
1641 if (XINT (lim
) > ZV
)
1642 XSETFASTINT (lim
, ZV
);
1643 if (XINT (lim
) < BEGV
)
1644 XSETFASTINT (lim
, BEGV
);
1646 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1647 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1648 string_multibyte
= SBYTES (string
) > SCHARS (string
);
1650 memset (fastmap
, 0, sizeof fastmap
);
1652 str
= SDATA (string
);
1653 size_byte
= SBYTES (string
);
1656 if (i_byte
< size_byte
1657 && SREF (string
, 0) == '^')
1659 negate
= 1; i_byte
++;
1662 /* Find the characters specified and set their elements of fastmap.
1663 Handle backslashes and ranges specially.
1665 If STRING contains non-ASCII characters, setup char_ranges for
1666 them and use fastmap only for their leading codes. */
1668 if (! string_multibyte
)
1670 bool string_has_eight_bit
= 0;
1672 /* At first setup fastmap. */
1673 while (i_byte
< size_byte
)
1677 if (handle_iso_classes
&& c
== '['
1678 && i_byte
< size_byte
1679 && str
[i_byte
] == ':')
1681 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1682 const unsigned char *class_end
= class_beg
;
1683 const unsigned char *class_limit
= str
+ size_byte
- 2;
1684 /* Leave room for the null. */
1685 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1688 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1689 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1691 while (class_end
< class_limit
1692 && *class_end
>= 'a' && *class_end
<= 'z')
1695 if (class_end
== class_beg
1696 || *class_end
!= ':' || class_end
[1] != ']')
1697 goto not_a_class_name
;
1699 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1700 class_name
[class_end
- class_beg
] = 0;
1702 cc
= re_wctype (class_name
);
1704 error ("Invalid ISO C character class");
1706 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1708 i_byte
= class_end
+ 2 - str
;
1715 if (i_byte
== size_byte
)
1720 /* Treat `-' as range character only if another character
1722 if (i_byte
+ 1 < size_byte
1723 && str
[i_byte
] == '-')
1727 /* Skip over the dash. */
1730 /* Get the end of the range. */
1733 && i_byte
< size_byte
)
1741 if (! ASCII_CHAR_P (c2
))
1742 string_has_eight_bit
= 1;
1748 if (! ASCII_CHAR_P (c
))
1749 string_has_eight_bit
= 1;
1753 /* If the current range is multibyte and STRING contains
1754 eight-bit chars, arrange fastmap and setup char_ranges for
1755 the corresponding multibyte chars. */
1756 if (multibyte
&& string_has_eight_bit
)
1759 char himap
[0200 + 1];
1760 memcpy (himap
, fastmap
+ 0200, 0200);
1762 memset (fastmap
+ 0200, 0, 0200);
1763 SAFE_NALLOCA (char_ranges
, 2, 128);
1766 while ((p1
= memchr (himap
+ i
, 1, 0200 - i
)))
1768 /* Deduce the next range C..C2 from the next clump of 1s
1769 in HIMAP starting with &HIMAP[I]. HIMAP is the high
1770 order half of the old FASTMAP. */
1771 int c2
, leading_code
;
1773 c
= BYTE8_TO_CHAR (i
+ 0200);
1775 c2
= BYTE8_TO_CHAR (i
+ 0200 - 1);
1777 char_ranges
[n_char_ranges
++] = c
;
1778 char_ranges
[n_char_ranges
++] = c2
;
1779 leading_code
= CHAR_LEADING_CODE (c
);
1780 memset (fastmap
+ leading_code
, 1,
1781 CHAR_LEADING_CODE (c2
) - leading_code
+ 1);
1785 else /* STRING is multibyte */
1787 SAFE_NALLOCA (char_ranges
, 2, SCHARS (string
));
1789 while (i_byte
< size_byte
)
1791 int leading_code
= str
[i_byte
];
1792 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1795 if (handle_iso_classes
&& c
== '['
1796 && i_byte
< size_byte
1797 && STRING_CHAR (str
+ i_byte
) == ':')
1799 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1800 const unsigned char *class_end
= class_beg
;
1801 const unsigned char *class_limit
= str
+ size_byte
- 2;
1802 /* Leave room for the null. */
1803 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1806 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1807 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1809 while (class_end
< class_limit
1810 && *class_end
>= 'a' && *class_end
<= 'z')
1813 if (class_end
== class_beg
1814 || *class_end
!= ':' || class_end
[1] != ']')
1815 goto not_a_class_name_multibyte
;
1817 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1818 class_name
[class_end
- class_beg
] = 0;
1820 cc
= re_wctype (class_name
);
1822 error ("Invalid ISO C character class");
1824 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1826 i_byte
= class_end
+ 2 - str
;
1830 not_a_class_name_multibyte
:
1833 if (i_byte
== size_byte
)
1836 leading_code
= str
[i_byte
];
1837 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1840 /* Treat `-' as range character only if another character
1842 if (i_byte
+ 1 < size_byte
1843 && str
[i_byte
] == '-')
1845 int c2
, leading_code2
;
1847 /* Skip over the dash. */
1850 /* Get the end of the range. */
1851 leading_code2
= str
[i_byte
];
1852 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1856 && i_byte
< size_byte
)
1858 leading_code2
= str
[i_byte
];
1859 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1865 if (ASCII_CHAR_P (c
))
1867 while (c
<= c2
&& c
< 0x80)
1869 leading_code
= CHAR_LEADING_CODE (c
);
1871 if (! ASCII_CHAR_P (c
))
1873 int lim2
= leading_code2
+ 1;
1874 while (leading_code
< lim2
)
1875 fastmap
[leading_code
++] = 1;
1878 char_ranges
[n_char_ranges
++] = c
;
1879 char_ranges
[n_char_ranges
++] = c2
;
1885 if (ASCII_CHAR_P (c
))
1889 fastmap
[leading_code
] = 1;
1890 char_ranges
[n_char_ranges
++] = c
;
1891 char_ranges
[n_char_ranges
++] = c
;
1896 /* If the current range is unibyte and STRING contains non-ASCII
1897 chars, arrange fastmap for the corresponding unibyte
1900 if (! multibyte
&& n_char_ranges
> 0)
1902 memset (fastmap
+ 0200, 0, 0200);
1903 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1905 int c1
= char_ranges
[i
];
1906 int lim2
= char_ranges
[i
+ 1] + 1;
1908 for (; c1
< lim2
; c1
++)
1910 int b
= CHAR_TO_BYTE_SAFE (c1
);
1918 /* If ^ was the first character, complement the fastmap. */
1922 for (i
= 0; i
< sizeof fastmap
; i
++)
1926 for (i
= 0; i
< 0200; i
++)
1928 /* All non-ASCII chars possibly match. */
1929 for (; i
< sizeof fastmap
; i
++)
1935 ptrdiff_t start_point
= PT
;
1937 ptrdiff_t pos_byte
= PT_BYTE
;
1938 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1942 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1943 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1947 endp
= CHAR_POS_ADDR (XINT (lim
));
1948 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1952 /* This code may look up syntax tables using functions that rely on the
1953 gl_state object. To make sure this object is not out of date,
1954 let's initialize it manually.
1955 We ignore syntax-table text-properties for now, since that's
1956 what we've done in the past. */
1957 SETUP_BUFFER_SYNTAX_TABLE ();
1972 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
1973 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1983 if (! ASCII_CHAR_P (c
))
1985 /* As we are looking at a multibyte character, we
1986 must look up the character in the table
1987 CHAR_RANGES. If there's no data in the table,
1988 that character is not what we want to skip. */
1990 /* The following code do the right thing even if
1991 n_char_ranges is zero (i.e. no data in
1993 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1994 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1996 if (!(negate
^ (i
< n_char_ranges
)))
2000 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2013 if (!NILP (iso_classes
) && in_classes (*p
, iso_classes
))
2018 goto fwd_unibyte_ok
;
2024 p
++, pos
++, pos_byte
++;
2032 unsigned char *prev_p
;
2042 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2043 c
= STRING_CHAR (p
);
2045 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
2055 if (! ASCII_CHAR_P (c
))
2057 /* See the comment in the previous similar code. */
2058 for (i
= 0; i
< n_char_ranges
; i
+= 2)
2059 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
2061 if (!(negate
^ (i
< n_char_ranges
)))
2065 pos
--, pos_byte
-= prev_p
- p
;
2078 if (! NILP (iso_classes
) && in_classes (p
[-1], iso_classes
))
2083 goto back_unibyte_ok
;
2086 if (!fastmap
[p
[-1]])
2089 p
--, pos
--, pos_byte
--;
2093 SET_PT_BOTH (pos
, pos_byte
);
2097 return make_number (PT
- start_point
);
2103 skip_syntaxes (bool forwardp
, Lisp_Object string
, Lisp_Object lim
)
2106 unsigned char fastmap
[0400];
2108 ptrdiff_t i
, i_byte
;
2110 ptrdiff_t size_byte
;
2113 CHECK_STRING (string
);
2116 XSETINT (lim
, forwardp
? ZV
: BEGV
);
2118 CHECK_NUMBER_COERCE_MARKER (lim
);
2120 /* In any case, don't allow scan outside bounds of buffer. */
2121 if (XINT (lim
) > ZV
)
2122 XSETFASTINT (lim
, ZV
);
2123 if (XINT (lim
) < BEGV
)
2124 XSETFASTINT (lim
, BEGV
);
2126 if (forwardp
? (PT
>= XFASTINT (lim
)) : (PT
<= XFASTINT (lim
)))
2127 return make_number (0);
2129 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
2130 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
2132 memset (fastmap
, 0, sizeof fastmap
);
2134 if (SBYTES (string
) > SCHARS (string
))
2135 /* As this is very rare case (syntax spec is ASCII only), don't
2136 consider efficiency. */
2137 string
= string_make_unibyte (string
);
2139 str
= SDATA (string
);
2140 size_byte
= SBYTES (string
);
2143 if (i_byte
< size_byte
2144 && SREF (string
, 0) == '^')
2146 negate
= 1; i_byte
++;
2149 /* Find the syntaxes specified and set their elements of fastmap. */
2151 while (i_byte
< size_byte
)
2154 fastmap
[syntax_spec_code
[c
]] = 1;
2157 /* If ^ was the first character, complement the fastmap. */
2159 for (i
= 0; i
< sizeof fastmap
; i
++)
2163 ptrdiff_t start_point
= PT
;
2165 ptrdiff_t pos_byte
= PT_BYTE
;
2166 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
2170 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
2171 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
2175 endp
= CHAR_POS_ADDR (XINT (lim
));
2176 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
2180 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
2196 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
2197 if (! fastmap
[SYNTAX (c
)])
2199 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2200 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2214 if (! fastmap
[SYNTAX (*p
)])
2216 p
++, pos
++, pos_byte
++;
2217 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2227 unsigned char *prev_p
;
2236 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2238 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2239 c
= STRING_CHAR (p
);
2240 if (! fastmap
[SYNTAX (c
)])
2242 pos
--, pos_byte
-= prev_p
- p
;
2256 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2257 if (! fastmap
[SYNTAX (p
[-1])])
2259 p
--, pos
--, pos_byte
--;
2264 SET_PT_BOTH (pos
, pos_byte
);
2267 return make_number (PT
- start_point
);
2271 /* Return true if character C belongs to one of the ISO classes
2272 in the list ISO_CLASSES. Each class is represented by an
2273 integer which is its type according to re_wctype. */
2276 in_classes (int c
, Lisp_Object iso_classes
)
2278 bool fits_class
= 0;
2280 while (CONSP (iso_classes
))
2283 elt
= XCAR (iso_classes
);
2284 iso_classes
= XCDR (iso_classes
);
2286 if (re_iswctype (c
, XFASTINT (elt
)))
2293 /* Jump over a comment, assuming we are at the beginning of one.
2294 FROM is the current position.
2295 FROM_BYTE is the bytepos corresponding to FROM.
2296 Do not move past STOP (a charpos).
2297 The comment over which we have to jump is of style STYLE
2298 (either SYNTAX_FLAGS_COMMENT_STYLE (foo) or ST_COMMENT_STYLE).
2299 NESTING should be positive to indicate the nesting at the beginning
2300 for nested comments and should be zero or negative else.
2301 ST_COMMENT_STYLE cannot be nested.
2302 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2303 (or 0 If the search cannot start in the middle of a two-character).
2305 If successful, return true and store the charpos of the comment's end
2306 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2307 Else, return false and store the charpos STOP into *CHARPOS_PTR, the
2308 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2309 (as defined for state.incomment) in *INCOMMENT_PTR.
2311 The comment end is the last character of the comment rather than the
2312 character just after the comment.
2314 Global syntax data is assumed to initially be valid for FROM and
2315 remains valid for forward search starting at the returned position. */
2318 forw_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
,
2319 EMACS_INT nesting
, int style
, int prev_syntax
,
2320 ptrdiff_t *charpos_ptr
, ptrdiff_t *bytepos_ptr
,
2321 EMACS_INT
*incomment_ptr
)
2324 register enum syntaxcode code
;
2325 register int syntax
, other_syntax
;
2327 if (nesting
<= 0) nesting
= -1;
2329 /* Enter the loop in the middle so that we find
2330 a 2-char comment ender if we start in the middle of it. */
2331 syntax
= prev_syntax
;
2332 if (syntax
!= 0) goto forw_incomment
;
2338 *incomment_ptr
= nesting
;
2339 *charpos_ptr
= from
;
2340 *bytepos_ptr
= from_byte
;
2343 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2344 syntax
= SYNTAX_WITH_FLAGS (c
);
2345 code
= syntax
& 0xff;
2346 if (code
== Sendcomment
2347 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
2348 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
2349 (nesting
> 0 && --nesting
== 0) : nesting
< 0))
2350 /* We have encountered a comment end of the same style
2351 as the comment sequence which began this comment
2354 if (code
== Scomment_fence
2355 && style
== ST_COMMENT_STYLE
)
2356 /* We have encountered a comment end of the same style
2357 as the comment sequence which began this comment
2362 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
2363 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
)
2364 /* We have encountered a nested comment of the same style
2365 as the comment sequence which began this comment section. */
2367 INC_BOTH (from
, from_byte
);
2368 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2371 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2372 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2373 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2374 SYNTAX_FLAGS_COMEND_SECOND (other_syntax
))
2375 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, other_syntax
) == style
2376 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2377 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
))
2378 ? nesting
> 0 : nesting
< 0))
2381 /* We have encountered a comment end of the same style
2382 as the comment sequence which began this comment section. */
2386 INC_BOTH (from
, from_byte
);
2387 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2392 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
2393 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2394 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2395 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
) == style
2396 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2397 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2398 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
)))
2399 /* We have encountered a nested comment of the same style
2400 as the comment sequence which began this comment section. */
2402 INC_BOTH (from
, from_byte
);
2403 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2407 *charpos_ptr
= from
;
2408 *bytepos_ptr
= from_byte
;
2412 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
2414 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2415 Stop scanning if we find something other than a comment or whitespace.
2416 Set point to where scanning stops.
2417 If COUNT comments are found as expected, with nothing except whitespace
2418 between them, return t; otherwise return nil. */)
2421 ptrdiff_t from
, from_byte
, stop
;
2423 enum syntaxcode code
;
2424 int comstyle
= 0; /* style of comment encountered */
2425 bool comnested
= 0; /* whether the comment is nestable or not */
2428 ptrdiff_t out_charpos
, out_bytepos
;
2431 CHECK_NUMBER (count
);
2432 count1
= XINT (count
);
2433 stop
= count1
> 0 ? ZV
: BEGV
;
2439 from_byte
= PT_BYTE
;
2441 SETUP_SYNTAX_TABLE (from
, count1
);
2446 bool comstart_first
;
2447 int syntax
, other_syntax
;
2451 SET_PT_BOTH (from
, from_byte
);
2455 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2456 syntax
= SYNTAX_WITH_FLAGS (c
);
2458 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2459 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2460 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2461 INC_BOTH (from
, from_byte
);
2462 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2463 if (from
< stop
&& comstart_first
2464 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2465 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2466 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
)))
2468 /* We have encountered a comment start sequence and we
2469 are ignoring all text inside comments. We must record
2470 the comment style this sequence begins so that later,
2471 only a comment end of the same style actually ends
2472 the comment section. */
2474 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2475 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2476 INC_BOTH (from
, from_byte
);
2477 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2480 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
2482 if (code
== Scomment_fence
)
2483 comstyle
= ST_COMMENT_STYLE
;
2484 else if (code
!= Scomment
)
2487 DEC_BOTH (from
, from_byte
);
2488 SET_PT_BOTH (from
, from_byte
);
2491 /* We're at the start of a comment. */
2492 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
2493 &out_charpos
, &out_bytepos
, &dummy
);
2494 from
= out_charpos
; from_byte
= out_bytepos
;
2498 SET_PT_BOTH (from
, from_byte
);
2501 INC_BOTH (from
, from_byte
);
2502 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2503 /* We have skipped one comment. */
2516 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
2521 DEC_BOTH (from
, from_byte
);
2522 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2523 quoted
= char_quoted (from
, from_byte
);
2524 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2525 syntax
= SYNTAX_WITH_FLAGS (c
);
2528 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2529 if (code
== Sendcomment
)
2530 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2531 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2532 && prev_char_comend_first (from
, from_byte
)
2533 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
2536 /* We must record the comment style encountered so that
2537 later, we can match only the proper comment begin
2538 sequence of the same style. */
2539 DEC_BOTH (from
, from_byte
);
2541 /* Calling char_quoted, above, set up global syntax position
2542 at the new value of FROM. */
2543 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2544 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2545 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2546 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2549 if (code
== Scomment_fence
)
2551 /* Skip until first preceding unquoted comment_fence. */
2552 bool fence_found
= 0;
2553 ptrdiff_t ini
= from
, ini_byte
= from_byte
;
2557 DEC_BOTH (from
, from_byte
);
2558 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2559 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2560 if (SYNTAX (c
) == Scomment_fence
2561 && !char_quoted (from
, from_byte
))
2566 else if (from
== stop
)
2569 if (fence_found
== 0)
2571 from
= ini
; /* Set point to ini + 1. */
2572 from_byte
= ini_byte
;
2576 /* We have skipped one comment. */
2579 else if (code
== Sendcomment
)
2581 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2582 &out_charpos
, &out_bytepos
);
2586 /* This end-of-line is not an end-of-comment.
2587 Treat it like a whitespace.
2588 CC-mode (and maybe others) relies on this behavior. */
2592 /* Failure: we should go back to the end of this
2593 not-quite-endcomment. */
2594 if (SYNTAX (c
) != code
)
2595 /* It was a two-char Sendcomment. */
2596 INC_BOTH (from
, from_byte
);
2602 /* We have skipped one comment. */
2603 from
= out_charpos
, from_byte
= out_bytepos
;
2607 else if (code
!= Swhitespace
|| quoted
)
2611 INC_BOTH (from
, from_byte
);
2612 SET_PT_BOTH (from
, from_byte
);
2620 SET_PT_BOTH (from
, from_byte
);
2625 /* Return syntax code of character C if C is an ASCII character
2626 or if MULTIBYTE_SYMBOL_P is false. Otherwise, return Ssymbol. */
2628 static enum syntaxcode
2629 syntax_multibyte (int c
, bool multibyte_symbol_p
)
2631 return ASCII_CHAR_P (c
) || !multibyte_symbol_p
? SYNTAX (c
) : Ssymbol
;
2635 scan_lists (EMACS_INT from
, EMACS_INT count
, EMACS_INT depth
, bool sexpflag
)
2638 ptrdiff_t stop
= count
> 0 ? ZV
: BEGV
;
2643 enum syntaxcode code
;
2644 EMACS_INT min_depth
= depth
; /* Err out if depth gets less than this. */
2645 int comstyle
= 0; /* Style of comment encountered. */
2646 bool comnested
= 0; /* Whether the comment is nestable or not. */
2648 EMACS_INT last_good
= from
;
2650 ptrdiff_t from_byte
;
2651 ptrdiff_t out_bytepos
, out_charpos
;
2653 bool multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2655 if (depth
> 0) min_depth
= 0;
2657 if (from
> ZV
) from
= ZV
;
2658 if (from
< BEGV
) from
= BEGV
;
2660 from_byte
= CHAR_TO_BYTE (from
);
2665 SETUP_SYNTAX_TABLE (from
, count
);
2670 bool comstart_first
, prefix
;
2671 int syntax
, other_syntax
;
2672 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2673 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2674 syntax
= SYNTAX_WITH_FLAGS (c
);
2675 code
= syntax_multibyte (c
, multibyte_symbol_p
);
2676 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2677 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2678 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2679 prefix
= SYNTAX_FLAGS_PREFIX (syntax
);
2680 if (depth
== min_depth
)
2682 INC_BOTH (from
, from_byte
);
2683 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2684 if (from
< stop
&& comstart_first
2685 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2686 other_syntax
= SYNTAX_WITH_FLAGS (c
),
2687 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2688 && parse_sexp_ignore_comments
)
2690 /* We have encountered a comment start sequence and we
2691 are ignoring all text inside comments. We must record
2692 the comment style this sequence begins so that later,
2693 only a comment end of the same style actually ends
2694 the comment section. */
2696 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2697 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2698 INC_BOTH (from
, from_byte
);
2699 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2711 INC_BOTH (from
, from_byte
);
2712 /* Treat following character as a word constituent. */
2715 if (depth
|| !sexpflag
) break;
2716 /* This word counts as a sexp; return at end of it. */
2719 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2721 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2722 switch (syntax_multibyte (c
, multibyte_symbol_p
))
2726 INC_BOTH (from
, from_byte
);
2737 INC_BOTH (from
, from_byte
);
2741 case Scomment_fence
:
2742 comstyle
= ST_COMMENT_STYLE
;
2745 if (!parse_sexp_ignore_comments
) break;
2746 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2747 found
= forw_comment (from
, from_byte
, stop
,
2748 comnested
, comstyle
, 0,
2749 &out_charpos
, &out_bytepos
, &dummy
);
2750 from
= out_charpos
, from_byte
= out_bytepos
;
2757 INC_BOTH (from
, from_byte
);
2758 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2764 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (from_byte
))
2766 INC_BOTH (from
, from_byte
);
2776 if (!++depth
) goto done
;
2781 if (!--depth
) goto done
;
2782 if (depth
< min_depth
)
2783 xsignal3 (Qscan_error
,
2784 build_string ("Containing expression ends prematurely"),
2785 make_number (last_good
), make_number (from
));
2790 temp_pos
= dec_bytepos (from_byte
);
2791 stringterm
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2794 enum syntaxcode c_code
;
2797 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2798 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2799 c_code
= syntax_multibyte (c
, multibyte_symbol_p
);
2801 ? c
== stringterm
&& c_code
== Sstring
2802 : c_code
== Sstring_fence
)
2805 if (c_code
== Scharquote
|| c_code
== Sescape
)
2806 INC_BOTH (from
, from_byte
);
2807 INC_BOTH (from
, from_byte
);
2809 INC_BOTH (from
, from_byte
);
2810 if (!depth
&& sexpflag
) goto done
;
2813 /* Ignore whitespace, punctuation, quote, endcomment. */
2818 /* Reached end of buffer. Error if within object, return nil if between */
2825 /* End of object reached */
2835 DEC_BOTH (from
, from_byte
);
2836 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2837 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2838 syntax
= SYNTAX_WITH_FLAGS (c
);
2839 code
= syntax_multibyte (c
, multibyte_symbol_p
);
2840 if (depth
== min_depth
)
2843 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2844 if (code
== Sendcomment
)
2845 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2846 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2847 && prev_char_comend_first (from
, from_byte
)
2848 && parse_sexp_ignore_comments
)
2850 /* We must record the comment style encountered so that
2851 later, we can match only the proper comment begin
2852 sequence of the same style. */
2853 int c2
, other_syntax
;
2854 DEC_BOTH (from
, from_byte
);
2855 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2857 c2
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2858 other_syntax
= SYNTAX_WITH_FLAGS (c2
);
2859 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2860 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2863 /* Quoting turns anything except a comment-ender
2864 into a word character. Note that this cannot be true
2865 if we decremented FROM in the if-statement above. */
2866 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2868 DEC_BOTH (from
, from_byte
);
2871 else if (SYNTAX_FLAGS_PREFIX (syntax
))
2880 if (depth
|| !sexpflag
) break;
2881 /* This word counts as a sexp; count object finished
2882 after passing it. */
2885 temp_pos
= from_byte
;
2886 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
2890 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2891 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2892 /* Don't allow comment-end to be quoted. */
2893 if (syntax_multibyte (c1
, multibyte_symbol_p
) == Sendcomment
)
2895 quoted
= char_quoted (from
- 1, temp_pos
);
2898 DEC_BOTH (from
, from_byte
);
2899 temp_pos
= dec_bytepos (temp_pos
);
2900 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2902 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2904 switch (syntax_multibyte (c1
, multibyte_symbol_p
))
2906 case Sword
: case Ssymbol
: case Squote
: break;
2907 default: goto done2
;
2909 DEC_BOTH (from
, from_byte
);
2918 temp_pos
= dec_bytepos (from_byte
);
2919 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2920 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (temp_pos
))
2921 DEC_BOTH (from
, from_byte
);
2931 if (!++depth
) goto done2
;
2936 if (!--depth
) goto done2
;
2937 if (depth
< min_depth
)
2938 xsignal3 (Qscan_error
,
2939 build_string ("Containing expression ends prematurely"),
2940 make_number (last_good
), make_number (from
));
2944 if (!parse_sexp_ignore_comments
)
2946 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2947 &out_charpos
, &out_bytepos
);
2948 /* FIXME: if !found, it really wasn't a comment-end.
2949 For single-char Sendcomment, we can't do much about it apart
2950 from skipping the char.
2951 For 2-char endcomments, we could try again, taking both
2952 chars as separate entities, but it's a lot of trouble
2953 for very little gain, so we don't bother either. -sm */
2955 from
= out_charpos
, from_byte
= out_bytepos
;
2958 case Scomment_fence
:
2964 DEC_BOTH (from
, from_byte
);
2965 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2966 if (!char_quoted (from
, from_byte
))
2968 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2969 if (syntax_multibyte (c
, multibyte_symbol_p
) == code
)
2973 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2977 stringterm
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2982 DEC_BOTH (from
, from_byte
);
2983 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2984 if (!char_quoted (from
, from_byte
))
2986 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2988 && (syntax_multibyte (c
, multibyte_symbol_p
)
2993 if (!depth
&& sexpflag
) goto done2
;
2996 /* Ignore whitespace, punctuation, quote, endcomment. */
3001 /* Reached start of buffer. Error if within object, return nil if between */
3014 XSETFASTINT (val
, from
);
3018 xsignal3 (Qscan_error
,
3019 build_string ("Unbalanced parentheses"),
3020 make_number (last_good
), make_number (from
));
3023 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
3024 doc
: /* Scan from character number FROM by COUNT lists.
3025 Scan forward if COUNT is positive, backward if COUNT is negative.
3026 Return the character number of the position thus found.
3028 A \"list", in this context, refers to a balanced parenthetical
3029 grouping, as determined by the syntax table.
3031 If DEPTH is nonzero, treat that as the nesting depth of the starting
3032 point (i.e. the starting point is DEPTH parentheses deep). This
3033 function scans over parentheses until the depth goes to zero COUNT
3034 times. Hence, positive DEPTH moves out that number of levels of
3035 parentheses, while negative DEPTH moves to a deeper level.
3037 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3039 If we reach the beginning or end of the accessible part of the buffer
3040 before we have scanned over COUNT lists, return nil if the depth at
3041 that point is zero, and signal a error if the depth is nonzero. */)
3042 (Lisp_Object from
, Lisp_Object count
, Lisp_Object depth
)
3044 CHECK_NUMBER (from
);
3045 CHECK_NUMBER (count
);
3046 CHECK_NUMBER (depth
);
3048 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
3051 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
3052 doc
: /* Scan from character number FROM by COUNT balanced expressions.
3053 If COUNT is negative, scan backwards.
3054 Returns the character number of the position thus found.
3056 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3058 If the beginning or end of (the accessible part of) the buffer is reached
3059 in the middle of a parenthetical grouping, an error is signaled.
3060 If the beginning or end is reached between groupings
3061 but before count is used up, nil is returned. */)
3062 (Lisp_Object from
, Lisp_Object count
)
3064 CHECK_NUMBER (from
);
3065 CHECK_NUMBER (count
);
3067 return scan_lists (XINT (from
), XINT (count
), 0, 1);
3070 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
3072 doc
: /* Move point backward over any number of chars with prefix syntax.
3073 This includes chars with expression prefix syntax class (') and those with
3074 the prefix syntax flag (p). */)
3077 ptrdiff_t beg
= BEGV
;
3078 ptrdiff_t opoint
= PT
;
3079 ptrdiff_t opoint_byte
= PT_BYTE
;
3081 ptrdiff_t pos_byte
= PT_BYTE
;
3086 SET_PT_BOTH (opoint
, opoint_byte
);
3091 SETUP_SYNTAX_TABLE (pos
, -1);
3093 DEC_BOTH (pos
, pos_byte
);
3095 while (!char_quoted (pos
, pos_byte
)
3096 /* Previous statement updates syntax table. */
3097 && ((c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
), SYNTAX (c
) == Squote
)
3098 || syntax_prefix_flag_p (c
)))
3101 opoint_byte
= pos_byte
;
3104 DEC_BOTH (pos
, pos_byte
);
3107 SET_PT_BOTH (opoint
, opoint_byte
);
3112 /* Parse forward from FROM / FROM_BYTE to END,
3113 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
3114 and return a description of the state of the parse at END.
3115 If STOPBEFORE, stop at the start of an atom.
3116 If COMMENTSTOP is 1, stop at the start of a comment.
3117 If COMMENTSTOP is -1, stop at the start or end of a comment,
3118 after the beginning of a string, or after the end of a string. */
3121 scan_sexps_forward (struct lisp_parse_state
*stateptr
,
3122 ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t end
,
3123 EMACS_INT targetdepth
, bool stopbefore
,
3124 Lisp_Object oldstate
, int commentstop
)
3126 struct lisp_parse_state state
;
3127 enum syntaxcode code
;
3130 struct level
{ ptrdiff_t last
, prev
; };
3131 struct level levelstart
[100];
3132 struct level
*curlevel
= levelstart
;
3133 struct level
*endlevel
= levelstart
+ 100;
3134 EMACS_INT depth
; /* Paren depth of current scanning location.
3135 level - levelstart equals this except
3136 when the depth becomes negative. */
3137 EMACS_INT mindepth
; /* Lowest DEPTH value seen. */
3138 bool start_quoted
= 0; /* True means starting after a char quote. */
3140 ptrdiff_t prev_from
; /* Keep one character before FROM. */
3141 ptrdiff_t prev_from_byte
;
3142 int prev_from_syntax
;
3143 bool boundary_stop
= commentstop
== -1;
3146 ptrdiff_t out_bytepos
, out_charpos
;
3150 prev_from_byte
= from_byte
;
3152 DEC_BOTH (prev_from
, prev_from_byte
);
3154 /* Use this macro instead of `from++'. */
3156 do { prev_from = from; \
3157 prev_from_byte = from_byte; \
3158 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
3159 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
3160 INC_BOTH (from, from_byte); \
3162 UPDATE_SYNTAX_TABLE_FORWARD (from); \
3168 if (NILP (oldstate
))
3171 state
.instring
= -1;
3172 state
.incomment
= 0;
3173 state
.comstyle
= 0; /* comment style a by default. */
3174 state
.comstr_start
= -1; /* no comment/string seen. */
3178 tem
= Fcar (oldstate
);
3184 oldstate
= Fcdr (oldstate
);
3185 oldstate
= Fcdr (oldstate
);
3186 oldstate
= Fcdr (oldstate
);
3187 tem
= Fcar (oldstate
);
3188 /* Check whether we are inside string_fence-style string: */
3189 state
.instring
= (!NILP (tem
)
3190 ? (CHARACTERP (tem
) ? XFASTINT (tem
) : ST_STRING_STYLE
)
3193 oldstate
= Fcdr (oldstate
);
3194 tem
= Fcar (oldstate
);
3195 state
.incomment
= (!NILP (tem
)
3196 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
3199 oldstate
= Fcdr (oldstate
);
3200 tem
= Fcar (oldstate
);
3201 start_quoted
= !NILP (tem
);
3203 /* if the eighth element of the list is nil, we are in comment
3204 style a. If it is non-nil, we are in comment style b */
3205 oldstate
= Fcdr (oldstate
);
3206 oldstate
= Fcdr (oldstate
);
3207 tem
= Fcar (oldstate
);
3208 state
.comstyle
= (NILP (tem
)
3210 : (RANGED_INTEGERP (0, tem
, ST_COMMENT_STYLE
)
3212 : ST_COMMENT_STYLE
));
3214 oldstate
= Fcdr (oldstate
);
3215 tem
= Fcar (oldstate
);
3216 state
.comstr_start
=
3217 RANGED_INTEGERP (PTRDIFF_MIN
, tem
, PTRDIFF_MAX
) ? XINT (tem
) : -1;
3218 oldstate
= Fcdr (oldstate
);
3219 tem
= Fcar (oldstate
);
3220 while (!NILP (tem
)) /* >= second enclosing sexps. */
3222 Lisp_Object temhd
= Fcar (tem
);
3223 if (RANGED_INTEGERP (PTRDIFF_MIN
, temhd
, PTRDIFF_MAX
))
3224 curlevel
->last
= XINT (temhd
);
3225 if (++curlevel
== endlevel
)
3226 curlevel
--; /* error ("Nesting too deep for parser"); */
3227 curlevel
->prev
= -1;
3228 curlevel
->last
= -1;
3235 curlevel
->prev
= -1;
3236 curlevel
->last
= -1;
3238 SETUP_SYNTAX_TABLE (prev_from
, 1);
3239 temp
= FETCH_CHAR (prev_from_byte
);
3240 prev_from_syntax
= SYNTAX_WITH_FLAGS (temp
);
3241 UPDATE_SYNTAX_TABLE_FORWARD (from
);
3243 /* Enter the loop at a place appropriate for initial state. */
3245 if (state
.incomment
)
3246 goto startincomment
;
3247 if (state
.instring
>= 0)
3249 nofence
= state
.instring
!= ST_STRING_STYLE
;
3251 goto startquotedinstring
;
3254 else if (start_quoted
)
3261 code
= prev_from_syntax
& 0xff;
3264 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
)
3265 && (c1
= FETCH_CHAR (from_byte
),
3266 syntax
= SYNTAX_WITH_FLAGS (c1
),
3267 SYNTAX_FLAGS_COMSTART_SECOND (syntax
)))
3268 /* Duplicate code to avoid a complex if-expression
3269 which causes trouble for the SGI compiler. */
3271 /* Record the comment style we have entered so that only
3272 the comment-end sequence of the same style actually
3273 terminates the comment section. */
3275 = SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_from_syntax
);
3276 comnested
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
)
3277 | SYNTAX_FLAGS_COMMENT_NESTED (syntax
));
3278 state
.incomment
= comnested
? 1 : -1;
3279 state
.comstr_start
= prev_from
;
3283 else if (code
== Scomment_fence
)
3285 /* Record the comment style we have entered so that only
3286 the comment-end sequence of the same style actually
3287 terminates the comment section. */
3288 state
.comstyle
= ST_COMMENT_STYLE
;
3289 state
.incomment
= -1;
3290 state
.comstr_start
= prev_from
;
3293 else if (code
== Scomment
)
3295 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
, 0);
3296 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
3298 state
.comstr_start
= prev_from
;
3301 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
3307 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3308 curlevel
->last
= prev_from
;
3310 if (from
== end
) goto endquoted
;
3313 /* treat following character as a word constituent */
3316 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3317 curlevel
->last
= prev_from
;
3321 int symchar
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3322 switch (SYNTAX (symchar
))
3327 if (from
== end
) goto endquoted
;
3339 curlevel
->prev
= curlevel
->last
;
3342 case Scomment_fence
: /* Can't happen because it's handled above. */
3344 if (commentstop
|| boundary_stop
) goto done
;
3346 /* The (from == BEGV) test was to enter the loop in the middle so
3347 that we find a 2-char comment ender even if we start in the
3348 middle of it. We don't want to do that if we're just at the
3349 beginning of the comment (think of (*) ... (*)). */
3350 found
= forw_comment (from
, from_byte
, end
,
3351 state
.incomment
, state
.comstyle
,
3352 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
3353 ? 0 : prev_from_syntax
,
3354 &out_charpos
, &out_bytepos
, &state
.incomment
);
3355 from
= out_charpos
; from_byte
= out_bytepos
;
3356 /* Beware! prev_from and friends are invalid now.
3357 Luckily, the `done' doesn't use them and the INC_FROM
3358 sets them to a sane value without looking at them. */
3359 if (!found
) goto done
;
3361 state
.incomment
= 0;
3362 state
.comstyle
= 0; /* reset the comment style */
3363 if (boundary_stop
) goto done
;
3367 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3369 /* curlevel++->last ran into compiler bug on Apollo */
3370 curlevel
->last
= prev_from
;
3371 if (++curlevel
== endlevel
)
3372 curlevel
--; /* error ("Nesting too deep for parser"); */
3373 curlevel
->prev
= -1;
3374 curlevel
->last
= -1;
3375 if (targetdepth
== depth
) goto done
;
3380 if (depth
< mindepth
)
3382 if (curlevel
!= levelstart
)
3384 curlevel
->prev
= curlevel
->last
;
3385 if (targetdepth
== depth
) goto done
;
3390 state
.comstr_start
= from
- 1;
3391 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3392 curlevel
->last
= prev_from
;
3393 state
.instring
= (code
== Sstring
3394 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte
))
3396 if (boundary_stop
) goto done
;
3399 nofence
= state
.instring
!= ST_STRING_STYLE
;
3404 enum syntaxcode c_code
;
3406 if (from
>= end
) goto done
;
3407 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3408 c_code
= SYNTAX (c
);
3410 /* Check C_CODE here so that if the char has
3411 a syntax-table property which says it is NOT
3412 a string character, it does not end the string. */
3413 if (nofence
&& c
== state
.instring
&& c_code
== Sstring
)
3419 if (!nofence
) goto string_end
;
3425 startquotedinstring
:
3426 if (from
>= end
) goto endquoted
;
3436 state
.instring
= -1;
3437 curlevel
->prev
= curlevel
->last
;
3439 if (boundary_stop
) goto done
;
3443 /* FIXME: We should do something with it. */
3446 /* Ignore whitespace, punctuation, quote, endcomment. */
3452 stop
: /* Here if stopping before start of sexp. */
3453 from
= prev_from
; /* We have just fetched the char that starts it; */
3454 from_byte
= prev_from_byte
;
3455 goto done
; /* but return the position before it. */
3460 state
.depth
= depth
;
3461 state
.mindepth
= mindepth
;
3462 state
.thislevelstart
= curlevel
->prev
;
3463 state
.prevlevelstart
3464 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
3465 state
.location
= from
;
3466 state
.location_byte
= from_byte
;
3467 state
.levelstarts
= Qnil
;
3468 while (curlevel
> levelstart
)
3469 state
.levelstarts
= Fcons (make_number ((--curlevel
)->last
),
3476 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
3477 doc
: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3478 Parsing stops at TO or when certain criteria are met;
3479 point is set to where parsing stops.
3480 If fifth arg OLDSTATE is omitted or nil,
3481 parsing assumes that FROM is the beginning of a function.
3482 Value is a list of elements describing final state of parsing:
3484 1. character address of start of innermost containing list; nil if none.
3485 2. character address of start of last complete sexp terminated.
3486 3. non-nil if inside a string.
3487 (it is the character that will terminate the string,
3488 or t if the string should be terminated by a generic string delimiter.)
3489 4. nil if outside a comment, t if inside a non-nestable comment,
3490 else an integer (the current comment nesting).
3491 5. t if following a quote character.
3492 6. the minimum paren-depth encountered during this scan.
3493 7. style of comment, if any.
3494 8. character address of start of comment or string; nil if not in one.
3495 9. Intermediate data for continuation of parsing (subject to change).
3496 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3497 in parentheses becomes equal to TARGETDEPTH.
3498 Fourth arg STOPBEFORE non-nil means stop when come to
3499 any character that starts a sexp.
3500 Fifth arg OLDSTATE is a list like what this function returns.
3501 It is used to initialize the state of the parse. Elements number 1, 2, 6
3503 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3504 If it is symbol `syntax-table', stop after the start of a comment or a
3505 string, or after end of a comment or a string. */)
3506 (Lisp_Object from
, Lisp_Object to
, Lisp_Object targetdepth
,
3507 Lisp_Object stopbefore
, Lisp_Object oldstate
, Lisp_Object commentstop
)
3509 struct lisp_parse_state state
;
3512 if (!NILP (targetdepth
))
3514 CHECK_NUMBER (targetdepth
);
3515 target
= XINT (targetdepth
);
3518 target
= TYPE_MINIMUM (EMACS_INT
); /* We won't reach this depth. */
3520 validate_region (&from
, &to
);
3521 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
3523 target
, !NILP (stopbefore
), oldstate
,
3525 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
3527 SET_PT_BOTH (state
.location
, state
.location_byte
);
3529 return Fcons (make_number (state
.depth
),
3530 Fcons (state
.prevlevelstart
< 0
3531 ? Qnil
: make_number (state
.prevlevelstart
),
3532 Fcons (state
.thislevelstart
< 0
3533 ? Qnil
: make_number (state
.thislevelstart
),
3534 Fcons (state
.instring
>= 0
3535 ? (state
.instring
== ST_STRING_STYLE
3536 ? Qt
: make_number (state
.instring
)) : Qnil
,
3537 Fcons (state
.incomment
< 0 ? Qt
:
3538 (state
.incomment
== 0 ? Qnil
:
3539 make_number (state
.incomment
)),
3540 Fcons (state
.quoted
? Qt
: Qnil
,
3541 Fcons (make_number (state
.mindepth
),
3542 Fcons ((state
.comstyle
3543 ? (state
.comstyle
== ST_COMMENT_STYLE
3545 : make_number (state
.comstyle
))
3547 Fcons (((state
.incomment
3548 || (state
.instring
>= 0))
3549 ? make_number (state
.comstr_start
)
3551 Fcons (state
.levelstarts
, Qnil
))))))))));
3555 init_syntax_once (void)
3560 /* This has to be done here, before we call Fmake_char_table. */
3561 DEFSYM (Qsyntax_table
, "syntax-table");
3563 /* Create objects which can be shared among syntax tables. */
3564 Vsyntax_code_object
= make_uninit_vector (Smax
);
3565 for (i
= 0; i
< Smax
; i
++)
3566 ASET (Vsyntax_code_object
, i
, Fcons (make_number (i
), Qnil
));
3568 /* Now we are ready to set up this property, so we can
3569 create syntax tables. */
3570 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
3572 temp
= AREF (Vsyntax_code_object
, Swhitespace
);
3574 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
3576 /* Control characters should not be whitespace. */
3577 temp
= AREF (Vsyntax_code_object
, Spunct
);
3578 for (i
= 0; i
<= ' ' - 1; i
++)
3579 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3580 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 0177, temp
);
3582 /* Except that a few really are whitespace. */
3583 temp
= AREF (Vsyntax_code_object
, Swhitespace
);
3584 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ' ', temp
);
3585 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\t', temp
);
3586 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\n', temp
);
3587 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 015, temp
);
3588 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 014, temp
);
3590 temp
= AREF (Vsyntax_code_object
, Sword
);
3591 for (i
= 'a'; i
<= 'z'; i
++)
3592 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3593 for (i
= 'A'; i
<= 'Z'; i
++)
3594 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3595 for (i
= '0'; i
<= '9'; i
++)
3596 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3598 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
3599 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
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 (Sopen
), make_number ('}')));
3611 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
3612 Fcons (make_number (Sclose
), make_number ('{')));
3613 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
3614 Fcons (make_number (Sstring
), Qnil
));
3615 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
3616 Fcons (make_number (Sescape
), Qnil
));
3618 temp
= AREF (Vsyntax_code_object
, Ssymbol
);
3619 for (i
= 0; i
< 10; i
++)
3621 c
= "_-+*/&|<>="[i
];
3622 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3625 temp
= AREF (Vsyntax_code_object
, Spunct
);
3626 for (i
= 0; i
< 12; i
++)
3628 c
= ".,;:?!#@~^'`"[i
];
3629 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3632 /* All multibyte characters have syntax `word' by default. */
3633 temp
= AREF (Vsyntax_code_object
, Sword
);
3634 char_table_set_range (Vstandard_syntax_table
, 0x80, MAX_CHAR
, temp
);
3638 syms_of_syntax (void)
3640 DEFSYM (Qsyntax_table_p
, "syntax-table-p");
3642 staticpro (&Vsyntax_code_object
);
3644 staticpro (&gl_state
.object
);
3645 staticpro (&gl_state
.global_code
);
3646 staticpro (&gl_state
.current_syntax_table
);
3647 staticpro (&gl_state
.old_prop
);
3649 /* Defined in regex.c. */
3650 staticpro (&re_match_object
);
3652 DEFSYM (Qscan_error
, "scan-error");
3653 Fput (Qscan_error
, Qerror_conditions
,
3654 listn (CONSTYPE_PURE
, 2, Qscan_error
, Qerror
));
3655 Fput (Qscan_error
, Qerror_message
,
3656 build_pure_c_string ("Scan error"));
3658 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments
,
3659 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3661 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties
,
3662 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3663 Otherwise, that text property is simply ignored.
3664 See the info node `(elisp)Syntax Properties' for a description of the
3665 `syntax-table' property. */);
3667 DEFVAR_INT ("syntax-propertize--done", syntax_propertize__done
,
3668 doc
: /* Position up to which syntax-table properties have been set. */);
3669 syntax_propertize__done
= -1;
3670 DEFSYM (Qinternal__syntax_propertize
, "internal--syntax-propertize");
3671 Fmake_variable_buffer_local (intern ("syntax-propertize--done"));
3673 words_include_escapes
= 0;
3674 DEFVAR_BOOL ("words-include-escapes", words_include_escapes
,
3675 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3677 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol
,
3678 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3679 multibyte_syntax_as_symbol
= 0;
3681 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3682 open_paren_in_column_0_is_defun_start
,
3683 doc
: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3684 open_paren_in_column_0_is_defun_start
= 1;
3687 DEFVAR_LISP ("find-word-boundary-function-table",
3688 Vfind_word_boundary_function_table
,
3690 Char table of functions to search for the word boundary.
3691 Each function is called with two arguments; POS and LIMIT.
3692 POS and LIMIT are character positions in the current buffer.
3694 If POS is less than LIMIT, POS is at the first character of a word,
3695 and the return value of a function is a position after the last
3696 character of that word.
3698 If POS is not less than LIMIT, POS is at the last character of a word,
3699 and the return value of a function is a position at the first
3700 character of that word.
3702 In both cases, LIMIT bounds the search. */);
3703 Vfind_word_boundary_function_table
= Fmake_char_table (Qnil
, Qnil
);
3705 defsubr (&Ssyntax_table_p
);
3706 defsubr (&Ssyntax_table
);
3707 defsubr (&Sstandard_syntax_table
);
3708 defsubr (&Scopy_syntax_table
);
3709 defsubr (&Sset_syntax_table
);
3710 defsubr (&Schar_syntax
);
3711 defsubr (&Smatching_paren
);
3712 defsubr (&Sstring_to_syntax
);
3713 defsubr (&Smodify_syntax_entry
);
3714 defsubr (&Sinternal_describe_syntax_value
);
3716 defsubr (&Sforward_word
);
3718 defsubr (&Sskip_chars_forward
);
3719 defsubr (&Sskip_chars_backward
);
3720 defsubr (&Sskip_syntax_forward
);
3721 defsubr (&Sskip_syntax_backward
);
3723 defsubr (&Sforward_comment
);
3724 defsubr (&Sscan_lists
);
3725 defsubr (&Sscan_sexps
);
3726 defsubr (&Sbackward_prefix_chars
);
3727 defsubr (&Sparse_partial_sexp
);