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