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
)
1696 if (handle_iso_classes
&& c
== '['
1697 && i_byte
< size_byte
1698 && str
[i_byte
] == ':')
1700 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1701 const unsigned char *class_end
= class_beg
;
1702 const unsigned char *class_limit
= str
+ size_byte
- 2;
1703 /* Leave room for the null. */
1704 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1707 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1708 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1710 while (class_end
< class_limit
1711 && *class_end
>= 'a' && *class_end
<= 'z')
1714 if (class_end
== class_beg
1715 || *class_end
!= ':' || class_end
[1] != ']')
1716 goto not_a_class_name
;
1718 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1719 class_name
[class_end
- class_beg
] = 0;
1721 cc
= re_wctype (class_name
);
1723 error ("Invalid ISO C character class");
1725 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1727 i_byte
= class_end
+ 2 - str
;
1734 if (i_byte
== size_byte
)
1739 /* Treat `-' as range character only if another character
1741 if (i_byte
+ 1 < size_byte
1742 && str
[i_byte
] == '-')
1746 /* Skip over the dash. */
1749 /* Get the end of the range. */
1752 && i_byte
< size_byte
)
1760 if (! ASCII_CHAR_P (c2
))
1761 string_has_eight_bit
= 1;
1767 if (! ASCII_CHAR_P (c
))
1768 string_has_eight_bit
= 1;
1772 /* If the current range is multibyte and STRING contains
1773 eight-bit chars, arrange fastmap and setup char_ranges for
1774 the corresponding multibyte chars. */
1775 if (multibyte
&& string_has_eight_bit
)
1778 char himap
[0200 + 1];
1779 memcpy (himap
, fastmap
+ 0200, 0200);
1781 memset (fastmap
+ 0200, 0, 0200);
1782 SAFE_NALLOCA (char_ranges
, 2, 128);
1785 while ((p1
= memchr (himap
+ i
, 1, 0200 - i
)))
1787 /* Deduce the next range C..C2 from the next clump of 1s
1788 in HIMAP starting with &HIMAP[I]. HIMAP is the high
1789 order half of the old FASTMAP. */
1790 int c2
, leading_code
;
1792 c
= BYTE8_TO_CHAR (i
+ 0200);
1794 c2
= BYTE8_TO_CHAR (i
+ 0200 - 1);
1796 char_ranges
[n_char_ranges
++] = c
;
1797 char_ranges
[n_char_ranges
++] = c2
;
1798 leading_code
= CHAR_LEADING_CODE (c
);
1799 memset (fastmap
+ leading_code
, 1,
1800 CHAR_LEADING_CODE (c2
) - leading_code
+ 1);
1804 else /* STRING is multibyte */
1806 SAFE_NALLOCA (char_ranges
, 2, SCHARS (string
));
1808 while (i_byte
< size_byte
)
1810 int leading_code
= str
[i_byte
];
1811 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1814 if (handle_iso_classes
&& c
== '['
1815 && i_byte
< size_byte
1816 && STRING_CHAR (str
+ i_byte
) == ':')
1818 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1819 const unsigned char *class_end
= class_beg
;
1820 const unsigned char *class_limit
= str
+ size_byte
- 2;
1821 /* Leave room for the null. */
1822 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1825 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1826 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1828 while (class_end
< class_limit
1829 && *class_end
>= 'a' && *class_end
<= 'z')
1832 if (class_end
== class_beg
1833 || *class_end
!= ':' || class_end
[1] != ']')
1834 goto not_a_class_name_multibyte
;
1836 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1837 class_name
[class_end
- class_beg
] = 0;
1839 cc
= re_wctype (class_name
);
1841 error ("Invalid ISO C character class");
1843 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1845 i_byte
= class_end
+ 2 - str
;
1849 not_a_class_name_multibyte
:
1852 if (i_byte
== size_byte
)
1855 leading_code
= str
[i_byte
];
1856 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1859 /* Treat `-' as range character only if another character
1861 if (i_byte
+ 1 < size_byte
1862 && str
[i_byte
] == '-')
1864 int c2
, leading_code2
;
1866 /* Skip over the dash. */
1869 /* Get the end of the range. */
1870 leading_code2
= str
[i_byte
];
1871 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1875 && i_byte
< size_byte
)
1877 leading_code2
= str
[i_byte
];
1878 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1884 if (ASCII_CHAR_P (c
))
1886 while (c
<= c2
&& c
< 0x80)
1888 leading_code
= CHAR_LEADING_CODE (c
);
1890 if (! ASCII_CHAR_P (c
))
1892 int lim2
= leading_code2
+ 1;
1893 while (leading_code
< lim2
)
1894 fastmap
[leading_code
++] = 1;
1897 char_ranges
[n_char_ranges
++] = c
;
1898 char_ranges
[n_char_ranges
++] = c2
;
1904 if (ASCII_CHAR_P (c
))
1908 fastmap
[leading_code
] = 1;
1909 char_ranges
[n_char_ranges
++] = c
;
1910 char_ranges
[n_char_ranges
++] = c
;
1915 /* If the current range is unibyte and STRING contains non-ASCII
1916 chars, arrange fastmap for the corresponding unibyte
1919 if (! multibyte
&& n_char_ranges
> 0)
1921 memset (fastmap
+ 0200, 0, 0200);
1922 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1924 int c1
= char_ranges
[i
];
1925 int lim2
= char_ranges
[i
+ 1] + 1;
1927 for (; c1
< lim2
; c1
++)
1929 int b
= CHAR_TO_BYTE_SAFE (c1
);
1937 /* If ^ was the first character, complement the fastmap. */
1941 for (i
= 0; i
< sizeof fastmap
; i
++)
1945 for (i
= 0; i
< 0200; i
++)
1947 /* All non-ASCII chars possibly match. */
1948 for (; i
< sizeof fastmap
; i
++)
1954 ptrdiff_t start_point
= PT
;
1956 ptrdiff_t pos_byte
= PT_BYTE
;
1957 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1961 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1962 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1966 endp
= CHAR_POS_ADDR (XINT (lim
));
1967 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1971 /* This code may look up syntax tables using functions that rely on the
1972 gl_state object. To make sure this object is not out of date,
1973 let's initialize it manually.
1974 We ignore syntax-table text-properties for now, since that's
1975 what we've done in the past. */
1976 SETUP_BUFFER_SYNTAX_TABLE ();
1991 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
1992 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
2002 if (! ASCII_CHAR_P (c
))
2004 /* As we are looking at a multibyte character, we
2005 must look up the character in the table
2006 CHAR_RANGES. If there's no data in the table,
2007 that character is not what we want to skip. */
2009 /* The following code do the right thing even if
2010 n_char_ranges is zero (i.e. no data in
2012 for (i
= 0; i
< n_char_ranges
; i
+= 2)
2013 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
2015 if (!(negate
^ (i
< n_char_ranges
)))
2019 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2032 if (!NILP (iso_classes
) && in_classes (*p
, iso_classes
))
2037 goto fwd_unibyte_ok
;
2043 p
++, pos
++, pos_byte
++;
2051 unsigned char *prev_p
;
2061 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2062 c
= STRING_CHAR (p
);
2064 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
2074 if (! ASCII_CHAR_P (c
))
2076 /* See the comment in the previous similar code. */
2077 for (i
= 0; i
< n_char_ranges
; i
+= 2)
2078 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
2080 if (!(negate
^ (i
< n_char_ranges
)))
2084 pos
--, pos_byte
-= prev_p
- p
;
2097 if (! NILP (iso_classes
) && in_classes (p
[-1], iso_classes
))
2102 goto back_unibyte_ok
;
2105 if (!fastmap
[p
[-1]])
2108 p
--, pos
--, pos_byte
--;
2112 SET_PT_BOTH (pos
, pos_byte
);
2116 return make_number (PT
- start_point
);
2122 skip_syntaxes (bool forwardp
, Lisp_Object string
, Lisp_Object lim
)
2125 unsigned char fastmap
[0400];
2127 ptrdiff_t i
, i_byte
;
2129 ptrdiff_t size_byte
;
2132 CHECK_STRING (string
);
2135 XSETINT (lim
, forwardp
? ZV
: BEGV
);
2137 CHECK_NUMBER_COERCE_MARKER (lim
);
2139 /* In any case, don't allow scan outside bounds of buffer. */
2140 if (XINT (lim
) > ZV
)
2141 XSETFASTINT (lim
, ZV
);
2142 if (XINT (lim
) < BEGV
)
2143 XSETFASTINT (lim
, BEGV
);
2145 if (forwardp
? (PT
>= XFASTINT (lim
)) : (PT
<= XFASTINT (lim
)))
2146 return make_number (0);
2148 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
2149 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
2151 memset (fastmap
, 0, sizeof fastmap
);
2153 if (SBYTES (string
) > SCHARS (string
))
2154 /* As this is very rare case (syntax spec is ASCII only), don't
2155 consider efficiency. */
2156 string
= string_make_unibyte (string
);
2158 str
= SDATA (string
);
2159 size_byte
= SBYTES (string
);
2162 if (i_byte
< size_byte
2163 && SREF (string
, 0) == '^')
2165 negate
= 1; i_byte
++;
2168 /* Find the syntaxes specified and set their elements of fastmap. */
2170 while (i_byte
< size_byte
)
2173 fastmap
[syntax_spec_code
[c
]] = 1;
2176 /* If ^ was the first character, complement the fastmap. */
2178 for (i
= 0; i
< sizeof fastmap
; i
++)
2182 ptrdiff_t start_point
= PT
;
2184 ptrdiff_t pos_byte
= PT_BYTE
;
2185 unsigned char *p
, *endp
, *stop
;
2188 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
2194 p
= BYTE_POS_ADDR (pos_byte
);
2195 endp
= XINT (lim
) == GPT
? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
2196 stop
= pos
< GPT
&& GPT
< XINT (lim
) ? GPT_ADDR
: endp
;
2210 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
2213 if (! fastmap
[SYNTAX (c
)])
2215 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2217 while (!parse_sexp_lookup_properties
2218 || pos
< gl_state
.e_property
);
2220 update_syntax_table_forward (pos
+ gl_state
.offset
,
2221 false, gl_state
.object
);
2226 p
= BYTE_POS_ADDR (pos_byte
);
2227 endp
= CHAR_POS_ADDR (XINT (lim
));
2228 stop
= pos
>= GPT
&& GPT
> XINT (lim
) ? GAP_END_ADDR
: endp
;
2234 unsigned char *prev_p
;
2243 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2245 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2246 c
= STRING_CHAR (p
);
2247 if (! fastmap
[SYNTAX (c
)])
2249 pos
--, pos_byte
-= prev_p
- p
;
2263 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2264 if (! fastmap
[SYNTAX (p
[-1])])
2266 p
--, pos
--, pos_byte
--;
2272 SET_PT_BOTH (pos
, pos_byte
);
2275 return make_number (PT
- start_point
);
2279 /* Return true if character C belongs to one of the ISO classes
2280 in the list ISO_CLASSES. Each class is represented by an
2281 integer which is its type according to re_wctype. */
2284 in_classes (int c
, Lisp_Object iso_classes
)
2286 bool fits_class
= 0;
2288 while (CONSP (iso_classes
))
2291 elt
= XCAR (iso_classes
);
2292 iso_classes
= XCDR (iso_classes
);
2294 if (re_iswctype (c
, XFASTINT (elt
)))
2301 /* Jump over a comment, assuming we are at the beginning of one.
2302 FROM is the current position.
2303 FROM_BYTE is the bytepos corresponding to FROM.
2304 Do not move past STOP (a charpos).
2305 The comment over which we have to jump is of style STYLE
2306 (either SYNTAX_FLAGS_COMMENT_STYLE (foo) or ST_COMMENT_STYLE).
2307 NESTING should be positive to indicate the nesting at the beginning
2308 for nested comments and should be zero or negative else.
2309 ST_COMMENT_STYLE cannot be nested.
2310 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2311 (or 0 If the search cannot start in the middle of a two-character).
2313 If successful, return true and store the charpos of the comment's
2314 end into *CHARPOS_PTR and the corresponding bytepos into
2315 *BYTEPOS_PTR. Else, return false and store the charpos STOP into
2316 *CHARPOS_PTR, the corresponding bytepos into *BYTEPOS_PTR and the
2317 current nesting (as defined for state->incomment) in
2318 *INCOMMENT_PTR. Should the last character scanned in an incomplete
2319 comment be a possible first character of a two character construct,
2320 we store its SYNTAX_WITH_FLAGS into *last_syntax_ptr. Otherwise,
2321 we store Smax into *last_syntax_ptr.
2323 The comment end is the last character of the comment rather than the
2324 character just after the comment.
2326 Global syntax data is assumed to initially be valid for FROM and
2327 remains valid for forward search starting at the returned position. */
2330 forw_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
,
2331 EMACS_INT nesting
, int style
, int prev_syntax
,
2332 ptrdiff_t *charpos_ptr
, ptrdiff_t *bytepos_ptr
,
2333 EMACS_INT
*incomment_ptr
, int *last_syntax_ptr
)
2336 register enum syntaxcode code
;
2337 register int syntax
, other_syntax
;
2339 if (nesting
<= 0) nesting
= -1;
2341 /* Enter the loop in the middle so that we find
2342 a 2-char comment ender if we start in the middle of it. */
2343 syntax
= prev_syntax
;
2344 code
= syntax
& 0xff;
2345 if (syntax
!= 0 && from
< stop
) goto forw_incomment
;
2351 *incomment_ptr
= nesting
;
2352 *charpos_ptr
= from
;
2353 *bytepos_ptr
= from_byte
;
2355 (code
== Sescape
|| code
== Scharquote
2356 || SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2358 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)))
2362 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2363 syntax
= SYNTAX_WITH_FLAGS (c
);
2364 code
= syntax
& 0xff;
2365 if (code
== Sendcomment
2366 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
2367 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
2368 (nesting
> 0 && --nesting
== 0) : nesting
< 0)
2369 && !(Vcomment_end_can_be_escaped
&& char_quoted (from
, from_byte
)))
2370 /* We have encountered a comment end of the same style
2371 as the comment sequence which began this comment
2374 if (code
== Scomment_fence
2375 && style
== ST_COMMENT_STYLE
)
2376 /* We have encountered a comment end of the same style
2377 as the comment sequence which began this comment
2382 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
2383 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
)
2384 /* We have encountered a nested comment of the same style
2385 as the comment sequence which began this comment section. */
2387 INC_BOTH (from
, from_byte
);
2388 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2391 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2392 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2393 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2394 SYNTAX_FLAGS_COMEND_SECOND (other_syntax
))
2395 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, other_syntax
) == style
2396 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2397 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
))
2398 ? nesting
> 0 : nesting
< 0))
2400 syntax
= Smax
; /* So that "|#" (lisp) can not return
2401 the syntax of "#" in *last_syntax_ptr. */
2403 /* We have encountered a comment end of the same style
2404 as the comment sequence which began this comment section. */
2408 INC_BOTH (from
, from_byte
);
2409 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2414 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
2415 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2416 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2417 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
) == style
2418 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2419 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2420 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
)))
2421 /* We have encountered a nested comment of the same style
2422 as the comment sequence which began this comment section. */
2424 syntax
= Smax
; /* So that "#|#" isn't also a comment ender. */
2425 INC_BOTH (from
, from_byte
);
2426 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2430 *charpos_ptr
= from
;
2431 *bytepos_ptr
= from_byte
;
2432 *last_syntax_ptr
= Smax
; /* Any syntactic power the last byte had is
2437 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
2439 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2440 Stop scanning if we find something other than a comment or whitespace.
2441 Set point to where scanning stops.
2442 If COUNT comments are found as expected, with nothing except whitespace
2443 between them, return t; otherwise return nil. */)
2446 ptrdiff_t from
, from_byte
, stop
;
2448 enum syntaxcode code
;
2449 int comstyle
= 0; /* style of comment encountered */
2450 bool comnested
= 0; /* whether the comment is nestable or not */
2453 ptrdiff_t out_charpos
, out_bytepos
;
2457 CHECK_NUMBER (count
);
2458 count1
= XINT (count
);
2459 stop
= count1
> 0 ? ZV
: BEGV
;
2465 from_byte
= PT_BYTE
;
2467 SETUP_SYNTAX_TABLE (from
, count1
);
2472 bool comstart_first
;
2473 int syntax
, other_syntax
;
2477 SET_PT_BOTH (from
, from_byte
);
2481 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2482 syntax
= SYNTAX_WITH_FLAGS (c
);
2484 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2485 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2486 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2487 INC_BOTH (from
, from_byte
);
2488 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2489 if (from
< stop
&& comstart_first
2490 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2491 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2492 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
)))
2494 /* We have encountered a comment start sequence and we
2495 are ignoring all text inside comments. We must record
2496 the comment style this sequence begins so that later,
2497 only a comment end of the same style actually ends
2498 the comment section. */
2500 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2501 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2502 INC_BOTH (from
, from_byte
);
2503 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2506 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
2508 if (code
== Scomment_fence
)
2509 comstyle
= ST_COMMENT_STYLE
;
2510 else if (code
!= Scomment
)
2513 DEC_BOTH (from
, from_byte
);
2514 SET_PT_BOTH (from
, from_byte
);
2517 /* We're at the start of a comment. */
2518 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
2519 &out_charpos
, &out_bytepos
, &dummy
, &dummy2
);
2520 from
= out_charpos
; from_byte
= out_bytepos
;
2524 SET_PT_BOTH (from
, from_byte
);
2527 INC_BOTH (from
, from_byte
);
2528 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2529 /* We have skipped one comment. */
2542 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
2547 DEC_BOTH (from
, from_byte
);
2548 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2549 quoted
= char_quoted (from
, from_byte
);
2550 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2551 syntax
= SYNTAX_WITH_FLAGS (c
);
2554 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2555 if (code
== Sendcomment
)
2556 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2557 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2558 && prev_char_comend_first (from
, from_byte
)
2559 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
2562 /* We must record the comment style encountered so that
2563 later, we can match only the proper comment begin
2564 sequence of the same style. */
2565 DEC_BOTH (from
, from_byte
);
2567 /* Calling char_quoted, above, set up global syntax position
2568 at the new value of FROM. */
2569 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2570 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2571 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2572 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2575 if (code
== Scomment_fence
)
2577 /* Skip until first preceding unquoted comment_fence. */
2578 bool fence_found
= 0;
2579 ptrdiff_t ini
= from
, ini_byte
= from_byte
;
2583 DEC_BOTH (from
, from_byte
);
2584 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2585 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2586 if (SYNTAX (c
) == Scomment_fence
2587 && !char_quoted (from
, from_byte
))
2592 else if (from
== stop
)
2595 if (fence_found
== 0)
2597 from
= ini
; /* Set point to ini + 1. */
2598 from_byte
= ini_byte
;
2602 /* We have skipped one comment. */
2605 else if (code
== Sendcomment
)
2607 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2608 &out_charpos
, &out_bytepos
);
2612 /* This end-of-line is not an end-of-comment.
2613 Treat it like a whitespace.
2614 CC-mode (and maybe others) relies on this behavior. */
2618 /* Failure: we should go back to the end of this
2619 not-quite-endcomment. */
2620 if (SYNTAX (c
) != code
)
2621 /* It was a two-char Sendcomment. */
2622 INC_BOTH (from
, from_byte
);
2628 /* We have skipped one comment. */
2629 from
= out_charpos
, from_byte
= out_bytepos
;
2633 else if (code
!= Swhitespace
|| quoted
)
2637 INC_BOTH (from
, from_byte
);
2638 SET_PT_BOTH (from
, from_byte
);
2646 SET_PT_BOTH (from
, from_byte
);
2651 /* Return syntax code of character C if C is an ASCII character
2652 or if MULTIBYTE_SYMBOL_P is false. Otherwise, return Ssymbol. */
2654 static enum syntaxcode
2655 syntax_multibyte (int c
, bool multibyte_symbol_p
)
2657 return ASCII_CHAR_P (c
) || !multibyte_symbol_p
? SYNTAX (c
) : Ssymbol
;
2661 scan_lists (EMACS_INT from
, EMACS_INT count
, EMACS_INT depth
, bool sexpflag
)
2664 ptrdiff_t stop
= count
> 0 ? ZV
: BEGV
;
2669 enum syntaxcode code
;
2670 EMACS_INT min_depth
= depth
; /* Err out if depth gets less than this. */
2671 int comstyle
= 0; /* Style of comment encountered. */
2672 bool comnested
= 0; /* Whether the comment is nestable or not. */
2674 EMACS_INT last_good
= from
;
2676 ptrdiff_t from_byte
;
2677 ptrdiff_t out_bytepos
, out_charpos
;
2680 bool multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2682 if (depth
> 0) min_depth
= 0;
2684 if (from
> ZV
) from
= ZV
;
2685 if (from
< BEGV
) from
= BEGV
;
2687 from_byte
= CHAR_TO_BYTE (from
);
2692 SETUP_SYNTAX_TABLE (from
, count
);
2697 bool comstart_first
, prefix
;
2698 int syntax
, other_syntax
;
2699 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2700 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2701 syntax
= SYNTAX_WITH_FLAGS (c
);
2702 code
= syntax_multibyte (c
, multibyte_symbol_p
);
2703 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2704 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2705 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2706 prefix
= SYNTAX_FLAGS_PREFIX (syntax
);
2707 if (depth
== min_depth
)
2709 INC_BOTH (from
, from_byte
);
2710 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2711 if (from
< stop
&& comstart_first
2712 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2713 other_syntax
= SYNTAX_WITH_FLAGS (c
),
2714 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2715 && parse_sexp_ignore_comments
)
2717 /* We have encountered a comment start sequence and we
2718 are ignoring all text inside comments. We must record
2719 the comment style this sequence begins so that later,
2720 only a comment end of the same style actually ends
2721 the comment section. */
2723 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2724 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2725 INC_BOTH (from
, from_byte
);
2726 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2738 INC_BOTH (from
, from_byte
);
2739 /* Treat following character as a word constituent. */
2742 if (depth
|| !sexpflag
) break;
2743 /* This word counts as a sexp; return at end of it. */
2746 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2748 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2749 switch (syntax_multibyte (c
, multibyte_symbol_p
))
2753 INC_BOTH (from
, from_byte
);
2764 INC_BOTH (from
, from_byte
);
2768 case Scomment_fence
:
2769 comstyle
= ST_COMMENT_STYLE
;
2772 if (!parse_sexp_ignore_comments
) break;
2773 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2774 found
= forw_comment (from
, from_byte
, stop
,
2775 comnested
, comstyle
, 0,
2776 &out_charpos
, &out_bytepos
, &dummy
,
2778 from
= out_charpos
, from_byte
= out_bytepos
;
2785 INC_BOTH (from
, from_byte
);
2786 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2792 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (from_byte
))
2794 INC_BOTH (from
, from_byte
);
2804 if (!++depth
) goto done
;
2809 if (!--depth
) goto done
;
2810 if (depth
< min_depth
)
2811 xsignal3 (Qscan_error
,
2812 build_string ("Containing expression ends prematurely"),
2813 make_number (last_good
), make_number (from
));
2818 temp_pos
= dec_bytepos (from_byte
);
2819 stringterm
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2822 enum syntaxcode c_code
;
2825 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2826 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2827 c_code
= syntax_multibyte (c
, multibyte_symbol_p
);
2829 ? c
== stringterm
&& c_code
== Sstring
2830 : c_code
== Sstring_fence
)
2833 if (c_code
== Scharquote
|| c_code
== Sescape
)
2834 INC_BOTH (from
, from_byte
);
2835 INC_BOTH (from
, from_byte
);
2837 INC_BOTH (from
, from_byte
);
2838 if (!depth
&& sexpflag
) goto done
;
2841 /* Ignore whitespace, punctuation, quote, endcomment. */
2846 /* Reached end of buffer. Error if within object, return nil if between */
2853 /* End of object reached */
2863 DEC_BOTH (from
, from_byte
);
2864 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2865 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2866 syntax
= SYNTAX_WITH_FLAGS (c
);
2867 code
= syntax_multibyte (c
, multibyte_symbol_p
);
2868 if (depth
== min_depth
)
2871 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2872 if (code
== Sendcomment
)
2873 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2874 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2875 && prev_char_comend_first (from
, from_byte
)
2876 && parse_sexp_ignore_comments
)
2878 /* We must record the comment style encountered so that
2879 later, we can match only the proper comment begin
2880 sequence of the same style. */
2881 int c2
, other_syntax
;
2882 DEC_BOTH (from
, from_byte
);
2883 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2885 c2
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2886 other_syntax
= SYNTAX_WITH_FLAGS (c2
);
2887 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2888 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2891 /* Quoting turns anything except a comment-ender
2892 into a word character. Note that this cannot be true
2893 if we decremented FROM in the if-statement above. */
2894 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2896 DEC_BOTH (from
, from_byte
);
2899 else if (SYNTAX_FLAGS_PREFIX (syntax
))
2908 if (depth
|| !sexpflag
) break;
2909 /* This word counts as a sexp; count object finished
2910 after passing it. */
2913 temp_pos
= from_byte
;
2914 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
2918 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2919 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2920 /* Don't allow comment-end to be quoted. */
2921 if (syntax_multibyte (c1
, multibyte_symbol_p
) == Sendcomment
)
2923 quoted
= char_quoted (from
- 1, temp_pos
);
2926 DEC_BOTH (from
, from_byte
);
2927 temp_pos
= dec_bytepos (temp_pos
);
2928 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2930 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2932 switch (syntax_multibyte (c1
, multibyte_symbol_p
))
2934 case Sword
: case Ssymbol
: case Squote
: break;
2935 default: goto done2
;
2937 DEC_BOTH (from
, from_byte
);
2946 temp_pos
= dec_bytepos (from_byte
);
2947 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2948 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (temp_pos
))
2949 DEC_BOTH (from
, from_byte
);
2959 if (!++depth
) goto done2
;
2964 if (!--depth
) goto done2
;
2965 if (depth
< min_depth
)
2966 xsignal3 (Qscan_error
,
2967 build_string ("Containing expression ends prematurely"),
2968 make_number (last_good
), make_number (from
));
2972 if (!parse_sexp_ignore_comments
)
2974 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2975 &out_charpos
, &out_bytepos
);
2976 /* FIXME: if !found, it really wasn't a comment-end.
2977 For single-char Sendcomment, we can't do much about it apart
2978 from skipping the char.
2979 For 2-char endcomments, we could try again, taking both
2980 chars as separate entities, but it's a lot of trouble
2981 for very little gain, so we don't bother either. -sm */
2983 from
= out_charpos
, from_byte
= out_bytepos
;
2986 case Scomment_fence
:
2992 DEC_BOTH (from
, from_byte
);
2993 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2994 if (!char_quoted (from
, from_byte
))
2996 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2997 if (syntax_multibyte (c
, multibyte_symbol_p
) == code
)
3001 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
3005 stringterm
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3010 DEC_BOTH (from
, from_byte
);
3011 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
3012 if (!char_quoted (from
, from_byte
))
3014 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3016 && (syntax_multibyte (c
, multibyte_symbol_p
)
3021 if (!depth
&& sexpflag
) goto done2
;
3024 /* Ignore whitespace, punctuation, quote, endcomment. */
3029 /* Reached start of buffer. Error if within object, return nil if between */
3042 XSETFASTINT (val
, from
);
3046 xsignal3 (Qscan_error
,
3047 build_string ("Unbalanced parentheses"),
3048 make_number (last_good
), make_number (from
));
3051 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
3052 doc
: /* Scan from character number FROM by COUNT lists.
3053 Scan forward if COUNT is positive, backward if COUNT is negative.
3054 Return the character number of the position thus found.
3056 A \"list", in this context, refers to a balanced parenthetical
3057 grouping, as determined by the syntax table.
3059 If DEPTH is nonzero, treat that as the nesting depth of the starting
3060 point (i.e. the starting point is DEPTH parentheses deep). This
3061 function scans over parentheses until the depth goes to zero COUNT
3062 times. Hence, positive DEPTH moves out that number of levels of
3063 parentheses, while negative DEPTH moves to a deeper level.
3065 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3067 If we reach the beginning or end of the accessible part of the buffer
3068 before we have scanned over COUNT lists, return nil if the depth at
3069 that point is zero, and signal a error if the depth is nonzero. */)
3070 (Lisp_Object from
, Lisp_Object count
, Lisp_Object depth
)
3072 CHECK_NUMBER (from
);
3073 CHECK_NUMBER (count
);
3074 CHECK_NUMBER (depth
);
3076 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
3079 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
3080 doc
: /* Scan from character number FROM by COUNT balanced expressions.
3081 If COUNT is negative, scan backwards.
3082 Returns the character number of the position thus found.
3084 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3086 If the beginning or end of (the accessible part of) the buffer is reached
3087 in the middle of a parenthetical grouping, an error is signaled.
3088 If the beginning or end is reached between groupings
3089 but before count is used up, nil is returned. */)
3090 (Lisp_Object from
, Lisp_Object count
)
3092 CHECK_NUMBER (from
);
3093 CHECK_NUMBER (count
);
3095 return scan_lists (XINT (from
), XINT (count
), 0, 1);
3098 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
3100 doc
: /* Move point backward over any number of chars with prefix syntax.
3101 This includes chars with expression prefix syntax class (\\=') and those with
3102 the prefix syntax flag (p). */)
3105 ptrdiff_t beg
= BEGV
;
3106 ptrdiff_t opoint
= PT
;
3107 ptrdiff_t opoint_byte
= PT_BYTE
;
3109 ptrdiff_t pos_byte
= PT_BYTE
;
3114 SET_PT_BOTH (opoint
, opoint_byte
);
3119 SETUP_SYNTAX_TABLE (pos
, -1);
3121 DEC_BOTH (pos
, pos_byte
);
3123 while (!char_quoted (pos
, pos_byte
)
3124 /* Previous statement updates syntax table. */
3125 && ((c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
), SYNTAX (c
) == Squote
)
3126 || syntax_prefix_flag_p (c
)))
3129 opoint_byte
= pos_byte
;
3133 DEC_BOTH (pos
, pos_byte
);
3136 SET_PT_BOTH (opoint
, opoint_byte
);
3141 /* Parse forward from FROM / FROM_BYTE to END,
3142 assuming that FROM has state STATE,
3143 and return a description of the state of the parse at END.
3144 If STOPBEFORE, stop at the start of an atom.
3145 If COMMENTSTOP is 1, stop at the start of a comment.
3146 If COMMENTSTOP is -1, stop at the start or end of a comment,
3147 after the beginning of a string, or after the end of a string. */
3150 scan_sexps_forward (struct lisp_parse_state
*state
,
3151 ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t end
,
3152 EMACS_INT targetdepth
, bool stopbefore
,
3155 enum syntaxcode code
;
3158 struct level
{ ptrdiff_t last
, prev
; };
3159 struct level levelstart
[100];
3160 struct level
*curlevel
= levelstart
;
3161 struct level
*endlevel
= levelstart
+ 100;
3162 EMACS_INT depth
; /* Paren depth of current scanning location.
3163 level - levelstart equals this except
3164 when the depth becomes negative. */
3165 EMACS_INT mindepth
; /* Lowest DEPTH value seen. */
3166 bool start_quoted
= 0; /* True means starting after a char quote. */
3168 ptrdiff_t prev_from
; /* Keep one character before FROM. */
3169 ptrdiff_t prev_from_byte
;
3170 int prev_from_syntax
, prev_prev_from_syntax
;
3171 bool boundary_stop
= commentstop
== -1;
3174 ptrdiff_t out_bytepos
, out_charpos
;
3178 prev_from_byte
= from_byte
;
3180 DEC_BOTH (prev_from
, prev_from_byte
);
3182 /* Use this macro instead of `from++'. */
3184 do { prev_from = from; \
3185 prev_from_byte = from_byte; \
3186 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
3187 prev_prev_from_syntax = prev_from_syntax; \
3188 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
3189 INC_BOTH (from, from_byte); \
3191 UPDATE_SYNTAX_TABLE_FORWARD (from); \
3197 depth
= state
->depth
;
3198 start_quoted
= state
->quoted
;
3199 prev_prev_from_syntax
= Smax
;
3200 prev_from_syntax
= state
->prev_syntax
;
3202 tem
= state
->levelstarts
;
3203 while (!NILP (tem
)) /* >= second enclosing sexps. */
3205 Lisp_Object temhd
= Fcar (tem
);
3206 if (RANGED_INTEGERP (PTRDIFF_MIN
, temhd
, PTRDIFF_MAX
))
3207 curlevel
->last
= XINT (temhd
);
3208 if (++curlevel
== endlevel
)
3209 curlevel
--; /* error ("Nesting too deep for parser"); */
3210 curlevel
->prev
= -1;
3211 curlevel
->last
= -1;
3214 curlevel
->prev
= -1;
3215 curlevel
->last
= -1;
3220 SETUP_SYNTAX_TABLE (from
, 1);
3222 /* Enter the loop at a place appropriate for initial state. */
3224 if (state
->incomment
)
3225 goto startincomment
;
3226 if (state
->instring
>= 0)
3228 nofence
= state
->instring
!= ST_STRING_STYLE
;
3230 goto startquotedinstring
;
3233 else if (start_quoted
)
3240 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
)
3241 && (c1
= FETCH_CHAR (from_byte
),
3242 syntax
= SYNTAX_WITH_FLAGS (c1
),
3243 SYNTAX_FLAGS_COMSTART_SECOND (syntax
)))
3244 /* Duplicate code to avoid a complex if-expression
3245 which causes trouble for the SGI compiler. */
3247 /* Record the comment style we have entered so that only
3248 the comment-end sequence of the same style actually
3249 terminates the comment section. */
3251 = SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_from_syntax
);
3252 comnested
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
)
3253 | SYNTAX_FLAGS_COMMENT_NESTED (syntax
));
3254 state
->incomment
= comnested
? 1 : -1;
3255 state
->comstr_start
= prev_from
;
3257 prev_from_syntax
= Smax
; /* the syntax has already been
3264 code
= prev_from_syntax
& 0xff;
3265 if (code
== Scomment_fence
)
3267 /* Record the comment style we have entered so that only
3268 the comment-end sequence of the same style actually
3269 terminates the comment section. */
3270 state
->comstyle
= ST_COMMENT_STYLE
;
3271 state
->incomment
= -1;
3272 state
->comstr_start
= prev_from
;
3275 else if (code
== Scomment
)
3277 state
->comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
, 0);
3278 state
->incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
3280 state
->comstr_start
= prev_from
;
3284 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
3290 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3291 curlevel
->last
= prev_from
;
3293 if (from
== end
) goto endquoted
;
3296 /* treat following character as a word constituent */
3299 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3300 curlevel
->last
= prev_from
;
3304 int symchar
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3305 switch (SYNTAX (symchar
))
3310 if (from
== end
) goto endquoted
;
3322 curlevel
->prev
= curlevel
->last
;
3325 case Scomment_fence
: /* Can't happen because it's handled above. */
3327 if (commentstop
|| boundary_stop
) goto done
;
3329 /* The (from == BEGV) test was to enter the loop in the middle so
3330 that we find a 2-char comment ender even if we start in the
3331 middle of it. We don't want to do that if we're just at the
3332 beginning of the comment (think of (*) ... (*)). */
3333 found
= forw_comment (from
, from_byte
, end
,
3334 state
->incomment
, state
->comstyle
,
3335 from
== BEGV
? 0 : prev_from_syntax
,
3336 &out_charpos
, &out_bytepos
, &state
->incomment
,
3338 from
= out_charpos
; from_byte
= out_bytepos
;
3339 /* Beware! prev_from and friends (except prev_from_syntax)
3340 are invalid now. Luckily, the `done' doesn't use them
3341 and the INC_FROM sets them to a sane value without
3343 if (!found
) goto done
;
3345 state
->incomment
= 0;
3346 state
->comstyle
= 0; /* reset the comment style */
3347 prev_from_syntax
= Smax
; /* For the comment closer */
3348 if (boundary_stop
) goto done
;
3352 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3354 /* curlevel++->last ran into compiler bug on Apollo */
3355 curlevel
->last
= prev_from
;
3356 if (++curlevel
== endlevel
)
3357 curlevel
--; /* error ("Nesting too deep for parser"); */
3358 curlevel
->prev
= -1;
3359 curlevel
->last
= -1;
3360 if (targetdepth
== depth
) goto done
;
3365 if (depth
< mindepth
)
3367 if (curlevel
!= levelstart
)
3369 curlevel
->prev
= curlevel
->last
;
3370 if (targetdepth
== depth
) goto done
;
3375 state
->comstr_start
= from
- 1;
3376 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3377 curlevel
->last
= prev_from
;
3378 state
->instring
= (code
== Sstring
3379 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte
))
3381 if (boundary_stop
) goto done
;
3384 nofence
= state
->instring
!= ST_STRING_STYLE
;
3389 enum syntaxcode c_code
;
3391 if (from
>= end
) goto done
;
3392 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3393 c_code
= SYNTAX (c
);
3395 /* Check C_CODE here so that if the char has
3396 a syntax-table property which says it is NOT
3397 a string character, it does not end the string. */
3398 if (nofence
&& c
== state
->instring
&& c_code
== Sstring
)
3404 if (!nofence
) goto string_end
;
3410 startquotedinstring
:
3411 if (from
>= end
) goto endquoted
;
3421 state
->instring
= -1;
3422 curlevel
->prev
= curlevel
->last
;
3424 if (boundary_stop
) goto done
;
3428 /* FIXME: We should do something with it. */
3431 /* Ignore whitespace, punctuation, quote, endcomment. */
3437 stop
: /* Here if stopping before start of sexp. */
3438 from
= prev_from
; /* We have just fetched the char that starts it; */
3439 from_byte
= prev_from_byte
;
3440 prev_from_syntax
= prev_prev_from_syntax
;
3441 goto done
; /* but return the position before it. */
3446 state
->depth
= depth
;
3447 state
->mindepth
= mindepth
;
3448 state
->thislevelstart
= curlevel
->prev
;
3449 state
->prevlevelstart
3450 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
3451 state
->location
= from
;
3452 state
->location_byte
= from_byte
;
3453 state
->levelstarts
= Qnil
;
3454 while (curlevel
> levelstart
)
3455 state
->levelstarts
= Fcons (make_number ((--curlevel
)->last
),
3456 state
->levelstarts
);
3457 state
->prev_syntax
= (SYNTAX_FLAGS_COMSTARTEND_FIRST (prev_from_syntax
)
3458 || state
->quoted
) ? prev_from_syntax
: Smax
;
3462 /* Convert a (lisp) parse state to the internal form used in
3463 scan_sexps_forward. */
3465 internalize_parse_state (Lisp_Object external
, struct lisp_parse_state
*state
)
3469 if (NILP (external
))
3472 state
->instring
= -1;
3473 state
->incomment
= 0;
3475 state
->comstyle
= 0; /* comment style a by default. */
3476 state
->comstr_start
= -1; /* no comment/string seen. */
3477 state
->levelstarts
= Qnil
;
3478 state
->prev_syntax
= Smax
;
3482 tem
= Fcar (external
);
3484 state
->depth
= XINT (tem
);
3488 external
= Fcdr (external
);
3489 external
= Fcdr (external
);
3490 external
= Fcdr (external
);
3491 tem
= Fcar (external
);
3492 /* Check whether we are inside string_fence-style string: */
3493 state
->instring
= (!NILP (tem
)
3494 ? (CHARACTERP (tem
) ? XFASTINT (tem
) : ST_STRING_STYLE
)
3497 external
= Fcdr (external
);
3498 tem
= Fcar (external
);
3499 state
->incomment
= (!NILP (tem
)
3500 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
3503 external
= Fcdr (external
);
3504 tem
= Fcar (external
);
3505 state
->quoted
= !NILP (tem
);
3507 /* if the eighth element of the list is nil, we are in comment
3508 style a. If it is non-nil, we are in comment style b */
3509 external
= Fcdr (external
);
3510 external
= Fcdr (external
);
3511 tem
= Fcar (external
);
3512 state
->comstyle
= (NILP (tem
)
3514 : (RANGED_INTEGERP (0, tem
, ST_COMMENT_STYLE
)
3516 : ST_COMMENT_STYLE
));
3518 external
= Fcdr (external
);
3519 tem
= Fcar (external
);
3520 state
->comstr_start
=
3521 RANGED_INTEGERP (PTRDIFF_MIN
, tem
, PTRDIFF_MAX
) ? XINT (tem
) : -1;
3522 external
= Fcdr (external
);
3523 tem
= Fcar (external
);
3524 state
->levelstarts
= tem
;
3526 external
= Fcdr (external
);
3527 tem
= Fcar (external
);
3528 state
->prev_syntax
= NILP (tem
) ? Smax
: XINT (tem
);
3532 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
3533 doc
: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3534 Parsing stops at TO or when certain criteria are met;
3535 point is set to where parsing stops.
3536 If fifth arg OLDSTATE is omitted or nil,
3537 parsing assumes that FROM is the beginning of a function.
3539 Value is a list of elements describing final state of parsing:
3541 1. character address of start of innermost containing list; nil if none.
3542 2. character address of start of last complete sexp terminated.
3543 3. non-nil if inside a string.
3544 (it is the character that will terminate the string,
3545 or t if the string should be terminated by a generic string delimiter.)
3546 4. nil if outside a comment, t if inside a non-nestable comment,
3547 else an integer (the current comment nesting).
3548 5. t if following a quote character.
3549 6. the minimum paren-depth encountered during this scan.
3550 7. style of comment, if any.
3551 8. character address of start of comment or string; nil if not in one.
3552 9. List of positions of currently open parens, outermost first.
3553 10. When the last position scanned holds the first character of a
3554 (potential) two character construct, the syntax of that position,
3555 otherwise nil. That construct can be a two character comment
3556 delimiter or an Escaped or Char-quoted character.
3557 11..... Possible further internal information used by `parse-partial-sexp'.
3559 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3560 in parentheses becomes equal to TARGETDEPTH.
3561 Fourth arg STOPBEFORE non-nil means stop when we come to
3562 any character that starts a sexp.
3563 Fifth arg OLDSTATE is a list like what this function returns.
3564 It is used to initialize the state of the parse. Elements number 1, 2, 6
3566 Sixth arg COMMENTSTOP non-nil means stop after the start of a comment.
3567 If it is the symbol `syntax-table', stop after the start of a comment or a
3568 string, or after end of a comment or a string. */)
3569 (Lisp_Object from
, Lisp_Object to
, Lisp_Object targetdepth
,
3570 Lisp_Object stopbefore
, Lisp_Object oldstate
, Lisp_Object commentstop
)
3572 struct lisp_parse_state state
;
3575 if (!NILP (targetdepth
))
3577 CHECK_NUMBER (targetdepth
);
3578 target
= XINT (targetdepth
);
3581 target
= TYPE_MINIMUM (EMACS_INT
); /* We won't reach this depth. */
3583 validate_region (&from
, &to
);
3584 internalize_parse_state (oldstate
, &state
);
3585 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
3587 target
, !NILP (stopbefore
),
3589 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
3591 SET_PT_BOTH (state
.location
, state
.location_byte
);
3594 Fcons (make_number (state
.depth
),
3595 Fcons (state
.prevlevelstart
< 0
3596 ? Qnil
: make_number (state
.prevlevelstart
),
3597 Fcons (state
.thislevelstart
< 0
3598 ? Qnil
: make_number (state
.thislevelstart
),
3599 Fcons (state
.instring
>= 0
3600 ? (state
.instring
== ST_STRING_STYLE
3601 ? Qt
: make_number (state
.instring
)) : Qnil
,
3602 Fcons (state
.incomment
< 0 ? Qt
:
3603 (state
.incomment
== 0 ? Qnil
:
3604 make_number (state
.incomment
)),
3605 Fcons (state
.quoted
? Qt
: Qnil
,
3606 Fcons (make_number (state
.mindepth
),
3607 Fcons ((state
.comstyle
3608 ? (state
.comstyle
== ST_COMMENT_STYLE
3610 : make_number (state
.comstyle
))
3612 Fcons (((state
.incomment
3613 || (state
.instring
>= 0))
3614 ? make_number (state
.comstr_start
)
3616 Fcons (state
.levelstarts
,
3617 Fcons (state
.prev_syntax
== Smax
3619 : make_number (state
.prev_syntax
),
3624 init_syntax_once (void)
3629 /* This has to be done here, before we call Fmake_char_table. */
3630 DEFSYM (Qsyntax_table
, "syntax-table");
3632 /* Create objects which can be shared among syntax tables. */
3633 Vsyntax_code_object
= make_uninit_vector (Smax
);
3634 for (i
= 0; i
< Smax
; i
++)
3635 ASET (Vsyntax_code_object
, i
, Fcons (make_number (i
), Qnil
));
3637 /* Now we are ready to set up this property, so we can
3638 create syntax tables. */
3639 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
3641 temp
= AREF (Vsyntax_code_object
, Swhitespace
);
3643 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
3645 /* Control characters should not be whitespace. */
3646 temp
= AREF (Vsyntax_code_object
, Spunct
);
3647 for (i
= 0; i
<= ' ' - 1; i
++)
3648 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3649 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 0177, temp
);
3651 /* Except that a few really are whitespace. */
3652 temp
= AREF (Vsyntax_code_object
, Swhitespace
);
3653 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ' ', temp
);
3654 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\t', temp
);
3655 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\n', temp
);
3656 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 015, temp
);
3657 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 014, temp
);
3659 temp
= AREF (Vsyntax_code_object
, Sword
);
3660 for (i
= 'a'; i
<= 'z'; i
++)
3661 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3662 for (i
= 'A'; i
<= 'Z'; i
++)
3663 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3664 for (i
= '0'; i
<= '9'; i
++)
3665 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3667 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
3668 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
3670 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
3671 Fcons (make_number (Sopen
), make_number (')')));
3672 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
3673 Fcons (make_number (Sclose
), make_number ('(')));
3674 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
3675 Fcons (make_number (Sopen
), make_number (']')));
3676 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
3677 Fcons (make_number (Sclose
), make_number ('[')));
3678 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
3679 Fcons (make_number (Sopen
), make_number ('}')));
3680 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
3681 Fcons (make_number (Sclose
), make_number ('{')));
3682 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
3683 Fcons (make_number (Sstring
), Qnil
));
3684 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
3685 Fcons (make_number (Sescape
), Qnil
));
3687 temp
= AREF (Vsyntax_code_object
, Ssymbol
);
3688 for (i
= 0; i
< 10; i
++)
3690 c
= "_-+*/&|<>="[i
];
3691 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3694 temp
= AREF (Vsyntax_code_object
, Spunct
);
3695 for (i
= 0; i
< 12; i
++)
3697 c
= ".,;:?!#@~^'`"[i
];
3698 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3701 /* All multibyte characters have syntax `word' by default. */
3702 temp
= AREF (Vsyntax_code_object
, Sword
);
3703 char_table_set_range (Vstandard_syntax_table
, 0x80, MAX_CHAR
, temp
);
3707 syms_of_syntax (void)
3709 DEFSYM (Qsyntax_table_p
, "syntax-table-p");
3711 staticpro (&Vsyntax_code_object
);
3713 staticpro (&gl_state
.object
);
3714 staticpro (&gl_state
.global_code
);
3715 staticpro (&gl_state
.current_syntax_table
);
3716 staticpro (&gl_state
.old_prop
);
3718 /* Defined in regex.c. */
3719 staticpro (&re_match_object
);
3721 DEFSYM (Qscan_error
, "scan-error");
3722 Fput (Qscan_error
, Qerror_conditions
,
3723 listn (CONSTYPE_PURE
, 2, Qscan_error
, Qerror
));
3724 Fput (Qscan_error
, Qerror_message
,
3725 build_pure_c_string ("Scan error"));
3727 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments
,
3728 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3730 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties
,
3731 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3732 Otherwise, that text property is simply ignored.
3733 See the info node `(elisp)Syntax Properties' for a description of the
3734 `syntax-table' property. */);
3736 DEFVAR_INT ("syntax-propertize--done", syntax_propertize__done
,
3737 doc
: /* Position up to which syntax-table properties have been set. */);
3738 syntax_propertize__done
= -1;
3739 DEFSYM (Qinternal__syntax_propertize
, "internal--syntax-propertize");
3740 Fmake_variable_buffer_local (intern ("syntax-propertize--done"));
3742 words_include_escapes
= 0;
3743 DEFVAR_BOOL ("words-include-escapes", words_include_escapes
,
3744 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3746 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol
,
3747 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3748 multibyte_syntax_as_symbol
= 0;
3750 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3751 open_paren_in_column_0_is_defun_start
,
3752 doc
: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3753 open_paren_in_column_0_is_defun_start
= 1;
3756 DEFVAR_LISP ("find-word-boundary-function-table",
3757 Vfind_word_boundary_function_table
,
3759 Char table of functions to search for the word boundary.
3760 Each function is called with two arguments; POS and LIMIT.
3761 POS and LIMIT are character positions in the current buffer.
3763 If POS is less than LIMIT, POS is at the first character of a word,
3764 and the return value of a function should be a position after the
3765 last character of that word.
3767 If POS is not less than LIMIT, POS is at the last character of a word,
3768 and the return value of a function should be a position at the first
3769 character of that word.
3771 In both cases, LIMIT bounds the search. */);
3772 Vfind_word_boundary_function_table
= Fmake_char_table (Qnil
, Qnil
);
3774 DEFVAR_BOOL ("comment-end-can-be-escaped", Vcomment_end_can_be_escaped
,
3775 doc
: /* Non-nil means an escaped ender inside a comment doesn't end the comment. */);
3776 Vcomment_end_can_be_escaped
= 0;
3777 DEFSYM (Qcomment_end_can_be_escaped
, "comment-end-can-be-escaped");
3778 Fmake_variable_buffer_local (Qcomment_end_can_be_escaped
);
3780 defsubr (&Ssyntax_table_p
);
3781 defsubr (&Ssyntax_table
);
3782 defsubr (&Sstandard_syntax_table
);
3783 defsubr (&Scopy_syntax_table
);
3784 defsubr (&Sset_syntax_table
);
3785 defsubr (&Schar_syntax
);
3786 defsubr (&Smatching_paren
);
3787 defsubr (&Sstring_to_syntax
);
3788 defsubr (&Smodify_syntax_entry
);
3789 defsubr (&Sinternal_describe_syntax_value
);
3791 defsubr (&Sforward_word
);
3793 defsubr (&Sskip_chars_forward
);
3794 defsubr (&Sskip_chars_backward
);
3795 defsubr (&Sskip_syntax_forward
);
3796 defsubr (&Sskip_syntax_backward
);
3798 defsubr (&Sforward_comment
);
3799 defsubr (&Sscan_lists
);
3800 defsubr (&Sscan_sexps
);
3801 defsubr (&Sbackward_prefix_chars
);
3802 defsubr (&Sparse_partial_sexp
);