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