1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2015 Free
3 Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
23 #include <sys/types.h>
27 #include "character.h"
33 #include "intervals.h"
36 /* Make syntax table lookup grant data in gl_state. */
37 #define SYNTAX(c) syntax_property (c, 1)
38 #define SYNTAX_ENTRY(c) syntax_property_entry (c, 1)
39 #define SYNTAX_WITH_FLAGS(c) syntax_property_with_flags (c, 1)
41 /* Eight single-bit flags have the following meanings:
42 1. This character is the first of a two-character comment-start sequence.
43 2. This character is the second of a two-character comment-start sequence.
44 3. This character is the first of a two-character comment-end sequence.
45 4. This character is the second of a two-character comment-end sequence.
46 5. This character is a prefix, for backward-prefix-chars.
47 6. The char is part of a delimiter for comments of style "b".
48 7. This character is part of a nestable comment sequence.
49 8. The char is part of a delimiter for comments of style "c".
50 Note that any two-character sequence whose first character has flag 1
51 and whose second character has flag 2 will be interpreted as a comment start.
53 Bits 6 and 8 discriminate among different comment styles.
54 Languages such as C++ allow two orthogonal syntax start/end pairs
55 and bit 6 determines whether a comment-end or Scommentend
56 ends style a or b. Comment markers can start style a, b, c, or bc.
57 Style a is always the default.
58 For 2-char comment markers, the style b flag is looked up only on the second
59 char of the comment marker and on the first char of the comment ender.
60 For style c (like the nested flag), the flag can be placed on any of
63 /* These functions extract specific flags from an integer
64 that holds the syntax code and the flags. */
67 SYNTAX_FLAGS_COMSTART_FIRST (int flags
)
69 return (flags
>> 16) & 1;
72 SYNTAX_FLAGS_COMSTART_SECOND (int flags
)
74 return (flags
>> 17) & 1;
77 SYNTAX_FLAGS_COMEND_FIRST (int flags
)
79 return (flags
>> 18) & 1;
82 SYNTAX_FLAGS_COMEND_SECOND (int flags
)
84 return (flags
>> 19) & 1;
87 SYNTAX_FLAGS_PREFIX (int flags
)
89 return (flags
>> 20) & 1;
92 SYNTAX_FLAGS_COMMENT_STYLEB (int flags
)
94 return (flags
>> 21) & 1;
97 SYNTAX_FLAGS_COMMENT_STYLEC (int flags
)
99 return (flags
>> 23) & 1;
102 SYNTAX_FLAGS_COMMENT_STYLEC2 (int flags
)
104 return (flags
>> 22) & 2; /* SYNTAX_FLAGS_COMMENT_STYLEC (flags) * 2 */
107 SYNTAX_FLAGS_COMMENT_NESTED (int flags
)
109 return (flags
>> 22) & 1;
112 /* FLAGS should be the flags of the main char of the comment marker, e.g.
113 the second for comstart and the first for comend. */
115 SYNTAX_FLAGS_COMMENT_STYLE (int flags
, int other_flags
)
117 return (SYNTAX_FLAGS_COMMENT_STYLEB (flags
)
118 | SYNTAX_FLAGS_COMMENT_STYLEC2 (flags
)
119 | SYNTAX_FLAGS_COMMENT_STYLEC2 (other_flags
));
122 /* Extract a particular flag for a given character. */
125 SYNTAX_COMEND_FIRST (int c
)
127 return SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c
));
130 /* We use these constants in place for comment-style and
131 string-ender-char to distinguish comments/strings started by
132 comment_fence and string_fence codes. */
136 ST_COMMENT_STYLE
= 256 + 1,
137 ST_STRING_STYLE
= 256 + 2
140 /* This is the internal form of the parse state used in parse-partial-sexp. */
142 struct lisp_parse_state
144 EMACS_INT depth
; /* Depth at end of parsing. */
145 int instring
; /* -1 if not within string, else desired terminator. */
146 EMACS_INT incomment
; /* -1 if in unnestable comment else comment nesting */
147 int comstyle
; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
148 bool quoted
; /* True if just after an escape char at end of parsing. */
149 EMACS_INT mindepth
; /* Minimum depth seen while scanning. */
150 /* Char number of most recent start-of-expression at current level */
151 ptrdiff_t thislevelstart
;
152 /* Char number of start of containing expression */
153 ptrdiff_t prevlevelstart
;
154 ptrdiff_t location
; /* Char number at which parsing stopped. */
155 ptrdiff_t location_byte
; /* Corresponding byte position. */
156 ptrdiff_t comstr_start
; /* Position of last comment/string starter. */
157 Lisp_Object levelstarts
; /* Char numbers of starts-of-expression
158 of levels (starting from outermost). */
161 /* These variables are a cache for finding the start of a defun.
162 find_start_pos is the place for which the defun start was found.
163 find_start_value is the defun start position found for it.
164 find_start_value_byte is the corresponding byte position.
165 find_start_buffer is the buffer it was found in.
166 find_start_begv is the BEGV value when it was found.
167 find_start_modiff is the value of MODIFF when it was found. */
169 static ptrdiff_t find_start_pos
;
170 static ptrdiff_t find_start_value
;
171 static ptrdiff_t find_start_value_byte
;
172 static struct buffer
*find_start_buffer
;
173 static ptrdiff_t find_start_begv
;
174 static EMACS_INT find_start_modiff
;
177 static Lisp_Object
skip_chars (bool, Lisp_Object
, Lisp_Object
, bool);
178 static Lisp_Object
skip_syntaxes (bool, Lisp_Object
, Lisp_Object
);
179 static Lisp_Object
scan_lists (EMACS_INT
, EMACS_INT
, EMACS_INT
, bool);
180 static void scan_sexps_forward (struct lisp_parse_state
*,
181 ptrdiff_t, ptrdiff_t, ptrdiff_t, EMACS_INT
,
182 bool, Lisp_Object
, int);
183 static bool in_classes (int, Lisp_Object
);
184 static void parse_sexp_propertize (ptrdiff_t charpos
);
186 /* This setter is used only in this file, so it can be private. */
188 bset_syntax_table (struct buffer
*b
, Lisp_Object val
)
190 b
->syntax_table_
= val
;
193 /* Whether the syntax of the character C has the prefix flag set. */
195 syntax_prefix_flag_p (int c
)
197 return SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c
));
200 struct gl_state_s gl_state
; /* Global state of syntax parser. */
202 enum { INTERVALS_AT_ONCE
= 10 }; /* 1 + max-number of intervals
203 to scan to property-change. */
205 /* Set the syntax entry VAL for char C in table TABLE. */
208 SET_RAW_SYNTAX_ENTRY (Lisp_Object table
, int c
, Lisp_Object val
)
210 CHAR_TABLE_SET (table
, c
, val
);
213 /* Set the syntax entry VAL for char-range RANGE in table TABLE.
214 RANGE is a cons (FROM . TO) specifying the range of characters. */
217 SET_RAW_SYNTAX_ENTRY_RANGE (Lisp_Object table
, Lisp_Object range
,
220 Fset_char_table_range (table
, range
, val
);
223 /* Extract the information from the entry for character C
224 in the current syntax table. */
229 Lisp_Object ent
= SYNTAX_ENTRY (c
);
230 return CONSP (ent
) ? XCDR (ent
) : Qnil
;
233 /* This should be called with FROM at the start of forward
234 search, or after the last position of the backward search. It
235 makes sure that the first char is picked up with correct table, so
236 one does not need to call UPDATE_SYNTAX_TABLE immediately after the
238 Sign of COUNT gives the direction of the search.
242 SETUP_SYNTAX_TABLE (ptrdiff_t from
, ptrdiff_t count
)
244 SETUP_BUFFER_SYNTAX_TABLE ();
245 gl_state
.b_property
= BEGV
;
246 gl_state
.e_property
= ZV
+ 1;
247 gl_state
.object
= Qnil
;
249 if (parse_sexp_lookup_properties
)
252 update_syntax_table_forward (from
, true, Qnil
);
253 else if (from
> BEGV
)
255 update_syntax_table (from
- 1, count
, true, Qnil
);
256 parse_sexp_propertize (from
- 1);
261 /* Same as above, but in OBJECT. If OBJECT is nil, use current buffer.
262 If it is t (which is only used in fast_c_string_match_ignore_case),
263 ignore properties altogether.
265 This is meant for regex.c to use. For buffers, regex.c passes arguments
266 to the UPDATE_SYNTAX_TABLE functions which are relative to BEGV.
267 So if it is a buffer, we set the offset field to BEGV. */
270 SETUP_SYNTAX_TABLE_FOR_OBJECT (Lisp_Object object
,
271 ptrdiff_t from
, ptrdiff_t count
)
273 SETUP_BUFFER_SYNTAX_TABLE ();
274 gl_state
.object
= object
;
275 if (BUFFERP (gl_state
.object
))
277 struct buffer
*buf
= XBUFFER (gl_state
.object
);
278 gl_state
.b_property
= 1;
279 gl_state
.e_property
= BUF_ZV (buf
) - BUF_BEGV (buf
) + 1;
280 gl_state
.offset
= BUF_BEGV (buf
) - 1;
282 else if (NILP (gl_state
.object
))
284 gl_state
.b_property
= 1;
285 gl_state
.e_property
= ZV
- BEGV
+ 1;
286 gl_state
.offset
= BEGV
- 1;
288 else if (EQ (gl_state
.object
, Qt
))
290 gl_state
.b_property
= 0;
291 gl_state
.e_property
= PTRDIFF_MAX
;
296 gl_state
.b_property
= 0;
297 gl_state
.e_property
= 1 + SCHARS (gl_state
.object
);
300 if (parse_sexp_lookup_properties
)
301 update_syntax_table (from
+ gl_state
.offset
- (count
<= 0),
302 count
, 1, gl_state
.object
);
305 /* Update gl_state to an appropriate interval which contains CHARPOS. The
306 sign of COUNT give the relative position of CHARPOS wrt the previously
307 valid interval. If INIT, only [be]_property fields of gl_state are
308 valid at start, the rest is filled basing on OBJECT.
310 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
311 direction than the intervals - or in an interval. We update the
312 current syntax-table basing on the property of this interval, and
313 update the interval to start further than CHARPOS - or be
314 NULL. We also update lim_property to be the next value of
315 charpos to call this subroutine again - or be before/after the
316 start/end of OBJECT. */
319 update_syntax_table (ptrdiff_t charpos
, EMACS_INT count
, bool init
,
322 Lisp_Object tmp_table
;
324 bool invalidate
= true;
329 gl_state
.old_prop
= Qnil
;
330 gl_state
.start
= gl_state
.b_property
;
331 gl_state
.stop
= gl_state
.e_property
;
332 i
= interval_of (charpos
, object
);
333 gl_state
.backward_i
= gl_state
.forward_i
= i
;
337 /* interval_of updates only ->position of the return value, so
338 update the parents manually to speed up update_interval. */
339 while (!NULL_PARENT (i
))
341 if (AM_RIGHT_CHILD (i
))
342 INTERVAL_PARENT (i
)->position
= i
->position
343 - LEFT_TOTAL_LENGTH (i
) + TOTAL_LENGTH (i
) /* right end */
344 - TOTAL_LENGTH (INTERVAL_PARENT (i
))
345 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i
));
347 INTERVAL_PARENT (i
)->position
= i
->position
- LEFT_TOTAL_LENGTH (i
)
349 i
= INTERVAL_PARENT (i
);
351 i
= gl_state
.forward_i
;
352 gl_state
.b_property
= i
->position
- gl_state
.offset
;
353 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
356 i
= count
> 0 ? gl_state
.forward_i
: gl_state
.backward_i
;
358 /* We are guaranteed to be called with CHARPOS either in i,
361 error ("Error in syntax_table logic for to-the-end intervals");
362 else if (charpos
< i
->position
) /* Move left. */
365 error ("Error in syntax_table logic for intervals <-");
366 /* Update the interval. */
367 i
= update_interval (i
, charpos
);
368 if (INTERVAL_LAST_POS (i
) != gl_state
.b_property
)
371 gl_state
.forward_i
= i
;
372 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
375 else if (charpos
>= INTERVAL_LAST_POS (i
)) /* Move right. */
378 error ("Error in syntax_table logic for intervals ->");
379 /* Update the interval. */
380 i
= update_interval (i
, charpos
);
381 if (i
->position
!= gl_state
.e_property
)
384 gl_state
.backward_i
= i
;
385 gl_state
.b_property
= i
->position
- gl_state
.offset
;
390 tmp_table
= textget (i
->plist
, Qsyntax_table
);
393 invalidate
= !EQ (tmp_table
, gl_state
.old_prop
); /* Need to invalidate? */
395 if (invalidate
) /* Did not get to adjacent interval. */
396 { /* with the same table => */
397 /* invalidate the old range. */
400 gl_state
.backward_i
= i
;
401 gl_state
.b_property
= i
->position
- gl_state
.offset
;
405 gl_state
.forward_i
= i
;
406 gl_state
.e_property
= INTERVAL_LAST_POS (i
) - gl_state
.offset
;
410 if (!EQ (tmp_table
, gl_state
.old_prop
))
412 gl_state
.current_syntax_table
= tmp_table
;
413 gl_state
.old_prop
= tmp_table
;
414 if (EQ (Fsyntax_table_p (tmp_table
), Qt
))
416 gl_state
.use_global
= 0;
418 else if (CONSP (tmp_table
))
420 gl_state
.use_global
= 1;
421 gl_state
.global_code
= tmp_table
;
425 gl_state
.use_global
= 0;
426 gl_state
.current_syntax_table
= BVAR (current_buffer
, syntax_table
);
432 if (cnt
&& !EQ (tmp_table
, textget (i
->plist
, Qsyntax_table
)))
436 gl_state
.e_property
= i
->position
- gl_state
.offset
;
437 gl_state
.forward_i
= i
;
442 = i
->position
+ LENGTH (i
) - gl_state
.offset
;
443 gl_state
.backward_i
= i
;
447 else if (cnt
== INTERVALS_AT_ONCE
)
452 = i
->position
+ LENGTH (i
) - gl_state
.offset
453 /* e_property at EOB is not set to ZV but to ZV+1, so that
454 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
455 having to check eob between the two. */
456 + (next_interval (i
) ? 0 : 1);
457 gl_state
.forward_i
= i
;
461 gl_state
.b_property
= i
->position
- gl_state
.offset
;
462 gl_state
.backward_i
= i
;
467 i
= count
> 0 ? next_interval (i
) : previous_interval (i
);
469 eassert (i
== NULL
); /* This property goes to the end. */
472 gl_state
.e_property
= gl_state
.stop
;
473 gl_state
.forward_i
= i
;
476 gl_state
.b_property
= gl_state
.start
;
480 parse_sexp_propertize (ptrdiff_t charpos
)
483 if (syntax_propertize__done
<= charpos
484 && syntax_propertize__done
< zv
)
486 EMACS_INT modiffs
= CHARS_MODIFF
;
487 safe_call1 (Qinternal__syntax_propertize
,
488 make_number (min (zv
, 1 + charpos
)));
489 if (modiffs
!= CHARS_MODIFF
)
490 error ("parse-sexp-propertize-function modified the buffer!");
491 if (syntax_propertize__done
<= charpos
492 && syntax_propertize__done
< zv
)
493 error ("parse-sexp-propertize-function did not move"
494 " syntax-propertize--done");
495 SETUP_SYNTAX_TABLE (charpos
, 1);
497 else if (gl_state
.e_property
> syntax_propertize__done
)
499 gl_state
.e_property
= syntax_propertize__done
;
500 gl_state
.e_property_truncated
= true;
502 else if (gl_state
.e_property_truncated
503 && gl_state
.e_property
< syntax_propertize__done
)
504 { /* When moving backward, e_property might be set without resetting
505 e_property_truncated, so the e_property_truncated flag may
506 occasionally be left raised spuriously. This should be rare. */
507 gl_state
.e_property_truncated
= false;
508 update_syntax_table_forward (charpos
, false, Qnil
);
513 update_syntax_table_forward (ptrdiff_t charpos
, bool init
,
516 if (gl_state
.e_property_truncated
)
518 eassert (NILP (object
));
519 eassert (charpos
>= gl_state
.e_property
);
520 parse_sexp_propertize (charpos
);
524 update_syntax_table (charpos
, 1, init
, object
);
525 if (gl_state
.e_property
> syntax_propertize__done
527 parse_sexp_propertize (charpos
);
531 /* Returns true if char at CHARPOS is quoted.
532 Global syntax-table data should be set up already to be good at CHARPOS
533 or after. On return global syntax data is good for lookup at CHARPOS. */
536 char_quoted (ptrdiff_t charpos
, ptrdiff_t bytepos
)
538 enum syntaxcode code
;
539 ptrdiff_t beg
= BEGV
;
541 ptrdiff_t orig
= charpos
;
543 while (charpos
> beg
)
546 DEC_BOTH (charpos
, bytepos
);
548 UPDATE_SYNTAX_TABLE_BACKWARD (charpos
);
549 c
= FETCH_CHAR_AS_MULTIBYTE (bytepos
);
551 if (! (code
== Scharquote
|| code
== Sescape
))
557 UPDATE_SYNTAX_TABLE (orig
);
561 /* Return the bytepos one character before BYTEPOS.
562 We assume that BYTEPOS is not at the start of the buffer. */
565 dec_bytepos (ptrdiff_t bytepos
)
567 if (NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
574 /* Return a defun-start position before POS and not too far before.
575 It should be the last one before POS, or nearly the last.
577 When open_paren_in_column_0_is_defun_start is nonzero,
578 only the beginning of the buffer is treated as a defun-start.
580 We record the information about where the scan started
581 and what its result was, so that another call in the same area
582 can return the same value very quickly.
584 There is no promise at which position the global syntax data is
585 valid on return from the subroutine, so the caller should explicitly
586 update the global data. */
589 find_defun_start (ptrdiff_t pos
, ptrdiff_t pos_byte
)
591 ptrdiff_t opoint
= PT
, opoint_byte
= PT_BYTE
;
593 /* Use previous finding, if it's valid and applies to this inquiry. */
594 if (current_buffer
== find_start_buffer
595 /* Reuse the defun-start even if POS is a little farther on.
596 POS might be in the next defun, but that's ok.
597 Our value may not be the best possible, but will still be usable. */
598 && pos
<= find_start_pos
+ 1000
599 && pos
>= find_start_value
600 && BEGV
== find_start_begv
601 && MODIFF
== find_start_modiff
)
602 return find_start_value
;
604 if (!open_paren_in_column_0_is_defun_start
)
606 find_start_value
= BEGV
;
607 find_start_value_byte
= BEGV_BYTE
;
611 /* Back up to start of line. */
612 scan_newline (pos
, pos_byte
, BEGV
, BEGV_BYTE
, -1, 1);
614 /* We optimize syntax-table lookup for rare updates. Thus we accept
615 only those `^\s(' which are good in global _and_ text-property
617 SETUP_BUFFER_SYNTAX_TABLE ();
622 /* Open-paren at start of line means we may have found our
624 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
625 if (SYNTAX (c
) == Sopen
)
627 SETUP_SYNTAX_TABLE (PT
+ 1, -1); /* Try again... */
628 c
= FETCH_CHAR_AS_MULTIBYTE (PT_BYTE
);
629 if (SYNTAX (c
) == Sopen
)
631 /* Now fallback to the default value. */
632 SETUP_BUFFER_SYNTAX_TABLE ();
634 /* Move to beg of previous line. */
635 scan_newline (PT
, PT_BYTE
, BEGV
, BEGV_BYTE
, -2, 1);
638 /* Record what we found, for the next try. */
639 find_start_value
= PT
;
640 find_start_value_byte
= PT_BYTE
;
641 TEMP_SET_PT_BOTH (opoint
, opoint_byte
);
644 find_start_buffer
= current_buffer
;
645 find_start_modiff
= MODIFF
;
646 find_start_begv
= BEGV
;
647 find_start_pos
= pos
;
649 return find_start_value
;
652 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
655 prev_char_comend_first (ptrdiff_t pos
, ptrdiff_t pos_byte
)
660 DEC_BOTH (pos
, pos_byte
);
661 UPDATE_SYNTAX_TABLE_BACKWARD (pos
);
662 c
= FETCH_CHAR (pos_byte
);
663 val
= SYNTAX_COMEND_FIRST (c
);
664 UPDATE_SYNTAX_TABLE_FORWARD (pos
+ 1);
668 /* Check whether charpos FROM is at the end of a comment.
669 FROM_BYTE is the bytepos corresponding to FROM.
670 Do not move back before STOP.
672 Return true if we find a comment ending at FROM/FROM_BYTE.
674 If successful, store the charpos of the comment's beginning
675 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
677 Global syntax data remains valid for backward search starting at
678 the returned value (or at FROM, if the search was not successful). */
681 back_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
,
682 bool comnested
, int comstyle
, ptrdiff_t *charpos_ptr
,
683 ptrdiff_t *bytepos_ptr
)
685 /* Look back, counting the parity of string-quotes,
686 and recording the comment-starters seen.
687 When we reach a safe place, assume that's not in a string;
688 then step the main scan to the earliest comment-starter seen
689 an even number of string quotes away from the safe place.
691 OFROM[I] is position of the earliest comment-starter seen
692 which is I+2X quotes from the comment-end.
693 PARITY is current parity of quotes from the comment end. */
694 int string_style
= -1; /* Presumed outside of any string. */
695 bool string_lossage
= 0;
696 /* Not a real lossage: indicates that we have passed a matching comment
697 starter plus a non-matching comment-ender, meaning that any matching
698 comment-starter we might see later could be a false positive (hidden
699 inside another comment).
700 Test case: { a (* b } c (* d *) */
701 bool comment_lossage
= 0;
702 ptrdiff_t comment_end
= from
;
703 ptrdiff_t comment_end_byte
= from_byte
;
704 ptrdiff_t comstart_pos
= 0;
705 ptrdiff_t comstart_byte
IF_LINT (= 0);
706 /* Place where the containing defun starts,
707 or 0 if we didn't come across it yet. */
708 ptrdiff_t defun_start
= 0;
709 ptrdiff_t defun_start_byte
= 0;
710 enum syntaxcode code
;
711 ptrdiff_t nesting
= 1; /* Current comment nesting. */
715 /* FIXME: A }} comment-ender style leads to incorrect behavior
716 in the case of {{ c }}} because we ignore the last two chars which are
717 assumed to be comment-enders although they aren't. */
719 /* At beginning of range to scan, we're outside of strings;
720 that determines quote parity to the comment-end. */
725 bool com2start
, com2end
, comstart
;
727 /* Move back and examine a character. */
728 DEC_BOTH (from
, from_byte
);
729 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
731 prev_syntax
= syntax
;
732 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
733 syntax
= SYNTAX_WITH_FLAGS (c
);
736 /* Check for 2-char comment markers. */
737 com2start
= (SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
738 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax
)
740 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax
, syntax
))
741 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
)
742 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
);
743 com2end
= (SYNTAX_FLAGS_COMEND_FIRST (syntax
)
744 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax
));
745 comstart
= (com2start
|| code
== Scomment
);
747 /* Nasty cases with overlapping 2-char comment markers:
748 - snmp-mode: -- c -- foo -- c --
756 /* If a 2-char comment sequence partly overlaps with another,
757 we don't try to be clever. E.g. |*| in C, or }% in modes that
758 have %..\n and %{..}%. */
759 if (from
> stop
&& (com2end
|| comstart
))
761 ptrdiff_t next
= from
, next_byte
= from_byte
;
762 int next_c
, next_syntax
;
763 DEC_BOTH (next
, next_byte
);
764 UPDATE_SYNTAX_TABLE_BACKWARD (next
);
765 next_c
= FETCH_CHAR_AS_MULTIBYTE (next_byte
);
766 next_syntax
= SYNTAX_WITH_FLAGS (next_c
);
767 if (((comstart
|| comnested
)
768 && SYNTAX_FLAGS_COMEND_SECOND (syntax
)
769 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax
))
770 || ((com2end
|| comnested
)
771 && SYNTAX_FLAGS_COMSTART_SECOND (syntax
)
773 == SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_syntax
))
774 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax
)))
776 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
779 if (com2start
&& comstart_pos
== 0)
780 /* We're looking at a comment starter. But it might be a comment
781 ender as well (see snmp-mode). The first time we see one, we
782 need to consider it as a comment starter,
783 and the subsequent times as a comment ender. */
786 /* Turn a 2-char comment sequences into the appropriate syntax. */
791 /* Ignore comment starters of a different style. */
792 else if (code
== Scomment
793 && (comstyle
!= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0)
794 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
) != comnested
))
797 /* Ignore escaped characters, except comment-enders. */
798 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
805 c
= (code
== Sstring_fence
? ST_STRING_STYLE
: ST_COMMENT_STYLE
);
807 /* Track parity of quotes. */
808 if (string_style
== -1)
809 /* Entering a string. */
811 else if (string_style
== c
)
812 /* Leaving the string. */
815 /* If we have two kinds of string delimiters.
816 There's no way to grok this scanning backwards. */
821 /* We've already checked that it is the relevant comstyle. */
822 if (string_style
!= -1 || comment_lossage
|| string_lossage
)
823 /* There are odd string quotes involved, so let's be careful.
824 Test case in Pascal: " { " a { " } */
829 /* Record best comment-starter so far. */
831 comstart_byte
= from_byte
;
833 else if (--nesting
<= 0)
834 /* nested comments have to be balanced, so we don't need to
835 keep looking for earlier ones. We use here the same (slightly
836 incorrect) reasoning as below: since it is followed by uniform
837 paired string quotes, this comment-start has to be outside of
838 strings, else the comment-end itself would be inside a string. */
843 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == comstyle
844 && ((com2end
&& SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax
))
845 || SYNTAX_FLAGS_COMMENT_NESTED (syntax
)) == comnested
)
846 /* This is the same style of comment ender as ours. */
851 /* Anything before that can't count because it would match
852 this comment-ender rather than ours. */
853 from
= stop
; /* Break out of the loop. */
855 else if (comstart_pos
!= 0 || c
!= '\n')
856 /* We're mixing comment styles here, so we'd better be careful.
857 The (comstart_pos != 0 || c != '\n') check is not quite correct
858 (we should just always set comment_lossage), but removing it
859 would imply that any multiline comment in C would go through
860 lossage, which seems overkill.
861 The failure should only happen in the rare cases such as
867 /* Assume a defun-start point is outside of strings. */
868 if (open_paren_in_column_0_is_defun_start
870 || (temp_byte
= dec_bytepos (from_byte
),
871 FETCH_CHAR (temp_byte
) == '\n')))
874 defun_start_byte
= from_byte
;
875 from
= stop
; /* Break out of the loop. */
884 if (comstart_pos
== 0)
887 from_byte
= comment_end_byte
;
888 UPDATE_SYNTAX_TABLE_FORWARD (comment_end
);
890 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
891 or `done'), then we've found the beginning of the non-nested comment. */
892 else if (1) /* !comnested */
895 from_byte
= comstart_byte
;
896 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
900 struct lisp_parse_state state
;
901 bool adjusted
= true;
902 /* We had two kinds of string delimiters mixed up
903 together. Decode this going forwards.
904 Scan fwd from a known safe place (beginning-of-defun)
905 to the one in question; this records where we
906 last passed a comment starter. */
907 /* If we did not already find the defun start, find it now. */
908 if (defun_start
== 0)
910 defun_start
= find_defun_start (comment_end
, comment_end_byte
);
911 defun_start_byte
= find_start_value_byte
;
912 adjusted
= (defun_start
> BEGV
);
916 scan_sexps_forward (&state
,
917 defun_start
, defun_start_byte
,
918 comment_end
, TYPE_MINIMUM (EMACS_INT
),
920 defun_start
= comment_end
;
925 = CONSP (state
.levelstarts
) ? XINT (XCAR (state
.levelstarts
))
926 : state
.thislevelstart
>= 0 ? state
.thislevelstart
928 find_start_value_byte
= CHAR_TO_BYTE (find_start_value
);
931 if (state
.incomment
== (comnested
? 1 : -1)
932 && state
.comstyle
== comstyle
)
933 from
= state
.comstr_start
;
938 /* If comment_end is inside some other comment, maybe ours
939 is nested, so we need to try again from within the
940 surrounding comment. Example: { a (* " *) */
942 /* FIXME: We should advance by one or two chars. */
943 defun_start
= state
.comstr_start
+ 2;
944 defun_start_byte
= CHAR_TO_BYTE (defun_start
);
947 } while (defun_start
< comment_end
);
949 from_byte
= CHAR_TO_BYTE (from
);
950 UPDATE_SYNTAX_TABLE_FORWARD (from
- 1);
955 *bytepos_ptr
= from_byte
;
957 return from
!= comment_end
;
960 DEFUN ("syntax-table-p", Fsyntax_table_p
, Ssyntax_table_p
, 1, 1, 0,
961 doc
: /* Return t if OBJECT is a syntax table.
962 Currently, any char-table counts as a syntax table. */)
965 if (CHAR_TABLE_P (object
)
966 && EQ (XCHAR_TABLE (object
)->purpose
, Qsyntax_table
))
972 check_syntax_table (Lisp_Object obj
)
974 CHECK_TYPE (CHAR_TABLE_P (obj
) && EQ (XCHAR_TABLE (obj
)->purpose
, Qsyntax_table
),
975 Qsyntax_table_p
, obj
);
978 DEFUN ("syntax-table", Fsyntax_table
, Ssyntax_table
, 0, 0, 0,
979 doc
: /* Return the current syntax table.
980 This is the one specified by the current buffer. */)
983 return BVAR (current_buffer
, syntax_table
);
986 DEFUN ("standard-syntax-table", Fstandard_syntax_table
,
987 Sstandard_syntax_table
, 0, 0, 0,
988 doc
: /* Return the standard syntax table.
989 This is the one used for new buffers. */)
992 return Vstandard_syntax_table
;
995 DEFUN ("copy-syntax-table", Fcopy_syntax_table
, Scopy_syntax_table
, 0, 1, 0,
996 doc
: /* Construct a new syntax table and return it.
997 It is a copy of the TABLE, which defaults to the standard syntax table. */)
1003 check_syntax_table (table
);
1005 table
= Vstandard_syntax_table
;
1007 copy
= Fcopy_sequence (table
);
1009 /* Only the standard syntax table should have a default element.
1010 Other syntax tables should inherit from parents instead. */
1011 set_char_table_defalt (copy
, Qnil
);
1013 /* Copied syntax tables should all have parents.
1014 If we copied one with no parent, such as the standard syntax table,
1015 use the standard syntax table as the copy's parent. */
1016 if (NILP (XCHAR_TABLE (copy
)->parent
))
1017 Fset_char_table_parent (copy
, Vstandard_syntax_table
);
1021 DEFUN ("set-syntax-table", Fset_syntax_table
, Sset_syntax_table
, 1, 1, 0,
1022 doc
: /* Select a new syntax table for the current buffer.
1023 One argument, a syntax table. */)
1027 check_syntax_table (table
);
1028 bset_syntax_table (current_buffer
, table
);
1029 /* Indicate that this buffer now has a specified syntax table. */
1030 idx
= PER_BUFFER_VAR_IDX (syntax_table
);
1031 SET_PER_BUFFER_VALUE_P (current_buffer
, idx
, 1);
1035 /* Convert a letter which signifies a syntax code
1036 into the code it signifies.
1037 This is used by modify-syntax-entry, and other things. */
1039 unsigned char const syntax_spec_code
[0400] =
1040 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1041 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1042 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1043 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1044 Swhitespace
, Scomment_fence
, Sstring
, 0377, Smath
, 0377, 0377, Squote
,
1045 Sopen
, Sclose
, 0377, 0377, 0377, Swhitespace
, Spunct
, Scharquote
,
1046 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1047 0377, 0377, 0377, 0377, Scomment
, 0377, Sendcomment
, 0377,
1048 Sinherit
, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
1049 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1050 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword
,
1051 0377, 0377, 0377, 0377, Sescape
, 0377, 0377, Ssymbol
,
1052 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
1053 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
1054 0377, 0377, 0377, 0377, 0377, 0377, 0377, Sword
,
1055 0377, 0377, 0377, 0377, Sstring_fence
, 0377, 0377, 0377
1058 /* Indexed by syntax code, give the letter that describes it. */
1060 char const syntax_code_spec
[16] =
1062 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
1066 /* Indexed by syntax code, give the object (cons of syntax code and
1067 nil) to be stored in syntax table. Since these objects can be
1068 shared among syntax tables, we generate them in advance. By
1069 sharing objects, the function `describe-syntax' can give a more
1071 static Lisp_Object Vsyntax_code_object
;
1074 DEFUN ("char-syntax", Fchar_syntax
, Schar_syntax
, 1, 1, 0,
1075 doc
: /* Return the syntax code of CHARACTER, described by a character.
1076 For example, if CHARACTER is a word constituent, the
1077 character `w' (119) is returned.
1078 The characters that correspond to various syntax codes
1079 are listed in the documentation of `modify-syntax-entry'. */)
1080 (Lisp_Object character
)
1083 CHECK_CHARACTER (character
);
1084 char_int
= XINT (character
);
1085 SETUP_BUFFER_SYNTAX_TABLE ();
1086 return make_number (syntax_code_spec
[SYNTAX (char_int
)]);
1089 DEFUN ("matching-paren", Fmatching_paren
, Smatching_paren
, 1, 1, 0,
1090 doc
: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
1091 (Lisp_Object character
)
1094 enum syntaxcode code
;
1095 CHECK_CHARACTER (character
);
1096 char_int
= XINT (character
);
1097 SETUP_BUFFER_SYNTAX_TABLE ();
1098 code
= SYNTAX (char_int
);
1099 if (code
== Sopen
|| code
== Sclose
)
1100 return SYNTAX_MATCH (char_int
);
1104 DEFUN ("string-to-syntax", Fstring_to_syntax
, Sstring_to_syntax
, 1, 1, 0,
1105 doc
: /* Convert a syntax descriptor STRING into a raw syntax descriptor.
1106 STRING should be a string of the form allowed as argument of
1107 `modify-syntax-entry'. The return value is a raw syntax descriptor: a
1108 cons cell (CODE . MATCHING-CHAR) which can be used, for example, as
1109 the value of a `syntax-table' text property. */)
1110 (Lisp_Object string
)
1112 const unsigned char *p
;
1116 CHECK_STRING (string
);
1119 val
= syntax_spec_code
[*p
++];
1121 error ("Invalid syntax description letter: %c", p
[-1]);
1123 if (val
== Sinherit
)
1129 int character
= STRING_CHAR_AND_LENGTH (p
, len
);
1130 XSETINT (match
, character
);
1131 if (XFASTINT (match
) == ' ')
1174 if (val
< ASIZE (Vsyntax_code_object
) && NILP (match
))
1175 return AREF (Vsyntax_code_object
, val
);
1177 /* Since we can't use a shared object, let's make a new one. */
1178 return Fcons (make_number (val
), match
);
1181 /* I really don't know why this is interactive
1182 help-form should at least be made useful whilst reading the second arg. */
1183 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry
, Smodify_syntax_entry
, 2, 3,
1184 "cSet syntax for character: \nsSet syntax for %s to: ",
1185 doc
: /* Set syntax for character CHAR according to string NEWENTRY.
1186 The syntax is changed only for table SYNTAX-TABLE, which defaults to
1187 the current buffer's syntax table.
1188 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
1189 in the range MIN to MAX are changed.
1190 The first character of NEWENTRY should be one of the following:
1191 Space or - whitespace syntax. w word constituent.
1192 _ symbol constituent. . punctuation.
1193 ( open-parenthesis. ) close-parenthesis.
1194 " string quote. \\ escape.
1195 $ paired delimiter. \\=' expression quote or prefix operator.
1196 < comment starter. > comment ender.
1197 / character-quote. @ inherit from parent table.
1198 | generic string fence. ! generic comment fence.
1200 Only single-character comment start and end sequences are represented thus.
1201 Two-character sequences are represented as described below.
1202 The second character of NEWENTRY is the matching parenthesis,
1203 used only if the first character is `(' or `)'.
1204 Any additional characters are flags.
1205 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
1206 1 means CHAR is the start of a two-char comment start sequence.
1207 2 means CHAR is the second character of such a sequence.
1208 3 means CHAR is the start of a two-char comment end sequence.
1209 4 means CHAR is the second character of such a sequence.
1211 There can be several orthogonal comment sequences. This is to support
1212 language modes such as C++. By default, all comment sequences are of style
1213 a, but you can set the comment sequence style to b (on the second character
1214 of a comment-start, and the first character of a comment-end sequence) and/or
1215 c (on any of its chars) using this flag:
1216 b means CHAR is part of comment sequence b.
1217 c means CHAR is part of comment sequence c.
1218 n means CHAR is part of a nestable comment sequence.
1220 p means CHAR is a prefix character for `backward-prefix-chars';
1221 such characters are treated as whitespace when they occur
1222 between expressions.
1223 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1224 (Lisp_Object c
, Lisp_Object newentry
, Lisp_Object syntax_table
)
1228 CHECK_CHARACTER_CAR (c
);
1229 CHECK_CHARACTER_CDR (c
);
1232 CHECK_CHARACTER (c
);
1234 if (NILP (syntax_table
))
1235 syntax_table
= BVAR (current_buffer
, syntax_table
);
1237 check_syntax_table (syntax_table
);
1239 newentry
= Fstring_to_syntax (newentry
);
1241 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table
, c
, newentry
);
1243 SET_RAW_SYNTAX_ENTRY (syntax_table
, XINT (c
), newentry
);
1245 /* We clear the regexp cache, since character classes can now have
1246 different values from those in the compiled regexps.*/
1247 clear_regexp_cache ();
1252 /* Dump syntax table to buffer in human-readable format */
1254 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value
,
1255 Sinternal_describe_syntax_value
, 1, 1, 0,
1256 doc
: /* Insert a description of the internal syntax description SYNTAX at point. */)
1257 (Lisp_Object syntax
)
1259 int code
, syntax_code
;
1260 bool start1
, start2
, end1
, end2
, prefix
, comstyleb
, comstylec
, comnested
;
1262 Lisp_Object first
, match_lisp
, value
= syntax
;
1266 insert_string ("default");
1270 if (CHAR_TABLE_P (value
))
1272 insert_string ("deeper char-table ...");
1278 insert_string ("invalid");
1282 first
= XCAR (value
);
1283 match_lisp
= XCDR (value
);
1285 if (!INTEGERP (first
) || !(NILP (match_lisp
) || CHARACTERP (match_lisp
)))
1287 insert_string ("invalid");
1291 syntax_code
= XINT (first
) & INT_MAX
;
1292 code
= syntax_code
& 0377;
1293 start1
= SYNTAX_FLAGS_COMSTART_FIRST (syntax_code
);
1294 start2
= SYNTAX_FLAGS_COMSTART_SECOND (syntax_code
);
1295 end1
= SYNTAX_FLAGS_COMEND_FIRST (syntax_code
);
1296 end2
= SYNTAX_FLAGS_COMEND_SECOND (syntax_code
);
1297 prefix
= SYNTAX_FLAGS_PREFIX (syntax_code
);
1298 comstyleb
= SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code
);
1299 comstylec
= SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code
);
1300 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax_code
);
1304 insert_string ("invalid");
1308 str
[0] = syntax_code_spec
[code
], str
[1] = 0;
1311 if (NILP (match_lisp
))
1314 insert_char (XINT (match_lisp
));
1335 insert_string ("\twhich means: ");
1340 insert_string ("whitespace"); break;
1342 insert_string ("punctuation"); break;
1344 insert_string ("word"); break;
1346 insert_string ("symbol"); break;
1348 insert_string ("open"); break;
1350 insert_string ("close"); break;
1352 insert_string ("prefix"); break;
1354 insert_string ("string"); break;
1356 insert_string ("math"); break;
1358 insert_string ("escape"); break;
1360 insert_string ("charquote"); break;
1362 insert_string ("comment"); break;
1364 insert_string ("endcomment"); break;
1366 insert_string ("inherit"); break;
1367 case Scomment_fence
:
1368 insert_string ("comment fence"); break;
1370 insert_string ("string fence"); break;
1372 insert_string ("invalid");
1376 if (!NILP (match_lisp
))
1378 insert_string (", matches ");
1379 insert_char (XINT (match_lisp
));
1383 insert_string (",\n\t is the first character of a comment-start sequence");
1385 insert_string (",\n\t is the second character of a comment-start sequence");
1388 insert_string (",\n\t is the first character of a comment-end sequence");
1390 insert_string (",\n\t is the second character of a comment-end sequence");
1392 insert_string (" (comment style b)");
1394 insert_string (" (comment style c)");
1396 insert_string (" (nestable)");
1400 AUTO_STRING (prefixdoc
,
1401 ",\n\t is a prefix character for `backward-prefix-chars'");
1402 insert1 (Fsubstitute_command_keys (prefixdoc
));
1408 /* Return the position across COUNT words from FROM.
1409 If that many words cannot be found before the end of the buffer, return 0.
1410 COUNT negative means scan backward and stop at word beginning. */
1413 scan_words (register ptrdiff_t from
, register EMACS_INT count
)
1415 register ptrdiff_t beg
= BEGV
;
1416 register ptrdiff_t end
= ZV
;
1417 register ptrdiff_t from_byte
= CHAR_TO_BYTE (from
);
1418 register enum syntaxcode code
;
1420 Lisp_Object func
, pos
;
1425 SETUP_SYNTAX_TABLE (from
, count
);
1436 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1437 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1438 code
= SYNTAX (ch0
);
1439 INC_BOTH (from
, from_byte
);
1440 if (words_include_escapes
1441 && (code
== Sescape
|| code
== Scharquote
))
1446 /* Now CH0 is a character which begins a word and FROM is the
1447 position of the next character. */
1448 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch0
);
1449 if (! NILP (Ffboundp (func
)))
1451 pos
= call2 (func
, make_number (from
- 1), make_number (end
));
1452 if (INTEGERP (pos
) && from
< XINT (pos
) && XINT (pos
) <= ZV
)
1455 from_byte
= CHAR_TO_BYTE (from
);
1462 if (from
== end
) break;
1463 UPDATE_SYNTAX_TABLE_FORWARD (from
);
1464 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1465 code
= SYNTAX (ch1
);
1467 && (! words_include_escapes
1468 || (code
!= Sescape
&& code
!= Scharquote
)))
1469 || word_boundary_p (ch0
, ch1
))
1471 INC_BOTH (from
, from_byte
);
1486 DEC_BOTH (from
, from_byte
);
1487 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1488 ch1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1489 code
= SYNTAX (ch1
);
1490 if (words_include_escapes
1491 && (code
== Sescape
|| code
== Scharquote
))
1496 /* Now CH1 is a character which ends a word and FROM is the
1498 func
= CHAR_TABLE_REF (Vfind_word_boundary_function_table
, ch1
);
1499 if (! NILP (Ffboundp (func
)))
1501 pos
= call2 (func
, make_number (from
), make_number (beg
));
1502 if (INTEGERP (pos
) && BEGV
<= XINT (pos
) && XINT (pos
) < from
)
1505 from_byte
= CHAR_TO_BYTE (from
);
1514 DEC_BOTH (from
, from_byte
);
1515 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
1516 ch0
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
1517 code
= SYNTAX (ch0
);
1519 && (! words_include_escapes
1520 || (code
!= Sescape
&& code
!= Scharquote
)))
1521 || word_boundary_p (ch0
, ch1
))
1523 INC_BOTH (from
, from_byte
);
1537 DEFUN ("forward-word", Fforward_word
, Sforward_word
, 0, 1, "^p",
1538 doc
: /* Move point forward ARG words (backward if ARG is negative).
1539 If ARG is omitted or nil, move point forward one word.
1541 If an edge of the buffer or a field boundary is reached, point is left there
1542 and the function returns nil. Field boundaries are not noticed if
1543 `inhibit-field-text-motion' is non-nil. */)
1547 ptrdiff_t orig_val
, val
;
1550 XSETFASTINT (arg
, 1);
1554 val
= orig_val
= scan_words (PT
, XINT (arg
));
1556 val
= XINT (arg
) > 0 ? ZV
: BEGV
;
1558 /* Avoid jumping out of an input field. */
1559 tmp
= Fconstrain_to_field (make_number (val
), make_number (PT
),
1561 val
= XFASTINT (tmp
);
1564 return val
== orig_val
? Qt
: Qnil
;
1567 DEFUN ("skip-chars-forward", Fskip_chars_forward
, Sskip_chars_forward
, 1, 2, 0,
1568 doc
: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1569 STRING is like the inside of a `[...]' in a regular expression
1570 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1571 (but not at the end of a range; quoting is never needed there).
1572 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1573 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1574 Char classes, e.g. `[:alpha:]', are supported.
1576 Returns the distance traveled, either zero or positive. */)
1577 (Lisp_Object string
, Lisp_Object lim
)
1579 return skip_chars (1, string
, lim
, 1);
1582 DEFUN ("skip-chars-backward", Fskip_chars_backward
, Sskip_chars_backward
, 1, 2, 0,
1583 doc
: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1584 See `skip-chars-forward' for details.
1585 Returns the distance traveled, either zero or negative. */)
1586 (Lisp_Object string
, Lisp_Object lim
)
1588 return skip_chars (0, string
, lim
, 1);
1591 DEFUN ("skip-syntax-forward", Fskip_syntax_forward
, Sskip_syntax_forward
, 1, 2, 0,
1592 doc
: /* Move point forward across chars in specified syntax classes.
1593 SYNTAX is a string of syntax code characters.
1594 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1595 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1596 This function returns the distance traveled, either zero or positive. */)
1597 (Lisp_Object syntax
, Lisp_Object lim
)
1599 return skip_syntaxes (1, syntax
, lim
);
1602 DEFUN ("skip-syntax-backward", Fskip_syntax_backward
, Sskip_syntax_backward
, 1, 2, 0,
1603 doc
: /* Move point backward across chars in specified syntax classes.
1604 SYNTAX is a string of syntax code characters.
1605 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1606 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1607 This function returns either zero or a negative number, and the absolute value
1608 of this is the distance traveled. */)
1609 (Lisp_Object syntax
, Lisp_Object lim
)
1611 return skip_syntaxes (0, syntax
, lim
);
1615 skip_chars (bool forwardp
, Lisp_Object string
, Lisp_Object lim
,
1616 bool handle_iso_classes
)
1620 /* Store the ranges of non-ASCII characters. */
1621 int *char_ranges
IF_LINT (= NULL
);
1622 int n_char_ranges
= 0;
1624 ptrdiff_t i
, i_byte
;
1625 /* True if the current buffer is multibyte and the region contains
1628 /* True if STRING is multibyte and it contains non-ASCII chars. */
1629 bool string_multibyte
;
1630 ptrdiff_t size_byte
;
1631 const unsigned char *str
;
1633 Lisp_Object iso_classes
;
1636 CHECK_STRING (string
);
1640 XSETINT (lim
, forwardp
? ZV
: BEGV
);
1642 CHECK_NUMBER_COERCE_MARKER (lim
);
1644 /* In any case, don't allow scan outside bounds of buffer. */
1645 if (XINT (lim
) > ZV
)
1646 XSETFASTINT (lim
, ZV
);
1647 if (XINT (lim
) < BEGV
)
1648 XSETFASTINT (lim
, BEGV
);
1650 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
1651 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
1652 string_multibyte
= SBYTES (string
) > SCHARS (string
);
1654 memset (fastmap
, 0, sizeof fastmap
);
1656 str
= SDATA (string
);
1657 size_byte
= SBYTES (string
);
1660 if (i_byte
< size_byte
1661 && SREF (string
, 0) == '^')
1663 negate
= 1; i_byte
++;
1666 /* Find the characters specified and set their elements of fastmap.
1667 Handle backslashes and ranges specially.
1669 If STRING contains non-ASCII characters, setup char_ranges for
1670 them and use fastmap only for their leading codes. */
1672 if (! string_multibyte
)
1674 bool string_has_eight_bit
= 0;
1676 /* At first setup fastmap. */
1677 while (i_byte
< size_byte
)
1681 if (handle_iso_classes
&& c
== '['
1682 && i_byte
< size_byte
1683 && str
[i_byte
] == ':')
1685 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1686 const unsigned char *class_end
= class_beg
;
1687 const unsigned char *class_limit
= str
+ size_byte
- 2;
1688 /* Leave room for the null. */
1689 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1692 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1693 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1695 while (class_end
< class_limit
1696 && *class_end
>= 'a' && *class_end
<= 'z')
1699 if (class_end
== class_beg
1700 || *class_end
!= ':' || class_end
[1] != ']')
1701 goto not_a_class_name
;
1703 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1704 class_name
[class_end
- class_beg
] = 0;
1706 cc
= re_wctype (class_name
);
1708 error ("Invalid ISO C character class");
1710 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1712 i_byte
= class_end
+ 2 - str
;
1719 if (i_byte
== size_byte
)
1724 /* Treat `-' as range character only if another character
1726 if (i_byte
+ 1 < size_byte
1727 && str
[i_byte
] == '-')
1731 /* Skip over the dash. */
1734 /* Get the end of the range. */
1737 && i_byte
< size_byte
)
1745 if (! ASCII_CHAR_P (c2
))
1746 string_has_eight_bit
= 1;
1752 if (! ASCII_CHAR_P (c
))
1753 string_has_eight_bit
= 1;
1757 /* If the current range is multibyte and STRING contains
1758 eight-bit chars, arrange fastmap and setup char_ranges for
1759 the corresponding multibyte chars. */
1760 if (multibyte
&& string_has_eight_bit
)
1763 char himap
[0200 + 1];
1764 memcpy (himap
, fastmap
+ 0200, 0200);
1766 memset (fastmap
+ 0200, 0, 0200);
1767 SAFE_NALLOCA (char_ranges
, 2, 128);
1770 while ((p1
= memchr (himap
+ i
, 1, 0200 - i
)))
1772 /* Deduce the next range C..C2 from the next clump of 1s
1773 in HIMAP starting with &HIMAP[I]. HIMAP is the high
1774 order half of the old FASTMAP. */
1775 int c2
, leading_code
;
1777 c
= BYTE8_TO_CHAR (i
+ 0200);
1779 c2
= BYTE8_TO_CHAR (i
+ 0200 - 1);
1781 char_ranges
[n_char_ranges
++] = c
;
1782 char_ranges
[n_char_ranges
++] = c2
;
1783 leading_code
= CHAR_LEADING_CODE (c
);
1784 memset (fastmap
+ leading_code
, 1,
1785 CHAR_LEADING_CODE (c2
) - leading_code
+ 1);
1789 else /* STRING is multibyte */
1791 SAFE_NALLOCA (char_ranges
, 2, SCHARS (string
));
1793 while (i_byte
< size_byte
)
1795 int leading_code
= str
[i_byte
];
1796 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1799 if (handle_iso_classes
&& c
== '['
1800 && i_byte
< size_byte
1801 && STRING_CHAR (str
+ i_byte
) == ':')
1803 const unsigned char *class_beg
= str
+ i_byte
+ 1;
1804 const unsigned char *class_end
= class_beg
;
1805 const unsigned char *class_limit
= str
+ size_byte
- 2;
1806 /* Leave room for the null. */
1807 unsigned char class_name
[CHAR_CLASS_MAX_LENGTH
+ 1];
1810 if (class_limit
- class_beg
> CHAR_CLASS_MAX_LENGTH
)
1811 class_limit
= class_beg
+ CHAR_CLASS_MAX_LENGTH
;
1813 while (class_end
< class_limit
1814 && *class_end
>= 'a' && *class_end
<= 'z')
1817 if (class_end
== class_beg
1818 || *class_end
!= ':' || class_end
[1] != ']')
1819 goto not_a_class_name_multibyte
;
1821 memcpy (class_name
, class_beg
, class_end
- class_beg
);
1822 class_name
[class_end
- class_beg
] = 0;
1824 cc
= re_wctype (class_name
);
1826 error ("Invalid ISO C character class");
1828 iso_classes
= Fcons (make_number (cc
), iso_classes
);
1830 i_byte
= class_end
+ 2 - str
;
1834 not_a_class_name_multibyte
:
1837 if (i_byte
== size_byte
)
1840 leading_code
= str
[i_byte
];
1841 c
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1844 /* Treat `-' as range character only if another character
1846 if (i_byte
+ 1 < size_byte
1847 && str
[i_byte
] == '-')
1849 int c2
, leading_code2
;
1851 /* Skip over the dash. */
1854 /* Get the end of the range. */
1855 leading_code2
= str
[i_byte
];
1856 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1860 && i_byte
< size_byte
)
1862 leading_code2
= str
[i_byte
];
1863 c2
= STRING_CHAR_AND_LENGTH (str
+ i_byte
, len
);
1869 if (ASCII_CHAR_P (c
))
1871 while (c
<= c2
&& c
< 0x80)
1873 leading_code
= CHAR_LEADING_CODE (c
);
1875 if (! ASCII_CHAR_P (c
))
1877 int lim2
= leading_code2
+ 1;
1878 while (leading_code
< lim2
)
1879 fastmap
[leading_code
++] = 1;
1882 char_ranges
[n_char_ranges
++] = c
;
1883 char_ranges
[n_char_ranges
++] = c2
;
1889 if (ASCII_CHAR_P (c
))
1893 fastmap
[leading_code
] = 1;
1894 char_ranges
[n_char_ranges
++] = c
;
1895 char_ranges
[n_char_ranges
++] = c
;
1900 /* If the current range is unibyte and STRING contains non-ASCII
1901 chars, arrange fastmap for the corresponding unibyte
1904 if (! multibyte
&& n_char_ranges
> 0)
1906 memset (fastmap
+ 0200, 0, 0200);
1907 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1909 int c1
= char_ranges
[i
];
1910 int lim2
= char_ranges
[i
+ 1] + 1;
1912 for (; c1
< lim2
; c1
++)
1914 int b
= CHAR_TO_BYTE_SAFE (c1
);
1922 /* If ^ was the first character, complement the fastmap. */
1926 for (i
= 0; i
< sizeof fastmap
; i
++)
1930 for (i
= 0; i
< 0200; i
++)
1932 /* All non-ASCII chars possibly match. */
1933 for (; i
< sizeof fastmap
; i
++)
1939 ptrdiff_t start_point
= PT
;
1941 ptrdiff_t pos_byte
= PT_BYTE
;
1942 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
1946 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
1947 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
1951 endp
= CHAR_POS_ADDR (XINT (lim
));
1952 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
1956 /* This code may look up syntax tables using functions that rely on the
1957 gl_state object. To make sure this object is not out of date,
1958 let's initialize it manually.
1959 We ignore syntax-table text-properties for now, since that's
1960 what we've done in the past. */
1961 SETUP_BUFFER_SYNTAX_TABLE ();
1976 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
1977 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
1987 if (! ASCII_CHAR_P (c
))
1989 /* As we are looking at a multibyte character, we
1990 must look up the character in the table
1991 CHAR_RANGES. If there's no data in the table,
1992 that character is not what we want to skip. */
1994 /* The following code do the right thing even if
1995 n_char_ranges is zero (i.e. no data in
1997 for (i
= 0; i
< n_char_ranges
; i
+= 2)
1998 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
2000 if (!(negate
^ (i
< n_char_ranges
)))
2004 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2017 if (!NILP (iso_classes
) && in_classes (*p
, iso_classes
))
2022 goto fwd_unibyte_ok
;
2028 p
++, pos
++, pos_byte
++;
2036 unsigned char *prev_p
;
2046 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2047 c
= STRING_CHAR (p
);
2049 if (! NILP (iso_classes
) && in_classes (c
, iso_classes
))
2059 if (! ASCII_CHAR_P (c
))
2061 /* See the comment in the previous similar code. */
2062 for (i
= 0; i
< n_char_ranges
; i
+= 2)
2063 if (c
>= char_ranges
[i
] && c
<= char_ranges
[i
+ 1])
2065 if (!(negate
^ (i
< n_char_ranges
)))
2069 pos
--, pos_byte
-= prev_p
- p
;
2082 if (! NILP (iso_classes
) && in_classes (p
[-1], iso_classes
))
2087 goto back_unibyte_ok
;
2090 if (!fastmap
[p
[-1]])
2093 p
--, pos
--, pos_byte
--;
2097 SET_PT_BOTH (pos
, pos_byte
);
2101 return make_number (PT
- start_point
);
2107 skip_syntaxes (bool forwardp
, Lisp_Object string
, Lisp_Object lim
)
2110 unsigned char fastmap
[0400];
2112 ptrdiff_t i
, i_byte
;
2114 ptrdiff_t size_byte
;
2117 CHECK_STRING (string
);
2120 XSETINT (lim
, forwardp
? ZV
: BEGV
);
2122 CHECK_NUMBER_COERCE_MARKER (lim
);
2124 /* In any case, don't allow scan outside bounds of buffer. */
2125 if (XINT (lim
) > ZV
)
2126 XSETFASTINT (lim
, ZV
);
2127 if (XINT (lim
) < BEGV
)
2128 XSETFASTINT (lim
, BEGV
);
2130 if (forwardp
? (PT
>= XFASTINT (lim
)) : (PT
<= XFASTINT (lim
)))
2131 return make_number (0);
2133 multibyte
= (!NILP (BVAR (current_buffer
, enable_multibyte_characters
))
2134 && (XINT (lim
) - PT
!= CHAR_TO_BYTE (XINT (lim
)) - PT_BYTE
));
2136 memset (fastmap
, 0, sizeof fastmap
);
2138 if (SBYTES (string
) > SCHARS (string
))
2139 /* As this is very rare case (syntax spec is ASCII only), don't
2140 consider efficiency. */
2141 string
= string_make_unibyte (string
);
2143 str
= SDATA (string
);
2144 size_byte
= SBYTES (string
);
2147 if (i_byte
< size_byte
2148 && SREF (string
, 0) == '^')
2150 negate
= 1; i_byte
++;
2153 /* Find the syntaxes specified and set their elements of fastmap. */
2155 while (i_byte
< size_byte
)
2158 fastmap
[syntax_spec_code
[c
]] = 1;
2161 /* If ^ was the first character, complement the fastmap. */
2163 for (i
= 0; i
< sizeof fastmap
; i
++)
2167 ptrdiff_t start_point
= PT
;
2169 ptrdiff_t pos_byte
= PT_BYTE
;
2170 unsigned char *p
= PT_ADDR
, *endp
, *stop
;
2174 endp
= (XINT (lim
) == GPT
) ? GPT_ADDR
: CHAR_POS_ADDR (XINT (lim
));
2175 stop
= (pos
< GPT
&& GPT
< XINT (lim
)) ? GPT_ADDR
: endp
;
2179 endp
= CHAR_POS_ADDR (XINT (lim
));
2180 stop
= (pos
>= GPT
&& GPT
> XINT (lim
)) ? GAP_END_ADDR
: endp
;
2184 SETUP_SYNTAX_TABLE (pos
, forwardp
? 1 : -1);
2200 c
= STRING_CHAR_AND_LENGTH (p
, nbytes
);
2201 if (! fastmap
[SYNTAX (c
)])
2203 p
+= nbytes
, pos
++, pos_byte
+= nbytes
;
2204 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2218 if (! fastmap
[SYNTAX (*p
)])
2220 p
++, pos
++, pos_byte
++;
2221 UPDATE_SYNTAX_TABLE_FORWARD (pos
);
2231 unsigned char *prev_p
;
2240 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2242 while (--p
>= stop
&& ! CHAR_HEAD_P (*p
));
2243 c
= STRING_CHAR (p
);
2244 if (! fastmap
[SYNTAX (c
)])
2246 pos
--, pos_byte
-= prev_p
- p
;
2260 UPDATE_SYNTAX_TABLE_BACKWARD (pos
- 1);
2261 if (! fastmap
[SYNTAX (p
[-1])])
2263 p
--, pos
--, pos_byte
--;
2268 SET_PT_BOTH (pos
, pos_byte
);
2271 return make_number (PT
- start_point
);
2275 /* Return true if character C belongs to one of the ISO classes
2276 in the list ISO_CLASSES. Each class is represented by an
2277 integer which is its type according to re_wctype. */
2280 in_classes (int c
, Lisp_Object iso_classes
)
2282 bool fits_class
= 0;
2284 while (CONSP (iso_classes
))
2287 elt
= XCAR (iso_classes
);
2288 iso_classes
= XCDR (iso_classes
);
2290 if (re_iswctype (c
, XFASTINT (elt
)))
2297 /* Jump over a comment, assuming we are at the beginning of one.
2298 FROM is the current position.
2299 FROM_BYTE is the bytepos corresponding to FROM.
2300 Do not move past STOP (a charpos).
2301 The comment over which we have to jump is of style STYLE
2302 (either SYNTAX_FLAGS_COMMENT_STYLE (foo) or ST_COMMENT_STYLE).
2303 NESTING should be positive to indicate the nesting at the beginning
2304 for nested comments and should be zero or negative else.
2305 ST_COMMENT_STYLE cannot be nested.
2306 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2307 (or 0 If the search cannot start in the middle of a two-character).
2309 If successful, return true and store the charpos of the comment's end
2310 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2311 Else, return false and store the charpos STOP into *CHARPOS_PTR, the
2312 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2313 (as defined for state.incomment) in *INCOMMENT_PTR.
2315 The comment end is the last character of the comment rather than the
2316 character just after the comment.
2318 Global syntax data is assumed to initially be valid for FROM and
2319 remains valid for forward search starting at the returned position. */
2322 forw_comment (ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t stop
,
2323 EMACS_INT nesting
, int style
, int prev_syntax
,
2324 ptrdiff_t *charpos_ptr
, ptrdiff_t *bytepos_ptr
,
2325 EMACS_INT
*incomment_ptr
)
2328 register enum syntaxcode code
;
2329 register int syntax
, other_syntax
;
2331 if (nesting
<= 0) nesting
= -1;
2333 /* Enter the loop in the middle so that we find
2334 a 2-char comment ender if we start in the middle of it. */
2335 syntax
= prev_syntax
;
2336 if (syntax
!= 0) goto forw_incomment
;
2342 *incomment_ptr
= nesting
;
2343 *charpos_ptr
= from
;
2344 *bytepos_ptr
= from_byte
;
2347 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2348 syntax
= SYNTAX_WITH_FLAGS (c
);
2349 code
= syntax
& 0xff;
2350 if (code
== Sendcomment
2351 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
2352 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ?
2353 (nesting
> 0 && --nesting
== 0) : nesting
< 0))
2354 /* We have encountered a comment end of the same style
2355 as the comment sequence which began this comment
2358 if (code
== Scomment_fence
2359 && style
== ST_COMMENT_STYLE
)
2360 /* We have encountered a comment end of the same style
2361 as the comment sequence which began this comment
2366 && SYNTAX_FLAGS_COMMENT_NESTED (syntax
)
2367 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0) == style
)
2368 /* We have encountered a nested comment of the same style
2369 as the comment sequence which began this comment section. */
2371 INC_BOTH (from
, from_byte
);
2372 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2375 if (from
< stop
&& SYNTAX_FLAGS_COMEND_FIRST (syntax
)
2376 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2377 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2378 SYNTAX_FLAGS_COMEND_SECOND (other_syntax
))
2379 && SYNTAX_FLAGS_COMMENT_STYLE (syntax
, other_syntax
) == style
2380 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2381 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
))
2382 ? nesting
> 0 : nesting
< 0))
2385 /* We have encountered a comment end of the same style
2386 as the comment sequence which began this comment section. */
2390 INC_BOTH (from
, from_byte
);
2391 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2396 && SYNTAX_FLAGS_COMSTART_FIRST (syntax
)
2397 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2398 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2399 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
) == style
2400 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2401 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax
) ||
2402 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
)))
2403 /* We have encountered a nested comment of the same style
2404 as the comment sequence which began this comment section. */
2406 INC_BOTH (from
, from_byte
);
2407 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2411 *charpos_ptr
= from
;
2412 *bytepos_ptr
= from_byte
;
2416 DEFUN ("forward-comment", Fforward_comment
, Sforward_comment
, 1, 1, 0,
2418 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2419 Stop scanning if we find something other than a comment or whitespace.
2420 Set point to where scanning stops.
2421 If COUNT comments are found as expected, with nothing except whitespace
2422 between them, return t; otherwise return nil. */)
2425 ptrdiff_t from
, from_byte
, stop
;
2427 enum syntaxcode code
;
2428 int comstyle
= 0; /* style of comment encountered */
2429 bool comnested
= 0; /* whether the comment is nestable or not */
2432 ptrdiff_t out_charpos
, out_bytepos
;
2435 CHECK_NUMBER (count
);
2436 count1
= XINT (count
);
2437 stop
= count1
> 0 ? ZV
: BEGV
;
2443 from_byte
= PT_BYTE
;
2445 SETUP_SYNTAX_TABLE (from
, count1
);
2450 bool comstart_first
;
2451 int syntax
, other_syntax
;
2455 SET_PT_BOTH (from
, from_byte
);
2459 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2460 syntax
= SYNTAX_WITH_FLAGS (c
);
2462 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2463 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2464 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2465 INC_BOTH (from
, from_byte
);
2466 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2467 if (from
< stop
&& comstart_first
2468 && (c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2469 other_syntax
= SYNTAX_WITH_FLAGS (c1
),
2470 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
)))
2472 /* We have encountered a comment start sequence and we
2473 are ignoring all text inside comments. We must record
2474 the comment style this sequence begins so that later,
2475 only a comment end of the same style actually ends
2476 the comment section. */
2478 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2479 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2480 INC_BOTH (from
, from_byte
);
2481 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2484 while (code
== Swhitespace
|| (code
== Sendcomment
&& c
== '\n'));
2486 if (code
== Scomment_fence
)
2487 comstyle
= ST_COMMENT_STYLE
;
2488 else if (code
!= Scomment
)
2491 DEC_BOTH (from
, from_byte
);
2492 SET_PT_BOTH (from
, from_byte
);
2495 /* We're at the start of a comment. */
2496 found
= forw_comment (from
, from_byte
, stop
, comnested
, comstyle
, 0,
2497 &out_charpos
, &out_bytepos
, &dummy
);
2498 from
= out_charpos
; from_byte
= out_bytepos
;
2502 SET_PT_BOTH (from
, from_byte
);
2505 INC_BOTH (from
, from_byte
);
2506 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2507 /* We have skipped one comment. */
2520 SET_PT_BOTH (BEGV
, BEGV_BYTE
);
2525 DEC_BOTH (from
, from_byte
);
2526 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2527 quoted
= char_quoted (from
, from_byte
);
2528 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2529 syntax
= SYNTAX_WITH_FLAGS (c
);
2532 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2533 if (code
== Sendcomment
)
2534 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2535 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2536 && prev_char_comend_first (from
, from_byte
)
2537 && !char_quoted (from
- 1, dec_bytepos (from_byte
)))
2540 /* We must record the comment style encountered so that
2541 later, we can match only the proper comment begin
2542 sequence of the same style. */
2543 DEC_BOTH (from
, from_byte
);
2545 /* Calling char_quoted, above, set up global syntax position
2546 at the new value of FROM. */
2547 c1
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2548 other_syntax
= SYNTAX_WITH_FLAGS (c1
);
2549 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2550 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2553 if (code
== Scomment_fence
)
2555 /* Skip until first preceding unquoted comment_fence. */
2556 bool fence_found
= 0;
2557 ptrdiff_t ini
= from
, ini_byte
= from_byte
;
2561 DEC_BOTH (from
, from_byte
);
2562 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2563 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2564 if (SYNTAX (c
) == Scomment_fence
2565 && !char_quoted (from
, from_byte
))
2570 else if (from
== stop
)
2573 if (fence_found
== 0)
2575 from
= ini
; /* Set point to ini + 1. */
2576 from_byte
= ini_byte
;
2580 /* We have skipped one comment. */
2583 else if (code
== Sendcomment
)
2585 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2586 &out_charpos
, &out_bytepos
);
2590 /* This end-of-line is not an end-of-comment.
2591 Treat it like a whitespace.
2592 CC-mode (and maybe others) relies on this behavior. */
2596 /* Failure: we should go back to the end of this
2597 not-quite-endcomment. */
2598 if (SYNTAX (c
) != code
)
2599 /* It was a two-char Sendcomment. */
2600 INC_BOTH (from
, from_byte
);
2606 /* We have skipped one comment. */
2607 from
= out_charpos
, from_byte
= out_bytepos
;
2611 else if (code
!= Swhitespace
|| quoted
)
2615 INC_BOTH (from
, from_byte
);
2616 SET_PT_BOTH (from
, from_byte
);
2624 SET_PT_BOTH (from
, from_byte
);
2629 /* Return syntax code of character C if C is an ASCII character
2630 or if MULTIBYTE_SYMBOL_P is false. Otherwise, return Ssymbol. */
2632 static enum syntaxcode
2633 syntax_multibyte (int c
, bool multibyte_symbol_p
)
2635 return ASCII_CHAR_P (c
) || !multibyte_symbol_p
? SYNTAX (c
) : Ssymbol
;
2639 scan_lists (EMACS_INT from
, EMACS_INT count
, EMACS_INT depth
, bool sexpflag
)
2642 ptrdiff_t stop
= count
> 0 ? ZV
: BEGV
;
2647 enum syntaxcode code
;
2648 EMACS_INT min_depth
= depth
; /* Err out if depth gets less than this. */
2649 int comstyle
= 0; /* Style of comment encountered. */
2650 bool comnested
= 0; /* Whether the comment is nestable or not. */
2652 EMACS_INT last_good
= from
;
2654 ptrdiff_t from_byte
;
2655 ptrdiff_t out_bytepos
, out_charpos
;
2657 bool multibyte_symbol_p
= sexpflag
&& multibyte_syntax_as_symbol
;
2659 if (depth
> 0) min_depth
= 0;
2661 if (from
> ZV
) from
= ZV
;
2662 if (from
< BEGV
) from
= BEGV
;
2664 from_byte
= CHAR_TO_BYTE (from
);
2669 SETUP_SYNTAX_TABLE (from
, count
);
2674 bool comstart_first
, prefix
;
2675 int syntax
, other_syntax
;
2676 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2677 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2678 syntax
= SYNTAX_WITH_FLAGS (c
);
2679 code
= syntax_multibyte (c
, multibyte_symbol_p
);
2680 comstart_first
= SYNTAX_FLAGS_COMSTART_FIRST (syntax
);
2681 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2682 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2683 prefix
= SYNTAX_FLAGS_PREFIX (syntax
);
2684 if (depth
== min_depth
)
2686 INC_BOTH (from
, from_byte
);
2687 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2688 if (from
< stop
&& comstart_first
2689 && (c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
),
2690 other_syntax
= SYNTAX_WITH_FLAGS (c
),
2691 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax
))
2692 && parse_sexp_ignore_comments
)
2694 /* We have encountered a comment start sequence and we
2695 are ignoring all text inside comments. We must record
2696 the comment style this sequence begins so that later,
2697 only a comment end of the same style actually ends
2698 the comment section. */
2700 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2701 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2702 INC_BOTH (from
, from_byte
);
2703 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2715 INC_BOTH (from
, from_byte
);
2716 /* Treat following character as a word constituent. */
2719 if (depth
|| !sexpflag
) break;
2720 /* This word counts as a sexp; return at end of it. */
2723 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2725 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2726 switch (syntax_multibyte (c
, multibyte_symbol_p
))
2730 INC_BOTH (from
, from_byte
);
2741 INC_BOTH (from
, from_byte
);
2745 case Scomment_fence
:
2746 comstyle
= ST_COMMENT_STYLE
;
2749 if (!parse_sexp_ignore_comments
) break;
2750 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2751 found
= forw_comment (from
, from_byte
, stop
,
2752 comnested
, comstyle
, 0,
2753 &out_charpos
, &out_bytepos
, &dummy
);
2754 from
= out_charpos
, from_byte
= out_bytepos
;
2761 INC_BOTH (from
, from_byte
);
2762 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2768 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (from_byte
))
2770 INC_BOTH (from
, from_byte
);
2780 if (!++depth
) goto done
;
2785 if (!--depth
) goto done
;
2786 if (depth
< min_depth
)
2787 xsignal3 (Qscan_error
,
2788 build_string ("Containing expression ends prematurely"),
2789 make_number (last_good
), make_number (from
));
2794 temp_pos
= dec_bytepos (from_byte
);
2795 stringterm
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2798 enum syntaxcode c_code
;
2801 UPDATE_SYNTAX_TABLE_FORWARD (from
);
2802 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2803 c_code
= syntax_multibyte (c
, multibyte_symbol_p
);
2805 ? c
== stringterm
&& c_code
== Sstring
2806 : c_code
== Sstring_fence
)
2809 if (c_code
== Scharquote
|| c_code
== Sescape
)
2810 INC_BOTH (from
, from_byte
);
2811 INC_BOTH (from
, from_byte
);
2813 INC_BOTH (from
, from_byte
);
2814 if (!depth
&& sexpflag
) goto done
;
2817 /* Ignore whitespace, punctuation, quote, endcomment. */
2822 /* Reached end of buffer. Error if within object, return nil if between */
2829 /* End of object reached */
2839 DEC_BOTH (from
, from_byte
);
2840 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2841 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2842 syntax
= SYNTAX_WITH_FLAGS (c
);
2843 code
= syntax_multibyte (c
, multibyte_symbol_p
);
2844 if (depth
== min_depth
)
2847 comnested
= SYNTAX_FLAGS_COMMENT_NESTED (syntax
);
2848 if (code
== Sendcomment
)
2849 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (syntax
, 0);
2850 if (from
> stop
&& SYNTAX_FLAGS_COMEND_SECOND (syntax
)
2851 && prev_char_comend_first (from
, from_byte
)
2852 && parse_sexp_ignore_comments
)
2854 /* We must record the comment style encountered so that
2855 later, we can match only the proper comment begin
2856 sequence of the same style. */
2857 int c2
, other_syntax
;
2858 DEC_BOTH (from
, from_byte
);
2859 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2861 c2
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2862 other_syntax
= SYNTAX_WITH_FLAGS (c2
);
2863 comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (other_syntax
, syntax
);
2864 comnested
|= SYNTAX_FLAGS_COMMENT_NESTED (other_syntax
);
2867 /* Quoting turns anything except a comment-ender
2868 into a word character. Note that this cannot be true
2869 if we decremented FROM in the if-statement above. */
2870 if (code
!= Sendcomment
&& char_quoted (from
, from_byte
))
2872 DEC_BOTH (from
, from_byte
);
2875 else if (SYNTAX_FLAGS_PREFIX (syntax
))
2884 if (depth
|| !sexpflag
) break;
2885 /* This word counts as a sexp; count object finished
2886 after passing it. */
2889 temp_pos
= from_byte
;
2890 if (! NILP (BVAR (current_buffer
, enable_multibyte_characters
)))
2894 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2895 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2896 /* Don't allow comment-end to be quoted. */
2897 if (syntax_multibyte (c1
, multibyte_symbol_p
) == Sendcomment
)
2899 quoted
= char_quoted (from
- 1, temp_pos
);
2902 DEC_BOTH (from
, from_byte
);
2903 temp_pos
= dec_bytepos (temp_pos
);
2904 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2906 c1
= FETCH_CHAR_AS_MULTIBYTE (temp_pos
);
2908 switch (syntax_multibyte (c1
, multibyte_symbol_p
))
2910 case Sword
: case Ssymbol
: case Squote
: break;
2911 default: goto done2
;
2913 DEC_BOTH (from
, from_byte
);
2922 temp_pos
= dec_bytepos (from_byte
);
2923 UPDATE_SYNTAX_TABLE_BACKWARD (from
- 1);
2924 if (from
!= stop
&& c
== FETCH_CHAR_AS_MULTIBYTE (temp_pos
))
2925 DEC_BOTH (from
, from_byte
);
2935 if (!++depth
) goto done2
;
2940 if (!--depth
) goto done2
;
2941 if (depth
< min_depth
)
2942 xsignal3 (Qscan_error
,
2943 build_string ("Containing expression ends prematurely"),
2944 make_number (last_good
), make_number (from
));
2948 if (!parse_sexp_ignore_comments
)
2950 found
= back_comment (from
, from_byte
, stop
, comnested
, comstyle
,
2951 &out_charpos
, &out_bytepos
);
2952 /* FIXME: if !found, it really wasn't a comment-end.
2953 For single-char Sendcomment, we can't do much about it apart
2954 from skipping the char.
2955 For 2-char endcomments, we could try again, taking both
2956 chars as separate entities, but it's a lot of trouble
2957 for very little gain, so we don't bother either. -sm */
2959 from
= out_charpos
, from_byte
= out_bytepos
;
2962 case Scomment_fence
:
2968 DEC_BOTH (from
, from_byte
);
2969 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2970 if (!char_quoted (from
, from_byte
))
2972 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2973 if (syntax_multibyte (c
, multibyte_symbol_p
) == code
)
2977 if (code
== Sstring_fence
&& !depth
&& sexpflag
) goto done2
;
2981 stringterm
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2986 DEC_BOTH (from
, from_byte
);
2987 UPDATE_SYNTAX_TABLE_BACKWARD (from
);
2988 if (!char_quoted (from
, from_byte
))
2990 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
2992 && (syntax_multibyte (c
, multibyte_symbol_p
)
2997 if (!depth
&& sexpflag
) goto done2
;
3000 /* Ignore whitespace, punctuation, quote, endcomment. */
3005 /* Reached start of buffer. Error if within object, return nil if between */
3018 XSETFASTINT (val
, from
);
3022 xsignal3 (Qscan_error
,
3023 build_string ("Unbalanced parentheses"),
3024 make_number (last_good
), make_number (from
));
3027 DEFUN ("scan-lists", Fscan_lists
, Sscan_lists
, 3, 3, 0,
3028 doc
: /* Scan from character number FROM by COUNT lists.
3029 Scan forward if COUNT is positive, backward if COUNT is negative.
3030 Return the character number of the position thus found.
3032 A \"list", in this context, refers to a balanced parenthetical
3033 grouping, as determined by the syntax table.
3035 If DEPTH is nonzero, treat that as the nesting depth of the starting
3036 point (i.e. the starting point is DEPTH parentheses deep). This
3037 function scans over parentheses until the depth goes to zero COUNT
3038 times. Hence, positive DEPTH moves out that number of levels of
3039 parentheses, while negative DEPTH moves to a deeper level.
3041 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3043 If we reach the beginning or end of the accessible part of the buffer
3044 before we have scanned over COUNT lists, return nil if the depth at
3045 that point is zero, and signal a error if the depth is nonzero. */)
3046 (Lisp_Object from
, Lisp_Object count
, Lisp_Object depth
)
3048 CHECK_NUMBER (from
);
3049 CHECK_NUMBER (count
);
3050 CHECK_NUMBER (depth
);
3052 return scan_lists (XINT (from
), XINT (count
), XINT (depth
), 0);
3055 DEFUN ("scan-sexps", Fscan_sexps
, Sscan_sexps
, 2, 2, 0,
3056 doc
: /* Scan from character number FROM by COUNT balanced expressions.
3057 If COUNT is negative, scan backwards.
3058 Returns the character number of the position thus found.
3060 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
3062 If the beginning or end of (the accessible part of) the buffer is reached
3063 in the middle of a parenthetical grouping, an error is signaled.
3064 If the beginning or end is reached between groupings
3065 but before count is used up, nil is returned. */)
3066 (Lisp_Object from
, Lisp_Object count
)
3068 CHECK_NUMBER (from
);
3069 CHECK_NUMBER (count
);
3071 return scan_lists (XINT (from
), XINT (count
), 0, 1);
3074 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars
, Sbackward_prefix_chars
,
3076 doc
: /* Move point backward over any number of chars with prefix syntax.
3077 This includes chars with expression prefix syntax class (') and those with
3078 the prefix syntax flag (p). */)
3081 ptrdiff_t beg
= BEGV
;
3082 ptrdiff_t opoint
= PT
;
3083 ptrdiff_t opoint_byte
= PT_BYTE
;
3085 ptrdiff_t pos_byte
= PT_BYTE
;
3090 SET_PT_BOTH (opoint
, opoint_byte
);
3095 SETUP_SYNTAX_TABLE (pos
, -1);
3097 DEC_BOTH (pos
, pos_byte
);
3099 while (!char_quoted (pos
, pos_byte
)
3100 /* Previous statement updates syntax table. */
3101 && ((c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
), SYNTAX (c
) == Squote
)
3102 || syntax_prefix_flag_p (c
)))
3105 opoint_byte
= pos_byte
;
3108 DEC_BOTH (pos
, pos_byte
);
3111 SET_PT_BOTH (opoint
, opoint_byte
);
3116 /* Parse forward from FROM / FROM_BYTE to END,
3117 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
3118 and return a description of the state of the parse at END.
3119 If STOPBEFORE, stop at the start of an atom.
3120 If COMMENTSTOP is 1, stop at the start of a comment.
3121 If COMMENTSTOP is -1, stop at the start or end of a comment,
3122 after the beginning of a string, or after the end of a string. */
3125 scan_sexps_forward (struct lisp_parse_state
*stateptr
,
3126 ptrdiff_t from
, ptrdiff_t from_byte
, ptrdiff_t end
,
3127 EMACS_INT targetdepth
, bool stopbefore
,
3128 Lisp_Object oldstate
, int commentstop
)
3130 struct lisp_parse_state state
;
3131 enum syntaxcode code
;
3134 struct level
{ ptrdiff_t last
, prev
; };
3135 struct level levelstart
[100];
3136 struct level
*curlevel
= levelstart
;
3137 struct level
*endlevel
= levelstart
+ 100;
3138 EMACS_INT depth
; /* Paren depth of current scanning location.
3139 level - levelstart equals this except
3140 when the depth becomes negative. */
3141 EMACS_INT mindepth
; /* Lowest DEPTH value seen. */
3142 bool start_quoted
= 0; /* True means starting after a char quote. */
3144 ptrdiff_t prev_from
; /* Keep one character before FROM. */
3145 ptrdiff_t prev_from_byte
;
3146 int prev_from_syntax
;
3147 bool boundary_stop
= commentstop
== -1;
3150 ptrdiff_t out_bytepos
, out_charpos
;
3154 prev_from_byte
= from_byte
;
3156 DEC_BOTH (prev_from
, prev_from_byte
);
3158 /* Use this macro instead of `from++'. */
3160 do { prev_from = from; \
3161 prev_from_byte = from_byte; \
3162 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
3163 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
3164 INC_BOTH (from, from_byte); \
3166 UPDATE_SYNTAX_TABLE_FORWARD (from); \
3172 if (NILP (oldstate
))
3175 state
.instring
= -1;
3176 state
.incomment
= 0;
3177 state
.comstyle
= 0; /* comment style a by default. */
3178 state
.comstr_start
= -1; /* no comment/string seen. */
3182 tem
= Fcar (oldstate
);
3188 oldstate
= Fcdr (oldstate
);
3189 oldstate
= Fcdr (oldstate
);
3190 oldstate
= Fcdr (oldstate
);
3191 tem
= Fcar (oldstate
);
3192 /* Check whether we are inside string_fence-style string: */
3193 state
.instring
= (!NILP (tem
)
3194 ? (CHARACTERP (tem
) ? XFASTINT (tem
) : ST_STRING_STYLE
)
3197 oldstate
= Fcdr (oldstate
);
3198 tem
= Fcar (oldstate
);
3199 state
.incomment
= (!NILP (tem
)
3200 ? (INTEGERP (tem
) ? XINT (tem
) : -1)
3203 oldstate
= Fcdr (oldstate
);
3204 tem
= Fcar (oldstate
);
3205 start_quoted
= !NILP (tem
);
3207 /* if the eighth element of the list is nil, we are in comment
3208 style a. If it is non-nil, we are in comment style b */
3209 oldstate
= Fcdr (oldstate
);
3210 oldstate
= Fcdr (oldstate
);
3211 tem
= Fcar (oldstate
);
3212 state
.comstyle
= (NILP (tem
)
3214 : (RANGED_INTEGERP (0, tem
, ST_COMMENT_STYLE
)
3216 : ST_COMMENT_STYLE
));
3218 oldstate
= Fcdr (oldstate
);
3219 tem
= Fcar (oldstate
);
3220 state
.comstr_start
=
3221 RANGED_INTEGERP (PTRDIFF_MIN
, tem
, PTRDIFF_MAX
) ? XINT (tem
) : -1;
3222 oldstate
= Fcdr (oldstate
);
3223 tem
= Fcar (oldstate
);
3224 while (!NILP (tem
)) /* >= second enclosing sexps. */
3226 Lisp_Object temhd
= Fcar (tem
);
3227 if (RANGED_INTEGERP (PTRDIFF_MIN
, temhd
, PTRDIFF_MAX
))
3228 curlevel
->last
= XINT (temhd
);
3229 if (++curlevel
== endlevel
)
3230 curlevel
--; /* error ("Nesting too deep for parser"); */
3231 curlevel
->prev
= -1;
3232 curlevel
->last
= -1;
3239 curlevel
->prev
= -1;
3240 curlevel
->last
= -1;
3242 SETUP_SYNTAX_TABLE (prev_from
, 1);
3243 temp
= FETCH_CHAR (prev_from_byte
);
3244 prev_from_syntax
= SYNTAX_WITH_FLAGS (temp
);
3245 UPDATE_SYNTAX_TABLE_FORWARD (from
);
3247 /* Enter the loop at a place appropriate for initial state. */
3249 if (state
.incomment
)
3250 goto startincomment
;
3251 if (state
.instring
>= 0)
3253 nofence
= state
.instring
!= ST_STRING_STYLE
;
3255 goto startquotedinstring
;
3258 else if (start_quoted
)
3265 code
= prev_from_syntax
& 0xff;
3268 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax
)
3269 && (c1
= FETCH_CHAR (from_byte
),
3270 syntax
= SYNTAX_WITH_FLAGS (c1
),
3271 SYNTAX_FLAGS_COMSTART_SECOND (syntax
)))
3272 /* Duplicate code to avoid a complex if-expression
3273 which causes trouble for the SGI compiler. */
3275 /* Record the comment style we have entered so that only
3276 the comment-end sequence of the same style actually
3277 terminates the comment section. */
3279 = SYNTAX_FLAGS_COMMENT_STYLE (syntax
, prev_from_syntax
);
3280 comnested
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
)
3281 | SYNTAX_FLAGS_COMMENT_NESTED (syntax
));
3282 state
.incomment
= comnested
? 1 : -1;
3283 state
.comstr_start
= prev_from
;
3287 else if (code
== Scomment_fence
)
3289 /* Record the comment style we have entered so that only
3290 the comment-end sequence of the same style actually
3291 terminates the comment section. */
3292 state
.comstyle
= ST_COMMENT_STYLE
;
3293 state
.incomment
= -1;
3294 state
.comstr_start
= prev_from
;
3297 else if (code
== Scomment
)
3299 state
.comstyle
= SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax
, 0);
3300 state
.incomment
= (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax
) ?
3302 state
.comstr_start
= prev_from
;
3305 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax
))
3311 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3312 curlevel
->last
= prev_from
;
3314 if (from
== end
) goto endquoted
;
3317 /* treat following character as a word constituent */
3320 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3321 curlevel
->last
= prev_from
;
3325 int symchar
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3326 switch (SYNTAX (symchar
))
3331 if (from
== end
) goto endquoted
;
3343 curlevel
->prev
= curlevel
->last
;
3346 case Scomment_fence
: /* Can't happen because it's handled above. */
3348 if (commentstop
|| boundary_stop
) goto done
;
3350 /* The (from == BEGV) test was to enter the loop in the middle so
3351 that we find a 2-char comment ender even if we start in the
3352 middle of it. We don't want to do that if we're just at the
3353 beginning of the comment (think of (*) ... (*)). */
3354 found
= forw_comment (from
, from_byte
, end
,
3355 state
.incomment
, state
.comstyle
,
3356 (from
== BEGV
|| from
< state
.comstr_start
+ 3)
3357 ? 0 : prev_from_syntax
,
3358 &out_charpos
, &out_bytepos
, &state
.incomment
);
3359 from
= out_charpos
; from_byte
= out_bytepos
;
3360 /* Beware! prev_from and friends are invalid now.
3361 Luckily, the `done' doesn't use them and the INC_FROM
3362 sets them to a sane value without looking at them. */
3363 if (!found
) goto done
;
3365 state
.incomment
= 0;
3366 state
.comstyle
= 0; /* reset the comment style */
3367 if (boundary_stop
) goto done
;
3371 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3373 /* curlevel++->last ran into compiler bug on Apollo */
3374 curlevel
->last
= prev_from
;
3375 if (++curlevel
== endlevel
)
3376 curlevel
--; /* error ("Nesting too deep for parser"); */
3377 curlevel
->prev
= -1;
3378 curlevel
->last
= -1;
3379 if (targetdepth
== depth
) goto done
;
3384 if (depth
< mindepth
)
3386 if (curlevel
!= levelstart
)
3388 curlevel
->prev
= curlevel
->last
;
3389 if (targetdepth
== depth
) goto done
;
3394 state
.comstr_start
= from
- 1;
3395 if (stopbefore
) goto stop
; /* this arg means stop at sexp start */
3396 curlevel
->last
= prev_from
;
3397 state
.instring
= (code
== Sstring
3398 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte
))
3400 if (boundary_stop
) goto done
;
3403 nofence
= state
.instring
!= ST_STRING_STYLE
;
3408 enum syntaxcode c_code
;
3410 if (from
>= end
) goto done
;
3411 c
= FETCH_CHAR_AS_MULTIBYTE (from_byte
);
3412 c_code
= SYNTAX (c
);
3414 /* Check C_CODE here so that if the char has
3415 a syntax-table property which says it is NOT
3416 a string character, it does not end the string. */
3417 if (nofence
&& c
== state
.instring
&& c_code
== Sstring
)
3423 if (!nofence
) goto string_end
;
3429 startquotedinstring
:
3430 if (from
>= end
) goto endquoted
;
3440 state
.instring
= -1;
3441 curlevel
->prev
= curlevel
->last
;
3443 if (boundary_stop
) goto done
;
3447 /* FIXME: We should do something with it. */
3450 /* Ignore whitespace, punctuation, quote, endcomment. */
3456 stop
: /* Here if stopping before start of sexp. */
3457 from
= prev_from
; /* We have just fetched the char that starts it; */
3458 from_byte
= prev_from_byte
;
3459 goto done
; /* but return the position before it. */
3464 state
.depth
= depth
;
3465 state
.mindepth
= mindepth
;
3466 state
.thislevelstart
= curlevel
->prev
;
3467 state
.prevlevelstart
3468 = (curlevel
== levelstart
) ? -1 : (curlevel
- 1)->last
;
3469 state
.location
= from
;
3470 state
.location_byte
= from_byte
;
3471 state
.levelstarts
= Qnil
;
3472 while (curlevel
> levelstart
)
3473 state
.levelstarts
= Fcons (make_number ((--curlevel
)->last
),
3480 DEFUN ("parse-partial-sexp", Fparse_partial_sexp
, Sparse_partial_sexp
, 2, 6, 0,
3481 doc
: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3482 Parsing stops at TO or when certain criteria are met;
3483 point is set to where parsing stops.
3484 If fifth arg OLDSTATE is omitted or nil,
3485 parsing assumes that FROM is the beginning of a function.
3486 Value is a list of elements describing final state of parsing:
3488 1. character address of start of innermost containing list; nil if none.
3489 2. character address of start of last complete sexp terminated.
3490 3. non-nil if inside a string.
3491 (it is the character that will terminate the string,
3492 or t if the string should be terminated by a generic string delimiter.)
3493 4. nil if outside a comment, t if inside a non-nestable comment,
3494 else an integer (the current comment nesting).
3495 5. t if following a quote character.
3496 6. the minimum paren-depth encountered during this scan.
3497 7. style of comment, if any.
3498 8. character address of start of comment or string; nil if not in one.
3499 9. Intermediate data for continuation of parsing (subject to change).
3500 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3501 in parentheses becomes equal to TARGETDEPTH.
3502 Fourth arg STOPBEFORE non-nil means stop when come to
3503 any character that starts a sexp.
3504 Fifth arg OLDSTATE is a list like what this function returns.
3505 It is used to initialize the state of the parse. Elements number 1, 2, 6
3507 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3508 If it is symbol `syntax-table', stop after the start of a comment or a
3509 string, or after end of a comment or a string. */)
3510 (Lisp_Object from
, Lisp_Object to
, Lisp_Object targetdepth
,
3511 Lisp_Object stopbefore
, Lisp_Object oldstate
, Lisp_Object commentstop
)
3513 struct lisp_parse_state state
;
3516 if (!NILP (targetdepth
))
3518 CHECK_NUMBER (targetdepth
);
3519 target
= XINT (targetdepth
);
3522 target
= TYPE_MINIMUM (EMACS_INT
); /* We won't reach this depth. */
3524 validate_region (&from
, &to
);
3525 scan_sexps_forward (&state
, XINT (from
), CHAR_TO_BYTE (XINT (from
)),
3527 target
, !NILP (stopbefore
), oldstate
,
3529 ? 0 : (EQ (commentstop
, Qsyntax_table
) ? -1 : 1)));
3531 SET_PT_BOTH (state
.location
, state
.location_byte
);
3533 return Fcons (make_number (state
.depth
),
3534 Fcons (state
.prevlevelstart
< 0
3535 ? Qnil
: make_number (state
.prevlevelstart
),
3536 Fcons (state
.thislevelstart
< 0
3537 ? Qnil
: make_number (state
.thislevelstart
),
3538 Fcons (state
.instring
>= 0
3539 ? (state
.instring
== ST_STRING_STYLE
3540 ? Qt
: make_number (state
.instring
)) : Qnil
,
3541 Fcons (state
.incomment
< 0 ? Qt
:
3542 (state
.incomment
== 0 ? Qnil
:
3543 make_number (state
.incomment
)),
3544 Fcons (state
.quoted
? Qt
: Qnil
,
3545 Fcons (make_number (state
.mindepth
),
3546 Fcons ((state
.comstyle
3547 ? (state
.comstyle
== ST_COMMENT_STYLE
3549 : make_number (state
.comstyle
))
3551 Fcons (((state
.incomment
3552 || (state
.instring
>= 0))
3553 ? make_number (state
.comstr_start
)
3555 Fcons (state
.levelstarts
, Qnil
))))))))));
3559 init_syntax_once (void)
3564 /* This has to be done here, before we call Fmake_char_table. */
3565 DEFSYM (Qsyntax_table
, "syntax-table");
3567 /* Create objects which can be shared among syntax tables. */
3568 Vsyntax_code_object
= make_uninit_vector (Smax
);
3569 for (i
= 0; i
< Smax
; i
++)
3570 ASET (Vsyntax_code_object
, i
, Fcons (make_number (i
), Qnil
));
3572 /* Now we are ready to set up this property, so we can
3573 create syntax tables. */
3574 Fput (Qsyntax_table
, Qchar_table_extra_slots
, make_number (0));
3576 temp
= AREF (Vsyntax_code_object
, Swhitespace
);
3578 Vstandard_syntax_table
= Fmake_char_table (Qsyntax_table
, temp
);
3580 /* Control characters should not be whitespace. */
3581 temp
= AREF (Vsyntax_code_object
, Spunct
);
3582 for (i
= 0; i
<= ' ' - 1; i
++)
3583 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3584 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 0177, temp
);
3586 /* Except that a few really are whitespace. */
3587 temp
= AREF (Vsyntax_code_object
, Swhitespace
);
3588 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ' ', temp
);
3589 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\t', temp
);
3590 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\n', temp
);
3591 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 015, temp
);
3592 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, 014, temp
);
3594 temp
= AREF (Vsyntax_code_object
, Sword
);
3595 for (i
= 'a'; i
<= 'z'; i
++)
3596 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3597 for (i
= 'A'; i
<= 'Z'; i
++)
3598 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3599 for (i
= '0'; i
<= '9'; i
++)
3600 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, i
, temp
);
3602 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '$', temp
);
3603 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '%', temp
);
3605 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '(',
3606 Fcons (make_number (Sopen
), make_number (')')));
3607 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ')',
3608 Fcons (make_number (Sclose
), make_number ('(')));
3609 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '[',
3610 Fcons (make_number (Sopen
), make_number (']')));
3611 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, ']',
3612 Fcons (make_number (Sclose
), make_number ('[')));
3613 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '{',
3614 Fcons (make_number (Sopen
), make_number ('}')));
3615 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '}',
3616 Fcons (make_number (Sclose
), make_number ('{')));
3617 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '"',
3618 Fcons (make_number (Sstring
), Qnil
));
3619 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, '\\',
3620 Fcons (make_number (Sescape
), Qnil
));
3622 temp
= AREF (Vsyntax_code_object
, Ssymbol
);
3623 for (i
= 0; i
< 10; i
++)
3625 c
= "_-+*/&|<>="[i
];
3626 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3629 temp
= AREF (Vsyntax_code_object
, Spunct
);
3630 for (i
= 0; i
< 12; i
++)
3632 c
= ".,;:?!#@~^'`"[i
];
3633 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table
, c
, temp
);
3636 /* All multibyte characters have syntax `word' by default. */
3637 temp
= AREF (Vsyntax_code_object
, Sword
);
3638 char_table_set_range (Vstandard_syntax_table
, 0x80, MAX_CHAR
, temp
);
3642 syms_of_syntax (void)
3644 DEFSYM (Qsyntax_table_p
, "syntax-table-p");
3646 staticpro (&Vsyntax_code_object
);
3648 staticpro (&gl_state
.object
);
3649 staticpro (&gl_state
.global_code
);
3650 staticpro (&gl_state
.current_syntax_table
);
3651 staticpro (&gl_state
.old_prop
);
3653 /* Defined in regex.c. */
3654 staticpro (&re_match_object
);
3656 DEFSYM (Qscan_error
, "scan-error");
3657 Fput (Qscan_error
, Qerror_conditions
,
3658 listn (CONSTYPE_PURE
, 2, Qscan_error
, Qerror
));
3659 Fput (Qscan_error
, Qerror_message
,
3660 build_pure_c_string ("Scan error"));
3662 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments
,
3663 doc
: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3665 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties
,
3666 doc
: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3667 Otherwise, that text property is simply ignored.
3668 See the info node `(elisp)Syntax Properties' for a description of the
3669 `syntax-table' property. */);
3671 DEFVAR_INT ("syntax-propertize--done", syntax_propertize__done
,
3672 doc
: /* Position up to which syntax-table properties have been set. */);
3673 syntax_propertize__done
= -1;
3674 DEFSYM (Qinternal__syntax_propertize
, "internal--syntax-propertize");
3676 words_include_escapes
= 0;
3677 DEFVAR_BOOL ("words-include-escapes", words_include_escapes
,
3678 doc
: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3680 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol
,
3681 doc
: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3682 multibyte_syntax_as_symbol
= 0;
3684 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3685 open_paren_in_column_0_is_defun_start
,
3686 doc
: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
3687 open_paren_in_column_0_is_defun_start
= 1;
3690 DEFVAR_LISP ("find-word-boundary-function-table",
3691 Vfind_word_boundary_function_table
,
3693 Char table of functions to search for the word boundary.
3694 Each function is called with two arguments; POS and LIMIT.
3695 POS and LIMIT are character positions in the current buffer.
3697 If POS is less than LIMIT, POS is at the first character of a word,
3698 and the return value of a function is a position after the last
3699 character of that word.
3701 If POS is not less than LIMIT, POS is at the last character of a word,
3702 and the return value of a function is a position at the first
3703 character of that word.
3705 In both cases, LIMIT bounds the search. */);
3706 Vfind_word_boundary_function_table
= Fmake_char_table (Qnil
, Qnil
);
3708 defsubr (&Ssyntax_table_p
);
3709 defsubr (&Ssyntax_table
);
3710 defsubr (&Sstandard_syntax_table
);
3711 defsubr (&Scopy_syntax_table
);
3712 defsubr (&Sset_syntax_table
);
3713 defsubr (&Schar_syntax
);
3714 defsubr (&Smatching_paren
);
3715 defsubr (&Sstring_to_syntax
);
3716 defsubr (&Smodify_syntax_entry
);
3717 defsubr (&Sinternal_describe_syntax_value
);
3719 defsubr (&Sforward_word
);
3721 defsubr (&Sskip_chars_forward
);
3722 defsubr (&Sskip_chars_backward
);
3723 defsubr (&Sskip_syntax_forward
);
3724 defsubr (&Sskip_syntax_backward
);
3726 defsubr (&Sforward_comment
);
3727 defsubr (&Sscan_lists
);
3728 defsubr (&Sscan_sexps
);
3729 defsubr (&Sbackward_prefix_chars
);
3730 defsubr (&Sparse_partial_sexp
);