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