More xmalloc and related cleanup.
[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"
5c7b02ab 28#include "character.h"
e5560ff7 29#include "buffer.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
eacd378d
PE
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 {
d311d28c 114 EMACS_INT depth; /* Depth at end of parsing. */
6449674e 115 int instring; /* -1 if not within string, else desired terminator. */
d311d28c 116 EMACS_INT incomment; /* -1 if in unnestable comment else comment nesting */
6449674e
SM
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 */
d311d28c 119 EMACS_INT mindepth; /* Minimum depth seen while scanning. */
6449674e 120 /* Char number of most recent start-of-expression at current level */
d311d28c 121 ptrdiff_t thislevelstart;
6449674e 122 /* Char number of start of containing expression */
d311d28c
PE
123 ptrdiff_t prevlevelstart;
124 ptrdiff_t location; /* Char number at which parsing stopped. */
125 ptrdiff_t comstr_start; /* Position of last comment/string starter. */
6449674e
SM
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
d311d28c
PE
138static ptrdiff_t find_start_pos;
139static ptrdiff_t find_start_value;
140static ptrdiff_t find_start_value_byte;
37bef230 141static struct buffer *find_start_buffer;
d311d28c
PE
142static ptrdiff_t find_start_begv;
143static EMACS_INT find_start_modiff;
6a140a74
RS
144
145
f57e2426
J
146static Lisp_Object skip_chars (int, Lisp_Object, Lisp_Object, int);
147static Lisp_Object skip_syntaxes (int, Lisp_Object, Lisp_Object);
148static Lisp_Object scan_lists (EMACS_INT, EMACS_INT, EMACS_INT, int);
149static void scan_sexps_forward (struct lisp_parse_state *,
d311d28c 150 ptrdiff_t, ptrdiff_t, ptrdiff_t, EMACS_INT,
f57e2426
J
151 int, Lisp_Object, int);
152static int in_classes (int, Lisp_Object);
195d1361 153\f
c5683ceb
SM
154/* Whether the syntax of the character C has the prefix flag set. */
155int syntax_prefix_flag_p (int c)
156{
157 return SYNTAX_PREFIX (c);
158}
195d1361
RS
159
160struct gl_state_s gl_state; /* Global state of syntax parser. */
161
195d1361
RS
162#define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
163 to scan to property-change. */
164
6a140a74
RS
165/* Update gl_state to an appropriate interval which contains CHARPOS. The
166 sign of COUNT give the relative position of CHARPOS wrt the previously
195d1361 167 valid interval. If INIT, only [be]_property fields of gl_state are
6a140a74 168 valid at start, the rest is filled basing on OBJECT.
195d1361 169
6a140a74 170 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
195d1361
RS
171 direction than the intervals - or in an interval. We update the
172 current syntax-table basing on the property of this interval, and
6a140a74 173 update the interval to start further than CHARPOS - or be
195d1361 174 NULL_INTERVAL. We also update lim_property to be the next value of
6a140a74 175 charpos to call this subroutine again - or be before/after the
195d1361
RS
176 start/end of OBJECT. */
177
178void
d311d28c 179update_syntax_table (ptrdiff_t charpos, EMACS_INT count, int init,
4f3a2f8d 180 Lisp_Object object)
195d1361
RS
181{
182 Lisp_Object tmp_table;
12cbf13f
PE
183 unsigned cnt = 0;
184 int invalidate = 1;
5a774522 185 INTERVAL i;
195d1361
RS
186
187 if (init)
188 {
bb0de084 189 gl_state.old_prop = Qnil;
195d1361
RS
190 gl_state.start = gl_state.b_property;
191 gl_state.stop = gl_state.e_property;
bb0de084
SM
192 i = interval_of (charpos, object);
193 gl_state.backward_i = gl_state.forward_i = i;
195d1361
RS
194 invalidate = 0;
195 if (NULL_INTERVAL_P (i))
196 return;
f902a008 197 /* interval_of updates only ->position of the return value, so
d80f4cc7 198 update the parents manually to speed up update_interval. */
bb0de084 199 while (!NULL_PARENT (i))
d80f4cc7
RS
200 {
201 if (AM_RIGHT_CHILD (i))
439d5cb4 202 INTERVAL_PARENT (i)->position = i->position
d80f4cc7 203 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
439d5cb4
KR
204 - TOTAL_LENGTH (INTERVAL_PARENT (i))
205 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
d80f4cc7 206 else
439d5cb4 207 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
d80f4cc7 208 + TOTAL_LENGTH (i);
439d5cb4 209 i = INTERVAL_PARENT (i);
d80f4cc7
RS
210 }
211 i = gl_state.forward_i;
bb0de084 212 gl_state.b_property = i->position - gl_state.offset;
ee0cdb48 213 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
195d1361
RS
214 goto update;
215 }
5a774522 216 i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
195d1361 217
423e705d 218 /* We are guaranteed to be called with CHARPOS either in i,
6a140a74 219 or further off. */
195d1361
RS
220 if (NULL_INTERVAL_P (i))
221 error ("Error in syntax_table logic for to-the-end intervals");
6a140a74 222 else if (charpos < i->position) /* Move left. */
195d1361
RS
223 {
224 if (count > 0)
f902a008 225 error ("Error in syntax_table logic for intervals <-");
195d1361 226 /* Update the interval. */
6a140a74 227 i = update_interval (i, charpos);
bb0de084 228 if (INTERVAL_LAST_POS (i) != gl_state.b_property)
195d1361
RS
229 {
230 invalidate = 0;
195d1361 231 gl_state.forward_i = i;
ee0cdb48 232 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
195d1361 233 }
423e705d 234 }
6a140a74 235 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
195d1361
RS
236 {
237 if (count < 0)
f902a008 238 error ("Error in syntax_table logic for intervals ->");
195d1361 239 /* Update the interval. */
6a140a74 240 i = update_interval (i, charpos);
bb0de084 241 if (i->position != gl_state.e_property)
195d1361
RS
242 {
243 invalidate = 0;
195d1361 244 gl_state.backward_i = i;
bb0de084 245 gl_state.b_property = i->position - gl_state.offset;
195d1361
RS
246 }
247 }
195d1361
RS
248
249 update:
250 tmp_table = textget (i->plist, Qsyntax_table);
251
252 if (invalidate)
253 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
7d0393cf 254
423e705d
SM
255 if (invalidate) /* Did not get to adjacent interval. */
256 { /* with the same table => */
257 /* invalidate the old range. */
195d1361
RS
258 if (count > 0)
259 {
260 gl_state.backward_i = i;
bb0de084
SM
261 gl_state.b_property = i->position - gl_state.offset;
262 }
263 else
195d1361 264 {
bb0de084 265 gl_state.forward_i = i;
ee0cdb48 266 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
195d1361
RS
267 }
268 }
37bef230 269
bb0de084 270 if (!EQ (tmp_table, gl_state.old_prop))
195d1361 271 {
bb0de084
SM
272 gl_state.current_syntax_table = tmp_table;
273 gl_state.old_prop = tmp_table;
274 if (EQ (Fsyntax_table_p (tmp_table), Qt))
275 {
276 gl_state.use_global = 0;
7d0393cf 277 }
bb0de084
SM
278 else if (CONSP (tmp_table))
279 {
280 gl_state.use_global = 1;
281 gl_state.global_code = tmp_table;
282 }
7d0393cf 283 else
bb0de084
SM
284 {
285 gl_state.use_global = 0;
4b4deea2 286 gl_state.current_syntax_table = BVAR (current_buffer, syntax_table);
bb0de084 287 }
195d1361
RS
288 }
289
290 while (!NULL_INTERVAL_P (i))
291 {
292 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
293 {
294 if (count > 0)
bb0de084
SM
295 {
296 gl_state.e_property = i->position - gl_state.offset;
297 gl_state.forward_i = i;
298 }
299 else
300 {
5a774522
SM
301 gl_state.b_property
302 = i->position + LENGTH (i) - gl_state.offset;
bb0de084
SM
303 gl_state.backward_i = i;
304 }
305 return;
195d1361 306 }
7d0393cf 307 else if (cnt == INTERVALS_AT_ONCE)
195d1361
RS
308 {
309 if (count > 0)
bb0de084 310 {
5a774522
SM
311 gl_state.e_property
312 = i->position + LENGTH (i) - gl_state.offset
313 /* e_property at EOB is not set to ZV but to ZV+1, so that
314 we can do INC(from);UPDATE_SYNTAX_TABLE_FORWARD without
315 having to check eob between the two. */
316 + (NULL_INTERVAL_P (next_interval (i)) ? 1 : 0);
bb0de084
SM
317 gl_state.forward_i = i;
318 }
319 else
320 {
321 gl_state.b_property = i->position - gl_state.offset;
322 gl_state.backward_i = i;
323 }
324 return;
195d1361
RS
325 }
326 cnt++;
327 i = count > 0 ? next_interval (i) : previous_interval (i);
328 }
bb0de084
SM
329 eassert (NULL_INTERVAL_P (i)); /* This property goes to the end. */
330 if (count > 0)
331 gl_state.e_property = gl_state.stop;
332 else
333 gl_state.b_property = gl_state.start;
195d1361
RS
334}
335\f
6a140a74
RS
336/* Returns TRUE if char at CHARPOS is quoted.
337 Global syntax-table data should be set up already to be good at CHARPOS
338 or after. On return global syntax data is good for lookup at CHARPOS. */
195d1361
RS
339
340static int
d311d28c 341char_quoted (ptrdiff_t charpos, ptrdiff_t bytepos)
195d1361
RS
342{
343 register enum syntaxcode code;
d311d28c 344 register ptrdiff_t beg = BEGV;
195d1361 345 register int quoted = 0;
d311d28c 346 ptrdiff_t orig = charpos;
6a140a74 347
02d8b017 348 while (charpos > beg)
195d1361 349 {
ab229fdd 350 int c;
02d8b017 351 DEC_BOTH (charpos, bytepos);
ab229fdd 352
6a140a74 353 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
327719ee 354 c = FETCH_CHAR_AS_MULTIBYTE (bytepos);
ab229fdd 355 code = SYNTAX (c);
6a140a74
RS
356 if (! (code == Scharquote || code == Sescape))
357 break;
358
6a140a74 359 quoted = !quoted;
195d1361 360 }
6a140a74
RS
361
362 UPDATE_SYNTAX_TABLE (orig);
195d1361
RS
363 return quoted;
364}
6a140a74 365
6a140a74
RS
366/* Return the bytepos one character before BYTEPOS.
367 We assume that BYTEPOS is not at the start of the buffer. */
368
d311d28c
PE
369static inline ptrdiff_t
370dec_bytepos (ptrdiff_t bytepos)
6a140a74 371{
4b4deea2 372 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
ef316cf0
RS
373 return bytepos - 1;
374
6a140a74
RS
375 DEC_POS (bytepos);
376 return bytepos;
377}
195d1361 378\f
2b34df4e 379/* Return a defun-start position before POS and not too far before.
7cc80f0a
RS
380 It should be the last one before POS, or nearly the last.
381
382 When open_paren_in_column_0_is_defun_start is nonzero,
1fd1cc2f 383 only the beginning of the buffer is treated as a defun-start.
7cc80f0a
RS
384
385 We record the information about where the scan started
386 and what its result was, so that another call in the same area
387 can return the same value very quickly.
195d1361
RS
388
389 There is no promise at which position the global syntax data is
390 valid on return from the subroutine, so the caller should explicitly
391 update the global data. */
37bef230 392
d311d28c
PE
393static ptrdiff_t
394find_defun_start (ptrdiff_t pos, ptrdiff_t pos_byte)
37bef230 395{
d311d28c 396 ptrdiff_t opoint = PT, opoint_byte = PT_BYTE;
37bef230 397
1fd1cc2f
RS
398 if (!open_paren_in_column_0_is_defun_start)
399 {
6e20a0d4 400 find_start_value = BEGV;
1fd1cc2f 401 find_start_value_byte = BEGV_BYTE;
6e20a0d4
CY
402 find_start_buffer = current_buffer;
403 find_start_modiff = MODIFF;
404 find_start_begv = BEGV;
405 find_start_pos = pos;
1fd1cc2f
RS
406 return BEGV;
407 }
408
37bef230
RS
409 /* Use previous finding, if it's valid and applies to this inquiry. */
410 if (current_buffer == find_start_buffer
411 /* Reuse the defun-start even if POS is a little farther on.
412 POS might be in the next defun, but that's ok.
413 Our value may not be the best possible, but will still be usable. */
414 && pos <= find_start_pos + 1000
415 && pos >= find_start_value
416 && BEGV == find_start_begv
417 && MODIFF == find_start_modiff)
418 return find_start_value;
419
420 /* Back up to start of line. */
6a140a74 421 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
37bef230 422
195d1361
RS
423 /* We optimize syntax-table lookup for rare updates. Thus we accept
424 only those `^\s(' which are good in global _and_ text-property
425 syntax-tables. */
d48cd3f4 426 SETUP_BUFFER_SYNTAX_TABLE ();
1fd1cc2f 427 while (PT > BEGV)
37bef230 428 {
ab229fdd
AS
429 int c;
430
1fd1cc2f
RS
431 /* Open-paren at start of line means we may have found our
432 defun-start. */
327719ee 433 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
ab229fdd 434 if (SYNTAX (c) == Sopen)
195d1361 435 {
1fd1cc2f 436 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
327719ee 437 c = FETCH_CHAR_AS_MULTIBYTE (PT_BYTE);
ab229fdd 438 if (SYNTAX (c) == Sopen)
1fd1cc2f
RS
439 break;
440 /* Now fallback to the default value. */
d48cd3f4 441 SETUP_BUFFER_SYNTAX_TABLE ();
195d1361 442 }
1fd1cc2f
RS
443 /* Move to beg of previous line. */
444 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
37bef230
RS
445 }
446
447 /* Record what we found, for the next try. */
6a140a74
RS
448 find_start_value = PT;
449 find_start_value_byte = PT_BYTE;
37bef230
RS
450 find_start_buffer = current_buffer;
451 find_start_modiff = MODIFF;
452 find_start_begv = BEGV;
453 find_start_pos = pos;
454
6a140a74
RS
455 TEMP_SET_PT_BOTH (opoint, opoint_byte);
456
37bef230
RS
457 return find_start_value;
458}
459\f
f902a008
RS
460/* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
461
462static int
d311d28c 463prev_char_comend_first (ptrdiff_t pos, ptrdiff_t pos_byte)
f902a008
RS
464{
465 int c, val;
466
467 DEC_BOTH (pos, pos_byte);
468 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
469 c = FETCH_CHAR (pos_byte);
470 val = SYNTAX_COMEND_FIRST (c);
471 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
472 return val;
473}
474
475/* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
476
3f679f55
SM
477/* static int
478 * prev_char_comstart_first (pos, pos_byte)
479 * int pos, pos_byte;
480 * {
481 * int c, val;
7d0393cf 482 *
3f679f55
SM
483 * DEC_BOTH (pos, pos_byte);
484 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
485 * c = FETCH_CHAR (pos_byte);
486 * val = SYNTAX_COMSTART_FIRST (c);
487 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
488 * return val;
489 * } */
f902a008 490
6a140a74
RS
491/* Checks whether charpos FROM is at the end of a comment.
492 FROM_BYTE is the bytepos corresponding to FROM.
493 Do not move back before STOP.
494
495 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
496 return -1 otherwise.
497
498 If successful, store the charpos of the comment's beginning
499 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
527a32d9
KH
500
501 Global syntax data remains valid for backward search starting at
502 the returned value (or at FROM, if the search was not successful). */
195d1361
RS
503
504static int
d311d28c 505back_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop, int comnested, int comstyle, ptrdiff_t *charpos_ptr, ptrdiff_t *bytepos_ptr)
195d1361
RS
506{
507 /* Look back, counting the parity of string-quotes,
508 and recording the comment-starters seen.
509 When we reach a safe place, assume that's not in a string;
510 then step the main scan to the earliest comment-starter seen
511 an even number of string quotes away from the safe place.
512
513 OFROM[I] is position of the earliest comment-starter seen
514 which is I+2X quotes from the comment-end.
515 PARITY is current parity of quotes from the comment end. */
3ee5041c 516 int string_style = -1; /* Presumed outside of any string. */
195d1361 517 int string_lossage = 0;
f7c436c1 518 /* Not a real lossage: indicates that we have passed a matching comment
7d0393cf 519 starter plus a non-matching comment-ender, meaning that any matching
f7c436c1
SM
520 comment-starter we might see later could be a false positive (hidden
521 inside another comment).
522 Test case: { a (* b } c (* d *) */
523 int comment_lossage = 0;
d311d28c
PE
524 ptrdiff_t comment_end = from;
525 ptrdiff_t comment_end_byte = from_byte;
526 ptrdiff_t comstart_pos = 0;
527 ptrdiff_t comstart_byte IF_LINT (= 0);
eb35b628
RS
528 /* Place where the containing defun starts,
529 or 0 if we didn't come across it yet. */
d311d28c
PE
530 ptrdiff_t defun_start = 0;
531 ptrdiff_t defun_start_byte = 0;
195d1361 532 register enum syntaxcode code;
95ff8dfc 533 int nesting = 1; /* current comment nesting */
ea315ed6 534 int c;
3f679f55
SM
535 int syntax = 0;
536
537 /* FIXME: A }} comment-ender style leads to incorrect behavior
538 in the case of {{ c }}} because we ignore the last two chars which are
539 assumed to be comment-enders although they aren't. */
195d1361
RS
540
541 /* At beginning of range to scan, we're outside of strings;
542 that determines quote parity to the comment-end. */
543 while (from != stop)
544 {
d311d28c 545 ptrdiff_t temp_byte;
4f3a2f8d 546 int prev_syntax, com2start, com2end;
fbb3da77 547 int comstart;
6a140a74 548
195d1361 549 /* Move back and examine a character. */
6a140a74 550 DEC_BOTH (from, from_byte);
195d1361
RS
551 UPDATE_SYNTAX_TABLE_BACKWARD (from);
552
3f679f55 553 prev_syntax = syntax;
b7dbcc19 554 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3f679f55 555 syntax = SYNTAX_WITH_FLAGS (c);
195d1361
RS
556 code = SYNTAX (c);
557
3f679f55
SM
558 /* Check for 2-char comment markers. */
559 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
560 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
c5683ceb
SM
561 && (comstyle
562 == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax, syntax))
3f679f55
SM
563 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
564 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
565 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
566 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
fbb3da77 567 comstart = (com2start || code == Scomment);
01f44d5a 568
3f679f55
SM
569 /* Nasty cases with overlapping 2-char comment markers:
570 - snmp-mode: -- c -- foo -- c --
571 --- c --
572 ------ c --
573 - c-mode: *||*
574 |* *|* *|
575 |*| |* |*|
576 /// */
577
578 /* If a 2-char comment sequence partly overlaps with another,
fbb3da77
SM
579 we don't try to be clever. E.g. |*| in C, or }% in modes that
580 have %..\n and %{..}%. */
581 if (from > stop && (com2end || comstart))
195d1361 582 {
d311d28c 583 ptrdiff_t next = from, next_byte = from_byte;
4f3a2f8d 584 int next_c, next_syntax;
3f679f55
SM
585 DEC_BOTH (next, next_byte);
586 UPDATE_SYNTAX_TABLE_BACKWARD (next);
b7dbcc19 587 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
3f679f55 588 next_syntax = SYNTAX_WITH_FLAGS (next_c);
fbb3da77 589 if (((comstart || comnested)
3f679f55
SM
590 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
591 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
592 || ((com2end || comnested)
593 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
c5683ceb
SM
594 && (comstyle
595 == SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_syntax))
3f679f55
SM
596 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
597 goto lossage;
598 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
f902a008 599 }
3f679f55
SM
600
601 if (com2start && comstart_pos == 0)
602 /* We're looking at a comment starter. But it might be a comment
603 ender as well (see snmp-mode). The first time we see one, we
604 need to consider it as a comment starter,
605 and the subsequent times as a comment ender. */
606 com2end = 0;
607
608 /* Turn a 2-char comment sequences into the appropriate syntax. */
609 if (com2end)
610 code = Sendcomment;
611 else if (com2start)
612 code = Scomment;
613 /* Ignore comment starters of a different style. */
614 else if (code == Scomment
c5683ceb 615 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0)
3f679f55 616 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
1aa963c8 617 continue;
195d1361 618
9828a477
KH
619 /* Ignore escaped characters, except comment-enders. */
620 if (code != Sendcomment && char_quoted (from, from_byte))
195d1361
RS
621 continue;
622
3ee5041c 623 switch (code)
195d1361 624 {
3ee5041c
SM
625 case Sstring_fence:
626 case Scomment_fence:
627 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
628 case Sstring:
629 /* Track parity of quotes. */
630 if (string_style == -1)
631 /* Entering a string. */
632 string_style = c;
633 else if (string_style == c)
634 /* Leaving the string. */
635 string_style = -1;
636 else
637 /* If we have two kinds of string delimiters.
638 There's no way to grok this scanning backwards. */
195d1361 639 string_lossage = 1;
3ee5041c 640 break;
7d0393cf 641
3ee5041c
SM
642 case Scomment:
643 /* We've already checked that it is the relevant comstyle. */
f7c436c1 644 if (string_style != -1 || comment_lossage || string_lossage)
3ee5041c
SM
645 /* There are odd string quotes involved, so let's be careful.
646 Test case in Pascal: " { " a { " } */
647 goto lossage;
648
f7c436c1
SM
649 if (!comnested)
650 {
651 /* Record best comment-starter so far. */
652 comstart_pos = from;
653 comstart_byte = from_byte;
654 }
655 else if (--nesting <= 0)
95ff8dfc
RS
656 /* nested comments have to be balanced, so we don't need to
657 keep looking for earlier ones. We use here the same (slightly
658 incorrect) reasoning as below: since it is followed by uniform
659 paired string quotes, this comment-start has to be outside of
660 strings, else the comment-end itself would be inside a string. */
661 goto done;
3ee5041c
SM
662 break;
663
02010917 664 case Sendcomment:
c5683ceb 665 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0) == comstyle
d070eb22 666 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
3f679f55 667 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
02010917
SM
668 /* This is the same style of comment ender as ours. */
669 {
670 if (comnested)
671 nesting++;
672 else
673 /* Anything before that can't count because it would match
674 this comment-ender rather than ours. */
675 from = stop; /* Break out of the loop. */
676 }
f7c436c1
SM
677 else if (comstart_pos != 0 || c != '\n')
678 /* We're mixing comment styles here, so we'd better be careful.
679 The (comstart_pos != 0 || c != '\n') check is not quite correct
680 (we should just always set comment_lossage), but removing it
681 would imply that any multiline comment in C would go through
682 lossage, which seems overkill.
683 The failure should only happen in the rare cases such as
684 { (* } *) */
685 comment_lossage = 1;
02010917 686 break;
195d1361 687
02010917
SM
688 case Sopen:
689 /* Assume a defun-start point is outside of strings. */
690 if (open_paren_in_column_0_is_defun_start
691 && (from == stop
692 || (temp_byte = dec_bytepos (from_byte),
693 FETCH_CHAR (temp_byte) == '\n')))
694 {
695 defun_start = from;
696 defun_start_byte = from_byte;
697 from = stop; /* Break out of the loop. */
698 }
eb35b628 699 break;
02010917
SM
700
701 default:
f7c436c1 702 break;
eb35b628 703 }
195d1361
RS
704 }
705
706 if (comstart_pos == 0)
707 {
708 from = comment_end;
6a140a74 709 from_byte = comment_end_byte;
195d1361
RS
710 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
711 }
f7c436c1
SM
712 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
713 or `done'), then we've found the beginning of the non-nested comment. */
714 else if (1) /* !comnested */
195d1361
RS
715 {
716 from = comstart_pos;
6a140a74 717 from_byte = comstart_byte;
789f3320 718 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
195d1361
RS
719 }
720 else
721 {
3ee5041c
SM
722 struct lisp_parse_state state;
723 lossage:
195d1361
RS
724 /* We had two kinds of string delimiters mixed up
725 together. Decode this going forwards.
02010917 726 Scan fwd from a known safe place (beginning-of-defun)
195d1361
RS
727 to the one in question; this records where we
728 last passed a comment starter. */
eb35b628
RS
729 /* If we did not already find the defun start, find it now. */
730 if (defun_start == 0)
731 {
732 defun_start = find_defun_start (comment_end, comment_end_byte);
733 defun_start_byte = find_start_value_byte;
734 }
02010917 735 do
195d1361 736 {
02010917
SM
737 scan_sexps_forward (&state,
738 defun_start, defun_start_byte,
d311d28c
PE
739 comment_end, TYPE_MINIMUM (EMACS_INT),
740 0, Qnil, 0);
02010917
SM
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))
28be1ada 990 return AREF (Vsyntax_code_object, 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 1101
d311d28c 1102 if (!INTEGERP (first) || !(NILP (match_lisp) || CHARACTERP (match_lisp)))
8489eb67 1103 {
62abe9cb
SM
1104 insert_string ("invalid");
1105 return syntax;
8489eb67
RS
1106 }
1107
d311d28c 1108 syntax_code = XINT (first) & INT_MAX;
c5683ceb
SM
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
d311d28c
PE
1226ptrdiff_t
1227scan_words (register ptrdiff_t from, register EMACS_INT count)
8489eb67 1228{
d311d28c
PE
1229 register ptrdiff_t beg = BEGV;
1230 register ptrdiff_t end = ZV;
1231 register ptrdiff_t 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));
17f85215 1266 if (INTEGERP (pos) && from < XINT (pos) && XINT (pos) <= ZV)
8f924df7
KH
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));
17f85215 1316 if (INTEGERP (pos) && BEGV <= XINT (pos) && XINT (pos) < from)
8f924df7
KH
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;
d311d28c 1360 ptrdiff_t 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;
d311d28c 1435 register ptrdiff_t 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;
d311d28c 1442 ptrdiff_t 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;
38182d90 1584 char_ranges = alloca (sizeof *char_ranges * 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 {
38182d90 1605 char_ranges = alloca (sizeof *char_ranges * SCHARS (string) * 2);
f003ca54 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 {
d311d28c
PE
1756 ptrdiff_t start_point = PT;
1757 ptrdiff_t pos = PT;
1758 ptrdiff_t 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;
d311d28c 1928 register ptrdiff_t i, i_byte;
b7dbcc19 1929 int multibyte;
d311d28c 1930 ptrdiff_t 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 {
d311d28c
PE
1983 ptrdiff_t start_point = PT;
1984 ptrdiff_t pos = PT;
1985 ptrdiff_t 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
d311d28c
PE
2138forw_comment (ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t stop,
2139 EMACS_INT nesting, int style, int prev_syntax,
2140 ptrdiff_t *charpos_ptr, ptrdiff_t *bytepos_ptr,
2141 EMACS_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{
d311d28c
PE
2243 register ptrdiff_t from;
2244 ptrdiff_t from_byte;
2245 register ptrdiff_t 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 2251 EMACS_INT count1;
d311d28c 2252 ptrdiff_t out_charpos, out_bytepos;
7c85f529 2253 EMACS_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;
d311d28c 2377 ptrdiff_t 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;
d311d28c 2460 register ptrdiff_t 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;
d311d28c 2466 EMACS_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 */
d311d28c 2469 ptrdiff_t temp_pos;
932e6895 2470 EMACS_INT last_good = from;
195d1361 2471 int found;
d311d28c
PE
2472 ptrdiff_t from_byte;
2473 ptrdiff_t out_bytepos, out_charpos;
2474 int temp;
7c85f529 2475 EMACS_INT dummy;
bd25db08 2476 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
8489eb67
RS
2477
2478 if (depth > 0) min_depth = 0;
2479
c3907a7e
RS
2480 if (from > ZV) from = ZV;
2481 if (from < BEGV) from = BEGV;
2482
2483 from_byte = CHAR_TO_BYTE (from);
2484
8489eb67
RS
2485 immediate_quit = 1;
2486 QUIT;
2487
195d1361 2488 SETUP_SYNTAX_TABLE (from, count);
8489eb67
RS
2489 while (count > 0)
2490 {
8489eb67
RS
2491 while (from < stop)
2492 {
c5683ceb 2493 int comstart_first, prefix, syntax, other_syntax;
195d1361 2494 UPDATE_SYNTAX_TABLE_FORWARD (from);
b7dbcc19 2495 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
c5683ceb 2496 syntax = SYNTAX_WITH_FLAGS (c);
bd25db08 2497 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
c5683ceb
SM
2498 comstart_first = SYNTAX_FLAGS_COMSTART_FIRST (syntax);
2499 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
2500 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2501 prefix = SYNTAX_FLAGS_PREFIX (syntax);
7bf5e9e4
RS
2502 if (depth == min_depth)
2503 last_good = from;
6a140a74 2504 INC_BOTH (from, from_byte);
195d1361 2505 UPDATE_SYNTAX_TABLE_FORWARD (from);
f902a008 2506 if (from < stop && comstart_first
327719ee 2507 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
c5683ceb
SM
2508 other_syntax = SYNTAX_WITH_FLAGS (c),
2509 SYNTAX_FLAGS_COMSTART_SECOND (other_syntax))
8489eb67 2510 && parse_sexp_ignore_comments)
e5d4f4dc 2511 {
7d0393cf 2512 /* we have encountered a comment start sequence and we
195d1361 2513 are ignoring all text inside comments. We must record
e5d4f4dc
RS
2514 the comment style this sequence begins so that later,
2515 only a comment end of the same style actually ends
2516 the comment section */
2517 code = Scomment;
c5683ceb
SM
2518 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2519 comnested
2520 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
6a140a74 2521 INC_BOTH (from, from_byte);
f902a008 2522 UPDATE_SYNTAX_TABLE_FORWARD (from);
e5d4f4dc 2523 }
7d0393cf 2524
f902a008 2525 if (prefix)
8489eb67
RS
2526 continue;
2527
0220c518 2528 switch (SWITCH_ENUM_CAST (code))
8489eb67
RS
2529 {
2530 case Sescape:
2531 case Scharquote:
8b8bf8e6
SM
2532 if (from == stop)
2533 goto lose;
6a140a74 2534 INC_BOTH (from, from_byte);
8489eb67
RS
2535 /* treat following character as a word constituent */
2536 case Sword:
2537 case Ssymbol:
2538 if (depth || !sexpflag) break;
195d1361 2539 /* This word counts as a sexp; return at end of it. */
8489eb67
RS
2540 while (from < stop)
2541 {
195d1361 2542 UPDATE_SYNTAX_TABLE_FORWARD (from);
7c7ca3d2
RS
2543
2544 /* Some compilers can't handle this inside the switch. */
b7dbcc19 2545 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
bd25db08 2546 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
7c7ca3d2 2547 switch (temp)
8489eb67
RS
2548 {
2549 case Scharquote:
2550 case Sescape:
6a140a74 2551 INC_BOTH (from, from_byte);
8b8bf8e6
SM
2552 if (from == stop)
2553 goto lose;
8489eb67
RS
2554 break;
2555 case Sword:
2556 case Ssymbol:
2557 case Squote:
2558 break;
2559 default:
2560 goto done;
2561 }
6a140a74 2562 INC_BOTH (from, from_byte);
8489eb67
RS
2563 }
2564 goto done;
2565
195d1361 2566 case Scomment_fence:
95ff8dfc
RS
2567 comstyle = ST_COMMENT_STYLE;
2568 /* FALLTHROUGH */
2569 case Scomment:
8489eb67 2570 if (!parse_sexp_ignore_comments) break;
95ff8dfc
RS
2571 UPDATE_SYNTAX_TABLE_FORWARD (from);
2572 found = forw_comment (from, from_byte, stop,
2573 comnested, comstyle, 0,
2574 &out_charpos, &out_bytepos, &dummy);
2575 from = out_charpos, from_byte = out_bytepos;
2576 if (!found)
8489eb67 2577 {
95ff8dfc
RS
2578 if (depth == 0)
2579 goto done;
2580 goto lose;
8489eb67 2581 }
95ff8dfc
RS
2582 INC_BOTH (from, from_byte);
2583 UPDATE_SYNTAX_TABLE_FORWARD (from);
8489eb67
RS
2584 break;
2585
2586 case Smath:
2587 if (!sexpflag)
2588 break;
b7dbcc19 2589 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
6a140a74
RS
2590 {
2591 INC_BOTH (from, from_byte);
2592 }
8489eb67
RS
2593 if (mathexit)
2594 {
2595 mathexit = 0;
2596 goto close1;
2597 }
2598 mathexit = 1;
2599
2600 case Sopen:
2601 if (!++depth) goto done;
2602 break;
2603
2604 case Sclose:
2605 close1:
2606 if (!--depth) goto done;
2607 if (depth < min_depth)
9471945b
KS
2608 xsignal3 (Qscan_error,
2609 build_string ("Containing expression ends prematurely"),
2610 make_number (last_good), make_number (from));
8489eb67
RS
2611 break;
2612
2613 case Sstring:
195d1361 2614 case Sstring_fence:
ef316cf0 2615 temp_pos = dec_bytepos (from_byte);
b7dbcc19 2616 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
8489eb67
RS
2617 while (1)
2618 {
8b8bf8e6
SM
2619 if (from >= stop)
2620 goto lose;
195d1361 2621 UPDATE_SYNTAX_TABLE_FORWARD (from);
b7dbcc19 2622 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
42ebfa31
SM
2623 if (code == Sstring
2624 ? (c == stringterm
2625 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
bd25db08 2626 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
195d1361 2627 break;
7c7ca3d2
RS
2628
2629 /* Some compilers can't handle this inside the switch. */
bd25db08 2630 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
7c7ca3d2 2631 switch (temp)
8489eb67
RS
2632 {
2633 case Scharquote:
2634 case Sescape:
6a140a74 2635 INC_BOTH (from, from_byte);
8489eb67 2636 }
6a140a74 2637 INC_BOTH (from, from_byte);
8489eb67 2638 }
6a140a74 2639 INC_BOTH (from, from_byte);
8489eb67
RS
2640 if (!depth && sexpflag) goto done;
2641 break;
240c43e8
SM
2642 default:
2643 /* Ignore whitespace, punctuation, quote, endcomment. */
2644 break;
8489eb67
RS
2645 }
2646 }
2647
2648 /* Reached end of buffer. Error if within object, return nil if between */
8b8bf8e6
SM
2649 if (depth)
2650 goto lose;
8489eb67
RS
2651
2652 immediate_quit = 0;
2653 return Qnil;
2654
2655 /* End of object reached */
2656 done:
2657 count--;
2658 }
2659
2660 while (count < 0)
2661 {
8489eb67
RS
2662 while (from > stop)
2663 {
c5683ceb 2664 int syntax;
6a140a74 2665 DEC_BOTH (from, from_byte);
195d1361 2666 UPDATE_SYNTAX_TABLE_BACKWARD (from);
b7dbcc19 2667 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
c5683ceb 2668 syntax= SYNTAX_WITH_FLAGS (c);
bd25db08 2669 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
7bf5e9e4
RS
2670 if (depth == min_depth)
2671 last_good = from;
7fc8191e 2672 comstyle = 0;
c5683ceb 2673 comnested = SYNTAX_FLAGS_COMMENT_NESTED (syntax);
7fc8191e 2674 if (code == Sendcomment)
c5683ceb
SM
2675 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (syntax, 0);
2676 if (from > stop && SYNTAX_FLAGS_COMEND_SECOND (syntax)
1674d9a2 2677 && prev_char_comend_first (from, from_byte)
8489eb67 2678 && parse_sexp_ignore_comments)
e5d4f4dc 2679 {
f902a008 2680 /* We must record the comment style encountered so that
e5d4f4dc 2681 later, we can match only the proper comment begin
f902a008 2682 sequence of the same style. */
01f44d5a 2683 int c2, other_syntax;
6a140a74 2684 DEC_BOTH (from, from_byte);
f902a008
RS
2685 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2686 code = Sendcomment;
01f44d5a
PE
2687 c2 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2688 other_syntax = SYNTAX_WITH_FLAGS (c2);
c5683ceb
SM
2689 comstyle = SYNTAX_FLAGS_COMMENT_STYLE (other_syntax, syntax);
2690 comnested
2691 = comnested || SYNTAX_FLAGS_COMMENT_NESTED (other_syntax);
e5d4f4dc 2692 }
7d0393cf 2693
9828a477 2694 /* Quoting turns anything except a comment-ender
e6365855 2695 into a word character. Note that this cannot be true
f902a008 2696 if we decremented FROM in the if-statement above. */
9828a477 2697 if (code != Sendcomment && char_quoted (from, from_byte))
240c43e8
SM
2698 {
2699 DEC_BOTH (from, from_byte);
2700 code = Sword;
2701 }
c5683ceb 2702 else if (SYNTAX_FLAGS_PREFIX (syntax))
8489eb67
RS
2703 continue;
2704
9828a477 2705 switch (SWITCH_ENUM_CAST (code))
8489eb67
RS
2706 {
2707 case Sword:
2708 case Ssymbol:
9828a477
KH
2709 case Sescape:
2710 case Scharquote:
8489eb67 2711 if (depth || !sexpflag) break;
195d1361
RS
2712 /* This word counts as a sexp; count object finished
2713 after passing it. */
8489eb67
RS
2714 while (from > stop)
2715 {
6a140a74 2716 temp_pos = from_byte;
4b4deea2 2717 if (! NILP (BVAR (current_buffer, enable_multibyte_characters)))
ef316cf0
RS
2718 DEC_POS (temp_pos);
2719 else
2720 temp_pos--;
6a140a74 2721 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
b7dbcc19 2722 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
bd25db08 2723 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
9828a477
KH
2724 /* Don't allow comment-end to be quoted. */
2725 if (temp_code == Sendcomment)
2726 goto done2;
6a140a74 2727 quoted = char_quoted (from - 1, temp_pos);
8489eb67 2728 if (quoted)
93da5fff 2729 {
6a140a74 2730 DEC_BOTH (from, from_byte);
ef316cf0 2731 temp_pos = dec_bytepos (temp_pos);
6a140a74 2732 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
93da5fff 2733 }
b7dbcc19 2734 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
bd25db08 2735 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
93da5fff
KH
2736 if (! (quoted || temp_code == Sword
2737 || temp_code == Ssymbol
2738 || temp_code == Squote))
8489eb67 2739 goto done2;
6a140a74 2740 DEC_BOTH (from, from_byte);
8489eb67
RS
2741 }
2742 goto done2;
2743
2744 case Smath:
2745 if (!sexpflag)
2746 break;
ef316cf0 2747 temp_pos = dec_bytepos (from_byte);
6a140a74 2748 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
b7dbcc19 2749 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
6a140a74 2750 DEC_BOTH (from, from_byte);
8489eb67
RS
2751 if (mathexit)
2752 {
2753 mathexit = 0;
2754 goto open2;
2755 }
2756 mathexit = 1;
2757
2758 case Sclose:
2759 if (!++depth) goto done2;
2760 break;
2761
2762 case Sopen:
2763 open2:
2764 if (!--depth) goto done2;
2765 if (depth < min_depth)
9471945b
KS
2766 xsignal3 (Qscan_error,
2767 build_string ("Containing expression ends prematurely"),
2768 make_number (last_good), make_number (from));
8489eb67
RS
2769 break;
2770
2771 case Sendcomment:
2772 if (!parse_sexp_ignore_comments)
2773 break;
95ff8dfc 2774 found = back_comment (from, from_byte, stop, comnested, comstyle,
6a140a74 2775 &out_charpos, &out_bytepos);
1aa963c8
SM
2776 /* FIXME: if found == -1, then it really wasn't a comment-end.
2777 For single-char Sendcomment, we can't do much about it apart
2778 from skipping the char.
2779 For 2-char endcomments, we could try again, taking both
2780 chars as separate entities, but it's a lot of trouble
2781 for very little gain, so we don't bother either. -sm */
6a140a74
RS
2782 if (found != -1)
2783 from = out_charpos, from_byte = out_bytepos;
8489eb67
RS
2784 break;
2785
195d1361
RS
2786 case Scomment_fence:
2787 case Sstring_fence:
2788 while (1)
2789 {
8b8bf8e6
SM
2790 if (from == stop)
2791 goto lose;
6b61353c 2792 DEC_BOTH (from, from_byte);
195d1361 2793 UPDATE_SYNTAX_TABLE_BACKWARD (from);
7d0393cf 2794 if (!char_quoted (from, from_byte)
b7dbcc19 2795 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
bd25db08 2796 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
195d1361
RS
2797 break;
2798 }
2799 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2800 break;
7d0393cf 2801
8489eb67 2802 case Sstring:
b7dbcc19 2803 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
8489eb67
RS
2804 while (1)
2805 {
8b8bf8e6
SM
2806 if (from == stop)
2807 goto lose;
6b61353c
KH
2808 DEC_BOTH (from, from_byte);
2809 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2810 if (!char_quoted (from, from_byte)
2811 && (stringterm
2812 == (c = FETCH_CHAR_AS_MULTIBYTE (from_byte)))
42ebfa31 2813 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
8489eb67 2814 break;
8489eb67 2815 }
8489eb67
RS
2816 if (!depth && sexpflag) goto done2;
2817 break;
240c43e8
SM
2818 default:
2819 /* Ignore whitespace, punctuation, quote, endcomment. */
2820 break;
8489eb67
RS
2821 }
2822 }
2823
2824 /* Reached start of buffer. Error if within object, return nil if between */
8b8bf8e6
SM
2825 if (depth)
2826 goto lose;
8489eb67
RS
2827
2828 immediate_quit = 0;
2829 return Qnil;
2830
2831 done2:
2832 count++;
2833 }
2834
2835
2836 immediate_quit = 0;
1e142fb7 2837 XSETFASTINT (val, from);
8489eb67
RS
2838 return val;
2839
2840 lose:
9471945b
KS
2841 xsignal3 (Qscan_error,
2842 build_string ("Unbalanced parentheses"),
2843 make_number (last_good), make_number (from));
8489eb67
RS
2844}
2845
8489eb67 2846DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
fdb82f93 2847 doc: /* Scan from character number FROM by COUNT lists.
7b926f3f
CY
2848Scan forward if COUNT is positive, backward if COUNT is negative.
2849Return the character number of the position thus found.
2850
2851A \"list", in this context, refers to a balanced parenthetical
2852grouping, as determined by the syntax table.
fdb82f93 2853
7b926f3f
CY
2854If DEPTH is nonzero, treat that as the nesting depth of the starting
2855point (i.e. the starting point is DEPTH parentheses deep). This
2856function scans over parentheses until the depth goes to zero COUNT
2857times. Hence, positive DEPTH moves out that number of levels of
221cd413 2858parentheses, while negative DEPTH moves to a deeper level.
fdb82f93
PJ
2859
2860Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2861
7b926f3f
CY
2862If we reach the beginning or end of the accessible part of the buffer
2863before we have scanned over COUNT lists, return nil if the depth at
2864that point is zero, and signal a error if the depth is nonzero. */)
5842a27b 2865 (Lisp_Object from, Lisp_Object count, Lisp_Object depth)
8489eb67 2866{
b7826503
PJ
2867 CHECK_NUMBER (from);
2868 CHECK_NUMBER (count);
2869 CHECK_NUMBER (depth);
8489eb67
RS
2870
2871 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2872}
2873
2874DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
fdb82f93
PJ
2875 doc: /* Scan from character number FROM by COUNT balanced expressions.
2876If COUNT is negative, scan backwards.
2877Returns the character number of the position thus found.
2878
2879Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2880
2881If the beginning or end of (the accessible part of) the buffer is reached
2882in the middle of a parenthetical grouping, an error is signaled.
2883If the beginning or end is reached between groupings
2884but before count is used up, nil is returned. */)
5842a27b 2885 (Lisp_Object from, Lisp_Object count)
8489eb67 2886{
b7826503
PJ
2887 CHECK_NUMBER (from);
2888 CHECK_NUMBER (count);
8489eb67
RS
2889
2890 return scan_lists (XINT (from), XINT (count), 0, 1);
2891}
2892
2893DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
fdb82f93
PJ
2894 0, 0, 0,
2895 doc: /* Move point backward over any number of chars with prefix syntax.
2896This includes chars with "quote" or "prefix" syntax (' or p). */)
5842a27b 2897 (void)
8489eb67 2898{
d311d28c
PE
2899 ptrdiff_t beg = BEGV;
2900 ptrdiff_t opoint = PT;
2901 ptrdiff_t opoint_byte = PT_BYTE;
2902 ptrdiff_t pos = PT;
2903 ptrdiff_t pos_byte = PT_BYTE;
93da5fff 2904 int c;
93da5fff 2905
7d0393cf 2906 if (pos <= beg)
195d1361 2907 {
f902a008
RS
2908 SET_PT_BOTH (opoint, opoint_byte);
2909
2910 return Qnil;
195d1361 2911 }
8489eb67 2912
f902a008
RS
2913 SETUP_SYNTAX_TABLE (pos, -1);
2914
6a140a74
RS
2915 DEC_BOTH (pos, pos_byte);
2916
1fd3172d 2917 while (!char_quoted (pos, pos_byte)
195d1361 2918 /* Previous statement updates syntax table. */
b7dbcc19 2919 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
93da5fff
KH
2920 || SYNTAX_PREFIX (c)))
2921 {
1fd3172d
RS
2922 opoint = pos;
2923 opoint_byte = pos_byte;
2924
2925 if (pos + 1 > beg)
2926 DEC_BOTH (pos, pos_byte);
93da5fff 2927 }
8489eb67 2928
6a140a74 2929 SET_PT_BOTH (opoint, opoint_byte);
8489eb67
RS
2930
2931 return Qnil;
2932}
2933\f
6a140a74 2934/* Parse forward from FROM / FROM_BYTE to END,
e5d4f4dc
RS
2935 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2936 and return a description of the state of the parse at END.
c81a3712 2937 If STOPBEFORE is nonzero, stop at the start of an atom.
644ea4df
RS
2938 If COMMENTSTOP is 1, stop at the start of a comment.
2939 If COMMENTSTOP is -1, stop at the start or end of a comment,
2940 after the beginning of a string, or after the end of a string. */
8489eb67 2941
340f92b5 2942static void
d5a3eaaf 2943scan_sexps_forward (struct lisp_parse_state *stateptr,
d311d28c
PE
2944 ptrdiff_t from, ptrdiff_t from_byte, ptrdiff_t end,
2945 EMACS_INT targetdepth, int stopbefore,
d5a3eaaf 2946 Lisp_Object oldstate, int commentstop)
8489eb67
RS
2947{
2948 struct lisp_parse_state state;
2949
2950 register enum syntaxcode code;
95ff8dfc
RS
2951 int c1;
2952 int comnested;
d311d28c 2953 struct level { ptrdiff_t last, prev; };
8489eb67
RS
2954 struct level levelstart[100];
2955 register struct level *curlevel = levelstart;
2956 struct level *endlevel = levelstart + 100;
d311d28c 2957 register EMACS_INT depth; /* Paren depth of current scanning location.
8489eb67
RS
2958 level - levelstart equals this except
2959 when the depth becomes negative. */
d311d28c 2960 EMACS_INT mindepth; /* Lowest DEPTH value seen. */
8489eb67
RS
2961 int start_quoted = 0; /* Nonzero means starting after a char quote */
2962 Lisp_Object tem;
d311d28c
PE
2963 ptrdiff_t prev_from; /* Keep one character before FROM. */
2964 ptrdiff_t prev_from_byte;
1fd3172d 2965 int prev_from_syntax;
195d1361
RS
2966 int boundary_stop = commentstop == -1;
2967 int nofence;
95ff8dfc 2968 int found;
d311d28c 2969 ptrdiff_t out_bytepos, out_charpos;
7c7ca3d2 2970 int temp;
93da5fff
KH
2971
2972 prev_from = from;
6a140a74
RS
2973 prev_from_byte = from_byte;
2974 if (from != BEGV)
2975 DEC_BOTH (prev_from, prev_from_byte);
93da5fff
KH
2976
2977 /* Use this macro instead of `from++'. */
6a140a74
RS
2978#define INC_FROM \
2979do { prev_from = from; \
2980 prev_from_byte = from_byte; \
327719ee 2981 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
ab229fdd 2982 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
ef316cf0 2983 INC_BOTH (from, from_byte); \
3ceb4629
SM
2984 if (from < end) \
2985 UPDATE_SYNTAX_TABLE_FORWARD (from); \
6a140a74 2986 } while (0)
8489eb67
RS
2987
2988 immediate_quit = 1;
2989 QUIT;
2990
265a9e55 2991 if (NILP (oldstate))
8489eb67
RS
2992 {
2993 depth = 0;
2994 state.instring = -1;
2995 state.incomment = 0;
195d1361
RS
2996 state.comstyle = 0; /* comment style a by default. */
2997 state.comstr_start = -1; /* no comment/string seen. */
8489eb67
RS
2998 }
2999 else
3000 {
3001 tem = Fcar (oldstate);
265a9e55 3002 if (!NILP (tem))
8489eb67
RS
3003 depth = XINT (tem);
3004 else
3005 depth = 0;
3006
3007 oldstate = Fcdr (oldstate);
3008 oldstate = Fcdr (oldstate);
3009 oldstate = Fcdr (oldstate);
3010 tem = Fcar (oldstate);
195d1361 3011 /* Check whether we are inside string_fence-style string: */
7d0393cf 3012 state.instring = (!NILP (tem)
d311d28c 3013 ? (CHARACTERP (tem) ? XFASTINT (tem) : ST_STRING_STYLE)
e76f1c44 3014 : -1);
8489eb67
RS
3015
3016 oldstate = Fcdr (oldstate);
3017 tem = Fcar (oldstate);
e76f1c44
GM
3018 state.incomment = (!NILP (tem)
3019 ? (INTEGERP (tem) ? XINT (tem) : -1)
95ff8dfc 3020 : 0);
8489eb67
RS
3021
3022 oldstate = Fcdr (oldstate);
3023 tem = Fcar (oldstate);
265a9e55 3024 start_quoted = !NILP (tem);
e5d4f4dc 3025
95ff8dfc 3026 /* if the eighth element of the list is nil, we are in comment
195d1361
RS
3027 style a. If it is non-nil, we are in comment style b */
3028 oldstate = Fcdr (oldstate);
e5d4f4dc 3029 oldstate = Fcdr (oldstate);
195d1361 3030 tem = Fcar (oldstate);
c5683ceb
SM
3031 state.comstyle = (NILP (tem)
3032 ? 0
d311d28c
PE
3033 : (RANGED_INTEGERP (0, tem, ST_COMMENT_STYLE)
3034 ? XINT (tem)
3035 : ST_COMMENT_STYLE));
195d1361 3036
e5d4f4dc 3037 oldstate = Fcdr (oldstate);
e5d4f4dc 3038 tem = Fcar (oldstate);
d311d28c
PE
3039 state.comstr_start =
3040 RANGED_INTEGERP (PTRDIFF_MIN, tem, PTRDIFF_MAX) ? XINT (tem) : -1;
1a102a58
RS
3041 oldstate = Fcdr (oldstate);
3042 tem = Fcar (oldstate);
3043 while (!NILP (tem)) /* >= second enclosing sexps. */
3044 {
d311d28c
PE
3045 Lisp_Object temhd = Fcar (tem);
3046 if (RANGED_INTEGERP (PTRDIFF_MIN, temhd, PTRDIFF_MAX))
3047 curlevel->last = XINT (temhd);
1a102a58 3048 if (++curlevel == endlevel)
02010917 3049 curlevel--; /* error ("Nesting too deep for parser"); */
1a102a58
RS
3050 curlevel->prev = -1;
3051 curlevel->last = -1;
3052 tem = Fcdr (tem);
3053 }
8489eb67
RS
3054 }
3055 state.quoted = 0;
3056 mindepth = depth;
3057
3058 curlevel->prev = -1;
3059 curlevel->last = -1;
3060
326b283b 3061 SETUP_SYNTAX_TABLE (prev_from, 1);
ab229fdd
AS
3062 temp = FETCH_CHAR (prev_from_byte);
3063 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
3ceb4629 3064 UPDATE_SYNTAX_TABLE_FORWARD (from);
326b283b 3065
195d1361 3066 /* Enter the loop at a place appropriate for initial state. */
8489eb67 3067
326b283b
RS
3068 if (state.incomment)
3069 goto startincomment;
8489eb67
RS
3070 if (state.instring >= 0)
3071 {
195d1361 3072 nofence = state.instring != ST_STRING_STYLE;
326b283b
RS
3073 if (start_quoted)
3074 goto startquotedinstring;
8489eb67
RS
3075 goto startinstring;
3076 }
326b283b
RS
3077 else if (start_quoted)
3078 goto startquoted;
1fd3172d 3079
8489eb67
RS
3080 while (from < end)
3081 {
c5683ceb 3082 int syntax;
93da5fff 3083 INC_FROM;
1fd3172d 3084 code = prev_from_syntax & 0xff;
4c920633 3085
c5d4b96f
SM
3086 if (from < end
3087 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3088 && (c1 = FETCH_CHAR (from_byte),
c5683ceb
SM
3089 syntax = SYNTAX_WITH_FLAGS (c1),
3090 SYNTAX_FLAGS_COMSTART_SECOND (syntax)))
c5d4b96f
SM
3091 /* Duplicate code to avoid a complex if-expression
3092 which causes trouble for the SGI compiler. */
95ff8dfc 3093 {
c5d4b96f
SM
3094 /* Record the comment style we have entered so that only
3095 the comment-end sequence of the same style actually
3096 terminates the comment section. */
c5683ceb
SM
3097 state.comstyle
3098 = SYNTAX_FLAGS_COMMENT_STYLE (syntax, prev_from_syntax);
c5d4b96f 3099 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
c5683ceb 3100 comnested = comnested || SYNTAX_FLAGS_COMMENT_NESTED (syntax);
c5d4b96f 3101 state.incomment = comnested ? 1 : -1;
95ff8dfc 3102 state.comstr_start = prev_from;
c5d4b96f
SM
3103 INC_FROM;
3104 code = Scomment;
95ff8dfc 3105 }
4c920633 3106 else if (code == Scomment_fence)
e5d4f4dc
RS
3107 {
3108 /* Record the comment style we have entered so that only
3109 the comment-end sequence of the same style actually
3110 terminates the comment section. */
95ff8dfc
RS
3111 state.comstyle = ST_COMMENT_STYLE;
3112 state.incomment = -1;
195d1361 3113 state.comstr_start = prev_from;
e5d4f4dc 3114 code = Scomment;
e5d4f4dc 3115 }
c5d4b96f
SM
3116 else if (code == Scomment)
3117 {
c5683ceb 3118 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax, 0);
c5d4b96f
SM
3119 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3120 1 : -1);
3121 state.comstr_start = prev_from;
3122 }
e5d4f4dc 3123
1fd3172d 3124 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
8489eb67 3125 continue;
0220c518 3126 switch (SWITCH_ENUM_CAST (code))
8489eb67
RS
3127 {
3128 case Sescape:
3129 case Scharquote:
3130 if (stopbefore) goto stop; /* this arg means stop at sexp start */
93da5fff 3131 curlevel->last = prev_from;
8489eb67
RS
3132 startquoted:
3133 if (from == end) goto endquoted;
93da5fff 3134 INC_FROM;
8489eb67
RS
3135 goto symstarted;
3136 /* treat following character as a word constituent */
3137 case Sword:
3138 case Ssymbol:
3139 if (stopbefore) goto stop; /* this arg means stop at sexp start */
93da5fff 3140 curlevel->last = prev_from;
8489eb67
RS
3141 symstarted:
3142 while (from < end)
3143 {
7c7ca3d2 3144 /* Some compilers can't handle this inside the switch. */
327719ee 3145 temp = FETCH_CHAR_AS_MULTIBYTE (from_byte);
ab229fdd 3146 temp = SYNTAX (temp);
7c7ca3d2 3147 switch (temp)
8489eb67
RS
3148 {
3149 case Scharquote:
3150 case Sescape:
93da5fff 3151 INC_FROM;
8489eb67
RS
3152 if (from == end) goto endquoted;
3153 break;
3154 case Sword:
3155 case Ssymbol:
3156 case Squote:
3157 break;
3158 default:
3159 goto symdone;
3160 }
93da5fff 3161 INC_FROM;
8489eb67
RS
3162 }
3163 symdone:
3164 curlevel->prev = curlevel->last;
3165 break;
3166
240c43e8 3167 case Scomment_fence: /* Can't happen because it's handled above. */
8489eb67 3168 case Scomment:
195d1361 3169 if (commentstop || boundary_stop) goto done;
d355bd8a
SM
3170 startincomment:
3171 /* The (from == BEGV) test was to enter the loop in the middle so
95ff8dfc 3172 that we find a 2-char comment ender even if we start in the
e6365855 3173 middle of it. We don't want to do that if we're just at the
d355bd8a 3174 beginning of the comment (think of (*) ... (*)). */
95ff8dfc
RS
3175 found = forw_comment (from, from_byte, end,
3176 state.incomment, state.comstyle,
e6365855
SM
3177 (from == BEGV || from < state.comstr_start + 3)
3178 ? 0 : prev_from_syntax,
95ff8dfc
RS
3179 &out_charpos, &out_bytepos, &state.incomment);
3180 from = out_charpos; from_byte = out_bytepos;
3181 /* Beware! prev_from and friends are invalid now.
3182 Luckily, the `done' doesn't use them and the INC_FROM
3183 sets them to a sane value without looking at them. */
3184 if (!found) goto done;
d355bd8a 3185 INC_FROM;
8489eb67 3186 state.incomment = 0;
e5d4f4dc 3187 state.comstyle = 0; /* reset the comment style */
195d1361 3188 if (boundary_stop) goto done;
8489eb67
RS
3189 break;
3190
3191 case Sopen:
3192 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3193 depth++;
3194 /* curlevel++->last ran into compiler bug on Apollo */
93da5fff 3195 curlevel->last = prev_from;
8489eb67 3196 if (++curlevel == endlevel)
02010917 3197 curlevel--; /* error ("Nesting too deep for parser"); */
8489eb67
RS
3198 curlevel->prev = -1;
3199 curlevel->last = -1;
30844415 3200 if (targetdepth == depth) goto done;
8489eb67
RS
3201 break;
3202
3203 case Sclose:
3204 depth--;
3205 if (depth < mindepth)
3206 mindepth = depth;
3207 if (curlevel != levelstart)
3208 curlevel--;
3209 curlevel->prev = curlevel->last;
30844415 3210 if (targetdepth == depth) goto done;
8489eb67
RS
3211 break;
3212
3213 case Sstring:
195d1361
RS
3214 case Sstring_fence:
3215 state.comstr_start = from - 1;
8489eb67 3216 if (stopbefore) goto stop; /* this arg means stop at sexp start */
93da5fff 3217 curlevel->last = prev_from;
7d0393cf 3218 state.instring = (code == Sstring
b7dbcc19 3219 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
195d1361
RS
3220 : ST_STRING_STYLE);
3221 if (boundary_stop) goto done;
8489eb67 3222 startinstring:
195d1361 3223 {
644ea4df 3224 nofence = state.instring != ST_STRING_STYLE;
7d0393cf 3225
644ea4df
RS
3226 while (1)
3227 {
3228 int c;
195d1361 3229
644ea4df 3230 if (from >= end) goto done;
b7dbcc19 3231 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
7c7ca3d2
RS
3232 /* Some compilers can't handle this inside the switch. */
3233 temp = SYNTAX (c);
ccf89641
KH
3234
3235 /* Check TEMP here so that if the char has
3236 a syntax-table property which says it is NOT
3237 a string character, it does not end the string. */
3238 if (nofence && c == state.instring && temp == Sstring)
3239 break;
3240
7c7ca3d2 3241 switch (temp)
644ea4df
RS
3242 {
3243 case Sstring_fence:
3244 if (!nofence) goto string_end;
3245 break;
3246 case Scharquote:
3247 case Sescape:
3248 INC_FROM;
3249 startquotedinstring:
3250 if (from >= end) goto endquoted;
195d1361 3251 }
644ea4df
RS
3252 INC_FROM;
3253 }
195d1361
RS
3254 }
3255 string_end:
8489eb67
RS
3256 state.instring = -1;
3257 curlevel->prev = curlevel->last;
93da5fff 3258 INC_FROM;
195d1361 3259 if (boundary_stop) goto done;
8489eb67
RS
3260 break;
3261
3262 case Smath:
240c43e8
SM
3263 /* FIXME: We should do something with it. */
3264 break;
3265 default:
3266 /* Ignore whitespace, punctuation, quote, endcomment. */
8489eb67
RS
3267 break;
3268 }
3269 }
3270 goto done;
3271
3272 stop: /* Here if stopping before start of sexp. */
93da5fff 3273 from = prev_from; /* We have just fetched the char that starts it; */
8489eb67
RS
3274 goto done; /* but return the position before it. */
3275
3276 endquoted:
3277 state.quoted = 1;
3278 done:
3279 state.depth = depth;
3280 state.mindepth = mindepth;
3281 state.thislevelstart = curlevel->prev;
3282 state.prevlevelstart
3283 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3284 state.location = from;
1a102a58 3285 state.levelstarts = Qnil;
eb4d412d
PE
3286 while (curlevel > levelstart)
3287 state.levelstarts = Fcons (make_number ((--curlevel)->last),
3288 state.levelstarts);
8489eb67
RS
3289 immediate_quit = 0;
3290
e5d4f4dc 3291 *stateptr = state;
8489eb67
RS
3292}
3293
c81a3712 3294DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
fdb82f93
PJ
3295 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3296Parsing stops at TO or when certain criteria are met;
3297 point is set to where parsing stops.
3298If fifth arg OLDSTATE is omitted or nil,
3299 parsing assumes that FROM is the beginning of a function.
0e6228bc 3300Value is a list of elements describing final state of parsing:
fdb82f93
PJ
3301 0. depth in parens.
3302 1. character address of start of innermost containing list; nil if none.
3303 2. character address of start of last complete sexp terminated.
3304 3. non-nil if inside a string.
3305 (it is the character that will terminate the string,
3306 or t if the string should be terminated by a generic string delimiter.)
7d0393cf 3307 4. nil if outside a comment, t if inside a non-nestable comment,
fdb82f93
PJ
3308 else an integer (the current comment nesting).
3309 5. t if following a quote character.
3310 6. the minimum paren-depth encountered during this scan.
c5683ceb 3311 7. style of comment, if any.
fdb82f93
PJ
3312 8. character address of start of comment or string; nil if not in one.
3313 9. Intermediate data for continuation of parsing (subject to change).
3314If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3315in parentheses becomes equal to TARGETDEPTH.
3316Fourth arg STOPBEFORE non-nil means stop when come to
3317 any character that starts a sexp.
0e6228bc 3318Fifth arg OLDSTATE is a list like what this function returns.
fdb82f93 3319 It is used to initialize the state of the parse. Elements number 1, 2, 6
d311d28c 3320 are ignored.
fdb82f93
PJ
3321Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3322 If it is symbol `syntax-table', stop after the start of a comment or a
3323 string, or after end of a comment or a string. */)
5842a27b 3324 (Lisp_Object from, Lisp_Object to, Lisp_Object targetdepth, Lisp_Object stopbefore, Lisp_Object oldstate, Lisp_Object commentstop)
8489eb67
RS
3325{
3326 struct lisp_parse_state state;
d311d28c 3327 EMACS_INT target;
8489eb67 3328
265a9e55 3329 if (!NILP (targetdepth))
8489eb67 3330 {
b7826503 3331 CHECK_NUMBER (targetdepth);
8489eb67
RS
3332 target = XINT (targetdepth);
3333 }
3334 else
d311d28c 3335 target = TYPE_MINIMUM (EMACS_INT); /* We won't reach this depth */
8489eb67
RS
3336
3337 validate_region (&from, &to);
6a140a74
RS
3338 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3339 XINT (to),
c81a3712 3340 target, !NILP (stopbefore), oldstate,
7d0393cf 3341 (NILP (commentstop)
195d1361 3342 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
8489eb67
RS
3343
3344 SET_PT (state.location);
7d0393cf 3345
8489eb67 3346 return Fcons (make_number (state.depth),
c5683ceb
SM
3347 Fcons (state.prevlevelstart < 0
3348 ? Qnil : make_number (state.prevlevelstart),
3349 Fcons (state.thislevelstart < 0
3350 ? Qnil : make_number (state.thislevelstart),
7d0393cf
JB
3351 Fcons (state.instring >= 0
3352 ? (state.instring == ST_STRING_STYLE
195d1361 3353 ? Qt : make_number (state.instring)) : Qnil,
95ff8dfc
RS
3354 Fcons (state.incomment < 0 ? Qt :
3355 (state.incomment == 0 ? Qnil :
3356 make_number (state.incomment)),
8489eb67 3357 Fcons (state.quoted ? Qt : Qnil,
bff37d2a 3358 Fcons (make_number (state.mindepth),
7d0393cf 3359 Fcons ((state.comstyle
bff37d2a 3360 ? (state.comstyle == ST_COMMENT_STYLE
c5683ceb
SM
3361 ? Qsyntax_table
3362 : make_number (state.comstyle))
3363 : Qnil),
0beaf54f
DL
3364 Fcons (((state.incomment
3365 || (state.instring >= 0))
bff37d2a
RS
3366 ? make_number (state.comstr_start)
3367 : Qnil),
1a102a58 3368 Fcons (state.levelstarts, Qnil))))))))));
8489eb67
RS
3369}
3370\f
dfcf069d 3371void
971de7fb 3372init_syntax_once (void)
8489eb67 3373{
78f9a1f7 3374 register int i, c;
8ea151b2 3375 Lisp_Object temp;
8489eb67 3376
5ebaddf5 3377 /* This has to be done here, before we call Fmake_char_table. */
cd3520a4 3378 DEFSYM (Qsyntax_table, "syntax-table");
5ebaddf5 3379
d67b4f80 3380 /* Intern_C_String this now in case it isn't already done.
5ebaddf5
RS
3381 Setting this variable twice is harmless.
3382 But don't staticpro it here--that is done in alloc.c. */
d67b4f80 3383 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
5ebaddf5 3384
93da5fff 3385 /* Create objects which can be shared among syntax tables. */
42ebfa31 3386 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
77b37c05 3387 for (i = 0; i < ASIZE (Vsyntax_code_object); i++)
28be1ada 3388 ASET (Vsyntax_code_object, i, Fcons (make_number (i), Qnil));
93da5fff 3389
5ebaddf5
RS
3390 /* Now we are ready to set up this property, so we can
3391 create syntax tables. */
3392 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3393
28be1ada 3394 temp = AREF (Vsyntax_code_object, (int) Swhitespace);
8489eb67 3395
5ebaddf5 3396 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
8489eb67 3397
aa7b08b4 3398 /* Control characters should not be whitespace. */
28be1ada 3399 temp = AREF (Vsyntax_code_object, (int) Spunct);
aa7b08b4
RS
3400 for (i = 0; i <= ' ' - 1; i++)
3401 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3402 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3403
3404 /* Except that a few really are whitespace. */
28be1ada 3405 temp = AREF (Vsyntax_code_object, (int) Swhitespace);
aa7b08b4
RS
3406 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3407 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3408 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3409 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3410 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3411
28be1ada 3412 temp = AREF (Vsyntax_code_object, (int) Sword);
8489eb67 3413 for (i = 'a'; i <= 'z'; i++)
8ea151b2 3414 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
8489eb67 3415 for (i = 'A'; i <= 'Z'; i++)
8ea151b2 3416 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
8489eb67 3417 for (i = '0'; i <= '9'; i++)
8ea151b2
RS
3418 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3419
3420 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3421 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3422
3423 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3424 Fcons (make_number (Sopen), make_number (')')));
3425 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3426 Fcons (make_number (Sclose), make_number ('(')));
3427 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3428 Fcons (make_number (Sopen), make_number (']')));
3429 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3430 Fcons (make_number (Sclose), make_number ('[')));
3431 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3432 Fcons (make_number (Sopen), make_number ('}')));
3433 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3434 Fcons (make_number (Sclose), make_number ('{')));
3435 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3436 Fcons (make_number ((int) Sstring), Qnil));
3437 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3438 Fcons (make_number ((int) Sescape), Qnil));
3439
28be1ada 3440 temp = AREF (Vsyntax_code_object, (int) Ssymbol);
8489eb67 3441 for (i = 0; i < 10; i++)
78f9a1f7
KH
3442 {
3443 c = "_-+*/&|<>="[i];
3444 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3445 }
8489eb67 3446
28be1ada 3447 temp = AREF (Vsyntax_code_object, (int) Spunct);
8489eb67 3448 for (i = 0; i < 12; i++)
78f9a1f7
KH
3449 {
3450 c = ".,;:?!#@~^'`"[i];
3451 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3452 }
bd25db08
KH
3453
3454 /* All multibyte characters have syntax `word' by default. */
28be1ada 3455 temp = AREF (Vsyntax_code_object, (int) Sword);
5c7b02ab 3456 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
8489eb67
RS
3457}
3458
dfcf069d 3459void
971de7fb 3460syms_of_syntax (void)
8489eb67 3461{
cd3520a4 3462 DEFSYM (Qsyntax_table_p, "syntax-table-p");
8489eb67 3463
93da5fff
KH
3464 staticpro (&Vsyntax_code_object);
3465
a2994c46
KS
3466 staticpro (&gl_state.object);
3467 staticpro (&gl_state.global_code);
3468 staticpro (&gl_state.current_syntax_table);
3469 staticpro (&gl_state.old_prop);
3470
3471 /* Defined in regex.c */
3472 staticpro (&re_match_object);
3473
cd3520a4 3474 DEFSYM (Qscan_error, "scan-error");
7bf5e9e4 3475 Fput (Qscan_error, Qerror_conditions,
d67b4f80 3476 pure_cons (Qscan_error, pure_cons (Qerror, Qnil)));
7bf5e9e4 3477 Fput (Qscan_error, Qerror_message,
d67b4f80 3478 make_pure_c_string ("Scan error"));
7bf5e9e4 3479
29208e82 3480 DEFVAR_BOOL ("parse-sexp-ignore-comments", parse_sexp_ignore_comments,
fdb82f93 3481 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
8489eb67 3482
29208e82 3483 DEFVAR_BOOL ("parse-sexp-lookup-properties", parse_sexp_lookup_properties,
fdb82f93
PJ
3484 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3485Otherwise, that text property is simply ignored.
3486See the info node `(elisp)Syntax Properties' for a description of the
3487`syntax-table' property. */);
195d1361 3488
8489eb67 3489 words_include_escapes = 0;
29208e82 3490 DEFVAR_BOOL ("words-include-escapes", words_include_escapes,
fdb82f93 3491 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
8489eb67 3492
29208e82 3493 DEFVAR_BOOL ("multibyte-syntax-as-symbol", multibyte_syntax_as_symbol,
fdb82f93 3494 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
bd25db08
KH
3495 multibyte_syntax_as_symbol = 0;
3496
f4ed767f 3497 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
29208e82 3498 open_paren_in_column_0_is_defun_start,
fb7ada5f 3499 doc: /* Non-nil means an open paren in column 0 denotes the start of a defun. */);
f4ed767f
GM
3500 open_paren_in_column_0_is_defun_start = 1;
3501
8f924df7
KH
3502
3503 DEFVAR_LISP ("find-word-boundary-function-table",
29208e82 3504 Vfind_word_boundary_function_table,
869bb237 3505 doc: /*
8f924df7 3506Char table of functions to search for the word boundary.
869bb237
KH
3507Each function is called with two arguments; POS and LIMIT.
3508POS and LIMIT are character positions in the current buffer.
3509
3510If POS is less than LIMIT, POS is at the first character of a word,
3511and the return value of a function is a position after the last
3512character of that word.
3513
3514If POS is not less than LIMIT, POS is at the last character of a word,
3515and the return value of a function is a position at the first
3516character of that word.
3517
3518In both cases, LIMIT bounds the search. */);
8f924df7 3519 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
869bb237 3520
8489eb67
RS
3521 defsubr (&Ssyntax_table_p);
3522 defsubr (&Ssyntax_table);
3523 defsubr (&Sstandard_syntax_table);
3524 defsubr (&Scopy_syntax_table);
3525 defsubr (&Sset_syntax_table);
3526 defsubr (&Schar_syntax);
beefa22e 3527 defsubr (&Smatching_paren);
c65adb44 3528 defsubr (&Sstring_to_syntax);
8489eb67 3529 defsubr (&Smodify_syntax_entry);
62abe9cb 3530 defsubr (&Sinternal_describe_syntax_value);
8489eb67
RS
3531
3532 defsubr (&Sforward_word);
3533
195d1361
RS
3534 defsubr (&Sskip_chars_forward);
3535 defsubr (&Sskip_chars_backward);
3536 defsubr (&Sskip_syntax_forward);
3537 defsubr (&Sskip_syntax_backward);
3538
b3cfe0c8 3539 defsubr (&Sforward_comment);
8489eb67
RS
3540 defsubr (&Sscan_lists);
3541 defsubr (&Sscan_sexps);
3542 defsubr (&Sbackward_prefix_chars);
3543 defsubr (&Sparse_partial_sexp);
3544}