1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2016 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 (at
10 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_COMSTARTEND_FIRST (int flags
)
86 return (flags
& 0x50000) != 0;
89 SYNTAX_FLAGS_PREFIX (int flags
)
91 return (flags
>> 20) & 1;
94 SYNTAX_FLAGS_COMMENT_STYLEB (int flags
)
96 return (flags
>> 21) & 1;
99 SYNTAX_FLAGS_COMMENT_STYLEC (int flags
)
101 return (flags
>> 23) & 1;
104 SYNTAX_FLAGS_COMMENT_STYLEC2 (int flags
)
106 return (flags
>> 22) & 2; /* SYNTAX_FLAGS_COMMENT_STYLEC (flags) * 2 */
109 SYNTAX_FLAGS_COMMENT_NESTED (int flags
)
111 return (flags
>> 22) & 1;
114 /* FLAGS should be the flags of the main char of the comment marker, e.g.
115 the second for comstart and the first for comend. */
117 SYNTAX_FLAGS_COMMENT_STYLE (int flags
, int other_flags
)
119 return (SYNTAX_FLAGS_COMMENT_STYLEB (flags
)
120 | SYNTAX_FLAGS_COMMENT_STYLEC2 (flags
)
121 | SYNTAX_FLAGS_COMMENT_STYLEC2 (other_flags
));
124 /* Extract a particular flag for a given character. */
127 SYNTAX_COMEND_FIRST (int c
)
129 return SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c
));
132 /* We use these constants in place for comment-style and
133 string-ender-char to distinguish comments/strings started by
134 comment_fence and string_fence codes. */
138 ST_COMMENT_STYLE
= 256 + 1,
139 ST_STRING_STYLE
= 256 + 2
142 /* This is the internal form of the parse state used in parse-partial-sexp. */
144 struct lisp_parse_state
146 EMACS_INT depth
; /* Depth at end of parsing. */
147 int instring
; /* -1 if not within string, else desired terminator. */
148 EMACS_INT incomment
; /* -1 if in unnestable comment else comment nesting */
149 int comstyle
; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
150 bool quoted
; /* True if just after an escape char at end of parsing. */
151 EMACS_INT mindepth
; /* Minimum depth seen while scanning. */
152 /* Char number of most recent start-of-expression at current level */
153 ptrdiff_t thislevelstart
;
154 /* Char number of start of containing expression */
155 ptrdiff_t prevlevelstart
;
156 ptrdiff_t location
; /* Char number at which parsing stopped. */
157 ptrdiff_t location_byte
; /* Corresponding byte position. */
158 ptrdiff_t comstr_start
; /* Position of last comment/string starter. */
159 Lisp_Object levelstarts
; /* Char numbers of starts-of-expression
160 of levels (starting from outermost). */
161 int prev_syntax
; /* Syntax of previous position scanned, when
162 that position (potentially) holds the first char
163 of a 2-char construct, i.e. comment delimiter
164 or Sescape, etc. Smax otherwise. */
167 /* These variables are a cache for finding the start of a defun.
168 find_start_pos is the place for which the defun start was found.
169 find_start_value is the defun start position found for it.
170 find_start_value_byte is the corresponding byte position.
171 find_start_buffer is the buffer it was found in.
172 find_start_begv is the BEGV value when it was found.
173 find_start_modiff is the value of MODIFF when it was found. */
175 static ptrdiff_t find_start_pos
;
176 static ptrdiff_t find_start_value
;
177 static ptrdiff_t find_start_value_byte
;
178 static struct buffer
*find_start_buffer
;
179 static ptrdiff_t find_start_begv
;
180 static EMACS_INT find_start_modiff
;
183 static Lisp_Object
skip_chars (bool, Lisp_Object
, Lisp_Object
, bool);
184 static Lisp_Object
skip_syntaxes (bool, Lisp_Object
, Lisp_Object
);
185 static Lisp_Object
scan_lists (EMACS_INT
, EMACS_INT
, EMACS_INT
, bool);
186 static void scan_sexps_forward (struct lisp_parse_state
*,
187 ptrdiff_t, ptrdiff_t, ptrdiff_t, EMACS_INT
,
189 static void internalize_parse_state (Lisp_Object
, struct lisp_parse_state
*);
190 static bool in_classes (int, Lisp_Object
);
191 static void parse_sexp_propertize (ptrdiff_t charpos
);
193 /* This setter is used only in this file, so it can be private. */
195 bset_syntax_table (struct buffer
*b
, Lisp_Object val
)
197 b
->syntax_table_
= val
;
200 /* Whether the syntax of the character C has the prefix flag set. */
202 syntax_prefix_flag_p (int c
)
204 return SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c
));
207 struct gl_state_s gl_state
; /* Global state of syntax parser. */
209 enum { INTERVALS_AT_ONCE
= 10 }; /* 1 + max-number of intervals
210 to scan to property-change. */
212 /* Set the syntax entry VAL for char C in table TABLE. */
215 SET_RAW_SYNTAX_ENTRY (Lisp_Object table
, int c
, Lisp_Object val
)
217 CHAR_TABLE_SET (table
, c
, val
);
220 /* Set the syntax entry VAL for char-range RANGE in table TABLE.
221 RANGE is a cons (FROM . TO) specifying the range of characters. */
224 SET_RAW_SYNTAX_ENTRY_RANGE (Lisp_Object table
, Lisp_Object range
,
227 Fset_char_table_range (table
, range
, val
);
230 /* Extract the information from the entry for character C
231 in the current syntax table. */
236 Lisp_Object ent
= SYNTAX_ENTRY (c
);
237 return CONSP (ent
) ? XCDR (ent
) : Qnil
;
240 /* This should be called with FROM at the start of forward
241 search, or after the last position of the backward search. It
242 makes sure that the first char is picked up with correct table, so
243 one does not need to call UPDATE_SYNTAX_TABLE immediately after the
245 Sign of COUNT gives the direction of the search.
249 SETUP_SYNTAX_TABLE (ptrdiff_t from
, ptrdiff_t count
)
251 SETUP_BUFFER_SYNTAX_TABLE ();
252 gl_state
.b_property
= BEGV
;
253 gl_state
.e_property
= ZV
+ 1;
254 gl_state
.object
= Qnil
;
256 if (parse_sexp_lookup_properties
)
259 update_syntax_table_forward (from
, true, Qnil
);
260 else if (from
> BEGV
)
262 update_syntax_table (from
- 1, count
, true, Qnil
);
263 parse_sexp_propertize (from
- 1);
268 /* Same as above, but in OBJECT. If OBJECT is nil, use current buffer.
269 If it is t (which is only used in fast_c_string_match_ignore_case),
270 ignore properties altogether.
272 This is meant for regex.c to use. For buffers, regex.c passes arguments
273 to the UPDATE_SYNTAX_TABLE functions which are relative to BEGV.
274 So if it is a buffer, we set the offset field to BEGV. */
277 SETUP_SYNTAX_TABLE_FOR_OBJECT (Lisp_Object object
,
278 ptrdiff_t from
, ptrdiff_t count
)
280 SETUP_BUFFER_SYNTAX_TABLE ();
281 gl_state
.object
= object
;
282 if (BUFFERP (gl_state
.object
))
284 struct buffer
*buf
= XBUFFER (gl_state
.object
);
285 gl_state
.b_property
= 1;
286 gl_state
.e_property
= BUF_ZV (buf
) - BUF_BEGV (buf
) + 1;
287 gl_state
.offset
= BUF_BEGV (buf
) - 1;
289 else if (NILP (gl_state
.object
))
291 gl_state
.b_property
= 1;
292 gl_state
.e_property
= ZV
- BEGV
+ 1;
293 gl_state
.offset
= BEGV
- 1;
295 else if (EQ (gl_state
.object
, Qt
))
297 gl_state
.b_property
= 0;
298 gl_state
.e_property
= PTRDIFF_MAX
;
303 gl_state
.b_property
= 0;
304 gl_state
.e_property
= 1 + SCHARS (gl_state
.object
);
307 if (parse_sexp_lookup_properties
)
308 update_syntax_table (from
+ gl_state
.offset
- (count
<= 0),
309 count
, 1, gl_state
.object
);
312 /* Update gl_state to an appropriate interval which contains CHARPOS. The
313 sign of COUNT give the relative position of CHARPOS wrt the previously
314 valid interval. If INIT, only [be]_property fields of gl_state are
315 valid at start, the rest is filled basing on OBJECT.
317 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
318 direction than the intervals - or in an interval. We update the
319 current syntax-table basing on the property of this interval, and
320 update the interval to start further than CHARPOS - or be
321 NULL. We also update lim_property to be the next value of
322 charpos to call this subroutine again - or be before/after the
323 start/end of OBJECT. */
326 update_syntax_table (ptrdiff_t charpos
, EMACS_INT count
, bool init
,
329 Lisp_Object tmp_table
;
331 bool invalidate
= true;
336 gl_state
.old_prop
= Qnil
;
337 gl_state
.start
= gl_state
.b_property
;
338 gl_state
.stop
= gl_state
.e_property
;
339 i
= interval_of (charpos
, object
);
340 gl_state
.backward_i
= gl_state
.forward_i
= i
;
344 /* interval_of updates only ->position of the return value, so
345 update the parents manually to speed up update_interval. */
346 while (!NULL_PARENT (i
))
348 if (AM_RIGHT_CHILD (i
))
349 INTERVAL_PARENT (i
)->position
= i
->position
350 - LEFT_TOTAL_LENGTH (i
) + TOTAL_LENGTH (i
) /* right end */
351 - TOTAL_LENGTH (INTERVAL_PARENT (i
))
352 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i
));
354 INTERVAL_PARENT (i
)->position
= i
->position
- LEFT_TOTAL_LENGTH (i
)
356 i
= INTERVAL_PARENT (i
);
358 i
= gl_state
.forward_i
;
359 gl_state
.b_property
= i
->position
- gl_state
.offset
;
360 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
363 i
= count
> 0 ? gl_state
.forward_i
: gl_state
.backward_i
;
365 /* We are guaranteed to be called with CHARPOS either in i,
368 error ("Error in syntax_table logic for to-the-end intervals");
369 else if (charpos
< i
->position
) /* Move left. */
372 error ("Error in syntax_table logic for intervals <-");
373 /* Update the interval. */
374 i
= update_interval (i
, charpos
);
375 if (INTERVAL_LAST_POS (i
) != gl_state
.b_property
)
378 gl_state
.forward_i
= i
;
379 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
382 else if (charpos
>= INTERVAL_LAST_POS (i
)) /* Move right. */
385 error ("Error in syntax_table logic for intervals ->");
386 /* Update the interval. */
387 i
= update_interval (i
, charpos
);
388 if (i
->position
!= gl_state
.e_property
)
391 gl_state
.backward_i
= i
;
392 gl_state
.b_property
= i
->position
- gl_state
.offset
;
397 tmp_table
= textget (i
->plist
, Qsyntax_table
);
400 invalidate
= !EQ (tmp_table
, gl_state
.old_prop
); /* Need to invalidate? */
402 if (invalidate
) /* Did not get to adjacent interval. */
403 { /* with the same table => */
404 /* invalidate the old range. */
407 gl_state
.backward_i
= i
;
408 gl_state
.b_property
= i
->position
- gl_state
.offset
;
412 gl_state
.forward_i
= i
;
413 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
417 if (!EQ (tmp_table
, gl_state
.old_prop
))
419 gl_state
.current_syntax_table
= tmp_table
;
420 gl_state
.old_prop
= tmp_table
;
421 if (EQ (Fsyntax_table_p (tmp_table
), Qt
))
423 gl_state
.use_global
= 0;
425 else if (CONSP (tmp_table
))
427 gl_state
.use_global
= 1;
428 gl_state
.global_code
= tmp_table
;
432 gl_state
.use_global
= 0;
433 gl_state
.current_syntax_table
= BVAR (current_buffer
, syntax_table
);
439 if (cnt
&& !EQ (tmp_table
, textget (i
->plist
, Qsyntax_table
)))
443 gl_state
.e_property
= i
->position
- gl_state
.offset
;
444 gl_state
.forward_i
= i
;
449 = i
->position
+ LENGTH (i
) - gl_state
.offset
;
450 gl_state
.backward_i
= i
;
454 else if (cnt
== INTERVALS_AT_ONCE
)
459 = i
->position
+ LENGTH (i
) - gl_state
.offset
460 /* e_property at EOB is not set to ZV but to ZV+1, so that
461 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
462 having to check eob between the two. */
463 + (next_interval (i
) ? 0 : 1);
464 gl_state
.forward_i
= i
;
468 gl_state
.b_property
= i
->position
- gl_state
.offset
;
469 gl_state
.backward_i
= i
;
474 i
= count
> 0 ? next_interval (i
) : previous_interval (i
);
476 eassert (i
== NULL
); /* This property goes to the end. */
479 gl_state
.e_property
= gl_state
.stop
;
480 gl_state
.forward_i
= i
;
483 gl_state
.b_property
= gl_state
.start
;
487 parse_sexp_propertize (ptrdiff_t charpos
)
490 if (syntax_propertize__done
<= charpos
491 && syntax_propertize__done
< zv
)
493 EMACS_INT modiffs
= CHARS_MODIFF
;
494 safe_call1 (Qinternal__syntax_propertize
,
495 make_number (min (zv
, 1 + charpos
)));
496 if (modiffs
!= CHARS_MODIFF
)
497 error ("parse-sexp-propertize-function modified the buffer!");
498 if (syntax_propertize__done
<= charpos
499 && syntax_propertize__done
< zv
)
500 error ("parse-sexp-propertize-function did not move"
501 " syntax-propertize--done");
502 SETUP_SYNTAX_TABLE (charpos
, 1);
504 else if (gl_state
.e_property
> syntax_propertize__done
)
506 gl_state
.e_property
= syntax_propertize__done
;
507 gl_state
.e_property_truncated
= true;
509 else if (gl_state
.e_property_truncated
510 && gl_state
.e_property
< syntax_propertize__done
)
511 { /* When moving backward, e_property might be set without resetting
512 e_property_truncated, so the e_property_truncated flag may
513 occasionally be left raised spuriously. This should be rare. */
514 gl_state
.e_property_truncated
= false;
515 update_syntax_table_forward (charpos
, false, Qnil
);
520 update_syntax_table_forward (ptrdiff_t charpos
, bool init
,
523 if (gl_state
.e_property_truncated
)
525 eassert (NILP (object
));
526 eassert (charpos
>= gl_state
.e_property
);
527 parse_sexp_propertize (charpos
);
531 update_syntax_table (charpos
, 1, init
, object
);
532 if (NILP (object
) && gl_state
.e_property
> syntax_propertize__done
)
533 parse_sexp_propertize (charpos
);
537 /* Returns true if char at CHARPOS is quoted.
538 Global syntax-table data should be set up already to be good at CHARPOS
539 or after. On return global syntax data is good for lookup at CHARPOS. */
542 char_quoted (ptrdiff_t charpos
, ptrdiff_t bytepos
)
544 enum syntaxcode code
;
545 ptrdiff_t beg
= BEGV
;
547 ptrdiff_t orig
= charpos
;
549 while (charpos
> beg
)
552 DEC_BOTH (charpos
, bytepos
);
554 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
);
555 c
= FETCH_CHAR_AS_MULTIBYTE (bytepos
);
557 if (! (code
== Scharquote
|| code
== Sescape
))
563 UPDATE_SYNTAX_TABLE (orig
);
567 /* Return the bytepos one character before BYTEPOS.
568 We assume that BYTEPOS is not at the start of the buffer. */
571 dec_bytepos (ptrdiff_t bytepos
)
573 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
580 /* Return a defun-start position before POS and not too far before.
581 It should be the last one before POS, or nearly the last.
583 When open_paren_in_column_0_is_defun_start is nonzero,
584 only the beginning of the buffer is treated as a defun-start.
586 We record the information about where the scan started
587 and what its result was, so that another call in the same area
588 can return the same value very quickly.
590 There is no promise at which position the global syntax data is
591 valid on return from the subroutine, so the caller should explicitly
592 update the global data. */
595 find_defun_start (ptrdiff_t pos
, ptrdiff_t pos_byte
)
597 ptrdiff_t opoint
= PT
, opoint_byte
= PT_BYTE
;
599 /* Use previous finding, if it's valid and applies to this inquiry. */
600 if (current_buffer
== find_start_buffer
601 /* Reuse the defun-start even if POS is a little farther on.
602 POS might be in the next defun, but that's ok.
603 Our value may not be the best possible, but will still be usable. */
604 && pos
<= find_start_pos
+ 1000
605 && pos
>= find_start_value
606 && BEGV
== find_start_begv
607 && MODIFF
== find_start_modiff
)
608 return find_start_value
;
610 if (!open_paren_in_column_0_is_defun_start
)
612 find_start_value
= BEGV
;
613 find_start_value_byte
= BEGV_BYTE
;
617 /* Back up to start of line. */
618 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
620 /* We optimize syntax-table lookup for rare updates. Thus we accept
621 only those `^\s(' which are good in global _and_ text-property
623 SETUP_BUFFER_SYNTAX_TABLE ();
628 /* Open-paren at start of line means we may have found our
630 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
631 if (SYNTAX (c
) == Sopen
)
633 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
634 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
635 if (SYNTAX (c
) == Sopen
)
637 /* Now fallback to the default value. */
638 SETUP_BUFFER_SYNTAX_TABLE ();
640 /* Move to beg of previous line. */
641 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
644 /* Record what we found, for the next try. */
645 find_start_value
= PT
;
646 find_start_value_byte
= PT_BYTE
;
647 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
650 find_start_buffer
= current_buffer
;
651 find_start_modiff
= MODIFF
;
652 find_start_begv
= BEGV
;
653 find_start_pos
= pos
;
655 return find_start_value
;
658 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
661 prev_char_comend_first (ptrdiff_t pos
, ptrdiff_t pos_byte
)
666 DEC_BOTH (pos
, pos_byte
);
667 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
668 c
= FETCH_CHAR (pos_byte
);
669 val
= SYNTAX_COMEND_FIRST (c
);
670 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
674 /* Check whether charpos FROM is at the end of a comment.
675 FROM_BYTE is the bytepos corresponding to FROM.
676 Do not move back before STOP.
678 Return true if we find a comment ending at FROM/FROM_BYTE.
680 If successful, store the charpos of the comment's beginning
681 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
683 Global syntax data remains valid for backward search starting at
684 the returned value (or at FROM, if the search was not successful). */
687 back_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
,
688 bool comnested
, int comstyle
, ptrdiff_t *charpos_ptr
,
689 ptrdiff_t *bytepos_ptr
)
691 /* Look back, counting the parity of string-quotes,
692 and recording the comment-starters seen.
693 When we reach a safe place, assume that's not in a string;
694 then step the main scan to the earliest comment-starter seen
695 an even number of string quotes away from the safe place.
697 OFROM[I] is position of the earliest comment-starter seen
698 which is I+2X quotes from the comment-end.
699 PARITY is current parity of quotes from the comment end. */
700 int string_style
= -1; /* Presumed outside of any string. */
701 bool string_lossage
= 0;
702 /* Not a real lossage: indicates that we have passed a matching comment
703 starter plus a non-matching comment-ender, meaning that any matching
704 comment-starter we might see later could be a false positive (hidden
705 inside another comment).
706 Test case: { a (* b } c (* d *) */
707 bool comment_lossage
= 0;
708 ptrdiff_t comment_end
= from
;
709 ptrdiff_t comment_end_byte
= from_byte
;
710 ptrdiff_t comstart_pos
= 0;
711 ptrdiff_t comstart_byte
;
712 /* Place where the containing defun starts,
713 or 0 if we didn't come across it yet. */
714 ptrdiff_t defun_start
= 0;
715 ptrdiff_t defun_start_byte
= 0;
716 enum syntaxcode code
;
717 ptrdiff_t nesting
= 1; /* Current comment nesting. */
721 /* FIXME: A }} comment-ender style leads to incorrect behavior
722 in the case of {{ c }}} because we ignore the last two chars which are
723 assumed to be comment-enders although they aren't. */
725 /* At beginning of range to scan, we're outside of strings;
726 that determines quote parity to the comment-end. */
731 bool com2start
, com2end
, comstart
;
733 /* Move back and examine a character. */
734 DEC_BOTH (from
, from_byte
);
735 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
737 prev_syntax
= syntax
;
738 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
739 syntax
= SYNTAX_WITH_FLAGS (c
);
742 /* Check for 2-char comment markers. */
743 com2start
= (SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
744 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax
)
746 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax
, syntax
))
747 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
748 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
);
749 com2end
= (SYNTAX_FLAGS_COMEND_FIRST (syntax
)
750 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax
));
751 comstart
= (com2start
|| code
== Scomment
);
753 /* Nasty cases with overlapping 2-char comment markers:
754 - snmp-mode: -- c -- foo -- c --
762 /* If a 2-char comment sequence partly overlaps with another,
763 we don't try to be clever. E.g. |*| in C, or }% in modes that
764 have %..\n and %{..}%. */
765 if (from
> stop
&& (com2end
|| comstart
))
767 ptrdiff_t next
= from
, next_byte
= from_byte
;
768 int next_c
, next_syntax
;
769 DEC_BOTH (next
, next_byte
);
770 UPDATE_SYNTAX_TABLE_BACKWARD (next
);
771 next_c
= FETCH_CHAR_AS_MULTIBYTE (next_byte
);
772 next_syntax
= SYNTAX_WITH_FLAGS (next_c
);
773 if (((comstart
|| comnested
)
774 && SYNTAX_FLAGS_COMEND_SECOND (syntax
)
775 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax
))
776 || ((com2end
|| comnested
)
777 && SYNTAX_FLAGS_COMSTART_SECOND (syntax
)
779 == SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_syntax
))
780 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax
)))
782 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
785 if (com2start
&& comstart_pos
== 0)
786 /* We're looking at a comment starter. But it might be a comment
787 ender as well (see snmp-mode). The first time we see one, we
788 need to consider it as a comment starter,
789 and the subsequent times as a comment ender. */
792 /* Turn a 2-char comment sequences into the appropriate syntax. */
797 /* Ignore comment starters of a different style. */
798 else if (code
== Scomment
799 && (comstyle
!= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0)
800 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
) != comnested
))
803 /* Ignore escaped characters, except comment-enders which cannot
805 if ((Vcomment_end_can_be_escaped
|| code
!= Sendcomment
)
806 && char_quoted (from
, from_byte
))
813 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
815 /* Track parity of quotes. */
816 if (string_style
== -1)
817 /* Entering a string. */
819 else if (string_style
== c
)
820 /* Leaving the string. */
823 /* If we have two kinds of string delimiters.
824 There's no way to grok this scanning backwards. */
829 /* We've already checked that it is the relevant comstyle. */
830 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
831 /* There are odd string quotes involved, so let's be careful.
832 Test case in Pascal: " { " a { " } */
837 /* Record best comment-starter so far. */
839 comstart_byte
= from_byte
;
841 else if (--nesting
<= 0)
842 /* nested comments have to be balanced, so we don't need to
843 keep looking for earlier ones. We use here the same (slightly
844 incorrect) reasoning as below: since it is followed by uniform
845 paired string quotes, this comment-start has to be outside of
846 strings, else the comment-end itself would be inside a string. */
851 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == comstyle
852 && ((com2end
&& SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
))
853 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
854 /* This is the same style of comment ender as ours. */
859 /* Anything before that can't count because it would match
860 this comment-ender rather than ours. */
861 from
= stop
; /* Break out of the loop. */
863 else if (comstart_pos
!= 0 || c
!= '\n')
864 /* We're mixing comment styles here, so we'd better be careful.
865 The (comstart_pos != 0 || c != '\n') check is not quite correct
866 (we should just always set comment_lossage), but removing it
867 would imply that any multiline comment in C would go through
868 lossage, which seems overkill.
869 The failure should only happen in the rare cases such as
875 /* Assume a defun-start point is outside of strings. */
876 if (open_paren_in_column_0_is_defun_start
878 || (temp_byte
= dec_bytepos (from_byte
),
879 FETCH_CHAR (temp_byte
) == '\n')))
882 defun_start_byte
= from_byte
;
883 from
= stop
; /* Break out of the loop. */
892 if (comstart_pos
== 0)
895 from_byte
= comment_end_byte
;
896 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
);
898 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
899 or `done'), then we've found the beginning of the non-nested comment. */
900 else if (1) /* !comnested */
903 from_byte
= comstart_byte
;
904 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
908 struct lisp_parse_state state
;
909 bool adjusted
= true;
910 /* We had two kinds of string delimiters mixed up
911 together. Decode this going forwards.
912 Scan fwd from a known safe place (beginning-of-defun)
913 to the one in question; this records where we
914 last passed a comment starter. */
915 /* If we did not already find the defun start, find it now. */
916 if (defun_start
== 0)
918 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
919 defun_start_byte
= find_start_value_byte
;
920 adjusted
= (defun_start
> BEGV
);
924 internalize_parse_state (Qnil
, &state
);
925 scan_sexps_forward (&state
,
926 defun_start
, defun_start_byte
,
927 comment_end
, TYPE_MINIMUM (EMACS_INT
),
929 defun_start
= comment_end
;
934 = CONSP (state
.levelstarts
) ? XINT (XCAR (state
.levelstarts
))
935 : state
.thislevelstart
>= 0 ? state
.thislevelstart
937 find_start_value_byte
= CHAR_TO_BYTE (find_start_value
);
940 if (state
.incomment
== (comnested
? 1 : -1)
941 && state
.comstyle
== comstyle
)
942 from
= state
.comstr_start
;
947 /* If comment_end is inside some other comment, maybe ours
948 is nested, so we need to try again from within the
949 surrounding comment. Example: { a (* " *) */
951 /* FIXME: We should advance by one or two chars. */
952 defun_start
= state
.comstr_start
+ 2;
953 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
956 } while (defun_start
< comment_end
);
958 from_byte
= CHAR_TO_BYTE (from
);
959 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
964 *bytepos_ptr
= from_byte
;
966 return from
!= comment_end
;
969 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
970 doc
: /* Return t if OBJECT is a syntax table.
971 Currently, any char-table counts as a syntax table. */)
974 if (CHAR_TABLE_P (object
)
975 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
981 check_syntax_table (Lisp_Object obj
)
983 CHECK_TYPE (CHAR_TABLE_P (obj
) && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
),
984 Qsyntax_table_p
, obj
);
987 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
988 doc
: /* Return the current syntax table.
989 This is the one specified by the current buffer. */)
992 return BVAR (current_buffer
, syntax_table
);
995 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
996 Sstandard_syntax_table
, 0, 0, 0,
997 doc
: /* Return the standard syntax table.
998 This is the one used for new buffers. */)
1001 return Vstandard_syntax_table
;
1004 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
1005 doc
: /* Construct a new syntax table and return it.
1006 It is a copy of the TABLE, which defaults to the standard syntax table. */)
1012 check_syntax_table (table
);
1014 table
= Vstandard_syntax_table
;
1016 copy
= Fcopy_sequence (table
);
1018 /* Only the standard syntax table should have a default element.
1019 Other syntax tables should inherit from parents instead. */
1020 set_char_table_defalt (copy
, Qnil
);
1022 /* Copied syntax tables should all have parents.
1023 If we copied one with no parent, such as the standard syntax table,
1024 use the standard syntax table as the copy's parent. */
1025 if (NILP (XCHAR_TABLE (copy
)->parent
))
1026 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
1030 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
1031 doc
: /* Select a new syntax table for the current buffer.
1032 One argument, a syntax table. */)
1036 check_syntax_table (table
);
1037 bset_syntax_table (current_buffer
, table
);
1038 /* Indicate that this buffer now has a specified syntax table. */
1039 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
1040 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
1044 /* Convert a letter which signifies a syntax code
1045 into the code it signifies.
1046 This is used by modify-syntax-entry, and other things. */
1048 unsigned char const syntax_spec_code
[0400] =
1049 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1050 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1051 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1052 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1053 Swhitespace
, Scomment_fence
, Sstring
, 0377, Smath
, 0377, 0377, Squote
,
1054 Sopen
, Sclose
, 0377, 0377, 0377, Swhitespace
, Spunct
, Scharquote
,
1055 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1056 0377, 0377, 0377, 0377, Scomment
, 0377, Sendcomment
, 0377,
1057 Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
1058 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1059 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword
,
1060 0377, 0377, 0377, 0377, Sescape
, 0377, 0377, Ssymbol
,
1061 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
1062 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1063 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword
,
1064 0377, 0377, 0377, 0377, Sstring_fence
, 0377, 0377, 0377
1067 /* Indexed by syntax code, give the letter that describes it. */
1069 char const syntax_code_spec
[16] =
1071 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
1075 /* Indexed by syntax code, give the object (cons of syntax code and
1076 nil) to be stored in syntax table. Since these objects can be
1077 shared among syntax tables, we generate them in advance. By
1078 sharing objects, the function `describe-syntax' can give a more
1080 static Lisp_Object Vsyntax_code_object
;
1083 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
1084 doc
: /* Return the syntax code of CHARACTER, described by a character.
1085 For example, if CHARACTER is a word constituent, the
1086 character `w' (119) is returned.
1087 The characters that correspond to various syntax codes
1088 are listed in the documentation of `modify-syntax-entry'. */)
1089 (Lisp_Object character
)
1092 CHECK_CHARACTER (character
);
1093 char_int
= XINT (character
);
1094 SETUP_BUFFER_SYNTAX_TABLE ();
1095 return make_number (syntax_code_spec
[SYNTAX (char_int
)]);
1098 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
1099 doc
: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
1100 (Lisp_Object character
)
1103 enum syntaxcode code
;
1104 CHECK_CHARACTER (character
);
1105 char_int
= XINT (character
);
1106 SETUP_BUFFER_SYNTAX_TABLE ();
1107 code
= SYNTAX (char_int
);
1108 if (code
== Sopen
|| code
== Sclose
)
1109 return SYNTAX_MATCH (char_int
);
1113 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
1114 doc
: /* Convert a syntax descriptor STRING into a raw syntax descriptor.
1115 STRING should be a string of the form allowed as argument of
1116 `modify-syntax-entry'. The return value is a raw syntax descriptor: a
1117 cons cell (CODE . MATCHING-CHAR) which can be used, for example, as
1118 the value of a `syntax-table' text property. */)
1119 (Lisp_Object string
)
1121 const unsigned char *p
;
1125 CHECK_STRING (string
);
1128 val
= syntax_spec_code
[*p
++];
1130 error ("Invalid syntax description letter: %c", p
[-1]);
1132 if (val
== Sinherit
)
1138 int character
= STRING_CHAR_AND_LENGTH (p
, len
);
1139 XSETINT (match
, character
);
1140 if (XFASTINT (match
) == ' ')
1183 if (val
< ASIZE (Vsyntax_code_object
) && NILP (match
))
1184 return AREF (Vsyntax_code_object
, val
);
1186 /* Since we can't use a shared object, let's make a new one. */
1187 return Fcons (make_number (val
), match
);
1190 /* I really don't know why this is interactive
1191 help-form should at least be made useful whilst reading the second arg. */
1192 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
1193 "cSet syntax for character: \nsSet syntax for %s to: ",
1194 doc
: /* Set syntax for character CHAR according to string NEWENTRY.
1195 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1196 the current buffer's syntax table.
1197 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1198 in the range MIN to MAX are changed.
1199 The first character of NEWENTRY should be one of the following:
1200 Space or - whitespace syntax. w word constituent.
1201 _ symbol constituent. . punctuation.
1202 ( open-parenthesis. ) close-parenthesis.
1203 " string quote. \\ escape.
1204 $ paired delimiter. \\=' expression quote or prefix operator.
1205 < comment starter. > comment ender.
1206 / character-quote. @ inherit from parent table.
1207 | generic string fence. ! generic comment fence.
1209 Only single-character comment start and end sequences are represented thus.
1210 Two-character sequences are represented as described below.
1211 The second character of NEWENTRY is the matching parenthesis,
1212 used only if the first character is `(' or `)'.
1213 Any additional characters are flags.
1214 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1215 1 means CHAR is the start of a two-char comment start sequence.
1216 2 means CHAR is the second character of such a sequence.
1217 3 means CHAR is the start of a two-char comment end sequence.
1218 4 means CHAR is the second character of such a sequence.
1220 There can be several orthogonal comment sequences. This is to support
1221 language modes such as C++. By default, all comment sequences are of style
1222 a, but you can set the comment sequence style to b (on the second character
1223 of a comment-start, and the first character of a comment-end sequence) and/or
1224 c (on any of its chars) using this flag:
1225 b means CHAR is part of comment sequence b.
1226 c means CHAR is part of comment sequence c.
1227 n means CHAR is part of a nestable comment sequence.
1229 p means CHAR is a prefix character for `backward-prefix-chars';
1230 such characters are treated as whitespace when they occur
1231 between expressions.
1232 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1233 (Lisp_Object c
, Lisp_Object newentry
, Lisp_Object syntax_table
)
1237 CHECK_CHARACTER_CAR (c
);
1238 CHECK_CHARACTER_CDR (c
);
1241 CHECK_CHARACTER (c
);
1243 if (NILP (syntax_table
))
1244 syntax_table
= BVAR (current_buffer
, syntax_table
);
1246 check_syntax_table (syntax_table
);
1248 newentry
= Fstring_to_syntax (newentry
);
1250 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table
, c
, newentry
);
1252 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), newentry
);
1254 /* We clear the regexp cache, since character classes can now have
1255 different values from those in the compiled regexps.*/
1256 clear_regexp_cache ();
1261 /* Dump syntax table to buffer in human-readable format */
1263 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value
,
1264 Sinternal_describe_syntax_value
, 1, 1, 0,
1265 doc
: /* Insert a description of the internal syntax description SYNTAX at point. */)
1266 (Lisp_Object syntax
)
1268 int code
, syntax_code
;
1269 bool start1
, start2
, end1
, end2
, prefix
, comstyleb
, comstylec
, comnested
;
1271 Lisp_Object first
, match_lisp
, value
= syntax
;
1275 insert_string ("default");
1279 if (CHAR_TABLE_P (value
))
1281 insert_string ("deeper char-table ...");
1287 insert_string ("invalid");
1291 first
= XCAR (value
);
1292 match_lisp
= XCDR (value
);
1294 if (!INTEGERP (first
) || !(NILP (match_lisp
) || CHARACTERP (match_lisp
)))
1296 insert_string ("invalid");
1300 syntax_code
= XINT (first
) & INT_MAX
;
1301 code
= syntax_code
& 0377;
1302 start1
= SYNTAX_FLAGS_COMSTART_FIRST (syntax_code
);
1303 start2
= SYNTAX_FLAGS_COMSTART_SECOND (syntax_code
);
1304 end1
= SYNTAX_FLAGS_COMEND_FIRST (syntax_code
);
1305 end2
= SYNTAX_FLAGS_COMEND_SECOND (syntax_code
);
1306 prefix
= SYNTAX_FLAGS_PREFIX (syntax_code
);
1307 comstyleb
= SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code
);
1308 comstylec
= SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code
);
1309 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax_code
);
1313 insert_string ("invalid");
1317 str
[0] = syntax_code_spec
[code
], str
[1] = 0;
1320 if (NILP (match_lisp
))
1323 insert_char (XINT (match_lisp
));
1344 insert_string ("\twhich means: ");
1349 insert_string ("whitespace"); break;
1351 insert_string ("punctuation"); break;
1353 insert_string ("word"); break;
1355 insert_string ("symbol"); break;
1357 insert_string ("open"); break;
1359 insert_string ("close"); break;
1361 insert_string ("prefix"); break;
1363 insert_string ("string"); break;
1365 insert_string ("math"); break;
1367 insert_string ("escape"); break;
1369 insert_string ("charquote"); break;
1371 insert_string ("comment"); break;
1373 insert_string ("endcomment"); break;
1375 insert_string ("inherit"); break;
1376 case Scomment_fence
:
1377 insert_string ("comment fence"); break;
1379 insert_string ("string fence"); break;
1381 insert_string ("invalid");
1385 if (!NILP (match_lisp
))
1387 insert_string (", matches ");
1388 insert_char (XINT (match_lisp
));
1392 insert_string (",\n\t is the first character of a comment-start sequence");
1394 insert_string (",\n\t is the second character of a comment-start sequence");
1397 insert_string (",\n\t is the first character of a comment-end sequence");
1399 insert_string (",\n\t is the second character of a comment-end sequence");
1401 insert_string (" (comment style b)");
1403 insert_string (" (comment style c)");
1405 insert_string (" (nestable)");
1409 AUTO_STRING (prefixdoc
,
1410 ",\n\t is a prefix character for `backward-prefix-chars'");
1411 insert1 (Fsubstitute_command_keys (prefixdoc
));
1417 /* Return the position across COUNT words from FROM.
1418 If that many words cannot be found before the end of the buffer, return 0.
1419 COUNT negative means scan backward and stop at word beginning. */
1422 scan_words (register ptrdiff_t from
, register EMACS_INT count
)
1424 register ptrdiff_t beg
= BEGV
;
1425 register ptrdiff_t end
= ZV
;
1426 register ptrdiff_t from_byte
= CHAR_TO_BYTE (from
);
1427 register enum syntaxcode code
;
1429 Lisp_Object func
, pos
;
1434 SETUP_SYNTAX_TABLE (from
, count
);
1445 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1446 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1447 code
= SYNTAX (ch0
);
1448 INC_BOTH (from
, from_byte
);
1449 if (words_include_escapes
1450 && (code
== Sescape
|| code
== Scharquote
))
1455 /* Now CH0 is a character which begins a word and FROM is the
1456 position of the next character. */
1457 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch0
);
1458 if (! NILP (Ffboundp (func
)))
1460 pos
= call2 (func
, make_number (from
- 1), make_number (end
));
1461 if (INTEGERP (pos
) && from
< XINT (pos
) && XINT (pos
) <= ZV
)
1464 from_byte
= CHAR_TO_BYTE (from
);
1471 if (from
== end
) break;
1472 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1473 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1474 code
= SYNTAX (ch1
);
1476 && (! words_include_escapes
1477 || (code
!= Sescape
&& code
!= Scharquote
)))
1478 || word_boundary_p (ch0
, ch1
))
1480 INC_BOTH (from
, from_byte
);
1495 DEC_BOTH (from
, from_byte
);
1496 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1497 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1498 code
= SYNTAX (ch1
);
1499 if (words_include_escapes
1500 && (code
== Sescape
|| code
== Scharquote
))
1505 /* Now CH1 is a character which ends a word and FROM is the
1507 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch1
);
1508 if (! NILP (Ffboundp (func
)))
1510 pos
= call2 (func
, make_number (from
), make_number (beg
));
1511 if (INTEGERP (pos
) && BEGV
<= XINT (pos
) && XINT (pos
) < from
)
1514 from_byte
= CHAR_TO_BYTE (from
);
1523 DEC_BOTH (from
, from_byte
);
1524 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1525 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1526 code
= SYNTAX (ch0
);
1528 && (! words_include_escapes
1529 || (code
!= Sescape
&& code
!= Scharquote
)))
1530 || word_boundary_p (ch0
, ch1
))
1532 INC_BOTH (from
, from_byte
);
1546 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 0, 1, "^p",
1547 doc
: /* Move point forward ARG words (backward if ARG is negative).
1548 If ARG is omitted or nil, move point forward one word.
1550 If an edge of the buffer or a field boundary is reached, point is
1551 left there and the function returns nil. Field boundaries are not
1552 noticed if `inhibit-field-text-motion' is non-nil.
1554 The word boundaries are normally determined by the buffer's syntax
1555 table, but `find-word-boundary-function-table', such as set up
1556 by `subword-mode', can change that. If a Lisp program needs to
1557 move by words determined strictly by the syntax table, it should
1558 use `forward-word-strictly' instead. */)
1562 ptrdiff_t orig_val
, val
;
1565 XSETFASTINT (arg
, 1);
1569 val
= orig_val
= scan_words (PT
, XINT (arg
));
1571 val
= XINT (arg
) > 0 ? ZV
: BEGV
;
1573 /* Avoid jumping out of an input field. */
1574 tmp
= Fconstrain_to_field (make_number (val
), make_number (PT
),
1576 val
= XFASTINT (tmp
);
1579 return val
== orig_val
? Qt
: Qnil
;
1582 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1583 doc
: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1584 STRING is like the inside of a `[...]' in a regular expression
1585 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1586 (but not at the end of a range; quoting is never needed there).
1587 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1588 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1589 Char classes, e.g. `[:alpha:]', are supported.
1591 Returns the distance traveled, either zero or positive. */)
1592 (Lisp_Object string
, Lisp_Object lim
)
1594 return skip_chars (1, string
, lim
, 1);
1597 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1598 doc
: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1599 See `skip-chars-forward' for details.
1600 Returns the distance traveled, either zero or negative. */)
1601 (Lisp_Object string
, Lisp_Object lim
)
1603 return skip_chars (0, string
, lim
, 1);
1606 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1607 doc
: /* Move point forward across chars in specified syntax classes.
1608 SYNTAX is a string of syntax code characters.
1609 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1610 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1611 This function returns the distance traveled, either zero or positive. */)
1612 (Lisp_Object syntax
, Lisp_Object lim
)
1614 return skip_syntaxes (1, syntax
, lim
);
1617 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1618 doc
: /* Move point backward across chars in specified syntax classes.
1619 SYNTAX is a string of syntax code characters.
1620 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1621 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1622 This function returns either zero or a negative number, and the absolute value
1623 of this is the distance traveled. */)
1624 (Lisp_Object syntax
, Lisp_Object lim
)
1626 return skip_syntaxes (0, syntax
, lim
);
1630 skip_chars (bool forwardp
, Lisp_Object string
, Lisp_Object lim
,
1631 bool handle_iso_classes
)
1635 /* Store the ranges of non-ASCII characters. */
1636 int *char_ranges UNINIT
;
1637 int n_char_ranges
= 0;
1639 ptrdiff_t i
, i_byte
;
1640 /* True if the current buffer is multibyte and the region contains
1643 /* True if STRING is multibyte and it contains non-ASCII chars. */
1644 bool string_multibyte
;
1645 ptrdiff_t size_byte
;
1646 const unsigned char *str
;
1648 Lisp_Object iso_classes
;
1651 CHECK_STRING (string
);
1655 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1657 CHECK_NUMBER_COERCE_MARKER (lim
);
1659 /* In any case, don't allow scan outside bounds of buffer. */
1660 if (XINT (lim
) > ZV
)
1661 XSETFASTINT (lim
, ZV
);
1662 if (XINT (lim
) < BEGV
)
1663 XSETFASTINT (lim
, BEGV
);
1665 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1666 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1667 string_multibyte
= SBYTES (string
) > SCHARS (string
);
1669 memset (fastmap
, 0, sizeof fastmap
);
1671 str
= SDATA (string
);
1672 size_byte
= SBYTES (string
);
1675 if (i_byte
< size_byte
1676 && SREF (string
, 0) == '^')
1678 negate
= 1; i_byte
++;
1681 /* Find the characters specified and set their elements of fastmap.
1682 Handle backslashes and ranges specially.
1684 If STRING contains non-ASCII characters, setup char_ranges for
1685 them and use fastmap only for their leading codes. */
1687 if (! string_multibyte
)
1689 bool string_has_eight_bit
= 0;
1691 /* At first setup fastmap. */
1692 while (i_byte
< size_byte
)
1694 if (handle_iso_classes
)
1696 const unsigned char *ch
= str
+ i_byte
;
1697 re_wctype_t cc
= re_wctype_parse (&ch
, size_byte
- i_byte
);
1699 error ("Invalid ISO C character class");
1702 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1712 if (i_byte
== size_byte
)
1717 /* Treat `-' as range character only if another character
1719 if (i_byte
+ 1 < size_byte
1720 && str
[i_byte
] == '-')
1724 /* Skip over the dash. */
1727 /* Get the end of the range. */
1730 && i_byte
< size_byte
)
1738 if (! ASCII_CHAR_P (c2
))
1739 string_has_eight_bit
= 1;
1745 if (! ASCII_CHAR_P (c
))
1746 string_has_eight_bit
= 1;
1750 /* If the current range is multibyte and STRING contains
1751 eight-bit chars, arrange fastmap and setup char_ranges for
1752 the corresponding multibyte chars. */
1753 if (multibyte
&& string_has_eight_bit
)
1756 char himap
[0200 + 1];
1757 memcpy (himap
, fastmap
+ 0200, 0200);
1759 memset (fastmap
+ 0200, 0, 0200);
1760 SAFE_NALLOCA (char_ranges
, 2, 128);
1763 while ((p1
= memchr (himap
+ i
, 1, 0200 - i
)))
1765 /* Deduce the next range C..C2 from the next clump of 1s
1766 in HIMAP starting with &HIMAP[I]. HIMAP is the high
1767 order half of the old FASTMAP. */
1768 int c2
, leading_code
;
1770 c
= BYTE8_TO_CHAR (i
+ 0200);
1772 c2
= BYTE8_TO_CHAR (i
+ 0200 - 1);
1774 char_ranges
[n_char_ranges
++] = c
;
1775 char_ranges
[n_char_ranges
++] = c2
;
1776 leading_code
= CHAR_LEADING_CODE (c
);
1777 memset (fastmap
+ leading_code
, 1,
1778 CHAR_LEADING_CODE (c2
) - leading_code
+ 1);
1782 else /* STRING is multibyte */
1784 SAFE_NALLOCA (char_ranges
, 2, SCHARS (string
));
1786 while (i_byte
< size_byte
)
1788 int leading_code
= str
[i_byte
];
1790 if (handle_iso_classes
)
1792 const unsigned char *ch
= str
+ i_byte
;
1793 re_wctype_t cc
= re_wctype_parse (&ch
, size_byte
- i_byte
);
1795 error ("Invalid ISO C character class");
1798 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1804 if (leading_code
== '\\')
1806 if (++i_byte
== size_byte
)
1809 leading_code
= str
[i_byte
];
1811 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1815 /* Treat `-' as range character only if another character
1817 if (i_byte
+ 1 < size_byte
1818 && str
[i_byte
] == '-')
1820 int c2
, leading_code2
;
1822 /* Skip over the dash. */
1825 /* Get the end of the range. */
1826 leading_code2
= str
[i_byte
];
1827 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1831 && i_byte
< size_byte
)
1833 leading_code2
= str
[i_byte
];
1834 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1840 if (ASCII_CHAR_P (c
))
1842 while (c
<= c2
&& c
< 0x80)
1844 leading_code
= CHAR_LEADING_CODE (c
);
1846 if (! ASCII_CHAR_P (c
))
1848 int lim2
= leading_code2
+ 1;
1849 while (leading_code
< lim2
)
1850 fastmap
[leading_code
++] = 1;
1853 char_ranges
[n_char_ranges
++] = c
;
1854 char_ranges
[n_char_ranges
++] = c2
;
1860 if (ASCII_CHAR_P (c
))
1864 fastmap
[leading_code
] = 1;
1865 char_ranges
[n_char_ranges
++] = c
;
1866 char_ranges
[n_char_ranges
++] = c
;
1871 /* If the current range is unibyte and STRING contains non-ASCII
1872 chars, arrange fastmap for the corresponding unibyte
1875 if (! multibyte
&& n_char_ranges
> 0)
1877 memset (fastmap
+ 0200, 0, 0200);
1878 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1880 int c1
= char_ranges
[i
];
1881 int lim2
= char_ranges
[i
+ 1] + 1;
1883 for (; c1
< lim2
; c1
++)
1885 int b
= CHAR_TO_BYTE_SAFE (c1
);
1893 /* If ^ was the first character, complement the fastmap. */
1897 for (i
= 0; i
< sizeof fastmap
; i
++)
1901 for (i
= 0; i
< 0200; i
++)
1903 /* All non-ASCII chars possibly match. */
1904 for (; i
< sizeof fastmap
; i
++)
1910 ptrdiff_t start_point
= PT
;
1912 ptrdiff_t pos_byte
= PT_BYTE
;
1913 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1917 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1918 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1922 endp
= CHAR_POS_ADDR (XINT (lim
));
1923 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1927 /* This code may look up syntax tables using functions that rely on the
1928 gl_state object. To make sure this object is not out of date,
1929 let's initialize it manually.
1930 We ignore syntax-table text-properties for now, since that's
1931 what we've done in the past. */
1932 SETUP_BUFFER_SYNTAX_TABLE ();
1947 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
1948 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1958 if (! ASCII_CHAR_P (c
))
1960 /* As we are looking at a multibyte character, we
1961 must look up the character in the table
1962 CHAR_RANGES. If there's no data in the table,
1963 that character is not what we want to skip. */
1965 /* The following code do the right thing even if
1966 n_char_ranges is zero (i.e. no data in
1968 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1969 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
1971 if (!(negate
^ (i
< n_char_ranges
)))
1975 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
1988 if (!NILP (iso_classes
) && in_classes (*p
, iso_classes
))
1993 goto fwd_unibyte_ok
;
1999 p
++, pos
++, pos_byte
++;
2007 unsigned char *prev_p
;
2017 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2018 c
= STRING_CHAR (p
);
2020 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
2030 if (! ASCII_CHAR_P (c
))
2032 /* See the comment in the previous similar code. */
2033 for (i
= 0; i
< n_char_ranges
; i
+= 2)
2034 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
2036 if (!(negate
^ (i
< n_char_ranges
)))
2040 pos
--, pos_byte
-= prev_p
- p
;
2053 if (! NILP (iso_classes
) && in_classes (p
[-1], iso_classes
))
2058 goto back_unibyte_ok
;
2061 if (!fastmap
[p
[-1]])
2064 p
--, pos
--, pos_byte
--;
2068 SET_PT_BOTH (pos
, pos_byte
);
2072 return make_number (PT
- start_point
);
2078 skip_syntaxes (bool forwardp
, Lisp_Object string
, Lisp_Object lim
)
2081 unsigned char fastmap
[0400];
2083 ptrdiff_t i
, i_byte
;
2085 ptrdiff_t size_byte
;
2088 CHECK_STRING (string
);
2091 XSETINT (lim
, forwardp
? ZV
: BEGV
);
2093 CHECK_NUMBER_COERCE_MARKER (lim
);
2095 /* In any case, don't allow scan outside bounds of buffer. */
2096 if (XINT (lim
) > ZV
)
2097 XSETFASTINT (lim
, ZV
);
2098 if (XINT (lim
) < BEGV
)
2099 XSETFASTINT (lim
, BEGV
);
2101 if (forwardp
? (PT
>= XFASTINT (lim
)) : (PT
<= XFASTINT (lim
)))
2102 return make_number (0);
2104 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
2105 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
2107 memset (fastmap
, 0, sizeof fastmap
);
2109 if (SBYTES (string
) > SCHARS (string
))
2110 /* As this is very rare case (syntax spec is ASCII only), don't
2111 consider efficiency. */
2112 string
= string_make_unibyte (string
);
2114 str
= SDATA (string
);
2115 size_byte
= SBYTES (string
);
2118 if (i_byte
< size_byte
2119 && SREF (string
, 0) == '^')
2121 negate
= 1; i_byte
++;
2124 /* Find the syntaxes specified and set their elements of fastmap. */
2126 while (i_byte
< size_byte
)
2129 fastmap
[syntax_spec_code
[c
]] = 1;
2132 /* If ^ was the first character, complement the fastmap. */
2134 for (i
= 0; i
< sizeof fastmap
; i
++)
2138 ptrdiff_t start_point
= PT
;
2140 ptrdiff_t pos_byte
= PT_BYTE
;
2141 unsigned char *p
, *endp
, *stop
;
2144 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
2150 p
= BYTE_POS_ADDR (pos_byte
);
2151 endp
= XINT (lim
) == GPT
? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
2152 stop
= pos
< GPT
&& GPT
< XINT (lim
) ? GPT_ADDR
: endp
;
2166 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
2169 if (! fastmap
[SYNTAX (c
)])
2171 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2173 while (!parse_sexp_lookup_properties
2174 || pos
< gl_state
.e_property
);
2176 update_syntax_table_forward (pos
+ gl_state
.offset
,
2177 false, gl_state
.object
);
2182 p
= BYTE_POS_ADDR (pos_byte
);
2183 endp
= CHAR_POS_ADDR (XINT (lim
));
2184 stop
= pos
>= GPT
&& GPT
> XINT (lim
) ? GAP_END_ADDR
: endp
;
2190 unsigned char *prev_p
;
2199 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2201 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2202 c
= STRING_CHAR (p
);
2203 if (! fastmap
[SYNTAX (c
)])
2205 pos
--, pos_byte
-= prev_p
- p
;
2219 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2220 if (! fastmap
[SYNTAX (p
[-1])])
2222 p
--, pos
--, pos_byte
--;
2228 SET_PT_BOTH (pos
, pos_byte
);
2231 return make_number (PT
- start_point
);
2235 /* Return true if character C belongs to one of the ISO classes
2236 in the list ISO_CLASSES. Each class is represented by an
2237 integer which is its type according to re_wctype. */
2240 in_classes (int c
, Lisp_Object iso_classes
)
2242 bool fits_class
= 0;
2244 while (CONSP (iso_classes
))
2247 elt
= XCAR (iso_classes
);
2248 iso_classes
= XCDR (iso_classes
);
2250 if (re_iswctype (c
, XFASTINT (elt
)))
2257 /* Jump over a comment, assuming we are at the beginning of one.
2258 FROM is the current position.
2259 FROM_BYTE is the bytepos corresponding to FROM.
2260 Do not move past STOP (a charpos).
2261 The comment over which we have to jump is of style STYLE
2262 (either SYNTAX_FLAGS_COMMENT_STYLE (foo) or ST_COMMENT_STYLE).
2263 NESTING should be positive to indicate the nesting at the beginning
2264 for nested comments and should be zero or negative else.
2265 ST_COMMENT_STYLE cannot be nested.
2266 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2267 (or 0 If the search cannot start in the middle of a two-character).
2269 If successful, return true and store the charpos of the comment's
2270 end into *CHARPOS_PTR and the corresponding bytepos into
2271 *BYTEPOS_PTR. Else, return false and store the charpos STOP into
2272 *CHARPOS_PTR, the corresponding bytepos into *BYTEPOS_PTR and the
2273 current nesting (as defined for state->incomment) in
2274 *INCOMMENT_PTR. Should the last character scanned in an incomplete
2275 comment be a possible first character of a two character construct,
2276 we store its SYNTAX_WITH_FLAGS into *last_syntax_ptr. Otherwise,
2277 we store Smax into *last_syntax_ptr.
2279 The comment end is the last character of the comment rather than the
2280 character just after the comment.
2282 Global syntax data is assumed to initially be valid for FROM and
2283 remains valid for forward search starting at the returned position. */
2286 forw_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
,
2287 EMACS_INT nesting
, int style
, int prev_syntax
,
2288 ptrdiff_t *charpos_ptr
, ptrdiff_t *bytepos_ptr
,
2289 EMACS_INT
*incomment_ptr
, int *last_syntax_ptr
)
2292 register enum syntaxcode code
;
2293 register int syntax
, other_syntax
;
2295 if (nesting
<= 0) nesting
= -1;
2297 /* Enter the loop in the middle so that we find
2298 a 2-char comment ender if we start in the middle of it. */
2299 syntax
= prev_syntax
;
2300 code
= syntax
& 0xff;
2301 if (syntax
!= 0 && from
< stop
) goto forw_incomment
;
2307 *incomment_ptr
= nesting
;
2308 *charpos_ptr
= from
;
2309 *bytepos_ptr
= from_byte
;
2311 (code
== Sescape
|| code
== Scharquote
2312 || SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2314 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)))
2318 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2319 syntax
= SYNTAX_WITH_FLAGS (c
);
2320 code
= syntax
& 0xff;
2321 if (code
== Sendcomment
2322 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
2323 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
2324 (nesting
> 0 && --nesting
== 0) : nesting
< 0)
2325 && !(Vcomment_end_can_be_escaped
&& char_quoted (from
, from_byte
)))
2326 /* We have encountered a comment end of the same style
2327 as the comment sequence which began this comment
2330 if (code
== Scomment_fence
2331 && style
== ST_COMMENT_STYLE
)
2332 /* We have encountered a comment end of the same style
2333 as the comment sequence which began this comment
2338 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
2339 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
)
2340 /* We have encountered a nested comment of the same style
2341 as the comment sequence which began this comment section. */
2343 INC_BOTH (from
, from_byte
);
2344 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2347 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2348 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2349 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2350 SYNTAX_FLAGS_COMEND_SECOND (other_syntax
))
2351 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, other_syntax
) == style
2352 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2353 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
))
2354 ? nesting
> 0 : nesting
< 0))
2356 syntax
= Smax
; /* So that "|#" (lisp) can not return
2357 the syntax of "#" in *last_syntax_ptr. */
2359 /* We have encountered a comment end of the same style
2360 as the comment sequence which began this comment section. */
2364 INC_BOTH (from
, from_byte
);
2365 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2370 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
2371 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2372 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2373 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
) == style
2374 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2375 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2376 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
)))
2377 /* We have encountered a nested comment of the same style
2378 as the comment sequence which began this comment section. */
2380 syntax
= Smax
; /* So that "#|#" isn't also a comment ender. */
2381 INC_BOTH (from
, from_byte
);
2382 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2386 *charpos_ptr
= from
;
2387 *bytepos_ptr
= from_byte
;
2388 *last_syntax_ptr
= Smax
; /* Any syntactic power the last byte had is
2393 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
2395 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2396 Stop scanning if we find something other than a comment or whitespace.
2397 Set point to where scanning stops.
2398 If COUNT comments are found as expected, with nothing except whitespace
2399 between them, return t; otherwise return nil. */)
2402 ptrdiff_t from
, from_byte
, stop
;
2404 enum syntaxcode code
;
2405 int comstyle
= 0; /* style of comment encountered */
2406 bool comnested
= 0; /* whether the comment is nestable or not */
2409 ptrdiff_t out_charpos
, out_bytepos
;
2413 CHECK_NUMBER (count
);
2414 count1
= XINT (count
);
2415 stop
= count1
> 0 ? ZV
: BEGV
;
2421 from_byte
= PT_BYTE
;
2423 SETUP_SYNTAX_TABLE (from
, count1
);
2428 bool comstart_first
;
2429 int syntax
, other_syntax
;
2433 SET_PT_BOTH (from
, from_byte
);
2437 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2438 syntax
= SYNTAX_WITH_FLAGS (c
);
2440 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2441 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2442 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2443 INC_BOTH (from
, from_byte
);
2444 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2445 if (from
< stop
&& comstart_first
2446 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2447 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2448 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
)))
2450 /* We have encountered a comment start sequence and we
2451 are ignoring all text inside comments. We must record
2452 the comment style this sequence begins so that later,
2453 only a comment end of the same style actually ends
2454 the comment section. */
2456 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2457 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2458 INC_BOTH (from
, from_byte
);
2459 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2462 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
2464 if (code
== Scomment_fence
)
2465 comstyle
= ST_COMMENT_STYLE
;
2466 else if (code
!= Scomment
)
2469 DEC_BOTH (from
, from_byte
);
2470 SET_PT_BOTH (from
, from_byte
);
2473 /* We're at the start of a comment. */
2474 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
2475 &out_charpos
, &out_bytepos
, &dummy
, &dummy2
);
2476 from
= out_charpos
; from_byte
= out_bytepos
;
2480 SET_PT_BOTH (from
, from_byte
);
2483 INC_BOTH (from
, from_byte
);
2484 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2485 /* We have skipped one comment. */
2498 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
2503 DEC_BOTH (from
, from_byte
);
2504 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2505 quoted
= char_quoted (from
, from_byte
);
2506 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2507 syntax
= SYNTAX_WITH_FLAGS (c
);
2510 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2511 if (code
== Sendcomment
)
2512 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2513 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2514 && prev_char_comend_first (from
, from_byte
)
2515 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
2518 /* We must record the comment style encountered so that
2519 later, we can match only the proper comment begin
2520 sequence of the same style. */
2521 DEC_BOTH (from
, from_byte
);
2523 /* Calling char_quoted, above, set up global syntax position
2524 at the new value of FROM. */
2525 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2526 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2527 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2528 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2531 if (code
== Scomment_fence
)
2533 /* Skip until first preceding unquoted comment_fence. */
2534 bool fence_found
= 0;
2535 ptrdiff_t ini
= from
, ini_byte
= from_byte
;
2539 DEC_BOTH (from
, from_byte
);
2540 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2541 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2542 if (SYNTAX (c
) == Scomment_fence
2543 && !char_quoted (from
, from_byte
))
2548 else if (from
== stop
)
2551 if (fence_found
== 0)
2553 from
= ini
; /* Set point to ini + 1. */
2554 from_byte
= ini_byte
;
2558 /* We have skipped one comment. */
2561 else if (code
== Sendcomment
)
2563 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2564 &out_charpos
, &out_bytepos
);
2568 /* This end-of-line is not an end-of-comment.
2569 Treat it like a whitespace.
2570 CC-mode (and maybe others) relies on this behavior. */
2574 /* Failure: we should go back to the end of this
2575 not-quite-endcomment. */
2576 if (SYNTAX (c
) != code
)
2577 /* It was a two-char Sendcomment. */
2578 INC_BOTH (from
, from_byte
);
2584 /* We have skipped one comment. */
2585 from
= out_charpos
, from_byte
= out_bytepos
;
2589 else if (code
!= Swhitespace
|| quoted
)
2593 INC_BOTH (from
, from_byte
);
2594 SET_PT_BOTH (from
, from_byte
);
2602 SET_PT_BOTH (from
, from_byte
);
2607 /* Return syntax code of character C if C is an ASCII character
2608 or if MULTIBYTE_SYMBOL_P is false. Otherwise, return Ssymbol. */
2610 static enum syntaxcode
2611 syntax_multibyte (int c
, bool multibyte_symbol_p
)
2613 return ASCII_CHAR_P (c
) || !multibyte_symbol_p
? SYNTAX (c
) : Ssymbol
;
2617 scan_lists (EMACS_INT from
, EMACS_INT count
, EMACS_INT depth
, bool sexpflag
)
2620 ptrdiff_t stop
= count
> 0 ? ZV
: BEGV
;
2625 enum syntaxcode code
;
2626 EMACS_INT min_depth
= depth
; /* Err out if depth gets less than this. */
2627 int comstyle
= 0; /* Style of comment encountered. */
2628 bool comnested
= 0; /* Whether the comment is nestable or not. */
2630 EMACS_INT last_good
= from
;
2632 ptrdiff_t from_byte
;
2633 ptrdiff_t out_bytepos
, out_charpos
;
2636 bool multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2638 if (depth
> 0) min_depth
= 0;
2640 if (from
> ZV
) from
= ZV
;
2641 if (from
< BEGV
) from
= BEGV
;
2643 from_byte
= CHAR_TO_BYTE (from
);
2648 SETUP_SYNTAX_TABLE (from
, count
);
2653 bool comstart_first
, prefix
;
2654 int syntax
, other_syntax
;
2655 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2656 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2657 syntax
= SYNTAX_WITH_FLAGS (c
);
2658 code
= syntax_multibyte (c
, multibyte_symbol_p
);
2659 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2660 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2661 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2662 prefix
= SYNTAX_FLAGS_PREFIX (syntax
);
2663 if (depth
== min_depth
)
2665 INC_BOTH (from
, from_byte
);
2666 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2667 if (from
< stop
&& comstart_first
2668 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2669 other_syntax
= SYNTAX_WITH_FLAGS (c
),
2670 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2671 && parse_sexp_ignore_comments
)
2673 /* We have encountered a comment start sequence and we
2674 are ignoring all text inside comments. We must record
2675 the comment style this sequence begins so that later,
2676 only a comment end of the same style actually ends
2677 the comment section. */
2679 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2680 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2681 INC_BOTH (from
, from_byte
);
2682 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2694 INC_BOTH (from
, from_byte
);
2695 /* Treat following character as a word constituent. */
2698 if (depth
|| !sexpflag
) break;
2699 /* This word counts as a sexp; return at end of it. */
2702 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2704 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2705 switch (syntax_multibyte (c
, multibyte_symbol_p
))
2709 INC_BOTH (from
, from_byte
);
2720 INC_BOTH (from
, from_byte
);
2724 case Scomment_fence
:
2725 comstyle
= ST_COMMENT_STYLE
;
2728 if (!parse_sexp_ignore_comments
) break;
2729 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2730 found
= forw_comment (from
, from_byte
, stop
,
2731 comnested
, comstyle
, 0,
2732 &out_charpos
, &out_bytepos
, &dummy
,
2734 from
= out_charpos
, from_byte
= out_bytepos
;
2741 INC_BOTH (from
, from_byte
);
2742 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2748 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (from_byte
))
2750 INC_BOTH (from
, from_byte
);
2760 if (!++depth
) goto done
;
2765 if (!--depth
) goto done
;
2766 if (depth
< min_depth
)
2767 xsignal3 (Qscan_error
,
2768 build_string ("Containing expression ends prematurely"),
2769 make_number (last_good
), make_number (from
));
2774 temp_pos
= dec_bytepos (from_byte
);
2775 stringterm
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2778 enum syntaxcode c_code
;
2781 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2782 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2783 c_code
= syntax_multibyte (c
, multibyte_symbol_p
);
2785 ? c
== stringterm
&& c_code
== Sstring
2786 : c_code
== Sstring_fence
)
2789 if (c_code
== Scharquote
|| c_code
== Sescape
)
2790 INC_BOTH (from
, from_byte
);
2791 INC_BOTH (from
, from_byte
);
2793 INC_BOTH (from
, from_byte
);
2794 if (!depth
&& sexpflag
) goto done
;
2797 /* Ignore whitespace, punctuation, quote, endcomment. */
2802 /* Reached end of buffer. Error if within object, return nil if between */
2809 /* End of object reached */
2819 DEC_BOTH (from
, from_byte
);
2820 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2821 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2822 syntax
= SYNTAX_WITH_FLAGS (c
);
2823 code
= syntax_multibyte (c
, multibyte_symbol_p
);
2824 if (depth
== min_depth
)
2827 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2828 if (code
== Sendcomment
)
2829 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2830 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2831 && prev_char_comend_first (from
, from_byte
)
2832 && parse_sexp_ignore_comments
)
2834 /* We must record the comment style encountered so that
2835 later, we can match only the proper comment begin
2836 sequence of the same style. */
2837 int c2
, other_syntax
;
2838 DEC_BOTH (from
, from_byte
);
2839 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2841 c2
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2842 other_syntax
= SYNTAX_WITH_FLAGS (c2
);
2843 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2844 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2847 /* Quoting turns anything except a comment-ender
2848 into a word character. Note that this cannot be true
2849 if we decremented FROM in the if-statement above. */
2850 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2852 DEC_BOTH (from
, from_byte
);
2855 else if (SYNTAX_FLAGS_PREFIX (syntax
))
2864 if (depth
|| !sexpflag
) break;
2865 /* This word counts as a sexp; count object finished
2866 after passing it. */
2869 temp_pos
= from_byte
;
2870 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
2874 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2875 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2876 /* Don't allow comment-end to be quoted. */
2877 if (syntax_multibyte (c1
, multibyte_symbol_p
) == Sendcomment
)
2879 quoted
= char_quoted (from
- 1, temp_pos
);
2882 DEC_BOTH (from
, from_byte
);
2883 temp_pos
= dec_bytepos (temp_pos
);
2884 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2886 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2888 switch (syntax_multibyte (c1
, multibyte_symbol_p
))
2890 case Sword
: case Ssymbol
: case Squote
: break;
2891 default: goto done2
;
2893 DEC_BOTH (from
, from_byte
);
2902 temp_pos
= dec_bytepos (from_byte
);
2903 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2904 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (temp_pos
))
2905 DEC_BOTH (from
, from_byte
);
2915 if (!++depth
) goto done2
;
2920 if (!--depth
) goto done2
;
2921 if (depth
< min_depth
)
2922 xsignal3 (Qscan_error
,
2923 build_string ("Containing expression ends prematurely"),
2924 make_number (last_good
), make_number (from
));
2928 if (!parse_sexp_ignore_comments
)
2930 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2931 &out_charpos
, &out_bytepos
);
2932 /* FIXME: if !found, it really wasn't a comment-end.
2933 For single-char Sendcomment, we can't do much about it apart
2934 from skipping the char.
2935 For 2-char endcomments, we could try again, taking both
2936 chars as separate entities, but it's a lot of trouble
2937 for very little gain, so we don't bother either. -sm */
2939 from
= out_charpos
, from_byte
= out_bytepos
;
2942 case Scomment_fence
:
2948 DEC_BOTH (from
, from_byte
);
2949 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2950 if (!char_quoted (from
, from_byte
))
2952 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2953 if (syntax_multibyte (c
, multibyte_symbol_p
) == code
)
2957 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2961 stringterm
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2966 DEC_BOTH (from
, from_byte
);
2967 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2968 if (!char_quoted (from
, from_byte
))
2970 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2972 && (syntax_multibyte (c
, multibyte_symbol_p
)
2977 if (!depth
&& sexpflag
) goto done2
;
2980 /* Ignore whitespace, punctuation, quote, endcomment. */
2985 /* Reached start of buffer. Error if within object, return nil if between */
2998 XSETFASTINT (val
, from
);
3002 xsignal3 (Qscan_error
,
3003 build_string ("Unbalanced parentheses"),
3004 make_number (last_good
), make_number (from
));
3007 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
3008 doc
: /* Scan from character number FROM by COUNT lists.
3009 Scan forward if COUNT is positive, backward if COUNT is negative.
3010 Return the character number of the position thus found.
3012 A \"list", in this context, refers to a balanced parenthetical
3013 grouping, as determined by the syntax table.
3015 If DEPTH is nonzero, treat that as the nesting depth of the starting
3016 point (i.e. the starting point is DEPTH parentheses deep). This
3017 function scans over parentheses until the depth goes to zero COUNT
3018 times. Hence, positive DEPTH moves out that number of levels of
3019 parentheses, while negative DEPTH moves to a deeper level.
3021 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3023 If we reach the beginning or end of the accessible part of the buffer
3024 before we have scanned over COUNT lists, return nil if the depth at
3025 that point is zero, and signal a error if the depth is nonzero. */)
3026 (Lisp_Object from
, Lisp_Object count
, Lisp_Object depth
)
3028 CHECK_NUMBER (from
);
3029 CHECK_NUMBER (count
);
3030 CHECK_NUMBER (depth
);
3032 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
3035 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
3036 doc
: /* Scan from character number FROM by COUNT balanced expressions.
3037 If COUNT is negative, scan backwards.
3038 Returns the character number of the position thus found.
3040 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3042 If the beginning or end of (the accessible part of) the buffer is reached
3043 in the middle of a parenthetical grouping, an error is signaled.
3044 If the beginning or end is reached between groupings
3045 but before count is used up, nil is returned. */)
3046 (Lisp_Object from
, Lisp_Object count
)
3048 CHECK_NUMBER (from
);
3049 CHECK_NUMBER (count
);
3051 return scan_lists (XINT (from
), XINT (count
), 0, 1);
3054 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
3056 doc
: /* Move point backward over any number of chars with prefix syntax.
3057 This includes chars with expression prefix syntax class (\\=') and those with
3058 the prefix syntax flag (p). */)
3061 ptrdiff_t beg
= BEGV
;
3062 ptrdiff_t opoint
= PT
;
3063 ptrdiff_t opoint_byte
= PT_BYTE
;
3065 ptrdiff_t pos_byte
= PT_BYTE
;
3070 SET_PT_BOTH (opoint
, opoint_byte
);
3075 SETUP_SYNTAX_TABLE (pos
, -1);
3077 DEC_BOTH (pos
, pos_byte
);
3079 while (!char_quoted (pos
, pos_byte
)
3080 /* Previous statement updates syntax table. */
3081 && ((c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
), SYNTAX (c
) == Squote
)
3082 || syntax_prefix_flag_p (c
)))
3085 opoint_byte
= pos_byte
;
3089 DEC_BOTH (pos
, pos_byte
);
3092 SET_PT_BOTH (opoint
, opoint_byte
);
3097 /* Parse forward from FROM / FROM_BYTE to END,
3098 assuming that FROM has state STATE,
3099 and return a description of the state of the parse at END.
3100 If STOPBEFORE, stop at the start of an atom.
3101 If COMMENTSTOP is 1, stop at the start of a comment.
3102 If COMMENTSTOP is -1, stop at the start or end of a comment,
3103 after the beginning of a string, or after the end of a string. */
3106 scan_sexps_forward (struct lisp_parse_state
*state
,
3107 ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t end
,
3108 EMACS_INT targetdepth
, bool stopbefore
,
3111 enum syntaxcode code
;
3114 struct level
{ ptrdiff_t last
, prev
; };
3115 struct level levelstart
[100];
3116 struct level
*curlevel
= levelstart
;
3117 struct level
*endlevel
= levelstart
+ 100;
3118 EMACS_INT depth
; /* Paren depth of current scanning location.
3119 level - levelstart equals this except
3120 when the depth becomes negative. */
3121 EMACS_INT mindepth
; /* Lowest DEPTH value seen. */
3122 bool start_quoted
= 0; /* True means starting after a char quote. */
3124 ptrdiff_t prev_from
; /* Keep one character before FROM. */
3125 ptrdiff_t prev_from_byte
;
3126 int prev_from_syntax
, prev_prev_from_syntax
;
3128 bool boundary_stop
= commentstop
== -1;
3131 ptrdiff_t out_bytepos
, out_charpos
;
3135 prev_from_byte
= from_byte
;
3137 DEC_BOTH (prev_from
, prev_from_byte
);
3139 /* Use this macro instead of `from++'. */
3141 do { prev_from = from; \
3142 prev_from_byte = from_byte; \
3143 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
3144 prev_prev_from_syntax = prev_from_syntax; \
3145 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
3146 INC_BOTH (from, from_byte); \
3148 UPDATE_SYNTAX_TABLE_FORWARD (from); \
3154 depth
= state
->depth
;
3155 start_quoted
= state
->quoted
;
3156 prev_prev_from_syntax
= Smax
;
3157 prev_from_syntax
= state
->prev_syntax
;
3159 tem
= state
->levelstarts
;
3160 while (!NILP (tem
)) /* >= second enclosing sexps. */
3162 Lisp_Object temhd
= Fcar (tem
);
3163 if (RANGED_INTEGERP (PTRDIFF_MIN
, temhd
, PTRDIFF_MAX
))
3164 curlevel
->last
= XINT (temhd
);
3165 if (++curlevel
== endlevel
)
3166 curlevel
--; /* error ("Nesting too deep for parser"); */
3167 curlevel
->prev
= -1;
3168 curlevel
->last
= -1;
3171 curlevel
->prev
= -1;
3172 curlevel
->last
= -1;
3177 SETUP_SYNTAX_TABLE (from
, 1);
3179 /* Enter the loop at a place appropriate for initial state. */
3181 if (state
->incomment
)
3182 goto startincomment
;
3183 if (state
->instring
>= 0)
3185 nofence
= state
->instring
!= ST_STRING_STYLE
;
3187 goto startquotedinstring
;
3190 else if (start_quoted
)
3195 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
)
3196 && (c1
= FETCH_CHAR (from_byte
),
3197 syntax
= SYNTAX_WITH_FLAGS (c1
),
3198 SYNTAX_FLAGS_COMSTART_SECOND (syntax
)))
3199 /* Duplicate code to avoid a complex if-expression
3200 which causes trouble for the SGI compiler. */
3202 /* Record the comment style we have entered so that only
3203 the comment-end sequence of the same style actually
3204 terminates the comment section. */
3206 = SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_from_syntax
);
3207 comnested
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
)
3208 | SYNTAX_FLAGS_COMMENT_NESTED (syntax
));
3209 state
->incomment
= comnested
? 1 : -1;
3210 state
->comstr_start
= prev_from
;
3212 prev_from_syntax
= Smax
; /* the syntax has already been
3219 code
= prev_from_syntax
& 0xff;
3220 if (code
== Scomment_fence
)
3222 /* Record the comment style we have entered so that only
3223 the comment-end sequence of the same style actually
3224 terminates the comment section. */
3225 state
->comstyle
= ST_COMMENT_STYLE
;
3226 state
->incomment
= -1;
3227 state
->comstr_start
= prev_from
;
3230 else if (code
== Scomment
)
3232 state
->comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
, 0);
3233 state
->incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
3235 state
->comstr_start
= prev_from
;
3239 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
3245 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3246 curlevel
->last
= prev_from
;
3248 if (from
== end
) goto endquoted
;
3251 /* treat following character as a word constituent */
3254 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3255 curlevel
->last
= prev_from
;
3259 int symchar
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3261 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
)
3262 && (syntax
= SYNTAX_WITH_FLAGS (symchar
),
3263 SYNTAX_FLAGS_COMSTART_SECOND (syntax
)))
3266 = SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_from_syntax
);
3267 comnested
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
)
3268 | SYNTAX_FLAGS_COMMENT_NESTED (syntax
));
3269 state
->incomment
= comnested
? 1 : -1;
3270 state
->comstr_start
= prev_from
;
3272 prev_from_syntax
= Smax
;
3277 switch (SYNTAX (symchar
))
3282 if (from
== end
) goto endquoted
;
3294 curlevel
->prev
= curlevel
->last
;
3297 case Scomment_fence
: /* Can't happen because it's handled above. */
3300 if (commentstop
|| boundary_stop
) goto done
;
3302 /* The (from == BEGV) test was to enter the loop in the middle so
3303 that we find a 2-char comment ender even if we start in the
3304 middle of it. We don't want to do that if we're just at the
3305 beginning of the comment (think of (*) ... (*)). */
3306 found
= forw_comment (from
, from_byte
, end
,
3307 state
->incomment
, state
->comstyle
,
3308 from
== BEGV
? 0 : prev_from_syntax
,
3309 &out_charpos
, &out_bytepos
, &state
->incomment
,
3311 from
= out_charpos
; from_byte
= out_bytepos
;
3312 /* Beware! prev_from and friends (except prev_from_syntax)
3313 are invalid now. Luckily, the `done' doesn't use them
3314 and the INC_FROM sets them to a sane value without
3316 if (!found
) goto done
;
3318 state
->incomment
= 0;
3319 state
->comstyle
= 0; /* reset the comment style */
3320 prev_from_syntax
= Smax
; /* For the comment closer */
3321 if (boundary_stop
) goto done
;
3325 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3327 /* curlevel++->last ran into compiler bug on Apollo */
3328 curlevel
->last
= prev_from
;
3329 if (++curlevel
== endlevel
)
3330 curlevel
--; /* error ("Nesting too deep for parser"); */
3331 curlevel
->prev
= -1;
3332 curlevel
->last
= -1;
3333 if (targetdepth
== depth
) goto done
;
3338 if (depth
< mindepth
)
3340 if (curlevel
!= levelstart
)
3342 curlevel
->prev
= curlevel
->last
;
3343 if (targetdepth
== depth
) goto done
;
3348 state
->comstr_start
= from
- 1;
3349 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3350 curlevel
->last
= prev_from
;
3351 state
->instring
= (code
== Sstring
3352 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte
))
3354 if (boundary_stop
) goto done
;
3357 nofence
= state
->instring
!= ST_STRING_STYLE
;
3362 enum syntaxcode c_code
;
3364 if (from
>= end
) goto done
;
3365 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3366 c_code
= SYNTAX (c
);
3368 /* Check C_CODE here so that if the char has
3369 a syntax-table property which says it is NOT
3370 a string character, it does not end the string. */
3371 if (nofence
&& c
== state
->instring
&& c_code
== Sstring
)
3377 if (!nofence
) goto string_end
;
3383 startquotedinstring
:
3384 if (from
>= end
) goto endquoted
;
3394 state
->instring
= -1;
3395 curlevel
->prev
= curlevel
->last
;
3397 if (boundary_stop
) goto done
;
3401 /* FIXME: We should do something with it. */
3404 /* Ignore whitespace, punctuation, quote, endcomment. */
3410 stop
: /* Here if stopping before start of sexp. */
3411 from
= prev_from
; /* We have just fetched the char that starts it; */
3412 from_byte
= prev_from_byte
;
3413 prev_from_syntax
= prev_prev_from_syntax
;
3414 goto done
; /* but return the position before it. */
3419 state
->depth
= depth
;
3420 state
->mindepth
= mindepth
;
3421 state
->thislevelstart
= curlevel
->prev
;
3422 state
->prevlevelstart
3423 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
3424 state
->location
= from
;
3425 state
->location_byte
= from_byte
;
3426 state
->levelstarts
= Qnil
;
3427 while (curlevel
> levelstart
)
3428 state
->levelstarts
= Fcons (make_number ((--curlevel
)->last
),
3429 state
->levelstarts
);
3430 state
->prev_syntax
= (SYNTAX_FLAGS_COMSTARTEND_FIRST (prev_from_syntax
)
3431 || state
->quoted
) ? prev_from_syntax
: Smax
;
3435 /* Convert a (lisp) parse state to the internal form used in
3436 scan_sexps_forward. */
3438 internalize_parse_state (Lisp_Object external
, struct lisp_parse_state
*state
)
3442 if (NILP (external
))
3445 state
->instring
= -1;
3446 state
->incomment
= 0;
3448 state
->comstyle
= 0; /* comment style a by default. */
3449 state
->comstr_start
= -1; /* no comment/string seen. */
3450 state
->levelstarts
= Qnil
;
3451 state
->prev_syntax
= Smax
;
3455 tem
= Fcar (external
);
3457 state
->depth
= XINT (tem
);
3461 external
= Fcdr (external
);
3462 external
= Fcdr (external
);
3463 external
= Fcdr (external
);
3464 tem
= Fcar (external
);
3465 /* Check whether we are inside string_fence-style string: */
3466 state
->instring
= (!NILP (tem
)
3467 ? (CHARACTERP (tem
) ? XFASTINT (tem
) : ST_STRING_STYLE
)
3470 external
= Fcdr (external
);
3471 tem
= Fcar (external
);
3472 state
->incomment
= (!NILP (tem
)
3473 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
3476 external
= Fcdr (external
);
3477 tem
= Fcar (external
);
3478 state
->quoted
= !NILP (tem
);
3480 /* if the eighth element of the list is nil, we are in comment
3481 style a. If it is non-nil, we are in comment style b */
3482 external
= Fcdr (external
);
3483 external
= Fcdr (external
);
3484 tem
= Fcar (external
);
3485 state
->comstyle
= (NILP (tem
)
3487 : (RANGED_INTEGERP (0, tem
, ST_COMMENT_STYLE
)
3489 : ST_COMMENT_STYLE
));
3491 external
= Fcdr (external
);
3492 tem
= Fcar (external
);
3493 state
->comstr_start
=
3494 RANGED_INTEGERP (PTRDIFF_MIN
, tem
, PTRDIFF_MAX
) ? XINT (tem
) : -1;
3495 external
= Fcdr (external
);
3496 tem
= Fcar (external
);
3497 state
->levelstarts
= tem
;
3499 external
= Fcdr (external
);
3500 tem
= Fcar (external
);
3501 state
->prev_syntax
= NILP (tem
) ? Smax
: XINT (tem
);
3505 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
3506 doc
: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3507 Parsing stops at TO or when certain criteria are met;
3508 point is set to where parsing stops.
3509 If fifth arg OLDSTATE is omitted or nil,
3510 parsing assumes that FROM is the beginning of a function.
3512 Value is a list of elements describing final state of parsing:
3514 1. character address of start of innermost containing list; nil if none.
3515 2. character address of start of last complete sexp terminated.
3516 3. non-nil if inside a string.
3517 (it is the character that will terminate the string,
3518 or t if the string should be terminated by a generic string delimiter.)
3519 4. nil if outside a comment, t if inside a non-nestable comment,
3520 else an integer (the current comment nesting).
3521 5. t if following a quote character.
3522 6. the minimum paren-depth encountered during this scan.
3523 7. style of comment, if any.
3524 8. character address of start of comment or string; nil if not in one.
3525 9. List of positions of currently open parens, outermost first.
3526 10. When the last position scanned holds the first character of a
3527 (potential) two character construct, the syntax of that position,
3528 otherwise nil. That construct can be a two character comment
3529 delimiter or an Escaped or Char-quoted character.
3530 11..... Possible further internal information used by `parse-partial-sexp'.
3532 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3533 in parentheses becomes equal to TARGETDEPTH.
3534 Fourth arg STOPBEFORE non-nil means stop when we come to
3535 any character that starts a sexp.
3536 Fifth arg OLDSTATE is a list like what this function returns.
3537 It is used to initialize the state of the parse. Elements number 1, 2, 6
3539 Sixth arg COMMENTSTOP non-nil means stop after the start of a comment.
3540 If it is the symbol `syntax-table', stop after the start of a comment or a
3541 string, or after end of a comment or a string. */)
3542 (Lisp_Object from
, Lisp_Object to
, Lisp_Object targetdepth
,
3543 Lisp_Object stopbefore
, Lisp_Object oldstate
, Lisp_Object commentstop
)
3545 struct lisp_parse_state state
;
3548 if (!NILP (targetdepth
))
3550 CHECK_NUMBER (targetdepth
);
3551 target
= XINT (targetdepth
);
3554 target
= TYPE_MINIMUM (EMACS_INT
); /* We won't reach this depth. */
3556 validate_region (&from
, &to
);
3557 internalize_parse_state (oldstate
, &state
);
3558 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
3560 target
, !NILP (stopbefore
),
3562 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
3564 SET_PT_BOTH (state
.location
, state
.location_byte
);
3567 Fcons (make_number (state
.depth
),
3568 Fcons (state
.prevlevelstart
< 0
3569 ? Qnil
: make_number (state
.prevlevelstart
),
3570 Fcons (state
.thislevelstart
< 0
3571 ? Qnil
: make_number (state
.thislevelstart
),
3572 Fcons (state
.instring
>= 0
3573 ? (state
.instring
== ST_STRING_STYLE
3574 ? Qt
: make_number (state
.instring
)) : Qnil
,
3575 Fcons (state
.incomment
< 0 ? Qt
:
3576 (state
.incomment
== 0 ? Qnil
:
3577 make_number (state
.incomment
)),
3578 Fcons (state
.quoted
? Qt
: Qnil
,
3579 Fcons (make_number (state
.mindepth
),
3580 Fcons ((state
.comstyle
3581 ? (state
.comstyle
== ST_COMMENT_STYLE
3583 : make_number (state
.comstyle
))
3585 Fcons (((state
.incomment
3586 || (state
.instring
>= 0))
3587 ? make_number (state
.comstr_start
)
3589 Fcons (state
.levelstarts
,
3590 Fcons (state
.prev_syntax
== Smax
3592 : make_number (state
.prev_syntax
),
3597 init_syntax_once (void)
3602 /* This has to be done here, before we call Fmake_char_table. */
3603 DEFSYM (Qsyntax_table
, "syntax-table");
3605 /* Create objects which can be shared among syntax tables. */
3606 Vsyntax_code_object
= make_uninit_vector (Smax
);
3607 for (i
= 0; i
< Smax
; i
++)
3608 ASET (Vsyntax_code_object
, i
, Fcons (make_number (i
), Qnil
));
3610 /* Now we are ready to set up this property, so we can
3611 create syntax tables. */
3612 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
3614 temp
= AREF (Vsyntax_code_object
, Swhitespace
);
3616 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
3618 /* Control characters should not be whitespace. */
3619 temp
= AREF (Vsyntax_code_object
, Spunct
);
3620 for (i
= 0; i
<= ' ' - 1; i
++)
3621 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3622 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 0177, temp
);
3624 /* Except that a few really are whitespace. */
3625 temp
= AREF (Vsyntax_code_object
, Swhitespace
);
3626 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ' ', temp
);
3627 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\t', temp
);
3628 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\n', temp
);
3629 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 015, temp
);
3630 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 014, temp
);
3632 temp
= AREF (Vsyntax_code_object
, Sword
);
3633 for (i
= 'a'; i
<= 'z'; i
++)
3634 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3635 for (i
= 'A'; i
<= 'Z'; i
++)
3636 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3637 for (i
= '0'; i
<= '9'; i
++)
3638 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3640 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
3641 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
3643 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
3644 Fcons (make_number (Sopen
), make_number (')')));
3645 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
3646 Fcons (make_number (Sclose
), make_number ('(')));
3647 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
3648 Fcons (make_number (Sopen
), make_number (']')));
3649 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
3650 Fcons (make_number (Sclose
), make_number ('[')));
3651 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
3652 Fcons (make_number (Sopen
), make_number ('}')));
3653 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
3654 Fcons (make_number (Sclose
), make_number ('{')));
3655 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
3656 Fcons (make_number (Sstring
), Qnil
));
3657 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
3658 Fcons (make_number (Sescape
), Qnil
));
3660 temp
= AREF (Vsyntax_code_object
, Ssymbol
);
3661 for (i
= 0; i
< 10; i
++)
3663 c
= "_-+*/&|<>="[i
];
3664 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3667 temp
= AREF (Vsyntax_code_object
, Spunct
);
3668 for (i
= 0; i
< 12; i
++)
3670 c
= ".,;:?!#@~^'`"[i
];
3671 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3674 /* All multibyte characters have syntax `word' by default. */
3675 temp
= AREF (Vsyntax_code_object
, Sword
);
3676 char_table_set_range (Vstandard_syntax_table
, 0x80, MAX_CHAR
, temp
);
3680 syms_of_syntax (void)
3682 DEFSYM (Qsyntax_table_p
, "syntax-table-p");
3684 staticpro (&Vsyntax_code_object
);
3686 staticpro (&gl_state
.object
);
3687 staticpro (&gl_state
.global_code
);
3688 staticpro (&gl_state
.current_syntax_table
);
3689 staticpro (&gl_state
.old_prop
);
3691 /* Defined in regex.c. */
3692 staticpro (&re_match_object
);
3694 DEFSYM (Qscan_error
, "scan-error");
3695 Fput (Qscan_error
, Qerror_conditions
,
3696 listn (CONSTYPE_PURE
, 2, Qscan_error
, Qerror
));
3697 Fput (Qscan_error
, Qerror_message
,
3698 build_pure_c_string ("Scan error"));
3700 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments
,
3701 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3703 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties
,
3704 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3705 Otherwise, that text property is simply ignored.
3706 See the info node `(elisp)Syntax Properties' for a description of the
3707 `syntax-table' property. */);
3709 DEFVAR_INT ("syntax-propertize--done", syntax_propertize__done
,
3710 doc
: /* Position up to which syntax-table properties have been set. */);
3711 syntax_propertize__done
= -1;
3712 DEFSYM (Qinternal__syntax_propertize
, "internal--syntax-propertize");
3713 Fmake_variable_buffer_local (intern ("syntax-propertize--done"));
3715 words_include_escapes
= 0;
3716 DEFVAR_BOOL ("words-include-escapes", words_include_escapes
,
3717 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3719 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol
,
3720 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3721 multibyte_syntax_as_symbol
= 0;
3723 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3724 open_paren_in_column_0_is_defun_start
,
3725 doc
: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3726 open_paren_in_column_0_is_defun_start
= 1;
3729 DEFVAR_LISP ("find-word-boundary-function-table",
3730 Vfind_word_boundary_function_table
,
3732 Char table of functions to search for the word boundary.
3733 Each function is called with two arguments; POS and LIMIT.
3734 POS and LIMIT are character positions in the current buffer.
3736 If POS is less than LIMIT, POS is at the first character of a word,
3737 and the return value of a function should be a position after the
3738 last character of that word.
3740 If POS is not less than LIMIT, POS is at the last character of a word,
3741 and the return value of a function should be a position at the first
3742 character of that word.
3744 In both cases, LIMIT bounds the search. */);
3745 Vfind_word_boundary_function_table
= Fmake_char_table (Qnil
, Qnil
);
3747 DEFVAR_BOOL ("comment-end-can-be-escaped", Vcomment_end_can_be_escaped
,
3748 doc
: /* Non-nil means an escaped ender inside a comment doesn't end the comment. */);
3749 Vcomment_end_can_be_escaped
= 0;
3750 DEFSYM (Qcomment_end_can_be_escaped
, "comment-end-can-be-escaped");
3751 Fmake_variable_buffer_local (Qcomment_end_can_be_escaped
);
3753 defsubr (&Ssyntax_table_p
);
3754 defsubr (&Ssyntax_table
);
3755 defsubr (&Sstandard_syntax_table
);
3756 defsubr (&Scopy_syntax_table
);
3757 defsubr (&Sset_syntax_table
);
3758 defsubr (&Schar_syntax
);
3759 defsubr (&Smatching_paren
);
3760 defsubr (&Sstring_to_syntax
);
3761 defsubr (&Smodify_syntax_entry
);
3762 defsubr (&Sinternal_describe_syntax_value
);
3764 defsubr (&Sforward_word
);
3766 defsubr (&Sskip_chars_forward
);
3767 defsubr (&Sskip_chars_backward
);
3768 defsubr (&Sskip_syntax_forward
);
3769 defsubr (&Sskip_syntax_backward
);
3771 defsubr (&Sforward_comment
);
3772 defsubr (&Sscan_lists
);
3773 defsubr (&Sscan_sexps
);
3774 defsubr (&Sbackward_prefix_chars
);
3775 defsubr (&Sparse_partial_sexp
);