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