(tags-query-replace): Set arg `map' of `perform-replace'
[emacs.git] / src / syntax.c
blob87cf2da2e39515522dbb861d02c01f0a7ad0bd83
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
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 "lisp.h"
25 #include "commands.h"
26 #include "buffer.h"
27 #include "character.h"
28 #include "keymap.h"
29 #include "regex.h"
31 /* Make syntax table lookup grant data in gl_state. */
32 #define SYNTAX_ENTRY_VIA_PROPERTY
34 #include "syntax.h"
35 #include "intervals.h"
37 /* We use these constants in place for comment-style and
38 string-ender-char to distinguish comments/strings started by
39 comment_fence and string_fence codes. */
41 #define ST_COMMENT_STYLE (256 + 1)
42 #define ST_STRING_STYLE (256 + 2)
43 #include "category.h"
45 Lisp_Object Qsyntax_table_p, Qsyntax_table, Qscan_error;
47 int words_include_escapes;
48 int parse_sexp_lookup_properties;
50 /* Nonzero means `scan-sexps' treat all multibyte characters as symbol. */
51 int multibyte_syntax_as_symbol;
53 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
54 if not compiled with GCC. No need to mark it, since it is used
55 only very temporarily. */
56 Lisp_Object syntax_temp;
58 /* Non-zero means an open parenthesis in column 0 is always considered
59 to be the start of a defun. Zero means an open parenthesis in
60 column 0 has no special meaning. */
62 int open_paren_in_column_0_is_defun_start;
64 /* This is the internal form of the parse state used in parse-partial-sexp. */
66 struct lisp_parse_state
68 int depth; /* Depth at end of parsing. */
69 int instring; /* -1 if not within string, else desired terminator. */
70 int incomment; /* -1 if in unnestable comment else comment nesting */
71 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
72 int quoted; /* Nonzero if just after an escape char at end of parsing */
73 int thislevelstart; /* Char number of most recent start-of-expression at current level */
74 int prevlevelstart; /* Char number of start of containing expression */
75 int location; /* Char number at which parsing stopped. */
76 int mindepth; /* Minimum depth seen while scanning. */
77 int comstr_start; /* Position just after last comment/string starter. */
78 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
79 of levels (starting from outermost). */
82 /* These variables are a cache for finding the start of a defun.
83 find_start_pos is the place for which the defun start was found.
84 find_start_value is the defun start position found for it.
85 find_start_value_byte is the corresponding byte position.
86 find_start_buffer is the buffer it was found in.
87 find_start_begv is the BEGV value when it was found.
88 find_start_modiff is the value of MODIFF when it was found. */
90 static EMACS_INT find_start_pos;
91 static EMACS_INT find_start_value;
92 static EMACS_INT find_start_value_byte;
93 static struct buffer *find_start_buffer;
94 static EMACS_INT find_start_begv;
95 static int find_start_modiff;
98 static int find_defun_start P_ ((EMACS_INT, EMACS_INT));
99 static int back_comment P_ ((EMACS_INT, EMACS_INT, EMACS_INT, int, int,
100 EMACS_INT *, EMACS_INT *));
101 static int char_quoted P_ ((int, int));
102 static Lisp_Object skip_chars P_ ((int, Lisp_Object, Lisp_Object, int));
103 static Lisp_Object skip_syntaxes P_ ((int, Lisp_Object, Lisp_Object));
104 static Lisp_Object scan_lists P_ ((EMACS_INT, EMACS_INT, EMACS_INT, int));
105 static void scan_sexps_forward P_ ((struct lisp_parse_state *,
106 EMACS_INT, EMACS_INT, EMACS_INT, int,
107 int, Lisp_Object, int));
108 static int in_classes P_ ((int, Lisp_Object));
111 struct gl_state_s gl_state; /* Global state of syntax parser. */
113 INTERVAL interval_of ();
114 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
115 to scan to property-change. */
117 /* Update gl_state to an appropriate interval which contains CHARPOS. The
118 sign of COUNT give the relative position of CHARPOS wrt the previously
119 valid interval. If INIT, only [be]_property fields of gl_state are
120 valid at start, the rest is filled basing on OBJECT.
122 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
123 direction than the intervals - or in an interval. We update the
124 current syntax-table basing on the property of this interval, and
125 update the interval to start further than CHARPOS - or be
126 NULL_INTERVAL. We also update lim_property to be the next value of
127 charpos to call this subroutine again - or be before/after the
128 start/end of OBJECT. */
130 void
131 update_syntax_table (charpos, count, init, object)
132 int charpos, count, init;
133 Lisp_Object object;
135 Lisp_Object tmp_table;
136 int cnt = 0, invalidate = 1;
137 INTERVAL i;
139 if (init)
141 gl_state.old_prop = Qnil;
142 gl_state.start = gl_state.b_property;
143 gl_state.stop = gl_state.e_property;
144 i = interval_of (charpos, object);
145 gl_state.backward_i = gl_state.forward_i = i;
146 invalidate = 0;
147 if (NULL_INTERVAL_P (i))
148 return;
149 /* interval_of updates only ->position of the return value, so
150 update the parents manually to speed up update_interval. */
151 while (!NULL_PARENT (i))
153 if (AM_RIGHT_CHILD (i))
154 INTERVAL_PARENT (i)->position = i->position
155 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
156 - TOTAL_LENGTH (INTERVAL_PARENT (i))
157 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
158 else
159 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
160 + TOTAL_LENGTH (i);
161 i = INTERVAL_PARENT (i);
163 i = gl_state.forward_i;
164 gl_state.b_property = i->position - gl_state.offset;
165 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
166 goto update;
168 i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
170 /* We are guaranteed to be called with CHARPOS either in i,
171 or further off. */
172 if (NULL_INTERVAL_P (i))
173 error ("Error in syntax_table logic for to-the-end intervals");
174 else if (charpos < i->position) /* Move left. */
176 if (count > 0)
177 error ("Error in syntax_table logic for intervals <-");
178 /* Update the interval. */
179 i = update_interval (i, charpos);
180 if (INTERVAL_LAST_POS (i) != gl_state.b_property)
182 invalidate = 0;
183 gl_state.forward_i = i;
184 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
187 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
189 if (count < 0)
190 error ("Error in syntax_table logic for intervals ->");
191 /* Update the interval. */
192 i = update_interval (i, charpos);
193 if (i->position != gl_state.e_property)
195 invalidate = 0;
196 gl_state.backward_i = i;
197 gl_state.b_property = i->position - gl_state.offset;
201 update:
202 tmp_table = textget (i->plist, Qsyntax_table);
204 if (invalidate)
205 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
207 if (invalidate) /* Did not get to adjacent interval. */
208 { /* with the same table => */
209 /* invalidate the old range. */
210 if (count > 0)
212 gl_state.backward_i = i;
213 gl_state.b_property = i->position - gl_state.offset;
215 else
217 gl_state.forward_i = i;
218 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
222 if (!EQ (tmp_table, gl_state.old_prop))
224 gl_state.current_syntax_table = tmp_table;
225 gl_state.old_prop = tmp_table;
226 if (EQ (Fsyntax_table_p (tmp_table), Qt))
228 gl_state.use_global = 0;
230 else if (CONSP (tmp_table))
232 gl_state.use_global = 1;
233 gl_state.global_code = tmp_table;
235 else
237 gl_state.use_global = 0;
238 gl_state.current_syntax_table = current_buffer->syntax_table;
242 while (!NULL_INTERVAL_P (i))
244 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
246 if (count > 0)
248 gl_state.e_property = i->position - gl_state.offset;
249 gl_state.forward_i = i;
251 else
253 gl_state.b_property
254 = i->position + LENGTH (i) - gl_state.offset;
255 gl_state.backward_i = i;
257 return;
259 else if (cnt == INTERVALS_AT_ONCE)
261 if (count > 0)
263 gl_state.e_property
264 = i->position + LENGTH (i) - gl_state.offset
265 /* e_property at EOB is not set to ZV but to ZV+1, so that
266 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
267 having to check eob between the two. */
268 + (NULL_INTERVAL_P (next_interval (i)) ? 1 : 0);
269 gl_state.forward_i = i;
271 else
273 gl_state.b_property = i->position - gl_state.offset;
274 gl_state.backward_i = i;
276 return;
278 cnt++;
279 i = count > 0 ? next_interval (i) : previous_interval (i);
281 eassert (NULL_INTERVAL_P (i)); /* This property goes to the end. */
282 if (count > 0)
283 gl_state.e_property = gl_state.stop;
284 else
285 gl_state.b_property = gl_state.start;
288 /* Returns TRUE if char at CHARPOS is quoted.
289 Global syntax-table data should be set up already to be good at CHARPOS
290 or after. On return global syntax data is good for lookup at CHARPOS. */
292 static int
293 char_quoted (charpos, bytepos)
294 register int charpos, bytepos;
296 register enum syntaxcode code;
297 register int beg = BEGV;
298 register int quoted = 0;
299 int orig = charpos;
301 while (charpos > beg)
303 int c;
304 DEC_BOTH (charpos, bytepos);
306 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
307 c = FETCH_CHAR_AS_MULTIBYTE (bytepos);
308 code = SYNTAX (c);
309 if (! (code == Scharquote || code == Sescape))
310 break;
312 quoted = !quoted;
315 UPDATE_SYNTAX_TABLE (orig);
316 return quoted;
319 /* Return the bytepos one character after BYTEPOS.
320 We assume that BYTEPOS is not at the end of the buffer. */
322 INLINE int
323 inc_bytepos (bytepos)
324 int bytepos;
326 if (NILP (current_buffer->enable_multibyte_characters))
327 return bytepos + 1;
329 INC_POS (bytepos);
330 return bytepos;
333 /* Return the bytepos one character before BYTEPOS.
334 We assume that BYTEPOS is not at the start of the buffer. */
336 INLINE int
337 dec_bytepos (bytepos)
338 int bytepos;
340 if (NILP (current_buffer->enable_multibyte_characters))
341 return bytepos - 1;
343 DEC_POS (bytepos);
344 return bytepos;
347 /* Return a defun-start position before before POS and not too far before.
348 It should be the last one before POS, or nearly the last.
350 When open_paren_in_column_0_is_defun_start is nonzero,
351 only the beginning of the buffer is treated as a defun-start.
353 We record the information about where the scan started
354 and what its result was, so that another call in the same area
355 can return the same value very quickly.
357 There is no promise at which position the global syntax data is
358 valid on return from the subroutine, so the caller should explicitly
359 update the global data. */
361 static int
362 find_defun_start (pos, pos_byte)
363 EMACS_INT pos, pos_byte;
365 EMACS_INT opoint = PT, opoint_byte = PT_BYTE;
367 if (!open_paren_in_column_0_is_defun_start)
369 find_start_value_byte = BEGV_BYTE;
370 return BEGV;
373 /* Use previous finding, if it's valid and applies to this inquiry. */
374 if (current_buffer == find_start_buffer
375 /* Reuse the defun-start even if POS is a little farther on.
376 POS might be in the next defun, but that's ok.
377 Our value may not be the best possible, but will still be usable. */
378 && pos <= find_start_pos + 1000
379 && pos >= find_start_value
380 && BEGV == find_start_begv
381 && MODIFF == find_start_modiff)
382 return find_start_value;
384 /* Back up to start of line. */
385 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
387 /* We optimize syntax-table lookup for rare updates. Thus we accept
388 only those `^\s(' which are good in global _and_ text-property
389 syntax-tables. */
390 gl_state.current_syntax_table = current_buffer->syntax_table;
391 gl_state.use_global = 0;
392 while (PT > BEGV)
394 int c;
396 /* Open-paren at start of line means we may have found our
397 defun-start. */
398 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
399 if (SYNTAX (c) == Sopen)
401 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
402 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
403 if (SYNTAX (c) == Sopen)
404 break;
405 /* Now fallback to the default value. */
406 gl_state.current_syntax_table = current_buffer->syntax_table;
407 gl_state.use_global = 0;
409 /* Move to beg of previous line. */
410 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
413 /* Record what we found, for the next try. */
414 find_start_value = PT;
415 find_start_value_byte = PT_BYTE;
416 find_start_buffer = current_buffer;
417 find_start_modiff = MODIFF;
418 find_start_begv = BEGV;
419 find_start_pos = pos;
421 TEMP_SET_PT_BOTH (opoint, opoint_byte);
423 return find_start_value;
426 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
428 static int
429 prev_char_comend_first (pos, pos_byte)
430 int pos, pos_byte;
432 int c, val;
434 DEC_BOTH (pos, pos_byte);
435 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
436 c = FETCH_CHAR (pos_byte);
437 val = SYNTAX_COMEND_FIRST (c);
438 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
439 return val;
442 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
444 /* static int
445 * prev_char_comstart_first (pos, pos_byte)
446 * int pos, pos_byte;
448 * int c, val;
450 * DEC_BOTH (pos, pos_byte);
451 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
452 * c = FETCH_CHAR (pos_byte);
453 * val = SYNTAX_COMSTART_FIRST (c);
454 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
455 * return val;
456 * } */
458 /* Checks whether charpos FROM is at the end of a comment.
459 FROM_BYTE is the bytepos corresponding to FROM.
460 Do not move back before STOP.
462 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
463 return -1 otherwise.
465 If successful, store the charpos of the comment's beginning
466 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
468 Global syntax data remains valid for backward search starting at
469 the returned value (or at FROM, if the search was not successful). */
471 static int
472 back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_ptr)
473 EMACS_INT from, from_byte, stop;
474 int comnested, comstyle;
475 EMACS_INT *charpos_ptr, *bytepos_ptr;
477 /* Look back, counting the parity of string-quotes,
478 and recording the comment-starters seen.
479 When we reach a safe place, assume that's not in a string;
480 then step the main scan to the earliest comment-starter seen
481 an even number of string quotes away from the safe place.
483 OFROM[I] is position of the earliest comment-starter seen
484 which is I+2X quotes from the comment-end.
485 PARITY is current parity of quotes from the comment end. */
486 int string_style = -1; /* Presumed outside of any string. */
487 int string_lossage = 0;
488 /* Not a real lossage: indicates that we have passed a matching comment
489 starter plus a non-matching comment-ender, meaning that any matching
490 comment-starter we might see later could be a false positive (hidden
491 inside another comment).
492 Test case: { a (* b } c (* d *) */
493 int comment_lossage = 0;
494 EMACS_INT comment_end = from;
495 EMACS_INT comment_end_byte = from_byte;
496 EMACS_INT comstart_pos = 0;
497 EMACS_INT comstart_byte;
498 /* Place where the containing defun starts,
499 or 0 if we didn't come across it yet. */
500 EMACS_INT defun_start = 0;
501 EMACS_INT defun_start_byte = 0;
502 register enum syntaxcode code;
503 int nesting = 1; /* current comment nesting */
504 int c;
505 int syntax = 0;
507 /* FIXME: A }} comment-ender style leads to incorrect behavior
508 in the case of {{ c }}} because we ignore the last two chars which are
509 assumed to be comment-enders although they aren't. */
511 /* At beginning of range to scan, we're outside of strings;
512 that determines quote parity to the comment-end. */
513 while (from != stop)
515 int temp_byte, prev_syntax;
516 int com2start, com2end;
518 /* Move back and examine a character. */
519 DEC_BOTH (from, from_byte);
520 UPDATE_SYNTAX_TABLE_BACKWARD (from);
522 prev_syntax = syntax;
523 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
524 syntax = SYNTAX_WITH_FLAGS (c);
525 code = SYNTAX (c);
527 /* Check for 2-char comment markers. */
528 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
529 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
530 && comstyle == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax)
531 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
532 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
533 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
534 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
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. */
547 if (from > stop && (com2end || com2start))
549 int next = from, next_byte = from_byte, next_c, next_syntax;
550 DEC_BOTH (next, next_byte);
551 UPDATE_SYNTAX_TABLE_BACKWARD (next);
552 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
553 next_syntax = SYNTAX_WITH_FLAGS (next_c);
554 if (((com2start || comnested)
555 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
556 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
557 || ((com2end || comnested)
558 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
559 && comstyle == SYNTAX_FLAGS_COMMENT_STYLE (syntax)
560 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
561 goto lossage;
562 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
565 if (com2start && comstart_pos == 0)
566 /* We're looking at a comment starter. But it might be a comment
567 ender as well (see snmp-mode). The first time we see one, we
568 need to consider it as a comment starter,
569 and the subsequent times as a comment ender. */
570 com2end = 0;
572 /* Turn a 2-char comment sequences into the appropriate syntax. */
573 if (com2end)
574 code = Sendcomment;
575 else if (com2start)
576 code = Scomment;
577 /* Ignore comment starters of a different style. */
578 else if (code == Scomment
579 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax)
580 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
581 continue;
583 /* Ignore escaped characters, except comment-enders. */
584 if (code != Sendcomment && char_quoted (from, from_byte))
585 continue;
587 switch (code)
589 case Sstring_fence:
590 case Scomment_fence:
591 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
592 case Sstring:
593 /* Track parity of quotes. */
594 if (string_style == -1)
595 /* Entering a string. */
596 string_style = c;
597 else if (string_style == c)
598 /* Leaving the string. */
599 string_style = -1;
600 else
601 /* If we have two kinds of string delimiters.
602 There's no way to grok this scanning backwards. */
603 string_lossage = 1;
604 break;
606 case Scomment:
607 /* We've already checked that it is the relevant comstyle. */
608 if (string_style != -1 || comment_lossage || string_lossage)
609 /* There are odd string quotes involved, so let's be careful.
610 Test case in Pascal: " { " a { " } */
611 goto lossage;
613 if (!comnested)
615 /* Record best comment-starter so far. */
616 comstart_pos = from;
617 comstart_byte = from_byte;
619 else if (--nesting <= 0)
620 /* nested comments have to be balanced, so we don't need to
621 keep looking for earlier ones. We use here the same (slightly
622 incorrect) reasoning as below: since it is followed by uniform
623 paired string quotes, this comment-start has to be outside of
624 strings, else the comment-end itself would be inside a string. */
625 goto done;
626 break;
628 case Sendcomment:
629 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax) == comstyle
630 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
631 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
632 /* This is the same style of comment ender as ours. */
634 if (comnested)
635 nesting++;
636 else
637 /* Anything before that can't count because it would match
638 this comment-ender rather than ours. */
639 from = stop; /* Break out of the loop. */
641 else if (comstart_pos != 0 || c != '\n')
642 /* We're mixing comment styles here, so we'd better be careful.
643 The (comstart_pos != 0 || c != '\n') check is not quite correct
644 (we should just always set comment_lossage), but removing it
645 would imply that any multiline comment in C would go through
646 lossage, which seems overkill.
647 The failure should only happen in the rare cases such as
648 { (* } *) */
649 comment_lossage = 1;
650 break;
652 case Sopen:
653 /* Assume a defun-start point is outside of strings. */
654 if (open_paren_in_column_0_is_defun_start
655 && (from == stop
656 || (temp_byte = dec_bytepos (from_byte),
657 FETCH_CHAR (temp_byte) == '\n')))
659 defun_start = from;
660 defun_start_byte = from_byte;
661 from = stop; /* Break out of the loop. */
663 break;
665 default:
666 break;
670 if (comstart_pos == 0)
672 from = comment_end;
673 from_byte = comment_end_byte;
674 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
676 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
677 or `done'), then we've found the beginning of the non-nested comment. */
678 else if (1) /* !comnested */
680 from = comstart_pos;
681 from_byte = comstart_byte;
682 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
684 else
686 struct lisp_parse_state state;
687 lossage:
688 /* We had two kinds of string delimiters mixed up
689 together. Decode this going forwards.
690 Scan fwd from a known safe place (beginning-of-defun)
691 to the one in question; this records where we
692 last passed a comment starter. */
693 /* If we did not already find the defun start, find it now. */
694 if (defun_start == 0)
696 defun_start = find_defun_start (comment_end, comment_end_byte);
697 defun_start_byte = find_start_value_byte;
701 scan_sexps_forward (&state,
702 defun_start, defun_start_byte,
703 comment_end, -10000, 0, Qnil, 0);
704 defun_start = comment_end;
705 if (state.incomment == (comnested ? 1 : -1)
706 && state.comstyle == comstyle)
707 from = state.comstr_start;
708 else
710 from = comment_end;
711 if (state.incomment)
712 /* If comment_end is inside some other comment, maybe ours
713 is nested, so we need to try again from within the
714 surrounding comment. Example: { a (* " *) */
716 /* FIXME: We should advance by one or two chars. */
717 defun_start = state.comstr_start + 2;
718 defun_start_byte = CHAR_TO_BYTE (defun_start);
721 } while (defun_start < comment_end);
723 from_byte = CHAR_TO_BYTE (from);
724 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
727 done:
728 *charpos_ptr = from;
729 *bytepos_ptr = from_byte;
731 return (from == comment_end) ? -1 : from;
734 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
735 doc: /* Return t if OBJECT is a syntax table.
736 Currently, any char-table counts as a syntax table. */)
737 (object)
738 Lisp_Object object;
740 if (CHAR_TABLE_P (object)
741 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
742 return Qt;
743 return Qnil;
746 static void
747 check_syntax_table (obj)
748 Lisp_Object obj;
750 CHECK_TYPE (CHAR_TABLE_P (obj) && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table),
751 Qsyntax_table_p, obj);
754 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
755 doc: /* Return the current syntax table.
756 This is the one specified by the current buffer. */)
759 return current_buffer->syntax_table;
762 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
763 Sstandard_syntax_table, 0, 0, 0,
764 doc: /* Return the standard syntax table.
765 This is the one used for new buffers. */)
768 return Vstandard_syntax_table;
771 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
772 doc: /* Construct a new syntax table and return it.
773 It is a copy of the TABLE, which defaults to the standard syntax table. */)
774 (table)
775 Lisp_Object table;
777 Lisp_Object copy;
779 if (!NILP (table))
780 check_syntax_table (table);
781 else
782 table = Vstandard_syntax_table;
784 copy = Fcopy_sequence (table);
786 /* Only the standard syntax table should have a default element.
787 Other syntax tables should inherit from parents instead. */
788 XCHAR_TABLE (copy)->defalt = Qnil;
790 /* Copied syntax tables should all have parents.
791 If we copied one with no parent, such as the standard syntax table,
792 use the standard syntax table as the copy's parent. */
793 if (NILP (XCHAR_TABLE (copy)->parent))
794 Fset_char_table_parent (copy, Vstandard_syntax_table);
795 return copy;
798 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
799 doc: /* Select a new syntax table for the current buffer.
800 One argument, a syntax table. */)
801 (table)
802 Lisp_Object table;
804 int idx;
805 check_syntax_table (table);
806 current_buffer->syntax_table = table;
807 /* Indicate that this buffer now has a specified syntax table. */
808 idx = PER_BUFFER_VAR_IDX (syntax_table);
809 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
810 return table;
813 /* Convert a letter which signifies a syntax code
814 into the code it signifies.
815 This is used by modify-syntax-entry, and other things. */
817 unsigned char syntax_spec_code[0400] =
818 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
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 (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377,
823 (char) Smath, 0377, 0377, (char) Squote,
824 (char) Sopen, (char) Sclose, 0377, 0377,
825 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
826 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
827 0377, 0377, 0377, 0377,
828 (char) Scomment, 0377, (char) Sendcomment, 0377,
829 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
830 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
831 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
832 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
833 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
834 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
835 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
836 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377
839 /* Indexed by syntax code, give the letter that describes it. */
841 char syntax_code_spec[16] =
843 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
844 '!', '|'
847 /* Indexed by syntax code, give the object (cons of syntax code and
848 nil) to be stored in syntax table. Since these objects can be
849 shared among syntax tables, we generate them in advance. By
850 sharing objects, the function `describe-syntax' can give a more
851 compact listing. */
852 static Lisp_Object Vsyntax_code_object;
855 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
856 doc: /* Return the syntax code of CHARACTER, described by a character.
857 For example, if CHARACTER is a word constituent,
858 the character `w' is returned.
859 The characters that correspond to various syntax codes
860 are listed in the documentation of `modify-syntax-entry'. */)
861 (character)
862 Lisp_Object character;
864 int char_int;
865 gl_state.current_syntax_table = current_buffer->syntax_table;
867 gl_state.use_global = 0;
868 CHECK_NUMBER (character);
869 char_int = XINT (character);
870 return make_number (syntax_code_spec[(int) SYNTAX (char_int)]);
873 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
874 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
875 (character)
876 Lisp_Object character;
878 int char_int, code;
879 gl_state.current_syntax_table = current_buffer->syntax_table;
880 gl_state.use_global = 0;
881 CHECK_NUMBER (character);
882 char_int = XINT (character);
883 code = SYNTAX (char_int);
884 if (code == Sopen || code == Sclose)
885 return SYNTAX_MATCH (char_int);
886 return Qnil;
889 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
890 doc: /* Convert a syntax specification STRING into syntax cell form.
891 STRING should be a string as it is allowed as argument of
892 `modify-syntax-entry'. Value is the equivalent cons cell
893 \(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
894 text property. */)
895 (string)
896 Lisp_Object string;
898 register const unsigned char *p;
899 register enum syntaxcode code;
900 int val;
901 Lisp_Object match;
903 CHECK_STRING (string);
905 p = SDATA (string);
906 code = (enum syntaxcode) syntax_spec_code[*p++];
907 if (((int) code & 0377) == 0377)
908 error ("Invalid syntax description letter: %c", p[-1]);
910 if (code == Sinherit)
911 return Qnil;
913 if (*p)
915 int len;
916 int character = (STRING_CHAR_AND_LENGTH
917 (p, SBYTES (string) - 1, 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 (Vsyntax_code_object)->size && 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 and 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 || ! EQ (CHAR_TABLE_REF (Vchar_script_table, ch1), script))
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 || ! EQ (CHAR_TABLE_REF (Vchar_script_table, ch0), script))
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 as 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 = unibyte_char_to_multibyte (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 = unibyte_char_to_multibyte (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, size_byte-i_byte, len);
1594 i_byte += len;
1596 if (handle_iso_classes && c == '['
1597 && i_byte < size_byte
1598 && STRING_CHAR (str + i_byte, size_byte - 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,
1639 size_byte - i_byte, len);
1640 i_byte += len;
1642 /* Treat `-' as range character only if another character
1643 follows. */
1644 if (i_byte + 1 < size_byte
1645 && str[i_byte] == '-')
1647 unsigned int c2;
1648 unsigned char leading_code2;
1650 /* Skip over the dash. */
1651 i_byte++;
1653 /* Get the end of the range. */
1654 leading_code2 = str[i_byte];
1655 c2 = STRING_CHAR_AND_LENGTH (str + i_byte,
1656 size_byte - i_byte, len);
1657 i_byte += len;
1659 if (c2 == '\\'
1660 && i_byte < size_byte)
1662 leading_code2 = str[i_byte];
1663 c2 =STRING_CHAR_AND_LENGTH (str + i_byte, size_byte-i_byte, len);
1664 i_byte += len;
1667 if (c > c2)
1668 continue;
1669 if (ASCII_CHAR_P (c))
1671 while (c <= c2 && c < 0x80)
1672 fastmap[c++] = 1;
1673 leading_code = CHAR_LEADING_CODE (c);
1675 if (! ASCII_CHAR_P (c))
1677 while (leading_code <= leading_code2)
1678 fastmap[leading_code++] = 1;
1679 if (c <= c2)
1681 char_ranges[n_char_ranges++] = c;
1682 char_ranges[n_char_ranges++] = c2;
1686 else
1688 if (ASCII_CHAR_P (c))
1689 fastmap[c] = 1;
1690 else
1692 fastmap[leading_code] = 1;
1693 char_ranges[n_char_ranges++] = c;
1694 char_ranges[n_char_ranges++] = c;
1699 /* If the current range is unibyte and STRING contains non-ASCII
1700 chars, arrange fastmap for the corresponding unibyte
1701 chars. */
1703 if (! multibyte && n_char_ranges > 0)
1705 bzero (fastmap + 0200, 0200);
1706 for (i = 0; i < n_char_ranges; i += 2)
1708 int c1 = char_ranges[i];
1709 int c2 = char_ranges[i + 1];
1711 for (; c1 <= c2; c1++)
1713 int b = CHAR_TO_BYTE_SAFE (c1);
1714 if (b >= 0)
1715 fastmap[b] = 1;
1721 /* If ^ was the first character, complement the fastmap. */
1722 if (negate)
1724 if (! multibyte)
1725 for (i = 0; i < sizeof fastmap; i++)
1726 fastmap[i] ^= 1;
1727 else
1729 for (i = 0; i < 0200; i++)
1730 fastmap[i] ^= 1;
1731 /* All non-ASCII chars possibly match. */
1732 for (; i < sizeof fastmap; i++)
1733 fastmap[i] = 1;
1738 int start_point = PT;
1739 int pos = PT;
1740 int pos_byte = PT_BYTE;
1741 unsigned char *p = PT_ADDR, *endp, *stop;
1743 if (forwardp)
1745 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1746 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1748 else
1750 endp = CHAR_POS_ADDR (XINT (lim));
1751 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1754 immediate_quit = 1;
1755 if (forwardp)
1757 if (multibyte)
1758 while (1)
1760 int nbytes;
1762 if (p >= stop)
1764 if (p >= endp)
1765 break;
1766 p = GAP_END_ADDR;
1767 stop = endp;
1769 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, nbytes);
1770 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1772 if (negate)
1773 break;
1774 else
1775 goto fwd_ok;
1778 if (! fastmap[*p])
1779 break;
1780 if (! ASCII_CHAR_P (c))
1782 /* As we are looking at a multibyte character, we
1783 must look up the character in the table
1784 CHAR_RANGES. If there's no data in the table,
1785 that character is not what we want to skip. */
1787 /* The following code do the right thing even if
1788 n_char_ranges is zero (i.e. no data in
1789 CHAR_RANGES). */
1790 for (i = 0; i < n_char_ranges; i += 2)
1791 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1792 break;
1793 if (!(negate ^ (i < n_char_ranges)))
1794 break;
1796 fwd_ok:
1797 p += nbytes, pos++, pos_byte += nbytes;
1799 else
1800 while (1)
1802 if (p >= stop)
1804 if (p >= endp)
1805 break;
1806 p = GAP_END_ADDR;
1807 stop = endp;
1810 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1812 if (negate)
1813 break;
1814 else
1815 goto fwd_unibyte_ok;
1818 if (!fastmap[*p])
1819 break;
1820 fwd_unibyte_ok:
1821 p++, pos++, pos_byte++;
1824 else
1826 if (multibyte)
1827 while (1)
1829 unsigned char *prev_p;
1831 if (p <= stop)
1833 if (p <= endp)
1834 break;
1835 p = GPT_ADDR;
1836 stop = endp;
1838 prev_p = p;
1839 while (--p >= stop && ! CHAR_HEAD_P (*p));
1840 c = STRING_CHAR (p, MAX_MULTIBYTE_LENGTH);
1842 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1844 if (negate)
1845 break;
1846 else
1847 goto back_ok;
1850 if (! fastmap[*p])
1851 break;
1852 if (! ASCII_CHAR_P (c))
1854 /* See the comment in the previous similar code. */
1855 for (i = 0; i < n_char_ranges; i += 2)
1856 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1857 break;
1858 if (!(negate ^ (i < n_char_ranges)))
1859 break;
1861 back_ok:
1862 pos--, pos_byte -= prev_p - p;
1864 else
1865 while (1)
1867 if (p <= stop)
1869 if (p <= endp)
1870 break;
1871 p = GPT_ADDR;
1872 stop = endp;
1875 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
1877 if (negate)
1878 break;
1879 else
1880 goto back_unibyte_ok;
1883 if (!fastmap[p[-1]])
1884 break;
1885 back_unibyte_ok:
1886 p--, pos--, pos_byte--;
1890 SET_PT_BOTH (pos, pos_byte);
1891 immediate_quit = 0;
1893 return make_number (PT - start_point);
1898 static Lisp_Object
1899 skip_syntaxes (forwardp, string, lim)
1900 int forwardp;
1901 Lisp_Object string, lim;
1903 register unsigned int c;
1904 unsigned char fastmap[0400];
1905 int negate = 0;
1906 register int i, i_byte;
1907 int multibyte;
1908 int size_byte;
1909 unsigned char *str;
1911 CHECK_STRING (string);
1913 if (NILP (lim))
1914 XSETINT (lim, forwardp ? ZV : BEGV);
1915 else
1916 CHECK_NUMBER_COERCE_MARKER (lim);
1918 /* In any case, don't allow scan outside bounds of buffer. */
1919 if (XINT (lim) > ZV)
1920 XSETFASTINT (lim, ZV);
1921 if (XINT (lim) < BEGV)
1922 XSETFASTINT (lim, BEGV);
1924 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
1925 return make_number (0);
1927 multibyte = (!NILP (current_buffer->enable_multibyte_characters)
1928 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1930 bzero (fastmap, sizeof fastmap);
1932 if (SBYTES (string) > SCHARS (string))
1933 /* As this is very rare case (syntax spec is ASCII only), don't
1934 consider efficiency. */
1935 string = string_make_unibyte (string);
1937 str = SDATA (string);
1938 size_byte = SBYTES (string);
1940 i_byte = 0;
1941 if (i_byte < size_byte
1942 && SREF (string, 0) == '^')
1944 negate = 1; i_byte++;
1947 /* Find the syntaxes specified and set their elements of fastmap. */
1949 while (i_byte < size_byte)
1951 c = str[i_byte++];
1952 fastmap[syntax_spec_code[c]] = 1;
1955 /* If ^ was the first character, complement the fastmap. */
1956 if (negate)
1957 for (i = 0; i < sizeof fastmap; i++)
1958 fastmap[i] ^= 1;
1961 int start_point = PT;
1962 int pos = PT;
1963 int pos_byte = PT_BYTE;
1964 unsigned char *p = PT_ADDR, *endp, *stop;
1966 if (forwardp)
1968 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1969 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1971 else
1973 endp = CHAR_POS_ADDR (XINT (lim));
1974 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1977 immediate_quit = 1;
1978 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
1979 if (forwardp)
1981 if (multibyte)
1983 while (1)
1985 int nbytes;
1987 if (p >= stop)
1989 if (p >= endp)
1990 break;
1991 p = GAP_END_ADDR;
1992 stop = endp;
1994 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, nbytes);
1995 if (! fastmap[(int) SYNTAX (c)])
1996 break;
1997 p += nbytes, pos++, pos_byte += nbytes;
1998 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2001 else
2003 while (1)
2005 if (p >= stop)
2007 if (p >= endp)
2008 break;
2009 p = GAP_END_ADDR;
2010 stop = endp;
2012 if (! fastmap[(int) SYNTAX (*p)])
2013 break;
2014 p++, pos++, pos_byte++;
2015 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2019 else
2021 if (multibyte)
2023 while (1)
2025 unsigned char *prev_p;
2027 if (p <= stop)
2029 if (p <= endp)
2030 break;
2031 p = GPT_ADDR;
2032 stop = endp;
2034 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2035 prev_p = p;
2036 while (--p >= stop && ! CHAR_HEAD_P (*p));
2037 c = STRING_CHAR (p, MAX_MULTIBYTE_LENGTH);
2038 if (! fastmap[(int) SYNTAX (c)])
2039 break;
2040 pos--, pos_byte -= prev_p - p;
2043 else
2045 while (1)
2047 if (p <= stop)
2049 if (p <= endp)
2050 break;
2051 p = GPT_ADDR;
2052 stop = endp;
2054 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
2055 if (! fastmap[(int) SYNTAX (p[-1])])
2056 break;
2057 p--, pos--, pos_byte--;
2062 SET_PT_BOTH (pos, pos_byte);
2063 immediate_quit = 0;
2065 return make_number (PT - start_point);
2069 /* Return 1 if character C belongs to one of the ISO classes
2070 in the list ISO_CLASSES. Each class is represented by an
2071 integer which is its type according to re_wctype. */
2073 static int
2074 in_classes (c, iso_classes)
2075 int c;
2076 Lisp_Object iso_classes;
2078 int fits_class = 0;
2080 while (! NILP (iso_classes))
2082 Lisp_Object elt;
2083 elt = XCAR (iso_classes);
2084 iso_classes = XCDR (iso_classes);
2086 if (re_iswctype (c, XFASTINT (elt)))
2087 fits_class = 1;
2090 return fits_class;
2093 /* Jump over a comment, assuming we are at the beginning of one.
2094 FROM is the current position.
2095 FROM_BYTE is the bytepos corresponding to FROM.
2096 Do not move past STOP (a charpos).
2097 The comment over which we have to jump is of style STYLE
2098 (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2099 NESTING should be positive to indicate the nesting at the beginning
2100 for nested comments and should be zero or negative else.
2101 ST_COMMENT_STYLE cannot be nested.
2102 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2103 (or 0 If the search cannot start in the middle of a two-character).
2105 If successful, return 1 and store the charpos of the comment's end
2106 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2107 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2108 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2109 (as defined for state.incomment) in *INCOMMENT_PTR.
2111 The comment end is the last character of the comment rather than the
2112 character just after the comment.
2114 Global syntax data is assumed to initially be valid for FROM and
2115 remains valid for forward search starting at the returned position. */
2117 static int
2118 forw_comment (from, from_byte, stop, nesting, style, prev_syntax,
2119 charpos_ptr, bytepos_ptr, incomment_ptr)
2120 EMACS_INT from, from_byte, stop;
2121 int nesting, style, prev_syntax;
2122 EMACS_INT *charpos_ptr, *bytepos_ptr;
2123 int *incomment_ptr;
2125 register int c, c1;
2126 register enum syntaxcode code;
2127 register int syntax;
2129 if (nesting <= 0) nesting = -1;
2131 /* Enter the loop in the middle so that we find
2132 a 2-char comment ender if we start in the middle of it. */
2133 syntax = prev_syntax;
2134 if (syntax != 0) goto forw_incomment;
2136 while (1)
2138 if (from == stop)
2140 *incomment_ptr = nesting;
2141 *charpos_ptr = from;
2142 *bytepos_ptr = from_byte;
2143 return 0;
2145 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2146 syntax = SYNTAX_WITH_FLAGS (c);
2147 code = syntax & 0xff;
2148 if (code == Sendcomment
2149 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
2150 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2151 (nesting > 0 && --nesting == 0) : nesting < 0))
2152 /* we have encountered a comment end of the same style
2153 as the comment sequence which began this comment
2154 section */
2155 break;
2156 if (code == Scomment_fence
2157 && style == ST_COMMENT_STYLE)
2158 /* we have encountered a comment end of the same style
2159 as the comment sequence which began this comment
2160 section. */
2161 break;
2162 if (nesting > 0
2163 && code == Scomment
2164 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
2165 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style)
2166 /* we have encountered a nested comment of the same style
2167 as the comment sequence which began this comment section */
2168 nesting++;
2169 INC_BOTH (from, from_byte);
2170 UPDATE_SYNTAX_TABLE_FORWARD (from);
2172 forw_incomment:
2173 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2174 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
2175 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2176 SYNTAX_COMEND_SECOND (c1))
2177 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2178 SYNTAX_COMMENT_NESTED (c1)) ? nesting > 0 : nesting < 0))
2180 if (--nesting <= 0)
2181 /* we have encountered a comment end of the same style
2182 as the comment sequence which began this comment
2183 section */
2184 break;
2185 else
2187 INC_BOTH (from, from_byte);
2188 UPDATE_SYNTAX_TABLE_FORWARD (from);
2191 if (nesting > 0
2192 && from < stop
2193 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2194 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2195 SYNTAX_COMMENT_STYLE (c1) == style
2196 && SYNTAX_COMSTART_SECOND (c1))
2197 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2198 SYNTAX_COMMENT_NESTED (c1)))
2199 /* we have encountered a nested comment of the same style
2200 as the comment sequence which began this comment
2201 section */
2203 INC_BOTH (from, from_byte);
2204 UPDATE_SYNTAX_TABLE_FORWARD (from);
2205 nesting++;
2208 *charpos_ptr = from;
2209 *bytepos_ptr = from_byte;
2210 return 1;
2213 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2214 doc: /*
2215 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2216 Stop scanning if we find something other than a comment or whitespace.
2217 Set point to where scanning stops.
2218 If COUNT comments are found as expected, with nothing except whitespace
2219 between them, return t; otherwise return nil. */)
2220 (count)
2221 Lisp_Object count;
2223 register EMACS_INT from;
2224 EMACS_INT from_byte;
2225 register EMACS_INT stop;
2226 register int c, c1;
2227 register enum syntaxcode code;
2228 int comstyle = 0; /* style of comment encountered */
2229 int comnested = 0; /* whether the comment is nestable or not */
2230 int found;
2231 EMACS_INT count1;
2232 EMACS_INT out_charpos, out_bytepos;
2233 int dummy;
2235 CHECK_NUMBER (count);
2236 count1 = XINT (count);
2237 stop = count1 > 0 ? ZV : BEGV;
2239 immediate_quit = 1;
2240 QUIT;
2242 from = PT;
2243 from_byte = PT_BYTE;
2245 SETUP_SYNTAX_TABLE (from, count1);
2246 while (count1 > 0)
2250 int comstart_first;
2252 if (from == stop)
2254 SET_PT_BOTH (from, from_byte);
2255 immediate_quit = 0;
2256 return Qnil;
2258 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2259 code = SYNTAX (c);
2260 comstart_first = SYNTAX_COMSTART_FIRST (c);
2261 comnested = SYNTAX_COMMENT_NESTED (c);
2262 comstyle = SYNTAX_COMMENT_STYLE (c);
2263 INC_BOTH (from, from_byte);
2264 UPDATE_SYNTAX_TABLE_FORWARD (from);
2265 if (from < stop && comstart_first
2266 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2267 SYNTAX_COMSTART_SECOND (c1)))
2269 /* We have encountered a comment start sequence and we
2270 are ignoring all text inside comments. We must record
2271 the comment style this sequence begins so that later,
2272 only a comment end of the same style actually ends
2273 the comment section. */
2274 code = Scomment;
2275 comstyle = SYNTAX_COMMENT_STYLE (c1);
2276 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2277 INC_BOTH (from, from_byte);
2278 UPDATE_SYNTAX_TABLE_FORWARD (from);
2281 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2283 if (code == Scomment_fence)
2284 comstyle = ST_COMMENT_STYLE;
2285 else if (code != Scomment)
2287 immediate_quit = 0;
2288 DEC_BOTH (from, from_byte);
2289 SET_PT_BOTH (from, from_byte);
2290 return Qnil;
2292 /* We're at the start of a comment. */
2293 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2294 &out_charpos, &out_bytepos, &dummy);
2295 from = out_charpos; from_byte = out_bytepos;
2296 if (!found)
2298 immediate_quit = 0;
2299 SET_PT_BOTH (from, from_byte);
2300 return Qnil;
2302 INC_BOTH (from, from_byte);
2303 UPDATE_SYNTAX_TABLE_FORWARD (from);
2304 /* We have skipped one comment. */
2305 count1--;
2308 while (count1 < 0)
2310 while (1)
2312 int quoted;
2314 if (from <= stop)
2316 SET_PT_BOTH (BEGV, BEGV_BYTE);
2317 immediate_quit = 0;
2318 return Qnil;
2321 DEC_BOTH (from, from_byte);
2322 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2323 quoted = char_quoted (from, from_byte);
2324 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2325 code = SYNTAX (c);
2326 comstyle = 0;
2327 comnested = SYNTAX_COMMENT_NESTED (c);
2328 if (code == Sendcomment)
2329 comstyle = SYNTAX_COMMENT_STYLE (c);
2330 if (from > stop && SYNTAX_COMEND_SECOND (c)
2331 && prev_char_comend_first (from, from_byte)
2332 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2334 /* We must record the comment style encountered so that
2335 later, we can match only the proper comment begin
2336 sequence of the same style. */
2337 DEC_BOTH (from, from_byte);
2338 code = Sendcomment;
2339 /* Calling char_quoted, above, set up global syntax position
2340 at the new value of FROM. */
2341 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2342 comstyle = SYNTAX_COMMENT_STYLE (c1);
2343 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2346 if (code == Scomment_fence)
2348 /* Skip until first preceding unquoted comment_fence. */
2349 int found = 0, ini = from, ini_byte = from_byte;
2351 while (1)
2353 DEC_BOTH (from, from_byte);
2354 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2355 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2356 if (SYNTAX (c) == Scomment_fence
2357 && !char_quoted (from, from_byte))
2359 found = 1;
2360 break;
2362 else if (from == stop)
2363 break;
2365 if (found == 0)
2367 from = ini; /* Set point to ini + 1. */
2368 from_byte = ini_byte;
2369 goto leave;
2371 else
2372 /* We have skipped one comment. */
2373 break;
2375 else if (code == Sendcomment)
2377 found = back_comment (from, from_byte, stop, comnested, comstyle,
2378 &out_charpos, &out_bytepos);
2379 if (found == -1)
2381 if (c == '\n')
2382 /* This end-of-line is not an end-of-comment.
2383 Treat it like a whitespace.
2384 CC-mode (and maybe others) relies on this behavior. */
2386 else
2388 /* Failure: we should go back to the end of this
2389 not-quite-endcomment. */
2390 if (SYNTAX(c) != code)
2391 /* It was a two-char Sendcomment. */
2392 INC_BOTH (from, from_byte);
2393 goto leave;
2396 else
2398 /* We have skipped one comment. */
2399 from = out_charpos, from_byte = out_bytepos;
2400 break;
2403 else if (code != Swhitespace || quoted)
2405 leave:
2406 immediate_quit = 0;
2407 INC_BOTH (from, from_byte);
2408 SET_PT_BOTH (from, from_byte);
2409 return Qnil;
2413 count1++;
2416 SET_PT_BOTH (from, from_byte);
2417 immediate_quit = 0;
2418 return Qt;
2421 /* Return syntax code of character C if C is an ASCII character
2422 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2424 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2425 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2426 ? SYNTAX (c) : Ssymbol)
2428 static Lisp_Object
2429 scan_lists (from, count, depth, sexpflag)
2430 register EMACS_INT from;
2431 EMACS_INT count, depth;
2432 int sexpflag;
2434 Lisp_Object val;
2435 register EMACS_INT stop = count > 0 ? ZV : BEGV;
2436 register int c, c1;
2437 int stringterm;
2438 int quoted;
2439 int mathexit = 0;
2440 register enum syntaxcode code, temp_code;
2441 int min_depth = depth; /* Err out if depth gets less than this. */
2442 int comstyle = 0; /* style of comment encountered */
2443 int comnested = 0; /* whether the comment is nestable or not */
2444 EMACS_INT temp_pos;
2445 EMACS_INT last_good = from;
2446 int found;
2447 EMACS_INT from_byte;
2448 EMACS_INT out_bytepos, out_charpos;
2449 int temp, dummy;
2450 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2452 if (depth > 0) min_depth = 0;
2454 if (from > ZV) from = ZV;
2455 if (from < BEGV) from = BEGV;
2457 from_byte = CHAR_TO_BYTE (from);
2459 immediate_quit = 1;
2460 QUIT;
2462 SETUP_SYNTAX_TABLE (from, count);
2463 while (count > 0)
2465 while (from < stop)
2467 int comstart_first, prefix;
2468 UPDATE_SYNTAX_TABLE_FORWARD (from);
2469 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2470 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2471 comstart_first = SYNTAX_COMSTART_FIRST (c);
2472 comnested = SYNTAX_COMMENT_NESTED (c);
2473 comstyle = SYNTAX_COMMENT_STYLE (c);
2474 prefix = SYNTAX_PREFIX (c);
2475 if (depth == min_depth)
2476 last_good = from;
2477 INC_BOTH (from, from_byte);
2478 UPDATE_SYNTAX_TABLE_FORWARD (from);
2479 if (from < stop && comstart_first
2480 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2481 SYNTAX_COMSTART_SECOND (c))
2482 && parse_sexp_ignore_comments)
2484 /* we have encountered a comment start sequence and we
2485 are ignoring all text inside comments. We must record
2486 the comment style this sequence begins so that later,
2487 only a comment end of the same style actually ends
2488 the comment section */
2489 code = Scomment;
2490 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2491 comstyle = SYNTAX_COMMENT_STYLE (c1);
2492 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2493 INC_BOTH (from, from_byte);
2494 UPDATE_SYNTAX_TABLE_FORWARD (from);
2497 if (prefix)
2498 continue;
2500 switch (SWITCH_ENUM_CAST (code))
2502 case Sescape:
2503 case Scharquote:
2504 if (from == stop)
2505 goto lose;
2506 INC_BOTH (from, from_byte);
2507 /* treat following character as a word constituent */
2508 case Sword:
2509 case Ssymbol:
2510 if (depth || !sexpflag) break;
2511 /* This word counts as a sexp; return at end of it. */
2512 while (from < stop)
2514 UPDATE_SYNTAX_TABLE_FORWARD (from);
2516 /* Some compilers can't handle this inside the switch. */
2517 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2518 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2519 switch (temp)
2521 case Scharquote:
2522 case Sescape:
2523 INC_BOTH (from, from_byte);
2524 if (from == stop)
2525 goto lose;
2526 break;
2527 case Sword:
2528 case Ssymbol:
2529 case Squote:
2530 break;
2531 default:
2532 goto done;
2534 INC_BOTH (from, from_byte);
2536 goto done;
2538 case Scomment_fence:
2539 comstyle = ST_COMMENT_STYLE;
2540 /* FALLTHROUGH */
2541 case Scomment:
2542 if (!parse_sexp_ignore_comments) break;
2543 UPDATE_SYNTAX_TABLE_FORWARD (from);
2544 found = forw_comment (from, from_byte, stop,
2545 comnested, comstyle, 0,
2546 &out_charpos, &out_bytepos, &dummy);
2547 from = out_charpos, from_byte = out_bytepos;
2548 if (!found)
2550 if (depth == 0)
2551 goto done;
2552 goto lose;
2554 INC_BOTH (from, from_byte);
2555 UPDATE_SYNTAX_TABLE_FORWARD (from);
2556 break;
2558 case Smath:
2559 if (!sexpflag)
2560 break;
2561 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2563 INC_BOTH (from, from_byte);
2565 if (mathexit)
2567 mathexit = 0;
2568 goto close1;
2570 mathexit = 1;
2572 case Sopen:
2573 if (!++depth) goto done;
2574 break;
2576 case Sclose:
2577 close1:
2578 if (!--depth) goto done;
2579 if (depth < min_depth)
2580 xsignal3 (Qscan_error,
2581 build_string ("Containing expression ends prematurely"),
2582 make_number (last_good), make_number (from));
2583 break;
2585 case Sstring:
2586 case Sstring_fence:
2587 temp_pos = dec_bytepos (from_byte);
2588 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2589 while (1)
2591 if (from >= stop)
2592 goto lose;
2593 UPDATE_SYNTAX_TABLE_FORWARD (from);
2594 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2595 if (code == Sstring
2596 ? (c == stringterm
2597 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2598 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
2599 break;
2601 /* Some compilers can't handle this inside the switch. */
2602 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2603 switch (temp)
2605 case Scharquote:
2606 case Sescape:
2607 INC_BOTH (from, from_byte);
2609 INC_BOTH (from, from_byte);
2611 INC_BOTH (from, from_byte);
2612 if (!depth && sexpflag) goto done;
2613 break;
2614 default:
2615 /* Ignore whitespace, punctuation, quote, endcomment. */
2616 break;
2620 /* Reached end of buffer. Error if within object, return nil if between */
2621 if (depth)
2622 goto lose;
2624 immediate_quit = 0;
2625 return Qnil;
2627 /* End of object reached */
2628 done:
2629 count--;
2632 while (count < 0)
2634 while (from > stop)
2636 DEC_BOTH (from, from_byte);
2637 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2638 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2639 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2640 if (depth == min_depth)
2641 last_good = from;
2642 comstyle = 0;
2643 comnested = SYNTAX_COMMENT_NESTED (c);
2644 if (code == Sendcomment)
2645 comstyle = SYNTAX_COMMENT_STYLE (c);
2646 if (from > stop && SYNTAX_COMEND_SECOND (c)
2647 && prev_char_comend_first (from, from_byte)
2648 && parse_sexp_ignore_comments)
2650 /* We must record the comment style encountered so that
2651 later, we can match only the proper comment begin
2652 sequence of the same style. */
2653 DEC_BOTH (from, from_byte);
2654 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2655 code = Sendcomment;
2656 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2657 comstyle = SYNTAX_COMMENT_STYLE (c1);
2658 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2661 /* Quoting turns anything except a comment-ender
2662 into a word character. Note that this cannot be true
2663 if we decremented FROM in the if-statement above. */
2664 if (code != Sendcomment && char_quoted (from, from_byte))
2666 DEC_BOTH (from, from_byte);
2667 code = Sword;
2669 else if (SYNTAX_PREFIX (c))
2670 continue;
2672 switch (SWITCH_ENUM_CAST (code))
2674 case Sword:
2675 case Ssymbol:
2676 case Sescape:
2677 case Scharquote:
2678 if (depth || !sexpflag) break;
2679 /* This word counts as a sexp; count object finished
2680 after passing it. */
2681 while (from > stop)
2683 temp_pos = from_byte;
2684 if (! NILP (current_buffer->enable_multibyte_characters))
2685 DEC_POS (temp_pos);
2686 else
2687 temp_pos--;
2688 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2689 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2690 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2691 /* Don't allow comment-end to be quoted. */
2692 if (temp_code == Sendcomment)
2693 goto done2;
2694 quoted = char_quoted (from - 1, temp_pos);
2695 if (quoted)
2697 DEC_BOTH (from, from_byte);
2698 temp_pos = dec_bytepos (temp_pos);
2699 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2701 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2702 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2703 if (! (quoted || temp_code == Sword
2704 || temp_code == Ssymbol
2705 || temp_code == Squote))
2706 goto done2;
2707 DEC_BOTH (from, from_byte);
2709 goto done2;
2711 case Smath:
2712 if (!sexpflag)
2713 break;
2714 temp_pos = dec_bytepos (from_byte);
2715 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2716 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2717 DEC_BOTH (from, from_byte);
2718 if (mathexit)
2720 mathexit = 0;
2721 goto open2;
2723 mathexit = 1;
2725 case Sclose:
2726 if (!++depth) goto done2;
2727 break;
2729 case Sopen:
2730 open2:
2731 if (!--depth) goto done2;
2732 if (depth < min_depth)
2733 xsignal3 (Qscan_error,
2734 build_string ("Containing expression ends prematurely"),
2735 make_number (last_good), make_number (from));
2736 break;
2738 case Sendcomment:
2739 if (!parse_sexp_ignore_comments)
2740 break;
2741 found = back_comment (from, from_byte, stop, comnested, comstyle,
2742 &out_charpos, &out_bytepos);
2743 /* FIXME: if found == -1, then it really wasn't a comment-end.
2744 For single-char Sendcomment, we can't do much about it apart
2745 from skipping the char.
2746 For 2-char endcomments, we could try again, taking both
2747 chars as separate entities, but it's a lot of trouble
2748 for very little gain, so we don't bother either. -sm */
2749 if (found != -1)
2750 from = out_charpos, from_byte = out_bytepos;
2751 break;
2753 case Scomment_fence:
2754 case Sstring_fence:
2755 while (1)
2757 if (from == stop)
2758 goto lose;
2759 DEC_BOTH (from, from_byte);
2760 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2761 if (!char_quoted (from, from_byte)
2762 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2763 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
2764 break;
2766 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2767 break;
2769 case Sstring:
2770 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2771 while (1)
2773 if (from == stop)
2774 goto lose;
2775 DEC_BOTH (from, from_byte);
2776 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2777 if (!char_quoted (from, from_byte)
2778 && (stringterm
2779 == (c = FETCH_CHAR_AS_MULTIBYTE (from_byte)))
2780 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2781 break;
2783 if (!depth && sexpflag) goto done2;
2784 break;
2785 default:
2786 /* Ignore whitespace, punctuation, quote, endcomment. */
2787 break;
2791 /* Reached start of buffer. Error if within object, return nil if between */
2792 if (depth)
2793 goto lose;
2795 immediate_quit = 0;
2796 return Qnil;
2798 done2:
2799 count++;
2803 immediate_quit = 0;
2804 XSETFASTINT (val, from);
2805 return val;
2807 lose:
2808 xsignal3 (Qscan_error,
2809 build_string ("Unbalanced parentheses"),
2810 make_number (last_good), make_number (from));
2813 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2814 doc: /* Scan from character number FROM by COUNT lists.
2815 Returns the character number of the position thus found.
2817 If DEPTH is nonzero, paren depth begins counting from that value,
2818 only places where the depth in parentheses becomes zero
2819 are candidates for stopping; COUNT such places are counted.
2820 Thus, a positive value for DEPTH means go out levels.
2822 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2824 If the beginning or end of (the accessible part of) the buffer is reached
2825 and the depth is wrong, an error is signaled.
2826 If the depth is right but the count is not used up, nil is returned. */)
2827 (from, count, depth)
2828 Lisp_Object from, count, depth;
2830 CHECK_NUMBER (from);
2831 CHECK_NUMBER (count);
2832 CHECK_NUMBER (depth);
2834 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2837 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2838 doc: /* Scan from character number FROM by COUNT balanced expressions.
2839 If COUNT is negative, scan backwards.
2840 Returns the character number of the position thus found.
2842 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2844 If the beginning or end of (the accessible part of) the buffer is reached
2845 in the middle of a parenthetical grouping, an error is signaled.
2846 If the beginning or end is reached between groupings
2847 but before count is used up, nil is returned. */)
2848 (from, count)
2849 Lisp_Object from, count;
2851 CHECK_NUMBER (from);
2852 CHECK_NUMBER (count);
2854 return scan_lists (XINT (from), XINT (count), 0, 1);
2857 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2858 0, 0, 0,
2859 doc: /* Move point backward over any number of chars with prefix syntax.
2860 This includes chars with "quote" or "prefix" syntax (' or p). */)
2863 int beg = BEGV;
2864 int opoint = PT;
2865 int opoint_byte = PT_BYTE;
2866 int pos = PT;
2867 int pos_byte = PT_BYTE;
2868 int c;
2870 if (pos <= beg)
2872 SET_PT_BOTH (opoint, opoint_byte);
2874 return Qnil;
2877 SETUP_SYNTAX_TABLE (pos, -1);
2879 DEC_BOTH (pos, pos_byte);
2881 while (!char_quoted (pos, pos_byte)
2882 /* Previous statement updates syntax table. */
2883 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
2884 || SYNTAX_PREFIX (c)))
2886 opoint = pos;
2887 opoint_byte = pos_byte;
2889 if (pos + 1 > beg)
2890 DEC_BOTH (pos, pos_byte);
2893 SET_PT_BOTH (opoint, opoint_byte);
2895 return Qnil;
2898 /* Parse forward from FROM / FROM_BYTE to END,
2899 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2900 and return a description of the state of the parse at END.
2901 If STOPBEFORE is nonzero, stop at the start of an atom.
2902 If COMMENTSTOP is 1, stop at the start of a comment.
2903 If COMMENTSTOP is -1, stop at the start or end of a comment,
2904 after the beginning of a string, or after the end of a string. */
2906 static void
2907 scan_sexps_forward (stateptr, from, from_byte, end, targetdepth,
2908 stopbefore, oldstate, commentstop)
2909 struct lisp_parse_state *stateptr;
2910 register EMACS_INT from;
2911 EMACS_INT from_byte, end;
2912 int targetdepth, stopbefore;
2913 Lisp_Object oldstate;
2914 int commentstop;
2916 struct lisp_parse_state state;
2918 register enum syntaxcode code;
2919 int c1;
2920 int comnested;
2921 struct level { int last, prev; };
2922 struct level levelstart[100];
2923 register struct level *curlevel = levelstart;
2924 struct level *endlevel = levelstart + 100;
2925 register int depth; /* Paren depth of current scanning location.
2926 level - levelstart equals this except
2927 when the depth becomes negative. */
2928 int mindepth; /* Lowest DEPTH value seen. */
2929 int start_quoted = 0; /* Nonzero means starting after a char quote */
2930 Lisp_Object tem;
2931 EMACS_INT prev_from; /* Keep one character before FROM. */
2932 EMACS_INT prev_from_byte;
2933 int prev_from_syntax;
2934 int boundary_stop = commentstop == -1;
2935 int nofence;
2936 int found;
2937 EMACS_INT out_bytepos, out_charpos;
2938 int temp;
2940 prev_from = from;
2941 prev_from_byte = from_byte;
2942 if (from != BEGV)
2943 DEC_BOTH (prev_from, prev_from_byte);
2945 /* Use this macro instead of `from++'. */
2946 #define INC_FROM \
2947 do { prev_from = from; \
2948 prev_from_byte = from_byte; \
2949 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
2950 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
2951 INC_BOTH (from, from_byte); \
2952 if (from < end) \
2953 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2954 } while (0)
2956 immediate_quit = 1;
2957 QUIT;
2959 if (NILP (oldstate))
2961 depth = 0;
2962 state.instring = -1;
2963 state.incomment = 0;
2964 state.comstyle = 0; /* comment style a by default. */
2965 state.comstr_start = -1; /* no comment/string seen. */
2967 else
2969 tem = Fcar (oldstate);
2970 if (!NILP (tem))
2971 depth = XINT (tem);
2972 else
2973 depth = 0;
2975 oldstate = Fcdr (oldstate);
2976 oldstate = Fcdr (oldstate);
2977 oldstate = Fcdr (oldstate);
2978 tem = Fcar (oldstate);
2979 /* Check whether we are inside string_fence-style string: */
2980 state.instring = (!NILP (tem)
2981 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
2982 : -1);
2984 oldstate = Fcdr (oldstate);
2985 tem = Fcar (oldstate);
2986 state.incomment = (!NILP (tem)
2987 ? (INTEGERP (tem) ? XINT (tem) : -1)
2988 : 0);
2990 oldstate = Fcdr (oldstate);
2991 tem = Fcar (oldstate);
2992 start_quoted = !NILP (tem);
2994 /* if the eighth element of the list is nil, we are in comment
2995 style a. If it is non-nil, we are in comment style b */
2996 oldstate = Fcdr (oldstate);
2997 oldstate = Fcdr (oldstate);
2998 tem = Fcar (oldstate);
2999 state.comstyle = NILP (tem) ? 0 : (EQ (tem, Qsyntax_table)
3000 ? ST_COMMENT_STYLE : 1);
3002 oldstate = Fcdr (oldstate);
3003 tem = Fcar (oldstate);
3004 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
3005 oldstate = Fcdr (oldstate);
3006 tem = Fcar (oldstate);
3007 while (!NILP (tem)) /* >= second enclosing sexps. */
3009 /* curlevel++->last ran into compiler bug on Apollo */
3010 curlevel->last = XINT (Fcar (tem));
3011 if (++curlevel == endlevel)
3012 curlevel--; /* error ("Nesting too deep for parser"); */
3013 curlevel->prev = -1;
3014 curlevel->last = -1;
3015 tem = Fcdr (tem);
3018 state.quoted = 0;
3019 mindepth = depth;
3021 curlevel->prev = -1;
3022 curlevel->last = -1;
3024 SETUP_SYNTAX_TABLE (prev_from, 1);
3025 temp = FETCH_CHAR (prev_from_byte);
3026 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
3027 UPDATE_SYNTAX_TABLE_FORWARD (from);
3029 /* Enter the loop at a place appropriate for initial state. */
3031 if (state.incomment)
3032 goto startincomment;
3033 if (state.instring >= 0)
3035 nofence = state.instring != ST_STRING_STYLE;
3036 if (start_quoted)
3037 goto startquotedinstring;
3038 goto startinstring;
3040 else if (start_quoted)
3041 goto startquoted;
3043 while (from < end)
3045 INC_FROM;
3046 code = prev_from_syntax & 0xff;
3048 if (from < end
3049 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3050 && (c1 = FETCH_CHAR (from_byte),
3051 SYNTAX_COMSTART_SECOND (c1)))
3052 /* Duplicate code to avoid a complex if-expression
3053 which causes trouble for the SGI compiler. */
3055 /* Record the comment style we have entered so that only
3056 the comment-end sequence of the same style actually
3057 terminates the comment section. */
3058 state.comstyle = SYNTAX_COMMENT_STYLE (c1);
3059 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
3060 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
3061 state.incomment = comnested ? 1 : -1;
3062 state.comstr_start = prev_from;
3063 INC_FROM;
3064 code = Scomment;
3066 else if (code == Scomment_fence)
3068 /* Record the comment style we have entered so that only
3069 the comment-end sequence of the same style actually
3070 terminates the comment section. */
3071 state.comstyle = ST_COMMENT_STYLE;
3072 state.incomment = -1;
3073 state.comstr_start = prev_from;
3074 code = Scomment;
3076 else if (code == Scomment)
3078 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax);
3079 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3080 1 : -1);
3081 state.comstr_start = prev_from;
3084 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
3085 continue;
3086 switch (SWITCH_ENUM_CAST (code))
3088 case Sescape:
3089 case Scharquote:
3090 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3091 curlevel->last = prev_from;
3092 startquoted:
3093 if (from == end) goto endquoted;
3094 INC_FROM;
3095 goto symstarted;
3096 /* treat following character as a word constituent */
3097 case Sword:
3098 case Ssymbol:
3099 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3100 curlevel->last = prev_from;
3101 symstarted:
3102 while (from < end)
3104 /* Some compilers can't handle this inside the switch. */
3105 temp = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3106 temp = SYNTAX (temp);
3107 switch (temp)
3109 case Scharquote:
3110 case Sescape:
3111 INC_FROM;
3112 if (from == end) goto endquoted;
3113 break;
3114 case Sword:
3115 case Ssymbol:
3116 case Squote:
3117 break;
3118 default:
3119 goto symdone;
3121 INC_FROM;
3123 symdone:
3124 curlevel->prev = curlevel->last;
3125 break;
3127 case Scomment_fence: /* Can't happen because it's handled above. */
3128 case Scomment:
3129 if (commentstop || boundary_stop) goto done;
3130 startincomment:
3131 /* The (from == BEGV) test was to enter the loop in the middle so
3132 that we find a 2-char comment ender even if we start in the
3133 middle of it. We don't want to do that if we're just at the
3134 beginning of the comment (think of (*) ... (*)). */
3135 found = forw_comment (from, from_byte, end,
3136 state.incomment, state.comstyle,
3137 (from == BEGV || from < state.comstr_start + 3)
3138 ? 0 : prev_from_syntax,
3139 &out_charpos, &out_bytepos, &state.incomment);
3140 from = out_charpos; from_byte = out_bytepos;
3141 /* Beware! prev_from and friends are invalid now.
3142 Luckily, the `done' doesn't use them and the INC_FROM
3143 sets them to a sane value without looking at them. */
3144 if (!found) goto done;
3145 INC_FROM;
3146 state.incomment = 0;
3147 state.comstyle = 0; /* reset the comment style */
3148 if (boundary_stop) goto done;
3149 break;
3151 case Sopen:
3152 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3153 depth++;
3154 /* curlevel++->last ran into compiler bug on Apollo */
3155 curlevel->last = prev_from;
3156 if (++curlevel == endlevel)
3157 curlevel--; /* error ("Nesting too deep for parser"); */
3158 curlevel->prev = -1;
3159 curlevel->last = -1;
3160 if (targetdepth == depth) goto done;
3161 break;
3163 case Sclose:
3164 depth--;
3165 if (depth < mindepth)
3166 mindepth = depth;
3167 if (curlevel != levelstart)
3168 curlevel--;
3169 curlevel->prev = curlevel->last;
3170 if (targetdepth == depth) goto done;
3171 break;
3173 case Sstring:
3174 case Sstring_fence:
3175 state.comstr_start = from - 1;
3176 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3177 curlevel->last = prev_from;
3178 state.instring = (code == Sstring
3179 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3180 : ST_STRING_STYLE);
3181 if (boundary_stop) goto done;
3182 startinstring:
3184 nofence = state.instring != ST_STRING_STYLE;
3186 while (1)
3188 int c;
3190 if (from >= end) goto done;
3191 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3192 /* Some compilers can't handle this inside the switch. */
3193 temp = SYNTAX (c);
3195 /* Check TEMP here so that if the char has
3196 a syntax-table property which says it is NOT
3197 a string character, it does not end the string. */
3198 if (nofence && c == state.instring && temp == Sstring)
3199 break;
3201 switch (temp)
3203 case Sstring_fence:
3204 if (!nofence) goto string_end;
3205 break;
3206 case Scharquote:
3207 case Sescape:
3208 INC_FROM;
3209 startquotedinstring:
3210 if (from >= end) goto endquoted;
3212 INC_FROM;
3215 string_end:
3216 state.instring = -1;
3217 curlevel->prev = curlevel->last;
3218 INC_FROM;
3219 if (boundary_stop) goto done;
3220 break;
3222 case Smath:
3223 /* FIXME: We should do something with it. */
3224 break;
3225 default:
3226 /* Ignore whitespace, punctuation, quote, endcomment. */
3227 break;
3230 goto done;
3232 stop: /* Here if stopping before start of sexp. */
3233 from = prev_from; /* We have just fetched the char that starts it; */
3234 goto done; /* but return the position before it. */
3236 endquoted:
3237 state.quoted = 1;
3238 done:
3239 state.depth = depth;
3240 state.mindepth = mindepth;
3241 state.thislevelstart = curlevel->prev;
3242 state.prevlevelstart
3243 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3244 state.location = from;
3245 state.levelstarts = Qnil;
3246 while (--curlevel >= levelstart)
3247 state.levelstarts = Fcons (make_number (curlevel->last),
3248 state.levelstarts);
3249 immediate_quit = 0;
3251 *stateptr = state;
3254 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3255 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3256 Parsing stops at TO or when certain criteria are met;
3257 point is set to where parsing stops.
3258 If fifth arg OLDSTATE is omitted or nil,
3259 parsing assumes that FROM is the beginning of a function.
3260 Value is a list of elements describing final state of parsing:
3261 0. depth in parens.
3262 1. character address of start of innermost containing list; nil if none.
3263 2. character address of start of last complete sexp terminated.
3264 3. non-nil if inside a string.
3265 (it is the character that will terminate the string,
3266 or t if the string should be terminated by a generic string delimiter.)
3267 4. nil if outside a comment, t if inside a non-nestable comment,
3268 else an integer (the current comment nesting).
3269 5. t if following a quote character.
3270 6. the minimum paren-depth encountered during this scan.
3271 7. t if in a comment of style b; symbol `syntax-table' if the comment
3272 should be terminated by a generic comment delimiter.
3273 8. character address of start of comment or string; nil if not in one.
3274 9. Intermediate data for continuation of parsing (subject to change).
3275 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3276 in parentheses becomes equal to TARGETDEPTH.
3277 Fourth arg STOPBEFORE non-nil means stop when come to
3278 any character that starts a sexp.
3279 Fifth arg OLDSTATE is a list like what this function returns.
3280 It is used to initialize the state of the parse. Elements number 1, 2, 6
3281 and 8 are ignored.
3282 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3283 If it is symbol `syntax-table', stop after the start of a comment or a
3284 string, or after end of a comment or a string. */)
3285 (from, to, targetdepth, stopbefore, oldstate, commentstop)
3286 Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop;
3288 struct lisp_parse_state state;
3289 int target;
3291 if (!NILP (targetdepth))
3293 CHECK_NUMBER (targetdepth);
3294 target = XINT (targetdepth);
3296 else
3297 target = -100000; /* We won't reach this depth */
3299 validate_region (&from, &to);
3300 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3301 XINT (to),
3302 target, !NILP (stopbefore), oldstate,
3303 (NILP (commentstop)
3304 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3306 SET_PT (state.location);
3308 return Fcons (make_number (state.depth),
3309 Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart),
3310 Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart),
3311 Fcons (state.instring >= 0
3312 ? (state.instring == ST_STRING_STYLE
3313 ? Qt : make_number (state.instring)) : Qnil,
3314 Fcons (state.incomment < 0 ? Qt :
3315 (state.incomment == 0 ? Qnil :
3316 make_number (state.incomment)),
3317 Fcons (state.quoted ? Qt : Qnil,
3318 Fcons (make_number (state.mindepth),
3319 Fcons ((state.comstyle
3320 ? (state.comstyle == ST_COMMENT_STYLE
3321 ? Qsyntax_table : Qt) :
3322 Qnil),
3323 Fcons (((state.incomment
3324 || (state.instring >= 0))
3325 ? make_number (state.comstr_start)
3326 : Qnil),
3327 Fcons (state.levelstarts, Qnil))))))))));
3330 void
3331 init_syntax_once ()
3333 register int i, c;
3334 Lisp_Object temp;
3336 /* This has to be done here, before we call Fmake_char_table. */
3337 Qsyntax_table = intern ("syntax-table");
3338 staticpro (&Qsyntax_table);
3340 /* Intern this now in case it isn't already done.
3341 Setting this variable twice is harmless.
3342 But don't staticpro it here--that is done in alloc.c. */
3343 Qchar_table_extra_slots = intern ("char-table-extra-slots");
3345 /* Create objects which can be shared among syntax tables. */
3346 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
3347 for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++)
3348 XVECTOR (Vsyntax_code_object)->contents[i]
3349 = Fcons (make_number (i), Qnil);
3351 /* Now we are ready to set up this property, so we can
3352 create syntax tables. */
3353 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3355 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3357 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3359 /* Control characters should not be whitespace. */
3360 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3361 for (i = 0; i <= ' ' - 1; i++)
3362 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3363 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3365 /* Except that a few really are whitespace. */
3366 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3367 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3368 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3369 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3370 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3371 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3373 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3374 for (i = 'a'; i <= 'z'; i++)
3375 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3376 for (i = 'A'; i <= 'Z'; i++)
3377 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3378 for (i = '0'; i <= '9'; i++)
3379 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3381 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3382 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3384 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3385 Fcons (make_number (Sopen), make_number (')')));
3386 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3387 Fcons (make_number (Sclose), make_number ('(')));
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 ((int) Sstring), Qnil));
3398 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3399 Fcons (make_number ((int) Sescape), Qnil));
3401 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
3402 for (i = 0; i < 10; i++)
3404 c = "_-+*/&|<>="[i];
3405 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3408 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3409 for (i = 0; i < 12; i++)
3411 c = ".,;:?!#@~^'`"[i];
3412 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3415 /* All multibyte characters have syntax `word' by default. */
3416 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3417 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3420 void
3421 syms_of_syntax ()
3423 Qsyntax_table_p = intern ("syntax-table-p");
3424 staticpro (&Qsyntax_table_p);
3426 staticpro (&Vsyntax_code_object);
3428 staticpro (&gl_state.object);
3429 staticpro (&gl_state.global_code);
3430 staticpro (&gl_state.current_syntax_table);
3431 staticpro (&gl_state.old_prop);
3433 /* Defined in regex.c */
3434 staticpro (&re_match_object);
3436 Qscan_error = intern ("scan-error");
3437 staticpro (&Qscan_error);
3438 Fput (Qscan_error, Qerror_conditions,
3439 Fcons (Qscan_error, Fcons (Qerror, Qnil)));
3440 Fput (Qscan_error, Qerror_message,
3441 build_string ("Scan error"));
3443 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
3444 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3446 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties,
3447 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3448 Otherwise, that text property is simply ignored.
3449 See the info node `(elisp)Syntax Properties' for a description of the
3450 `syntax-table' property. */);
3452 words_include_escapes = 0;
3453 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
3454 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3456 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol,
3457 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3458 multibyte_syntax_as_symbol = 0;
3460 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3461 &open_paren_in_column_0_is_defun_start,
3462 doc: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3463 open_paren_in_column_0_is_defun_start = 1;
3466 DEFVAR_LISP ("find-word-boundary-function-table",
3467 &Vfind_word_boundary_function_table,
3468 doc: /*
3469 Char table of functions to search for the word boundary.
3470 Each function is called with two arguments; POS and LIMIT.
3471 POS and LIMIT are character positions in the current buffer.
3473 If POS is less than LIMIT, POS is at the first character of a word,
3474 and the return value of a function is a position after the last
3475 character of that word.
3477 If POS is not less than LIMIT, POS is at the last character of a word,
3478 and the return value of a function is a position at the first
3479 character of that word.
3481 In both cases, LIMIT bounds the search. */);
3482 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3484 defsubr (&Ssyntax_table_p);
3485 defsubr (&Ssyntax_table);
3486 defsubr (&Sstandard_syntax_table);
3487 defsubr (&Scopy_syntax_table);
3488 defsubr (&Sset_syntax_table);
3489 defsubr (&Schar_syntax);
3490 defsubr (&Smatching_paren);
3491 defsubr (&Sstring_to_syntax);
3492 defsubr (&Smodify_syntax_entry);
3493 defsubr (&Sinternal_describe_syntax_value);
3495 defsubr (&Sforward_word);
3497 defsubr (&Sskip_chars_forward);
3498 defsubr (&Sskip_chars_backward);
3499 defsubr (&Sskip_syntax_forward);
3500 defsubr (&Sskip_syntax_backward);
3502 defsubr (&Sforward_comment);
3503 defsubr (&Sscan_lists);
3504 defsubr (&Sscan_sexps);
3505 defsubr (&Sbackward_prefix_chars);
3506 defsubr (&Sparse_partial_sexp);
3509 /* arch-tag: 3e297b9f-088e-4b64-8f4c-fb0b3443e412
3510 (do not change this comment) */