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