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