Trailing whitepace deleted.
[emacs.git] / lisp / emacs-lisp / syntax.el
blobd7ad3bacd4585260bb5a4321a6b663d993ff8261
1 ;;; syntax.el --- helper functions to find syntactic context
3 ;; Copyright (C) 2000 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: internal
8 ;; This file is part of GNU Emacs.
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 2, or (at your option)
13 ;; any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; The main exported function is `syntax-ppss'. You might also need
28 ;; to call `syntax-ppss-after-change-function' or to add it to
29 ;; after-change-functions'(although this is automatically done by
30 ;; syntax-ppss when needed, but that might fail if syntax-ppss is
31 ;; called in a context where after-change-functions is temporarily
32 ;; let-bound to nil).
34 ;;; Todo:
36 ;; - do something about the case where the syntax-table is changed.
37 ;; This typically happens with tex-mode and its `$' operator.
38 ;; - move font-lock-syntactic-keywords in here. Then again, maybe not.
39 ;; - new functions `syntax-state', ... to replace uses of parse-partial-state
40 ;; with something higher-level (similar to syntax-ppss-context).
41 ;; - interaction with mmm-mode.
42 ;; - what to do when the buffer is narrowed ?
44 ;;; Code:
46 ;; Note: PPSS stands for `parse-partial-sexp state'
48 (eval-when-compile (require 'cl))
50 (defsubst syntax-ppss-depth (ppss)
51 (nth 0 ppss))
53 (defsubst syntax-ppss-context (ppss)
54 (cond
55 ((nth 3 ppss) 'string)
56 ((nth 4 ppss) 'comment)
57 (t nil)))
59 (defvar syntax-ppss-max-span 20000
60 "Threshold below which cache info is deemed unnecessary.
61 We try to make sure that cache entries are at least this far apart
62 from each other, to avoid keeping too much useless info.")
64 (defvar syntax-begin-function nil
65 "Function to move back outside of any comment/string/paren.
66 This function should move the cursor back to some syntactically safe
67 point (where the PPSS is equivalent to nil).")
69 (defvar syntax-ppss-cache nil
70 "List of (POS . PPSS) pairs, in decreasing POS order.")
71 (make-variable-buffer-local 'syntax-ppss-cache)
72 (defvar syntax-ppss-last nil
73 "Cache of (LAST-POS . LAST-PPSS).")
74 (make-variable-buffer-local 'syntax-ppss-last)
76 (defun syntax-ppss-after-change-function (beg &rest ignored)
77 ;; Flush invalid cache entries.
78 (while (and syntax-ppss-cache (> (caar syntax-ppss-cache) beg))
79 (setq syntax-ppss-cache (cdr syntax-ppss-cache)))
80 ;; Throw away `last' value if made invalid.
81 (when (< beg (or (car syntax-ppss-last) 0))
82 (if (< beg (or (car (nth 10 syntax-ppss-last))
83 (nth 9 syntax-ppss-last)
84 (nth 2 syntax-ppss-last)
85 0))
86 (setq syntax-ppss-last nil)
87 (setcar syntax-ppss-last nil)))
88 ;; Unregister if there's no cache left. Sadly this doesn't work
89 ;; because `after-change-functions' is temporarily bound to nil here.
90 ;; (unless syntax-ppss-cache
91 ;; (remove-hook 'after-change-functions
92 ;; 'syntax-ppss-after-change-function t))
95 (defvar syntax-ppss-stats
96 [(0 . 0.0) (0 . 0.0) (0 . 0.0) (0 . 0.0) (0 . 0.0) (1 . 2500.0)])
97 (defun syntax-ppss-stats ()
98 (mapcar (lambda (x)
99 (condition-case nil
100 (cons (car x) (truncate (/ (cdr x) (car x))))
101 (error nil)))
102 syntax-ppss-stats))
104 ;;;###autoload
105 (defun syntax-ppss (&optional pos)
106 "Parse-Partial-Sexp State at POS.
107 The returned value is the same as `parse-partial-sexp' except that
108 the 2nd and 6th values of the returned state cannot be relied upon.
110 If the caller knows the PPSS of a nearby position, she can pass it
111 in OLP-PPSS (with or without its corresponding OLD-POS) to try and
112 avoid a more expansive scan.
113 Point is at POS when this function returns."
114 ;; Default values.
115 (unless pos (setq pos (point)))
117 (let ((old-ppss (cdr syntax-ppss-last))
118 (old-pos (car syntax-ppss-last))
119 (ppss nil)
120 (pt-min (point-min)))
121 (if (and old-pos (> old-pos pos)) (setq old-pos nil))
122 ;; Use the OLD-POS if usable and close. Don't update the `last' cache.
123 (if (and old-pos (< (- pos old-pos)
124 ;; The time to find PPSS using syntax-begin-function
125 ;; is assumed to be about 2 * distance.
126 (* 2 (/ (cdr (aref syntax-ppss-stats 5))
127 (1+ (car (aref syntax-ppss-stats 5)))))))
128 (progn
129 (incf (car (aref syntax-ppss-stats 0)))
130 (incf (cdr (aref syntax-ppss-stats 0)) (- pos old-pos))
131 (parse-partial-sexp old-pos pos nil nil old-ppss))
133 (cond
134 ;; Use OLD-PPSS if possible and close enough.
135 ((and (not old-pos) old-ppss
136 ;; BEWARE! We rely on the undocumented 9th field.
137 ;; The 9th field currently contains the list of positions
138 ;; of open-parens of the enclosing parens. I.e. those positions
139 ;; are outside of any string/comment and the first of those is
140 ;; outside of any paren (i.e. corresponds to a nil ppss).
141 ;; If this list is empty but we are in a string or comment,
142 ;; then the 8th field contains a similar "toplevel" position.
143 ;; If `pt-min' is too far from `pos', we could try to use
144 ;; other positions in (nth 9 old-ppss), but that doesn't seem
145 ;; to happen in practice and it would complicate this code
146 ;; (and the after-change-function code even more). But maybe it
147 ;; would be useful in "degenerate" cases such as when the whole
148 ;; file is wrapped in a set of parenthesis.
149 (setq pt-min (or (car (nth 9 old-ppss))
150 (nth 8 old-ppss)
151 (nth 2 old-ppss)))
152 (<= pt-min pos) (< (- pos pt-min) syntax-ppss-max-span))
153 (incf (car (aref syntax-ppss-stats 1)))
154 (incf (cdr (aref syntax-ppss-stats 1)) (- pos pt-min))
155 (setq ppss (parse-partial-sexp pt-min pos)))
156 ;; The OLD-* data can't be used. Consult the cache.
158 (let ((cache-pred nil)
159 (cache syntax-ppss-cache)
160 (pt-min (point-min))
161 ;; I differentiate between PT-MIN and PT-BEST because I feel
162 ;; like it might be important to ensure that the cache is only
163 ;; filled with 100% sure data (whereas syntax-begin-function
164 ;; might return incorrect data). Maybe that's just stupid.
165 (pt-best (point-min))
166 (ppss-best nil))
167 ;; look for a usable cache entry.
168 (while (and cache (< pos (caar cache)))
169 (setq cache-pred cache)
170 (setq cache (cdr cache)))
171 (if cache (setq pt-min (caar cache) ppss (cdar cache)))
173 ;; Setup the after-change function if necessary.
174 (unless (or syntax-ppss-cache syntax-ppss-last)
175 (add-hook 'after-change-functions
176 'syntax-ppss-after-change-function nil t))
178 ;; Use the best of OLD-POS and CACHE.
179 (if (or (not old-pos) (< old-pos pt-min))
180 (setq pt-best pt-min ppss-best ppss)
181 (incf (car (aref syntax-ppss-stats 4)))
182 (incf (cdr (aref syntax-ppss-stats 4)) (- pos old-pos))
183 (setq pt-best old-pos ppss-best old-ppss))
185 ;; Use the `syntax-begin-function' if available.
186 ;; We could try using that function earlier, but:
187 ;; - The result might not be 100% reliable, so it's better to use
188 ;; the cache if available.
189 ;; - The function might be slow.
190 ;; - If this function almost always finds a safe nearby spot,
191 ;; the cache won't be populated, so consulting it is cheap.
192 (unless (or syntax-begin-function
193 (not (boundp 'font-lock-beginning-of-syntax-function))
194 (not font-lock-beginning-of-syntax-function))
195 (set (make-local-variable 'syntax-begin-function)
196 font-lock-beginning-of-syntax-function))
197 (when (and syntax-begin-function
198 (progn (goto-char pos)
199 (funcall syntax-begin-function)
200 ;; Make sure it's better.
201 (> (point) pt-best))
202 ;; Simple sanity check.
203 (not (memq (get-text-property (point) 'face)
204 '(font-lock-string-face font-lock-comment-face
205 font-lock-doc-face))))
206 (incf (car (aref syntax-ppss-stats 5)))
207 (incf (cdr (aref syntax-ppss-stats 5)) (- pos (point)))
208 (setq pt-best (point) ppss-best nil))
210 (cond
211 ;; Quick case when we found a nearby pos.
212 ((< (- pos pt-best) syntax-ppss-max-span)
213 (incf (car (aref syntax-ppss-stats 2)))
214 (incf (cdr (aref syntax-ppss-stats 2)) (- pos pt-best))
215 (setq ppss (parse-partial-sexp pt-best pos nil nil ppss-best)))
216 ;; Slow case: compute the state from some known position and
217 ;; populate the cache so we won't need to do it again soon.
219 (incf (car (aref syntax-ppss-stats 3)))
220 (incf (cdr (aref syntax-ppss-stats 3)) (- pos pt-min))
222 ;; If `pt-min' is too far, add a few intermediate entries.
223 (while (> (- pos pt-min) (* 2 syntax-ppss-max-span))
224 (setq ppss (parse-partial-sexp
225 pt-min (setq pt-min (/ (+ pt-min pos) 2))
226 nil nil ppss))
227 (let ((pair (cons pt-min ppss)))
228 (if cache-pred
229 (push pair (cdr cache-pred))
230 (push pair syntax-ppss-cache))))
232 ;; Compute the actual return value.
233 (setq ppss (parse-partial-sexp pt-min pos nil nil ppss))
235 ;; Debugging check.
236 ;; (let ((real-ppss (parse-partial-sexp (point-min) pos)))
237 ;; (setcar (last ppss 4) 0)
238 ;; (setcar (last real-ppss 4) 0)
239 ;; (setcar (last ppss 8) nil)
240 ;; (setcar (last real-ppss 8) nil)
241 ;; (unless (equal ppss real-ppss)
242 ;; (message "!!Syntax: %s != %s" ppss real-ppss)
243 ;; (setq ppss real-ppss)))
245 ;; Store it in the cache.
246 (let ((pair (cons pos ppss)))
247 (if cache-pred
248 (if (> (- (caar cache-pred) pos) syntax-ppss-max-span)
249 (push pair (cdr cache-pred))
250 (setcar cache-pred pair))
251 (if (or (null syntax-ppss-cache)
252 (> (- (caar syntax-ppss-cache) pos)
253 syntax-ppss-max-span))
254 (push pair syntax-ppss-cache)
255 (setcar syntax-ppss-cache pair)))))))))
257 (setq syntax-ppss-last (cons pos ppss))
258 ppss)))
260 ;; Debugging functions
262 (defun syntax-ppss-debug ()
263 (let ((pt nil)
264 (min-diffs nil))
265 (dolist (x (append syntax-ppss-cache (list (cons (point-min) nil))))
266 (when pt (push (- pt (car x)) min-diffs))
267 (setq pt (car x)))
268 min-diffs))
270 ;; XEmacs compatibility functions
272 ;; (defun buffer-syntactic-context (&optional buffer)
273 ;; "Syntactic context at point in BUFFER.
274 ;; Either of `string', `comment' or `nil'.
275 ;; This is an XEmacs compatibility function."
276 ;; (with-current-buffer (or buffer (current-buffer))
277 ;; (syntax-ppss-context (syntax-ppss))))
279 ;; (defun buffer-syntactic-context-depth (&optional buffer)
280 ;; "Syntactic parenthesis depth at point in BUFFER.
281 ;; This is an XEmacs compatibility function."
282 ;; (with-current-buffer (or buffer (current-buffer))
283 ;; (syntax-ppss-depth (syntax-ppss))))
285 (provide 'syntax)
286 ;;; syntax.el ends here