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