(Fexpand_abbrev): Remove unused variables.
[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;
123 int cnt = 0, doing_extra = 0, invalidate = 1;
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
RS
347{
348 int tem;
349 int shortage;
6a140a74 350 int opoint = PT, opoint_byte = PT_BYTE;
37bef230
RS
351
352 /* Use previous finding, if it's valid and applies to this inquiry. */
353 if (current_buffer == find_start_buffer
354 /* Reuse the defun-start even if POS is a little farther on.
355 POS might be in the next defun, but that's ok.
356 Our value may not be the best possible, but will still be usable. */
357 && pos <= find_start_pos + 1000
358 && pos >= find_start_value
359 && BEGV == find_start_begv
360 && MODIFF == find_start_modiff)
361 return find_start_value;
362
363 /* Back up to start of line. */
6a140a74 364 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
37bef230 365
195d1361
RS
366 /* We optimize syntax-table lookup for rare updates. Thus we accept
367 only those `^\s(' which are good in global _and_ text-property
368 syntax-tables. */
369 gl_state.current_syntax_table = current_buffer->syntax_table;
370 gl_state.use_global = 0;
6a140a74 371 while (PT > BEGV)
37bef230
RS
372 {
373 /* Open-paren at start of line means we found our defun-start. */
6a140a74 374 if (SYNTAX (FETCH_CHAR (PT_BYTE)) == Sopen)
195d1361 375 {
6a140a74
RS
376 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
377 if (SYNTAX (FETCH_CHAR (PT_BYTE)) == Sopen)
195d1361
RS
378 break;
379 /* Now fallback to the default value. */
380 gl_state.current_syntax_table = current_buffer->syntax_table;
381 gl_state.use_global = 0;
382 }
37bef230 383 /* Move to beg of previous line. */
6a140a74 384 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
37bef230
RS
385 }
386
387 /* Record what we found, for the next try. */
6a140a74
RS
388 find_start_value = PT;
389 find_start_value_byte = PT_BYTE;
37bef230
RS
390 find_start_buffer = current_buffer;
391 find_start_modiff = MODIFF;
392 find_start_begv = BEGV;
393 find_start_pos = pos;
394
6a140a74
RS
395 TEMP_SET_PT_BOTH (opoint, opoint_byte);
396
37bef230
RS
397 return find_start_value;
398}
399\f
f902a008
RS
400/* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
401
402static int
403prev_char_comend_first (pos, pos_byte)
404 int pos, pos_byte;
405{
406 int c, val;
407
408 DEC_BOTH (pos, pos_byte);
409 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
410 c = FETCH_CHAR (pos_byte);
411 val = SYNTAX_COMEND_FIRST (c);
412 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
413 return val;
414}
415
416/* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
417
418static int
419prev_char_comstart_first (pos, pos_byte)
420 int pos, pos_byte;
421{
422 int c, val;
423
424 DEC_BOTH (pos, pos_byte);
425 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
426 c = FETCH_CHAR (pos_byte);
427 val = SYNTAX_COMSTART_FIRST (c);
428 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
429 return val;
430}
431
6a140a74
RS
432/* Checks whether charpos FROM is at the end of a comment.
433 FROM_BYTE is the bytepos corresponding to FROM.
434 Do not move back before STOP.
435
436 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
437 return -1 otherwise.
438
439 If successful, store the charpos of the comment's beginning
440 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
527a32d9
KH
441
442 Global syntax data remains valid for backward search starting at
443 the returned value (or at FROM, if the search was not successful). */
195d1361
RS
444
445static int
95ff8dfc 446back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_ptr)
6a140a74 447 int from, from_byte, stop;
95ff8dfc 448 int comnested, comstyle;
6a140a74 449 int *charpos_ptr, *bytepos_ptr;
195d1361
RS
450{
451 /* Look back, counting the parity of string-quotes,
452 and recording the comment-starters seen.
453 When we reach a safe place, assume that's not in a string;
454 then step the main scan to the earliest comment-starter seen
455 an even number of string quotes away from the safe place.
456
457 OFROM[I] is position of the earliest comment-starter seen
458 which is I+2X quotes from the comment-end.
459 PARITY is current parity of quotes from the comment end. */
460 int parity = 0;
461 int my_stringend = 0;
462 int string_lossage = 0;
463 int comment_end = from;
6a140a74 464 int comment_end_byte = from_byte;
195d1361 465 int comstart_pos = 0;
6a140a74 466 int comstart_byte;
eb35b628
RS
467 /* Value that PARITY had, when we reached the position
468 in COMSTART_POS. */
469 int comstart_parity = 0;
195d1361 470 int scanstart = from - 1;
eb35b628
RS
471 /* Place where the containing defun starts,
472 or 0 if we didn't come across it yet. */
473 int defun_start = 0;
474 int defun_start_byte = 0;
195d1361 475 register enum syntaxcode code;
95ff8dfc 476 int nesting = 1; /* current comment nesting */
ea315ed6 477 int c;
195d1361
RS
478
479 /* At beginning of range to scan, we're outside of strings;
480 that determines quote parity to the comment-end. */
481 while (from != stop)
482 {
f902a008 483 int temp_byte, prev_comend_second;
6a140a74 484
195d1361 485 /* Move back and examine a character. */
6a140a74 486 DEC_BOTH (from, from_byte);
195d1361
RS
487 UPDATE_SYNTAX_TABLE_BACKWARD (from);
488
6a140a74 489 c = FETCH_CHAR (from_byte);
195d1361
RS
490 code = SYNTAX (c);
491
527a32d9 492 /* If this char is the second of a 2-char comment end sequence,
195d1361
RS
493 back up and give the pair the appropriate syntax. */
494 if (from > stop && SYNTAX_COMEND_SECOND (c)
f902a008 495 && prev_char_comend_first (from, from_byte))
195d1361
RS
496 {
497 code = Sendcomment;
6a140a74 498 DEC_BOTH (from, from_byte);
195d1361 499 UPDATE_SYNTAX_TABLE_BACKWARD (from);
6a140a74 500 c = FETCH_CHAR (from_byte);
195d1361
RS
501 }
502
503 /* If this char starts a 2-char comment start sequence,
504 treat it like a 1-char comment starter. */
f902a008
RS
505 if (from < scanstart && SYNTAX_COMSTART_FIRST (c))
506 {
507 temp_byte = inc_bytepos (from_byte);
508 UPDATE_SYNTAX_TABLE_FORWARD (from + 1);
509 if (SYNTAX_COMSTART_SECOND (FETCH_CHAR (temp_byte))
510 && comstyle == SYNTAX_COMMENT_STYLE (FETCH_CHAR (temp_byte)))
511 code = Scomment;
512 UPDATE_SYNTAX_TABLE_BACKWARD (from);
513 }
195d1361 514
9828a477
KH
515 /* Ignore escaped characters, except comment-enders. */
516 if (code != Sendcomment && char_quoted (from, from_byte))
195d1361
RS
517 continue;
518
519 /* Track parity of quotes. */
520 if (code == Sstring)
521 {
522 parity ^= 1;
523 if (my_stringend == 0)
524 my_stringend = c;
525 /* If we have two kinds of string delimiters.
526 There's no way to grok this scanning backwards. */
527 else if (my_stringend != c)
528 string_lossage = 1;
529 }
530
531 if (code == Sstring_fence || code == Scomment_fence)
532 {
533 parity ^= 1;
534 if (my_stringend == 0)
6a140a74
RS
535 my_stringend
536 = code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE;
195d1361
RS
537 /* If we have two kinds of string delimiters.
538 There's no way to grok this scanning backwards. */
539 else if (my_stringend != (code == Sstring_fence
540 ? ST_STRING_STYLE : ST_COMMENT_STYLE))
541 string_lossage = 1;
542 }
543
195d1361 544 if (code == Scomment)
95ff8dfc
RS
545 /* FIXME: we should also check that the comstyle is correct
546 if the Scomment is a single-char. */
195d1361 547 {
95ff8dfc
RS
548 if (comnested && --nesting <= 0 && parity == 0 && !string_lossage)
549 /* nested comments have to be balanced, so we don't need to
550 keep looking for earlier ones. We use here the same (slightly
551 incorrect) reasoning as below: since it is followed by uniform
552 paired string quotes, this comment-start has to be outside of
553 strings, else the comment-end itself would be inside a string. */
554 goto done;
555
556 /* Record comment-starters according to that
557 quote-parity to the comment-end. */
195d1361
RS
558 comstart_parity = parity;
559 comstart_pos = from;
6a140a74 560 comstart_byte = from_byte;
195d1361
RS
561 }
562
563 /* If we find another earlier comment-ender,
564 any comment-starts earlier than that don't count
565 (because they go with the earlier comment-ender). */
566 if (code == Sendcomment
6a140a74 567 && SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte)) == comstyle)
95ff8dfc
RS
568 if (comnested)
569 nesting++;
570 else
571 break;
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;
e5d4f4dc 958 char desc, match, 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{
1307 register unsigned char *p, *pend;
1308 register unsigned int c;
7e68b0ea 1309 register int ch;
195d1361 1310 unsigned char fastmap[0400];
7e68b0ea
RS
1311 /* If SYNTAXP is 0, STRING may contain multi-byte form of characters
1312 of which codes don't fit in FASTMAP. In that case, we set the
1313 first byte of multibyte form (i.e. base leading-code) in FASTMAP
1314 and set the actual ranges of characters in CHAR_RANGES. In the
1315 form "X-Y" of STRING, both X and Y must belong to the same
1316 character set because a range striding across character sets is
1317 meaningless. */
5df4982e 1318 int *char_ranges;
7e68b0ea 1319 int n_char_ranges = 0;
195d1361 1320 int negate = 0;
4101e6fe 1321 register int i, i_byte;
7e68b0ea 1322 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
1674d9a2
RS
1323 int string_multibyte;
1324 int size_byte;
195d1361
RS
1325
1326 CHECK_STRING (string, 0);
5df4982e 1327 char_ranges = (int *) alloca (XSTRING (string)->size * (sizeof (int)) * 2);
1674d9a2
RS
1328 string_multibyte = STRING_MULTIBYTE (string);
1329 size_byte = STRING_BYTES (XSTRING (string));
195d1361
RS
1330
1331 if (NILP (lim))
1332 XSETINT (lim, forwardp ? ZV : BEGV);
1333 else
6a140a74 1334 CHECK_NUMBER_COERCE_MARKER (lim, 0);
195d1361
RS
1335
1336 /* In any case, don't allow scan outside bounds of buffer. */
195d1361
RS
1337 if (XINT (lim) > ZV)
1338 XSETFASTINT (lim, ZV);
1339 if (XINT (lim) < BEGV)
1340 XSETFASTINT (lim, BEGV);
1341
195d1361
RS
1342 bzero (fastmap, sizeof fastmap);
1343
4101e6fe
RS
1344 i = 0, i_byte = 0;
1345
13090112 1346 if (i_byte < size_byte
4101e6fe 1347 && XSTRING (string)->data[0] == '^')
195d1361 1348 {
4101e6fe 1349 negate = 1; i++, i_byte++;
195d1361
RS
1350 }
1351
1352 /* Find the characters specified and set their elements of fastmap.
1353 If syntaxp, each character counts as itself.
1354 Otherwise, handle backslashes and ranges specially. */
1355
13090112 1356 while (i_byte < size_byte)
195d1361 1357 {
4101e6fe
RS
1358 int c_leading_code;
1359
1360 if (string_multibyte)
7e68b0ea 1361 {
4101e6fe
RS
1362 c_leading_code = XSTRING (string)->data[i_byte];
1363 FETCH_STRING_CHAR_ADVANCE (c, string, i, i_byte);
7e68b0ea
RS
1364 }
1365 else
13090112 1366 c = c_leading_code = XSTRING (string)->data[i_byte++];
4101e6fe
RS
1367
1368 /* Convert multibyteness between what the string has
1369 and what the buffer has. */
1370 if (multibyte)
b29ff194 1371 c = unibyte_char_to_multibyte (c);
4101e6fe
RS
1372 else
1373 c &= 0377;
1374
195d1361 1375 if (syntaxp)
4101e6fe 1376 fastmap[syntax_spec_code[c & 0377]] = 1;
195d1361
RS
1377 else
1378 {
1379 if (c == '\\')
1380 {
13090112 1381 if (i_byte == size_byte)
4101e6fe
RS
1382 break;
1383
1384 if (string_multibyte)
74cf0beb 1385 {
13090112 1386 c_leading_code = XSTRING (string)->data[i_byte];
74cf0beb
RS
1387 FETCH_STRING_CHAR_ADVANCE (c, string, i, i_byte);
1388 }
4101e6fe 1389 else
13090112 1390 c = c_leading_code = XSTRING (string)->data[i_byte++];
195d1361 1391 }
13090112 1392 if (i_byte < size_byte
06274af5 1393 && XSTRING (string)->data[i_byte] == '-')
195d1361 1394 {
06274af5 1395 unsigned int c2, c2_leading_code;
4101e6fe
RS
1396
1397 /* Skip over the dash. */
1398 i++, i_byte++;
1399
13090112 1400 if (i_byte == size_byte)
4101e6fe
RS
1401 break;
1402
1403 /* Get the end of the range. */
1404 if (string_multibyte)
06274af5
KH
1405 {
1406 c2_leading_code = XSTRING (string)->data[i_byte];
1407 FETCH_STRING_CHAR_ADVANCE (c2, string, i, i_byte);
1408 }
4101e6fe 1409 else
13090112 1410 c2 = XSTRING (string)->data[i_byte++];
7e68b0ea 1411
4101e6fe 1412 if (SINGLE_BYTE_CHAR_P (c))
06274af5
KH
1413 {
1414 if (! SINGLE_BYTE_CHAR_P (c2))
1415 error ("Invalid charcter range: %s",
1416 XSTRING (string)->data);
1417 while (c <= c2)
1418 {
1419 fastmap[c] = 1;
1420 c++;
1421 }
1422 }
7e68b0ea 1423 else
195d1361 1424 {
06274af5
KH
1425 if (c_leading_code != c2_leading_code)
1426 error ("Invalid charcter range: %s",
1427 XSTRING (string)->data);
4101e6fe
RS
1428 fastmap[c_leading_code] = 1;
1429 if (c <= c2)
1430 {
1431 char_ranges[n_char_ranges++] = c;
1432 char_ranges[n_char_ranges++] = c2;
1433 }
195d1361 1434 }
195d1361
RS
1435 }
1436 else
7e68b0ea 1437 {
4101e6fe
RS
1438 fastmap[c_leading_code] = 1;
1439 if (!SINGLE_BYTE_CHAR_P (c))
7e68b0ea 1440 {
4101e6fe
RS
1441 char_ranges[n_char_ranges++] = c;
1442 char_ranges[n_char_ranges++] = c;
7e68b0ea
RS
1443 }
1444 }
195d1361
RS
1445 }
1446 }
1447
7e68b0ea
RS
1448 /* If ^ was the first character, complement the fastmap. In
1449 addition, as all multibyte characters have possibility of
1450 matching, set all entries for base leading codes, which is
1451 harmless even if SYNTAXP is 1. */
195d1361
RS
1452
1453 if (negate)
1454 for (i = 0; i < sizeof fastmap; i++)
7e68b0ea
RS
1455 {
1456 if (!multibyte || !BASE_LEADING_CODE_P (i))
1457 fastmap[i] ^= 1;
1458 else
1459 fastmap[i] = 1;
1460 }
195d1361
RS
1461
1462 {
1463 int start_point = PT;
1464 int pos = PT;
6a140a74 1465 int pos_byte = PT_BYTE;
195d1361
RS
1466
1467 immediate_quit = 1;
1468 if (syntaxp)
1469 {
1470 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
1471 if (forwardp)
1472 {
7e68b0ea 1473 if (multibyte)
195d1361 1474 {
ee0cdb48
KH
1475 if (pos < XINT (lim))
1476 while (fastmap[(int) SYNTAX (FETCH_CHAR (pos_byte))])
1477 {
1478 /* Since we already checked for multibyteness,
1479 avoid using INC_BOTH which checks again. */
1480 INC_POS (pos_byte);
1481 pos++;
1482 if (pos >= XINT (lim))
1483 break;
1484 UPDATE_SYNTAX_TABLE_FORWARD (pos);
1485 }
7e68b0ea
RS
1486 }
1487 else
1488 {
1489 while (pos < XINT (lim)
1490 && fastmap[(int) SYNTAX (FETCH_BYTE (pos))])
1491 {
1492 pos++;
1493 UPDATE_SYNTAX_TABLE_FORWARD (pos);
1494 }
195d1361
RS
1495 }
1496 }
1497 else
1498 {
7e68b0ea 1499 if (multibyte)
195d1361 1500 {
7e68b0ea
RS
1501 while (pos > XINT (lim))
1502 {
6a140a74 1503 int savepos = pos_byte;
ef316cf0
RS
1504 /* Since we already checked for multibyteness,
1505 avoid using DEC_BOTH which checks again. */
1506 pos--;
1507 DEC_POS (pos_byte);
4c8535eb 1508 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
6a140a74 1509 if (!fastmap[(int) SYNTAX (FETCH_CHAR (pos_byte))])
7e68b0ea 1510 {
6a140a74
RS
1511 pos++;
1512 pos_byte = savepos;
7e68b0ea
RS
1513 break;
1514 }
7e68b0ea
RS
1515 }
1516 }
1517 else
1518 {
ee0cdb48
KH
1519 if (pos > XINT (lim))
1520 while (fastmap[(int) SYNTAX (FETCH_BYTE (pos - 1))])
1521 {
1522 pos--;
1523 if (pos <= XINT (lim))
4c8535eb 1524 break;
ee0cdb48
KH
1525 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
1526 }
195d1361
RS
1527 }
1528 }
1529 }
1530 else
1531 {
1532 if (forwardp)
1533 {
7e68b0ea 1534 if (multibyte)
6a140a74 1535 while (pos < XINT (lim) && fastmap[(c = FETCH_BYTE (pos_byte))])
7e68b0ea
RS
1536 {
1537 if (!BASE_LEADING_CODE_P (c))
6a140a74 1538 INC_BOTH (pos, pos_byte);
7e68b0ea
RS
1539 else if (n_char_ranges)
1540 {
1541 /* We much check CHAR_RANGES for a multibyte
1542 character. */
6a140a74 1543 ch = FETCH_MULTIBYTE_CHAR (pos_byte);
7e68b0ea
RS
1544 for (i = 0; i < n_char_ranges; i += 2)
1545 if ((ch >= char_ranges[i] && ch <= char_ranges[i + 1]))
1546 break;
1547 if (!(negate ^ (i < n_char_ranges)))
1548 break;
1549
6a140a74 1550 INC_BOTH (pos, pos_byte);
7e68b0ea
RS
1551 }
1552 else
1553 {
1554 if (!negate) break;
6a140a74 1555 INC_BOTH (pos, pos_byte);
7e68b0ea
RS
1556 }
1557 }
1558 else
1559 while (pos < XINT (lim) && fastmap[FETCH_BYTE (pos)])
1560 pos++;
195d1361
RS
1561 }
1562 else
1563 {
7e68b0ea
RS
1564 if (multibyte)
1565 while (pos > XINT (lim))
1566 {
6a140a74
RS
1567 int savepos = pos_byte;
1568 DEC_BOTH (pos, pos_byte);
1569 if (fastmap[(c = FETCH_BYTE (pos_byte))])
7e68b0ea
RS
1570 {
1571 if (!BASE_LEADING_CODE_P (c))
1572 ;
1573 else if (n_char_ranges)
1574 {
1575 /* We much check CHAR_RANGES for a multibyte
1576 character. */
6a140a74 1577 ch = FETCH_MULTIBYTE_CHAR (pos_byte);
7e68b0ea
RS
1578 for (i = 0; i < n_char_ranges; i += 2)
1579 if (ch >= char_ranges[i] && ch <= char_ranges[i + 1])
1580 break;
1581 if (!(negate ^ (i < n_char_ranges)))
1582 {
6a140a74
RS
1583 pos++;
1584 pos_byte = savepos;
7e68b0ea
RS
1585 break;
1586 }
1587 }
1588 else
1589 if (!negate)
1590 {
6a140a74
RS
1591 pos++;
1592 pos_byte = savepos;
7e68b0ea
RS
1593 break;
1594 }
1595 }
1596 else
1597 {
6a140a74
RS
1598 pos++;
1599 pos_byte = savepos;
7e68b0ea
RS
1600 break;
1601 }
1602 }
1603 else
1604 while (pos > XINT (lim) && fastmap[FETCH_BYTE (pos - 1)])
1605 pos--;
195d1361
RS
1606 }
1607 }
7e68b0ea 1608
6a140a74
RS
1609#if 0 /* Not needed now that a position in mid-character
1610 cannot be specified in Lisp. */
7e68b0ea
RS
1611 if (multibyte
1612 /* INC_POS or DEC_POS might have moved POS over LIM. */
1613 && (forwardp ? (pos > XINT (lim)) : (pos < XINT (lim))))
1614 pos = XINT (lim);
6a140a74 1615#endif
7e68b0ea 1616
6a140a74
RS
1617 if (! multibyte)
1618 pos_byte = pos;
1619
1620 SET_PT_BOTH (pos, pos_byte);
195d1361
RS
1621 immediate_quit = 0;
1622
1623 return make_number (PT - start_point);
1624 }
1625}
1626\f
95ff8dfc
RS
1627/* Jump over a comment, assuming we are at the beginning of one.
1628 FROM is the current position.
1629 FROM_BYTE is the bytepos corresponding to FROM.
1630 Do not move past STOP (a charpos).
1631 The comment over which we have to jump is of style STYLE
1632 (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
1633 NESTING should be positive to indicate the nesting at the beginning
1634 for nested comments and should be zero or negative else.
1635 ST_COMMENT_STYLE cannot be nested.
1636 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
1637 (or 0 If the search cannot start in the middle of a two-character).
1638
1639 If successful, return 1 and store the charpos of the comment's end
1640 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
1641 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
1642 corresponding bytepos into *BYTEPOS_PTR and the current nesting
1643 (as defined for state.incomment) in *INCOMMENT_PTR.
1644
1645 The comment end is the last character of the comment rather than the
1646 character just after the comment.
1647
1648 Global syntax data is assumed to initially be valid for FROM and
1649 remains valid for forward search starting at the returned position. */
1650
1651static int
1652forw_comment (from, from_byte, stop, nesting, style, prev_syntax,
1653 charpos_ptr, bytepos_ptr, incomment_ptr)
1654 int from, from_byte, stop;
1655 int nesting, style, prev_syntax;
1656 int *charpos_ptr, *bytepos_ptr, *incomment_ptr;
1657{
1658 register int c, c1;
1659 register enum syntaxcode code;
1660 register int syntax;
1661
1662 if (nesting <= 0) nesting = -1;
1663
1664 /* Enter the loop in the middle so that we find
1665 a 2-char comment ender if we start in the middle of it. */
1666 syntax = prev_syntax;
1667 if (syntax != 0) goto forw_incomment;
1668
1669 while (1)
1670 {
1671 if (from == stop)
1672 {
1673 *incomment_ptr = nesting;
1674 *charpos_ptr = from;
1675 *bytepos_ptr = from_byte;
1676 return 0;
1677 }
1678 c = FETCH_CHAR (from_byte);
1679 syntax = SYNTAX_WITH_FLAGS (c);
1680 code = syntax & 0xff;
1681 if (code == Sendcomment
1682 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
1683 && --nesting <= 0)
1684 /* we have encountered a comment end of the same style
1685 as the comment sequence which began this comment
1686 section */
1687 break;
1688 if (code == Scomment_fence
1689 && style == ST_COMMENT_STYLE)
1690 /* we have encountered a comment end of the same style
1691 as the comment sequence which began this comment
1692 section. */
1693 break;
1694 if (nesting > 0
1695 && code == Scomment
1696 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style)
1697 /* we have encountered a nested comment of the same style
1698 as the comment sequence which began this comment section */
1699 nesting++;
1700 INC_BOTH (from, from_byte);
1701 UPDATE_SYNTAX_TABLE_FORWARD (from);
1702
1703 forw_incomment:
1704 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
1705 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
1706 && (c1 = FETCH_CHAR (from_byte),
1707 SYNTAX_COMEND_SECOND (c1)))
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 if (nesting > 0
1719 && from < stop
1720 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
1721 && (c1 = FETCH_CHAR (from_byte),
1722 SYNTAX_COMMENT_STYLE (c1) == style
1723 && SYNTAX_COMSTART_SECOND (c1)))
1724 /* we have encountered a nested comment of the same style
1725 as the comment sequence which began this comment
1726 section */
1727 {
1728 INC_BOTH (from, from_byte);
1729 UPDATE_SYNTAX_TABLE_FORWARD (from);
1730 nesting++;
1731 }
1732 }
1733 *charpos_ptr = from;
1734 *bytepos_ptr = from_byte;
1735 return 1;
1736}
1737
b3cfe0c8
RS
1738DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
1739 "Move forward across up to N comments. If N is negative, move backward.\n\
b3cfe0c8 1740Stop scanning if we find something other than a comment or whitespace.\n\
c81a3712 1741Set point to where scanning stops.\n\
b3cfe0c8
RS
1742If N comments are found as expected, with nothing except whitespace\n\
1743between them, return t; otherwise return nil.")
1744 (count)
840f481c 1745 Lisp_Object count;
b3cfe0c8
RS
1746{
1747 register int from;
6a140a74 1748 int from_byte;
b3cfe0c8 1749 register int stop;
8ea151b2 1750 register int c, c1;
b3cfe0c8
RS
1751 register enum syntaxcode code;
1752 int comstyle = 0; /* style of comment encountered */
95ff8dfc 1753 int comnested = 0; /* whether the comment is nestable or not */
be720845 1754 int found;
840f481c 1755 int count1;
6a140a74 1756 int out_charpos, out_bytepos;
95ff8dfc 1757 int dummy;
840f481c
RS
1758
1759 CHECK_NUMBER (count, 0);
1760 count1 = XINT (count);
195d1361 1761 stop = count1 > 0 ? ZV : BEGV;
b3cfe0c8
RS
1762
1763 immediate_quit = 1;
1764 QUIT;
1765
1766 from = PT;
6a140a74 1767 from_byte = PT_BYTE;
b3cfe0c8 1768
195d1361 1769 SETUP_SYNTAX_TABLE (from, count1);
840f481c 1770 while (count1 > 0)
b3cfe0c8 1771 {
04882296 1772 do
b3cfe0c8 1773 {
f902a008
RS
1774 int comstart_first;
1775
04882296
KH
1776 if (from == stop)
1777 {
ef316cf0 1778 SET_PT_BOTH (from, from_byte);
b7e6e612 1779 immediate_quit = 0;
04882296
KH
1780 return Qnil;
1781 }
6a140a74 1782 c = FETCH_CHAR (from_byte);
b3cfe0c8 1783 code = SYNTAX (c);
f902a008 1784 comstart_first = SYNTAX_COMSTART_FIRST (c);
95ff8dfc 1785 comnested = SYNTAX_COMMENT_NESTED (c);
6a140a74 1786 INC_BOTH (from, from_byte);
f902a008 1787 UPDATE_SYNTAX_TABLE_FORWARD (from);
b3cfe0c8 1788 comstyle = 0;
f902a008 1789 if (from < stop && comstart_first
6a140a74 1790 && (c1 = FETCH_CHAR (from_byte),
8ea151b2 1791 SYNTAX_COMSTART_SECOND (c1)))
b3cfe0c8 1792 {
7fc8191e
RS
1793 /* We have encountered a comment start sequence and we
1794 are ignoring all text inside comments. We must record
b3cfe0c8
RS
1795 the comment style this sequence begins so that later,
1796 only a comment end of the same style actually ends
7fc8191e 1797 the comment section. */
b3cfe0c8 1798 code = Scomment;
8ea151b2 1799 comstyle = SYNTAX_COMMENT_STYLE (c1);
95ff8dfc 1800 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
6a140a74 1801 INC_BOTH (from, from_byte);
95ff8dfc 1802 UPDATE_SYNTAX_TABLE_FORWARD (from);
b3cfe0c8 1803 }
04882296
KH
1804 }
1805 while (code == Swhitespace || code == Sendcomment);
6a140a74 1806
da71ab91
KH
1807 if (code == Scomment_fence)
1808 comstyle = ST_COMMENT_STYLE;
1809 else if (code != Scomment)
04882296
KH
1810 {
1811 immediate_quit = 0;
6a140a74 1812 DEC_BOTH (from, from_byte);
ef316cf0 1813 SET_PT_BOTH (from, from_byte);
04882296
KH
1814 return Qnil;
1815 }
1816 /* We're at the start of a comment. */
95ff8dfc
RS
1817 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
1818 &out_charpos, &out_bytepos, &dummy);
1819 from = out_charpos; from_byte = out_bytepos;
1820 if (!found)
04882296 1821 {
95ff8dfc
RS
1822 immediate_quit = 0;
1823 SET_PT_BOTH (from, from_byte);
1824 return Qnil;
b3cfe0c8 1825 }
95ff8dfc
RS
1826 INC_BOTH (from, from_byte);
1827 UPDATE_SYNTAX_TABLE_FORWARD (from);
04882296 1828 /* We have skipped one comment. */
840f481c 1829 count1--;
b3cfe0c8
RS
1830 }
1831
840f481c 1832 while (count1 < 0)
b3cfe0c8 1833 {
b9145dbb 1834 while (1)
b3cfe0c8 1835 {
f902a008
RS
1836 int quoted, comstart_second;
1837
b9145dbb
RS
1838 if (from <= stop)
1839 {
6a140a74 1840 SET_PT_BOTH (BEGV, BEGV_BYTE);
b9145dbb
RS
1841 immediate_quit = 0;
1842 return Qnil;
1843 }
b3cfe0c8 1844
6a140a74 1845 DEC_BOTH (from, from_byte);
f902a008 1846 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
6a140a74 1847 quoted = char_quoted (from, from_byte);
b3cfe0c8 1848 if (quoted)
195d1361 1849 {
6a140a74
RS
1850 DEC_BOTH (from, from_byte);
1851 goto leave;
195d1361 1852 }
6a140a74 1853 c = FETCH_CHAR (from_byte);
b3cfe0c8
RS
1854 code = SYNTAX (c);
1855 comstyle = 0;
95ff8dfc 1856 comnested = SYNTAX_COMMENT_NESTED (c);
7fc8191e
RS
1857 if (code == Sendcomment)
1858 comstyle = SYNTAX_COMMENT_STYLE (c);
f902a008 1859 comstart_second = SYNTAX_COMSTART_SECOND (c);
b3cfe0c8 1860 if (from > stop && SYNTAX_COMEND_SECOND (c)
f902a008 1861 && prev_char_comend_first (from, from_byte)
89c6809a 1862 && !char_quoted (from - 1, dec_bytepos (from_byte)))
b3cfe0c8 1863 {
7fc8191e 1864 /* We must record the comment style encountered so that
b3cfe0c8 1865 later, we can match only the proper comment begin
7fc8191e 1866 sequence of the same style. */
6a140a74 1867 DEC_BOTH (from, from_byte);
f902a008
RS
1868 code = Sendcomment;
1869 /* Calling char_quoted, above, set up global syntax position
1870 at the new value of FROM. */
95ff8dfc
RS
1871 c1 = FETCH_CHAR (from_byte);
1872 comstyle = SYNTAX_COMMENT_STYLE (c1);
1873 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
b3cfe0c8 1874 }
f902a008
RS
1875 if (from > stop && comstart_second
1876 && prev_char_comstart_first (from, from_byte)
89c6809a 1877 && !char_quoted (from - 1, dec_bytepos (from_byte)))
ae03af34 1878 {
ae03af34 1879 code = Scomment;
6a140a74 1880 DEC_BOTH (from, from_byte);
ae03af34 1881 }
b3cfe0c8 1882
195d1361
RS
1883 if (code == Scomment_fence)
1884 {
1885 /* Skip until first preceding unquoted comment_fence. */
6a140a74 1886 int found = 0, ini = from, ini_byte = from_byte;
195d1361 1887
6a140a74 1888 while (1)
195d1361 1889 {
6a140a74
RS
1890 DEC_BOTH (from, from_byte);
1891 if (from == stop)
1892 break;
195d1361 1893 UPDATE_SYNTAX_TABLE_BACKWARD (from);
6a140a74
RS
1894 c = FETCH_CHAR (from_byte);
1895 if (SYNTAX (c) == Scomment_fence
1896 && !char_quoted (from, from_byte))
195d1361
RS
1897 {
1898 found = 1;
1899 break;
1900 }
1901 }
1902 if (found == 0)
1903 {
1904 from = ini; /* Set point to ini + 1. */
6a140a74 1905 from_byte = ini_byte;
195d1361
RS
1906 goto leave;
1907 }
1908 }
1909 else if (code == Sendcomment)
b3cfe0c8 1910 {
95ff8dfc 1911 found = back_comment (from, from_byte, stop, comnested, comstyle,
6a140a74
RS
1912 &out_charpos, &out_bytepos);
1913 if (found != -1)
1914 from = out_charpos, from_byte = out_bytepos;
7fc8191e
RS
1915 /* We have skipped one comment. */
1916 break;
b3cfe0c8 1917 }
195d1361 1918 else if (code != Swhitespace && code != Scomment)
b3cfe0c8 1919 {
195d1361 1920 leave:
b3cfe0c8 1921 immediate_quit = 0;
6a140a74 1922 INC_BOTH (from, from_byte);
ef316cf0 1923 SET_PT_BOTH (from, from_byte);
b3cfe0c8
RS
1924 return Qnil;
1925 }
1926 }
1927
840f481c 1928 count1++;
b3cfe0c8
RS
1929 }
1930
ef316cf0 1931 SET_PT_BOTH (from, from_byte);
b3cfe0c8
RS
1932 immediate_quit = 0;
1933 return Qt;
1934}
1935\f
6a140a74 1936static Lisp_Object
8489eb67
RS
1937scan_lists (from, count, depth, sexpflag)
1938 register int from;
1939 int count, depth, sexpflag;
1940{
1941 Lisp_Object val;
195d1361 1942 register int stop = count > 0 ? ZV : BEGV;
93da5fff
KH
1943 register int c, c1;
1944 int stringterm;
8489eb67
RS
1945 int quoted;
1946 int mathexit = 0;
93da5fff 1947 register enum syntaxcode code, temp_code;
195d1361 1948 int min_depth = depth; /* Err out if depth gets less than this. */
e5d4f4dc 1949 int comstyle = 0; /* style of comment encountered */
95ff8dfc 1950 int comnested = 0; /* whether the comment is nestable or not */
93da5fff 1951 int temp_pos;
7bf5e9e4 1952 int last_good = from;
195d1361 1953 int found;
c3907a7e 1954 int from_byte;
6a140a74 1955 int out_bytepos, out_charpos;
95ff8dfc 1956 int temp, dummy;
8489eb67
RS
1957
1958 if (depth > 0) min_depth = 0;
1959
c3907a7e
RS
1960 if (from > ZV) from = ZV;
1961 if (from < BEGV) from = BEGV;
1962
1963 from_byte = CHAR_TO_BYTE (from);
1964
8489eb67
RS
1965 immediate_quit = 1;
1966 QUIT;
1967
195d1361 1968 SETUP_SYNTAX_TABLE (from, count);
8489eb67
RS
1969 while (count > 0)
1970 {
8489eb67
RS
1971 while (from < stop)
1972 {
f902a008 1973 int comstart_first, prefix;
195d1361 1974 UPDATE_SYNTAX_TABLE_FORWARD (from);
6a140a74 1975 c = FETCH_CHAR (from_byte);
e5d4f4dc 1976 code = SYNTAX (c);
f902a008 1977 comstart_first = SYNTAX_COMSTART_FIRST (c);
95ff8dfc 1978 comnested = SYNTAX_COMMENT_NESTED (c);
f902a008 1979 prefix = SYNTAX_PREFIX (c);
7bf5e9e4
RS
1980 if (depth == min_depth)
1981 last_good = from;
6a140a74 1982 INC_BOTH (from, from_byte);
195d1361 1983 UPDATE_SYNTAX_TABLE_FORWARD (from);
f902a008 1984 if (from < stop && comstart_first
6a140a74 1985 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from_byte))
8489eb67 1986 && parse_sexp_ignore_comments)
e5d4f4dc
RS
1987 {
1988 /* we have encountered a comment start sequence and we
195d1361 1989 are ignoring all text inside comments. We must record
e5d4f4dc
RS
1990 the comment style this sequence begins so that later,
1991 only a comment end of the same style actually ends
1992 the comment section */
1993 code = Scomment;
95ff8dfc
RS
1994 c1 = FETCH_CHAR (from_byte);
1995 comstyle = SYNTAX_COMMENT_STYLE (c1);
1996 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
6a140a74 1997 INC_BOTH (from, from_byte);
f902a008 1998 UPDATE_SYNTAX_TABLE_FORWARD (from);
e5d4f4dc
RS
1999 }
2000
f902a008 2001 if (prefix)
8489eb67
RS
2002 continue;
2003
0220c518 2004 switch (SWITCH_ENUM_CAST (code))
8489eb67
RS
2005 {
2006 case Sescape:
2007 case Scharquote:
2008 if (from == stop) goto lose;
6a140a74 2009 INC_BOTH (from, from_byte);
8489eb67
RS
2010 /* treat following character as a word constituent */
2011 case Sword:
2012 case Ssymbol:
2013 if (depth || !sexpflag) break;
195d1361 2014 /* This word counts as a sexp; return at end of it. */
8489eb67
RS
2015 while (from < stop)
2016 {
195d1361 2017 UPDATE_SYNTAX_TABLE_FORWARD (from);
7c7ca3d2
RS
2018
2019 /* Some compilers can't handle this inside the switch. */
2020 temp = SYNTAX (FETCH_CHAR (from_byte));
2021 switch (temp)
8489eb67
RS
2022 {
2023 case Scharquote:
2024 case Sescape:
6a140a74 2025 INC_BOTH (from, from_byte);
8489eb67
RS
2026 if (from == stop) goto lose;
2027 break;
2028 case Sword:
2029 case Ssymbol:
2030 case Squote:
2031 break;
2032 default:
2033 goto done;
2034 }
6a140a74 2035 INC_BOTH (from, from_byte);
8489eb67
RS
2036 }
2037 goto done;
2038
195d1361 2039 case Scomment_fence:
95ff8dfc
RS
2040 comstyle = ST_COMMENT_STYLE;
2041 /* FALLTHROUGH */
2042 case Scomment:
8489eb67 2043 if (!parse_sexp_ignore_comments) break;
95ff8dfc
RS
2044 UPDATE_SYNTAX_TABLE_FORWARD (from);
2045 found = forw_comment (from, from_byte, stop,
2046 comnested, comstyle, 0,
2047 &out_charpos, &out_bytepos, &dummy);
2048 from = out_charpos, from_byte = out_bytepos;
2049 if (!found)
8489eb67 2050 {
95ff8dfc
RS
2051 if (depth == 0)
2052 goto done;
2053 goto lose;
8489eb67 2054 }
95ff8dfc
RS
2055 INC_BOTH (from, from_byte);
2056 UPDATE_SYNTAX_TABLE_FORWARD (from);
8489eb67
RS
2057 break;
2058
2059 case Smath:
2060 if (!sexpflag)
2061 break;
6a140a74
RS
2062 if (from != stop && c == FETCH_CHAR (from_byte))
2063 {
2064 INC_BOTH (from, from_byte);
2065 }
8489eb67
RS
2066 if (mathexit)
2067 {
2068 mathexit = 0;
2069 goto close1;
2070 }
2071 mathexit = 1;
2072
2073 case Sopen:
2074 if (!++depth) goto done;
2075 break;
2076
2077 case Sclose:
2078 close1:
2079 if (!--depth) goto done;
2080 if (depth < min_depth)
7bf5e9e4
RS
2081 Fsignal (Qscan_error,
2082 Fcons (build_string ("Containing expression ends prematurely"),
2083 Fcons (make_number (last_good),
2084 Fcons (make_number (from), Qnil))));
8489eb67
RS
2085 break;
2086
2087 case Sstring:
195d1361 2088 case Sstring_fence:
ef316cf0 2089 temp_pos = dec_bytepos (from_byte);
93da5fff 2090 stringterm = FETCH_CHAR (temp_pos);
8489eb67
RS
2091 while (1)
2092 {
2093 if (from >= stop) goto lose;
195d1361
RS
2094 UPDATE_SYNTAX_TABLE_FORWARD (from);
2095 if (code == Sstring
6a140a74
RS
2096 ? (FETCH_CHAR (from_byte) == stringterm)
2097 : SYNTAX (FETCH_CHAR (from_byte)) == Sstring_fence)
195d1361 2098 break;
7c7ca3d2
RS
2099
2100 /* Some compilers can't handle this inside the switch. */
2101 temp = SYNTAX (FETCH_CHAR (from_byte));
2102 switch (temp)
8489eb67
RS
2103 {
2104 case Scharquote:
2105 case Sescape:
6a140a74 2106 INC_BOTH (from, from_byte);
8489eb67 2107 }
6a140a74 2108 INC_BOTH (from, from_byte);
8489eb67 2109 }
6a140a74 2110 INC_BOTH (from, from_byte);
8489eb67
RS
2111 if (!depth && sexpflag) goto done;
2112 break;
2113 }
2114 }
2115
2116 /* Reached end of buffer. Error if within object, return nil if between */
2117 if (depth) goto lose;
2118
2119 immediate_quit = 0;
2120 return Qnil;
2121
2122 /* End of object reached */
2123 done:
2124 count--;
2125 }
2126
2127 while (count < 0)
2128 {
8489eb67
RS
2129 while (from > stop)
2130 {
6a140a74 2131 DEC_BOTH (from, from_byte);
195d1361 2132 UPDATE_SYNTAX_TABLE_BACKWARD (from);
6a140a74 2133 c = FETCH_CHAR (from_byte);
8489eb67 2134 code = SYNTAX (c);
7bf5e9e4
RS
2135 if (depth == min_depth)
2136 last_good = from;
7fc8191e 2137 comstyle = 0;
95ff8dfc 2138 comnested = SYNTAX_COMMENT_NESTED (c);
7fc8191e
RS
2139 if (code == Sendcomment)
2140 comstyle = SYNTAX_COMMENT_STYLE (c);
8489eb67 2141 if (from > stop && SYNTAX_COMEND_SECOND (c)
1674d9a2 2142 && prev_char_comend_first (from, from_byte)
8489eb67 2143 && parse_sexp_ignore_comments)
e5d4f4dc 2144 {
f902a008 2145 /* We must record the comment style encountered so that
e5d4f4dc 2146 later, we can match only the proper comment begin
f902a008 2147 sequence of the same style. */
6a140a74 2148 DEC_BOTH (from, from_byte);
f902a008
RS
2149 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2150 code = Sendcomment;
95ff8dfc
RS
2151 c1 = FETCH_CHAR (from_byte);
2152 comstyle = SYNTAX_COMMENT_STYLE (c1);
2153 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
e5d4f4dc
RS
2154 }
2155
9828a477 2156 /* Quoting turns anything except a comment-ender
f902a008
RS
2157 into a word character. Note that this if cannot be true
2158 if we decremented FROM in the if-statement above. */
9828a477
KH
2159 if (code != Sendcomment && char_quoted (from, from_byte))
2160 code = Sword;
2161 else if (SYNTAX_PREFIX (c))
8489eb67
RS
2162 continue;
2163
9828a477 2164 switch (SWITCH_ENUM_CAST (code))
8489eb67
RS
2165 {
2166 case Sword:
2167 case Ssymbol:
9828a477
KH
2168 case Sescape:
2169 case Scharquote:
8489eb67 2170 if (depth || !sexpflag) break;
195d1361
RS
2171 /* This word counts as a sexp; count object finished
2172 after passing it. */
8489eb67
RS
2173 while (from > stop)
2174 {
6a140a74 2175 temp_pos = from_byte;
ef316cf0
RS
2176 if (! NILP (current_buffer->enable_multibyte_characters))
2177 DEC_POS (temp_pos);
2178 else
2179 temp_pos--;
6a140a74 2180 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
9828a477
KH
2181 c1 = FETCH_CHAR (temp_pos);
2182 temp_code = SYNTAX (c1);
2183 /* Don't allow comment-end to be quoted. */
2184 if (temp_code == Sendcomment)
2185 goto done2;
6a140a74 2186 quoted = char_quoted (from - 1, temp_pos);
8489eb67 2187 if (quoted)
93da5fff 2188 {
6a140a74 2189 DEC_BOTH (from, from_byte);
ef316cf0 2190 temp_pos = dec_bytepos (temp_pos);
6a140a74 2191 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
93da5fff
KH
2192 }
2193 c1 = FETCH_CHAR (temp_pos);
2194 temp_code = SYNTAX (c1);
2195 if (! (quoted || temp_code == Sword
2196 || temp_code == Ssymbol
2197 || temp_code == Squote))
8489eb67 2198 goto done2;
6a140a74 2199 DEC_BOTH (from, from_byte);
8489eb67
RS
2200 }
2201 goto done2;
2202
2203 case Smath:
2204 if (!sexpflag)
2205 break;
ef316cf0 2206 temp_pos = dec_bytepos (from_byte);
6a140a74 2207 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
93da5fff 2208 if (from != stop && c == FETCH_CHAR (temp_pos))
6a140a74 2209 DEC_BOTH (from, from_byte);
8489eb67
RS
2210 if (mathexit)
2211 {
2212 mathexit = 0;
2213 goto open2;
2214 }
2215 mathexit = 1;
2216
2217 case Sclose:
2218 if (!++depth) goto done2;
2219 break;
2220
2221 case Sopen:
2222 open2:
2223 if (!--depth) goto done2;
2224 if (depth < min_depth)
7bf5e9e4
RS
2225 Fsignal (Qscan_error,
2226 Fcons (build_string ("Containing expression ends prematurely"),
2227 Fcons (make_number (last_good),
2228 Fcons (make_number (from), Qnil))));
8489eb67
RS
2229 break;
2230
2231 case Sendcomment:
2232 if (!parse_sexp_ignore_comments)
2233 break;
95ff8dfc 2234 found = back_comment (from, from_byte, stop, comnested, comstyle,
6a140a74
RS
2235 &out_charpos, &out_bytepos);
2236 if (found != -1)
2237 from = out_charpos, from_byte = out_bytepos;
8489eb67
RS
2238 break;
2239
195d1361
RS
2240 case Scomment_fence:
2241 case Sstring_fence:
2242 while (1)
2243 {
6a140a74 2244 DEC_BOTH (from, from_byte);
195d1361
RS
2245 if (from == stop) goto lose;
2246 UPDATE_SYNTAX_TABLE_BACKWARD (from);
6a140a74
RS
2247 if (!char_quoted (from, from_byte)
2248 && SYNTAX (FETCH_CHAR (from_byte)) == code)
195d1361
RS
2249 break;
2250 }
2251 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2252 break;
2253
8489eb67 2254 case Sstring:
6a140a74 2255 stringterm = FETCH_CHAR (from_byte);
8489eb67
RS
2256 while (1)
2257 {
2258 if (from == stop) goto lose;
6a140a74 2259 temp_pos = from_byte;
ef316cf0
RS
2260 if (! NILP (current_buffer->enable_multibyte_characters))
2261 DEC_POS (temp_pos);
2262 else
2263 temp_pos--;
6a140a74
RS
2264 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2265 if (!char_quoted (from - 1, temp_pos)
93da5fff 2266 && stringterm == FETCH_CHAR (temp_pos))
8489eb67 2267 break;
6a140a74 2268 DEC_BOTH (from, from_byte);
8489eb67 2269 }
6a140a74 2270 DEC_BOTH (from, from_byte);
8489eb67
RS
2271 if (!depth && sexpflag) goto done2;
2272 break;
2273 }
2274 }
2275
2276 /* Reached start of buffer. Error if within object, return nil if between */
2277 if (depth) goto lose;
2278
2279 immediate_quit = 0;
2280 return Qnil;
2281
2282 done2:
2283 count++;
2284 }
2285
2286
2287 immediate_quit = 0;
1e142fb7 2288 XSETFASTINT (val, from);
8489eb67
RS
2289 return val;
2290
2291 lose:
7bf5e9e4
RS
2292 Fsignal (Qscan_error,
2293 Fcons (build_string ("Unbalanced parentheses"),
2294 Fcons (make_number (last_good),
2295 Fcons (make_number (from), Qnil))));
2296
8489eb67
RS
2297 /* NOTREACHED */
2298}
2299
8489eb67
RS
2300DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2301 "Scan from character number FROM by COUNT lists.\n\
2302Returns the character number of the position thus found.\n\
2303\n\
2304If DEPTH is nonzero, paren depth begins counting from that value,\n\
2305only places where the depth in parentheses becomes zero\n\
2306are candidates for stopping; COUNT such places are counted.\n\
2307Thus, a positive value for DEPTH means go out levels.\n\
2308\n\
2309Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
2310\n\
2311If the beginning or end of (the accessible part of) the buffer is reached\n\
2312and the depth is wrong, an error is signaled.\n\
2313If the depth is right but the count is not used up, nil is returned.")
2314 (from, count, depth)
2315 Lisp_Object from, count, depth;
2316{
2317 CHECK_NUMBER (from, 0);
2318 CHECK_NUMBER (count, 1);
2319 CHECK_NUMBER (depth, 2);
2320
2321 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2322}
2323
2324DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2325 "Scan from character number FROM by COUNT balanced expressions.\n\
2326If COUNT is negative, scan backwards.\n\
2327Returns the character number of the position thus found.\n\
2328\n\
2329Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
2330\n\
2331If the beginning or end of (the accessible part of) the buffer is reached\n\
2332in the middle of a parenthetical grouping, an error is signaled.\n\
2333If the beginning or end is reached between groupings\n\
2334but before count is used up, nil is returned.")
2335 (from, count)
2336 Lisp_Object from, count;
2337{
2338 CHECK_NUMBER (from, 0);
2339 CHECK_NUMBER (count, 1);
2340
2341 return scan_lists (XINT (from), XINT (count), 0, 1);
2342}
2343
2344DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2345 0, 0, 0,
2346 "Move point backward over any number of chars with prefix syntax.\n\
2347This includes chars with \"quote\" or \"prefix\" syntax (' or p).")
2348 ()
2349{
2350 int beg = BEGV;
6a140a74
RS
2351 int opoint = PT;
2352 int opoint_byte = PT_BYTE;
6ec8bbd2 2353 int pos = PT;
6a140a74 2354 int pos_byte = PT_BYTE;
93da5fff 2355 int c;
93da5fff 2356
f902a008 2357 if (pos <= beg)
195d1361 2358 {
f902a008
RS
2359 SET_PT_BOTH (opoint, opoint_byte);
2360
2361 return Qnil;
195d1361 2362 }
8489eb67 2363
f902a008
RS
2364 SETUP_SYNTAX_TABLE (pos, -1);
2365
6a140a74
RS
2366 DEC_BOTH (pos, pos_byte);
2367
1fd3172d 2368 while (!char_quoted (pos, pos_byte)
195d1361 2369 /* Previous statement updates syntax table. */
6a140a74 2370 && ((c = FETCH_CHAR (pos_byte), SYNTAX (c) == Squote)
93da5fff
KH
2371 || SYNTAX_PREFIX (c)))
2372 {
1fd3172d
RS
2373 opoint = pos;
2374 opoint_byte = pos_byte;
2375
2376 if (pos + 1 > beg)
2377 DEC_BOTH (pos, pos_byte);
93da5fff 2378 }
8489eb67 2379
6a140a74 2380 SET_PT_BOTH (opoint, opoint_byte);
8489eb67
RS
2381
2382 return Qnil;
2383}
2384\f
6a140a74 2385/* Parse forward from FROM / FROM_BYTE to END,
e5d4f4dc
RS
2386 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2387 and return a description of the state of the parse at END.
c81a3712 2388 If STOPBEFORE is nonzero, stop at the start of an atom.
644ea4df
RS
2389 If COMMENTSTOP is 1, stop at the start of a comment.
2390 If COMMENTSTOP is -1, stop at the start or end of a comment,
2391 after the beginning of a string, or after the end of a string. */
8489eb67 2392
340f92b5 2393static void
6a140a74 2394scan_sexps_forward (stateptr, from, from_byte, end, targetdepth,
c81a3712 2395 stopbefore, oldstate, commentstop)
e5d4f4dc 2396 struct lisp_parse_state *stateptr;
8489eb67
RS
2397 register int from;
2398 int end, targetdepth, stopbefore;
2399 Lisp_Object oldstate;
c81a3712 2400 int commentstop;
8489eb67
RS
2401{
2402 struct lisp_parse_state state;
2403
2404 register enum syntaxcode code;
95ff8dfc
RS
2405 int c1;
2406 int comnested;
8489eb67
RS
2407 struct level { int last, prev; };
2408 struct level levelstart[100];
2409 register struct level *curlevel = levelstart;
2410 struct level *endlevel = levelstart + 100;
8489eb67
RS
2411 register int depth; /* Paren depth of current scanning location.
2412 level - levelstart equals this except
2413 when the depth becomes negative. */
2414 int mindepth; /* Lowest DEPTH value seen. */
2415 int start_quoted = 0; /* Nonzero means starting after a char quote */
2416 Lisp_Object tem;
93da5fff 2417 int prev_from; /* Keep one character before FROM. */
6a140a74 2418 int prev_from_byte;
1fd3172d 2419 int prev_from_syntax;
195d1361
RS
2420 int boundary_stop = commentstop == -1;
2421 int nofence;
95ff8dfc
RS
2422 int found;
2423 int out_bytepos, out_charpos;
7c7ca3d2 2424 int temp;
93da5fff
KH
2425
2426 prev_from = from;
6a140a74
RS
2427 prev_from_byte = from_byte;
2428 if (from != BEGV)
2429 DEC_BOTH (prev_from, prev_from_byte);
93da5fff
KH
2430
2431 /* Use this macro instead of `from++'. */
6a140a74
RS
2432#define INC_FROM \
2433do { prev_from = from; \
2434 prev_from_byte = from_byte; \
1fd3172d
RS
2435 prev_from_syntax \
2436 = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte)); \
ef316cf0 2437 INC_BOTH (from, from_byte); \
1fd3172d 2438 UPDATE_SYNTAX_TABLE_FORWARD (from); \
6a140a74 2439 } while (0)
8489eb67
RS
2440
2441 immediate_quit = 1;
2442 QUIT;
2443
265a9e55 2444 if (NILP (oldstate))
8489eb67
RS
2445 {
2446 depth = 0;
2447 state.instring = -1;
2448 state.incomment = 0;
195d1361
RS
2449 state.comstyle = 0; /* comment style a by default. */
2450 state.comstr_start = -1; /* no comment/string seen. */
8489eb67
RS
2451 }
2452 else
2453 {
2454 tem = Fcar (oldstate);
265a9e55 2455 if (!NILP (tem))
8489eb67
RS
2456 depth = XINT (tem);
2457 else
2458 depth = 0;
2459
2460 oldstate = Fcdr (oldstate);
2461 oldstate = Fcdr (oldstate);
2462 oldstate = Fcdr (oldstate);
2463 tem = Fcar (oldstate);
195d1361
RS
2464 /* Check whether we are inside string_fence-style string: */
2465 state.instring = ( !NILP (tem)
2466 ? ( INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
2467 : -1);
8489eb67
RS
2468
2469 oldstate = Fcdr (oldstate);
2470 tem = Fcar (oldstate);
95ff8dfc
RS
2471 state.incomment = ( !NILP (tem)
2472 ? ( INTEGERP (tem) ? XINT (tem) : -1)
2473 : 0);
8489eb67
RS
2474
2475 oldstate = Fcdr (oldstate);
2476 tem = Fcar (oldstate);
265a9e55 2477 start_quoted = !NILP (tem);
e5d4f4dc 2478
95ff8dfc 2479 /* if the eighth element of the list is nil, we are in comment
195d1361
RS
2480 style a. If it is non-nil, we are in comment style b */
2481 oldstate = Fcdr (oldstate);
e5d4f4dc 2482 oldstate = Fcdr (oldstate);
195d1361
RS
2483 tem = Fcar (oldstate);
2484 state.comstyle = NILP (tem) ? 0 : ( EQ (tem, Qsyntax_table)
2485 ? ST_COMMENT_STYLE : 1 );
2486
e5d4f4dc 2487 oldstate = Fcdr (oldstate);
e5d4f4dc 2488 tem = Fcar (oldstate);
195d1361 2489 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
1a102a58
RS
2490 oldstate = Fcdr (oldstate);
2491 tem = Fcar (oldstate);
2492 while (!NILP (tem)) /* >= second enclosing sexps. */
2493 {
2494 /* curlevel++->last ran into compiler bug on Apollo */
2495 curlevel->last = XINT (Fcar (tem));
2496 if (++curlevel == endlevel)
2497 error ("Nesting too deep for parser");
2498 curlevel->prev = -1;
2499 curlevel->last = -1;
2500 tem = Fcdr (tem);
2501 }
8489eb67
RS
2502 }
2503 state.quoted = 0;
2504 mindepth = depth;
2505
2506 curlevel->prev = -1;
2507 curlevel->last = -1;
2508
326b283b
RS
2509 SETUP_SYNTAX_TABLE (prev_from, 1);
2510 prev_from_syntax = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte));
2511 UPDATE_SYNTAX_TABLE_FORWARD (from);
2512
195d1361 2513 /* Enter the loop at a place appropriate for initial state. */
8489eb67 2514
326b283b
RS
2515 if (state.incomment)
2516 goto startincomment;
8489eb67
RS
2517 if (state.instring >= 0)
2518 {
195d1361 2519 nofence = state.instring != ST_STRING_STYLE;
326b283b
RS
2520 if (start_quoted)
2521 goto startquotedinstring;
8489eb67
RS
2522 goto startinstring;
2523 }
326b283b
RS
2524 else if (start_quoted)
2525 goto startquoted;
1fd3172d 2526
ccf89641 2527#if 0 /* This seems to be redundant with the identical code above. */
1fd3172d
RS
2528 SETUP_SYNTAX_TABLE (prev_from, 1);
2529 prev_from_syntax = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte));
2530 UPDATE_SYNTAX_TABLE_FORWARD (from);
ccf89641 2531#endif
1fd3172d 2532
8489eb67
RS
2533 while (from < end)
2534 {
93da5fff 2535 INC_FROM;
1fd3172d 2536 code = prev_from_syntax & 0xff;
4c920633 2537
8f9dc2ed 2538 if (code == Scomment)
95ff8dfc
RS
2539 {
2540 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
2541 1 : -1);
2542 state.comstr_start = prev_from;
2543 }
4c920633 2544 else if (code == Scomment_fence)
e5d4f4dc
RS
2545 {
2546 /* Record the comment style we have entered so that only
2547 the comment-end sequence of the same style actually
2548 terminates the comment section. */
95ff8dfc
RS
2549 state.comstyle = ST_COMMENT_STYLE;
2550 state.incomment = -1;
195d1361 2551 state.comstr_start = prev_from;
e5d4f4dc 2552 code = Scomment;
e5d4f4dc 2553 }
4c920633 2554 else if (from < end)
1fd3172d 2555 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax))
95ff8dfc
RS
2556 if (c1 = FETCH_CHAR (from_byte),
2557 SYNTAX_COMSTART_SECOND (c1))
1fd3172d 2558 /* Duplicate code to avoid a complex if-expression
4c920633
RS
2559 which causes trouble for the SGI compiler. */
2560 {
2561 /* Record the comment style we have entered so that only
2562 the comment-end sequence of the same style actually
2563 terminates the comment section. */
95ff8dfc
RS
2564 state.comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from_byte));
2565 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
2566 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2567 state.incomment = comnested ? 1 : -1;
4c920633 2568 state.comstr_start = prev_from;
95ff8dfc 2569 INC_FROM;
4c920633
RS
2570 code = Scomment;
2571 }
e5d4f4dc 2572
1fd3172d 2573 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
8489eb67 2574 continue;
0220c518 2575 switch (SWITCH_ENUM_CAST (code))
8489eb67
RS
2576 {
2577 case Sescape:
2578 case Scharquote:
2579 if (stopbefore) goto stop; /* this arg means stop at sexp start */
93da5fff 2580 curlevel->last = prev_from;
8489eb67
RS
2581 startquoted:
2582 if (from == end) goto endquoted;
93da5fff 2583 INC_FROM;
8489eb67
RS
2584 goto symstarted;
2585 /* treat following character as a word constituent */
2586 case Sword:
2587 case Ssymbol:
2588 if (stopbefore) goto stop; /* this arg means stop at sexp start */
93da5fff 2589 curlevel->last = prev_from;
8489eb67
RS
2590 symstarted:
2591 while (from < end)
2592 {
7c7ca3d2
RS
2593 /* Some compilers can't handle this inside the switch. */
2594 temp = SYNTAX (FETCH_CHAR (from_byte));
2595 switch (temp)
8489eb67
RS
2596 {
2597 case Scharquote:
2598 case Sescape:
93da5fff 2599 INC_FROM;
8489eb67
RS
2600 if (from == end) goto endquoted;
2601 break;
2602 case Sword:
2603 case Ssymbol:
2604 case Squote:
2605 break;
2606 default:
2607 goto symdone;
2608 }
93da5fff 2609 INC_FROM;
8489eb67
RS
2610 }
2611 symdone:
2612 curlevel->prev = curlevel->last;
2613 break;
2614
5a28e48c 2615 startincomment:
195d1361 2616 if (commentstop == 1)
5a28e48c 2617 goto done;
195d1361 2618 goto commentloop;
5a28e48c 2619
8489eb67 2620 case Scomment:
95ff8dfc 2621 assert (state.incomment != 0); /* state.incomment = -1; */
195d1361
RS
2622 if (commentstop || boundary_stop) goto done;
2623 commentloop:
95ff8dfc
RS
2624 /* The (from == BEGV) test is to enter the loop in the middle so
2625 that we find a 2-char comment ender even if we start in the
2626 middle of it. */
2627 found = forw_comment (from, from_byte, end,
2628 state.incomment, state.comstyle,
2629 (from == BEGV) ? 0 : prev_from_syntax,
2630 &out_charpos, &out_bytepos, &state.incomment);
2631 from = out_charpos; from_byte = out_bytepos;
2632 /* Beware! prev_from and friends are invalid now.
2633 Luckily, the `done' doesn't use them and the INC_FROM
2634 sets them to a sane value without looking at them. */
2635 if (!found) goto done;
195d1361 2636 INC_FROM;
8489eb67 2637 state.incomment = 0;
e5d4f4dc 2638 state.comstyle = 0; /* reset the comment style */
195d1361 2639 if (boundary_stop) goto done;
8489eb67
RS
2640 break;
2641
2642 case Sopen:
2643 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2644 depth++;
2645 /* curlevel++->last ran into compiler bug on Apollo */
93da5fff 2646 curlevel->last = prev_from;
8489eb67
RS
2647 if (++curlevel == endlevel)
2648 error ("Nesting too deep for parser");
2649 curlevel->prev = -1;
2650 curlevel->last = -1;
30844415 2651 if (targetdepth == depth) goto done;
8489eb67
RS
2652 break;
2653
2654 case Sclose:
2655 depth--;
2656 if (depth < mindepth)
2657 mindepth = depth;
2658 if (curlevel != levelstart)
2659 curlevel--;
2660 curlevel->prev = curlevel->last;
30844415 2661 if (targetdepth == depth) goto done;
8489eb67
RS
2662 break;
2663
2664 case Sstring:
195d1361
RS
2665 case Sstring_fence:
2666 state.comstr_start = from - 1;
8489eb67 2667 if (stopbefore) goto stop; /* this arg means stop at sexp start */
93da5fff 2668 curlevel->last = prev_from;
195d1361 2669 state.instring = (code == Sstring
6a140a74 2670 ? (FETCH_CHAR (prev_from_byte))
195d1361
RS
2671 : ST_STRING_STYLE);
2672 if (boundary_stop) goto done;
8489eb67 2673 startinstring:
195d1361 2674 {
644ea4df 2675 nofence = state.instring != ST_STRING_STYLE;
195d1361 2676
644ea4df
RS
2677 while (1)
2678 {
2679 int c;
195d1361 2680
644ea4df 2681 if (from >= end) goto done;
6a140a74 2682 c = FETCH_CHAR (from_byte);
7c7ca3d2
RS
2683 /* Some compilers can't handle this inside the switch. */
2684 temp = SYNTAX (c);
ccf89641
KH
2685
2686 /* Check TEMP here so that if the char has
2687 a syntax-table property which says it is NOT
2688 a string character, it does not end the string. */
2689 if (nofence && c == state.instring && temp == Sstring)
2690 break;
2691
7c7ca3d2 2692 switch (temp)
644ea4df
RS
2693 {
2694 case Sstring_fence:
2695 if (!nofence) goto string_end;
2696 break;
2697 case Scharquote:
2698 case Sescape:
2699 INC_FROM;
2700 startquotedinstring:
2701 if (from >= end) goto endquoted;
195d1361 2702 }
644ea4df
RS
2703 INC_FROM;
2704 }
195d1361
RS
2705 }
2706 string_end:
8489eb67
RS
2707 state.instring = -1;
2708 curlevel->prev = curlevel->last;
93da5fff 2709 INC_FROM;
195d1361 2710 if (boundary_stop) goto done;
8489eb67
RS
2711 break;
2712
2713 case Smath:
2714 break;
2715 }
2716 }
2717 goto done;
2718
2719 stop: /* Here if stopping before start of sexp. */
93da5fff 2720 from = prev_from; /* We have just fetched the char that starts it; */
8489eb67
RS
2721 goto done; /* but return the position before it. */
2722
2723 endquoted:
2724 state.quoted = 1;
2725 done:
2726 state.depth = depth;
2727 state.mindepth = mindepth;
2728 state.thislevelstart = curlevel->prev;
2729 state.prevlevelstart
2730 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
2731 state.location = from;
1a102a58
RS
2732 state.levelstarts = Qnil;
2733 while (--curlevel >= levelstart)
2734 state.levelstarts = Fcons (make_number (curlevel->last),
2735 state.levelstarts);
8489eb67
RS
2736 immediate_quit = 0;
2737
e5d4f4dc 2738 *stateptr = state;
8489eb67
RS
2739}
2740
2741/* This comment supplies the doc string for parse-partial-sexp,
2742 for make-docfile to see. We cannot put this in the real DEFUN
2743 due to limits in the Unix cpp.
2744
c81a3712 2745DEFUN ("parse-partial-sexp", Ffoo, Sfoo, 2, 6, 0,
8489eb67
RS
2746 "Parse Lisp syntax starting at FROM until TO; return status of parse at TO.\n\
2747Parsing stops at TO or when certain criteria are met;\n\
2748 point is set to where parsing stops.\n\
2749If fifth arg STATE is omitted or nil,\n\
2750 parsing assumes that FROM is the beginning of a function.\n\
1a102a58 2751Value is a list of ten elements describing final state of parsing:\n\
af50f9e5
RS
2752 0. depth in parens.\n\
2753 1. character address of start of innermost containing list; nil if none.\n\
2754 2. character address of start of last complete sexp terminated.\n\
2755 3. non-nil if inside a string.\n\
195d1361 2756 (it is the character that will terminate the string,\n\
bff37d2a 2757 or t if the string should be terminated by a generic string delimiter.)\n\
95ff8dfc
RS
2758 4. nil if outside a comment, t if inside a non-nestable comment, \n\
2759 else an integer (the current comment nesting).\n\
af50f9e5
RS
2760 5. t if following a quote character.\n\
2761 6. the minimum paren-depth encountered during this scan.\n\
bff37d2a
RS
2762 7. t if in a comment of style b; `syntax-table' if the comment\n\
2763 should be terminated by a generic comment delimiter.\n\
2764 8. character address of start of comment or string; nil if not in one.\n\
1a102a58 2765 9. Intermediate data for continuation of parsing (subject to change).\n\
8489eb67
RS
2766If third arg TARGETDEPTH is non-nil, parsing stops if the depth\n\
2767in parentheses becomes equal to TARGETDEPTH.\n\
2768Fourth arg STOPBEFORE non-nil means stop when come to\n\
2769 any character that starts a sexp.\n\
bff37d2a
RS
2770Fifth arg STATE is a nine-element list like what this function returns.\n\
2771 It is used to initialize the state of the parse. Elements number 1, 2, 6\n\
2772 and 8 are ignored; you can leave off element 8 (the last) entirely.\n\
2773Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.\n\
2774 If it is `syntax-table', stop after the start of a comment or a string,\n\
2775 or after end of a comment or a string.")
c81a3712 2776 (from, to, targetdepth, stopbefore, state, commentstop)
8489eb67
RS
2777*/
2778
c81a3712 2779DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
8489eb67 2780 0 /* See immediately above */)
c81a3712
RS
2781 (from, to, targetdepth, stopbefore, oldstate, commentstop)
2782 Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop;
8489eb67
RS
2783{
2784 struct lisp_parse_state state;
2785 int target;
2786
265a9e55 2787 if (!NILP (targetdepth))
8489eb67
RS
2788 {
2789 CHECK_NUMBER (targetdepth, 3);
2790 target = XINT (targetdepth);
2791 }
2792 else
2793 target = -100000; /* We won't reach this depth */
2794
2795 validate_region (&from, &to);
6a140a74
RS
2796 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
2797 XINT (to),
c81a3712 2798 target, !NILP (stopbefore), oldstate,
195d1361
RS
2799 (NILP (commentstop)
2800 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
8489eb67
RS
2801
2802 SET_PT (state.location);
2803
2804 return Fcons (make_number (state.depth),
2805 Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart),
2806 Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart),
195d1361
RS
2807 Fcons (state.instring >= 0
2808 ? (state.instring == ST_STRING_STYLE
2809 ? Qt : make_number (state.instring)) : Qnil,
95ff8dfc
RS
2810 Fcons (state.incomment < 0 ? Qt :
2811 (state.incomment == 0 ? Qnil :
2812 make_number (state.incomment)),
8489eb67 2813 Fcons (state.quoted ? Qt : Qnil,
bff37d2a
RS
2814 Fcons (make_number (state.mindepth),
2815 Fcons ((state.comstyle
2816 ? (state.comstyle == ST_COMMENT_STYLE
2817 ? Qsyntax_table : Qt) :
2818 Qnil),
0beaf54f
DL
2819 Fcons (((state.incomment
2820 || (state.instring >= 0))
bff37d2a
RS
2821 ? make_number (state.comstr_start)
2822 : Qnil),
1a102a58 2823 Fcons (state.levelstarts, Qnil))))))))));
8489eb67
RS
2824}
2825\f
dfcf069d 2826void
8489eb67
RS
2827init_syntax_once ()
2828{
78f9a1f7 2829 register int i, c;
8ea151b2 2830 Lisp_Object temp;
8489eb67 2831
5ebaddf5
RS
2832 /* This has to be done here, before we call Fmake_char_table. */
2833 Qsyntax_table = intern ("syntax-table");
2834 staticpro (&Qsyntax_table);
2835
2836 /* Intern this now in case it isn't already done.
2837 Setting this variable twice is harmless.
2838 But don't staticpro it here--that is done in alloc.c. */
2839 Qchar_table_extra_slots = intern ("char-table-extra-slots");
2840
93da5fff 2841 /* Create objects which can be shared among syntax tables. */
44d8a793 2842 Vsyntax_code_object = Fmake_vector (make_number (13), Qnil);
93da5fff
KH
2843 for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++)
2844 XVECTOR (Vsyntax_code_object)->contents[i]
2845 = Fcons (make_number (i), Qnil);
2846
5ebaddf5
RS
2847 /* Now we are ready to set up this property, so we can
2848 create syntax tables. */
2849 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
2850
93da5fff 2851 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
8489eb67 2852
5ebaddf5 2853 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
8489eb67 2854
93da5fff 2855 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
8489eb67 2856 for (i = 'a'; i <= 'z'; i++)
8ea151b2 2857 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
8489eb67 2858 for (i = 'A'; i <= 'Z'; i++)
8ea151b2 2859 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
8489eb67 2860 for (i = '0'; i <= '9'; i++)
8ea151b2
RS
2861 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
2862
2863 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
2864 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
2865
2866 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
2867 Fcons (make_number (Sopen), make_number (')')));
2868 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
2869 Fcons (make_number (Sclose), make_number ('(')));
2870 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
2871 Fcons (make_number (Sopen), make_number (']')));
2872 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
2873 Fcons (make_number (Sclose), make_number ('[')));
2874 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
2875 Fcons (make_number (Sopen), make_number ('}')));
2876 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
2877 Fcons (make_number (Sclose), make_number ('{')));
2878 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
2879 Fcons (make_number ((int) Sstring), Qnil));
2880 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
2881 Fcons (make_number ((int) Sescape), Qnil));
2882
93da5fff 2883 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
8489eb67 2884 for (i = 0; i < 10; i++)
78f9a1f7
KH
2885 {
2886 c = "_-+*/&|<>="[i];
2887 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
2888 }
8489eb67 2889
93da5fff 2890 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
8489eb67 2891 for (i = 0; i < 12; i++)
78f9a1f7
KH
2892 {
2893 c = ".,;:?!#@~^'`"[i];
2894 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
2895 }
8489eb67
RS
2896}
2897
dfcf069d 2898void
8489eb67
RS
2899syms_of_syntax ()
2900{
2901 Qsyntax_table_p = intern ("syntax-table-p");
2902 staticpro (&Qsyntax_table_p);
2903
93da5fff
KH
2904 staticpro (&Vsyntax_code_object);
2905
7bf5e9e4
RS
2906 Qscan_error = intern ("scan-error");
2907 staticpro (&Qscan_error);
2908 Fput (Qscan_error, Qerror_conditions,
f3be100f 2909 Fcons (Qscan_error, Fcons (Qerror, Qnil)));
7bf5e9e4
RS
2910 Fput (Qscan_error, Qerror_message,
2911 build_string ("Scan error"));
2912
8489eb67
RS
2913 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
2914 "Non-nil means `forward-sexp', etc., should treat comments as whitespace.");
2915
195d1361
RS
2916 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties,
2917 "Non-nil means `forward-sexp', etc., grant `syntax-table' property.\n\
2918The value of this property should be either a syntax table, or a cons\n\
2919of the form (SYNTAXCODE . MATCHCHAR), SYNTAXCODE being the numeric\n\
2920syntax code, MATCHCHAR being nil or the character to match (which is\n\
2921relevant only for open/close type.");
2922
8489eb67
RS
2923 words_include_escapes = 0;
2924 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
2925 "Non-nil means `forward-word', etc., should treat escape chars part of words.");
2926
2927 defsubr (&Ssyntax_table_p);
2928 defsubr (&Ssyntax_table);
2929 defsubr (&Sstandard_syntax_table);
2930 defsubr (&Scopy_syntax_table);
2931 defsubr (&Sset_syntax_table);
2932 defsubr (&Schar_syntax);
beefa22e 2933 defsubr (&Smatching_paren);
8489eb67
RS
2934 defsubr (&Smodify_syntax_entry);
2935 defsubr (&Sdescribe_syntax);
2936
2937 defsubr (&Sforward_word);
2938
195d1361
RS
2939 defsubr (&Sskip_chars_forward);
2940 defsubr (&Sskip_chars_backward);
2941 defsubr (&Sskip_syntax_forward);
2942 defsubr (&Sskip_syntax_backward);
2943
b3cfe0c8 2944 defsubr (&Sforward_comment);
8489eb67
RS
2945 defsubr (&Sscan_lists);
2946 defsubr (&Sscan_sexps);
2947 defsubr (&Sbackward_prefix_chars);
2948 defsubr (&Sparse_partial_sexp);
2949}