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