Mercurial > hg > xemacs-beta
annotate src/syntax.c @ 5118:e0db3c197671 ben-lisp-object
merge up to latest default branch, doesn't compile yet
author | Ben Wing <ben@xemacs.org> |
---|---|
date | Sat, 26 Dec 2009 21:18:49 -0600 |
parents | aa5ed11f473b |
children | d1247f3cc363 |
rev | line source |
---|---|
428 | 1 /* XEmacs routines to deal with syntax tables; also word and list parsing. |
2 Copyright (C) 1985-1994 Free Software Foundation, Inc. | |
3 Copyright (C) 1995 Sun Microsystems, Inc. | |
1296 | 4 Copyright (C) 2001, 2002, 2003 Ben Wing. |
428 | 5 |
6 This file is part of XEmacs. | |
7 | |
8 XEmacs is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
10 Free Software Foundation; either version 2, or (at your option) any | |
11 later version. | |
12 | |
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
19 along with XEmacs; see the file COPYING. If not, write to | |
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
21 Boston, MA 02111-1307, USA. */ | |
22 | |
23 /* Synched up with: FSF 19.28. */ | |
24 | |
25 /* This file has been Mule-ized. */ | |
26 | |
27 #include <config.h> | |
28 #include "lisp.h" | |
29 | |
30 #include "buffer.h" | |
31 #include "syntax.h" | |
460 | 32 #include "extents.h" |
428 | 33 |
4710
3a87551bfeb5
Fixes for a number of minor warnings issued by gcc. See xemacs-patches message
Jerry James <james@xemacs.org>
parents:
4653
diff
changeset
|
34 #ifdef NEW_GC |
3a87551bfeb5
Fixes for a number of minor warnings issued by gcc. See xemacs-patches message
Jerry James <james@xemacs.org>
parents:
4653
diff
changeset
|
35 # define UNUSED_IF_NEW_GC(decl) UNUSED (decl) |
3a87551bfeb5
Fixes for a number of minor warnings issued by gcc. See xemacs-patches message
Jerry James <james@xemacs.org>
parents:
4653
diff
changeset
|
36 #else |
3a87551bfeb5
Fixes for a number of minor warnings issued by gcc. See xemacs-patches message
Jerry James <james@xemacs.org>
parents:
4653
diff
changeset
|
37 # define UNUSED_IF_NEW_GC(decl) decl |
3a87551bfeb5
Fixes for a number of minor warnings issued by gcc. See xemacs-patches message
Jerry James <james@xemacs.org>
parents:
4653
diff
changeset
|
38 #endif |
3a87551bfeb5
Fixes for a number of minor warnings issued by gcc. See xemacs-patches message
Jerry James <james@xemacs.org>
parents:
4653
diff
changeset
|
39 |
460 | 40 #define ST_COMMENT_STYLE 0x101 |
41 #define ST_STRING_STYLE 0x102 | |
42 | |
43 Lisp_Object Qsyntax_table; | |
44 int lookup_syntax_properties; | |
45 | |
428 | 46 Lisp_Object Qsyntax_table_p; |
47 | |
48 int words_include_escapes; | |
49 | |
50 int parse_sexp_ignore_comments; | |
51 | |
52 /* The following two variables are provided to tell additional information | |
53 to the regex routines. We do it this way rather than change the | |
54 arguments to re_search_2() in an attempt to maintain some call | |
55 compatibility with other versions of the regex code. */ | |
56 | |
57 /* Tell the regex routines not to QUIT. Normally there is a QUIT | |
58 each iteration in re_search_2(). */ | |
59 int no_quit_in_re_search; | |
60 | |
826 | 61 /* The standard syntax table is stored where it will automatically |
62 be used in all new buffers. */ | |
428 | 63 Lisp_Object Vstandard_syntax_table; |
64 | |
65 Lisp_Object Vsyntax_designator_chars_string; | |
66 | |
826 | 67 Lisp_Object Vtemp_table_for_use_updating_syntax_tables; |
68 | |
1296 | 69 /* A value that is guaranteed not be in a syntax table. */ |
70 Lisp_Object Vbogus_syntax_table_value; | |
71 | |
826 | 72 static void syntax_cache_table_was_changed (struct buffer *buf); |
73 | |
428 | 74 /* This is the internal form of the parse state used in parse-partial-sexp. */ |
75 | |
76 struct lisp_parse_state | |
77 { | |
78 int depth; /* Depth at end of parsing */ | |
867 | 79 Ichar instring; /* -1 if not within string, else desired terminator */ |
428 | 80 int incomment; /* Nonzero if within a comment at end of parsing */ |
460 | 81 int comstyle; /* comment style a=0, or b=1, or ST_COMMENT_STYLE */ |
428 | 82 int quoted; /* Nonzero if just after an escape char at end of |
83 parsing */ | |
665 | 84 Charbpos thislevelstart;/* Char number of most recent start-of-expression |
428 | 85 at current level */ |
665 | 86 Charbpos prevlevelstart;/* Char number of start of containing expression */ |
87 Charbpos location; /* Char number at which parsing stopped */ | |
428 | 88 int mindepth; /* Minimum depth seen while scanning */ |
826 | 89 Charbpos comstr_start;/* Position just after last comment/string starter */ |
90 Lisp_Object levelstarts;/* Char numbers of starts-of-expression | |
91 of levels (starting from outermost). */ | |
428 | 92 }; |
93 | |
94 /* These variables are a cache for finding the start of a defun. | |
95 find_start_pos is the place for which the defun start was found. | |
96 find_start_value is the defun start position found for it. | |
97 find_start_buffer is the buffer it was found in. | |
98 find_start_begv is the BEGV value when it was found. | |
99 find_start_modiff is the value of MODIFF when it was found. */ | |
100 | |
665 | 101 static Charbpos find_start_pos; |
102 static Charbpos find_start_value; | |
428 | 103 static struct buffer *find_start_buffer; |
665 | 104 static Charbpos find_start_begv; |
428 | 105 static int find_start_modiff; |
106 | |
107 /* Find a defun-start that is the last one before POS (or nearly the last). | |
108 We record what we find, so that another call in the same area | |
109 can return the same value right away. */ | |
110 | |
665 | 111 static Charbpos |
112 find_defun_start (struct buffer *buf, Charbpos pos) | |
428 | 113 { |
665 | 114 Charbpos tem; |
826 | 115 struct syntax_cache *scache; |
116 | |
428 | 117 /* Use previous finding, if it's valid and applies to this inquiry. */ |
118 if (buf == find_start_buffer | |
119 /* Reuse the defun-start even if POS is a little farther on. | |
120 POS might be in the next defun, but that's ok. | |
121 Our value may not be the best possible, but will still be usable. */ | |
122 && pos <= find_start_pos + 1000 | |
123 && pos >= find_start_value | |
124 && BUF_BEGV (buf) == find_start_begv | |
125 && BUF_MODIFF (buf) == find_start_modiff) | |
126 return find_start_value; | |
127 | |
128 /* Back up to start of line. */ | |
129 tem = find_next_newline (buf, pos, -1); | |
130 | |
826 | 131 scache = setup_buffer_syntax_cache (buf, tem, 1); |
428 | 132 while (tem > BUF_BEGV (buf)) |
133 { | |
826 | 134 UPDATE_SYNTAX_CACHE_BACKWARD (scache, tem); |
460 | 135 |
428 | 136 /* Open-paren at start of line means we found our defun-start. */ |
826 | 137 if (SYNTAX_FROM_CACHE (scache, BUF_FETCH_CHAR (buf, tem)) == Sopen) |
428 | 138 break; |
139 /* Move to beg of previous line. */ | |
140 tem = find_next_newline (buf, tem, -2); | |
141 } | |
142 | |
143 /* Record what we found, for the next try. */ | |
144 find_start_value = tem; | |
145 find_start_buffer = buf; | |
146 find_start_modiff = BUF_MODIFF (buf); | |
147 find_start_begv = BUF_BEGV (buf); | |
148 find_start_pos = pos; | |
149 | |
150 return find_start_value; | |
151 } | |
152 | |
153 DEFUN ("syntax-table-p", Fsyntax_table_p, 1, 1, 0, /* | |
444 | 154 Return t if OBJECT is a syntax table. |
428 | 155 */ |
444 | 156 (object)) |
428 | 157 { |
444 | 158 return (CHAR_TABLEP (object) |
159 && XCHAR_TABLE_TYPE (object) == CHAR_TABLE_TYPE_SYNTAX) | |
428 | 160 ? Qt : Qnil; |
161 } | |
162 | |
163 static Lisp_Object | |
164 check_syntax_table (Lisp_Object obj, Lisp_Object default_) | |
165 { | |
166 if (NILP (obj)) | |
167 obj = default_; | |
168 while (NILP (Fsyntax_table_p (obj))) | |
169 obj = wrong_type_argument (Qsyntax_table_p, obj); | |
170 return obj; | |
171 } | |
172 | |
173 DEFUN ("syntax-table", Fsyntax_table, 0, 1, 0, /* | |
174 Return the current syntax table. | |
175 This is the one specified by the current buffer, or by BUFFER if it | |
176 is non-nil. | |
177 */ | |
178 (buffer)) | |
179 { | |
180 return decode_buffer (buffer, 0)->syntax_table; | |
181 } | |
182 | |
826 | 183 #ifdef DEBUG_XEMACS |
184 | |
185 DEFUN ("mirror-syntax-table", Fmirror_syntax_table, 0, 1, 0, /* | |
186 Return the current mirror syntax table, for debugging purposes. | |
187 This is the one specified by the current buffer, or by BUFFER if it | |
188 is non-nil. | |
189 */ | |
190 (buffer)) | |
191 { | |
192 return decode_buffer (buffer, 0)->mirror_syntax_table; | |
193 } | |
194 | |
195 DEFUN ("syntax-cache-info", Fsyntax_cache_info, 0, 1, 0, /* | |
196 Return info about the syntax cache in BUFFER. | |
197 BUFFER defaults to the current buffer if nil. | |
198 */ | |
199 (buffer)) | |
200 { | |
201 struct buffer *buf = decode_buffer (buffer, 0); | |
202 struct syntax_cache *cache = buf->syntax_cache; | |
203 return list4 (cache->start, cache->end, make_int (cache->prev_change), | |
204 make_int (cache->next_change)); | |
205 } | |
206 | |
207 #endif /* DEBUG_XEMACS */ | |
208 | |
428 | 209 DEFUN ("standard-syntax-table", Fstandard_syntax_table, 0, 0, 0, /* |
210 Return the standard syntax table. | |
211 This is the one used for new buffers. | |
212 */ | |
213 ()) | |
214 { | |
215 return Vstandard_syntax_table; | |
216 } | |
217 | |
218 DEFUN ("copy-syntax-table", Fcopy_syntax_table, 0, 1, 0, /* | |
444 | 219 Return a new syntax table which is a copy of SYNTAX-TABLE. |
220 SYNTAX-TABLE defaults to the standard syntax table. | |
428 | 221 */ |
444 | 222 (syntax_table)) |
428 | 223 { |
224 if (NILP (Vstandard_syntax_table)) | |
225 return Fmake_char_table (Qsyntax); | |
226 | |
444 | 227 syntax_table = check_syntax_table (syntax_table, Vstandard_syntax_table); |
228 return Fcopy_char_table (syntax_table); | |
428 | 229 } |
230 | |
231 DEFUN ("set-syntax-table", Fset_syntax_table, 1, 2, 0, /* | |
444 | 232 Select SYNTAX-TABLE as the new syntax table for BUFFER. |
428 | 233 BUFFER defaults to the current buffer if omitted. |
234 */ | |
444 | 235 (syntax_table, buffer)) |
428 | 236 { |
237 struct buffer *buf = decode_buffer (buffer, 0); | |
444 | 238 syntax_table = check_syntax_table (syntax_table, Qnil); |
239 buf->syntax_table = syntax_table; | |
240 buf->mirror_syntax_table = XCHAR_TABLE (syntax_table)->mirror_table; | |
826 | 241 syntax_cache_table_was_changed (buf); |
428 | 242 /* Indicate that this buffer now has a specified syntax table. */ |
243 buf->local_var_flags |= XINT (buffer_local_flags.syntax_table); | |
444 | 244 return syntax_table; |
428 | 245 } |
3252 | 246 |
247 | |
428 | 248 |
3252 | 249 /* |
250 * Syntax caching | |
251 */ | |
252 | |
253 /* syntax_cache object implementation */ | |
254 | |
255 static const struct memory_description syntax_cache_description_1 [] = { | |
256 { XD_LISP_OBJECT, offsetof (struct syntax_cache, object) }, | |
257 { XD_LISP_OBJECT, offsetof (struct syntax_cache, buffer) }, | |
258 { XD_LISP_OBJECT, offsetof (struct syntax_cache, syntax_table) }, | |
259 { XD_LISP_OBJECT, offsetof (struct syntax_cache, mirror_table) }, | |
260 { XD_LISP_OBJECT, offsetof (struct syntax_cache, start) }, | |
261 { XD_LISP_OBJECT, offsetof (struct syntax_cache, end) }, | |
262 { XD_END } | |
263 }; | |
264 | |
265 #ifdef NEW_GC | |
5118
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents:
4759
diff
changeset
|
266 DEFINE_DUMPABLE_INTERNAL_LISP_OBJECT ("syntax-cache", syntax_cache, |
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents:
4759
diff
changeset
|
267 0, syntax_cache_description_1, |
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents:
4759
diff
changeset
|
268 Lisp_Syntax_Cache); |
3252 | 269 #else /* not NEW_GC */ |
270 | |
271 const struct sized_memory_description syntax_cache_description = { | |
272 sizeof (struct syntax_cache), | |
273 syntax_cache_description_1 | |
274 }; | |
275 #endif /* not NEW_GC */ | |
276 | |
277 /* static syntax cache utilities */ | |
278 | |
279 static void | |
280 syntax_cache_table_was_changed (struct buffer *buf) | |
281 { | |
282 struct syntax_cache *cache = buf->syntax_cache; | |
283 if (cache->no_syntax_table_prop) | |
284 { | |
285 cache->syntax_table = | |
286 BUFFER_SYNTAX_TABLE (buf); | |
287 cache->mirror_table = | |
288 BUFFER_MIRROR_SYNTAX_TABLE (buf); | |
289 } | |
290 } | |
291 | |
292 static void | |
293 reset_buffer_syntax_cache_range (struct syntax_cache *cache, | |
294 Lisp_Object buffer, int infinite) | |
295 { | |
296 Fset_marker (cache->start, make_int (1), buffer); | |
297 Fset_marker (cache->end, make_int (1), buffer); | |
298 Fset_marker_insertion_type (cache->start, Qt); | |
299 Fset_marker_insertion_type (cache->end, Qnil); | |
300 /* #### Should we "cache->no_syntax_table_prop = 1;" here? */ | |
301 /* #### Cf comment on INFINITE in init_syntax_cache. -- sjt */ | |
302 if (infinite) | |
303 { | |
304 cache->prev_change = EMACS_INT_MIN; | |
305 cache->next_change = EMACS_INT_MAX; | |
306 } | |
307 else | |
308 { | |
309 cache->prev_change = -1; | |
310 cache->next_change = -1; | |
311 } | |
312 } | |
826 | 313 |
314 static void | |
315 init_syntax_cache (struct syntax_cache *cache, Lisp_Object object, | |
316 struct buffer *buffer, int infinite) | |
317 { | |
318 xzero (*cache); | |
319 cache->object = object; | |
320 cache->buffer = buffer; | |
321 cache->no_syntax_table_prop = 1; | |
1296 | 322 cache->syntax_table = |
323 BUFFER_SYNTAX_TABLE (cache->buffer); | |
324 cache->mirror_table = | |
826 | 325 BUFFER_MIRROR_SYNTAX_TABLE (cache->buffer); |
326 cache->start = Qnil; | |
327 cache->end = Qnil; | |
3250 | 328 /* #### I'm not sure what INFINITE is for, but it's apparently needed by |
329 setup_syntax_cache(). It looks like it's supposed to guarantee that | |
330 the test for POS outside of cache-valid range will never succeed, so | |
331 that update_syntax_cache won't get called, but it's hard to be sure. | |
332 Cf reset_buffer_syntax_cache_range. -- sjt */ | |
826 | 333 if (infinite) |
334 { | |
335 cache->prev_change = EMACS_INT_MIN; | |
336 cache->next_change = EMACS_INT_MAX; | |
337 } | |
338 else | |
339 { | |
340 cache->prev_change = -1; | |
341 cache->next_change = -1; | |
342 } | |
343 } | |
344 | |
3252 | 345 /* external syntax cache API */ |
346 | |
3250 | 347 /* #### This function and associated logic still needs work, and especially |
348 documentation. */ | |
349 struct syntax_cache * /* return CACHE or the cache of OBJECT */ | |
350 setup_syntax_cache (struct syntax_cache *cache, /* syntax cache, may be NULL | |
351 if OBJECT is a buffer */ | |
352 Lisp_Object object, /* the object (if any) cache | |
353 is associated with */ | |
354 struct buffer *buffer, /* the buffer to use as source | |
355 of the syntax table */ | |
356 Charxpos from, /* initial position of cache */ | |
357 int count) /* direction? see code */ | |
826 | 358 { |
3250 | 359 /* If OBJECT is a buffer, use its cache. Initialize cache. Make it valid |
360 for the whole buffer if the syntax-table property is not being respected. | |
361 Else if OBJECT is not a buffer, initialize the cache passed in CACHE. | |
362 If the syntax-table property is being respected, update the cache. */ | |
826 | 363 if (BUFFERP (object)) |
3250 | 364 { |
365 cache = XBUFFER (object)->syntax_cache; | |
366 if (!lookup_syntax_properties) | |
367 reset_buffer_syntax_cache_range (cache, object, 1); | |
368 } | |
369 else | |
826 | 370 init_syntax_cache (cache, object, buffer, 0); |
371 if (lookup_syntax_properties) | |
372 { | |
373 if (count <= 0) | |
374 { | |
375 from--; | |
2167 | 376 from = buffer_or_string_clip_to_accessible_char (cache->object, |
826 | 377 from); |
378 } | |
379 if (!(from >= cache->prev_change && from < cache->next_change)) | |
380 update_syntax_cache (cache, from, count); | |
381 } | |
1296 | 382 #ifdef NOT_WORTH_THE_EFFORT |
383 update_mirror_syntax_if_dirty (cache->mirror_table); | |
384 #endif /* NOT_WORTH_THE_EFFORT */ | |
826 | 385 return cache; |
386 } | |
387 | |
388 struct syntax_cache * | |
389 setup_buffer_syntax_cache (struct buffer *buffer, Charxpos from, int count) | |
390 { | |
391 return setup_syntax_cache (NULL, wrap_buffer (buffer), buffer, from, count); | |
392 } | |
393 | |
460 | 394 /* |
395 Update syntax_cache to an appropriate setting for position POS | |
396 | |
397 The sign of COUNT gives the relative position of POS wrt the | |
398 previously valid interval. (not currently used) | |
399 | |
400 `syntax_cache.*_change' are the next and previous positions at | |
401 which syntax_code and c_s_t will need to be recalculated. | |
402 | |
3025 | 403 #### Currently this code uses `get-char-property', which will |
460 | 404 return the "last smallest" extent at a given position. In cases |
405 where overlapping extents are defined, this code will simply use | |
406 whatever is returned by get-char-property. | |
407 | |
408 It might be worth it at some point to merge provided syntax tables | |
826 | 409 outward to the current buffer (#### rewrite in English please?!). */ |
460 | 410 |
411 void | |
2286 | 412 update_syntax_cache (struct syntax_cache *cache, Charxpos cpos, |
413 int UNUSED (count)) | |
460 | 414 { |
415 Lisp_Object tmp_table; | |
826 | 416 Bytexpos pos; |
417 Bytexpos lim; | |
418 Bytexpos next, prev; | |
419 int at_begin = 0, at_end = 0; | |
460 | 420 |
826 | 421 if (NILP (cache->object)) |
422 return; | |
423 | |
424 pos = buffer_or_string_charxpos_to_bytexpos (cache->object, cpos); | |
425 | |
426 tmp_table = get_char_property (pos, Qsyntax_table, cache->object, | |
427 EXTENT_AT_AFTER, 0); | |
2506 | 428 lim = next_previous_single_property_change (pos, Qsyntax_table, |
429 cache->object, -1, 1, 0); | |
826 | 430 if (lim < 0) |
460 | 431 { |
826 | 432 next = buffer_or_string_absolute_end_byte (cache->object); |
433 at_begin = 1; | |
460 | 434 } |
826 | 435 else |
436 next = lim; | |
460 | 437 |
826 | 438 if (pos < buffer_or_string_absolute_end_byte (cache->object)) |
439 pos = next_bytexpos (cache->object, pos); | |
2506 | 440 lim = next_previous_single_property_change (pos, Qsyntax_table, |
441 cache->object, -1, 0, 0); | |
826 | 442 if (lim < 0) |
460 | 443 { |
826 | 444 prev = buffer_or_string_absolute_begin_byte (cache->object); |
445 at_end = 1; | |
460 | 446 } |
447 else | |
826 | 448 prev = lim; |
460 | 449 |
826 | 450 cache->prev_change = |
451 buffer_or_string_bytexpos_to_charxpos (cache->object, prev); | |
452 cache->next_change = | |
453 buffer_or_string_bytexpos_to_charxpos (cache->object, next); | |
460 | 454 |
826 | 455 if (BUFFERP (cache->object)) |
456 { | |
457 /* If we are at the beginning or end of buffer, check to see if there's | |
458 a zero-length `syntax-table' extent there (highly unlikely); if not, | |
459 then we can safely make the end closed, so it will take in newly | |
460 inserted text. (If such an extent is inserted, we will be informed | |
3250 | 461 through signal_syntax_cache_extent_changed().) */ |
826 | 462 Fset_marker (cache->start, make_int (cache->prev_change), cache->object); |
463 Fset_marker_insertion_type | |
464 (cache->start, | |
465 at_begin && NILP (extent_at (prev, cache->object, Qsyntax_table, | |
466 NULL, EXTENT_AT_AT, 0)) | |
467 ? Qnil : Qt); | |
468 Fset_marker (cache->end, make_int (cache->next_change), cache->object); | |
469 Fset_marker_insertion_type | |
470 (cache->end, | |
471 at_end && NILP (extent_at (next, cache->object, Qsyntax_table, | |
472 NULL, EXTENT_AT_AT, 0)) | |
473 ? Qt : Qnil); | |
474 } | |
475 | |
476 if (!NILP (Fsyntax_table_p (tmp_table))) | |
477 { | |
478 cache->use_code = 0; | |
1296 | 479 cache->syntax_table = tmp_table; |
480 cache->mirror_table = XCHAR_TABLE (tmp_table)->mirror_table; | |
826 | 481 cache->no_syntax_table_prop = 0; |
1296 | 482 #ifdef NOT_WORTH_THE_EFFORT |
483 update_mirror_syntax_if_dirty (cache->mirror_table); | |
484 #endif /* NOT_WORTH_THE_EFFORT */ | |
826 | 485 } |
486 else if (CONSP (tmp_table) && INTP (XCAR (tmp_table))) | |
487 { | |
488 cache->use_code = 1; | |
489 cache->syntax_code = XINT (XCAR (tmp_table)); | |
490 cache->no_syntax_table_prop = 0; | |
491 } | |
492 else | |
493 { | |
494 cache->use_code = 0; | |
495 cache->no_syntax_table_prop = 1; | |
1296 | 496 cache->syntax_table = BUFFER_SYNTAX_TABLE (cache->buffer); |
497 cache->mirror_table = BUFFER_MIRROR_SYNTAX_TABLE (cache->buffer); | |
498 #ifdef NOT_WORTH_THE_EFFORT | |
499 update_mirror_syntax_if_dirty (cache->mirror_table); | |
500 #endif /* NOT_WORTH_THE_EFFORT */ | |
460 | 501 } |
502 } | |
3252 | 503 |
504 /* buffer-specific APIs used in buffer.c | |
505 #### This is really unclean; | |
506 the syntax cache should just be a LISP object */ | |
507 | |
508 void | |
509 mark_buffer_syntax_cache (struct buffer *buf) | |
510 { | |
511 struct syntax_cache *cache = buf->syntax_cache; | |
512 if (!cache) /* Vbuffer_defaults and such don't have caches */ | |
513 return; | |
514 mark_object (cache->object); | |
515 if (cache->buffer) | |
516 mark_object (wrap_buffer (cache->buffer)); | |
517 mark_object (cache->syntax_table); | |
518 mark_object (cache->mirror_table); | |
519 mark_object (cache->start); | |
520 mark_object (cache->end); | |
521 } | |
522 | |
523 void | |
524 init_buffer_syntax_cache (struct buffer *buf) | |
525 { | |
526 struct syntax_cache *cache; | |
527 #ifdef NEW_GC | |
528 buf->syntax_cache = alloc_lrecord_type (struct syntax_cache, | |
529 &lrecord_syntax_cache); | |
530 #else /* not NEW_GC */ | |
531 buf->syntax_cache = xnew_and_zero (struct syntax_cache); | |
532 #endif /* not NEW_GC */ | |
533 cache = buf->syntax_cache; | |
534 cache->object = wrap_buffer (buf); | |
535 cache->buffer = buf; | |
536 cache->no_syntax_table_prop = 1; | |
537 cache->syntax_table = BUFFER_SYNTAX_TABLE (cache->buffer); | |
538 cache->mirror_table = BUFFER_MIRROR_SYNTAX_TABLE (cache->buffer); | |
539 cache->start = Fmake_marker (); | |
540 cache->end = Fmake_marker (); | |
541 reset_buffer_syntax_cache_range (cache, cache->object, 0); | |
542 } | |
543 | |
544 /* finalize the syntax cache for BUF */ | |
545 | |
546 void | |
4710
3a87551bfeb5
Fixes for a number of minor warnings issued by gcc. See xemacs-patches message
Jerry James <james@xemacs.org>
parents:
4653
diff
changeset
|
547 uninit_buffer_syntax_cache (struct buffer *UNUSED_IF_NEW_GC (buf)) |
3252 | 548 { |
4141 | 549 #ifndef NEW_GC |
3252 | 550 xfree (buf->syntax_cache, struct syntax_cache *); |
551 buf->syntax_cache = 0; | |
4141 | 552 #endif /* not NEW_GC */ |
3252 | 553 } |
554 | |
555 /* extent-specific APIs used in extents.c and insdel.c */ | |
556 | |
557 /* The syntax-table property on the range covered by EXTENT may be changing, | |
558 either because EXTENT has a syntax-table property and is being attached | |
559 or detached (this includes having its endpoints changed), or because | |
560 the value of EXTENT's syntax-table property is changing. */ | |
561 | |
562 void | |
563 signal_syntax_cache_extent_changed (EXTENT extent) | |
564 { | |
565 Lisp_Object buffer = Fextent_object (wrap_extent (extent)); | |
566 if (BUFFERP (buffer)) | |
567 { | |
568 /* This was getting called with the buffer's start and end null, eg in | |
569 cperl mode, which triggers an assert in byte_marker_position. Cf | |
570 thread rooted at <yxz7j7xzk97.fsf@gimli.holgi.priv> on xemacs-beta. | |
571 <yxzfymklb6p.fsf@gimli.holgi.priv> has a recipe, but you also need | |
572 to delete or type SPC to get the crash. | |
573 #### Delete this comment when setup_syntax_cache is made sane. */ | |
574 struct syntax_cache *cache = XBUFFER (buffer)->syntax_cache; | |
575 /* #### would this be slower or less accurate in character terms? */ | |
576 Bytexpos start = extent_endpoint_byte (extent, 0); | |
577 Bytexpos end = extent_endpoint_byte (extent, 1); | |
578 Bytexpos start2 = byte_marker_position (cache->start); | |
579 Bytexpos end2 = byte_marker_position (cache->end); | |
580 /* If the extent is entirely before or entirely after the cache | |
581 range, it doesn't overlap. Otherwise, invalidate the range. */ | |
582 if (!(end < start2 || start > end2)) | |
583 reset_buffer_syntax_cache_range (cache, buffer, 0); | |
584 } | |
585 } | |
586 | |
587 /* Extents have been adjusted for insertion or deletion, so we need to | |
588 refetch the start and end position of the extent */ | |
589 void | |
590 signal_syntax_cache_extent_adjust (struct buffer *buf) | |
591 { | |
592 struct syntax_cache *cache = buf->syntax_cache; | |
593 /* If the cache was invalid before, leave it that way. We only want | |
594 to update the limits of validity when they were actually valid. */ | |
595 if (cache->prev_change < 0) | |
596 return; | |
597 cache->prev_change = marker_position (cache->start); | |
598 cache->next_change = marker_position (cache->end); | |
599 } | |
600 | |
601 | |
460 | 602 |
428 | 603 /* Convert a letter which signifies a syntax code |
604 into the code it signifies. | |
605 This is used by modify-syntax-entry, and other things. */ | |
606 | |
442 | 607 const unsigned char syntax_spec_code[0400] = |
428 | 608 { 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, |
609 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, | |
610 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, | |
611 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, | |
612 (char) Swhitespace, 0377, (char) Sstring, 0377, | |
613 (char) Smath, 0377, 0377, (char) Squote, | |
614 (char) Sopen, (char) Sclose, 0377, 0377, | |
615 0377, (char) Swhitespace, (char) Spunct, (char) Scharquote, | |
616 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, | |
617 0377, 0377, 0377, 0377, | |
618 (char) Scomment, 0377, (char) Sendcomment, 0377, | |
619 (char) Sinherit, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* @, A ... */ | |
620 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, | |
621 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword, | |
622 0377, 0377, 0377, 0377, (char) Sescape, 0377, 0377, (char) Ssymbol, | |
623 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, /* `, a, ... */ | |
624 0377, 0377, 0377, 0377, 0377, 0377, 0377, 0377, | |
625 0377, 0377, 0377, 0377, 0377, 0377, 0377, (char) Sword, | |
460 | 626 0377, 0377, 0377, 0377, (char) Sstring_fence, 0377, 0377, 0377 |
428 | 627 }; |
628 | |
460 | 629 const unsigned char syntax_code_spec[] = " .w_()'\"$\\/<>@!|"; |
428 | 630 |
631 DEFUN ("syntax-designator-chars", Fsyntax_designator_chars, 0, 0, 0, /* | |
632 Return a string of the recognized syntax designator chars. | |
633 The chars are ordered by their internal syntax codes, which are | |
634 numbered starting at 0. | |
635 */ | |
636 ()) | |
637 { | |
638 return Vsyntax_designator_chars_string; | |
639 } | |
640 | |
641 DEFUN ("char-syntax", Fchar_syntax, 1, 2, 0, /* | |
444 | 642 Return the syntax code of CHARACTER, described by a character. |
643 For example, if CHARACTER is a word constituent, | |
644 the character `?w' is returned. | |
428 | 645 The characters that correspond to various syntax codes |
646 are listed in the documentation of `modify-syntax-entry'. | |
444 | 647 Optional second argument SYNTAX-TABLE defaults to the current buffer's |
428 | 648 syntax table. |
649 */ | |
444 | 650 (character, syntax_table)) |
428 | 651 { |
826 | 652 Lisp_Object mirrortab; |
428 | 653 |
444 | 654 if (NILP (character)) |
428 | 655 { |
444 | 656 character = make_char ('\000'); |
428 | 657 } |
444 | 658 CHECK_CHAR_COERCE_INT (character); |
826 | 659 syntax_table = check_syntax_table (syntax_table, |
660 current_buffer->syntax_table); | |
661 mirrortab = XCHAR_TABLE (syntax_table)->mirror_table; | |
662 return make_char (syntax_code_spec[(int) SYNTAX (mirrortab, | |
663 XCHAR (character))]); | |
428 | 664 } |
665 | |
666 #ifdef MULE | |
667 | |
668 enum syntaxcode | |
2286 | 669 charset_syntax (struct buffer *UNUSED (buf), Lisp_Object UNUSED (charset), |
670 int *multi_p_out) | |
428 | 671 { |
672 *multi_p_out = 1; | |
826 | 673 /* !!#### get this right */ |
3152 | 674 return Sword; |
428 | 675 } |
676 | |
677 #endif | |
678 | |
679 Lisp_Object | |
867 | 680 syntax_match (Lisp_Object syntax_table, Ichar ch) |
428 | 681 { |
826 | 682 Lisp_Object code = get_char_table (ch, syntax_table); |
428 | 683 Lisp_Object code2 = code; |
684 | |
685 if (CONSP (code)) | |
686 code2 = XCAR (code); | |
687 if (SYNTAX_FROM_CODE (XINT (code2)) == Sinherit) | |
826 | 688 code = get_char_table (ch, Vstandard_syntax_table); |
428 | 689 |
690 return CONSP (code) ? XCDR (code) : Qnil; | |
691 } | |
692 | |
693 DEFUN ("matching-paren", Fmatching_paren, 1, 2, 0, /* | |
444 | 694 Return the matching parenthesis of CHARACTER, or nil if none. |
695 Optional second argument SYNTAX-TABLE defaults to the current buffer's | |
428 | 696 syntax table. |
697 */ | |
444 | 698 (character, syntax_table)) |
428 | 699 { |
826 | 700 Lisp_Object mirrortab; |
1315 | 701 enum syntaxcode code; |
428 | 702 |
444 | 703 CHECK_CHAR_COERCE_INT (character); |
826 | 704 syntax_table = check_syntax_table (syntax_table, |
705 current_buffer->syntax_table); | |
706 mirrortab = XCHAR_TABLE (syntax_table)->mirror_table; | |
444 | 707 code = SYNTAX (mirrortab, XCHAR (character)); |
428 | 708 if (code == Sopen || code == Sclose || code == Sstring) |
444 | 709 return syntax_match (syntax_table, XCHAR (character)); |
428 | 710 return Qnil; |
711 } | |
712 | |
713 | |
714 | |
715 #ifdef MULE | |
716 /* Return 1 if there is a word boundary between two word-constituent | |
717 characters C1 and C2 if they appear in this order, else return 0. | |
718 There is no word boundary between two word-constituent ASCII | |
719 characters. */ | |
720 #define WORD_BOUNDARY_P(c1, c2) \ | |
867 | 721 (!(ichar_ascii_p (c1) && ichar_ascii_p (c2)) \ |
428 | 722 && word_boundary_p (c1, c2)) |
723 #endif | |
724 | |
725 /* Return the position across COUNT words from FROM. | |
726 If that many words cannot be found before the end of the buffer, return 0. | |
727 COUNT negative means scan backward and stop at word beginning. */ | |
728 | |
665 | 729 Charbpos |
730 scan_words (struct buffer *buf, Charbpos from, int count) | |
428 | 731 { |
665 | 732 Charbpos limit = count > 0 ? BUF_ZV (buf) : BUF_BEGV (buf); |
867 | 733 Ichar ch0, ch1; |
428 | 734 enum syntaxcode code; |
826 | 735 struct syntax_cache *scache = setup_buffer_syntax_cache (buf, from, count); |
460 | 736 |
428 | 737 /* #### is it really worth it to hand expand both cases? JV */ |
738 while (count > 0) | |
739 { | |
740 QUIT; | |
741 | |
742 while (1) | |
743 { | |
744 if (from == limit) | |
745 return 0; | |
746 | |
826 | 747 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
428 | 748 ch0 = BUF_FETCH_CHAR (buf, from); |
826 | 749 code = SYNTAX_FROM_CACHE (scache, ch0); |
428 | 750 |
442 | 751 from++; |
428 | 752 if (words_include_escapes |
753 && (code == Sescape || code == Scharquote)) | |
754 break; | |
755 if (code == Sword) | |
756 break; | |
757 } | |
758 | |
759 QUIT; | |
760 | |
761 while (from != limit) | |
762 { | |
826 | 763 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
428 | 764 ch1 = BUF_FETCH_CHAR (buf, from); |
826 | 765 code = SYNTAX_FROM_CACHE (scache, ch1); |
428 | 766 if (!(words_include_escapes |
767 && (code == Sescape || code == Scharquote))) | |
768 if (code != Sword | |
769 #ifdef MULE | |
770 || WORD_BOUNDARY_P (ch0, ch1) | |
434 | 771 #endif |
428 | 772 ) |
773 break; | |
774 #ifdef MULE | |
775 ch0 = ch1; | |
434 | 776 #endif |
428 | 777 from++; |
778 } | |
779 count--; | |
780 } | |
781 | |
782 while (count < 0) | |
783 { | |
784 QUIT; | |
785 | |
786 while (1) | |
787 { | |
788 if (from == limit) | |
789 return 0; | |
790 | |
826 | 791 UPDATE_SYNTAX_CACHE_BACKWARD (scache, from - 1); |
428 | 792 ch1 = BUF_FETCH_CHAR (buf, from - 1); |
826 | 793 code = SYNTAX_FROM_CACHE (scache, ch1); |
460 | 794 from--; |
442 | 795 |
428 | 796 if (words_include_escapes |
797 && (code == Sescape || code == Scharquote)) | |
798 break; | |
799 if (code == Sword) | |
800 break; | |
801 } | |
802 | |
803 QUIT; | |
804 | |
805 while (from != limit) | |
806 { | |
826 | 807 UPDATE_SYNTAX_CACHE_BACKWARD (scache, from - 1); |
428 | 808 ch0 = BUF_FETCH_CHAR (buf, from - 1); |
826 | 809 code = SYNTAX_FROM_CACHE (scache, ch0); |
460 | 810 |
428 | 811 if (!(words_include_escapes |
812 && (code == Sescape || code == Scharquote))) | |
813 if (code != Sword | |
814 #ifdef MULE | |
815 || WORD_BOUNDARY_P (ch0, ch1) | |
816 #endif | |
817 ) | |
818 break; | |
819 #ifdef MULE | |
820 ch1 = ch0; | |
821 #endif | |
822 from--; | |
823 } | |
824 count++; | |
825 } | |
826 | |
827 return from; | |
828 } | |
829 | |
446 | 830 DEFUN ("forward-word", Fforward_word, 0, 2, "_p", /* |
428 | 831 Move point forward COUNT words (backward if COUNT is negative). |
446 | 832 Normally t is returned, but if an edge of the buffer is reached, |
833 point is left there and nil is returned. | |
428 | 834 |
462 | 835 The characters that are moved over may be added to the current selection |
836 \(i.e. active region) if the Shift key is held down, a motion key is used | |
837 to invoke this command, and `shifted-motion-keys-select-region' is t; see | |
838 the documentation for this variable for more details. | |
839 | |
446 | 840 COUNT defaults to 1, and BUFFER defaults to the current buffer. |
428 | 841 */ |
842 (count, buffer)) | |
843 { | |
665 | 844 Charbpos val; |
428 | 845 struct buffer *buf = decode_buffer (buffer, 0); |
446 | 846 EMACS_INT n; |
847 | |
848 if (NILP (count)) | |
849 n = 1; | |
850 else | |
851 { | |
852 CHECK_INT (count); | |
853 n = XINT (count); | |
854 } | |
428 | 855 |
446 | 856 val = scan_words (buf, BUF_PT (buf), n); |
857 if (val) | |
428 | 858 { |
446 | 859 BUF_SET_PT (buf, val); |
860 return Qt; | |
861 } | |
862 else | |
863 { | |
864 BUF_SET_PT (buf, n > 0 ? BUF_ZV (buf) : BUF_BEGV (buf)); | |
428 | 865 return Qnil; |
866 } | |
867 } | |
868 | |
869 static void scan_sexps_forward (struct buffer *buf, | |
870 struct lisp_parse_state *, | |
665 | 871 Charbpos from, Charbpos end, |
428 | 872 int targetdepth, int stopbefore, |
873 Lisp_Object oldstate, | |
874 int commentstop); | |
875 | |
876 static int | |
665 | 877 find_start_of_comment (struct buffer *buf, Charbpos from, Charbpos stop, |
460 | 878 int comstyle) |
428 | 879 { |
867 | 880 Ichar c; |
428 | 881 enum syntaxcode code; |
882 | |
883 /* Look back, counting the parity of string-quotes, | |
884 and recording the comment-starters seen. | |
885 When we reach a safe place, assume that's not in a string; | |
886 then step the main scan to the earliest comment-starter seen | |
887 an even number of string quotes away from the safe place. | |
888 | |
889 OFROM[I] is position of the earliest comment-starter seen | |
890 which is I+2X quotes from the comment-end. | |
891 PARITY is current parity of quotes from the comment end. */ | |
892 int parity = 0; | |
867 | 893 Ichar my_stringend = 0; |
428 | 894 int string_lossage = 0; |
665 | 895 Charbpos comment_end = from; |
896 Charbpos comstart_pos = 0; | |
428 | 897 int comstart_parity = 0; |
898 int styles_match_p = 0; | |
460 | 899 /* mask to match comment styles against; for ST_COMMENT_STYLE, this |
900 will get set to SYNTAX_COMMENT_STYLE_B, but never get checked */ | |
901 int mask = comstyle ? SYNTAX_COMMENT_STYLE_B : SYNTAX_COMMENT_STYLE_A; | |
826 | 902 struct syntax_cache *scache = buf->syntax_cache; |
428 | 903 |
904 /* At beginning of range to scan, we're outside of strings; | |
905 that determines quote parity to the comment-end. */ | |
906 while (from != stop) | |
907 { | |
460 | 908 int syncode; |
909 | |
428 | 910 /* Move back and examine a character. */ |
911 from--; | |
826 | 912 UPDATE_SYNTAX_CACHE_BACKWARD (scache, from); |
428 | 913 |
914 c = BUF_FETCH_CHAR (buf, from); | |
826 | 915 syncode = SYNTAX_CODE_FROM_CACHE (scache, c); |
916 code = SYNTAX_FROM_CODE (syncode); | |
428 | 917 |
918 /* is this a 1-char comment end sequence? if so, try | |
919 to see if style matches previously extracted mask */ | |
920 if (code == Sendcomment) | |
921 { | |
922 styles_match_p = | |
460 | 923 SYNTAX_CODE_COMMENT_1CHAR_MASK (syncode) & mask; |
428 | 924 } |
925 | |
926 /* or are we looking at a 1-char comment start sequence | |
927 of the style matching mask? */ | |
460 | 928 else if (code == Scomment) |
428 | 929 { |
460 | 930 styles_match_p = |
931 SYNTAX_CODE_COMMENT_1CHAR_MASK (syncode) & mask; | |
428 | 932 } |
933 | |
460 | 934 /* otherwise, is this a 2-char comment end or start sequence? */ |
935 else if (from > stop) | |
936 do | |
937 { | |
938 /* 2-char comment end sequence? */ | |
939 if (SYNTAX_CODE_END_SECOND_P (syncode)) | |
940 { | |
941 int prev_syncode; | |
826 | 942 UPDATE_SYNTAX_CACHE_BACKWARD (scache, from - 1); |
460 | 943 prev_syncode = |
1315 | 944 SYNTAX_CODE_FROM_CACHE (scache, |
945 BUF_FETCH_CHAR (buf, from - 1)); | |
460 | 946 |
947 if (SYNTAX_CODES_END_P (prev_syncode, syncode)) | |
948 { | |
949 code = Sendcomment; | |
950 styles_match_p = | |
826 | 951 SYNTAX_CODES_COMMENT_MASK_END (prev_syncode, |
952 syncode) & mask; | |
460 | 953 from--; |
826 | 954 UPDATE_SYNTAX_CACHE_BACKWARD (scache, from); |
460 | 955 c = BUF_FETCH_CHAR (buf, from); |
956 | |
957 /* Found a comment-end sequence, so skip past the | |
958 check for a comment-start */ | |
959 break; | |
960 } | |
961 } | |
962 | |
963 /* 2-char comment start sequence? */ | |
964 if (SYNTAX_CODE_START_SECOND_P (syncode)) | |
965 { | |
966 int prev_syncode; | |
826 | 967 UPDATE_SYNTAX_CACHE_BACKWARD (scache, from - 1); |
460 | 968 prev_syncode = |
1315 | 969 SYNTAX_CODE_FROM_CACHE (scache, |
970 BUF_FETCH_CHAR (buf, from - 1)); | |
460 | 971 |
972 if (SYNTAX_CODES_START_P (prev_syncode, syncode)) | |
973 { | |
974 code = Scomment; | |
975 styles_match_p = | |
826 | 976 SYNTAX_CODES_COMMENT_MASK_START (prev_syncode, |
977 syncode) & mask; | |
460 | 978 from--; |
826 | 979 UPDATE_SYNTAX_CACHE_BACKWARD (scache, from); |
460 | 980 c = BUF_FETCH_CHAR (buf, from); |
981 } | |
982 } | |
983 } while (0); | |
428 | 984 |
985 /* Ignore escaped characters. */ | |
986 if (char_quoted (buf, from)) | |
987 continue; | |
988 | |
989 /* Track parity of quotes. */ | |
990 if (code == Sstring) | |
991 { | |
992 parity ^= 1; | |
993 if (my_stringend == 0) | |
994 my_stringend = c; | |
995 /* If we have two kinds of string delimiters. | |
996 There's no way to grok this scanning backwards. */ | |
997 else if (my_stringend != c) | |
998 string_lossage = 1; | |
999 } | |
1000 | |
460 | 1001 if (code == Sstring_fence || code == Scomment_fence) |
1002 { | |
1003 parity ^= 1; | |
1004 if (my_stringend == 0) | |
1005 my_stringend = | |
1006 code == Sstring_fence ? ST_STRING_STYLE : ST_COMMENT_STYLE; | |
1007 /* If we have two kinds of string delimiters. | |
1008 There's no way to grok this scanning backwards. */ | |
1009 else if (my_stringend != (code == Sstring_fence | |
1010 ? ST_STRING_STYLE : ST_COMMENT_STYLE)) | |
1011 string_lossage = 1; | |
1012 } | |
1013 | |
428 | 1014 /* Record comment-starters according to that |
1015 quote-parity to the comment-end. */ | |
1016 if (code == Scomment && styles_match_p) | |
1017 { | |
1018 comstart_parity = parity; | |
1019 comstart_pos = from; | |
1020 } | |
1021 | |
1022 /* If we find another earlier comment-ender, | |
1023 any comment-starts earlier than that don't count | |
1024 (because they go with the earlier comment-ender). */ | |
1025 if (code == Sendcomment && styles_match_p) | |
1026 break; | |
1027 | |
1028 /* Assume a defun-start point is outside of strings. */ | |
1029 if (code == Sopen | |
1030 && (from == stop || BUF_FETCH_CHAR (buf, from - 1) == '\n')) | |
1031 break; | |
1032 } | |
1033 | |
1034 if (comstart_pos == 0) | |
1035 from = comment_end; | |
1036 /* If the earliest comment starter | |
1037 is followed by uniform paired string quotes or none, | |
1038 we know it can't be inside a string | |
1039 since if it were then the comment ender would be inside one. | |
1040 So it does start a comment. Skip back to it. */ | |
1041 else if (comstart_parity == 0 && !string_lossage) | |
1042 from = comstart_pos; | |
1043 else | |
1044 { | |
1045 /* We had two kinds of string delimiters mixed up | |
1046 together. Decode this going forwards. | |
1047 Scan fwd from the previous comment ender | |
1048 to the one in question; this records where we | |
1049 last passed a comment starter. */ | |
1050 | |
1051 struct lisp_parse_state state; | |
1052 scan_sexps_forward (buf, &state, find_defun_start (buf, comment_end), | |
1053 comment_end - 1, -10000, 0, Qnil, 0); | |
1054 if (state.incomment) | |
460 | 1055 from = state.comstr_start; |
428 | 1056 else |
1057 /* We can't grok this as a comment; scan it normally. */ | |
1058 from = comment_end; | |
826 | 1059 UPDATE_SYNTAX_CACHE_FORWARD (scache, from - 1); |
428 | 1060 } |
1061 return from; | |
1062 } | |
1063 | |
665 | 1064 static Charbpos |
826 | 1065 find_end_of_comment (struct buffer *buf, Charbpos from, Charbpos stop, |
1066 int comstyle) | |
428 | 1067 { |
1068 int c; | |
460 | 1069 int prev_code; |
1070 /* mask to match comment styles against; for ST_COMMENT_STYLE, this | |
1071 will get set to SYNTAX_COMMENT_STYLE_B, but never get checked */ | |
1072 int mask = comstyle ? SYNTAX_COMMENT_STYLE_B : SYNTAX_COMMENT_STYLE_A; | |
826 | 1073 struct syntax_cache *scache = buf->syntax_cache; |
428 | 1074 |
460 | 1075 /* This is only called by functions which have already set up the |
1076 syntax_cache and are keeping it up-to-date */ | |
428 | 1077 while (1) |
1078 { | |
1079 if (from == stop) | |
1080 { | |
1081 return -1; | |
1082 } | |
460 | 1083 |
826 | 1084 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
428 | 1085 c = BUF_FETCH_CHAR (buf, from); |
460 | 1086 |
1087 /* Test for generic comments */ | |
1088 if (comstyle == ST_COMMENT_STYLE) | |
1089 { | |
826 | 1090 if (SYNTAX_FROM_CACHE (scache, c) == Scomment_fence) |
460 | 1091 { |
1092 from++; | |
826 | 1093 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
460 | 1094 break; |
1095 } | |
1096 from++; | |
1097 continue; /* No need to test other comment styles in a | |
1098 generic comment */ | |
1099 } | |
1100 else | |
1101 | |
826 | 1102 if (SYNTAX_FROM_CACHE (scache, c) == Sendcomment |
460 | 1103 && SYNTAX_CODE_MATCHES_1CHAR_P |
826 | 1104 (SYNTAX_CODE_FROM_CACHE (scache, c), mask)) |
428 | 1105 /* we have encountered a comment end of the same style |
1106 as the comment sequence which began this comment | |
1107 section */ | |
460 | 1108 { |
1109 from++; | |
826 | 1110 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
460 | 1111 break; |
1112 } | |
428 | 1113 |
826 | 1114 prev_code = SYNTAX_CODE_FROM_CACHE (scache, c); |
428 | 1115 from++; |
826 | 1116 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
428 | 1117 if (from < stop |
460 | 1118 && SYNTAX_CODES_MATCH_END_P |
1119 (prev_code, | |
826 | 1120 SYNTAX_CODE_FROM_CACHE (scache, BUF_FETCH_CHAR (buf, from)), |
460 | 1121 mask) |
1122 | |
1123 ) | |
428 | 1124 /* we have encountered a comment end of the same style |
1125 as the comment sequence which began this comment | |
1126 section */ | |
460 | 1127 { |
1128 from++; | |
826 | 1129 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
460 | 1130 break; |
1131 } | |
428 | 1132 } |
1133 return from; | |
1134 } | |
1135 | |
1136 | |
1137 /* #### between FSF 19.23 and 19.28 there are some changes to the logic | |
1138 in this function (and minor changes to find_start_of_comment(), | |
1139 above, which is part of Fforward_comment() in FSF). Attempts to port | |
1140 that logic made this function break, so I'm leaving it out. If anyone | |
1141 ever complains about this function not working properly, take a look | |
1142 at those changes. --ben */ | |
1143 | |
446 | 1144 DEFUN ("forward-comment", Fforward_comment, 0, 2, 0, /* |
444 | 1145 Move forward across up to COUNT comments, or backwards if COUNT is negative. |
428 | 1146 Stop scanning if we find something other than a comment or whitespace. |
1147 Set point to where scanning stops. | |
444 | 1148 If COUNT comments are found as expected, with nothing except whitespace |
428 | 1149 between them, return t; otherwise return nil. |
1150 Point is set in either case. | |
446 | 1151 COUNT defaults to 1, and BUFFER defaults to the current buffer. |
428 | 1152 */ |
444 | 1153 (count, buffer)) |
428 | 1154 { |
665 | 1155 Charbpos from; |
1156 Charbpos stop; | |
867 | 1157 Ichar c; |
428 | 1158 enum syntaxcode code; |
460 | 1159 int syncode; |
444 | 1160 EMACS_INT n; |
428 | 1161 struct buffer *buf = decode_buffer (buffer, 0); |
826 | 1162 struct syntax_cache *scache; |
1163 | |
446 | 1164 if (NILP (count)) |
1165 n = 1; | |
1166 else | |
1167 { | |
1168 CHECK_INT (count); | |
1169 n = XINT (count); | |
1170 } | |
428 | 1171 |
1172 from = BUF_PT (buf); | |
1173 | |
826 | 1174 scache = setup_buffer_syntax_cache (buf, from, n); |
444 | 1175 while (n > 0) |
428 | 1176 { |
1177 QUIT; | |
1178 | |
1179 stop = BUF_ZV (buf); | |
1180 while (from < stop) | |
1181 { | |
460 | 1182 int comstyle = 0; /* mask for finding matching comment style */ |
428 | 1183 |
1184 if (char_quoted (buf, from)) | |
1185 { | |
1186 from++; | |
1187 continue; | |
1188 } | |
1189 | |
826 | 1190 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
428 | 1191 c = BUF_FETCH_CHAR (buf, from); |
826 | 1192 syncode = SYNTAX_CODE_FROM_CACHE (scache, c); |
1193 code = SYNTAX_FROM_CODE (syncode); | |
428 | 1194 |
1195 if (code == Scomment) | |
1196 { | |
1197 /* we have encountered a single character comment start | |
1198 sequence, and we are ignoring all text inside comments. | |
1199 we must record the comment style this character begins | |
1200 so that later, only a comment end of the same style actually | |
1201 ends the comment section */ | |
460 | 1202 comstyle = SYNTAX_CODE_COMMENT_1CHAR_MASK (syncode) |
1203 == SYNTAX_COMMENT_STYLE_A ? 0 : 1; | |
428 | 1204 } |
1205 | |
460 | 1206 else if (code == Scomment_fence) |
1207 { | |
1208 from++; | |
1209 code = Scomment; | |
1210 comstyle = ST_COMMENT_STYLE; | |
1211 } | |
1212 | |
428 | 1213 else if (from < stop |
460 | 1214 && SYNTAX_CODE_START_FIRST_P (syncode)) |
428 | 1215 { |
460 | 1216 int next_syncode; |
826 | 1217 UPDATE_SYNTAX_CACHE_FORWARD (scache, from + 1); |
460 | 1218 next_syncode = |
826 | 1219 SYNTAX_CODE_FROM_CACHE (scache, BUF_FETCH_CHAR (buf, from + 1)); |
460 | 1220 |
1221 if (SYNTAX_CODES_START_P (syncode, next_syncode)) | |
1222 { | |
1223 /* we have encountered a 2char comment start sequence and we | |
1224 are ignoring all text inside comments. we must record | |
1225 the comment style this sequence begins so that later, | |
1226 only a comment end of the same style actually ends | |
1227 the comment section */ | |
1228 code = Scomment; | |
1229 comstyle = | |
1230 SYNTAX_CODES_COMMENT_MASK_START (syncode, next_syncode) | |
1231 == SYNTAX_COMMENT_STYLE_A ? 0 : 1; | |
1232 from++; | |
1233 } | |
428 | 1234 } |
1235 | |
1236 if (code == Scomment) | |
1237 { | |
826 | 1238 Charbpos newfrom = find_end_of_comment (buf, from, stop, |
1239 comstyle); | |
428 | 1240 if (newfrom < 0) |
1241 { | |
1242 /* we stopped because from==stop */ | |
1243 BUF_SET_PT (buf, stop); | |
1244 return Qnil; | |
1245 } | |
1246 from = newfrom; | |
1247 | |
1248 /* We have skipped one comment. */ | |
1249 break; | |
1250 } | |
1251 else if (code != Swhitespace | |
1252 && code != Sendcomment | |
1253 && code != Scomment ) | |
1254 { | |
1255 BUF_SET_PT (buf, from); | |
1256 return Qnil; | |
1257 } | |
1258 from++; | |
1259 } | |
1260 | |
1261 /* End of comment reached */ | |
444 | 1262 n--; |
428 | 1263 } |
1264 | |
444 | 1265 while (n < 0) |
428 | 1266 { |
1267 QUIT; | |
1268 | |
1269 stop = BUF_BEGV (buf); | |
1270 while (from > stop) | |
1271 { | |
460 | 1272 int comstyle = 0; /* mask for finding matching comment style */ |
428 | 1273 |
1274 from--; | |
1275 if (char_quoted (buf, from)) | |
1276 { | |
1277 from--; | |
1278 continue; | |
1279 } | |
1280 | |
1281 c = BUF_FETCH_CHAR (buf, from); | |
826 | 1282 syncode = SYNTAX_CODE_FROM_CACHE (scache, c); |
1283 code = SYNTAX_FROM_CODE (syncode); | |
428 | 1284 |
1285 if (code == Sendcomment) | |
1286 { | |
1287 /* we have found a single char end comment. we must record | |
1288 the comment style encountered so that later, we can match | |
1289 only the proper comment begin sequence of the same style */ | |
460 | 1290 comstyle = SYNTAX_CODE_COMMENT_1CHAR_MASK (syncode) |
1291 == SYNTAX_COMMENT_STYLE_A ? 0 : 1; | |
1292 } | |
1293 | |
1294 else if (code == Scomment_fence) | |
1295 { | |
1296 code = Sendcomment; | |
1297 comstyle = ST_COMMENT_STYLE; | |
428 | 1298 } |
1299 | |
1300 else if (from > stop | |
460 | 1301 && SYNTAX_CODE_END_SECOND_P (syncode)) |
428 | 1302 { |
460 | 1303 int prev_syncode; |
826 | 1304 UPDATE_SYNTAX_CACHE_BACKWARD (scache, from - 1); |
460 | 1305 prev_syncode = |
826 | 1306 SYNTAX_CODE_FROM_CACHE (scache, BUF_FETCH_CHAR (buf, from - 1)); |
460 | 1307 if (SYNTAX_CODES_END_P (prev_syncode, syncode)) |
1308 { | |
1309 /* We must record the comment style encountered so that | |
1310 later, we can match only the proper comment begin | |
1311 sequence of the same style. */ | |
1312 code = Sendcomment; | |
1313 comstyle = SYNTAX_CODES_COMMENT_MASK_END | |
1314 (prev_syncode, syncode) == SYNTAX_COMMENT_STYLE_A ? 0 : 1; | |
1315 from--; | |
1316 } | |
428 | 1317 } |
1318 | |
1319 if (code == Sendcomment) | |
1320 { | |
460 | 1321 from = find_start_of_comment (buf, from, stop, comstyle); |
428 | 1322 break; |
1323 } | |
1324 | |
1325 else if (code != Swhitespace | |
460 | 1326 && code != Scomment |
1327 && code != Sendcomment) | |
428 | 1328 { |
1329 BUF_SET_PT (buf, from + 1); | |
1330 return Qnil; | |
1331 } | |
1332 } | |
1333 | |
444 | 1334 n++; |
428 | 1335 } |
1336 | |
1337 BUF_SET_PT (buf, from); | |
1338 return Qt; | |
1339 } | |
1340 | |
1341 | |
1342 Lisp_Object | |
665 | 1343 scan_lists (struct buffer *buf, Charbpos from, int count, int depth, |
444 | 1344 int sexpflag, int noerror) |
428 | 1345 { |
665 | 1346 Charbpos stop; |
867 | 1347 Ichar c; |
428 | 1348 int quoted; |
1349 int mathexit = 0; | |
1350 enum syntaxcode code; | |
460 | 1351 int syncode; |
428 | 1352 int min_depth = depth; /* Err out if depth gets less than this. */ |
826 | 1353 struct syntax_cache *scache; |
1354 | |
428 | 1355 if (depth > 0) min_depth = 0; |
1356 | |
826 | 1357 scache = setup_buffer_syntax_cache (buf, from, count); |
428 | 1358 while (count > 0) |
1359 { | |
1360 QUIT; | |
1361 | |
1362 stop = BUF_ZV (buf); | |
1363 while (from < stop) | |
1364 { | |
460 | 1365 int comstyle = 0; /* mask for finding matching comment style */ |
428 | 1366 |
826 | 1367 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
428 | 1368 c = BUF_FETCH_CHAR (buf, from); |
826 | 1369 syncode = SYNTAX_CODE_FROM_CACHE (scache, c); |
1370 code = SYNTAX_FROM_CODE (syncode); | |
428 | 1371 from++; |
1372 | |
1373 /* a 1-char comment start sequence */ | |
1374 if (code == Scomment && parse_sexp_ignore_comments) | |
1375 { | |
460 | 1376 comstyle = SYNTAX_CODE_COMMENT_1CHAR_MASK (syncode) == |
1377 SYNTAX_COMMENT_STYLE_A ? 0 : 1; | |
428 | 1378 } |
1379 | |
1380 /* else, a 2-char comment start sequence? */ | |
1381 else if (from < stop | |
460 | 1382 && SYNTAX_CODE_START_FIRST_P (syncode) |
428 | 1383 && parse_sexp_ignore_comments) |
1384 { | |
460 | 1385 int next_syncode; |
826 | 1386 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
460 | 1387 next_syncode = |
826 | 1388 SYNTAX_CODE_FROM_CACHE (scache, BUF_FETCH_CHAR (buf, from)); |
460 | 1389 |
1390 if (SYNTAX_CODES_START_P (syncode, next_syncode)) | |
1391 { | |
826 | 1392 /* we have encountered a comment start sequence and we |
1393 are ignoring all text inside comments. we must record | |
1394 the comment style this sequence begins so that later, | |
1395 only a comment end of the same style actually ends | |
1396 the comment section */ | |
1397 code = Scomment; | |
460 | 1398 comstyle = SYNTAX_CODES_COMMENT_MASK_START |
1399 (syncode, next_syncode) == SYNTAX_COMMENT_STYLE_A ? 0 : 1; | |
826 | 1400 from++; |
1401 } | |
428 | 1402 } |
826 | 1403 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
428 | 1404 |
460 | 1405 if (SYNTAX_CODE_PREFIX (syncode)) |
428 | 1406 continue; |
1407 | |
1408 switch (code) | |
1409 { | |
1410 case Sescape: | |
1411 case Scharquote: | |
1412 if (from == stop) goto lose; | |
1413 from++; | |
1414 /* treat following character as a word constituent */ | |
1415 case Sword: | |
1416 case Ssymbol: | |
1417 if (depth || !sexpflag) break; | |
1418 /* This word counts as a sexp; return at end of it. */ | |
1419 while (from < stop) | |
1420 { | |
826 | 1421 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
1422 switch (SYNTAX_FROM_CACHE (scache, BUF_FETCH_CHAR (buf, from))) | |
428 | 1423 { |
1424 case Scharquote: | |
1425 case Sescape: | |
1426 from++; | |
1427 if (from == stop) goto lose; | |
1428 break; | |
1429 case Sword: | |
1430 case Ssymbol: | |
1431 case Squote: | |
1432 break; | |
1433 default: | |
1434 goto done; | |
1435 } | |
1436 from++; | |
1437 } | |
1438 goto done; | |
1439 | |
460 | 1440 case Scomment_fence: |
1441 comstyle = ST_COMMENT_STYLE; | |
428 | 1442 case Scomment: |
1443 if (!parse_sexp_ignore_comments) | |
1444 break; | |
826 | 1445 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
428 | 1446 { |
665 | 1447 Charbpos newfrom = |
460 | 1448 find_end_of_comment (buf, from, stop, comstyle); |
428 | 1449 if (newfrom < 0) |
1450 { | |
1451 /* we stopped because from == stop in search forward */ | |
1452 from = stop; | |
1453 if (depth == 0) | |
1454 goto done; | |
1455 goto lose; | |
1456 } | |
1457 from = newfrom; | |
1458 } | |
1459 break; | |
1460 | |
1461 case Smath: | |
1462 if (!sexpflag) | |
1463 break; | |
1464 if (from != stop && c == BUF_FETCH_CHAR (buf, from)) | |
1465 from++; | |
1466 if (mathexit) | |
1467 { | |
1468 mathexit = 0; | |
1469 goto close1; | |
1470 } | |
1471 mathexit = 1; | |
1472 | |
1473 case Sopen: | |
1474 if (!++depth) goto done; | |
1475 break; | |
1476 | |
1477 case Sclose: | |
1478 close1: | |
1479 if (!--depth) goto done; | |
1480 if (depth < min_depth) | |
1481 { | |
444 | 1482 if (noerror) |
428 | 1483 return Qnil; |
826 | 1484 syntax_error ("Containing expression ends prematurely", |
1485 Qunbound); | |
428 | 1486 } |
1487 break; | |
1488 | |
460 | 1489 case Sstring_fence: |
428 | 1490 case Sstring: |
1491 { | |
867 | 1492 Ichar stringterm; |
460 | 1493 |
1494 if (code != Sstring_fence) | |
1495 { | |
826 | 1496 /* XEmacs change: call syntax_match on character */ |
867 | 1497 Ichar ch = BUF_FETCH_CHAR (buf, from - 1); |
460 | 1498 Lisp_Object stermobj = |
1296 | 1499 syntax_match (scache->syntax_table, ch); |
428 | 1500 |
1501 if (CHARP (stermobj)) | |
1502 stringterm = XCHAR (stermobj); | |
1503 else | |
1504 stringterm = ch; | |
460 | 1505 } |
1506 else | |
1507 stringterm = '\0'; /* avoid compiler warnings */ | |
428 | 1508 |
1509 while (1) | |
1510 { | |
1511 if (from >= stop) | |
1512 goto lose; | |
826 | 1513 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
460 | 1514 c = BUF_FETCH_CHAR (buf, from); |
1515 if (code == Sstring | |
1516 ? c == stringterm | |
826 | 1517 : SYNTAX_FROM_CACHE (scache, c) == Sstring_fence) |
428 | 1518 break; |
460 | 1519 |
826 | 1520 switch (SYNTAX_FROM_CACHE (scache, c)) |
428 | 1521 { |
1522 case Scharquote: | |
1523 case Sescape: | |
1524 from++; | |
1525 break; | |
1526 default: | |
1527 break; | |
1528 } | |
1529 from++; | |
1530 } | |
1531 from++; | |
1532 if (!depth && sexpflag) goto done; | |
1533 break; | |
1534 } | |
1535 | |
1536 default: | |
1537 break; | |
1538 } | |
1539 } | |
1540 | |
1541 /* Reached end of buffer. Error if within object, | |
1542 return nil if between */ | |
1543 if (depth) goto lose; | |
1544 | |
1545 return Qnil; | |
1546 | |
1547 /* End of object reached */ | |
1548 done: | |
1549 count--; | |
1550 } | |
1551 | |
1552 while (count < 0) | |
1553 { | |
1554 QUIT; | |
1555 | |
1556 stop = BUF_BEGV (buf); | |
1557 while (from > stop) | |
1558 { | |
460 | 1559 int comstyle = 0; /* mask for finding matching comment style */ |
428 | 1560 |
1561 from--; | |
826 | 1562 UPDATE_SYNTAX_CACHE_BACKWARD (scache, from); |
428 | 1563 quoted = char_quoted (buf, from); |
1564 if (quoted) | |
460 | 1565 { |
428 | 1566 from--; |
826 | 1567 UPDATE_SYNTAX_CACHE_BACKWARD (scache, from); |
460 | 1568 } |
428 | 1569 |
1570 c = BUF_FETCH_CHAR (buf, from); | |
826 | 1571 syncode = SYNTAX_CODE_FROM_CACHE (scache, c); |
1572 code = SYNTAX_FROM_CODE (syncode); | |
428 | 1573 |
1574 if (code == Sendcomment && parse_sexp_ignore_comments) | |
1575 { | |
1576 /* we have found a single char end comment. we must record | |
1577 the comment style encountered so that later, we can match | |
1578 only the proper comment begin sequence of the same style */ | |
460 | 1579 comstyle = SYNTAX_CODE_COMMENT_1CHAR_MASK (syncode) |
1580 == SYNTAX_COMMENT_STYLE_A ? 0 : 1; | |
428 | 1581 } |
1582 | |
1583 else if (from > stop | |
460 | 1584 && SYNTAX_CODE_END_SECOND_P (syncode) |
428 | 1585 && !char_quoted (buf, from - 1) |
1586 && parse_sexp_ignore_comments) | |
1587 { | |
460 | 1588 int prev_syncode; |
826 | 1589 UPDATE_SYNTAX_CACHE_BACKWARD (scache, from - 1); |
1590 prev_syncode = | |
1591 SYNTAX_CODE_FROM_CACHE (scache, BUF_FETCH_CHAR (buf, from - 1)); | |
460 | 1592 |
1593 if (SYNTAX_CODES_END_P (prev_syncode, syncode)) | |
1594 { | |
428 | 1595 /* we must record the comment style encountered so that |
1596 later, we can match only the proper comment begin | |
1597 sequence of the same style */ | |
1598 code = Sendcomment; | |
460 | 1599 comstyle = SYNTAX_CODES_COMMENT_MASK_END |
1600 (prev_syncode, syncode) == SYNTAX_COMMENT_STYLE_A ? 0 : 1; | |
428 | 1601 from--; |
1602 } | |
460 | 1603 } |
428 | 1604 |
460 | 1605 if (SYNTAX_CODE_PREFIX (syncode)) |
428 | 1606 continue; |
1607 | |
434 | 1608 switch (quoted ? Sword : code) |
428 | 1609 { |
1610 case Sword: | |
1611 case Ssymbol: | |
1612 if (depth || !sexpflag) break; | |
1613 /* This word counts as a sexp; count object finished after | |
1614 passing it. */ | |
1615 while (from > stop) | |
1616 { | |
826 | 1617 UPDATE_SYNTAX_CACHE_BACKWARD (scache, from); |
428 | 1618 quoted = char_quoted (buf, from - 1); |
1619 | |
1620 if (quoted) | |
1621 from--; | |
1622 if (! (quoted | |
1623 || (syncode = | |
826 | 1624 SYNTAX_FROM_CACHE (scache, BUF_FETCH_CHAR (buf, |
1625 from - 1))) | |
428 | 1626 == Sword |
1627 || syncode == Ssymbol | |
1628 || syncode == Squote)) | |
1629 goto done2; | |
1630 from--; | |
1631 } | |
1632 goto done2; | |
1633 | |
1634 case Smath: | |
1635 if (!sexpflag) | |
1636 break; | |
1637 if (from != stop && c == BUF_FETCH_CHAR (buf, from - 1)) | |
1638 from--; | |
1639 if (mathexit) | |
1640 { | |
1641 mathexit = 0; | |
1642 goto open2; | |
1643 } | |
1644 mathexit = 1; | |
1645 | |
1646 case Sclose: | |
1647 if (!++depth) goto done2; | |
1648 break; | |
1649 | |
1650 case Sopen: | |
1651 open2: | |
1652 if (!--depth) goto done2; | |
1653 if (depth < min_depth) | |
1654 { | |
444 | 1655 if (noerror) |
428 | 1656 return Qnil; |
826 | 1657 syntax_error ("Containing expression ends prematurely", |
1658 Qunbound); | |
428 | 1659 } |
1660 break; | |
1661 | |
460 | 1662 case Scomment_fence: |
1663 comstyle = ST_COMMENT_STYLE; | |
428 | 1664 case Sendcomment: |
1665 if (parse_sexp_ignore_comments) | |
460 | 1666 from = find_start_of_comment (buf, from, stop, comstyle); |
428 | 1667 break; |
1668 | |
460 | 1669 case Sstring_fence: |
428 | 1670 case Sstring: |
1671 { | |
867 | 1672 Ichar stringterm; |
460 | 1673 |
1674 if (code != Sstring_fence) | |
1675 { | |
428 | 1676 /* XEmacs change: call syntax_match() on character */ |
867 | 1677 Ichar ch = BUF_FETCH_CHAR (buf, from); |
460 | 1678 Lisp_Object stermobj = |
1296 | 1679 syntax_match (scache->syntax_table, ch); |
428 | 1680 |
1681 if (CHARP (stermobj)) | |
1682 stringterm = XCHAR (stermobj); | |
1683 else | |
1684 stringterm = ch; | |
460 | 1685 } |
1686 else | |
1687 stringterm = '\0'; /* avoid compiler warnings */ | |
428 | 1688 |
1689 while (1) | |
1690 { | |
1691 if (from == stop) goto lose; | |
460 | 1692 |
826 | 1693 UPDATE_SYNTAX_CACHE_BACKWARD (scache, from - 1); |
460 | 1694 c = BUF_FETCH_CHAR (buf, from - 1); |
1695 | |
1696 if ((code == Sstring | |
1697 ? c == stringterm | |
826 | 1698 : SYNTAX_FROM_CACHE (scache, c) == Sstring_fence) |
460 | 1699 && !char_quoted (buf, from - 1)) |
1700 { | |
428 | 1701 break; |
460 | 1702 } |
1703 | |
428 | 1704 from--; |
1705 } | |
1706 from--; | |
1707 if (!depth && sexpflag) goto done2; | |
1708 break; | |
1709 } | |
1710 } | |
1711 } | |
1712 | |
1713 /* Reached start of buffer. Error if within object, | |
1714 return nil if between */ | |
1715 if (depth) goto lose; | |
1716 | |
1717 return Qnil; | |
1718 | |
1719 done2: | |
1720 count++; | |
1721 } | |
1722 | |
1723 | |
1724 return (make_int (from)); | |
1725 | |
1726 lose: | |
444 | 1727 if (!noerror) |
826 | 1728 syntax_error ("Unbalanced parentheses", Qunbound); |
428 | 1729 return Qnil; |
1730 } | |
1731 | |
1732 int | |
665 | 1733 char_quoted (struct buffer *buf, Charbpos pos) |
428 | 1734 { |
1735 enum syntaxcode code; | |
665 | 1736 Charbpos beg = BUF_BEGV (buf); |
428 | 1737 int quoted = 0; |
665 | 1738 Charbpos startpos = pos; |
826 | 1739 struct syntax_cache *scache = buf->syntax_cache; |
460 | 1740 |
1741 while (pos > beg) | |
1742 { | |
826 | 1743 UPDATE_SYNTAX_CACHE_BACKWARD (scache, pos - 1); |
1744 code = SYNTAX_FROM_CACHE (scache, BUF_FETCH_CHAR (buf, pos - 1)); | |
428 | 1745 |
460 | 1746 if (code != Scharquote && code != Sescape) |
1747 break; | |
1748 pos--; | |
1749 quoted = !quoted; | |
1750 } | |
1751 | |
826 | 1752 UPDATE_SYNTAX_CACHE (scache, startpos); |
428 | 1753 return quoted; |
1754 } | |
1755 | |
1756 DEFUN ("scan-lists", Fscan_lists, 3, 5, 0, /* | |
1757 Scan from character number FROM by COUNT lists. | |
1758 Returns the character number of the position thus found. | |
1759 | |
1760 If DEPTH is nonzero, paren depth begins counting from that value, | |
1761 only places where the depth in parentheses becomes zero | |
1762 are candidates for stopping; COUNT such places are counted. | |
1763 Thus, a positive value for DEPTH means go out levels. | |
1764 | |
1765 Comments are ignored if `parse-sexp-ignore-comments' is non-nil. | |
1766 | |
1767 If the beginning or end of (the accessible part of) the buffer is reached | |
1768 and the depth is wrong, an error is signaled. | |
1769 If the depth is right but the count is not used up, nil is returned. | |
1770 | |
1771 If optional arg BUFFER is non-nil, scanning occurs in that buffer instead | |
1772 of in the current buffer. | |
1773 | |
1774 If optional arg NOERROR is non-nil, scan-lists will return nil instead of | |
1775 signalling an error. | |
1776 */ | |
444 | 1777 (from, count, depth, buffer, noerror)) |
428 | 1778 { |
1779 struct buffer *buf; | |
1780 | |
1781 CHECK_INT (from); | |
1782 CHECK_INT (count); | |
1783 CHECK_INT (depth); | |
1784 buf = decode_buffer (buffer, 0); | |
1785 | |
1786 return scan_lists (buf, XINT (from), XINT (count), XINT (depth), 0, | |
444 | 1787 !NILP (noerror)); |
428 | 1788 } |
1789 | |
1790 DEFUN ("scan-sexps", Fscan_sexps, 2, 4, 0, /* | |
1791 Scan from character number FROM by COUNT balanced expressions. | |
1792 If COUNT is negative, scan backwards. | |
1793 Returns the character number of the position thus found. | |
1794 | |
1795 Comments are ignored if `parse-sexp-ignore-comments' is non-nil. | |
1796 | |
1797 If the beginning or end of (the accessible part of) the buffer is reached | |
1798 in the middle of a parenthetical grouping, an error is signaled. | |
1799 If the beginning or end is reached between groupings | |
1800 but before count is used up, nil is returned. | |
1801 | |
1802 If optional arg BUFFER is non-nil, scanning occurs in that buffer instead | |
1803 of in the current buffer. | |
1804 | |
1805 If optional arg NOERROR is non-nil, scan-sexps will return nil instead of | |
1806 signalling an error. | |
1807 */ | |
444 | 1808 (from, count, buffer, noerror)) |
428 | 1809 { |
1810 struct buffer *buf = decode_buffer (buffer, 0); | |
1811 CHECK_INT (from); | |
1812 CHECK_INT (count); | |
1813 | |
444 | 1814 return scan_lists (buf, XINT (from), XINT (count), 0, 1, !NILP (noerror)); |
428 | 1815 } |
1816 | |
1817 DEFUN ("backward-prefix-chars", Fbackward_prefix_chars, 0, 1, 0, /* | |
1818 Move point backward over any number of chars with prefix syntax. | |
1819 This includes chars with "quote" or "prefix" syntax (' or p). | |
1820 | |
1821 Optional arg BUFFER defaults to the current buffer. | |
1822 */ | |
1823 (buffer)) | |
1824 { | |
1825 struct buffer *buf = decode_buffer (buffer, 0); | |
665 | 1826 Charbpos beg = BUF_BEGV (buf); |
1827 Charbpos pos = BUF_PT (buf); | |
867 | 1828 Ichar c = '\0'; /* initialize to avoid compiler warnings */ |
826 | 1829 struct syntax_cache *scache; |
1830 | |
1831 scache = setup_buffer_syntax_cache (buf, pos, -1); | |
428 | 1832 |
1833 while (pos > beg && !char_quoted (buf, pos - 1) | |
460 | 1834 /* Previous statement updates syntax table. */ |
826 | 1835 && (SYNTAX_FROM_CACHE (scache, c = BUF_FETCH_CHAR (buf, pos - 1)) == Squote |
1836 || SYNTAX_CODE_PREFIX (SYNTAX_CODE_FROM_CACHE (scache, c)))) | |
428 | 1837 pos--; |
1838 | |
1839 BUF_SET_PT (buf, pos); | |
1840 | |
1841 return Qnil; | |
1842 } | |
1843 | |
1844 /* Parse forward from FROM to END, | |
1845 assuming that FROM has state OLDSTATE (nil means FROM is start of function), | |
1846 and return a description of the state of the parse at END. | |
1847 If STOPBEFORE is nonzero, stop at the start of an atom. | |
1848 If COMMENTSTOP is nonzero, stop at the start of a comment. */ | |
1849 | |
1850 static void | |
1851 scan_sexps_forward (struct buffer *buf, struct lisp_parse_state *stateptr, | |
665 | 1852 Charbpos from, Charbpos end, |
428 | 1853 int targetdepth, int stopbefore, |
1854 Lisp_Object oldstate, | |
1855 int commentstop) | |
1856 { | |
1857 struct lisp_parse_state state; | |
1858 | |
1859 enum syntaxcode code; | |
1860 struct level { int last, prev; }; | |
1861 struct level levelstart[100]; | |
1862 struct level *curlevel = levelstart; | |
1863 struct level *endlevel = levelstart + 100; | |
1864 int depth; /* Paren depth of current scanning location. | |
1865 level - levelstart equals this except | |
1866 when the depth becomes negative. */ | |
1867 int mindepth; /* Lowest DEPTH value seen. */ | |
1868 int start_quoted = 0; /* Nonzero means starting after a char quote */ | |
460 | 1869 int boundary_stop = commentstop == -1; |
428 | 1870 Lisp_Object tem; |
826 | 1871 struct syntax_cache *scache; |
1872 | |
1873 scache = setup_buffer_syntax_cache (buf, from, 1); | |
428 | 1874 if (NILP (oldstate)) |
1875 { | |
1876 depth = 0; | |
1877 state.instring = -1; | |
1878 state.incomment = 0; | |
1879 state.comstyle = 0; /* comment style a by default */ | |
460 | 1880 state.comstr_start = -1; /* no comment/string seen. */ |
428 | 1881 } |
1882 else | |
1883 { | |
1884 tem = Fcar (oldstate); /* elt 0, depth */ | |
1885 if (!NILP (tem)) | |
1886 depth = XINT (tem); | |
1887 else | |
1888 depth = 0; | |
1889 | |
1890 oldstate = Fcdr (oldstate); | |
1891 oldstate = Fcdr (oldstate); | |
1892 oldstate = Fcdr (oldstate); | |
1893 tem = Fcar (oldstate); /* elt 3, instring */ | |
460 | 1894 state.instring = ( !NILP (tem) |
1895 ? ( INTP (tem) ? XINT (tem) : ST_STRING_STYLE) | |
1896 : -1); | |
428 | 1897 |
460 | 1898 oldstate = Fcdr (oldstate); |
1899 tem = Fcar (oldstate); /* elt 4, incomment */ | |
428 | 1900 state.incomment = !NILP (tem); |
1901 | |
1902 oldstate = Fcdr (oldstate); | |
1903 tem = Fcar (oldstate); /* elt 5, follows-quote */ | |
1904 start_quoted = !NILP (tem); | |
1905 | |
1906 /* if the eighth element of the list is nil, we are in comment style | |
3025 | 1907 a; if it is t, we are in comment style b; if it is `syntax-table', |
460 | 1908 we are in a generic comment */ |
428 | 1909 oldstate = Fcdr (oldstate); |
1910 oldstate = Fcdr (oldstate); | |
460 | 1911 tem = Fcar (oldstate); /* elt 7, comment style a/b/fence */ |
1912 state.comstyle = NILP (tem) ? 0 : ( EQ (tem, Qsyntax_table) | |
1913 ? ST_COMMENT_STYLE : 1 ); | |
1914 | |
1915 oldstate = Fcdr (oldstate); /* elt 8, start of last comment/string */ | |
1916 tem = Fcar (oldstate); | |
1917 state.comstr_start = NILP (tem) ? -1 : XINT (tem); | |
1918 | |
1919 /* elt 9, char numbers of starts-of-expression of levels | |
1920 (starting from outermost). */ | |
1921 oldstate = Fcdr (oldstate); | |
1922 tem = Fcar (oldstate); /* elt 9, intermediate data for | |
1923 continuation of parsing (subject | |
1924 to change). */ | |
1925 while (!NILP (tem)) /* >= second enclosing sexps. */ | |
1926 { | |
1927 curlevel->last = XINT (Fcar (tem)); | |
1928 if (++curlevel == endlevel) | |
826 | 1929 stack_overflow ("Nesting too deep for parser", |
1930 make_int (curlevel - levelstart)); | |
460 | 1931 curlevel->prev = -1; |
1932 curlevel->last = -1; | |
1933 tem = Fcdr (tem); | |
1934 } | |
428 | 1935 } |
1936 state.quoted = 0; | |
1937 mindepth = depth; | |
1938 | |
1939 curlevel->prev = -1; | |
1940 curlevel->last = -1; | |
1941 | |
1942 /* Enter the loop at a place appropriate for initial state. */ | |
1943 | |
1944 if (state.incomment) goto startincomment; | |
1945 if (state.instring >= 0) | |
1946 { | |
1947 if (start_quoted) goto startquotedinstring; | |
1948 goto startinstring; | |
1949 } | |
1950 if (start_quoted) goto startquoted; | |
1951 | |
1952 while (from < end) | |
1953 { | |
867 | 1954 Ichar c; |
460 | 1955 int syncode; |
1956 | |
428 | 1957 QUIT; |
1958 | |
826 | 1959 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
460 | 1960 c = BUF_FETCH_CHAR (buf, from); |
826 | 1961 syncode = SYNTAX_CODE_FROM_CACHE (scache, c); |
1962 code = SYNTAX_FROM_CODE (syncode); | |
428 | 1963 from++; |
1964 | |
1965 /* record the comment style we have entered so that only the | |
1966 comment-ender sequence (or single char) of the same style | |
1967 actually terminates the comment section. */ | |
460 | 1968 if (code == Scomment) |
1969 { | |
1970 state.comstyle = | |
1971 SYNTAX_CODE_COMMENT_1CHAR_MASK (syncode) | |
1972 == SYNTAX_COMMENT_STYLE_A ? 0 : 1; | |
1973 state.comstr_start = from - 1; | |
1974 } | |
1975 | |
1976 /* a generic comment delimiter? */ | |
1977 else if (code == Scomment_fence) | |
1978 { | |
1979 state.comstyle = ST_COMMENT_STYLE; | |
1980 state.comstr_start = from - 1; | |
1981 code = Scomment; | |
428 | 1982 } |
1983 | |
1984 else if (from < end && | |
460 | 1985 SYNTAX_CODE_START_FIRST_P (syncode)) |
428 | 1986 { |
460 | 1987 int next_syncode; |
826 | 1988 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
460 | 1989 next_syncode = |
826 | 1990 SYNTAX_CODE_FROM_CACHE (scache, BUF_FETCH_CHAR (buf, from)); |
460 | 1991 |
1992 if (SYNTAX_CODES_START_P (syncode, next_syncode)) | |
1993 { | |
428 | 1994 code = Scomment; |
460 | 1995 state.comstyle = SYNTAX_CODES_COMMENT_MASK_START |
1996 (syncode, next_syncode) == SYNTAX_COMMENT_STYLE_A ? 0 : 1; | |
1997 state.comstr_start = from - 1; | |
428 | 1998 from++; |
826 | 1999 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
460 | 2000 } |
428 | 2001 } |
2002 | |
460 | 2003 if (SYNTAX_CODE_PREFIX (syncode)) |
428 | 2004 continue; |
2005 switch (code) | |
2006 { | |
2007 case Sescape: | |
2008 case Scharquote: | |
2009 if (stopbefore) goto stop; /* this arg means stop at sexp start */ | |
2010 curlevel->last = from - 1; | |
2011 startquoted: | |
2012 if (from == end) goto endquoted; | |
2013 from++; | |
2014 goto symstarted; | |
2015 /* treat following character as a word constituent */ | |
2016 case Sword: | |
2017 case Ssymbol: | |
2018 if (stopbefore) goto stop; /* this arg means stop at sexp start */ | |
2019 curlevel->last = from - 1; | |
2020 symstarted: | |
2021 while (from < end) | |
2022 { | |
826 | 2023 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
2024 switch (SYNTAX_FROM_CACHE (scache, BUF_FETCH_CHAR (buf, from))) | |
428 | 2025 { |
2026 case Scharquote: | |
2027 case Sescape: | |
2028 from++; | |
2029 if (from == end) goto endquoted; | |
2030 break; | |
2031 case Sword: | |
2032 case Ssymbol: | |
2033 case Squote: | |
2034 break; | |
2035 default: | |
2036 goto symdone; | |
2037 } | |
2038 from++; | |
2039 } | |
2040 symdone: | |
2041 curlevel->prev = curlevel->last; | |
2042 break; | |
2043 | |
2044 case Scomment: | |
2045 state.incomment = 1; | |
460 | 2046 if (commentstop || boundary_stop) goto done; |
428 | 2047 startincomment: |
460 | 2048 if (commentstop == 1) |
428 | 2049 goto done; |
826 | 2050 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
428 | 2051 { |
826 | 2052 Charbpos newfrom = find_end_of_comment (buf, from, end, |
2053 state.comstyle); | |
428 | 2054 if (newfrom < 0) |
2055 { | |
2056 /* we terminated search because from == end */ | |
2057 from = end; | |
2058 goto done; | |
2059 } | |
2060 from = newfrom; | |
2061 } | |
2062 state.incomment = 0; | |
2063 state.comstyle = 0; /* reset the comment style */ | |
460 | 2064 if (boundary_stop) goto done; |
428 | 2065 break; |
2066 | |
2067 case Sopen: | |
2068 if (stopbefore) goto stop; /* this arg means stop at sexp start */ | |
2069 depth++; | |
2070 curlevel->last = from - 1; | |
2071 if (++curlevel == endlevel) | |
826 | 2072 stack_overflow ("Nesting too deep for parser", |
2073 make_int (curlevel - levelstart)); | |
428 | 2074 curlevel->prev = -1; |
2075 curlevel->last = -1; | |
2076 if (targetdepth == depth) goto done; | |
2077 break; | |
2078 | |
2079 case Sclose: | |
2080 depth--; | |
2081 if (depth < mindepth) | |
2082 mindepth = depth; | |
2083 if (curlevel != levelstart) | |
2084 curlevel--; | |
2085 curlevel->prev = curlevel->last; | |
2086 if (targetdepth == depth) goto done; | |
2087 break; | |
2088 | |
2089 case Sstring: | |
460 | 2090 case Sstring_fence: |
2091 state.comstr_start = from - 1; | |
428 | 2092 if (stopbefore) goto stop; /* this arg means stop at sexp start */ |
2093 curlevel->last = from - 1; | |
460 | 2094 if (code == Sstring_fence) |
428 | 2095 { |
460 | 2096 state.instring = ST_STRING_STYLE; |
2097 } | |
2098 else | |
2099 { | |
2100 /* XEmacs change: call syntax_match() on character */ | |
867 | 2101 Ichar ch = BUF_FETCH_CHAR (buf, from - 1); |
460 | 2102 Lisp_Object stermobj = |
1296 | 2103 syntax_match (scache->syntax_table, ch); |
428 | 2104 |
2105 if (CHARP (stermobj)) | |
2106 state.instring = XCHAR (stermobj); | |
2107 else | |
2108 state.instring = ch; | |
2109 } | |
460 | 2110 if (boundary_stop) goto done; |
428 | 2111 startinstring: |
2112 while (1) | |
2113 { | |
460 | 2114 enum syntaxcode temp_code; |
2115 | |
428 | 2116 if (from >= end) goto done; |
460 | 2117 |
826 | 2118 UPDATE_SYNTAX_CACHE_FORWARD (scache, from); |
460 | 2119 c = BUF_FETCH_CHAR (buf, from); |
826 | 2120 temp_code = SYNTAX_FROM_CACHE (scache, c); |
460 | 2121 |
2122 if ( | |
2123 state.instring != ST_STRING_STYLE && | |
2124 temp_code == Sstring && | |
2125 c == state.instring) break; | |
2126 | |
2127 switch (temp_code) | |
428 | 2128 { |
460 | 2129 case Sstring_fence: |
2130 if (state.instring == ST_STRING_STYLE) | |
2131 goto string_end; | |
2132 break; | |
428 | 2133 case Scharquote: |
2134 case Sescape: | |
2135 { | |
2136 from++; | |
2137 startquotedinstring: | |
2138 if (from >= end) goto endquoted; | |
2139 break; | |
2140 } | |
2141 default: | |
2142 break; | |
2143 } | |
2144 from++; | |
2145 } | |
460 | 2146 string_end: |
428 | 2147 state.instring = -1; |
2148 curlevel->prev = curlevel->last; | |
2149 from++; | |
460 | 2150 if (boundary_stop) goto done; |
428 | 2151 break; |
2152 | |
2153 case Smath: | |
2154 break; | |
2155 | |
2156 case Swhitespace: | |
2157 case Spunct: | |
2158 case Squote: | |
2159 case Sendcomment: | |
460 | 2160 case Scomment_fence: |
428 | 2161 case Sinherit: |
2162 case Smax: | |
2163 break; | |
2164 } | |
2165 } | |
2166 goto done; | |
2167 | |
2168 stop: /* Here if stopping before start of sexp. */ | |
2169 from--; /* We have just fetched the char that starts it; */ | |
2170 goto done; /* but return the position before it. */ | |
2171 | |
2172 endquoted: | |
2173 state.quoted = 1; | |
2174 done: | |
2175 state.depth = depth; | |
2176 state.mindepth = mindepth; | |
2177 state.thislevelstart = curlevel->prev; | |
2178 state.prevlevelstart | |
2179 = (curlevel == levelstart) ? -1 : (curlevel - 1)->last; | |
2180 state.location = from; | |
460 | 2181 state.levelstarts = Qnil; |
2182 while (--curlevel >= levelstart) | |
2183 state.levelstarts = Fcons (make_int (curlevel->last), | |
2184 state.levelstarts); | |
428 | 2185 |
2186 *stateptr = state; | |
2187 } | |
2188 | |
2189 DEFUN ("parse-partial-sexp", Fparse_partial_sexp, 2, 7, 0, /* | |
2190 Parse Lisp syntax starting at FROM until TO; return status of parse at TO. | |
2191 Parsing stops at TO or when certain criteria are met; | |
2192 point is set to where parsing stops. | |
444 | 2193 If fifth arg OLDSTATE is omitted or nil, |
428 | 2194 parsing assumes that FROM is the beginning of a function. |
460 | 2195 Value is a list of nine elements describing final state of parsing: |
428 | 2196 0. depth in parens. |
2197 1. character address of start of innermost containing list; nil if none. | |
2198 2. character address of start of last complete sexp terminated. | |
2199 3. non-nil if inside a string. | |
460 | 2200 (It is the character that will terminate the string, |
2201 or t if the string should be terminated by an explicit | |
2202 `syntax-table' property.) | |
428 | 2203 4. t if inside a comment. |
2204 5. t if following a quote character. | |
2205 6. the minimum paren-depth encountered during this scan. | |
460 | 2206 7. nil if in comment style a, or not in a comment; t if in comment style b; |
2207 `syntax-table' if given by an explicit `syntax-table' property. | |
2208 8. character address of start of last comment or string; nil if none. | |
2209 9. Intermediate data for continuation of parsing (subject to change). | |
428 | 2210 If third arg TARGETDEPTH is non-nil, parsing stops if the depth |
2211 in parentheses becomes equal to TARGETDEPTH. | |
2212 Fourth arg STOPBEFORE non-nil means stop when come to | |
2213 any character that starts a sexp. | |
460 | 2214 Fifth arg OLDSTATE is a nine-element list like what this function returns. |
428 | 2215 It is used to initialize the state of the parse. Its second and third |
2216 elements are ignored. | |
460 | 2217 Sixth arg COMMENTSTOP non-nil means stop at the start of a comment. If it |
2218 is `syntax-table', stop after the start of a comment or a string, or after | |
2219 the end of a comment or string. | |
826 | 2220 Seventh arg BUFFER specifies the buffer to do the parsing in, and defaults |
2221 to the current buffer. | |
428 | 2222 */ |
2223 (from, to, targetdepth, stopbefore, oldstate, commentstop, buffer)) | |
2224 { | |
2225 struct lisp_parse_state state; | |
2226 int target; | |
665 | 2227 Charbpos start, end; |
428 | 2228 struct buffer *buf = decode_buffer (buffer, 0); |
2229 Lisp_Object val; | |
2230 | |
2231 if (!NILP (targetdepth)) | |
2232 { | |
2233 CHECK_INT (targetdepth); | |
2234 target = XINT (targetdepth); | |
2235 } | |
2236 else | |
2237 target = -100000; /* We won't reach this depth */ | |
2238 | |
2239 get_buffer_range_char (buf, from, to, &start, &end, 0); | |
2240 scan_sexps_forward (buf, &state, start, end, | |
2241 target, !NILP (stopbefore), oldstate, | |
460 | 2242 (NILP (commentstop) |
2243 ? 0 : (EQ (commentstop, Qsyntax_table) ? -1 : 1))); | |
428 | 2244 BUF_SET_PT (buf, state.location); |
2245 | |
2246 /* reverse order */ | |
2247 val = Qnil; | |
460 | 2248 val = Fcons (state.levelstarts, val); |
2249 val = Fcons ((state.incomment || (state.instring >= 0)) | |
2250 ? make_int (state.comstr_start) : Qnil, val); | |
2251 val = Fcons (state.comstyle ? (state.comstyle == ST_COMMENT_STYLE | |
2252 ? Qsyntax_table : Qt) : Qnil, val); | |
428 | 2253 val = Fcons (make_int (state.mindepth), val); |
2254 val = Fcons (state.quoted ? Qt : Qnil, val); | |
2255 val = Fcons (state.incomment ? Qt : Qnil, val); | |
460 | 2256 val = Fcons (state.instring < 0 |
2257 ? Qnil | |
2258 : (state.instring == ST_STRING_STYLE | |
2259 ? Qt : make_int (state.instring)), val); | |
826 | 2260 val = Fcons (state.thislevelstart < 0 ? Qnil : |
2261 make_int (state.thislevelstart), val); | |
2262 val = Fcons (state.prevlevelstart < 0 ? Qnil : | |
2263 make_int (state.prevlevelstart), val); | |
428 | 2264 val = Fcons (make_int (state.depth), val); |
2265 | |
2266 return val; | |
2267 } | |
2268 | |
2269 | |
2270 /* Updating of the mirror syntax table. | |
2271 | |
1296 | 2272 Each syntax table has a corresponding mirror table in it. Whenever we |
2273 make a change to a syntax table, we set a dirty flag. When accessing a | |
2274 value from the mirror table and the table is dirty, we call | |
2275 update_syntax_table() to clean it up. | |
428 | 2276 |
2277 #### We really only need to map over the changed range. | |
2278 | |
2279 If we change the standard syntax table, we need to map over | |
2280 all tables because any of them could be inheriting from the | |
2281 standard syntax table. | |
2282 | |
2283 When `set-syntax-table' is called, we set the buffer's mirror | |
2284 syntax table as well. | |
2285 */ | |
2286 | |
826 | 2287 static int |
2286 | 2288 copy_to_mirrortab (struct chartab_range *range, Lisp_Object UNUSED (table), |
826 | 2289 Lisp_Object val, void *arg) |
428 | 2290 { |
826 | 2291 Lisp_Object mirrortab = VOID_TO_LISP (arg); |
428 | 2292 |
2293 if (CONSP (val)) | |
2294 val = XCAR (val); | |
826 | 2295 if (SYNTAX_FROM_CODE (XINT (val)) != Sinherit) |
2296 put_char_table (mirrortab, range, val); | |
2297 return 0; | |
2298 } | |
2299 | |
2300 static int | |
2286 | 2301 copy_if_not_already_present (struct chartab_range *range, |
2302 Lisp_Object UNUSED (table), | |
826 | 2303 Lisp_Object val, void *arg) |
2304 { | |
1296 | 2305 Lisp_Object mirrortab = VOID_TO_LISP (arg); |
826 | 2306 if (CONSP (val)) |
2307 val = XCAR (val); | |
2308 if (SYNTAX_FROM_CODE (XINT (val)) != Sinherit) | |
2309 { | |
2310 Lisp_Object existing = | |
1296 | 2311 updating_mirror_get_range_char_table (range, mirrortab, |
2312 Vbogus_syntax_table_value); | |
826 | 2313 if (NILP (existing)) |
2314 /* nothing at all */ | |
1296 | 2315 put_char_table (mirrortab, range, val); |
2316 else if (!EQ (existing, Vbogus_syntax_table_value)) | |
826 | 2317 /* full */ |
2318 ; | |
2319 else | |
2320 { | |
2321 Freset_char_table (Vtemp_table_for_use_updating_syntax_tables); | |
2322 copy_char_table_range | |
1296 | 2323 (mirrortab, Vtemp_table_for_use_updating_syntax_tables, range); |
2324 put_char_table (mirrortab, range, val); | |
826 | 2325 copy_char_table_range |
1296 | 2326 (Vtemp_table_for_use_updating_syntax_tables, mirrortab, range); |
826 | 2327 } |
428 | 2328 } |
826 | 2329 |
428 | 2330 return 0; |
2331 } | |
2332 | |
2333 static void | |
826 | 2334 update_just_this_syntax_table (Lisp_Object table) |
428 | 2335 { |
2336 struct chartab_range range; | |
826 | 2337 Lisp_Object mirrortab = XCHAR_TABLE (table)->mirror_table; |
2338 | |
1296 | 2339 assert (!XCHAR_TABLE (table)->mirror_table_p); |
826 | 2340 range.type = CHARTAB_RANGE_ALL; |
2341 Freset_char_table (mirrortab); | |
1296 | 2342 |
826 | 2343 /* First, copy the tables values other than inherit into the mirror |
2344 table. Then, for tables other than the standard syntax table, map | |
2345 over the standard table, copying values into the mirror table only if | |
2346 entries don't already exist in that table. (The copying step requires | |
2347 another mapping.) | |
2348 */ | |
428 | 2349 |
826 | 2350 map_char_table (table, &range, copy_to_mirrortab, LISP_TO_VOID (mirrortab)); |
2351 /* second clause catches bootstrapping problems when initializing the | |
2352 standard syntax table */ | |
2353 if (!EQ (table, Vstandard_syntax_table) && !NILP (Vstandard_syntax_table)) | |
1296 | 2354 map_char_table (Vstandard_syntax_table, &range, |
2355 copy_if_not_already_present, LISP_TO_VOID (mirrortab)); | |
3152 | 2356 /* The resetting made the default be Qnil. Put it back to Sword. */ |
2357 set_char_table_default (mirrortab, make_int (Sword)); | |
1296 | 2358 XCHAR_TABLE (mirrortab)->dirty = 0; |
428 | 2359 } |
2360 | |
2361 /* Called from chartab.c when a change is made to a syntax table. | |
2362 If this is the standard syntax table, we need to recompute | |
2363 *all* syntax tables (yuck). Otherwise we just recompute this | |
2364 one. */ | |
2365 | |
2366 void | |
826 | 2367 update_syntax_table (Lisp_Object table) |
428 | 2368 { |
1296 | 2369 Lisp_Object nonmirror = XCHAR_TABLE (table)->mirror_table; |
2370 assert (XCHAR_TABLE (table)->mirror_table_p); | |
2371 if (EQ (nonmirror, Vstandard_syntax_table)) | |
428 | 2372 { |
2373 Lisp_Object syntab; | |
2374 | |
2375 for (syntab = Vall_syntax_tables; !NILP (syntab); | |
2376 syntab = XCHAR_TABLE (syntab)->next_table) | |
826 | 2377 update_just_this_syntax_table (syntab); |
428 | 2378 } |
2379 else | |
1296 | 2380 update_just_this_syntax_table (nonmirror); |
428 | 2381 } |
2382 | |
2383 | |
2384 /************************************************************************/ | |
2385 /* initialization */ | |
2386 /************************************************************************/ | |
2387 | |
2388 void | |
2389 syms_of_syntax (void) | |
2390 { | |
3092 | 2391 #ifdef NEW_GC |
5118
e0db3c197671
merge up to latest default branch, doesn't compile yet
Ben Wing <ben@xemacs.org>
parents:
4759
diff
changeset
|
2392 INIT_LISP_OBJECT (syntax_cache); |
3092 | 2393 #endif /* NEW_GC */ |
563 | 2394 DEFSYMBOL (Qsyntax_table_p); |
2395 DEFSYMBOL (Qsyntax_table); | |
428 | 2396 |
2397 DEFSUBR (Fsyntax_table_p); | |
2398 DEFSUBR (Fsyntax_table); | |
826 | 2399 #ifdef DEBUG_XEMACS |
2400 DEFSUBR (Fmirror_syntax_table); | |
2401 DEFSUBR (Fsyntax_cache_info); | |
2402 #endif /* DEBUG_XEMACS */ | |
428 | 2403 DEFSUBR (Fstandard_syntax_table); |
2404 DEFSUBR (Fcopy_syntax_table); | |
2405 DEFSUBR (Fset_syntax_table); | |
2406 DEFSUBR (Fsyntax_designator_chars); | |
2407 DEFSUBR (Fchar_syntax); | |
2408 DEFSUBR (Fmatching_paren); | |
2409 /* DEFSUBR (Fmodify_syntax_entry); now in Lisp. */ | |
2410 /* DEFSUBR (Fdescribe_syntax); now in Lisp. */ | |
2411 | |
2412 DEFSUBR (Fforward_word); | |
2413 | |
2414 DEFSUBR (Fforward_comment); | |
2415 DEFSUBR (Fscan_lists); | |
2416 DEFSUBR (Fscan_sexps); | |
2417 DEFSUBR (Fbackward_prefix_chars); | |
2418 DEFSUBR (Fparse_partial_sexp); | |
2419 } | |
2420 | |
2421 void | |
2422 vars_of_syntax (void) | |
2423 { | |
2424 DEFVAR_BOOL ("parse-sexp-ignore-comments", &parse_sexp_ignore_comments /* | |
2425 Non-nil means `forward-sexp', etc., should treat comments as whitespace. | |
2426 */ ); | |
434 | 2427 parse_sexp_ignore_comments = 0; |
428 | 2428 |
460 | 2429 DEFVAR_BOOL ("lookup-syntax-properties", &lookup_syntax_properties /* |
826 | 2430 Non-nil means `forward-sexp', etc., respect the `syntax-table' property. |
2431 This property can be placed on buffers or strings and can be used to explicitly | |
2432 specify the syntax table to be used for looking up the syntax of the chars | |
2433 having this property, or to directly specify the syntax of the chars. | |
2434 | |
460 | 2435 The value of this property should be either a syntax table, or a cons |
2436 of the form (SYNTAXCODE . MATCHCHAR), SYNTAXCODE being the numeric | |
2437 syntax code, MATCHCHAR being nil or the character to match (which is | |
826 | 2438 relevant only when the syntax code is open/close-type). |
460 | 2439 */ ); |
2440 lookup_syntax_properties = 1; | |
2441 | |
428 | 2442 DEFVAR_BOOL ("words-include-escapes", &words_include_escapes /* |
2443 Non-nil means `forward-word', etc., should treat escape chars part of words. | |
2444 */ ); | |
434 | 2445 words_include_escapes = 0; |
428 | 2446 |
2447 no_quit_in_re_search = 0; | |
1296 | 2448 |
2449 Vbogus_syntax_table_value = make_float (0.0); | |
2450 staticpro (&Vbogus_syntax_table_value); | |
428 | 2451 } |
2452 | |
2453 static void | |
3540 | 2454 define_standard_syntax (const UExtbyte *p, enum syntaxcode syn) |
428 | 2455 { |
2456 for (; *p; p++) | |
2457 Fput_char_table (make_char (*p), make_int (syn), Vstandard_syntax_table); | |
2458 } | |
2459 | |
2460 void | |
2461 complex_vars_of_syntax (void) | |
2462 { | |
867 | 2463 Ichar i; |
3540 | 2464 const UExtbyte *p; /* Latin-1, not internal format. */ |
2465 | |
2466 #define SET_RANGE_SYNTAX(start, end, syntax) \ | |
2467 do { \ | |
2468 for (i = start; i <= end; i++) \ | |
2469 Fput_char_table(make_char(i), make_int(syntax), \ | |
2470 Vstandard_syntax_table); \ | |
2471 } while (0) | |
2472 | |
2473 /* Set this now, so first buffer creation can refer to it. | |
2474 | |
2475 Make it nil before calling copy-syntax-table so that copy-syntax-table | |
2476 will know not to try to copy from garbage */ | |
428 | 2477 Vstandard_syntax_table = Qnil; |
2478 Vstandard_syntax_table = Fcopy_syntax_table (Qnil); | |
2479 staticpro (&Vstandard_syntax_table); | |
2480 | |
826 | 2481 Vtemp_table_for_use_updating_syntax_tables = Fmake_char_table (Qgeneric); |
2482 staticpro (&Vtemp_table_for_use_updating_syntax_tables); | |
2483 | |
428 | 2484 Vsyntax_designator_chars_string = make_string_nocopy (syntax_code_spec, |
2485 Smax); | |
2486 staticpro (&Vsyntax_designator_chars_string); | |
2487 | |
3540 | 2488 /* Default character syntax is word. */ |
3152 | 2489 set_char_table_default (Vstandard_syntax_table, make_int (Sword)); |
428 | 2490 |
3540 | 2491 /* Control 0; treat as punctuation */ |
2492 SET_RANGE_SYNTAX(0, 32, Spunct); | |
428 | 2493 |
3544 | 2494 /* The whitespace--overwriting some of the above changes. |
2495 | |
2496 String literals are const char *s, not const unsigned char *s. */ | |
4653
25e5e5346d31
?\012 is whitespace, as it always should have been, thank you Karl Kleinpaste.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4141
diff
changeset
|
2497 define_standard_syntax((const UExtbyte *)" \t\015\014\012", Swhitespace); |
3540 | 2498 |
2499 /* DEL plus Control 1 */ | |
2500 SET_RANGE_SYNTAX(127, 159, Spunct); | |
2501 | |
3544 | 2502 define_standard_syntax ((const UExtbyte *)"\"", Sstring); |
2503 define_standard_syntax ((const UExtbyte *)"\\", Sescape); | |
2504 define_standard_syntax ((const UExtbyte *)"_-+*/&|<>=", Ssymbol); | |
2505 define_standard_syntax ((const UExtbyte *)".,;:?!#@~^'`", Spunct); | |
428 | 2506 |
3544 | 2507 for (p = (const UExtbyte *)"()[]{}"; *p; p+=2) |
428 | 2508 { |
2509 Fput_char_table (make_char (p[0]), | |
2510 Fcons (make_int (Sopen), make_char (p[1])), | |
2511 Vstandard_syntax_table); | |
2512 Fput_char_table (make_char (p[1]), | |
2513 Fcons (make_int (Sclose), make_char (p[0])), | |
2514 Vstandard_syntax_table); | |
2515 } | |
3540 | 2516 |
2517 /* Latin 1 "symbols." This contrasts with the FSF, where they're word | |
2518 constituents. */ | |
2519 SET_RANGE_SYNTAX(0240, 0277, Ssymbol); | |
2520 | |
2521 /* The guillemets. These are not parentheses, in contrast to what the old | |
2522 code did. */ | |
3569 | 2523 define_standard_syntax((const UExtbyte *)"\253\273", Spunct); |
3540 | 2524 |
2525 /* The inverted exclamation mark, and the multiplication and division | |
2526 signs. */ | |
3544 | 2527 define_standard_syntax((const UExtbyte *)"\241\327\367", Spunct); |
3540 | 2528 |
2529 #undef SET_RANGE_SYNTAX | |
428 | 2530 } |