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