* net/tramp.el (tramp-handle-shell-command): Set process sentinel
[emacs.git] / lisp / paren.el
blobfb5c0bf36e927dcd53f3f2c9136f86522c8f7f1c
1 ;;; paren.el --- highlight matching paren
3 ;; Copyright (C) 1993, 1996, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: rms@gnu.org
6 ;; Maintainer: FSF
7 ;; Keywords: languages, faces
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Put this into your ~/.emacs:
28 ;; (show-paren-mode t)
30 ;; It will display highlighting on whatever paren matches the one
31 ;; before or after point.
33 ;;; Code:
35 (defgroup paren-showing nil
36 "Showing (un)matching of parens and expressions."
37 :prefix "show-paren-"
38 :group 'paren-matching)
40 ;; This is the overlay used to highlight the matching paren.
41 (defvar show-paren-overlay nil)
42 ;; This is the overlay used to highlight the closeparen right before point.
43 (defvar show-paren-overlay-1 nil)
45 (defcustom show-paren-style 'parenthesis
46 "Style used when showing a matching paren.
47 Valid styles are `parenthesis' (meaning show the matching paren),
48 `expression' (meaning show the entire expression enclosed by the paren) and
49 `mixed' (meaning show the matching paren if it is visible, and the expression
50 otherwise)."
51 :type '(choice (const parenthesis) (const expression) (const mixed))
52 :group 'paren-showing)
54 (defcustom show-paren-delay 0.125
55 "Time in seconds to delay before showing a matching paren."
56 :type '(number :tag "seconds")
57 :group 'paren-showing)
59 (defcustom show-paren-priority 1000
60 "Priority of paren highlighting overlays."
61 :type 'integer
62 :group 'paren-showing
63 :version "21.1")
65 (defcustom show-paren-ring-bell-on-mismatch nil
66 "If non-nil, beep if mismatched paren is detected."
67 :type 'boolean
68 :group 'paren-showing
69 :version "20.3")
71 (defgroup paren-showing-faces nil
72 "Group for faces of Show Paren mode."
73 :group 'paren-showing
74 :group 'faces
75 :version "22.1")
77 (defface show-paren-match
78 '((((class color) (background light))
79 :background "turquoise") ; looks OK on tty (becomes cyan)
80 (((class color) (background dark))
81 :background "steelblue3") ; looks OK on tty (becomes blue)
82 (((background dark))
83 :background "grey50")
85 :background "gray"))
86 "Show Paren mode face used for a matching paren."
87 :group 'paren-showing-faces)
88 (define-obsolete-face-alias 'show-paren-match-face 'show-paren-match "22.1")
90 (defface show-paren-mismatch
91 '((((class color)) (:foreground "white" :background "purple"))
92 (t (:inverse-video t)))
93 "Show Paren mode face used for a mismatching paren."
94 :group 'paren-showing-faces)
95 (define-obsolete-face-alias 'show-paren-mismatch-face
96 'show-paren-mismatch "22.1")
98 (defvar show-paren-highlight-openparen t
99 "*Non-nil turns on openparen highlighting when matching forward.")
101 (defvar show-paren-idle-timer nil)
103 ;;;###autoload
104 (define-minor-mode show-paren-mode
105 "Toggle Show Paren mode.
106 With prefix ARG, turn Show Paren mode on if and only if ARG is positive.
107 Returns the new status of Show Paren mode (non-nil means on).
109 When Show Paren mode is enabled, any matching parenthesis is highlighted
110 in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time."
111 :global t :group 'paren-showing
112 ;; Enable or disable the mechanism.
113 ;; First get rid of the old idle timer.
114 (if show-paren-idle-timer
115 (cancel-timer show-paren-idle-timer))
116 (setq show-paren-idle-timer nil)
117 ;; If show-paren-mode is enabled in some buffer now,
118 ;; set up a new timer.
119 (when (memq t (mapcar (lambda (buffer)
120 (with-current-buffer buffer
121 show-paren-mode))
122 (buffer-list)))
123 (setq show-paren-idle-timer (run-with-idle-timer
124 show-paren-delay t
125 'show-paren-function)))
126 (unless show-paren-mode
127 (and show-paren-overlay
128 (eq (overlay-buffer show-paren-overlay) (current-buffer))
129 (delete-overlay show-paren-overlay))
130 (and show-paren-overlay-1
131 (eq (overlay-buffer show-paren-overlay-1) (current-buffer))
132 (delete-overlay show-paren-overlay-1))))
134 ;; Find the place to show, if there is one,
135 ;; and show it until input arrives.
136 (defun show-paren-function ()
137 (if show-paren-mode
138 (let* ((oldpos (point))
139 (dir (cond ((eq (syntax-class (syntax-after (1- (point)))) 5) -1)
140 ((eq (syntax-class (syntax-after (point))) 4) 1)))
141 (unescaped
142 (when dir
143 ;; Verify an even number of quoting characters precede the paren.
144 ;; Follow the same logic as in `blink-matching-open'.
145 (= (if (= dir -1) 1 0)
146 (logand 1 (- (point)
147 (save-excursion
148 (if (= dir -1) (forward-char -1))
149 (skip-syntax-backward "/\\")
150 (point)))))))
151 pos mismatch face)
153 ;; Find the other end of the sexp.
154 (when unescaped
155 (save-excursion
156 (save-restriction
157 ;; Determine the range within which to look for a match.
158 (when blink-matching-paren-distance
159 (narrow-to-region
160 (max (point-min) (- (point) blink-matching-paren-distance))
161 (min (point-max) (+ (point) blink-matching-paren-distance))))
162 ;; Scan across one sexp within that range.
163 ;; Errors or nil mean there is a mismatch.
164 (condition-case ()
165 (setq pos (scan-sexps (point) dir))
166 (error (setq pos t mismatch t)))
167 ;; Move back the other way and verify we get back to the
168 ;; starting point. If not, these two parens don't really match.
169 ;; Maybe the one at point is escaped and doesn't really count.
170 (when (integerp pos)
171 (unless (condition-case ()
172 (eq (point) (scan-sexps pos (- dir)))
173 (error nil))
174 (setq pos nil)))
175 ;; If found a "matching" paren, see if it is the right
176 ;; kind of paren to match the one we started at.
177 (when (integerp pos)
178 (let ((beg (min pos oldpos)) (end (max pos oldpos)))
179 (unless (eq (syntax-class (syntax-after beg)) 8)
180 (setq mismatch
181 (not (or (eq (char-before end)
182 ;; This can give nil.
183 (cdr (syntax-after beg)))
184 (eq (char-after beg)
185 ;; This can give nil.
186 (cdr (syntax-after (1- end))))
187 ;; The cdr might hold a new paren-class
188 ;; info rather than a matching-char info,
189 ;; in which case the two CDRs should match.
190 (eq (cdr (syntax-after (1- end)))
191 (cdr (syntax-after beg))))))))))))
193 ;; Highlight the other end of the sexp, or unhighlight if none.
194 (if (not pos)
195 (progn
196 ;; If not at a paren that has a match,
197 ;; turn off any previous paren highlighting.
198 (and show-paren-overlay (overlay-buffer show-paren-overlay)
199 (delete-overlay show-paren-overlay))
200 (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
201 (delete-overlay show-paren-overlay-1)))
203 ;; Use the correct face.
204 (if mismatch
205 (progn
206 (if show-paren-ring-bell-on-mismatch
207 (beep))
208 (setq face 'show-paren-mismatch))
209 (setq face 'show-paren-match))
211 ;; If matching backwards, highlight the closeparen
212 ;; before point as well as its matching open.
213 ;; If matching forward, and the openparen is unbalanced,
214 ;; highlight the paren at point to indicate misbalance.
215 ;; Otherwise, turn off any such highlighting.
216 (if (and (not show-paren-highlight-openparen) (= dir 1) (integerp pos))
217 (when (and show-paren-overlay-1
218 (overlay-buffer show-paren-overlay-1))
219 (delete-overlay show-paren-overlay-1))
220 (let ((from (if (= dir 1)
221 (point)
222 (- (point) 1)))
223 (to (if (= dir 1)
224 (+ (point) 1)
225 (point))))
226 (if show-paren-overlay-1
227 (move-overlay show-paren-overlay-1 from to (current-buffer))
228 (setq show-paren-overlay-1 (make-overlay from to nil t)))
229 ;; Always set the overlay face, since it varies.
230 (overlay-put show-paren-overlay-1 'priority show-paren-priority)
231 (overlay-put show-paren-overlay-1 'face face)))
233 ;; Turn on highlighting for the matching paren, if found.
234 ;; If it's an unmatched paren, turn off any such highlighting.
235 (unless (integerp pos)
236 (delete-overlay show-paren-overlay))
237 (let ((to (if (or (eq show-paren-style 'expression)
238 (and (eq show-paren-style 'mixed)
239 (not (pos-visible-in-window-p pos))))
240 (point)
241 pos))
242 (from (if (or (eq show-paren-style 'expression)
243 (and (eq show-paren-style 'mixed)
244 (not (pos-visible-in-window-p pos))))
246 (save-excursion
247 (goto-char pos)
248 (- (point) dir)))))
249 (if show-paren-overlay
250 (move-overlay show-paren-overlay from to (current-buffer))
251 (setq show-paren-overlay (make-overlay from to nil t))))
253 ;; Always set the overlay face, since it varies.
254 (overlay-put show-paren-overlay 'priority show-paren-priority)
255 (overlay-put show-paren-overlay 'face face)))
256 ;; show-paren-mode is nil in this buffer.
257 (and show-paren-overlay
258 (delete-overlay show-paren-overlay))
259 (and show-paren-overlay-1
260 (delete-overlay show-paren-overlay-1))))
262 (provide 'paren)
264 ;;; paren.el ends here