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