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