* customize.texi (Composite Types): Move alist/plist from Simple Types (Bug#7545).
[emacs.git] / src / syntax.c
blobbfdf0e5ee6d6c65f412326cb3d426bf72d32530d
1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993, 1994, 1995, 1997, 1998, 1999, 2001,
3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
22 #include <config.h>
23 #include <ctype.h>
24 #include <setjmp.h>
25 #include "lisp.h"
26 #include "commands.h"
27 #include "buffer.h"
28 #include "character.h"
29 #include "keymap.h"
30 #include "regex.h"
32 /* Make syntax table lookup grant data in gl_state. */
33 #define SYNTAX_ENTRY_VIA_PROPERTY
35 #include "syntax.h"
36 #include "intervals.h"
38 /* We use these constants in place for comment-style and
39 string-ender-char to distinguish comments/strings started by
40 comment_fence and string_fence codes. */
42 #define ST_COMMENT_STYLE (256 + 1)
43 #define ST_STRING_STYLE (256 + 2)
44 #include "category.h"
46 Lisp_Object Qsyntax_table_p, Qsyntax_table, Qscan_error;
48 int words_include_escapes;
49 int parse_sexp_lookup_properties;
51 /* Nonzero means `scan-sexps' treat all multibyte characters as symbol. */
52 int multibyte_syntax_as_symbol;
54 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
55 if not compiled with GCC. No need to mark it, since it is used
56 only very temporarily. */
57 Lisp_Object syntax_temp;
59 /* Non-zero means an open parenthesis in column 0 is always considered
60 to be the start of a defun. Zero means an open parenthesis in
61 column 0 has no special meaning. */
63 int open_paren_in_column_0_is_defun_start;
65 /* This is the internal form of the parse state used in parse-partial-sexp. */
67 struct lisp_parse_state
69 int depth; /* Depth at end of parsing. */
70 int instring; /* -1 if not within string, else desired terminator. */
71 int incomment; /* -1 if in unnestable comment else comment nesting */
72 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
73 int quoted; /* Nonzero if just after an escape char at end of parsing */
74 int mindepth; /* Minimum depth seen while scanning. */
75 /* Char number of most recent start-of-expression at current level */
76 EMACS_INT thislevelstart;
77 /* Char number of start of containing expression */
78 EMACS_INT prevlevelstart;
79 EMACS_INT location; /* Char number at which parsing stopped. */
80 EMACS_INT comstr_start; /* Position of last comment/string starter. */
81 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
82 of levels (starting from outermost). */
85 /* These variables are a cache for finding the start of a defun.
86 find_start_pos is the place for which the defun start was found.
87 find_start_value is the defun start position found for it.
88 find_start_value_byte is the corresponding byte position.
89 find_start_buffer is the buffer it was found in.
90 find_start_begv is the BEGV value when it was found.
91 find_start_modiff is the value of MODIFF when it was found. */
93 static EMACS_INT find_start_pos;
94 static EMACS_INT find_start_value;
95 static EMACS_INT find_start_value_byte;
96 static struct buffer *find_start_buffer;
97 static EMACS_INT find_start_begv;
98 static int find_start_modiff;
101 static Lisp_Object skip_chars P_ ((int, Lisp_Object, Lisp_Object, int));
102 static Lisp_Object skip_syntaxes P_ ((int, Lisp_Object, Lisp_Object));
103 static Lisp_Object scan_lists P_ ((EMACS_INT, EMACS_INT, EMACS_INT, int));
104 static void scan_sexps_forward P_ ((struct lisp_parse_state *,
105 EMACS_INT, EMACS_INT, EMACS_INT, int,
106 int, Lisp_Object, int));
107 static int in_classes P_ ((int, Lisp_Object));
110 struct gl_state_s gl_state; /* Global state of syntax parser. */
112 INTERVAL interval_of ();
113 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
114 to scan to property-change. */
116 /* Update gl_state to an appropriate interval which contains CHARPOS. The
117 sign of COUNT give the relative position of CHARPOS wrt the previously
118 valid interval. If INIT, only [be]_property fields of gl_state are
119 valid at start, the rest is filled basing on OBJECT.
121 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
122 direction than the intervals - or in an interval. We update the
123 current syntax-table basing on the property of this interval, and
124 update the interval to start further than CHARPOS - or be
125 NULL_INTERVAL. We also update lim_property to be the next value of
126 charpos to call this subroutine again - or be before/after the
127 start/end of OBJECT. */
129 void
130 update_syntax_table (charpos, count, init, object)
131 int charpos, count, init;
132 Lisp_Object object;
134 Lisp_Object tmp_table;
135 int cnt = 0, invalidate = 1;
136 INTERVAL i;
138 if (init)
140 gl_state.old_prop = Qnil;
141 gl_state.start = gl_state.b_property;
142 gl_state.stop = gl_state.e_property;
143 i = interval_of (charpos, object);
144 gl_state.backward_i = gl_state.forward_i = i;
145 invalidate = 0;
146 if (NULL_INTERVAL_P (i))
147 return;
148 /* interval_of updates only ->position of the return value, so
149 update the parents manually to speed up update_interval. */
150 while (!NULL_PARENT (i))
152 if (AM_RIGHT_CHILD (i))
153 INTERVAL_PARENT (i)->position = i->position
154 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
155 - TOTAL_LENGTH (INTERVAL_PARENT (i))
156 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
157 else
158 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
159 + TOTAL_LENGTH (i);
160 i = INTERVAL_PARENT (i);
162 i = gl_state.forward_i;
163 gl_state.b_property = i->position - gl_state.offset;
164 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
165 goto update;
167 i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
169 /* We are guaranteed to be called with CHARPOS either in i,
170 or further off. */
171 if (NULL_INTERVAL_P (i))
172 error ("Error in syntax_table logic for to-the-end intervals");
173 else if (charpos < i->position) /* Move left. */
175 if (count > 0)
176 error ("Error in syntax_table logic for intervals <-");
177 /* Update the interval. */
178 i = update_interval (i, charpos);
179 if (INTERVAL_LAST_POS (i) != gl_state.b_property)
181 invalidate = 0;
182 gl_state.forward_i = i;
183 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
186 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
188 if (count < 0)
189 error ("Error in syntax_table logic for intervals ->");
190 /* Update the interval. */
191 i = update_interval (i, charpos);
192 if (i->position != gl_state.e_property)
194 invalidate = 0;
195 gl_state.backward_i = i;
196 gl_state.b_property = i->position - gl_state.offset;
200 update:
201 tmp_table = textget (i->plist, Qsyntax_table);
203 if (invalidate)
204 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
206 if (invalidate) /* Did not get to adjacent interval. */
207 { /* with the same table => */
208 /* invalidate the old range. */
209 if (count > 0)
211 gl_state.backward_i = i;
212 gl_state.b_property = i->position - gl_state.offset;
214 else
216 gl_state.forward_i = i;
217 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
221 if (!EQ (tmp_table, gl_state.old_prop))
223 gl_state.current_syntax_table = tmp_table;
224 gl_state.old_prop = tmp_table;
225 if (EQ (Fsyntax_table_p (tmp_table), Qt))
227 gl_state.use_global = 0;
229 else if (CONSP (tmp_table))
231 gl_state.use_global = 1;
232 gl_state.global_code = tmp_table;
234 else
236 gl_state.use_global = 0;
237 gl_state.current_syntax_table = current_buffer->syntax_table;
241 while (!NULL_INTERVAL_P (i))
243 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
245 if (count > 0)
247 gl_state.e_property = i->position - gl_state.offset;
248 gl_state.forward_i = i;
250 else
252 gl_state.b_property
253 = i->position + LENGTH (i) - gl_state.offset;
254 gl_state.backward_i = i;
256 return;
258 else if (cnt == INTERVALS_AT_ONCE)
260 if (count > 0)
262 gl_state.e_property
263 = i->position + LENGTH (i) - gl_state.offset
264 /* e_property at EOB is not set to ZV but to ZV+1, so that
265 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
266 having to check eob between the two. */
267 + (NULL_INTERVAL_P (next_interval (i)) ? 1 : 0);
268 gl_state.forward_i = i;
270 else
272 gl_state.b_property = i->position - gl_state.offset;
273 gl_state.backward_i = i;
275 return;
277 cnt++;
278 i = count > 0 ? next_interval (i) : previous_interval (i);
280 eassert (NULL_INTERVAL_P (i)); /* This property goes to the end. */
281 if (count > 0)
282 gl_state.e_property = gl_state.stop;
283 else
284 gl_state.b_property = gl_state.start;
287 /* Returns TRUE if char at CHARPOS is quoted.
288 Global syntax-table data should be set up already to be good at CHARPOS
289 or after. On return global syntax data is good for lookup at CHARPOS. */
291 static int
292 char_quoted (EMACS_INT charpos, EMACS_INT bytepos)
294 register enum syntaxcode code;
295 register EMACS_INT beg = BEGV;
296 register int quoted = 0;
297 EMACS_INT orig = charpos;
299 while (charpos > beg)
301 int c;
302 DEC_BOTH (charpos, bytepos);
304 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
305 c = FETCH_CHAR_AS_MULTIBYTE (bytepos);
306 code = SYNTAX (c);
307 if (! (code == Scharquote || code == Sescape))
308 break;
310 quoted = !quoted;
313 UPDATE_SYNTAX_TABLE (orig);
314 return quoted;
317 /* Return the bytepos one character after BYTEPOS.
318 We assume that BYTEPOS is not at the end of the buffer. */
320 INLINE EMACS_INT
321 inc_bytepos (bytepos)
322 EMACS_INT bytepos;
324 if (NILP (current_buffer->enable_multibyte_characters))
325 return bytepos + 1;
327 INC_POS (bytepos);
328 return bytepos;
331 /* Return the bytepos one character before BYTEPOS.
332 We assume that BYTEPOS is not at the start of the buffer. */
334 INLINE EMACS_INT
335 dec_bytepos (bytepos)
336 EMACS_INT bytepos;
338 if (NILP (current_buffer->enable_multibyte_characters))
339 return bytepos - 1;
341 DEC_POS (bytepos);
342 return bytepos;
345 /* Return a defun-start position before POS and not too far before.
346 It should be the last one before POS, or nearly the last.
348 When open_paren_in_column_0_is_defun_start is nonzero,
349 only the beginning of the buffer is treated as a defun-start.
351 We record the information about where the scan started
352 and what its result was, so that another call in the same area
353 can return the same value very quickly.
355 There is no promise at which position the global syntax data is
356 valid on return from the subroutine, so the caller should explicitly
357 update the global data. */
359 static EMACS_INT
360 find_defun_start (pos, pos_byte)
361 EMACS_INT pos, pos_byte;
363 EMACS_INT opoint = PT, opoint_byte = PT_BYTE;
365 if (!open_paren_in_column_0_is_defun_start)
367 find_start_value_byte = BEGV_BYTE;
368 return BEGV;
371 /* Use previous finding, if it's valid and applies to this inquiry. */
372 if (current_buffer == find_start_buffer
373 /* Reuse the defun-start even if POS is a little farther on.
374 POS might be in the next defun, but that's ok.
375 Our value may not be the best possible, but will still be usable. */
376 && pos <= find_start_pos + 1000
377 && pos >= find_start_value
378 && BEGV == find_start_begv
379 && MODIFF == find_start_modiff)
380 return find_start_value;
382 /* Back up to start of line. */
383 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
385 /* We optimize syntax-table lookup for rare updates. Thus we accept
386 only those `^\s(' which are good in global _and_ text-property
387 syntax-tables. */
388 gl_state.current_syntax_table = current_buffer->syntax_table;
389 gl_state.use_global = 0;
390 while (PT > BEGV)
392 int c;
394 /* Open-paren at start of line means we may have found our
395 defun-start. */
396 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
397 if (SYNTAX (c) == Sopen)
399 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
400 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
401 if (SYNTAX (c) == Sopen)
402 break;
403 /* Now fallback to the default value. */
404 gl_state.current_syntax_table = current_buffer->syntax_table;
405 gl_state.use_global = 0;
407 /* Move to beg of previous line. */
408 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
411 /* Record what we found, for the next try. */
412 find_start_value = PT;
413 find_start_value_byte = PT_BYTE;
414 find_start_buffer = current_buffer;
415 find_start_modiff = MODIFF;
416 find_start_begv = BEGV;
417 find_start_pos = pos;
419 TEMP_SET_PT_BOTH (opoint, opoint_byte);
421 return find_start_value;
424 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
426 static int
427 prev_char_comend_first (pos, pos_byte)
428 int pos, pos_byte;
430 int c, val;
432 DEC_BOTH (pos, pos_byte);
433 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
434 c = FETCH_CHAR (pos_byte);
435 val = SYNTAX_COMEND_FIRST (c);
436 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
437 return val;
440 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
442 /* static int
443 * prev_char_comstart_first (pos, pos_byte)
444 * int pos, pos_byte;
446 * int c, val;
448 * DEC_BOTH (pos, pos_byte);
449 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
450 * c = FETCH_CHAR (pos_byte);
451 * val = SYNTAX_COMSTART_FIRST (c);
452 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
453 * return val;
454 * } */
456 /* Checks whether charpos FROM is at the end of a comment.
457 FROM_BYTE is the bytepos corresponding to FROM.
458 Do not move back before STOP.
460 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
461 return -1 otherwise.
463 If successful, store the charpos of the comment's beginning
464 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
466 Global syntax data remains valid for backward search starting at
467 the returned value (or at FROM, if the search was not successful). */
469 static int
470 back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_ptr)
471 EMACS_INT from, from_byte, stop;
472 int comnested, comstyle;
473 EMACS_INT *charpos_ptr, *bytepos_ptr;
475 /* Look back, counting the parity of string-quotes,
476 and recording the comment-starters seen.
477 When we reach a safe place, assume that's not in a string;
478 then step the main scan to the earliest comment-starter seen
479 an even number of string quotes away from the safe place.
481 OFROM[I] is position of the earliest comment-starter seen
482 which is I+2X quotes from the comment-end.
483 PARITY is current parity of quotes from the comment end. */
484 int string_style = -1; /* Presumed outside of any string. */
485 int string_lossage = 0;
486 /* Not a real lossage: indicates that we have passed a matching comment
487 starter plus a non-matching comment-ender, meaning that any matching
488 comment-starter we might see later could be a false positive (hidden
489 inside another comment).
490 Test case: { a (* b } c (* d *) */
491 int comment_lossage = 0;
492 EMACS_INT comment_end = from;
493 EMACS_INT comment_end_byte = from_byte;
494 EMACS_INT comstart_pos = 0;
495 EMACS_INT comstart_byte;
496 /* Place where the containing defun starts,
497 or 0 if we didn't come across it yet. */
498 EMACS_INT defun_start = 0;
499 EMACS_INT defun_start_byte = 0;
500 register enum syntaxcode code;
501 int nesting = 1; /* current comment nesting */
502 int c;
503 int syntax = 0;
505 /* FIXME: A }} comment-ender style leads to incorrect behavior
506 in the case of {{ c }}} because we ignore the last two chars which are
507 assumed to be comment-enders although they aren't. */
509 /* At beginning of range to scan, we're outside of strings;
510 that determines quote parity to the comment-end. */
511 while (from != stop)
513 int temp_byte, prev_syntax;
514 int com2start, com2end;
515 int comstart;
517 /* Move back and examine a character. */
518 DEC_BOTH (from, from_byte);
519 UPDATE_SYNTAX_TABLE_BACKWARD (from);
521 prev_syntax = syntax;
522 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
523 syntax = SYNTAX_WITH_FLAGS (c);
524 code = SYNTAX (c);
526 /* Check for 2-char comment markers. */
527 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
528 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
529 && comstyle == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax)
530 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
531 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
532 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
533 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
534 comstart = (com2start || code == Scomment);
536 /* Nasty cases with overlapping 2-char comment markers:
537 - snmp-mode: -- c -- foo -- c --
538 --- c --
539 ------ c --
540 - c-mode: *||*
541 |* *|* *|
542 |*| |* |*|
543 /// */
545 /* If a 2-char comment sequence partly overlaps with another,
546 we don't try to be clever. E.g. |*| in C, or }% in modes that
547 have %..\n and %{..}%. */
548 if (from > stop && (com2end || comstart))
550 int next = from, next_byte = from_byte, next_c, next_syntax;
551 DEC_BOTH (next, next_byte);
552 UPDATE_SYNTAX_TABLE_BACKWARD (next);
553 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
554 next_syntax = SYNTAX_WITH_FLAGS (next_c);
555 if (((comstart || comnested)
556 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
557 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
558 || ((com2end || comnested)
559 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
560 && comstyle == SYNTAX_FLAGS_COMMENT_STYLE (syntax)
561 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
562 goto lossage;
563 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
566 if (com2start && comstart_pos == 0)
567 /* We're looking at a comment starter. But it might be a comment
568 ender as well (see snmp-mode). The first time we see one, we
569 need to consider it as a comment starter,
570 and the subsequent times as a comment ender. */
571 com2end = 0;
573 /* Turn a 2-char comment sequences into the appropriate syntax. */
574 if (com2end)
575 code = Sendcomment;
576 else if (com2start)
577 code = Scomment;
578 /* Ignore comment starters of a different style. */
579 else if (code == Scomment
580 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax)
581 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
582 continue;
584 /* Ignore escaped characters, except comment-enders. */
585 if (code != Sendcomment && char_quoted (from, from_byte))
586 continue;
588 switch (code)
590 case Sstring_fence:
591 case Scomment_fence:
592 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
593 case Sstring:
594 /* Track parity of quotes. */
595 if (string_style == -1)
596 /* Entering a string. */
597 string_style = c;
598 else if (string_style == c)
599 /* Leaving the string. */
600 string_style = -1;
601 else
602 /* If we have two kinds of string delimiters.
603 There's no way to grok this scanning backwards. */
604 string_lossage = 1;
605 break;
607 case Scomment:
608 /* We've already checked that it is the relevant comstyle. */
609 if (string_style != -1 || comment_lossage || string_lossage)
610 /* There are odd string quotes involved, so let's be careful.
611 Test case in Pascal: " { " a { " } */
612 goto lossage;
614 if (!comnested)
616 /* Record best comment-starter so far. */
617 comstart_pos = from;
618 comstart_byte = from_byte;
620 else if (--nesting <= 0)
621 /* nested comments have to be balanced, so we don't need to
622 keep looking for earlier ones. We use here the same (slightly
623 incorrect) reasoning as below: since it is followed by uniform
624 paired string quotes, this comment-start has to be outside of
625 strings, else the comment-end itself would be inside a string. */
626 goto done;
627 break;
629 case Sendcomment:
630 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax) == comstyle
631 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
632 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
633 /* This is the same style of comment ender as ours. */
635 if (comnested)
636 nesting++;
637 else
638 /* Anything before that can't count because it would match
639 this comment-ender rather than ours. */
640 from = stop; /* Break out of the loop. */
642 else if (comstart_pos != 0 || c != '\n')
643 /* We're mixing comment styles here, so we'd better be careful.
644 The (comstart_pos != 0 || c != '\n') check is not quite correct
645 (we should just always set comment_lossage), but removing it
646 would imply that any multiline comment in C would go through
647 lossage, which seems overkill.
648 The failure should only happen in the rare cases such as
649 { (* } *) */
650 comment_lossage = 1;
651 break;
653 case Sopen:
654 /* Assume a defun-start point is outside of strings. */
655 if (open_paren_in_column_0_is_defun_start
656 && (from == stop
657 || (temp_byte = dec_bytepos (from_byte),
658 FETCH_CHAR (temp_byte) == '\n')))
660 defun_start = from;
661 defun_start_byte = from_byte;
662 from = stop; /* Break out of the loop. */
664 break;
666 default:
667 break;
671 if (comstart_pos == 0)
673 from = comment_end;
674 from_byte = comment_end_byte;
675 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
677 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
678 or `done'), then we've found the beginning of the non-nested comment. */
679 else if (1) /* !comnested */
681 from = comstart_pos;
682 from_byte = comstart_byte;
683 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
685 else
687 struct lisp_parse_state state;
688 lossage:
689 /* We had two kinds of string delimiters mixed up
690 together. Decode this going forwards.
691 Scan fwd from a known safe place (beginning-of-defun)
692 to the one in question; this records where we
693 last passed a comment starter. */
694 /* If we did not already find the defun start, find it now. */
695 if (defun_start == 0)
697 defun_start = find_defun_start (comment_end, comment_end_byte);
698 defun_start_byte = find_start_value_byte;
702 scan_sexps_forward (&state,
703 defun_start, defun_start_byte,
704 comment_end, -10000, 0, Qnil, 0);
705 defun_start = comment_end;
706 if (state.incomment == (comnested ? 1 : -1)
707 && state.comstyle == comstyle)
708 from = state.comstr_start;
709 else
711 from = comment_end;
712 if (state.incomment)
713 /* If comment_end is inside some other comment, maybe ours
714 is nested, so we need to try again from within the
715 surrounding comment. Example: { a (* " *) */
717 /* FIXME: We should advance by one or two chars. */
718 defun_start = state.comstr_start + 2;
719 defun_start_byte = CHAR_TO_BYTE (defun_start);
722 } while (defun_start < comment_end);
724 from_byte = CHAR_TO_BYTE (from);
725 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
728 done:
729 *charpos_ptr = from;
730 *bytepos_ptr = from_byte;
732 return (from == comment_end) ? -1 : from;
735 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
736 doc: /* Return t if OBJECT is a syntax table.
737 Currently, any char-table counts as a syntax table. */)
738 (object)
739 Lisp_Object object;
741 if (CHAR_TABLE_P (object)
742 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
743 return Qt;
744 return Qnil;
747 static void
748 check_syntax_table (obj)
749 Lisp_Object obj;
751 CHECK_TYPE (CHAR_TABLE_P (obj) && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table),
752 Qsyntax_table_p, obj);
755 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
756 doc: /* Return the current syntax table.
757 This is the one specified by the current buffer. */)
760 return current_buffer->syntax_table;
763 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
764 Sstandard_syntax_table, 0, 0, 0,
765 doc: /* Return the standard syntax table.
766 This is the one used for new buffers. */)
769 return Vstandard_syntax_table;
772 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
773 doc: /* Construct a new syntax table and return it.
774 It is a copy of the TABLE, which defaults to the standard syntax table. */)
775 (table)
776 Lisp_Object table;
778 Lisp_Object copy;
780 if (!NILP (table))
781 check_syntax_table (table);
782 else
783 table = Vstandard_syntax_table;
785 copy = Fcopy_sequence (table);
787 /* Only the standard syntax table should have a default element.
788 Other syntax tables should inherit from parents instead. */
789 XCHAR_TABLE (copy)->defalt = Qnil;
791 /* Copied syntax tables should all have parents.
792 If we copied one with no parent, such as the standard syntax table,
793 use the standard syntax table as the copy's parent. */
794 if (NILP (XCHAR_TABLE (copy)->parent))
795 Fset_char_table_parent (copy, Vstandard_syntax_table);
796 return copy;
799 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
800 doc: /* Select a new syntax table for the current buffer.
801 One argument, a syntax table. */)
802 (table)
803 Lisp_Object table;
805 int idx;
806 check_syntax_table (table);
807 current_buffer->syntax_table = table;
808 /* Indicate that this buffer now has a specified syntax table. */
809 idx = PER_BUFFER_VAR_IDX (syntax_table);
810 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
811 return table;
814 /* Convert a letter which signifies a syntax code
815 into the code it signifies.
816 This is used by modify-syntax-entry, and other things. */
818 unsigned char syntax_spec_code[0400] =
819 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
820 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
821 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
822 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
823 (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377,
824 (char) Smath, 0377, 0377, (char) Squote,
825 (char) Sopen, (char) Sclose, 0377, 0377,
826 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
827 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
828 0377, 0377, 0377, 0377,
829 (char) Scomment, 0377, (char) Sendcomment, 0377,
830 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
831 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
832 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
833 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
834 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
835 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
836 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
837 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377
840 /* Indexed by syntax code, give the letter that describes it. */
842 char syntax_code_spec[16] =
844 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
845 '!', '|'
848 /* Indexed by syntax code, give the object (cons of syntax code and
849 nil) to be stored in syntax table. Since these objects can be
850 shared among syntax tables, we generate them in advance. By
851 sharing objects, the function `describe-syntax' can give a more
852 compact listing. */
853 static Lisp_Object Vsyntax_code_object;
856 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
857 doc: /* Return the syntax code of CHARACTER, described by a character.
858 For example, if CHARACTER is a word constituent, the
859 character `w' (119) is returned.
860 The characters that correspond to various syntax codes
861 are listed in the documentation of `modify-syntax-entry'. */)
862 (character)
863 Lisp_Object character;
865 int char_int;
866 gl_state.current_syntax_table = current_buffer->syntax_table;
868 gl_state.use_global = 0;
869 CHECK_NUMBER (character);
870 char_int = XINT (character);
871 return make_number (syntax_code_spec[(int) SYNTAX (char_int)]);
874 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
875 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
876 (character)
877 Lisp_Object character;
879 int char_int, code;
880 gl_state.current_syntax_table = current_buffer->syntax_table;
881 gl_state.use_global = 0;
882 CHECK_NUMBER (character);
883 char_int = XINT (character);
884 code = SYNTAX (char_int);
885 if (code == Sopen || code == Sclose)
886 return SYNTAX_MATCH (char_int);
887 return Qnil;
890 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
891 doc: /* Convert a syntax specification STRING into syntax cell form.
892 STRING should be a string as it is allowed as argument of
893 `modify-syntax-entry'. Value is the equivalent cons cell
894 \(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
895 text property. */)
896 (string)
897 Lisp_Object string;
899 register const unsigned char *p;
900 register enum syntaxcode code;
901 int val;
902 Lisp_Object match;
904 CHECK_STRING (string);
906 p = SDATA (string);
907 code = (enum syntaxcode) syntax_spec_code[*p++];
908 if (((int) code & 0377) == 0377)
909 error ("Invalid syntax description letter: %c", p[-1]);
911 if (code == Sinherit)
912 return Qnil;
914 if (*p)
916 int len;
917 int character = STRING_CHAR_AND_LENGTH (p, len);
918 XSETINT (match, character);
919 if (XFASTINT (match) == ' ')
920 match = Qnil;
921 p += len;
923 else
924 match = Qnil;
926 val = (int) code;
927 while (*p)
928 switch (*p++)
930 case '1':
931 val |= 1 << 16;
932 break;
934 case '2':
935 val |= 1 << 17;
936 break;
938 case '3':
939 val |= 1 << 18;
940 break;
942 case '4':
943 val |= 1 << 19;
944 break;
946 case 'p':
947 val |= 1 << 20;
948 break;
950 case 'b':
951 val |= 1 << 21;
952 break;
954 case 'n':
955 val |= 1 << 22;
956 break;
959 if (val < XVECTOR_SIZE (Vsyntax_code_object) && NILP (match))
960 return XVECTOR (Vsyntax_code_object)->contents[val];
961 else
962 /* Since we can't use a shared object, let's make a new one. */
963 return Fcons (make_number (val), match);
966 /* I really don't know why this is interactive
967 help-form should at least be made useful whilst reading the second arg. */
968 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
969 "cSet syntax for character: \nsSet syntax for %s to: ",
970 doc: /* Set syntax for character CHAR according to string NEWENTRY.
971 The syntax is changed only for table SYNTAX-TABLE, which defaults to
972 the current buffer's syntax table.
973 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
974 in the range MIN to MAX are changed.
975 The first character of NEWENTRY should be one of the following:
976 Space or - whitespace syntax. w word constituent.
977 _ symbol constituent. . punctuation.
978 ( open-parenthesis. ) close-parenthesis.
979 " string quote. \\ escape.
980 $ paired delimiter. ' expression quote or prefix operator.
981 < comment starter. > comment ender.
982 / character-quote. @ inherit from `standard-syntax-table'.
983 | generic string fence. ! generic comment fence.
985 Only single-character comment start and end sequences are represented thus.
986 Two-character sequences are represented as described below.
987 The second character of NEWENTRY is the matching parenthesis,
988 used only if the first character is `(' or `)'.
989 Any additional characters are flags.
990 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
991 1 means CHAR is the start of a two-char comment start sequence.
992 2 means CHAR is the second character of such a sequence.
993 3 means CHAR is the start of a two-char comment end sequence.
994 4 means CHAR is the second character of such a sequence.
996 There can be up to two orthogonal comment sequences. This is to support
997 language modes such as C++. By default, all comment sequences are of style
998 a, but you can set the comment sequence style to b (on the second character
999 of a comment-start, or the first character of a comment-end sequence) using
1000 this flag:
1001 b means CHAR is part of comment sequence b.
1002 n means CHAR is part of a nestable comment sequence.
1004 p means CHAR is a prefix character for `backward-prefix-chars';
1005 such characters are treated as whitespace when they occur
1006 between expressions.
1007 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
1008 (c, newentry, syntax_table)
1009 Lisp_Object c, newentry, syntax_table;
1011 if (CONSP (c))
1013 CHECK_CHARACTER_CAR (c);
1014 CHECK_CHARACTER_CDR (c);
1016 else
1017 CHECK_CHARACTER (c);
1019 if (NILP (syntax_table))
1020 syntax_table = current_buffer->syntax_table;
1021 else
1022 check_syntax_table (syntax_table);
1024 newentry = Fstring_to_syntax (newentry);
1025 if (CONSP (c))
1026 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1027 else
1028 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
1030 /* We clear the regexp cache, since character classes can now have
1031 different values from those in the compiled regexps.*/
1032 clear_regexp_cache ();
1034 return Qnil;
1037 /* Dump syntax table to buffer in human-readable format */
1039 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1040 Sinternal_describe_syntax_value, 1, 1, 0,
1041 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1042 (syntax)
1043 Lisp_Object syntax;
1045 register enum syntaxcode code;
1046 char desc, start1, start2, end1, end2, prefix, comstyle, comnested;
1047 char str[2];
1048 Lisp_Object first, match_lisp, value = syntax;
1050 if (NILP (value))
1052 insert_string ("default");
1053 return syntax;
1056 if (CHAR_TABLE_P (value))
1058 insert_string ("deeper char-table ...");
1059 return syntax;
1062 if (!CONSP (value))
1064 insert_string ("invalid");
1065 return syntax;
1068 first = XCAR (value);
1069 match_lisp = XCDR (value);
1071 if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp)))
1073 insert_string ("invalid");
1074 return syntax;
1077 code = (enum syntaxcode) (XINT (first) & 0377);
1078 start1 = (XINT (first) >> 16) & 1;
1079 start2 = (XINT (first) >> 17) & 1;
1080 end1 = (XINT (first) >> 18) & 1;
1081 end2 = (XINT (first) >> 19) & 1;
1082 prefix = (XINT (first) >> 20) & 1;
1083 comstyle = (XINT (first) >> 21) & 1;
1084 comnested = (XINT (first) >> 22) & 1;
1086 if ((int) code < 0 || (int) code >= (int) Smax)
1088 insert_string ("invalid");
1089 return syntax;
1091 desc = syntax_code_spec[(int) code];
1093 str[0] = desc, str[1] = 0;
1094 insert (str, 1);
1096 if (NILP (match_lisp))
1097 insert (" ", 1);
1098 else
1099 insert_char (XINT (match_lisp));
1101 if (start1)
1102 insert ("1", 1);
1103 if (start2)
1104 insert ("2", 1);
1106 if (end1)
1107 insert ("3", 1);
1108 if (end2)
1109 insert ("4", 1);
1111 if (prefix)
1112 insert ("p", 1);
1113 if (comstyle)
1114 insert ("b", 1);
1115 if (comnested)
1116 insert ("n", 1);
1118 insert_string ("\twhich means: ");
1120 switch (SWITCH_ENUM_CAST (code))
1122 case Swhitespace:
1123 insert_string ("whitespace"); break;
1124 case Spunct:
1125 insert_string ("punctuation"); break;
1126 case Sword:
1127 insert_string ("word"); break;
1128 case Ssymbol:
1129 insert_string ("symbol"); break;
1130 case Sopen:
1131 insert_string ("open"); break;
1132 case Sclose:
1133 insert_string ("close"); break;
1134 case Squote:
1135 insert_string ("prefix"); break;
1136 case Sstring:
1137 insert_string ("string"); break;
1138 case Smath:
1139 insert_string ("math"); break;
1140 case Sescape:
1141 insert_string ("escape"); break;
1142 case Scharquote:
1143 insert_string ("charquote"); break;
1144 case Scomment:
1145 insert_string ("comment"); break;
1146 case Sendcomment:
1147 insert_string ("endcomment"); break;
1148 case Sinherit:
1149 insert_string ("inherit"); break;
1150 case Scomment_fence:
1151 insert_string ("comment fence"); break;
1152 case Sstring_fence:
1153 insert_string ("string fence"); break;
1154 default:
1155 insert_string ("invalid");
1156 return syntax;
1159 if (!NILP (match_lisp))
1161 insert_string (", matches ");
1162 insert_char (XINT (match_lisp));
1165 if (start1)
1166 insert_string (",\n\t is the first character of a comment-start sequence");
1167 if (start2)
1168 insert_string (",\n\t is the second character of a comment-start sequence");
1170 if (end1)
1171 insert_string (",\n\t is the first character of a comment-end sequence");
1172 if (end2)
1173 insert_string (",\n\t is the second character of a comment-end sequence");
1174 if (comstyle)
1175 insert_string (" (comment style b)");
1176 if (comnested)
1177 insert_string (" (nestable)");
1179 if (prefix)
1180 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1182 return syntax;
1185 int parse_sexp_ignore_comments;
1187 /* Char-table of functions that find the next or previous word
1188 boundary. */
1189 Lisp_Object Vfind_word_boundary_function_table;
1191 /* Return the position across COUNT words from FROM.
1192 If that many words cannot be found before the end of the buffer, return 0.
1193 COUNT negative means scan backward and stop at word beginning. */
1196 scan_words (from, count)
1197 register int from, count;
1199 register int beg = BEGV;
1200 register int end = ZV;
1201 register int from_byte = CHAR_TO_BYTE (from);
1202 register enum syntaxcode code;
1203 int ch0, ch1;
1204 Lisp_Object func, script, pos;
1206 immediate_quit = 1;
1207 QUIT;
1209 SETUP_SYNTAX_TABLE (from, count);
1211 while (count > 0)
1213 while (1)
1215 if (from == end)
1217 immediate_quit = 0;
1218 return 0;
1220 UPDATE_SYNTAX_TABLE_FORWARD (from);
1221 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1222 code = SYNTAX (ch0);
1223 INC_BOTH (from, from_byte);
1224 if (words_include_escapes
1225 && (code == Sescape || code == Scharquote))
1226 break;
1227 if (code == Sword)
1228 break;
1230 /* Now CH0 is a character which begins a word and FROM is the
1231 position of the next character. */
1232 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
1233 if (! NILP (Ffboundp (func)))
1235 pos = call2 (func, make_number (from - 1), make_number (end));
1236 if (INTEGERP (pos) && XINT (pos) > from)
1238 from = XINT (pos);
1239 from_byte = CHAR_TO_BYTE (from);
1242 else
1244 script = CHAR_TABLE_REF (Vchar_script_table, ch0);
1245 while (1)
1247 if (from == end) break;
1248 UPDATE_SYNTAX_TABLE_FORWARD (from);
1249 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1250 code = SYNTAX (ch1);
1251 if ((code != Sword
1252 && (! words_include_escapes
1253 || (code != Sescape && code != Scharquote)))
1254 || word_boundary_p (ch0, ch1))
1255 break;
1256 INC_BOTH (from, from_byte);
1257 ch0 = ch1;
1260 count--;
1262 while (count < 0)
1264 while (1)
1266 if (from == beg)
1268 immediate_quit = 0;
1269 return 0;
1271 DEC_BOTH (from, from_byte);
1272 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1273 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1274 code = SYNTAX (ch1);
1275 if (words_include_escapes
1276 && (code == Sescape || code == Scharquote))
1277 break;
1278 if (code == Sword)
1279 break;
1281 /* Now CH1 is a character which ends a word and FROM is the
1282 position of it. */
1283 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
1284 if (! NILP (Ffboundp (func)))
1286 pos = call2 (func, make_number (from), make_number (beg));
1287 if (INTEGERP (pos) && XINT (pos) < from)
1289 from = XINT (pos);
1290 from_byte = CHAR_TO_BYTE (from);
1293 else
1295 script = CHAR_TABLE_REF (Vchar_script_table, ch1);
1296 while (1)
1298 if (from == beg)
1299 break;
1300 DEC_BOTH (from, from_byte);
1301 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1302 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1303 code = SYNTAX (ch0);
1304 if ((code != Sword
1305 && (! words_include_escapes
1306 || (code != Sescape && code != Scharquote)))
1307 || word_boundary_p (ch0, ch1))
1309 INC_BOTH (from, from_byte);
1310 break;
1312 ch1 = ch0;
1315 count++;
1318 immediate_quit = 0;
1320 return from;
1323 DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p",
1324 doc: /* Move point forward ARG words (backward if ARG is negative).
1325 Normally returns t.
1326 If an edge of the buffer or a field boundary is reached, point is left there
1327 and the function returns nil. Field boundaries are not noticed if
1328 `inhibit-field-text-motion' is non-nil. */)
1329 (arg)
1330 Lisp_Object arg;
1332 Lisp_Object tmp;
1333 int orig_val, val;
1335 if (NILP (arg))
1336 XSETFASTINT (arg, 1);
1337 else
1338 CHECK_NUMBER (arg);
1340 val = orig_val = scan_words (PT, XINT (arg));
1341 if (! orig_val)
1342 val = XINT (arg) > 0 ? ZV : BEGV;
1344 /* Avoid jumping out of an input field. */
1345 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1346 Qt, Qnil, Qnil);
1347 val = XFASTINT (tmp);
1349 SET_PT (val);
1350 return val == orig_val ? Qt : Qnil;
1353 Lisp_Object skip_chars ();
1355 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1356 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1357 STRING is like the inside of a `[...]' in a regular expression
1358 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1359 (but not at the end of a range; quoting is never needed there).
1360 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1361 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1362 Char classes, e.g. `[:alpha:]', are supported.
1364 Returns the distance traveled, either zero or positive. */)
1365 (string, lim)
1366 Lisp_Object string, lim;
1368 return skip_chars (1, string, lim, 1);
1371 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1372 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1373 See `skip-chars-forward' for details.
1374 Returns the distance traveled, either zero or negative. */)
1375 (string, lim)
1376 Lisp_Object string, lim;
1378 return skip_chars (0, string, lim, 1);
1381 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1382 doc: /* Move point forward across chars in specified syntax classes.
1383 SYNTAX is a string of syntax code characters.
1384 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1385 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1386 This function returns the distance traveled, either zero or positive. */)
1387 (syntax, lim)
1388 Lisp_Object syntax, lim;
1390 return skip_syntaxes (1, syntax, lim);
1393 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1394 doc: /* Move point backward across chars in specified syntax classes.
1395 SYNTAX is a string of syntax code characters.
1396 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1397 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1398 This function returns the distance traveled, either zero or negative. */)
1399 (syntax, lim)
1400 Lisp_Object syntax, lim;
1402 return skip_syntaxes (0, syntax, lim);
1405 static Lisp_Object
1406 skip_chars (forwardp, string, lim, handle_iso_classes)
1407 int forwardp;
1408 Lisp_Object string, lim;
1409 int handle_iso_classes;
1411 register unsigned int c;
1412 unsigned char fastmap[0400];
1413 /* Store the ranges of non-ASCII characters. */
1414 int *char_ranges;
1415 int n_char_ranges = 0;
1416 int negate = 0;
1417 register int i, i_byte;
1418 /* Set to 1 if the current buffer is multibyte and the region
1419 contains non-ASCII chars. */
1420 int multibyte;
1421 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1422 chars. */
1423 int string_multibyte;
1424 int size_byte;
1425 const unsigned char *str;
1426 int len;
1427 Lisp_Object iso_classes;
1429 CHECK_STRING (string);
1430 iso_classes = Qnil;
1432 if (NILP (lim))
1433 XSETINT (lim, forwardp ? ZV : BEGV);
1434 else
1435 CHECK_NUMBER_COERCE_MARKER (lim);
1437 /* In any case, don't allow scan outside bounds of buffer. */
1438 if (XINT (lim) > ZV)
1439 XSETFASTINT (lim, ZV);
1440 if (XINT (lim) < BEGV)
1441 XSETFASTINT (lim, BEGV);
1443 multibyte = (!NILP (current_buffer->enable_multibyte_characters)
1444 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1445 string_multibyte = SBYTES (string) > SCHARS (string);
1447 bzero (fastmap, sizeof fastmap);
1449 str = SDATA (string);
1450 size_byte = SBYTES (string);
1452 i_byte = 0;
1453 if (i_byte < size_byte
1454 && SREF (string, 0) == '^')
1456 negate = 1; i_byte++;
1459 /* Find the characters specified and set their elements of fastmap.
1460 Handle backslashes and ranges specially.
1462 If STRING contains non-ASCII characters, setup char_ranges for
1463 them and use fastmap only for their leading codes. */
1465 if (! string_multibyte)
1467 int string_has_eight_bit = 0;
1469 /* At first setup fastmap. */
1470 while (i_byte < size_byte)
1472 c = str[i_byte++];
1474 if (handle_iso_classes && c == '['
1475 && i_byte < size_byte
1476 && str[i_byte] == ':')
1478 const unsigned char *class_beg = str + i_byte + 1;
1479 const unsigned char *class_end = class_beg;
1480 const unsigned char *class_limit = str + size_byte - 2;
1481 /* Leave room for the null. */
1482 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1483 re_wctype_t cc;
1485 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1486 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1488 while (class_end < class_limit
1489 && *class_end >= 'a' && *class_end <= 'z')
1490 class_end++;
1492 if (class_end == class_beg
1493 || *class_end != ':' || class_end[1] != ']')
1494 goto not_a_class_name;
1496 bcopy (class_beg, class_name, class_end - class_beg);
1497 class_name[class_end - class_beg] = 0;
1499 cc = re_wctype (class_name);
1500 if (cc == 0)
1501 error ("Invalid ISO C character class");
1503 iso_classes = Fcons (make_number (cc), iso_classes);
1505 i_byte = class_end + 2 - str;
1506 continue;
1509 not_a_class_name:
1510 if (c == '\\')
1512 if (i_byte == size_byte)
1513 break;
1515 c = str[i_byte++];
1517 /* Treat `-' as range character only if another character
1518 follows. */
1519 if (i_byte + 1 < size_byte
1520 && str[i_byte] == '-')
1522 unsigned int c2;
1524 /* Skip over the dash. */
1525 i_byte++;
1527 /* Get the end of the range. */
1528 c2 = str[i_byte++];
1529 if (c2 == '\\'
1530 && i_byte < size_byte)
1531 c2 = str[i_byte++];
1533 if (c <= c2)
1535 while (c <= c2)
1536 fastmap[c++] = 1;
1537 if (! ASCII_CHAR_P (c2))
1538 string_has_eight_bit = 1;
1541 else
1543 fastmap[c] = 1;
1544 if (! ASCII_CHAR_P (c))
1545 string_has_eight_bit = 1;
1549 /* If the current range is multibyte and STRING contains
1550 eight-bit chars, arrange fastmap and setup char_ranges for
1551 the corresponding multibyte chars. */
1552 if (multibyte && string_has_eight_bit)
1554 unsigned char fastmap2[0400];
1555 int range_start_byte, range_start_char;
1557 bcopy (fastmap2 + 0200, fastmap + 0200, 0200);
1558 bzero (fastmap + 0200, 0200);
1559 /* We are sure that this loop stops. */
1560 for (i = 0200; ! fastmap2[i]; i++);
1561 c = BYTE8_TO_CHAR (i);
1562 fastmap[CHAR_LEADING_CODE (c)] = 1;
1563 range_start_byte = i;
1564 range_start_char = c;
1565 char_ranges = (int *) alloca (sizeof (int) * 128 * 2);
1566 for (i = 129; i < 0400; i++)
1568 c = BYTE8_TO_CHAR (i);
1569 fastmap[CHAR_LEADING_CODE (c)] = 1;
1570 if (i - range_start_byte != c - range_start_char)
1572 char_ranges[n_char_ranges++] = range_start_char;
1573 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1574 + range_start_char);
1575 range_start_byte = i;
1576 range_start_char = c;
1579 char_ranges[n_char_ranges++] = range_start_char;
1580 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1581 + range_start_char);
1584 else /* STRING is multibyte */
1586 char_ranges = (int *) alloca (sizeof (int) * SCHARS (string) * 2);
1588 while (i_byte < size_byte)
1590 unsigned char leading_code;
1592 leading_code = str[i_byte];
1593 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1594 i_byte += len;
1596 if (handle_iso_classes && c == '['
1597 && i_byte < size_byte
1598 && STRING_CHAR (str + i_byte) == ':')
1600 const unsigned char *class_beg = str + i_byte + 1;
1601 const unsigned char *class_end = class_beg;
1602 const unsigned char *class_limit = str + size_byte - 2;
1603 /* Leave room for the null. */
1604 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1605 re_wctype_t cc;
1607 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1608 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1610 while (class_end < class_limit
1611 && *class_end >= 'a' && *class_end <= 'z')
1612 class_end++;
1614 if (class_end == class_beg
1615 || *class_end != ':' || class_end[1] != ']')
1616 goto not_a_class_name_multibyte;
1618 bcopy (class_beg, class_name, class_end - class_beg);
1619 class_name[class_end - class_beg] = 0;
1621 cc = re_wctype (class_name);
1622 if (cc == 0)
1623 error ("Invalid ISO C character class");
1625 iso_classes = Fcons (make_number (cc), iso_classes);
1627 i_byte = class_end + 2 - str;
1628 continue;
1631 not_a_class_name_multibyte:
1632 if (c == '\\')
1634 if (i_byte == size_byte)
1635 break;
1637 leading_code = str[i_byte];
1638 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1639 i_byte += len;
1641 /* Treat `-' as range character only if another character
1642 follows. */
1643 if (i_byte + 1 < size_byte
1644 && str[i_byte] == '-')
1646 unsigned int c2;
1647 unsigned char leading_code2;
1649 /* Skip over the dash. */
1650 i_byte++;
1652 /* Get the end of the range. */
1653 leading_code2 = str[i_byte];
1654 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
1655 i_byte += len;
1657 if (c2 == '\\'
1658 && i_byte < size_byte)
1660 leading_code2 = str[i_byte];
1661 c2 =STRING_CHAR_AND_LENGTH (str + i_byte, len);
1662 i_byte += len;
1665 if (c > c2)
1666 continue;
1667 if (ASCII_CHAR_P (c))
1669 while (c <= c2 && c < 0x80)
1670 fastmap[c++] = 1;
1671 leading_code = CHAR_LEADING_CODE (c);
1673 if (! ASCII_CHAR_P (c))
1675 while (leading_code <= leading_code2)
1676 fastmap[leading_code++] = 1;
1677 if (c <= c2)
1679 char_ranges[n_char_ranges++] = c;
1680 char_ranges[n_char_ranges++] = c2;
1684 else
1686 if (ASCII_CHAR_P (c))
1687 fastmap[c] = 1;
1688 else
1690 fastmap[leading_code] = 1;
1691 char_ranges[n_char_ranges++] = c;
1692 char_ranges[n_char_ranges++] = c;
1697 /* If the current range is unibyte and STRING contains non-ASCII
1698 chars, arrange fastmap for the corresponding unibyte
1699 chars. */
1701 if (! multibyte && n_char_ranges > 0)
1703 bzero (fastmap + 0200, 0200);
1704 for (i = 0; i < n_char_ranges; i += 2)
1706 int c1 = char_ranges[i];
1707 int c2 = char_ranges[i + 1];
1709 for (; c1 <= c2; c1++)
1711 int b = CHAR_TO_BYTE_SAFE (c1);
1712 if (b >= 0)
1713 fastmap[b] = 1;
1719 /* If ^ was the first character, complement the fastmap. */
1720 if (negate)
1722 if (! multibyte)
1723 for (i = 0; i < sizeof fastmap; i++)
1724 fastmap[i] ^= 1;
1725 else
1727 for (i = 0; i < 0200; i++)
1728 fastmap[i] ^= 1;
1729 /* All non-ASCII chars possibly match. */
1730 for (; i < sizeof fastmap; i++)
1731 fastmap[i] = 1;
1736 int start_point = PT;
1737 int pos = PT;
1738 int pos_byte = PT_BYTE;
1739 unsigned char *p = PT_ADDR, *endp, *stop;
1741 if (forwardp)
1743 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1744 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1746 else
1748 endp = CHAR_POS_ADDR (XINT (lim));
1749 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1752 immediate_quit = 1;
1753 /* This code may look up syntax tables using macros that rely on the
1754 gl_state object. To make sure this object is not out of date,
1755 let's initialize it manually.
1756 We ignore syntax-table text-properties for now, since that's
1757 what we've done in the past. */
1758 SETUP_SYNTAX_TABLE (BEGV, 0);
1759 if (forwardp)
1761 if (multibyte)
1762 while (1)
1764 int nbytes;
1766 if (p >= stop)
1768 if (p >= endp)
1769 break;
1770 p = GAP_END_ADDR;
1771 stop = endp;
1773 c = STRING_CHAR_AND_LENGTH (p, nbytes);
1774 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1776 if (negate)
1777 break;
1778 else
1779 goto fwd_ok;
1782 if (! fastmap[*p])
1783 break;
1784 if (! ASCII_CHAR_P (c))
1786 /* As we are looking at a multibyte character, we
1787 must look up the character in the table
1788 CHAR_RANGES. If there's no data in the table,
1789 that character is not what we want to skip. */
1791 /* The following code do the right thing even if
1792 n_char_ranges is zero (i.e. no data in
1793 CHAR_RANGES). */
1794 for (i = 0; i < n_char_ranges; i += 2)
1795 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1796 break;
1797 if (!(negate ^ (i < n_char_ranges)))
1798 break;
1800 fwd_ok:
1801 p += nbytes, pos++, pos_byte += nbytes;
1803 else
1804 while (1)
1806 if (p >= stop)
1808 if (p >= endp)
1809 break;
1810 p = GAP_END_ADDR;
1811 stop = endp;
1814 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1816 if (negate)
1817 break;
1818 else
1819 goto fwd_unibyte_ok;
1822 if (!fastmap[*p])
1823 break;
1824 fwd_unibyte_ok:
1825 p++, pos++, pos_byte++;
1828 else
1830 if (multibyte)
1831 while (1)
1833 unsigned char *prev_p;
1835 if (p <= stop)
1837 if (p <= endp)
1838 break;
1839 p = GPT_ADDR;
1840 stop = endp;
1842 prev_p = p;
1843 while (--p >= stop && ! CHAR_HEAD_P (*p));
1844 c = STRING_CHAR (p);
1846 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1848 if (negate)
1849 break;
1850 else
1851 goto back_ok;
1854 if (! fastmap[*p])
1855 break;
1856 if (! ASCII_CHAR_P (c))
1858 /* See the comment in the previous similar code. */
1859 for (i = 0; i < n_char_ranges; i += 2)
1860 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1861 break;
1862 if (!(negate ^ (i < n_char_ranges)))
1863 break;
1865 back_ok:
1866 pos--, pos_byte -= prev_p - p;
1868 else
1869 while (1)
1871 if (p <= stop)
1873 if (p <= endp)
1874 break;
1875 p = GPT_ADDR;
1876 stop = endp;
1879 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
1881 if (negate)
1882 break;
1883 else
1884 goto back_unibyte_ok;
1887 if (!fastmap[p[-1]])
1888 break;
1889 back_unibyte_ok:
1890 p--, pos--, pos_byte--;
1894 SET_PT_BOTH (pos, pos_byte);
1895 immediate_quit = 0;
1897 return make_number (PT - start_point);
1902 static Lisp_Object
1903 skip_syntaxes (forwardp, string, lim)
1904 int forwardp;
1905 Lisp_Object string, lim;
1907 register unsigned int c;
1908 unsigned char fastmap[0400];
1909 int negate = 0;
1910 register int i, i_byte;
1911 int multibyte;
1912 int size_byte;
1913 unsigned char *str;
1915 CHECK_STRING (string);
1917 if (NILP (lim))
1918 XSETINT (lim, forwardp ? ZV : BEGV);
1919 else
1920 CHECK_NUMBER_COERCE_MARKER (lim);
1922 /* In any case, don't allow scan outside bounds of buffer. */
1923 if (XINT (lim) > ZV)
1924 XSETFASTINT (lim, ZV);
1925 if (XINT (lim) < BEGV)
1926 XSETFASTINT (lim, BEGV);
1928 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
1929 return make_number (0);
1931 multibyte = (!NILP (current_buffer->enable_multibyte_characters)
1932 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1934 bzero (fastmap, sizeof fastmap);
1936 if (SBYTES (string) > SCHARS (string))
1937 /* As this is very rare case (syntax spec is ASCII only), don't
1938 consider efficiency. */
1939 string = string_make_unibyte (string);
1941 str = SDATA (string);
1942 size_byte = SBYTES (string);
1944 i_byte = 0;
1945 if (i_byte < size_byte
1946 && SREF (string, 0) == '^')
1948 negate = 1; i_byte++;
1951 /* Find the syntaxes specified and set their elements of fastmap. */
1953 while (i_byte < size_byte)
1955 c = str[i_byte++];
1956 fastmap[syntax_spec_code[c]] = 1;
1959 /* If ^ was the first character, complement the fastmap. */
1960 if (negate)
1961 for (i = 0; i < sizeof fastmap; i++)
1962 fastmap[i] ^= 1;
1965 int start_point = PT;
1966 int pos = PT;
1967 int pos_byte = PT_BYTE;
1968 unsigned char *p = PT_ADDR, *endp, *stop;
1970 if (forwardp)
1972 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1973 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1975 else
1977 endp = CHAR_POS_ADDR (XINT (lim));
1978 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1981 immediate_quit = 1;
1982 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
1983 if (forwardp)
1985 if (multibyte)
1987 while (1)
1989 int nbytes;
1991 if (p >= stop)
1993 if (p >= endp)
1994 break;
1995 p = GAP_END_ADDR;
1996 stop = endp;
1998 c = STRING_CHAR_AND_LENGTH (p, nbytes);
1999 if (! fastmap[(int) SYNTAX (c)])
2000 break;
2001 p += nbytes, pos++, pos_byte += nbytes;
2002 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2005 else
2007 while (1)
2009 if (p >= stop)
2011 if (p >= endp)
2012 break;
2013 p = GAP_END_ADDR;
2014 stop = endp;
2016 if (! fastmap[(int) SYNTAX (*p)])
2017 break;
2018 p++, pos++, pos_byte++;
2019 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2023 else
2025 if (multibyte)
2027 while (1)
2029 unsigned char *prev_p;
2031 if (p <= stop)
2033 if (p <= endp)
2034 break;
2035 p = GPT_ADDR;
2036 stop = endp;
2038 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2039 prev_p = p;
2040 while (--p >= stop && ! CHAR_HEAD_P (*p));
2041 c = STRING_CHAR (p);
2042 if (! fastmap[(int) SYNTAX (c)])
2043 break;
2044 pos--, pos_byte -= prev_p - p;
2047 else
2049 while (1)
2051 if (p <= stop)
2053 if (p <= endp)
2054 break;
2055 p = GPT_ADDR;
2056 stop = endp;
2058 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2059 if (! fastmap[(int) SYNTAX (p[-1])])
2060 break;
2061 p--, pos--, pos_byte--;
2066 SET_PT_BOTH (pos, pos_byte);
2067 immediate_quit = 0;
2069 return make_number (PT - start_point);
2073 /* Return 1 if character C belongs to one of the ISO classes
2074 in the list ISO_CLASSES. Each class is represented by an
2075 integer which is its type according to re_wctype. */
2077 static int
2078 in_classes (c, iso_classes)
2079 int c;
2080 Lisp_Object iso_classes;
2082 int fits_class = 0;
2084 while (CONSP (iso_classes))
2086 Lisp_Object elt;
2087 elt = XCAR (iso_classes);
2088 iso_classes = XCDR (iso_classes);
2090 if (re_iswctype (c, XFASTINT (elt)))
2091 fits_class = 1;
2094 return fits_class;
2097 /* Jump over a comment, assuming we are at the beginning of one.
2098 FROM is the current position.
2099 FROM_BYTE is the bytepos corresponding to FROM.
2100 Do not move past STOP (a charpos).
2101 The comment over which we have to jump is of style STYLE
2102 (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2103 NESTING should be positive to indicate the nesting at the beginning
2104 for nested comments and should be zero or negative else.
2105 ST_COMMENT_STYLE cannot be nested.
2106 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2107 (or 0 If the search cannot start in the middle of a two-character).
2109 If successful, return 1 and store the charpos of the comment's end
2110 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2111 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2112 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2113 (as defined for state.incomment) in *INCOMMENT_PTR.
2115 The comment end is the last character of the comment rather than the
2116 character just after the comment.
2118 Global syntax data is assumed to initially be valid for FROM and
2119 remains valid for forward search starting at the returned position. */
2121 static int
2122 forw_comment (from, from_byte, stop, nesting, style, prev_syntax,
2123 charpos_ptr, bytepos_ptr, incomment_ptr)
2124 EMACS_INT from, from_byte, stop;
2125 int nesting, style, prev_syntax;
2126 EMACS_INT *charpos_ptr, *bytepos_ptr;
2127 int *incomment_ptr;
2129 register int c, c1;
2130 register enum syntaxcode code;
2131 register int syntax;
2133 if (nesting <= 0) nesting = -1;
2135 /* Enter the loop in the middle so that we find
2136 a 2-char comment ender if we start in the middle of it. */
2137 syntax = prev_syntax;
2138 if (syntax != 0) goto forw_incomment;
2140 while (1)
2142 if (from == stop)
2144 *incomment_ptr = nesting;
2145 *charpos_ptr = from;
2146 *bytepos_ptr = from_byte;
2147 return 0;
2149 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2150 syntax = SYNTAX_WITH_FLAGS (c);
2151 code = syntax & 0xff;
2152 if (code == Sendcomment
2153 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
2154 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2155 (nesting > 0 && --nesting == 0) : nesting < 0))
2156 /* we have encountered a comment end of the same style
2157 as the comment sequence which began this comment
2158 section */
2159 break;
2160 if (code == Scomment_fence
2161 && style == ST_COMMENT_STYLE)
2162 /* we have encountered a comment end of the same style
2163 as the comment sequence which began this comment
2164 section. */
2165 break;
2166 if (nesting > 0
2167 && code == Scomment
2168 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2169 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style)
2170 /* we have encountered a nested comment of the same style
2171 as the comment sequence which began this comment section */
2172 nesting++;
2173 INC_BOTH (from, from_byte);
2174 UPDATE_SYNTAX_TABLE_FORWARD (from);
2176 forw_incomment:
2177 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2178 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
2179 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2180 SYNTAX_COMEND_SECOND (c1))
2181 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2182 SYNTAX_COMMENT_NESTED (c1)) ? nesting > 0 : nesting < 0))
2184 if (--nesting <= 0)
2185 /* we have encountered a comment end of the same style
2186 as the comment sequence which began this comment
2187 section */
2188 break;
2189 else
2191 INC_BOTH (from, from_byte);
2192 UPDATE_SYNTAX_TABLE_FORWARD (from);
2195 if (nesting > 0
2196 && from < stop
2197 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2198 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2199 SYNTAX_COMMENT_STYLE (c1) == style
2200 && SYNTAX_COMSTART_SECOND (c1))
2201 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2202 SYNTAX_COMMENT_NESTED (c1)))
2203 /* we have encountered a nested comment of the same style
2204 as the comment sequence which began this comment
2205 section */
2207 INC_BOTH (from, from_byte);
2208 UPDATE_SYNTAX_TABLE_FORWARD (from);
2209 nesting++;
2212 *charpos_ptr = from;
2213 *bytepos_ptr = from_byte;
2214 return 1;
2217 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2218 doc: /*
2219 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2220 Stop scanning if we find something other than a comment or whitespace.
2221 Set point to where scanning stops.
2222 If COUNT comments are found as expected, with nothing except whitespace
2223 between them, return t; otherwise return nil. */)
2224 (count)
2225 Lisp_Object count;
2227 register EMACS_INT from;
2228 EMACS_INT from_byte;
2229 register EMACS_INT stop;
2230 register int c, c1;
2231 register enum syntaxcode code;
2232 int comstyle = 0; /* style of comment encountered */
2233 int comnested = 0; /* whether the comment is nestable or not */
2234 int found;
2235 EMACS_INT count1;
2236 EMACS_INT out_charpos, out_bytepos;
2237 int dummy;
2239 CHECK_NUMBER (count);
2240 count1 = XINT (count);
2241 stop = count1 > 0 ? ZV : BEGV;
2243 immediate_quit = 1;
2244 QUIT;
2246 from = PT;
2247 from_byte = PT_BYTE;
2249 SETUP_SYNTAX_TABLE (from, count1);
2250 while (count1 > 0)
2254 int comstart_first;
2256 if (from == stop)
2258 SET_PT_BOTH (from, from_byte);
2259 immediate_quit = 0;
2260 return Qnil;
2262 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2263 code = SYNTAX (c);
2264 comstart_first = SYNTAX_COMSTART_FIRST (c);
2265 comnested = SYNTAX_COMMENT_NESTED (c);
2266 comstyle = SYNTAX_COMMENT_STYLE (c);
2267 INC_BOTH (from, from_byte);
2268 UPDATE_SYNTAX_TABLE_FORWARD (from);
2269 if (from < stop && comstart_first
2270 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2271 SYNTAX_COMSTART_SECOND (c1)))
2273 /* We have encountered a comment start sequence and we
2274 are ignoring all text inside comments. We must record
2275 the comment style this sequence begins so that later,
2276 only a comment end of the same style actually ends
2277 the comment section. */
2278 code = Scomment;
2279 comstyle = SYNTAX_COMMENT_STYLE (c1);
2280 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2281 INC_BOTH (from, from_byte);
2282 UPDATE_SYNTAX_TABLE_FORWARD (from);
2285 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2287 if (code == Scomment_fence)
2288 comstyle = ST_COMMENT_STYLE;
2289 else if (code != Scomment)
2291 immediate_quit = 0;
2292 DEC_BOTH (from, from_byte);
2293 SET_PT_BOTH (from, from_byte);
2294 return Qnil;
2296 /* We're at the start of a comment. */
2297 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2298 &out_charpos, &out_bytepos, &dummy);
2299 from = out_charpos; from_byte = out_bytepos;
2300 if (!found)
2302 immediate_quit = 0;
2303 SET_PT_BOTH (from, from_byte);
2304 return Qnil;
2306 INC_BOTH (from, from_byte);
2307 UPDATE_SYNTAX_TABLE_FORWARD (from);
2308 /* We have skipped one comment. */
2309 count1--;
2312 while (count1 < 0)
2314 while (1)
2316 int quoted;
2318 if (from <= stop)
2320 SET_PT_BOTH (BEGV, BEGV_BYTE);
2321 immediate_quit = 0;
2322 return Qnil;
2325 DEC_BOTH (from, from_byte);
2326 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2327 quoted = char_quoted (from, from_byte);
2328 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2329 code = SYNTAX (c);
2330 comstyle = 0;
2331 comnested = SYNTAX_COMMENT_NESTED (c);
2332 if (code == Sendcomment)
2333 comstyle = SYNTAX_COMMENT_STYLE (c);
2334 if (from > stop && SYNTAX_COMEND_SECOND (c)
2335 && prev_char_comend_first (from, from_byte)
2336 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2338 /* We must record the comment style encountered so that
2339 later, we can match only the proper comment begin
2340 sequence of the same style. */
2341 DEC_BOTH (from, from_byte);
2342 code = Sendcomment;
2343 /* Calling char_quoted, above, set up global syntax position
2344 at the new value of FROM. */
2345 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2346 comstyle = SYNTAX_COMMENT_STYLE (c1);
2347 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2350 if (code == Scomment_fence)
2352 /* Skip until first preceding unquoted comment_fence. */
2353 int found = 0, ini = from, ini_byte = from_byte;
2355 while (1)
2357 DEC_BOTH (from, from_byte);
2358 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2359 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2360 if (SYNTAX (c) == Scomment_fence
2361 && !char_quoted (from, from_byte))
2363 found = 1;
2364 break;
2366 else if (from == stop)
2367 break;
2369 if (found == 0)
2371 from = ini; /* Set point to ini + 1. */
2372 from_byte = ini_byte;
2373 goto leave;
2375 else
2376 /* We have skipped one comment. */
2377 break;
2379 else if (code == Sendcomment)
2381 found = back_comment (from, from_byte, stop, comnested, comstyle,
2382 &out_charpos, &out_bytepos);
2383 if (found == -1)
2385 if (c == '\n')
2386 /* This end-of-line is not an end-of-comment.
2387 Treat it like a whitespace.
2388 CC-mode (and maybe others) relies on this behavior. */
2390 else
2392 /* Failure: we should go back to the end of this
2393 not-quite-endcomment. */
2394 if (SYNTAX(c) != code)
2395 /* It was a two-char Sendcomment. */
2396 INC_BOTH (from, from_byte);
2397 goto leave;
2400 else
2402 /* We have skipped one comment. */
2403 from = out_charpos, from_byte = out_bytepos;
2404 break;
2407 else if (code != Swhitespace || quoted)
2409 leave:
2410 immediate_quit = 0;
2411 INC_BOTH (from, from_byte);
2412 SET_PT_BOTH (from, from_byte);
2413 return Qnil;
2417 count1++;
2420 SET_PT_BOTH (from, from_byte);
2421 immediate_quit = 0;
2422 return Qt;
2425 /* Return syntax code of character C if C is an ASCII character
2426 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2428 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2429 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2430 ? SYNTAX (c) : Ssymbol)
2432 static Lisp_Object
2433 scan_lists (from, count, depth, sexpflag)
2434 register EMACS_INT from;
2435 EMACS_INT count, depth;
2436 int sexpflag;
2438 Lisp_Object val;
2439 register EMACS_INT stop = count > 0 ? ZV : BEGV;
2440 register int c, c1;
2441 int stringterm;
2442 int quoted;
2443 int mathexit = 0;
2444 register enum syntaxcode code, temp_code;
2445 int min_depth = depth; /* Err out if depth gets less than this. */
2446 int comstyle = 0; /* style of comment encountered */
2447 int comnested = 0; /* whether the comment is nestable or not */
2448 EMACS_INT temp_pos;
2449 EMACS_INT last_good = from;
2450 int found;
2451 EMACS_INT from_byte;
2452 EMACS_INT out_bytepos, out_charpos;
2453 int temp, dummy;
2454 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2456 if (depth > 0) min_depth = 0;
2458 if (from > ZV) from = ZV;
2459 if (from < BEGV) from = BEGV;
2461 from_byte = CHAR_TO_BYTE (from);
2463 immediate_quit = 1;
2464 QUIT;
2466 SETUP_SYNTAX_TABLE (from, count);
2467 while (count > 0)
2469 while (from < stop)
2471 int comstart_first, prefix;
2472 UPDATE_SYNTAX_TABLE_FORWARD (from);
2473 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2474 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2475 comstart_first = SYNTAX_COMSTART_FIRST (c);
2476 comnested = SYNTAX_COMMENT_NESTED (c);
2477 comstyle = SYNTAX_COMMENT_STYLE (c);
2478 prefix = SYNTAX_PREFIX (c);
2479 if (depth == min_depth)
2480 last_good = from;
2481 INC_BOTH (from, from_byte);
2482 UPDATE_SYNTAX_TABLE_FORWARD (from);
2483 if (from < stop && comstart_first
2484 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2485 SYNTAX_COMSTART_SECOND (c))
2486 && parse_sexp_ignore_comments)
2488 /* we have encountered a comment start sequence and we
2489 are ignoring all text inside comments. We must record
2490 the comment style this sequence begins so that later,
2491 only a comment end of the same style actually ends
2492 the comment section */
2493 code = Scomment;
2494 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2495 comstyle = SYNTAX_COMMENT_STYLE (c1);
2496 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2497 INC_BOTH (from, from_byte);
2498 UPDATE_SYNTAX_TABLE_FORWARD (from);
2501 if (prefix)
2502 continue;
2504 switch (SWITCH_ENUM_CAST (code))
2506 case Sescape:
2507 case Scharquote:
2508 if (from == stop)
2509 goto lose;
2510 INC_BOTH (from, from_byte);
2511 /* treat following character as a word constituent */
2512 case Sword:
2513 case Ssymbol:
2514 if (depth || !sexpflag) break;
2515 /* This word counts as a sexp; return at end of it. */
2516 while (from < stop)
2518 UPDATE_SYNTAX_TABLE_FORWARD (from);
2520 /* Some compilers can't handle this inside the switch. */
2521 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2522 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2523 switch (temp)
2525 case Scharquote:
2526 case Sescape:
2527 INC_BOTH (from, from_byte);
2528 if (from == stop)
2529 goto lose;
2530 break;
2531 case Sword:
2532 case Ssymbol:
2533 case Squote:
2534 break;
2535 default:
2536 goto done;
2538 INC_BOTH (from, from_byte);
2540 goto done;
2542 case Scomment_fence:
2543 comstyle = ST_COMMENT_STYLE;
2544 /* FALLTHROUGH */
2545 case Scomment:
2546 if (!parse_sexp_ignore_comments) break;
2547 UPDATE_SYNTAX_TABLE_FORWARD (from);
2548 found = forw_comment (from, from_byte, stop,
2549 comnested, comstyle, 0,
2550 &out_charpos, &out_bytepos, &dummy);
2551 from = out_charpos, from_byte = out_bytepos;
2552 if (!found)
2554 if (depth == 0)
2555 goto done;
2556 goto lose;
2558 INC_BOTH (from, from_byte);
2559 UPDATE_SYNTAX_TABLE_FORWARD (from);
2560 break;
2562 case Smath:
2563 if (!sexpflag)
2564 break;
2565 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2567 INC_BOTH (from, from_byte);
2569 if (mathexit)
2571 mathexit = 0;
2572 goto close1;
2574 mathexit = 1;
2576 case Sopen:
2577 if (!++depth) goto done;
2578 break;
2580 case Sclose:
2581 close1:
2582 if (!--depth) goto done;
2583 if (depth < min_depth)
2584 xsignal3 (Qscan_error,
2585 build_string ("Containing expression ends prematurely"),
2586 make_number (last_good), make_number (from));
2587 break;
2589 case Sstring:
2590 case Sstring_fence:
2591 temp_pos = dec_bytepos (from_byte);
2592 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2593 while (1)
2595 if (from >= stop)
2596 goto lose;
2597 UPDATE_SYNTAX_TABLE_FORWARD (from);
2598 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2599 if (code == Sstring
2600 ? (c == stringterm
2601 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2602 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
2603 break;
2605 /* Some compilers can't handle this inside the switch. */
2606 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2607 switch (temp)
2609 case Scharquote:
2610 case Sescape:
2611 INC_BOTH (from, from_byte);
2613 INC_BOTH (from, from_byte);
2615 INC_BOTH (from, from_byte);
2616 if (!depth && sexpflag) goto done;
2617 break;
2618 default:
2619 /* Ignore whitespace, punctuation, quote, endcomment. */
2620 break;
2624 /* Reached end of buffer. Error if within object, return nil if between */
2625 if (depth)
2626 goto lose;
2628 immediate_quit = 0;
2629 return Qnil;
2631 /* End of object reached */
2632 done:
2633 count--;
2636 while (count < 0)
2638 while (from > stop)
2640 DEC_BOTH (from, from_byte);
2641 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2642 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2643 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2644 if (depth == min_depth)
2645 last_good = from;
2646 comstyle = 0;
2647 comnested = SYNTAX_COMMENT_NESTED (c);
2648 if (code == Sendcomment)
2649 comstyle = SYNTAX_COMMENT_STYLE (c);
2650 if (from > stop && SYNTAX_COMEND_SECOND (c)
2651 && prev_char_comend_first (from, from_byte)
2652 && parse_sexp_ignore_comments)
2654 /* We must record the comment style encountered so that
2655 later, we can match only the proper comment begin
2656 sequence of the same style. */
2657 DEC_BOTH (from, from_byte);
2658 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2659 code = Sendcomment;
2660 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2661 comstyle = SYNTAX_COMMENT_STYLE (c1);
2662 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2665 /* Quoting turns anything except a comment-ender
2666 into a word character. Note that this cannot be true
2667 if we decremented FROM in the if-statement above. */
2668 if (code != Sendcomment && char_quoted (from, from_byte))
2670 DEC_BOTH (from, from_byte);
2671 code = Sword;
2673 else if (SYNTAX_PREFIX (c))
2674 continue;
2676 switch (SWITCH_ENUM_CAST (code))
2678 case Sword:
2679 case Ssymbol:
2680 case Sescape:
2681 case Scharquote:
2682 if (depth || !sexpflag) break;
2683 /* This word counts as a sexp; count object finished
2684 after passing it. */
2685 while (from > stop)
2687 temp_pos = from_byte;
2688 if (! NILP (current_buffer->enable_multibyte_characters))
2689 DEC_POS (temp_pos);
2690 else
2691 temp_pos--;
2692 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2693 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2694 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2695 /* Don't allow comment-end to be quoted. */
2696 if (temp_code == Sendcomment)
2697 goto done2;
2698 quoted = char_quoted (from - 1, temp_pos);
2699 if (quoted)
2701 DEC_BOTH (from, from_byte);
2702 temp_pos = dec_bytepos (temp_pos);
2703 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2705 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2706 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2707 if (! (quoted || temp_code == Sword
2708 || temp_code == Ssymbol
2709 || temp_code == Squote))
2710 goto done2;
2711 DEC_BOTH (from, from_byte);
2713 goto done2;
2715 case Smath:
2716 if (!sexpflag)
2717 break;
2718 temp_pos = dec_bytepos (from_byte);
2719 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2720 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2721 DEC_BOTH (from, from_byte);
2722 if (mathexit)
2724 mathexit = 0;
2725 goto open2;
2727 mathexit = 1;
2729 case Sclose:
2730 if (!++depth) goto done2;
2731 break;
2733 case Sopen:
2734 open2:
2735 if (!--depth) goto done2;
2736 if (depth < min_depth)
2737 xsignal3 (Qscan_error,
2738 build_string ("Containing expression ends prematurely"),
2739 make_number (last_good), make_number (from));
2740 break;
2742 case Sendcomment:
2743 if (!parse_sexp_ignore_comments)
2744 break;
2745 found = back_comment (from, from_byte, stop, comnested, comstyle,
2746 &out_charpos, &out_bytepos);
2747 /* FIXME: if found == -1, then it really wasn't a comment-end.
2748 For single-char Sendcomment, we can't do much about it apart
2749 from skipping the char.
2750 For 2-char endcomments, we could try again, taking both
2751 chars as separate entities, but it's a lot of trouble
2752 for very little gain, so we don't bother either. -sm */
2753 if (found != -1)
2754 from = out_charpos, from_byte = out_bytepos;
2755 break;
2757 case Scomment_fence:
2758 case Sstring_fence:
2759 while (1)
2761 if (from == stop)
2762 goto lose;
2763 DEC_BOTH (from, from_byte);
2764 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2765 if (!char_quoted (from, from_byte)
2766 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2767 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
2768 break;
2770 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2771 break;
2773 case Sstring:
2774 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2775 while (1)
2777 if (from == stop)
2778 goto lose;
2779 DEC_BOTH (from, from_byte);
2780 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2781 if (!char_quoted (from, from_byte)
2782 && (stringterm
2783 == (c = FETCH_CHAR_AS_MULTIBYTE (from_byte)))
2784 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2785 break;
2787 if (!depth && sexpflag) goto done2;
2788 break;
2789 default:
2790 /* Ignore whitespace, punctuation, quote, endcomment. */
2791 break;
2795 /* Reached start of buffer. Error if within object, return nil if between */
2796 if (depth)
2797 goto lose;
2799 immediate_quit = 0;
2800 return Qnil;
2802 done2:
2803 count++;
2807 immediate_quit = 0;
2808 XSETFASTINT (val, from);
2809 return val;
2811 lose:
2812 xsignal3 (Qscan_error,
2813 build_string ("Unbalanced parentheses"),
2814 make_number (last_good), make_number (from));
2817 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2818 doc: /* Scan from character number FROM by COUNT lists.
2819 Returns the character number of the position thus found.
2821 If DEPTH is nonzero, paren depth begins counting from that value,
2822 only places where the depth in parentheses becomes zero
2823 are candidates for stopping; COUNT such places are counted.
2824 Thus, a positive value for DEPTH means go out levels.
2826 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2828 If the beginning or end of (the accessible part of) the buffer is reached
2829 and the depth is wrong, an error is signaled.
2830 If the depth is right but the count is not used up, nil is returned. */)
2831 (from, count, depth)
2832 Lisp_Object from, count, depth;
2834 CHECK_NUMBER (from);
2835 CHECK_NUMBER (count);
2836 CHECK_NUMBER (depth);
2838 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2841 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2842 doc: /* Scan from character number FROM by COUNT balanced expressions.
2843 If COUNT is negative, scan backwards.
2844 Returns the character number of the position thus found.
2846 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2848 If the beginning or end of (the accessible part of) the buffer is reached
2849 in the middle of a parenthetical grouping, an error is signaled.
2850 If the beginning or end is reached between groupings
2851 but before count is used up, nil is returned. */)
2852 (from, count)
2853 Lisp_Object from, count;
2855 CHECK_NUMBER (from);
2856 CHECK_NUMBER (count);
2858 return scan_lists (XINT (from), XINT (count), 0, 1);
2861 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2862 0, 0, 0,
2863 doc: /* Move point backward over any number of chars with prefix syntax.
2864 This includes chars with "quote" or "prefix" syntax (' or p). */)
2867 int beg = BEGV;
2868 int opoint = PT;
2869 int opoint_byte = PT_BYTE;
2870 int pos = PT;
2871 int pos_byte = PT_BYTE;
2872 int c;
2874 if (pos <= beg)
2876 SET_PT_BOTH (opoint, opoint_byte);
2878 return Qnil;
2881 SETUP_SYNTAX_TABLE (pos, -1);
2883 DEC_BOTH (pos, pos_byte);
2885 while (!char_quoted (pos, pos_byte)
2886 /* Previous statement updates syntax table. */
2887 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
2888 || SYNTAX_PREFIX (c)))
2890 opoint = pos;
2891 opoint_byte = pos_byte;
2893 if (pos + 1 > beg)
2894 DEC_BOTH (pos, pos_byte);
2897 SET_PT_BOTH (opoint, opoint_byte);
2899 return Qnil;
2902 /* Parse forward from FROM / FROM_BYTE to END,
2903 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2904 and return a description of the state of the parse at END.
2905 If STOPBEFORE is nonzero, stop at the start of an atom.
2906 If COMMENTSTOP is 1, stop at the start of a comment.
2907 If COMMENTSTOP is -1, stop at the start or end of a comment,
2908 after the beginning of a string, or after the end of a string. */
2910 static void
2911 scan_sexps_forward (stateptr, from, from_byte, end, targetdepth,
2912 stopbefore, oldstate, commentstop)
2913 struct lisp_parse_state *stateptr;
2914 register EMACS_INT from;
2915 EMACS_INT from_byte, end;
2916 int targetdepth, stopbefore;
2917 Lisp_Object oldstate;
2918 int commentstop;
2920 struct lisp_parse_state state;
2922 register enum syntaxcode code;
2923 int c1;
2924 int comnested;
2925 struct level { int last, prev; };
2926 struct level levelstart[100];
2927 register struct level *curlevel = levelstart;
2928 struct level *endlevel = levelstart + 100;
2929 register int depth; /* Paren depth of current scanning location.
2930 level - levelstart equals this except
2931 when the depth becomes negative. */
2932 int mindepth; /* Lowest DEPTH value seen. */
2933 int start_quoted = 0; /* Nonzero means starting after a char quote */
2934 Lisp_Object tem;
2935 EMACS_INT prev_from; /* Keep one character before FROM. */
2936 EMACS_INT prev_from_byte;
2937 int prev_from_syntax;
2938 int boundary_stop = commentstop == -1;
2939 int nofence;
2940 int found;
2941 EMACS_INT out_bytepos, out_charpos;
2942 int temp;
2944 prev_from = from;
2945 prev_from_byte = from_byte;
2946 if (from != BEGV)
2947 DEC_BOTH (prev_from, prev_from_byte);
2949 /* Use this macro instead of `from++'. */
2950 #define INC_FROM \
2951 do { prev_from = from; \
2952 prev_from_byte = from_byte; \
2953 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2954 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2955 INC_BOTH (from, from_byte); \
2956 if (from < end) \
2957 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2958 } while (0)
2960 immediate_quit = 1;
2961 QUIT;
2963 if (NILP (oldstate))
2965 depth = 0;
2966 state.instring = -1;
2967 state.incomment = 0;
2968 state.comstyle = 0; /* comment style a by default. */
2969 state.comstr_start = -1; /* no comment/string seen. */
2971 else
2973 tem = Fcar (oldstate);
2974 if (!NILP (tem))
2975 depth = XINT (tem);
2976 else
2977 depth = 0;
2979 oldstate = Fcdr (oldstate);
2980 oldstate = Fcdr (oldstate);
2981 oldstate = Fcdr (oldstate);
2982 tem = Fcar (oldstate);
2983 /* Check whether we are inside string_fence-style string: */
2984 state.instring = (!NILP (tem)
2985 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
2986 : -1);
2988 oldstate = Fcdr (oldstate);
2989 tem = Fcar (oldstate);
2990 state.incomment = (!NILP (tem)
2991 ? (INTEGERP (tem) ? XINT (tem) : -1)
2992 : 0);
2994 oldstate = Fcdr (oldstate);
2995 tem = Fcar (oldstate);
2996 start_quoted = !NILP (tem);
2998 /* if the eighth element of the list is nil, we are in comment
2999 style a. If it is non-nil, we are in comment style b */
3000 oldstate = Fcdr (oldstate);
3001 oldstate = Fcdr (oldstate);
3002 tem = Fcar (oldstate);
3003 state.comstyle = NILP (tem) ? 0 : (EQ (tem, Qsyntax_table)
3004 ? ST_COMMENT_STYLE : 1);
3006 oldstate = Fcdr (oldstate);
3007 tem = Fcar (oldstate);
3008 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
3009 oldstate = Fcdr (oldstate);
3010 tem = Fcar (oldstate);
3011 while (!NILP (tem)) /* >= second enclosing sexps. */
3013 /* curlevel++->last ran into compiler bug on Apollo */
3014 curlevel->last = XINT (Fcar (tem));
3015 if (++curlevel == endlevel)
3016 curlevel--; /* error ("Nesting too deep for parser"); */
3017 curlevel->prev = -1;
3018 curlevel->last = -1;
3019 tem = Fcdr (tem);
3022 state.quoted = 0;
3023 mindepth = depth;
3025 curlevel->prev = -1;
3026 curlevel->last = -1;
3028 SETUP_SYNTAX_TABLE (prev_from, 1);
3029 temp = FETCH_CHAR (prev_from_byte);
3030 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
3031 UPDATE_SYNTAX_TABLE_FORWARD (from);
3033 /* Enter the loop at a place appropriate for initial state. */
3035 if (state.incomment)
3036 goto startincomment;
3037 if (state.instring >= 0)
3039 nofence = state.instring != ST_STRING_STYLE;
3040 if (start_quoted)
3041 goto startquotedinstring;
3042 goto startinstring;
3044 else if (start_quoted)
3045 goto startquoted;
3047 while (from < end)
3049 INC_FROM;
3050 code = prev_from_syntax & 0xff;
3052 if (from < end
3053 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3054 && (c1 = FETCH_CHAR (from_byte),
3055 SYNTAX_COMSTART_SECOND (c1)))
3056 /* Duplicate code to avoid a complex if-expression
3057 which causes trouble for the SGI compiler. */
3059 /* Record the comment style we have entered so that only
3060 the comment-end sequence of the same style actually
3061 terminates the comment section. */
3062 state.comstyle = SYNTAX_COMMENT_STYLE (c1);
3063 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
3064 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
3065 state.incomment = comnested ? 1 : -1;
3066 state.comstr_start = prev_from;
3067 INC_FROM;
3068 code = Scomment;
3070 else if (code == Scomment_fence)
3072 /* Record the comment style we have entered so that only
3073 the comment-end sequence of the same style actually
3074 terminates the comment section. */
3075 state.comstyle = ST_COMMENT_STYLE;
3076 state.incomment = -1;
3077 state.comstr_start = prev_from;
3078 code = Scomment;
3080 else if (code == Scomment)
3082 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax);
3083 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3084 1 : -1);
3085 state.comstr_start = prev_from;
3088 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3089 continue;
3090 switch (SWITCH_ENUM_CAST (code))
3092 case Sescape:
3093 case Scharquote:
3094 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3095 curlevel->last = prev_from;
3096 startquoted:
3097 if (from == end) goto endquoted;
3098 INC_FROM;
3099 goto symstarted;
3100 /* treat following character as a word constituent */
3101 case Sword:
3102 case Ssymbol:
3103 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3104 curlevel->last = prev_from;
3105 symstarted:
3106 while (from < end)
3108 /* Some compilers can't handle this inside the switch. */
3109 temp = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3110 temp = SYNTAX (temp);
3111 switch (temp)
3113 case Scharquote:
3114 case Sescape:
3115 INC_FROM;
3116 if (from == end) goto endquoted;
3117 break;
3118 case Sword:
3119 case Ssymbol:
3120 case Squote:
3121 break;
3122 default:
3123 goto symdone;
3125 INC_FROM;
3127 symdone:
3128 curlevel->prev = curlevel->last;
3129 break;
3131 case Scomment_fence: /* Can't happen because it's handled above. */
3132 case Scomment:
3133 if (commentstop || boundary_stop) goto done;
3134 startincomment:
3135 /* The (from == BEGV) test was to enter the loop in the middle so
3136 that we find a 2-char comment ender even if we start in the
3137 middle of it. We don't want to do that if we're just at the
3138 beginning of the comment (think of (*) ... (*)). */
3139 found = forw_comment (from, from_byte, end,
3140 state.incomment, state.comstyle,
3141 (from == BEGV || from < state.comstr_start + 3)
3142 ? 0 : prev_from_syntax,
3143 &out_charpos, &out_bytepos, &state.incomment);
3144 from = out_charpos; from_byte = out_bytepos;
3145 /* Beware! prev_from and friends are invalid now.
3146 Luckily, the `done' doesn't use them and the INC_FROM
3147 sets them to a sane value without looking at them. */
3148 if (!found) goto done;
3149 INC_FROM;
3150 state.incomment = 0;
3151 state.comstyle = 0; /* reset the comment style */
3152 if (boundary_stop) goto done;
3153 break;
3155 case Sopen:
3156 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3157 depth++;
3158 /* curlevel++->last ran into compiler bug on Apollo */
3159 curlevel->last = prev_from;
3160 if (++curlevel == endlevel)
3161 curlevel--; /* error ("Nesting too deep for parser"); */
3162 curlevel->prev = -1;
3163 curlevel->last = -1;
3164 if (targetdepth == depth) goto done;
3165 break;
3167 case Sclose:
3168 depth--;
3169 if (depth < mindepth)
3170 mindepth = depth;
3171 if (curlevel != levelstart)
3172 curlevel--;
3173 curlevel->prev = curlevel->last;
3174 if (targetdepth == depth) goto done;
3175 break;
3177 case Sstring:
3178 case Sstring_fence:
3179 state.comstr_start = from - 1;
3180 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3181 curlevel->last = prev_from;
3182 state.instring = (code == Sstring
3183 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3184 : ST_STRING_STYLE);
3185 if (boundary_stop) goto done;
3186 startinstring:
3188 nofence = state.instring != ST_STRING_STYLE;
3190 while (1)
3192 int c;
3194 if (from >= end) goto done;
3195 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3196 /* Some compilers can't handle this inside the switch. */
3197 temp = SYNTAX (c);
3199 /* Check TEMP here so that if the char has
3200 a syntax-table property which says it is NOT
3201 a string character, it does not end the string. */
3202 if (nofence && c == state.instring && temp == Sstring)
3203 break;
3205 switch (temp)
3207 case Sstring_fence:
3208 if (!nofence) goto string_end;
3209 break;
3210 case Scharquote:
3211 case Sescape:
3212 INC_FROM;
3213 startquotedinstring:
3214 if (from >= end) goto endquoted;
3216 INC_FROM;
3219 string_end:
3220 state.instring = -1;
3221 curlevel->prev = curlevel->last;
3222 INC_FROM;
3223 if (boundary_stop) goto done;
3224 break;
3226 case Smath:
3227 /* FIXME: We should do something with it. */
3228 break;
3229 default:
3230 /* Ignore whitespace, punctuation, quote, endcomment. */
3231 break;
3234 goto done;
3236 stop: /* Here if stopping before start of sexp. */
3237 from = prev_from; /* We have just fetched the char that starts it; */
3238 goto done; /* but return the position before it. */
3240 endquoted:
3241 state.quoted = 1;
3242 done:
3243 state.depth = depth;
3244 state.mindepth = mindepth;
3245 state.thislevelstart = curlevel->prev;
3246 state.prevlevelstart
3247 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3248 state.location = from;
3249 state.levelstarts = Qnil;
3250 while (--curlevel >= levelstart)
3251 state.levelstarts = Fcons (make_number (curlevel->last),
3252 state.levelstarts);
3253 immediate_quit = 0;
3255 *stateptr = state;
3258 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3259 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3260 Parsing stops at TO or when certain criteria are met;
3261 point is set to where parsing stops.
3262 If fifth arg OLDSTATE is omitted or nil,
3263 parsing assumes that FROM is the beginning of a function.
3264 Value is a list of elements describing final state of parsing:
3265 0. depth in parens.
3266 1. character address of start of innermost containing list; nil if none.
3267 2. character address of start of last complete sexp terminated.
3268 3. non-nil if inside a string.
3269 (it is the character that will terminate the string,
3270 or t if the string should be terminated by a generic string delimiter.)
3271 4. nil if outside a comment, t if inside a non-nestable comment,
3272 else an integer (the current comment nesting).
3273 5. t if following a quote character.
3274 6. the minimum paren-depth encountered during this scan.
3275 7. t if in a comment of style b; symbol `syntax-table' if the comment
3276 should be terminated by a generic comment delimiter.
3277 8. character address of start of comment or string; nil if not in one.
3278 9. Intermediate data for continuation of parsing (subject to change).
3279 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3280 in parentheses becomes equal to TARGETDEPTH.
3281 Fourth arg STOPBEFORE non-nil means stop when come to
3282 any character that starts a sexp.
3283 Fifth arg OLDSTATE is a list like what this function returns.
3284 It is used to initialize the state of the parse. Elements number 1, 2, 6
3285 and 8 are ignored.
3286 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3287 If it is symbol `syntax-table', stop after the start of a comment or a
3288 string, or after end of a comment or a string. */)
3289 (from, to, targetdepth, stopbefore, oldstate, commentstop)
3290 Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop;
3292 struct lisp_parse_state state;
3293 int target;
3295 if (!NILP (targetdepth))
3297 CHECK_NUMBER (targetdepth);
3298 target = XINT (targetdepth);
3300 else
3301 target = -100000; /* We won't reach this depth */
3303 validate_region (&from, &to);
3304 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3305 XINT (to),
3306 target, !NILP (stopbefore), oldstate,
3307 (NILP (commentstop)
3308 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3310 SET_PT (state.location);
3312 return Fcons (make_number (state.depth),
3313 Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart),
3314 Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart),
3315 Fcons (state.instring >= 0
3316 ? (state.instring == ST_STRING_STYLE
3317 ? Qt : make_number (state.instring)) : Qnil,
3318 Fcons (state.incomment < 0 ? Qt :
3319 (state.incomment == 0 ? Qnil :
3320 make_number (state.incomment)),
3321 Fcons (state.quoted ? Qt : Qnil,
3322 Fcons (make_number (state.mindepth),
3323 Fcons ((state.comstyle
3324 ? (state.comstyle == ST_COMMENT_STYLE
3325 ? Qsyntax_table : Qt) :
3326 Qnil),
3327 Fcons (((state.incomment
3328 || (state.instring >= 0))
3329 ? make_number (state.comstr_start)
3330 : Qnil),
3331 Fcons (state.levelstarts, Qnil))))))))));
3334 void
3335 init_syntax_once ()
3337 register int i, c;
3338 Lisp_Object temp;
3340 /* This has to be done here, before we call Fmake_char_table. */
3341 Qsyntax_table = intern_c_string ("syntax-table");
3342 staticpro (&Qsyntax_table);
3344 /* Intern_C_String this now in case it isn't already done.
3345 Setting this variable twice is harmless.
3346 But don't staticpro it here--that is done in alloc.c. */
3347 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
3349 /* Create objects which can be shared among syntax tables. */
3350 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
3351 for (i = 0; i < XVECTOR_SIZE (Vsyntax_code_object); i++)
3352 XVECTOR (Vsyntax_code_object)->contents[i]
3353 = Fcons (make_number (i), Qnil);
3355 /* Now we are ready to set up this property, so we can
3356 create syntax tables. */
3357 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3359 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3361 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3363 /* Control characters should not be whitespace. */
3364 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3365 for (i = 0; i <= ' ' - 1; i++)
3366 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3367 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3369 /* Except that a few really are whitespace. */
3370 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3371 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3372 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3373 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3374 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3375 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3377 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3378 for (i = 'a'; i <= 'z'; i++)
3379 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3380 for (i = 'A'; i <= 'Z'; i++)
3381 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3382 for (i = '0'; i <= '9'; i++)
3383 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3385 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3386 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3388 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3389 Fcons (make_number (Sopen), make_number (')')));
3390 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3391 Fcons (make_number (Sclose), make_number ('(')));
3392 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3393 Fcons (make_number (Sopen), make_number (']')));
3394 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3395 Fcons (make_number (Sclose), make_number ('[')));
3396 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3397 Fcons (make_number (Sopen), make_number ('}')));
3398 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3399 Fcons (make_number (Sclose), make_number ('{')));
3400 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3401 Fcons (make_number ((int) Sstring), Qnil));
3402 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3403 Fcons (make_number ((int) Sescape), Qnil));
3405 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
3406 for (i = 0; i < 10; i++)
3408 c = "_-+*/&|<>="[i];
3409 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3412 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3413 for (i = 0; i < 12; i++)
3415 c = ".,;:?!#@~^'`"[i];
3416 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3419 /* All multibyte characters have syntax `word' by default. */
3420 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3421 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3424 void
3425 syms_of_syntax ()
3427 Qsyntax_table_p = intern_c_string ("syntax-table-p");
3428 staticpro (&Qsyntax_table_p);
3430 staticpro (&Vsyntax_code_object);
3432 staticpro (&gl_state.object);
3433 staticpro (&gl_state.global_code);
3434 staticpro (&gl_state.current_syntax_table);
3435 staticpro (&gl_state.old_prop);
3437 /* Defined in regex.c */
3438 staticpro (&re_match_object);
3440 Qscan_error = intern_c_string ("scan-error");
3441 staticpro (&Qscan_error);
3442 Fput (Qscan_error, Qerror_conditions,
3443 pure_cons (Qscan_error, pure_cons (Qerror, Qnil)));
3444 Fput (Qscan_error, Qerror_message,
3445 make_pure_c_string ("Scan error"));
3447 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
3448 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3450 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties,
3451 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3452 Otherwise, that text property is simply ignored.
3453 See the info node `(elisp)Syntax Properties' for a description of the
3454 `syntax-table' property. */);
3456 words_include_escapes = 0;
3457 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
3458 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3460 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol,
3461 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3462 multibyte_syntax_as_symbol = 0;
3464 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3465 &open_paren_in_column_0_is_defun_start,
3466 doc: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3467 open_paren_in_column_0_is_defun_start = 1;
3470 DEFVAR_LISP ("find-word-boundary-function-table",
3471 &Vfind_word_boundary_function_table,
3472 doc: /*
3473 Char table of functions to search for the word boundary.
3474 Each function is called with two arguments; POS and LIMIT.
3475 POS and LIMIT are character positions in the current buffer.
3477 If POS is less than LIMIT, POS is at the first character of a word,
3478 and the return value of a function is a position after the last
3479 character of that word.
3481 If POS is not less than LIMIT, POS is at the last character of a word,
3482 and the return value of a function is a position at the first
3483 character of that word.
3485 In both cases, LIMIT bounds the search. */);
3486 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3488 defsubr (&Ssyntax_table_p);
3489 defsubr (&Ssyntax_table);
3490 defsubr (&Sstandard_syntax_table);
3491 defsubr (&Scopy_syntax_table);
3492 defsubr (&Sset_syntax_table);
3493 defsubr (&Schar_syntax);
3494 defsubr (&Smatching_paren);
3495 defsubr (&Sstring_to_syntax);
3496 defsubr (&Smodify_syntax_entry);
3497 defsubr (&Sinternal_describe_syntax_value);
3499 defsubr (&Sforward_word);
3501 defsubr (&Sskip_chars_forward);
3502 defsubr (&Sskip_chars_backward);
3503 defsubr (&Sskip_syntax_forward);
3504 defsubr (&Sskip_syntax_backward);
3506 defsubr (&Sforward_comment);
3507 defsubr (&Sscan_lists);
3508 defsubr (&Sscan_sexps);
3509 defsubr (&Sbackward_prefix_chars);
3510 defsubr (&Sparse_partial_sexp);
3513 /* arch-tag: 3e297b9f-088e-4b64-8f4c-fb0b3443e412
3514 (do not change this comment) */