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