1 /* XEmacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985-1994 Free Software Foundation, Inc.
3 Copyright (C) 1995 Sun Microsystems, Inc.
5 This file is part of XEmacs.
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Synched up with: FSF 19.28. */
24 /* This file has been Mule-ized. */
33 /* Here is a comment from Ken'ichi HANDA <handa@etl.go.jp>
34 explaining the purpose of the Sextword syntax category:
36 Japanese words are not separated by spaces, which makes finding word
37 boundaries very difficult. Theoretically it's impossible without
38 using natural language processing techniques. But, by defining
39 pseudo-words as below (much simplified for letting you understand it
40 easily) for Japanese, we can have a convenient forward-word function
43 A Japanese word is a sequence of characters that consists of
44 zero or more Kanji characters followed by zero or more
47 Then, the problem is that now we can't say that a sequence of
48 word-constituents makes up a WORD. For instance, both Hiragana "A"
49 and Kanji "KAN" are word-constituents but the sequence of these two
50 letters can't be a single word.
52 So, we introduced Sextword for Japanese letters. A character of
53 Sextword is a word-constituent but a word boundary may exist between
54 two such characters. */
56 /* Mule 2.4 doesn't seem to have Sextword - I'm removing it -- mrb */
57 /* Recovered by tomo */
59 #define ST_COMMENT_STYLE 0x101
60 #define ST_STRING_STYLE 0x102
62 Lisp_Object Qsyntax_table;
63 int lookup_syntax_properties;
65 Lisp_Object Qsyntax_table_p;
67 int words_include_escapes;
69 int parse_sexp_ignore_comments;
71 /* The following two variables are provided to tell additional information
72 to the regex routines. We do it this way rather than change the
73 arguments to re_search_2() in an attempt to maintain some call
74 compatibility with other versions of the regex code. */
76 /* Tell the regex routines not to QUIT. Normally there is a QUIT
77 each iteration in re_search_2(). */
78 int no_quit_in_re_search;
80 /* Tell the regex routines which buffer to access for SYNTAX() lookups
82 struct buffer *regex_emacs_buffer;
84 /* In Emacs, this is the string or buffer in which we
85 are matching. It is used for looking up syntax properties. */
86 Lisp_Object regex_match_object;
88 Lisp_Object Vstandard_syntax_table;
90 Lisp_Object Vsyntax_designator_chars_string;
92 /* This is the internal form of the parse state used in parse-partial-sexp. */
94 struct lisp_parse_state
96 int depth; /* Depth at end of parsing */
97 Emchar instring; /* -1 if not within string, else desired terminator */
98 int incomment; /* Nonzero if within a comment at end of parsing */
99 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE */
100 int quoted; /* Nonzero if just after an escape char at end of
102 Bufpos thislevelstart;/* Char number of most recent start-of-expression
104 Bufpos prevlevelstart;/* Char number of start of containing expression */
105 Bufpos location; /* Char number at which parsing stopped */
106 int mindepth; /* Minimum depth seen while scanning */
107 Bufpos comstr_start; /* Position just after last comment/string starter */
108 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
109 of levels (starting from outermost). */
112 /* These variables are a cache for finding the start of a defun.
113 find_start_pos is the place for which the defun start was found.
114 find_start_value is the defun start position found for it.
115 find_start_buffer is the buffer it was found in.
116 find_start_begv is the BEGV value when it was found.
117 find_start_modiff is the value of MODIFF when it was found. */
119 static Bufpos find_start_pos;
120 static Bufpos find_start_value;
121 static struct buffer *find_start_buffer;
122 static Bufpos find_start_begv;
123 static int find_start_modiff;
125 /* Find a defun-start that is the last one before POS (or nearly the last).
126 We record what we find, so that another call in the same area
127 can return the same value right away. */
130 find_defun_start (struct buffer *buf, Bufpos pos)
134 /* Use previous finding, if it's valid and applies to this inquiry. */
135 if (buf == find_start_buffer
136 /* Reuse the defun-start even if POS is a little farther on.
137 POS might be in the next defun, but that's ok.
138 Our value may not be the best possible, but will still be usable. */
139 && pos <= find_start_pos + 1000
140 && pos >= find_start_value
141 && BUF_BEGV (buf) == find_start_begv
142 && BUF_MODIFF (buf) == find_start_modiff)
143 return find_start_value;
145 /* Back up to start of line. */
146 tem = find_next_newline (buf, pos, -1);
148 SETUP_SYNTAX_CACHE (tem, 1);
149 while (tem > BUF_BEGV (buf))
151 UPDATE_SYNTAX_CACHE_BACKWARD(tem);
153 /* Open-paren at start of line means we found our defun-start. */
154 if (SYNTAX_FROM_CACHE (mirrortab, BUF_FETCH_CHAR (buf, tem)) == Sopen)
156 /* Move to beg of previous line. */
157 tem = find_next_newline (buf, tem, -2);
160 /* Record what we found, for the next try. */
161 find_start_value = tem;
162 find_start_buffer = buf;
163 find_start_modiff = BUF_MODIFF (buf);
164 find_start_begv = BUF_BEGV (buf);
165 find_start_pos = pos;
167 return find_start_value;
170 DEFUN ("syntax-table-p", Fsyntax_table_p, 1, 1, 0, /*
171 Return t if OBJECT is a syntax table.
172 Any vector of 256 elements will do.
176 return (CHAR_TABLEP (object)
177 && XCHAR_TABLE_TYPE (object) == CHAR_TABLE_TYPE_SYNTAX)
182 check_syntax_table (Lisp_Object obj, Lisp_Object default_)
186 while (NILP (Fsyntax_table_p (obj)))
187 obj = wrong_type_argument (Qsyntax_table_p, obj);
191 DEFUN ("syntax-table", Fsyntax_table, 0, 1, 0, /*
192 Return the current syntax table.
193 This is the one specified by the current buffer, or by BUFFER if it
198 return decode_buffer (buffer, 0)->syntax_table;
201 DEFUN ("standard-syntax-table", Fstandard_syntax_table, 0, 0, 0, /*
202 Return the standard syntax table.
203 This is the one used for new buffers.
207 return Vstandard_syntax_table;
210 DEFUN ("copy-syntax-table", Fcopy_syntax_table, 0, 1, 0, /*
211 Return a new syntax table which is a copy of SYNTAX-TABLE.
212 SYNTAX-TABLE defaults to the standard syntax table.
216 if (NILP (Vstandard_syntax_table))
217 return Fmake_char_table (Qsyntax);
219 syntax_table = check_syntax_table (syntax_table, Vstandard_syntax_table);
220 return Fcopy_char_table (syntax_table);
223 DEFUN ("set-syntax-table", Fset_syntax_table, 1, 2, 0, /*
224 Select SYNTAX-TABLE as the new syntax table for BUFFER.
225 BUFFER defaults to the current buffer if omitted.
227 (syntax_table, buffer))
229 struct buffer *buf = decode_buffer (buffer, 0);
230 syntax_table = check_syntax_table (syntax_table, Qnil);
231 buf->syntax_table = syntax_table;
232 buf->mirror_syntax_table = XCHAR_TABLE (syntax_table)->mirror_table;
233 /* Indicate that this buffer now has a specified syntax table. */
234 buf->local_var_flags |= XINT (buffer_local_flags.syntax_table);
238 /* The current syntax state */
239 struct syntax_cache syntax_cache;
243 Update syntax_cache to an appropriate setting for position POS
245 The sign of COUNT gives the relative position of POS wrt the
246 previously valid interval. (not currently used)
248 `syntax_cache.*_change' are the next and previous positions at
249 which syntax_code and c_s_t will need to be recalculated.
251 #### Currently this code uses 'get-char-property', which will
252 return the "last smallest" extent at a given position. In cases
253 where overlapping extents are defined, this code will simply use
254 whatever is returned by get-char-property.
256 It might be worth it at some point to merge provided syntax tables
257 outward to the current buffer. */
260 update_syntax_cache (int pos, int count, int init)
262 Lisp_Object tmp_table;
266 syntax_cache.prev_change = -1;
267 syntax_cache.next_change = -1;
270 if (pos > syntax_cache.prev_change &&
271 pos < syntax_cache.next_change)
277 if (NILP (syntax_cache.object) || EQ (syntax_cache.object, Qt))
279 int get_change_before = pos + 1;
281 tmp_table = Fget_char_property (make_int(pos), Qsyntax_table,
282 make_buffer (syntax_cache.buffer), Qnil);
283 syntax_cache.next_change =
284 XINT (Fnext_extent_change (make_int (pos > 0 ? pos : 1),
285 make_buffer (syntax_cache.buffer)));
287 if (get_change_before < 1)
288 get_change_before = 1;
289 else if (get_change_before > BUF_ZV (syntax_cache.buffer))
290 get_change_before = BUF_ZV (syntax_cache.buffer);
292 syntax_cache.prev_change =
293 XINT (Fprevious_extent_change (make_int (get_change_before),
294 make_buffer (syntax_cache.buffer)));
298 int get_change_before = pos + 1;
300 tmp_table = Fget_char_property (make_int(pos), Qsyntax_table,
301 syntax_cache.object, Qnil);
302 syntax_cache.next_change =
303 XINT (Fnext_extent_change (make_int (pos >= 0 ? pos : 0),
304 syntax_cache.object));
306 if (get_change_before < 0)
307 get_change_before = 0;
308 else if (get_change_before > XSTRING_LENGTH(syntax_cache.object))
309 get_change_before = XSTRING_LENGTH(syntax_cache.object);
311 syntax_cache.prev_change =
312 XINT (Fprevious_extent_change (make_int (pos >= 0 ? pos : 0),
313 syntax_cache.object));
316 if (EQ (Fsyntax_table_p (tmp_table), Qt))
318 syntax_cache.use_code = 0;
319 syntax_cache.current_syntax_table =
320 XCHAR_TABLE (tmp_table)->mirror_table;
322 else if (CONSP (tmp_table) && INTP (XCAR (tmp_table)))
324 syntax_cache.use_code = 1;
325 syntax_cache.syntax_code = XINT (XCAR(tmp_table));
329 syntax_cache.use_code = 0;
330 syntax_cache.current_syntax_table =
331 syntax_cache.buffer->mirror_syntax_table;
336 /* Convert a letter which signifies a syntax code
337 into the code it signifies.
338 This is used by modify-syntax-entry, and other things. */
340 const unsigned char syntax_spec_code[0400] =
341 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
342 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
343 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
344 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
345 (char) Swhitespace, 0377, (char) Sstring, 0377,
346 (char) Smath, 0377, 0377, (char) Squote,
347 (char) Sopen, (char) Sclose, 0377, 0377,
348 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
349 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
350 0377, 0377, 0377, 0377,
351 (char) Scomment, 0377, (char) Sendcomment, 0377,
352 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
353 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
354 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
355 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
356 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
357 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
358 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
359 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377
362 const unsigned char syntax_code_spec[] = " .w_()'\"$\\/<>@!|";
364 DEFUN ("syntax-designator-chars", Fsyntax_designator_chars, 0, 0, 0, /*
365 Return a string of the recognized syntax designator chars.
366 The chars are ordered by their internal syntax codes, which are
367 numbered starting at 0.
371 return Vsyntax_designator_chars_string;
374 DEFUN ("char-syntax", Fchar_syntax, 1, 2, 0, /*
375 Return the syntax code of CHARACTER, described by a character.
376 For example, if CHARACTER is a word constituent,
377 the character `?w' is returned.
378 The characters that correspond to various syntax codes
379 are listed in the documentation of `modify-syntax-entry'.
380 Optional second argument SYNTAX-TABLE defaults to the current buffer's
383 (character, syntax_table))
385 Lisp_Char_Table *mirrortab;
387 if (NILP (character))
389 character = make_char ('\000');
391 CHECK_CHAR_COERCE_INT (character);
392 syntax_table = check_syntax_table (syntax_table, current_buffer->syntax_table);
393 mirrortab = XCHAR_TABLE (XCHAR_TABLE (syntax_table)->mirror_table);
394 return make_char (syntax_code_spec[(int) SYNTAX (mirrortab, XCHAR (character))]);
400 charset_syntax (struct buffer *buf, Lisp_Object charset, int *multi_p_out)
403 /* #### get this right */
410 syntax_match (Lisp_Object syntax_table, Emchar ch)
412 Lisp_Object code = XCHAR_TABLE_VALUE_UNSAFE (syntax_table, ch);
413 Lisp_Object code2 = code;
417 if (SYNTAX_FROM_CODE (XINT (code2)) == Sinherit)
418 code = XCHAR_TABLE_VALUE_UNSAFE (Vstandard_syntax_table, ch);
420 return CONSP (code) ? XCDR (code) : Qnil;
423 DEFUN ("matching-paren", Fmatching_paren, 1, 2, 0, /*
424 Return the matching parenthesis of CHARACTER, or nil if none.
425 Optional second argument SYNTAX-TABLE defaults to the current buffer's
428 (character, syntax_table))
430 Lisp_Char_Table *mirrortab;
433 CHECK_CHAR_COERCE_INT (character);
434 syntax_table = check_syntax_table (syntax_table, current_buffer->syntax_table);
435 mirrortab = XCHAR_TABLE (XCHAR_TABLE (syntax_table)->mirror_table);
436 code = SYNTAX (mirrortab, XCHAR (character));
437 if (code == Sopen || code == Sclose || code == Sstring)
438 return syntax_match (syntax_table, XCHAR (character));
445 /* Return 1 if there is a word boundary between two word-constituent
446 characters C1 and C2 if they appear in this order, else return 0.
447 There is no word boundary between two word-constituent ASCII
449 #define WORD_BOUNDARY_P(c1, c2) \
450 (!(CHAR_ASCII_P (c1) && CHAR_ASCII_P (c2)) \
451 && word_boundary_p (c1, c2))
453 extern int word_boundary_p (Emchar c1, Emchar c2);
456 /* Return the position across COUNT words from FROM.
457 If that many words cannot be found before the end of the buffer, return 0.
458 COUNT negative means scan backward and stop at word beginning. */
461 scan_words (struct buffer *buf, Bufpos from, int count)
463 Bufpos limit = count > 0 ? BUF_ZV (buf) : BUF_BEGV (buf);
465 enum syntaxcode code;
467 SETUP_SYNTAX_CACHE_FOR_BUFFER (buf, from, count);
469 /* #### is it really worth it to hand expand both cases? JV */
479 UPDATE_SYNTAX_CACHE_FORWARD (from);
480 ch0 = BUF_FETCH_CHAR (buf, from);
481 code = SYNTAX_FROM_CACHE (mirrortab, ch0);
484 if (words_include_escapes
485 && (code == Sescape || code == Scharquote))
493 while (from != limit)
495 UPDATE_SYNTAX_CACHE_FORWARD (from);
496 ch1 = BUF_FETCH_CHAR (buf, from);
497 code = SYNTAX_FROM_CACHE (mirrortab, ch1);
498 if (!(words_include_escapes
499 && (code == Sescape || code == Scharquote)))
502 || WORD_BOUNDARY_P (ch0, ch1)
523 UPDATE_SYNTAX_CACHE_BACKWARD (from - 1);
524 ch1 = BUF_FETCH_CHAR (buf, from - 1);
525 code = SYNTAX_FROM_CACHE (mirrortab, ch1);
528 if (words_include_escapes
529 && (code == Sescape || code == Scharquote))
537 while (from != limit)
539 UPDATE_SYNTAX_CACHE_BACKWARD (from - 1);
540 ch0 = BUF_FETCH_CHAR (buf, from - 1);
541 code = SYNTAX_FROM_CACHE (mirrortab, ch0);
543 if (!(words_include_escapes
544 && (code == Sescape || code == Scharquote)))
547 || WORD_BOUNDARY_P (ch0, ch1)
562 DEFUN ("forward-word", Fforward_word, 0, 2, "_p", /*
563 Move point forward COUNT words (backward if COUNT is negative).
564 Normally t is returned, but if an edge of the buffer is reached,
565 point is left there and nil is returned.
567 The characters that are moved over may be added to the current selection
568 \(i.e. active region) if the Shift key is held down, a motion key is used
569 to invoke this command, and `shifted-motion-keys-select-region' is t; see
570 the documentation for this variable for more details.
572 COUNT defaults to 1, and BUFFER defaults to the current buffer.
577 struct buffer *buf = decode_buffer (buffer, 0);
588 val = scan_words (buf, BUF_PT (buf), n);
591 BUF_SET_PT (buf, val);
596 BUF_SET_PT (buf, n > 0 ? BUF_ZV (buf) : BUF_BEGV (buf));
601 static void scan_sexps_forward (struct buffer *buf,
602 struct lisp_parse_state *,
603 Bufpos from, Bufpos end,
604 int targetdepth, int stopbefore,
605 Lisp_Object oldstate,
609 find_start_of_comment (struct buffer *buf, Bufpos from, Bufpos stop,
613 enum syntaxcode code;
615 /* Look back, counting the parity of string-quotes,
616 and recording the comment-starters seen.
617 When we reach a safe place, assume that's not in a string;
618 then step the main scan to the earliest comment-starter seen
619 an even number of string quotes away from the safe place.
621 OFROM[I] is position of the earliest comment-starter seen
622 which is I+2X quotes from the comment-end.
623 PARITY is current parity of quotes from the comment end. */
625 Emchar my_stringend = 0;
626 int string_lossage = 0;
627 Bufpos comment_end = from;
628 Bufpos comstart_pos = 0;
629 int comstart_parity = 0;
630 int styles_match_p = 0;
631 /* mask to match comment styles against; for ST_COMMENT_STYLE, this
632 will get set to SYNTAX_COMMENT_STYLE_B, but never get checked */
633 int mask = comstyle ? SYNTAX_COMMENT_STYLE_B : SYNTAX_COMMENT_STYLE_A;
635 /* At beginning of range to scan, we're outside of strings;
636 that determines quote parity to the comment-end. */
641 /* Move back and examine a character. */
643 UPDATE_SYNTAX_CACHE_BACKWARD (from);
645 c = BUF_FETCH_CHAR (buf, from);
646 code = SYNTAX_FROM_CACHE (mirrortab, c);
647 syncode = SYNTAX_CODE_FROM_CACHE (mirrortab, c);
649 /* is this a 1-char comment end sequence? if so, try
650 to see if style matches previously extracted mask */
651 if (code == Sendcomment)
654 SYNTAX_CODE_COMMENT_1CHAR_MASK (syncode) & mask;
657 /* or are we looking at a 1-char comment start sequence
658 of the style matching mask? */
659 else if (code == Scomment)
662 SYNTAX_CODE_COMMENT_1CHAR_MASK (syncode) & mask;
665 /* otherwise, is this a 2-char comment end or start sequence? */
666 else if (from > stop)
669 /* 2-char comment end sequence? */
670 if (SYNTAX_CODE_END_SECOND_P (syncode))
673 UPDATE_SYNTAX_CACHE_BACKWARD (from - 1);
675 SYNTAX_CODE_FROM_CACHE (mirrortab, BUF_FETCH_CHAR (buf, from - 1));
677 if (SYNTAX_CODES_END_P (prev_syncode, syncode))
681 SYNTAX_CODES_COMMENT_MASK_END (prev_syncode, syncode);
683 UPDATE_SYNTAX_CACHE_BACKWARD (from);
684 c = BUF_FETCH_CHAR (buf, from);
686 /* Found a comment-end sequence, so skip past the
687 check for a comment-start */
692 /* 2-char comment start sequence? */
693 if (SYNTAX_CODE_START_SECOND_P (syncode))
696 UPDATE_SYNTAX_CACHE_BACKWARD (from - 1);
698 SYNTAX_CODE_FROM_CACHE (mirrortab, BUF_FETCH_CHAR (buf, from - 1));
700 if (SYNTAX_CODES_START_P (prev_syncode, syncode))
704 SYNTAX_CODES_COMMENT_MASK_START (prev_syncode, syncode);
706 UPDATE_SYNTAX_CACHE_BACKWARD (from);
707 c = BUF_FETCH_CHAR (buf, from);
712 /* Ignore escaped characters. */
713 if (char_quoted (buf, from))
716 /* Track parity of quotes. */
720 if (my_stringend == 0)
722 /* If we have two kinds of string delimiters.
723 There's no way to grok this scanning backwards. */
724 else if (my_stringend != c)
728 if (code == Sstring_fence || code == Scomment_fence)
731 if (my_stringend == 0)
733 code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE;
734 /* If we have two kinds of string delimiters.
735 There's no way to grok this scanning backwards. */
736 else if (my_stringend != (code == Sstring_fence
737 ? ST_STRING_STYLE : ST_COMMENT_STYLE))
741 /* Record comment-starters according to that
742 quote-parity to the comment-end. */
743 if (code == Scomment && styles_match_p)
745 comstart_parity = parity;
749 /* If we find another earlier comment-ender,
750 any comment-starts earlier than that don't count
751 (because they go with the earlier comment-ender). */
752 if (code == Sendcomment && styles_match_p)
755 /* Assume a defun-start point is outside of strings. */
757 && (from == stop || BUF_FETCH_CHAR (buf, from - 1) == '\n'))
761 if (comstart_pos == 0)
763 /* If the earliest comment starter
764 is followed by uniform paired string quotes or none,
765 we know it can't be inside a string
766 since if it were then the comment ender would be inside one.
767 So it does start a comment. Skip back to it. */
768 else if (comstart_parity == 0 && !string_lossage)
772 /* We had two kinds of string delimiters mixed up
773 together. Decode this going forwards.
774 Scan fwd from the previous comment ender
775 to the one in question; this records where we
776 last passed a comment starter. */
778 struct lisp_parse_state state;
779 scan_sexps_forward (buf, &state, find_defun_start (buf, comment_end),
780 comment_end - 1, -10000, 0, Qnil, 0);
782 from = state.comstr_start;
784 /* We can't grok this as a comment; scan it normally. */
786 UPDATE_SYNTAX_CACHE_FORWARD (from - 1);
792 find_end_of_comment (struct buffer *buf, Bufpos from, Bufpos stop, int comstyle)
796 /* mask to match comment styles against; for ST_COMMENT_STYLE, this
797 will get set to SYNTAX_COMMENT_STYLE_B, but never get checked */
798 int mask = comstyle ? SYNTAX_COMMENT_STYLE_B : SYNTAX_COMMENT_STYLE_A;
800 /* This is only called by functions which have already set up the
801 syntax_cache and are keeping it up-to-date */
809 UPDATE_SYNTAX_CACHE_FORWARD (from);
810 c = BUF_FETCH_CHAR (buf, from);
812 /* Test for generic comments */
813 if (comstyle == ST_COMMENT_STYLE)
815 if (SYNTAX_FROM_CACHE (mirrortab, c) == Scomment_fence)
818 UPDATE_SYNTAX_CACHE_FORWARD (from);
822 continue; /* No need to test other comment styles in a
827 if (SYNTAX_FROM_CACHE (mirrortab, c) == Sendcomment
828 && SYNTAX_CODE_MATCHES_1CHAR_P
829 (SYNTAX_CODE_FROM_CACHE (mirrortab, c), mask))
830 /* we have encountered a comment end of the same style
831 as the comment sequence which began this comment
835 UPDATE_SYNTAX_CACHE_FORWARD (from);
839 prev_code = SYNTAX_CODE_FROM_CACHE (mirrortab, c);
841 UPDATE_SYNTAX_CACHE_FORWARD (from);
843 && SYNTAX_CODES_MATCH_END_P
845 SYNTAX_CODE_FROM_CACHE (mirrortab, BUF_FETCH_CHAR (buf, from)),
849 /* we have encountered a comment end of the same style
850 as the comment sequence which began this comment
854 UPDATE_SYNTAX_CACHE_FORWARD (from);
862 /* #### between FSF 19.23 and 19.28 there are some changes to the logic
863 in this function (and minor changes to find_start_of_comment(),
864 above, which is part of Fforward_comment() in FSF). Attempts to port
865 that logic made this function break, so I'm leaving it out. If anyone
866 ever complains about this function not working properly, take a look
867 at those changes. --ben */
869 DEFUN ("forward-comment", Fforward_comment, 0, 2, 0, /*
870 Move forward across up to COUNT comments, or backwards if COUNT is negative.
871 Stop scanning if we find something other than a comment or whitespace.
872 Set point to where scanning stops.
873 If COUNT comments are found as expected, with nothing except whitespace
874 between them, return t; otherwise return nil.
875 Point is set in either case.
876 COUNT defaults to 1, and BUFFER defaults to the current buffer.
883 enum syntaxcode code;
886 struct buffer *buf = decode_buffer (buffer, 0);
898 SETUP_SYNTAX_CACHE (from, n);
906 int comstyle = 0; /* mask for finding matching comment style */
908 if (char_quoted (buf, from))
914 UPDATE_SYNTAX_CACHE_FORWARD (from);
915 c = BUF_FETCH_CHAR (buf, from);
916 code = SYNTAX_FROM_CACHE (mirrortab, c);
917 syncode = SYNTAX_CODE_FROM_CACHE (mirrortab, c);
919 if (code == Scomment)
921 /* we have encountered a single character comment start
922 sequence, and we are ignoring all text inside comments.
923 we must record the comment style this character begins
924 so that later, only a comment end of the same style actually
925 ends the comment section */
926 comstyle = SYNTAX_CODE_COMMENT_1CHAR_MASK (syncode)
927 == SYNTAX_COMMENT_STYLE_A ? 0 : 1;
930 else if (code == Scomment_fence)
934 comstyle = ST_COMMENT_STYLE;
938 && SYNTAX_CODE_START_FIRST_P (syncode))
941 UPDATE_SYNTAX_CACHE_FORWARD (from + 1);
943 SYNTAX_CODE_FROM_CACHE (mirrortab,
944 BUF_FETCH_CHAR (buf, from + 1));
946 if (SYNTAX_CODES_START_P (syncode, next_syncode))
948 /* we have encountered a 2char comment start sequence and we
949 are ignoring all text inside comments. we must record
950 the comment style this sequence begins so that later,
951 only a comment end of the same style actually ends
952 the comment section */
955 SYNTAX_CODES_COMMENT_MASK_START (syncode, next_syncode)
956 == SYNTAX_COMMENT_STYLE_A ? 0 : 1;
961 if (code == Scomment)
963 Bufpos newfrom = find_end_of_comment (buf, from, stop, comstyle);
966 /* we stopped because from==stop */
967 BUF_SET_PT (buf, stop);
972 /* We have skipped one comment. */
975 else if (code != Swhitespace
976 && code != Sendcomment
977 && code != Scomment )
979 BUF_SET_PT (buf, from);
985 /* End of comment reached */
993 stop = BUF_BEGV (buf);
996 int comstyle = 0; /* mask for finding matching comment style */
999 if (char_quoted (buf, from))
1005 c = BUF_FETCH_CHAR (buf, from);
1006 code = SYNTAX_FROM_CACHE (mirrortab, c);
1007 syncode = SYNTAX_CODE_FROM_CACHE (mirrortab, c);
1009 if (code == Sendcomment)
1011 /* we have found a single char end comment. we must record
1012 the comment style encountered so that later, we can match
1013 only the proper comment begin sequence of the same style */
1014 comstyle = SYNTAX_CODE_COMMENT_1CHAR_MASK (syncode)
1015 == SYNTAX_COMMENT_STYLE_A ? 0 : 1;
1018 else if (code == Scomment_fence)
1021 comstyle = ST_COMMENT_STYLE;
1024 else if (from > stop
1025 && SYNTAX_CODE_END_SECOND_P (syncode))
1028 UPDATE_SYNTAX_CACHE_BACKWARD (from - 1);
1030 SYNTAX_CODE_FROM_CACHE (mirrortab,
1031 BUF_FETCH_CHAR (buf, from - 1));
1032 if (SYNTAX_CODES_END_P (prev_syncode, syncode))
1034 /* We must record the comment style encountered so that
1035 later, we can match only the proper comment begin
1036 sequence of the same style. */
1038 comstyle = SYNTAX_CODES_COMMENT_MASK_END
1039 (prev_syncode, syncode) == SYNTAX_COMMENT_STYLE_A ? 0 : 1;
1044 if (code == Sendcomment)
1046 from = find_start_of_comment (buf, from, stop, comstyle);
1050 else if (code != Swhitespace
1052 && code != Sendcomment)
1054 BUF_SET_PT (buf, from + 1);
1062 BUF_SET_PT (buf, from);
1068 scan_lists (struct buffer *buf, Bufpos from, int count, int depth,
1069 int sexpflag, int noerror)
1075 enum syntaxcode code;
1077 int min_depth = depth; /* Err out if depth gets less than this. */
1079 if (depth > 0) min_depth = 0;
1081 SETUP_SYNTAX_CACHE_FOR_BUFFER (buf, from, count);
1086 stop = BUF_ZV (buf);
1089 int comstyle = 0; /* mask for finding matching comment style */
1091 UPDATE_SYNTAX_CACHE_FORWARD (from);
1092 c = BUF_FETCH_CHAR (buf, from);
1093 code = SYNTAX_FROM_CACHE (mirrortab, c);
1094 syncode = SYNTAX_CODE_FROM_CACHE (mirrortab, c);
1097 /* a 1-char comment start sequence */
1098 if (code == Scomment && parse_sexp_ignore_comments)
1100 comstyle = SYNTAX_CODE_COMMENT_1CHAR_MASK (syncode) ==
1101 SYNTAX_COMMENT_STYLE_A ? 0 : 1;
1104 /* else, a 2-char comment start sequence? */
1105 else if (from < stop
1106 && SYNTAX_CODE_START_FIRST_P (syncode)
1107 && parse_sexp_ignore_comments)
1110 UPDATE_SYNTAX_CACHE_FORWARD (from);
1112 SYNTAX_CODE_FROM_CACHE (mirrortab, BUF_FETCH_CHAR (buf, from));
1114 if (SYNTAX_CODES_START_P (syncode, next_syncode))
1116 /* we have encountered a comment start sequence and we
1117 are ignoring all text inside comments. we must record
1118 the comment style this sequence begins so that later,
1119 only a comment end of the same style actually ends
1120 the comment section */
1122 comstyle = SYNTAX_CODES_COMMENT_MASK_START
1123 (syncode, next_syncode) == SYNTAX_COMMENT_STYLE_A ? 0 : 1;
1127 UPDATE_SYNTAX_CACHE_FORWARD (from);
1129 if (SYNTAX_CODE_PREFIX (syncode))
1136 if (from == stop) goto lose;
1138 /* treat following character as a word constituent */
1141 if (depth || !sexpflag) break;
1142 /* This word counts as a sexp; return at end of it. */
1145 UPDATE_SYNTAX_CACHE_FORWARD (from);
1146 switch (SYNTAX_FROM_CACHE (mirrortab,
1147 BUF_FETCH_CHAR (buf, from)))
1152 if (from == stop) goto lose;
1165 case Scomment_fence:
1166 comstyle = ST_COMMENT_STYLE;
1168 if (!parse_sexp_ignore_comments)
1170 UPDATE_SYNTAX_CACHE_FORWARD (from);
1173 find_end_of_comment (buf, from, stop, comstyle);
1176 /* we stopped because from == stop in search forward */
1189 if (from != stop && c == BUF_FETCH_CHAR (buf, from))
1199 if (!++depth) goto done;
1204 if (!--depth) goto done;
1205 if (depth < min_depth)
1209 error ("Containing expression ends prematurely");
1218 if (code != Sstring_fence)
1220 /* XEmacs change: call syntax_match on character */
1221 Emchar ch = BUF_FETCH_CHAR (buf, from - 1);
1222 Lisp_Object stermobj =
1223 syntax_match (syntax_cache.current_syntax_table, ch);
1225 if (CHARP (stermobj))
1226 stringterm = XCHAR (stermobj);
1231 stringterm = '\0'; /* avoid compiler warnings */
1237 UPDATE_SYNTAX_CACHE_FORWARD (from);
1238 c = BUF_FETCH_CHAR (buf, from);
1241 : SYNTAX_FROM_CACHE (mirrortab, c) == Sstring_fence)
1244 switch (SYNTAX_FROM_CACHE (mirrortab, c))
1256 if (!depth && sexpflag) goto done;
1265 /* Reached end of buffer. Error if within object,
1266 return nil if between */
1267 if (depth) goto lose;
1271 /* End of object reached */
1280 stop = BUF_BEGV (buf);
1283 int comstyle = 0; /* mask for finding matching comment style */
1286 UPDATE_SYNTAX_CACHE_BACKWARD (from);
1287 quoted = char_quoted (buf, from);
1291 UPDATE_SYNTAX_CACHE_BACKWARD (from);
1294 c = BUF_FETCH_CHAR (buf, from);
1295 code = SYNTAX_FROM_CACHE (mirrortab, c);
1296 syncode = SYNTAX_CODE_FROM_CACHE (mirrortab, c);
1298 if (code == Sendcomment && parse_sexp_ignore_comments)
1300 /* we have found a single char end comment. we must record
1301 the comment style encountered so that later, we can match
1302 only the proper comment begin sequence of the same style */
1303 comstyle = SYNTAX_CODE_COMMENT_1CHAR_MASK (syncode)
1304 == SYNTAX_COMMENT_STYLE_A ? 0 : 1;
1307 else if (from > stop
1308 && SYNTAX_CODE_END_SECOND_P (syncode)
1309 && !char_quoted (buf, from - 1)
1310 && parse_sexp_ignore_comments)
1313 UPDATE_SYNTAX_CACHE_BACKWARD (from - 1);
1314 prev_syncode = SYNTAX_CODE_FROM_CACHE
1315 (mirrortab, BUF_FETCH_CHAR (buf, from - 1));
1317 if (SYNTAX_CODES_END_P (prev_syncode, syncode))
1319 /* we must record the comment style encountered so that
1320 later, we can match only the proper comment begin
1321 sequence of the same style */
1323 comstyle = SYNTAX_CODES_COMMENT_MASK_END
1324 (prev_syncode, syncode) == SYNTAX_COMMENT_STYLE_A ? 0 : 1;
1329 if (SYNTAX_CODE_PREFIX (syncode))
1332 switch (quoted ? Sword : code)
1336 if (depth || !sexpflag) break;
1337 /* This word counts as a sexp; count object finished after
1341 UPDATE_SYNTAX_CACHE_BACKWARD (from);
1342 quoted = char_quoted (buf, from - 1);
1348 SYNTAX_FROM_CACHE (mirrortab,
1349 BUF_FETCH_CHAR (buf, from - 1)))
1351 || syncode == Ssymbol
1352 || syncode == Squote))
1361 if (from != stop && c == BUF_FETCH_CHAR (buf, from - 1))
1371 if (!++depth) goto done2;
1376 if (!--depth) goto done2;
1377 if (depth < min_depth)
1381 error ("Containing expression ends prematurely");
1385 case Scomment_fence:
1386 comstyle = ST_COMMENT_STYLE;
1388 if (parse_sexp_ignore_comments)
1389 from = find_start_of_comment (buf, from, stop, comstyle);
1397 if (code != Sstring_fence)
1399 /* XEmacs change: call syntax_match() on character */
1400 Emchar ch = BUF_FETCH_CHAR (buf, from);
1401 Lisp_Object stermobj =
1402 syntax_match (syntax_cache.current_syntax_table, ch);
1404 if (CHARP (stermobj))
1405 stringterm = XCHAR (stermobj);
1410 stringterm = '\0'; /* avoid compiler warnings */
1414 if (from == stop) goto lose;
1416 UPDATE_SYNTAX_CACHE_BACKWARD (from - 1);
1417 c = BUF_FETCH_CHAR (buf, from - 1);
1419 if ((code == Sstring
1421 : SYNTAX_FROM_CACHE (mirrortab, c) == Sstring_fence)
1422 && !char_quoted (buf, from - 1))
1430 if (!depth && sexpflag) goto done2;
1436 /* Reached start of buffer. Error if within object,
1437 return nil if between */
1438 if (depth) goto lose;
1447 return (make_int (from));
1451 error ("Unbalanced parentheses");
1456 char_quoted (struct buffer *buf, Bufpos pos)
1458 enum syntaxcode code;
1459 Bufpos beg = BUF_BEGV (buf);
1461 Bufpos startpos = pos;
1465 UPDATE_SYNTAX_CACHE_BACKWARD (pos - 1);
1466 code = SYNTAX_FROM_CACHE (mirrortab, BUF_FETCH_CHAR (buf, pos - 1));
1468 if (code != Scharquote && code != Sescape)
1474 UPDATE_SYNTAX_CACHE (startpos);
1478 DEFUN ("scan-lists", Fscan_lists, 3, 5, 0, /*
1479 Scan from character number FROM by COUNT lists.
1480 Returns the character number of the position thus found.
1482 If DEPTH is nonzero, paren depth begins counting from that value,
1483 only places where the depth in parentheses becomes zero
1484 are candidates for stopping; COUNT such places are counted.
1485 Thus, a positive value for DEPTH means go out levels.
1487 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
1489 If the beginning or end of (the accessible part of) the buffer is reached
1490 and the depth is wrong, an error is signaled.
1491 If the depth is right but the count is not used up, nil is returned.
1493 If optional arg BUFFER is non-nil, scanning occurs in that buffer instead
1494 of in the current buffer.
1496 If optional arg NOERROR is non-nil, scan-lists will return nil instead of
1497 signalling an error.
1499 (from, count, depth, buffer, noerror))
1506 buf = decode_buffer (buffer, 0);
1508 return scan_lists (buf, XINT (from), XINT (count), XINT (depth), 0,
1512 DEFUN ("scan-sexps", Fscan_sexps, 2, 4, 0, /*
1513 Scan from character number FROM by COUNT balanced expressions.
1514 If COUNT is negative, scan backwards.
1515 Returns the character number of the position thus found.
1517 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
1519 If the beginning or end of (the accessible part of) the buffer is reached
1520 in the middle of a parenthetical grouping, an error is signaled.
1521 If the beginning or end is reached between groupings
1522 but before count is used up, nil is returned.
1524 If optional arg BUFFER is non-nil, scanning occurs in that buffer instead
1525 of in the current buffer.
1527 If optional arg NOERROR is non-nil, scan-sexps will return nil instead of
1528 signalling an error.
1530 (from, count, buffer, noerror))
1532 struct buffer *buf = decode_buffer (buffer, 0);
1536 return scan_lists (buf, XINT (from), XINT (count), 0, 1, !NILP (noerror));
1539 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, 0, 1, 0, /*
1540 Move point backward over any number of chars with prefix syntax.
1541 This includes chars with "quote" or "prefix" syntax (' or p).
1543 Optional arg BUFFER defaults to the current buffer.
1547 struct buffer *buf = decode_buffer (buffer, 0);
1548 Bufpos beg = BUF_BEGV (buf);
1549 Bufpos pos = BUF_PT (buf);
1551 Lisp_Char_Table *mirrortab = XCHAR_TABLE (buf->mirror_syntax_table);
1553 Emchar c = '\0'; /* initialize to avoid compiler warnings */
1556 SETUP_SYNTAX_CACHE_FOR_BUFFER (buf, pos, -1);
1558 while (pos > beg && !char_quoted (buf, pos - 1)
1559 /* Previous statement updates syntax table. */
1560 && (SYNTAX_FROM_CACHE (mirrortab, c = BUF_FETCH_CHAR (buf, pos - 1)) == Squote
1561 || SYNTAX_CODE_PREFIX (SYNTAX_CODE_FROM_CACHE (mirrortab, c))))
1564 BUF_SET_PT (buf, pos);
1569 /* Parse forward from FROM to END,
1570 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
1571 and return a description of the state of the parse at END.
1572 If STOPBEFORE is nonzero, stop at the start of an atom.
1573 If COMMENTSTOP is nonzero, stop at the start of a comment. */
1576 scan_sexps_forward (struct buffer *buf, struct lisp_parse_state *stateptr,
1577 Bufpos from, Bufpos end,
1578 int targetdepth, int stopbefore,
1579 Lisp_Object oldstate,
1582 struct lisp_parse_state state;
1584 enum syntaxcode code;
1585 struct level { int last, prev; };
1586 struct level levelstart[100];
1587 struct level *curlevel = levelstart;
1588 struct level *endlevel = levelstart + 100;
1589 int depth; /* Paren depth of current scanning location.
1590 level - levelstart equals this except
1591 when the depth becomes negative. */
1592 int mindepth; /* Lowest DEPTH value seen. */
1593 int start_quoted = 0; /* Nonzero means starting after a char quote */
1594 int boundary_stop = commentstop == -1;
1597 SETUP_SYNTAX_CACHE (from, 1);
1598 if (NILP (oldstate))
1601 state.instring = -1;
1602 state.incomment = 0;
1603 state.comstyle = 0; /* comment style a by default */
1604 state.comstr_start = -1; /* no comment/string seen. */
1608 tem = Fcar (oldstate); /* elt 0, depth */
1614 oldstate = Fcdr (oldstate);
1615 oldstate = Fcdr (oldstate);
1616 oldstate = Fcdr (oldstate);
1617 tem = Fcar (oldstate); /* elt 3, instring */
1618 state.instring = ( !NILP (tem)
1619 ? ( INTP (tem) ? XINT (tem) : ST_STRING_STYLE)
1622 oldstate = Fcdr (oldstate);
1623 tem = Fcar (oldstate); /* elt 4, incomment */
1624 state.incomment = !NILP (tem);
1626 oldstate = Fcdr (oldstate);
1627 tem = Fcar (oldstate); /* elt 5, follows-quote */
1628 start_quoted = !NILP (tem);
1630 /* if the eighth element of the list is nil, we are in comment style
1631 a; if it is t, we are in comment style b; if it is 'syntax-table,
1632 we are in a generic comment */
1633 oldstate = Fcdr (oldstate);
1634 oldstate = Fcdr (oldstate);
1635 tem = Fcar (oldstate); /* elt 7, comment style a/b/fence */
1636 state.comstyle = NILP (tem) ? 0 : ( EQ (tem, Qsyntax_table)
1637 ? ST_COMMENT_STYLE : 1 );
1639 oldstate = Fcdr (oldstate); /* elt 8, start of last comment/string */
1640 tem = Fcar (oldstate);
1641 state.comstr_start = NILP (tem) ? -1 : XINT (tem);
1643 /* elt 9, char numbers of starts-of-expression of levels
1644 (starting from outermost). */
1645 oldstate = Fcdr (oldstate);
1646 tem = Fcar (oldstate); /* elt 9, intermediate data for
1647 continuation of parsing (subject
1649 while (!NILP (tem)) /* >= second enclosing sexps. */
1651 curlevel->last = XINT (Fcar (tem));
1652 if (++curlevel == endlevel)
1653 error ("Nesting too deep for parser");
1654 curlevel->prev = -1;
1655 curlevel->last = -1;
1662 curlevel->prev = -1;
1663 curlevel->last = -1;
1665 /* Enter the loop at a place appropriate for initial state. */
1667 if (state.incomment) goto startincomment;
1668 if (state.instring >= 0)
1670 if (start_quoted) goto startquotedinstring;
1673 if (start_quoted) goto startquoted;
1682 UPDATE_SYNTAX_CACHE_FORWARD (from);
1683 c = BUF_FETCH_CHAR (buf, from);
1684 code = SYNTAX_FROM_CACHE (mirrortab, c);
1685 syncode = SYNTAX_CODE_FROM_CACHE (mirrortab, c);
1688 /* record the comment style we have entered so that only the
1689 comment-ender sequence (or single char) of the same style
1690 actually terminates the comment section. */
1691 if (code == Scomment)
1694 SYNTAX_CODE_COMMENT_1CHAR_MASK (syncode)
1695 == SYNTAX_COMMENT_STYLE_A ? 0 : 1;
1696 state.comstr_start = from - 1;
1699 /* a generic comment delimiter? */
1700 else if (code == Scomment_fence)
1702 state.comstyle = ST_COMMENT_STYLE;
1703 state.comstr_start = from - 1;
1707 else if (from < end &&
1708 SYNTAX_CODE_START_FIRST_P (syncode))
1711 UPDATE_SYNTAX_CACHE_FORWARD (from);
1713 SYNTAX_CODE_FROM_CACHE (mirrortab, BUF_FETCH_CHAR (buf, from));
1715 if (SYNTAX_CODES_START_P (syncode, next_syncode))
1718 state.comstyle = SYNTAX_CODES_COMMENT_MASK_START
1719 (syncode, next_syncode) == SYNTAX_COMMENT_STYLE_A ? 0 : 1;
1720 state.comstr_start = from - 1;
1722 UPDATE_SYNTAX_CACHE_FORWARD (from);
1726 if (SYNTAX_CODE_PREFIX (syncode))
1732 if (stopbefore) goto stop; /* this arg means stop at sexp start */
1733 curlevel->last = from - 1;
1735 if (from == end) goto endquoted;
1738 /* treat following character as a word constituent */
1741 if (stopbefore) goto stop; /* this arg means stop at sexp start */
1742 curlevel->last = from - 1;
1746 UPDATE_SYNTAX_CACHE_FORWARD (from);
1747 switch (SYNTAX_FROM_CACHE (mirrortab, BUF_FETCH_CHAR (buf, from)))
1752 if (from == end) goto endquoted;
1764 curlevel->prev = curlevel->last;
1768 state.incomment = 1;
1769 if (commentstop || boundary_stop) goto done;
1771 if (commentstop == 1)
1773 UPDATE_SYNTAX_CACHE_FORWARD (from);
1775 Bufpos newfrom = find_end_of_comment (buf, from, end, state.comstyle);
1778 /* we terminated search because from == end */
1784 state.incomment = 0;
1785 state.comstyle = 0; /* reset the comment style */
1786 if (boundary_stop) goto done;
1790 if (stopbefore) goto stop; /* this arg means stop at sexp start */
1792 /* curlevel++->last ran into compiler bug on Apollo */
1793 curlevel->last = from - 1;
1794 if (++curlevel == endlevel)
1795 error ("Nesting too deep for parser");
1796 curlevel->prev = -1;
1797 curlevel->last = -1;
1798 if (targetdepth == depth) goto done;
1803 if (depth < mindepth)
1805 if (curlevel != levelstart)
1807 curlevel->prev = curlevel->last;
1808 if (targetdepth == depth) goto done;
1813 state.comstr_start = from - 1;
1814 if (stopbefore) goto stop; /* this arg means stop at sexp start */
1815 curlevel->last = from - 1;
1816 if (code == Sstring_fence)
1818 state.instring = ST_STRING_STYLE;
1822 /* XEmacs change: call syntax_match() on character */
1823 Emchar ch = BUF_FETCH_CHAR (buf, from - 1);
1824 Lisp_Object stermobj =
1825 syntax_match (syntax_cache.current_syntax_table, ch);
1827 if (CHARP (stermobj))
1828 state.instring = XCHAR (stermobj);
1830 state.instring = ch;
1832 if (boundary_stop) goto done;
1836 enum syntaxcode temp_code;
1838 if (from >= end) goto done;
1840 UPDATE_SYNTAX_CACHE_FORWARD (from);
1841 c = BUF_FETCH_CHAR (buf, from);
1842 temp_code = SYNTAX_FROM_CACHE (mirrortab, c);
1845 state.instring != ST_STRING_STYLE &&
1846 temp_code == Sstring &&
1847 c == state.instring) break;
1852 if (state.instring == ST_STRING_STYLE)
1859 startquotedinstring:
1860 if (from >= end) goto endquoted;
1869 state.instring = -1;
1870 curlevel->prev = curlevel->last;
1872 if (boundary_stop) goto done;
1882 case Scomment_fence:
1890 stop: /* Here if stopping before start of sexp. */
1891 from--; /* We have just fetched the char that starts it; */
1892 goto done; /* but return the position before it. */
1897 state.depth = depth;
1898 state.mindepth = mindepth;
1899 state.thislevelstart = curlevel->prev;
1900 state.prevlevelstart
1901 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
1902 state.location = from;
1903 state.levelstarts = Qnil;
1904 while (--curlevel >= levelstart)
1905 state.levelstarts = Fcons (make_int (curlevel->last),
1911 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, 2, 7, 0, /*
1912 Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
1913 Parsing stops at TO or when certain criteria are met;
1914 point is set to where parsing stops.
1915 If fifth arg OLDSTATE is omitted or nil,
1916 parsing assumes that FROM is the beginning of a function.
1917 Value is a list of nine elements describing final state of parsing:
1919 1. character address of start of innermost containing list; nil if none.
1920 2. character address of start of last complete sexp terminated.
1921 3. non-nil if inside a string.
1922 (It is the character that will terminate the string,
1923 or t if the string should be terminated by an explicit
1924 `syntax-table' property.)
1925 4. t if inside a comment.
1926 5. t if following a quote character.
1927 6. the minimum paren-depth encountered during this scan.
1928 7. nil if in comment style a, or not in a comment; t if in comment style b;
1929 `syntax-table' if given by an explicit `syntax-table' property.
1930 8. character address of start of last comment or string; nil if none.
1931 9. Intermediate data for continuation of parsing (subject to change).
1932 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
1933 in parentheses becomes equal to TARGETDEPTH.
1934 Fourth arg STOPBEFORE non-nil means stop when come to
1935 any character that starts a sexp.
1936 Fifth arg OLDSTATE is a nine-element list like what this function returns.
1937 It is used to initialize the state of the parse. Its second and third
1938 elements are ignored.
1939 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment. If it
1940 is `syntax-table', stop after the start of a comment or a string, or after
1941 the end of a comment or string.
1943 (from, to, targetdepth, stopbefore, oldstate, commentstop, buffer))
1945 struct lisp_parse_state state;
1948 struct buffer *buf = decode_buffer (buffer, 0);
1951 if (!NILP (targetdepth))
1953 CHECK_INT (targetdepth);
1954 target = XINT (targetdepth);
1957 target = -100000; /* We won't reach this depth */
1959 get_buffer_range_char (buf, from, to, &start, &end, 0);
1960 scan_sexps_forward (buf, &state, start, end,
1961 target, !NILP (stopbefore), oldstate,
1963 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
1964 BUF_SET_PT (buf, state.location);
1968 val = Fcons (state.levelstarts, val);
1969 val = Fcons ((state.incomment || (state.instring >= 0))
1970 ? make_int (state.comstr_start) : Qnil, val);
1971 val = Fcons (state.comstyle ? (state.comstyle == ST_COMMENT_STYLE
1972 ? Qsyntax_table : Qt) : Qnil, val);
1973 val = Fcons (make_int (state.mindepth), val);
1974 val = Fcons (state.quoted ? Qt : Qnil, val);
1975 val = Fcons (state.incomment ? Qt : Qnil, val);
1976 val = Fcons (state.instring < 0
1978 : (state.instring == ST_STRING_STYLE
1979 ? Qt : make_int (state.instring)), val);
1980 val = Fcons (state.thislevelstart < 0 ? Qnil : make_int (state.thislevelstart), val);
1981 val = Fcons (state.prevlevelstart < 0 ? Qnil : make_int (state.prevlevelstart), val);
1982 val = Fcons (make_int (state.depth), val);
1988 /* Updating of the mirror syntax table.
1990 Each syntax table has a corresponding mirror table in it.
1991 Whenever we make a change to a syntax table, we call
1992 update_syntax_table() on it.
1994 #### We really only need to map over the changed range.
1996 If we change the standard syntax table, we need to map over
1997 all tables because any of them could be inheriting from the
1998 standard syntax table.
2000 When `set-syntax-table' is called, we set the buffer's mirror
2001 syntax table as well.
2006 Lisp_Object mirrortab;
2011 cmst_mapfun (struct chartab_range *range, Lisp_Object val, void *arg)
2013 struct cmst_arg *closure = (struct cmst_arg *) arg;
2017 if (SYNTAX_FROM_CODE (XINT (val)) == Sinherit
2018 && closure->check_inherit)
2020 struct cmst_arg recursive;
2022 recursive.mirrortab = closure->mirrortab;
2023 recursive.check_inherit = 0;
2024 map_char_table (XCHAR_TABLE (Vstandard_syntax_table), range,
2025 cmst_mapfun, &recursive);
2028 put_char_table (XCHAR_TABLE (closure->mirrortab), range, val);
2033 update_just_this_syntax_table (Lisp_Char_Table *ct)
2035 struct chartab_range range;
2036 struct cmst_arg arg;
2038 arg.mirrortab = ct->mirror_table;
2039 arg.check_inherit = (CHAR_TABLEP (Vstandard_syntax_table)
2040 && ct != XCHAR_TABLE (Vstandard_syntax_table));
2041 range.type = CHARTAB_RANGE_ALL;
2042 map_char_table (ct, &range, cmst_mapfun, &arg);
2045 /* Called from chartab.c when a change is made to a syntax table.
2046 If this is the standard syntax table, we need to recompute
2047 *all* syntax tables (yuck). Otherwise we just recompute this
2051 update_syntax_table (Lisp_Char_Table *ct)
2053 /* Don't be stymied at startup. */
2054 if (CHAR_TABLEP (Vstandard_syntax_table)
2055 && ct == XCHAR_TABLE (Vstandard_syntax_table))
2059 for (syntab = Vall_syntax_tables; !NILP (syntab);
2060 syntab = XCHAR_TABLE (syntab)->next_table)
2061 update_just_this_syntax_table (XCHAR_TABLE (syntab));
2064 update_just_this_syntax_table (ct);
2068 /************************************************************************/
2069 /* initialization */
2070 /************************************************************************/
2073 syms_of_syntax (void)
2075 defsymbol (&Qsyntax_table_p, "syntax-table-p");
2076 defsymbol (&Qsyntax_table, "syntax-table");
2078 DEFSUBR (Fsyntax_table_p);
2079 DEFSUBR (Fsyntax_table);
2080 DEFSUBR (Fstandard_syntax_table);
2081 DEFSUBR (Fcopy_syntax_table);
2082 DEFSUBR (Fset_syntax_table);
2083 DEFSUBR (Fsyntax_designator_chars);
2084 DEFSUBR (Fchar_syntax);
2085 DEFSUBR (Fmatching_paren);
2086 /* DEFSUBR (Fmodify_syntax_entry); now in Lisp. */
2087 /* DEFSUBR (Fdescribe_syntax); now in Lisp. */
2089 DEFSUBR (Fforward_word);
2091 DEFSUBR (Fforward_comment);
2092 DEFSUBR (Fscan_lists);
2093 DEFSUBR (Fscan_sexps);
2094 DEFSUBR (Fbackward_prefix_chars);
2095 DEFSUBR (Fparse_partial_sexp);
2099 vars_of_syntax (void)
2101 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments /*
2102 Non-nil means `forward-sexp', etc., should treat comments as whitespace.
2104 parse_sexp_ignore_comments = 0;
2106 DEFVAR_BOOL ("lookup-syntax-properties", &lookup_syntax_properties /*
2107 Non-nil means `forward-sexp', etc., grant `syntax-table' property.
2108 The value of this property should be either a syntax table, or a cons
2109 of the form (SYNTAXCODE . MATCHCHAR), SYNTAXCODE being the numeric
2110 syntax code, MATCHCHAR being nil or the character to match (which is
2111 relevant only for open/close type.
2113 lookup_syntax_properties = 1;
2115 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes /*
2116 Non-nil means `forward-word', etc., should treat escape chars part of words.
2118 words_include_escapes = 0;
2120 no_quit_in_re_search = 0;
2124 define_standard_syntax (const char *p, enum syntaxcode syn)
2127 Fput_char_table (make_char (*p), make_int (syn), Vstandard_syntax_table);
2131 complex_vars_of_syntax (void)
2135 /* Set this now, so first buffer creation can refer to it. */
2136 /* Make it nil before calling copy-syntax-table
2137 so that copy-syntax-table will know not to try to copy from garbage */
2138 Vstandard_syntax_table = Qnil;
2139 Vstandard_syntax_table = Fcopy_syntax_table (Qnil);
2140 staticpro (&Vstandard_syntax_table);
2142 Vsyntax_designator_chars_string = make_string_nocopy (syntax_code_spec,
2144 staticpro (&Vsyntax_designator_chars_string);
2146 fill_char_table (XCHAR_TABLE (Vstandard_syntax_table), make_int (Spunct));
2148 for (i = 0; i <= 32; i++) /* Control 0 plus SPACE */
2149 Fput_char_table (make_char (i), make_int (Swhitespace),
2150 Vstandard_syntax_table);
2151 for (i = 127; i <= 159; i++) /* DEL plus Control 1 */
2152 Fput_char_table (make_char (i), make_int (Swhitespace),
2153 Vstandard_syntax_table);
2155 define_standard_syntax ("abcdefghijklmnopqrstuvwxyz"
2156 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
2159 define_standard_syntax ("\"", Sstring);
2160 define_standard_syntax ("\\", Sescape);
2161 define_standard_syntax ("_-+*/&|<>=", Ssymbol);
2162 define_standard_syntax (".,;:?!#@~^'`", Spunct);
2164 for (p = "()[]{}"; *p; p+=2)
2166 Fput_char_table (make_char (p[0]),
2167 Fcons (make_int (Sopen), make_char (p[1])),
2168 Vstandard_syntax_table);
2169 Fput_char_table (make_char (p[1]),
2170 Fcons (make_int (Sclose), make_char (p[0])),
2171 Vstandard_syntax_table);