428
|
1 /* Routines to compute the current syntactic context, for font-lock mode.
|
|
2 Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
|
|
3 Copyright (C) 1995 Sun Microsystems, Inc.
|
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Not in FSF. */
|
|
23
|
|
24 /* This code computes the syntactic context of the current point, that is,
|
|
25 whether point is within a comment, a string, what have you. It does
|
|
26 this by picking a point "known" to be outside of any syntactic constructs
|
|
27 and moving forward, examining the syntax of each character.
|
|
28
|
|
29 Two caches are used: one caches the last point computed, and the other
|
|
30 caches the last point at the beginning of a line. This makes there
|
|
31 be little penalty for moving left-to-right on a line a character at a
|
|
32 time; makes starting over on a line be cheap; and makes random-accessing
|
|
33 within a line relatively cheap.
|
|
34
|
|
35 When we move to a different line farther down in the file (but within the
|
|
36 current top-level form) we simply continue computing forward. If we move
|
|
37 backward more than a line, or move beyond the end of the current tlf, or
|
|
38 switch buffers, then we call `beginning-of-defun' and start over from
|
|
39 there.
|
|
40
|
|
41 #### We should really rewrite this to keep extents over the buffer
|
|
42 that hold the current syntactic information. This would be a big win.
|
|
43 This way there would be no guessing or incorrect results.
|
|
44 */
|
|
45
|
|
46 #include <config.h>
|
|
47 #include "lisp.h"
|
|
48
|
|
49 #include "buffer.h"
|
|
50 #include "insdel.h"
|
|
51 #include "syntax.h"
|
|
52
|
|
53 Lisp_Object Qcomment;
|
|
54 Lisp_Object Qblock_comment;
|
|
55 Lisp_Object Qbeginning_of_defun;
|
|
56
|
|
57 enum syntactic_context
|
|
58 {
|
|
59 context_none,
|
|
60 context_string,
|
|
61 context_comment,
|
460
|
62 context_block_comment,
|
|
63 context_generic_comment,
|
|
64 context_generic_string
|
428
|
65 };
|
|
66
|
|
67 enum block_comment_context
|
|
68 {
|
|
69 ccontext_none,
|
|
70 ccontext_start1,
|
|
71 ccontext_start2,
|
|
72 ccontext_end1
|
|
73 };
|
|
74
|
|
75 enum comment_style
|
|
76 {
|
|
77 comment_style_none,
|
|
78 comment_style_a,
|
|
79 comment_style_b
|
|
80 };
|
|
81
|
|
82 struct context_cache
|
|
83 {
|
665
|
84 Charbpos start_point; /* beginning of defun */
|
|
85 Charbpos cur_point; /* cache location */
|
|
86 Charbpos end_point; /* end of defun */
|
428
|
87 struct buffer *buffer; /* does this need to be staticpro'd? */
|
|
88 enum syntactic_context context; /* single-char-syntax state */
|
|
89 enum block_comment_context ccontext; /* block-comment state */
|
|
90 enum comment_style style; /* which comment group */
|
|
91 Emchar scontext; /* active string delimiter */
|
|
92 int depth; /* depth in parens */
|
|
93 int backslash_p; /* just read a backslash */
|
|
94 int needs_its_head_reexamined; /* we're apparently outside of
|
|
95 a top level form, and far away
|
|
96 from it. This is a bad situation
|
|
97 because it will lead to constant
|
|
98 slowness as we keep going way
|
|
99 back to that form and moving
|
|
100 forward again. In this case,
|
|
101 we try to compute a "pseudo-
|
|
102 top-level-form" where the
|
|
103 depth is 0 and the context
|
|
104 is none at both ends. */
|
|
105 };
|
|
106
|
|
107 /* We have two caches; one for the current point and one for
|
|
108 the beginning of line. We used to rely on the caller to
|
|
109 tell us when to invalidate them, but now we do it ourselves;
|
|
110 it lets us be smarter. */
|
|
111
|
|
112 static struct context_cache context_cache;
|
|
113
|
|
114 static struct context_cache bol_context_cache;
|
|
115
|
|
116 int font_lock_debug;
|
|
117
|
|
118 #define reset_context_cache(cc) memset (cc, 0, sizeof (struct context_cache))
|
|
119
|
|
120 /* This function is called from signal_after_change() to tell us when
|
|
121 textual changes are made so we can flush our caches when necessary.
|
|
122
|
|
123 We make the following somewhat heuristic assumptions:
|
|
124
|
|
125 (remember that current_point is always >= start_point, but may be
|
|
126 less than or greater than end_point (we might not be inside any
|
|
127 top-level form)).
|
|
128
|
|
129 1) Textual changes before the beginning of the current top-level form
|
|
130 don't affect anything; all we need to do is offset the caches
|
|
131 appropriately.
|
|
132 2) Textual changes right at the beginning of the current
|
|
133 top-level form messes things up and requires that we flush
|
|
134 the caches.
|
|
135 3) Textual changes after the beginning of the current top-level form
|
|
136 and before one or both or the caches invalidates the corresponding
|
|
137 cache(s).
|
|
138 4) Textual changes after the caches and before the end of the
|
|
139 current top-level form don't affect anything; all we need to do is
|
|
140 offset the caches appropriately.
|
|
141 5) Textual changes right at the end of the current top-level form
|
|
142 necessitate recomputing that end value.
|
|
143 6) Textual changes after the end of the current top-level form
|
|
144 are ignored. */
|
|
145
|
|
146
|
|
147 void
|
665
|
148 font_lock_maybe_update_syntactic_caches (struct buffer *buf, Charbpos start,
|
|
149 Charbpos orig_end, Charbpos new_end)
|
428
|
150 {
|
|
151 /* Note: either both context_cache and bol_context_cache are valid and
|
|
152 point to the same buffer, or both are invalid. If we have to
|
|
153 invalidate just context_cache, we recopy it from bol_context_cache.
|
|
154 */
|
|
155 if (context_cache.buffer != buf)
|
|
156 /* caches don't apply */
|
|
157 return;
|
|
158 /* NOTE: The order of the if statements below is important. If you
|
|
159 change them around unthinkingly, you will probably break something. */
|
|
160 if (orig_end <= context_cache.start_point - 1)
|
|
161 {
|
|
162 /* case 1: before the beginning of the current top-level form */
|
|
163 Charcount diff = new_end - orig_end;
|
|
164 if (font_lock_debug)
|
|
165 stderr_out ("font-lock; Case 1\n");
|
|
166 context_cache.start_point += diff;
|
|
167 context_cache.cur_point += diff;
|
|
168 context_cache.end_point += diff;
|
|
169 bol_context_cache.start_point += diff;
|
|
170 bol_context_cache.cur_point += diff;
|
|
171 bol_context_cache.end_point += diff;
|
|
172 }
|
|
173 else if (start <= context_cache.start_point)
|
|
174 {
|
|
175 if (font_lock_debug)
|
|
176 stderr_out ("font-lock; Case 2\n");
|
|
177 /* case 2: right at the current top-level form (paren that starts
|
|
178 top level form got deleted or moved away from the newline it
|
|
179 was touching) */
|
|
180 reset_context_cache (&context_cache);
|
|
181 reset_context_cache (&bol_context_cache);
|
|
182 }
|
|
183 /* OK, now we know that the start is after the beginning of the
|
|
184 current top-level form. */
|
|
185 else if (start < bol_context_cache.cur_point)
|
|
186 {
|
|
187 if (font_lock_debug)
|
|
188 stderr_out ("font-lock; Case 3 (1)\n");
|
|
189 /* case 3: after the beginning of the current top-level form
|
|
190 and before both of the caches */
|
|
191 reset_context_cache (&context_cache);
|
|
192 reset_context_cache (&bol_context_cache);
|
|
193 }
|
|
194 else if (start < context_cache.cur_point)
|
|
195 {
|
|
196 if (font_lock_debug)
|
|
197 stderr_out ("font-lock; Case 3 (2)\n");
|
|
198 /* case 3: but only need to invalidate one cache */
|
|
199 context_cache = bol_context_cache;
|
|
200 }
|
|
201 /* OK, now we know that the start is after the caches. */
|
|
202 else if (start >= context_cache.end_point)
|
|
203 {
|
|
204 if (font_lock_debug)
|
|
205 stderr_out ("font-lock; Case 6\n");
|
|
206 /* case 6: after the end of the current top-level form
|
|
207 and after the caches. */
|
|
208 }
|
|
209 else if (orig_end <= context_cache.end_point - 2)
|
|
210 {
|
|
211 /* case 4: after the caches and before the end of the
|
|
212 current top-level form */
|
|
213 Charcount diff = new_end - orig_end;
|
|
214 if (font_lock_debug)
|
|
215 stderr_out ("font-lock; Case 4\n");
|
|
216 context_cache.end_point += diff;
|
|
217 bol_context_cache.end_point += diff;
|
|
218 }
|
|
219 else
|
|
220 {
|
|
221 if (font_lock_debug)
|
|
222 stderr_out ("font-lock; Case 5\n");
|
|
223 /* case 5: right at the end of the current top-level form */
|
|
224 context_cache.end_point = context_cache.start_point - 1;
|
|
225 bol_context_cache.end_point = context_cache.start_point - 1;
|
|
226 }
|
|
227 }
|
|
228
|
|
229 /* This function is called from Fkill_buffer(). */
|
|
230
|
|
231 void
|
|
232 font_lock_buffer_was_killed (struct buffer *buf)
|
|
233 {
|
|
234 if (context_cache.buffer == buf)
|
|
235 {
|
|
236 reset_context_cache (&context_cache);
|
|
237 reset_context_cache (&bol_context_cache);
|
|
238 }
|
|
239 }
|
|
240
|
665
|
241 static Charbpos
|
|
242 beginning_of_defun (struct buffer *buf, Charbpos pt)
|
428
|
243 {
|
|
244 /* This function can GC */
|
665
|
245 Charbpos opt = BUF_PT (buf);
|
428
|
246 if (pt == BUF_BEGV (buf))
|
|
247 return pt;
|
|
248 BUF_SET_PT (buf, pt);
|
|
249 /* There used to be some kludginess to call c++-beginning-of-defun
|
|
250 if we're in C++ mode. There's no point in this any more;
|
|
251 we're using cc-mode. If you really want to get the old c++
|
|
252 mode working, fix it rather than the C code. */
|
|
253 call0_in_buffer (buf, Qbeginning_of_defun);
|
|
254 pt = BUF_PT (buf);
|
|
255 BUF_SET_PT (buf, opt);
|
|
256 return pt;
|
|
257 }
|
|
258
|
665
|
259 static Charbpos
|
|
260 end_of_defun (struct buffer *buf, Charbpos pt)
|
428
|
261 {
|
|
262 Lisp_Object retval = scan_lists (buf, pt, 1, 0, 0, 1);
|
|
263 if (NILP (retval))
|
|
264 return BUF_ZV (buf);
|
|
265 else
|
|
266 return XINT (retval);
|
|
267 }
|
|
268
|
|
269 /* Set up context_cache for attempting to determine the syntactic context
|
|
270 in buffer BUF at point PT. */
|
|
271
|
|
272 static void
|
665
|
273 setup_context_cache (struct buffer *buf, Charbpos pt)
|
428
|
274 {
|
|
275 int recomputed_start_point = 0;
|
|
276 /* This function can GC */
|
|
277 if (context_cache.buffer != buf || pt < context_cache.start_point)
|
|
278 {
|
|
279 start_over:
|
|
280 if (font_lock_debug)
|
|
281 stderr_out ("reset context cache\n");
|
|
282 /* OK, completely invalid. */
|
|
283 reset_context_cache (&context_cache);
|
|
284 reset_context_cache (&bol_context_cache);
|
|
285 }
|
|
286 if (!context_cache.buffer)
|
|
287 {
|
|
288 /* Need to recompute the start point. */
|
|
289 if (font_lock_debug)
|
|
290 stderr_out ("recompute start\n");
|
|
291 context_cache.start_point = beginning_of_defun (buf, pt);
|
|
292 recomputed_start_point = 1;
|
|
293 bol_context_cache.start_point = context_cache.start_point;
|
|
294 bol_context_cache.buffer = context_cache.buffer = buf;
|
|
295 }
|
|
296 if (context_cache.end_point < context_cache.start_point)
|
|
297 {
|
|
298 /* Need to recompute the end point. */
|
|
299 if (font_lock_debug)
|
|
300 stderr_out ("recompute end\n");
|
|
301 context_cache.end_point = end_of_defun (buf, context_cache.start_point);
|
|
302 bol_context_cache.end_point = context_cache.end_point;
|
|
303 }
|
|
304 if (bol_context_cache.cur_point == 0 ||
|
|
305 pt < bol_context_cache.cur_point)
|
|
306 {
|
|
307 if (font_lock_debug)
|
|
308 stderr_out ("reset to start\n");
|
|
309 if (pt > context_cache.end_point
|
|
310 /* 3000 is some arbitrary delta but seems reasonable;
|
|
311 about the size of a reasonable function */
|
|
312 && pt - context_cache.end_point > 3000)
|
|
313 /* If we're far past the end of the top level form,
|
|
314 don't trust it; recompute it. */
|
|
315 {
|
|
316 /* But don't get in an infinite loop doing this.
|
|
317 If we're really far past the end of the top level
|
|
318 form, try to compute a pseudo-top-level form. */
|
|
319 if (recomputed_start_point)
|
|
320 context_cache.needs_its_head_reexamined = 1;
|
|
321 else
|
|
322 /* force recomputation */
|
|
323 goto start_over;
|
|
324 }
|
|
325 /* Go to the nearest end of the top-level form that's before
|
|
326 us. */
|
|
327 if (pt > context_cache.end_point)
|
|
328 pt = context_cache.end_point;
|
|
329 else
|
|
330 pt = context_cache.start_point;
|
|
331 /* Reset current point to start of buffer. */
|
|
332 context_cache.cur_point = pt;
|
|
333 context_cache.context = context_none;
|
|
334 context_cache.ccontext = ccontext_none;
|
|
335 context_cache.style = comment_style_none;
|
|
336 context_cache.scontext = '\000';
|
|
337 context_cache.depth = 0;
|
460
|
338 /* #### shouldn't this be checking the character's syntax instead of
|
|
339 explicitly testing for backslash characters? */
|
428
|
340 context_cache.backslash_p = ((pt > 1) &&
|
|
341 (BUF_FETCH_CHAR (buf, pt - 1) == '\\'));
|
|
342 /* Note that the BOL context cache may not be at the beginning
|
|
343 of the line, but that should be OK, nobody's checking. */
|
|
344 bol_context_cache = context_cache;
|
|
345 return;
|
|
346 }
|
|
347 else if (pt < context_cache.cur_point)
|
|
348 {
|
|
349 if (font_lock_debug)
|
|
350 stderr_out ("reset to bol\n");
|
|
351 /* bol cache is OK but current_cache is not. */
|
|
352 context_cache = bol_context_cache;
|
|
353 return;
|
|
354 }
|
|
355 else if (pt <= context_cache.end_point)
|
|
356 {
|
|
357 if (font_lock_debug)
|
|
358 stderr_out ("everything is OK\n");
|
|
359 /* in same top-level form. */
|
|
360 return;
|
|
361 }
|
|
362 {
|
|
363 /* OK, we're past the end of the top-level form. */
|
665
|
364 Charbpos maxpt = max (context_cache.end_point, context_cache.cur_point);
|
428
|
365 #if 0
|
|
366 int shortage;
|
|
367 #endif
|
|
368
|
|
369 if (font_lock_debug)
|
|
370 stderr_out ("past end\n");
|
|
371 if (pt <= maxpt)
|
|
372 /* OK, fine. */
|
|
373 return;
|
|
374 #if 0
|
442
|
375 /* This appears to cause huge slowdowns in files which have no
|
|
376 top-level forms.
|
428
|
377
|
|
378 In any case, it's not really necessary that we know for
|
|
379 sure the top-level form we're in; if we're in a form
|
|
380 but the form we have recorded is the previous one,
|
|
381 it will be OK. */
|
|
382
|
|
383 scan_buffer (buf, '\n', maxpt, pt, 1, &shortage, 1);
|
|
384 if (!shortage)
|
|
385 /* If there was a newline in the region past the known universe,
|
|
386 we might be inside another top-level form, so start over.
|
|
387 Otherwise, we're outside of any top-level forms and we know
|
|
388 the one directly before us, so it's OK. */
|
|
389 goto start_over;
|
|
390 #endif
|
|
391 }
|
|
392 }
|
|
393
|
647
|
394 /* You'd think it wouldn't be necessary to cast something to the type
|
|
395 it's already defined is, but if you're GCC, you apparently think
|
|
396 differently */
|
460
|
397 #define SYNTAX_START_STYLE(c1, c2) \
|
647
|
398 ((enum comment_style) \
|
460
|
399 (SYNTAX_CODES_MATCH_START_P (c1, c2, SYNTAX_COMMENT_STYLE_A) ? \
|
428
|
400 comment_style_a : \
|
460
|
401 SYNTAX_CODES_MATCH_START_P (c1, c2, SYNTAX_COMMENT_STYLE_B) ? \
|
428
|
402 comment_style_b : \
|
647
|
403 comment_style_none))
|
428
|
404
|
460
|
405 #define SYNTAX_END_STYLE(c1, c2) \
|
647
|
406 ((enum comment_style) \
|
|
407 (SYNTAX_CODES_MATCH_END_P (c1, c2, SYNTAX_COMMENT_STYLE_A) ? \
|
460
|
408 comment_style_a : \
|
|
409 SYNTAX_CODES_MATCH_END_P (c1, c2, SYNTAX_COMMENT_STYLE_B) ? \
|
|
410 comment_style_b : \
|
647
|
411 comment_style_none))
|
460
|
412
|
|
413 #define SINGLE_SYNTAX_STYLE(c) \
|
647
|
414 ((enum comment_style) \
|
|
415 (SYNTAX_CODE_MATCHES_1CHAR_P (c, SYNTAX_COMMENT_STYLE_A) ? \
|
428
|
416 comment_style_a : \
|
460
|
417 SYNTAX_CODE_MATCHES_1CHAR_P (c, SYNTAX_COMMENT_STYLE_B) ? \
|
428
|
418 comment_style_b : \
|
647
|
419 comment_style_none))
|
428
|
420
|
|
421 /* Set up context_cache for position PT in BUF. */
|
|
422
|
|
423 static void
|
665
|
424 find_context (struct buffer *buf, Charbpos pt)
|
428
|
425 {
|
|
426 /* This function can GC */
|
460
|
427 #ifndef emacs
|
440
|
428 Lisp_Char_Table *mirrortab = XCHAR_TABLE (buf->mirror_syntax_table);
|
428
|
429 Lisp_Object syntaxtab = buf->syntax_table;
|
460
|
430 #endif
|
428
|
431 Emchar prev_c, c;
|
460
|
432 int prev_syncode, syncode;
|
665
|
433 Charbpos target = pt;
|
428
|
434 setup_context_cache (buf, pt);
|
|
435 pt = context_cache.cur_point;
|
|
436
|
460
|
437 SETUP_SYNTAX_CACHE (pt - 1, 1);
|
428
|
438 if (pt > BUF_BEGV (buf))
|
460
|
439 {
|
|
440 c = BUF_FETCH_CHAR (buf, pt - 1);
|
|
441 syncode = SYNTAX_CODE_FROM_CACHE (mirrortab, c);
|
|
442 }
|
428
|
443 else
|
460
|
444 {
|
|
445 c = '\n'; /* to get bol_context_cache at point-min */
|
|
446 syncode = Swhitespace;
|
|
447 }
|
428
|
448
|
|
449 for (; pt < target; pt++, context_cache.cur_point = pt)
|
|
450 {
|
|
451 if (context_cache.needs_its_head_reexamined)
|
|
452 {
|
|
453 if (context_cache.depth == 0
|
|
454 && context_cache.context == context_none)
|
|
455 {
|
|
456 /* We've found an anchor spot.
|
|
457 Try to put the start of defun within 6000 chars of
|
|
458 the target, and the end of defun as close as possible.
|
|
459 6000 is also arbitrary but tries to strike a balance
|
|
460 between two conflicting pulls when dealing with a
|
|
461 file that has lots of stuff sitting outside of a top-
|
|
462 level form:
|
|
463
|
|
464 a) If you move past the start of defun, you will
|
|
465 have to recompute defun, which in this case
|
|
466 means that start of defun goes all the way back
|
|
467 to the beginning of the file; so you want
|
|
468 to set start of defun a ways back from the
|
|
469 current point.
|
|
470 b) If you move a line backwards but within start of
|
|
471 defun, you have to move back to start of defun;
|
|
472 so you don't want start of defun too far from
|
|
473 the current point.
|
|
474 */
|
|
475 if (target - context_cache.start_point > 6000)
|
|
476 context_cache.start_point = pt;
|
|
477 context_cache.end_point = pt;
|
|
478 bol_context_cache = context_cache;
|
|
479 }
|
|
480 }
|
|
481
|
460
|
482 UPDATE_SYNTAX_CACHE_FORWARD (pt);
|
428
|
483 prev_c = c;
|
460
|
484 prev_syncode = syncode;
|
428
|
485 c = BUF_FETCH_CHAR (buf, pt);
|
460
|
486 syncode = SYNTAX_CODE_FROM_CACHE (mirrortab, c);
|
428
|
487
|
|
488 if (prev_c == '\n')
|
|
489 bol_context_cache = context_cache;
|
|
490
|
|
491 if (context_cache.backslash_p)
|
|
492 {
|
|
493 context_cache.backslash_p = 0;
|
|
494 continue;
|
|
495 }
|
|
496
|
460
|
497 switch (SYNTAX_FROM_CACHE (mirrortab, c))
|
428
|
498 {
|
|
499 case Sescape:
|
|
500 context_cache.backslash_p = 1;
|
|
501 break;
|
|
502
|
|
503 case Sopen:
|
|
504 if (context_cache.context == context_none)
|
|
505 context_cache.depth++;
|
|
506 break;
|
|
507
|
|
508 case Sclose:
|
|
509 if (context_cache.context == context_none)
|
|
510 context_cache.depth--;
|
|
511 break;
|
|
512
|
|
513 case Scomment:
|
|
514 if (context_cache.context == context_none)
|
|
515 {
|
|
516 context_cache.context = context_comment;
|
|
517 context_cache.ccontext = ccontext_none;
|
460
|
518 context_cache.style = SINGLE_SYNTAX_STYLE (syncode);
|
428
|
519 if (context_cache.style == comment_style_none) abort ();
|
|
520 }
|
|
521 break;
|
|
522
|
|
523 case Sendcomment:
|
460
|
524 if (context_cache.style != SINGLE_SYNTAX_STYLE (syncode))
|
428
|
525 ;
|
|
526 else if (context_cache.context == context_comment)
|
|
527 {
|
|
528 context_cache.context = context_none;
|
|
529 context_cache.style = comment_style_none;
|
|
530 }
|
|
531 else if (context_cache.context == context_block_comment &&
|
|
532 (context_cache.ccontext == ccontext_start2 ||
|
|
533 context_cache.ccontext == ccontext_end1))
|
|
534 {
|
|
535 context_cache.context = context_none;
|
|
536 context_cache.ccontext = ccontext_none;
|
|
537 context_cache.style = comment_style_none;
|
|
538 }
|
|
539 break;
|
|
540
|
|
541 case Sstring:
|
|
542 {
|
|
543 if (context_cache.context == context_string &&
|
|
544 context_cache.scontext == c)
|
|
545 {
|
|
546 context_cache.context = context_none;
|
|
547 context_cache.scontext = '\000';
|
|
548 }
|
|
549 else if (context_cache.context == context_none)
|
|
550 {
|
460
|
551 Lisp_Object stringtermobj =
|
|
552 syntax_match (syntax_cache.current_syntax_table, c);
|
428
|
553 Emchar stringterm;
|
|
554
|
|
555 if (CHARP (stringtermobj))
|
|
556 stringterm = XCHAR (stringtermobj);
|
|
557 else
|
|
558 stringterm = c;
|
|
559 context_cache.context = context_string;
|
|
560 context_cache.scontext = stringterm;
|
|
561 context_cache.ccontext = ccontext_none;
|
|
562 }
|
|
563 break;
|
|
564 }
|
460
|
565
|
|
566 case Scomment_fence:
|
|
567 {
|
|
568 if (context_cache.context == context_generic_comment)
|
|
569 {
|
|
570 context_cache.context = context_none;
|
|
571 }
|
|
572 else if (context_cache.context == context_none)
|
|
573 {
|
|
574 context_cache.context = context_generic_comment;
|
|
575 context_cache.ccontext = ccontext_none;
|
|
576 }
|
|
577 break;
|
|
578 }
|
|
579
|
|
580 case Sstring_fence:
|
|
581 {
|
|
582 if (context_cache.context == context_generic_string)
|
|
583 {
|
|
584 context_cache.context = context_none;
|
|
585 }
|
|
586 else if (context_cache.context == context_none)
|
|
587 {
|
|
588 context_cache.context = context_generic_string;
|
|
589 context_cache.ccontext = ccontext_none;
|
|
590 }
|
|
591 break;
|
|
592 }
|
|
593
|
428
|
594 default:
|
|
595 ;
|
|
596 }
|
|
597
|
|
598 /* That takes care of the characters with manifest syntax.
|
|
599 Now we've got to hack multi-char sequences that start
|
|
600 and end block comments.
|
|
601 */
|
460
|
602 if ((SYNTAX_CODE_COMMENT_BITS (syncode) &
|
428
|
603 SYNTAX_SECOND_CHAR_START) &&
|
|
604 context_cache.context == context_none &&
|
|
605 context_cache.ccontext == ccontext_start1 &&
|
460
|
606 SYNTAX_CODES_START_P (prev_syncode, syncode) /* the two chars match */
|
428
|
607 )
|
|
608 {
|
|
609 context_cache.ccontext = ccontext_start2;
|
460
|
610 context_cache.style = SYNTAX_START_STYLE (prev_syncode, syncode);
|
428
|
611 if (context_cache.style == comment_style_none) abort ();
|
|
612 }
|
460
|
613 else if ((SYNTAX_CODE_COMMENT_BITS (syncode) &
|
428
|
614 SYNTAX_FIRST_CHAR_START) &&
|
|
615 context_cache.context == context_none &&
|
|
616 (context_cache.ccontext == ccontext_none ||
|
|
617 context_cache.ccontext == ccontext_start1))
|
|
618 {
|
|
619 context_cache.ccontext = ccontext_start1;
|
|
620 context_cache.style = comment_style_none; /* should be this already*/
|
|
621 }
|
460
|
622 else if ((SYNTAX_CODE_COMMENT_BITS (syncode) &
|
428
|
623 SYNTAX_SECOND_CHAR_END) &&
|
647
|
624 context_cache.context ==
|
|
625 (enum syntactic_context) context_block_comment &&
|
|
626 context_cache.ccontext ==
|
|
627 (enum block_comment_context) ccontext_end1 &&
|
460
|
628 SYNTAX_CODES_END_P (prev_syncode, syncode) &&
|
428
|
629 /* the two chars match */
|
|
630 context_cache.style ==
|
460
|
631 SYNTAX_END_STYLE (prev_syncode, syncode)
|
428
|
632 )
|
|
633 {
|
|
634 context_cache.context = context_none;
|
|
635 context_cache.ccontext = ccontext_none;
|
|
636 context_cache.style = comment_style_none;
|
|
637 }
|
460
|
638 else if ((SYNTAX_CODE_COMMENT_BITS (syncode) &
|
428
|
639 SYNTAX_FIRST_CHAR_END) &&
|
|
640 context_cache.context == context_block_comment &&
|
460
|
641 context_cache.style == SINGLE_SYNTAX_STYLE (syncode) &&
|
428
|
642 (context_cache.ccontext == ccontext_start2 ||
|
|
643 context_cache.ccontext == ccontext_end1))
|
460
|
644 /* #### is it right to check for end1 here??
|
|
645 yes, because this might be a repetition of the first char
|
|
646 of a comment-end sequence. ie, '/xxx foo xxx/' or
|
|
647 '/xxx foo x/', where 'x' = '*' -- mct */
|
428
|
648 {
|
|
649 if (context_cache.style == comment_style_none) abort ();
|
|
650 context_cache.ccontext = ccontext_end1;
|
|
651 }
|
|
652
|
|
653 else if (context_cache.ccontext == ccontext_start1)
|
|
654 {
|
|
655 if (context_cache.context != context_none) abort ();
|
|
656 context_cache.ccontext = ccontext_none;
|
|
657 }
|
|
658 else if (context_cache.ccontext == ccontext_end1)
|
|
659 {
|
|
660 if (context_cache.context != context_block_comment) abort ();
|
|
661 context_cache.context = context_none;
|
|
662 context_cache.ccontext = ccontext_start2;
|
|
663 }
|
|
664
|
|
665 if (context_cache.ccontext == ccontext_start2 &&
|
|
666 context_cache.context == context_none)
|
|
667 {
|
|
668 context_cache.context = context_block_comment;
|
|
669 if (context_cache.style == comment_style_none) abort ();
|
|
670 }
|
|
671 else if (context_cache.ccontext == ccontext_none &&
|
|
672 context_cache.context == context_block_comment)
|
|
673 {
|
|
674 context_cache.context = context_none;
|
|
675 }
|
|
676 }
|
|
677
|
|
678 context_cache.needs_its_head_reexamined = 0;
|
|
679 }
|
|
680
|
|
681 static Lisp_Object
|
|
682 context_to_symbol (enum syntactic_context context)
|
|
683 {
|
|
684 switch (context)
|
|
685 {
|
460
|
686 case context_none: return Qnil;
|
|
687 case context_string: return Qstring;
|
|
688 case context_comment: return Qcomment;
|
|
689 case context_block_comment: return Qblock_comment;
|
|
690 case context_generic_comment: return Qblock_comment;
|
|
691 case context_generic_string: return Qstring;
|
428
|
692 default: abort (); return Qnil; /* suppress compiler warning */
|
|
693 }
|
|
694 }
|
|
695
|
|
696 DEFUN ("buffer-syntactic-context", Fbuffer_syntactic_context, 0, 1, 0, /*
|
|
697 Return the syntactic context of BUFFER at point.
|
|
698 If BUFFER is nil or omitted, the current buffer is assumed.
|
|
699 The returned value is one of the following symbols:
|
|
700
|
|
701 nil ; meaning no special interpretation
|
|
702 string ; meaning point is within a string
|
|
703 comment ; meaning point is within a line comment
|
|
704 block-comment ; meaning point is within a block comment
|
|
705
|
|
706 See also the function `buffer-syntactic-context-depth', which returns
|
|
707 the current nesting-depth within all parenthesis-syntax delimiters
|
|
708 and the function `syntactically-sectionize', which will map a function
|
|
709 over each syntactic context in a region.
|
|
710
|
|
711 WARNING: this may alter match-data.
|
|
712 */
|
|
713 (buffer))
|
|
714 {
|
|
715 /* This function can GC */
|
|
716 struct buffer *buf = decode_buffer (buffer, 0);
|
|
717 find_context (buf, BUF_PT (buf));
|
|
718 return context_to_symbol (context_cache.context);
|
|
719 }
|
|
720
|
|
721 DEFUN ("buffer-syntactic-context-depth", Fbuffer_syntactic_context_depth,
|
|
722 0, 1, 0, /*
|
|
723 Return the depth within all parenthesis-syntax delimiters at point.
|
|
724 If BUFFER is nil or omitted, the current buffer is assumed.
|
|
725 WARNING: this may alter match-data.
|
|
726 */
|
|
727 (buffer))
|
|
728 {
|
|
729 /* This function can GC */
|
|
730 struct buffer *buf = decode_buffer (buffer, 0);
|
|
731 find_context (buf, BUF_PT (buf));
|
|
732 return make_int (context_cache.depth);
|
|
733 }
|
|
734
|
|
735
|
|
736 DEFUN ("syntactically-sectionize", Fsyntactically_sectionize, 3, 4, 0, /*
|
|
737 Call FUNCTION for each contiguous syntactic context in the region.
|
|
738 Call the given function with four arguments: the start and end of the
|
|
739 region, a symbol representing the syntactic context, and the current
|
|
740 depth (as returned by the functions `buffer-syntactic-context' and
|
|
741 `buffer-syntactic-context-depth'). When this function is called, the
|
|
742 current buffer will be set to BUFFER.
|
|
743
|
|
744 WARNING: this may alter match-data.
|
|
745 */
|
|
746 (function, start, end, buffer))
|
|
747 {
|
|
748 /* This function can GC */
|
665
|
749 Charbpos s, pt, e;
|
428
|
750 int edepth;
|
|
751 enum syntactic_context this_context;
|
|
752 Lisp_Object extent = Qnil;
|
|
753 struct gcpro gcpro1;
|
|
754 struct buffer *buf = decode_buffer (buffer, 0);
|
|
755
|
|
756 get_buffer_range_char (buf, start, end, &s, &e, 0);
|
|
757
|
|
758 pt = s;
|
|
759 find_context (buf, pt);
|
|
760
|
|
761 GCPRO1 (extent);
|
|
762 while (pt < e)
|
|
763 {
|
665
|
764 Charbpos estart, eend;
|
428
|
765 /* skip over "blank" areas, and bug out at end-of-buffer. */
|
|
766 while (context_cache.context == context_none)
|
|
767 {
|
|
768 pt++;
|
|
769 if (pt >= e) goto DONE_LABEL;
|
|
770 find_context (buf, pt);
|
|
771 }
|
|
772 /* We've found a non-blank area; keep going until we reach its end */
|
|
773 this_context = context_cache.context;
|
|
774 estart = pt;
|
|
775
|
|
776 /* Minor kludge: consider the comment-start character(s) a part of
|
|
777 the comment.
|
|
778 */
|
|
779 if (this_context == context_block_comment &&
|
|
780 context_cache.ccontext == ccontext_start2)
|
|
781 estart -= 2;
|
460
|
782 else if (this_context == context_comment
|
|
783 || this_context == context_generic_comment
|
|
784 )
|
428
|
785 estart -= 1;
|
|
786
|
|
787 edepth = context_cache.depth;
|
|
788 while (context_cache.context == this_context && pt < e)
|
|
789 {
|
|
790 pt++;
|
|
791 find_context (buf, pt);
|
|
792 }
|
|
793
|
|
794 eend = pt;
|
|
795
|
|
796 /* Minor kludge: consider the character which terminated the comment
|
|
797 a part of the comment.
|
|
798 */
|
|
799 if ((this_context == context_block_comment ||
|
460
|
800 this_context == context_comment
|
|
801 || this_context == context_generic_comment
|
|
802 )
|
428
|
803 && pt < e)
|
|
804 eend++;
|
|
805
|
|
806 if (estart == eend)
|
|
807 continue;
|
|
808 /* Make sure not to pass in values that are outside the
|
|
809 actual bounds of this function. */
|
|
810 call4_in_buffer (buf, function, make_int (max (s, estart)),
|
|
811 make_int (eend == e ? e : eend - 1),
|
|
812 context_to_symbol (this_context),
|
|
813 make_int (edepth));
|
|
814 }
|
|
815 DONE_LABEL:
|
|
816 UNGCPRO;
|
|
817 return Qnil;
|
|
818 }
|
|
819
|
|
820 void
|
|
821 syms_of_font_lock (void)
|
|
822 {
|
563
|
823 DEFSYMBOL (Qcomment);
|
|
824 DEFSYMBOL (Qblock_comment);
|
|
825 DEFSYMBOL (Qbeginning_of_defun);
|
428
|
826
|
|
827 DEFSUBR (Fbuffer_syntactic_context);
|
|
828 DEFSUBR (Fbuffer_syntactic_context_depth);
|
|
829 DEFSUBR (Fsyntactically_sectionize);
|
|
830 }
|
|
831
|
|
832 void
|
|
833 reinit_vars_of_font_lock (void)
|
|
834 {
|
|
835 xzero (context_cache);
|
|
836 xzero (bol_context_cache);
|
|
837 }
|
|
838
|
|
839 void
|
|
840 vars_of_font_lock (void)
|
|
841 {
|
|
842 reinit_vars_of_font_lock ();
|
|
843 }
|