quick-install-emacs: Use more portable shell syntax
[bpt/emacs.git] / src / syntax.c
CommitLineData
8489eb67 1/* GNU Emacs routines to deal with syntax tables; also word and list parsing.
429ab54e 2 Copyright (C) 1985, 1987, 1993, 1994, 1995, 1997, 1998, 1999, 2001,
114f9c96 3 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
429ab54e 4 Free Software Foundation, Inc.
8489eb67
RS
5
6This file is part of GNU Emacs.
7
9ec0b715 8GNU Emacs is free software: you can redistribute it and/or modify
8489eb67 9it under the terms of the GNU General Public License as published by
9ec0b715
GM
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
8489eb67
RS
12
13GNU Emacs is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
9ec0b715 19along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
8489eb67
RS
20
21
18160b98 22#include <config.h>
8489eb67 23#include <ctype.h>
d7306fe6 24#include <setjmp.h>
8489eb67
RS
25#include "lisp.h"
26#include "commands.h"
27#include "buffer.h"
5c7b02ab 28#include "character.h"
e35f6ff7 29#include "keymap.h"
a1bc88d4 30#include "regex.h"
195d1361
RS
31
32/* Make syntax table lookup grant data in gl_state. */
33#define SYNTAX_ENTRY_VIA_PROPERTY
34
8489eb67 35#include "syntax.h"
195d1361
RS
36#include "intervals.h"
37
38/* We use these constants in place for comment-style and
39 string-ender-char to distinguish comments/strings started by
40 comment_fence and string_fence codes. */
41
42#define ST_COMMENT_STYLE (256 + 1)
43#define ST_STRING_STYLE (256 + 2)
93da5fff 44#include "category.h"
8489eb67 45
7bf5e9e4 46Lisp_Object Qsyntax_table_p, Qsyntax_table, Qscan_error;
8489eb67
RS
47
48int words_include_escapes;
195d1361 49int parse_sexp_lookup_properties;
8489eb67 50
bd25db08
KH
51/* Nonzero means `scan-sexps' treat all multibyte characters as symbol. */
52int multibyte_syntax_as_symbol;
53
8ea151b2
RS
54/* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
55 if not compiled with GCC. No need to mark it, since it is used
56 only very temporarily. */
57Lisp_Object syntax_temp;
58
f4ed767f
GM
59/* Non-zero means an open parenthesis in column 0 is always considered
60 to be the start of a defun. Zero means an open parenthesis in
61 column 0 has no special meaning. */
62
63int open_paren_in_column_0_is_defun_start;
64
e5d4f4dc
RS
65/* This is the internal form of the parse state used in parse-partial-sexp. */
66
67struct lisp_parse_state
68 {
6449674e
SM
69 int depth; /* Depth at end of parsing. */
70 int instring; /* -1 if not within string, else desired terminator. */
71 int incomment; /* -1 if in unnestable comment else comment nesting */
72 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
73 int quoted; /* Nonzero if just after an escape char at end of parsing */
74 int mindepth; /* Minimum depth seen while scanning. */
75 /* Char number of most recent start-of-expression at current level */
76 EMACS_INT thislevelstart;
77 /* Char number of start of containing expression */
78 EMACS_INT prevlevelstart;
79 EMACS_INT location; /* Char number at which parsing stopped. */
80 EMACS_INT comstr_start; /* Position of last comment/string starter. */
81 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
82 of levels (starting from outermost). */
e5d4f4dc
RS
83 };
84\f
37bef230
RS
85/* These variables are a cache for finding the start of a defun.
86 find_start_pos is the place for which the defun start was found.
87 find_start_value is the defun start position found for it.
6a140a74 88 find_start_value_byte is the corresponding byte position.
37bef230
RS
89 find_start_buffer is the buffer it was found in.
90 find_start_begv is the BEGV value when it was found.
91 find_start_modiff is the value of MODIFF when it was found. */
92
8e2911c2
AS
93static EMACS_INT find_start_pos;
94static EMACS_INT find_start_value;
95static EMACS_INT find_start_value_byte;
37bef230 96static struct buffer *find_start_buffer;
8e2911c2 97static EMACS_INT find_start_begv;
37bef230 98static int find_start_modiff;
6a140a74
RS
99
100
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;
62a6e103 914 int character = STRING_CHAR_AND_LENGTH (p, len);
93da5fff 915 XSETINT (match, character);
d1be9f0f
RS
916 if (XFASTINT (match) == ' ')
917 match = Qnil;
93da5fff 918 p += len;
d1be9f0f
RS
919 }
920 else
8ea151b2 921 match = Qnil;
8489eb67 922
8ea151b2 923 val = (int) code;
8489eb67
RS
924 while (*p)
925 switch (*p++)
926 {
927 case '1':
247e20a8 928 val |= 1 << 16;
8489eb67
RS
929 break;
930
931 case '2':
247e20a8 932 val |= 1 << 17;
8489eb67
RS
933 break;
934
935 case '3':
247e20a8 936 val |= 1 << 18;
8489eb67
RS
937 break;
938
939 case '4':
247e20a8 940 val |= 1 << 19;
8489eb67
RS
941 break;
942
943 case 'p':
247e20a8 944 val |= 1 << 20;
8489eb67 945 break;
e5d4f4dc
RS
946
947 case 'b':
247e20a8 948 val |= 1 << 21;
e5d4f4dc 949 break;
95ff8dfc
RS
950
951 case 'n':
952 val |= 1 << 22;
953 break;
8489eb67 954 }
7d0393cf 955
93da5fff 956 if (val < XVECTOR (Vsyntax_code_object)->size && NILP (match))
c65adb44 957 return XVECTOR (Vsyntax_code_object)->contents[val];
93da5fff
KH
958 else
959 /* Since we can't use a shared object, let's make a new one. */
c65adb44
SM
960 return Fcons (make_number (val), match);
961}
962
fdb82f93 963/* I really don't know why this is interactive
2c9e1900 964 help-form should at least be made useful whilst reading the second arg. */
7d0393cf 965DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
c65adb44 966 "cSet syntax for character: \nsSet syntax for %s to: ",
a50a10a0 967 doc: /* Set syntax for character CHAR according to string NEWENTRY.
d7ee9fab 968The syntax is changed only for table SYNTAX-TABLE, which defaults to
fdb82f93 969 the current buffer's syntax table.
5c7b02ab 970CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
fedc6ab5 971in the range MIN to MAX are changed.
fdb82f93
PJ
972The first character of NEWENTRY should be one of the following:
973 Space or - whitespace syntax. w word constituent.
974 _ symbol constituent. . punctuation.
975 ( open-parenthesis. ) close-parenthesis.
976 " string quote. \\ escape.
977 $ paired delimiter. ' expression quote or prefix operator.
978 < comment starter. > comment ender.
979 / character-quote. @ inherit from `standard-syntax-table'.
980 | generic string fence. ! generic comment fence.
981
982Only single-character comment start and end sequences are represented thus.
983Two-character sequences are represented as described below.
984The second character of NEWENTRY is the matching parenthesis,
985 used only if the first character is `(' or `)'.
986Any additional characters are flags.
987Defined flags are the characters 1, 2, 3, 4, b, p, and n.
a50a10a0
PJ
988 1 means CHAR is the start of a two-char comment start sequence.
989 2 means CHAR is the second character of such a sequence.
990 3 means CHAR is the start of a two-char comment end sequence.
991 4 means CHAR is the second character of such a sequence.
fdb82f93
PJ
992
993There can be up to two orthogonal comment sequences. This is to support
994language modes such as C++. By default, all comment sequences are of style
995a, but you can set the comment sequence style to b (on the second character
996of a comment-start, or the first character of a comment-end sequence) using
997this flag:
a50a10a0
PJ
998 b means CHAR is part of comment sequence b.
999 n means CHAR is part of a nestable comment sequence.
fdb82f93 1000
a50a10a0 1001 p means CHAR is a prefix character for `backward-prefix-chars';
fdb82f93 1002 such characters are treated as whitespace when they occur
a50a10a0 1003 between expressions.
fedc6ab5 1004usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
fdb82f93 1005 (c, newentry, syntax_table)
c65adb44
SM
1006 Lisp_Object c, newentry, syntax_table;
1007{
5c7b02ab
KH
1008 if (CONSP (c))
1009 {
8f924df7
KH
1010 CHECK_CHARACTER_CAR (c);
1011 CHECK_CHARACTER_CDR (c);
5c7b02ab
KH
1012 }
1013 else
1014 CHECK_CHARACTER (c);
c65adb44
SM
1015
1016 if (NILP (syntax_table))
1017 syntax_table = current_buffer->syntax_table;
1018 else
1019 check_syntax_table (syntax_table);
8489eb67 1020
5c7b02ab
KH
1021 newentry = Fstring_to_syntax (newentry);
1022 if (CONSP (c))
1023 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1024 else
1025 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
e5b94d44
CY
1026
1027 /* We clear the regexp cache, since character classes can now have
1028 different values from those in the compiled regexps.*/
1029 clear_regexp_cache ();
1030
8489eb67
RS
1031 return Qnil;
1032}
1033\f
1034/* Dump syntax table to buffer in human-readable format */
1035
62abe9cb
SM
1036DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1037 Sinternal_describe_syntax_value, 1, 1, 0,
1038 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1039 (syntax)
1040 Lisp_Object syntax;
8489eb67
RS
1041{
1042 register enum syntaxcode code;
e6365855 1043 char desc, start1, start2, end1, end2, prefix, comstyle, comnested;
8489eb67 1044 char str[2];
62abe9cb 1045 Lisp_Object first, match_lisp, value = syntax;
8489eb67 1046
8ea151b2
RS
1047 if (NILP (value))
1048 {
62abe9cb
SM
1049 insert_string ("default");
1050 return syntax;
8ea151b2
RS
1051 }
1052
908b7fea
KH
1053 if (CHAR_TABLE_P (value))
1054 {
62abe9cb
SM
1055 insert_string ("deeper char-table ...");
1056 return syntax;
908b7fea
KH
1057 }
1058
8ea151b2
RS
1059 if (!CONSP (value))
1060 {
62abe9cb
SM
1061 insert_string ("invalid");
1062 return syntax;
8ea151b2
RS
1063 }
1064
c1d497be
KR
1065 first = XCAR (value);
1066 match_lisp = XCDR (value);
8ea151b2
RS
1067
1068 if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp)))
8489eb67 1069 {
62abe9cb
SM
1070 insert_string ("invalid");
1071 return syntax;
8489eb67
RS
1072 }
1073
e704cb4b 1074 code = (enum syntaxcode) (XINT (first) & 0377);
8ea151b2
RS
1075 start1 = (XINT (first) >> 16) & 1;
1076 start2 = (XINT (first) >> 17) & 1;
1077 end1 = (XINT (first) >> 18) & 1;
1078 end2 = (XINT (first) >> 19) & 1;
1079 prefix = (XINT (first) >> 20) & 1;
1080 comstyle = (XINT (first) >> 21) & 1;
e6365855 1081 comnested = (XINT (first) >> 22) & 1;
8489eb67
RS
1082
1083 if ((int) code < 0 || (int) code >= (int) Smax)
1084 {
1085 insert_string ("invalid");
62abe9cb 1086 return syntax;
8489eb67
RS
1087 }
1088 desc = syntax_code_spec[(int) code];
1089
1090 str[0] = desc, str[1] = 0;
1091 insert (str, 1);
1092
93da5fff
KH
1093 if (NILP (match_lisp))
1094 insert (" ", 1);
1095 else
1096 insert_char (XINT (match_lisp));
8489eb67 1097
8489eb67
RS
1098 if (start1)
1099 insert ("1", 1);
1100 if (start2)
1101 insert ("2", 1);
1102
1103 if (end1)
1104 insert ("3", 1);
1105 if (end2)
1106 insert ("4", 1);
1107
1108 if (prefix)
1109 insert ("p", 1);
e5d4f4dc
RS
1110 if (comstyle)
1111 insert ("b", 1);
e6365855
SM
1112 if (comnested)
1113 insert ("n", 1);
8489eb67
RS
1114
1115 insert_string ("\twhich means: ");
1116
0220c518 1117 switch (SWITCH_ENUM_CAST (code))
8489eb67
RS
1118 {
1119 case Swhitespace:
1120 insert_string ("whitespace"); break;
1121 case Spunct:
1122 insert_string ("punctuation"); break;
1123 case Sword:
1124 insert_string ("word"); break;
1125 case Ssymbol:
1126 insert_string ("symbol"); break;
1127 case Sopen:
1128 insert_string ("open"); break;
1129 case Sclose:
1130 insert_string ("close"); break;
1131 case Squote:
d671382e 1132 insert_string ("prefix"); break;
8489eb67
RS
1133 case Sstring:
1134 insert_string ("string"); break;
1135 case Smath:
1136 insert_string ("math"); break;
1137 case Sescape:
1138 insert_string ("escape"); break;
1139 case Scharquote:
1140 insert_string ("charquote"); break;
1141 case Scomment:
1142 insert_string ("comment"); break;
1143 case Sendcomment:
1144 insert_string ("endcomment"); break;
d671382e
SM
1145 case Sinherit:
1146 insert_string ("inherit"); break;
1147 case Scomment_fence:
1148 insert_string ("comment fence"); break;
1149 case Sstring_fence:
1150 insert_string ("string fence"); break;
8489eb67
RS
1151 default:
1152 insert_string ("invalid");
62abe9cb 1153 return syntax;
8489eb67
RS
1154 }
1155
8ea151b2 1156 if (!NILP (match_lisp))
8489eb67
RS
1157 {
1158 insert_string (", matches ");
8ea151b2 1159 insert_char (XINT (match_lisp));
8489eb67
RS
1160 }
1161
1162 if (start1)
1163 insert_string (",\n\t is the first character of a comment-start sequence");
1164 if (start2)
1165 insert_string (",\n\t is the second character of a comment-start sequence");
1166
1167 if (end1)
1168 insert_string (",\n\t is the first character of a comment-end sequence");
1169 if (end2)
1170 insert_string (",\n\t is the second character of a comment-end sequence");
e5d4f4dc
RS
1171 if (comstyle)
1172 insert_string (" (comment style b)");
e6365855
SM
1173 if (comnested)
1174 insert_string (" (nestable)");
e5d4f4dc 1175
8489eb67
RS
1176 if (prefix)
1177 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1178
62abe9cb
SM
1179 return syntax;
1180}
8489eb67 1181\f
195d1361
RS
1182int parse_sexp_ignore_comments;
1183
8f924df7
KH
1184/* Char-table of functions that find the next or previous word
1185 boundary. */
1186Lisp_Object Vfind_word_boundary_function_table;
869bb237 1187
8489eb67
RS
1188/* Return the position across COUNT words from FROM.
1189 If that many words cannot be found before the end of the buffer, return 0.
1190 COUNT negative means scan backward and stop at word beginning. */
1191
dfcf069d 1192int
8489eb67
RS
1193scan_words (from, count)
1194 register int from, count;
1195{
1196 register int beg = BEGV;
1197 register int end = ZV;
6a140a74 1198 register int from_byte = CHAR_TO_BYTE (from);
93da5fff
KH
1199 register enum syntaxcode code;
1200 int ch0, ch1;
8f924df7 1201 Lisp_Object func, script, pos;
8489eb67
RS
1202
1203 immediate_quit = 1;
1204 QUIT;
1205
195d1361
RS
1206 SETUP_SYNTAX_TABLE (from, count);
1207
8489eb67
RS
1208 while (count > 0)
1209 {
1210 while (1)
1211 {
1212 if (from == end)
1213 {
1214 immediate_quit = 0;
1215 return 0;
1216 }
195d1361 1217 UPDATE_SYNTAX_TABLE_FORWARD (from);
b7dbcc19 1218 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
93da5fff 1219 code = SYNTAX (ch0);
6a140a74 1220 INC_BOTH (from, from_byte);
8489eb67
RS
1221 if (words_include_escapes
1222 && (code == Sescape || code == Scharquote))
1223 break;
1224 if (code == Sword)
1225 break;
8489eb67 1226 }
93da5fff
KH
1227 /* Now CH0 is a character which begins a word and FROM is the
1228 position of the next character. */
8f924df7 1229 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
869bb237 1230 if (! NILP (Ffboundp (func)))
8489eb67 1231 {
869bb237 1232 pos = call2 (func, make_number (from - 1), make_number (end));
8f924df7
KH
1233 if (INTEGERP (pos) && XINT (pos) > from)
1234 {
1235 from = XINT (pos);
1236 from_byte = CHAR_TO_BYTE (from);
1237 }
8489eb67 1238 }
869bb237 1239 else
8cb8232a 1240 {
8cb8232a
KH
1241 script = CHAR_TABLE_REF (Vchar_script_table, ch0);
1242 while (1)
1243 {
1244 if (from == end) break;
1245 UPDATE_SYNTAX_TABLE_FORWARD (from);
b7dbcc19 1246 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
8cb8232a
KH
1247 code = SYNTAX (ch1);
1248 if ((code != Sword
1249 && (! words_include_escapes
1250 || (code != Sescape && code != Scharquote)))
2b70a11c 1251 || word_boundary_p (ch0, ch1))
869bb237 1252 break;
8cb8232a
KH
1253 INC_BOTH (from, from_byte);
1254 ch0 = ch1;
1255 }
8489eb67
RS
1256 }
1257 count--;
1258 }
1259 while (count < 0)
1260 {
1261 while (1)
1262 {
1263 if (from == beg)
1264 {
1265 immediate_quit = 0;
1266 return 0;
1267 }
6a140a74 1268 DEC_BOTH (from, from_byte);
195d1361 1269 UPDATE_SYNTAX_TABLE_BACKWARD (from);
b7dbcc19 1270 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
93da5fff 1271 code = SYNTAX (ch1);
8489eb67
RS
1272 if (words_include_escapes
1273 && (code == Sescape || code == Scharquote))
1274 break;
1275 if (code == Sword)
1276 break;
8489eb67 1277 }
93da5fff
KH
1278 /* Now CH1 is a character which ends a word and FROM is the
1279 position of it. */
8f924df7 1280 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
869bb237 1281 if (! NILP (Ffboundp (func)))
8f924df7 1282 {
869bb237 1283 pos = call2 (func, make_number (from), make_number (beg));
8f924df7
KH
1284 if (INTEGERP (pos) && XINT (pos) < from)
1285 {
1286 from = XINT (pos);
1287 from_byte = CHAR_TO_BYTE (from);
1288 }
8489eb67 1289 }
869bb237 1290 else
8489eb67 1291 {
8cb8232a
KH
1292 script = CHAR_TABLE_REF (Vchar_script_table, ch1);
1293 while (1)
1294 {
8cb8232a
KH
1295 if (from == beg)
1296 break;
262be72a 1297 DEC_BOTH (from, from_byte);
8cb8232a 1298 UPDATE_SYNTAX_TABLE_BACKWARD (from);
262be72a 1299 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
8cb8232a
KH
1300 code = SYNTAX (ch0);
1301 if ((code != Sword
1302 && (! words_include_escapes
1303 || (code != Sescape && code != Scharquote)))
2b70a11c 1304 || word_boundary_p (ch0, ch1))
262be72a
MB
1305 {
1306 INC_BOTH (from, from_byte);
1307 break;
1308 }
8cb8232a
KH
1309 ch1 = ch0;
1310 }
8489eb67
RS
1311 }
1312 count++;
1313 }
1314
1315 immediate_quit = 0;
1316
1317 return from;
1318}
1319
1e9dbb5f 1320DEFUN ("forward-word", Fforward_word, Sforward_word, 0, 1, "^p",
fdb82f93
PJ
1321 doc: /* Move point forward ARG words (backward if ARG is negative).
1322Normally returns t.
1323If an edge of the buffer or a field boundary is reached, point is left there
1324and the function returns nil. Field boundaries are not noticed if
1325`inhibit-field-text-motion' is non-nil. */)
839966f3
KH
1326 (arg)
1327 Lisp_Object arg;
8489eb67 1328{
c096ae4d 1329 Lisp_Object tmp;
2c6ea900 1330 int orig_val, val;
8489eb67 1331
839966f3
KH
1332 if (NILP (arg))
1333 XSETFASTINT (arg, 1);
1334 else
1335 CHECK_NUMBER (arg);
1336
1337 val = orig_val = scan_words (PT, XINT (arg));
2c6ea900 1338 if (! orig_val)
839966f3 1339 val = XINT (arg) > 0 ? ZV : BEGV;
5878ee6f 1340
b8855607 1341 /* Avoid jumping out of an input field. */
c096ae4d
SM
1342 tmp = Fconstrain_to_field (make_number (val), make_number (PT),
1343 Qt, Qnil, Qnil);
1344 val = XFASTINT (tmp);
7d0393cf 1345
8489eb67 1346 SET_PT (val);
e6d8341f 1347 return val == orig_val ? Qt : Qnil;
8489eb67
RS
1348}
1349\f
195d1361
RS
1350Lisp_Object skip_chars ();
1351
1352DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
fdb82f93
PJ
1353 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1354STRING is like the inside of a `[...]' in a regular expression
1355except that `]' is never special and `\\' quotes `^', `-' or `\\'
7087d5e9 1356 (but not at the end of a range; quoting is never needed there).
fdb82f93
PJ
1357Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1358With arg "^a-zA-Z", skips nonletters stopping before first letter.
a1bc88d4
RS
1359Char classes, e.g. `[:alpha:]', are supported.
1360
1361Returns the distance traveled, either zero or positive. */)
fdb82f93 1362 (string, lim)
195d1361
RS
1363 Lisp_Object string, lim;
1364{
327719ee 1365 return skip_chars (1, string, lim, 1);
195d1361
RS
1366}
1367
1368DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
fdb82f93
PJ
1369 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1370See `skip-chars-forward' for details.
1371Returns the distance traveled, either zero or negative. */)
1372 (string, lim)
195d1361
RS
1373 Lisp_Object string, lim;
1374{
327719ee 1375 return skip_chars (0, string, lim, 1);
195d1361
RS
1376}
1377
1378DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
fdb82f93
PJ
1379 doc: /* Move point forward across chars in specified syntax classes.
1380SYNTAX is a string of syntax code characters.
1381Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1382If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1383This function returns the distance traveled, either zero or positive. */)
1384 (syntax, lim)
195d1361
RS
1385 Lisp_Object syntax, lim;
1386{
b7dbcc19 1387 return skip_syntaxes (1, syntax, lim);
195d1361
RS
1388}
1389
1390DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
fdb82f93
PJ
1391 doc: /* Move point backward across chars in specified syntax classes.
1392SYNTAX is a string of syntax code characters.
1393Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1394If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1395This function returns the distance traveled, either zero or negative. */)
1396 (syntax, lim)
195d1361
RS
1397 Lisp_Object syntax, lim;
1398{
b7dbcc19 1399 return skip_syntaxes (0, syntax, lim);
195d1361
RS
1400}
1401
6a140a74 1402static Lisp_Object
327719ee 1403skip_chars (forwardp, string, lim, handle_iso_classes)
b7dbcc19 1404 int forwardp;
195d1361 1405 Lisp_Object string, lim;
a1bc88d4 1406 int handle_iso_classes;
195d1361 1407{
195d1361
RS
1408 register unsigned int c;
1409 unsigned char fastmap[0400];
b7dbcc19 1410 /* Store the ranges of non-ASCII characters. */
5df4982e 1411 int *char_ranges;
7e68b0ea 1412 int n_char_ranges = 0;
195d1361 1413 int negate = 0;
4101e6fe 1414 register int i, i_byte;
b7dbcc19
KH
1415 /* Set to 1 if the current buffer is multibyte and the region
1416 contains non-ASCII chars. */
1417 int multibyte;
1418 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1419 chars. */
1674d9a2
RS
1420 int string_multibyte;
1421 int size_byte;
2e567bd3 1422 const unsigned char *str;
82d497fc 1423 int len;
a1bc88d4 1424 Lisp_Object iso_classes;
195d1361 1425
b7826503 1426 CHECK_STRING (string);
a1bc88d4 1427 iso_classes = Qnil;
82d497fc 1428
195d1361
RS
1429 if (NILP (lim))
1430 XSETINT (lim, forwardp ? ZV : BEGV);
1431 else
b7826503 1432 CHECK_NUMBER_COERCE_MARKER (lim);
195d1361
RS
1433
1434 /* In any case, don't allow scan outside bounds of buffer. */
195d1361
RS
1435 if (XINT (lim) > ZV)
1436 XSETFASTINT (lim, ZV);
1437 if (XINT (lim) < BEGV)
1438 XSETFASTINT (lim, BEGV);
1439
b7dbcc19 1440 multibyte = (!NILP (current_buffer->enable_multibyte_characters)
92eaa22e 1441 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
8f924df7 1442 string_multibyte = SBYTES (string) > SCHARS (string);
b7dbcc19 1443
195d1361
RS
1444 bzero (fastmap, sizeof fastmap);
1445
8f924df7
KH
1446 str = SDATA (string);
1447 size_byte = SBYTES (string);
4101e6fe 1448
82d497fc 1449 i_byte = 0;
13090112 1450 if (i_byte < size_byte
d5db4077 1451 && SREF (string, 0) == '^')
195d1361 1452 {
82d497fc 1453 negate = 1; i_byte++;
195d1361
RS
1454 }
1455
1456 /* Find the characters specified and set their elements of fastmap.
b7dbcc19 1457 Handle backslashes and ranges specially.
195d1361 1458
b7dbcc19
KH
1459 If STRING contains non-ASCII characters, setup char_ranges for
1460 them and use fastmap only for their leading codes. */
4101e6fe 1461
b7dbcc19 1462 if (! string_multibyte)
195d1361 1463 {
b7dbcc19 1464 int string_has_eight_bit = 0;
4101e6fe 1465
b7dbcc19
KH
1466 /* At first setup fastmap. */
1467 while (i_byte < size_byte)
1468 {
1469 c = str[i_byte++];
1470
a1bc88d4
RS
1471 if (handle_iso_classes && c == '['
1472 && i_byte < size_byte
327719ee 1473 && str[i_byte] == ':')
a1bc88d4
RS
1474 {
1475 const unsigned char *class_beg = str + i_byte + 1;
1476 const unsigned char *class_end = class_beg;
b3bda4fd 1477 const unsigned char *class_limit = str + size_byte - 2;
db9cd97a 1478 /* Leave room for the null. */
a1bc88d4
RS
1479 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1480 re_wctype_t cc;
1481
1482 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1483 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1484
b3bda4fd
KS
1485 while (class_end < class_limit
1486 && *class_end >= 'a' && *class_end <= 'z')
a1bc88d4
RS
1487 class_end++;
1488
b3bda4fd
KS
1489 if (class_end == class_beg
1490 || *class_end != ':' || class_end[1] != ']')
1491 goto not_a_class_name;
a1bc88d4
RS
1492
1493 bcopy (class_beg, class_name, class_end - class_beg);
1494 class_name[class_end - class_beg] = 0;
1495
1496 cc = re_wctype (class_name);
1497 if (cc == 0)
1498 error ("Invalid ISO C character class");
1499
1500 iso_classes = Fcons (make_number (cc), iso_classes);
1501
1502 i_byte = class_end + 2 - str;
1503 continue;
1504 }
1505
b3bda4fd 1506 not_a_class_name:
b7dbcc19
KH
1507 if (c == '\\')
1508 {
13090112 1509 if (i_byte == size_byte)
4101e6fe
RS
1510 break;
1511
5c7b02ab 1512 c = str[i_byte++];
195d1361 1513 }
839966f3
KH
1514 /* Treat `-' as range character only if another character
1515 follows. */
1516 if (i_byte + 1 < size_byte
82d497fc 1517 && str[i_byte] == '-')
195d1361 1518 {
9690d026 1519 unsigned int c2;
4101e6fe
RS
1520
1521 /* Skip over the dash. */
82d497fc 1522 i_byte++;
4101e6fe 1523
4101e6fe 1524 /* Get the end of the range. */
5c7b02ab 1525 c2 = str[i_byte++];
b7dbcc19
KH
1526 if (c2 == '\\'
1527 && i_byte < size_byte)
1528 c2 = str[i_byte++];
1529
8f924df7
KH
1530 if (c <= c2)
1531 {
1532 while (c <= c2)
1533 fastmap[c++] = 1;
1534 if (! ASCII_CHAR_P (c2))
1535 string_has_eight_bit = 1;
1536 }
195d1361
RS
1537 }
1538 else
b7dbcc19
KH
1539 {
1540 fastmap[c] = 1;
1541 if (! ASCII_CHAR_P (c))
1542 string_has_eight_bit = 1;
1543 }
1544 }
1545
1546 /* If the current range is multibyte and STRING contains
1547 eight-bit chars, arrange fastmap and setup char_ranges for
1548 the corresponding multibyte chars. */
1549 if (multibyte && string_has_eight_bit)
1550 {
1551 unsigned char fastmap2[0400];
1552 int range_start_byte, range_start_char;
1553
1554 bcopy (fastmap2 + 0200, fastmap + 0200, 0200);
1555 bzero (fastmap + 0200, 0200);
1556 /* We are sure that this loop stops. */
1557 for (i = 0200; ! fastmap2[i]; i++);
4c0354d7 1558 c = BYTE8_TO_CHAR (i);
b7dbcc19
KH
1559 fastmap[CHAR_LEADING_CODE (c)] = 1;
1560 range_start_byte = i;
1561 range_start_char = c;
f003ca54 1562 char_ranges = (int *) alloca (sizeof (int) * 128 * 2);
b7dbcc19
KH
1563 for (i = 129; i < 0400; i++)
1564 {
4c0354d7 1565 c = BYTE8_TO_CHAR (i);
b7dbcc19
KH
1566 fastmap[CHAR_LEADING_CODE (c)] = 1;
1567 if (i - range_start_byte != c - range_start_char)
1568 {
1569 char_ranges[n_char_ranges++] = range_start_char;
1570 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1571 + range_start_char);
1572 range_start_byte = i;
1573 range_start_char = c;
8f924df7 1574 }
b7dbcc19
KH
1575 }
1576 char_ranges[n_char_ranges++] = range_start_char;
1577 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1578 + range_start_char);
195d1361
RS
1579 }
1580 }
f003ca54 1581 else /* STRING is multibyte */
b7dbcc19 1582 {
f003ca54
KH
1583 char_ranges = (int *) alloca (sizeof (int) * SCHARS (string) * 2);
1584
b7dbcc19 1585 while (i_byte < size_byte)
195d1361 1586 {
b7dbcc19 1587 unsigned char leading_code;
5c7b02ab 1588
b7dbcc19 1589 leading_code = str[i_byte];
62a6e103 1590 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
b7dbcc19 1591 i_byte += len;
5c7b02ab 1592
327719ee
MB
1593 if (handle_iso_classes && c == '['
1594 && i_byte < size_byte
62a6e103 1595 && STRING_CHAR (str + i_byte) == ':')
327719ee
MB
1596 {
1597 const unsigned char *class_beg = str + i_byte + 1;
1598 const unsigned char *class_end = class_beg;
1599 const unsigned char *class_limit = str + size_byte - 2;
1600 /* Leave room for the null. */
1601 unsigned char class_name[CHAR_CLASS_MAX_LENGTH + 1];
1602 re_wctype_t cc;
1603
1604 if (class_limit - class_beg > CHAR_CLASS_MAX_LENGTH)
1605 class_limit = class_beg + CHAR_CLASS_MAX_LENGTH;
1606
1607 while (class_end < class_limit
1608 && *class_end >= 'a' && *class_end <= 'z')
1609 class_end++;
1610
1611 if (class_end == class_beg
1612 || *class_end != ':' || class_end[1] != ']')
1613 goto not_a_class_name_multibyte;
1614
1615 bcopy (class_beg, class_name, class_end - class_beg);
1616 class_name[class_end - class_beg] = 0;
1617
1618 cc = re_wctype (class_name);
1619 if (cc == 0)
1620 error ("Invalid ISO C character class");
1621
1622 iso_classes = Fcons (make_number (cc), iso_classes);
1623
1624 i_byte = class_end + 2 - str;
1625 continue;
1626 }
1627
1628 not_a_class_name_multibyte:
195d1361
RS
1629 if (c == '\\')
1630 {
13090112 1631 if (i_byte == size_byte)
4101e6fe
RS
1632 break;
1633
b7dbcc19 1634 leading_code = str[i_byte];
62a6e103 1635 c = STRING_CHAR_AND_LENGTH (str + i_byte, len);
82d497fc 1636 i_byte += len;
195d1361 1637 }
839966f3
KH
1638 /* Treat `-' as range character only if another character
1639 follows. */
1640 if (i_byte + 1 < size_byte
82d497fc 1641 && str[i_byte] == '-')
195d1361 1642 {
9690d026 1643 unsigned int c2;
b7dbcc19 1644 unsigned char leading_code2;
4101e6fe
RS
1645
1646 /* Skip over the dash. */
82d497fc 1647 i_byte++;
4101e6fe 1648
4101e6fe 1649 /* Get the end of the range. */
b7dbcc19 1650 leading_code2 = str[i_byte];
62a6e103 1651 c2 = STRING_CHAR_AND_LENGTH (str + i_byte, len);
82d497fc 1652 i_byte += len;
7e68b0ea 1653
b7dbcc19
KH
1654 if (c2 == '\\'
1655 && i_byte < size_byte)
1656 {
1657 leading_code2 = str[i_byte];
62a6e103 1658 c2 =STRING_CHAR_AND_LENGTH (str + i_byte, len);
b7dbcc19
KH
1659 i_byte += len;
1660 }
1661
f003ca54
KH
1662 if (c > c2)
1663 continue;
b7dbcc19 1664 if (ASCII_CHAR_P (c))
06274af5 1665 {
5c7b02ab
KH
1666 while (c <= c2 && c < 0x80)
1667 fastmap[c++] = 1;
b7dbcc19
KH
1668 leading_code = CHAR_LEADING_CODE (c);
1669 }
1670 if (! ASCII_CHAR_P (c))
1671 {
1672 while (leading_code <= leading_code2)
1673 fastmap[leading_code++] = 1;
1674 if (c <= c2)
e39091b6 1675 {
b7dbcc19 1676 char_ranges[n_char_ranges++] = c;
e39091b6 1677 char_ranges[n_char_ranges++] = c2;
06274af5
KH
1678 }
1679 }
195d1361
RS
1680 }
1681 else
7e68b0ea 1682 {
b7dbcc19 1683 if (ASCII_CHAR_P (c))
e39091b6
KH
1684 fastmap[c] = 1;
1685 else
7e68b0ea 1686 {
b7dbcc19 1687 fastmap[leading_code] = 1;
4101e6fe
RS
1688 char_ranges[n_char_ranges++] = c;
1689 char_ranges[n_char_ranges++] = c;
7e68b0ea
RS
1690 }
1691 }
195d1361 1692 }
b7dbcc19
KH
1693
1694 /* If the current range is unibyte and STRING contains non-ASCII
1695 chars, arrange fastmap for the corresponding unibyte
1696 chars. */
1697
1698 if (! multibyte && n_char_ranges > 0)
1699 {
1700 bzero (fastmap + 0200, 0200);
1701 for (i = 0; i < n_char_ranges; i += 2)
1702 {
1703 int c1 = char_ranges[i];
1704 int c2 = char_ranges[i + 1];
1705
1706 for (; c1 <= c2; c1++)
2afc21f5
SM
1707 {
1708 int b = CHAR_TO_BYTE_SAFE (c1);
1709 if (b >= 0)
1710 fastmap[b] = 1;
1711 }
b7dbcc19
KH
1712 }
1713 }
195d1361
RS
1714 }
1715
9690d026 1716 /* If ^ was the first character, complement the fastmap. */
195d1361 1717 if (negate)
b7dbcc19
KH
1718 {
1719 if (! multibyte)
1720 for (i = 0; i < sizeof fastmap; i++)
1721 fastmap[i] ^= 1;
1722 else
1723 {
1724 for (i = 0; i < 0200; i++)
1725 fastmap[i] ^= 1;
1726 /* All non-ASCII chars possibly match. */
1727 for (; i < sizeof fastmap; i++)
1728 fastmap[i] = 1;
1729 }
1730 }
195d1361
RS
1731
1732 {
1733 int start_point = PT;
1734 int pos = PT;
6a140a74 1735 int pos_byte = PT_BYTE;
9af7511a
KH
1736 unsigned char *p = PT_ADDR, *endp, *stop;
1737
1738 if (forwardp)
1739 {
1740 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
d7ee9fab 1741 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
9af7511a
KH
1742 }
1743 else
1744 {
1745 endp = CHAR_POS_ADDR (XINT (lim));
d7ee9fab 1746 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
9af7511a 1747 }
195d1361
RS
1748
1749 immediate_quit = 1;
618db430
SM
1750 /* This code may look up syntax tables using macros that rely on the
1751 gl_state object. To make sure this object is not out of date,
1752 let's initialize it manually.
1753 We ignore syntax-table text-properties for now, since that's
1754 what we've done in the past. */
1755 SETUP_SYNTAX_TABLE (BEGV, 0);
b7dbcc19 1756 if (forwardp)
195d1361 1757 {
b7dbcc19 1758 if (multibyte)
8f924df7 1759 while (1)
b7dbcc19 1760 {
8f924df7 1761 int nbytes;
9af7511a 1762
8f924df7 1763 if (p >= stop)
9af7511a 1764 {
8f924df7 1765 if (p >= endp)
9af7511a 1766 break;
8f924df7
KH
1767 p = GAP_END_ADDR;
1768 stop = endp;
9af7511a 1769 }
62a6e103 1770 c = STRING_CHAR_AND_LENGTH (p, nbytes);
327719ee 1771 if (! NILP (iso_classes) && in_classes (c, iso_classes))
9af7511a 1772 {
327719ee 1773 if (negate)
9af7511a 1774 break;
327719ee
MB
1775 else
1776 goto fwd_ok;
9af7511a 1777 }
9af7511a 1778
8f924df7 1779 if (! fastmap[*p])
b7dbcc19
KH
1780 break;
1781 if (! ASCII_CHAR_P (c))
9af7511a 1782 {
b7dbcc19
KH
1783 /* As we are looking at a multibyte character, we
1784 must look up the character in the table
1785 CHAR_RANGES. If there's no data in the table,
1786 that character is not what we want to skip. */
1787
1788 /* The following code do the right thing even if
1789 n_char_ranges is zero (i.e. no data in
1790 CHAR_RANGES). */
1791 for (i = 0; i < n_char_ranges; i += 2)
1792 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
9af7511a 1793 break;
b7dbcc19
KH
1794 if (!(negate ^ (i < n_char_ranges)))
1795 break;
9af7511a 1796 }
327719ee 1797 fwd_ok:
8f924df7 1798 p += nbytes, pos++, pos_byte += nbytes;
b7dbcc19
KH
1799 }
1800 else
8f924df7
KH
1801 while (1)
1802 {
1803 if (p >= stop)
9af7511a 1804 {
8f924df7 1805 if (p >= endp)
9af7511a 1806 break;
8f924df7
KH
1807 p = GAP_END_ADDR;
1808 stop = endp;
9af7511a 1809 }
327719ee
MB
1810
1811 if (!NILP (iso_classes) && in_classes (*p, iso_classes))
1812 {
1813 if (negate)
1814 break;
1815 else
1816 goto fwd_unibyte_ok;
1817 }
1818
8f924df7
KH
1819 if (!fastmap[*p])
1820 break;
327719ee 1821 fwd_unibyte_ok:
8f924df7
KH
1822 p++, pos++, pos_byte++;
1823 }
195d1361
RS
1824 }
1825 else
1826 {
b7dbcc19 1827 if (multibyte)
8f924df7 1828 while (1)
b7dbcc19 1829 {
8f924df7 1830 unsigned char *prev_p;
9af7511a 1831
8f924df7 1832 if (p <= stop)
9af7511a 1833 {
8f924df7 1834 if (p <= endp)
9af7511a 1835 break;
8f924df7
KH
1836 p = GPT_ADDR;
1837 stop = endp;
9af7511a 1838 }
8f924df7
KH
1839 prev_p = p;
1840 while (--p >= stop && ! CHAR_HEAD_P (*p));
62a6e103 1841 c = STRING_CHAR (p);
a1bc88d4 1842
327719ee
MB
1843 if (! NILP (iso_classes) && in_classes (c, iso_classes))
1844 {
1845 if (negate)
1846 break;
9690d026 1847 else
327719ee 1848 goto back_ok;
7e68b0ea 1849 }
a1bc88d4 1850
8f924df7 1851 if (! fastmap[*p])
b7dbcc19
KH
1852 break;
1853 if (! ASCII_CHAR_P (c))
7e68b0ea 1854 {
b7dbcc19
KH
1855 /* See the comment in the previous similar code. */
1856 for (i = 0; i < n_char_ranges; i += 2)
1857 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1858 break;
1859 if (!(negate ^ (i < n_char_ranges)))
1860 break;
7e68b0ea 1861 }
327719ee 1862 back_ok:
8f924df7 1863 pos--, pos_byte -= prev_p - p;
b7dbcc19
KH
1864 }
1865 else
8f924df7
KH
1866 while (1)
1867 {
1868 if (p <= stop)
9af7511a 1869 {
8f924df7 1870 if (p <= endp)
9af7511a 1871 break;
8f924df7
KH
1872 p = GPT_ADDR;
1873 stop = endp;
9af7511a 1874 }
e39091b6 1875
327719ee
MB
1876 if (! NILP (iso_classes) && in_classes (p[-1], iso_classes))
1877 {
1878 if (negate)
1879 break;
9af7511a 1880 else
327719ee
MB
1881 goto back_unibyte_ok;
1882 }
a1bc88d4 1883
8f924df7
KH
1884 if (!fastmap[p[-1]])
1885 break;
327719ee 1886 back_unibyte_ok:
8f924df7
KH
1887 p--, pos--, pos_byte--;
1888 }
195d1361 1889 }
7e68b0ea 1890
b7dbcc19
KH
1891 SET_PT_BOTH (pos, pos_byte);
1892 immediate_quit = 0;
1893
1894 return make_number (PT - start_point);
1895 }
1896}
7e68b0ea 1897
b7dbcc19
KH
1898
1899static Lisp_Object
1900skip_syntaxes (forwardp, string, lim)
1901 int forwardp;
1902 Lisp_Object string, lim;
1903{
1904 register unsigned int c;
1905 unsigned char fastmap[0400];
1906 int negate = 0;
1907 register int i, i_byte;
1908 int multibyte;
1909 int size_byte;
1910 unsigned char *str;
1911
1912 CHECK_STRING (string);
1913
1914 if (NILP (lim))
1915 XSETINT (lim, forwardp ? ZV : BEGV);
1916 else
1917 CHECK_NUMBER_COERCE_MARKER (lim);
1918
1919 /* In any case, don't allow scan outside bounds of buffer. */
1920 if (XINT (lim) > ZV)
1921 XSETFASTINT (lim, ZV);
1922 if (XINT (lim) < BEGV)
1923 XSETFASTINT (lim, BEGV);
1924
ade8ee9e 1925 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
49feb1cd 1926 return make_number (0);
8c6e735b 1927
b7dbcc19 1928 multibyte = (!NILP (current_buffer->enable_multibyte_characters)
92eaa22e 1929 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
b7dbcc19
KH
1930
1931 bzero (fastmap, sizeof fastmap);
1932
8f924df7
KH
1933 if (SBYTES (string) > SCHARS (string))
1934 /* As this is very rare case (syntax spec is ASCII only), don't
1935 consider efficiency. */
bc796a59
KH
1936 string = string_make_unibyte (string);
1937
8f924df7
KH
1938 str = SDATA (string);
1939 size_byte = SBYTES (string);
bc796a59 1940
b7dbcc19
KH
1941 i_byte = 0;
1942 if (i_byte < size_byte
8f924df7 1943 && SREF (string, 0) == '^')
b7dbcc19
KH
1944 {
1945 negate = 1; i_byte++;
1946 }
1947
b7dbcc19
KH
1948 /* Find the syntaxes specified and set their elements of fastmap. */
1949
1950 while (i_byte < size_byte)
1951 {
1952 c = str[i_byte++];
8f924df7 1953 fastmap[syntax_spec_code[c]] = 1;
b7dbcc19 1954 }
195d1361 1955
9690d026 1956 /* If ^ was the first character, complement the fastmap. */
195d1361
RS
1957 if (negate)
1958 for (i = 0; i < sizeof fastmap; i++)
9690d026 1959 fastmap[i] ^= 1;
195d1361
RS
1960
1961 {
1962 int start_point = PT;
1963 int pos = PT;
6a140a74 1964 int pos_byte = PT_BYTE;
8f924df7
KH
1965 unsigned char *p = PT_ADDR, *endp, *stop;
1966
1967 if (forwardp)
1968 {
1969 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1970 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1971 }
1972 else
1973 {
1974 endp = CHAR_POS_ADDR (XINT (lim));
1975 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1976 }
195d1361
RS
1977
1978 immediate_quit = 1;
b7dbcc19
KH
1979 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
1980 if (forwardp)
195d1361 1981 {
b7dbcc19 1982 if (multibyte)
195d1361 1983 {
8f924df7 1984 while (1)
8c6e735b 1985 {
8f924df7
KH
1986 int nbytes;
1987
1988 if (p >= stop)
1989 {
1990 if (p >= endp)
1991 break;
1992 p = GAP_END_ADDR;
1993 stop = endp;
1994 }
62a6e103 1995 c = STRING_CHAR_AND_LENGTH (p, nbytes);
8f924df7 1996 if (! fastmap[(int) SYNTAX (c)])
8c6e735b 1997 break;
8f924df7 1998 p += nbytes, pos++, pos_byte += nbytes;
8c6e735b
KH
1999 UPDATE_SYNTAX_TABLE_FORWARD (pos);
2000 }
195d1361
RS
2001 }
2002 else
2003 {
8c6e735b 2004 while (1)
7e68b0ea 2005 {
8f924df7
KH
2006 if (p >= stop)
2007 {
2008 if (p >= endp)
2009 break;
2010 p = GAP_END_ADDR;
2011 stop = endp;
2012 }
2013 if (! fastmap[(int) SYNTAX (*p)])
8c6e735b 2014 break;
8f924df7 2015 p++, pos++, pos_byte++;
b7dbcc19 2016 UPDATE_SYNTAX_TABLE_FORWARD (pos);
195d1361
RS
2017 }
2018 }
2019 }
2020 else
2021 {
b7dbcc19 2022 if (multibyte)
195d1361 2023 {
8c6e735b 2024 while (1)
b7dbcc19 2025 {
8f924df7
KH
2026 unsigned char *prev_p;
2027
2028 if (p <= stop)
b7dbcc19 2029 {
8f924df7
KH
2030 if (p <= endp)
2031 break;
2032 p = GPT_ADDR;
2033 stop = endp;
b7dbcc19 2034 }
262be72a 2035 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
8f924df7
KH
2036 prev_p = p;
2037 while (--p >= stop && ! CHAR_HEAD_P (*p));
62a6e103 2038 c = STRING_CHAR (p);
8f924df7
KH
2039 if (! fastmap[(int) SYNTAX (c)])
2040 break;
2041 pos--, pos_byte -= prev_p - p;
b7dbcc19 2042 }
195d1361
RS
2043 }
2044 else
2045 {
8c6e735b
KH
2046 while (1)
2047 {
8f924df7
KH
2048 if (p <= stop)
2049 {
2050 if (p <= endp)
2051 break;
2052 p = GPT_ADDR;
2053 stop = endp;
2054 }
102f6132 2055 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
8f924df7 2056 if (! fastmap[(int) SYNTAX (p[-1])])
8c6e735b 2057 break;
8f924df7 2058 p--, pos--, pos_byte--;
8c6e735b 2059 }
195d1361
RS
2060 }
2061 }
6a140a74
RS
2062
2063 SET_PT_BOTH (pos, pos_byte);
195d1361
RS
2064 immediate_quit = 0;
2065
2066 return make_number (PT - start_point);
2067 }
2068}
a1bc88d4
RS
2069
2070/* Return 1 if character C belongs to one of the ISO classes
2071 in the list ISO_CLASSES. Each class is represented by an
2072 integer which is its type according to re_wctype. */
2073
2074static int
2075in_classes (c, iso_classes)
2076 int c;
2077 Lisp_Object iso_classes;
2078{
2079 int fits_class = 0;
2080
618db430 2081 while (CONSP (iso_classes))
a1bc88d4
RS
2082 {
2083 Lisp_Object elt;
2084 elt = XCAR (iso_classes);
2085 iso_classes = XCDR (iso_classes);
2086
2087 if (re_iswctype (c, XFASTINT (elt)))
2088 fits_class = 1;
2089 }
2090
2091 return fits_class;
2092}
195d1361 2093\f
95ff8dfc
RS
2094/* Jump over a comment, assuming we are at the beginning of one.
2095 FROM is the current position.
2096 FROM_BYTE is the bytepos corresponding to FROM.
2097 Do not move past STOP (a charpos).
2098 The comment over which we have to jump is of style STYLE
2099 (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
2100 NESTING should be positive to indicate the nesting at the beginning
2101 for nested comments and should be zero or negative else.
2102 ST_COMMENT_STYLE cannot be nested.
2103 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
2104 (or 0 If the search cannot start in the middle of a two-character).
2105
2106 If successful, return 1 and store the charpos of the comment's end
2107 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
2108 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
2109 corresponding bytepos into *BYTEPOS_PTR and the current nesting
2110 (as defined for state.incomment) in *INCOMMENT_PTR.
2111
2112 The comment end is the last character of the comment rather than the
2113 character just after the comment.
2114
2115 Global syntax data is assumed to initially be valid for FROM and
2116 remains valid for forward search starting at the returned position. */
2117
2118static int
2119forw_comment (from, from_byte, stop, nesting, style, prev_syntax,
2120 charpos_ptr, bytepos_ptr, incomment_ptr)
a0aa7fcf 2121 EMACS_INT from, from_byte, stop;
95ff8dfc 2122 int nesting, style, prev_syntax;
a0aa7fcf
SM
2123 EMACS_INT *charpos_ptr, *bytepos_ptr;
2124 int *incomment_ptr;
95ff8dfc
RS
2125{
2126 register int c, c1;
2127 register enum syntaxcode code;
2128 register int syntax;
2129
2130 if (nesting <= 0) nesting = -1;
2131
2132 /* Enter the loop in the middle so that we find
2133 a 2-char comment ender if we start in the middle of it. */
2134 syntax = prev_syntax;
2135 if (syntax != 0) goto forw_incomment;
2136
2137 while (1)
2138 {
2139 if (from == stop)
2140 {
2141 *incomment_ptr = nesting;
2142 *charpos_ptr = from;
2143 *bytepos_ptr = from_byte;
2144 return 0;
2145 }
8f924df7 2146 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
95ff8dfc
RS
2147 syntax = SYNTAX_WITH_FLAGS (c);
2148 code = syntax & 0xff;
2149 if (code == Sendcomment
2150 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
abf8a9ff
SM
2151 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
2152 (nesting > 0 && --nesting == 0) : nesting < 0))
95ff8dfc
RS
2153 /* we have encountered a comment end of the same style
2154 as the comment sequence which began this comment
2155 section */
2156 break;
2157 if (code == Scomment_fence
2158 && style == ST_COMMENT_STYLE)
2159 /* we have encountered a comment end of the same style
2160 as the comment sequence which began this comment
2161 section. */
2162 break;
2163 if (nesting > 0
2164 && code == Scomment
abf8a9ff 2165 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
95ff8dfc
RS
2166 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style)
2167 /* we have encountered a nested comment of the same style
2168 as the comment sequence which began this comment section */
2169 nesting++;
2170 INC_BOTH (from, from_byte);
2171 UPDATE_SYNTAX_TABLE_FORWARD (from);
7d0393cf 2172
95ff8dfc
RS
2173 forw_incomment:
2174 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2175 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
8f924df7 2176 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
abf8a9ff
SM
2177 SYNTAX_COMEND_SECOND (c1))
2178 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2179 SYNTAX_COMMENT_NESTED (c1)) ? nesting > 0 : nesting < 0))
4ffe723b
GM
2180 {
2181 if (--nesting <= 0)
2182 /* we have encountered a comment end of the same style
2183 as the comment sequence which began this comment
2184 section */
2185 break;
2186 else
2187 {
2188 INC_BOTH (from, from_byte);
2189 UPDATE_SYNTAX_TABLE_FORWARD (from);
2190 }
2191 }
95ff8dfc
RS
2192 if (nesting > 0
2193 && from < stop
2194 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
8f924df7 2195 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
95ff8dfc 2196 SYNTAX_COMMENT_STYLE (c1) == style
abf8a9ff
SM
2197 && SYNTAX_COMSTART_SECOND (c1))
2198 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2199 SYNTAX_COMMENT_NESTED (c1)))
95ff8dfc
RS
2200 /* we have encountered a nested comment of the same style
2201 as the comment sequence which began this comment
2202 section */
2203 {
2204 INC_BOTH (from, from_byte);
2205 UPDATE_SYNTAX_TABLE_FORWARD (from);
2206 nesting++;
2207 }
2208 }
2209 *charpos_ptr = from;
2210 *bytepos_ptr = from_byte;
2211 return 1;
2212}
2213
b3cfe0c8 2214DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
177c0ea7 2215 doc: /*
ee648c11 2216Move forward across up to COUNT comments. If COUNT is negative, move backward.
fdb82f93
PJ
2217Stop scanning if we find something other than a comment or whitespace.
2218Set point to where scanning stops.
ee648c11 2219If COUNT comments are found as expected, with nothing except whitespace
fdb82f93
PJ
2220between them, return t; otherwise return nil. */)
2221 (count)
840f481c 2222 Lisp_Object count;
b3cfe0c8 2223{
2312c580
SM
2224 register EMACS_INT from;
2225 EMACS_INT from_byte;
2226 register EMACS_INT stop;
8ea151b2 2227 register int c, c1;
b3cfe0c8
RS
2228 register enum syntaxcode code;
2229 int comstyle = 0; /* style of comment encountered */
95ff8dfc 2230 int comnested = 0; /* whether the comment is nestable or not */
be720845 2231 int found;
2312c580
SM
2232 EMACS_INT count1;
2233 EMACS_INT out_charpos, out_bytepos;
95ff8dfc 2234 int dummy;
840f481c 2235
b7826503 2236 CHECK_NUMBER (count);
840f481c 2237 count1 = XINT (count);
195d1361 2238 stop = count1 > 0 ? ZV : BEGV;
b3cfe0c8
RS
2239
2240 immediate_quit = 1;
2241 QUIT;
2242
2243 from = PT;
6a140a74 2244 from_byte = PT_BYTE;
b3cfe0c8 2245
195d1361 2246 SETUP_SYNTAX_TABLE (from, count1);
840f481c 2247 while (count1 > 0)
b3cfe0c8 2248 {
04882296 2249 do
b3cfe0c8 2250 {
f902a008
RS
2251 int comstart_first;
2252
04882296
KH
2253 if (from == stop)
2254 {
ef316cf0 2255 SET_PT_BOTH (from, from_byte);
b7e6e612 2256 immediate_quit = 0;
04882296
KH
2257 return Qnil;
2258 }
b7dbcc19 2259 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
b3cfe0c8 2260 code = SYNTAX (c);
f902a008 2261 comstart_first = SYNTAX_COMSTART_FIRST (c);
95ff8dfc 2262 comnested = SYNTAX_COMMENT_NESTED (c);
e6365855 2263 comstyle = SYNTAX_COMMENT_STYLE (c);
6a140a74 2264 INC_BOTH (from, from_byte);
f902a008 2265 UPDATE_SYNTAX_TABLE_FORWARD (from);
f902a008 2266 if (from < stop && comstart_first
b7dbcc19 2267 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
8ea151b2 2268 SYNTAX_COMSTART_SECOND (c1)))
b3cfe0c8 2269 {
7d0393cf 2270 /* We have encountered a comment start sequence and we
7fc8191e 2271 are ignoring all text inside comments. We must record
b3cfe0c8
RS
2272 the comment style this sequence begins so that later,
2273 only a comment end of the same style actually ends
7fc8191e 2274 the comment section. */
b3cfe0c8 2275 code = Scomment;
8ea151b2 2276 comstyle = SYNTAX_COMMENT_STYLE (c1);
95ff8dfc 2277 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
6a140a74 2278 INC_BOTH (from, from_byte);
95ff8dfc 2279 UPDATE_SYNTAX_TABLE_FORWARD (from);
b3cfe0c8 2280 }
04882296 2281 }
abf8a9ff 2282 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
6a140a74 2283
da71ab91
KH
2284 if (code == Scomment_fence)
2285 comstyle = ST_COMMENT_STYLE;
2286 else if (code != Scomment)
04882296
KH
2287 {
2288 immediate_quit = 0;
6a140a74 2289 DEC_BOTH (from, from_byte);
ef316cf0 2290 SET_PT_BOTH (from, from_byte);
04882296
KH
2291 return Qnil;
2292 }
2293 /* We're at the start of a comment. */
95ff8dfc
RS
2294 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2295 &out_charpos, &out_bytepos, &dummy);
2296 from = out_charpos; from_byte = out_bytepos;
2297 if (!found)
04882296 2298 {
95ff8dfc
RS
2299 immediate_quit = 0;
2300 SET_PT_BOTH (from, from_byte);
2301 return Qnil;
b3cfe0c8 2302 }
95ff8dfc
RS
2303 INC_BOTH (from, from_byte);
2304 UPDATE_SYNTAX_TABLE_FORWARD (from);
04882296 2305 /* We have skipped one comment. */
840f481c 2306 count1--;
b3cfe0c8
RS
2307 }
2308
840f481c 2309 while (count1 < 0)
b3cfe0c8 2310 {
b9145dbb 2311 while (1)
b3cfe0c8 2312 {
c65adb44 2313 int quoted;
f902a008 2314
b9145dbb
RS
2315 if (from <= stop)
2316 {
6a140a74 2317 SET_PT_BOTH (BEGV, BEGV_BYTE);
b9145dbb
RS
2318 immediate_quit = 0;
2319 return Qnil;
2320 }
b3cfe0c8 2321
6a140a74 2322 DEC_BOTH (from, from_byte);
f902a008 2323 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
6a140a74 2324 quoted = char_quoted (from, from_byte);
b7dbcc19 2325 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
b3cfe0c8
RS
2326 code = SYNTAX (c);
2327 comstyle = 0;
95ff8dfc 2328 comnested = SYNTAX_COMMENT_NESTED (c);
7fc8191e
RS
2329 if (code == Sendcomment)
2330 comstyle = SYNTAX_COMMENT_STYLE (c);
b3cfe0c8 2331 if (from > stop && SYNTAX_COMEND_SECOND (c)
f902a008 2332 && prev_char_comend_first (from, from_byte)
89c6809a 2333 && !char_quoted (from - 1, dec_bytepos (from_byte)))
b3cfe0c8 2334 {
7fc8191e 2335 /* We must record the comment style encountered so that
b3cfe0c8 2336 later, we can match only the proper comment begin
7fc8191e 2337 sequence of the same style. */
6a140a74 2338 DEC_BOTH (from, from_byte);
f902a008
RS
2339 code = Sendcomment;
2340 /* Calling char_quoted, above, set up global syntax position
2341 at the new value of FROM. */
b7dbcc19 2342 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
95ff8dfc
RS
2343 comstyle = SYNTAX_COMMENT_STYLE (c1);
2344 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
b3cfe0c8
RS
2345 }
2346
195d1361
RS
2347 if (code == Scomment_fence)
2348 {
2349 /* Skip until first preceding unquoted comment_fence. */
6a140a74 2350 int found = 0, ini = from, ini_byte = from_byte;
7d0393cf 2351
6a140a74 2352 while (1)
195d1361 2353 {
6a140a74 2354 DEC_BOTH (from, from_byte);
195d1361 2355 UPDATE_SYNTAX_TABLE_BACKWARD (from);
b7dbcc19 2356 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
6a140a74 2357 if (SYNTAX (c) == Scomment_fence
7d0393cf 2358 && !char_quoted (from, from_byte))
195d1361 2359 {
7d0393cf 2360 found = 1;
195d1361
RS
2361 break;
2362 }
fcdd4585
SM
2363 else if (from == stop)
2364 break;
195d1361
RS
2365 }
2366 if (found == 0)
2367 {
2368 from = ini; /* Set point to ini + 1. */
6a140a74 2369 from_byte = ini_byte;
195d1361
RS
2370 goto leave;
2371 }
39c41ad4
SM
2372 else
2373 /* We have skipped one comment. */
2374 break;
195d1361
RS
2375 }
2376 else if (code == Sendcomment)
b3cfe0c8 2377 {
95ff8dfc 2378 found = back_comment (from, from_byte, stop, comnested, comstyle,
6a140a74 2379 &out_charpos, &out_bytepos);
1aa963c8
SM
2380 if (found == -1)
2381 {
abf8a9ff
SM
2382 if (c == '\n')
2383 /* This end-of-line is not an end-of-comment.
2384 Treat it like a whitespace.
2385 CC-mode (and maybe others) relies on this behavior. */
2386 ;
2387 else
2388 {
2389 /* Failure: we should go back to the end of this
2390 not-quite-endcomment. */
2391 if (SYNTAX(c) != code)
2392 /* It was a two-char Sendcomment. */
2393 INC_BOTH (from, from_byte);
2394 goto leave;
2395 }
1aa963c8 2396 }
2b927d02 2397 else
abf8a9ff
SM
2398 {
2399 /* We have skipped one comment. */
2400 from = out_charpos, from_byte = out_bytepos;
2401 break;
2402 }
b3cfe0c8 2403 }
bb0de084 2404 else if (code != Swhitespace || quoted)
b3cfe0c8 2405 {
195d1361 2406 leave:
b3cfe0c8 2407 immediate_quit = 0;
6a140a74 2408 INC_BOTH (from, from_byte);
ef316cf0 2409 SET_PT_BOTH (from, from_byte);
b3cfe0c8
RS
2410 return Qnil;
2411 }
2412 }
2413
840f481c 2414 count1++;
b3cfe0c8
RS
2415 }
2416
ef316cf0 2417 SET_PT_BOTH (from, from_byte);
b3cfe0c8
RS
2418 immediate_quit = 0;
2419 return Qt;
2420}
2421\f
b7dbcc19 2422/* Return syntax code of character C if C is an ASCII character
db9f48c3 2423 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
bd25db08 2424
b7dbcc19
KH
2425#define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2426 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
bd25db08
KH
2427 ? SYNTAX (c) : Ssymbol)
2428
6a140a74 2429static Lisp_Object
8489eb67 2430scan_lists (from, count, depth, sexpflag)
932e6895
SM
2431 register EMACS_INT from;
2432 EMACS_INT count, depth;
2433 int sexpflag;
8489eb67
RS
2434{
2435 Lisp_Object val;
932e6895 2436 register EMACS_INT stop = count > 0 ? ZV : BEGV;
93da5fff
KH
2437 register int c, c1;
2438 int stringterm;
8489eb67
RS
2439 int quoted;
2440 int mathexit = 0;
93da5fff 2441 register enum syntaxcode code, temp_code;
195d1361 2442 int min_depth = depth; /* Err out if depth gets less than this. */
e5d4f4dc 2443 int comstyle = 0; /* style of comment encountered */
95ff8dfc 2444 int comnested = 0; /* whether the comment is nestable or not */
932e6895
SM
2445 EMACS_INT temp_pos;
2446 EMACS_INT last_good = from;
195d1361 2447 int found;
932e6895
SM
2448 EMACS_INT from_byte;
2449 EMACS_INT out_bytepos, out_charpos;
95ff8dfc 2450 int temp, dummy;
bd25db08 2451 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
8489eb67
RS
2452
2453 if (depth > 0) min_depth = 0;
2454
c3907a7e
RS
2455 if (from > ZV) from = ZV;
2456 if (from < BEGV) from = BEGV;
2457
2458 from_byte = CHAR_TO_BYTE (from);
2459
8489eb67
RS
2460 immediate_quit = 1;
2461 QUIT;
2462
195d1361 2463 SETUP_SYNTAX_TABLE (from, count);
8489eb67
RS
2464 while (count > 0)
2465 {
8489eb67
RS
2466 while (from < stop)
2467 {
f902a008 2468 int comstart_first, prefix;
195d1361 2469 UPDATE_SYNTAX_TABLE_FORWARD (from);
b7dbcc19 2470 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
bd25db08 2471 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
f902a008 2472 comstart_first = SYNTAX_COMSTART_FIRST (c);
95ff8dfc 2473 comnested = SYNTAX_COMMENT_NESTED (c);
e6365855 2474 comstyle = SYNTAX_COMMENT_STYLE (c);
f902a008 2475 prefix = SYNTAX_PREFIX (c);
7bf5e9e4
RS
2476 if (depth == min_depth)
2477 last_good = from;
6a140a74 2478 INC_BOTH (from, from_byte);
195d1361 2479 UPDATE_SYNTAX_TABLE_FORWARD (from);
f902a008 2480 if (from < stop && comstart_first
327719ee
MB
2481 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2482 SYNTAX_COMSTART_SECOND (c))
8489eb67 2483 && parse_sexp_ignore_comments)
e5d4f4dc 2484 {
7d0393cf 2485 /* we have encountered a comment start sequence and we
195d1361 2486 are ignoring all text inside comments. We must record
e5d4f4dc
RS
2487 the comment style this sequence begins so that later,
2488 only a comment end of the same style actually ends
2489 the comment section */
2490 code = Scomment;
b7dbcc19 2491 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
95ff8dfc
RS
2492 comstyle = SYNTAX_COMMENT_STYLE (c1);
2493 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
6a140a74 2494 INC_BOTH (from, from_byte);
f902a008 2495 UPDATE_SYNTAX_TABLE_FORWARD (from);
e5d4f4dc 2496 }
7d0393cf 2497
f902a008 2498 if (prefix)
8489eb67
RS
2499 continue;
2500
0220c518 2501 switch (SWITCH_ENUM_CAST (code))
8489eb67
RS
2502 {
2503 case Sescape:
2504 case Scharquote:
8b8bf8e6
SM
2505 if (from == stop)
2506 goto lose;
6a140a74 2507 INC_BOTH (from, from_byte);
8489eb67
RS
2508 /* treat following character as a word constituent */
2509 case Sword:
2510 case Ssymbol:
2511 if (depth || !sexpflag) break;
195d1361 2512 /* This word counts as a sexp; return at end of it. */
8489eb67
RS
2513 while (from < stop)
2514 {
195d1361 2515 UPDATE_SYNTAX_TABLE_FORWARD (from);
7c7ca3d2
RS
2516
2517 /* Some compilers can't handle this inside the switch. */
b7dbcc19 2518 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
bd25db08 2519 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
7c7ca3d2 2520 switch (temp)
8489eb67
RS
2521 {
2522 case Scharquote:
2523 case Sescape:
6a140a74 2524 INC_BOTH (from, from_byte);
8b8bf8e6
SM
2525 if (from == stop)
2526 goto lose;
8489eb67
RS
2527 break;
2528 case Sword:
2529 case Ssymbol:
2530 case Squote:
2531 break;
2532 default:
2533 goto done;
2534 }
6a140a74 2535 INC_BOTH (from, from_byte);
8489eb67
RS
2536 }
2537 goto done;
2538
195d1361 2539 case Scomment_fence:
95ff8dfc
RS
2540 comstyle = ST_COMMENT_STYLE;
2541 /* FALLTHROUGH */
2542 case Scomment:
8489eb67 2543 if (!parse_sexp_ignore_comments) break;
95ff8dfc
RS
2544 UPDATE_SYNTAX_TABLE_FORWARD (from);
2545 found = forw_comment (from, from_byte, stop,
2546 comnested, comstyle, 0,
2547 &out_charpos, &out_bytepos, &dummy);
2548 from = out_charpos, from_byte = out_bytepos;
2549 if (!found)
8489eb67 2550 {
95ff8dfc
RS
2551 if (depth == 0)
2552 goto done;
2553 goto lose;
8489eb67 2554 }
95ff8dfc
RS
2555 INC_BOTH (from, from_byte);
2556 UPDATE_SYNTAX_TABLE_FORWARD (from);
8489eb67
RS
2557 break;
2558
2559 case Smath:
2560 if (!sexpflag)
2561 break;
b7dbcc19 2562 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
6a140a74
RS
2563 {
2564 INC_BOTH (from, from_byte);
2565 }
8489eb67
RS
2566 if (mathexit)
2567 {
2568 mathexit = 0;
2569 goto close1;
2570 }
2571 mathexit = 1;
2572
2573 case Sopen:
2574 if (!++depth) goto done;
2575 break;
2576
2577 case Sclose:
2578 close1:
2579 if (!--depth) goto done;
2580 if (depth < min_depth)
9471945b
KS
2581 xsignal3 (Qscan_error,
2582 build_string ("Containing expression ends prematurely"),
2583 make_number (last_good), make_number (from));
8489eb67
RS
2584 break;
2585
2586 case Sstring:
195d1361 2587 case Sstring_fence:
ef316cf0 2588 temp_pos = dec_bytepos (from_byte);
b7dbcc19 2589 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
8489eb67
RS
2590 while (1)
2591 {
8b8bf8e6
SM
2592 if (from >= stop)
2593 goto lose;
195d1361 2594 UPDATE_SYNTAX_TABLE_FORWARD (from);
b7dbcc19 2595 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
42ebfa31
SM
2596 if (code == Sstring
2597 ? (c == stringterm
2598 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
bd25db08 2599 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
195d1361 2600 break;
7c7ca3d2
RS
2601
2602 /* Some compilers can't handle this inside the switch. */
bd25db08 2603 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
7c7ca3d2 2604 switch (temp)
8489eb67
RS
2605 {
2606 case Scharquote:
2607 case Sescape:
6a140a74 2608 INC_BOTH (from, from_byte);
8489eb67 2609 }
6a140a74 2610 INC_BOTH (from, from_byte);
8489eb67 2611 }
6a140a74 2612 INC_BOTH (from, from_byte);
8489eb67
RS
2613 if (!depth && sexpflag) goto done;
2614 break;
240c43e8
SM
2615 default:
2616 /* Ignore whitespace, punctuation, quote, endcomment. */
2617 break;
8489eb67
RS
2618 }
2619 }
2620
2621 /* Reached end of buffer. Error if within object, return nil if between */
8b8bf8e6
SM
2622 if (depth)
2623 goto lose;
8489eb67
RS
2624
2625 immediate_quit = 0;
2626 return Qnil;
2627
2628 /* End of object reached */
2629 done:
2630 count--;
2631 }
2632
2633 while (count < 0)
2634 {
8489eb67
RS
2635 while (from > stop)
2636 {
6a140a74 2637 DEC_BOTH (from, from_byte);
195d1361 2638 UPDATE_SYNTAX_TABLE_BACKWARD (from);
b7dbcc19 2639 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
bd25db08 2640 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
7bf5e9e4
RS
2641 if (depth == min_depth)
2642 last_good = from;
7fc8191e 2643 comstyle = 0;
95ff8dfc 2644 comnested = SYNTAX_COMMENT_NESTED (c);
7fc8191e
RS
2645 if (code == Sendcomment)
2646 comstyle = SYNTAX_COMMENT_STYLE (c);
8489eb67 2647 if (from > stop && SYNTAX_COMEND_SECOND (c)
1674d9a2 2648 && prev_char_comend_first (from, from_byte)
8489eb67 2649 && parse_sexp_ignore_comments)
e5d4f4dc 2650 {
f902a008 2651 /* We must record the comment style encountered so that
e5d4f4dc 2652 later, we can match only the proper comment begin
f902a008 2653 sequence of the same style. */
6a140a74 2654 DEC_BOTH (from, from_byte);
f902a008
RS
2655 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2656 code = Sendcomment;
b7dbcc19 2657 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
95ff8dfc
RS
2658 comstyle = SYNTAX_COMMENT_STYLE (c1);
2659 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
e5d4f4dc 2660 }
7d0393cf 2661
9828a477 2662 /* Quoting turns anything except a comment-ender
e6365855 2663 into a word character. Note that this cannot be true
f902a008 2664 if we decremented FROM in the if-statement above. */
9828a477 2665 if (code != Sendcomment && char_quoted (from, from_byte))
240c43e8
SM
2666 {
2667 DEC_BOTH (from, from_byte);
2668 code = Sword;
2669 }
9828a477 2670 else if (SYNTAX_PREFIX (c))
8489eb67
RS
2671 continue;
2672
9828a477 2673 switch (SWITCH_ENUM_CAST (code))
8489eb67
RS
2674 {
2675 case Sword:
2676 case Ssymbol:
9828a477
KH
2677 case Sescape:
2678 case Scharquote:
8489eb67 2679 if (depth || !sexpflag) break;
195d1361
RS
2680 /* This word counts as a sexp; count object finished
2681 after passing it. */
8489eb67
RS
2682 while (from > stop)
2683 {
6a140a74 2684 temp_pos = from_byte;
ef316cf0
RS
2685 if (! NILP (current_buffer->enable_multibyte_characters))
2686 DEC_POS (temp_pos);
2687 else
2688 temp_pos--;
6a140a74 2689 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
b7dbcc19 2690 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
bd25db08 2691 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
9828a477
KH
2692 /* Don't allow comment-end to be quoted. */
2693 if (temp_code == Sendcomment)
2694 goto done2;
6a140a74 2695 quoted = char_quoted (from - 1, temp_pos);
8489eb67 2696 if (quoted)
93da5fff 2697 {
6a140a74 2698 DEC_BOTH (from, from_byte);
ef316cf0 2699 temp_pos = dec_bytepos (temp_pos);
6a140a74 2700 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
93da5fff 2701 }
b7dbcc19 2702 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
bd25db08 2703 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
93da5fff
KH
2704 if (! (quoted || temp_code == Sword
2705 || temp_code == Ssymbol
2706 || temp_code == Squote))
8489eb67 2707 goto done2;
6a140a74 2708 DEC_BOTH (from, from_byte);
8489eb67
RS
2709 }
2710 goto done2;
2711
2712 case Smath:
2713 if (!sexpflag)
2714 break;
ef316cf0 2715 temp_pos = dec_bytepos (from_byte);
6a140a74 2716 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
b7dbcc19 2717 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
6a140a74 2718 DEC_BOTH (from, from_byte);
8489eb67
RS
2719 if (mathexit)
2720 {
2721 mathexit = 0;
2722 goto open2;
2723 }
2724 mathexit = 1;
2725
2726 case Sclose:
2727 if (!++depth) goto done2;
2728 break;
2729
2730 case Sopen:
2731 open2:
2732 if (!--depth) goto done2;
2733 if (depth < min_depth)
9471945b
KS
2734 xsignal3 (Qscan_error,
2735 build_string ("Containing expression ends prematurely"),
2736 make_number (last_good), make_number (from));
8489eb67
RS
2737 break;
2738
2739 case Sendcomment:
2740 if (!parse_sexp_ignore_comments)
2741 break;
95ff8dfc 2742 found = back_comment (from, from_byte, stop, comnested, comstyle,
6a140a74 2743 &out_charpos, &out_bytepos);
1aa963c8
SM
2744 /* FIXME: if found == -1, then it really wasn't a comment-end.
2745 For single-char Sendcomment, we can't do much about it apart
2746 from skipping the char.
2747 For 2-char endcomments, we could try again, taking both
2748 chars as separate entities, but it's a lot of trouble
2749 for very little gain, so we don't bother either. -sm */
6a140a74
RS
2750 if (found != -1)
2751 from = out_charpos, from_byte = out_bytepos;
8489eb67
RS
2752 break;
2753
195d1361
RS
2754 case Scomment_fence:
2755 case Sstring_fence:
2756 while (1)
2757 {
8b8bf8e6
SM
2758 if (from == stop)
2759 goto lose;
6b61353c 2760 DEC_BOTH (from, from_byte);
195d1361 2761 UPDATE_SYNTAX_TABLE_BACKWARD (from);
7d0393cf 2762 if (!char_quoted (from, from_byte)
b7dbcc19 2763 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
bd25db08 2764 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
195d1361
RS
2765 break;
2766 }
2767 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2768 break;
7d0393cf 2769
8489eb67 2770 case Sstring:
b7dbcc19 2771 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
8489eb67
RS
2772 while (1)
2773 {
8b8bf8e6
SM
2774 if (from == stop)
2775 goto lose;
6b61353c
KH
2776 DEC_BOTH (from, from_byte);
2777 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2778 if (!char_quoted (from, from_byte)
2779 && (stringterm
2780 == (c = FETCH_CHAR_AS_MULTIBYTE (from_byte)))
42ebfa31 2781 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
8489eb67 2782 break;
8489eb67 2783 }
8489eb67
RS
2784 if (!depth && sexpflag) goto done2;
2785 break;
240c43e8
SM
2786 default:
2787 /* Ignore whitespace, punctuation, quote, endcomment. */
2788 break;
8489eb67
RS
2789 }
2790 }
2791
2792 /* Reached start of buffer. Error if within object, return nil if between */
8b8bf8e6
SM
2793 if (depth)
2794 goto lose;
8489eb67
RS
2795
2796 immediate_quit = 0;
2797 return Qnil;
2798
2799 done2:
2800 count++;
2801 }
2802
2803
2804 immediate_quit = 0;
1e142fb7 2805 XSETFASTINT (val, from);
8489eb67
RS
2806 return val;
2807
2808 lose:
9471945b
KS
2809 xsignal3 (Qscan_error,
2810 build_string ("Unbalanced parentheses"),
2811 make_number (last_good), make_number (from));
8489eb67
RS
2812}
2813
8489eb67 2814DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
fdb82f93
PJ
2815 doc: /* Scan from character number FROM by COUNT lists.
2816Returns the character number of the position thus found.
2817
2818If DEPTH is nonzero, paren depth begins counting from that value,
2819only places where the depth in parentheses becomes zero
2820are candidates for stopping; COUNT such places are counted.
2821Thus, a positive value for DEPTH means go out levels.
2822
2823Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2824
2825If the beginning or end of (the accessible part of) the buffer is reached
2826and the depth is wrong, an error is signaled.
2827If the depth is right but the count is not used up, nil is returned. */)
2828 (from, count, depth)
8489eb67
RS
2829 Lisp_Object from, count, depth;
2830{
b7826503
PJ
2831 CHECK_NUMBER (from);
2832 CHECK_NUMBER (count);
2833 CHECK_NUMBER (depth);
8489eb67
RS
2834
2835 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2836}
2837
2838DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
fdb82f93
PJ
2839 doc: /* Scan from character number FROM by COUNT balanced expressions.
2840If COUNT is negative, scan backwards.
2841Returns the character number of the position thus found.
2842
2843Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2844
2845If the beginning or end of (the accessible part of) the buffer is reached
2846in the middle of a parenthetical grouping, an error is signaled.
2847If the beginning or end is reached between groupings
2848but before count is used up, nil is returned. */)
2849 (from, count)
8489eb67
RS
2850 Lisp_Object from, count;
2851{
b7826503
PJ
2852 CHECK_NUMBER (from);
2853 CHECK_NUMBER (count);
8489eb67
RS
2854
2855 return scan_lists (XINT (from), XINT (count), 0, 1);
2856}
2857
2858DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
fdb82f93
PJ
2859 0, 0, 0,
2860 doc: /* Move point backward over any number of chars with prefix syntax.
2861This includes chars with "quote" or "prefix" syntax (' or p). */)
2862 ()
8489eb67
RS
2863{
2864 int beg = BEGV;
6a140a74
RS
2865 int opoint = PT;
2866 int opoint_byte = PT_BYTE;
6ec8bbd2 2867 int pos = PT;
6a140a74 2868 int pos_byte = PT_BYTE;
93da5fff 2869 int c;
93da5fff 2870
7d0393cf 2871 if (pos <= beg)
195d1361 2872 {
f902a008
RS
2873 SET_PT_BOTH (opoint, opoint_byte);
2874
2875 return Qnil;
195d1361 2876 }
8489eb67 2877
f902a008
RS
2878 SETUP_SYNTAX_TABLE (pos, -1);
2879
6a140a74
RS
2880 DEC_BOTH (pos, pos_byte);
2881
1fd3172d 2882 while (!char_quoted (pos, pos_byte)
195d1361 2883 /* Previous statement updates syntax table. */
b7dbcc19 2884 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
93da5fff
KH
2885 || SYNTAX_PREFIX (c)))
2886 {
1fd3172d
RS
2887 opoint = pos;
2888 opoint_byte = pos_byte;
2889
2890 if (pos + 1 > beg)
2891 DEC_BOTH (pos, pos_byte);
93da5fff 2892 }
8489eb67 2893
6a140a74 2894 SET_PT_BOTH (opoint, opoint_byte);
8489eb67
RS
2895
2896 return Qnil;
2897}
2898\f
6a140a74 2899/* Parse forward from FROM / FROM_BYTE to END,
e5d4f4dc
RS
2900 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2901 and return a description of the state of the parse at END.
c81a3712 2902 If STOPBEFORE is nonzero, stop at the start of an atom.
644ea4df
RS
2903 If COMMENTSTOP is 1, stop at the start of a comment.
2904 If COMMENTSTOP is -1, stop at the start or end of a comment,
2905 after the beginning of a string, or after the end of a string. */
8489eb67 2906
340f92b5 2907static void
6a140a74 2908scan_sexps_forward (stateptr, from, from_byte, end, targetdepth,
c81a3712 2909 stopbefore, oldstate, commentstop)
e5d4f4dc 2910 struct lisp_parse_state *stateptr;
8e2911c2
AS
2911 register EMACS_INT from;
2912 EMACS_INT from_byte, end;
2913 int targetdepth, stopbefore;
8489eb67 2914 Lisp_Object oldstate;
c81a3712 2915 int commentstop;
8489eb67
RS
2916{
2917 struct lisp_parse_state state;
2918
2919 register enum syntaxcode code;
95ff8dfc
RS
2920 int c1;
2921 int comnested;
8489eb67
RS
2922 struct level { int last, prev; };
2923 struct level levelstart[100];
2924 register struct level *curlevel = levelstart;
2925 struct level *endlevel = levelstart + 100;
8489eb67
RS
2926 register int depth; /* Paren depth of current scanning location.
2927 level - levelstart equals this except
2928 when the depth becomes negative. */
2929 int mindepth; /* Lowest DEPTH value seen. */
2930 int start_quoted = 0; /* Nonzero means starting after a char quote */
2931 Lisp_Object tem;
8e2911c2
AS
2932 EMACS_INT prev_from; /* Keep one character before FROM. */
2933 EMACS_INT prev_from_byte;
1fd3172d 2934 int prev_from_syntax;
195d1361
RS
2935 int boundary_stop = commentstop == -1;
2936 int nofence;
95ff8dfc 2937 int found;
60c86a83 2938 EMACS_INT out_bytepos, out_charpos;
7c7ca3d2 2939 int temp;
93da5fff
KH
2940
2941 prev_from = from;
6a140a74
RS
2942 prev_from_byte = from_byte;
2943 if (from != BEGV)
2944 DEC_BOTH (prev_from, prev_from_byte);
93da5fff
KH
2945
2946 /* Use this macro instead of `from++'. */
6a140a74
RS
2947#define INC_FROM \
2948do { prev_from = from; \
2949 prev_from_byte = from_byte; \
327719ee 2950 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
ab229fdd 2951 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
ef316cf0 2952 INC_BOTH (from, from_byte); \
3ceb4629
SM
2953 if (from < end) \
2954 UPDATE_SYNTAX_TABLE_FORWARD (from); \
6a140a74 2955 } while (0)
8489eb67
RS
2956
2957 immediate_quit = 1;
2958 QUIT;
2959
265a9e55 2960 if (NILP (oldstate))
8489eb67
RS
2961 {
2962 depth = 0;
2963 state.instring = -1;
2964 state.incomment = 0;
195d1361
RS
2965 state.comstyle = 0; /* comment style a by default. */
2966 state.comstr_start = -1; /* no comment/string seen. */
8489eb67
RS
2967 }
2968 else
2969 {
2970 tem = Fcar (oldstate);
265a9e55 2971 if (!NILP (tem))
8489eb67
RS
2972 depth = XINT (tem);
2973 else
2974 depth = 0;
2975
2976 oldstate = Fcdr (oldstate);
2977 oldstate = Fcdr (oldstate);
2978 oldstate = Fcdr (oldstate);
2979 tem = Fcar (oldstate);
195d1361 2980 /* Check whether we are inside string_fence-style string: */
7d0393cf
JB
2981 state.instring = (!NILP (tem)
2982 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
e76f1c44 2983 : -1);
8489eb67
RS
2984
2985 oldstate = Fcdr (oldstate);
2986 tem = Fcar (oldstate);
e76f1c44
GM
2987 state.incomment = (!NILP (tem)
2988 ? (INTEGERP (tem) ? XINT (tem) : -1)
95ff8dfc 2989 : 0);
8489eb67
RS
2990
2991 oldstate = Fcdr (oldstate);
2992 tem = Fcar (oldstate);
265a9e55 2993 start_quoted = !NILP (tem);
e5d4f4dc 2994
95ff8dfc 2995 /* if the eighth element of the list is nil, we are in comment
195d1361
RS
2996 style a. If it is non-nil, we are in comment style b */
2997 oldstate = Fcdr (oldstate);
e5d4f4dc 2998 oldstate = Fcdr (oldstate);
195d1361 2999 tem = Fcar (oldstate);
7d0393cf 3000 state.comstyle = NILP (tem) ? 0 : (EQ (tem, Qsyntax_table)
e76f1c44 3001 ? ST_COMMENT_STYLE : 1);
195d1361 3002
e5d4f4dc 3003 oldstate = Fcdr (oldstate);
e5d4f4dc 3004 tem = Fcar (oldstate);
195d1361 3005 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
1a102a58
RS
3006 oldstate = Fcdr (oldstate);
3007 tem = Fcar (oldstate);
3008 while (!NILP (tem)) /* >= second enclosing sexps. */
3009 {
3010 /* curlevel++->last ran into compiler bug on Apollo */
3011 curlevel->last = XINT (Fcar (tem));
3012 if (++curlevel == endlevel)
02010917 3013 curlevel--; /* error ("Nesting too deep for parser"); */
1a102a58
RS
3014 curlevel->prev = -1;
3015 curlevel->last = -1;
3016 tem = Fcdr (tem);
3017 }
8489eb67
RS
3018 }
3019 state.quoted = 0;
3020 mindepth = depth;
3021
3022 curlevel->prev = -1;
3023 curlevel->last = -1;
3024
326b283b 3025 SETUP_SYNTAX_TABLE (prev_from, 1);
ab229fdd
AS
3026 temp = FETCH_CHAR (prev_from_byte);
3027 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
3ceb4629 3028 UPDATE_SYNTAX_TABLE_FORWARD (from);
326b283b 3029
195d1361 3030 /* Enter the loop at a place appropriate for initial state. */
8489eb67 3031
326b283b
RS
3032 if (state.incomment)
3033 goto startincomment;
8489eb67
RS
3034 if (state.instring >= 0)
3035 {
195d1361 3036 nofence = state.instring != ST_STRING_STYLE;
326b283b
RS
3037 if (start_quoted)
3038 goto startquotedinstring;
8489eb67
RS
3039 goto startinstring;
3040 }
326b283b
RS
3041 else if (start_quoted)
3042 goto startquoted;
1fd3172d 3043
8489eb67
RS
3044 while (from < end)
3045 {
93da5fff 3046 INC_FROM;
1fd3172d 3047 code = prev_from_syntax & 0xff;
4c920633 3048
c5d4b96f
SM
3049 if (from < end
3050 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3051 && (c1 = FETCH_CHAR (from_byte),
3052 SYNTAX_COMSTART_SECOND (c1)))
3053 /* Duplicate code to avoid a complex if-expression
3054 which causes trouble for the SGI compiler. */
95ff8dfc 3055 {
c5d4b96f
SM
3056 /* Record the comment style we have entered so that only
3057 the comment-end sequence of the same style actually
3058 terminates the comment section. */
3059 state.comstyle = SYNTAX_COMMENT_STYLE (c1);
3060 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
3061 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
3062 state.incomment = comnested ? 1 : -1;
95ff8dfc 3063 state.comstr_start = prev_from;
c5d4b96f
SM
3064 INC_FROM;
3065 code = Scomment;
95ff8dfc 3066 }
4c920633 3067 else if (code == Scomment_fence)
e5d4f4dc
RS
3068 {
3069 /* Record the comment style we have entered so that only
3070 the comment-end sequence of the same style actually
3071 terminates the comment section. */
95ff8dfc
RS
3072 state.comstyle = ST_COMMENT_STYLE;
3073 state.incomment = -1;
195d1361 3074 state.comstr_start = prev_from;
e5d4f4dc 3075 code = Scomment;
e5d4f4dc 3076 }
c5d4b96f
SM
3077 else if (code == Scomment)
3078 {
3079 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax);
3080 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3081 1 : -1);
3082 state.comstr_start = prev_from;
3083 }
e5d4f4dc 3084
1fd3172d 3085 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
8489eb67 3086 continue;
0220c518 3087 switch (SWITCH_ENUM_CAST (code))
8489eb67
RS
3088 {
3089 case Sescape:
3090 case Scharquote:
3091 if (stopbefore) goto stop; /* this arg means stop at sexp start */
93da5fff 3092 curlevel->last = prev_from;
8489eb67
RS
3093 startquoted:
3094 if (from == end) goto endquoted;
93da5fff 3095 INC_FROM;
8489eb67
RS
3096 goto symstarted;
3097 /* treat following character as a word constituent */
3098 case Sword:
3099 case Ssymbol:
3100 if (stopbefore) goto stop; /* this arg means stop at sexp start */
93da5fff 3101 curlevel->last = prev_from;
8489eb67
RS
3102 symstarted:
3103 while (from < end)
3104 {
7c7ca3d2 3105 /* Some compilers can't handle this inside the switch. */
327719ee 3106 temp = FETCH_CHAR_AS_MULTIBYTE (from_byte);
ab229fdd 3107 temp = SYNTAX (temp);
7c7ca3d2 3108 switch (temp)
8489eb67
RS
3109 {
3110 case Scharquote:
3111 case Sescape:
93da5fff 3112 INC_FROM;
8489eb67
RS
3113 if (from == end) goto endquoted;
3114 break;
3115 case Sword:
3116 case Ssymbol:
3117 case Squote:
3118 break;
3119 default:
3120 goto symdone;
3121 }
93da5fff 3122 INC_FROM;
8489eb67
RS
3123 }
3124 symdone:
3125 curlevel->prev = curlevel->last;
3126 break;
3127
240c43e8 3128 case Scomment_fence: /* Can't happen because it's handled above. */
8489eb67 3129 case Scomment:
195d1361 3130 if (commentstop || boundary_stop) goto done;
d355bd8a
SM
3131 startincomment:
3132 /* The (from == BEGV) test was to enter the loop in the middle so
95ff8dfc 3133 that we find a 2-char comment ender even if we start in the
e6365855 3134 middle of it. We don't want to do that if we're just at the
d355bd8a 3135 beginning of the comment (think of (*) ... (*)). */
95ff8dfc
RS
3136 found = forw_comment (from, from_byte, end,
3137 state.incomment, state.comstyle,
e6365855
SM
3138 (from == BEGV || from < state.comstr_start + 3)
3139 ? 0 : prev_from_syntax,
95ff8dfc
RS
3140 &out_charpos, &out_bytepos, &state.incomment);
3141 from = out_charpos; from_byte = out_bytepos;
3142 /* Beware! prev_from and friends are invalid now.
3143 Luckily, the `done' doesn't use them and the INC_FROM
3144 sets them to a sane value without looking at them. */
3145 if (!found) goto done;
d355bd8a 3146 INC_FROM;
8489eb67 3147 state.incomment = 0;
e5d4f4dc 3148 state.comstyle = 0; /* reset the comment style */
195d1361 3149 if (boundary_stop) goto done;
8489eb67
RS
3150 break;
3151
3152 case Sopen:
3153 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3154 depth++;
3155 /* curlevel++->last ran into compiler bug on Apollo */
93da5fff 3156 curlevel->last = prev_from;
8489eb67 3157 if (++curlevel == endlevel)
02010917 3158 curlevel--; /* error ("Nesting too deep for parser"); */
8489eb67
RS
3159 curlevel->prev = -1;
3160 curlevel->last = -1;
30844415 3161 if (targetdepth == depth) goto done;
8489eb67
RS
3162 break;
3163
3164 case Sclose:
3165 depth--;
3166 if (depth < mindepth)
3167 mindepth = depth;
3168 if (curlevel != levelstart)
3169 curlevel--;
3170 curlevel->prev = curlevel->last;
30844415 3171 if (targetdepth == depth) goto done;
8489eb67
RS
3172 break;
3173
3174 case Sstring:
195d1361
RS
3175 case Sstring_fence:
3176 state.comstr_start = from - 1;
8489eb67 3177 if (stopbefore) goto stop; /* this arg means stop at sexp start */
93da5fff 3178 curlevel->last = prev_from;
7d0393cf 3179 state.instring = (code == Sstring
b7dbcc19 3180 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
195d1361
RS
3181 : ST_STRING_STYLE);
3182 if (boundary_stop) goto done;
8489eb67 3183 startinstring:
195d1361 3184 {
644ea4df 3185 nofence = state.instring != ST_STRING_STYLE;
7d0393cf 3186
644ea4df
RS
3187 while (1)
3188 {
3189 int c;
195d1361 3190
644ea4df 3191 if (from >= end) goto done;
b7dbcc19 3192 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
7c7ca3d2
RS
3193 /* Some compilers can't handle this inside the switch. */
3194 temp = SYNTAX (c);
ccf89641
KH
3195
3196 /* Check TEMP here so that if the char has
3197 a syntax-table property which says it is NOT
3198 a string character, it does not end the string. */
3199 if (nofence && c == state.instring && temp == Sstring)
3200 break;
3201
7c7ca3d2 3202 switch (temp)
644ea4df
RS
3203 {
3204 case Sstring_fence:
3205 if (!nofence) goto string_end;
3206 break;
3207 case Scharquote:
3208 case Sescape:
3209 INC_FROM;
3210 startquotedinstring:
3211 if (from >= end) goto endquoted;
195d1361 3212 }
644ea4df
RS
3213 INC_FROM;
3214 }
195d1361
RS
3215 }
3216 string_end:
8489eb67
RS
3217 state.instring = -1;
3218 curlevel->prev = curlevel->last;
93da5fff 3219 INC_FROM;
195d1361 3220 if (boundary_stop) goto done;
8489eb67
RS
3221 break;
3222
3223 case Smath:
240c43e8
SM
3224 /* FIXME: We should do something with it. */
3225 break;
3226 default:
3227 /* Ignore whitespace, punctuation, quote, endcomment. */
8489eb67
RS
3228 break;
3229 }
3230 }
3231 goto done;
3232
3233 stop: /* Here if stopping before start of sexp. */
93da5fff 3234 from = prev_from; /* We have just fetched the char that starts it; */
8489eb67
RS
3235 goto done; /* but return the position before it. */
3236
3237 endquoted:
3238 state.quoted = 1;
3239 done:
3240 state.depth = depth;
3241 state.mindepth = mindepth;
3242 state.thislevelstart = curlevel->prev;
3243 state.prevlevelstart
3244 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3245 state.location = from;
1a102a58
RS
3246 state.levelstarts = Qnil;
3247 while (--curlevel >= levelstart)
3248 state.levelstarts = Fcons (make_number (curlevel->last),
3249 state.levelstarts);
8489eb67
RS
3250 immediate_quit = 0;
3251
e5d4f4dc 3252 *stateptr = state;
8489eb67
RS
3253}
3254
c81a3712 3255DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
fdb82f93
PJ
3256 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3257Parsing stops at TO or when certain criteria are met;
3258 point is set to where parsing stops.
3259If fifth arg OLDSTATE is omitted or nil,
3260 parsing assumes that FROM is the beginning of a function.
0e6228bc 3261Value is a list of elements describing final state of parsing:
fdb82f93
PJ
3262 0. depth in parens.
3263 1. character address of start of innermost containing list; nil if none.
3264 2. character address of start of last complete sexp terminated.
3265 3. non-nil if inside a string.
3266 (it is the character that will terminate the string,
3267 or t if the string should be terminated by a generic string delimiter.)
7d0393cf 3268 4. nil if outside a comment, t if inside a non-nestable comment,
fdb82f93
PJ
3269 else an integer (the current comment nesting).
3270 5. t if following a quote character.
3271 6. the minimum paren-depth encountered during this scan.
3272 7. t if in a comment of style b; symbol `syntax-table' if the comment
3273 should be terminated by a generic comment delimiter.
3274 8. character address of start of comment or string; nil if not in one.
3275 9. Intermediate data for continuation of parsing (subject to change).
3276If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3277in parentheses becomes equal to TARGETDEPTH.
3278Fourth arg STOPBEFORE non-nil means stop when come to
3279 any character that starts a sexp.
0e6228bc 3280Fifth arg OLDSTATE is a list like what this function returns.
fdb82f93 3281 It is used to initialize the state of the parse. Elements number 1, 2, 6
43726c05 3282 and 8 are ignored.
fdb82f93
PJ
3283Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3284 If it is symbol `syntax-table', stop after the start of a comment or a
3285 string, or after end of a comment or a string. */)
3286 (from, to, targetdepth, stopbefore, oldstate, commentstop)
c81a3712 3287 Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop;
8489eb67
RS
3288{
3289 struct lisp_parse_state state;
3290 int target;
3291
265a9e55 3292 if (!NILP (targetdepth))
8489eb67 3293 {
b7826503 3294 CHECK_NUMBER (targetdepth);
8489eb67
RS
3295 target = XINT (targetdepth);
3296 }
3297 else
3298 target = -100000; /* We won't reach this depth */
3299
3300 validate_region (&from, &to);
6a140a74
RS
3301 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3302 XINT (to),
c81a3712 3303 target, !NILP (stopbefore), oldstate,
7d0393cf 3304 (NILP (commentstop)
195d1361 3305 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
8489eb67
RS
3306
3307 SET_PT (state.location);
7d0393cf 3308
8489eb67
RS
3309 return Fcons (make_number (state.depth),
3310 Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart),
3311 Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart),
7d0393cf
JB
3312 Fcons (state.instring >= 0
3313 ? (state.instring == ST_STRING_STYLE
195d1361 3314 ? Qt : make_number (state.instring)) : Qnil,
95ff8dfc
RS
3315 Fcons (state.incomment < 0 ? Qt :
3316 (state.incomment == 0 ? Qnil :
3317 make_number (state.incomment)),
8489eb67 3318 Fcons (state.quoted ? Qt : Qnil,
bff37d2a 3319 Fcons (make_number (state.mindepth),
7d0393cf 3320 Fcons ((state.comstyle
bff37d2a
RS
3321 ? (state.comstyle == ST_COMMENT_STYLE
3322 ? Qsyntax_table : Qt) :
3323 Qnil),
0beaf54f
DL
3324 Fcons (((state.incomment
3325 || (state.instring >= 0))
bff37d2a
RS
3326 ? make_number (state.comstr_start)
3327 : Qnil),
1a102a58 3328 Fcons (state.levelstarts, Qnil))))))))));
8489eb67
RS
3329}
3330\f
dfcf069d 3331void
8489eb67
RS
3332init_syntax_once ()
3333{
78f9a1f7 3334 register int i, c;
8ea151b2 3335 Lisp_Object temp;
8489eb67 3336
5ebaddf5 3337 /* This has to be done here, before we call Fmake_char_table. */
d67b4f80 3338 Qsyntax_table = intern_c_string ("syntax-table");
5ebaddf5
RS
3339 staticpro (&Qsyntax_table);
3340
d67b4f80 3341 /* Intern_C_String this now in case it isn't already done.
5ebaddf5
RS
3342 Setting this variable twice is harmless.
3343 But don't staticpro it here--that is done in alloc.c. */
d67b4f80 3344 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
5ebaddf5 3345
93da5fff 3346 /* Create objects which can be shared among syntax tables. */
42ebfa31 3347 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
93da5fff
KH
3348 for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++)
3349 XVECTOR (Vsyntax_code_object)->contents[i]
3350 = Fcons (make_number (i), Qnil);
3351
5ebaddf5
RS
3352 /* Now we are ready to set up this property, so we can
3353 create syntax tables. */
3354 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3355
93da5fff 3356 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
8489eb67 3357
5ebaddf5 3358 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
8489eb67 3359
aa7b08b4
RS
3360 /* Control characters should not be whitespace. */
3361 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3362 for (i = 0; i <= ' ' - 1; i++)
3363 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3364 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3365
3366 /* Except that a few really are whitespace. */
3367 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3368 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3369 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3370 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3371 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3372 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3373
93da5fff 3374 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
8489eb67 3375 for (i = 'a'; i <= 'z'; i++)
8ea151b2 3376 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
8489eb67 3377 for (i = 'A'; i <= 'Z'; i++)
8ea151b2 3378 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
8489eb67 3379 for (i = '0'; i <= '9'; i++)
8ea151b2
RS
3380 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3381
3382 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3383 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3384
3385 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3386 Fcons (make_number (Sopen), make_number (')')));
3387 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3388 Fcons (make_number (Sclose), make_number ('(')));
3389 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3390 Fcons (make_number (Sopen), make_number (']')));
3391 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3392 Fcons (make_number (Sclose), make_number ('[')));
3393 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3394 Fcons (make_number (Sopen), make_number ('}')));
3395 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3396 Fcons (make_number (Sclose), make_number ('{')));
3397 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3398 Fcons (make_number ((int) Sstring), Qnil));
3399 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3400 Fcons (make_number ((int) Sescape), Qnil));
3401
93da5fff 3402 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
8489eb67 3403 for (i = 0; i < 10; i++)
78f9a1f7
KH
3404 {
3405 c = "_-+*/&|<>="[i];
3406 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3407 }
8489eb67 3408
93da5fff 3409 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
8489eb67 3410 for (i = 0; i < 12; i++)
78f9a1f7
KH
3411 {
3412 c = ".,;:?!#@~^'`"[i];
3413 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3414 }
bd25db08
KH
3415
3416 /* All multibyte characters have syntax `word' by default. */
3417 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
5c7b02ab 3418 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
8489eb67
RS
3419}
3420
dfcf069d 3421void
8489eb67
RS
3422syms_of_syntax ()
3423{
d67b4f80 3424 Qsyntax_table_p = intern_c_string ("syntax-table-p");
8489eb67
RS
3425 staticpro (&Qsyntax_table_p);
3426
93da5fff
KH
3427 staticpro (&Vsyntax_code_object);
3428
a2994c46
KS
3429 staticpro (&gl_state.object);
3430 staticpro (&gl_state.global_code);
3431 staticpro (&gl_state.current_syntax_table);
3432 staticpro (&gl_state.old_prop);
3433
3434 /* Defined in regex.c */
3435 staticpro (&re_match_object);
3436
d67b4f80 3437 Qscan_error = intern_c_string ("scan-error");
7bf5e9e4
RS
3438 staticpro (&Qscan_error);
3439 Fput (Qscan_error, Qerror_conditions,
d67b4f80 3440 pure_cons (Qscan_error, pure_cons (Qerror, Qnil)));
7bf5e9e4 3441 Fput (Qscan_error, Qerror_message,
d67b4f80 3442 make_pure_c_string ("Scan error"));
7bf5e9e4 3443
8489eb67 3444 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
fdb82f93 3445 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
8489eb67 3446
195d1361 3447 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties,
fdb82f93
PJ
3448 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3449Otherwise, that text property is simply ignored.
3450See the info node `(elisp)Syntax Properties' for a description of the
3451`syntax-table' property. */);
195d1361 3452
8489eb67
RS
3453 words_include_escapes = 0;
3454 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
fdb82f93 3455 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
8489eb67 3456
bd25db08 3457 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol,
fdb82f93 3458 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
bd25db08
KH
3459 multibyte_syntax_as_symbol = 0;
3460
f4ed767f
GM
3461 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3462 &open_paren_in_column_0_is_defun_start,
b222e415 3463 doc: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
f4ed767f
GM
3464 open_paren_in_column_0_is_defun_start = 1;
3465
8f924df7
KH
3466
3467 DEFVAR_LISP ("find-word-boundary-function-table",
3468 &Vfind_word_boundary_function_table,
869bb237 3469 doc: /*
8f924df7 3470Char table of functions to search for the word boundary.
869bb237
KH
3471Each function is called with two arguments; POS and LIMIT.
3472POS and LIMIT are character positions in the current buffer.
3473
3474If POS is less than LIMIT, POS is at the first character of a word,
3475and the return value of a function is a position after the last
3476character of that word.
3477
3478If POS is not less than LIMIT, POS is at the last character of a word,
3479and the return value of a function is a position at the first
3480character of that word.
3481
3482In both cases, LIMIT bounds the search. */);
8f924df7 3483 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
869bb237 3484
8489eb67
RS
3485 defsubr (&Ssyntax_table_p);
3486 defsubr (&Ssyntax_table);
3487 defsubr (&Sstandard_syntax_table);
3488 defsubr (&Scopy_syntax_table);
3489 defsubr (&Sset_syntax_table);
3490 defsubr (&Schar_syntax);
beefa22e 3491 defsubr (&Smatching_paren);
c65adb44 3492 defsubr (&Sstring_to_syntax);
8489eb67 3493 defsubr (&Smodify_syntax_entry);
62abe9cb 3494 defsubr (&Sinternal_describe_syntax_value);
8489eb67
RS
3495
3496 defsubr (&Sforward_word);
3497
195d1361
RS
3498 defsubr (&Sskip_chars_forward);
3499 defsubr (&Sskip_chars_backward);
3500 defsubr (&Sskip_syntax_forward);
3501 defsubr (&Sskip_syntax_backward);
3502
b3cfe0c8 3503 defsubr (&Sforward_comment);
8489eb67
RS
3504 defsubr (&Sscan_lists);
3505 defsubr (&Sscan_sexps);
3506 defsubr (&Sbackward_prefix_chars);
3507 defsubr (&Sparse_partial_sexp);
3508}
839966f3
KH
3509
3510/* arch-tag: 3e297b9f-088e-4b64-8f4c-fb0b3443e412
3511 (do not change this comment) */