Merge from emacs--rel--22
[bpt/emacs.git] / lisp / emacs-lisp / syntax.el
1 ;;; syntax.el --- helper functions to find syntactic context
2
3 ;; Copyright (C) 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Maintainer: FSF
7 ;; Keywords: internal
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; The main exported function is `syntax-ppss'. You might also need
27 ;; to call `syntax-ppss-flush-cache' or to add it to
28 ;; before-change-functions'(although this is automatically done by
29 ;; syntax-ppss when needed, but that might fail if syntax-ppss is
30 ;; called in a context where before-change-functions is temporarily
31 ;; let-bound to nil).
32
33 ;;; Todo:
34
35 ;; - do something about the case where the syntax-table is changed.
36 ;; This typically happens with tex-mode and its `$' operator.
37 ;; - move font-lock-syntactic-keywords in here. Then again, maybe not.
38 ;; - new functions `syntax-state', ... to replace uses of parse-partial-state
39 ;; with something higher-level (similar to syntax-ppss-context).
40 ;; - interaction with mmm-mode.
41
42 ;;; Code:
43
44 ;; Note: PPSS stands for `parse-partial-sexp state'
45
46 (eval-when-compile (require 'cl))
47
48 (defvar font-lock-beginning-of-syntax-function)
49
50 (defsubst syntax-ppss-depth (ppss)
51 (nth 0 ppss))
52
53 (defun syntax-ppss-toplevel-pos (ppss)
54 "Get the latest syntactically outermost position found in a syntactic scan.
55 PPSS is a scan state, as returned by `partial-parse-sexp' or `syntax-ppss'.
56 An \"outermost position\" means one that it is outside of any syntactic entity:
57 outside of any parentheses, comments, or strings encountered in the scan.
58 If no such position is recorded in PPSS (because the end of the scan was
59 itself at the outermost level), return nil."
60 ;; BEWARE! We rely on the undocumented 9th field. The 9th field currently
61 ;; contains the list of positions of the enclosing open-parens.
62 ;; I.e. those positions are outside of any string/comment and the first of
63 ;; those is outside of any paren (i.e. corresponds to a nil ppss).
64 ;; If this list is empty but we are in a string or comment, then the 8th
65 ;; field contains a similar "toplevel" position.
66 (or (car (nth 9 ppss))
67 (nth 8 ppss)))
68
69 (defsubst syntax-ppss-context (ppss)
70 (cond
71 ((nth 3 ppss) 'string)
72 ((nth 4 ppss) 'comment)
73 (t nil)))
74
75 (defvar syntax-ppss-max-span 20000
76 "Threshold below which cache info is deemed unnecessary.
77 We try to make sure that cache entries are at least this far apart
78 from each other, to avoid keeping too much useless info.")
79
80 (defvar syntax-begin-function nil
81 "Function to move back outside of any comment/string/paren.
82 This function should move the cursor back to some syntactically safe
83 point (where the PPSS is equivalent to nil).")
84
85 (defvar syntax-ppss-cache nil
86 "List of (POS . PPSS) pairs, in decreasing POS order.")
87 (make-variable-buffer-local 'syntax-ppss-cache)
88 (defvar syntax-ppss-last nil
89 "Cache of (LAST-POS . LAST-PPSS).")
90 (make-variable-buffer-local 'syntax-ppss-last)
91
92 (defalias 'syntax-ppss-after-change-function 'syntax-ppss-flush-cache)
93 (defun syntax-ppss-flush-cache (beg &rest ignored)
94 "Flush the cache of `syntax-ppss' starting at position BEG."
95 ;; Flush invalid cache entries.
96 (while (and syntax-ppss-cache (> (caar syntax-ppss-cache) beg))
97 (setq syntax-ppss-cache (cdr syntax-ppss-cache)))
98 ;; Throw away `last' value if made invalid.
99 (when (< beg (or (car syntax-ppss-last) 0))
100 ;; If syntax-begin-function jumped to BEG, then the old state at BEG can
101 ;; depend on the text after BEG (which is presumably changed). So if
102 ;; BEG=(car (nth 10 syntax-ppss-last)) don't reuse that data because the
103 ;; assumed nil state at BEG may not be valid any more.
104 (if (<= beg (or (syntax-ppss-toplevel-pos (cdr syntax-ppss-last))
105 (nth 3 syntax-ppss-last)
106 0))
107 (setq syntax-ppss-last nil)
108 (setcar syntax-ppss-last nil)))
109 ;; Unregister if there's no cache left. Sadly this doesn't work
110 ;; because `before-change-functions' is temporarily bound to nil here.
111 ;; (unless syntax-ppss-cache
112 ;; (remove-hook 'before-change-functions 'syntax-ppss-flush-cache t))
113 )
114
115 (defvar syntax-ppss-stats
116 [(0 . 0.0) (0 . 0.0) (0 . 0.0) (0 . 0.0) (0 . 0.0) (1 . 2500.0)])
117 (defun syntax-ppss-stats ()
118 (mapcar (lambda (x)
119 (condition-case nil
120 (cons (car x) (truncate (/ (cdr x) (car x))))
121 (error nil)))
122 syntax-ppss-stats))
123
124 (defun syntax-ppss (&optional pos)
125 "Parse-Partial-Sexp State at POS.
126 The returned value is the same as `parse-partial-sexp' except that
127 the 2nd and 6th values of the returned state cannot be relied upon.
128 Point is at POS when this function returns."
129 ;; Default values.
130 (unless pos (setq pos (point)))
131 ;;
132 (let ((old-ppss (cdr syntax-ppss-last))
133 (old-pos (car syntax-ppss-last))
134 (ppss nil)
135 (pt-min (point-min)))
136 (if (and old-pos (> old-pos pos)) (setq old-pos nil))
137 ;; Use the OLD-POS if usable and close. Don't update the `last' cache.
138 (condition-case nil
139 (if (and old-pos (< (- pos old-pos)
140 ;; The time to use syntax-begin-function and
141 ;; find PPSS is assumed to be about 2 * distance.
142 (* 2 (/ (cdr (aref syntax-ppss-stats 5))
143 (1+ (car (aref syntax-ppss-stats 5)))))))
144 (progn
145 (incf (car (aref syntax-ppss-stats 0)))
146 (incf (cdr (aref syntax-ppss-stats 0)) (- pos old-pos))
147 (parse-partial-sexp old-pos pos nil nil old-ppss))
148
149 (cond
150 ;; Use OLD-PPSS if possible and close enough.
151 ((and (not old-pos) old-ppss
152 ;; If `pt-min' is too far from `pos', we could try to use
153 ;; other positions in (nth 9 old-ppss), but that doesn't
154 ;; seem to happen in practice and it would complicate this
155 ;; code (and the before-change-function code even more).
156 ;; But maybe it would be useful in "degenerate" cases such
157 ;; as when the whole file is wrapped in a set
158 ;; of parentheses.
159 (setq pt-min (or (syntax-ppss-toplevel-pos old-ppss)
160 (nth 2 old-ppss)))
161 (<= pt-min pos) (< (- pos pt-min) syntax-ppss-max-span))
162 (incf (car (aref syntax-ppss-stats 1)))
163 (incf (cdr (aref syntax-ppss-stats 1)) (- pos pt-min))
164 (setq ppss (parse-partial-sexp pt-min pos)))
165 ;; The OLD-* data can't be used. Consult the cache.
166 (t
167 (let ((cache-pred nil)
168 (cache syntax-ppss-cache)
169 (pt-min (point-min))
170 ;; I differentiate between PT-MIN and PT-BEST because
171 ;; I feel like it might be important to ensure that the
172 ;; cache is only filled with 100% sure data (whereas
173 ;; syntax-begin-function might return incorrect data).
174 ;; Maybe that's just stupid.
175 (pt-best (point-min))
176 (ppss-best nil))
177 ;; look for a usable cache entry.
178 (while (and cache (< pos (caar cache)))
179 (setq cache-pred cache)
180 (setq cache (cdr cache)))
181 (if cache (setq pt-min (caar cache) ppss (cdar cache)))
182
183 ;; Setup the before-change function if necessary.
184 (unless (or syntax-ppss-cache syntax-ppss-last)
185 (add-hook 'before-change-functions
186 'syntax-ppss-flush-cache t t))
187
188 ;; Use the best of OLD-POS and CACHE.
189 (if (or (not old-pos) (< old-pos pt-min))
190 (setq pt-best pt-min ppss-best ppss)
191 (incf (car (aref syntax-ppss-stats 4)))
192 (incf (cdr (aref syntax-ppss-stats 4)) (- pos old-pos))
193 (setq pt-best old-pos ppss-best old-ppss))
194
195 ;; Use the `syntax-begin-function' if available.
196 ;; We could try using that function earlier, but:
197 ;; - The result might not be 100% reliable, so it's better to use
198 ;; the cache if available.
199 ;; - The function might be slow.
200 ;; - If this function almost always finds a safe nearby spot,
201 ;; the cache won't be populated, so consulting it is cheap.
202 (when (and (not syntax-begin-function)
203 (boundp 'font-lock-beginning-of-syntax-function)
204 font-lock-beginning-of-syntax-function)
205 (set (make-local-variable 'syntax-begin-function)
206 font-lock-beginning-of-syntax-function))
207 (when (and syntax-begin-function
208 (progn (goto-char pos)
209 (funcall syntax-begin-function)
210 ;; Make sure it's better.
211 (> (point) pt-best))
212 ;; Simple sanity check.
213 (not (memq (get-text-property (point) 'face)
214 '(font-lock-string-face font-lock-doc-face
215 font-lock-comment-face))))
216 (incf (car (aref syntax-ppss-stats 5)))
217 (incf (cdr (aref syntax-ppss-stats 5)) (- pos (point)))
218 (setq pt-best (point) ppss-best nil))
219
220 (cond
221 ;; Quick case when we found a nearby pos.
222 ((< (- pos pt-best) syntax-ppss-max-span)
223 (incf (car (aref syntax-ppss-stats 2)))
224 (incf (cdr (aref syntax-ppss-stats 2)) (- pos pt-best))
225 (setq ppss (parse-partial-sexp pt-best pos nil nil ppss-best)))
226 ;; Slow case: compute the state from some known position and
227 ;; populate the cache so we won't need to do it again soon.
228 (t
229 (incf (car (aref syntax-ppss-stats 3)))
230 (incf (cdr (aref syntax-ppss-stats 3)) (- pos pt-min))
231
232 ;; If `pt-min' is too far, add a few intermediate entries.
233 (while (> (- pos pt-min) (* 2 syntax-ppss-max-span))
234 (setq ppss (parse-partial-sexp
235 pt-min (setq pt-min (/ (+ pt-min pos) 2))
236 nil nil ppss))
237 (let ((pair (cons pt-min ppss)))
238 (if cache-pred
239 (push pair (cdr cache-pred))
240 (push pair syntax-ppss-cache))))
241
242 ;; Compute the actual return value.
243 (setq ppss (parse-partial-sexp pt-min pos nil nil ppss))
244
245 ;; Debugging check.
246 ;; (let ((real-ppss (parse-partial-sexp (point-min) pos)))
247 ;; (setcar (last ppss 4) 0)
248 ;; (setcar (last real-ppss 4) 0)
249 ;; (setcar (last ppss 8) nil)
250 ;; (setcar (last real-ppss 8) nil)
251 ;; (unless (equal ppss real-ppss)
252 ;; (message "!!Syntax: %s != %s" ppss real-ppss)
253 ;; (setq ppss real-ppss)))
254
255 ;; Store it in the cache.
256 (let ((pair (cons pos ppss)))
257 (if cache-pred
258 (if (> (- (caar cache-pred) pos) syntax-ppss-max-span)
259 (push pair (cdr cache-pred))
260 (setcar cache-pred pair))
261 (if (or (null syntax-ppss-cache)
262 (> (- (caar syntax-ppss-cache) pos)
263 syntax-ppss-max-span))
264 (push pair syntax-ppss-cache)
265 (setcar syntax-ppss-cache pair)))))))))
266
267 (setq syntax-ppss-last (cons pos ppss))
268 ppss)
269 (args-out-of-range
270 ;; If the buffer is more narrowed than when we built the cache,
271 ;; we may end up calling parse-partial-sexp with a position before
272 ;; point-min. In that case, just parse from point-min assuming
273 ;; a nil state.
274 (parse-partial-sexp (point-min) pos)))))
275
276 ;; Debugging functions
277
278 (defun syntax-ppss-debug ()
279 (let ((pt nil)
280 (min-diffs nil))
281 (dolist (x (append syntax-ppss-cache (list (cons (point-min) nil))))
282 (when pt (push (- pt (car x)) min-diffs))
283 (setq pt (car x)))
284 min-diffs))
285
286 ;; XEmacs compatibility functions
287
288 ;; (defun buffer-syntactic-context (&optional buffer)
289 ;; "Syntactic context at point in BUFFER.
290 ;; Either of `string', `comment' or `nil'.
291 ;; This is an XEmacs compatibility function."
292 ;; (with-current-buffer (or buffer (current-buffer))
293 ;; (syntax-ppss-context (syntax-ppss))))
294
295 ;; (defun buffer-syntactic-context-depth (&optional buffer)
296 ;; "Syntactic parenthesis depth at point in BUFFER.
297 ;; This is an XEmacs compatibility function."
298 ;; (with-current-buffer (or buffer (current-buffer))
299 ;; (syntax-ppss-depth (syntax-ppss))))
300
301 (provide 'syntax)
302
303 ;; arch-tag: 302f1eeb-e77c-4680-a8c5-c543e01161a5
304 ;;; syntax.el ends here