annotate lisp/emacs-lisp/syntax.el @ 105489:8db047b72241

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