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