1 ;;; highlight-parentheses.el --- highlight surrounding parentheses
3 ;; Copyright (C) 2007, 2009 Nikolaj Schumacher
5 ;; Author: Nikolaj Schumacher <bugs * nschum de>
7 ;; Keywords: faces, matching
8 ;; URL: http://nschum.de/src/emacs/highlight-parentheses/
9 ;; Compatibility: GNU Emacs 22.x, GNU Emacs 23.x
11 ;; This file is NOT part of GNU Emacs.
13 ;; This program is free software; you can redistribute it and/or
14 ;; modify it under the terms of the GNU General Public License
15 ;; as published by the Free Software Foundation; either version 2
16 ;; of the License, or (at your option) any later version.
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
28 ;; Add the following to your .emacs file:
29 ;; (require 'highlight-parentheses)
31 ;; Enable `highlight-parentheses-mode'.
36 ;; Added setter for color variables.
39 ;; Added background highlighting and faces.
42 ;; Support for defcustom.
49 (eval-when-compile (require 'cl
))
51 (defgroup highlight-parentheses nil
52 "Highlight surrounding parentheses"
56 (defun hl-paren-set (variable value
)
58 (when (fboundp 'hl-paren-color-update
)
59 (hl-paren-color-update)))
61 (defcustom hl-paren-colors
62 '("firebrick1" "IndianRed1" "IndianRed3" "IndianRed4")
63 "*List of colors for the highlighted parentheses.
64 The list starts with the the inside parentheses and moves outwards."
67 :group
'highlight-parentheses
)
69 (defcustom hl-paren-background-colors nil
70 "*List of colors for the background highlighted parentheses.
71 The list starts with the the inside parentheses and moves outwards."
74 :group
'highlight-parentheses
)
76 (defface hl-paren-face nil
77 "*Face used for highlighting parentheses.
78 Color attributes might be overriden by `hl-paren-colors' and
79 `hl-paren-background-colors'."
80 :group
'highlight-parentheses
)
82 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
84 (defvar hl-paren-overlays nil
85 "This buffers currently active overlays.")
86 (make-variable-buffer-local 'hl-paren-overlays
)
88 (defvar hl-paren-last-point
0
89 "The last point for which parentheses were highlighted.
90 This is used to prevent analyzing the same context over and over.")
91 (make-variable-buffer-local 'hl-paren-last-point
)
93 (defun hl-paren-highlight ()
94 "Highlight the parentheses around point."
95 (unless (= (point) hl-paren-last-point
)
96 (setq hl-paren-last-point
(point))
97 (let ((overlays hl-paren-overlays
)
102 (while (and (setq pos1
(cadr (syntax-ppss pos1
)))
104 (move-overlay (pop overlays
) pos1
(1+ pos1
))
105 (when (setq pos2
(scan-sexps pos1
1))
106 (move-overlay (pop overlays
) (1- pos2
) pos2
)
110 (dolist (ov overlays
)
111 (move-overlay ov
1 1)))))
114 (define-minor-mode highlight-parentheses-mode
115 "Minor mode to highlight the surrounding parentheses."
117 (if highlight-parentheses-mode
119 (hl-paren-create-overlays)
120 (add-hook 'post-command-hook
'hl-paren-highlight nil t
))
121 (mapc 'delete-overlay hl-paren-overlays
)
122 (kill-local-variable 'hl-paren-overlays
)
123 (kill-local-variable 'hl-paren-point
)
124 (remove-hook 'post-command-hook
'hl-paren-highlight t
)))
126 ;;; overlays ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
128 (defun hl-paren-create-overlays ()
129 (let ((fg hl-paren-colors
)
130 (bg hl-paren-background-colors
)
133 (setq attributes
(face-attr-construct 'hl-paren-face
))
135 (setq attributes
(plist-put attributes
:foreground
(car fg
))))
138 (setq attributes
(plist-put attributes
:background
(car bg
))))
140 (dotimes (i 2) ;; front and back
141 (push (make-overlay 0 0) hl-paren-overlays
)
142 (overlay-put (car hl-paren-overlays
) 'face attributes
)))
143 (setq hl-paren-overlays
(nreverse hl-paren-overlays
))))
145 (defun hl-paren-color-update ()
146 (dolist (buffer (buffer-list))
147 (with-current-buffer buffer
148 (when hl-paren-overlays
149 (mapc 'delete-overlay hl-paren-overlays
)
150 (setq hl-paren-overlays nil
)
151 (hl-paren-create-overlays)
152 (let ((hl-paren-last-point -
1)) ;; force update
153 (hl-paren-highlight))))))
155 (provide 'highlight-parentheses
)
157 ;;; highlight-parentheses.el ends here