]> code.delx.au - gnu-emacs/blob - lisp/emacs-lisp/syntax.el
5101925494354d5c52d53ab9a4aeb3833e514c3e
[gnu-emacs] / lisp / emacs-lisp / syntax.el
1 ;;; syntax.el --- helper functions to find syntactic context -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 2000-2015 Free Software Foundation, Inc.
4
5 ;; Maintainer: emacs-devel@gnu.org
6 ;; Keywords: internal
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; The main exported function is `syntax-ppss'. You might also need
26 ;; to call `syntax-ppss-flush-cache' or to add it to
27 ;; before-change-functions'(although this is automatically done by
28 ;; syntax-ppss when needed, but that might fail if syntax-ppss is
29 ;; called in a context where before-change-functions is temporarily
30 ;; let-bound to nil).
31
32 ;;; Todo:
33
34 ;; - do something about the case where the syntax-table is changed.
35 ;; This typically happens with tex-mode and its `$' operator.
36 ;; - new functions `syntax-state', ... to replace uses of parse-partial-state
37 ;; with something higher-level (similar to syntax-ppss-context).
38 ;; - interaction with mmm-mode.
39
40 ;;; Code:
41
42 ;; Note: PPSS stands for `parse-partial-sexp state'
43
44 (eval-when-compile (require 'cl-lib))
45
46 (defvar font-lock-beginning-of-syntax-function)
47
48 ;;; Applying syntax-table properties where needed.
49
50 (defvar syntax-propertize-function nil
51 ;; Rather than a -functions hook, this is a -function because it's easier
52 ;; to do a single scan than several scans: with multiple scans, one cannot
53 ;; assume that the text before point has been propertized, so syntax-ppss
54 ;; gives unreliable results (and stores them in its cache to boot, so we'd
55 ;; have to flush that cache between each function, and we couldn't use
56 ;; syntax-ppss-flush-cache since that would not only flush the cache but also
57 ;; reset syntax-propertize--done which should not be done in this case).
58 "Mode-specific function to apply `syntax-table' text properties.
59 It is the work horse of `syntax-propertize', which is called by things like
60 Font-Lock and indentation.
61
62 It is given two arguments, START and END: the start and end of the text to
63 which `syntax-table' might need to be applied. Major modes can use this to
64 override the buffer's syntax table for special syntactic constructs that
65 cannot be handled just by the buffer's syntax-table.
66
67 The specified function may call `syntax-ppss' on any position
68 before END, but it should not call `syntax-ppss-flush-cache',
69 which means that it should not call `syntax-ppss' on some
70 position and later modify the buffer on some earlier position.")
71
72 (defvar syntax-propertize-chunk-size 500)
73
74 (defvar syntax-propertize-extend-region-functions
75 '(syntax-propertize-wholelines)
76 "Special hook run just before proceeding to propertize a region.
77 This is used to allow major modes to help `syntax-propertize' find safe buffer
78 positions as beginning and end of the propertized region. Its most common use
79 is to solve the problem of /identification/ of multiline elements by providing
80 a function that tries to find such elements and move the boundaries such that
81 they do not fall in the middle of one.
82 Each function is called with two arguments (START and END) and it should return
83 either a cons (NEW-START . NEW-END) or nil if no adjustment should be made.
84 These functions are run in turn repeatedly until they all return nil.
85 Put first the functions more likely to cause a change and cheaper to compute.")
86 ;; Mark it as a special hook which doesn't use any global setting
87 ;; (i.e. doesn't obey the element t in the buffer-local value).
88 (make-variable-buffer-local 'syntax-propertize-extend-region-functions)
89
90 (defun syntax-propertize-wholelines (start end)
91 (goto-char start)
92 (cons (line-beginning-position)
93 (progn (goto-char end)
94 (if (bolp) (point) (line-beginning-position 2)))))
95
96 (defun syntax-propertize-multiline (beg end)
97 "Let `syntax-propertize' pay attention to the syntax-multiline property."
98 (when (and (> beg (point-min))
99 (get-text-property (1- beg) 'syntax-multiline))
100 (setq beg (or (previous-single-property-change beg 'syntax-multiline)
101 (point-min))))
102 ;;
103 (when (get-text-property end 'syntax-multiline)
104 (setq end (or (text-property-any end (point-max)
105 'syntax-multiline nil)
106 (point-max))))
107 (cons beg end))
108
109 (defun syntax-propertize--shift-groups (re n)
110 (replace-regexp-in-string
111 "\\\\(\\?\\([0-9]+\\):"
112 (lambda (s)
113 (replace-match
114 (number-to-string (+ n (string-to-number (match-string 1 s))))
115 t t s 1))
116 re t t))
117
118 (defmacro syntax-propertize-precompile-rules (&rest rules)
119 "Return a precompiled form of RULES to pass to `syntax-propertize-rules'.
120 The arg RULES can be of the same form as in `syntax-propertize-rules'.
121 The return value is an object that can be passed as a rule to
122 `syntax-propertize-rules'.
123 I.e. this is useful only when you want to share rules among several
124 `syntax-propertize-function's."
125 (declare (debug syntax-propertize-rules))
126 ;; Precompile? Yeah, right!
127 ;; Seriously, tho, this is a macro for 2 reasons:
128 ;; - we could indeed do some pre-compilation at some point in the future,
129 ;; e.g. fi/when we switch to a DFA-based implementation of
130 ;; syntax-propertize-rules.
131 ;; - this lets Edebug properly annotate the expressions inside RULES.
132 `',rules)
133
134 (defmacro syntax-propertize-rules (&rest rules)
135 "Make a function that applies RULES for use in `syntax-propertize-function'.
136 The function will scan the buffer, applying the rules where they match.
137 The buffer is scanned a single time, like \"lex\" would, rather than once
138 per rule.
139
140 Each RULE can be a symbol, in which case that symbol's value should be,
141 at macro-expansion time, a precompiled set of rules, as returned
142 by `syntax-propertize-precompile-rules'.
143
144 Otherwise, RULE should have the form (REGEXP HIGHLIGHT1 ... HIGHLIGHTn), where
145 REGEXP is an expression (evaluated at time of macro-expansion) that returns
146 a regexp, and where HIGHLIGHTs have the form (NUMBER SYNTAX) which means to
147 apply the property SYNTAX to the chars matched by the subgroup NUMBER
148 of the regular expression, if NUMBER did match.
149 SYNTAX is an expression that returns a value to apply as `syntax-table'
150 property. Some expressions are handled specially:
151 - if SYNTAX is a string, then it is converted with `string-to-syntax';
152 - if SYNTAX has the form (prog1 EXP . EXPS) then the value returned by EXP
153 will be applied to the buffer before running EXPS and if EXP is a string it
154 is also converted with `string-to-syntax'.
155 The SYNTAX expression is responsible to save the `match-data' if needed
156 for subsequent HIGHLIGHTs.
157 Also SYNTAX is free to move point, in which case RULES may not be applied to
158 some parts of the text or may be applied several times to other parts.
159
160 Note: back-references in REGEXPs do not work."
161 (declare (debug (&rest &or symbolp ;FIXME: edebug this eval step.
162 (form &rest
163 (numberp
164 [&or stringp ;FIXME: Use &wrap
165 ("prog1" [&or stringp def-form] def-body)
166 def-form])))))
167 (let ((newrules nil))
168 (while rules
169 (if (symbolp (car rules))
170 (setq rules (append (symbol-value (pop rules)) rules))
171 (push (pop rules) newrules)))
172 (setq rules (nreverse newrules)))
173 (let* ((offset 0)
174 (branches '())
175 ;; We'd like to use a real DFA-based lexer, usually, but since Emacs
176 ;; doesn't have one yet, we fallback on building one large regexp
177 ;; and use groups to determine which branch of the regexp matched.
178 (re
179 (mapconcat
180 (lambda (rule)
181 (let* ((orig-re (eval (car rule)))
182 (re orig-re))
183 (when (and (assq 0 rule) (cdr rules))
184 ;; If there's more than 1 rule, and the rule want to apply
185 ;; highlight to match 0, create an extra group to be able to
186 ;; tell when *this* match 0 has succeeded.
187 (cl-incf offset)
188 (setq re (concat "\\(" re "\\)")))
189 (setq re (syntax-propertize--shift-groups re offset))
190 (let ((code '())
191 (condition
192 (cond
193 ((assq 0 rule) (if (zerop offset) t
194 `(match-beginning ,offset)))
195 ((null (cddr rule))
196 `(match-beginning ,(+ offset (car (cadr rule)))))
197 (t
198 `(or ,@(mapcar
199 (lambda (case)
200 `(match-beginning ,(+ offset (car case))))
201 (cdr rule))))))
202 (nocode t)
203 (offset offset))
204 ;; If some of the subgroup rules include Elisp code, then we
205 ;; need to set the match-data so it's consistent with what the
206 ;; code expects. If not, then we can simply use shifted
207 ;; offset in our own code.
208 (unless (zerop offset)
209 (dolist (case (cdr rule))
210 (unless (stringp (cadr case))
211 (setq nocode nil)))
212 (unless nocode
213 (push `(let ((md (match-data 'ints)))
214 ;; Keep match 0 as is, but shift everything else.
215 (setcdr (cdr md) (nthcdr ,(* (1+ offset) 2) md))
216 (set-match-data md))
217 code)
218 (setq offset 0)))
219 ;; Now construct the code for each subgroup rules.
220 (dolist (case (cdr rule))
221 (cl-assert (null (cddr case)))
222 (let* ((gn (+ offset (car case)))
223 (action (nth 1 case))
224 (thiscode
225 (cond
226 ((stringp action)
227 `((put-text-property
228 (match-beginning ,gn) (match-end ,gn)
229 'syntax-table
230 ',(string-to-syntax action))))
231 ((eq (car-safe action) 'ignore)
232 (cdr action))
233 ((eq (car-safe action) 'prog1)
234 (if (stringp (nth 1 action))
235 `((put-text-property
236 (match-beginning ,gn) (match-end ,gn)
237 'syntax-table
238 ',(string-to-syntax (nth 1 action)))
239 ,@(nthcdr 2 action))
240 `((let ((mb (match-beginning ,gn))
241 (me (match-end ,gn))
242 (syntax ,(nth 1 action)))
243 (if syntax
244 (put-text-property
245 mb me 'syntax-table syntax))
246 ,@(nthcdr 2 action)))))
247 (t
248 `((let ((mb (match-beginning ,gn))
249 (me (match-end ,gn))
250 (syntax ,action))
251 (if syntax
252 (put-text-property
253 mb me 'syntax-table syntax))))))))
254
255 (if (or (not (cddr rule)) (zerop gn))
256 (setq code (nconc (nreverse thiscode) code))
257 (push `(if (match-beginning ,gn)
258 ;; Try and generate clean code with no
259 ;; extraneous progn.
260 ,(if (null (cdr thiscode))
261 (car thiscode)
262 `(progn ,@thiscode)))
263 code))))
264 (push (cons condition (nreverse code))
265 branches))
266 (cl-incf offset (regexp-opt-depth orig-re))
267 re))
268 rules
269 "\\|")))
270 `(lambda (start end)
271 (goto-char start)
272 (while (and (< (point) end)
273 (re-search-forward ,re end t))
274 (cond ,@(nreverse branches))))))
275
276 (defun syntax-propertize-via-font-lock (keywords)
277 "Propertize for syntax in START..END using font-lock syntax.
278 KEYWORDS obeys the format used in `font-lock-syntactic-keywords'.
279 The return value is a function suitable for `syntax-propertize-function'."
280 (lambda (start end)
281 (with-no-warnings
282 (let ((font-lock-syntactic-keywords keywords))
283 (font-lock-fontify-syntactic-keywords-region start end)
284 ;; In case it was eval'd/compiled.
285 (setq keywords font-lock-syntactic-keywords)))))
286
287 (defun syntax-propertize (pos)
288 "Ensure that syntax-table properties are set until POS."
289 (when (< syntax-propertize--done pos)
290 (if (null syntax-propertize-function)
291 (setq syntax-propertize--done (max (point-max) pos))
292 ;; (message "Needs to syntax-propertize from %s to %s"
293 ;; syntax-propertize--done pos)
294 (set (make-local-variable 'parse-sexp-lookup-properties) t)
295 (save-excursion
296 (with-silent-modifications
297 (make-local-variable 'syntax-propertize--done) ;Just in case!
298 (let* ((start (max (min syntax-propertize--done (point-max))
299 (point-min)))
300 (end (max pos
301 (min (point-max)
302 (+ start syntax-propertize-chunk-size))))
303 (funs syntax-propertize-extend-region-functions))
304 (while funs
305 (let ((new (funcall (pop funs) start end))
306 ;; Avoid recursion!
307 (syntax-propertize--done most-positive-fixnum))
308 (if (or (null new)
309 (and (>= (car new) start) (<= (cdr new) end)))
310 nil
311 (setq start (car new))
312 (setq end (cdr new))
313 ;; If there's been a change, we should go through the
314 ;; list again since this new position may
315 ;; warrant a different answer from one of the funs we've
316 ;; already seen.
317 (unless (eq funs
318 (cdr syntax-propertize-extend-region-functions))
319 (setq funs syntax-propertize-extend-region-functions)))))
320 ;; Move the limit before calling the function, so the function
321 ;; can use syntax-ppss.
322 (setq syntax-propertize--done end)
323 ;; (message "syntax-propertizing from %s to %s" start end)
324 (remove-text-properties start end
325 '(syntax-table nil syntax-multiline nil))
326 ;; Avoid recursion!
327 (let ((syntax-propertize--done most-positive-fixnum))
328 (funcall syntax-propertize-function start end))))))))
329
330 ;;; Link syntax-propertize with syntax.c.
331
332 (defvar syntax-propertize-chunks
333 ;; We're not sure how far we'll go. In my tests, using chunks of 20000
334 ;; brings to overhead to something negligible. Passing ‘charpos’ directly
335 ;; also works (basically works line-by-line) but results in an overhead which
336 ;; I thought was a bit too high (like around 50%).
337 2000)
338
339 (defun internal--syntax-propertize (charpos)
340 ;; FIXME: Called directly from C.
341 (syntax-propertize (min (+ syntax-propertize-chunks charpos) (point-max))))
342
343 ;;; Incrementally compute and memoize parser state.
344
345 (defsubst syntax-ppss-depth (ppss)
346 (nth 0 ppss))
347
348 (defun syntax-ppss-toplevel-pos (ppss)
349 "Get the latest syntactically outermost position found in a syntactic scan.
350 PPSS is a scan state, as returned by `parse-partial-sexp' or `syntax-ppss'.
351 An \"outermost position\" means one that it is outside of any syntactic entity:
352 outside of any parentheses, comments, or strings encountered in the scan.
353 If no such position is recorded in PPSS (because the end of the scan was
354 itself at the outermost level), return nil."
355 ;; BEWARE! We rely on the undocumented 9th field. The 9th field currently
356 ;; contains the list of positions of the enclosing open-parens.
357 ;; I.e. those positions are outside of any string/comment and the first of
358 ;; those is outside of any paren (i.e. corresponds to a nil ppss).
359 ;; If this list is empty but we are in a string or comment, then the 8th
360 ;; field contains a similar "toplevel" position.
361 (or (car (nth 9 ppss))
362 (nth 8 ppss)))
363
364 (defsubst syntax-ppss-context (ppss)
365 (cond
366 ((nth 3 ppss) 'string)
367 ((nth 4 ppss) 'comment)
368 (t nil)))
369
370 (defvar syntax-ppss-max-span 20000
371 "Threshold below which cache info is deemed unnecessary.
372 We try to make sure that cache entries are at least this far apart
373 from each other, to avoid keeping too much useless info.")
374
375 (defvar syntax-begin-function nil
376 "Function to move back outside of any comment/string/paren.
377 This function should move the cursor back to some syntactically safe
378 point (where the PPSS is equivalent to nil).")
379
380 (defvar syntax-ppss-cache nil
381 "List of (POS . PPSS) pairs, in decreasing POS order.")
382 (make-variable-buffer-local 'syntax-ppss-cache)
383 (defvar syntax-ppss-last nil
384 "Cache of (LAST-POS . LAST-PPSS).")
385 (make-variable-buffer-local 'syntax-ppss-last)
386
387 (defalias 'syntax-ppss-after-change-function 'syntax-ppss-flush-cache)
388 (defun syntax-ppss-flush-cache (beg &rest ignored)
389 "Flush the cache of `syntax-ppss' starting at position BEG."
390 ;; Set syntax-propertize to refontify anything past beg.
391 (setq syntax-propertize--done (min beg syntax-propertize--done))
392 ;; Flush invalid cache entries.
393 (while (and syntax-ppss-cache (> (caar syntax-ppss-cache) beg))
394 (setq syntax-ppss-cache (cdr syntax-ppss-cache)))
395 ;; Throw away `last' value if made invalid.
396 (when (< beg (or (car syntax-ppss-last) 0))
397 ;; If syntax-begin-function jumped to BEG, then the old state at BEG can
398 ;; depend on the text after BEG (which is presumably changed). So if
399 ;; BEG=(car (nth 10 syntax-ppss-last)) don't reuse that data because the
400 ;; assumed nil state at BEG may not be valid any more.
401 (if (<= beg (or (syntax-ppss-toplevel-pos (cdr syntax-ppss-last))
402 (nth 3 syntax-ppss-last)
403 0))
404 (setq syntax-ppss-last nil)
405 (setcar syntax-ppss-last nil)))
406 ;; Unregister if there's no cache left. Sadly this doesn't work
407 ;; because `before-change-functions' is temporarily bound to nil here.
408 ;; (unless syntax-ppss-cache
409 ;; (remove-hook 'before-change-functions 'syntax-ppss-flush-cache t))
410 )
411
412 (defvar syntax-ppss-stats
413 [(0 . 0.0) (0 . 0.0) (0 . 0.0) (0 . 0.0) (0 . 0.0) (1 . 2500.0)])
414 (defun syntax-ppss-stats ()
415 (mapcar (lambda (x)
416 (condition-case nil
417 (cons (car x) (truncate (/ (cdr x) (car x))))
418 (error nil)))
419 syntax-ppss-stats))
420
421 (defun syntax-ppss (&optional pos)
422 "Parse-Partial-Sexp State at POS, defaulting to point.
423 The returned value is the same as that of `parse-partial-sexp'
424 run from `point-min' to POS except that values at positions 2 and 6
425 in the returned list (counting from 0) cannot be relied upon.
426 Point is at POS when this function returns.
427
428 It is necessary to call `syntax-ppss-flush-cache' explicitly if
429 this function is called while `before-change-functions' is
430 temporarily let-bound, or if the buffer is modified without
431 running the hook."
432 ;; Default values.
433 (unless pos (setq pos (point)))
434 (syntax-propertize pos)
435 ;;
436 (let ((old-ppss (cdr syntax-ppss-last))
437 (old-pos (car syntax-ppss-last))
438 (ppss nil)
439 (pt-min (point-min)))
440 (if (and old-pos (> old-pos pos)) (setq old-pos nil))
441 ;; Use the OLD-POS if usable and close. Don't update the `last' cache.
442 (condition-case nil
443 (if (and old-pos (< (- pos old-pos)
444 ;; The time to use syntax-begin-function and
445 ;; find PPSS is assumed to be about 2 * distance.
446 (* 2 (/ (cdr (aref syntax-ppss-stats 5))
447 (1+ (car (aref syntax-ppss-stats 5)))))))
448 (progn
449 (cl-incf (car (aref syntax-ppss-stats 0)))
450 (cl-incf (cdr (aref syntax-ppss-stats 0)) (- pos old-pos))
451 (parse-partial-sexp old-pos pos nil nil old-ppss))
452
453 (cond
454 ;; Use OLD-PPSS if possible and close enough.
455 ((and (not old-pos) old-ppss
456 ;; If `pt-min' is too far from `pos', we could try to use
457 ;; other positions in (nth 9 old-ppss), but that doesn't
458 ;; seem to happen in practice and it would complicate this
459 ;; code (and the before-change-function code even more).
460 ;; But maybe it would be useful in "degenerate" cases such
461 ;; as when the whole file is wrapped in a set
462 ;; of parentheses.
463 (setq pt-min (or (syntax-ppss-toplevel-pos old-ppss)
464 (nth 2 old-ppss)))
465 (<= pt-min pos) (< (- pos pt-min) syntax-ppss-max-span))
466 (cl-incf (car (aref syntax-ppss-stats 1)))
467 (cl-incf (cdr (aref syntax-ppss-stats 1)) (- pos pt-min))
468 (setq ppss (parse-partial-sexp pt-min pos)))
469 ;; The OLD-* data can't be used. Consult the cache.
470 (t
471 (let ((cache-pred nil)
472 (cache syntax-ppss-cache)
473 (pt-min (point-min))
474 ;; I differentiate between PT-MIN and PT-BEST because
475 ;; I feel like it might be important to ensure that the
476 ;; cache is only filled with 100% sure data (whereas
477 ;; syntax-begin-function might return incorrect data).
478 ;; Maybe that's just stupid.
479 (pt-best (point-min))
480 (ppss-best nil))
481 ;; look for a usable cache entry.
482 (while (and cache (< pos (caar cache)))
483 (setq cache-pred cache)
484 (setq cache (cdr cache)))
485 (if cache (setq pt-min (caar cache) ppss (cdar cache)))
486
487 ;; Setup the before-change function if necessary.
488 (unless (or syntax-ppss-cache syntax-ppss-last)
489 (add-hook 'before-change-functions
490 'syntax-ppss-flush-cache t t))
491
492 ;; Use the best of OLD-POS and CACHE.
493 (if (or (not old-pos) (< old-pos pt-min))
494 (setq pt-best pt-min ppss-best ppss)
495 (cl-incf (car (aref syntax-ppss-stats 4)))
496 (cl-incf (cdr (aref syntax-ppss-stats 4)) (- pos old-pos))
497 (setq pt-best old-pos ppss-best old-ppss))
498
499 ;; Use the `syntax-begin-function' if available.
500 ;; We could try using that function earlier, but:
501 ;; - The result might not be 100% reliable, so it's better to use
502 ;; the cache if available.
503 ;; - The function might be slow.
504 ;; - If this function almost always finds a safe nearby spot,
505 ;; the cache won't be populated, so consulting it is cheap.
506 (when (and (not syntax-begin-function)
507 (boundp 'font-lock-beginning-of-syntax-function)
508 font-lock-beginning-of-syntax-function)
509 (set (make-local-variable 'syntax-begin-function)
510 font-lock-beginning-of-syntax-function))
511 (when (and syntax-begin-function
512 (progn (goto-char pos)
513 (funcall syntax-begin-function)
514 ;; Make sure it's better.
515 (> (point) pt-best))
516 ;; Simple sanity checks.
517 (< (point) pos) ; backward-paragraph can fail here.
518 (not (memq (get-text-property (point) 'face)
519 '(font-lock-string-face font-lock-doc-face
520 font-lock-comment-face))))
521 (cl-incf (car (aref syntax-ppss-stats 5)))
522 (cl-incf (cdr (aref syntax-ppss-stats 5)) (- pos (point)))
523 (setq pt-best (point) ppss-best nil))
524
525 (cond
526 ;; Quick case when we found a nearby pos.
527 ((< (- pos pt-best) syntax-ppss-max-span)
528 (cl-incf (car (aref syntax-ppss-stats 2)))
529 (cl-incf (cdr (aref syntax-ppss-stats 2)) (- pos pt-best))
530 (setq ppss (parse-partial-sexp pt-best pos nil nil ppss-best)))
531 ;; Slow case: compute the state from some known position and
532 ;; populate the cache so we won't need to do it again soon.
533 (t
534 (cl-incf (car (aref syntax-ppss-stats 3)))
535 (cl-incf (cdr (aref syntax-ppss-stats 3)) (- pos pt-min))
536
537 ;; If `pt-min' is too far, add a few intermediate entries.
538 (while (> (- pos pt-min) (* 2 syntax-ppss-max-span))
539 (setq ppss (parse-partial-sexp
540 pt-min (setq pt-min (/ (+ pt-min pos) 2))
541 nil nil ppss))
542 (push (cons pt-min ppss)
543 (if cache-pred (cdr cache-pred) syntax-ppss-cache)))
544
545 ;; Compute the actual return value.
546 (setq ppss (parse-partial-sexp pt-min pos nil nil ppss))
547
548 ;; Debugging check.
549 ;; (let ((real-ppss (parse-partial-sexp (point-min) pos)))
550 ;; (setcar (last ppss 4) 0)
551 ;; (setcar (last real-ppss 4) 0)
552 ;; (setcar (last ppss 8) nil)
553 ;; (setcar (last real-ppss 8) nil)
554 ;; (unless (equal ppss real-ppss)
555 ;; (message "!!Syntax: %s != %s" ppss real-ppss)
556 ;; (setq ppss real-ppss)))
557
558 ;; Store it in the cache.
559 (let ((pair (cons pos ppss)))
560 (if cache-pred
561 (if (> (- (caar cache-pred) pos) syntax-ppss-max-span)
562 (push pair (cdr cache-pred))
563 (setcar cache-pred pair))
564 (if (or (null syntax-ppss-cache)
565 (> (- (caar syntax-ppss-cache) pos)
566 syntax-ppss-max-span))
567 (push pair syntax-ppss-cache)
568 (setcar syntax-ppss-cache pair)))))))))
569
570 (setq syntax-ppss-last (cons pos ppss))
571 ppss)
572 (args-out-of-range
573 ;; If the buffer is more narrowed than when we built the cache,
574 ;; we may end up calling parse-partial-sexp with a position before
575 ;; point-min. In that case, just parse from point-min assuming
576 ;; a nil state.
577 (parse-partial-sexp (point-min) pos)))))
578
579 ;; Debugging functions
580
581 (defun syntax-ppss-debug ()
582 (let ((pt nil)
583 (min-diffs nil))
584 (dolist (x (append syntax-ppss-cache (list (cons (point-min) nil))))
585 (when pt (push (- pt (car x)) min-diffs))
586 (setq pt (car x)))
587 min-diffs))
588
589 ;; XEmacs compatibility functions
590
591 ;; (defun buffer-syntactic-context (&optional buffer)
592 ;; "Syntactic context at point in BUFFER.
593 ;; Either of `string', `comment' or nil.
594 ;; This is an XEmacs compatibility function."
595 ;; (with-current-buffer (or buffer (current-buffer))
596 ;; (syntax-ppss-context (syntax-ppss))))
597
598 ;; (defun buffer-syntactic-context-depth (&optional buffer)
599 ;; "Syntactic parenthesis depth at point in BUFFER.
600 ;; This is an XEmacs compatibility function."
601 ;; (with-current-buffer (or buffer (current-buffer))
602 ;; (syntax-ppss-depth (syntax-ppss))))
603
604 (provide 'syntax)
605
606 ;;; syntax.el ends here