aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKarl Heuer1999-08-16 03:18:40 +0000
committerKarl Heuer1999-08-16 03:18:40 +0000
commit050d5fc2205ccd26c1014d96790646c2e06716be (patch)
tree61d41530c1561bb9689f250df0b9793c9a095857
parent7220ca11af9b20616e893e8d16dd6f77ac488bee (diff)
downloademacs-050d5fc2205ccd26c1014d96790646c2e06716be.tar.gz
emacs-050d5fc2205ccd26c1014d96790646c2e06716be.zip
(show-paren-mode): Support making show-paren-mode
a buffer-local variable. Don't check for a window system. (show-paren-function): Check whether show-paren-function is enabled in current buffer; do the right thing if not. Don't check for a window system. (show-paren-mode): Make it a user variable.
-rw-r--r--lisp/paren.el232
1 files changed, 125 insertions, 107 deletions
diff --git a/lisp/paren.el b/lisp/paren.el
index b59b96e4f40..50af83bf3b2 100644
--- a/lisp/paren.el
+++ b/lisp/paren.el
@@ -41,7 +41,7 @@
41(defvar show-paren-overlay-1 nil) 41(defvar show-paren-overlay-1 nil)
42 42
43(defcustom show-paren-mode nil 43(defcustom show-paren-mode nil
44 "Toggle Show Paren mode. 44 "*Toggle Show Paren mode.
45When Show Paren mode is enabled, any matching parenthesis is highlighted 45When Show Paren mode is enabled, any matching parenthesis is highlighted
46after `show-paren-delay' seconds of Emacs idle time. 46after `show-paren-delay' seconds of Emacs idle time.
47Setting this variable directly does not take effect; 47Setting this variable directly does not take effect;
@@ -99,121 +99,139 @@ Returns the new status of Show Paren mode (non-nil means on).
99When Show Paren mode is enabled, any matching parenthesis is highlighted 99When Show Paren mode is enabled, any matching parenthesis is highlighted
100in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time." 100in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time."
101 (interactive "P") 101 (interactive "P")
102 (when window-system 102 (let ((on-p (if arg
103 (let ((on-p (if arg 103 (> (prefix-numeric-value arg) 0)
104 (> (prefix-numeric-value arg) 0) 104 (not show-paren-mode))))
105 (not show-paren-mode)))) 105 (setq show-paren-mode on-p)
106 (setq blink-matching-paren-on-screen (not on-p)) 106 ;; Turn off the usual paren-matching method
107 (when show-paren-idle-timer 107 ;; when this one is turned on.
108 (if (local-variable-p 'show-paren-mode)
109 (make-local-variable 'blink-matching-paren-on-screen)
110 (kill-local-variable 'blink-matching-paren-on-screen))
111 (setq blink-matching-paren-on-screen (not on-p))
112
113 ;; Now enable or disable the mechanism.
114 ;; First get rid of the old idle timer.
115 (if show-paren-idle-timer
108 (cancel-timer show-paren-idle-timer)) 116 (cancel-timer show-paren-idle-timer))
109 (if on-p 117 (setq show-paren-idle-timer nil)
110 (setq show-paren-idle-timer (run-with-idle-timer 118 ;; If show-paren-mode is enabled in some buffer now,
111 show-paren-delay t 119 ;; set up a new timer.
112 'show-paren-function)) 120 (when (memq t (mapcar (lambda (buffer)
113 (and show-paren-overlay (overlay-buffer show-paren-overlay) 121 (with-current-buffer buffer
114 (delete-overlay show-paren-overlay)) 122 show-paren-mode))
115 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1) 123 (buffer-list)))
116 (delete-overlay show-paren-overlay-1))) 124 (setq show-paren-idle-timer (run-with-idle-timer
117 (setq show-paren-mode on-p)))) 125 show-paren-delay t
126 'show-paren-function)))
127 (unless on-p
128 (and show-paren-overlay
129 (eq (overlay-buffer show-paren-overlay) (current-buffer))
130 (delete-overlay show-paren-overlay))
131 (and show-paren-overlay-1
132 (eq (overlay-buffer show-paren-overlay-1) (current-buffer))
133 (delete-overlay show-paren-overlay-1)))))
118 134
119;; Find the place to show, if there is one, 135;; Find the place to show, if there is one,
120;; and show it until input arrives. 136;; and show it until input arrives.
121(defun show-paren-function () 137(defun show-paren-function ()
122 ;; Do nothing if no window system to display results with. 138 (if show-paren-mode
123 ;; Do nothing if executing keyboard macro. 139 (let (pos dir mismatch face (oldpos (point)))
124 ;; Do nothing if input is pending. 140 (cond ((eq (char-syntax (preceding-char)) ?\))
125 (when window-system 141 (setq dir -1))
126 (let (pos dir mismatch face (oldpos (point))) 142 ((eq (char-syntax (following-char)) ?\()
127 (cond ((eq (char-syntax (preceding-char)) ?\)) 143 (setq dir 1)))
128 (setq dir -1))
129 ((eq (char-syntax (following-char)) ?\()
130 (setq dir 1)))
131 ;;
132 ;; Find the other end of the sexp.
133 (when dir
134 (save-excursion
135 (save-restriction
136 ;; Determine the range within which to look for a match.
137 (when blink-matching-paren-distance
138 (narrow-to-region
139 (max (point-min) (- (point) blink-matching-paren-distance))
140 (min (point-max) (+ (point) blink-matching-paren-distance))))
141 ;; Scan across one sexp within that range.
142 ;; Errors or nil mean there is a mismatch.
143 (condition-case ()
144 (setq pos (scan-sexps (point) dir))
145 (error (setq pos t mismatch t)))
146 ;; If found a "matching" paren, see if it is the right
147 ;; kind of paren to match the one we started at.
148 (when (integerp pos)
149 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
150 (when (/= (char-syntax (char-after beg)) ?\$)
151 (setq mismatch
152 (not (eq (char-before end)
153 ;; This can give nil.
154 (matching-paren (char-after beg)))))))))))
155 ;;
156 ;; Highlight the other end of the sexp, or unhighlight if none.
157 (if (not pos)
158 (progn
159 ;; If not at a paren that has a match,
160 ;; turn off any previous paren highlighting.
161 (and show-paren-overlay (overlay-buffer show-paren-overlay)
162 (delete-overlay show-paren-overlay))
163 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
164 (delete-overlay show-paren-overlay-1)))
165 ;;
166 ;; Use the correct face.
167 (if mismatch
168 (progn
169 (if show-paren-ring-bell-on-mismatch
170 (beep))
171 (setq face 'show-paren-mismatch-face))
172 (setq face 'show-paren-match-face))
173 ;; 144 ;;
174 ;; If matching backwards, highlight the closeparen 145 ;; Find the other end of the sexp.
175 ;; before point as well as its matching open. 146 (when dir
176 ;; If matching forward, and the openparen is unbalanced, 147 (save-excursion
177 ;; highlight the paren at point to indicate misbalance. 148 (save-restriction
178 ;; Otherwise, turn off any such highlighting. 149 ;; Determine the range within which to look for a match.
179 (if (and (= dir 1) (integerp pos)) 150 (when blink-matching-paren-distance
180 (when (and show-paren-overlay-1 151 (narrow-to-region
181 (overlay-buffer show-paren-overlay-1)) 152 (max (point-min) (- (point) blink-matching-paren-distance))
182 (delete-overlay show-paren-overlay-1)) 153 (min (point-max) (+ (point) blink-matching-paren-distance))))
183 (let ((from (if (= dir 1) 154 ;; Scan across one sexp within that range.
184 (point) 155 ;; Errors or nil mean there is a mismatch.
185 (forward-point -1))) 156 (condition-case ()
186 (to (if (= dir 1) 157 (setq pos (scan-sexps (point) dir))
187 (forward-point 1) 158 (error (setq pos t mismatch t)))
188 (point)))) 159 ;; If found a "matching" paren, see if it is the right
189 (if show-paren-overlay-1 160 ;; kind of paren to match the one we started at.
190 (move-overlay show-paren-overlay-1 from to (current-buffer)) 161 (when (integerp pos)
191 (setq show-paren-overlay-1 (make-overlay from to))) 162 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
192 ;; Always set the overlay face, since it varies. 163 (when (/= (char-syntax (char-after beg)) ?\$)
193 (overlay-put show-paren-overlay-1 'face face))) 164 (setq mismatch
165 (not (eq (char-before end)
166 ;; This can give nil.
167 (matching-paren (char-after beg)))))))))))
194 ;; 168 ;;
195 ;; Turn on highlighting for the matching paren, if found. 169 ;; Highlight the other end of the sexp, or unhighlight if none.
196 ;; If it's an unmatched paren, turn off any such highlighting. 170 (if (not pos)
197 (unless (integerp pos) 171 (progn
198 (delete-overlay show-paren-overlay)) 172 ;; If not at a paren that has a match,
199 (let ((to (if (or (eq show-paren-style 'expression) 173 ;; turn off any previous paren highlighting.
200 (and (eq show-paren-style 'mixed) 174 (and show-paren-overlay (overlay-buffer show-paren-overlay)
201 (not (pos-visible-in-window-p pos)))) 175 (delete-overlay show-paren-overlay))
202 (point) 176 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
203 pos)) 177 (delete-overlay show-paren-overlay-1)))
204 (from (if (or (eq show-paren-style 'expression) 178 ;;
179 ;; Use the correct face.
180 (if mismatch
181 (progn
182 (if show-paren-ring-bell-on-mismatch
183 (beep))
184 (setq face 'show-paren-mismatch-face))
185 (setq face 'show-paren-match-face))
186 ;;
187 ;; If matching backwards, highlight the closeparen
188 ;; before point as well as its matching open.
189 ;; If matching forward, and the openparen is unbalanced,
190 ;; highlight the paren at point to indicate misbalance.
191 ;; Otherwise, turn off any such highlighting.
192 (if (and (= dir 1) (integerp pos))
193 (when (and show-paren-overlay-1
194 (overlay-buffer show-paren-overlay-1))
195 (delete-overlay show-paren-overlay-1))
196 (let ((from (if (= dir 1)
197 (point)
198 (forward-point -1)))
199 (to (if (= dir 1)
200 (forward-point 1)
201 (point))))
202 (if show-paren-overlay-1
203 (move-overlay show-paren-overlay-1 from to (current-buffer))
204 (setq show-paren-overlay-1 (make-overlay from to)))
205 ;; Always set the overlay face, since it varies.
206 (overlay-put show-paren-overlay-1 'face face)))
207 ;;
208 ;; Turn on highlighting for the matching paren, if found.
209 ;; If it's an unmatched paren, turn off any such highlighting.
210 (unless (integerp pos)
211 (delete-overlay show-paren-overlay))
212 (let ((to (if (or (eq show-paren-style 'expression)
205 (and (eq show-paren-style 'mixed) 213 (and (eq show-paren-style 'mixed)
206 (not (pos-visible-in-window-p pos)))) 214 (not (pos-visible-in-window-p pos))))
207 pos 215 (point)
208 (save-excursion 216 pos))
209 (goto-char pos) 217 (from (if (or (eq show-paren-style 'expression)
210 (forward-point (- dir)))))) 218 (and (eq show-paren-style 'mixed)
211 (if show-paren-overlay 219 (not (pos-visible-in-window-p pos))))
212 (move-overlay show-paren-overlay from to (current-buffer)) 220 pos
213 (setq show-paren-overlay (make-overlay from to)))) 221 (save-excursion
214 ;; 222 (goto-char pos)
215 ;; Always set the overlay face, since it varies. 223 (forward-point (- dir))))))
216 (overlay-put show-paren-overlay 'face face))))) 224 (if show-paren-overlay
225 (move-overlay show-paren-overlay from to (current-buffer))
226 (setq show-paren-overlay (make-overlay from to))))
227 ;;
228 ;; Always set the overlay face, since it varies.
229 (overlay-put show-paren-overlay 'face face)))
230 ;; show-paren-mode is nil in this buffer.
231 (and show-paren-overlay
232 (delete-overlay show-paren-overlay))
233 (and show-paren-overlay-1
234 (delete-overlay show-paren-overlay-1))))
217 235
218(provide 'paren) 236(provide 'paren)
219 237