annotate lisp/emacs-lisp/syntax.el @ 110501:906ce0f04ef3

Trivial bytecomp.el change. * lisp/emacs-lisp/bytecomp.el (byte-compile-file-form-defvar): (byte-compile-defvar, byte-compile-cl-warn): Start warnings with lower-case, like the majority.
author Glenn Morris <rgm@gnu.org>
date Thu, 23 Sep 2010 00:55:01 -0700
parents 14dab55b2888
children 417b1e4d63cd
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,
106815
1d1d5d9bd884 Add 2010 to copyright years.
Glenn Morris <rgm@gnu.org>
parents: 100908
diff changeset
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 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 ;; - new functions `syntax-state', ... to replace uses of parse-partial-state
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
38 ;; with something higher-level (similar to syntax-ppss-context).
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
39 ;; - interaction with mmm-mode.
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
65194
364fe66bab84 (font-lock-beginning-of-syntax-function): Add defvar.
Juanma Barranquero <lekktu@gmail.com>
parents: 64751
diff changeset
47 (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
48
110305
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
49 ;;; Applying syntax-table properties where needed.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
50
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
51 (defvar syntax-propertize-function nil
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
52 ;; Rather than a -functions hook, this is a -function because it's easier
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
53 ;; to do a single scan than several scans: with multiple scans, one cannot
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
54 ;; assume that the text before point has been propertized, so syntax-ppss
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
55 ;; gives unreliable results (and stores them in its cache to boot, so we'd
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
56 ;; have to flush that cache between each function, and we couldn't use
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
57 ;; syntax-ppss-flush-cache since that would not only flush the cache but also
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
58 ;; reset syntax-propertize--done which should not be done in this case).
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
59 "Mode-specific function to apply the syntax-table properties.
110403
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
60 Called with 2 arguments: START and END.
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
61 This function can call `syntax-ppss' on any position before END, but it
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
62 should not call `syntax-ppss-flush-cache', which means that it should not
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
63 call `syntax-ppss' on some position and later modify the buffer on some
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
64 earlier position.")
110305
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
65
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
66 (defvar syntax-propertize-chunk-size 500)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
67
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
68 (defvar syntax-propertize-extend-region-functions
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
69 '(syntax-propertize-wholelines)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
70 "Special hook run just before proceeding to propertize a region.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
71 This is used to allow major modes to help `syntax-propertize' find safe buffer
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
72 positions as beginning and end of the propertized region. Its most common use
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
73 is to solve the problem of /identification/ of multiline elements by providing
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
74 a function that tries to find such elements and move the boundaries such that
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
75 they do not fall in the middle of one.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
76 Each function is called with two arguments (START and END) and it should return
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
77 either a cons (NEW-START . NEW-END) or nil if no adjustment should be made.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
78 These functions are run in turn repeatedly until they all return nil.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
79 Put first the functions more likely to cause a change and cheaper to compute.")
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
80 ;; Mark it as a special hook which doesn't use any global setting
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
81 ;; (i.e. doesn't obey the element t in the buffer-local value).
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
82 (make-variable-buffer-local 'syntax-propertize-extend-region-functions)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
83
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
84 (defun syntax-propertize-wholelines (start end)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
85 (goto-char start)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
86 (cons (line-beginning-position)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
87 (progn (goto-char end)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
88 (if (bolp) (point) (line-beginning-position 2)))))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
89
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
90 (defun syntax-propertize-multiline (beg end)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
91 "Let `syntax-propertize' pay attention to the syntax-multiline property."
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
92 (when (and (> beg (point-min))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
93 (get-text-property (1- beg) 'syntax-multiline))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
94 (setq beg (or (previous-single-property-change beg 'syntax-multiline)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
95 (point-min))))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
96 ;;
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
97 (when (get-text-property end 'font-lock-multiline)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
98 (setq end (or (text-property-any end (point-max)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
99 'syntax-multiline nil)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
100 (point-max))))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
101 (cons beg end))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
102
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
103 (defvar syntax-propertize--done -1
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
104 "Position upto which syntax-table properties have been set.")
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
105 (make-variable-buffer-local 'syntax-propertize--done)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
106
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
107 (defun syntax-propertize--shift-groups (re n)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
108 (replace-regexp-in-string
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
109 "\\\\(\\?\\([0-9]+\\):"
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
110 (lambda (s)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
111 (replace-match
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
112 (number-to-string (+ n (string-to-number (match-string 1 s))))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
113 t t s 1))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
114 re t t))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
115
110403
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
116 (defmacro syntax-propertize-precompile-rules (&rest rules)
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
117 "Return a precompiled form of RULES to pass to `syntax-propertize-rules'.
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
118 The arg RULES can be of the same form as in `syntax-propertize-rules'.
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
119 The return value is an object that can be passed as a rule to
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
120 `syntax-propertize-rules'.
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
121 I.e. this is useful only when you want to share rules among several
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
122 syntax-propertize-functions."
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
123 (declare (debug syntax-propertize-rules))
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
124 ;; Precompile? Yeah, right!
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
125 ;; Seriously, tho, this is a macro for 2 reasons:
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
126 ;; - we could indeed do some pre-compilation at some point in the future,
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
127 ;; e.g. fi/when we switch to a DFA-based implementation of
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
128 ;; syntax-propertize-rules.
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
129 ;; - this lets Edebug properly annotate the expressions inside RULES.
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
130 `',rules)
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
131
110305
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
132 (defmacro syntax-propertize-rules (&rest rules)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
133 "Make a function that applies RULES for use in `syntax-propertize-function'.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
134 The function will scan the buffer, applying the rules where they match.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
135 The buffer is scanned a single time, like \"lex\" would, rather than once
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
136 per rule.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
137
110403
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
138 Each RULE can be a symbol, in which case that symbol's value should be,
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
139 at macro-expansion time, a precompiled set of rules, as returned
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
140 by `syntax-propertize-precompile-rules'.
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
141
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
142 Otherwise, RULE should have the form (REGEXP HIGHLIGHT1 ... HIGHLIGHTn), where
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
143 REGEXP is an expression (evaluated at time of macro-expansion) that returns
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
144 a regexp, and where HIGHLIGHTs have the form (NUMBER SYNTAX) which means to
110305
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
145 apply the property SYNTAX to the chars matched by the subgroup NUMBER
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
146 of the regular expression, if NUMBER did match.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
147 SYNTAX is an expression that returns a value to apply as `syntax-table'
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
148 property. Some expressions are handled specially:
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
149 - if SYNTAX is a string, then it is converted with `string-to-syntax';
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
150 - if SYNTAX has the form (prog1 EXP . EXPS) then the value returned by EXP
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
151 will be applied to the buffer before running EXPS and if EXP is a string it
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
152 is also converted with `string-to-syntax'.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
153 The SYNTAX expression is responsible to save the `match-data' if needed
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
154 for subsequent HIGHLIGHTs.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
155 Also SYNTAX is free to move point, in which case RULES may not be applied to
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
156 some parts of the text or may be applied several times to other parts.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
157
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
158 Note: back-references in REGEXPs do not work."
110403
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
159 (declare (debug (&rest &or symbolp ;FIXME: edebug this eval step.
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
160 (form &rest
110305
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
161 (numberp
110403
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
162 [&or stringp ;FIXME: Use &wrap
110305
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
163 ("prog1" [&or stringp def-form] def-body)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
164 def-form])))))
110403
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
165 (let ((newrules nil))
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
166 (while rules
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
167 (if (symbolp (car rules))
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
168 (setq rules (append (symbol-value (pop rules)) rules))
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
169 (push (pop rules) newrules)))
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
170 (setq rules (nreverse newrules)))
110305
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
171 (let* ((offset 0)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
172 (branches '())
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
173 ;; We'd like to use a real DFA-based lexer, usually, but since Emacs
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
174 ;; doesn't have one yet, we fallback on building one large regexp
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
175 ;; and use groups to determine which branch of the regexp matched.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
176 (re
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
177 (mapconcat
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
178 (lambda (rule)
110403
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
179 (let* ((orig-re (eval (car rule)))
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
180 (re orig-re))
110305
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
181 (when (and (assq 0 rule) (cdr rules))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
182 ;; If there's more than 1 rule, and the rule want to apply
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
183 ;; highlight to match 0, create an extra group to be able to
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
184 ;; tell when *this* match 0 has succeeded.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
185 (incf offset)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
186 (setq re (concat "\\(" re "\\)")))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
187 (setq re (syntax-propertize--shift-groups re offset))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
188 (let ((code '())
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
189 (condition
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
190 (cond
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
191 ((assq 0 rule) (if (zerop offset) t
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
192 `(match-beginning ,offset)))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
193 ((null (cddr rule))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
194 `(match-beginning ,(+ offset (car (cadr rule)))))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
195 (t
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
196 `(or ,@(mapcar
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
197 (lambda (case)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
198 `(match-beginning ,(+ offset (car case))))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
199 (cdr rule))))))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
200 (nocode t)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
201 (offset offset))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
202 ;; If some of the subgroup rules include Elisp code, then we
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
203 ;; need to set the match-data so it's consistent with what the
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
204 ;; code expects. If not, then we can simply use shifted
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
205 ;; offset in our own code.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
206 (unless (zerop offset)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
207 (dolist (case (cdr rule))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
208 (unless (stringp (cadr case))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
209 (setq nocode nil)))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
210 (unless nocode
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
211 (push `(let ((md (match-data 'ints)))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
212 ;; Keep match 0 as is, but shift everything else.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
213 (setcdr (cdr md) (nthcdr ,(* (1+ offset) 2) md))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
214 (set-match-data md))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
215 code)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
216 (setq offset 0)))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
217 ;; Now construct the code for each subgroup rules.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
218 (dolist (case (cdr rule))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
219 (assert (null (cddr case)))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
220 (let* ((gn (+ offset (car case)))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
221 (action (nth 1 case))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
222 (thiscode
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
223 (cond
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
224 ((stringp action)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
225 `((put-text-property
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
226 (match-beginning ,gn) (match-end ,gn)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
227 'syntax-table
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
228 ',(string-to-syntax action))))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
229 ((eq (car-safe action) 'ignore)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
230 (cdr action))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
231 ((eq (car-safe action) 'prog1)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
232 (if (stringp (nth 1 action))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
233 `((put-text-property
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
234 (match-beginning ,gn) (match-end ,gn)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
235 'syntax-table
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
236 ',(string-to-syntax (nth 1 action)))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
237 ,@(nthcdr 2 action))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
238 `((let ((mb (match-beginning ,gn))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
239 (me (match-end ,gn))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
240 (syntax ,(nth 1 action)))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
241 (if syntax
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
242 (put-text-property
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
243 mb me 'syntax-table syntax))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
244 ,@(nthcdr 2 action)))))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
245 (t
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
246 `((let ((mb (match-beginning ,gn))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
247 (me (match-end ,gn))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
248 (syntax ,action))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
249 (if syntax
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
250 (put-text-property
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
251 mb me 'syntax-table syntax))))))))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
252
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
253 (if (or (not (cddr rule)) (zerop gn))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
254 (setq code (nconc (nreverse thiscode) code))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
255 (push `(if (match-beginning ,gn)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
256 ;; Try and generate clean code with no
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
257 ;; extraneous progn.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
258 ,(if (null (cdr thiscode))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
259 (car thiscode)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
260 `(progn ,@thiscode)))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
261 code))))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
262 (push (cons condition (nreverse code))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
263 branches))
110403
14dab55b2888 Fix and improve last syntax-propertize patch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110305
diff changeset
264 (incf offset (regexp-opt-depth orig-re))
110305
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
265 re))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
266 rules
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
267 "\\|")))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
268 `(lambda (start end)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
269 (goto-char start)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
270 (while (and (< (point) end)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
271 (re-search-forward ,re end t))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
272 (cond ,@(nreverse branches))))))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
273
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
274 (defun syntax-propertize-via-font-lock (keywords)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
275 "Propertize for syntax in START..END using font-lock syntax.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
276 KEYWORDS obeys the format used in `font-lock-syntactic-keywords'.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
277 The return value is a function suitable for `syntax-propertize-function'."
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
278 (lexical-let ((keywords keywords))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
279 (lambda (start end)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
280 (with-no-warnings
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
281 (let ((font-lock-syntactic-keywords keywords))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
282 (font-lock-fontify-syntactic-keywords-region start end)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
283 ;; In case it was eval'd/compiled.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
284 (setq keywords font-lock-syntactic-keywords))))))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
285
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
286 (defun syntax-propertize (pos)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
287 "Ensure that syntax-table properties are set upto POS."
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
288 (when (and syntax-propertize-function
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
289 (< syntax-propertize--done pos))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
290 ;; (message "Needs to syntax-propertize from %s to %s"
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
291 ;; syntax-propertize--done pos)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
292 (set (make-local-variable 'parse-sexp-lookup-properties) t)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
293 (save-excursion
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
294 (with-silent-modifications
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
295 (let* ((start (max syntax-propertize--done (point-min)))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
296 (end (max pos
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
297 (min (point-max)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
298 (+ start syntax-propertize-chunk-size))))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
299 (funs syntax-propertize-extend-region-functions))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
300 (while funs
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
301 (let ((new (funcall (pop funs) start end)))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
302 (if (or (null new)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
303 (and (>= (car new) start) (<= (cdr new) end)))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
304 nil
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
305 (setq start (car new))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
306 (setq end (cdr new))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
307 ;; If there's been a change, we should go through the
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
308 ;; list again since this new position may
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
309 ;; warrant a different answer from one of the funs we've
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
310 ;; already seen.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
311 (unless (eq funs
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
312 (cdr syntax-propertize-extend-region-functions))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
313 (setq funs syntax-propertize-extend-region-functions)))))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
314 ;; Move the limit before calling the function, so the function
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
315 ;; can use syntax-ppss.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
316 (setq syntax-propertize--done end)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
317 ;; (message "syntax-propertizing from %s to %s" start end)
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
318 (remove-text-properties start end
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
319 '(syntax-table nil syntax-multiline nil))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
320 (funcall syntax-propertize-function start end))))))
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
321
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
322 ;;; Incrementally compute and memoize parser state.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
323
39756
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
324 (defsubst syntax-ppss-depth (ppss)
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
325 (nth 0 ppss))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
326
74600
ab428131b528 (syntax-ppss-toplevel-pos): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70505
diff changeset
327 (defun syntax-ppss-toplevel-pos (ppss)
74724
80f7458bbb20 (syntax-ppss-toplevel-pos): Improve docstring.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 74600
diff changeset
328 "Get the latest syntactically outermost position found in a syntactic scan.
109698
e3caf0b09548 * lisp/emacs-lisp/syntax.el (syntax-ppss-toplevel-pos): Fix typo in docstring.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 106815
diff changeset
329 PPSS is a scan state, as returned by `parse-partial-sexp' or `syntax-ppss'.
74724
80f7458bbb20 (syntax-ppss-toplevel-pos): Improve docstring.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 74600
diff changeset
330 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
331 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
332 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
333 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
334 ;; 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
335 ;; 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
336 ;; 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
337 ;; 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
338 ;; 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
339 ;; field contains a similar "toplevel" position.
74600
ab428131b528 (syntax-ppss-toplevel-pos): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70505
diff changeset
340 (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
341 (nth 8 ppss)))
74600
ab428131b528 (syntax-ppss-toplevel-pos): New fun.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 70505
diff changeset
342
39756
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
343 (defsubst syntax-ppss-context (ppss)
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
344 (cond
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
345 ((nth 3 ppss) 'string)
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
346 ((nth 4 ppss) 'comment)
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
347 (t nil)))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
348
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
349 (defvar syntax-ppss-max-span 20000
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
350 "Threshold below which cache info is deemed unnecessary.
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
351 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
352 from each other, to avoid keeping too much useless info.")
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
353
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
354 (defvar syntax-begin-function nil
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
355 "Function to move back outside of any comment/string/paren.
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
356 This function should move the cursor back to some syntactically safe
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
357 point (where the PPSS is equivalent to nil).")
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
358
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
359 (defvar syntax-ppss-cache nil
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
360 "List of (POS . PPSS) pairs, in decreasing POS order.")
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
361 (make-variable-buffer-local 'syntax-ppss-cache)
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
362 (defvar syntax-ppss-last nil
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
363 "Cache of (LAST-POS . LAST-PPSS).")
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
364 (make-variable-buffer-local 'syntax-ppss-last)
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
365
51941
9bf93e387b5e (syntax-ppss-flush-cache): Rename from syntax-ppss-after-change-function.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 49598
diff changeset
366 (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
367 (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
368 "Flush the cache of `syntax-ppss' starting at position BEG."
110305
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
369 ;; Set syntax-propertize to refontify anything past beg.
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
370 (setq syntax-propertize--done (min beg syntax-propertize--done))
39756
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
371 ;; Flush invalid cache entries.
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
372 (while (and syntax-ppss-cache (> (caar syntax-ppss-cache) beg))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
373 (setq syntax-ppss-cache (cdr syntax-ppss-cache)))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
374 ;; Throw away `last' value if made invalid.
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
375 (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
376 ;; 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
377 ;; 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
378 ;; 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
379 ;; 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
380 (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
381 (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
382 0))
39756
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
383 (setq syntax-ppss-last nil)
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
384 (setcar syntax-ppss-last nil)))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
385 ;; 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
386 ;; because `before-change-functions' is temporarily bound to nil here.
39756
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
387 ;; (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
388 ;; (remove-hook 'before-change-functions 'syntax-ppss-flush-cache t))
39756
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
389 )
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
390
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
391 (defvar syntax-ppss-stats
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
392 [(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
393 (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
394 (mapcar (lambda (x)
894b9bc4ca7a (syntax-ppss-stats): Be more robust when dividing by 0.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39889
diff changeset
395 (condition-case nil
894b9bc4ca7a (syntax-ppss-stats): Be more robust when dividing by 0.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 39889
diff changeset
396 (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
397 (error nil)))
39756
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
398 syntax-ppss-stats))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
399
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
400 (defun syntax-ppss (&optional pos)
98536
55ce7d80f7dc (syntax-ppss): Doc fix.
Andreas Schwab <schwab@suse.de>
parents: 94655
diff changeset
401 "Parse-Partial-Sexp State at POS, defaulting to point.
39756
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
402 The returned value is the same as `parse-partial-sexp' except that
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
403 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
404 Point is at POS when this function returns."
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
405 ;; Default values.
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
406 (unless pos (setq pos (point)))
110305
b10051866f51 New syntax-propertize functionality.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 110187
diff changeset
407 (syntax-propertize pos)
49598
0d8b17d428b5 Trailing whitepace deleted.
Juanma Barranquero <lekktu@gmail.com>
parents: 47356
diff changeset
408 ;;
39756
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
409 (let ((old-ppss (cdr syntax-ppss-last))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
410 (old-pos (car syntax-ppss-last))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
411 (ppss nil)
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
412 (pt-min (point-min)))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
413 (if (and old-pos (> old-pos pos)) (setq old-pos nil))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
414 ;; 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
415 (condition-case nil
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
416 (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
417 ;; 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
418 ;; 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
419 (* 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
420 (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
421 (progn
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
422 (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
423 (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
424 (parse-partial-sexp old-pos pos nil nil old-ppss))
39756
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
425
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
426 (cond
51942
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
427 ;; 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
428 ((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
429 ;; 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
430 ;; 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
431 ;; 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
432 ;; 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
433 ;; 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
434 ;; 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
435 ;; of parentheses.
7bf99b0f8330 (syntax-ppss-flush-cache, syntax-ppss): Use syntax-ppss-toplevel-pos.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 74724
diff changeset
436 (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
437 (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
438 (<= 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
439 (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
440 (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
441 (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
442 ;; The OLD-* data can't be used. Consult the cache.
39756
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
443 (t
51942
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
444 (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
445 (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
446 (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
447 ;; 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
448 ;; 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
449 ;; 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
450 ;; 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
451 ;; 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
452 (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
453 (ppss-best nil))
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
454 ;; 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
455 (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
456 (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
457 (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
458 (if cache (setq pt-min (caar cache) ppss (cdar cache)))
39756
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
459
70505
53adfd470fe4 (syntax-ppss): Flush the cache before rather than after a buffer modification.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 68648
diff changeset
460 ;; 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
461 (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
462 (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
463 'syntax-ppss-flush-cache t t))
39756
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
464
51942
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
465 ;; 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
466 (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
467 (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
468 (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
469 (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
470 (setq pt-best old-pos ppss-best old-ppss))
39756
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
471
51942
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
472 ;; 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
473 ;; 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
474 ;; - 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
475 ;; 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
476 ;; - 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
477 ;; - 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
478 ;; 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
479 (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
480 (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
481 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
482 (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
483 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
484 (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
485 (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
486 (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
487 ;; 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
488 (> (point) pt-best))
110187
98d8e4cd2326 * lisp/emacs-lisp/syntax.el (syntax-ppss): More sanity check to catch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109698
diff changeset
489 ;; Simple sanity checks.
98d8e4cd2326 * lisp/emacs-lisp/syntax.el (syntax-ppss): More sanity check to catch
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 109698
diff changeset
490 (< (point) pos) ; backward-paragraph can fail here.
51942
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
491 (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
492 '(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
493 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
494 (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
495 (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
496 (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
497
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
498 (cond
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
499 ;; 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
500 ((< (- 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
501 (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
502 (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
503 (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
504 ;; 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
505 ;; 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
506 (t
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
507 (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
508 (incf (cdr (aref syntax-ppss-stats 3)) (- pos pt-min))
39756
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
509
51942
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
510 ;; 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
511 (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
512 (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
513 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
514 nil nil ppss))
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
515 (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
516 (if cache-pred
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
517 (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
518 (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
519
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
520 ;; 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
521 (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
522
51942
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
523 ;; Debugging check.
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
524 ;; (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
525 ;; (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
526 ;; (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
527 ;; (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
528 ;; (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
529 ;; (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
530 ;; (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
531 ;; (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
532
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
533 ;; 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
534 (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
535 (if cache-pred
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
536 (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
537 (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
538 (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
539 (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
540 (> (- (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
541 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
542 (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
543 (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
544
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
545 (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
546 ppss)
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
547 (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
548 ;; 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
549 ;; 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
550 ;; 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
551 ;; a nil state.
5fdfe35f3ac4 (syntax-ppss): Catch the case where the buffer is narrowed.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 51941
diff changeset
552 (parse-partial-sexp (point-min) pos)))))
39756
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
553
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
554 ;; Debugging functions
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
555
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
556 (defun syntax-ppss-debug ()
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
557 (let ((pt nil)
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
558 (min-diffs nil))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
559 (dolist (x (append syntax-ppss-cache (list (cons (point-min) nil))))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
560 (when pt (push (- pt (car x)) min-diffs))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
561 (setq pt (car x)))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
562 min-diffs))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
563
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
564 ;; XEmacs compatibility functions
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
565
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
566 ;; (defun buffer-syntactic-context (&optional buffer)
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
567 ;; "Syntactic context at point in BUFFER.
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
568 ;; Either of `string', `comment' or `nil'.
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
569 ;; This is an XEmacs compatibility function."
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
570 ;; (with-current-buffer (or buffer (current-buffer))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
571 ;; (syntax-ppss-context (syntax-ppss))))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
572
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
573 ;; (defun buffer-syntactic-context-depth (&optional buffer)
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
574 ;; "Syntactic parenthesis depth at point in BUFFER.
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
575 ;; This is an XEmacs compatibility function."
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
576 ;; (with-current-buffer (or buffer (current-buffer))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
577 ;; (syntax-ppss-depth (syntax-ppss))))
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
578
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
579 (provide 'syntax)
52401
695cf19ef79e Add arch taglines
Miles Bader <miles@gnu.org>
parents: 51942
diff changeset
580
66266
f15e775dfb97 (syntax-ppss-flush-cache): Fix corner boundary case.
Stefan Monnier <monnier@iro.umontreal.ca>
parents: 65194
diff changeset
581 ;; arch-tag: 302f1eeb-e77c-4680-a8c5-c543e01161a5
39756
cc0f91bb368d New file.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff changeset
582 ;;; syntax.el ends here