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 which cannot
795 if ((Vcomment_end_can_be_escaped
|| code
!= Sendcomment
)
796 && char_quoted (from
, from_byte
))
803 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
805 /* Track parity of quotes. */
806 if (string_style
== -1)
807 /* Entering a string. */
809 else if (string_style
== c
)
810 /* Leaving the string. */
813 /* If we have two kinds of string delimiters.
814 There's no way to grok this scanning backwards. */
819 /* We've already checked that it is the relevant comstyle. */
820 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
821 /* There are odd string quotes involved, so let's be careful.
822 Test case in Pascal: " { " a { " } */
827 /* Record best comment-starter so far. */
829 comstart_byte
= from_byte
;
831 else if (--nesting
<= 0)
832 /* nested comments have to be balanced, so we don't need to
833 keep looking for earlier ones. We use here the same (slightly
834 incorrect) reasoning as below: since it is followed by uniform
835 paired string quotes, this comment-start has to be outside of
836 strings, else the comment-end itself would be inside a string. */
841 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == comstyle
842 && ((com2end
&& SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
))
843 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
844 /* This is the same style of comment ender as ours. */
849 /* Anything before that can't count because it would match
850 this comment-ender rather than ours. */
851 from
= stop
; /* Break out of the loop. */
853 else if (comstart_pos
!= 0 || c
!= '\n')
854 /* We're mixing comment styles here, so we'd better be careful.
855 The (comstart_pos != 0 || c != '\n') check is not quite correct
856 (we should just always set comment_lossage), but removing it
857 would imply that any multiline comment in C would go through
858 lossage, which seems overkill.
859 The failure should only happen in the rare cases such as
865 /* Assume a defun-start point is outside of strings. */
866 if (open_paren_in_column_0_is_defun_start
868 || (temp_byte
= dec_bytepos (from_byte
),
869 FETCH_CHAR (temp_byte
) == '\n')))
872 defun_start_byte
= from_byte
;
873 from
= stop
; /* Break out of the loop. */
882 if (comstart_pos
== 0)
885 from_byte
= comment_end_byte
;
886 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
);
888 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
889 or `done'), then we've found the beginning of the non-nested comment. */
890 else if (1) /* !comnested */
893 from_byte
= comstart_byte
;
894 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
898 struct lisp_parse_state state
;
899 bool adjusted
= true;
900 /* We had two kinds of string delimiters mixed up
901 together. Decode this going forwards.
902 Scan fwd from a known safe place (beginning-of-defun)
903 to the one in question; this records where we
904 last passed a comment starter. */
905 /* If we did not already find the defun start, find it now. */
906 if (defun_start
== 0)
908 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
909 defun_start_byte
= find_start_value_byte
;
910 adjusted
= (defun_start
> BEGV
);
914 scan_sexps_forward (&state
,
915 defun_start
, defun_start_byte
,
916 comment_end
, TYPE_MINIMUM (EMACS_INT
),
918 defun_start
= comment_end
;
923 = CONSP (state
.levelstarts
) ? XINT (XCAR (state
.levelstarts
))
924 : state
.thislevelstart
>= 0 ? state
.thislevelstart
926 find_start_value_byte
= CHAR_TO_BYTE (find_start_value
);
929 if (state
.incomment
== (comnested
? 1 : -1)
930 && state
.comstyle
== comstyle
)
931 from
= state
.comstr_start
;
936 /* If comment_end is inside some other comment, maybe ours
937 is nested, so we need to try again from within the
938 surrounding comment. Example: { a (* " *) */
940 /* FIXME: We should advance by one or two chars. */
941 defun_start
= state
.comstr_start
+ 2;
942 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
945 } while (defun_start
< comment_end
);
947 from_byte
= CHAR_TO_BYTE (from
);
948 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
953 *bytepos_ptr
= from_byte
;
955 return from
!= comment_end
;
958 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
959 doc
: /* Return t if OBJECT is a syntax table.
960 Currently, any char-table counts as a syntax table. */)
963 if (CHAR_TABLE_P (object
)
964 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
970 check_syntax_table (Lisp_Object obj
)
972 CHECK_TYPE (CHAR_TABLE_P (obj
) && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
),
973 Qsyntax_table_p
, obj
);
976 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
977 doc
: /* Return the current syntax table.
978 This is the one specified by the current buffer. */)
981 return BVAR (current_buffer
, syntax_table
);
984 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
985 Sstandard_syntax_table
, 0, 0, 0,
986 doc
: /* Return the standard syntax table.
987 This is the one used for new buffers. */)
990 return Vstandard_syntax_table
;
993 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
994 doc
: /* Construct a new syntax table and return it.
995 It is a copy of the TABLE, which defaults to the standard syntax table. */)
1001 check_syntax_table (table
);
1003 table
= Vstandard_syntax_table
;
1005 copy
= Fcopy_sequence (table
);
1007 /* Only the standard syntax table should have a default element.
1008 Other syntax tables should inherit from parents instead. */
1009 set_char_table_defalt (copy
, Qnil
);
1011 /* Copied syntax tables should all have parents.
1012 If we copied one with no parent, such as the standard syntax table,
1013 use the standard syntax table as the copy's parent. */
1014 if (NILP (XCHAR_TABLE (copy
)->parent
))
1015 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
1019 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
1020 doc
: /* Select a new syntax table for the current buffer.
1021 One argument, a syntax table. */)
1025 check_syntax_table (table
);
1026 bset_syntax_table (current_buffer
, table
);
1027 /* Indicate that this buffer now has a specified syntax table. */
1028 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
1029 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
1033 /* Convert a letter which signifies a syntax code
1034 into the code it signifies.
1035 This is used by modify-syntax-entry, and other things. */
1037 unsigned char const syntax_spec_code
[0400] =
1038 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1039 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1040 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1041 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1042 Swhitespace
, Scomment_fence
, Sstring
, 0377, Smath
, 0377, 0377, Squote
,
1043 Sopen
, Sclose
, 0377, 0377, 0377, Swhitespace
, Spunct
, Scharquote
,
1044 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1045 0377, 0377, 0377, 0377, Scomment
, 0377, Sendcomment
, 0377,
1046 Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
1047 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1048 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword
,
1049 0377, 0377, 0377, 0377, Sescape
, 0377, 0377, Ssymbol
,
1050 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
1051 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1052 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword
,
1053 0377, 0377, 0377, 0377, Sstring_fence
, 0377, 0377, 0377
1056 /* Indexed by syntax code, give the letter that describes it. */
1058 char const syntax_code_spec
[16] =
1060 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
1064 /* Indexed by syntax code, give the object (cons of syntax code and
1065 nil) to be stored in syntax table. Since these objects can be
1066 shared among syntax tables, we generate them in advance. By
1067 sharing objects, the function `describe-syntax' can give a more
1069 static Lisp_Object Vsyntax_code_object
;
1072 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
1073 doc
: /* Return the syntax code of CHARACTER, described by a character.
1074 For example, if CHARACTER is a word constituent, the
1075 character `w' (119) is returned.
1076 The characters that correspond to various syntax codes
1077 are listed in the documentation of `modify-syntax-entry'. */)
1078 (Lisp_Object character
)
1081 CHECK_CHARACTER (character
);
1082 char_int
= XINT (character
);
1083 SETUP_BUFFER_SYNTAX_TABLE ();
1084 return make_number (syntax_code_spec
[SYNTAX (char_int
)]);
1087 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
1088 doc
: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
1089 (Lisp_Object character
)
1092 enum syntaxcode code
;
1093 CHECK_CHARACTER (character
);
1094 char_int
= XINT (character
);
1095 SETUP_BUFFER_SYNTAX_TABLE ();
1096 code
= SYNTAX (char_int
);
1097 if (code
== Sopen
|| code
== Sclose
)
1098 return SYNTAX_MATCH (char_int
);
1102 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
1103 doc
: /* Convert a syntax descriptor STRING into a raw syntax descriptor.
1104 STRING should be a string of the form allowed as argument of
1105 `modify-syntax-entry'. The return value is a raw syntax descriptor: a
1106 cons cell (CODE . MATCHING-CHAR) which can be used, for example, as
1107 the value of a `syntax-table' text property. */)
1108 (Lisp_Object string
)
1110 const unsigned char *p
;
1114 CHECK_STRING (string
);
1117 val
= syntax_spec_code
[*p
++];
1119 error ("Invalid syntax description letter: %c", p
[-1]);
1121 if (val
== Sinherit
)
1127 int character
= STRING_CHAR_AND_LENGTH (p
, len
);
1128 XSETINT (match
, character
);
1129 if (XFASTINT (match
) == ' ')
1172 if (val
< ASIZE (Vsyntax_code_object
) && NILP (match
))
1173 return AREF (Vsyntax_code_object
, val
);
1175 /* Since we can't use a shared object, let's make a new one. */
1176 return Fcons (make_number (val
), match
);
1179 /* I really don't know why this is interactive
1180 help-form should at least be made useful whilst reading the second arg. */
1181 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
1182 "cSet syntax for character: \nsSet syntax for %s to: ",
1183 doc
: /* Set syntax for character CHAR according to string NEWENTRY.
1184 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1185 the current buffer's syntax table.
1186 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1187 in the range MIN to MAX are changed.
1188 The first character of NEWENTRY should be one of the following:
1189 Space or - whitespace syntax. w word constituent.
1190 _ symbol constituent. . punctuation.
1191 ( open-parenthesis. ) close-parenthesis.
1192 " string quote. \\ escape.
1193 $ paired delimiter. \\=' expression quote or prefix operator.
1194 < comment starter. > comment ender.
1195 / character-quote. @ inherit from parent table.
1196 | generic string fence. ! generic comment fence.
1198 Only single-character comment start and end sequences are represented thus.
1199 Two-character sequences are represented as described below.
1200 The second character of NEWENTRY is the matching parenthesis,
1201 used only if the first character is `(' or `)'.
1202 Any additional characters are flags.
1203 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1204 1 means CHAR is the start of a two-char comment start sequence.
1205 2 means CHAR is the second character of such a sequence.
1206 3 means CHAR is the start of a two-char comment end sequence.
1207 4 means CHAR is the second character of such a sequence.
1209 There can be several orthogonal comment sequences. This is to support
1210 language modes such as C++. By default, all comment sequences are of style
1211 a, but you can set the comment sequence style to b (on the second character
1212 of a comment-start, and the first character of a comment-end sequence) and/or
1213 c (on any of its chars) using this flag:
1214 b means CHAR is part of comment sequence b.
1215 c means CHAR is part of comment sequence c.
1216 n means CHAR is part of a nestable comment sequence.
1218 p means CHAR is a prefix character for `backward-prefix-chars';
1219 such characters are treated as whitespace when they occur
1220 between expressions.
1221 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1222 (Lisp_Object c
, Lisp_Object newentry
, Lisp_Object syntax_table
)
1226 CHECK_CHARACTER_CAR (c
);
1227 CHECK_CHARACTER_CDR (c
);
1230 CHECK_CHARACTER (c
);
1232 if (NILP (syntax_table
))
1233 syntax_table
= BVAR (current_buffer
, syntax_table
);
1235 check_syntax_table (syntax_table
);
1237 newentry
= Fstring_to_syntax (newentry
);
1239 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table
, c
, newentry
);
1241 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), newentry
);
1243 /* We clear the regexp cache, since character classes can now have
1244 different values from those in the compiled regexps.*/
1245 clear_regexp_cache ();
1250 /* Dump syntax table to buffer in human-readable format */
1252 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value
,
1253 Sinternal_describe_syntax_value
, 1, 1, 0,
1254 doc
: /* Insert a description of the internal syntax description SYNTAX at point. */)
1255 (Lisp_Object syntax
)
1257 int code
, syntax_code
;
1258 bool start1
, start2
, end1
, end2
, prefix
, comstyleb
, comstylec
, comnested
;
1260 Lisp_Object first
, match_lisp
, value
= syntax
;
1264 insert_string ("default");
1268 if (CHAR_TABLE_P (value
))
1270 insert_string ("deeper char-table ...");
1276 insert_string ("invalid");
1280 first
= XCAR (value
);
1281 match_lisp
= XCDR (value
);
1283 if (!INTEGERP (first
) || !(NILP (match_lisp
) || CHARACTERP (match_lisp
)))
1285 insert_string ("invalid");
1289 syntax_code
= XINT (first
) & INT_MAX
;
1290 code
= syntax_code
& 0377;
1291 start1
= SYNTAX_FLAGS_COMSTART_FIRST (syntax_code
);
1292 start2
= SYNTAX_FLAGS_COMSTART_SECOND (syntax_code
);
1293 end1
= SYNTAX_FLAGS_COMEND_FIRST (syntax_code
);
1294 end2
= SYNTAX_FLAGS_COMEND_SECOND (syntax_code
);
1295 prefix
= SYNTAX_FLAGS_PREFIX (syntax_code
);
1296 comstyleb
= SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code
);
1297 comstylec
= SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code
);
1298 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax_code
);
1302 insert_string ("invalid");
1306 str
[0] = syntax_code_spec
[code
], str
[1] = 0;
1309 if (NILP (match_lisp
))
1312 insert_char (XINT (match_lisp
));
1333 insert_string ("\twhich means: ");
1338 insert_string ("whitespace"); break;
1340 insert_string ("punctuation"); break;
1342 insert_string ("word"); break;
1344 insert_string ("symbol"); break;
1346 insert_string ("open"); break;
1348 insert_string ("close"); break;
1350 insert_string ("prefix"); break;
1352 insert_string ("string"); break;
1354 insert_string ("math"); break;
1356 insert_string ("escape"); break;
1358 insert_string ("charquote"); break;
1360 insert_string ("comment"); break;
1362 insert_string ("endcomment"); break;
1364 insert_string ("inherit"); break;
1365 case Scomment_fence
:
1366 insert_string ("comment fence"); break;
1368 insert_string ("string fence"); break;
1370 insert_string ("invalid");
1374 if (!NILP (match_lisp
))
1376 insert_string (", matches ");
1377 insert_char (XINT (match_lisp
));
1381 insert_string (",\n\t is the first character of a comment-start sequence");
1383 insert_string (",\n\t is the second character of a comment-start sequence");
1386 insert_string (",\n\t is the first character of a comment-end sequence");
1388 insert_string (",\n\t is the second character of a comment-end sequence");
1390 insert_string (" (comment style b)");
1392 insert_string (" (comment style c)");
1394 insert_string (" (nestable)");
1398 AUTO_STRING (prefixdoc
,
1399 ",\n\t is a prefix character for `backward-prefix-chars'");
1400 insert1 (Fsubstitute_command_keys (prefixdoc
));
1406 /* Return the position across COUNT words from FROM.
1407 If that many words cannot be found before the end of the buffer, return 0.
1408 COUNT negative means scan backward and stop at word beginning. */
1411 scan_words (register ptrdiff_t from
, register EMACS_INT count
)
1413 register ptrdiff_t beg
= BEGV
;
1414 register ptrdiff_t end
= ZV
;
1415 register ptrdiff_t from_byte
= CHAR_TO_BYTE (from
);
1416 register enum syntaxcode code
;
1418 Lisp_Object func
, pos
;
1423 SETUP_SYNTAX_TABLE (from
, count
);
1434 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1435 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1436 code
= SYNTAX (ch0
);
1437 INC_BOTH (from
, from_byte
);
1438 if (words_include_escapes
1439 && (code
== Sescape
|| code
== Scharquote
))
1444 /* Now CH0 is a character which begins a word and FROM is the
1445 position of the next character. */
1446 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch0
);
1447 if (! NILP (Ffboundp (func
)))
1449 pos
= call2 (func
, make_number (from
- 1), make_number (end
));
1450 if (INTEGERP (pos
) && from
< XINT (pos
) && XINT (pos
) <= ZV
)
1453 from_byte
= CHAR_TO_BYTE (from
);
1460 if (from
== end
) break;
1461 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1462 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1463 code
= SYNTAX (ch1
);
1465 && (! words_include_escapes
1466 || (code
!= Sescape
&& code
!= Scharquote
)))
1467 || word_boundary_p (ch0
, ch1
))
1469 INC_BOTH (from
, from_byte
);
1484 DEC_BOTH (from
, from_byte
);
1485 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1486 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1487 code
= SYNTAX (ch1
);
1488 if (words_include_escapes
1489 && (code
== Sescape
|| code
== Scharquote
))
1494 /* Now CH1 is a character which ends a word and FROM is the
1496 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch1
);
1497 if (! NILP (Ffboundp (func
)))
1499 pos
= call2 (func
, make_number (from
), make_number (beg
));
1500 if (INTEGERP (pos
) && BEGV
<= XINT (pos
) && XINT (pos
) < from
)
1503 from_byte
= CHAR_TO_BYTE (from
);
1512 DEC_BOTH (from
, from_byte
);
1513 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1514 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1515 code
= SYNTAX (ch0
);
1517 && (! words_include_escapes
1518 || (code
!= Sescape
&& code
!= Scharquote
)))
1519 || word_boundary_p (ch0
, ch1
))
1521 INC_BOTH (from
, from_byte
);
1535 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 0, 1, "^p",
1536 doc
: /* Move point forward ARG words (backward if ARG is negative).
1537 If ARG is omitted or nil, move point forward one word.
1539 If an edge of the buffer or a field boundary is reached, point is left there
1540 and the function returns nil. Field boundaries are not noticed if
1541 `inhibit-field-text-motion' is non-nil. */)
1545 ptrdiff_t orig_val
, val
;
1548 XSETFASTINT (arg
, 1);
1552 val
= orig_val
= scan_words (PT
, XINT (arg
));
1554 val
= XINT (arg
) > 0 ? ZV
: BEGV
;
1556 /* Avoid jumping out of an input field. */
1557 tmp
= Fconstrain_to_field (make_number (val
), make_number (PT
),
1559 val
= XFASTINT (tmp
);
1562 return val
== orig_val
? Qt
: Qnil
;
1565 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1566 doc
: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1567 STRING is like the inside of a `[...]' in a regular expression
1568 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1569 (but not at the end of a range; quoting is never needed there).
1570 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1571 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1572 Char classes, e.g. `[:alpha:]', are supported.
1574 Returns the distance traveled, either zero or positive. */)
1575 (Lisp_Object string
, Lisp_Object lim
)
1577 return skip_chars (1, string
, lim
, 1);
1580 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1581 doc
: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1582 See `skip-chars-forward' for details.
1583 Returns the distance traveled, either zero or negative. */)
1584 (Lisp_Object string
, Lisp_Object lim
)
1586 return skip_chars (0, string
, lim
, 1);
1589 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1590 doc
: /* Move point forward across chars in specified syntax classes.
1591 SYNTAX is a string of syntax code characters.
1592 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1593 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1594 This function returns the distance traveled, either zero or positive. */)
1595 (Lisp_Object syntax
, Lisp_Object lim
)
1597 return skip_syntaxes (1, syntax
, lim
);
1600 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1601 doc
: /* Move point backward across chars in specified syntax classes.
1602 SYNTAX is a string of syntax code characters.
1603 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1604 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1605 This function returns either zero or a negative number, and the absolute value
1606 of this is the distance traveled. */)
1607 (Lisp_Object syntax
, Lisp_Object lim
)
1609 return skip_syntaxes (0, syntax
, lim
);
1613 skip_chars (bool forwardp
, Lisp_Object string
, Lisp_Object lim
,
1614 bool handle_iso_classes
)
1618 /* Store the ranges of non-ASCII characters. */
1619 int *char_ranges
IF_LINT (= NULL
);
1620 int n_char_ranges
= 0;
1622 ptrdiff_t i
, i_byte
;
1623 /* True if the current buffer is multibyte and the region contains
1626 /* True if STRING is multibyte and it contains non-ASCII chars. */
1627 bool string_multibyte
;
1628 ptrdiff_t size_byte
;
1629 const unsigned char *str
;
1631 Lisp_Object iso_classes
;
1634 CHECK_STRING (string
);
1638 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1640 CHECK_NUMBER_COERCE_MARKER (lim
);
1642 /* In any case, don't allow scan outside bounds of buffer. */
1643 if (XINT (lim
) > ZV
)
1644 XSETFASTINT (lim
, ZV
);
1645 if (XINT (lim
) < BEGV
)
1646 XSETFASTINT (lim
, BEGV
);
1648 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1649 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1650 string_multibyte
= SBYTES (string
) > SCHARS (string
);
1652 memset (fastmap
, 0, sizeof fastmap
);
1654 str
= SDATA (string
);
1655 size_byte
= SBYTES (string
);
1658 if (i_byte
< size_byte
1659 && SREF (string
, 0) == '^')
1661 negate
= 1; i_byte
++;
1664 /* Find the characters specified and set their elements of fastmap.
1665 Handle backslashes and ranges specially.
1667 If STRING contains non-ASCII characters, setup char_ranges for
1668 them and use fastmap only for their leading codes. */
1670 if (! string_multibyte
)
1672 bool string_has_eight_bit
= 0;
1674 /* At first setup fastmap. */
1675 while (i_byte
< size_byte
)
1679 if (handle_iso_classes
&& c
== '['
1680 && i_byte
< size_byte
1681 && str
[i_byte
] == ':')
1683 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1684 const unsigned char *class_end
= class_beg
;
1685 const unsigned char *class_limit
= str
+ size_byte
- 2;
1686 /* Leave room for the null. */
1687 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1690 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1691 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1693 while (class_end
< class_limit
1694 && *class_end
>= 'a' && *class_end
<= 'z')
1697 if (class_end
== class_beg
1698 || *class_end
!= ':' || class_end
[1] != ']')
1699 goto not_a_class_name
;
1701 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1702 class_name
[class_end
- class_beg
] = 0;
1704 cc
= re_wctype (class_name
);
1706 error ("Invalid ISO C character class");
1708 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1710 i_byte
= class_end
+ 2 - str
;
1717 if (i_byte
== size_byte
)
1722 /* Treat `-' as range character only if another character
1724 if (i_byte
+ 1 < size_byte
1725 && str
[i_byte
] == '-')
1729 /* Skip over the dash. */
1732 /* Get the end of the range. */
1735 && i_byte
< size_byte
)
1743 if (! ASCII_CHAR_P (c2
))
1744 string_has_eight_bit
= 1;
1750 if (! ASCII_CHAR_P (c
))
1751 string_has_eight_bit
= 1;
1755 /* If the current range is multibyte and STRING contains
1756 eight-bit chars, arrange fastmap and setup char_ranges for
1757 the corresponding multibyte chars. */
1758 if (multibyte
&& string_has_eight_bit
)
1761 char himap
[0200 + 1];
1762 memcpy (himap
, fastmap
+ 0200, 0200);
1764 memset (fastmap
+ 0200, 0, 0200);
1765 SAFE_NALLOCA (char_ranges
, 2, 128);
1768 while ((p1
= memchr (himap
+ i
, 1, 0200 - i
)))
1770 /* Deduce the next range C..C2 from the next clump of 1s
1771 in HIMAP starting with &HIMAP[I]. HIMAP is the high
1772 order half of the old FASTMAP. */
1773 int c2
, leading_code
;
1775 c
= BYTE8_TO_CHAR (i
+ 0200);
1777 c2
= BYTE8_TO_CHAR (i
+ 0200 - 1);
1779 char_ranges
[n_char_ranges
++] = c
;
1780 char_ranges
[n_char_ranges
++] = c2
;
1781 leading_code
= CHAR_LEADING_CODE (c
);
1782 memset (fastmap
+ leading_code
, 1,
1783 CHAR_LEADING_CODE (c2
) - leading_code
+ 1);
1787 else /* STRING is multibyte */
1789 SAFE_NALLOCA (char_ranges
, 2, SCHARS (string
));
1791 while (i_byte
< size_byte
)
1793 int leading_code
= str
[i_byte
];
1794 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1797 if (handle_iso_classes
&& c
== '['
1798 && i_byte
< size_byte
1799 && STRING_CHAR (str
+ i_byte
) == ':')
1801 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1802 const unsigned char *class_end
= class_beg
;
1803 const unsigned char *class_limit
= str
+ size_byte
- 2;
1804 /* Leave room for the null. */
1805 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1808 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1809 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1811 while (class_end
< class_limit
1812 && *class_end
>= 'a' && *class_end
<= 'z')
1815 if (class_end
== class_beg
1816 || *class_end
!= ':' || class_end
[1] != ']')
1817 goto not_a_class_name_multibyte
;
1819 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1820 class_name
[class_end
- class_beg
] = 0;
1822 cc
= re_wctype (class_name
);
1824 error ("Invalid ISO C character class");
1826 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1828 i_byte
= class_end
+ 2 - str
;
1832 not_a_class_name_multibyte
:
1835 if (i_byte
== size_byte
)
1838 leading_code
= str
[i_byte
];
1839 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1842 /* Treat `-' as range character only if another character
1844 if (i_byte
+ 1 < size_byte
1845 && str
[i_byte
] == '-')
1847 int c2
, leading_code2
;
1849 /* Skip over the dash. */
1852 /* Get the end of the range. */
1853 leading_code2
= str
[i_byte
];
1854 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1858 && i_byte
< size_byte
)
1860 leading_code2
= str
[i_byte
];
1861 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1867 if (ASCII_CHAR_P (c
))
1869 while (c
<= c2
&& c
< 0x80)
1871 leading_code
= CHAR_LEADING_CODE (c
);
1873 if (! ASCII_CHAR_P (c
))
1875 int lim2
= leading_code2
+ 1;
1876 while (leading_code
< lim2
)
1877 fastmap
[leading_code
++] = 1;
1880 char_ranges
[n_char_ranges
++] = c
;
1881 char_ranges
[n_char_ranges
++] = c2
;
1887 if (ASCII_CHAR_P (c
))
1891 fastmap
[leading_code
] = 1;
1892 char_ranges
[n_char_ranges
++] = c
;
1893 char_ranges
[n_char_ranges
++] = c
;
1898 /* If the current range is unibyte and STRING contains non-ASCII
1899 chars, arrange fastmap for the corresponding unibyte
1902 if (! multibyte
&& n_char_ranges
> 0)
1904 memset (fastmap
+ 0200, 0, 0200);
1905 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1907 int c1
= char_ranges
[i
];
1908 int lim2
= char_ranges
[i
+ 1] + 1;
1910 for (; c1
< lim2
; c1
++)
1912 int b
= CHAR_TO_BYTE_SAFE (c1
);
1920 /* If ^ was the first character, complement the fastmap. */
1924 for (i
= 0; i
< sizeof fastmap
; i
++)
1928 for (i
= 0; i
< 0200; i
++)
1930 /* All non-ASCII chars possibly match. */
1931 for (; i
< sizeof fastmap
; i
++)
1937 ptrdiff_t start_point
= PT
;
1939 ptrdiff_t pos_byte
= PT_BYTE
;
1940 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1944 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1945 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1949 endp
= CHAR_POS_ADDR (XINT (lim
));
1950 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1954 /* This code may look up syntax tables using functions that rely on the
1955 gl_state object. To make sure this object is not out of date,
1956 let's initialize it manually.
1957 We ignore syntax-table text-properties for now, since that's
1958 what we've done in the past. */
1959 SETUP_BUFFER_SYNTAX_TABLE ();
1974 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
1975 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1985 if (! ASCII_CHAR_P (c
))
1987 /* As we are looking at a multibyte character, we
1988 must look up the character in the table
1989 CHAR_RANGES. If there's no data in the table,
1990 that character is not what we want to skip. */
1992 /* The following code do the right thing even if
1993 n_char_ranges is zero (i.e. no data in
1995 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1996 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1998 if (!(negate
^ (i
< n_char_ranges
)))
2002 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2015 if (!NILP (iso_classes
) && in_classes (*p
, iso_classes
))
2020 goto fwd_unibyte_ok
;
2026 p
++, pos
++, pos_byte
++;
2034 unsigned char *prev_p
;
2044 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2045 c
= STRING_CHAR (p
);
2047 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
2057 if (! ASCII_CHAR_P (c
))
2059 /* See the comment in the previous similar code. */
2060 for (i
= 0; i
< n_char_ranges
; i
+= 2)
2061 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
2063 if (!(negate
^ (i
< n_char_ranges
)))
2067 pos
--, pos_byte
-= prev_p
- p
;
2080 if (! NILP (iso_classes
) && in_classes (p
[-1], iso_classes
))
2085 goto back_unibyte_ok
;
2088 if (!fastmap
[p
[-1]])
2091 p
--, pos
--, pos_byte
--;
2095 SET_PT_BOTH (pos
, pos_byte
);
2099 return make_number (PT
- start_point
);
2105 skip_syntaxes (bool forwardp
, Lisp_Object string
, Lisp_Object lim
)
2108 unsigned char fastmap
[0400];
2110 ptrdiff_t i
, i_byte
;
2112 ptrdiff_t size_byte
;
2115 CHECK_STRING (string
);
2118 XSETINT (lim
, forwardp
? ZV
: BEGV
);
2120 CHECK_NUMBER_COERCE_MARKER (lim
);
2122 /* In any case, don't allow scan outside bounds of buffer. */
2123 if (XINT (lim
) > ZV
)
2124 XSETFASTINT (lim
, ZV
);
2125 if (XINT (lim
) < BEGV
)
2126 XSETFASTINT (lim
, BEGV
);
2128 if (forwardp
? (PT
>= XFASTINT (lim
)) : (PT
<= XFASTINT (lim
)))
2129 return make_number (0);
2131 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
2132 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
2134 memset (fastmap
, 0, sizeof fastmap
);
2136 if (SBYTES (string
) > SCHARS (string
))
2137 /* As this is very rare case (syntax spec is ASCII only), don't
2138 consider efficiency. */
2139 string
= string_make_unibyte (string
);
2141 str
= SDATA (string
);
2142 size_byte
= SBYTES (string
);
2145 if (i_byte
< size_byte
2146 && SREF (string
, 0) == '^')
2148 negate
= 1; i_byte
++;
2151 /* Find the syntaxes specified and set their elements of fastmap. */
2153 while (i_byte
< size_byte
)
2156 fastmap
[syntax_spec_code
[c
]] = 1;
2159 /* If ^ was the first character, complement the fastmap. */
2161 for (i
= 0; i
< sizeof fastmap
; i
++)
2165 ptrdiff_t start_point
= PT
;
2167 ptrdiff_t pos_byte
= PT_BYTE
;
2168 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
2172 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
2173 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
2177 endp
= CHAR_POS_ADDR (XINT (lim
));
2178 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
2182 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
2198 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
2199 if (! fastmap
[SYNTAX (c
)])
2201 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2202 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2216 if (! fastmap
[SYNTAX (*p
)])
2218 p
++, pos
++, pos_byte
++;
2219 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2229 unsigned char *prev_p
;
2238 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2240 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2241 c
= STRING_CHAR (p
);
2242 if (! fastmap
[SYNTAX (c
)])
2244 pos
--, pos_byte
-= prev_p
- p
;
2258 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2259 if (! fastmap
[SYNTAX (p
[-1])])
2261 p
--, pos
--, pos_byte
--;
2266 SET_PT_BOTH (pos
, pos_byte
);
2269 return make_number (PT
- start_point
);
2273 /* Return true if character C belongs to one of the ISO classes
2274 in the list ISO_CLASSES. Each class is represented by an
2275 integer which is its type according to re_wctype. */
2278 in_classes (int c
, Lisp_Object iso_classes
)
2280 bool fits_class
= 0;
2282 while (CONSP (iso_classes
))
2285 elt
= XCAR (iso_classes
);
2286 iso_classes
= XCDR (iso_classes
);
2288 if (re_iswctype (c
, XFASTINT (elt
)))
2295 /* Jump over a comment, assuming we are at the beginning of one.
2296 FROM is the current position.
2297 FROM_BYTE is the bytepos corresponding to FROM.
2298 Do not move past STOP (a charpos).
2299 The comment over which we have to jump is of style STYLE
2300 (either SYNTAX_FLAGS_COMMENT_STYLE (foo) or ST_COMMENT_STYLE).
2301 NESTING should be positive to indicate the nesting at the beginning
2302 for nested comments and should be zero or negative else.
2303 ST_COMMENT_STYLE cannot be nested.
2304 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2305 (or 0 If the search cannot start in the middle of a two-character).
2307 If successful, return true and store the charpos of the comment's end
2308 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2309 Else, return false and store the charpos STOP into *CHARPOS_PTR, the
2310 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2311 (as defined for state.incomment) in *INCOMMENT_PTR.
2313 The comment end is the last character of the comment rather than the
2314 character just after the comment.
2316 Global syntax data is assumed to initially be valid for FROM and
2317 remains valid for forward search starting at the returned position. */
2320 forw_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
,
2321 EMACS_INT nesting
, int style
, int prev_syntax
,
2322 ptrdiff_t *charpos_ptr
, ptrdiff_t *bytepos_ptr
,
2323 EMACS_INT
*incomment_ptr
)
2326 register enum syntaxcode code
;
2327 register int syntax
, other_syntax
;
2329 if (nesting
<= 0) nesting
= -1;
2331 /* Enter the loop in the middle so that we find
2332 a 2-char comment ender if we start in the middle of it. */
2333 syntax
= prev_syntax
;
2334 if (syntax
!= 0) goto forw_incomment
;
2340 *incomment_ptr
= nesting
;
2341 *charpos_ptr
= from
;
2342 *bytepos_ptr
= from_byte
;
2345 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2346 syntax
= SYNTAX_WITH_FLAGS (c
);
2347 code
= syntax
& 0xff;
2348 if (code
== Sendcomment
2349 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
2350 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
2351 (nesting
> 0 && --nesting
== 0) : nesting
< 0)
2352 && !(Vcomment_end_can_be_escaped
&& char_quoted (from
, from_byte
)))
2353 /* We have encountered a comment end of the same style
2354 as the comment sequence which began this comment
2357 if (code
== Scomment_fence
2358 && style
== ST_COMMENT_STYLE
)
2359 /* We have encountered a comment end of the same style
2360 as the comment sequence which began this comment
2365 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
2366 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
)
2367 /* We have encountered a nested comment of the same style
2368 as the comment sequence which began this comment section. */
2370 INC_BOTH (from
, from_byte
);
2371 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2374 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2375 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2376 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2377 SYNTAX_FLAGS_COMEND_SECOND (other_syntax
))
2378 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, other_syntax
) == style
2379 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2380 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
))
2381 ? nesting
> 0 : nesting
< 0))
2384 /* We have encountered a comment end of the same style
2385 as the comment sequence which began this comment section. */
2389 INC_BOTH (from
, from_byte
);
2390 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2395 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
2396 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2397 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2398 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
) == style
2399 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2400 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2401 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
)))
2402 /* We have encountered a nested comment of the same style
2403 as the comment sequence which began this comment section. */
2405 INC_BOTH (from
, from_byte
);
2406 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2410 *charpos_ptr
= from
;
2411 *bytepos_ptr
= from_byte
;
2415 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
2417 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2418 Stop scanning if we find something other than a comment or whitespace.
2419 Set point to where scanning stops.
2420 If COUNT comments are found as expected, with nothing except whitespace
2421 between them, return t; otherwise return nil. */)
2424 ptrdiff_t from
, from_byte
, stop
;
2426 enum syntaxcode code
;
2427 int comstyle
= 0; /* style of comment encountered */
2428 bool comnested
= 0; /* whether the comment is nestable or not */
2431 ptrdiff_t out_charpos
, out_bytepos
;
2434 CHECK_NUMBER (count
);
2435 count1
= XINT (count
);
2436 stop
= count1
> 0 ? ZV
: BEGV
;
2442 from_byte
= PT_BYTE
;
2444 SETUP_SYNTAX_TABLE (from
, count1
);
2449 bool comstart_first
;
2450 int syntax
, other_syntax
;
2454 SET_PT_BOTH (from
, from_byte
);
2458 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2459 syntax
= SYNTAX_WITH_FLAGS (c
);
2461 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2462 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2463 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2464 INC_BOTH (from
, from_byte
);
2465 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2466 if (from
< stop
&& comstart_first
2467 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2468 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2469 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
)))
2471 /* We have encountered a comment start sequence and we
2472 are ignoring all text inside comments. We must record
2473 the comment style this sequence begins so that later,
2474 only a comment end of the same style actually ends
2475 the comment section. */
2477 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2478 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2479 INC_BOTH (from
, from_byte
);
2480 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2483 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
2485 if (code
== Scomment_fence
)
2486 comstyle
= ST_COMMENT_STYLE
;
2487 else if (code
!= Scomment
)
2490 DEC_BOTH (from
, from_byte
);
2491 SET_PT_BOTH (from
, from_byte
);
2494 /* We're at the start of a comment. */
2495 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
2496 &out_charpos
, &out_bytepos
, &dummy
);
2497 from
= out_charpos
; from_byte
= out_bytepos
;
2501 SET_PT_BOTH (from
, from_byte
);
2504 INC_BOTH (from
, from_byte
);
2505 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2506 /* We have skipped one comment. */
2519 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
2524 DEC_BOTH (from
, from_byte
);
2525 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2526 quoted
= char_quoted (from
, from_byte
);
2527 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2528 syntax
= SYNTAX_WITH_FLAGS (c
);
2531 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2532 if (code
== Sendcomment
)
2533 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2534 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2535 && prev_char_comend_first (from
, from_byte
)
2536 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
2539 /* We must record the comment style encountered so that
2540 later, we can match only the proper comment begin
2541 sequence of the same style. */
2542 DEC_BOTH (from
, from_byte
);
2544 /* Calling char_quoted, above, set up global syntax position
2545 at the new value of FROM. */
2546 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2547 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2548 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2549 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2552 if (code
== Scomment_fence
)
2554 /* Skip until first preceding unquoted comment_fence. */
2555 bool fence_found
= 0;
2556 ptrdiff_t ini
= from
, ini_byte
= from_byte
;
2560 DEC_BOTH (from
, from_byte
);
2561 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2562 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2563 if (SYNTAX (c
) == Scomment_fence
2564 && !char_quoted (from
, from_byte
))
2569 else if (from
== stop
)
2572 if (fence_found
== 0)
2574 from
= ini
; /* Set point to ini + 1. */
2575 from_byte
= ini_byte
;
2579 /* We have skipped one comment. */
2582 else if (code
== Sendcomment
)
2584 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2585 &out_charpos
, &out_bytepos
);
2589 /* This end-of-line is not an end-of-comment.
2590 Treat it like a whitespace.
2591 CC-mode (and maybe others) relies on this behavior. */
2595 /* Failure: we should go back to the end of this
2596 not-quite-endcomment. */
2597 if (SYNTAX (c
) != code
)
2598 /* It was a two-char Sendcomment. */
2599 INC_BOTH (from
, from_byte
);
2605 /* We have skipped one comment. */
2606 from
= out_charpos
, from_byte
= out_bytepos
;
2610 else if (code
!= Swhitespace
|| quoted
)
2614 INC_BOTH (from
, from_byte
);
2615 SET_PT_BOTH (from
, from_byte
);
2623 SET_PT_BOTH (from
, from_byte
);
2628 /* Return syntax code of character C if C is an ASCII character
2629 or if MULTIBYTE_SYMBOL_P is false. Otherwise, return Ssymbol. */
2631 static enum syntaxcode
2632 syntax_multibyte (int c
, bool multibyte_symbol_p
)
2634 return ASCII_CHAR_P (c
) || !multibyte_symbol_p
? SYNTAX (c
) : Ssymbol
;
2638 scan_lists (EMACS_INT from
, EMACS_INT count
, EMACS_INT depth
, bool sexpflag
)
2641 ptrdiff_t stop
= count
> 0 ? ZV
: BEGV
;
2646 enum syntaxcode code
;
2647 EMACS_INT min_depth
= depth
; /* Err out if depth gets less than this. */
2648 int comstyle
= 0; /* Style of comment encountered. */
2649 bool comnested
= 0; /* Whether the comment is nestable or not. */
2651 EMACS_INT last_good
= from
;
2653 ptrdiff_t from_byte
;
2654 ptrdiff_t out_bytepos
, out_charpos
;
2656 bool multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2658 if (depth
> 0) min_depth
= 0;
2660 if (from
> ZV
) from
= ZV
;
2661 if (from
< BEGV
) from
= BEGV
;
2663 from_byte
= CHAR_TO_BYTE (from
);
2668 SETUP_SYNTAX_TABLE (from
, count
);
2673 bool comstart_first
, prefix
;
2674 int syntax
, other_syntax
;
2675 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2676 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2677 syntax
= SYNTAX_WITH_FLAGS (c
);
2678 code
= syntax_multibyte (c
, multibyte_symbol_p
);
2679 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2680 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2681 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2682 prefix
= SYNTAX_FLAGS_PREFIX (syntax
);
2683 if (depth
== min_depth
)
2685 INC_BOTH (from
, from_byte
);
2686 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2687 if (from
< stop
&& comstart_first
2688 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2689 other_syntax
= SYNTAX_WITH_FLAGS (c
),
2690 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2691 && parse_sexp_ignore_comments
)
2693 /* We have encountered a comment start sequence and we
2694 are ignoring all text inside comments. We must record
2695 the comment style this sequence begins so that later,
2696 only a comment end of the same style actually ends
2697 the comment section. */
2699 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2700 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2701 INC_BOTH (from
, from_byte
);
2702 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2714 INC_BOTH (from
, from_byte
);
2715 /* Treat following character as a word constituent. */
2718 if (depth
|| !sexpflag
) break;
2719 /* This word counts as a sexp; return at end of it. */
2722 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2724 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2725 switch (syntax_multibyte (c
, multibyte_symbol_p
))
2729 INC_BOTH (from
, from_byte
);
2740 INC_BOTH (from
, from_byte
);
2744 case Scomment_fence
:
2745 comstyle
= ST_COMMENT_STYLE
;
2748 if (!parse_sexp_ignore_comments
) break;
2749 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2750 found
= forw_comment (from
, from_byte
, stop
,
2751 comnested
, comstyle
, 0,
2752 &out_charpos
, &out_bytepos
, &dummy
);
2753 from
= out_charpos
, from_byte
= out_bytepos
;
2760 INC_BOTH (from
, from_byte
);
2761 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2767 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (from_byte
))
2769 INC_BOTH (from
, from_byte
);
2779 if (!++depth
) goto done
;
2784 if (!--depth
) goto done
;
2785 if (depth
< min_depth
)
2786 xsignal3 (Qscan_error
,
2787 build_string ("Containing expression ends prematurely"),
2788 make_number (last_good
), make_number (from
));
2793 temp_pos
= dec_bytepos (from_byte
);
2794 stringterm
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2797 enum syntaxcode c_code
;
2800 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2801 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2802 c_code
= syntax_multibyte (c
, multibyte_symbol_p
);
2804 ? c
== stringterm
&& c_code
== Sstring
2805 : c_code
== Sstring_fence
)
2808 if (c_code
== Scharquote
|| c_code
== Sescape
)
2809 INC_BOTH (from
, from_byte
);
2810 INC_BOTH (from
, from_byte
);
2812 INC_BOTH (from
, from_byte
);
2813 if (!depth
&& sexpflag
) goto done
;
2816 /* Ignore whitespace, punctuation, quote, endcomment. */
2821 /* Reached end of buffer. Error if within object, return nil if between */
2828 /* End of object reached */
2838 DEC_BOTH (from
, from_byte
);
2839 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2840 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2841 syntax
= SYNTAX_WITH_FLAGS (c
);
2842 code
= syntax_multibyte (c
, multibyte_symbol_p
);
2843 if (depth
== min_depth
)
2846 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2847 if (code
== Sendcomment
)
2848 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2849 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2850 && prev_char_comend_first (from
, from_byte
)
2851 && parse_sexp_ignore_comments
)
2853 /* We must record the comment style encountered so that
2854 later, we can match only the proper comment begin
2855 sequence of the same style. */
2856 int c2
, other_syntax
;
2857 DEC_BOTH (from
, from_byte
);
2858 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2860 c2
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2861 other_syntax
= SYNTAX_WITH_FLAGS (c2
);
2862 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2863 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2866 /* Quoting turns anything except a comment-ender
2867 into a word character. Note that this cannot be true
2868 if we decremented FROM in the if-statement above. */
2869 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2871 DEC_BOTH (from
, from_byte
);
2874 else if (SYNTAX_FLAGS_PREFIX (syntax
))
2883 if (depth
|| !sexpflag
) break;
2884 /* This word counts as a sexp; count object finished
2885 after passing it. */
2888 temp_pos
= from_byte
;
2889 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
2893 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2894 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2895 /* Don't allow comment-end to be quoted. */
2896 if (syntax_multibyte (c1
, multibyte_symbol_p
) == Sendcomment
)
2898 quoted
= char_quoted (from
- 1, temp_pos
);
2901 DEC_BOTH (from
, from_byte
);
2902 temp_pos
= dec_bytepos (temp_pos
);
2903 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2905 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2907 switch (syntax_multibyte (c1
, multibyte_symbol_p
))
2909 case Sword
: case Ssymbol
: case Squote
: break;
2910 default: goto done2
;
2912 DEC_BOTH (from
, from_byte
);
2921 temp_pos
= dec_bytepos (from_byte
);
2922 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2923 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (temp_pos
))
2924 DEC_BOTH (from
, from_byte
);
2934 if (!++depth
) goto done2
;
2939 if (!--depth
) goto done2
;
2940 if (depth
< min_depth
)
2941 xsignal3 (Qscan_error
,
2942 build_string ("Containing expression ends prematurely"),
2943 make_number (last_good
), make_number (from
));
2947 if (!parse_sexp_ignore_comments
)
2949 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2950 &out_charpos
, &out_bytepos
);
2951 /* FIXME: if !found, it really wasn't a comment-end.
2952 For single-char Sendcomment, we can't do much about it apart
2953 from skipping the char.
2954 For 2-char endcomments, we could try again, taking both
2955 chars as separate entities, but it's a lot of trouble
2956 for very little gain, so we don't bother either. -sm */
2958 from
= out_charpos
, from_byte
= out_bytepos
;
2961 case Scomment_fence
:
2967 DEC_BOTH (from
, from_byte
);
2968 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2969 if (!char_quoted (from
, from_byte
))
2971 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2972 if (syntax_multibyte (c
, multibyte_symbol_p
) == code
)
2976 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2980 stringterm
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2985 DEC_BOTH (from
, from_byte
);
2986 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2987 if (!char_quoted (from
, from_byte
))
2989 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2991 && (syntax_multibyte (c
, multibyte_symbol_p
)
2996 if (!depth
&& sexpflag
) goto done2
;
2999 /* Ignore whitespace, punctuation, quote, endcomment. */
3004 /* Reached start of buffer. Error if within object, return nil if between */
3017 XSETFASTINT (val
, from
);
3021 xsignal3 (Qscan_error
,
3022 build_string ("Unbalanced parentheses"),
3023 make_number (last_good
), make_number (from
));
3026 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
3027 doc
: /* Scan from character number FROM by COUNT lists.
3028 Scan forward if COUNT is positive, backward if COUNT is negative.
3029 Return the character number of the position thus found.
3031 A \"list", in this context, refers to a balanced parenthetical
3032 grouping, as determined by the syntax table.
3034 If DEPTH is nonzero, treat that as the nesting depth of the starting
3035 point (i.e. the starting point is DEPTH parentheses deep). This
3036 function scans over parentheses until the depth goes to zero COUNT
3037 times. Hence, positive DEPTH moves out that number of levels of
3038 parentheses, while negative DEPTH moves to a deeper level.
3040 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3042 If we reach the beginning or end of the accessible part of the buffer
3043 before we have scanned over COUNT lists, return nil if the depth at
3044 that point is zero, and signal a error if the depth is nonzero. */)
3045 (Lisp_Object from
, Lisp_Object count
, Lisp_Object depth
)
3047 CHECK_NUMBER (from
);
3048 CHECK_NUMBER (count
);
3049 CHECK_NUMBER (depth
);
3051 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
3054 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
3055 doc
: /* Scan from character number FROM by COUNT balanced expressions.
3056 If COUNT is negative, scan backwards.
3057 Returns the character number of the position thus found.
3059 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3061 If the beginning or end of (the accessible part of) the buffer is reached
3062 in the middle of a parenthetical grouping, an error is signaled.
3063 If the beginning or end is reached between groupings
3064 but before count is used up, nil is returned. */)
3065 (Lisp_Object from
, Lisp_Object count
)
3067 CHECK_NUMBER (from
);
3068 CHECK_NUMBER (count
);
3070 return scan_lists (XINT (from
), XINT (count
), 0, 1);
3073 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
3075 doc
: /* Move point backward over any number of chars with prefix syntax.
3076 This includes chars with expression prefix syntax class (') and those with
3077 the prefix syntax flag (p). */)
3080 ptrdiff_t beg
= BEGV
;
3081 ptrdiff_t opoint
= PT
;
3082 ptrdiff_t opoint_byte
= PT_BYTE
;
3084 ptrdiff_t pos_byte
= PT_BYTE
;
3089 SET_PT_BOTH (opoint
, opoint_byte
);
3094 SETUP_SYNTAX_TABLE (pos
, -1);
3096 DEC_BOTH (pos
, pos_byte
);
3098 while (!char_quoted (pos
, pos_byte
)
3099 /* Previous statement updates syntax table. */
3100 && ((c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
), SYNTAX (c
) == Squote
)
3101 || syntax_prefix_flag_p (c
)))
3104 opoint_byte
= pos_byte
;
3107 DEC_BOTH (pos
, pos_byte
);
3110 SET_PT_BOTH (opoint
, opoint_byte
);
3115 /* Parse forward from FROM / FROM_BYTE to END,
3116 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
3117 and return a description of the state of the parse at END.
3118 If STOPBEFORE, stop at the start of an atom.
3119 If COMMENTSTOP is 1, stop at the start of a comment.
3120 If COMMENTSTOP is -1, stop at the start or end of a comment,
3121 after the beginning of a string, or after the end of a string. */
3124 scan_sexps_forward (struct lisp_parse_state
*stateptr
,
3125 ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t end
,
3126 EMACS_INT targetdepth
, bool stopbefore
,
3127 Lisp_Object oldstate
, int commentstop
)
3129 struct lisp_parse_state state
;
3130 enum syntaxcode code
;
3133 struct level
{ ptrdiff_t last
, prev
; };
3134 struct level levelstart
[100];
3135 struct level
*curlevel
= levelstart
;
3136 struct level
*endlevel
= levelstart
+ 100;
3137 EMACS_INT depth
; /* Paren depth of current scanning location.
3138 level - levelstart equals this except
3139 when the depth becomes negative. */
3140 EMACS_INT mindepth
; /* Lowest DEPTH value seen. */
3141 bool start_quoted
= 0; /* True means starting after a char quote. */
3143 ptrdiff_t prev_from
; /* Keep one character before FROM. */
3144 ptrdiff_t prev_from_byte
;
3145 int prev_from_syntax
;
3146 bool boundary_stop
= commentstop
== -1;
3149 ptrdiff_t out_bytepos
, out_charpos
;
3153 prev_from_byte
= from_byte
;
3155 DEC_BOTH (prev_from
, prev_from_byte
);
3157 /* Use this macro instead of `from++'. */
3159 do { prev_from = from; \
3160 prev_from_byte = from_byte; \
3161 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
3162 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
3163 INC_BOTH (from, from_byte); \
3165 UPDATE_SYNTAX_TABLE_FORWARD (from); \
3171 if (NILP (oldstate
))
3174 state
.instring
= -1;
3175 state
.incomment
= 0;
3176 state
.comstyle
= 0; /* comment style a by default. */
3177 state
.comstr_start
= -1; /* no comment/string seen. */
3181 tem
= Fcar (oldstate
);
3187 oldstate
= Fcdr (oldstate
);
3188 oldstate
= Fcdr (oldstate
);
3189 oldstate
= Fcdr (oldstate
);
3190 tem
= Fcar (oldstate
);
3191 /* Check whether we are inside string_fence-style string: */
3192 state
.instring
= (!NILP (tem
)
3193 ? (CHARACTERP (tem
) ? XFASTINT (tem
) : ST_STRING_STYLE
)
3196 oldstate
= Fcdr (oldstate
);
3197 tem
= Fcar (oldstate
);
3198 state
.incomment
= (!NILP (tem
)
3199 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
3202 oldstate
= Fcdr (oldstate
);
3203 tem
= Fcar (oldstate
);
3204 start_quoted
= !NILP (tem
);
3206 /* if the eighth element of the list is nil, we are in comment
3207 style a. If it is non-nil, we are in comment style b */
3208 oldstate
= Fcdr (oldstate
);
3209 oldstate
= Fcdr (oldstate
);
3210 tem
= Fcar (oldstate
);
3211 state
.comstyle
= (NILP (tem
)
3213 : (RANGED_INTEGERP (0, tem
, ST_COMMENT_STYLE
)
3215 : ST_COMMENT_STYLE
));
3217 oldstate
= Fcdr (oldstate
);
3218 tem
= Fcar (oldstate
);
3219 state
.comstr_start
=
3220 RANGED_INTEGERP (PTRDIFF_MIN
, tem
, PTRDIFF_MAX
) ? XINT (tem
) : -1;
3221 oldstate
= Fcdr (oldstate
);
3222 tem
= Fcar (oldstate
);
3223 while (!NILP (tem
)) /* >= second enclosing sexps. */
3225 Lisp_Object temhd
= Fcar (tem
);
3226 if (RANGED_INTEGERP (PTRDIFF_MIN
, temhd
, PTRDIFF_MAX
))
3227 curlevel
->last
= XINT (temhd
);
3228 if (++curlevel
== endlevel
)
3229 curlevel
--; /* error ("Nesting too deep for parser"); */
3230 curlevel
->prev
= -1;
3231 curlevel
->last
= -1;
3238 curlevel
->prev
= -1;
3239 curlevel
->last
= -1;
3241 SETUP_SYNTAX_TABLE (prev_from
, 1);
3242 temp
= FETCH_CHAR (prev_from_byte
);
3243 prev_from_syntax
= SYNTAX_WITH_FLAGS (temp
);
3244 UPDATE_SYNTAX_TABLE_FORWARD (from
);
3246 /* Enter the loop at a place appropriate for initial state. */
3248 if (state
.incomment
)
3249 goto startincomment
;
3250 if (state
.instring
>= 0)
3252 nofence
= state
.instring
!= ST_STRING_STYLE
;
3254 goto startquotedinstring
;
3257 else if (start_quoted
)
3264 code
= prev_from_syntax
& 0xff;
3267 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
)
3268 && (c1
= FETCH_CHAR (from_byte
),
3269 syntax
= SYNTAX_WITH_FLAGS (c1
),
3270 SYNTAX_FLAGS_COMSTART_SECOND (syntax
)))
3271 /* Duplicate code to avoid a complex if-expression
3272 which causes trouble for the SGI compiler. */
3274 /* Record the comment style we have entered so that only
3275 the comment-end sequence of the same style actually
3276 terminates the comment section. */
3278 = SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_from_syntax
);
3279 comnested
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
)
3280 | SYNTAX_FLAGS_COMMENT_NESTED (syntax
));
3281 state
.incomment
= comnested
? 1 : -1;
3282 state
.comstr_start
= prev_from
;
3286 else if (code
== Scomment_fence
)
3288 /* Record the comment style we have entered so that only
3289 the comment-end sequence of the same style actually
3290 terminates the comment section. */
3291 state
.comstyle
= ST_COMMENT_STYLE
;
3292 state
.incomment
= -1;
3293 state
.comstr_start
= prev_from
;
3296 else if (code
== Scomment
)
3298 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
, 0);
3299 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
3301 state
.comstr_start
= prev_from
;
3304 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
3310 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3311 curlevel
->last
= prev_from
;
3313 if (from
== end
) goto endquoted
;
3316 /* treat following character as a word constituent */
3319 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3320 curlevel
->last
= prev_from
;
3324 int symchar
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3325 switch (SYNTAX (symchar
))
3330 if (from
== end
) goto endquoted
;
3342 curlevel
->prev
= curlevel
->last
;
3345 case Scomment_fence
: /* Can't happen because it's handled above. */
3347 if (commentstop
|| boundary_stop
) goto done
;
3349 /* The (from == BEGV) test was to enter the loop in the middle so
3350 that we find a 2-char comment ender even if we start in the
3351 middle of it. We don't want to do that if we're just at the
3352 beginning of the comment (think of (*) ... (*)). */
3353 found
= forw_comment (from
, from_byte
, end
,
3354 state
.incomment
, state
.comstyle
,
3355 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
3356 ? 0 : prev_from_syntax
,
3357 &out_charpos
, &out_bytepos
, &state
.incomment
);
3358 from
= out_charpos
; from_byte
= out_bytepos
;
3359 /* Beware! prev_from and friends are invalid now.
3360 Luckily, the `done' doesn't use them and the INC_FROM
3361 sets them to a sane value without looking at them. */
3362 if (!found
) goto done
;
3364 state
.incomment
= 0;
3365 state
.comstyle
= 0; /* reset the comment style */
3366 if (boundary_stop
) goto done
;
3370 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3372 /* curlevel++->last ran into compiler bug on Apollo */
3373 curlevel
->last
= prev_from
;
3374 if (++curlevel
== endlevel
)
3375 curlevel
--; /* error ("Nesting too deep for parser"); */
3376 curlevel
->prev
= -1;
3377 curlevel
->last
= -1;
3378 if (targetdepth
== depth
) goto done
;
3383 if (depth
< mindepth
)
3385 if (curlevel
!= levelstart
)
3387 curlevel
->prev
= curlevel
->last
;
3388 if (targetdepth
== depth
) goto done
;
3393 state
.comstr_start
= from
- 1;
3394 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3395 curlevel
->last
= prev_from
;
3396 state
.instring
= (code
== Sstring
3397 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte
))
3399 if (boundary_stop
) goto done
;
3402 nofence
= state
.instring
!= ST_STRING_STYLE
;
3407 enum syntaxcode c_code
;
3409 if (from
>= end
) goto done
;
3410 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3411 c_code
= SYNTAX (c
);
3413 /* Check C_CODE here so that if the char has
3414 a syntax-table property which says it is NOT
3415 a string character, it does not end the string. */
3416 if (nofence
&& c
== state
.instring
&& c_code
== Sstring
)
3422 if (!nofence
) goto string_end
;
3428 startquotedinstring
:
3429 if (from
>= end
) goto endquoted
;
3439 state
.instring
= -1;
3440 curlevel
->prev
= curlevel
->last
;
3442 if (boundary_stop
) goto done
;
3446 /* FIXME: We should do something with it. */
3449 /* Ignore whitespace, punctuation, quote, endcomment. */
3455 stop
: /* Here if stopping before start of sexp. */
3456 from
= prev_from
; /* We have just fetched the char that starts it; */
3457 from_byte
= prev_from_byte
;
3458 goto done
; /* but return the position before it. */
3463 state
.depth
= depth
;
3464 state
.mindepth
= mindepth
;
3465 state
.thislevelstart
= curlevel
->prev
;
3466 state
.prevlevelstart
3467 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
3468 state
.location
= from
;
3469 state
.location_byte
= from_byte
;
3470 state
.levelstarts
= Qnil
;
3471 while (curlevel
> levelstart
)
3472 state
.levelstarts
= Fcons (make_number ((--curlevel
)->last
),
3479 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
3480 doc
: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3481 Parsing stops at TO or when certain criteria are met;
3482 point is set to where parsing stops.
3483 If fifth arg OLDSTATE is omitted or nil,
3484 parsing assumes that FROM is the beginning of a function.
3485 Value is a list of elements describing final state of parsing:
3487 1. character address of start of innermost containing list; nil if none.
3488 2. character address of start of last complete sexp terminated.
3489 3. non-nil if inside a string.
3490 (it is the character that will terminate the string,
3491 or t if the string should be terminated by a generic string delimiter.)
3492 4. nil if outside a comment, t if inside a non-nestable comment,
3493 else an integer (the current comment nesting).
3494 5. t if following a quote character.
3495 6. the minimum paren-depth encountered during this scan.
3496 7. style of comment, if any.
3497 8. character address of start of comment or string; nil if not in one.
3498 9. Intermediate data for continuation of parsing (subject to change).
3499 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3500 in parentheses becomes equal to TARGETDEPTH.
3501 Fourth arg STOPBEFORE non-nil means stop when come to
3502 any character that starts a sexp.
3503 Fifth arg OLDSTATE is a list like what this function returns.
3504 It is used to initialize the state of the parse. Elements number 1, 2, 6
3506 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3507 If it is symbol `syntax-table', stop after the start of a comment or a
3508 string, or after end of a comment or a string. */)
3509 (Lisp_Object from
, Lisp_Object to
, Lisp_Object targetdepth
,
3510 Lisp_Object stopbefore
, Lisp_Object oldstate
, Lisp_Object commentstop
)
3512 struct lisp_parse_state state
;
3515 if (!NILP (targetdepth
))
3517 CHECK_NUMBER (targetdepth
);
3518 target
= XINT (targetdepth
);
3521 target
= TYPE_MINIMUM (EMACS_INT
); /* We won't reach this depth. */
3523 validate_region (&from
, &to
);
3524 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
3526 target
, !NILP (stopbefore
), oldstate
,
3528 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
3530 SET_PT_BOTH (state
.location
, state
.location_byte
);
3532 return Fcons (make_number (state
.depth
),
3533 Fcons (state
.prevlevelstart
< 0
3534 ? Qnil
: make_number (state
.prevlevelstart
),
3535 Fcons (state
.thislevelstart
< 0
3536 ? Qnil
: make_number (state
.thislevelstart
),
3537 Fcons (state
.instring
>= 0
3538 ? (state
.instring
== ST_STRING_STYLE
3539 ? Qt
: make_number (state
.instring
)) : Qnil
,
3540 Fcons (state
.incomment
< 0 ? Qt
:
3541 (state
.incomment
== 0 ? Qnil
:
3542 make_number (state
.incomment
)),
3543 Fcons (state
.quoted
? Qt
: Qnil
,
3544 Fcons (make_number (state
.mindepth
),
3545 Fcons ((state
.comstyle
3546 ? (state
.comstyle
== ST_COMMENT_STYLE
3548 : make_number (state
.comstyle
))
3550 Fcons (((state
.incomment
3551 || (state
.instring
>= 0))
3552 ? make_number (state
.comstr_start
)
3554 Fcons (state
.levelstarts
, Qnil
))))))))));
3558 init_syntax_once (void)
3563 /* This has to be done here, before we call Fmake_char_table. */
3564 DEFSYM (Qsyntax_table
, "syntax-table");
3566 /* Create objects which can be shared among syntax tables. */
3567 Vsyntax_code_object
= make_uninit_vector (Smax
);
3568 for (i
= 0; i
< Smax
; i
++)
3569 ASET (Vsyntax_code_object
, i
, Fcons (make_number (i
), Qnil
));
3571 /* Now we are ready to set up this property, so we can
3572 create syntax tables. */
3573 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
3575 temp
= AREF (Vsyntax_code_object
, Swhitespace
);
3577 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
3579 /* Control characters should not be whitespace. */
3580 temp
= AREF (Vsyntax_code_object
, Spunct
);
3581 for (i
= 0; i
<= ' ' - 1; i
++)
3582 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3583 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 0177, temp
);
3585 /* Except that a few really are whitespace. */
3586 temp
= AREF (Vsyntax_code_object
, Swhitespace
);
3587 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ' ', temp
);
3588 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\t', temp
);
3589 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\n', temp
);
3590 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 015, temp
);
3591 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 014, temp
);
3593 temp
= AREF (Vsyntax_code_object
, Sword
);
3594 for (i
= 'a'; i
<= 'z'; i
++)
3595 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3596 for (i
= 'A'; i
<= 'Z'; i
++)
3597 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3598 for (i
= '0'; i
<= '9'; i
++)
3599 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3601 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
3602 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
3604 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
3605 Fcons (make_number (Sopen
), make_number (')')));
3606 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
3607 Fcons (make_number (Sclose
), make_number ('(')));
3608 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
3609 Fcons (make_number (Sopen
), make_number (']')));
3610 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
3611 Fcons (make_number (Sclose
), make_number ('[')));
3612 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
3613 Fcons (make_number (Sopen
), make_number ('}')));
3614 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
3615 Fcons (make_number (Sclose
), make_number ('{')));
3616 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
3617 Fcons (make_number (Sstring
), Qnil
));
3618 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
3619 Fcons (make_number (Sescape
), Qnil
));
3621 temp
= AREF (Vsyntax_code_object
, Ssymbol
);
3622 for (i
= 0; i
< 10; i
++)
3624 c
= "_-+*/&|<>="[i
];
3625 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3628 temp
= AREF (Vsyntax_code_object
, Spunct
);
3629 for (i
= 0; i
< 12; i
++)
3631 c
= ".,;:?!#@~^'`"[i
];
3632 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3635 /* All multibyte characters have syntax `word' by default. */
3636 temp
= AREF (Vsyntax_code_object
, Sword
);
3637 char_table_set_range (Vstandard_syntax_table
, 0x80, MAX_CHAR
, temp
);
3641 syms_of_syntax (void)
3643 DEFSYM (Qsyntax_table_p
, "syntax-table-p");
3645 staticpro (&Vsyntax_code_object
);
3647 staticpro (&gl_state
.object
);
3648 staticpro (&gl_state
.global_code
);
3649 staticpro (&gl_state
.current_syntax_table
);
3650 staticpro (&gl_state
.old_prop
);
3652 /* Defined in regex.c. */
3653 staticpro (&re_match_object
);
3655 DEFSYM (Qscan_error
, "scan-error");
3656 Fput (Qscan_error
, Qerror_conditions
,
3657 listn (CONSTYPE_PURE
, 2, Qscan_error
, Qerror
));
3658 Fput (Qscan_error
, Qerror_message
,
3659 build_pure_c_string ("Scan error"));
3661 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments
,
3662 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3664 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties
,
3665 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3666 Otherwise, that text property is simply ignored.
3667 See the info node `(elisp)Syntax Properties' for a description of the
3668 `syntax-table' property. */);
3670 DEFVAR_INT ("syntax-propertize--done", syntax_propertize__done
,
3671 doc
: /* Position up to which syntax-table properties have been set. */);
3672 syntax_propertize__done
= -1;
3673 DEFSYM (Qinternal__syntax_propertize
, "internal--syntax-propertize");
3674 Fmake_variable_buffer_local (intern ("syntax-propertize--done"));
3676 words_include_escapes
= 0;
3677 DEFVAR_BOOL ("words-include-escapes", words_include_escapes
,
3678 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3680 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol
,
3681 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3682 multibyte_syntax_as_symbol
= 0;
3684 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3685 open_paren_in_column_0_is_defun_start
,
3686 doc
: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3687 open_paren_in_column_0_is_defun_start
= 1;
3690 DEFVAR_LISP ("find-word-boundary-function-table",
3691 Vfind_word_boundary_function_table
,
3693 Char table of functions to search for the word boundary.
3694 Each function is called with two arguments; POS and LIMIT.
3695 POS and LIMIT are character positions in the current buffer.
3697 If POS is less than LIMIT, POS is at the first character of a word,
3698 and the return value of a function is a position after the last
3699 character of that word.
3701 If POS is not less than LIMIT, POS is at the last character of a word,
3702 and the return value of a function is a position at the first
3703 character of that word.
3705 In both cases, LIMIT bounds the search. */);
3706 Vfind_word_boundary_function_table
= Fmake_char_table (Qnil
, Qnil
);
3708 DEFVAR_BOOL ("comment-end-can-be-escaped", Vcomment_end_can_be_escaped
,
3709 doc
: /* Non-nil means an escaped ender inside a comment doesn'tend the comment. */);
3710 Vcomment_end_can_be_escaped
= 0;
3711 DEFSYM (Qcomment_end_can_be_escaped
, "comment-end-can-be-escaped");
3712 Fmake_variable_buffer_local (Qcomment_end_can_be_escaped
);
3714 defsubr (&Ssyntax_table_p
);
3715 defsubr (&Ssyntax_table
);
3716 defsubr (&Sstandard_syntax_table
);
3717 defsubr (&Scopy_syntax_table
);
3718 defsubr (&Sset_syntax_table
);
3719 defsubr (&Schar_syntax
);
3720 defsubr (&Smatching_paren
);
3721 defsubr (&Sstring_to_syntax
);
3722 defsubr (&Smodify_syntax_entry
);
3723 defsubr (&Sinternal_describe_syntax_value
);
3725 defsubr (&Sforward_word
);
3727 defsubr (&Sskip_chars_forward
);
3728 defsubr (&Sskip_chars_backward
);
3729 defsubr (&Sskip_syntax_forward
);
3730 defsubr (&Sskip_syntax_backward
);
3732 defsubr (&Sforward_comment
);
3733 defsubr (&Sscan_lists
);
3734 defsubr (&Sscan_sexps
);
3735 defsubr (&Sbackward_prefix_chars
);
3736 defsubr (&Sparse_partial_sexp
);