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