annotate lisp/packages/blink-paren.el @ 189:489f57a838ef r20-3b21

Import from CVS: tag r20-3b21
author cvs
date Mon, 13 Aug 2007 09:57:07 +0200
parents 0293115a14e9
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; blink-paren.el --- blink the matching paren, just like Zmacs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 ;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 ;; Author: devin@lucid.com.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; Keywords: faces
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;; XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;; under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;; XEmacs is distributed in the hope that it will be useful, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;; You should have received a copy of the GNU General Public License
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 0
diff changeset
20 ;; along with XEmacs; see the file COPYING. If not, write to the
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 0
diff changeset
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 0
diff changeset
22 ;; Boston, MA 02111-1307, USA.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 ;;; Synched up with: Not in FSF.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 (defvar blink-paren-timeout 0.2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 "*If the cursor is on a parenthesis, the matching parenthesis will blink.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 This variable controls how long each phase of the blink lasts in seconds.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 This should be a fractional part of a second (a float.)")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 (defvar highlight-paren-expression nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 "*If true, highlight the whole expression of the paren under the cursor
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 instead of blinking (or highlighting) the matching paren. This will highlight
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 the expression using the `highlight-expression' face.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;;; The blinking paren alternates between the faces blink-paren-on and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;;; blink-paren-off. The default is for -on to look just like default
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;;; text, and -off to be invisible. You can change this so that, for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;;; example, the blinking paren fluctuates between bold and italic...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;;; You can make the matching paren merely be highlighted (and not blink)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 ;;; by setting the blink-paren-on and blink-paren-off faces to have the same
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 ;;; attributes; if you do this, then emacs will not consume as much CPU.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 ;;; If highlight-paren-expression is true, then the whole sexp between the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 ;;; parens will be displayed in the `highlight-expression' face instead.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 (make-face 'blink-paren-on)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 (make-face 'blink-paren-off)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 (make-face 'highlight-expression)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 ;; extent used to change the face of the matching paren
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 (defvar blink-paren-extent nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 ;; timeout to blink the face
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 (defvar blink-paren-timeout-id nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 ;; find if we should look foward or backward to find the matching paren
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 (defun blink-paren-sexp-dir ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 (cond ((and (< (point) (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 (eq (char-syntax (char-after (point))) ?\())
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 ((and (> (point) (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 (eq (char-syntax (char-after (- (point) 1))) ?\)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 -1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 (t ())))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 ;; make an extent on the matching paren if any. return it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 (defun blink-paren-make-extent ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 (let ((dir (blink-paren-sexp-dir)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (and dir
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (condition-case ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 (let* ((parse-sexp-ignore-comments t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (other-pos (let ((pmin (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 (pmax (point-max))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 (point (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 (unwind-protect
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (narrow-to-region
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (max pmin (- point blink-matching-paren-distance))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 (min pmax (+ point blink-matching-paren-distance)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 (forward-sexp dir) (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 (narrow-to-region pmin pmax)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 (goto-char point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 (extent (if (= dir 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 (make-extent (if highlight-paren-expression
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 (- other-pos 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 other-pos)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (make-extent other-pos
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (if highlight-paren-expression
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (+ other-pos 1))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (set-extent-face extent (if highlight-paren-expression
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 'highlight-expression
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 'blink-paren-on))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 extent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (error nil)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 ;; callback for the timeout
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 ;; swap the face of the extent on the matching paren
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (defun blink-paren-timeout (arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 ;; The extent could have been deleted for some reason and not point to a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 ;; buffer anymore. So catch any error to remove the timeout.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (condition-case ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (set-extent-face blink-paren-extent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (if (eq (extent-face blink-paren-extent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 'blink-paren-on)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 'blink-paren-off
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 'blink-paren-on))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (error (blink-paren-pre-command))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 ;; called after each command is executed in the post-command-hook
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 ;; add the extent and the time-out if we are on a paren.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (defun blink-paren-post-command ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 (blink-paren-pre-command)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 (if (and (setq blink-paren-extent (blink-paren-make-extent))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (not highlight-paren-expression)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (not (and (face-equal 'blink-paren-on 'blink-paren-off)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (set-extent-face blink-paren-extent 'blink-paren-on)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 (or (floatp blink-paren-timeout)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 (integerp blink-paren-timeout)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 (setq blink-paren-timeout-id
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (add-timeout blink-paren-timeout 'blink-paren-timeout ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 blink-paren-timeout))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 ;; called before a new command is executed in the pre-command-hook
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 ;; cleanup by removing the extent and the time-out
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 (defun blink-paren-pre-command ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 (condition-case c ; don't ever signal an error in pre-command-hook!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 (let ((inhibit-quit t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 (if blink-paren-timeout-id
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (disable-timeout (prog1 blink-paren-timeout-id
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (setq blink-paren-timeout-id nil))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (if blink-paren-extent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 (delete-extent (prog1 blink-paren-extent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 (setq blink-paren-extent nil)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 (message "blink paren error! %s" c))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (defun blink-paren (&optional arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 "Toggles paren blinking on and off.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 With a positive argument, turns it on.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 With a non-positive argument, turns it off."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (interactive "P")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 (let* ((was-on (not (not (memq 'blink-paren-pre-command pre-command-hook))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 (on-p (if (null arg)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 (not was-on)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 (> (prefix-numeric-value arg) 0))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (cond (on-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 ;; in case blink paren was dumped, this needs to be setup
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 (or (face-differs-from-default-p 'blink-paren-off)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (set-face-background 'blink-paren-off (face-background 'default))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 (set-face-foreground 'blink-paren-off (face-background 'default))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 (or (face-differs-from-default-p 'highlight-expression)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 (set-face-underline-p 'highlight-expression t))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 (add-hook 'pre-command-hook 'blink-paren-pre-command)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 (add-hook 'post-command-hook 'blink-paren-post-command)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 (setq blink-matching-paren nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 (remove-hook 'pre-command-hook 'blink-paren-pre-command)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 (remove-hook 'post-command-hook 'blink-paren-post-command)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 (and blink-paren-extent (detach-extent blink-paren-extent))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 (setq blink-matching-paren t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 on-p))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (defun blink-paren-init ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 "obsolete - use `blink-paren' instead."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 (blink-paren 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 (provide 'blink-paren)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 (blink-paren 1)