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