(syms_of_ntproc) <w32-get-true-file-attributes>: Doc fix.
[bpt/emacs.git] / src / syntax.c
CommitLineData
8489eb67 1/* GNU Emacs routines to deal with syntax tables; also word and list parsing.
429ab54e 2 Copyright (C) 1985, 1987, 1993, 1994, 1995, 1997, 1998, 1999, 2001,
8cabe764 3 2002, 2003, 2004, 2005, 2006, 2007, 2008
429ab54e 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
1e9dbb5f 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:
8b8bf8e6
SM
2504 if (from == stop)
2505 goto lose;
6a140a74 2506 INC_BOTH (from, from_byte);
8489eb67
RS
2507 /* treat following character as a word constituent */
2508 case Sword:
2509 case Ssymbol:
2510 if (depth || !sexpflag) break;
195d1361 2511 /* This word counts as a sexp; return at end of it. */
8489eb67
RS
2512 while (from < stop)
2513 {
195d1361 2514 UPDATE_SYNTAX_TABLE_FORWARD (from);
7c7ca3d2
RS
2515
2516 /* Some compilers can't handle this inside the switch. */
b7dbcc19 2517 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
bd25db08 2518 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
7c7ca3d2 2519 switch (temp)
8489eb67
RS
2520 {
2521 case Scharquote:
2522 case Sescape:
6a140a74 2523 INC_BOTH (from, from_byte);
8b8bf8e6
SM
2524 if (from == stop)
2525 goto lose;
8489eb67
RS
2526 break;
2527 case Sword:
2528 case Ssymbol:
2529 case Squote:
2530 break;
2531 default:
2532 goto done;
2533 }
6a140a74 2534 INC_BOTH (from, from_byte);
8489eb67
RS
2535 }
2536 goto done;
2537
195d1361 2538 case Scomment_fence:
95ff8dfc
RS
2539 comstyle = ST_COMMENT_STYLE;
2540 /* FALLTHROUGH */
2541 case Scomment:
8489eb67 2542 if (!parse_sexp_ignore_comments) break;
95ff8dfc
RS
2543 UPDATE_SYNTAX_TABLE_FORWARD (from);
2544 found = forw_comment (from, from_byte, stop,
2545 comnested, comstyle, 0,
2546 &out_charpos, &out_bytepos, &dummy);
2547 from = out_charpos, from_byte = out_bytepos;
2548 if (!found)
8489eb67 2549 {
95ff8dfc
RS
2550 if (depth == 0)
2551 goto done;
2552 goto lose;
8489eb67 2553 }
95ff8dfc
RS
2554 INC_BOTH (from, from_byte);
2555 UPDATE_SYNTAX_TABLE_FORWARD (from);
8489eb67
RS
2556 break;
2557
2558 case Smath:
2559 if (!sexpflag)
2560 break;
b7dbcc19 2561 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
6a140a74
RS
2562 {
2563 INC_BOTH (from, from_byte);
2564 }
8489eb67
RS
2565 if (mathexit)
2566 {
2567 mathexit = 0;
2568 goto close1;
2569 }
2570 mathexit = 1;
2571
2572 case Sopen:
2573 if (!++depth) goto done;
2574 break;
2575
2576 case Sclose:
2577 close1:
2578 if (!--depth) goto done;
2579 if (depth < min_depth)
9471945b
KS
2580 xsignal3 (Qscan_error,
2581 build_string ("Containing expression ends prematurely"),
2582 make_number (last_good), make_number (from));
8489eb67
RS
2583 break;
2584
2585 case Sstring:
195d1361 2586 case Sstring_fence:
ef316cf0 2587 temp_pos = dec_bytepos (from_byte);
b7dbcc19 2588 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
8489eb67
RS
2589 while (1)
2590 {
8b8bf8e6
SM
2591 if (from >= stop)
2592 goto lose;
195d1361 2593 UPDATE_SYNTAX_TABLE_FORWARD (from);
b7dbcc19 2594 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
42ebfa31
SM
2595 if (code == Sstring
2596 ? (c == stringterm
2597 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
bd25db08 2598 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
195d1361 2599 break;
7c7ca3d2
RS
2600
2601 /* Some compilers can't handle this inside the switch. */
bd25db08 2602 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
7c7ca3d2 2603 switch (temp)
8489eb67
RS
2604 {
2605 case Scharquote:
2606 case Sescape:
6a140a74 2607 INC_BOTH (from, from_byte);
8489eb67 2608 }
6a140a74 2609 INC_BOTH (from, from_byte);
8489eb67 2610 }
6a140a74 2611 INC_BOTH (from, from_byte);
8489eb67
RS
2612 if (!depth && sexpflag) goto done;
2613 break;
240c43e8
SM
2614 default:
2615 /* Ignore whitespace, punctuation, quote, endcomment. */
2616 break;
8489eb67
RS
2617 }
2618 }
2619
2620 /* Reached end of buffer. Error if within object, return nil if between */
8b8bf8e6
SM
2621 if (depth)
2622 goto lose;
8489eb67
RS
2623
2624 immediate_quit = 0;
2625 return Qnil;
2626
2627 /* End of object reached */
2628 done:
2629 count--;
2630 }
2631
2632 while (count < 0)
2633 {
8489eb67
RS
2634 while (from > stop)
2635 {
6a140a74 2636 DEC_BOTH (from, from_byte);
195d1361 2637 UPDATE_SYNTAX_TABLE_BACKWARD (from);
b7dbcc19 2638 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
bd25db08 2639 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
7bf5e9e4
RS
2640 if (depth == min_depth)
2641 last_good = from;
7fc8191e 2642 comstyle = 0;
95ff8dfc 2643 comnested = SYNTAX_COMMENT_NESTED (c);
7fc8191e
RS
2644 if (code == Sendcomment)
2645 comstyle = SYNTAX_COMMENT_STYLE (c);
8489eb67 2646 if (from > stop && SYNTAX_COMEND_SECOND (c)
1674d9a2 2647 && prev_char_comend_first (from, from_byte)
8489eb67 2648 && parse_sexp_ignore_comments)
e5d4f4dc 2649 {
f902a008 2650 /* We must record the comment style encountered so that
e5d4f4dc 2651 later, we can match only the proper comment begin
f902a008 2652 sequence of the same style. */
6a140a74 2653 DEC_BOTH (from, from_byte);
f902a008
RS
2654 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2655 code = Sendcomment;
b7dbcc19 2656 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
95ff8dfc
RS
2657 comstyle = SYNTAX_COMMENT_STYLE (c1);
2658 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
e5d4f4dc 2659 }
7d0393cf 2660
9828a477 2661 /* Quoting turns anything except a comment-ender
e6365855 2662 into a word character. Note that this cannot be true
f902a008 2663 if we decremented FROM in the if-statement above. */
9828a477 2664 if (code != Sendcomment && char_quoted (from, from_byte))
240c43e8
SM
2665 {
2666 DEC_BOTH (from, from_byte);
2667 code = Sword;
2668 }
9828a477 2669 else if (SYNTAX_PREFIX (c))
8489eb67
RS
2670 continue;
2671
9828a477 2672 switch (SWITCH_ENUM_CAST (code))
8489eb67
RS
2673 {
2674 case Sword:
2675 case Ssymbol:
9828a477
KH
2676 case Sescape:
2677 case Scharquote:
8489eb67 2678 if (depth || !sexpflag) break;
195d1361
RS
2679 /* This word counts as a sexp; count object finished
2680 after passing it. */
8489eb67
RS
2681 while (from > stop)
2682 {
6a140a74 2683 temp_pos = from_byte;
ef316cf0
RS
2684 if (! NILP (current_buffer->enable_multibyte_characters))
2685 DEC_POS (temp_pos);
2686 else
2687 temp_pos--;
6a140a74 2688 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
b7dbcc19 2689 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
bd25db08 2690 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
9828a477
KH
2691 /* Don't allow comment-end to be quoted. */
2692 if (temp_code == Sendcomment)
2693 goto done2;
6a140a74 2694 quoted = char_quoted (from - 1, temp_pos);
8489eb67 2695 if (quoted)
93da5fff 2696 {
6a140a74 2697 DEC_BOTH (from, from_byte);
ef316cf0 2698 temp_pos = dec_bytepos (temp_pos);
6a140a74 2699 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
93da5fff 2700 }
b7dbcc19 2701 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
bd25db08 2702 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
93da5fff
KH
2703 if (! (quoted || temp_code == Sword
2704 || temp_code == Ssymbol
2705 || temp_code == Squote))
8489eb67 2706 goto done2;
6a140a74 2707 DEC_BOTH (from, from_byte);
8489eb67
RS
2708 }
2709 goto done2;
2710
2711 case Smath:
2712 if (!sexpflag)
2713 break;
ef316cf0 2714 temp_pos = dec_bytepos (from_byte);
6a140a74 2715 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
b7dbcc19 2716 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
6a140a74 2717 DEC_BOTH (from, from_byte);
8489eb67
RS
2718 if (mathexit)
2719 {
2720 mathexit = 0;
2721 goto open2;
2722 }
2723 mathexit = 1;
2724
2725 case Sclose:
2726 if (!++depth) goto done2;
2727 break;
2728
2729 case Sopen:
2730 open2:
2731 if (!--depth) goto done2;
2732 if (depth < min_depth)
9471945b
KS
2733 xsignal3 (Qscan_error,
2734 build_string ("Containing expression ends prematurely"),
2735 make_number (last_good), make_number (from));
8489eb67
RS
2736 break;
2737
2738 case Sendcomment:
2739 if (!parse_sexp_ignore_comments)
2740 break;
95ff8dfc 2741 found = back_comment (from, from_byte, stop, comnested, comstyle,
6a140a74 2742 &out_charpos, &out_bytepos);
1aa963c8
SM
2743 /* FIXME: if found == -1, then it really wasn't a comment-end.
2744 For single-char Sendcomment, we can't do much about it apart
2745 from skipping the char.
2746 For 2-char endcomments, we could try again, taking both
2747 chars as separate entities, but it's a lot of trouble
2748 for very little gain, so we don't bother either. -sm */
6a140a74
RS
2749 if (found != -1)
2750 from = out_charpos, from_byte = out_bytepos;
8489eb67
RS
2751 break;
2752
195d1361
RS
2753 case Scomment_fence:
2754 case Sstring_fence:
2755 while (1)
2756 {
8b8bf8e6
SM
2757 if (from == stop)
2758 goto lose;
6b61353c 2759 DEC_BOTH (from, from_byte);
195d1361 2760 UPDATE_SYNTAX_TABLE_BACKWARD (from);
7d0393cf 2761 if (!char_quoted (from, from_byte)
b7dbcc19 2762 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
bd25db08 2763 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
195d1361
RS
2764 break;
2765 }
2766 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2767 break;
7d0393cf 2768
8489eb67 2769 case Sstring:
b7dbcc19 2770 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
8489eb67
RS
2771 while (1)
2772 {
8b8bf8e6
SM
2773 if (from == stop)
2774 goto lose;
6b61353c
KH
2775 DEC_BOTH (from, from_byte);
2776 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2777 if (!char_quoted (from, from_byte)
2778 && (stringterm
2779 == (c = FETCH_CHAR_AS_MULTIBYTE (from_byte)))
42ebfa31 2780 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
8489eb67 2781 break;
8489eb67 2782 }
8489eb67
RS
2783 if (!depth && sexpflag) goto done2;
2784 break;
240c43e8
SM
2785 default:
2786 /* Ignore whitespace, punctuation, quote, endcomment. */
2787 break;
8489eb67
RS
2788 }
2789 }
2790
2791 /* Reached start of buffer. Error if within object, return nil if between */
8b8bf8e6
SM
2792 if (depth)
2793 goto lose;
8489eb67
RS
2794
2795 immediate_quit = 0;
2796 return Qnil;
2797
2798 done2:
2799 count++;
2800 }
2801
2802
2803 immediate_quit = 0;
1e142fb7 2804 XSETFASTINT (val, from);
8489eb67
RS
2805 return val;
2806
2807 lose:
9471945b
KS
2808 xsignal3 (Qscan_error,
2809 build_string ("Unbalanced parentheses"),
2810 make_number (last_good), make_number (from));
8489eb67
RS
2811}
2812
8489eb67 2813DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
fdb82f93
PJ
2814 doc: /* Scan from character number FROM by COUNT lists.
2815Returns the character number of the position thus found.
2816
2817If DEPTH is nonzero, paren depth begins counting from that value,
2818only places where the depth in parentheses becomes zero
2819are candidates for stopping; COUNT such places are counted.
2820Thus, a positive value for DEPTH means go out levels.
2821
2822Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2823
2824If the beginning or end of (the accessible part of) the buffer is reached
2825and the depth is wrong, an error is signaled.
2826If the depth is right but the count is not used up, nil is returned. */)
2827 (from, count, depth)
8489eb67
RS
2828 Lisp_Object from, count, depth;
2829{
b7826503
PJ
2830 CHECK_NUMBER (from);
2831 CHECK_NUMBER (count);
2832 CHECK_NUMBER (depth);
8489eb67
RS
2833
2834 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2835}
2836
2837DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
fdb82f93
PJ
2838 doc: /* Scan from character number FROM by COUNT balanced expressions.
2839If COUNT is negative, scan backwards.
2840Returns the character number of the position thus found.
2841
2842Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2843
2844If the beginning or end of (the accessible part of) the buffer is reached
2845in the middle of a parenthetical grouping, an error is signaled.
2846If the beginning or end is reached between groupings
2847but before count is used up, nil is returned. */)
2848 (from, count)
8489eb67
RS
2849 Lisp_Object from, count;
2850{
b7826503
PJ
2851 CHECK_NUMBER (from);
2852 CHECK_NUMBER (count);
8489eb67
RS
2853
2854 return scan_lists (XINT (from), XINT (count), 0, 1);
2855}
2856
2857DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
fdb82f93
PJ
2858 0, 0, 0,
2859 doc: /* Move point backward over any number of chars with prefix syntax.
2860This includes chars with "quote" or "prefix" syntax (' or p). */)
2861 ()
8489eb67
RS
2862{
2863 int beg = BEGV;
6a140a74
RS
2864 int opoint = PT;
2865 int opoint_byte = PT_BYTE;
6ec8bbd2 2866 int pos = PT;
6a140a74 2867 int pos_byte = PT_BYTE;
93da5fff 2868 int c;
93da5fff 2869
7d0393cf 2870 if (pos <= beg)
195d1361 2871 {
f902a008
RS
2872 SET_PT_BOTH (opoint, opoint_byte);
2873
2874 return Qnil;
195d1361 2875 }
8489eb67 2876
f902a008
RS
2877 SETUP_SYNTAX_TABLE (pos, -1);
2878
6a140a74
RS
2879 DEC_BOTH (pos, pos_byte);
2880
1fd3172d 2881 while (!char_quoted (pos, pos_byte)
195d1361 2882 /* Previous statement updates syntax table. */
b7dbcc19 2883 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
93da5fff
KH
2884 || SYNTAX_PREFIX (c)))
2885 {
1fd3172d
RS
2886 opoint = pos;
2887 opoint_byte = pos_byte;
2888
2889 if (pos + 1 > beg)
2890 DEC_BOTH (pos, pos_byte);
93da5fff 2891 }
8489eb67 2892
6a140a74 2893 SET_PT_BOTH (opoint, opoint_byte);
8489eb67
RS
2894
2895 return Qnil;
2896}
2897\f
6a140a74 2898/* Parse forward from FROM / FROM_BYTE to END,
e5d4f4dc
RS
2899 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2900 and return a description of the state of the parse at END.
c81a3712 2901 If STOPBEFORE is nonzero, stop at the start of an atom.
644ea4df
RS
2902 If COMMENTSTOP is 1, stop at the start of a comment.
2903 If COMMENTSTOP is -1, stop at the start or end of a comment,
2904 after the beginning of a string, or after the end of a string. */
8489eb67 2905
340f92b5 2906static void
6a140a74 2907scan_sexps_forward (stateptr, from, from_byte, end, targetdepth,
c81a3712 2908 stopbefore, oldstate, commentstop)
e5d4f4dc 2909 struct lisp_parse_state *stateptr;
8489eb67 2910 register int from;
38355d47 2911 int from_byte;
8489eb67
RS
2912 int end, targetdepth, stopbefore;
2913 Lisp_Object oldstate;
c81a3712 2914 int commentstop;
8489eb67
RS
2915{
2916 struct lisp_parse_state state;
2917
2918 register enum syntaxcode code;
95ff8dfc
RS
2919 int c1;
2920 int comnested;
8489eb67
RS
2921 struct level { int last, prev; };
2922 struct level levelstart[100];
2923 register struct level *curlevel = levelstart;
2924 struct level *endlevel = levelstart + 100;
8489eb67
RS
2925 register int depth; /* Paren depth of current scanning location.
2926 level - levelstart equals this except
2927 when the depth becomes negative. */
2928 int mindepth; /* Lowest DEPTH value seen. */
2929 int start_quoted = 0; /* Nonzero means starting after a char quote */
2930 Lisp_Object tem;
93da5fff 2931 int prev_from; /* Keep one character before FROM. */
6a140a74 2932 int prev_from_byte;
1fd3172d 2933 int prev_from_syntax;
195d1361
RS
2934 int boundary_stop = commentstop == -1;
2935 int nofence;
95ff8dfc 2936 int found;
60c86a83 2937 EMACS_INT out_bytepos, out_charpos;
7c7ca3d2 2938 int temp;
93da5fff
KH
2939
2940 prev_from = from;
6a140a74
RS
2941 prev_from_byte = from_byte;
2942 if (from != BEGV)
2943 DEC_BOTH (prev_from, prev_from_byte);
93da5fff
KH
2944
2945 /* Use this macro instead of `from++'. */
6a140a74
RS
2946#define INC_FROM \
2947do { prev_from = from; \
2948 prev_from_byte = from_byte; \
327719ee 2949 temp = FETCH_CHAR_AS_MULTIBYTE (prev_from_byte); \
ab229fdd 2950 prev_from_syntax = SYNTAX_WITH_FLAGS (temp); \
ef316cf0 2951 INC_BOTH (from, from_byte); \
3ceb4629
SM
2952 if (from < end) \
2953 UPDATE_SYNTAX_TABLE_FORWARD (from); \
6a140a74 2954 } while (0)
8489eb67
RS
2955
2956 immediate_quit = 1;
2957 QUIT;
2958
265a9e55 2959 if (NILP (oldstate))
8489eb67
RS
2960 {
2961 depth = 0;
2962 state.instring = -1;
2963 state.incomment = 0;
195d1361
RS
2964 state.comstyle = 0; /* comment style a by default. */
2965 state.comstr_start = -1; /* no comment/string seen. */
8489eb67
RS
2966 }
2967 else
2968 {
2969 tem = Fcar (oldstate);
265a9e55 2970 if (!NILP (tem))
8489eb67
RS
2971 depth = XINT (tem);
2972 else
2973 depth = 0;
2974
2975 oldstate = Fcdr (oldstate);
2976 oldstate = Fcdr (oldstate);
2977 oldstate = Fcdr (oldstate);
2978 tem = Fcar (oldstate);
195d1361 2979 /* Check whether we are inside string_fence-style string: */
7d0393cf
JB
2980 state.instring = (!NILP (tem)
2981 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
e76f1c44 2982 : -1);
8489eb67
RS
2983
2984 oldstate = Fcdr (oldstate);
2985 tem = Fcar (oldstate);
e76f1c44
GM
2986 state.incomment = (!NILP (tem)
2987 ? (INTEGERP (tem) ? XINT (tem) : -1)
95ff8dfc 2988 : 0);
8489eb67
RS
2989
2990 oldstate = Fcdr (oldstate);
2991 tem = Fcar (oldstate);
265a9e55 2992 start_quoted = !NILP (tem);
e5d4f4dc 2993
95ff8dfc 2994 /* if the eighth element of the list is nil, we are in comment
195d1361
RS
2995 style a. If it is non-nil, we are in comment style b */
2996 oldstate = Fcdr (oldstate);
e5d4f4dc 2997 oldstate = Fcdr (oldstate);
195d1361 2998 tem = Fcar (oldstate);
7d0393cf 2999 state.comstyle = NILP (tem) ? 0 : (EQ (tem, Qsyntax_table)
e76f1c44 3000 ? ST_COMMENT_STYLE : 1);
195d1361 3001
e5d4f4dc 3002 oldstate = Fcdr (oldstate);
e5d4f4dc 3003 tem = Fcar (oldstate);
195d1361 3004 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
1a102a58
RS
3005 oldstate = Fcdr (oldstate);
3006 tem = Fcar (oldstate);
3007 while (!NILP (tem)) /* >= second enclosing sexps. */
3008 {
3009 /* curlevel++->last ran into compiler bug on Apollo */
3010 curlevel->last = XINT (Fcar (tem));
3011 if (++curlevel == endlevel)
02010917 3012 curlevel--; /* error ("Nesting too deep for parser"); */
1a102a58
RS
3013 curlevel->prev = -1;
3014 curlevel->last = -1;
3015 tem = Fcdr (tem);
3016 }
8489eb67
RS
3017 }
3018 state.quoted = 0;
3019 mindepth = depth;
3020
3021 curlevel->prev = -1;
3022 curlevel->last = -1;
3023
326b283b 3024 SETUP_SYNTAX_TABLE (prev_from, 1);
ab229fdd
AS
3025 temp = FETCH_CHAR (prev_from_byte);
3026 prev_from_syntax = SYNTAX_WITH_FLAGS (temp);
3ceb4629 3027 UPDATE_SYNTAX_TABLE_FORWARD (from);
326b283b 3028
195d1361 3029 /* Enter the loop at a place appropriate for initial state. */
8489eb67 3030
326b283b
RS
3031 if (state.incomment)
3032 goto startincomment;
8489eb67
RS
3033 if (state.instring >= 0)
3034 {
195d1361 3035 nofence = state.instring != ST_STRING_STYLE;
326b283b
RS
3036 if (start_quoted)
3037 goto startquotedinstring;
8489eb67
RS
3038 goto startinstring;
3039 }
326b283b
RS
3040 else if (start_quoted)
3041 goto startquoted;
1fd3172d 3042
8489eb67
RS
3043 while (from < end)
3044 {
93da5fff 3045 INC_FROM;
1fd3172d 3046 code = prev_from_syntax & 0xff;
4c920633 3047
c5d4b96f
SM
3048 if (from < end
3049 && SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax)
3050 && (c1 = FETCH_CHAR (from_byte),
3051 SYNTAX_COMSTART_SECOND (c1)))
3052 /* Duplicate code to avoid a complex if-expression
3053 which causes trouble for the SGI compiler. */
95ff8dfc 3054 {
c5d4b96f
SM
3055 /* Record the comment style we have entered so that only
3056 the comment-end sequence of the same style actually
3057 terminates the comment section. */
3058 state.comstyle = SYNTAX_COMMENT_STYLE (c1);
3059 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
3060 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
3061 state.incomment = comnested ? 1 : -1;
95ff8dfc 3062 state.comstr_start = prev_from;
c5d4b96f
SM
3063 INC_FROM;
3064 code = Scomment;
95ff8dfc 3065 }
4c920633 3066 else if (code == Scomment_fence)
e5d4f4dc
RS
3067 {
3068 /* Record the comment style we have entered so that only
3069 the comment-end sequence of the same style actually
3070 terminates the comment section. */
95ff8dfc
RS
3071 state.comstyle = ST_COMMENT_STYLE;
3072 state.incomment = -1;
195d1361 3073 state.comstr_start = prev_from;
e5d4f4dc 3074 code = Scomment;
e5d4f4dc 3075 }
c5d4b96f
SM
3076 else if (code == Scomment)
3077 {
3078 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax);
3079 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
3080 1 : -1);
3081 state.comstr_start = prev_from;
3082 }
e5d4f4dc 3083
1fd3172d 3084 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
8489eb67 3085 continue;
0220c518 3086 switch (SWITCH_ENUM_CAST (code))
8489eb67
RS
3087 {
3088 case Sescape:
3089 case Scharquote:
3090 if (stopbefore) goto stop; /* this arg means stop at sexp start */
93da5fff 3091 curlevel->last = prev_from;
8489eb67
RS
3092 startquoted:
3093 if (from == end) goto endquoted;
93da5fff 3094 INC_FROM;
8489eb67
RS
3095 goto symstarted;
3096 /* treat following character as a word constituent */
3097 case Sword:
3098 case Ssymbol:
3099 if (stopbefore) goto stop; /* this arg means stop at sexp start */
93da5fff 3100 curlevel->last = prev_from;
8489eb67
RS
3101 symstarted:
3102 while (from < end)
3103 {
7c7ca3d2 3104 /* Some compilers can't handle this inside the switch. */
327719ee 3105 temp = FETCH_CHAR_AS_MULTIBYTE (from_byte);
ab229fdd 3106 temp = SYNTAX (temp);
7c7ca3d2 3107 switch (temp)
8489eb67
RS
3108 {
3109 case Scharquote:
3110 case Sescape:
93da5fff 3111 INC_FROM;
8489eb67
RS
3112 if (from == end) goto endquoted;
3113 break;
3114 case Sword:
3115 case Ssymbol:
3116 case Squote:
3117 break;
3118 default:
3119 goto symdone;
3120 }
93da5fff 3121 INC_FROM;
8489eb67
RS
3122 }
3123 symdone:
3124 curlevel->prev = curlevel->last;
3125 break;
3126
240c43e8 3127 case Scomment_fence: /* Can't happen because it's handled above. */
8489eb67 3128 case Scomment:
195d1361 3129 if (commentstop || boundary_stop) goto done;
d355bd8a
SM
3130 startincomment:
3131 /* The (from == BEGV) test was to enter the loop in the middle so
95ff8dfc 3132 that we find a 2-char comment ender even if we start in the
e6365855 3133 middle of it. We don't want to do that if we're just at the
d355bd8a 3134 beginning of the comment (think of (*) ... (*)). */
95ff8dfc
RS
3135 found = forw_comment (from, from_byte, end,
3136 state.incomment, state.comstyle,
e6365855
SM
3137 (from == BEGV || from < state.comstr_start + 3)
3138 ? 0 : prev_from_syntax,
95ff8dfc
RS
3139 &out_charpos, &out_bytepos, &state.incomment);
3140 from = out_charpos; from_byte = out_bytepos;
3141 /* Beware! prev_from and friends are invalid now.
3142 Luckily, the `done' doesn't use them and the INC_FROM
3143 sets them to a sane value without looking at them. */
3144 if (!found) goto done;
d355bd8a 3145 INC_FROM;
8489eb67 3146 state.incomment = 0;
e5d4f4dc 3147 state.comstyle = 0; /* reset the comment style */
195d1361 3148 if (boundary_stop) goto done;
8489eb67
RS
3149 break;
3150
3151 case Sopen:
3152 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3153 depth++;
3154 /* curlevel++->last ran into compiler bug on Apollo */
93da5fff 3155 curlevel->last = prev_from;
8489eb67 3156 if (++curlevel == endlevel)
02010917 3157 curlevel--; /* error ("Nesting too deep for parser"); */
8489eb67
RS
3158 curlevel->prev = -1;
3159 curlevel->last = -1;
30844415 3160 if (targetdepth == depth) goto done;
8489eb67
RS
3161 break;
3162
3163 case Sclose:
3164 depth--;
3165 if (depth < mindepth)
3166 mindepth = depth;
3167 if (curlevel != levelstart)
3168 curlevel--;
3169 curlevel->prev = curlevel->last;
30844415 3170 if (targetdepth == depth) goto done;
8489eb67
RS
3171 break;
3172
3173 case Sstring:
195d1361
RS
3174 case Sstring_fence:
3175 state.comstr_start = from - 1;
8489eb67 3176 if (stopbefore) goto stop; /* this arg means stop at sexp start */
93da5fff 3177 curlevel->last = prev_from;
7d0393cf 3178 state.instring = (code == Sstring
b7dbcc19 3179 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
195d1361
RS
3180 : ST_STRING_STYLE);
3181 if (boundary_stop) goto done;
8489eb67 3182 startinstring:
195d1361 3183 {
644ea4df 3184 nofence = state.instring != ST_STRING_STYLE;
7d0393cf 3185
644ea4df
RS
3186 while (1)
3187 {
3188 int c;
195d1361 3189
644ea4df 3190 if (from >= end) goto done;
b7dbcc19 3191 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
7c7ca3d2
RS
3192 /* Some compilers can't handle this inside the switch. */
3193 temp = SYNTAX (c);
ccf89641
KH
3194
3195 /* Check TEMP here so that if the char has
3196 a syntax-table property which says it is NOT
3197 a string character, it does not end the string. */
3198 if (nofence && c == state.instring && temp == Sstring)
3199 break;
3200
7c7ca3d2 3201 switch (temp)
644ea4df
RS
3202 {
3203 case Sstring_fence:
3204 if (!nofence) goto string_end;
3205 break;
3206 case Scharquote:
3207 case Sescape:
3208 INC_FROM;
3209 startquotedinstring:
3210 if (from >= end) goto endquoted;
195d1361 3211 }
644ea4df
RS
3212 INC_FROM;
3213 }
195d1361
RS
3214 }
3215 string_end:
8489eb67
RS
3216 state.instring = -1;
3217 curlevel->prev = curlevel->last;
93da5fff 3218 INC_FROM;
195d1361 3219 if (boundary_stop) goto done;
8489eb67
RS
3220 break;
3221
3222 case Smath:
240c43e8
SM
3223 /* FIXME: We should do something with it. */
3224 break;
3225 default:
3226 /* Ignore whitespace, punctuation, quote, endcomment. */
8489eb67
RS
3227 break;
3228 }
3229 }
3230 goto done;
3231
3232 stop: /* Here if stopping before start of sexp. */
93da5fff 3233 from = prev_from; /* We have just fetched the char that starts it; */
8489eb67
RS
3234 goto done; /* but return the position before it. */
3235
3236 endquoted:
3237 state.quoted = 1;
3238 done:
3239 state.depth = depth;
3240 state.mindepth = mindepth;
3241 state.thislevelstart = curlevel->prev;
3242 state.prevlevelstart
3243 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3244 state.location = from;
1a102a58
RS
3245 state.levelstarts = Qnil;
3246 while (--curlevel >= levelstart)
3247 state.levelstarts = Fcons (make_number (curlevel->last),
3248 state.levelstarts);
8489eb67
RS
3249 immediate_quit = 0;
3250
e5d4f4dc 3251 *stateptr = state;
8489eb67
RS
3252}
3253
c81a3712 3254DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
fdb82f93
PJ
3255 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3256Parsing stops at TO or when certain criteria are met;
3257 point is set to where parsing stops.
3258If fifth arg OLDSTATE is omitted or nil,
3259 parsing assumes that FROM is the beginning of a function.
0e6228bc 3260Value is a list of elements describing final state of parsing:
fdb82f93
PJ
3261 0. depth in parens.
3262 1. character address of start of innermost containing list; nil if none.
3263 2. character address of start of last complete sexp terminated.
3264 3. non-nil if inside a string.
3265 (it is the character that will terminate the string,
3266 or t if the string should be terminated by a generic string delimiter.)
7d0393cf 3267 4. nil if outside a comment, t if inside a non-nestable comment,
fdb82f93
PJ
3268 else an integer (the current comment nesting).
3269 5. t if following a quote character.
3270 6. the minimum paren-depth encountered during this scan.
3271 7. t if in a comment of style b; symbol `syntax-table' if the comment
3272 should be terminated by a generic comment delimiter.
3273 8. character address of start of comment or string; nil if not in one.
3274 9. Intermediate data for continuation of parsing (subject to change).
3275If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3276in parentheses becomes equal to TARGETDEPTH.
3277Fourth arg STOPBEFORE non-nil means stop when come to
3278 any character that starts a sexp.
0e6228bc 3279Fifth arg OLDSTATE is a list like what this function returns.
fdb82f93 3280 It is used to initialize the state of the parse. Elements number 1, 2, 6
43726c05 3281 and 8 are ignored.
fdb82f93
PJ
3282Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3283 If it is symbol `syntax-table', stop after the start of a comment or a
3284 string, or after end of a comment or a string. */)
3285 (from, to, targetdepth, stopbefore, oldstate, commentstop)
c81a3712 3286 Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop;
8489eb67
RS
3287{
3288 struct lisp_parse_state state;
3289 int target;
3290
265a9e55 3291 if (!NILP (targetdepth))
8489eb67 3292 {
b7826503 3293 CHECK_NUMBER (targetdepth);
8489eb67
RS
3294 target = XINT (targetdepth);
3295 }
3296 else
3297 target = -100000; /* We won't reach this depth */
3298
3299 validate_region (&from, &to);
6a140a74
RS
3300 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3301 XINT (to),
c81a3712 3302 target, !NILP (stopbefore), oldstate,
7d0393cf 3303 (NILP (commentstop)
195d1361 3304 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
8489eb67
RS
3305
3306 SET_PT (state.location);
7d0393cf 3307
8489eb67
RS
3308 return Fcons (make_number (state.depth),
3309 Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart),
3310 Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart),
7d0393cf
JB
3311 Fcons (state.instring >= 0
3312 ? (state.instring == ST_STRING_STYLE
195d1361 3313 ? Qt : make_number (state.instring)) : Qnil,
95ff8dfc
RS
3314 Fcons (state.incomment < 0 ? Qt :
3315 (state.incomment == 0 ? Qnil :
3316 make_number (state.incomment)),
8489eb67 3317 Fcons (state.quoted ? Qt : Qnil,
bff37d2a 3318 Fcons (make_number (state.mindepth),
7d0393cf 3319 Fcons ((state.comstyle
bff37d2a
RS
3320 ? (state.comstyle == ST_COMMENT_STYLE
3321 ? Qsyntax_table : Qt) :
3322 Qnil),
0beaf54f
DL
3323 Fcons (((state.incomment
3324 || (state.instring >= 0))
bff37d2a
RS
3325 ? make_number (state.comstr_start)
3326 : Qnil),
1a102a58 3327 Fcons (state.levelstarts, Qnil))))))))));
8489eb67
RS
3328}
3329\f
dfcf069d 3330void
8489eb67
RS
3331init_syntax_once ()
3332{
78f9a1f7 3333 register int i, c;
8ea151b2 3334 Lisp_Object temp;
8489eb67 3335
5ebaddf5
RS
3336 /* This has to be done here, before we call Fmake_char_table. */
3337 Qsyntax_table = intern ("syntax-table");
3338 staticpro (&Qsyntax_table);
3339
3340 /* Intern this now in case it isn't already done.
3341 Setting this variable twice is harmless.
3342 But don't staticpro it here--that is done in alloc.c. */
3343 Qchar_table_extra_slots = intern ("char-table-extra-slots");
3344
93da5fff 3345 /* Create objects which can be shared among syntax tables. */
42ebfa31 3346 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
93da5fff
KH
3347 for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++)
3348 XVECTOR (Vsyntax_code_object)->contents[i]
3349 = Fcons (make_number (i), Qnil);
3350
5ebaddf5
RS
3351 /* Now we are ready to set up this property, so we can
3352 create syntax tables. */
3353 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3354
93da5fff 3355 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
8489eb67 3356
5ebaddf5 3357 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
8489eb67 3358
aa7b08b4
RS
3359 /* Control characters should not be whitespace. */
3360 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3361 for (i = 0; i <= ' ' - 1; i++)
3362 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3363 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 0177, temp);
3364
3365 /* Except that a few really are whitespace. */
3366 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3367 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ' ', temp);
3368 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\t', temp);
3369 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\n', temp);
3370 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 015, temp);
3371 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, 014, temp);
3372
93da5fff 3373 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
8489eb67 3374 for (i = 'a'; i <= 'z'; i++)
8ea151b2 3375 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
8489eb67 3376 for (i = 'A'; i <= 'Z'; i++)
8ea151b2 3377 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
8489eb67 3378 for (i = '0'; i <= '9'; i++)
8ea151b2
RS
3379 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3380
3381 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3382 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3383
3384 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3385 Fcons (make_number (Sopen), make_number (')')));
3386 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3387 Fcons (make_number (Sclose), make_number ('(')));
3388 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3389 Fcons (make_number (Sopen), make_number (']')));
3390 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3391 Fcons (make_number (Sclose), make_number ('[')));
3392 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3393 Fcons (make_number (Sopen), make_number ('}')));
3394 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3395 Fcons (make_number (Sclose), make_number ('{')));
3396 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3397 Fcons (make_number ((int) Sstring), Qnil));
3398 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3399 Fcons (make_number ((int) Sescape), Qnil));
3400
93da5fff 3401 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
8489eb67 3402 for (i = 0; i < 10; i++)
78f9a1f7
KH
3403 {
3404 c = "_-+*/&|<>="[i];
3405 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3406 }
8489eb67 3407
93da5fff 3408 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
8489eb67 3409 for (i = 0; i < 12; i++)
78f9a1f7
KH
3410 {
3411 c = ".,;:?!#@~^'`"[i];
3412 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3413 }
bd25db08
KH
3414
3415 /* All multibyte characters have syntax `word' by default. */
3416 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
5c7b02ab 3417 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
8489eb67
RS
3418}
3419
dfcf069d 3420void
8489eb67
RS
3421syms_of_syntax ()
3422{
3423 Qsyntax_table_p = intern ("syntax-table-p");
3424 staticpro (&Qsyntax_table_p);
3425
93da5fff
KH
3426 staticpro (&Vsyntax_code_object);
3427
a2994c46
KS
3428 staticpro (&gl_state.object);
3429 staticpro (&gl_state.global_code);
3430 staticpro (&gl_state.current_syntax_table);
3431 staticpro (&gl_state.old_prop);
3432
3433 /* Defined in regex.c */
3434 staticpro (&re_match_object);
3435
7bf5e9e4
RS
3436 Qscan_error = intern ("scan-error");
3437 staticpro (&Qscan_error);
3438 Fput (Qscan_error, Qerror_conditions,
f3be100f 3439 Fcons (Qscan_error, Fcons (Qerror, Qnil)));
7bf5e9e4
RS
3440 Fput (Qscan_error, Qerror_message,
3441 build_string ("Scan error"));
3442
8489eb67 3443 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
fdb82f93 3444 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
8489eb67 3445
195d1361 3446 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties,
fdb82f93
PJ
3447 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3448Otherwise, that text property is simply ignored.
3449See the info node `(elisp)Syntax Properties' for a description of the
3450`syntax-table' property. */);
195d1361 3451
8489eb67
RS
3452 words_include_escapes = 0;
3453 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
fdb82f93 3454 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
8489eb67 3455
bd25db08 3456 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol,
fdb82f93 3457 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
bd25db08
KH
3458 multibyte_syntax_as_symbol = 0;
3459
f4ed767f
GM
3460 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3461 &open_paren_in_column_0_is_defun_start,
b222e415 3462 doc: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
f4ed767f
GM
3463 open_paren_in_column_0_is_defun_start = 1;
3464
8f924df7
KH
3465
3466 DEFVAR_LISP ("find-word-boundary-function-table",
3467 &Vfind_word_boundary_function_table,
869bb237 3468 doc: /*
8f924df7 3469Char table of functions to search for the word boundary.
869bb237
KH
3470Each function is called with two arguments; POS and LIMIT.
3471POS and LIMIT are character positions in the current buffer.
3472
3473If POS is less than LIMIT, POS is at the first character of a word,
3474and the return value of a function is a position after the last
3475character of that word.
3476
3477If POS is not less than LIMIT, POS is at the last character of a word,
3478and the return value of a function is a position at the first
3479character of that word.
3480
3481In both cases, LIMIT bounds the search. */);
8f924df7 3482 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
869bb237 3483
8489eb67
RS
3484 defsubr (&Ssyntax_table_p);
3485 defsubr (&Ssyntax_table);
3486 defsubr (&Sstandard_syntax_table);
3487 defsubr (&Scopy_syntax_table);
3488 defsubr (&Sset_syntax_table);
3489 defsubr (&Schar_syntax);
beefa22e 3490 defsubr (&Smatching_paren);
c65adb44 3491 defsubr (&Sstring_to_syntax);
8489eb67 3492 defsubr (&Smodify_syntax_entry);
62abe9cb 3493 defsubr (&Sinternal_describe_syntax_value);
8489eb67
RS
3494
3495 defsubr (&Sforward_word);
3496
195d1361
RS
3497 defsubr (&Sskip_chars_forward);
3498 defsubr (&Sskip_chars_backward);
3499 defsubr (&Sskip_syntax_forward);
3500 defsubr (&Sskip_syntax_backward);
3501
b3cfe0c8 3502 defsubr (&Sforward_comment);
8489eb67
RS
3503 defsubr (&Sscan_lists);
3504 defsubr (&Sscan_sexps);
3505 defsubr (&Sbackward_prefix_chars);
3506 defsubr (&Sparse_partial_sexp);
3507}
839966f3
KH
3508
3509/* arch-tag: 3e297b9f-088e-4b64-8f4c-fb0b3443e412
3510 (do not change this comment) */