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