Merge from emacs-23; up to 2012-01-19T07:15:48Z!rgm@gnu.org.
[bpt/emacs.git] / src / syntax.c
CommitLineData
8489eb67 1/* GNU Emacs routines to deal with syntax tables; also word and list parsing.
acaf905b 2 Copyright (C) 1985, 1987, 1993-1995, 1997-1999, 2001-2012
429ab54e 3 Free Software Foundation, Inc.
8489eb67
RS
4
5This file is part of GNU Emacs.
6
9ec0b715 7GNU Emacs is free software: you can redistribute it and/or modify
8489eb67 8it under the terms of the GNU General Public License as published by
9ec0b715
GM
9the Free Software Foundation, either version 3 of the License, or
10(at your option) any later version.
8489eb67
RS
11
12GNU Emacs is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
9ec0b715 18along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
8489eb67
RS
19
20
18160b98 21#include <config.h>
1b2de274 22
8489eb67 23#include <ctype.h>
1b2de274 24#include <sys/types.h>
d7306fe6 25#include <setjmp.h>
8489eb67
RS
26#include "lisp.h"
27#include "commands.h"
28#include "buffer.h"
5c7b02ab 29#include "character.h"
e35f6ff7 30#include "keymap.h"
a1bc88d4 31#include "regex.h"
195d1361
RS
32
33/* Make syntax table lookup grant data in gl_state. */
34#define SYNTAX_ENTRY_VIA_PROPERTY
35
8489eb67 36#include "syntax.h"
195d1361 37#include "intervals.h"
c5683ceb
SM
38#include "category.h"
39
40/* Then there are seven single-bit flags that have the following meanings:
41 1. This character is the first of a two-character comment-start sequence.
42 2. This character is the second of a two-character comment-start sequence.
43 3. This character is the first of a two-character comment-end sequence.
44 4. This character is the second of a two-character comment-end sequence.
45 5. This character is a prefix, for backward-prefix-chars.
46 6. The char is part of a delimiter for comments of style "b".
47 7. This character is part of a nestable comment sequence.
48 8. The char is part of a delimiter for comments of style "c".
49 Note that any two-character sequence whose first character has flag 1
50 and whose second character has flag 2 will be interpreted as a comment start.
51
52 bit 6 and 8 are used to discriminate between different comment styles.
53 Languages such as C++ allow two orthogonal syntax start/end pairs
54 and bit 6 is used to determine whether a comment-end or Scommentend
55 ends style a or b. Comment markers can start style a, b, c, or bc.
56 Style a is always the default.
57 For 2-char comment markers, the style b flag is only looked up on the second
58 char of the comment marker and on the first char of the comment ender.
59 For style c (like to for the nested flag), the flag can be placed on any
60 one of the chars.
61 */
62
63/* These macros extract specific flags from an integer
64 that holds the syntax code and the flags. */
65
66#define SYNTAX_FLAGS_COMSTART_FIRST(flags) (((flags) >> 16) & 1)
67
68#define SYNTAX_FLAGS_COMSTART_SECOND(flags) (((flags) >> 17) & 1)
69
70#define SYNTAX_FLAGS_COMEND_FIRST(flags) (((flags) >> 18) & 1)
71
72#define SYNTAX_FLAGS_COMEND_SECOND(flags) (((flags) >> 19) & 1)
73
74#define SYNTAX_FLAGS_PREFIX(flags) (((flags) >> 20) & 1)
75
76#define SYNTAX_FLAGS_COMMENT_STYLEB(flags) (((flags) >> 21) & 1)
77#define SYNTAX_FLAGS_COMMENT_STYLEC(flags) (((flags) >> 22) & 2)
78/* FLAGS should be the flags of the main char of the comment marker, e.g.
79 the second for comstart and the first for comend. */
80#define SYNTAX_FLAGS_COMMENT_STYLE(flags, other_flags) \
81 (SYNTAX_FLAGS_COMMENT_STYLEB (flags) \
82 | SYNTAX_FLAGS_COMMENT_STYLEC (flags) \
83 | SYNTAX_FLAGS_COMMENT_STYLEC (other_flags))
84
85#define SYNTAX_FLAGS_COMMENT_NESTED(flags) (((flags) >> 22) & 1)
86
87/* These macros extract a particular flag for a given character. */
88
89#define SYNTAX_COMEND_FIRST(c) \
90 (SYNTAX_FLAGS_COMEND_FIRST (SYNTAX_WITH_FLAGS (c)))
91#define SYNTAX_PREFIX(c) (SYNTAX_FLAGS_PREFIX (SYNTAX_WITH_FLAGS (c)))
195d1361
RS
92
93/* We use these constants in place for comment-style and
94 string-ender-char to distinguish comments/strings started by
95 comment_fence and string_fence codes. */
96
97#define ST_COMMENT_STYLE (256 + 1)
98#define ST_STRING_STYLE (256 + 2)
8489eb67 99
955cbe7b
PE
100static Lisp_Object Qsyntax_table_p;
101static Lisp_Object Qsyntax_table, Qscan_error;
8489eb67 102
d4b43b22 103#ifndef __GNUC__
8ea151b2
RS
104/* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
105 if not compiled with GCC. No need to mark it, since it is used
106 only very temporarily. */
107Lisp_Object syntax_temp;
d4b43b22 108#endif
8ea151b2 109
e5d4f4dc
RS
110/* This is the internal form of the parse state used in parse-partial-sexp. */
111
112struct lisp_parse_state
113 {
6449674e
SM
114 int depth; /* Depth at end of parsing. */
115 int instring; /* -1 if not within string, else desired terminator. */
116 int incomment; /* -1 if in unnestable comment else comment nesting */
117 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
118 int quoted; /* Nonzero if just after an escape char at end of parsing */
119 int mindepth; /* Minimum depth seen while scanning. */
120 /* Char number of most recent start-of-expression at current level */
121 EMACS_INT thislevelstart;
122 /* Char number of start of containing expression */
123 EMACS_INT prevlevelstart;
124 EMACS_INT location; /* Char number at which parsing stopped. */
125 EMACS_INT comstr_start; /* Position of last comment/string starter. */
126 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
127 of levels (starting from outermost). */
e5d4f4dc
RS
128 };
129\f
37bef230
RS
130/* These variables are a cache for finding the start of a defun.
131 find_start_pos is the place for which the defun start was found.
132 find_start_value is the defun start position found for it.
6a140a74 133 find_start_value_byte is the corresponding byte position.
37bef230
RS
134 find_start_buffer is the buffer it was found in.
135 find_start_begv is the BEGV value when it was found.
136 find_start_modiff is the value of MODIFF when it was found. */
137
8e2911c2
AS
138static EMACS_INT find_start_pos;
139static EMACS_INT find_start_value;
140static EMACS_INT find_start_value_byte;
37bef230 141static struct buffer *find_start_buffer;
8e2911c2 142static EMACS_INT find_start_begv;
37bef230 143static int find_start_modiff;
6a140a74
RS
144
145
cd64ea1d 146static Lisp_Object Fsyntax_table_p (Lisp_Object);
f57e2426
J
147static Lisp_Object skip_chars (int, Lisp_Object, Lisp_Object, int);
148static Lisp_Object skip_syntaxes (int, Lisp_Object, Lisp_Object);
149static Lisp_Object scan_lists (EMACS_INT, EMACS_INT, EMACS_INT, int);
150static void scan_sexps_forward (struct lisp_parse_state *,
151 EMACS_INT, EMACS_INT, EMACS_INT, int,
152 int, Lisp_Object, int);
153static int in_classes (int, Lisp_Object);
195d1361 154\f
c5683ceb
SM
155/* Whether the syntax of the character C has the prefix flag set. */
156int syntax_prefix_flag_p (int c)
157{
158 return SYNTAX_PREFIX (c);
159}
195d1361
RS
160
161struct gl_state_s gl_state; /* Global state of syntax parser. */
162
195d1361
RS
163#define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
164 to scan to property-change. */
165
6a140a74
RS
166/* Update gl_state to an appropriate interval which contains CHARPOS. The
167 sign of COUNT give the relative position of CHARPOS wrt the previously
195d1361 168 valid interval. If INIT, only [be]_property fields of gl_state are
6a140a74 169 valid at start, the rest is filled basing on OBJECT.
195d1361 170
6a140a74 171 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
195d1361
RS
172 direction than the intervals - or in an interval. We update the
173 current syntax-table basing on the property of this interval, and
6a140a74 174 update the interval to start further than CHARPOS - or be
195d1361 175 NULL_INTERVAL. We also update lim_property to be the next value of
6a140a74 176 charpos to call this subroutine again - or be before/after the
195d1361
RS
177 start/end of OBJECT. */
178
179void
d1dfb56c 180update_syntax_table (EMACS_INT charpos, EMACS_INT count, int init,
4f3a2f8d 181 Lisp_Object object)
195d1361
RS
182{
183 Lisp_Object tmp_table;
12cbf13f
PE
184 unsigned cnt = 0;
185 int invalidate = 1;
5a774522 186 INTERVAL i;
195d1361
RS
187
188 if (init)
189 {
bb0de084 190 gl_state.old_prop = Qnil;
195d1361
RS
191 gl_state.start = gl_state.b_property;
192 gl_state.stop = gl_state.e_property;
bb0de084
SM
193 i = interval_of (charpos, object);
194 gl_state.backward_i = gl_state.forward_i = i;
195d1361
RS
195 invalidate = 0;
196 if (NULL_INTERVAL_P (i))
197 return;
f902a008 198 /* interval_of updates only ->position of the return value, so
d80f4cc7 199 update the parents manually to speed up update_interval. */
bb0de084 200 while (!NULL_PARENT (i))
d80f4cc7
RS
201 {
202 if (AM_RIGHT_CHILD (i))
439d5cb4 203 INTERVAL_PARENT (i)->position = i->position
d80f4cc7 204 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
439d5cb4
KR
205 - TOTAL_LENGTH (INTERVAL_PARENT (i))
206 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
d80f4cc7 207 else
439d5cb4 208 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
d80f4cc7 209 + TOTAL_LENGTH (i);
439d5cb4 210 i = INTERVAL_PARENT (i);
d80f4cc7
RS
211 }
212 i = gl_state.forward_i;
bb0de084 213 gl_state.b_property = i->position - gl_state.offset;
ee0cdb48 214 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
195d1361
RS
215 goto update;
216 }
5a774522 217 i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
195d1361 218
423e705d 219 /* We are guaranteed to be called with CHARPOS either in i,
6a140a74 220 or further off. */
195d1361
RS
221 if (NULL_INTERVAL_P (i))
222 error ("Error in syntax_table logic for to-the-end intervals");
6a140a74 223 else if (charpos < i->position) /* Move left. */
195d1361
RS
224 {
225 if (count > 0)
f902a008 226 error ("Error in syntax_table logic for intervals <-");
195d1361 227 /* Update the interval. */
6a140a74 228 i = update_interval (i, charpos);
bb0de084 229 if (INTERVAL_LAST_POS (i) != gl_state.b_property)
195d1361
RS
230 {
231 invalidate = 0;
195d1361 232 gl_state.forward_i = i;
ee0cdb48 233 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
195d1361 234 }
423e705d 235 }
6a140a74 236 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
195d1361
RS
237 {
238 if (count < 0)
f902a008 239 error ("Error in syntax_table logic for intervals ->");
195d1361 240 /* Update the interval. */
6a140a74 241 i = update_interval (i, charpos);
bb0de084 242 if (i->position != gl_state.e_property)
195d1361
RS
243 {
244 invalidate = 0;
195d1361 245 gl_state.backward_i = i;
bb0de084 246 gl_state.b_property = i->position - gl_state.offset;
195d1361
RS
247 }
248 }
195d1361
RS
249
250 update:
251 tmp_table = textget (i->plist, Qsyntax_table);
252
253 if (invalidate)
254 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
7d0393cf 255
423e705d
SM
256 if (invalidate) /* Did not get to adjacent interval. */
257 { /* with the same table => */
258 /* invalidate the old range. */
195d1361
RS
259 if (count > 0)
260 {
261 gl_state.backward_i = i;
bb0de084
SM
262 gl_state.b_property = i->position - gl_state.offset;
263 }
264 else
195d1361 265 {
bb0de084 266 gl_state.forward_i = i;
ee0cdb48 267 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
195d1361
RS
268 }
269 }
37bef230 270
bb0de084 271 if (!EQ (tmp_table, gl_state.old_prop))
195d1361 272 {
bb0de084
SM
273 gl_state.current_syntax_table = tmp_table;
274 gl_state.old_prop = tmp_table;
275 if (EQ (Fsyntax_table_p (tmp_table), Qt))
276 {
277 gl_state.use_global = 0;
7d0393cf 278 }
bb0de084
SM
279 else if (CONSP (tmp_table))
280 {
281 gl_state.use_global = 1;
282 gl_state.global_code = tmp_table;
283 }
7d0393cf 284 else
bb0de084
SM
285 {
286 gl_state.use_global = 0;
4b4deea2 287 gl_state.current_syntax_table = BVAR (current_buffer, syntax_table);
bb0de084 288 }
195d1361
RS
289 }
290
291 while (!NULL_INTERVAL_P (i))
292 {
293 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
294 {
295 if (count > 0)
bb0de084
SM
296 {
297 gl_state.e_property = i->position - gl_state.offset;
298 gl_state.forward_i = i;
299 }
300 else
301 {
5a774522
SM
302 gl_state.b_property
303 = i->position + LENGTH (i) - gl_state.offset;
bb0de084
SM
304 gl_state.backward_i = i;
305 }
306 return;
195d1361 307 }
7d0393cf 308 else if (cnt == INTERVALS_AT_ONCE)
195d1361
RS
309 {
310 if (count > 0)
bb0de084 311 {
5a774522
SM
312 gl_state.e_property
313 = i->position + LENGTH (i) - gl_state.offset
314 /* e_property at EOB is not set to ZV but to ZV+1, so that
315 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
316 having to check eob between the two. */
317 + (NULL_INTERVAL_P (next_interval (i)) ? 1 : 0);
bb0de084
SM
318 gl_state.forward_i = i;
319 }
320 else
321 {
322 gl_state.b_property = i->position - gl_state.offset;
323 gl_state.backward_i = i;
324 }
325 return;
195d1361
RS
326 }
327 cnt++;
328 i = count > 0 ? next_interval (i) : previous_interval (i);
329 }
bb0de084
SM
330 eassert (NULL_INTERVAL_P (i)); /* This property goes to the end. */
331 if (count > 0)
332 gl_state.e_property = gl_state.stop;
333 else
334 gl_state.b_property = gl_state.start;
195d1361
RS
335}
336\f
6a140a74
RS
337/* Returns TRUE if char at CHARPOS is quoted.
338 Global syntax-table data should be set up already to be good at CHARPOS
339 or after. On return global syntax data is good for lookup at CHARPOS. */
195d1361
RS
340
341static int
6449674e 342char_quoted (EMACS_INT charpos, EMACS_INT bytepos)
195d1361
RS
343{
344 register enum syntaxcode code;
6449674e 345 register EMACS_INT beg = BEGV;
195d1361 346 register int quoted = 0;
6449674e 347 EMACS_INT orig = charpos;
6a140a74 348
02d8b017 349 while (charpos > beg)
195d1361 350 {
ab229fdd 351 int c;
02d8b017 352 DEC_BOTH (charpos, bytepos);
ab229fdd 353
6a140a74 354 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
327719ee 355 c = FETCH_CHAR_AS_MULTIBYTE (bytepos);
ab229fdd 356 code = SYNTAX (c);
6a140a74
RS
357 if (! (code == Scharquote || code == Sescape))
358 break;
359
6a140a74 360 quoted = !quoted;
195d1361 361 }
6a140a74
RS
362
363 UPDATE_SYNTAX_TABLE (orig);
195d1361
RS
364 return quoted;
365}
6a140a74 366
6a140a74
RS
367/* Return the bytepos one character before BYTEPOS.
368 We assume that BYTEPOS is not at the start of the buffer. */
369
55d4c1b2 370static inline EMACS_INT
971de7fb 371dec_bytepos (EMACS_INT bytepos)
6a140a74 372{
4b4deea2 373 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
ef316cf0
RS
374 return bytepos - 1;
375
6a140a74
RS
376 DEC_POS (bytepos);
377 return bytepos;
378}
195d1361 379\f
2b34df4e 380/* Return a defun-start position before POS and not too far before.
7cc80f0a
RS
381 It should be the last one before POS, or nearly the last.
382
383 When open_paren_in_column_0_is_defun_start is nonzero,
1fd1cc2f 384 only the beginning of the buffer is treated as a defun-start.
7cc80f0a
RS
385
386 We record the information about where the scan started
387 and what its result was, so that another call in the same area
388 can return the same value very quickly.
195d1361
RS
389
390 There is no promise at which position the global syntax data is
391 valid on return from the subroutine, so the caller should explicitly
392 update the global data. */
37bef230 393
6449674e 394static EMACS_INT
971de7fb 395find_defun_start (EMACS_INT pos, EMACS_INT pos_byte)
37bef230 396{
8e2911c2 397 EMACS_INT opoint = PT, opoint_byte = PT_BYTE;
37bef230 398
1fd1cc2f
RS
399 if (!open_paren_in_column_0_is_defun_start)
400 {
6e20a0d4 401 find_start_value = BEGV;
1fd1cc2f 402 find_start_value_byte = BEGV_BYTE;
6e20a0d4
CY
403 find_start_buffer = current_buffer;
404 find_start_modiff = MODIFF;
405 find_start_begv = BEGV;
406 find_start_pos = pos;
1fd1cc2f
RS
407 return BEGV;
408 }
409
37bef230
RS
410 /* Use previous finding, if it's valid and applies to this inquiry. */
411 if (current_buffer == find_start_buffer
412 /* Reuse the defun-start even if POS is a little farther on.
413 POS might be in the next defun, but that's ok.
414 Our value may not be the best possible, but will still be usable. */
415 && pos <= find_start_pos + 1000
416 && pos >= find_start_value
417 && BEGV == find_start_begv
418 && MODIFF == find_start_modiff)
419 return find_start_value;
420
421 /* Back up to start of line. */
6a140a74 422 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
37bef230 423
195d1361
RS
424 /* We optimize syntax-table lookup for rare updates. Thus we accept
425 only those `^\s(' which are good in global _and_ text-property
426 syntax-tables. */
d48cd3f4 427 SETUP_BUFFER_SYNTAX_TABLE ();
1fd1cc2f 428 while (PT > BEGV)
37bef230 429 {
ab229fdd
AS
430 int c;
431
1fd1cc2f
RS
432 /* Open-paren at start of line means we may have found our
433 defun-start. */
327719ee 434 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
ab229fdd 435 if (SYNTAX (c) == Sopen)
195d1361 436 {
1fd1cc2f 437 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
327719ee 438 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
ab229fdd 439 if (SYNTAX (c) == Sopen)
1fd1cc2f
RS
440 break;
441 /* Now fallback to the default value. */
d48cd3f4 442 SETUP_BUFFER_SYNTAX_TABLE ();
195d1361 443 }
1fd1cc2f
RS
444 /* Move to beg of previous line. */
445 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
37bef230
RS
446 }
447
448 /* Record what we found, for the next try. */
6a140a74
RS
449 find_start_value = PT;
450 find_start_value_byte = PT_BYTE;
37bef230
RS
451 find_start_buffer = current_buffer;
452 find_start_modiff = MODIFF;
453 find_start_begv = BEGV;
454 find_start_pos = pos;
455
6a140a74
RS
456 TEMP_SET_PT_BOTH (opoint, opoint_byte);
457
37bef230
RS
458 return find_start_value;
459}
460\f
f902a008
RS
461/* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
462
463static int
4f3a2f8d 464prev_char_comend_first (EMACS_INT pos, EMACS_INT pos_byte)
f902a008
RS
465{
466 int c, val;
467
468 DEC_BOTH (pos, pos_byte);
469 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
470 c = FETCH_CHAR (pos_byte);
471 val = SYNTAX_COMEND_FIRST (c);
472 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
473 return val;
474}
475
476/* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
477
3f679f55
SM
478/* static int
479 * prev_char_comstart_first (pos, pos_byte)
480 * int pos, pos_byte;
481 * {
482 * int c, val;
7d0393cf 483 *
3f679f55
SM
484 * DEC_BOTH (pos, pos_byte);
485 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
486 * c = FETCH_CHAR (pos_byte);
487 * val = SYNTAX_COMSTART_FIRST (c);
488 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
489 * return val;
490 * } */
f902a008 491
6a140a74
RS
492/* Checks whether charpos FROM is at the end of a comment.
493 FROM_BYTE is the bytepos corresponding to FROM.
494 Do not move back before STOP.
495
496 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
497 return -1 otherwise.
498
499 If successful, store the charpos of the comment's beginning
500 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
527a32d9
KH
501
502 Global syntax data remains valid for backward search starting at
503 the returned value (or at FROM, if the search was not successful). */
195d1361
RS
504
505static int
971de7fb 506back_comment (EMACS_INT from, EMACS_INT from_byte, EMACS_INT stop, int comnested, int comstyle, EMACS_INT *charpos_ptr, EMACS_INT *bytepos_ptr)
195d1361
RS
507{
508 /* Look back, counting the parity of string-quotes,
509 and recording the comment-starters seen.
510 When we reach a safe place, assume that's not in a string;
511 then step the main scan to the earliest comment-starter seen
512 an even number of string quotes away from the safe place.
513
514 OFROM[I] is position of the earliest comment-starter seen
515 which is I+2X quotes from the comment-end.
516 PARITY is current parity of quotes from the comment end. */
3ee5041c 517 int string_style = -1; /* Presumed outside of any string. */
195d1361 518 int string_lossage = 0;
f7c436c1 519 /* Not a real lossage: indicates that we have passed a matching comment
7d0393cf 520 starter plus a non-matching comment-ender, meaning that any matching
f7c436c1
SM
521 comment-starter we might see later could be a false positive (hidden
522 inside another comment).
523 Test case: { a (* b } c (* d *) */
524 int comment_lossage = 0;
8e2911c2
AS
525 EMACS_INT comment_end = from;
526 EMACS_INT comment_end_byte = from_byte;
527 EMACS_INT comstart_pos = 0;
4f63c6bb 528 EMACS_INT comstart_byte IF_LINT (= 0);
eb35b628
RS
529 /* Place where the containing defun starts,
530 or 0 if we didn't come across it yet. */
8e2911c2
AS
531 EMACS_INT defun_start = 0;
532 EMACS_INT defun_start_byte = 0;
195d1361 533 register enum syntaxcode code;
95ff8dfc 534 int nesting = 1; /* current comment nesting */
ea315ed6 535 int c;
3f679f55
SM
536 int syntax = 0;
537
538 /* FIXME: A }} comment-ender style leads to incorrect behavior
539 in the case of {{ c }}} because we ignore the last two chars which are
540 assumed to be comment-enders although they aren't. */
195d1361
RS
541
542 /* At beginning of range to scan, we're outside of strings;
543 that determines quote parity to the comment-end. */
544 while (from != stop)
545 {
4f3a2f8d
EZ
546 EMACS_INT temp_byte;
547 int prev_syntax, com2start, com2end;
fbb3da77 548 int comstart;
6a140a74 549
195d1361 550 /* Move back and examine a character. */
6a140a74 551 DEC_BOTH (from, from_byte);
195d1361
RS
552 UPDATE_SYNTAX_TABLE_BACKWARD (from);
553
3f679f55 554 prev_syntax = syntax;
b7dbcc19 555 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3f679f55 556 syntax = SYNTAX_WITH_FLAGS (c);
195d1361
RS
557 code = SYNTAX (c);
558
3f679f55
SM
559 /* Check for 2-char comment markers. */
560 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
561 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
c5683ceb
SM
562 && (comstyle
563 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax, syntax))
3f679f55
SM
564 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
565 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
566 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
567 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
fbb3da77 568 comstart = (com2start || code == Scomment);
01f44d5a 569
3f679f55
SM
570 /* Nasty cases with overlapping 2-char comment markers:
571 - snmp-mode: -- c -- foo -- c --
572 --- c --
573 ------ c --
574 - c-mode: *||*
575 |* *|* *|
576 |*| |* |*|
577 /// */
578
579 /* If a 2-char comment sequence partly overlaps with another,
fbb3da77
SM
580 we don't try to be clever. E.g. |*| in C, or }% in modes that
581 have %..\n and %{..}%. */
582 if (from > stop && (com2end || comstart))
195d1361 583 {
4f3a2f8d
EZ
584 EMACS_INT next = from, next_byte = from_byte;
585 int next_c, next_syntax;
3f679f55
SM
586 DEC_BOTH (next, next_byte);
587 UPDATE_SYNTAX_TABLE_BACKWARD (next);
b7dbcc19 588 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
3f679f55 589 next_syntax = SYNTAX_WITH_FLAGS (next_c);
fbb3da77 590 if (((comstart || comnested)
3f679f55
SM
591 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
592 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
593 || ((com2end || comnested)
594 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
c5683ceb
SM
595 && (comstyle
596 == SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_syntax))
3f679f55
SM
597 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
598 goto lossage;
599 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
f902a008 600 }
3f679f55
SM
601
602 if (com2start && comstart_pos == 0)
603 /* We're looking at a comment starter. But it might be a comment
604 ender as well (see snmp-mode). The first time we see one, we
605 need to consider it as a comment starter,
606 and the subsequent times as a comment ender. */
607 com2end = 0;
608
609 /* Turn a 2-char comment sequences into the appropriate syntax. */
610 if (com2end)
611 code = Sendcomment;
612 else if (com2start)
613 code = Scomment;
614 /* Ignore comment starters of a different style. */
615 else if (code == Scomment
c5683ceb 616 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0)
3f679f55 617 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
1aa963c8 618 continue;
195d1361 619
9828a477
KH
620 /* Ignore escaped characters, except comment-enders. */
621 if (code != Sendcomment && char_quoted (from, from_byte))
195d1361
RS
622 continue;
623
3ee5041c 624 switch (code)
195d1361 625 {
3ee5041c
SM
626 case Sstring_fence:
627 case Scomment_fence:
628 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
629 case Sstring:
630 /* Track parity of quotes. */
631 if (string_style == -1)
632 /* Entering a string. */
633 string_style = c;
634 else if (string_style == c)
635 /* Leaving the string. */
636 string_style = -1;
637 else
638 /* If we have two kinds of string delimiters.
639 There's no way to grok this scanning backwards. */
195d1361 640 string_lossage = 1;
3ee5041c 641 break;
7d0393cf 642
3ee5041c
SM
643 case Scomment:
644 /* We've already checked that it is the relevant comstyle. */
f7c436c1 645 if (string_style != -1 || comment_lossage || string_lossage)
3ee5041c
SM
646 /* There are odd string quotes involved, so let's be careful.
647 Test case in Pascal: " { " a { " } */
648 goto lossage;
649
f7c436c1
SM
650 if (!comnested)
651 {
652 /* Record best comment-starter so far. */
653 comstart_pos = from;
654 comstart_byte = from_byte;
655 }
656 else if (--nesting <= 0)
95ff8dfc
RS
657 /* nested comments have to be balanced, so we don't need to
658 keep looking for earlier ones. We use here the same (slightly
659 incorrect) reasoning as below: since it is followed by uniform
660 paired string quotes, this comment-start has to be outside of
661 strings, else the comment-end itself would be inside a string. */
662 goto done;
3ee5041c
SM
663 break;
664
02010917 665 case Sendcomment:
c5683ceb 666 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == comstyle
d070eb22 667 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
3f679f55 668 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
02010917
SM
669 /* This is the same style of comment ender as ours. */
670 {
671 if (comnested)
672 nesting++;
673 else
674 /* Anything before that can't count because it would match
675 this comment-ender rather than ours. */
676 from = stop; /* Break out of the loop. */
677 }
f7c436c1
SM
678 else if (comstart_pos != 0 || c != '\n')
679 /* We're mixing comment styles here, so we'd better be careful.
680 The (comstart_pos != 0 || c != '\n') check is not quite correct
681 (we should just always set comment_lossage), but removing it
682 would imply that any multiline comment in C would go through
683 lossage, which seems overkill.
684 The failure should only happen in the rare cases such as
685 { (* } *) */
686 comment_lossage = 1;
02010917 687 break;
195d1361 688
02010917
SM
689 case Sopen:
690 /* Assume a defun-start point is outside of strings. */
691 if (open_paren_in_column_0_is_defun_start
692 && (from == stop
693 || (temp_byte = dec_bytepos (from_byte),
694 FETCH_CHAR (temp_byte) == '\n')))
695 {
696 defun_start = from;
697 defun_start_byte = from_byte;
698 from = stop; /* Break out of the loop. */
699 }
eb35b628 700 break;
02010917
SM
701
702 default:
f7c436c1 703 break;
eb35b628 704 }
195d1361
RS
705 }
706
707 if (comstart_pos == 0)
708 {
709 from = comment_end;
6a140a74 710 from_byte = comment_end_byte;
195d1361
RS
711 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
712 }
f7c436c1
SM
713 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
714 or `done'), then we've found the beginning of the non-nested comment. */
715 else if (1) /* !comnested */
195d1361
RS
716 {
717 from = comstart_pos;
6a140a74 718 from_byte = comstart_byte;
789f3320 719 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
195d1361
RS
720 }
721 else
722 {
3ee5041c
SM
723 struct lisp_parse_state state;
724 lossage:
195d1361
RS
725 /* We had two kinds of string delimiters mixed up
726 together. Decode this going forwards.
02010917 727 Scan fwd from a known safe place (beginning-of-defun)
195d1361
RS
728 to the one in question; this records where we
729 last passed a comment starter. */
eb35b628
RS
730 /* If we did not already find the defun start, find it now. */
731 if (defun_start == 0)
732 {
733 defun_start = find_defun_start (comment_end, comment_end_byte);
734 defun_start_byte = find_start_value_byte;
735 }
02010917 736 do
195d1361 737 {
02010917
SM
738 scan_sexps_forward (&state,
739 defun_start, defun_start_byte,
740 comment_end, -10000, 0, Qnil, 0);
741 defun_start = comment_end;
742 if (state.incomment == (comnested ? 1 : -1)
743 && state.comstyle == comstyle)
744 from = state.comstr_start;
745 else
746 {
747 from = comment_end;
748 if (state.incomment)
749 /* If comment_end is inside some other comment, maybe ours
750 is nested, so we need to try again from within the
751 surrounding comment. Example: { a (* " *) */
752 {
753 /* FIXME: We should advance by one or two chars. */
754 defun_start = state.comstr_start + 2;
755 defun_start_byte = CHAR_TO_BYTE (defun_start);
756 }
757 }
758 } while (defun_start < comment_end);
759
6a140a74 760 from_byte = CHAR_TO_BYTE (from);
195d1361
RS
761 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
762 }
7d0393cf 763
95ff8dfc 764 done:
6a140a74
RS
765 *charpos_ptr = from;
766 *bytepos_ptr = from_byte;
767
1aa963c8 768 return (from == comment_end) ? -1 : from;
195d1361
RS
769}
770\f
8489eb67 771DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
fdb82f93
PJ
772 doc: /* Return t if OBJECT is a syntax table.
773Currently, any char-table counts as a syntax table. */)
5842a27b 774 (Lisp_Object object)
8489eb67 775{
2203e1e8 776 if (CHAR_TABLE_P (object)
e704cb4b 777 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
8489eb67
RS
778 return Qt;
779 return Qnil;
780}
781
8ea151b2 782static void
971de7fb 783check_syntax_table (Lisp_Object obj)
8489eb67 784{
47f5f6ae
KS
785 CHECK_TYPE (CHAR_TABLE_P (obj) && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table),
786 Qsyntax_table_p, obj);
7d0393cf 787}
8489eb67 788
8489eb67 789DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
fdb82f93
PJ
790 doc: /* Return the current syntax table.
791This is the one specified by the current buffer. */)
5842a27b 792 (void)
8489eb67 793{
4b4deea2 794 return BVAR (current_buffer, syntax_table);
8489eb67
RS
795}
796
797DEFUN ("standard-syntax-table", Fstandard_syntax_table,
798 Sstandard_syntax_table, 0, 0, 0,
fdb82f93
PJ
799 doc: /* Return the standard syntax table.
800This is the one used for new buffers. */)
5842a27b 801 (void)
8489eb67
RS
802{
803 return Vstandard_syntax_table;
804}
805
806DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
fdb82f93
PJ
807 doc: /* Construct a new syntax table and return it.
808It is a copy of the TABLE, which defaults to the standard syntax table. */)
5842a27b 809 (Lisp_Object table)
8489eb67 810{
8ea151b2
RS
811 Lisp_Object copy;
812
265a9e55 813 if (!NILP (table))
8ea151b2
RS
814 check_syntax_table (table);
815 else
816 table = Vstandard_syntax_table;
817
818 copy = Fcopy_sequence (table);
0f867324
RS
819
820 /* Only the standard syntax table should have a default element.
821 Other syntax tables should inherit from parents instead. */
822 XCHAR_TABLE (copy)->defalt = Qnil;
823
824 /* Copied syntax tables should all have parents.
825 If we copied one with no parent, such as the standard syntax table,
826 use the standard syntax table as the copy's parent. */
827 if (NILP (XCHAR_TABLE (copy)->parent))
828 Fset_char_table_parent (copy, Vstandard_syntax_table);
8ea151b2 829 return copy;
8489eb67
RS
830}
831
832DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
fdb82f93
PJ
833 doc: /* Select a new syntax table for the current buffer.
834One argument, a syntax table. */)
5842a27b 835 (Lisp_Object table)
8489eb67 836{
4e4dfa78 837 int idx;
8ea151b2 838 check_syntax_table (table);
4b4deea2 839 BVAR (current_buffer, syntax_table) = table;
8489eb67 840 /* Indicate that this buffer now has a specified syntax table. */
f6cd0527
GM
841 idx = PER_BUFFER_VAR_IDX (syntax_table);
842 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
8489eb67
RS
843 return table;
844}
845\f
846/* Convert a letter which signifies a syntax code
847 into the code it signifies.
195d1361 848 This is used by modify-syntax-entry, and other things. */
8489eb67
RS
849
850unsigned char syntax_spec_code[0400] =
851 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
852 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
853 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
854 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
195d1361 855 (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377,
8489eb67
RS
856 (char) Smath, 0377, 0377, (char) Squote,
857 (char) Sopen, (char) Sclose, 0377, 0377,
858 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
859 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
860 0377, 0377, 0377, 0377,
861 (char) Scomment, 0377, (char) Sendcomment, 0377,
6cb71bf6 862 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
8489eb67
RS
863 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
864 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
865 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
866 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
867 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
868 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
195d1361 869 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377
8489eb67
RS
870 };
871
195d1361 872/* Indexed by syntax code, give the letter that describes it. */
8489eb67 873
195d1361 874char syntax_code_spec[16] =
8489eb67 875 {
195d1361
RS
876 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
877 '!', '|'
8489eb67 878 };
93da5fff
KH
879
880/* Indexed by syntax code, give the object (cons of syntax code and
881 nil) to be stored in syntax table. Since these objects can be
882 shared among syntax tables, we generate them in advance. By
883 sharing objects, the function `describe-syntax' can give a more
884 compact listing. */
885static Lisp_Object Vsyntax_code_object;
886
8489eb67
RS
887\f
888DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
fdb82f93 889 doc: /* Return the syntax code of CHARACTER, described by a character.
e2b6daf4
CY
890For example, if CHARACTER is a word constituent, the
891character `w' (119) is returned.
fdb82f93
PJ
892The characters that correspond to various syntax codes
893are listed in the documentation of `modify-syntax-entry'. */)
5842a27b 894 (Lisp_Object character)
8489eb67 895{
8ea151b2 896 int char_int;
774b9a60 897 CHECK_CHARACTER (character);
2203e1e8 898 char_int = XINT (character);
d48cd3f4 899 SETUP_BUFFER_SYNTAX_TABLE ();
8ea151b2 900 return make_number (syntax_code_spec[(int) SYNTAX (char_int)]);
beefa22e
RS
901}
902
903DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
fdb82f93 904 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
5842a27b 905 (Lisp_Object character)
beefa22e 906{
8ea151b2 907 int char_int, code;
b7826503 908 CHECK_NUMBER (character);
2203e1e8 909 char_int = XINT (character);
d48cd3f4 910 SETUP_BUFFER_SYNTAX_TABLE ();
8ea151b2 911 code = SYNTAX (char_int);
a8bd7cd8 912 if (code == Sopen || code == Sclose)
2e34157c 913 return SYNTAX_MATCH (char_int);
beefa22e 914 return Qnil;
8489eb67
RS
915}
916
c65adb44 917DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
fdb82f93
PJ
918 doc: /* Convert a syntax specification STRING into syntax cell form.
919STRING should be a string as it is allowed as argument of
920`modify-syntax-entry'. Value is the equivalent cons cell
bce3dc36 921\(CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
fdb82f93 922text property. */)
5842a27b 923 (Lisp_Object string)
8489eb67 924{
2e567bd3 925 register const unsigned char *p;
8489eb67 926 register enum syntaxcode code;
247e20a8 927 int val;
8ea151b2 928 Lisp_Object match;
8489eb67 929
b7826503 930 CHECK_STRING (string);
8489eb67 931
d5db4077 932 p = SDATA (string);
8489eb67
RS
933 code = (enum syntaxcode) syntax_spec_code[*p++];
934 if (((int) code & 0377) == 0377)
b764a653 935 error ("Invalid syntax description letter: %c", p[-1]);
8489eb67 936
8ea151b2 937 if (code == Sinherit)
c65adb44 938 return Qnil;
8ea151b2
RS
939
940 if (*p)
d1be9f0f 941 {
93da5fff 942 int len;
62a6e103 943 int character = STRING_CHAR_AND_LENGTH (p, len);
93da5fff 944 XSETINT (match, character);
d1be9f0f
RS
945 if (XFASTINT (match) == ' ')
946 match = Qnil;
93da5fff 947 p += len;
d1be9f0f
RS
948 }
949 else
8ea151b2 950 match = Qnil;
8489eb67 951
8ea151b2 952 val = (int) code;
8489eb67
RS
953 while (*p)
954 switch (*p++)
955 {
956 case '1':
247e20a8 957 val |= 1 << 16;
8489eb67
RS
958 break;
959
960 case '2':
247e20a8 961 val |= 1 << 17;
8489eb67
RS
962 break;
963
964 case '3':
247e20a8 965 val |= 1 << 18;
8489eb67
RS
966 break;
967
968 case '4':
247e20a8 969 val |= 1 << 19;
8489eb67
RS
970 break;
971
972 case 'p':
247e20a8 973 val |= 1 << 20;
8489eb67 974 break;
e5d4f4dc
RS
975
976 case 'b':
247e20a8 977 val |= 1 << 21;
e5d4f4dc 978 break;
95ff8dfc
RS
979
980 case 'n':
981 val |= 1 << 22;
982 break;
c5683ceb
SM
983
984 case 'c':
985 val |= 1 << 23;
986 break;
8489eb67 987 }
7d0393cf 988
77b37c05 989 if (val < ASIZE (Vsyntax_code_object) && NILP (match))
c65adb44 990 return XVECTOR (Vsyntax_code_object)->contents[val];
93da5fff
KH
991 else
992 /* Since we can't use a shared object, let's make a new one. */
c65adb44
SM
993 return Fcons (make_number (val), match);
994}
995
fdb82f93 996/* I really don't know why this is interactive
2c9e1900 997 help-form should at least be made useful whilst reading the second arg. */
7d0393cf 998DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
c65adb44 999 "cSet syntax for character: \nsSet syntax for %s to: ",
a50a10a0 1000 doc: /* Set syntax for character CHAR according to string NEWENTRY.
d7ee9fab 1001The syntax is changed only for table SYNTAX-TABLE, which defaults to
fdb82f93 1002 the current buffer's syntax table.
5c7b02ab 1003CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
fedc6ab5 1004in the range MIN to MAX are changed.
fdb82f93
PJ
1005The first character of NEWENTRY should be one of the following:
1006 Space or - whitespace syntax. w word constituent.
1007 _ symbol constituent. . punctuation.
1008 ( open-parenthesis. ) close-parenthesis.
1009 " string quote. \\ escape.
1010 $ paired delimiter. ' expression quote or prefix operator.
1011 < comment starter. > comment ender.
1012 / character-quote. @ inherit from `standard-syntax-table'.
1013 | generic string fence. ! generic comment fence.
1014
1015Only single-character comment start and end sequences are represented thus.
1016Two-character sequences are represented as described below.
1017The second character of NEWENTRY is the matching parenthesis,
1018 used only if the first character is `(' or `)'.
1019Any additional characters are flags.
1020Defined flags are the characters 1, 2, 3, 4, b, p, and n.
a50a10a0
PJ
1021 1 means CHAR is the start of a two-char comment start sequence.
1022 2 means CHAR is the second character of such a sequence.
1023 3 means CHAR is the start of a two-char comment end sequence.
1024 4 means CHAR is the second character of such a sequence.
fdb82f93 1025
c5683ceb 1026There can be several orthogonal comment sequences. This is to support
fdb82f93
PJ
1027language modes such as C++. By default, all comment sequences are of style
1028a, but you can set the comment sequence style to b (on the second character
c5683ceb
SM
1029of a comment-start, and the first character of a comment-end sequence) and/or
1030c (on any of its chars) using this flag:
a50a10a0 1031 b means CHAR is part of comment sequence b.
c5683ceb 1032 c means CHAR is part of comment sequence c.
a50a10a0 1033 n means CHAR is part of a nestable comment sequence.
fdb82f93 1034
a50a10a0 1035 p means CHAR is a prefix character for `backward-prefix-chars';
fdb82f93 1036 such characters are treated as whitespace when they occur
a50a10a0 1037 between expressions.
fedc6ab5 1038usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
5842a27b 1039 (Lisp_Object c, Lisp_Object newentry, Lisp_Object syntax_table)
c65adb44 1040{
5c7b02ab
KH
1041 if (CONSP (c))
1042 {
8f924df7
KH
1043 CHECK_CHARACTER_CAR (c);
1044 CHECK_CHARACTER_CDR (c);
5c7b02ab
KH
1045 }
1046 else
1047 CHECK_CHARACTER (c);
c65adb44
SM
1048
1049 if (NILP (syntax_table))
4b4deea2 1050 syntax_table = BVAR (current_buffer, syntax_table);
c65adb44
SM
1051 else
1052 check_syntax_table (syntax_table);
8489eb67 1053
5c7b02ab
KH
1054 newentry = Fstring_to_syntax (newentry);
1055 if (CONSP (c))
1056 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1057 else
1058 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
e5b94d44
CY
1059
1060 /* We clear the regexp cache, since character classes can now have
1061 different values from those in the compiled regexps.*/
1062 clear_regexp_cache ();
1063
8489eb67
RS
1064 return Qnil;
1065}
1066\f
1067/* Dump syntax table to buffer in human-readable format */
1068
62abe9cb
SM
1069DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1070 Sinternal_describe_syntax_value, 1, 1, 0,
1071 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
5842a27b 1072 (Lisp_Object syntax)
8489eb67
RS
1073{
1074 register enum syntaxcode code;
c5683ceb
SM
1075 int syntax_code;
1076 char desc, start1, start2, end1, end2, prefix,
1077 comstyleb, comstylec, comnested;
8489eb67 1078 char str[2];
62abe9cb 1079 Lisp_Object first, match_lisp, value = syntax;
8489eb67 1080
8ea151b2
RS
1081 if (NILP (value))
1082 {
62abe9cb
SM
1083 insert_string ("default");
1084 return syntax;
8ea151b2
RS
1085 }
1086
908b7fea
KH
1087 if (CHAR_TABLE_P (value))
1088 {
62abe9cb
SM
1089 insert_string ("deeper char-table ...");
1090 return syntax;
908b7fea
KH
1091 }
1092
8ea151b2
RS
1093 if (!CONSP (value))
1094 {
62abe9cb
SM
1095 insert_string ("invalid");
1096 return syntax;
8ea151b2
RS
1097 }
1098
c1d497be
KR
1099 first = XCAR (value);
1100 match_lisp = XCDR (value);
8ea151b2
RS
1101
1102 if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp)))
8489eb67 1103 {
62abe9cb
SM
1104 insert_string ("invalid");
1105 return syntax;
8489eb67
RS
1106 }
1107
c5683ceb
SM
1108 syntax_code = XINT (first);
1109 code = (enum syntaxcode) (syntax_code & 0377);
1110 start1 = SYNTAX_FLAGS_COMSTART_FIRST (syntax_code);
1111 start2 = SYNTAX_FLAGS_COMSTART_SECOND (syntax_code);;
1112 end1 = SYNTAX_FLAGS_COMEND_FIRST (syntax_code);
1113 end2 = SYNTAX_FLAGS_COMEND_SECOND (syntax_code);
1114 prefix = SYNTAX_FLAGS_PREFIX (syntax_code);
1115 comstyleb = SYNTAX_FLAGS_COMMENT_STYLEB (syntax_code);
1116 comstylec = SYNTAX_FLAGS_COMMENT_STYLEC (syntax_code);
1117 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax_code);
8489eb67
RS
1118
1119 if ((int) code < 0 || (int) code >= (int) Smax)
1120 {
1121 insert_string ("invalid");
62abe9cb 1122 return syntax;
8489eb67
RS
1123 }
1124 desc = syntax_code_spec[(int) code];
1125
1126 str[0] = desc, str[1] = 0;
1127 insert (str, 1);
1128
93da5fff
KH
1129 if (NILP (match_lisp))
1130 insert (" ", 1);
1131 else
1132 insert_char (XINT (match_lisp));
8489eb67 1133
8489eb67
RS
1134 if (start1)
1135 insert ("1", 1);
1136 if (start2)
1137 insert ("2", 1);
1138
1139 if (end1)
1140 insert ("3", 1);
1141 if (end2)
1142 insert ("4", 1);
1143
1144 if (prefix)
1145 insert ("p", 1);
c5683ceb 1146 if (comstyleb)
e5d4f4dc 1147 insert ("b", 1);
c5683ceb
SM
1148 if (comstylec)
1149 insert ("c", 1);
e6365855
SM
1150 if (comnested)
1151 insert ("n", 1);
8489eb67
RS
1152
1153 insert_string ("\twhich means: ");
1154
0220c518 1155 switch (SWITCH_ENUM_CAST (code))
8489eb67
RS
1156 {
1157 case Swhitespace:
1158 insert_string ("whitespace"); break;
1159 case Spunct:
1160 insert_string ("punctuation"); break;
1161 case Sword:
1162 insert_string ("word"); break;
1163 case Ssymbol:
1164 insert_string ("symbol"); break;
1165 case Sopen:
1166 insert_string ("open"); break;
1167 case Sclose:
1168 insert_string ("close"); break;
1169 case Squote:
d671382e 1170 insert_string ("prefix"); break;
8489eb67
RS
1171 case Sstring:
1172 insert_string ("string"); break;
1173 case Smath:
1174 insert_string ("math"); break;
1175 case Sescape:
1176 insert_string ("escape"); break;
1177 case Scharquote:
1178 insert_string ("charquote"); break;
1179 case Scomment:
1180 insert_string ("comment"); break;
1181 case Sendcomment:
1182 insert_string ("endcomment"); break;
d671382e
SM
1183 case Sinherit:
1184 insert_string ("inherit"); break;
1185 case Scomment_fence:
1186 insert_string ("comment fence"); break;
1187 case Sstring_fence:
1188 insert_string ("string fence"); break;
8489eb67
RS
1189 default:
1190 insert_string ("invalid");
62abe9cb 1191 return syntax;
8489eb67
RS
1192 }
1193
8ea151b2 1194 if (!NILP (match_lisp))
8489eb67
RS
1195 {
1196 insert_string (", matches ");
8ea151b2 1197 insert_char (XINT (match_lisp));
8489eb67
RS
1198 }
1199
1200 if (start1)
1201 insert_string (",\n\t is the first character of a comment-start sequence");
1202 if (start2)
1203 insert_string (",\n\t is the second character of a comment-start sequence");
1204
1205 if (end1)
1206 insert_string (",\n\t is the first character of a comment-end sequence");
1207 if (end2)
1208 insert_string (",\n\t is the second character of a comment-end sequence");
c5683ceb 1209 if (comstyleb)
e5d4f4dc 1210 insert_string (" (comment style b)");
c5683ceb
SM
1211 if (comstylec)
1212 insert_string (" (comment style c)");
e6365855
SM
1213 if (comnested)
1214 insert_string (" (nestable)");
e5d4f4dc 1215
8489eb67
RS
1216 if (prefix)
1217 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1218
62abe9cb
SM
1219 return syntax;
1220}
8489eb67
RS
1221\f
1222/* Return the position across COUNT words from FROM.
1223 If that many words cannot be found before the end of the buffer, return 0.
1224 COUNT negative means scan backward and stop at word beginning. */
1225
4f3a2f8d
EZ
1226EMACS_INT
1227scan_words (register EMACS_INT from, register EMACS_INT count)
8489eb67 1228{
4f3a2f8d
EZ
1229 register EMACS_INT beg = BEGV;
1230 register EMACS_INT end = ZV;
1231 register EMACS_INT from_byte = CHAR_TO_BYTE (from);
93da5fff
KH
1232 register enum syntaxcode code;
1233 int ch0, ch1;
fca8fe46 1234 Lisp_Object func, pos;
8489eb67
RS
1235
1236 immediate_quit = 1;
1237 QUIT;
1238
195d1361
RS
1239 SETUP_SYNTAX_TABLE (from, count);
1240
8489eb67
RS
1241 while (count > 0)
1242 {
1243 while (1)
1244 {
1245 if (from == end)
1246 {
1247 immediate_quit = 0;
1248 return 0;
1249 }
195d1361 1250 UPDATE_SYNTAX_TABLE_FORWARD (from);
b7dbcc19 1251 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
93da5fff 1252 code = SYNTAX (ch0);
6a140a74 1253 INC_BOTH (from, from_byte);
8489eb67
RS
1254 if (words_include_escapes
1255 && (code == Sescape || code == Scharquote))
1256 break;
1257 if (code == Sword)
1258 break;
8489eb67 1259 }
93da5fff
KH
1260 /* Now CH0 is a character which begins a word and FROM is the
1261 position of the next character. */
8f924df7 1262 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
869bb237 1263 if (! NILP (Ffboundp (func)))
8489eb67 1264 {
869bb237 1265 pos = call2 (func, make_number (from - 1), make_number (end));
8f924df7
KH
1266 if (INTEGERP (pos) && XINT (pos) > from)
1267 {
1268 from = XINT (pos);
1269 from_byte = CHAR_TO_BYTE (from);
1270 }
8489eb67 1271 }
869bb237 1272 else
8cb8232a 1273 {
8cb8232a
KH
1274 while (1)
1275 {
1276 if (from == end) break;
1277 UPDATE_SYNTAX_TABLE_FORWARD (from);
b7dbcc19 1278 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
8cb8232a
KH
1279 code = SYNTAX (ch1);
1280 if ((code != Sword
1281 && (! words_include_escapes
1282 || (code != Sescape && code != Scharquote)))
2b70a11c 1283 || word_boundary_p (ch0, ch1))
869bb237 1284 break;
8cb8232a
KH
1285 INC_BOTH (from, from_byte);
1286 ch0 = ch1;
1287 }
8489eb67
RS
1288 }
1289 count--;
1290 }
1291 while (count < 0)
1292 {
1293 while (1)
1294 {
1295 if (from == beg)
1296 {
1297 immediate_quit = 0;
1298 return 0;
1299 }
6a140a74 1300 DEC_BOTH (from, from_byte);
195d1361 1301 UPDATE_SYNTAX_TABLE_BACKWARD (from);
b7dbcc19 1302 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
93da5fff 1303 code = SYNTAX (ch1);
8489eb67
RS
1304 if (words_include_escapes
1305 && (code == Sescape || code == Scharquote))
1306 break;
1307 if (code == Sword)
1308 break;
8489eb67 1309 }
93da5fff
KH
1310 /* Now CH1 is a character which ends a word and FROM is the
1311 position of it. */
8f924df7 1312 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
869bb237 1313 if (! NILP (Ffboundp (func)))
8f924df7 1314 {
869bb237 1315 pos = call2 (func, make_number (from), make_number (beg));
8f924df7
KH
1316 if (INTEGERP (pos) && XINT (pos) < from)
1317 {
1318 from = XINT (pos);
1319 from_byte = CHAR_TO_BYTE (from);
1320 }
8489eb67 1321 }
869bb237 1322 else
8489eb67 1323 {
8cb8232a
KH
1324 while (1)
1325 {
8cb8232a
KH
1326 if (from == beg)
1327 break;
262be72a 1328 DEC_BOTH (from, from_byte);
8cb8232a 1329 UPDATE_SYNTAX_TABLE_BACKWARD (from);
262be72a 1330 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
8cb8232a
KH
1331 code = SYNTAX (ch0);
1332 if ((code != Sword
1333 && (! words_include_escapes
1334 || (code != Sescape && code != Scharquote)))
2b70a11c 1335 || word_boundary_p (ch0, ch1))
262be72a
MB
1336 {
1337 INC_BOTH (from, from_byte);
1338 break;
1339 }
8cb8232a
KH
1340 ch1 = ch0;
1341 }
8489eb67
RS
1342 }
1343 count++;
1344 }
1345
1346 immediate_quit = 0;
1347
1348 return from;
1349}
1350
a7ca3326 1351DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p",
fdb82f93
PJ
1352 doc: /* Move point forward ARG words (backward if ARG is negative).
1353Normally returns t.
1354If an edge of the buffer or a field boundary is reached, point is left there
1355and the function returns nil. Field boundaries are not noticed if
1356`inhibit-field-text-motion' is non-nil. */)
5842a27b 1357 (Lisp_Object arg)
8489eb67 1358{
c096ae4d 1359 Lisp_Object tmp;
2c6ea900 1360 int orig_val, val;
8489eb67 1361
839966f3
KH
1362 if (NILP (arg))
1363 XSETFASTINT (arg, 1);
1364 else
1365 CHECK_NUMBER (arg);
1366
1367 val = orig_val = scan_words (PT, XINT (arg));
2c6ea900 1368 if (! orig_val)
839966f3 1369 val = XINT (arg) > 0 ? ZV : BEGV;
5878ee6f 1370
b8855607 1371 /* Avoid jumping out of an input field. */
c096ae4d
SM
1372 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1373 Qt, Qnil, Qnil);
1374 val = XFASTINT (tmp);
7d0393cf 1375
8489eb67 1376 SET_PT (val);
e6d8341f 1377 return val == orig_val ? Qt : Qnil;
8489eb67
RS
1378}
1379\f
a7ca3326 1380DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
fdb82f93
PJ
1381 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1382STRING is like the inside of a `[...]' in a regular expression
1383except that `]' is never special and `\\' quotes `^', `-' or `\\'
7087d5e9 1384 (but not at the end of a range; quoting is never needed there).
fdb82f93
PJ
1385Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1386With arg "^a-zA-Z", skips nonletters stopping before first letter.
a1bc88d4
RS
1387Char classes, e.g. `[:alpha:]', are supported.
1388
1389Returns the distance traveled, either zero or positive. */)
5842a27b 1390 (Lisp_Object string, Lisp_Object lim)
195d1361 1391{
327719ee 1392 return skip_chars (1, string, lim, 1);
195d1361
RS
1393}
1394
a7ca3326 1395DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
fdb82f93
PJ
1396 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1397See `skip-chars-forward' for details.
1398Returns the distance traveled, either zero or negative. */)
5842a27b 1399 (Lisp_Object string, Lisp_Object lim)
195d1361 1400{
327719ee 1401 return skip_chars (0, string, lim, 1);
195d1361
RS
1402}
1403
1404DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
fdb82f93
PJ
1405 doc: /* Move point forward across chars in specified syntax classes.
1406SYNTAX is a string of syntax code characters.
1407Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1408If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1409This function returns the distance traveled, either zero or positive. */)
5842a27b 1410 (Lisp_Object syntax, Lisp_Object lim)
195d1361 1411{
b7dbcc19 1412 return skip_syntaxes (1, syntax, lim);
195d1361
RS
1413}
1414
1415DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
fdb82f93
PJ
1416 doc: /* Move point backward across chars in specified syntax classes.
1417SYNTAX is a string of syntax code characters.
1418Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1419If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1420This function returns the distance traveled, either zero or negative. */)
5842a27b 1421 (Lisp_Object syntax, Lisp_Object lim)
195d1361 1422{
b7dbcc19 1423 return skip_syntaxes (0, syntax, lim);
195d1361
RS
1424}
1425
6a140a74 1426static Lisp_Object
971de7fb 1427skip_chars (int forwardp, Lisp_Object string, Lisp_Object lim, int handle_iso_classes)
195d1361 1428{
195d1361
RS
1429 register unsigned int c;
1430 unsigned char fastmap[0400];
b7dbcc19 1431 /* Store the ranges of non-ASCII characters. */
4f63c6bb 1432 int *char_ranges IF_LINT (= NULL);
7e68b0ea 1433 int n_char_ranges = 0;
195d1361 1434 int negate = 0;
4f3a2f8d 1435 register EMACS_INT i, i_byte;
b7dbcc19
KH
1436 /* Set to 1 if the current buffer is multibyte and the region
1437 contains non-ASCII chars. */
1438 int multibyte;
1439 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1440 chars. */
1674d9a2 1441 int string_multibyte;
4f3a2f8d 1442 EMACS_INT size_byte;
2e567bd3 1443 const unsigned char *str;
82d497fc 1444 int len;
a1bc88d4 1445 Lisp_Object iso_classes;
195d1361 1446
b7826503 1447 CHECK_STRING (string);
a1bc88d4 1448 iso_classes = Qnil;
82d497fc 1449
195d1361
RS
1450 if (NILP (lim))
1451 XSETINT (lim, forwardp ? ZV : BEGV);
1452 else
b7826503 1453 CHECK_NUMBER_COERCE_MARKER (lim);
195d1361
RS
1454
1455 /* In any case, don't allow scan outside bounds of buffer. */
195d1361
RS
1456 if (XINT (lim) > ZV)
1457 XSETFASTINT (lim, ZV);
1458 if (XINT (lim) < BEGV)
1459 XSETFASTINT (lim, BEGV);
1460
4b4deea2 1461 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
92eaa22e 1462 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
8f924df7 1463 string_multibyte = SBYTES (string) > SCHARS (string);
b7dbcc19 1464
72af86bd 1465 memset (fastmap, 0, sizeof fastmap);
195d1361 1466
8f924df7
KH
1467 str = SDATA (string);
1468 size_byte = SBYTES (string);
4101e6fe 1469
82d497fc 1470 i_byte = 0;
13090112 1471 if (i_byte < size_byte
d5db4077 1472 && SREF (string, 0) == '^')
195d1361 1473 {
82d497fc 1474 negate = 1; i_byte++;
195d1361
RS
1475 }
1476
1477 /* Find the characters specified and set their elements of fastmap.
b7dbcc19 1478 Handle backslashes and ranges specially.
195d1361 1479
b7dbcc19
KH
1480 If STRING contains non-ASCII characters, setup char_ranges for
1481 them and use fastmap only for their leading codes. */
4101e6fe 1482
b7dbcc19 1483 if (! string_multibyte)
195d1361 1484 {
b7dbcc19 1485 int string_has_eight_bit = 0;
4101e6fe 1486
b7dbcc19
KH
1487 /* At first setup fastmap. */
1488 while (i_byte < size_byte)
1489 {
1490 c = str[i_byte++];
1491
a1bc88d4
RS
1492 if (handle_iso_classes && c == '['
1493 && i_byte < size_byte
327719ee 1494 && str[i_byte] == ':')
a1bc88d4
RS
1495 {
1496 const unsigned char *class_beg = str + i_byte + 1;
1497 const unsigned char *class_end = class_beg;
b3bda4fd 1498 const unsigned char *class_limit = str + size_byte - 2;
db9cd97a 1499 /* Leave room for the null. */
a1bc88d4
RS
1500 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1501 re_wctype_t cc;
1502
1503 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1504 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1505
b3bda4fd
KS
1506 while (class_end < class_limit
1507 && *class_end >= 'a' && *class_end <= 'z')
a1bc88d4
RS
1508 class_end++;
1509
b3bda4fd
KS
1510 if (class_end == class_beg
1511 || *class_end != ':' || class_end[1] != ']')
1512 goto not_a_class_name;
a1bc88d4 1513
72af86bd 1514 memcpy (class_name, class_beg, class_end - class_beg);
a1bc88d4
RS
1515 class_name[class_end - class_beg] = 0;
1516
1517 cc = re_wctype (class_name);
1518 if (cc == 0)
1519 error ("Invalid ISO C character class");
1520
1521 iso_classes = Fcons (make_number (cc), iso_classes);
1522
1523 i_byte = class_end + 2 - str;
1524 continue;
1525 }
1526
b3bda4fd 1527 not_a_class_name:
b7dbcc19
KH
1528 if (c == '\\')
1529 {
13090112 1530 if (i_byte == size_byte)
4101e6fe
RS
1531 break;
1532
5c7b02ab 1533 c = str[i_byte++];
195d1361 1534 }
839966f3
KH
1535 /* Treat `-' as range character only if another character
1536 follows. */
1537 if (i_byte + 1 < size_byte
82d497fc 1538 && str[i_byte] == '-')
195d1361 1539 {
9690d026 1540 unsigned int c2;
4101e6fe
RS
1541
1542 /* Skip over the dash. */
82d497fc 1543 i_byte++;
4101e6fe 1544
4101e6fe 1545 /* Get the end of the range. */
5c7b02ab 1546 c2 = str[i_byte++];
b7dbcc19
KH
1547 if (c2 == '\\'
1548 && i_byte < size_byte)
1549 c2 = str[i_byte++];
1550
8f924df7
KH
1551 if (c <= c2)
1552 {
1e69125e
PE
1553 unsigned lim2 = c2 + 1;
1554 while (c < lim2)
8f924df7
KH
1555 fastmap[c++] = 1;
1556 if (! ASCII_CHAR_P (c2))
1557 string_has_eight_bit = 1;
1558 }
195d1361
RS
1559 }
1560 else
b7dbcc19
KH
1561 {
1562 fastmap[c] = 1;
1563 if (! ASCII_CHAR_P (c))
1564 string_has_eight_bit = 1;
1565 }
1566 }
1567
1568 /* If the current range is multibyte and STRING contains
1569 eight-bit chars, arrange fastmap and setup char_ranges for
1570 the corresponding multibyte chars. */
1571 if (multibyte && string_has_eight_bit)
1572 {
1573 unsigned char fastmap2[0400];
1574 int range_start_byte, range_start_char;
1575
72af86bd
AS
1576 memcpy (fastmap + 0200, fastmap2 + 0200, 0200);
1577 memset (fastmap + 0200, 0, 0200);
b7dbcc19
KH
1578 /* We are sure that this loop stops. */
1579 for (i = 0200; ! fastmap2[i]; i++);
4c0354d7 1580 c = BYTE8_TO_CHAR (i);
b7dbcc19
KH
1581 fastmap[CHAR_LEADING_CODE (c)] = 1;
1582 range_start_byte = i;
1583 range_start_char = c;
f003ca54 1584 char_ranges = (int *) alloca (sizeof (int) * 128 * 2);
b7dbcc19
KH
1585 for (i = 129; i < 0400; i++)
1586 {
4c0354d7 1587 c = BYTE8_TO_CHAR (i);
b7dbcc19
KH
1588 fastmap[CHAR_LEADING_CODE (c)] = 1;
1589 if (i - range_start_byte != c - range_start_char)
1590 {
1591 char_ranges[n_char_ranges++] = range_start_char;
1592 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1593 + range_start_char);
1594 range_start_byte = i;
1595 range_start_char = c;
8f924df7 1596 }
b7dbcc19
KH
1597 }
1598 char_ranges[n_char_ranges++] = range_start_char;
1599 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1600 + range_start_char);
195d1361
RS
1601 }
1602 }
f003ca54 1603 else /* STRING is multibyte */
b7dbcc19 1604 {
f003ca54
KH
1605 char_ranges = (int *) alloca (sizeof (int) * SCHARS (string) * 2);
1606
b7dbcc19 1607 while (i_byte < size_byte)
195d1361 1608 {
b7dbcc19 1609 unsigned char leading_code;
5c7b02ab 1610
b7dbcc19 1611 leading_code = str[i_byte];
62a6e103 1612 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
b7dbcc19 1613 i_byte += len;
5c7b02ab 1614
327719ee
MB
1615 if (handle_iso_classes && c == '['
1616 && i_byte < size_byte
62a6e103 1617 && STRING_CHAR (str + i_byte) == ':')
327719ee
MB
1618 {
1619 const unsigned char *class_beg = str + i_byte + 1;
1620 const unsigned char *class_end = class_beg;
1621 const unsigned char *class_limit = str + size_byte - 2;
1622 /* Leave room for the null. */
1623 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1624 re_wctype_t cc;
1625
1626 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1627 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1628
1629 while (class_end < class_limit
1630 && *class_end >= 'a' && *class_end <= 'z')
1631 class_end++;
1632
1633 if (class_end == class_beg
1634 || *class_end != ':' || class_end[1] != ']')
1635 goto not_a_class_name_multibyte;
1636
72af86bd 1637 memcpy (class_name, class_beg, class_end - class_beg);
327719ee
MB
1638 class_name[class_end - class_beg] = 0;
1639
1640 cc = re_wctype (class_name);
1641 if (cc == 0)
1642 error ("Invalid ISO C character class");
1643
1644 iso_classes = Fcons (make_number (cc), iso_classes);
1645
1646 i_byte = class_end + 2 - str;
1647 continue;
1648 }
1649
1650 not_a_class_name_multibyte:
195d1361
RS
1651 if (c == '\\')
1652 {
13090112 1653 if (i_byte == size_byte)
4101e6fe
RS
1654 break;
1655
b7dbcc19 1656 leading_code = str[i_byte];
62a6e103 1657 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
82d497fc 1658 i_byte += len;
195d1361 1659 }
839966f3
KH
1660 /* Treat `-' as range character only if another character
1661 follows. */
1662 if (i_byte + 1 < size_byte
82d497fc 1663 && str[i_byte] == '-')
195d1361 1664 {
9690d026 1665 unsigned int c2;
b7dbcc19 1666 unsigned char leading_code2;
4101e6fe
RS
1667
1668 /* Skip over the dash. */
82d497fc 1669 i_byte++;
4101e6fe 1670
4101e6fe 1671 /* Get the end of the range. */
b7dbcc19 1672 leading_code2 = str[i_byte];
62a6e103 1673 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
82d497fc 1674 i_byte += len;
7e68b0ea 1675
b7dbcc19
KH
1676 if (c2 == '\\'
1677 && i_byte < size_byte)
1678 {
1679 leading_code2 = str[i_byte];
62a6e103 1680 c2 =STRING_CHAR_AND_LENGTH (str + i_byte, len);
b7dbcc19
KH
1681 i_byte += len;
1682 }
1683
f003ca54
KH
1684 if (c > c2)
1685 continue;
b7dbcc19 1686 if (ASCII_CHAR_P (c))
06274af5 1687 {
5c7b02ab
KH
1688 while (c <= c2 && c < 0x80)
1689 fastmap[c++] = 1;
b7dbcc19
KH
1690 leading_code = CHAR_LEADING_CODE (c);
1691 }
1692 if (! ASCII_CHAR_P (c))
1693 {
1e69125e
PE
1694 unsigned lim2 = leading_code2 + 1;
1695 while (leading_code < lim2)
b7dbcc19
KH
1696 fastmap[leading_code++] = 1;
1697 if (c <= c2)
e39091b6 1698 {
b7dbcc19 1699 char_ranges[n_char_ranges++] = c;
e39091b6 1700 char_ranges[n_char_ranges++] = c2;
06274af5
KH
1701 }
1702 }
195d1361
RS
1703 }
1704 else
7e68b0ea 1705 {
b7dbcc19 1706 if (ASCII_CHAR_P (c))
e39091b6
KH
1707 fastmap[c] = 1;
1708 else
7e68b0ea 1709 {
b7dbcc19 1710 fastmap[leading_code] = 1;
4101e6fe
RS
1711 char_ranges[n_char_ranges++] = c;
1712 char_ranges[n_char_ranges++] = c;
7e68b0ea
RS
1713 }
1714 }
195d1361 1715 }
b7dbcc19
KH
1716
1717 /* If the current range is unibyte and STRING contains non-ASCII
1718 chars, arrange fastmap for the corresponding unibyte
1719 chars. */
1720
1721 if (! multibyte && n_char_ranges > 0)
1722 {
72af86bd 1723 memset (fastmap + 0200, 0, 0200);
b7dbcc19
KH
1724 for (i = 0; i < n_char_ranges; i += 2)
1725 {
1726 int c1 = char_ranges[i];
1e69125e 1727 unsigned lim2 = char_ranges[i + 1] + 1;
b7dbcc19 1728
1e69125e 1729 for (; c1 < lim2; c1++)
2afc21f5
SM
1730 {
1731 int b = CHAR_TO_BYTE_SAFE (c1);
1732 if (b >= 0)
1733 fastmap[b] = 1;
1734 }
b7dbcc19
KH
1735 }
1736 }
195d1361
RS
1737 }
1738
9690d026 1739 /* If ^ was the first character, complement the fastmap. */
195d1361 1740 if (negate)
b7dbcc19
KH
1741 {
1742 if (! multibyte)
1743 for (i = 0; i < sizeof fastmap; i++)
1744 fastmap[i] ^= 1;
1745 else
1746 {
1747 for (i = 0; i < 0200; i++)
1748 fastmap[i] ^= 1;
1749 /* All non-ASCII chars possibly match. */
1750 for (; i < sizeof fastmap; i++)
1751 fastmap[i] = 1;
1752 }
1753 }
195d1361
RS
1754
1755 {
4f3a2f8d
EZ
1756 EMACS_INT start_point = PT;
1757 EMACS_INT pos = PT;
1758 EMACS_INT pos_byte = PT_BYTE;
9af7511a
KH
1759 unsigned char *p = PT_ADDR, *endp, *stop;
1760
1761 if (forwardp)
1762 {
1763 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
d7ee9fab 1764 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
9af7511a
KH
1765 }
1766 else
1767 {
1768 endp = CHAR_POS_ADDR (XINT (lim));
d7ee9fab 1769 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
9af7511a 1770 }
195d1361
RS
1771
1772 immediate_quit = 1;
618db430
SM
1773 /* This code may look up syntax tables using macros that rely on the
1774 gl_state object. To make sure this object is not out of date,
1775 let's initialize it manually.
1776 We ignore syntax-table text-properties for now, since that's
1777 what we've done in the past. */
d48cd3f4 1778 SETUP_BUFFER_SYNTAX_TABLE ();
b7dbcc19 1779 if (forwardp)
195d1361 1780 {
b7dbcc19 1781 if (multibyte)
8f924df7 1782 while (1)
b7dbcc19 1783 {
8f924df7 1784 int nbytes;
9af7511a 1785
8f924df7 1786 if (p >= stop)
9af7511a 1787 {
8f924df7 1788 if (p >= endp)
9af7511a 1789 break;
8f924df7
KH
1790 p = GAP_END_ADDR;
1791 stop = endp;
9af7511a 1792 }
62a6e103 1793 c = STRING_CHAR_AND_LENGTH (p, nbytes);
327719ee 1794 if (! NILP (iso_classes) && in_classes (c, iso_classes))
9af7511a 1795 {
327719ee 1796 if (negate)
9af7511a 1797 break;
327719ee
MB
1798 else
1799 goto fwd_ok;
9af7511a 1800 }
9af7511a 1801
8f924df7 1802 if (! fastmap[*p])
b7dbcc19
KH
1803 break;
1804 if (! ASCII_CHAR_P (c))
9af7511a 1805 {
b7dbcc19
KH
1806 /* As we are looking at a multibyte character, we
1807 must look up the character in the table
1808 CHAR_RANGES. If there's no data in the table,
1809 that character is not what we want to skip. */
1810
1811 /* The following code do the right thing even if
1812 n_char_ranges is zero (i.e. no data in
1813 CHAR_RANGES). */
1814 for (i = 0; i < n_char_ranges; i += 2)
1815 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
9af7511a 1816 break;
b7dbcc19
KH
1817 if (!(negate ^ (i < n_char_ranges)))
1818 break;
9af7511a 1819 }
327719ee 1820 fwd_ok:
8f924df7 1821 p += nbytes, pos++, pos_byte += nbytes;
b7dbcc19
KH
1822 }
1823 else
8f924df7
KH
1824 while (1)
1825 {
1826 if (p >= stop)
9af7511a 1827 {
8f924df7 1828 if (p >= endp)
9af7511a 1829 break;
8f924df7
KH
1830 p = GAP_END_ADDR;
1831 stop = endp;
9af7511a 1832 }
327719ee
MB
1833
1834 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1835 {
1836 if (negate)
1837 break;
1838 else
1839 goto fwd_unibyte_ok;
1840 }
1841
8f924df7
KH
1842 if (!fastmap[*p])
1843 break;
327719ee 1844 fwd_unibyte_ok:
8f924df7
KH
1845 p++, pos++, pos_byte++;
1846 }
195d1361
RS
1847 }
1848 else
1849 {
b7dbcc19 1850 if (multibyte)
8f924df7 1851 while (1)
b7dbcc19 1852 {
8f924df7 1853 unsigned char *prev_p;
9af7511a 1854
8f924df7 1855 if (p <= stop)
9af7511a 1856 {
8f924df7 1857 if (p <= endp)
9af7511a 1858 break;
8f924df7
KH
1859 p = GPT_ADDR;
1860 stop = endp;
9af7511a 1861 }
8f924df7
KH
1862 prev_p = p;
1863 while (--p >= stop && ! CHAR_HEAD_P (*p));
62a6e103 1864 c = STRING_CHAR (p);
a1bc88d4 1865
327719ee
MB
1866 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1867 {
1868 if (negate)
1869 break;
9690d026 1870 else
327719ee 1871 goto back_ok;
7e68b0ea 1872 }
a1bc88d4 1873
8f924df7 1874 if (! fastmap[*p])
b7dbcc19
KH
1875 break;
1876 if (! ASCII_CHAR_P (c))
7e68b0ea 1877 {
b7dbcc19
KH
1878 /* See the comment in the previous similar code. */
1879 for (i = 0; i < n_char_ranges; i += 2)
1880 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1881 break;
1882 if (!(negate ^ (i < n_char_ranges)))
1883 break;
7e68b0ea 1884 }
327719ee 1885 back_ok:
8f924df7 1886 pos--, pos_byte -= prev_p - p;
b7dbcc19
KH
1887 }
1888 else
8f924df7
KH
1889 while (1)
1890 {
1891 if (p <= stop)
9af7511a 1892 {
8f924df7 1893 if (p <= endp)
9af7511a 1894 break;
8f924df7
KH
1895 p = GPT_ADDR;
1896 stop = endp;
9af7511a 1897 }
e39091b6 1898
327719ee
MB
1899 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
1900 {
1901 if (negate)
1902 break;
9af7511a 1903 else
327719ee
MB
1904 goto back_unibyte_ok;
1905 }
a1bc88d4 1906
8f924df7
KH
1907 if (!fastmap[p[-1]])
1908 break;
327719ee 1909 back_unibyte_ok:
8f924df7
KH
1910 p--, pos--, pos_byte--;
1911 }
195d1361 1912 }
7e68b0ea 1913
b7dbcc19
KH
1914 SET_PT_BOTH (pos, pos_byte);
1915 immediate_quit = 0;
1916
1917 return make_number (PT - start_point);
1918 }
1919}
7e68b0ea 1920
b7dbcc19
KH
1921
1922static Lisp_Object
971de7fb 1923skip_syntaxes (int forwardp, Lisp_Object string, Lisp_Object lim)
b7dbcc19
KH
1924{
1925 register unsigned int c;
1926 unsigned char fastmap[0400];
1927 int negate = 0;
4f3a2f8d 1928 register EMACS_INT i, i_byte;
b7dbcc19 1929 int multibyte;
4f3a2f8d 1930 EMACS_INT size_byte;
b7dbcc19
KH
1931 unsigned char *str;
1932
1933 CHECK_STRING (string);
1934
1935 if (NILP (lim))
1936 XSETINT (lim, forwardp ? ZV : BEGV);
1937 else
1938 CHECK_NUMBER_COERCE_MARKER (lim);
1939
1940 /* In any case, don't allow scan outside bounds of buffer. */
1941 if (XINT (lim) > ZV)
1942 XSETFASTINT (lim, ZV);
1943 if (XINT (lim) < BEGV)
1944 XSETFASTINT (lim, BEGV);
1945
ade8ee9e 1946 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
49feb1cd 1947 return make_number (0);
8c6e735b 1948
4b4deea2 1949 multibyte = (!NILP (BVAR (current_buffer, enable_multibyte_characters))
92eaa22e 1950 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
b7dbcc19 1951
72af86bd 1952 memset (fastmap, 0, sizeof fastmap);
b7dbcc19 1953
8f924df7
KH
1954 if (SBYTES (string) > SCHARS (string))
1955 /* As this is very rare case (syntax spec is ASCII only), don't
1956 consider efficiency. */
bc796a59
KH
1957 string = string_make_unibyte (string);
1958
8f924df7
KH
1959 str = SDATA (string);
1960 size_byte = SBYTES (string);
bc796a59 1961
b7dbcc19
KH
1962 i_byte = 0;
1963 if (i_byte < size_byte
8f924df7 1964 && SREF (string, 0) == '^')
b7dbcc19
KH
1965 {
1966 negate = 1; i_byte++;
1967 }
1968
b7dbcc19
KH
1969 /* Find the syntaxes specified and set their elements of fastmap. */
1970
1971 while (i_byte < size_byte)
1972 {
1973 c = str[i_byte++];
8f924df7 1974 fastmap[syntax_spec_code[c]] = 1;
b7dbcc19 1975 }
195d1361 1976
9690d026 1977 /* If ^ was the first character, complement the fastmap. */
195d1361
RS
1978 if (negate)
1979 for (i = 0; i < sizeof fastmap; i++)
9690d026 1980 fastmap[i] ^= 1;
195d1361
RS
1981
1982 {
4f3a2f8d
EZ
1983 EMACS_INT start_point = PT;
1984 EMACS_INT pos = PT;
1985 EMACS_INT pos_byte = PT_BYTE;
8f924df7
KH
1986 unsigned char *p = PT_ADDR, *endp, *stop;
1987
1988 if (forwardp)
1989 {
1990 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1991 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1992 }
1993 else
1994 {
1995 endp = CHAR_POS_ADDR (XINT (lim));
1996 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1997 }
195d1361
RS
1998
1999 immediate_quit = 1;
b7dbcc19
KH
2000 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
2001 if (forwardp)
195d1361 2002 {
b7dbcc19 2003 if (multibyte)
195d1361 2004 {
8f924df7 2005 while (1)
8c6e735b 2006 {
8f924df7
KH
2007 int nbytes;
2008
2009 if (p >= stop)
2010 {
2011 if (p >= endp)
2012 break;
2013 p = GAP_END_ADDR;
2014 stop = endp;
2015 }
62a6e103 2016 c = STRING_CHAR_AND_LENGTH (p, nbytes);
8f924df7 2017 if (! fastmap[(int) SYNTAX (c)])
8c6e735b 2018 break;
8f924df7 2019 p += nbytes, pos++, pos_byte += nbytes;
8c6e735b
KH
2020 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2021 }
195d1361
RS
2022 }
2023 else
2024 {
8c6e735b 2025 while (1)
7e68b0ea 2026 {
8f924df7
KH
2027 if (p >= stop)
2028 {
2029 if (p >= endp)
2030 break;
2031 p = GAP_END_ADDR;
2032 stop = endp;
2033 }
2034 if (! fastmap[(int) SYNTAX (*p)])
8c6e735b 2035 break;
8f924df7 2036 p++, pos++, pos_byte++;
b7dbcc19 2037 UPDATE_SYNTAX_TABLE_FORWARD (pos);
195d1361
RS
2038 }
2039 }
2040 }
2041 else
2042 {
b7dbcc19 2043 if (multibyte)
195d1361 2044 {
8c6e735b 2045 while (1)
b7dbcc19 2046 {
8f924df7
KH
2047 unsigned char *prev_p;
2048
2049 if (p <= stop)
b7dbcc19 2050 {
8f924df7
KH
2051 if (p <= endp)
2052 break;
2053 p = GPT_ADDR;
2054 stop = endp;
b7dbcc19 2055 }
262be72a 2056 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
8f924df7
KH
2057 prev_p = p;
2058 while (--p >= stop && ! CHAR_HEAD_P (*p));
62a6e103 2059 c = STRING_CHAR (p);
8f924df7
KH
2060 if (! fastmap[(int) SYNTAX (c)])
2061 break;
2062 pos--, pos_byte -= prev_p - p;
b7dbcc19 2063 }
195d1361
RS
2064 }
2065 else
2066 {
8c6e735b
KH
2067 while (1)
2068 {
8f924df7
KH
2069 if (p <= stop)
2070 {
2071 if (p <= endp)
2072 break;
2073 p = GPT_ADDR;
2074 stop = endp;
2075 }
102f6132 2076 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
8f924df7 2077 if (! fastmap[(int) SYNTAX (p[-1])])
8c6e735b 2078 break;
8f924df7 2079 p--, pos--, pos_byte--;
8c6e735b 2080 }
195d1361
RS
2081 }
2082 }
6a140a74
RS
2083
2084 SET_PT_BOTH (pos, pos_byte);
195d1361
RS
2085 immediate_quit = 0;
2086
2087 return make_number (PT - start_point);
2088 }
2089}
a1bc88d4
RS
2090
2091/* Return 1 if character C belongs to one of the ISO classes
2092 in the list ISO_CLASSES. Each class is represented by an
2093 integer which is its type according to re_wctype. */
2094
2095static int
971de7fb 2096in_classes (int c, Lisp_Object iso_classes)
a1bc88d4
RS
2097{
2098 int fits_class = 0;
2099
618db430 2100 while (CONSP (iso_classes))
a1bc88d4
RS
2101 {
2102 Lisp_Object elt;
2103 elt = XCAR (iso_classes);
2104 iso_classes = XCDR (iso_classes);
2105
2106 if (re_iswctype (c, XFASTINT (elt)))
2107 fits_class = 1;
2108 }
2109
2110 return fits_class;
2111}
195d1361 2112\f
95ff8dfc
RS
2113/* Jump over a comment, assuming we are at the beginning of one.
2114 FROM is the current position.
2115 FROM_BYTE is the bytepos corresponding to FROM.
2116 Do not move past STOP (a charpos).
2117 The comment over which we have to jump is of style STYLE
c5683ceb 2118 (either SYNTAX_FLAGS_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
95ff8dfc
RS
2119 NESTING should be positive to indicate the nesting at the beginning
2120 for nested comments and should be zero or negative else.
2121 ST_COMMENT_STYLE cannot be nested.
2122 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2123 (or 0 If the search cannot start in the middle of a two-character).
2124
2125 If successful, return 1 and store the charpos of the comment's end
2126 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2127 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2128 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2129 (as defined for state.incomment) in *INCOMMENT_PTR.
2130
2131 The comment end is the last character of the comment rather than the
2132 character just after the comment.
2133
2134 Global syntax data is assumed to initially be valid for FROM and
2135 remains valid for forward search starting at the returned position. */
2136
2137static int
dd4c5104
DN
2138forw_comment (EMACS_INT from, EMACS_INT from_byte, EMACS_INT stop,
2139 int nesting, int style, int prev_syntax,
2140 EMACS_INT *charpos_ptr, EMACS_INT *bytepos_ptr,
2141 int *incomment_ptr)
95ff8dfc
RS
2142{
2143 register int c, c1;
2144 register enum syntaxcode code;
c5683ceb 2145 register int syntax, other_syntax;
95ff8dfc
RS
2146
2147 if (nesting <= 0) nesting = -1;
2148
2149 /* Enter the loop in the middle so that we find
2150 a 2-char comment ender if we start in the middle of it. */
2151 syntax = prev_syntax;
2152 if (syntax != 0) goto forw_incomment;
2153
2154 while (1)
2155 {
2156 if (from == stop)
2157 {
2158 *incomment_ptr = nesting;
2159 *charpos_ptr = from;
2160 *bytepos_ptr = from_byte;
2161 return 0;
2162 }
8f924df7 2163 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
95ff8dfc
RS
2164 syntax = SYNTAX_WITH_FLAGS (c);
2165 code = syntax & 0xff;
2166 if (code == Sendcomment
c5683ceb 2167 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style
abf8a9ff
SM
2168 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2169 (nesting > 0 && --nesting == 0) : nesting < 0))
95ff8dfc
RS
2170 /* we have encountered a comment end of the same style
2171 as the comment sequence which began this comment
2172 section */
2173 break;
2174 if (code == Scomment_fence
2175 && style == ST_COMMENT_STYLE)
2176 /* we have encountered a comment end of the same style
2177 as the comment sequence which began this comment
2178 section. */
2179 break;
2180 if (nesting > 0
2181 && code == Scomment
abf8a9ff 2182 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
c5683ceb 2183 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == style)
95ff8dfc
RS
2184 /* we have encountered a nested comment of the same style
2185 as the comment sequence which began this comment section */
2186 nesting++;
2187 INC_BOTH (from, from_byte);
2188 UPDATE_SYNTAX_TABLE_FORWARD (from);
7d0393cf 2189
95ff8dfc
RS
2190 forw_incomment:
2191 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
8f924df7 2192 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
c5683ceb
SM
2193 other_syntax = SYNTAX_WITH_FLAGS (c1),
2194 SYNTAX_FLAGS_COMEND_SECOND (other_syntax))
2195 && SYNTAX_FLAGS_COMMENT_STYLE (syntax, other_syntax) == style
abf8a9ff 2196 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
c5683ceb
SM
2197 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax))
2198 ? nesting > 0 : nesting < 0))
4ffe723b
GM
2199 {
2200 if (--nesting <= 0)
2201 /* we have encountered a comment end of the same style
2202 as the comment sequence which began this comment
2203 section */
2204 break;
2205 else
2206 {
2207 INC_BOTH (from, from_byte);
2208 UPDATE_SYNTAX_TABLE_FORWARD (from);
2209 }
2210 }
95ff8dfc
RS
2211 if (nesting > 0
2212 && from < stop
2213 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
8f924df7 2214 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
c5683ceb
SM
2215 other_syntax = SYNTAX_WITH_FLAGS (c1),
2216 SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax) == style
2217 && SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
abf8a9ff 2218 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
c5683ceb 2219 SYNTAX_FLAGS_COMMENT_NESTED (other_syntax)))
95ff8dfc
RS
2220 /* we have encountered a nested comment of the same style
2221 as the comment sequence which began this comment
2222 section */
2223 {
2224 INC_BOTH (from, from_byte);
2225 UPDATE_SYNTAX_TABLE_FORWARD (from);
2226 nesting++;
2227 }
2228 }
2229 *charpos_ptr = from;
2230 *bytepos_ptr = from_byte;
2231 return 1;
2232}
2233
b3cfe0c8 2234DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
177c0ea7 2235 doc: /*
ee648c11 2236Move forward across up to COUNT comments. If COUNT is negative, move backward.
fdb82f93
PJ
2237Stop scanning if we find something other than a comment or whitespace.
2238Set point to where scanning stops.
ee648c11 2239If COUNT comments are found as expected, with nothing except whitespace
fdb82f93 2240between them, return t; otherwise return nil. */)
5842a27b 2241 (Lisp_Object count)
b3cfe0c8 2242{
2312c580
SM
2243 register EMACS_INT from;
2244 EMACS_INT from_byte;
2245 register EMACS_INT stop;
8ea151b2 2246 register int c, c1;
b3cfe0c8
RS
2247 register enum syntaxcode code;
2248 int comstyle = 0; /* style of comment encountered */
95ff8dfc 2249 int comnested = 0; /* whether the comment is nestable or not */
be720845 2250 int found;
2312c580
SM
2251 EMACS_INT count1;
2252 EMACS_INT out_charpos, out_bytepos;
95ff8dfc 2253 int dummy;
840f481c 2254
b7826503 2255 CHECK_NUMBER (count);
840f481c 2256 count1 = XINT (count);
195d1361 2257 stop = count1 > 0 ? ZV : BEGV;
b3cfe0c8
RS
2258
2259 immediate_quit = 1;
2260 QUIT;
2261
2262 from = PT;
6a140a74 2263 from_byte = PT_BYTE;
b3cfe0c8 2264
195d1361 2265 SETUP_SYNTAX_TABLE (from, count1);
840f481c 2266 while (count1 > 0)
b3cfe0c8 2267 {
04882296 2268 do
b3cfe0c8 2269 {
c5683ceb 2270 int comstart_first, syntax, other_syntax;
f902a008 2271
04882296
KH
2272 if (from == stop)
2273 {
ef316cf0 2274 SET_PT_BOTH (from, from_byte);
b7e6e612 2275 immediate_quit = 0;
04882296
KH
2276 return Qnil;
2277 }
b7dbcc19 2278 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
c5683ceb 2279 syntax = SYNTAX_WITH_FLAGS (c);
b3cfe0c8 2280 code = SYNTAX (c);
c5683ceb
SM
2281 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2282 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2283 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
6a140a74 2284 INC_BOTH (from, from_byte);
f902a008 2285 UPDATE_SYNTAX_TABLE_FORWARD (from);
f902a008 2286 if (from < stop && comstart_first
b7dbcc19 2287 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
c5683ceb
SM
2288 other_syntax = SYNTAX_WITH_FLAGS (c1),
2289 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax)))
b3cfe0c8 2290 {
7d0393cf 2291 /* We have encountered a comment start sequence and we
7fc8191e 2292 are ignoring all text inside comments. We must record
b3cfe0c8
RS
2293 the comment style this sequence begins so that later,
2294 only a comment end of the same style actually ends
7fc8191e 2295 the comment section. */
b3cfe0c8 2296 code = Scomment;
c5683ceb
SM
2297 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2298 comnested
2299 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
6a140a74 2300 INC_BOTH (from, from_byte);
95ff8dfc 2301 UPDATE_SYNTAX_TABLE_FORWARD (from);
b3cfe0c8 2302 }
04882296 2303 }
abf8a9ff 2304 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
6a140a74 2305
da71ab91
KH
2306 if (code == Scomment_fence)
2307 comstyle = ST_COMMENT_STYLE;
2308 else if (code != Scomment)
04882296
KH
2309 {
2310 immediate_quit = 0;
6a140a74 2311 DEC_BOTH (from, from_byte);
ef316cf0 2312 SET_PT_BOTH (from, from_byte);
04882296
KH
2313 return Qnil;
2314 }
2315 /* We're at the start of a comment. */
95ff8dfc
RS
2316 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2317 &out_charpos, &out_bytepos, &dummy);
2318 from = out_charpos; from_byte = out_bytepos;
2319 if (!found)
04882296 2320 {
95ff8dfc
RS
2321 immediate_quit = 0;
2322 SET_PT_BOTH (from, from_byte);
2323 return Qnil;
b3cfe0c8 2324 }
95ff8dfc
RS
2325 INC_BOTH (from, from_byte);
2326 UPDATE_SYNTAX_TABLE_FORWARD (from);
04882296 2327 /* We have skipped one comment. */
840f481c 2328 count1--;
b3cfe0c8
RS
2329 }
2330
840f481c 2331 while (count1 < 0)
b3cfe0c8 2332 {
b9145dbb 2333 while (1)
b3cfe0c8 2334 {
c5683ceb 2335 int quoted, syntax;
f902a008 2336
b9145dbb
RS
2337 if (from <= stop)
2338 {
6a140a74 2339 SET_PT_BOTH (BEGV, BEGV_BYTE);
b9145dbb
RS
2340 immediate_quit = 0;
2341 return Qnil;
2342 }
b3cfe0c8 2343
6a140a74 2344 DEC_BOTH (from, from_byte);
f902a008 2345 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
6a140a74 2346 quoted = char_quoted (from, from_byte);
b7dbcc19 2347 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
c5683ceb 2348 syntax = SYNTAX_WITH_FLAGS (c);
b3cfe0c8
RS
2349 code = SYNTAX (c);
2350 comstyle = 0;
c5683ceb 2351 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
7fc8191e 2352 if (code == Sendcomment)
c5683ceb
SM
2353 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2354 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
f902a008 2355 && prev_char_comend_first (from, from_byte)
89c6809a 2356 && !char_quoted (from - 1, dec_bytepos (from_byte)))
b3cfe0c8 2357 {
c5683ceb 2358 int other_syntax;
7fc8191e 2359 /* We must record the comment style encountered so that
b3cfe0c8 2360 later, we can match only the proper comment begin
7fc8191e 2361 sequence of the same style. */
6a140a74 2362 DEC_BOTH (from, from_byte);
f902a008
RS
2363 code = Sendcomment;
2364 /* Calling char_quoted, above, set up global syntax position
2365 at the new value of FROM. */
b7dbcc19 2366 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
c5683ceb
SM
2367 other_syntax = SYNTAX_WITH_FLAGS (c1);
2368 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2369 comnested
2370 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
b3cfe0c8
RS
2371 }
2372
195d1361
RS
2373 if (code == Scomment_fence)
2374 {
2375 /* Skip until first preceding unquoted comment_fence. */
01f44d5a 2376 int fence_found = 0;
4f3a2f8d 2377 EMACS_INT ini = from, ini_byte = from_byte;
7d0393cf 2378
6a140a74 2379 while (1)
195d1361 2380 {
6a140a74 2381 DEC_BOTH (from, from_byte);
195d1361 2382 UPDATE_SYNTAX_TABLE_BACKWARD (from);
b7dbcc19 2383 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
6a140a74 2384 if (SYNTAX (c) == Scomment_fence
7d0393cf 2385 && !char_quoted (from, from_byte))
195d1361 2386 {
01f44d5a 2387 fence_found = 1;
195d1361
RS
2388 break;
2389 }
fcdd4585
SM
2390 else if (from == stop)
2391 break;
195d1361 2392 }
01f44d5a 2393 if (fence_found == 0)
195d1361
RS
2394 {
2395 from = ini; /* Set point to ini + 1. */
6a140a74 2396 from_byte = ini_byte;
195d1361
RS
2397 goto leave;
2398 }
39c41ad4
SM
2399 else
2400 /* We have skipped one comment. */
2401 break;
195d1361
RS
2402 }
2403 else if (code == Sendcomment)
b3cfe0c8 2404 {
95ff8dfc 2405 found = back_comment (from, from_byte, stop, comnested, comstyle,
6a140a74 2406 &out_charpos, &out_bytepos);
1aa963c8
SM
2407 if (found == -1)
2408 {
abf8a9ff
SM
2409 if (c == '\n')
2410 /* This end-of-line is not an end-of-comment.
2411 Treat it like a whitespace.
2412 CC-mode (and maybe others) relies on this behavior. */
2413 ;
2414 else
2415 {
2416 /* Failure: we should go back to the end of this
2417 not-quite-endcomment. */
c5683ceb 2418 if (SYNTAX (c) != code)
abf8a9ff
SM
2419 /* It was a two-char Sendcomment. */
2420 INC_BOTH (from, from_byte);
2421 goto leave;
2422 }
1aa963c8 2423 }
2b927d02 2424 else
abf8a9ff
SM
2425 {
2426 /* We have skipped one comment. */
2427 from = out_charpos, from_byte = out_bytepos;
2428 break;
2429 }
b3cfe0c8 2430 }
bb0de084 2431 else if (code != Swhitespace || quoted)
b3cfe0c8 2432 {
195d1361 2433 leave:
b3cfe0c8 2434 immediate_quit = 0;
6a140a74 2435 INC_BOTH (from, from_byte);
ef316cf0 2436 SET_PT_BOTH (from, from_byte);
b3cfe0c8
RS
2437 return Qnil;
2438 }
2439 }
2440
840f481c 2441 count1++;
b3cfe0c8
RS
2442 }
2443
ef316cf0 2444 SET_PT_BOTH (from, from_byte);
b3cfe0c8
RS
2445 immediate_quit = 0;
2446 return Qt;
2447}
2448\f
b7dbcc19 2449/* Return syntax code of character C if C is an ASCII character
db9f48c3 2450 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
bd25db08 2451
b7dbcc19
KH
2452#define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2453 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
bd25db08
KH
2454 ? SYNTAX (c) : Ssymbol)
2455
6a140a74 2456static Lisp_Object
971de7fb 2457scan_lists (register EMACS_INT from, EMACS_INT count, EMACS_INT depth, int sexpflag)
8489eb67
RS
2458{
2459 Lisp_Object val;
932e6895 2460 register EMACS_INT stop = count > 0 ? ZV : BEGV;
93da5fff
KH
2461 register int c, c1;
2462 int stringterm;
8489eb67
RS
2463 int quoted;
2464 int mathexit = 0;
93da5fff 2465 register enum syntaxcode code, temp_code;
195d1361 2466 int min_depth = depth; /* Err out if depth gets less than this. */
e5d4f4dc 2467 int comstyle = 0; /* style of comment encountered */
95ff8dfc 2468 int comnested = 0; /* whether the comment is nestable or not */
932e6895
SM
2469 EMACS_INT temp_pos;
2470 EMACS_INT last_good = from;
195d1361 2471 int found;
932e6895
SM
2472 EMACS_INT from_byte;
2473 EMACS_INT out_bytepos, out_charpos;
95ff8dfc 2474 int temp, dummy;
bd25db08 2475 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
8489eb67
RS
2476
2477 if (depth > 0) min_depth = 0;
2478
c3907a7e
RS
2479 if (from > ZV) from = ZV;
2480 if (from < BEGV) from = BEGV;
2481
2482 from_byte = CHAR_TO_BYTE (from);
2483
8489eb67
RS
2484 immediate_quit = 1;
2485 QUIT;
2486
195d1361 2487 SETUP_SYNTAX_TABLE (from, count);
8489eb67
RS
2488 while (count > 0)
2489 {
8489eb67
RS
2490 while (from < stop)
2491 {
c5683ceb 2492 int comstart_first, prefix, syntax, other_syntax;
195d1361 2493 UPDATE_SYNTAX_TABLE_FORWARD (from);
b7dbcc19 2494 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
c5683ceb 2495 syntax = SYNTAX_WITH_FLAGS (c);
bd25db08 2496 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
c5683ceb
SM
2497 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2498 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2499 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2500 prefix = SYNTAX_FLAGS_PREFIX (syntax);
7bf5e9e4
RS
2501 if (depth == min_depth)
2502 last_good = from;
6a140a74 2503 INC_BOTH (from, from_byte);
195d1361 2504 UPDATE_SYNTAX_TABLE_FORWARD (from);
f902a008 2505 if (from < stop && comstart_first
327719ee 2506 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
c5683ceb
SM
2507 other_syntax = SYNTAX_WITH_FLAGS (c),
2508 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
8489eb67 2509 && parse_sexp_ignore_comments)
e5d4f4dc 2510 {
7d0393cf 2511 /* we have encountered a comment start sequence and we
195d1361 2512 are ignoring all text inside comments. We must record
e5d4f4dc
RS
2513 the comment style this sequence begins so that later,
2514 only a comment end of the same style actually ends
2515 the comment section */
2516 code = Scomment;
c5683ceb
SM
2517 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2518 comnested
2519 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
6a140a74 2520 INC_BOTH (from, from_byte);
f902a008 2521 UPDATE_SYNTAX_TABLE_FORWARD (from);
e5d4f4dc 2522 }
7d0393cf 2523
f902a008 2524 if (prefix)
8489eb67
RS
2525 continue;
2526
0220c518 2527 switch (SWITCH_ENUM_CAST (code))
8489eb67
RS
2528 {
2529 case Sescape:
2530 case Scharquote:
8b8bf8e6
SM
2531 if (from == stop)
2532 goto lose;
6a140a74 2533 INC_BOTH (from, from_byte);
8489eb67
RS
2534 /* treat following character as a word constituent */
2535 case Sword:
2536 case Ssymbol:
2537 if (depth || !sexpflag) break;
195d1361 2538 /* This word counts as a sexp; return at end of it. */
8489eb67
RS
2539 while (from < stop)
2540 {
195d1361 2541 UPDATE_SYNTAX_TABLE_FORWARD (from);
7c7ca3d2
RS
2542
2543 /* Some compilers can't handle this inside the switch. */
b7dbcc19 2544 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
bd25db08 2545 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
7c7ca3d2 2546 switch (temp)
8489eb67
RS
2547 {
2548 case Scharquote:
2549 case Sescape:
6a140a74 2550 INC_BOTH (from, from_byte);
8b8bf8e6
SM
2551 if (from == stop)
2552 goto lose;
8489eb67
RS
2553 break;
2554 case Sword:
2555 case Ssymbol:
2556 case Squote:
2557 break;
2558 default:
2559 goto done;
2560 }
6a140a74 2561 INC_BOTH (from, from_byte);
8489eb67
RS
2562 }
2563 goto done;
2564
195d1361 2565 case Scomment_fence:
95ff8dfc
RS
2566 comstyle = ST_COMMENT_STYLE;
2567 /* FALLTHROUGH */
2568 case Scomment:
8489eb67 2569 if (!parse_sexp_ignore_comments) break;
95ff8dfc
RS
2570 UPDATE_SYNTAX_TABLE_FORWARD (from);
2571 found = forw_comment (from, from_byte, stop,
2572 comnested, comstyle, 0,
2573 &out_charpos, &out_bytepos, &dummy);
2574 from = out_charpos, from_byte = out_bytepos;
2575 if (!found)
8489eb67 2576 {
95ff8dfc
RS
2577 if (depth == 0)
2578 goto done;
2579 goto lose;
8489eb67 2580 }
95ff8dfc
RS
2581 INC_BOTH (from, from_byte);
2582 UPDATE_SYNTAX_TABLE_FORWARD (from);
8489eb67
RS
2583 break;
2584
2585 case Smath:
2586 if (!sexpflag)
2587 break;
b7dbcc19 2588 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
6a140a74
RS
2589 {
2590 INC_BOTH (from, from_byte);
2591 }
8489eb67
RS
2592 if (mathexit)
2593 {
2594 mathexit = 0;
2595 goto close1;
2596 }
2597 mathexit = 1;
2598
2599 case Sopen:
2600 if (!++depth) goto done;
2601 break;
2602
2603 case Sclose:
2604 close1:
2605 if (!--depth) goto done;
2606 if (depth < min_depth)
9471945b
KS
2607 xsignal3 (Qscan_error,
2608 build_string ("Containing expression ends prematurely"),
2609 make_number (last_good), make_number (from));
8489eb67
RS
2610 break;
2611
2612 case Sstring:
195d1361 2613 case Sstring_fence:
ef316cf0 2614 temp_pos = dec_bytepos (from_byte);
b7dbcc19 2615 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
8489eb67
RS
2616 while (1)
2617 {
8b8bf8e6
SM
2618 if (from >= stop)
2619 goto lose;
195d1361 2620 UPDATE_SYNTAX_TABLE_FORWARD (from);
b7dbcc19 2621 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
42ebfa31
SM
2622 if (code == Sstring
2623 ? (c == stringterm
2624 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
bd25db08 2625 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
195d1361 2626 break;
7c7ca3d2
RS
2627
2628 /* Some compilers can't handle this inside the switch. */
bd25db08 2629 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
7c7ca3d2 2630 switch (temp)
8489eb67
RS
2631 {
2632 case Scharquote:
2633 case Sescape:
6a140a74 2634 INC_BOTH (from, from_byte);
8489eb67 2635 }
6a140a74 2636 INC_BOTH (from, from_byte);
8489eb67 2637 }
6a140a74 2638 INC_BOTH (from, from_byte);
8489eb67
RS
2639 if (!depth && sexpflag) goto done;
2640 break;
240c43e8
SM
2641 default:
2642 /* Ignore whitespace, punctuation, quote, endcomment. */
2643 break;
8489eb67
RS
2644 }
2645 }
2646
2647 /* Reached end of buffer. Error if within object, return nil if between */
8b8bf8e6
SM
2648 if (depth)
2649 goto lose;
8489eb67
RS
2650
2651 immediate_quit = 0;
2652 return Qnil;
2653
2654 /* End of object reached */
2655 done:
2656 count--;
2657 }
2658
2659 while (count < 0)
2660 {
8489eb67
RS
2661 while (from > stop)
2662 {
c5683ceb 2663 int syntax;
6a140a74 2664 DEC_BOTH (from, from_byte);
195d1361 2665 UPDATE_SYNTAX_TABLE_BACKWARD (from);
b7dbcc19 2666 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
c5683ceb 2667 syntax= SYNTAX_WITH_FLAGS (c);
bd25db08 2668 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
7bf5e9e4
RS
2669 if (depth == min_depth)
2670 last_good = from;
7fc8191e 2671 comstyle = 0;
c5683ceb 2672 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
7fc8191e 2673 if (code == Sendcomment)
c5683ceb
SM
2674 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2675 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
1674d9a2 2676 && prev_char_comend_first (from, from_byte)
8489eb67 2677 && parse_sexp_ignore_comments)
e5d4f4dc 2678 {
f902a008 2679 /* We must record the comment style encountered so that
e5d4f4dc 2680 later, we can match only the proper comment begin
f902a008 2681 sequence of the same style. */
01f44d5a 2682 int c2, other_syntax;
6a140a74 2683 DEC_BOTH (from, from_byte);
f902a008
RS
2684 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2685 code = Sendcomment;
01f44d5a
PE
2686 c2 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2687 other_syntax = SYNTAX_WITH_FLAGS (c2);
c5683ceb
SM
2688 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2689 comnested
2690 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
e5d4f4dc 2691 }
7d0393cf 2692
9828a477 2693 /* Quoting turns anything except a comment-ender
e6365855 2694 into a word character. Note that this cannot be true
f902a008 2695 if we decremented FROM in the if-statement above. */
9828a477 2696 if (code != Sendcomment && char_quoted (from, from_byte))
240c43e8
SM
2697 {
2698 DEC_BOTH (from, from_byte);
2699 code = Sword;
2700 }
c5683ceb 2701 else if (SYNTAX_FLAGS_PREFIX (syntax))
8489eb67
RS
2702 continue;
2703
9828a477 2704 switch (SWITCH_ENUM_CAST (code))
8489eb67
RS
2705 {
2706 case Sword:
2707 case Ssymbol:
9828a477
KH
2708 case Sescape:
2709 case Scharquote:
8489eb67 2710 if (depth || !sexpflag) break;
195d1361
RS
2711 /* This word counts as a sexp; count object finished
2712 after passing it. */
8489eb67
RS
2713 while (from > stop)
2714 {
6a140a74 2715 temp_pos = from_byte;
4b4deea2 2716 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
ef316cf0
RS
2717 DEC_POS (temp_pos);
2718 else
2719 temp_pos--;
6a140a74 2720 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
b7dbcc19 2721 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
bd25db08 2722 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
9828a477
KH
2723 /* Don't allow comment-end to be quoted. */
2724 if (temp_code == Sendcomment)
2725 goto done2;
6a140a74 2726 quoted = char_quoted (from - 1, temp_pos);
8489eb67 2727 if (quoted)
93da5fff 2728 {
6a140a74 2729 DEC_BOTH (from, from_byte);
ef316cf0 2730 temp_pos = dec_bytepos (temp_pos);
6a140a74 2731 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
93da5fff 2732 }
b7dbcc19 2733 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
bd25db08 2734 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
93da5fff
KH
2735 if (! (quoted || temp_code == Sword
2736 || temp_code == Ssymbol
2737 || temp_code == Squote))
8489eb67 2738 goto done2;
6a140a74 2739 DEC_BOTH (from, from_byte);
8489eb67
RS
2740 }
2741 goto done2;
2742
2743 case Smath:
2744 if (!sexpflag)
2745 break;
ef316cf0 2746 temp_pos = dec_bytepos (from_byte);
6a140a74 2747 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
b7dbcc19 2748 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
6a140a74 2749 DEC_BOTH (from, from_byte);
8489eb67
RS
2750 if (mathexit)
2751 {
2752 mathexit = 0;
2753 goto open2;
2754 }
2755 mathexit = 1;
2756
2757 case Sclose:
2758 if (!++depth) goto done2;
2759 break;
2760
2761 case Sopen:
2762 open2:
2763 if (!--depth) goto done2;
2764 if (depth < min_depth)
9471945b
KS
2765 xsignal3 (Qscan_error,
2766 build_string ("Containing expression ends prematurely"),
2767 make_number (last_good), make_number (from));
8489eb67
RS
2768 break;
2769
2770 case Sendcomment:
2771 if (!parse_sexp_ignore_comments)
2772 break;
95ff8dfc 2773 found = back_comment (from, from_byte, stop, comnested, comstyle,
6a140a74 2774 &out_charpos, &out_bytepos);
1aa963c8
SM
2775 /* FIXME: if found == -1, then it really wasn't a comment-end.
2776 For single-char Sendcomment, we can't do much about it apart
2777 from skipping the char.
2778 For 2-char endcomments, we could try again, taking both
2779 chars as separate entities, but it's a lot of trouble
2780 for very little gain, so we don't bother either. -sm */
6a140a74
RS
2781 if (found != -1)
2782 from = out_charpos, from_byte = out_bytepos;
8489eb67
RS
2783 break;
2784
195d1361
RS
2785 case Scomment_fence:
2786 case Sstring_fence:
2787 while (1)
2788 {
8b8bf8e6
SM
2789 if (from == stop)
2790 goto lose;
6b61353c 2791 DEC_BOTH (from, from_byte);
195d1361 2792 UPDATE_SYNTAX_TABLE_BACKWARD (from);
7d0393cf 2793 if (!char_quoted (from, from_byte)
b7dbcc19 2794 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
bd25db08 2795 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
195d1361
RS
2796 break;
2797 }
2798 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2799 break;
7d0393cf 2800
8489eb67 2801 case Sstring:
b7dbcc19 2802 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
8489eb67
RS
2803 while (1)
2804 {
8b8bf8e6
SM
2805 if (from == stop)
2806 goto lose;
6b61353c
KH
2807 DEC_BOTH (from, from_byte);
2808 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2809 if (!char_quoted (from, from_byte)
2810 && (stringterm
2811 == (c = FETCH_CHAR_AS_MULTIBYTE (from_byte)))
42ebfa31 2812 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
8489eb67 2813 break;
8489eb67 2814 }
8489eb67
RS
2815 if (!depth && sexpflag) goto done2;
2816 break;
240c43e8
SM
2817 default:
2818 /* Ignore whitespace, punctuation, quote, endcomment. */
2819 break;
8489eb67
RS
2820 }
2821 }
2822
2823 /* Reached start of buffer. Error if within object, return nil if between */
8b8bf8e6
SM
2824 if (depth)
2825 goto lose;
8489eb67
RS
2826
2827 immediate_quit = 0;
2828 return Qnil;
2829
2830 done2:
2831 count++;
2832 }
2833
2834
2835 immediate_quit = 0;
1e142fb7 2836 XSETFASTINT (val, from);
8489eb67
RS
2837 return val;
2838
2839 lose:
9471945b
KS
2840 xsignal3 (Qscan_error,
2841 build_string ("Unbalanced parentheses"),
2842 make_number (last_good), make_number (from));
8489eb67
RS
2843}
2844
8489eb67 2845DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
fdb82f93
PJ
2846 doc: /* Scan from character number FROM by COUNT lists.
2847Returns the character number of the position thus found.
2848
2849If DEPTH is nonzero, paren depth begins counting from that value,
2850only places where the depth in parentheses becomes zero
2851are candidates for stopping; COUNT such places are counted.
2852Thus, a positive value for DEPTH means go out levels.
2853
2854Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2855
2856If the beginning or end of (the accessible part of) the buffer is reached
2857and the depth is wrong, an error is signaled.
2858If the depth is right but the count is not used up, nil is returned. */)
5842a27b 2859 (Lisp_Object from, Lisp_Object count, Lisp_Object depth)
8489eb67 2860{
b7826503
PJ
2861 CHECK_NUMBER (from);
2862 CHECK_NUMBER (count);
2863 CHECK_NUMBER (depth);
8489eb67
RS
2864
2865 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2866}
2867
2868DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
fdb82f93
PJ
2869 doc: /* Scan from character number FROM by COUNT balanced expressions.
2870If COUNT is negative, scan backwards.
2871Returns the character number of the position thus found.
2872
2873Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2874
2875If the beginning or end of (the accessible part of) the buffer is reached
2876in the middle of a parenthetical grouping, an error is signaled.
2877If the beginning or end is reached between groupings
2878but before count is used up, nil is returned. */)
5842a27b 2879 (Lisp_Object from, Lisp_Object count)
8489eb67 2880{
b7826503
PJ
2881 CHECK_NUMBER (from);
2882 CHECK_NUMBER (count);
8489eb67
RS
2883
2884 return scan_lists (XINT (from), XINT (count), 0, 1);
2885}
2886
2887DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
fdb82f93
PJ
2888 0, 0, 0,
2889 doc: /* Move point backward over any number of chars with prefix syntax.
2890This includes chars with "quote" or "prefix" syntax (' or p). */)
5842a27b 2891 (void)
8489eb67 2892{
4f3a2f8d
EZ
2893 EMACS_INT beg = BEGV;
2894 EMACS_INT opoint = PT;
2895 EMACS_INT opoint_byte = PT_BYTE;
2896 EMACS_INT pos = PT;
2897 EMACS_INT pos_byte = PT_BYTE;
93da5fff 2898 int c;
93da5fff 2899
7d0393cf 2900 if (pos <= beg)
195d1361 2901 {
f902a008
RS
2902 SET_PT_BOTH (opoint, opoint_byte);
2903
2904 return Qnil;
195d1361 2905 }
8489eb67 2906
f902a008
RS
2907 SETUP_SYNTAX_TABLE (pos, -1);
2908
6a140a74
RS
2909 DEC_BOTH (pos, pos_byte);
2910
1fd3172d 2911 while (!char_quoted (pos, pos_byte)
195d1361 2912 /* Previous statement updates syntax table. */
b7dbcc19 2913 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
93da5fff
KH
2914 || SYNTAX_PREFIX (c)))
2915 {
1fd3172d
RS
2916 opoint = pos;
2917 opoint_byte = pos_byte;
2918
2919 if (pos + 1 > beg)
2920 DEC_BOTH (pos, pos_byte);
93da5fff 2921 }
8489eb67 2922
6a140a74 2923 SET_PT_BOTH (opoint, opoint_byte);
8489eb67
RS
2924
2925 return Qnil;
2926}
2927\f
6a140a74 2928/* Parse forward from FROM / FROM_BYTE to END,
e5d4f4dc
RS
2929 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2930 and return a description of the state of the parse at END.
c81a3712 2931 If STOPBEFORE is nonzero, stop at the start of an atom.
644ea4df
RS
2932 If COMMENTSTOP is 1, stop at the start of a comment.
2933 If COMMENTSTOP is -1, stop at the start or end of a comment,
2934 after the beginning of a string, or after the end of a string. */
8489eb67 2935
340f92b5 2936static void
d5a3eaaf
AS
2937scan_sexps_forward (struct lisp_parse_state *stateptr,
2938 EMACS_INT from, EMACS_INT from_byte, EMACS_INT end,
2939 int targetdepth, int stopbefore,
2940 Lisp_Object oldstate, int commentstop)
8489eb67
RS
2941{
2942 struct lisp_parse_state state;
2943
2944 register enum syntaxcode code;
95ff8dfc
RS
2945 int c1;
2946 int comnested;
8489eb67
RS
2947 struct level { int last, prev; };
2948 struct level levelstart[100];
2949 register struct level *curlevel = levelstart;
2950 struct level *endlevel = levelstart + 100;
8489eb67
RS
2951 register int depth; /* Paren depth of current scanning location.
2952 level - levelstart equals this except
2953 when the depth becomes negative. */
2954 int mindepth; /* Lowest DEPTH value seen. */
2955 int start_quoted = 0; /* Nonzero means starting after a char quote */
2956 Lisp_Object tem;
8e2911c2
AS
2957 EMACS_INT prev_from; /* Keep one character before FROM. */
2958 EMACS_INT prev_from_byte;
1fd3172d 2959 int prev_from_syntax;
195d1361
RS
2960 int boundary_stop = commentstop == -1;
2961 int nofence;
95ff8dfc 2962 int found;
60c86a83 2963 EMACS_INT out_bytepos, out_charpos;
7c7ca3d2 2964 int temp;
93da5fff
KH
2965
2966 prev_from = from;
6a140a74
RS
2967 prev_from_byte = from_byte;
2968 if (from != BEGV)
2969 DEC_BOTH (prev_from, prev_from_byte);
93da5fff
KH
2970
2971 /* Use this macro instead of `from++'. */
6a140a74
RS
2972#define INC_FROM \
2973do { prev_from = from; \
2974 prev_from_byte = from_byte; \
327719ee 2975 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
ab229fdd 2976 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
ef316cf0 2977 INC_BOTH (from, from_byte); \
3ceb4629
SM
2978 if (from < end) \
2979 UPDATE_SYNTAX_TABLE_FORWARD (from); \
6a140a74 2980 } while (0)
8489eb67
RS
2981
2982 immediate_quit = 1;
2983 QUIT;
2984
265a9e55 2985 if (NILP (oldstate))
8489eb67
RS
2986 {
2987 depth = 0;
2988 state.instring = -1;
2989 state.incomment = 0;
195d1361
RS
2990 state.comstyle = 0; /* comment style a by default. */
2991 state.comstr_start = -1; /* no comment/string seen. */
8489eb67
RS
2992 }
2993 else
2994 {
2995 tem = Fcar (oldstate);
265a9e55 2996 if (!NILP (tem))
8489eb67
RS
2997 depth = XINT (tem);
2998 else
2999 depth = 0;
3000
3001 oldstate = Fcdr (oldstate);
3002 oldstate = Fcdr (oldstate);
3003 oldstate = Fcdr (oldstate);
3004 tem = Fcar (oldstate);
195d1361 3005 /* Check whether we are inside string_fence-style string: */
7d0393cf
JB
3006 state.instring = (!NILP (tem)
3007 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
e76f1c44 3008 : -1);
8489eb67
RS
3009
3010 oldstate = Fcdr (oldstate);
3011 tem = Fcar (oldstate);
e76f1c44
GM
3012 state.incomment = (!NILP (tem)
3013 ? (INTEGERP (tem) ? XINT (tem) : -1)
95ff8dfc 3014 : 0);
8489eb67
RS
3015
3016 oldstate = Fcdr (oldstate);
3017 tem = Fcar (oldstate);
265a9e55 3018 start_quoted = !NILP (tem);
e5d4f4dc 3019
95ff8dfc 3020 /* if the eighth element of the list is nil, we are in comment
195d1361
RS
3021 style a. If it is non-nil, we are in comment style b */
3022 oldstate = Fcdr (oldstate);
e5d4f4dc 3023 oldstate = Fcdr (oldstate);
195d1361 3024 tem = Fcar (oldstate);
c5683ceb
SM
3025 state.comstyle = (NILP (tem)
3026 ? 0
3027 : (EQ (tem, Qsyntax_table)
3028 ? ST_COMMENT_STYLE
3029 : INTEGERP (tem) ? XINT (tem) : 1));
195d1361 3030
e5d4f4dc 3031 oldstate = Fcdr (oldstate);
e5d4f4dc 3032 tem = Fcar (oldstate);
195d1361 3033 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
1a102a58
RS
3034 oldstate = Fcdr (oldstate);
3035 tem = Fcar (oldstate);
3036 while (!NILP (tem)) /* >= second enclosing sexps. */
3037 {
3038 /* curlevel++->last ran into compiler bug on Apollo */
3039 curlevel->last = XINT (Fcar (tem));
3040 if (++curlevel == endlevel)
02010917 3041 curlevel--; /* error ("Nesting too deep for parser"); */
1a102a58
RS
3042 curlevel->prev = -1;
3043 curlevel->last = -1;
3044 tem = Fcdr (tem);
3045 }
8489eb67
RS
3046 }
3047 state.quoted = 0;
3048 mindepth = depth;
3049
3050 curlevel->prev = -1;
3051 curlevel->last = -1;
3052
326b283b 3053 SETUP_SYNTAX_TABLE (prev_from, 1);
ab229fdd
AS
3054 temp = FETCH_CHAR (prev_from_byte);
3055 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
3ceb4629 3056 UPDATE_SYNTAX_TABLE_FORWARD (from);
326b283b 3057
195d1361 3058 /* Enter the loop at a place appropriate for initial state. */
8489eb67 3059
326b283b
RS
3060 if (state.incomment)
3061 goto startincomment;
8489eb67
RS
3062 if (state.instring >= 0)
3063 {
195d1361 3064 nofence = state.instring != ST_STRING_STYLE;
326b283b
RS
3065 if (start_quoted)
3066 goto startquotedinstring;
8489eb67
RS
3067 goto startinstring;
3068 }
326b283b
RS
3069 else if (start_quoted)
3070 goto startquoted;
1fd3172d 3071
8489eb67
RS
3072 while (from < end)
3073 {
c5683ceb 3074 int syntax;
93da5fff 3075 INC_FROM;
1fd3172d 3076 code = prev_from_syntax & 0xff;
4c920633 3077
c5d4b96f
SM
3078 if (from < end
3079 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3080 && (c1 = FETCH_CHAR (from_byte),
c5683ceb
SM
3081 syntax = SYNTAX_WITH_FLAGS (c1),
3082 SYNTAX_FLAGS_COMSTART_SECOND (syntax)))
c5d4b96f
SM
3083 /* Duplicate code to avoid a complex if-expression
3084 which causes trouble for the SGI compiler. */
95ff8dfc 3085 {
c5d4b96f
SM
3086 /* Record the comment style we have entered so that only
3087 the comment-end sequence of the same style actually
3088 terminates the comment section. */
c5683ceb
SM
3089 state.comstyle
3090 = SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_from_syntax);
c5d4b96f 3091 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
c5683ceb 3092 comnested = comnested || SYNTAX_FLAGS_COMMENT_NESTED (syntax);
c5d4b96f 3093 state.incomment = comnested ? 1 : -1;
95ff8dfc 3094 state.comstr_start = prev_from;
c5d4b96f
SM
3095 INC_FROM;
3096 code = Scomment;
95ff8dfc 3097 }
4c920633 3098 else if (code == Scomment_fence)
e5d4f4dc
RS
3099 {
3100 /* Record the comment style we have entered so that only
3101 the comment-end sequence of the same style actually
3102 terminates the comment section. */
95ff8dfc
RS
3103 state.comstyle = ST_COMMENT_STYLE;
3104 state.incomment = -1;
195d1361 3105 state.comstr_start = prev_from;
e5d4f4dc 3106 code = Scomment;
e5d4f4dc 3107 }
c5d4b96f
SM
3108 else if (code == Scomment)
3109 {
c5683ceb 3110 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax, 0);
c5d4b96f
SM
3111 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3112 1 : -1);
3113 state.comstr_start = prev_from;
3114 }
e5d4f4dc 3115
1fd3172d 3116 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
8489eb67 3117 continue;
0220c518 3118 switch (SWITCH_ENUM_CAST (code))
8489eb67
RS
3119 {
3120 case Sescape:
3121 case Scharquote:
3122 if (stopbefore) goto stop; /* this arg means stop at sexp start */
93da5fff 3123 curlevel->last = prev_from;
8489eb67
RS
3124 startquoted:
3125 if (from == end) goto endquoted;
93da5fff 3126 INC_FROM;
8489eb67
RS
3127 goto symstarted;
3128 /* treat following character as a word constituent */
3129 case Sword:
3130 case Ssymbol:
3131 if (stopbefore) goto stop; /* this arg means stop at sexp start */
93da5fff 3132 curlevel->last = prev_from;
8489eb67
RS
3133 symstarted:
3134 while (from < end)
3135 {
7c7ca3d2 3136 /* Some compilers can't handle this inside the switch. */
327719ee 3137 temp = FETCH_CHAR_AS_MULTIBYTE (from_byte);
ab229fdd 3138 temp = SYNTAX (temp);
7c7ca3d2 3139 switch (temp)
8489eb67
RS
3140 {
3141 case Scharquote:
3142 case Sescape:
93da5fff 3143 INC_FROM;
8489eb67
RS
3144 if (from == end) goto endquoted;
3145 break;
3146 case Sword:
3147 case Ssymbol:
3148 case Squote:
3149 break;
3150 default:
3151 goto symdone;
3152 }
93da5fff 3153 INC_FROM;
8489eb67
RS
3154 }
3155 symdone:
3156 curlevel->prev = curlevel->last;
3157 break;
3158
240c43e8 3159 case Scomment_fence: /* Can't happen because it's handled above. */
8489eb67 3160 case Scomment:
195d1361 3161 if (commentstop || boundary_stop) goto done;
d355bd8a
SM
3162 startincomment:
3163 /* The (from == BEGV) test was to enter the loop in the middle so
95ff8dfc 3164 that we find a 2-char comment ender even if we start in the
e6365855 3165 middle of it. We don't want to do that if we're just at the
d355bd8a 3166 beginning of the comment (think of (*) ... (*)). */
95ff8dfc
RS
3167 found = forw_comment (from, from_byte, end,
3168 state.incomment, state.comstyle,
e6365855
SM
3169 (from == BEGV || from < state.comstr_start + 3)
3170 ? 0 : prev_from_syntax,
95ff8dfc
RS
3171 &out_charpos, &out_bytepos, &state.incomment);
3172 from = out_charpos; from_byte = out_bytepos;
3173 /* Beware! prev_from and friends are invalid now.
3174 Luckily, the `done' doesn't use them and the INC_FROM
3175 sets them to a sane value without looking at them. */
3176 if (!found) goto done;
d355bd8a 3177 INC_FROM;
8489eb67 3178 state.incomment = 0;
e5d4f4dc 3179 state.comstyle = 0; /* reset the comment style */
195d1361 3180 if (boundary_stop) goto done;
8489eb67
RS
3181 break;
3182
3183 case Sopen:
3184 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3185 depth++;
3186 /* curlevel++->last ran into compiler bug on Apollo */
93da5fff 3187 curlevel->last = prev_from;
8489eb67 3188 if (++curlevel == endlevel)
02010917 3189 curlevel--; /* error ("Nesting too deep for parser"); */
8489eb67
RS
3190 curlevel->prev = -1;
3191 curlevel->last = -1;
30844415 3192 if (targetdepth == depth) goto done;
8489eb67
RS
3193 break;
3194
3195 case Sclose:
3196 depth--;
3197 if (depth < mindepth)
3198 mindepth = depth;
3199 if (curlevel != levelstart)
3200 curlevel--;
3201 curlevel->prev = curlevel->last;
30844415 3202 if (targetdepth == depth) goto done;
8489eb67
RS
3203 break;
3204
3205 case Sstring:
195d1361
RS
3206 case Sstring_fence:
3207 state.comstr_start = from - 1;
8489eb67 3208 if (stopbefore) goto stop; /* this arg means stop at sexp start */
93da5fff 3209 curlevel->last = prev_from;
7d0393cf 3210 state.instring = (code == Sstring
b7dbcc19 3211 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
195d1361
RS
3212 : ST_STRING_STYLE);
3213 if (boundary_stop) goto done;
8489eb67 3214 startinstring:
195d1361 3215 {
644ea4df 3216 nofence = state.instring != ST_STRING_STYLE;
7d0393cf 3217
644ea4df
RS
3218 while (1)
3219 {
3220 int c;
195d1361 3221
644ea4df 3222 if (from >= end) goto done;
b7dbcc19 3223 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
7c7ca3d2
RS
3224 /* Some compilers can't handle this inside the switch. */
3225 temp = SYNTAX (c);
ccf89641
KH
3226
3227 /* Check TEMP here so that if the char has
3228 a syntax-table property which says it is NOT
3229 a string character, it does not end the string. */
3230 if (nofence && c == state.instring && temp == Sstring)
3231 break;
3232
7c7ca3d2 3233 switch (temp)
644ea4df
RS
3234 {
3235 case Sstring_fence:
3236 if (!nofence) goto string_end;
3237 break;
3238 case Scharquote:
3239 case Sescape:
3240 INC_FROM;
3241 startquotedinstring:
3242 if (from >= end) goto endquoted;
195d1361 3243 }
644ea4df
RS
3244 INC_FROM;
3245 }
195d1361
RS
3246 }
3247 string_end:
8489eb67
RS
3248 state.instring = -1;
3249 curlevel->prev = curlevel->last;
93da5fff 3250 INC_FROM;
195d1361 3251 if (boundary_stop) goto done;
8489eb67
RS
3252 break;
3253
3254 case Smath:
240c43e8
SM
3255 /* FIXME: We should do something with it. */
3256 break;
3257 default:
3258 /* Ignore whitespace, punctuation, quote, endcomment. */
8489eb67
RS
3259 break;
3260 }
3261 }
3262 goto done;
3263
3264 stop: /* Here if stopping before start of sexp. */
93da5fff 3265 from = prev_from; /* We have just fetched the char that starts it; */
8489eb67
RS
3266 goto done; /* but return the position before it. */
3267
3268 endquoted:
3269 state.quoted = 1;
3270 done:
3271 state.depth = depth;
3272 state.mindepth = mindepth;
3273 state.thislevelstart = curlevel->prev;
3274 state.prevlevelstart
3275 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3276 state.location = from;
1a102a58 3277 state.levelstarts = Qnil;
eb4d412d
PE
3278 while (curlevel > levelstart)
3279 state.levelstarts = Fcons (make_number ((--curlevel)->last),
3280 state.levelstarts);
8489eb67
RS
3281 immediate_quit = 0;
3282
e5d4f4dc 3283 *stateptr = state;
8489eb67
RS
3284}
3285
c81a3712 3286DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
fdb82f93
PJ
3287 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3288Parsing stops at TO or when certain criteria are met;
3289 point is set to where parsing stops.
3290If fifth arg OLDSTATE is omitted or nil,
3291 parsing assumes that FROM is the beginning of a function.
0e6228bc 3292Value is a list of elements describing final state of parsing:
fdb82f93
PJ
3293 0. depth in parens.
3294 1. character address of start of innermost containing list; nil if none.
3295 2. character address of start of last complete sexp terminated.
3296 3. non-nil if inside a string.
3297 (it is the character that will terminate the string,
3298 or t if the string should be terminated by a generic string delimiter.)
7d0393cf 3299 4. nil if outside a comment, t if inside a non-nestable comment,
fdb82f93
PJ
3300 else an integer (the current comment nesting).
3301 5. t if following a quote character.
3302 6. the minimum paren-depth encountered during this scan.
c5683ceb 3303 7. style of comment, if any.
fdb82f93
PJ
3304 8. character address of start of comment or string; nil if not in one.
3305 9. Intermediate data for continuation of parsing (subject to change).
3306If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3307in parentheses becomes equal to TARGETDEPTH.
3308Fourth arg STOPBEFORE non-nil means stop when come to
3309 any character that starts a sexp.
0e6228bc 3310Fifth arg OLDSTATE is a list like what this function returns.
fdb82f93 3311 It is used to initialize the state of the parse. Elements number 1, 2, 6
43726c05 3312 and 8 are ignored.
fdb82f93
PJ
3313Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3314 If it is symbol `syntax-table', stop after the start of a comment or a
3315 string, or after end of a comment or a string. */)
5842a27b 3316 (Lisp_Object from, Lisp_Object to, Lisp_Object targetdepth, Lisp_Object stopbefore, Lisp_Object oldstate, Lisp_Object commentstop)
8489eb67
RS
3317{
3318 struct lisp_parse_state state;
3319 int target;
3320
265a9e55 3321 if (!NILP (targetdepth))
8489eb67 3322 {
b7826503 3323 CHECK_NUMBER (targetdepth);
8489eb67
RS
3324 target = XINT (targetdepth);
3325 }
3326 else
3327 target = -100000; /* We won't reach this depth */
3328
3329 validate_region (&from, &to);
6a140a74
RS
3330 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3331 XINT (to),
c81a3712 3332 target, !NILP (stopbefore), oldstate,
7d0393cf 3333 (NILP (commentstop)
195d1361 3334 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
8489eb67
RS
3335
3336 SET_PT (state.location);
7d0393cf 3337
8489eb67 3338 return Fcons (make_number (state.depth),
c5683ceb
SM
3339 Fcons (state.prevlevelstart < 0
3340 ? Qnil : make_number (state.prevlevelstart),
3341 Fcons (state.thislevelstart < 0
3342 ? Qnil : make_number (state.thislevelstart),
7d0393cf
JB
3343 Fcons (state.instring >= 0
3344 ? (state.instring == ST_STRING_STYLE
195d1361 3345 ? Qt : make_number (state.instring)) : Qnil,
95ff8dfc
RS
3346 Fcons (state.incomment < 0 ? Qt :
3347 (state.incomment == 0 ? Qnil :
3348 make_number (state.incomment)),
8489eb67 3349 Fcons (state.quoted ? Qt : Qnil,
bff37d2a 3350 Fcons (make_number (state.mindepth),
7d0393cf 3351 Fcons ((state.comstyle
bff37d2a 3352 ? (state.comstyle == ST_COMMENT_STYLE
c5683ceb
SM
3353 ? Qsyntax_table
3354 : make_number (state.comstyle))
3355 : Qnil),
0beaf54f
DL
3356 Fcons (((state.incomment
3357 || (state.instring >= 0))
bff37d2a
RS
3358 ? make_number (state.comstr_start)
3359 : Qnil),
1a102a58 3360 Fcons (state.levelstarts, Qnil))))))))));
8489eb67
RS
3361}
3362\f
dfcf069d 3363void
971de7fb 3364init_syntax_once (void)
8489eb67 3365{
78f9a1f7 3366 register int i, c;
8ea151b2 3367 Lisp_Object temp;
8489eb67 3368
5ebaddf5 3369 /* This has to be done here, before we call Fmake_char_table. */
cd3520a4 3370 DEFSYM (Qsyntax_table, "syntax-table");
5ebaddf5 3371
d67b4f80 3372 /* Intern_C_String this now in case it isn't already done.
5ebaddf5
RS
3373 Setting this variable twice is harmless.
3374 But don't staticpro it here--that is done in alloc.c. */
d67b4f80 3375 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
5ebaddf5 3376
93da5fff 3377 /* Create objects which can be shared among syntax tables. */
42ebfa31 3378 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
77b37c05 3379 for (i = 0; i < ASIZE (Vsyntax_code_object); i++)
93da5fff
KH
3380 XVECTOR (Vsyntax_code_object)->contents[i]
3381 = Fcons (make_number (i), Qnil);
3382
5ebaddf5
RS
3383 /* Now we are ready to set up this property, so we can
3384 create syntax tables. */
3385 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3386
93da5fff 3387 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
8489eb67 3388
5ebaddf5 3389 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
8489eb67 3390
aa7b08b4
RS
3391 /* Control characters should not be whitespace. */
3392 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3393 for (i = 0; i <= ' ' - 1; i++)
3394 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3395 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3396
3397 /* Except that a few really are whitespace. */
3398 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3399 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3400 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3401 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3402 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3403 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3404
93da5fff 3405 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
8489eb67 3406 for (i = 'a'; i <= 'z'; i++)
8ea151b2 3407 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
8489eb67 3408 for (i = 'A'; i <= 'Z'; i++)
8ea151b2 3409 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
8489eb67 3410 for (i = '0'; i <= '9'; i++)
8ea151b2
RS
3411 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3412
3413 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3414 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3415
3416 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3417 Fcons (make_number (Sopen), make_number (')')));
3418 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3419 Fcons (make_number (Sclose), make_number ('(')));
3420 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3421 Fcons (make_number (Sopen), make_number (']')));
3422 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3423 Fcons (make_number (Sclose), make_number ('[')));
3424 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3425 Fcons (make_number (Sopen), make_number ('}')));
3426 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3427 Fcons (make_number (Sclose), make_number ('{')));
3428 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3429 Fcons (make_number ((int) Sstring), Qnil));
3430 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3431 Fcons (make_number ((int) Sescape), Qnil));
3432
93da5fff 3433 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
8489eb67 3434 for (i = 0; i < 10; i++)
78f9a1f7
KH
3435 {
3436 c = "_-+*/&|<>="[i];
3437 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3438 }
8489eb67 3439
93da5fff 3440 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
8489eb67 3441 for (i = 0; i < 12; i++)
78f9a1f7
KH
3442 {
3443 c = ".,;:?!#@~^'`"[i];
3444 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3445 }
bd25db08
KH
3446
3447 /* All multibyte characters have syntax `word' by default. */
3448 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
5c7b02ab 3449 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
8489eb67
RS
3450}
3451
dfcf069d 3452void
971de7fb 3453syms_of_syntax (void)
8489eb67 3454{
cd3520a4 3455 DEFSYM (Qsyntax_table_p, "syntax-table-p");
8489eb67 3456
93da5fff
KH
3457 staticpro (&Vsyntax_code_object);
3458
a2994c46
KS
3459 staticpro (&gl_state.object);
3460 staticpro (&gl_state.global_code);
3461 staticpro (&gl_state.current_syntax_table);
3462 staticpro (&gl_state.old_prop);
3463
3464 /* Defined in regex.c */
3465 staticpro (&re_match_object);
3466
cd3520a4 3467 DEFSYM (Qscan_error, "scan-error");
7bf5e9e4 3468 Fput (Qscan_error, Qerror_conditions,
d67b4f80 3469 pure_cons (Qscan_error, pure_cons (Qerror, Qnil)));
7bf5e9e4 3470 Fput (Qscan_error, Qerror_message,
d67b4f80 3471 make_pure_c_string ("Scan error"));
7bf5e9e4 3472
29208e82 3473 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments,
fdb82f93 3474 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
8489eb67 3475
29208e82 3476 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties,
fdb82f93
PJ
3477 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3478Otherwise, that text property is simply ignored.
3479See the info node `(elisp)Syntax Properties' for a description of the
3480`syntax-table' property. */);
195d1361 3481
8489eb67 3482 words_include_escapes = 0;
29208e82 3483 DEFVAR_BOOL ("words-include-escapes", words_include_escapes,
fdb82f93 3484 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
8489eb67 3485
29208e82 3486 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol,
fdb82f93 3487 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
bd25db08
KH
3488 multibyte_syntax_as_symbol = 0;
3489
f4ed767f 3490 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
29208e82 3491 open_paren_in_column_0_is_defun_start,
b222e415 3492 doc: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
f4ed767f
GM
3493 open_paren_in_column_0_is_defun_start = 1;
3494
8f924df7
KH
3495
3496 DEFVAR_LISP ("find-word-boundary-function-table",
29208e82 3497 Vfind_word_boundary_function_table,
869bb237 3498 doc: /*
8f924df7 3499Char table of functions to search for the word boundary.
869bb237
KH
3500Each function is called with two arguments; POS and LIMIT.
3501POS and LIMIT are character positions in the current buffer.
3502
3503If POS is less than LIMIT, POS is at the first character of a word,
3504and the return value of a function is a position after the last
3505character of that word.
3506
3507If POS is not less than LIMIT, POS is at the last character of a word,
3508and the return value of a function is a position at the first
3509character of that word.
3510
3511In both cases, LIMIT bounds the search. */);
8f924df7 3512 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
869bb237 3513
8489eb67
RS
3514 defsubr (&Ssyntax_table_p);
3515 defsubr (&Ssyntax_table);
3516 defsubr (&Sstandard_syntax_table);
3517 defsubr (&Scopy_syntax_table);
3518 defsubr (&Sset_syntax_table);
3519 defsubr (&Schar_syntax);
beefa22e 3520 defsubr (&Smatching_paren);
c65adb44 3521 defsubr (&Sstring_to_syntax);
8489eb67 3522 defsubr (&Smodify_syntax_entry);
62abe9cb 3523 defsubr (&Sinternal_describe_syntax_value);
8489eb67
RS
3524
3525 defsubr (&Sforward_word);
3526
195d1361
RS
3527 defsubr (&Sskip_chars_forward);
3528 defsubr (&Sskip_chars_backward);
3529 defsubr (&Sskip_syntax_forward);
3530 defsubr (&Sskip_syntax_backward);
3531
b3cfe0c8 3532 defsubr (&Sforward_comment);
8489eb67
RS
3533 defsubr (&Sscan_lists);
3534 defsubr (&Sscan_sexps);
3535 defsubr (&Sbackward_prefix_chars);
3536 defsubr (&Sparse_partial_sexp);
3537}