-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpjb-blink.el
137 lines (130 loc) · 6.24 KB
/
pjb-blink.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
;;;; -*- mode:emacs-lisp;coding:utf-8 -*-
;;;;****************************************************************************
;;;;FILE: pjb-blink.el
;;;;LANGUAGE: emacs lisp
;;;;SYSTEM: POSIX
;;;;USER-INTERFACE: NONE
;;;;DESCRIPTION
;;;;
;;;; Alternate blinking parens.
;;;;
;;;;AUTHORS
;;;; <PJB> Pascal Bourguignon <[email protected]>
;;;;MODIFICATIONS
;;;; 2005-09-20 <PJB> Created.
;;;;BUGS
;;;;LEGAL
;;;; GPL
;;;;
;;;; Copyright Pascal Bourguignon 2005 - 2011
;;;;
;;;; This program is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU General Public License
;;;; as published by the Free Software Foundation; either version
;;;; 2 of the License, or (at your option) any later version.
;;;;
;;;; This program is distributed in the hope that it will be
;;;; useful, but WITHOUT ANY WARRANTY; without even the implied
;;;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;;;; PURPOSE. See the GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public
;;;; License along with this program; if not, write to the Free
;;;; Software Foundation, Inc., 59 Temple Place, Suite 330,
;;;; Boston, MA 02111-1307 USA
;;;;****************************************************************************
(defun blink (start end)
(set-mark start)
(goto-char (1+ end)))
(defun unblink (start end)
"Nothing to do")
;; Another implementation of blink and unblink could set the face,
;; or put some overlay on the region or on the start and end characters.
(defun blink-matching-open-and-close ()
"Move cursor momentarily to the beginning of the sexp before point."
(interactive)
(let ((close-point (1- (point))))
(and (> (point) (1+ (point-min)))
blink-matching-paren
;; Verify an even number of quoting characters precede the close.
(= 1 (logand 1 (- (point)
(save-excursion
(forward-char -1)
(skip-syntax-backward "/\\")
(point)))))
(let* ((oldpos (point))
(blinkpos)
(mismatch)
matching-paren)
(save-excursion
(save-restriction
(if blink-matching-paren-distance
(narrow-to-region (max (point-min)
(- (point) blink-matching-paren-distance))
oldpos))
(condition-case ()
(let ((parse-sexp-ignore-comments
(and parse-sexp-ignore-comments
(not blink-matching-paren-dont-ignore-comments))))
(setq blinkpos (scan-sexps oldpos -1)))
(error nil)))
(and blinkpos
;; Not syntax '$'.
(not (eq (syntax-class (syntax-after blinkpos)) 8))
(setq matching-paren
(let ((syntax (syntax-after blinkpos)))
(and (consp syntax)
(eq (syntax-class syntax) 4)
(cdr syntax)))
mismatch
(or (null matching-paren)
(/= (char-after (1- oldpos))
matching-paren))))
(if mismatch (setq blinkpos nil))
(if blinkpos
;; Don't log messages about paren matching.
(let (message-log-max)
(blink blinkpos close-point)
(if (pos-visible-in-window-p)
(and blink-matching-paren-on-screen
(sit-for blink-matching-delay))
(goto-char blinkpos)
(message
"Matches %s"
;; Show what precedes the open in its line, if anything.
(if (save-excursion
(skip-chars-backward " \t")
(not (bolp)))
(buffer-substring (progn (beginning-of-line) (point))
(1+ blinkpos))
;; Show what follows the open in its line, if anything.
(if (save-excursion
(forward-char 1)
(skip-chars-forward " \t")
(not (eolp)))
(buffer-substring blinkpos
(progn (end-of-line) (point)))
;; Otherwise show the previous nonblank line,
;; if there is one.
(if (save-excursion
(skip-chars-backward "\n \t")
(not (bobp)))
(concat
(buffer-substring (progn
(skip-chars-backward "\n \t")
(beginning-of-line)
(point))
(progn (end-of-line)
(skip-chars-backward " \t")
(point)))
;; Replace the newline and other whitespace with `...'.
"..."
(buffer-substring blinkpos (1+ blinkpos)))
;; There is nothing to show except the char itself.
(buffer-substring blinkpos (1+ blinkpos)))))))
(unblink blinkpos close-point))
(cond (mismatch
(message "Mismatched parentheses"))
((not blink-matching-paren-distance)
(message "Unmatched parenthesis")))))))))
(setf blink-paren-function (function blink-matching-open-and-close))