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