(check_syntax_table): Check the purpose slot.
[bpt/emacs.git] / src / syntax.c
1 /* GNU Emacs routines to deal with syntax tables; also word and list parsing.
2 Copyright (C) 1985, 1987, 1993, 1994, 1995 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21 #include <config.h>
22 #include <ctype.h>
23 #include "lisp.h"
24 #include "commands.h"
25 #include "buffer.h"
26 #include "syntax.h"
27
28 Lisp_Object Qsyntax_table_p, Qsyntax_table;
29
30 static void scan_sexps_forward ();
31 static int char_quoted ();
32
33 int words_include_escapes;
34
35 /* Used as a temporary in SYNTAX_ENTRY and other macros in syntax.h,
36 if not compiled with GCC. No need to mark it, since it is used
37 only very temporarily. */
38 Lisp_Object syntax_temp;
39
40 /* This is the internal form of the parse state used in parse-partial-sexp. */
41
42 struct lisp_parse_state
43 {
44 int depth; /* Depth at end of parsing */
45 int instring; /* -1 if not within string, else desired terminator. */
46 int incomment; /* Nonzero if within a comment at end of parsing */
47 int comstyle; /* comment style a=0, or b=1 */
48 int quoted; /* Nonzero if just after an escape char at end of parsing */
49 int thislevelstart; /* Char number of most recent start-of-expression at current level */
50 int prevlevelstart; /* Char number of start of containing expression */
51 int location; /* Char number at which parsing stopped. */
52 int mindepth; /* Minimum depth seen while scanning. */
53 int comstart; /* Position just after last comment starter. */
54 };
55 \f
56 /* These variables are a cache for finding the start of a defun.
57 find_start_pos is the place for which the defun start was found.
58 find_start_value is the defun start position found for it.
59 find_start_buffer is the buffer it was found in.
60 find_start_begv is the BEGV value when it was found.
61 find_start_modiff is the value of MODIFF when it was found. */
62
63 static int find_start_pos;
64 static int find_start_value;
65 static struct buffer *find_start_buffer;
66 static int find_start_begv;
67 static int find_start_modiff;
68
69 /* Find a defun-start that is the last one before POS (or nearly the last).
70 We record what we find, so that another call in the same area
71 can return the same value right away. */
72
73 static int
74 find_defun_start (pos)
75 int pos;
76 {
77 int tem;
78 int shortage;
79
80 /* Use previous finding, if it's valid and applies to this inquiry. */
81 if (current_buffer == find_start_buffer
82 /* Reuse the defun-start even if POS is a little farther on.
83 POS might be in the next defun, but that's ok.
84 Our value may not be the best possible, but will still be usable. */
85 && pos <= find_start_pos + 1000
86 && pos >= find_start_value
87 && BEGV == find_start_begv
88 && MODIFF == find_start_modiff)
89 return find_start_value;
90
91 /* Back up to start of line. */
92 tem = scan_buffer ('\n', pos, BEGV, -1, &shortage, 1);
93
94 while (tem > BEGV)
95 {
96 /* Open-paren at start of line means we found our defun-start. */
97 if (SYNTAX (FETCH_CHAR (tem)) == Sopen)
98 break;
99 /* Move to beg of previous line. */
100 tem = scan_buffer ('\n', tem, BEGV, -2, &shortage, 1);
101 }
102
103 /* Record what we found, for the next try. */
104 find_start_value = tem;
105 find_start_buffer = current_buffer;
106 find_start_modiff = MODIFF;
107 find_start_begv = BEGV;
108 find_start_pos = pos;
109
110 return find_start_value;
111 }
112 \f
113 DEFUN ("syntax-table-p", Fsyntax_table_p, Ssyntax_table_p, 1, 1, 0,
114 "Return t if ARG is a syntax table.\n\
115 Currently, any char-table counts as a syntax table.")
116 (obj)
117 Lisp_Object obj;
118 {
119 if (CHAR_TABLE_P (obj)
120 && XCHAR_TABLE (obj)->purpose == Qsyntax_table)
121 return Qt;
122 return Qnil;
123 }
124
125 static void
126 check_syntax_table (obj)
127 Lisp_Object obj;
128 {
129 if (!(CHAR_TABLE_P (obj)
130 && XCHAR_TABLE (obj)->purpose == Qsyntax_table))
131 wrong_type_argument (Qsyntax_table_p, obj);
132 }
133
134 DEFUN ("syntax-table", Fsyntax_table, Ssyntax_table, 0, 0, 0,
135 "Return the current syntax table.\n\
136 This is the one specified by the current buffer.")
137 ()
138 {
139 return current_buffer->syntax_table;
140 }
141
142 DEFUN ("standard-syntax-table", Fstandard_syntax_table,
143 Sstandard_syntax_table, 0, 0, 0,
144 "Return the standard syntax table.\n\
145 This is the one used for new buffers.")
146 ()
147 {
148 return Vstandard_syntax_table;
149 }
150
151 DEFUN ("copy-syntax-table", Fcopy_syntax_table, Scopy_syntax_table, 0, 1, 0,
152 "Construct a new syntax table and return it.\n\
153 It is a copy of the TABLE, which defaults to the standard syntax table.")
154 (table)
155 Lisp_Object table;
156 {
157 Lisp_Object copy;
158
159 if (!NILP (table))
160 check_syntax_table (table);
161 else
162 table = Vstandard_syntax_table;
163
164 copy = Fcopy_sequence (table);
165 Fset_char_table_parent (copy, Vstandard_syntax_table);
166 return copy;
167 }
168
169 DEFUN ("set-syntax-table", Fset_syntax_table, Sset_syntax_table, 1, 1, 0,
170 "Select a new syntax table for the current buffer.\n\
171 One argument, a syntax table.")
172 (table)
173 Lisp_Object table;
174 {
175 check_syntax_table (table);
176 current_buffer->syntax_table = table;
177 /* Indicate that this buffer now has a specified syntax table. */
178 current_buffer->local_var_flags
179 |= XFASTINT (buffer_local_flags.syntax_table);
180 return table;
181 }
182 \f
183 /* Convert a letter which signifies a syntax code
184 into the code it signifies.
185 This is used by modify-syntax-entry, and other things. */
186
187 unsigned char syntax_spec_code[0400] =
188 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
189 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
190 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
191 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
192 (char) Swhitespace, 0377, (char) Sstring, 0377,
193 (char) Smath, 0377, 0377, (char) Squote,
194 (char) Sopen, (char) Sclose, 0377, 0377,
195 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote,
196 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
197 0377, 0377, 0377, 0377,
198 (char) Scomment, 0377, (char) Sendcomment, 0377,
199 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */
200 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
201 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
202 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol,
203 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */
204 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377,
205 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword,
206 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377
207 };
208
209 /* Indexed by syntax code, give the letter that describes it. */
210
211 char syntax_code_spec[14] =
212 {
213 ' ', '.', 'w', '_', '(', ')', '\'', '\"', '$', '\\', '/', '<', '>', '@'
214 };
215 \f
216 /* Look up the value for CHARACTER in syntax table TABLE's parent
217 and its parents. SYNTAX_ENTRY calls this, when TABLE itself has nil
218 for CHARACTER. It's actually used only when not compiled with GCC. */
219
220 Lisp_Object
221 syntax_parent_lookup (table, character)
222 Lisp_Object table;
223 int character;
224 {
225 Lisp_Object value;
226
227 while (1)
228 {
229 table = XCHAR_TABLE (table)->parent;
230 if (NILP (table))
231 return Qnil;
232
233 value = XCHAR_TABLE (table)->contents[character];
234 if (!NILP (value))
235 return value;
236 }
237 }
238
239 DEFUN ("char-syntax", Fchar_syntax, Schar_syntax, 1, 1, 0,
240 "Return the syntax code of CHAR, described by a character.\n\
241 For example, if CHAR is a word constituent, the character `?w' is returned.\n\
242 The characters that correspond to various syntax codes\n\
243 are listed in the documentation of `modify-syntax-entry'.")
244 (ch)
245 Lisp_Object ch;
246 {
247 int char_int;
248 CHECK_NUMBER (ch, 0);
249 char_int = XINT (ch);
250 return make_number (syntax_code_spec[(int) SYNTAX (char_int)]);
251 }
252
253 DEFUN ("matching-paren", Fmatching_paren, Smatching_paren, 1, 1, 0,
254 "Return the matching parenthesis of CHAR, or nil if none.")
255 (ch)
256 Lisp_Object ch;
257 {
258 int char_int, code;
259 CHECK_NUMBER (ch, 0);
260 char_int = XINT (ch);
261 code = SYNTAX (char_int);
262 if (code == Sopen || code == Sclose)
263 return make_number (SYNTAX_MATCH (char_int));
264 return Qnil;
265 }
266
267 /* This comment supplies the doc string for modify-syntax-entry,
268 for make-docfile to see. We cannot put this in the real DEFUN
269 due to limits in the Unix cpp.
270
271 DEFUN ("modify-syntax-entry", foo, bar, 2, 3, 0,
272 "Set syntax for character CHAR according to string S.\n\
273 The syntax is changed only for table TABLE, which defaults to\n\
274 the current buffer's syntax table.\n\
275 The first character of S should be one of the following:\n\
276 Space or - whitespace syntax. w word constituent.\n\
277 _ symbol constituent. . punctuation.\n\
278 ( open-parenthesis. ) close-parenthesis.\n\
279 \" string quote. \\ escape.\n\
280 $ paired delimiter. ' expression quote or prefix operator.\n\
281 < comment starter. > comment ender.\n\
282 / character-quote. @ inherit from `standard-syntax-table'.\n\
283 \n\
284 Only single-character comment start and end sequences are represented thus.\n\
285 Two-character sequences are represented as described below.\n\
286 The second character of S is the matching parenthesis,\n\
287 used only if the first character is `(' or `)'.\n\
288 Any additional characters are flags.\n\
289 Defined flags are the characters 1, 2, 3, 4, b, and p.\n\
290 1 means C is the start of a two-char comment start sequence.\n\
291 2 means C is the second character of such a sequence.\n\
292 3 means C is the start of a two-char comment end sequence.\n\
293 4 means C is the second character of such a sequence.\n\
294 \n\
295 There can be up to two orthogonal comment sequences. This is to support\n\
296 language modes such as C++. By default, all comment sequences are of style\n\
297 a, but you can set the comment sequence style to b (on the second character\n\
298 of a comment-start, or the first character of a comment-end sequence) using\n\
299 this flag:\n\
300 b means C is part of comment sequence b.\n\
301 \n\
302 p means C is a prefix character for `backward-prefix-chars';\n\
303 such characters are treated as whitespace when they occur\n\
304 between expressions.")
305 (char, s, table)
306 */
307
308 DEFUN ("modify-syntax-entry", Fmodify_syntax_entry, Smodify_syntax_entry, 2, 3,
309 /* I really don't know why this is interactive
310 help-form should at least be made useful whilst reading the second arg
311 */
312 "cSet syntax for character: \nsSet syntax for %s to: ",
313 0 /* See immediately above */)
314 (c, newentry, syntax_table)
315 Lisp_Object c, newentry, syntax_table;
316 {
317 register unsigned char *p;
318 register enum syntaxcode code;
319 int val;
320 Lisp_Object match;
321
322 CHECK_NUMBER (c, 0);
323 CHECK_STRING (newentry, 1);
324
325 if (NILP (syntax_table))
326 syntax_table = current_buffer->syntax_table;
327 else
328 check_syntax_table (syntax_table);
329
330 p = XSTRING (newentry)->data;
331 code = (enum syntaxcode) syntax_spec_code[*p++];
332 if (((int) code & 0377) == 0377)
333 error ("invalid syntax description letter: %c", c);
334
335 if (code == Sinherit)
336 {
337 SET_RAW_SYNTAX_ENTRY (syntax_table, c, Qnil);
338 return Qnil;
339 }
340
341 if (*p)
342 {
343 XSETINT (match, *p++);
344 if (XFASTINT (match) == ' ')
345 match = Qnil;
346 }
347 else
348 match = Qnil;
349
350 val = (int) code;
351 while (*p)
352 switch (*p++)
353 {
354 case '1':
355 val |= 1 << 16;
356 break;
357
358 case '2':
359 val |= 1 << 17;
360 break;
361
362 case '3':
363 val |= 1 << 18;
364 break;
365
366 case '4':
367 val |= 1 << 19;
368 break;
369
370 case 'p':
371 val |= 1 << 20;
372 break;
373
374 case 'b':
375 val |= 1 << 21;
376 break;
377 }
378
379 SET_RAW_SYNTAX_ENTRY (syntax_table, c,
380 Fcons (make_number (val), match));
381
382 return Qnil;
383 }
384 \f
385 /* Dump syntax table to buffer in human-readable format */
386
387 static void
388 describe_syntax (value)
389 Lisp_Object value;
390 {
391 register enum syntaxcode code;
392 char desc, match, start1, start2, end1, end2, prefix, comstyle;
393 char str[2];
394 Lisp_Object first, match_lisp;
395
396 Findent_to (make_number (16), make_number (1));
397
398 if (NILP (value))
399 {
400 insert_string ("inherit");
401 return;
402 }
403
404 if (!CONSP (value))
405 {
406 insert_string ("invalid");
407 return;
408 }
409
410 first = XCONS (value)->car;
411 match_lisp = XCONS (value)->cdr;
412
413 if (!INTEGERP (first) || !(NILP (match_lisp) || INTEGERP (match_lisp)))
414 {
415 insert_string ("invalid");
416 return;
417 }
418
419 code = (enum syntaxcode) (first & 0377);
420 start1 = (XINT (first) >> 16) & 1;
421 start2 = (XINT (first) >> 17) & 1;
422 end1 = (XINT (first) >> 18) & 1;
423 end2 = (XINT (first) >> 19) & 1;
424 prefix = (XINT (first) >> 20) & 1;
425 comstyle = (XINT (first) >> 21) & 1;
426
427 if ((int) code < 0 || (int) code >= (int) Smax)
428 {
429 insert_string ("invalid");
430 return;
431 }
432 desc = syntax_code_spec[(int) code];
433
434 str[0] = desc, str[1] = 0;
435 insert (str, 1);
436
437 str[0] = !NILP (match_lisp) ? XINT (match_lisp) : ' ';
438 insert (str, 1);
439
440 if (start1)
441 insert ("1", 1);
442 if (start2)
443 insert ("2", 1);
444
445 if (end1)
446 insert ("3", 1);
447 if (end2)
448 insert ("4", 1);
449
450 if (prefix)
451 insert ("p", 1);
452 if (comstyle)
453 insert ("b", 1);
454
455 insert_string ("\twhich means: ");
456
457 switch (SWITCH_ENUM_CAST (code))
458 {
459 case Swhitespace:
460 insert_string ("whitespace"); break;
461 case Spunct:
462 insert_string ("punctuation"); break;
463 case Sword:
464 insert_string ("word"); break;
465 case Ssymbol:
466 insert_string ("symbol"); break;
467 case Sopen:
468 insert_string ("open"); break;
469 case Sclose:
470 insert_string ("close"); break;
471 case Squote:
472 insert_string ("quote"); break;
473 case Sstring:
474 insert_string ("string"); break;
475 case Smath:
476 insert_string ("math"); break;
477 case Sescape:
478 insert_string ("escape"); break;
479 case Scharquote:
480 insert_string ("charquote"); break;
481 case Scomment:
482 insert_string ("comment"); break;
483 case Sendcomment:
484 insert_string ("endcomment"); break;
485 default:
486 insert_string ("invalid");
487 return;
488 }
489
490 if (!NILP (match_lisp))
491 {
492 insert_string (", matches ");
493 insert_char (XINT (match_lisp));
494 }
495
496 if (start1)
497 insert_string (",\n\t is the first character of a comment-start sequence");
498 if (start2)
499 insert_string (",\n\t is the second character of a comment-start sequence");
500
501 if (end1)
502 insert_string (",\n\t is the first character of a comment-end sequence");
503 if (end2)
504 insert_string (",\n\t is the second character of a comment-end sequence");
505 if (comstyle)
506 insert_string (" (comment style b)");
507
508 if (prefix)
509 insert_string (",\n\t is a prefix character for `backward-prefix-chars'");
510
511 insert_string ("\n");
512 }
513
514 static Lisp_Object
515 describe_syntax_1 (vector)
516 Lisp_Object vector;
517 {
518 struct buffer *old = current_buffer;
519 set_buffer_internal (XBUFFER (Vstandard_output));
520 describe_vector (vector, Qnil, describe_syntax, 0, Qnil, Qnil);
521 call0 (intern ("help-mode"));
522 set_buffer_internal (old);
523 return Qnil;
524 }
525
526 DEFUN ("describe-syntax", Fdescribe_syntax, Sdescribe_syntax, 0, 0, "",
527 "Describe the syntax specifications in the syntax table.\n\
528 The descriptions are inserted in a buffer, which is then displayed.")
529 ()
530 {
531 internal_with_output_to_temp_buffer
532 ("*Help*", describe_syntax_1, current_buffer->syntax_table);
533
534 return Qnil;
535 }
536 \f
537 /* Return the position across COUNT words from FROM.
538 If that many words cannot be found before the end of the buffer, return 0.
539 COUNT negative means scan backward and stop at word beginning. */
540
541 scan_words (from, count)
542 register int from, count;
543 {
544 register int beg = BEGV;
545 register int end = ZV;
546 register int code;
547 int charcode;
548
549 immediate_quit = 1;
550 QUIT;
551
552 while (count > 0)
553 {
554 while (1)
555 {
556 if (from == end)
557 {
558 immediate_quit = 0;
559 return 0;
560 }
561 charcode = FETCH_CHAR (from);
562 code = SYNTAX (charcode);
563 if (words_include_escapes
564 && (code == Sescape || code == Scharquote))
565 break;
566 if (code == Sword)
567 break;
568 from++;
569 }
570 while (1)
571 {
572 if (from == end) break;
573 charcode = FETCH_CHAR (from);
574 code = SYNTAX (charcode);
575 if (!(words_include_escapes
576 && (code == Sescape || code == Scharquote)))
577 if (code != Sword)
578 break;
579 from++;
580 }
581 count--;
582 }
583 while (count < 0)
584 {
585 while (1)
586 {
587 if (from == beg)
588 {
589 immediate_quit = 0;
590 return 0;
591 }
592 charcode = FETCH_CHAR (from - 1);
593 code = SYNTAX (charcode);
594 if (words_include_escapes
595 && (code == Sescape || code == Scharquote))
596 break;
597 if (code == Sword)
598 break;
599 from--;
600 }
601 while (1)
602 {
603 if (from == beg) break;
604 charcode = FETCH_CHAR (from - 1);
605 code = SYNTAX (charcode);
606 if (!(words_include_escapes
607 && (code == Sescape || code == Scharquote)))
608 if (code != Sword)
609 break;
610 from--;
611 }
612 count++;
613 }
614
615 immediate_quit = 0;
616
617 return from;
618 }
619
620 DEFUN ("forward-word", Fforward_word, Sforward_word, 1, 1, "p",
621 "Move point forward ARG words (backward if ARG is negative).\n\
622 Normally returns t.\n\
623 If an edge of the buffer is reached, point is left there\n\
624 and nil is returned.")
625 (count)
626 Lisp_Object count;
627 {
628 int val;
629 CHECK_NUMBER (count, 0);
630
631 if (!(val = scan_words (point, XINT (count))))
632 {
633 SET_PT (XINT (count) > 0 ? ZV : BEGV);
634 return Qnil;
635 }
636 SET_PT (val);
637 return Qt;
638 }
639 \f
640 DEFUN ("forward-comment", Fforward_comment, Sforward_comment, 1, 1, 0,
641 "Move forward across up to N comments. If N is negative, move backward.\n\
642 Stop scanning if we find something other than a comment or whitespace.\n\
643 Set point to where scanning stops.\n\
644 If N comments are found as expected, with nothing except whitespace\n\
645 between them, return t; otherwise return nil.")
646 (count)
647 Lisp_Object count;
648 {
649 register int from;
650 register int stop;
651 register int c, c1;
652 register enum syntaxcode code;
653 int comstyle = 0; /* style of comment encountered */
654 int found;
655 int count1;
656
657 CHECK_NUMBER (count, 0);
658 count1 = XINT (count);
659
660 immediate_quit = 1;
661 QUIT;
662
663 from = PT;
664
665 while (count1 > 0)
666 {
667 stop = ZV;
668 do
669 {
670 if (from == stop)
671 {
672 SET_PT (from);
673 immediate_quit = 0;
674 return Qnil;
675 }
676 c = FETCH_CHAR (from);
677 code = SYNTAX (c);
678 from++;
679 comstyle = 0;
680 if (from < stop && SYNTAX_COMSTART_FIRST (c)
681 && (c1 = FETCH_CHAR (from),
682 SYNTAX_COMSTART_SECOND (c1)))
683 {
684 /* We have encountered a comment start sequence and we
685 are ignoring all text inside comments. We must record
686 the comment style this sequence begins so that later,
687 only a comment end of the same style actually ends
688 the comment section. */
689 code = Scomment;
690 comstyle = SYNTAX_COMMENT_STYLE (c1);
691 from++;
692 }
693 }
694 while (code == Swhitespace || code == Sendcomment);
695 if (code != Scomment)
696 {
697 immediate_quit = 0;
698 SET_PT (from - 1);
699 return Qnil;
700 }
701 /* We're at the start of a comment. */
702 while (1)
703 {
704 if (from == stop)
705 {
706 immediate_quit = 0;
707 SET_PT (from);
708 return Qnil;
709 }
710 c = FETCH_CHAR (from);
711 from++;
712 if (SYNTAX (c) == Sendcomment
713 && SYNTAX_COMMENT_STYLE (c) == comstyle)
714 /* we have encountered a comment end of the same style
715 as the comment sequence which began this comment
716 section */
717 break;
718 if (from < stop && SYNTAX_COMEND_FIRST (c)
719 && (c1 = FETCH_CHAR (from),
720 SYNTAX_COMEND_SECOND (c1))
721 && SYNTAX_COMMENT_STYLE (c) == comstyle)
722 /* we have encountered a comment end of the same style
723 as the comment sequence which began this comment
724 section */
725 { from++; break; }
726 }
727 /* We have skipped one comment. */
728 count1--;
729 }
730
731 while (count1 < 0)
732 {
733 stop = BEGV;
734 while (from > stop)
735 {
736 int quoted;
737
738 from--;
739 quoted = char_quoted (from);
740 if (quoted)
741 from--;
742 c = FETCH_CHAR (from);
743 code = SYNTAX (c);
744 comstyle = 0;
745 if (code == Sendcomment)
746 comstyle = SYNTAX_COMMENT_STYLE (c);
747 if (from > stop && SYNTAX_COMEND_SECOND (c)
748 && (c1 = FETCH_CHAR (from - 1),
749 SYNTAX_COMEND_FIRST (c1))
750 && !char_quoted (from - 1))
751 {
752 /* We must record the comment style encountered so that
753 later, we can match only the proper comment begin
754 sequence of the same style. */
755 code = Sendcomment;
756 comstyle = SYNTAX_COMMENT_STYLE (c1);
757 from--;
758 }
759
760 if (code == Sendcomment && !quoted)
761 {
762 #if 0
763 if (code != SYNTAX (c))
764 /* For a two-char comment ender, we can assume
765 it does end a comment. So scan back in a simple way. */
766 {
767 if (from != stop) from--;
768 while (1)
769 {
770 if ((c = FETCH_CHAR (from),
771 SYNTAX (c) == Scomment)
772 && SYNTAX_COMMENT_STYLE (c) == comstyle)
773 break;
774 if (from == stop)
775 {
776 immediate_quit = 0;
777 SET_PT (from);
778 return Qnil;
779 }
780 from--;
781 if (SYNTAX_COMSTART_SECOND (c)
782 && (c1 = FETCH_CHAR (from),
783 SYNTAX_COMSTART_FIRST (c1))
784 && SYNTAX_COMMENT_STYLE (c) == comstyle
785 && !char_quoted (from))
786 break;
787 }
788 break;
789 }
790 #endif /* 0 */
791
792 /* Look back, counting the parity of string-quotes,
793 and recording the comment-starters seen.
794 When we reach a safe place, assume that's not in a string;
795 then step the main scan to the earliest comment-starter seen
796 an even number of string quotes away from the safe place.
797
798 OFROM[I] is position of the earliest comment-starter seen
799 which is I+2X quotes from the comment-end.
800 PARITY is current parity of quotes from the comment end. */
801 {
802 int parity = 0;
803 char my_stringend = 0;
804 int string_lossage = 0;
805 int comment_end = from;
806 int comstart_pos = 0;
807 int comstart_parity = 0;
808 int scanstart = from - 1;
809
810 /* At beginning of range to scan, we're outside of strings;
811 that determines quote parity to the comment-end. */
812 while (from != stop)
813 {
814 /* Move back and examine a character. */
815 from--;
816
817 c = FETCH_CHAR (from);
818 code = SYNTAX (c);
819
820 /* If this char is the second of a 2-char comment sequence,
821 back up and give the pair the appropriate syntax. */
822 if (from > stop && SYNTAX_COMEND_SECOND (c)
823 && SYNTAX_COMEND_FIRST (FETCH_CHAR (from - 1)))
824 {
825 code = Sendcomment;
826 from--;
827 c = FETCH_CHAR (from);
828 }
829
830 /* If this char starts a 2-char comment start sequence,
831 treat it like a 1-char comment starter. */
832 if (from < scanstart && SYNTAX_COMSTART_FIRST (c)
833 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from + 1))
834 && comstyle == SYNTAX_COMMENT_STYLE (FETCH_CHAR (from + 1)))
835 code = Scomment;
836
837 /* Ignore escaped characters. */
838 if (char_quoted (from))
839 continue;
840
841 /* Track parity of quotes. */
842 if (code == Sstring)
843 {
844 parity ^= 1;
845 if (my_stringend == 0)
846 my_stringend = c;
847 /* If we have two kinds of string delimiters.
848 There's no way to grok this scanning backwards. */
849 else if (my_stringend != c)
850 string_lossage = 1;
851 }
852
853 /* Record comment-starters according to that
854 quote-parity to the comment-end. */
855 if (code == Scomment)
856 {
857 comstart_parity = parity;
858 comstart_pos = from;
859 }
860
861 /* If we find another earlier comment-ender,
862 any comment-starts earlier than that don't count
863 (because they go with the earlier comment-ender). */
864 if (code == Sendcomment
865 && SYNTAX_COMMENT_STYLE (FETCH_CHAR (from)) == comstyle)
866 break;
867
868 /* Assume a defun-start point is outside of strings. */
869 if (code == Sopen
870 && (from == stop || FETCH_CHAR (from - 1) == '\n'))
871 break;
872 }
873
874 if (comstart_pos == 0)
875 from = comment_end;
876 /* If the earliest comment starter
877 is followed by uniform paired string quotes or none,
878 we know it can't be inside a string
879 since if it were then the comment ender would be inside one.
880 So it does start a comment. Skip back to it. */
881 else if (comstart_parity == 0 && !string_lossage)
882 from = comstart_pos;
883 else
884 {
885 /* We had two kinds of string delimiters mixed up
886 together. Decode this going forwards.
887 Scan fwd from the previous comment ender
888 to the one in question; this records where we
889 last passed a comment starter. */
890 struct lisp_parse_state state;
891 scan_sexps_forward (&state, find_defun_start (comment_end),
892 comment_end - 1, -10000, 0, Qnil, 0);
893 if (state.incomment)
894 from = state.comstart;
895 else
896 /* We can't grok this as a comment; scan it normally. */
897 from = comment_end;
898 }
899 }
900 /* We have skipped one comment. */
901 break;
902 }
903 else if ((code != Swhitespace && code != Scomment) || quoted)
904 {
905 immediate_quit = 0;
906 SET_PT (from + 1);
907 return Qnil;
908 }
909 }
910
911 count1++;
912 }
913
914 SET_PT (from);
915 immediate_quit = 0;
916 return Qt;
917 }
918 \f
919 int parse_sexp_ignore_comments;
920
921 Lisp_Object
922 scan_lists (from, count, depth, sexpflag)
923 register int from;
924 int count, depth, sexpflag;
925 {
926 Lisp_Object val;
927 register int stop;
928 register int c;
929 unsigned char stringterm;
930 int quoted;
931 int mathexit = 0;
932 register enum syntaxcode code;
933 int min_depth = depth; /* Err out if depth gets less than this. */
934 int comstyle = 0; /* style of comment encountered */
935
936 if (depth > 0) min_depth = 0;
937
938 immediate_quit = 1;
939 QUIT;
940
941 while (count > 0)
942 {
943 stop = ZV;
944 while (from < stop)
945 {
946 c = FETCH_CHAR (from);
947 code = SYNTAX (c);
948 from++;
949 if (from < stop && SYNTAX_COMSTART_FIRST (c)
950 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from))
951 && parse_sexp_ignore_comments)
952 {
953 /* we have encountered a comment start sequence and we
954 are ignoring all text inside comments. we must record
955 the comment style this sequence begins so that later,
956 only a comment end of the same style actually ends
957 the comment section */
958 code = Scomment;
959 comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from));
960 from++;
961 }
962
963 if (SYNTAX_PREFIX (c))
964 continue;
965
966 switch (SWITCH_ENUM_CAST (code))
967 {
968 case Sescape:
969 case Scharquote:
970 if (from == stop) goto lose;
971 from++;
972 /* treat following character as a word constituent */
973 case Sword:
974 case Ssymbol:
975 if (depth || !sexpflag) break;
976 /* This word counts as a sexp; return at end of it. */
977 while (from < stop)
978 {
979 switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from))))
980 {
981 case Scharquote:
982 case Sescape:
983 from++;
984 if (from == stop) goto lose;
985 break;
986 case Sword:
987 case Ssymbol:
988 case Squote:
989 break;
990 default:
991 goto done;
992 }
993 from++;
994 }
995 goto done;
996
997 case Scomment:
998 if (!parse_sexp_ignore_comments) break;
999 while (1)
1000 {
1001 if (from == stop)
1002 {
1003 if (depth == 0)
1004 goto done;
1005 goto lose;
1006 }
1007 c = FETCH_CHAR (from);
1008 if (SYNTAX (c) == Sendcomment
1009 && SYNTAX_COMMENT_STYLE (c) == comstyle)
1010 /* we have encountered a comment end of the same style
1011 as the comment sequence which began this comment
1012 section */
1013 break;
1014 from++;
1015 if (from < stop && SYNTAX_COMEND_FIRST (c)
1016 && SYNTAX_COMEND_SECOND (FETCH_CHAR (from))
1017 && SYNTAX_COMMENT_STYLE (c) == comstyle)
1018 /* we have encountered a comment end of the same style
1019 as the comment sequence which began this comment
1020 section */
1021 { from++; break; }
1022 }
1023 break;
1024
1025 case Smath:
1026 if (!sexpflag)
1027 break;
1028 if (from != stop && c == FETCH_CHAR (from))
1029 from++;
1030 if (mathexit)
1031 {
1032 mathexit = 0;
1033 goto close1;
1034 }
1035 mathexit = 1;
1036
1037 case Sopen:
1038 if (!++depth) goto done;
1039 break;
1040
1041 case Sclose:
1042 close1:
1043 if (!--depth) goto done;
1044 if (depth < min_depth)
1045 error ("Containing expression ends prematurely");
1046 break;
1047
1048 case Sstring:
1049 stringterm = FETCH_CHAR (from - 1);
1050 while (1)
1051 {
1052 if (from >= stop) goto lose;
1053 if (FETCH_CHAR (from) == stringterm) break;
1054 switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from))))
1055 {
1056 case Scharquote:
1057 case Sescape:
1058 from++;
1059 }
1060 from++;
1061 }
1062 from++;
1063 if (!depth && sexpflag) goto done;
1064 break;
1065 }
1066 }
1067
1068 /* Reached end of buffer. Error if within object, return nil if between */
1069 if (depth) goto lose;
1070
1071 immediate_quit = 0;
1072 return Qnil;
1073
1074 /* End of object reached */
1075 done:
1076 count--;
1077 }
1078
1079 while (count < 0)
1080 {
1081 stop = BEGV;
1082 while (from > stop)
1083 {
1084 from--;
1085 if (quoted = char_quoted (from))
1086 from--;
1087 c = FETCH_CHAR (from);
1088 code = SYNTAX (c);
1089 comstyle = 0;
1090 if (code == Sendcomment)
1091 comstyle = SYNTAX_COMMENT_STYLE (c);
1092 if (from > stop && SYNTAX_COMEND_SECOND (c)
1093 && SYNTAX_COMEND_FIRST (FETCH_CHAR (from - 1))
1094 && !char_quoted (from - 1)
1095 && parse_sexp_ignore_comments)
1096 {
1097 /* we must record the comment style encountered so that
1098 later, we can match only the proper comment begin
1099 sequence of the same style */
1100 code = Sendcomment;
1101 comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from - 1));
1102 from--;
1103 }
1104
1105 if (SYNTAX_PREFIX (c))
1106 continue;
1107
1108 switch (SWITCH_ENUM_CAST (quoted ? Sword : code))
1109 {
1110 case Sword:
1111 case Ssymbol:
1112 if (depth || !sexpflag) break;
1113 /* This word counts as a sexp; count object finished after passing it. */
1114 while (from > stop)
1115 {
1116 quoted = char_quoted (from - 1);
1117 if (quoted)
1118 from--;
1119 if (! (quoted || SYNTAX (FETCH_CHAR (from - 1)) == Sword
1120 || SYNTAX (FETCH_CHAR (from - 1)) == Ssymbol
1121 || SYNTAX (FETCH_CHAR (from - 1)) == Squote))
1122 goto done2;
1123 from--;
1124 }
1125 goto done2;
1126
1127 case Smath:
1128 if (!sexpflag)
1129 break;
1130 if (from != stop && c == FETCH_CHAR (from - 1))
1131 from--;
1132 if (mathexit)
1133 {
1134 mathexit = 0;
1135 goto open2;
1136 }
1137 mathexit = 1;
1138
1139 case Sclose:
1140 if (!++depth) goto done2;
1141 break;
1142
1143 case Sopen:
1144 open2:
1145 if (!--depth) goto done2;
1146 if (depth < min_depth)
1147 error ("Containing expression ends prematurely");
1148 break;
1149
1150 case Sendcomment:
1151 if (!parse_sexp_ignore_comments)
1152 break;
1153 #if 0
1154 if (code != SYNTAX (c))
1155 /* For a two-char comment ender, we can assume
1156 it does end a comment. So scan back in a simple way. */
1157 {
1158 if (from != stop) from--;
1159 while (1)
1160 {
1161 if (SYNTAX (c = FETCH_CHAR (from)) == Scomment
1162 && SYNTAX_COMMENT_STYLE (c) == comstyle)
1163 break;
1164 if (from == stop)
1165 {
1166 if (depth == 0)
1167 goto done2;
1168 goto lose;
1169 }
1170 from--;
1171 if (SYNTAX_COMSTART_SECOND (c)
1172 && SYNTAX_COMSTART_FIRST (FETCH_CHAR (from))
1173 && SYNTAX_COMMENT_STYLE (c) == comstyle
1174 && !char_quoted (from))
1175 break;
1176 }
1177 break;
1178 }
1179 #endif /* 0 */
1180
1181 /* Look back, counting the parity of string-quotes,
1182 and recording the comment-starters seen.
1183 When we reach a safe place, assume that's not in a string;
1184 then step the main scan to the earliest comment-starter seen
1185 an even number of string quotes away from the safe place.
1186
1187 OFROM[I] is position of the earliest comment-starter seen
1188 which is I+2X quotes from the comment-end.
1189 PARITY is current parity of quotes from the comment end. */
1190 {
1191 int parity = 0;
1192 char my_stringend = 0;
1193 int string_lossage = 0;
1194 int comment_end = from;
1195 int comstart_pos = 0;
1196 int comstart_parity = 0;
1197 int scanstart = from - 1;
1198
1199 /* At beginning of range to scan, we're outside of strings;
1200 that determines quote parity to the comment-end. */
1201 while (from != stop)
1202 {
1203 /* Move back and examine a character. */
1204 from--;
1205
1206 c = FETCH_CHAR (from);
1207 code = SYNTAX (c);
1208
1209 /* If this char is the second of a 2-char comment sequence,
1210 back up and give the pair the appropriate syntax. */
1211 if (from > stop && SYNTAX_COMEND_SECOND (c)
1212 && SYNTAX_COMEND_FIRST (FETCH_CHAR (from - 1)))
1213 {
1214 code = Sendcomment;
1215 from--;
1216 c = FETCH_CHAR (from);
1217 }
1218
1219 /* If this char starts a 2-char comment start sequence,
1220 treat it like a 1-char comment starter. */
1221 if (from < scanstart && SYNTAX_COMSTART_FIRST (c)
1222 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from + 1))
1223 && comstyle == SYNTAX_COMMENT_STYLE (FETCH_CHAR (from + 1)))
1224 code = Scomment;
1225
1226 /* Ignore escaped characters. */
1227 if (char_quoted (from))
1228 continue;
1229
1230 /* Track parity of quotes. */
1231 if (code == Sstring)
1232 {
1233 parity ^= 1;
1234 if (my_stringend == 0)
1235 my_stringend = c;
1236 /* If we have two kinds of string delimiters.
1237 There's no way to grok this scanning backwards. */
1238 else if (my_stringend != c)
1239 string_lossage = 1;
1240 }
1241
1242 /* Record comment-starters according to that
1243 quote-parity to the comment-end. */
1244 if (code == Scomment)
1245 {
1246 comstart_parity = parity;
1247 comstart_pos = from;
1248 }
1249
1250 /* If we find another earlier comment-ender,
1251 any comment-starts earlier than that don't count
1252 (because they go with the earlier comment-ender). */
1253 if (code == Sendcomment
1254 && SYNTAX_COMMENT_STYLE (FETCH_CHAR (from)) == comstyle)
1255 break;
1256
1257 /* Assume a defun-start point is outside of strings. */
1258 if (code == Sopen
1259 && (from == stop || FETCH_CHAR (from - 1) == '\n'))
1260 break;
1261 }
1262
1263 if (comstart_pos == 0)
1264 from = comment_end;
1265 /* If the earliest comment starter
1266 is followed by uniform paired string quotes or none,
1267 we know it can't be inside a string
1268 since if it were then the comment ender would be inside one.
1269 So it does start a comment. Skip back to it. */
1270 else if (comstart_parity == 0 && !string_lossage)
1271 from = comstart_pos;
1272 else
1273 {
1274 /* We had two kinds of string delimiters mixed up
1275 together. Decode this going forwards.
1276 Scan fwd from the previous comment ender
1277 to the one in question; this records where we
1278 last passed a comment starter. */
1279 struct lisp_parse_state state;
1280 scan_sexps_forward (&state, find_defun_start (comment_end),
1281 comment_end - 1, -10000, 0, Qnil, 0);
1282 if (state.incomment)
1283 from = state.comstart;
1284 else
1285 /* We can't grok this as a comment; scan it normally. */
1286 from = comment_end;
1287 }
1288 }
1289 break;
1290
1291 case Sstring:
1292 stringterm = FETCH_CHAR (from);
1293 while (1)
1294 {
1295 if (from == stop) goto lose;
1296 if (!char_quoted (from - 1)
1297 && stringterm == FETCH_CHAR (from - 1))
1298 break;
1299 from--;
1300 }
1301 from--;
1302 if (!depth && sexpflag) goto done2;
1303 break;
1304 }
1305 }
1306
1307 /* Reached start of buffer. Error if within object, return nil if between */
1308 if (depth) goto lose;
1309
1310 immediate_quit = 0;
1311 return Qnil;
1312
1313 done2:
1314 count++;
1315 }
1316
1317
1318 immediate_quit = 0;
1319 XSETFASTINT (val, from);
1320 return val;
1321
1322 lose:
1323 error ("Unbalanced parentheses");
1324 /* NOTREACHED */
1325 }
1326
1327 static int
1328 char_quoted (pos)
1329 register int pos;
1330 {
1331 register enum syntaxcode code;
1332 register int beg = BEGV;
1333 register int quoted = 0;
1334
1335 while (pos > beg
1336 && ((code = SYNTAX (FETCH_CHAR (pos - 1))) == Scharquote
1337 || code == Sescape))
1338 pos--, quoted = !quoted;
1339 return quoted;
1340 }
1341
1342 DEFUN ("scan-lists", Fscan_lists, Sscan_lists, 3, 3, 0,
1343 "Scan from character number FROM by COUNT lists.\n\
1344 Returns the character number of the position thus found.\n\
1345 \n\
1346 If DEPTH is nonzero, paren depth begins counting from that value,\n\
1347 only places where the depth in parentheses becomes zero\n\
1348 are candidates for stopping; COUNT such places are counted.\n\
1349 Thus, a positive value for DEPTH means go out levels.\n\
1350 \n\
1351 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
1352 \n\
1353 If the beginning or end of (the accessible part of) the buffer is reached\n\
1354 and the depth is wrong, an error is signaled.\n\
1355 If the depth is right but the count is not used up, nil is returned.")
1356 (from, count, depth)
1357 Lisp_Object from, count, depth;
1358 {
1359 CHECK_NUMBER (from, 0);
1360 CHECK_NUMBER (count, 1);
1361 CHECK_NUMBER (depth, 2);
1362
1363 return scan_lists (XINT (from), XINT (count), XINT (depth), 0);
1364 }
1365
1366 DEFUN ("scan-sexps", Fscan_sexps, Sscan_sexps, 2, 2, 0,
1367 "Scan from character number FROM by COUNT balanced expressions.\n\
1368 If COUNT is negative, scan backwards.\n\
1369 Returns the character number of the position thus found.\n\
1370 \n\
1371 Comments are ignored if `parse-sexp-ignore-comments' is non-nil.\n\
1372 \n\
1373 If the beginning or end of (the accessible part of) the buffer is reached\n\
1374 in the middle of a parenthetical grouping, an error is signaled.\n\
1375 If the beginning or end is reached between groupings\n\
1376 but before count is used up, nil is returned.")
1377 (from, count)
1378 Lisp_Object from, count;
1379 {
1380 CHECK_NUMBER (from, 0);
1381 CHECK_NUMBER (count, 1);
1382
1383 return scan_lists (XINT (from), XINT (count), 0, 1);
1384 }
1385
1386 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, Sbackward_prefix_chars,
1387 0, 0, 0,
1388 "Move point backward over any number of chars with prefix syntax.\n\
1389 This includes chars with \"quote\" or \"prefix\" syntax (' or p).")
1390 ()
1391 {
1392 int beg = BEGV;
1393 int pos = point;
1394
1395 while (pos > beg && !char_quoted (pos - 1)
1396 && (SYNTAX (FETCH_CHAR (pos - 1)) == Squote
1397 || SYNTAX_PREFIX (FETCH_CHAR (pos - 1))))
1398 pos--;
1399
1400 SET_PT (pos);
1401
1402 return Qnil;
1403 }
1404 \f
1405 /* Parse forward from FROM to END,
1406 assuming that FROM has state OLDSTATE (nil means FROM is start of function),
1407 and return a description of the state of the parse at END.
1408 If STOPBEFORE is nonzero, stop at the start of an atom.
1409 If COMMENTSTOP is nonzero, stop at the start of a comment. */
1410
1411 static void
1412 scan_sexps_forward (stateptr, from, end, targetdepth,
1413 stopbefore, oldstate, commentstop)
1414 struct lisp_parse_state *stateptr;
1415 register int from;
1416 int end, targetdepth, stopbefore;
1417 Lisp_Object oldstate;
1418 int commentstop;
1419 {
1420 struct lisp_parse_state state;
1421
1422 register enum syntaxcode code;
1423 struct level { int last, prev; };
1424 struct level levelstart[100];
1425 register struct level *curlevel = levelstart;
1426 struct level *endlevel = levelstart + 100;
1427 char prev;
1428 register int depth; /* Paren depth of current scanning location.
1429 level - levelstart equals this except
1430 when the depth becomes negative. */
1431 int mindepth; /* Lowest DEPTH value seen. */
1432 int start_quoted = 0; /* Nonzero means starting after a char quote */
1433 Lisp_Object tem;
1434
1435 immediate_quit = 1;
1436 QUIT;
1437
1438 if (NILP (oldstate))
1439 {
1440 depth = 0;
1441 state.instring = -1;
1442 state.incomment = 0;
1443 state.comstyle = 0; /* comment style a by default */
1444 }
1445 else
1446 {
1447 tem = Fcar (oldstate);
1448 if (!NILP (tem))
1449 depth = XINT (tem);
1450 else
1451 depth = 0;
1452
1453 oldstate = Fcdr (oldstate);
1454 oldstate = Fcdr (oldstate);
1455 oldstate = Fcdr (oldstate);
1456 tem = Fcar (oldstate);
1457 state.instring = !NILP (tem) ? XINT (tem) : -1;
1458
1459 oldstate = Fcdr (oldstate);
1460 tem = Fcar (oldstate);
1461 state.incomment = !NILP (tem);
1462
1463 oldstate = Fcdr (oldstate);
1464 tem = Fcar (oldstate);
1465 start_quoted = !NILP (tem);
1466
1467 /* if the eight element of the list is nil, we are in comment
1468 style a. if it is non-nil, we are in comment style b */
1469 oldstate = Fcdr (oldstate);
1470 oldstate = Fcdr (oldstate);
1471 tem = Fcar (oldstate);
1472 state.comstyle = !NILP (tem);
1473 }
1474 state.quoted = 0;
1475 mindepth = depth;
1476
1477 curlevel->prev = -1;
1478 curlevel->last = -1;
1479
1480 /* Enter the loop at a place appropriate for initial state. */
1481
1482 if (state.incomment) goto startincomment;
1483 if (state.instring >= 0)
1484 {
1485 if (start_quoted) goto startquotedinstring;
1486 goto startinstring;
1487 }
1488 if (start_quoted) goto startquoted;
1489
1490 while (from < end)
1491 {
1492 code = SYNTAX (FETCH_CHAR (from));
1493 from++;
1494 if (code == Scomment)
1495 state.comstart = from-1;
1496
1497 else if (from < end && SYNTAX_COMSTART_FIRST (FETCH_CHAR (from - 1))
1498 && SYNTAX_COMSTART_SECOND (FETCH_CHAR (from)))
1499 {
1500 /* Record the comment style we have entered so that only
1501 the comment-end sequence of the same style actually
1502 terminates the comment section. */
1503 code = Scomment;
1504 state.comstyle = SYNTAX_COMMENT_STYLE (FETCH_CHAR (from));
1505 state.comstart = from-1;
1506 from++;
1507 }
1508
1509 if (SYNTAX_PREFIX (FETCH_CHAR (from - 1)))
1510 continue;
1511 switch (SWITCH_ENUM_CAST (code))
1512 {
1513 case Sescape:
1514 case Scharquote:
1515 if (stopbefore) goto stop; /* this arg means stop at sexp start */
1516 curlevel->last = from - 1;
1517 startquoted:
1518 if (from == end) goto endquoted;
1519 from++;
1520 goto symstarted;
1521 /* treat following character as a word constituent */
1522 case Sword:
1523 case Ssymbol:
1524 if (stopbefore) goto stop; /* this arg means stop at sexp start */
1525 curlevel->last = from - 1;
1526 symstarted:
1527 while (from < end)
1528 {
1529 switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from))))
1530 {
1531 case Scharquote:
1532 case Sescape:
1533 from++;
1534 if (from == end) goto endquoted;
1535 break;
1536 case Sword:
1537 case Ssymbol:
1538 case Squote:
1539 break;
1540 default:
1541 goto symdone;
1542 }
1543 from++;
1544 }
1545 symdone:
1546 curlevel->prev = curlevel->last;
1547 break;
1548
1549 startincomment:
1550 if (commentstop)
1551 goto done;
1552 if (from != BEGV)
1553 {
1554 /* Enter the loop in the middle so that we find
1555 a 2-char comment ender if we start in the middle of it. */
1556 prev = FETCH_CHAR (from - 1);
1557 goto startincomment_1;
1558 }
1559 /* At beginning of buffer, enter the loop the ordinary way. */
1560
1561 case Scomment:
1562 state.incomment = 1;
1563 if (commentstop)
1564 goto done;
1565 while (1)
1566 {
1567 if (from == end) goto done;
1568 prev = FETCH_CHAR (from);
1569 if (SYNTAX (prev) == Sendcomment
1570 && SYNTAX_COMMENT_STYLE (prev) == state.comstyle)
1571 /* Only terminate the comment section if the endcomment
1572 of the same style as the start sequence has been
1573 encountered. */
1574 break;
1575 from++;
1576 startincomment_1:
1577 if (from < end && SYNTAX_COMEND_FIRST (prev)
1578 && SYNTAX_COMEND_SECOND (FETCH_CHAR (from))
1579 && SYNTAX_COMMENT_STYLE (prev) == state.comstyle)
1580 /* Only terminate the comment section if the end-comment
1581 sequence of the same style as the start sequence has
1582 been encountered. */
1583 { from++; break; }
1584 }
1585 state.incomment = 0;
1586 state.comstyle = 0; /* reset the comment style */
1587 break;
1588
1589 case Sopen:
1590 if (stopbefore) goto stop; /* this arg means stop at sexp start */
1591 depth++;
1592 /* curlevel++->last ran into compiler bug on Apollo */
1593 curlevel->last = from - 1;
1594 if (++curlevel == endlevel)
1595 error ("Nesting too deep for parser");
1596 curlevel->prev = -1;
1597 curlevel->last = -1;
1598 if (targetdepth == depth) goto done;
1599 break;
1600
1601 case Sclose:
1602 depth--;
1603 if (depth < mindepth)
1604 mindepth = depth;
1605 if (curlevel != levelstart)
1606 curlevel--;
1607 curlevel->prev = curlevel->last;
1608 if (targetdepth == depth) goto done;
1609 break;
1610
1611 case Sstring:
1612 if (stopbefore) goto stop; /* this arg means stop at sexp start */
1613 curlevel->last = from - 1;
1614 state.instring = FETCH_CHAR (from - 1);
1615 startinstring:
1616 while (1)
1617 {
1618 if (from >= end) goto done;
1619 if (FETCH_CHAR (from) == state.instring) break;
1620 switch (SWITCH_ENUM_CAST (SYNTAX (FETCH_CHAR (from))))
1621 {
1622 case Scharquote:
1623 case Sescape:
1624 from++;
1625 startquotedinstring:
1626 if (from >= end) goto endquoted;
1627 }
1628 from++;
1629 }
1630 state.instring = -1;
1631 curlevel->prev = curlevel->last;
1632 from++;
1633 break;
1634
1635 case Smath:
1636 break;
1637 }
1638 }
1639 goto done;
1640
1641 stop: /* Here if stopping before start of sexp. */
1642 from--; /* We have just fetched the char that starts it; */
1643 goto done; /* but return the position before it. */
1644
1645 endquoted:
1646 state.quoted = 1;
1647 done:
1648 state.depth = depth;
1649 state.mindepth = mindepth;
1650 state.thislevelstart = curlevel->prev;
1651 state.prevlevelstart
1652 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last;
1653 state.location = from;
1654 immediate_quit = 0;
1655
1656 *stateptr = state;
1657 }
1658
1659 /* This comment supplies the doc string for parse-partial-sexp,
1660 for make-docfile to see. We cannot put this in the real DEFUN
1661 due to limits in the Unix cpp.
1662
1663 DEFUN ("parse-partial-sexp", Ffoo, Sfoo, 2, 6, 0,
1664 "Parse Lisp syntax starting at FROM until TO; return status of parse at TO.\n\
1665 Parsing stops at TO or when certain criteria are met;\n\
1666 point is set to where parsing stops.\n\
1667 If fifth arg STATE is omitted or nil,\n\
1668 parsing assumes that FROM is the beginning of a function.\n\
1669 Value is a list of eight elements describing final state of parsing:\n\
1670 0. depth in parens.\n\
1671 1. character address of start of innermost containing list; nil if none.\n\
1672 2. character address of start of last complete sexp terminated.\n\
1673 3. non-nil if inside a string.\n\
1674 (it is the character that will terminate the string.)\n\
1675 4. t if inside a comment.\n\
1676 5. t if following a quote character.\n\
1677 6. the minimum paren-depth encountered during this scan.\n\
1678 7. t if in a comment of style `b'.\n\
1679 If third arg TARGETDEPTH is non-nil, parsing stops if the depth\n\
1680 in parentheses becomes equal to TARGETDEPTH.\n\
1681 Fourth arg STOPBEFORE non-nil means stop when come to\n\
1682 any character that starts a sexp.\n\
1683 Fifth arg STATE is an eight-list like what this function returns.\n\
1684 It is used to initialize the state of the parse. Its second and third
1685 elements are ignored.
1686 Sixth args COMMENTSTOP non-nil means stop at the start of a comment.")
1687 (from, to, targetdepth, stopbefore, state, commentstop)
1688 */
1689
1690 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, Sparse_partial_sexp, 2, 6, 0,
1691 0 /* See immediately above */)
1692 (from, to, targetdepth, stopbefore, oldstate, commentstop)
1693 Lisp_Object from, to, targetdepth, stopbefore, oldstate, commentstop;
1694 {
1695 struct lisp_parse_state state;
1696 int target;
1697
1698 if (!NILP (targetdepth))
1699 {
1700 CHECK_NUMBER (targetdepth, 3);
1701 target = XINT (targetdepth);
1702 }
1703 else
1704 target = -100000; /* We won't reach this depth */
1705
1706 validate_region (&from, &to);
1707 scan_sexps_forward (&state, XINT (from), XINT (to),
1708 target, !NILP (stopbefore), oldstate,
1709 !NILP (commentstop));
1710
1711 SET_PT (state.location);
1712
1713 return Fcons (make_number (state.depth),
1714 Fcons (state.prevlevelstart < 0 ? Qnil : make_number (state.prevlevelstart),
1715 Fcons (state.thislevelstart < 0 ? Qnil : make_number (state.thislevelstart),
1716 Fcons (state.instring >= 0 ? make_number (state.instring) : Qnil,
1717 Fcons (state.incomment ? Qt : Qnil,
1718 Fcons (state.quoted ? Qt : Qnil,
1719 Fcons (make_number (state.mindepth),
1720 Fcons (state.comstyle ? Qt : Qnil,
1721 Qnil))))))));
1722 }
1723 \f
1724 init_syntax_once ()
1725 {
1726 register int i;
1727 Lisp_Object temp;
1728
1729 /* This has to be done here, before we call Fmake_char_table. */
1730 Qsyntax_table = intern ("syntax-table");
1731 staticpro (&Qsyntax_table);
1732
1733 /* Intern this now in case it isn't already done.
1734 Setting this variable twice is harmless.
1735 But don't staticpro it here--that is done in alloc.c. */
1736 Qchar_table_extra_slots = intern ("char-table-extra-slots");
1737
1738 /* Now we are ready to set up this property, so we can
1739 create syntax tables. */
1740 Fput (Qsyntax_table, Qchar_table_extra_slots, make_number (0));
1741
1742 temp = Fcons (make_number ((int) Swhitespace), Qnil);
1743
1744 Vstandard_syntax_table = Fmake_char_table (Qsyntax_table, temp);
1745
1746 temp = Fcons (make_number ((int) Sword), Qnil);
1747 for (i = 'a'; i <= 'z'; i++)
1748 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
1749 for (i = 'A'; i <= 'Z'; i++)
1750 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
1751 for (i = '0'; i <= '9'; i++)
1752 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, i, temp);
1753
1754 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '$', temp);
1755 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '%', temp);
1756
1757 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '(',
1758 Fcons (make_number (Sopen), make_number (')')));
1759 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ')',
1760 Fcons (make_number (Sclose), make_number ('(')));
1761 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '[',
1762 Fcons (make_number (Sopen), make_number (']')));
1763 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ']',
1764 Fcons (make_number (Sclose), make_number ('[')));
1765 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '{',
1766 Fcons (make_number (Sopen), make_number ('}')));
1767 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '}',
1768 Fcons (make_number (Sclose), make_number ('{')));
1769 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '"',
1770 Fcons (make_number ((int) Sstring), Qnil));
1771 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, '\\',
1772 Fcons (make_number ((int) Sescape), Qnil));
1773
1774 temp = Fcons (make_number ((int) Ssymbol), Qnil);
1775 for (i = 0; i < 10; i++)
1776 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, "_-+*/&|<>="[i], temp);
1777
1778 temp = Fcons (make_number ((int) Spunct), Qnil);
1779 for (i = 0; i < 12; i++)
1780 SET_RAW_SYNTAX_ENTRY (Vstandard_syntax_table, ".,;:?!#@~^'`"[i], temp);
1781 }
1782
1783 syms_of_syntax ()
1784 {
1785 Qsyntax_table_p = intern ("syntax-table-p");
1786 staticpro (&Qsyntax_table_p);
1787
1788 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments,
1789 "Non-nil means `forward-sexp', etc., should treat comments as whitespace.");
1790
1791 words_include_escapes = 0;
1792 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes,
1793 "Non-nil means `forward-word', etc., should treat escape chars part of words.");
1794
1795 defsubr (&Ssyntax_table_p);
1796 defsubr (&Ssyntax_table);
1797 defsubr (&Sstandard_syntax_table);
1798 defsubr (&Scopy_syntax_table);
1799 defsubr (&Sset_syntax_table);
1800 defsubr (&Schar_syntax);
1801 defsubr (&Smatching_paren);
1802 defsubr (&Smodify_syntax_entry);
1803 defsubr (&Sdescribe_syntax);
1804
1805 defsubr (&Sforward_word);
1806
1807 defsubr (&Sforward_comment);
1808 defsubr (&Sscan_lists);
1809 defsubr (&Sscan_sexps);
1810 defsubr (&Sbackward_prefix_chars);
1811 defsubr (&Sparse_partial_sexp);
1812 }