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