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