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