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