]> code.delx.au - gnu-emacs/blob - src/syntax.c
*** empty log message ***
[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, 1999 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 "character.h"
28 #include "keymap.h"
29
30 /* Make syntax table lookup grant data in gl_state. */
31 #define SYNTAX_ENTRY_VIA_PROPERTY
32
33 #include "syntax.h"
34 #include "intervals.h"
35
36 /* We use these constants in place for comment-style and
37 string-ender-char to distinguish comments/strings started by
38 comment_fence and string_fence codes. */
39
40 #define ST_COMMENT_STYLE (256 + 1)
41 #define ST_STRING_STYLE (256 + 2)
42 #include "category.h"
43
44 Lisp_Object Qsyntax_table_p, Qsyntax_table, Qscan_error;
45
46 int words_include_escapes;
47 int parse_sexp_lookup_properties;
48
49 /* Nonzero means `scan-sexps' treat all multibyte characters as symbol. */
50 int multibyte_syntax_as_symbol;
51
52 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
53 if not compiled with GCC. No need to mark it, since it is used
54 only very temporarily. */
55 Lisp_Object syntax_temp;
56
57 /* Non-zero means an open parenthesis in column 0 is always considered
58 to be the start of a defun. Zero means an open parenthesis in
59 column 0 has no special meaning. */
60
61 int open_paren_in_column_0_is_defun_start;
62
63 /* This is the internal form of the parse state used in parse-partial-sexp. */
64
65 struct lisp_parse_state
66 {
67 int depth; /* Depth at end of parsing. */
68 int instring; /* -1 if not within string, else desired terminator. */
69 int incomment; /* -1 if in unnestable comment else comment nesting */
70 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE. */
71 int quoted; /* Nonzero if just after an escape char at end of parsing */
72 int thislevelstart; /* Char number of most recent start-of-expression at current level */
73 int prevlevelstart; /* Char number of start of containing expression */
74 int location; /* Char number at which parsing stopped. */
75 int mindepth; /* Minimum depth seen while scanning. */
76 int comstr_start; /* Position just after last comment/string starter. */
77 Lisp_Object levelstarts; /* Char numbers of starts-of-expression
78 of levels (starting from outermost). */
79 };
80 \f
81 /* These variables are a cache for finding the start of a defun.
82 find_start_pos is the place for which the defun start was found.
83 find_start_value is the defun start position found for it.
84 find_start_value_byte is the corresponding byte position.
85 find_start_buffer is the buffer it was found in.
86 find_start_begv is the BEGV value when it was found.
87 find_start_modiff is the value of MODIFF when it was found. */
88
89 static int find_start_pos;
90 static int find_start_value;
91 static int find_start_value_byte;
92 static struct buffer *find_start_buffer;
93 static int find_start_begv;
94 static int find_start_modiff;
95
96
97 static int find_defun_start P_ ((int, int));
98 static int back_comment P_ ((int, int, int, int, int, int *, int *));
99 static int char_quoted P_ ((int, int));
100 static Lisp_Object skip_chars P_ ((int, Lisp_Object, Lisp_Object));
101 static Lisp_Object skip_syntaxes P_ ((int, Lisp_Object, Lisp_Object));
102 static Lisp_Object scan_lists P_ ((int, int, int, int));
103 static void scan_sexps_forward P_ ((struct lisp_parse_state *,
104 int, int, int, int,
105 int, Lisp_Object, int));
106 \f
107
108 struct gl_state_s gl_state; /* Global state of syntax parser. */
109
110 INTERVAL interval_of ();
111 #define INTERVALS_AT_ONCE 10 /* 1 + max-number of intervals
112 to scan to property-change. */
113
114 /* Update gl_state to an appropriate interval which contains CHARPOS. The
115 sign of COUNT give the relative position of CHARPOS wrt the previously
116 valid interval. If INIT, only [be]_property fields of gl_state are
117 valid at start, the rest is filled basing on OBJECT.
118
119 `gl_state.*_i' are the intervals, and CHARPOS is further in the search
120 direction than the intervals - or in an interval. We update the
121 current syntax-table basing on the property of this interval, and
122 update the interval to start further than CHARPOS - or be
123 NULL_INTERVAL. We also update lim_property to be the next value of
124 charpos to call this subroutine again - or be before/after the
125 start/end of OBJECT. */
126
127 void
128 update_syntax_table (charpos, count, init, object)
129 int charpos, count, init;
130 Lisp_Object object;
131 {
132 Lisp_Object tmp_table;
133 int cnt = 0, invalidate = 1;
134 INTERVAL i, oldi;
135
136 if (init)
137 {
138 gl_state.old_prop = Qnil;
139 gl_state.start = gl_state.b_property;
140 gl_state.stop = gl_state.e_property;
141 i = interval_of (charpos, object);
142 gl_state.backward_i = gl_state.forward_i = i;
143 invalidate = 0;
144 if (NULL_INTERVAL_P (i))
145 return;
146 /* interval_of updates only ->position of the return value, so
147 update the parents manually to speed up update_interval. */
148 while (!NULL_PARENT (i))
149 {
150 if (AM_RIGHT_CHILD (i))
151 INTERVAL_PARENT (i)->position = i->position
152 - LEFT_TOTAL_LENGTH (i) + TOTAL_LENGTH (i) /* right end */
153 - TOTAL_LENGTH (INTERVAL_PARENT (i))
154 + LEFT_TOTAL_LENGTH (INTERVAL_PARENT (i));
155 else
156 INTERVAL_PARENT (i)->position = i->position - LEFT_TOTAL_LENGTH (i)
157 + TOTAL_LENGTH (i);
158 i = INTERVAL_PARENT (i);
159 }
160 i = gl_state.forward_i;
161 gl_state.b_property = i->position - gl_state.offset;
162 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
163 goto update;
164 }
165 oldi = i = count > 0 ? gl_state.forward_i : gl_state.backward_i;
166
167 /* We are guaranteed to be called with CHARPOS either in i,
168 or further off. */
169 if (NULL_INTERVAL_P (i))
170 error ("Error in syntax_table logic for to-the-end intervals");
171 else if (charpos < i->position) /* Move left. */
172 {
173 if (count > 0)
174 error ("Error in syntax_table logic for intervals <-");
175 /* Update the interval. */
176 i = update_interval (i, charpos);
177 if (INTERVAL_LAST_POS (i) != gl_state.b_property)
178 {
179 invalidate = 0;
180 gl_state.forward_i = i;
181 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
182 }
183 }
184 else if (charpos >= INTERVAL_LAST_POS (i)) /* Move right. */
185 {
186 if (count < 0)
187 error ("Error in syntax_table logic for intervals ->");
188 /* Update the interval. */
189 i = update_interval (i, charpos);
190 if (i->position != gl_state.e_property)
191 {
192 invalidate = 0;
193 gl_state.backward_i = i;
194 gl_state.b_property = i->position - gl_state.offset;
195 }
196 }
197
198 update:
199 tmp_table = textget (i->plist, Qsyntax_table);
200
201 if (invalidate)
202 invalidate = !EQ (tmp_table, gl_state.old_prop); /* Need to invalidate? */
203
204 if (invalidate) /* Did not get to adjacent interval. */
205 { /* with the same table => */
206 /* invalidate the old range. */
207 if (count > 0)
208 {
209 gl_state.backward_i = i;
210 gl_state.b_property = i->position - gl_state.offset;
211 }
212 else
213 {
214 gl_state.forward_i = i;
215 gl_state.e_property = INTERVAL_LAST_POS (i) - gl_state.offset;
216 }
217 }
218
219 if (!EQ (tmp_table, gl_state.old_prop))
220 {
221 gl_state.current_syntax_table = tmp_table;
222 gl_state.old_prop = tmp_table;
223 if (EQ (Fsyntax_table_p (tmp_table), Qt))
224 {
225 gl_state.use_global = 0;
226 }
227 else if (CONSP (tmp_table))
228 {
229 gl_state.use_global = 1;
230 gl_state.global_code = tmp_table;
231 }
232 else
233 {
234 gl_state.use_global = 0;
235 gl_state.current_syntax_table = current_buffer->syntax_table;
236 }
237 }
238
239 while (!NULL_INTERVAL_P (i))
240 {
241 if (cnt && !EQ (tmp_table, textget (i->plist, Qsyntax_table)))
242 {
243 if (count > 0)
244 {
245 gl_state.e_property = i->position - gl_state.offset;
246 gl_state.forward_i = i;
247 }
248 else
249 {
250 gl_state.b_property = i->position + LENGTH (i) - gl_state.offset;
251 gl_state.backward_i = i;
252 }
253 return;
254 }
255 else if (cnt == INTERVALS_AT_ONCE)
256 {
257 if (count > 0)
258 {
259 gl_state.e_property = i->position + LENGTH (i) - gl_state.offset;
260 gl_state.forward_i = i;
261 }
262 else
263 {
264 gl_state.b_property = i->position - gl_state.offset;
265 gl_state.backward_i = i;
266 }
267 return;
268 }
269 cnt++;
270 i = count > 0 ? next_interval (i) : previous_interval (i);
271 }
272 eassert (NULL_INTERVAL_P (i)); /* This property goes to the end. */
273 if (count > 0)
274 gl_state.e_property = gl_state.stop;
275 else
276 gl_state.b_property = gl_state.start;
277 }
278 \f
279 /* Returns TRUE if char at CHARPOS is quoted.
280 Global syntax-table data should be set up already to be good at CHARPOS
281 or after. On return global syntax data is good for lookup at CHARPOS. */
282
283 static int
284 char_quoted (charpos, bytepos)
285 register int charpos, bytepos;
286 {
287 register enum syntaxcode code;
288 register int beg = BEGV;
289 register int quoted = 0;
290 int orig = charpos;
291
292 DEC_BOTH (charpos, bytepos);
293
294 while (bytepos >= beg)
295 {
296 UPDATE_SYNTAX_TABLE_BACKWARD (charpos);
297 code = SYNTAX (FETCH_CHAR_AS_MULTIBYTE (bytepos));
298 if (! (code == Scharquote || code == Sescape))
299 break;
300
301 DEC_BOTH (charpos, bytepos);
302 quoted = !quoted;
303 }
304
305 UPDATE_SYNTAX_TABLE (orig);
306 return quoted;
307 }
308
309 /* Return the bytepos one character after BYTEPOS.
310 We assume that BYTEPOS is not at the end of the buffer. */
311
312 INLINE int
313 inc_bytepos (bytepos)
314 int bytepos;
315 {
316 if (NILP (current_buffer->enable_multibyte_characters))
317 return bytepos + 1;
318
319 INC_POS (bytepos);
320 return bytepos;
321 }
322
323 /* Return the bytepos one character before BYTEPOS.
324 We assume that BYTEPOS is not at the start of the buffer. */
325
326 INLINE int
327 dec_bytepos (bytepos)
328 int bytepos;
329 {
330 if (NILP (current_buffer->enable_multibyte_characters))
331 return bytepos - 1;
332
333 DEC_POS (bytepos);
334 return bytepos;
335 }
336 \f
337 /* Return a defun-start position before before POS and not too far before.
338 It should be the last one before POS, or nearly the last.
339
340 When open_paren_in_column_0_is_defun_start is nonzero,
341 only the beginning of the buffer is treated as a defun-start.
342
343 We record the information about where the scan started
344 and what its result was, so that another call in the same area
345 can return the same value very quickly.
346
347 There is no promise at which position the global syntax data is
348 valid on return from the subroutine, so the caller should explicitly
349 update the global data. */
350
351 static int
352 find_defun_start (pos, pos_byte)
353 int pos, pos_byte;
354 {
355 int opoint = PT, opoint_byte = PT_BYTE;
356
357 if (!open_paren_in_column_0_is_defun_start)
358 {
359 find_start_value_byte = BEGV_BYTE;
360 return BEGV;
361 }
362
363 /* Use previous finding, if it's valid and applies to this inquiry. */
364 if (current_buffer == find_start_buffer
365 /* Reuse the defun-start even if POS is a little farther on.
366 POS might be in the next defun, but that's ok.
367 Our value may not be the best possible, but will still be usable. */
368 && pos <= find_start_pos + 1000
369 && pos >= find_start_value
370 && BEGV == find_start_begv
371 && MODIFF == find_start_modiff)
372 return find_start_value;
373
374 /* Back up to start of line. */
375 scan_newline (pos, pos_byte, BEGV, BEGV_BYTE, -1, 1);
376
377 /* We optimize syntax-table lookup for rare updates. Thus we accept
378 only those `^\s(' which are good in global _and_ text-property
379 syntax-tables. */
380 gl_state.current_syntax_table = current_buffer->syntax_table;
381 gl_state.use_global = 0;
382 while (PT > BEGV)
383 {
384 /* Open-paren at start of line means we may have found our
385 defun-start. */
386 if (SYNTAX (FETCH_CHAR_AS_MULTIBYTE (PT_BYTE)) == Sopen)
387 {
388 SETUP_SYNTAX_TABLE (PT + 1, -1); /* Try again... */
389 if (SYNTAX (FETCH_CHAR_AS_MULTIBYTE (PT_BYTE)) == Sopen)
390 break;
391 /* Now fallback to the default value. */
392 gl_state.current_syntax_table = current_buffer->syntax_table;
393 gl_state.use_global = 0;
394 }
395 /* Move to beg of previous line. */
396 scan_newline (PT, PT_BYTE, BEGV, BEGV_BYTE, -2, 1);
397 }
398
399 /* Record what we found, for the next try. */
400 find_start_value = PT;
401 find_start_value_byte = PT_BYTE;
402 find_start_buffer = current_buffer;
403 find_start_modiff = MODIFF;
404 find_start_begv = BEGV;
405 find_start_pos = pos;
406
407 TEMP_SET_PT_BOTH (opoint, opoint_byte);
408
409 return find_start_value;
410 }
411 \f
412 /* Return the SYNTAX_COMEND_FIRST of the character before POS, POS_BYTE. */
413
414 static int
415 prev_char_comend_first (pos, pos_byte)
416 int pos, pos_byte;
417 {
418 int c, val;
419
420 DEC_BOTH (pos, pos_byte);
421 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
422 c = FETCH_CHAR (pos_byte);
423 val = SYNTAX_COMEND_FIRST (c);
424 UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
425 return val;
426 }
427
428 /* Return the SYNTAX_COMSTART_FIRST of the character before POS, POS_BYTE. */
429
430 /* static int
431 * prev_char_comstart_first (pos, pos_byte)
432 * int pos, pos_byte;
433 * {
434 * int c, val;
435 *
436 * DEC_BOTH (pos, pos_byte);
437 * UPDATE_SYNTAX_TABLE_BACKWARD (pos);
438 * c = FETCH_CHAR (pos_byte);
439 * val = SYNTAX_COMSTART_FIRST (c);
440 * UPDATE_SYNTAX_TABLE_FORWARD (pos + 1);
441 * return val;
442 * } */
443
444 /* Checks whether charpos FROM is at the end of a comment.
445 FROM_BYTE is the bytepos corresponding to FROM.
446 Do not move back before STOP.
447
448 Return a positive value if we find a comment ending at FROM/FROM_BYTE;
449 return -1 otherwise.
450
451 If successful, store the charpos of the comment's beginning
452 into *CHARPOS_PTR, and the bytepos into *BYTEPOS_PTR.
453
454 Global syntax data remains valid for backward search starting at
455 the returned value (or at FROM, if the search was not successful). */
456
457 static int
458 back_comment (from, from_byte, stop, comnested, comstyle, charpos_ptr, bytepos_ptr)
459 int from, from_byte, stop;
460 int comnested, comstyle;
461 int *charpos_ptr, *bytepos_ptr;
462 {
463 /* Look back, counting the parity of string-quotes,
464 and recording the comment-starters seen.
465 When we reach a safe place, assume that's not in a string;
466 then step the main scan to the earliest comment-starter seen
467 an even number of string quotes away from the safe place.
468
469 OFROM[I] is position of the earliest comment-starter seen
470 which is I+2X quotes from the comment-end.
471 PARITY is current parity of quotes from the comment end. */
472 int string_style = -1; /* Presumed outside of any string. */
473 int string_lossage = 0;
474 /* Not a real lossage: indicates that we have passed a matching comment
475 starter plus a non-matching comment-ender, meaning that any matching
476 comment-starter we might see later could be a false positive (hidden
477 inside another comment).
478 Test case: { a (* b } c (* d *) */
479 int comment_lossage = 0;
480 int comment_end = from;
481 int comment_end_byte = from_byte;
482 int comstart_pos = 0;
483 int comstart_byte;
484 /* Place where the containing defun starts,
485 or 0 if we didn't come across it yet. */
486 int defun_start = 0;
487 int defun_start_byte = 0;
488 register enum syntaxcode code;
489 int nesting = 1; /* current comment nesting */
490 int c;
491 int syntax = 0;
492
493 /* FIXME: A }} comment-ender style leads to incorrect behavior
494 in the case of {{ c }}} because we ignore the last two chars which are
495 assumed to be comment-enders although they aren't. */
496
497 /* At beginning of range to scan, we're outside of strings;
498 that determines quote parity to the comment-end. */
499 while (from != stop)
500 {
501 int temp_byte, prev_syntax;
502 int com2start, com2end;
503
504 /* Move back and examine a character. */
505 DEC_BOTH (from, from_byte);
506 UPDATE_SYNTAX_TABLE_BACKWARD (from);
507
508 prev_syntax = syntax;
509 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
510 syntax = SYNTAX_WITH_FLAGS (c);
511 code = SYNTAX (c);
512
513 /* Check for 2-char comment markers. */
514 com2start = (SYNTAX_FLAGS_COMSTART_FIRST (syntax)
515 && SYNTAX_FLAGS_COMSTART_SECOND (prev_syntax)
516 && comstyle == SYNTAX_FLAGS_COMMENT_STYLE (prev_syntax)
517 && (SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax)
518 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested);
519 com2end = (SYNTAX_FLAGS_COMEND_FIRST (syntax)
520 && SYNTAX_FLAGS_COMEND_SECOND (prev_syntax));
521
522 /* Nasty cases with overlapping 2-char comment markers:
523 - snmp-mode: -- c -- foo -- c --
524 --- c --
525 ------ c --
526 - c-mode: *||*
527 |* *|* *|
528 |*| |* |*|
529 /// */
530
531 /* If a 2-char comment sequence partly overlaps with another,
532 we don't try to be clever. */
533 if (from > stop && (com2end || com2start))
534 {
535 int next = from, next_byte = from_byte, next_c, next_syntax;
536 DEC_BOTH (next, next_byte);
537 UPDATE_SYNTAX_TABLE_BACKWARD (next);
538 next_c = FETCH_CHAR_AS_MULTIBYTE (next_byte);
539 next_syntax = SYNTAX_WITH_FLAGS (next_c);
540 if (((com2start || comnested)
541 && SYNTAX_FLAGS_COMEND_SECOND (syntax)
542 && SYNTAX_FLAGS_COMEND_FIRST (next_syntax))
543 || ((com2end || comnested)
544 && SYNTAX_FLAGS_COMSTART_SECOND (syntax)
545 && comstyle == SYNTAX_FLAGS_COMMENT_STYLE (syntax)
546 && SYNTAX_FLAGS_COMSTART_FIRST (next_syntax)))
547 goto lossage;
548 /* UPDATE_SYNTAX_TABLE_FORWARD (next + 1); */
549 }
550
551 if (com2start && comstart_pos == 0)
552 /* We're looking at a comment starter. But it might be a comment
553 ender as well (see snmp-mode). The first time we see one, we
554 need to consider it as a comment starter,
555 and the subsequent times as a comment ender. */
556 com2end = 0;
557
558 /* Turn a 2-char comment sequences into the appropriate syntax. */
559 if (com2end)
560 code = Sendcomment;
561 else if (com2start)
562 code = Scomment;
563 /* Ignore comment starters of a different style. */
564 else if (code == Scomment
565 && (comstyle != SYNTAX_FLAGS_COMMENT_STYLE (syntax)
566 || SYNTAX_FLAGS_COMMENT_NESTED (syntax) != comnested))
567 continue;
568
569 /* Ignore escaped characters, except comment-enders. */
570 if (code != Sendcomment && char_quoted (from, from_byte))
571 continue;
572
573 switch (code)
574 {
575 case Sstring_fence:
576 case Scomment_fence:
577 c = (code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE);
578 case Sstring:
579 /* Track parity of quotes. */
580 if (string_style == -1)
581 /* Entering a string. */
582 string_style = c;
583 else if (string_style == c)
584 /* Leaving the string. */
585 string_style = -1;
586 else
587 /* If we have two kinds of string delimiters.
588 There's no way to grok this scanning backwards. */
589 string_lossage = 1;
590 break;
591
592 case Scomment:
593 /* We've already checked that it is the relevant comstyle. */
594 if (string_style != -1 || comment_lossage || string_lossage)
595 /* There are odd string quotes involved, so let's be careful.
596 Test case in Pascal: " { " a { " } */
597 goto lossage;
598
599 if (!comnested)
600 {
601 /* Record best comment-starter so far. */
602 comstart_pos = from;
603 comstart_byte = from_byte;
604 }
605 else if (--nesting <= 0)
606 /* nested comments have to be balanced, so we don't need to
607 keep looking for earlier ones. We use here the same (slightly
608 incorrect) reasoning as below: since it is followed by uniform
609 paired string quotes, this comment-start has to be outside of
610 strings, else the comment-end itself would be inside a string. */
611 goto done;
612 break;
613
614 case Sendcomment:
615 if (SYNTAX_FLAGS_COMMENT_STYLE (syntax) == comstyle
616 && ((com2end && SYNTAX_FLAGS_COMMENT_NESTED (prev_syntax))
617 || SYNTAX_FLAGS_COMMENT_NESTED (syntax)) == comnested)
618 /* This is the same style of comment ender as ours. */
619 {
620 if (comnested)
621 nesting++;
622 else
623 /* Anything before that can't count because it would match
624 this comment-ender rather than ours. */
625 from = stop; /* Break out of the loop. */
626 }
627 else if (comstart_pos != 0 || c != '\n')
628 /* We're mixing comment styles here, so we'd better be careful.
629 The (comstart_pos != 0 || c != '\n') check is not quite correct
630 (we should just always set comment_lossage), but removing it
631 would imply that any multiline comment in C would go through
632 lossage, which seems overkill.
633 The failure should only happen in the rare cases such as
634 { (* } *) */
635 comment_lossage = 1;
636 break;
637
638 case Sopen:
639 /* Assume a defun-start point is outside of strings. */
640 if (open_paren_in_column_0_is_defun_start
641 && (from == stop
642 || (temp_byte = dec_bytepos (from_byte),
643 FETCH_CHAR (temp_byte) == '\n')))
644 {
645 defun_start = from;
646 defun_start_byte = from_byte;
647 from = stop; /* Break out of the loop. */
648 }
649 break;
650
651 default:
652 break;
653 }
654 }
655
656 if (comstart_pos == 0)
657 {
658 from = comment_end;
659 from_byte = comment_end_byte;
660 UPDATE_SYNTAX_TABLE_FORWARD (comment_end - 1);
661 }
662 /* If comstart_pos is set and we get here (ie. didn't jump to `lossage'
663 or `done'), then we've found the beginning of the non-nested comment. */
664 else if (1) /* !comnested */
665 {
666 from = comstart_pos;
667 from_byte = comstart_byte;
668 /* Globals are correct now. */
669 }
670 else
671 {
672 struct lisp_parse_state state;
673 lossage:
674 /* We had two kinds of string delimiters mixed up
675 together. Decode this going forwards.
676 Scan fwd from a known safe place (beginning-of-defun)
677 to the one in question; this records where we
678 last passed a comment starter. */
679 /* If we did not already find the defun start, find it now. */
680 if (defun_start == 0)
681 {
682 defun_start = find_defun_start (comment_end, comment_end_byte);
683 defun_start_byte = find_start_value_byte;
684 }
685 do
686 {
687 scan_sexps_forward (&state,
688 defun_start, defun_start_byte,
689 comment_end, -10000, 0, Qnil, 0);
690 defun_start = comment_end;
691 if (state.incomment == (comnested ? 1 : -1)
692 && state.comstyle == comstyle)
693 from = state.comstr_start;
694 else
695 {
696 from = comment_end;
697 if (state.incomment)
698 /* If comment_end is inside some other comment, maybe ours
699 is nested, so we need to try again from within the
700 surrounding comment. Example: { a (* " *) */
701 {
702 /* FIXME: We should advance by one or two chars. */
703 defun_start = state.comstr_start + 2;
704 defun_start_byte = CHAR_TO_BYTE (defun_start);
705 }
706 }
707 } while (defun_start < comment_end);
708
709 from_byte = CHAR_TO_BYTE (from);
710 UPDATE_SYNTAX_TABLE_FORWARD (from - 1);
711 }
712
713 done:
714 *charpos_ptr = from;
715 *bytepos_ptr = from_byte;
716
717 return (from == comment_end) ? -1 : from;
718 }
719 \f
720 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
721 doc: /* Return t if OBJECT is a syntax table.
722 Currently, any char-table counts as a syntax table. */)
723 (object)
724 Lisp_Object object;
725 {
726 if (CHAR_TABLE_P (object)
727 && EQ (XCHAR_TABLE (object)->purpose, Qsyntax_table))
728 return Qt;
729 return Qnil;
730 }
731
732 static void
733 check_syntax_table (obj)
734 Lisp_Object obj;
735 {
736 if (!(CHAR_TABLE_P (obj)
737 && EQ (XCHAR_TABLE (obj)->purpose, Qsyntax_table)))
738 wrong_type_argument (Qsyntax_table_p, obj);
739 }
740
741 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
742 doc: /* Return the current syntax table.
743 This is the one specified by the current buffer. */)
744 ()
745 {
746 return current_buffer->syntax_table;
747 }
748
749 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
750 Sstandard_syntax_table, 0, 0, 0,
751 doc: /* Return the standard syntax table.
752 This is the one used for new buffers. */)
753 ()
754 {
755 return Vstandard_syntax_table;
756 }
757
758 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
759 doc: /* Construct a new syntax table and return it.
760 It is a copy of the TABLE, which defaults to the standard syntax table. */)
761 (table)
762 Lisp_Object table;
763 {
764 Lisp_Object copy;
765
766 if (!NILP (table))
767 check_syntax_table (table);
768 else
769 table = Vstandard_syntax_table;
770
771 copy = Fcopy_sequence (table);
772
773 /* Only the standard syntax table should have a default element.
774 Other syntax tables should inherit from parents instead. */
775 XCHAR_TABLE (copy)->defalt = Qnil;
776
777 /* Copied syntax tables should all have parents.
778 If we copied one with no parent, such as the standard syntax table,
779 use the standard syntax table as the copy's parent. */
780 if (NILP (XCHAR_TABLE (copy)->parent))
781 Fset_char_table_parent (copy, Vstandard_syntax_table);
782 return copy;
783 }
784
785 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
786 doc: /* Select a new syntax table for the current buffer.
787 One argument, a syntax table. */)
788 (table)
789 Lisp_Object table;
790 {
791 int idx;
792 check_syntax_table (table);
793 current_buffer->syntax_table = table;
794 /* Indicate that this buffer now has a specified syntax table. */
795 idx = PER_BUFFER_VAR_IDX (syntax_table);
796 SET_PER_BUFFER_VALUE_P (current_buffer, idx, 1);
797 return table;
798 }
799 \f
800 /* Convert a letter which signifies a syntax code
801 into the code it signifies.
802 This is used by modify-syntax-entry, and other things. */
803
804 unsigned char syntax_spec_code[0400] =
805 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
806 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
807 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
808 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
809 (char) Swhitespace, (char) Scomment_fence, (char) Sstring, 0377,
810 (char) Smath, 0377, 0377, (char) Squote,
811 (char) Sopen, (char) Sclose, 0377, 0377,
812 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
813 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
814 0377, 0377, 0377, 0377,
815 (char) Scomment, 0377, (char) Sendcomment, 0377,
816 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
817 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
818 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
819 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
820 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
821 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
822 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
823 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377
824 };
825
826 /* Indexed by syntax code, give the letter that describes it. */
827
828 char syntax_code_spec[16] =
829 {
830 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@',
831 '!', '|'
832 };
833
834 /* Indexed by syntax code, give the object (cons of syntax code and
835 nil) to be stored in syntax table. Since these objects can be
836 shared among syntax tables, we generate them in advance. By
837 sharing objects, the function `describe-syntax' can give a more
838 compact listing. */
839 static Lisp_Object Vsyntax_code_object;
840
841 \f
842 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
843 doc: /* Return the syntax code of CHARACTER, described by a character.
844 For example, if CHARACTER is a word constituent,
845 the character `w' is returned.
846 The characters that correspond to various syntax codes
847 are listed in the documentation of `modify-syntax-entry'. */)
848 (character)
849 Lisp_Object character;
850 {
851 int char_int;
852 gl_state.current_syntax_table = current_buffer->syntax_table;
853
854 gl_state.use_global = 0;
855 CHECK_NUMBER (character);
856 char_int = XINT (character);
857 return make_number (syntax_code_spec[(int) SYNTAX (char_int)]);
858 }
859
860 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
861 doc: /* Return the matching parenthesis of CHARACTER, or nil if none. */)
862 (character)
863 Lisp_Object character;
864 {
865 int char_int, code;
866 gl_state.current_syntax_table = current_buffer->syntax_table;
867 gl_state.use_global = 0;
868 CHECK_NUMBER (character);
869 char_int = XINT (character);
870 code = SYNTAX (char_int);
871 if (code == Sopen || code == Sclose)
872 return SYNTAX_MATCH (char_int);
873 return Qnil;
874 }
875
876 DEFUN ("string-to-syntax", Fstring_to_syntax, Sstring_to_syntax, 1, 1, 0,
877 doc: /* Convert a syntax specification STRING into syntax cell form.
878 STRING should be a string as it is allowed as argument of
879 `modify-syntax-entry'. Value is the equivalent cons cell
880 (CODE . MATCHING-CHAR) that can be used as value of a `syntax-table'
881 text property. */)
882 (string)
883 Lisp_Object string;
884 {
885 register const unsigned char *p;
886 register enum syntaxcode code;
887 int val;
888 Lisp_Object match;
889
890 CHECK_STRING (string);
891
892 p = SDATA (string);
893 code = (enum syntaxcode) syntax_spec_code[*p++];
894 if (((int) code & 0377) == 0377)
895 error ("invalid syntax description letter: %c", p[-1]);
896
897 if (code == Sinherit)
898 return Qnil;
899
900 if (*p)
901 {
902 int len;
903 int character = (STRING_CHAR_AND_LENGTH
904 (p, SBYTES (string) - 1, len));
905 XSETINT (match, character);
906 if (XFASTINT (match) == ' ')
907 match = Qnil;
908 p += len;
909 }
910 else
911 match = Qnil;
912
913 val = (int) code;
914 while (*p)
915 switch (*p++)
916 {
917 case '1':
918 val |= 1 << 16;
919 break;
920
921 case '2':
922 val |= 1 << 17;
923 break;
924
925 case '3':
926 val |= 1 << 18;
927 break;
928
929 case '4':
930 val |= 1 << 19;
931 break;
932
933 case 'p':
934 val |= 1 << 20;
935 break;
936
937 case 'b':
938 val |= 1 << 21;
939 break;
940
941 case 'n':
942 val |= 1 << 22;
943 break;
944 }
945
946 if (val < XVECTOR (Vsyntax_code_object)->size && NILP (match))
947 return XVECTOR (Vsyntax_code_object)->contents[val];
948 else
949 /* Since we can't use a shared object, let's make a new one. */
950 return Fcons (make_number (val), match);
951 }
952
953 /* I really don't know why this is interactive
954 help-form should at least be made useful whilst reading the second arg. */
955 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
956 "cSet syntax for character: \nsSet syntax for %s to: ",
957 doc: /* Set syntax for character CHAR according to string NEWENTRY.
958 The syntax is changed only for table SYNTAX_TABLE, which defaults to
959 the current buffer's syntax table.
960 CHAR may be a cons (MIN . MAX), in which case, syntaxes of all characters
961 in the range MIN and MAX are changed.
962 The first character of NEWENTRY should be one of the following:
963 Space or - whitespace syntax. w word constituent.
964 _ symbol constituent. . punctuation.
965 ( open-parenthesis. ) close-parenthesis.
966 " string quote. \\ escape.
967 $ paired delimiter. ' expression quote or prefix operator.
968 < comment starter. > comment ender.
969 / character-quote. @ inherit from `standard-syntax-table'.
970 | generic string fence. ! generic comment fence.
971
972 Only single-character comment start and end sequences are represented thus.
973 Two-character sequences are represented as described below.
974 The second character of NEWENTRY is the matching parenthesis,
975 used only if the first character is `(' or `)'.
976 Any additional characters are flags.
977 Defined flags are the characters 1, 2, 3, 4, b, p, and n.
978 1 means CHAR is the start of a two-char comment start sequence.
979 2 means CHAR is the second character of such a sequence.
980 3 means CHAR is the start of a two-char comment end sequence.
981 4 means CHAR is the second character of such a sequence.
982
983 There can be up to two orthogonal comment sequences. This is to support
984 language modes such as C++. By default, all comment sequences are of style
985 a, but you can set the comment sequence style to b (on the second character
986 of a comment-start, or the first character of a comment-end sequence) using
987 this flag:
988 b means CHAR is part of comment sequence b.
989 n means CHAR is part of a nestable comment sequence.
990
991 p means CHAR is a prefix character for `backward-prefix-chars';
992 such characters are treated as whitespace when they occur
993 between expressions.
994 usage: (modify-syntax-entry CHAR NEWENTRY &optional SYNTAX-TABLE) */)
995 (c, newentry, syntax_table)
996 Lisp_Object c, newentry, syntax_table;
997 {
998 if (CONSP (c))
999 {
1000 CHECK_CHARACTER_CAR (c);
1001 CHECK_CHARACTER_CDR (c);
1002 }
1003 else
1004 CHECK_CHARACTER (c);
1005
1006 if (NILP (syntax_table))
1007 syntax_table = current_buffer->syntax_table;
1008 else
1009 check_syntax_table (syntax_table);
1010
1011 newentry = Fstring_to_syntax (newentry);
1012 if (CONSP (c))
1013 SET_RAW_SYNTAX_ENTRY_RANGE (syntax_table, c, newentry);
1014 else
1015 SET_RAW_SYNTAX_ENTRY (syntax_table, XINT (c), newentry);
1016 return Qnil;
1017 }
1018 \f
1019 /* Dump syntax table to buffer in human-readable format */
1020
1021 DEFUN ("internal-describe-syntax-value", Finternal_describe_syntax_value,
1022 Sinternal_describe_syntax_value, 1, 1, 0,
1023 doc: /* Insert a description of the internal syntax description SYNTAX at point. */)
1024 (syntax)
1025 Lisp_Object syntax;
1026 {
1027 register enum syntaxcode code;
1028 char desc, start1, start2, end1, end2, prefix, comstyle, comnested;
1029 char str[2];
1030 Lisp_Object first, match_lisp, value = syntax;
1031
1032 if (NILP (value))
1033 {
1034 insert_string ("default");
1035 return syntax;
1036 }
1037
1038 if (CHAR_TABLE_P (value))
1039 {
1040 insert_string ("deeper char-table ...");
1041 return syntax;
1042 }
1043
1044 if (!CONSP (value))
1045 {
1046 insert_string ("invalid");
1047 return syntax;
1048 }
1049
1050 first = XCAR (value);
1051 match_lisp = XCDR (value);
1052
1053 if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp)))
1054 {
1055 insert_string ("invalid");
1056 return syntax;
1057 }
1058
1059 code = (enum syntaxcode) (XINT (first) & 0377);
1060 start1 = (XINT (first) >> 16) & 1;
1061 start2 = (XINT (first) >> 17) & 1;
1062 end1 = (XINT (first) >> 18) & 1;
1063 end2 = (XINT (first) >> 19) & 1;
1064 prefix = (XINT (first) >> 20) & 1;
1065 comstyle = (XINT (first) >> 21) & 1;
1066 comnested = (XINT (first) >> 22) & 1;
1067
1068 if ((int) code < 0 || (int) code >= (int) Smax)
1069 {
1070 insert_string ("invalid");
1071 return syntax;
1072 }
1073 desc = syntax_code_spec[(int) code];
1074
1075 str[0] = desc, str[1] = 0;
1076 insert (str, 1);
1077
1078 if (NILP (match_lisp))
1079 insert (" ", 1);
1080 else
1081 insert_char (XINT (match_lisp));
1082
1083 if (start1)
1084 insert ("1", 1);
1085 if (start2)
1086 insert ("2", 1);
1087
1088 if (end1)
1089 insert ("3", 1);
1090 if (end2)
1091 insert ("4", 1);
1092
1093 if (prefix)
1094 insert ("p", 1);
1095 if (comstyle)
1096 insert ("b", 1);
1097 if (comnested)
1098 insert ("n", 1);
1099
1100 insert_string ("\twhich means: ");
1101
1102 switch (SWITCH_ENUM_CAST (code))
1103 {
1104 case Swhitespace:
1105 insert_string ("whitespace"); break;
1106 case Spunct:
1107 insert_string ("punctuation"); break;
1108 case Sword:
1109 insert_string ("word"); break;
1110 case Ssymbol:
1111 insert_string ("symbol"); break;
1112 case Sopen:
1113 insert_string ("open"); break;
1114 case Sclose:
1115 insert_string ("close"); break;
1116 case Squote:
1117 insert_string ("prefix"); break;
1118 case Sstring:
1119 insert_string ("string"); break;
1120 case Smath:
1121 insert_string ("math"); break;
1122 case Sescape:
1123 insert_string ("escape"); break;
1124 case Scharquote:
1125 insert_string ("charquote"); break;
1126 case Scomment:
1127 insert_string ("comment"); break;
1128 case Sendcomment:
1129 insert_string ("endcomment"); break;
1130 case Sinherit:
1131 insert_string ("inherit"); break;
1132 case Scomment_fence:
1133 insert_string ("comment fence"); break;
1134 case Sstring_fence:
1135 insert_string ("string fence"); break;
1136 default:
1137 insert_string ("invalid");
1138 return syntax;
1139 }
1140
1141 if (!NILP (match_lisp))
1142 {
1143 insert_string (", matches ");
1144 insert_char (XINT (match_lisp));
1145 }
1146
1147 if (start1)
1148 insert_string (",\n\t is the first character of a comment-start sequence");
1149 if (start2)
1150 insert_string (",\n\t is the second character of a comment-start sequence");
1151
1152 if (end1)
1153 insert_string (",\n\t is the first character of a comment-end sequence");
1154 if (end2)
1155 insert_string (",\n\t is the second character of a comment-end sequence");
1156 if (comstyle)
1157 insert_string (" (comment style b)");
1158 if (comnested)
1159 insert_string (" (nestable)");
1160
1161 if (prefix)
1162 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
1163
1164 return syntax;
1165 }
1166 \f
1167 int parse_sexp_ignore_comments;
1168
1169 /* Char-table of functions that find the next or previous word
1170 boundary. */
1171 Lisp_Object Vfind_word_boundary_function_table;
1172
1173 /* Return the position across COUNT words from FROM.
1174 If that many words cannot be found before the end of the buffer, return 0.
1175 COUNT negative means scan backward and stop at word beginning. */
1176
1177 int
1178 scan_words (from, count)
1179 register int from, count;
1180 {
1181 register int beg = BEGV;
1182 register int end = ZV;
1183 register int from_byte = CHAR_TO_BYTE (from);
1184 register enum syntaxcode code;
1185 int ch0, ch1;
1186 Lisp_Object func, script, pos;
1187
1188 immediate_quit = 1;
1189 QUIT;
1190
1191 SETUP_SYNTAX_TABLE (from, count);
1192
1193 while (count > 0)
1194 {
1195 while (1)
1196 {
1197 if (from == end)
1198 {
1199 immediate_quit = 0;
1200 return 0;
1201 }
1202 UPDATE_SYNTAX_TABLE_FORWARD (from);
1203 ch0 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1204 code = SYNTAX (ch0);
1205 INC_BOTH (from, from_byte);
1206 if (words_include_escapes
1207 && (code == Sescape || code == Scharquote))
1208 break;
1209 if (code == Sword)
1210 break;
1211 }
1212 /* Now CH0 is a character which begins a word and FROM is the
1213 position of the next character. */
1214 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch0);
1215 if (! NILP (Ffboundp (func)))
1216 {
1217 pos = call2 (func, make_number (from - 1), make_number (end));
1218 if (INTEGERP (pos) && XINT (pos) > from)
1219 {
1220 from = XINT (pos);
1221 from_byte = CHAR_TO_BYTE (from);
1222 }
1223 }
1224 else
1225 {
1226 script = CHAR_TABLE_REF (Vchar_script_table, ch0);
1227 while (1)
1228 {
1229 if (from == end) break;
1230 UPDATE_SYNTAX_TABLE_FORWARD (from);
1231 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1232 code = SYNTAX (ch1);
1233 if ((code != Sword
1234 && (! words_include_escapes
1235 || (code != Sescape && code != Scharquote)))
1236 || ! EQ (CHAR_TABLE_REF (Vchar_script_table, ch1), script))
1237 break;
1238 INC_BOTH (from, from_byte);
1239 ch0 = ch1;
1240 }
1241 }
1242 count--;
1243 }
1244 while (count < 0)
1245 {
1246 while (1)
1247 {
1248 if (from == beg)
1249 {
1250 immediate_quit = 0;
1251 return 0;
1252 }
1253 DEC_BOTH (from, from_byte);
1254 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1255 ch1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1256 code = SYNTAX (ch1);
1257 if (words_include_escapes
1258 && (code == Sescape || code == Scharquote))
1259 break;
1260 if (code == Sword)
1261 break;
1262 }
1263 /* Now CH1 is a character which ends a word and FROM is the
1264 position of it. */
1265 func = CHAR_TABLE_REF (Vfind_word_boundary_function_table, ch1);
1266 if (! NILP (Ffboundp (func)))
1267 {
1268 pos = call2 (func, make_number (from), make_number (beg));
1269 if (INTEGERP (pos) && XINT (pos) < from)
1270 {
1271 from = XINT (pos);
1272 from_byte = CHAR_TO_BYTE (from);
1273 }
1274 }
1275 else
1276 {
1277 script = CHAR_TABLE_REF (Vchar_script_table, ch1);
1278 while (1)
1279 {
1280 int temp_byte;
1281
1282 if (from == beg)
1283 break;
1284 temp_byte = dec_bytepos (from_byte);
1285 UPDATE_SYNTAX_TABLE_BACKWARD (from);
1286 ch0 = FETCH_CHAR_AS_MULTIBYTE (temp_byte);
1287 code = SYNTAX (ch0);
1288 if ((code != Sword
1289 && (! words_include_escapes
1290 || (code != Sescape && code != Scharquote)))
1291 || ! EQ (CHAR_TABLE_REF (Vchar_script_table, ch0), script))
1292 break;
1293 DEC_BOTH (from, from_byte);
1294 ch1 = ch0;
1295 }
1296 }
1297 count++;
1298 }
1299
1300 immediate_quit = 0;
1301
1302 return from;
1303 }
1304
1305 DEFUN ("forward-word", Fforward_word, Sforward_word, 1, 1, "p",
1306 doc: /* Move point forward ARG words (backward if ARG is negative).
1307 Normally returns t.
1308 If an edge of the buffer or a field boundary is reached, point is left there
1309 and the function returns nil. Field boundaries are not noticed if
1310 `inhibit-field-text-motion' is non-nil. */)
1311 (count)
1312 Lisp_Object count;
1313 {
1314 int orig_val, val;
1315 CHECK_NUMBER (count);
1316
1317 val = orig_val = scan_words (PT, XINT (count));
1318 if (! orig_val)
1319 val = XINT (count) > 0 ? ZV : BEGV;
1320
1321 /* Avoid jumping out of an input field. */
1322 val = XFASTINT (Fconstrain_to_field (make_number (val), make_number (PT),
1323 Qt, Qnil, Qnil));
1324
1325 SET_PT (val);
1326 return val == orig_val ? Qt : Qnil;
1327 }
1328 \f
1329 Lisp_Object skip_chars ();
1330
1331 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0,
1332 doc: /* Move point forward, stopping before a char not in STRING, or at pos LIM.
1333 STRING is like the inside of a `[...]' in a regular expression
1334 except that `]' is never special and `\\' quotes `^', `-' or `\\'
1335 (but not as the end of a range; quoting is never needed there).
1336 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
1337 With arg "^a-zA-Z", skips nonletters stopping before first letter.
1338 Returns the distance traveled, either zero or positive.
1339 Note that char classes, e.g. `[:alpha:]', are not currently supported;
1340 they will be treated as literals. */)
1341 (string, lim)
1342 Lisp_Object string, lim;
1343 {
1344 return skip_chars (1, string, lim);
1345 }
1346
1347 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0,
1348 doc: /* Move point backward, stopping after a char not in STRING, or at pos LIM.
1349 See `skip-chars-forward' for details.
1350 Returns the distance traveled, either zero or negative. */)
1351 (string, lim)
1352 Lisp_Object string, lim;
1353 {
1354 return skip_chars (0, string, lim);
1355 }
1356
1357 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0,
1358 doc: /* Move point forward across chars in specified syntax classes.
1359 SYNTAX is a string of syntax code characters.
1360 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
1361 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1362 This function returns the distance traveled, either zero or positive. */)
1363 (syntax, lim)
1364 Lisp_Object syntax, lim;
1365 {
1366 return skip_syntaxes (1, syntax, lim);
1367 }
1368
1369 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0,
1370 doc: /* Move point backward across chars in specified syntax classes.
1371 SYNTAX is a string of syntax code characters.
1372 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
1373 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
1374 This function returns the distance traveled, either zero or negative. */)
1375 (syntax, lim)
1376 Lisp_Object syntax, lim;
1377 {
1378 return skip_syntaxes (0, syntax, lim);
1379 }
1380
1381 static Lisp_Object
1382 skip_chars (forwardp, string, lim)
1383 int forwardp;
1384 Lisp_Object string, lim;
1385 {
1386 register unsigned int c;
1387 unsigned char fastmap[0400];
1388 /* Store the ranges of non-ASCII characters. */
1389 int *char_ranges;
1390 int n_char_ranges = 0;
1391 int negate = 0;
1392 register int i, i_byte;
1393 /* Set to 1 if the current buffer is multibyte and the region
1394 contains non-ASCII chars. */
1395 int multibyte;
1396 /* Set to 1 if STRING is multibyte and it contains non-ASCII
1397 chars. */
1398 int string_multibyte;
1399 int size_byte;
1400 const unsigned char *str;
1401 int len;
1402
1403 CHECK_STRING (string);
1404
1405 if (NILP (lim))
1406 XSETINT (lim, forwardp ? ZV : BEGV);
1407 else
1408 CHECK_NUMBER_COERCE_MARKER (lim);
1409
1410 /* In any case, don't allow scan outside bounds of buffer. */
1411 if (XINT (lim) > ZV)
1412 XSETFASTINT (lim, ZV);
1413 if (XINT (lim) < BEGV)
1414 XSETFASTINT (lim, BEGV);
1415
1416 multibyte = (!NILP (current_buffer->enable_multibyte_characters)
1417 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1418 string_multibyte = SBYTES (string) > SCHARS (string);
1419
1420 bzero (fastmap, sizeof fastmap);
1421 if (multibyte)
1422 char_ranges = (int *) alloca (SCHARS (string) * (sizeof (int)) * 2);
1423
1424 str = SDATA (string);
1425 size_byte = SBYTES (string);
1426
1427 i_byte = 0;
1428 if (i_byte < size_byte
1429 && SREF (string, 0) == '^')
1430 {
1431 negate = 1; i_byte++;
1432 }
1433
1434 /* Find the characters specified and set their elements of fastmap.
1435 Handle backslashes and ranges specially.
1436
1437 If STRING contains non-ASCII characters, setup char_ranges for
1438 them and use fastmap only for their leading codes. */
1439
1440 if (! string_multibyte)
1441 {
1442 int string_has_eight_bit = 0;
1443
1444 /* At first setup fastmap. */
1445 while (i_byte < size_byte)
1446 {
1447 c = str[i_byte++];
1448
1449 if (c == '\\')
1450 {
1451 if (i_byte == size_byte)
1452 break;
1453
1454 c = str[i_byte++];
1455 }
1456 if (i_byte < size_byte
1457 && str[i_byte] == '-')
1458 {
1459 unsigned int c2;
1460
1461 /* Skip over the dash. */
1462 i_byte++;
1463
1464 if (i_byte == size_byte)
1465 break;
1466
1467 /* Get the end of the range. */
1468 c2 = str[i_byte++];
1469 if (c2 == '\\'
1470 && i_byte < size_byte)
1471 c2 = str[i_byte++];
1472
1473 if (c <= c2)
1474 {
1475 while (c <= c2)
1476 fastmap[c++] = 1;
1477 if (! ASCII_CHAR_P (c2))
1478 string_has_eight_bit = 1;
1479 }
1480 }
1481 else
1482 {
1483 fastmap[c] = 1;
1484 if (! ASCII_CHAR_P (c))
1485 string_has_eight_bit = 1;
1486 }
1487 }
1488
1489 /* If the current range is multibyte and STRING contains
1490 eight-bit chars, arrange fastmap and setup char_ranges for
1491 the corresponding multibyte chars. */
1492 if (multibyte && string_has_eight_bit)
1493 {
1494 unsigned char fastmap2[0400];
1495 int range_start_byte, range_start_char;
1496
1497 bcopy (fastmap2 + 0200, fastmap + 0200, 0200);
1498 bzero (fastmap + 0200, 0200);
1499 /* We are sure that this loop stops. */
1500 for (i = 0200; ! fastmap2[i]; i++);
1501 c = unibyte_char_to_multibyte (i);
1502 fastmap[CHAR_LEADING_CODE (c)] = 1;
1503 range_start_byte = i;
1504 range_start_char = c;
1505 for (i = 129; i < 0400; i++)
1506 {
1507 c = unibyte_char_to_multibyte (i);
1508 fastmap[CHAR_LEADING_CODE (c)] = 1;
1509 if (i - range_start_byte != c - range_start_char)
1510 {
1511 char_ranges[n_char_ranges++] = range_start_char;
1512 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1513 + range_start_char);
1514 range_start_byte = i;
1515 range_start_char = c;
1516 }
1517 }
1518 char_ranges[n_char_ranges++] = range_start_char;
1519 char_ranges[n_char_ranges++] = ((i - 1 - range_start_byte)
1520 + range_start_char);
1521 }
1522 }
1523 else
1524 {
1525 while (i_byte < size_byte)
1526 {
1527 unsigned char leading_code;
1528
1529 leading_code = str[i_byte];
1530 c = STRING_CHAR_AND_LENGTH (str + i_byte, size_byte-i_byte, len);
1531 i_byte += len;
1532
1533 if (c == '\\')
1534 {
1535 if (i_byte == size_byte)
1536 break;
1537
1538 leading_code = str[i_byte];
1539 c = STRING_CHAR_AND_LENGTH (str+i_byte, size_byte-i_byte, len);
1540 i_byte += len;
1541 }
1542 if (i_byte < size_byte
1543 && str[i_byte] == '-')
1544 {
1545 unsigned int c2;
1546 unsigned char leading_code2;
1547
1548 /* Skip over the dash. */
1549 i_byte++;
1550
1551 if (i_byte == size_byte)
1552 break;
1553
1554 /* Get the end of the range. */
1555 leading_code2 = str[i_byte];
1556 c2 =STRING_CHAR_AND_LENGTH (str + i_byte, size_byte-i_byte, len);
1557 i_byte += len;
1558
1559 if (c2 == '\\'
1560 && i_byte < size_byte)
1561 {
1562 leading_code2 = str[i_byte];
1563 c2 =STRING_CHAR_AND_LENGTH (str + i_byte, size_byte-i_byte, len);
1564 i_byte += len;
1565 }
1566
1567 if (ASCII_CHAR_P (c))
1568 {
1569 while (c <= c2 && c < 0x80)
1570 fastmap[c++] = 1;
1571 leading_code = CHAR_LEADING_CODE (c);
1572 }
1573 if (! ASCII_CHAR_P (c))
1574 {
1575 while (leading_code <= leading_code2)
1576 fastmap[leading_code++] = 1;
1577 if (c <= c2)
1578 {
1579 char_ranges[n_char_ranges++] = c;
1580 char_ranges[n_char_ranges++] = c2;
1581 }
1582 }
1583 }
1584 else
1585 {
1586 if (ASCII_CHAR_P (c))
1587 fastmap[c] = 1;
1588 else
1589 {
1590 fastmap[leading_code] = 1;
1591 char_ranges[n_char_ranges++] = c;
1592 char_ranges[n_char_ranges++] = c;
1593 }
1594 }
1595 }
1596
1597 /* If the current range is unibyte and STRING contains non-ASCII
1598 chars, arrange fastmap for the corresponding unibyte
1599 chars. */
1600
1601 if (! multibyte && n_char_ranges > 0)
1602 {
1603 bzero (fastmap + 0200, 0200);
1604 for (i = 0; i < n_char_ranges; i += 2)
1605 {
1606 int c1 = char_ranges[i];
1607 int c2 = char_ranges[i + 1];
1608
1609 for (; c1 <= c2; c1++)
1610 fastmap[CHAR_TO_BYTE8 (c1)] = 1;
1611 }
1612 }
1613 }
1614
1615 /* If ^ was the first character, complement the fastmap. */
1616 if (negate)
1617 {
1618 if (! multibyte)
1619 for (i = 0; i < sizeof fastmap; i++)
1620 fastmap[i] ^= 1;
1621 else
1622 {
1623 for (i = 0; i < 0200; i++)
1624 fastmap[i] ^= 1;
1625 /* All non-ASCII chars possibly match. */
1626 for (; i < sizeof fastmap; i++)
1627 fastmap[i] = 1;
1628 }
1629 }
1630
1631 {
1632 int start_point = PT;
1633 int pos = PT;
1634 int pos_byte = PT_BYTE;
1635 unsigned char *p = PT_ADDR, *endp, *stop;
1636
1637 if (forwardp)
1638 {
1639 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1640 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1641 }
1642 else
1643 {
1644 endp = CHAR_POS_ADDR (XINT (lim));
1645 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1646 }
1647
1648 immediate_quit = 1;
1649 if (forwardp)
1650 {
1651 if (multibyte)
1652 while (1)
1653 {
1654 int nbytes;
1655
1656 if (p >= stop)
1657 {
1658 if (p >= endp)
1659 break;
1660 p = GAP_END_ADDR;
1661 stop = endp;
1662 }
1663 if (! fastmap[*p])
1664 break;
1665 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, nbytes);
1666 if (! ASCII_CHAR_P (c))
1667 {
1668 /* As we are looking at a multibyte character, we
1669 must look up the character in the table
1670 CHAR_RANGES. If there's no data in the table,
1671 that character is not what we want to skip. */
1672
1673 /* The following code do the right thing even if
1674 n_char_ranges is zero (i.e. no data in
1675 CHAR_RANGES). */
1676 for (i = 0; i < n_char_ranges; i += 2)
1677 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1678 break;
1679 if (!(negate ^ (i < n_char_ranges)))
1680 break;
1681 }
1682 p += nbytes, pos++, pos_byte += nbytes;
1683 }
1684 else
1685 while (1)
1686 {
1687 if (p >= stop)
1688 {
1689 if (p >= endp)
1690 break;
1691 p = GAP_END_ADDR;
1692 stop = endp;
1693 }
1694 if (!fastmap[*p])
1695 break;
1696 p++, pos++, pos_byte++;
1697 }
1698 }
1699 else
1700 {
1701 if (multibyte)
1702 while (1)
1703 {
1704 unsigned char *prev_p;
1705
1706 if (p <= stop)
1707 {
1708 if (p <= endp)
1709 break;
1710 p = GPT_ADDR;
1711 stop = endp;
1712 }
1713 prev_p = p;
1714 while (--p >= stop && ! CHAR_HEAD_P (*p));
1715 if (! fastmap[*p])
1716 break;
1717 c = STRING_CHAR (p, MAX_MULTIBYTE_LENGTH);
1718 if (! ASCII_CHAR_P (c))
1719 {
1720 /* See the comment in the previous similar code. */
1721 for (i = 0; i < n_char_ranges; i += 2)
1722 if (c >= char_ranges[i] && c <= char_ranges[i + 1])
1723 break;
1724 if (!(negate ^ (i < n_char_ranges)))
1725 break;
1726 }
1727 pos--, pos_byte -= prev_p - p;
1728 }
1729 else
1730 while (1)
1731 {
1732 if (p <= stop)
1733 {
1734 if (p <= endp)
1735 break;
1736 p = GPT_ADDR;
1737 stop = endp;
1738 }
1739 if (!fastmap[p[-1]])
1740 break;
1741 p--, pos--, pos_byte--;
1742 }
1743 }
1744
1745 SET_PT_BOTH (pos, pos_byte);
1746 immediate_quit = 0;
1747
1748 return make_number (PT - start_point);
1749 }
1750 }
1751
1752
1753 static Lisp_Object
1754 skip_syntaxes (forwardp, string, lim)
1755 int forwardp;
1756 Lisp_Object string, lim;
1757 {
1758 register unsigned int c;
1759 unsigned char fastmap[0400];
1760 int negate = 0;
1761 register int i, i_byte;
1762 int multibyte;
1763 int size_byte;
1764 unsigned char *str;
1765
1766 CHECK_STRING (string);
1767
1768 if (NILP (lim))
1769 XSETINT (lim, forwardp ? ZV : BEGV);
1770 else
1771 CHECK_NUMBER_COERCE_MARKER (lim);
1772
1773 /* In any case, don't allow scan outside bounds of buffer. */
1774 if (XINT (lim) > ZV)
1775 XSETFASTINT (lim, ZV);
1776 if (XINT (lim) < BEGV)
1777 XSETFASTINT (lim, BEGV);
1778
1779 if (forwardp ? (PT >= XFASTINT (lim)) : (PT <= XFASTINT (lim)))
1780 return Qnil;
1781
1782 multibyte = (!NILP (current_buffer->enable_multibyte_characters)
1783 && (XINT (lim) - PT != CHAR_TO_BYTE (XINT (lim)) - PT_BYTE));
1784
1785 bzero (fastmap, sizeof fastmap);
1786
1787 if (SBYTES (string) > SCHARS (string))
1788 /* As this is very rare case (syntax spec is ASCII only), don't
1789 consider efficiency. */
1790 string = string_make_unibyte (string);
1791
1792 str = SDATA (string);
1793 size_byte = SBYTES (string);
1794
1795 i_byte = 0;
1796 if (i_byte < size_byte
1797 && SREF (string, 0) == '^')
1798 {
1799 negate = 1; i_byte++;
1800 }
1801
1802 /* Find the syntaxes specified and set their elements of fastmap. */
1803
1804 while (i_byte < size_byte)
1805 {
1806 c = str[i_byte++];
1807 fastmap[syntax_spec_code[c]] = 1;
1808 }
1809
1810 /* If ^ was the first character, complement the fastmap. */
1811 if (negate)
1812 for (i = 0; i < sizeof fastmap; i++)
1813 fastmap[i] ^= 1;
1814
1815 {
1816 int start_point = PT;
1817 int pos = PT;
1818 int pos_byte = PT_BYTE;
1819 unsigned char *p = PT_ADDR, *endp, *stop;
1820
1821 if (forwardp)
1822 {
1823 endp = (XINT (lim) == GPT) ? GPT_ADDR : CHAR_POS_ADDR (XINT (lim));
1824 stop = (pos < GPT && GPT < XINT (lim)) ? GPT_ADDR : endp;
1825 }
1826 else
1827 {
1828 endp = CHAR_POS_ADDR (XINT (lim));
1829 stop = (pos >= GPT && GPT > XINT (lim)) ? GAP_END_ADDR : endp;
1830 }
1831
1832 immediate_quit = 1;
1833 SETUP_SYNTAX_TABLE (pos, forwardp ? 1 : -1);
1834 if (forwardp)
1835 {
1836 if (multibyte)
1837 {
1838 while (1)
1839 {
1840 int nbytes;
1841
1842 if (p >= stop)
1843 {
1844 if (p >= endp)
1845 break;
1846 p = GAP_END_ADDR;
1847 stop = endp;
1848 }
1849 c = STRING_CHAR_AND_LENGTH (p, MAX_MULTIBYTE_LENGTH, nbytes);
1850 if (! fastmap[(int) SYNTAX (c)])
1851 break;
1852 p += nbytes, pos++, pos_byte += nbytes;
1853 UPDATE_SYNTAX_TABLE_FORWARD (pos);
1854 }
1855 }
1856 else
1857 {
1858 while (1)
1859 {
1860 if (p >= stop)
1861 {
1862 if (p >= endp)
1863 break;
1864 p = GAP_END_ADDR;
1865 stop = endp;
1866 }
1867 if (! fastmap[(int) SYNTAX (*p)])
1868 break;
1869 p++, pos++, pos_byte++;
1870 UPDATE_SYNTAX_TABLE_FORWARD (pos);
1871 }
1872 }
1873 }
1874 else
1875 {
1876 if (multibyte)
1877 {
1878 while (1)
1879 {
1880 unsigned char *prev_p;
1881
1882 if (p <= stop)
1883 {
1884 if (p <= endp)
1885 break;
1886 p = GPT_ADDR;
1887 stop = endp;
1888 }
1889 prev_p = p;
1890 while (--p >= stop && ! CHAR_HEAD_P (*p));
1891 c = STRING_CHAR (p, MAX_MULTIBYTE_LENGTH);
1892 if (! fastmap[(int) SYNTAX (c)])
1893 break;
1894 pos--, pos_byte -= prev_p - p;
1895 UPDATE_SYNTAX_TABLE_BACKWARD (pos);
1896 }
1897 }
1898 else
1899 {
1900 while (1)
1901 {
1902 if (p <= stop)
1903 {
1904 if (p <= endp)
1905 break;
1906 p = GPT_ADDR;
1907 stop = endp;
1908 }
1909 if (! fastmap[(int) SYNTAX (p[-1])])
1910 break;
1911 p--, pos--, pos_byte--;
1912 UPDATE_SYNTAX_TABLE_BACKWARD (pos - 1);
1913 }
1914 }
1915 }
1916
1917 SET_PT_BOTH (pos, pos_byte);
1918 immediate_quit = 0;
1919
1920 return make_number (PT - start_point);
1921 }
1922 }
1923 \f
1924 /* Jump over a comment, assuming we are at the beginning of one.
1925 FROM is the current position.
1926 FROM_BYTE is the bytepos corresponding to FROM.
1927 Do not move past STOP (a charpos).
1928 The comment over which we have to jump is of style STYLE
1929 (either SYNTAX_COMMENT_STYLE(foo) or ST_COMMENT_STYLE).
1930 NESTING should be positive to indicate the nesting at the beginning
1931 for nested comments and should be zero or negative else.
1932 ST_COMMENT_STYLE cannot be nested.
1933 PREV_SYNTAX is the SYNTAX_WITH_FLAGS of the previous character
1934 (or 0 If the search cannot start in the middle of a two-character).
1935
1936 If successful, return 1 and store the charpos of the comment's end
1937 into *CHARPOS_PTR and the corresponding bytepos into *BYTEPOS_PTR.
1938 Else, return 0 and store the charpos STOP into *CHARPOS_PTR, the
1939 corresponding bytepos into *BYTEPOS_PTR and the current nesting
1940 (as defined for state.incomment) in *INCOMMENT_PTR.
1941
1942 The comment end is the last character of the comment rather than the
1943 character just after the comment.
1944
1945 Global syntax data is assumed to initially be valid for FROM and
1946 remains valid for forward search starting at the returned position. */
1947
1948 static int
1949 forw_comment (from, from_byte, stop, nesting, style, prev_syntax,
1950 charpos_ptr, bytepos_ptr, incomment_ptr)
1951 int from, from_byte, stop;
1952 int nesting, style, prev_syntax;
1953 int *charpos_ptr, *bytepos_ptr, *incomment_ptr;
1954 {
1955 register int c, c1;
1956 register enum syntaxcode code;
1957 register int syntax;
1958
1959 if (nesting <= 0) nesting = -1;
1960
1961 /* Enter the loop in the middle so that we find
1962 a 2-char comment ender if we start in the middle of it. */
1963 syntax = prev_syntax;
1964 if (syntax != 0) goto forw_incomment;
1965
1966 while (1)
1967 {
1968 if (from == stop)
1969 {
1970 *incomment_ptr = nesting;
1971 *charpos_ptr = from;
1972 *bytepos_ptr = from_byte;
1973 return 0;
1974 }
1975 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
1976 syntax = SYNTAX_WITH_FLAGS (c);
1977 code = syntax & 0xff;
1978 if (code == Sendcomment
1979 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
1980 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ?
1981 (nesting > 0 && --nesting == 0) : nesting < 0))
1982 /* we have encountered a comment end of the same style
1983 as the comment sequence which began this comment
1984 section */
1985 break;
1986 if (code == Scomment_fence
1987 && style == ST_COMMENT_STYLE)
1988 /* we have encountered a comment end of the same style
1989 as the comment sequence which began this comment
1990 section. */
1991 break;
1992 if (nesting > 0
1993 && code == Scomment
1994 && SYNTAX_FLAGS_COMMENT_NESTED (syntax)
1995 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style)
1996 /* we have encountered a nested comment of the same style
1997 as the comment sequence which began this comment section */
1998 nesting++;
1999 INC_BOTH (from, from_byte);
2000 UPDATE_SYNTAX_TABLE_FORWARD (from);
2001
2002 forw_incomment:
2003 if (from < stop && SYNTAX_FLAGS_COMEND_FIRST (syntax)
2004 && SYNTAX_FLAGS_COMMENT_STYLE (syntax) == style
2005 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2006 SYNTAX_COMEND_SECOND (c1))
2007 && ((SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2008 SYNTAX_COMMENT_NESTED (c1)) ? nesting > 0 : nesting < 0))
2009 {
2010 if (--nesting <= 0)
2011 /* we have encountered a comment end of the same style
2012 as the comment sequence which began this comment
2013 section */
2014 break;
2015 else
2016 {
2017 INC_BOTH (from, from_byte);
2018 UPDATE_SYNTAX_TABLE_FORWARD (from);
2019 }
2020 }
2021 if (nesting > 0
2022 && from < stop
2023 && SYNTAX_FLAGS_COMSTART_FIRST (syntax)
2024 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2025 SYNTAX_COMMENT_STYLE (c1) == style
2026 && SYNTAX_COMSTART_SECOND (c1))
2027 && (SYNTAX_FLAGS_COMMENT_NESTED (syntax) ||
2028 SYNTAX_COMMENT_NESTED (c1)))
2029 /* we have encountered a nested comment of the same style
2030 as the comment sequence which began this comment
2031 section */
2032 {
2033 INC_BOTH (from, from_byte);
2034 UPDATE_SYNTAX_TABLE_FORWARD (from);
2035 nesting++;
2036 }
2037 }
2038 *charpos_ptr = from;
2039 *bytepos_ptr = from_byte;
2040 return 1;
2041 }
2042
2043 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
2044 doc: /*
2045 Move forward across up to COUNT comments. If COUNT is negative, move backward.
2046 Stop scanning if we find something other than a comment or whitespace.
2047 Set point to where scanning stops.
2048 If COUNT comments are found as expected, with nothing except whitespace
2049 between them, return t; otherwise return nil. */)
2050 (count)
2051 Lisp_Object count;
2052 {
2053 register int from;
2054 int from_byte;
2055 register int stop;
2056 register int c, c1;
2057 register enum syntaxcode code;
2058 int comstyle = 0; /* style of comment encountered */
2059 int comnested = 0; /* whether the comment is nestable or not */
2060 int found;
2061 int count1;
2062 int out_charpos, out_bytepos;
2063 int dummy;
2064
2065 CHECK_NUMBER (count);
2066 count1 = XINT (count);
2067 stop = count1 > 0 ? ZV : BEGV;
2068
2069 immediate_quit = 1;
2070 QUIT;
2071
2072 from = PT;
2073 from_byte = PT_BYTE;
2074
2075 SETUP_SYNTAX_TABLE (from, count1);
2076 while (count1 > 0)
2077 {
2078 do
2079 {
2080 int comstart_first;
2081
2082 if (from == stop)
2083 {
2084 SET_PT_BOTH (from, from_byte);
2085 immediate_quit = 0;
2086 return Qnil;
2087 }
2088 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2089 code = SYNTAX (c);
2090 comstart_first = SYNTAX_COMSTART_FIRST (c);
2091 comnested = SYNTAX_COMMENT_NESTED (c);
2092 comstyle = SYNTAX_COMMENT_STYLE (c);
2093 INC_BOTH (from, from_byte);
2094 UPDATE_SYNTAX_TABLE_FORWARD (from);
2095 if (from < stop && comstart_first
2096 && (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2097 SYNTAX_COMSTART_SECOND (c1)))
2098 {
2099 /* We have encountered a comment start sequence and we
2100 are ignoring all text inside comments. We must record
2101 the comment style this sequence begins so that later,
2102 only a comment end of the same style actually ends
2103 the comment section. */
2104 code = Scomment;
2105 comstyle = SYNTAX_COMMENT_STYLE (c1);
2106 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2107 INC_BOTH (from, from_byte);
2108 UPDATE_SYNTAX_TABLE_FORWARD (from);
2109 }
2110 }
2111 while (code == Swhitespace || (code == Sendcomment && c == '\n'));
2112
2113 if (code == Scomment_fence)
2114 comstyle = ST_COMMENT_STYLE;
2115 else if (code != Scomment)
2116 {
2117 immediate_quit = 0;
2118 DEC_BOTH (from, from_byte);
2119 SET_PT_BOTH (from, from_byte);
2120 return Qnil;
2121 }
2122 /* We're at the start of a comment. */
2123 found = forw_comment (from, from_byte, stop, comnested, comstyle, 0,
2124 &out_charpos, &out_bytepos, &dummy);
2125 from = out_charpos; from_byte = out_bytepos;
2126 if (!found)
2127 {
2128 immediate_quit = 0;
2129 SET_PT_BOTH (from, from_byte);
2130 return Qnil;
2131 }
2132 INC_BOTH (from, from_byte);
2133 UPDATE_SYNTAX_TABLE_FORWARD (from);
2134 /* We have skipped one comment. */
2135 count1--;
2136 }
2137
2138 while (count1 < 0)
2139 {
2140 while (1)
2141 {
2142 int quoted;
2143
2144 if (from <= stop)
2145 {
2146 SET_PT_BOTH (BEGV, BEGV_BYTE);
2147 immediate_quit = 0;
2148 return Qnil;
2149 }
2150
2151 DEC_BOTH (from, from_byte);
2152 /* char_quoted does UPDATE_SYNTAX_TABLE_BACKWARD (from). */
2153 quoted = char_quoted (from, from_byte);
2154 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2155 code = SYNTAX (c);
2156 comstyle = 0;
2157 comnested = SYNTAX_COMMENT_NESTED (c);
2158 if (code == Sendcomment)
2159 comstyle = SYNTAX_COMMENT_STYLE (c);
2160 if (from > stop && SYNTAX_COMEND_SECOND (c)
2161 && prev_char_comend_first (from, from_byte)
2162 && !char_quoted (from - 1, dec_bytepos (from_byte)))
2163 {
2164 /* We must record the comment style encountered so that
2165 later, we can match only the proper comment begin
2166 sequence of the same style. */
2167 DEC_BOTH (from, from_byte);
2168 code = Sendcomment;
2169 /* Calling char_quoted, above, set up global syntax position
2170 at the new value of FROM. */
2171 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2172 comstyle = SYNTAX_COMMENT_STYLE (c1);
2173 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2174 }
2175
2176 if (code == Scomment_fence)
2177 {
2178 /* Skip until first preceding unquoted comment_fence. */
2179 int found = 0, ini = from, ini_byte = from_byte;
2180
2181 while (1)
2182 {
2183 DEC_BOTH (from, from_byte);
2184 if (from == stop)
2185 break;
2186 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2187 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2188 if (SYNTAX (c) == Scomment_fence
2189 && !char_quoted (from, from_byte))
2190 {
2191 found = 1;
2192 break;
2193 }
2194 }
2195 if (found == 0)
2196 {
2197 from = ini; /* Set point to ini + 1. */
2198 from_byte = ini_byte;
2199 goto leave;
2200 }
2201 }
2202 else if (code == Sendcomment)
2203 {
2204 found = back_comment (from, from_byte, stop, comnested, comstyle,
2205 &out_charpos, &out_bytepos);
2206 if (found == -1)
2207 {
2208 if (c == '\n')
2209 /* This end-of-line is not an end-of-comment.
2210 Treat it like a whitespace.
2211 CC-mode (and maybe others) relies on this behavior. */
2212 ;
2213 else
2214 {
2215 /* Failure: we should go back to the end of this
2216 not-quite-endcomment. */
2217 if (SYNTAX(c) != code)
2218 /* It was a two-char Sendcomment. */
2219 INC_BOTH (from, from_byte);
2220 goto leave;
2221 }
2222 }
2223 else
2224 {
2225 /* We have skipped one comment. */
2226 from = out_charpos, from_byte = out_bytepos;
2227 break;
2228 }
2229 }
2230 else if (code != Swhitespace || quoted)
2231 {
2232 leave:
2233 immediate_quit = 0;
2234 INC_BOTH (from, from_byte);
2235 SET_PT_BOTH (from, from_byte);
2236 return Qnil;
2237 }
2238 }
2239
2240 count1++;
2241 }
2242
2243 SET_PT_BOTH (from, from_byte);
2244 immediate_quit = 0;
2245 return Qt;
2246 }
2247 \f
2248 /* Return syntax code of character C if C is an ASCII character
2249 or `multibyte_symbol_p' is zero. Otherwise, return Ssymbol. */
2250
2251 #define SYNTAX_WITH_MULTIBYTE_CHECK(c) \
2252 ((ASCII_CHAR_P (c) || !multibyte_symbol_p) \
2253 ? SYNTAX (c) : Ssymbol)
2254
2255 static Lisp_Object
2256 scan_lists (from, count, depth, sexpflag)
2257 register int from;
2258 int count, depth, sexpflag;
2259 {
2260 Lisp_Object val;
2261 register int stop = count > 0 ? ZV : BEGV;
2262 register int c, c1;
2263 int stringterm;
2264 int quoted;
2265 int mathexit = 0;
2266 register enum syntaxcode code, temp_code;
2267 int min_depth = depth; /* Err out if depth gets less than this. */
2268 int comstyle = 0; /* style of comment encountered */
2269 int comnested = 0; /* whether the comment is nestable or not */
2270 int temp_pos;
2271 int last_good = from;
2272 int found;
2273 int from_byte;
2274 int out_bytepos, out_charpos;
2275 int temp, dummy;
2276 int multibyte_symbol_p = sexpflag && multibyte_syntax_as_symbol;
2277
2278 if (depth > 0) min_depth = 0;
2279
2280 if (from > ZV) from = ZV;
2281 if (from < BEGV) from = BEGV;
2282
2283 from_byte = CHAR_TO_BYTE (from);
2284
2285 immediate_quit = 1;
2286 QUIT;
2287
2288 SETUP_SYNTAX_TABLE (from, count);
2289 while (count > 0)
2290 {
2291 while (from < stop)
2292 {
2293 int comstart_first, prefix;
2294 UPDATE_SYNTAX_TABLE_FORWARD (from);
2295 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2296 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2297 comstart_first = SYNTAX_COMSTART_FIRST (c);
2298 comnested = SYNTAX_COMMENT_NESTED (c);
2299 comstyle = SYNTAX_COMMENT_STYLE (c);
2300 prefix = SYNTAX_PREFIX (c);
2301 if (depth == min_depth)
2302 last_good = from;
2303 INC_BOTH (from, from_byte);
2304 UPDATE_SYNTAX_TABLE_FORWARD (from);
2305 if (from < stop && comstart_first
2306 && SYNTAX_COMSTART_SECOND (FETCH_CHAR_AS_MULTIBYTE (from_byte))
2307 && parse_sexp_ignore_comments)
2308 {
2309 /* we have encountered a comment start sequence and we
2310 are ignoring all text inside comments. We must record
2311 the comment style this sequence begins so that later,
2312 only a comment end of the same style actually ends
2313 the comment section */
2314 code = Scomment;
2315 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2316 comstyle = SYNTAX_COMMENT_STYLE (c1);
2317 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2318 INC_BOTH (from, from_byte);
2319 UPDATE_SYNTAX_TABLE_FORWARD (from);
2320 }
2321
2322 if (prefix)
2323 continue;
2324
2325 switch (SWITCH_ENUM_CAST (code))
2326 {
2327 case Sescape:
2328 case Scharquote:
2329 if (from == stop) goto lose;
2330 INC_BOTH (from, from_byte);
2331 /* treat following character as a word constituent */
2332 case Sword:
2333 case Ssymbol:
2334 if (depth || !sexpflag) break;
2335 /* This word counts as a sexp; return at end of it. */
2336 while (from < stop)
2337 {
2338 UPDATE_SYNTAX_TABLE_FORWARD (from);
2339
2340 /* Some compilers can't handle this inside the switch. */
2341 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2342 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2343 switch (temp)
2344 {
2345 case Scharquote:
2346 case Sescape:
2347 INC_BOTH (from, from_byte);
2348 if (from == stop) goto lose;
2349 break;
2350 case Sword:
2351 case Ssymbol:
2352 case Squote:
2353 break;
2354 default:
2355 goto done;
2356 }
2357 INC_BOTH (from, from_byte);
2358 }
2359 goto done;
2360
2361 case Scomment_fence:
2362 comstyle = ST_COMMENT_STYLE;
2363 /* FALLTHROUGH */
2364 case Scomment:
2365 if (!parse_sexp_ignore_comments) break;
2366 UPDATE_SYNTAX_TABLE_FORWARD (from);
2367 found = forw_comment (from, from_byte, stop,
2368 comnested, comstyle, 0,
2369 &out_charpos, &out_bytepos, &dummy);
2370 from = out_charpos, from_byte = out_bytepos;
2371 if (!found)
2372 {
2373 if (depth == 0)
2374 goto done;
2375 goto lose;
2376 }
2377 INC_BOTH (from, from_byte);
2378 UPDATE_SYNTAX_TABLE_FORWARD (from);
2379 break;
2380
2381 case Smath:
2382 if (!sexpflag)
2383 break;
2384 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (from_byte))
2385 {
2386 INC_BOTH (from, from_byte);
2387 }
2388 if (mathexit)
2389 {
2390 mathexit = 0;
2391 goto close1;
2392 }
2393 mathexit = 1;
2394
2395 case Sopen:
2396 if (!++depth) goto done;
2397 break;
2398
2399 case Sclose:
2400 close1:
2401 if (!--depth) goto done;
2402 if (depth < min_depth)
2403 Fsignal (Qscan_error,
2404 Fcons (build_string ("Containing expression ends prematurely"),
2405 Fcons (make_number (last_good),
2406 Fcons (make_number (from), Qnil))));
2407 break;
2408
2409 case Sstring:
2410 case Sstring_fence:
2411 temp_pos = dec_bytepos (from_byte);
2412 stringterm = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2413 while (1)
2414 {
2415 if (from >= stop) goto lose;
2416 UPDATE_SYNTAX_TABLE_FORWARD (from);
2417 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2418 if (code == Sstring
2419 ? (c == stringterm
2420 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2421 : SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring_fence)
2422 break;
2423
2424 /* Some compilers can't handle this inside the switch. */
2425 temp = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2426 switch (temp)
2427 {
2428 case Scharquote:
2429 case Sescape:
2430 INC_BOTH (from, from_byte);
2431 }
2432 INC_BOTH (from, from_byte);
2433 }
2434 INC_BOTH (from, from_byte);
2435 if (!depth && sexpflag) goto done;
2436 break;
2437 default:
2438 /* Ignore whitespace, punctuation, quote, endcomment. */
2439 break;
2440 }
2441 }
2442
2443 /* Reached end of buffer. Error if within object, return nil if between */
2444 if (depth) goto lose;
2445
2446 immediate_quit = 0;
2447 return Qnil;
2448
2449 /* End of object reached */
2450 done:
2451 count--;
2452 }
2453
2454 while (count < 0)
2455 {
2456 while (from > stop)
2457 {
2458 DEC_BOTH (from, from_byte);
2459 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2460 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2461 code = SYNTAX_WITH_MULTIBYTE_CHECK (c);
2462 if (depth == min_depth)
2463 last_good = from;
2464 comstyle = 0;
2465 comnested = SYNTAX_COMMENT_NESTED (c);
2466 if (code == Sendcomment)
2467 comstyle = SYNTAX_COMMENT_STYLE (c);
2468 if (from > stop && SYNTAX_COMEND_SECOND (c)
2469 && prev_char_comend_first (from, from_byte)
2470 && parse_sexp_ignore_comments)
2471 {
2472 /* We must record the comment style encountered so that
2473 later, we can match only the proper comment begin
2474 sequence of the same style. */
2475 DEC_BOTH (from, from_byte);
2476 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2477 code = Sendcomment;
2478 c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2479 comstyle = SYNTAX_COMMENT_STYLE (c1);
2480 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2481 }
2482
2483 /* Quoting turns anything except a comment-ender
2484 into a word character. Note that this cannot be true
2485 if we decremented FROM in the if-statement above. */
2486 if (code != Sendcomment && char_quoted (from, from_byte))
2487 {
2488 DEC_BOTH (from, from_byte);
2489 code = Sword;
2490 }
2491 else if (SYNTAX_PREFIX (c))
2492 continue;
2493
2494 switch (SWITCH_ENUM_CAST (code))
2495 {
2496 case Sword:
2497 case Ssymbol:
2498 case Sescape:
2499 case Scharquote:
2500 if (depth || !sexpflag) break;
2501 /* This word counts as a sexp; count object finished
2502 after passing it. */
2503 while (from > stop)
2504 {
2505 temp_pos = from_byte;
2506 if (! NILP (current_buffer->enable_multibyte_characters))
2507 DEC_POS (temp_pos);
2508 else
2509 temp_pos--;
2510 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2511 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2512 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2513 /* Don't allow comment-end to be quoted. */
2514 if (temp_code == Sendcomment)
2515 goto done2;
2516 quoted = char_quoted (from - 1, temp_pos);
2517 if (quoted)
2518 {
2519 DEC_BOTH (from, from_byte);
2520 temp_pos = dec_bytepos (temp_pos);
2521 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2522 }
2523 c1 = FETCH_CHAR_AS_MULTIBYTE (temp_pos);
2524 temp_code = SYNTAX_WITH_MULTIBYTE_CHECK (c1);
2525 if (! (quoted || temp_code == Sword
2526 || temp_code == Ssymbol
2527 || temp_code == Squote))
2528 goto done2;
2529 DEC_BOTH (from, from_byte);
2530 }
2531 goto done2;
2532
2533 case Smath:
2534 if (!sexpflag)
2535 break;
2536 temp_pos = dec_bytepos (from_byte);
2537 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2538 if (from != stop && c == FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2539 DEC_BOTH (from, from_byte);
2540 if (mathexit)
2541 {
2542 mathexit = 0;
2543 goto open2;
2544 }
2545 mathexit = 1;
2546
2547 case Sclose:
2548 if (!++depth) goto done2;
2549 break;
2550
2551 case Sopen:
2552 open2:
2553 if (!--depth) goto done2;
2554 if (depth < min_depth)
2555 Fsignal (Qscan_error,
2556 Fcons (build_string ("Containing expression ends prematurely"),
2557 Fcons (make_number (last_good),
2558 Fcons (make_number (from), Qnil))));
2559 break;
2560
2561 case Sendcomment:
2562 if (!parse_sexp_ignore_comments)
2563 break;
2564 found = back_comment (from, from_byte, stop, comnested, comstyle,
2565 &out_charpos, &out_bytepos);
2566 /* FIXME: if found == -1, then it really wasn't a comment-end.
2567 For single-char Sendcomment, we can't do much about it apart
2568 from skipping the char.
2569 For 2-char endcomments, we could try again, taking both
2570 chars as separate entities, but it's a lot of trouble
2571 for very little gain, so we don't bother either. -sm */
2572 if (found != -1)
2573 from = out_charpos, from_byte = out_bytepos;
2574 break;
2575
2576 case Scomment_fence:
2577 case Sstring_fence:
2578 while (1)
2579 {
2580 DEC_BOTH (from, from_byte);
2581 if (from == stop) goto lose;
2582 UPDATE_SYNTAX_TABLE_BACKWARD (from);
2583 if (!char_quoted (from, from_byte)
2584 && (c = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2585 SYNTAX_WITH_MULTIBYTE_CHECK (c) == code))
2586 break;
2587 }
2588 if (code == Sstring_fence && !depth && sexpflag) goto done2;
2589 break;
2590
2591 case Sstring:
2592 stringterm = FETCH_CHAR_AS_MULTIBYTE (from_byte);
2593 while (1)
2594 {
2595 if (from == stop) goto lose;
2596 temp_pos = from_byte;
2597 if (! NILP (current_buffer->enable_multibyte_characters))
2598 DEC_POS (temp_pos);
2599 else
2600 temp_pos--;
2601 UPDATE_SYNTAX_TABLE_BACKWARD (from - 1);
2602 if (!char_quoted (from - 1, temp_pos)
2603 && stringterm == (c = FETCH_CHAR_AS_MULTIBYTE (temp_pos))
2604 && SYNTAX_WITH_MULTIBYTE_CHECK (c) == Sstring)
2605 break;
2606 DEC_BOTH (from, from_byte);
2607 }
2608 DEC_BOTH (from, from_byte);
2609 if (!depth && sexpflag) goto done2;
2610 break;
2611 default:
2612 /* Ignore whitespace, punctuation, quote, endcomment. */
2613 break;
2614 }
2615 }
2616
2617 /* Reached start of buffer. Error if within object, return nil if between */
2618 if (depth) goto lose;
2619
2620 immediate_quit = 0;
2621 return Qnil;
2622
2623 done2:
2624 count++;
2625 }
2626
2627
2628 immediate_quit = 0;
2629 XSETFASTINT (val, from);
2630 return val;
2631
2632 lose:
2633 Fsignal (Qscan_error,
2634 Fcons (build_string ("Unbalanced parentheses"),
2635 Fcons (make_number (last_good),
2636 Fcons (make_number (from), Qnil))));
2637
2638 /* NOTREACHED */
2639 }
2640
2641 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
2642 doc: /* Scan from character number FROM by COUNT lists.
2643 Returns the character number of the position thus found.
2644
2645 If DEPTH is nonzero, paren depth begins counting from that value,
2646 only places where the depth in parentheses becomes zero
2647 are candidates for stopping; COUNT such places are counted.
2648 Thus, a positive value for DEPTH means go out levels.
2649
2650 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2651
2652 If the beginning or end of (the accessible part of) the buffer is reached
2653 and the depth is wrong, an error is signaled.
2654 If the depth is right but the count is not used up, nil is returned. */)
2655 (from, count, depth)
2656 Lisp_Object from, count, depth;
2657 {
2658 CHECK_NUMBER (from);
2659 CHECK_NUMBER (count);
2660 CHECK_NUMBER (depth);
2661
2662 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
2663 }
2664
2665 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
2666 doc: /* Scan from character number FROM by COUNT balanced expressions.
2667 If COUNT is negative, scan backwards.
2668 Returns the character number of the position thus found.
2669
2670 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.
2671
2672 If the beginning or end of (the accessible part of) the buffer is reached
2673 in the middle of a parenthetical grouping, an error is signaled.
2674 If the beginning or end is reached between groupings
2675 but before count is used up, nil is returned. */)
2676 (from, count)
2677 Lisp_Object from, count;
2678 {
2679 CHECK_NUMBER (from);
2680 CHECK_NUMBER (count);
2681
2682 return scan_lists (XINT (from), XINT (count), 0, 1);
2683 }
2684
2685 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
2686 0, 0, 0,
2687 doc: /* Move point backward over any number of chars with prefix syntax.
2688 This includes chars with "quote" or "prefix" syntax (' or p). */)
2689 ()
2690 {
2691 int beg = BEGV;
2692 int opoint = PT;
2693 int opoint_byte = PT_BYTE;
2694 int pos = PT;
2695 int pos_byte = PT_BYTE;
2696 int c;
2697
2698 if (pos <= beg)
2699 {
2700 SET_PT_BOTH (opoint, opoint_byte);
2701
2702 return Qnil;
2703 }
2704
2705 SETUP_SYNTAX_TABLE (pos, -1);
2706
2707 DEC_BOTH (pos, pos_byte);
2708
2709 while (!char_quoted (pos, pos_byte)
2710 /* Previous statement updates syntax table. */
2711 && ((c = FETCH_CHAR_AS_MULTIBYTE (pos_byte), SYNTAX (c) == Squote)
2712 || SYNTAX_PREFIX (c)))
2713 {
2714 opoint = pos;
2715 opoint_byte = pos_byte;
2716
2717 if (pos + 1 > beg)
2718 DEC_BOTH (pos, pos_byte);
2719 }
2720
2721 SET_PT_BOTH (opoint, opoint_byte);
2722
2723 return Qnil;
2724 }
2725 \f
2726 /* Parse forward from FROM / FROM_BYTE to END,
2727 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
2728 and return a description of the state of the parse at END.
2729 If STOPBEFORE is nonzero, stop at the start of an atom.
2730 If COMMENTSTOP is 1, stop at the start of a comment.
2731 If COMMENTSTOP is -1, stop at the start or end of a comment,
2732 after the beginning of a string, or after the end of a string. */
2733
2734 static void
2735 scan_sexps_forward (stateptr, from, from_byte, end, targetdepth,
2736 stopbefore, oldstate, commentstop)
2737 struct lisp_parse_state *stateptr;
2738 register int from;
2739 int from_byte;
2740 int end, targetdepth, stopbefore;
2741 Lisp_Object oldstate;
2742 int commentstop;
2743 {
2744 struct lisp_parse_state state;
2745
2746 register enum syntaxcode code;
2747 int c1;
2748 int comnested;
2749 struct level { int last, prev; };
2750 struct level levelstart[100];
2751 register struct level *curlevel = levelstart;
2752 struct level *endlevel = levelstart + 100;
2753 register int depth; /* Paren depth of current scanning location.
2754 level - levelstart equals this except
2755 when the depth becomes negative. */
2756 int mindepth; /* Lowest DEPTH value seen. */
2757 int start_quoted = 0; /* Nonzero means starting after a char quote */
2758 Lisp_Object tem;
2759 int prev_from; /* Keep one character before FROM. */
2760 int prev_from_byte;
2761 int prev_from_syntax;
2762 int boundary_stop = commentstop == -1;
2763 int nofence;
2764 int found;
2765 int out_bytepos, out_charpos;
2766 int temp;
2767
2768 prev_from = from;
2769 prev_from_byte = from_byte;
2770 if (from != BEGV)
2771 DEC_BOTH (prev_from, prev_from_byte);
2772
2773 /* Use this macro instead of `from++'. */
2774 #define INC_FROM \
2775 do { prev_from = from; \
2776 prev_from_byte = from_byte; \
2777 prev_from_syntax \
2778 = SYNTAX_WITH_FLAGS (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte)); \
2779 INC_BOTH (from, from_byte); \
2780 if (from < end) \
2781 UPDATE_SYNTAX_TABLE_FORWARD (from); \
2782 } while (0)
2783
2784 immediate_quit = 1;
2785 QUIT;
2786
2787 if (NILP (oldstate))
2788 {
2789 depth = 0;
2790 state.instring = -1;
2791 state.incomment = 0;
2792 state.comstyle = 0; /* comment style a by default. */
2793 state.comstr_start = -1; /* no comment/string seen. */
2794 }
2795 else
2796 {
2797 tem = Fcar (oldstate);
2798 if (!NILP (tem))
2799 depth = XINT (tem);
2800 else
2801 depth = 0;
2802
2803 oldstate = Fcdr (oldstate);
2804 oldstate = Fcdr (oldstate);
2805 oldstate = Fcdr (oldstate);
2806 tem = Fcar (oldstate);
2807 /* Check whether we are inside string_fence-style string: */
2808 state.instring = (!NILP (tem)
2809 ? (INTEGERP (tem) ? XINT (tem) : ST_STRING_STYLE)
2810 : -1);
2811
2812 oldstate = Fcdr (oldstate);
2813 tem = Fcar (oldstate);
2814 state.incomment = (!NILP (tem)
2815 ? (INTEGERP (tem) ? XINT (tem) : -1)
2816 : 0);
2817
2818 oldstate = Fcdr (oldstate);
2819 tem = Fcar (oldstate);
2820 start_quoted = !NILP (tem);
2821
2822 /* if the eighth element of the list is nil, we are in comment
2823 style a. If it is non-nil, we are in comment style b */
2824 oldstate = Fcdr (oldstate);
2825 oldstate = Fcdr (oldstate);
2826 tem = Fcar (oldstate);
2827 state.comstyle = NILP (tem) ? 0 : (EQ (tem, Qsyntax_table)
2828 ? ST_COMMENT_STYLE : 1);
2829
2830 oldstate = Fcdr (oldstate);
2831 tem = Fcar (oldstate);
2832 state.comstr_start = NILP (tem) ? -1 : XINT (tem) ;
2833 oldstate = Fcdr (oldstate);
2834 tem = Fcar (oldstate);
2835 while (!NILP (tem)) /* >= second enclosing sexps. */
2836 {
2837 /* curlevel++->last ran into compiler bug on Apollo */
2838 curlevel->last = XINT (Fcar (tem));
2839 if (++curlevel == endlevel)
2840 curlevel--; /* error ("Nesting too deep for parser"); */
2841 curlevel->prev = -1;
2842 curlevel->last = -1;
2843 tem = Fcdr (tem);
2844 }
2845 }
2846 state.quoted = 0;
2847 mindepth = depth;
2848
2849 curlevel->prev = -1;
2850 curlevel->last = -1;
2851
2852 SETUP_SYNTAX_TABLE (prev_from, 1);
2853 prev_from_syntax = SYNTAX_WITH_FLAGS (FETCH_CHAR (prev_from_byte));
2854 UPDATE_SYNTAX_TABLE_FORWARD (from);
2855
2856 /* Enter the loop at a place appropriate for initial state. */
2857
2858 if (state.incomment)
2859 goto startincomment;
2860 if (state.instring >= 0)
2861 {
2862 nofence = state.instring != ST_STRING_STYLE;
2863 if (start_quoted)
2864 goto startquotedinstring;
2865 goto startinstring;
2866 }
2867 else if (start_quoted)
2868 goto startquoted;
2869
2870 while (from < end)
2871 {
2872 INC_FROM;
2873 code = prev_from_syntax & 0xff;
2874
2875 if (code == Scomment)
2876 {
2877 state.comstyle = SYNTAX_FLAGS_COMMENT_STYLE (prev_from_syntax);
2878 state.incomment = (SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax) ?
2879 1 : -1);
2880 state.comstr_start = prev_from;
2881 }
2882 else if (code == Scomment_fence)
2883 {
2884 /* Record the comment style we have entered so that only
2885 the comment-end sequence of the same style actually
2886 terminates the comment section. */
2887 state.comstyle = ST_COMMENT_STYLE;
2888 state.incomment = -1;
2889 state.comstr_start = prev_from;
2890 code = Scomment;
2891 }
2892 else if (from < end)
2893 if (SYNTAX_FLAGS_COMSTART_FIRST (prev_from_syntax))
2894 if (c1 = FETCH_CHAR_AS_MULTIBYTE (from_byte),
2895 SYNTAX_COMSTART_SECOND (c1))
2896 /* Duplicate code to avoid a complex if-expression
2897 which causes trouble for the SGI compiler. */
2898 {
2899 /* Record the comment style we have entered so that only
2900 the comment-end sequence of the same style actually
2901 terminates the comment section. */
2902 state.comstyle = SYNTAX_COMMENT_STYLE (c1);
2903 comnested = SYNTAX_FLAGS_COMMENT_NESTED (prev_from_syntax);
2904 comnested = comnested || SYNTAX_COMMENT_NESTED (c1);
2905 state.incomment = comnested ? 1 : -1;
2906 state.comstr_start = prev_from;
2907 INC_FROM;
2908 code = Scomment;
2909 }
2910
2911 if (SYNTAX_FLAGS_PREFIX (prev_from_syntax))
2912 continue;
2913 switch (SWITCH_ENUM_CAST (code))
2914 {
2915 case Sescape:
2916 case Scharquote:
2917 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2918 curlevel->last = prev_from;
2919 startquoted:
2920 if (from == end) goto endquoted;
2921 INC_FROM;
2922 goto symstarted;
2923 /* treat following character as a word constituent */
2924 case Sword:
2925 case Ssymbol:
2926 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2927 curlevel->last = prev_from;
2928 symstarted:
2929 while (from < end)
2930 {
2931 /* Some compilers can't handle this inside the switch. */
2932 temp = SYNTAX (FETCH_CHAR_AS_MULTIBYTE (from_byte));
2933 switch (temp)
2934 {
2935 case Scharquote:
2936 case Sescape:
2937 INC_FROM;
2938 if (from == end) goto endquoted;
2939 break;
2940 case Sword:
2941 case Ssymbol:
2942 case Squote:
2943 break;
2944 default:
2945 goto symdone;
2946 }
2947 INC_FROM;
2948 }
2949 symdone:
2950 curlevel->prev = curlevel->last;
2951 break;
2952
2953 case Scomment_fence: /* Can't happen because it's handled above. */
2954 case Scomment:
2955 if (commentstop || boundary_stop) goto done;
2956 startincomment:
2957 /* The (from == BEGV) test was to enter the loop in the middle so
2958 that we find a 2-char comment ender even if we start in the
2959 middle of it. We don't want to do that if we're just at the
2960 beginning of the comment (think of (*) ... (*)). */
2961 found = forw_comment (from, from_byte, end,
2962 state.incomment, state.comstyle,
2963 (from == BEGV || from < state.comstr_start + 3)
2964 ? 0 : prev_from_syntax,
2965 &out_charpos, &out_bytepos, &state.incomment);
2966 from = out_charpos; from_byte = out_bytepos;
2967 /* Beware! prev_from and friends are invalid now.
2968 Luckily, the `done' doesn't use them and the INC_FROM
2969 sets them to a sane value without looking at them. */
2970 if (!found) goto done;
2971 INC_FROM;
2972 state.incomment = 0;
2973 state.comstyle = 0; /* reset the comment style */
2974 if (boundary_stop) goto done;
2975 break;
2976
2977 case Sopen:
2978 if (stopbefore) goto stop; /* this arg means stop at sexp start */
2979 depth++;
2980 /* curlevel++->last ran into compiler bug on Apollo */
2981 curlevel->last = prev_from;
2982 if (++curlevel == endlevel)
2983 curlevel--; /* error ("Nesting too deep for parser"); */
2984 curlevel->prev = -1;
2985 curlevel->last = -1;
2986 if (targetdepth == depth) goto done;
2987 break;
2988
2989 case Sclose:
2990 depth--;
2991 if (depth < mindepth)
2992 mindepth = depth;
2993 if (curlevel != levelstart)
2994 curlevel--;
2995 curlevel->prev = curlevel->last;
2996 if (targetdepth == depth) goto done;
2997 break;
2998
2999 case Sstring:
3000 case Sstring_fence:
3001 state.comstr_start = from - 1;
3002 if (stopbefore) goto stop; /* this arg means stop at sexp start */
3003 curlevel->last = prev_from;
3004 state.instring = (code == Sstring
3005 ? (FETCH_CHAR_AS_MULTIBYTE (prev_from_byte))
3006 : ST_STRING_STYLE);
3007 if (boundary_stop) goto done;
3008 startinstring:
3009 {
3010 nofence = state.instring != ST_STRING_STYLE;
3011
3012 while (1)
3013 {
3014 int c;
3015
3016 if (from >= end) goto done;
3017 c = FETCH_CHAR_AS_MULTIBYTE (from_byte);
3018 /* Some compilers can't handle this inside the switch. */
3019 temp = SYNTAX (c);
3020
3021 /* Check TEMP here so that if the char has
3022 a syntax-table property which says it is NOT
3023 a string character, it does not end the string. */
3024 if (nofence && c == state.instring && temp == Sstring)
3025 break;
3026
3027 switch (temp)
3028 {
3029 case Sstring_fence:
3030 if (!nofence) goto string_end;
3031 break;
3032 case Scharquote:
3033 case Sescape:
3034 INC_FROM;
3035 startquotedinstring:
3036 if (from >= end) goto endquoted;
3037 }
3038 INC_FROM;
3039 }
3040 }
3041 string_end:
3042 state.instring = -1;
3043 curlevel->prev = curlevel->last;
3044 INC_FROM;
3045 if (boundary_stop) goto done;
3046 break;
3047
3048 case Smath:
3049 /* FIXME: We should do something with it. */
3050 break;
3051 default:
3052 /* Ignore whitespace, punctuation, quote, endcomment. */
3053 break;
3054 }
3055 }
3056 goto done;
3057
3058 stop: /* Here if stopping before start of sexp. */
3059 from = prev_from; /* We have just fetched the char that starts it; */
3060 goto done; /* but return the position before it. */
3061
3062 endquoted:
3063 state.quoted = 1;
3064 done:
3065 state.depth = depth;
3066 state.mindepth = mindepth;
3067 state.thislevelstart = curlevel->prev;
3068 state.prevlevelstart
3069 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
3070 state.location = from;
3071 state.levelstarts = Qnil;
3072 while (--curlevel >= levelstart)
3073 state.levelstarts = Fcons (make_number (curlevel->last),
3074 state.levelstarts);
3075 immediate_quit = 0;
3076
3077 *stateptr = state;
3078 }
3079
3080 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
3081 doc: /* Parse Lisp syntax starting at FROM until TO; return status of parse at TO.
3082 Parsing stops at TO or when certain criteria are met;
3083 point is set to where parsing stops.
3084 If fifth arg OLDSTATE is omitted or nil,
3085 parsing assumes that FROM is the beginning of a function.
3086 Value is a list of ten elements describing final state of parsing:
3087 0. depth in parens.
3088 1. character address of start of innermost containing list; nil if none.
3089 2. character address of start of last complete sexp terminated.
3090 3. non-nil if inside a string.
3091 (it is the character that will terminate the string,
3092 or t if the string should be terminated by a generic string delimiter.)
3093 4. nil if outside a comment, t if inside a non-nestable comment,
3094 else an integer (the current comment nesting).
3095 5. t if following a quote character.
3096 6. the minimum paren-depth encountered during this scan.
3097 7. t if in a comment of style b; symbol `syntax-table' if the comment
3098 should be terminated by a generic comment delimiter.
3099 8. character address of start of comment or string; nil if not in one.
3100 9. Intermediate data for continuation of parsing (subject to change).
3101 If third arg TARGETDEPTH is non-nil, parsing stops if the depth
3102 in parentheses becomes equal to TARGETDEPTH.
3103 Fourth arg STOPBEFORE non-nil means stop when come to
3104 any character that starts a sexp.
3105 Fifth arg OLDSTATE is a nine-element list like what this function returns.
3106 It is used to initialize the state of the parse. Elements number 1, 2, 6
3107 and 8 are ignored; you can leave off element 8 (the last) entirely.
3108 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment.
3109 If it is symbol `syntax-table', stop after the start of a comment or a
3110 string, or after end of a comment or a string. */)
3111 (from, to, targetdepth, stopbefore, oldstate, commentstop)
3112 Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop;
3113 {
3114 struct lisp_parse_state state;
3115 int target;
3116
3117 if (!NILP (targetdepth))
3118 {
3119 CHECK_NUMBER (targetdepth);
3120 target = XINT (targetdepth);
3121 }
3122 else
3123 target = -100000; /* We won't reach this depth */
3124
3125 validate_region (&from, &to);
3126 scan_sexps_forward (&state, XINT (from), CHAR_TO_BYTE (XINT (from)),
3127 XINT (to),
3128 target, !NILP (stopbefore), oldstate,
3129 (NILP (commentstop)
3130 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1)));
3131
3132 SET_PT (state.location);
3133
3134 return Fcons (make_number (state.depth),
3135 Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart),
3136 Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart),
3137 Fcons (state.instring >= 0
3138 ? (state.instring == ST_STRING_STYLE
3139 ? Qt : make_number (state.instring)) : Qnil,
3140 Fcons (state.incomment < 0 ? Qt :
3141 (state.incomment == 0 ? Qnil :
3142 make_number (state.incomment)),
3143 Fcons (state.quoted ? Qt : Qnil,
3144 Fcons (make_number (state.mindepth),
3145 Fcons ((state.comstyle
3146 ? (state.comstyle == ST_COMMENT_STYLE
3147 ? Qsyntax_table : Qt) :
3148 Qnil),
3149 Fcons (((state.incomment
3150 || (state.instring >= 0))
3151 ? make_number (state.comstr_start)
3152 : Qnil),
3153 Fcons (state.levelstarts, Qnil))))))))));
3154 }
3155 \f
3156 void
3157 init_syntax_once ()
3158 {
3159 register int i, c;
3160 Lisp_Object temp;
3161
3162 /* This has to be done here, before we call Fmake_char_table. */
3163 Qsyntax_table = intern ("syntax-table");
3164 staticpro (&Qsyntax_table);
3165
3166 /* Intern this now in case it isn't already done.
3167 Setting this variable twice is harmless.
3168 But don't staticpro it here--that is done in alloc.c. */
3169 Qchar_table_extra_slots = intern ("char-table-extra-slots");
3170
3171 /* Create objects which can be shared among syntax tables. */
3172 Vsyntax_code_object = Fmake_vector (make_number (Smax), Qnil);
3173 for (i = 0; i < XVECTOR (Vsyntax_code_object)->size; i++)
3174 XVECTOR (Vsyntax_code_object)->contents[i]
3175 = Fcons (make_number (i), Qnil);
3176
3177 /* Now we are ready to set up this property, so we can
3178 create syntax tables. */
3179 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
3180
3181 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Swhitespace];
3182
3183 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
3184
3185 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3186 for (i = 'a'; i <= 'z'; i++)
3187 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3188 for (i = 'A'; i <= 'Z'; i++)
3189 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3190 for (i = '0'; i <= '9'; i++)
3191 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
3192
3193 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
3194 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
3195
3196 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
3197 Fcons (make_number (Sopen), make_number (')')));
3198 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
3199 Fcons (make_number (Sclose), make_number ('(')));
3200 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
3201 Fcons (make_number (Sopen), make_number (']')));
3202 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
3203 Fcons (make_number (Sclose), make_number ('[')));
3204 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
3205 Fcons (make_number (Sopen), make_number ('}')));
3206 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
3207 Fcons (make_number (Sclose), make_number ('{')));
3208 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
3209 Fcons (make_number ((int) Sstring), Qnil));
3210 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
3211 Fcons (make_number ((int) Sescape), Qnil));
3212
3213 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Ssymbol];
3214 for (i = 0; i < 10; i++)
3215 {
3216 c = "_-+*/&|<>="[i];
3217 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3218 }
3219
3220 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Spunct];
3221 for (i = 0; i < 12; i++)
3222 {
3223 c = ".,;:?!#@~^'`"[i];
3224 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, c, temp);
3225 }
3226
3227 /* All multibyte characters have syntax `word' by default. */
3228 temp = XVECTOR (Vsyntax_code_object)->contents[(int) Sword];
3229 char_table_set_range (Vstandard_syntax_table, 0x80, MAX_CHAR, temp);
3230 }
3231
3232 void
3233 syms_of_syntax ()
3234 {
3235 Qsyntax_table_p = intern ("syntax-table-p");
3236 staticpro (&Qsyntax_table_p);
3237
3238 staticpro (&Vsyntax_code_object);
3239
3240 Qscan_error = intern ("scan-error");
3241 staticpro (&Qscan_error);
3242 Fput (Qscan_error, Qerror_conditions,
3243 Fcons (Qscan_error, Fcons (Qerror, Qnil)));
3244 Fput (Qscan_error, Qerror_message,
3245 build_string ("Scan error"));
3246
3247 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
3248 doc: /* Non-nil means `forward-sexp', etc., should treat comments as whitespace. */);
3249
3250 DEFVAR_BOOL ("parse-sexp-lookup-properties", &parse_sexp_lookup_properties,
3251 doc: /* Non-nil means `forward-sexp', etc., obey `syntax-table' property.
3252 Otherwise, that text property is simply ignored.
3253 See the info node `(elisp)Syntax Properties' for a description of the
3254 `syntax-table' property. */);
3255
3256 words_include_escapes = 0;
3257 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
3258 doc: /* Non-nil means `forward-word', etc., should treat escape chars part of words. */);
3259
3260 DEFVAR_BOOL ("multibyte-syntax-as-symbol", &multibyte_syntax_as_symbol,
3261 doc: /* Non-nil means `scan-sexps' treats all multibyte characters as symbol. */);
3262 multibyte_syntax_as_symbol = 0;
3263
3264 DEFVAR_BOOL ("open-paren-in-column-0-is-defun-start",
3265 &open_paren_in_column_0_is_defun_start,
3266 doc: /* *Non-nil means an open paren in column 0 denotes the start of a defun. */);
3267 open_paren_in_column_0_is_defun_start = 1;
3268
3269
3270 DEFVAR_LISP ("find-word-boundary-function-table",
3271 &Vfind_word_boundary_function_table,
3272 doc: /*
3273 Char table of functions to search for the word boundary.
3274 Each function is called with two arguments; POS and LIMIT.
3275 POS and LIMIT are character positions in the current buffer.
3276
3277 If POS is less than LIMIT, POS is at the first character of a word,
3278 and the return value of a function is a position after the last
3279 character of that word.
3280
3281 If POS is not less than LIMIT, POS is at the last character of a word,
3282 and the return value of a function is a position at the first
3283 character of that word.
3284
3285 In both cases, LIMIT bounds the search. */);
3286 Vfind_word_boundary_function_table = Fmake_char_table (Qnil, Qnil);
3287
3288 defsubr (&Ssyntax_table_p);
3289 defsubr (&Ssyntax_table);
3290 defsubr (&Sstandard_syntax_table);
3291 defsubr (&Scopy_syntax_table);
3292 defsubr (&Sset_syntax_table);
3293 defsubr (&Schar_syntax);
3294 defsubr (&Smatching_paren);
3295 defsubr (&Sstring_to_syntax);
3296 defsubr (&Smodify_syntax_entry);
3297 defsubr (&Sinternal_describe_syntax_value);
3298
3299 defsubr (&Sforward_word);
3300
3301 defsubr (&Sskip_chars_forward);
3302 defsubr (&Sskip_chars_backward);
3303 defsubr (&Sskip_syntax_forward);
3304 defsubr (&Sskip_syntax_backward);
3305
3306 defsubr (&Sforward_comment);
3307 defsubr (&Sscan_lists);
3308 defsubr (&Sscan_sexps);
3309 defsubr (&Sbackward_prefix_chars);
3310 defsubr (&Sparse_partial_sexp);
3311 }