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