annotate lisp/emacs-lisp/syntax.el @ 44598:0384805eb2d3

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