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