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