26
|
1 ;;; mic-paren.el --- highlight matching parenthesises.
|
|
2 ;;; Version 1.3.1 - 97-02-27
|
|
3 ;;; Copyright (C) 1997 Mikael Sjödin (mic@docs.uu.se)
|
6
|
4 ;;;
|
24
|
5 ;;; Author: Mikael Sjödin -- mic@docs.uu.se
|
26
|
6 ;;; Additional code by: Vinicius Jose Latorre <vinicius@cpqd.br>
|
|
7 ;;; Steven L Baur <steve@altair.xemacs.org>
|
|
8 ;;;
|
6
|
9 ;;; Keywords: languages, faces
|
|
10 ;;;
|
|
11 ;;; This file is NOT part of GNU Emacs.
|
|
12 ;;; You may however redistribute it and/or modify it under the terms of the GNU
|
|
13 ;;; General Public License as published by the Free Software Foundation; either
|
|
14 ;;; version 2, or (at your option) any later version.
|
|
15 ;;;
|
|
16 ;;; mic-paren 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.
|
|
20
|
|
21 ;;; ----------------------------------------------------------------------
|
|
22 ;;; Short Description:
|
|
23 ;;;
|
|
24 ;;; Load this file and Emacs will display highlighting on whatever
|
|
25 ;;; parenthesis matches the one before or after point. This is an extension to
|
|
26 ;;; the paren.el file distributed with Emacs. The default behaviour is similar
|
|
27 ;;; to paren.el but try the authors favourite options:
|
|
28 ;;; (setq paren-face 'bold)
|
|
29 ;;; (setq paren-sexp-mode t)
|
|
30
|
|
31 ;;; ----------------------------------------------------------------------
|
|
32 ;;; Installation:
|
|
33 ;;;
|
|
34 ;;; o Place this file in a directory in your 'load-path.
|
|
35 ;;; o Put the following in your .emacs file:
|
|
36 ;;; (if window-system
|
|
37 ;;; (require 'mic-paren))
|
|
38 ;;; o Restart your Emacs. mic-paren is now installed and activated!
|
|
39 ;;; o To list the possible customisation enter `C-h f paren-activate'
|
26
|
40 ;;;
|
6
|
41
|
|
42 ;;; ----------------------------------------------------------------------
|
|
43 ;;; Long Description:
|
|
44 ;;;
|
|
45 ;;; mic-paren.el is an extension to the packages paren.el and stig-paren.el for
|
|
46 ;;; Emacs. When mic-paren is active (it is activated when loaded) Emacs normal
|
|
47 ;;; parenthesis matching is deactivated. Instead parenthesis matching will be
|
|
48 ;;; performed as soon as the cursor is positioned at a parenthesis. The
|
|
49 ;;; matching parenthesis (or the entire expression between the parenthesises)
|
|
50 ;;; is highlighted until the cursor is moved away from the parenthesis.
|
|
51 ;;; Features include:
|
12
|
52 ;;; o Both forward and backward parenthesis matching (simultaneously if
|
6
|
53 ;;; cursor is between two expressions).
|
|
54 ;;; o Indication of mismatched parenthesises.
|
12
|
55 ;;; o Recognition of "escaped" parenthesises.
|
6
|
56 ;;; o Option to select if only the matching parenthesis or the entire
|
|
57 ;;; expression should be highlighted.
|
|
58 ;;; o Message describing the match when the matching parenthesis is
|
|
59 ;;; off-screen.
|
|
60 ;;; o Optional delayed highlighting (useful on slow systems),
|
|
61 ;;; o Functions to activate/deactivate mic-paren.el is provided.
|
|
62 ;;; o Numerous options to control the behaviour and appearance of
|
|
63 ;;; mic-paren.el.
|
|
64 ;;;
|
12
|
65 ;;; mic-paren.el is developed and tested under Emacs 19.28 - 19.34. It should
|
|
66 ;;; work on earlier and forthcoming Emacs versions. XEmacs compatibility has
|
26
|
67 ;;; been provided by Steven L Baur <steve@altair.xemacs.org>. Jan Dubois
|
|
68 ;;; (jaduboi@ibm.net) provided help to get mic-paren to work in OS/2.
|
6
|
69 ;;;
|
|
70 ;;; This file can be obtained from http://www.docs.uu.se/~mic/emacs.html
|
|
71
|
26
|
72 ;;; ----------------------------------------------------------------------
|
|
73 ;;; Versions:
|
|
74 ;;;
|
|
75 ;;; v1.3.1 Some spelling corrected (from Vinicius Jose Latorre
|
|
76 ;;; <vinicius@cpqd.br> and Steven L Baur <steve@altair.xemacs.org>)
|
|
77 ;;;
|
|
78 ;;; v1.3 Added code from Vinicius Jose Latorre <vinicius@cpqd.br> to
|
|
79 ;;; highlight unmathced parenthesises (useful in minibuffer)
|
|
80 ;;;
|
|
81 ;;; v1.2.1 Fixed stuff to work with OS/2 emx-emacs
|
|
82 ;;; - checks if x-display-color-p is bound before calling it
|
|
83 ;;; - changed how X/Lucid Emacs is deteced
|
|
84 ;;; Added automatic load of the timer-feature (+ variable to disable
|
|
85 ;;; the loading)
|
|
86
|
6
|
87 ;;; ======================================================================
|
|
88 ;;; User Options:
|
|
89
|
|
90 (defvar paren-priority nil
|
|
91 "*Defines the behaviour of mic-paren when point is between a closing and an
|
|
92 opening parenthesis.
|
|
93
|
|
94 A value of 'close means highlight the parenthesis matching the
|
|
95 close-parenthesis before the point.
|
|
96
|
|
97 A value of 'open means highlight the parenthesis matching the open-parenthesis
|
|
98 after the point.
|
|
99
|
|
100 Any other value means highlight both parenthesis matching the parenthesis
|
|
101 beside the point.")
|
|
102
|
|
103
|
|
104 ;;; ------------------------------
|
|
105
|
|
106 (defvar paren-sexp-mode nil
|
|
107 "*If nil only the matching parenthesis is highlighted.
|
|
108 If non-nil the whole s-expression between the matching parenthesis is
|
|
109 highlighted.")
|
|
110
|
|
111 ;;; ------------------------------
|
|
112
|
|
113 (defvar paren-highlight-at-point t
|
|
114 "*If non-nil and point is after a close parenthesis, both the close and
|
|
115 open parenthesis is highlighted. If nil, only the open parenthesis is
|
|
116 highlighted.")
|
|
117
|
|
118 ;;; ------------------------------
|
|
119
|
|
120 (defvar paren-highlight-offscreen nil
|
|
121 "*If non-nil stig-paren will highlight text which is not visible in the
|
|
122 current buffer.
|
|
123
|
|
124 This is useful if you regularly display the current buffer in multiple windows
|
|
125 or frames. For instance if you use follow-mode (by andersl@csd.uu.se), however
|
|
126 it may slow down your Emacs.
|
|
127
|
|
128 (This variable is ignored (treated as non-nil) if you set paren-sexp-mode to
|
|
129 non-nil.)")
|
|
130
|
|
131 ;;; ------------------------------
|
|
132
|
|
133 (defvar paren-message-offscreen t
|
|
134 "*Display message if matching parenthesis is off-screen.")
|
|
135
|
|
136 ;;; ------------------------------
|
|
137
|
|
138 (defvar paren-message-no-match t
|
|
139 "*Display message if no matching parenthesis is found.")
|
|
140
|
|
141 ;;; ------------------------------
|
|
142
|
|
143 (defvar paren-ding-unmatched nil
|
|
144 "*Make noise if the cursor is at an unmatched parenthesis or no matching
|
|
145 parenthesis is found.
|
|
146
|
|
147 Even if nil, typing an unmatched parenthesis produces a ding.")
|
|
148
|
|
149 ;;; ------------------------------
|
|
150
|
|
151 (defvar paren-delay nil
|
|
152 "*This variable controls when highlighting is done. The variable has
|
|
153 different meaning in different versions of Emacs.
|
|
154
|
|
155 In Emacs 19.29 and below:
|
|
156 This variable is ignored.
|
|
157
|
|
158 In Emacs 19.30:
|
|
159 A value of nil will make highlighting happen immediately (this may slow down
|
|
160 your Emacs if running on a slow system). Any non-nil value will delay
|
|
161 highlighting for the time specified by post-command-idle-delay.
|
|
162
|
|
163 In Emacs 19.31 and above:
|
|
164 A value of nil will make highlighting happen immediately (this may slow down
|
|
165 your Emacs if running on a slow system). If not nil, the value should be a
|
|
166 number (possible a floating point number if your Emacs support floating point
|
|
167 numbers). The number is the delay before mic-paren performs highlighting.
|
|
168
|
|
169 If you change this variable when mic-paren is active you have to re-activate
|
|
170 (with M-x paren-activate) mic-paren for the change to take effect.")
|
|
171
|
|
172
|
|
173 ;;; ------------------------------
|
|
174
|
|
175 (defvar paren-dont-touch-blink nil
|
|
176 "*If non-nil mic-paren will not change the value of blink-matching-paren when
|
|
177 activated of deactivated.
|
|
178
|
|
179 If nil mic-paren turns of blinking when activated and turns on blinking when
|
|
180 deactivated.")
|
|
181
|
|
182 ;;; ------------------------------
|
|
183
|
|
184 (defvar paren-dont-activate-on-load nil
|
|
185 "*If non-nil mic-paren will not activate itself when loaded.")
|
|
186
|
|
187 ;;; ------------------------------
|
|
188
|
26
|
189 (defvar paren-dont-load-timer (not (string-match "XEmacs\\|Lucid"
|
|
190 emacs-version))
|
|
191 "*If non-nil mic-paren will not try to load the timer-feature when loaded.
|
|
192
|
|
193 (I have no idea why Emacs user ever want to set this to non-nil but I hate
|
|
194 packages which loads/activates stuff I don't want to use so I provide this way
|
|
195 to prevent the loading if someone doesn't want timers to be loaded.)")
|
|
196
|
|
197 ;;; ------------------------------
|
|
198
|
|
199 (defvar paren-face (if (and (fboundp 'x-display-color-p)
|
|
200 (x-display-color-p))
|
|
201 'highlight 'underline)
|
6
|
202 "*Face to use for showing the matching parenthesis.")
|
|
203
|
|
204 ;;; ------------------------------
|
|
205
|
26
|
206 (defvar paren-mismatch-face (if (and (fboundp 'x-display-color-p)
|
|
207 (x-display-color-p))
|
6
|
208 (let ((fn 'paren-mismatch-face))
|
|
209 (copy-face 'default fn)
|
|
210 (set-face-background fn "DeepPink")
|
|
211 fn)
|
|
212 'modeline)
|
|
213 "*Face to use when highlighting a mismatched parenthesis.")
|
|
214
|
26
|
215 ;;; ------------------------------
|
|
216
|
|
217 (defvar paren-no-match-face (if (x-display-color-p)
|
|
218 (let ((fn 'paren-no-match-face))
|
|
219 (copy-face 'default fn)
|
|
220 (set-face-background fn "yellow")
|
|
221 fn)
|
|
222 'default)
|
|
223 "*Face to use when highlighting an unmatched parenthesis.")
|
|
224
|
6
|
225 ;;; ======================================================================
|
|
226 ;;; User Functions:
|
|
227
|
26
|
228 ;; XEmacs compatibility (mainly by Steven L Baur <steve@altair.xemacs.org>)
|
6
|
229 (eval-and-compile
|
26
|
230 (if (string-match "\\(Lucid\\|XEmacs\\)" emacs-version)
|
6
|
231 (progn
|
|
232 (fset 'mic-make-overlay 'make-extent)
|
|
233 (fset 'mic-delete-overlay 'delete-extent)
|
|
234 (fset 'mic-overlay-put 'set-extent-property)
|
|
235 (defun mic-cancel-timer (timer) (delete-itimer timer))
|
26
|
236 (fset 'mic-run-with-idle-timer 'start-itimer)
|
6
|
237 )
|
|
238 (fset 'mic-make-overlay 'make-overlay)
|
|
239 (fset 'mic-delete-overlay 'delete-overlay)
|
|
240 (fset 'mic-overlay-put 'overlay-put)
|
|
241 (fset 'mic-cancel-timer 'cancel-timer)
|
|
242 (fset 'mic-run-with-idle-timer 'run-with-idle-timer)
|
|
243 ))
|
|
244
|
|
245
|
|
246 (defun paren-activate ()
|
|
247 "Activates mic-paren parenthesis highlighting.
|
|
248 paren-activate deactivates the paren.el and stig-paren.el packages if they are
|
|
249 active
|
|
250 Options:
|
|
251 paren-priority
|
|
252 paren-sexp-mode
|
|
253 paren-highlight-at-point
|
|
254 paren-highlight-offscreen
|
|
255 paren-message-offscreen
|
|
256 paren-message-no-match
|
|
257 paren-ding-unmatched
|
|
258 paren-delay
|
|
259 paren-dont-touch-blink
|
|
260 paren-dont-activate-on-load
|
|
261 paren-face
|
|
262 paren-mismatch-face"
|
|
263 (interactive)
|
|
264 ;; Deactivate mic-paren.el (To remove redundant hooks)
|
|
265 (paren-deactivate)
|
|
266 ;; Deactivate paren.el if loaded
|
|
267 (if (boundp 'post-command-idle-hook)
|
|
268 (remove-hook 'post-command-idle-hook 'show-paren-command-hook))
|
|
269 (remove-hook 'post-command-hook 'show-paren-command-hook)
|
|
270 (and (boundp 'show-paren-overlay)
|
|
271 show-paren-overlay
|
|
272 (mic-delete-overlay show-paren-overlay))
|
|
273 (and (boundp 'show-paren-overlay-1)
|
|
274 show-paren-overlay-1
|
|
275 (mic-delete-overlay show-paren-overlay-1))
|
|
276 ;; Deactivate stig-paren.el if loaded
|
|
277 (if (boundp 'post-command-idle-hook)
|
|
278 (remove-hook 'post-command-idle-hook 'stig-paren-command-hook))
|
|
279 (remove-hook 'post-command-hook 'stig-paren-command-hook)
|
|
280 (remove-hook 'post-command-hook 'stig-paren-safe-command-hook)
|
|
281 (remove-hook 'pre-command-hook 'stig-paren-delete-overlay)
|
|
282 ;; Deactivate Emacs standard parenthesis blinking
|
|
283 (or paren-dont-touch-blink
|
|
284 (setq blink-matching-paren nil))
|
|
285
|
12
|
286 (cond(
|
6
|
287 ;; If timers are available use them
|
|
288 ;; (Emacs 19.31 and above)
|
12
|
289 (featurep 'timer)
|
|
290 (if (numberp paren-delay)
|
|
291 (setq mic-paren-idle-timer
|
|
292 (mic-run-with-idle-timer paren-delay t
|
|
293 'mic-paren-command-idle-hook))
|
|
294 (add-hook 'post-command-hook 'mic-paren-command-hook)))
|
6
|
295 ;; If the idle hook exists assume it is functioning and use it
|
|
296 ;; (Emacs 19.30)
|
|
297 ((and (boundp 'post-command-idle-hook)
|
|
298 (boundp 'post-command-idle-delay))
|
|
299 (if paren-delay
|
|
300 (add-hook 'post-command-idle-hook 'mic-paren-command-idle-hook)
|
|
301 (add-hook 'post-command-hook 'mic-paren-command-hook)))
|
|
302 ;; Check if we (at least) have a post-comand-hook, and use it
|
|
303 ;; (Emacs 19.29 and below)
|
|
304 ((boundp 'post-command-hook)
|
|
305 (add-hook 'post-command-hook 'mic-paren-command-hook))
|
|
306 ;; Not possible to install mic-paren hooks
|
|
307 (t (error "Cannot activate mic-paren in this Emacs version"))))
|
|
308
|
|
309
|
|
310
|
|
311 (defun paren-deactivate ()
|
|
312 "Deactivates mic-paren parenthesis highlighting"
|
|
313 (interactive)
|
|
314 ;; Deactivate (don't bother to check where/if mic-paren is acivte, just
|
|
315 ;; delete all possible hooks and timers)
|
|
316 (if (boundp 'post-command-idle-hook)
|
|
317 (remove-hook 'post-command-idle-hook 'mic-paren-command-idle-hook))
|
|
318 (if mic-paren-idle-timer
|
|
319 (mic-cancel-timer mic-paren-idle-timer))
|
|
320 (remove-hook 'post-command-hook 'mic-paren-command-hook)
|
|
321
|
|
322 ;; Remove any old highlighs
|
|
323 (mic-delete-overlay mic-paren-backw-overlay)
|
|
324 (mic-delete-overlay mic-paren-point-overlay)
|
|
325 (mic-delete-overlay mic-paren-forw-overlay)
|
|
326
|
|
327 ;; Reactivate Emacs standard parenthesis blinking
|
|
328 (or paren-dont-touch-blink
|
|
329 (setq blink-matching-paren t))
|
|
330 )
|
|
331
|
|
332 ;;; ======================================================================
|
|
333 ;;; Internal variables:
|
|
334
|
|
335 (defvar mic-paren-backw-overlay (mic-make-overlay (point-min) (point-min))
|
|
336 "Overlay for the open-paren which matches the close-paren before
|
|
337 point. When in sexp-mode this is the overlay for the expression before point.")
|
|
338
|
|
339 (defvar mic-paren-point-overlay (mic-make-overlay (point-min) (point-min))
|
|
340 "Overlay for the close-paren before point.
|
|
341 (Not used when is sexp-mode.)")
|
|
342
|
|
343 (defvar mic-paren-forw-overlay (mic-make-overlay (point-min) (point-min))
|
|
344 "Overlay for the close-paren which matches the open-paren after
|
|
345 point. When in sexp-mode this is the overlay for the expression after point.")
|
|
346
|
|
347 (defvar mic-paren-idle-timer nil
|
|
348 "Idle-timer. Used only in Emacs 19.31 and above (and if paren-delay is nil)")
|
|
349
|
|
350
|
|
351
|
|
352
|
|
353 ;;; ======================================================================
|
|
354 ;;; Internal function:
|
|
355
|
|
356
|
|
357
|
|
358 (defun mic-paren-command-hook ()
|
|
359 (or executing-kbd-macro
|
|
360 (input-pending-p) ;[This might cause trouble since the
|
|
361 ; function is unreliable]
|
|
362 (condition-case paren-error
|
24
|
363 (mic-paren-highlight)
|
6
|
364 (error
|
|
365 (if (not (window-minibuffer-p (selected-window)))
|
|
366 (message "mic-paren catched error (please report): %s"
|
|
367 paren-error))))))
|
|
368
|
|
369 (defun mic-paren-command-idle-hook ()
|
|
370 (condition-case paren-error
|
24
|
371 (mic-paren-highlight)
|
6
|
372 (error
|
|
373 (if (not (window-minibuffer-p (selected-window)))
|
|
374 (message "mic-paren catched error (please report): %s"
|
|
375 paren-error)))))
|
|
376
|
|
377
|
24
|
378 (defun mic-paren-highlight ()
|
6
|
379 "The main-function of mic-paren. Does all highlighting, dinging, messages,
|
|
380 cleaning-up."
|
|
381 ;; Remove any old highlighting
|
|
382 (mic-delete-overlay mic-paren-forw-overlay)
|
|
383 (mic-delete-overlay mic-paren-point-overlay)
|
|
384 (mic-delete-overlay mic-paren-backw-overlay)
|
|
385
|
|
386 ;; Handle backward highlighting (when after a close-paren):
|
|
387 ;; If positioned after a close-paren, and
|
|
388 ;; not before an open-paren when priority=open, and
|
|
389 ;; the close-paren is not escaped then
|
|
390 ;; perform highlighting
|
|
391 ;; else
|
|
392 ;; remove any old backward highlights
|
|
393 (if (and (eq (char-syntax (preceding-char)) ?\))
|
|
394 (not (and (eq (char-syntax (following-char)) ?\()
|
|
395 (eq paren-priority 'open)))
|
|
396 (paren-evenp (paren-backslashes-before-char (1- (point)))))
|
|
397 (let (open)
|
|
398 ;; Find the position for the open-paren
|
|
399 (save-excursion
|
|
400 (save-restriction
|
|
401 (if blink-matching-paren-distance
|
|
402 (narrow-to-region
|
|
403 (max (point-min)
|
|
404 (- (point) blink-matching-paren-distance))
|
|
405 (point-max)))
|
|
406 (condition-case ()
|
|
407 (setq open (scan-sexps (point) -1))
|
|
408 (error nil))))
|
|
409
|
|
410 ;; If match found
|
26
|
411 ;; highlight expression and/or print messages
|
6
|
412 ;; else
|
26
|
413 ;; highlight unmatched paren
|
6
|
414 ;; print no-match message
|
|
415 (if open
|
|
416 (let ((mismatch (/= (matching-paren (preceding-char))
|
|
417 (char-after open)))
|
|
418 (visible (pos-visible-in-window-p open)))
|
|
419 ;; If highlight is appropriate
|
24
|
420 ;; highlight
|
6
|
421 ;; else
|
|
422 ;; remove any old highlight
|
|
423 (if (or visible paren-highlight-offscreen paren-sexp-mode)
|
|
424 ;; If sexp-mode
|
|
425 ;; highlight sexp
|
|
426 ;; else
|
|
427 ;; highlight the two parens
|
|
428 (if paren-sexp-mode
|
|
429 (progn
|
|
430 (setq mic-paren-backw-overlay
|
|
431 (mic-make-overlay open (point)))
|
|
432 (if mismatch
|
|
433 (mic-overlay-put mic-paren-backw-overlay
|
|
434 'face paren-mismatch-face)
|
|
435 (mic-overlay-put mic-paren-backw-overlay
|
|
436 'face paren-face)))
|
|
437 (setq mic-paren-backw-overlay
|
|
438 (mic-make-overlay open (1+ open)))
|
|
439 (and paren-highlight-at-point
|
|
440 (setq mic-paren-point-overlay
|
|
441 (mic-make-overlay (1- (point)) (point))))
|
|
442 (if mismatch
|
|
443 (progn
|
|
444 (mic-overlay-put mic-paren-backw-overlay
|
|
445 'face paren-mismatch-face)
|
|
446 (and paren-highlight-at-point
|
|
447 (mic-overlay-put mic-paren-point-overlay
|
|
448 'face paren-mismatch-face)))
|
|
449 (mic-overlay-put mic-paren-backw-overlay
|
|
450 'face paren-face)
|
|
451 (and paren-highlight-at-point
|
|
452 (mic-overlay-put mic-paren-point-overlay
|
|
453 'face paren-face)))))
|
|
454 ;; Print messages if match is offscreen
|
|
455 (and paren-message-offscreen
|
|
456 (not visible)
|
|
457 (not (window-minibuffer-p (selected-window)))
|
|
458 (message "%s %s"
|
|
459 (if mismatch "MISMATCH:" "Matches")
|
|
460 (mic-paren-get-matching-open-text open)))
|
|
461 ;; Ding if mismatch
|
|
462 (and mismatch
|
|
463 paren-ding-unmatched
|
|
464 (ding)))
|
26
|
465 (setq mic-paren-backw-overlay
|
|
466 (mic-make-overlay (1- (point)) (point)))
|
|
467 (mic-overlay-put mic-paren-backw-overlay
|
|
468 'face paren-no-match-face)
|
6
|
469 (and paren-message-no-match
|
|
470 (not (window-minibuffer-p (selected-window)))
|
|
471 (message "No opening parenthesis found"))
|
|
472 (and paren-message-no-match
|
|
473 paren-ding-unmatched
|
|
474 (ding)))))
|
|
475
|
|
476 ;; Handle forward highlighting (when before an open-paren):
|
|
477 ;; If positioned before an open-paren, and
|
|
478 ;; not after a close-paren when priority=close, and
|
|
479 ;; the open-paren is not escaped then
|
|
480 ;; perform highlighting
|
|
481 ;; else
|
|
482 ;; remove any old forward highlights
|
|
483 (if (and (eq (char-syntax (following-char)) ?\()
|
|
484 (not (and (eq (char-syntax (preceding-char)) ?\))
|
|
485 (eq paren-priority 'close)))
|
|
486 (paren-evenp (paren-backslashes-before-char (point))))
|
|
487 (let (close)
|
|
488 ;; Find the position for the close-paren
|
|
489 (save-excursion
|
|
490 (save-restriction
|
|
491 (if blink-matching-paren-distance
|
|
492 (narrow-to-region
|
|
493 (point-min)
|
|
494 (min (point-max)
|
|
495 (+ (point) blink-matching-paren-distance))))
|
|
496 (condition-case ()
|
|
497 (setq close (scan-sexps (point) 1))
|
|
498 (error nil))))
|
|
499 ;; If match found
|
26
|
500 ;; highlight expression and/or print messages
|
6
|
501 ;; else
|
26
|
502 ;; highlight unmatched paren
|
6
|
503 ;; print no-match message
|
|
504 (if close
|
|
505 (let ((mismatch (/= (matching-paren (following-char))
|
|
506 (char-after (1- close))))
|
|
507 (visible (pos-visible-in-window-p close)))
|
|
508 ;; If highlight is appropriate
|
24
|
509 ;; highlight
|
6
|
510 ;; else
|
|
511 ;; remove any old highlight
|
|
512 (if (or visible paren-highlight-offscreen paren-sexp-mode)
|
|
513 ;; If sexp-mode
|
|
514 ;; highlight sexp
|
|
515 ;; else
|
|
516 ;; highlight the two parens
|
|
517 (if paren-sexp-mode
|
|
518 (progn
|
|
519 (setq mic-paren-forw-overlay
|
|
520 (mic-make-overlay (point) close))
|
|
521 (if mismatch
|
|
522 (mic-overlay-put mic-paren-forw-overlay
|
|
523 'face paren-mismatch-face)
|
|
524 (mic-overlay-put mic-paren-forw-overlay
|
|
525 'face paren-face)))
|
|
526 (setq mic-paren-forw-overlay
|
|
527 (mic-make-overlay (1- close) close))
|
|
528 (if mismatch
|
|
529 (mic-overlay-put mic-paren-forw-overlay
|
|
530 'face paren-mismatch-face)
|
|
531 (mic-overlay-put mic-paren-forw-overlay
|
|
532 'face paren-face))))
|
|
533
|
|
534 ;; Print messages if match is offscreen
|
|
535 (and paren-message-offscreen
|
|
536 (not visible)
|
|
537 (not (window-minibuffer-p (selected-window)))
|
|
538 (message "%s %s"
|
|
539 (if mismatch "MISMATCH:" "Matches")
|
|
540 (mic-paren-get-matching-close-text close)))
|
|
541 ;; Ding if mismatch
|
|
542 (and mismatch
|
|
543 paren-ding-unmatched
|
|
544 (ding)))
|
26
|
545 (setq mic-paren-forw-overlay
|
|
546 (mic-make-overlay (point) (1+ (point))))
|
|
547 (mic-overlay-put mic-paren-forw-overlay
|
|
548 'face paren-no-match-face)
|
6
|
549 (and paren-message-no-match
|
|
550 (not (window-minibuffer-p (selected-window)))
|
|
551 (message "No closing parenthesis found"))
|
|
552 (and paren-message-no-match
|
|
553 paren-ding-unmatched
|
|
554 (ding))))))
|
|
555
|
|
556 ;;; --------------------------------------------------
|
|
557
|
|
558 (defun mic-paren-get-matching-open-text (open)
|
|
559 "Returns a string with the context around OPEN-paren."
|
|
560 ;; If there's stuff on this line preceding the paren, then display text from
|
|
561 ;; beginning of line to paren.
|
|
562 ;;
|
|
563 ;; If, however, the paren is at the beginning of a line, then skip whitespace
|
|
564 ;; forward and display text from paren to end of the next line containing
|
|
565 ;; non-space text.
|
|
566 ;;
|
|
567 ;; (Same as in stig-paren.el)
|
|
568 (save-excursion
|
|
569 (goto-char open)
|
|
570 (if (save-excursion
|
|
571 (skip-chars-backward " \t")
|
|
572 (not (bolp)))
|
|
573 (progn
|
|
574 (beginning-of-line)
|
|
575 (concat (buffer-substring (point) (1+ open)) "..."))
|
|
576 (forward-char 1) ;From the beginning-of-line
|
|
577 (skip-chars-forward "\n \t")
|
|
578 (end-of-line)
|
|
579 (buffer-substring open (point)))))
|
|
580
|
|
581
|
|
582 (defun mic-paren-get-matching-close-text (close)
|
|
583 "Returns a string with the context around CLOSE-paren."
|
|
584 ;; The whole line up until the close-paren with "..." appended if there are
|
|
585 ;; more text after the close-paren
|
|
586 (save-excursion
|
|
587 (goto-char close)
|
|
588 (beginning-of-line)
|
|
589 (concat
|
|
590 (buffer-substring (point) close)
|
|
591 (progn
|
|
592 (goto-char close)
|
|
593 (if (looking-at "[ \t]*$")
|
|
594 ""
|
|
595 "...")))))
|
|
596
|
|
597
|
|
598 (defun paren-evenp (number)
|
|
599 "Returns t if NUMBER is an even number, nil otherwise"
|
|
600 (eq 0 (% number 2)))
|
|
601
|
|
602 (defun paren-backslashes-before-char (pnt)
|
|
603 (setq pnt (1- pnt))
|
|
604 (let ((n 0))
|
|
605 (while (and (>= pnt (point-min))
|
|
606 (eq (char-syntax (char-after pnt)) ?\\))
|
|
607 (setq n (1+ n))
|
|
608 (setq pnt (1- pnt)))
|
|
609 n))
|
|
610
|
|
611
|
|
612
|
|
613 ;;; ======================================================================
|
|
614 ;;; Initialisation when loading:
|
|
615
|
26
|
616 ;;; Try to load the timer feature if its not already loaded
|
|
617 (or paren-dont-load-timer
|
|
618 (featurep 'timer)
|
|
619 (condition-case ()
|
|
620 (require 'timer)
|
|
621 (error nil)))
|
|
622
|
6
|
623
|
|
624 (or paren-dont-activate-on-load
|
|
625 (paren-activate))
|
|
626
|
|
627 ;;; This is in case mic-paren.el is preloaded. [Does this work? /Mic]
|
|
628 (add-hook 'window-setup-hook
|
|
629 (function (lambda ()
|
|
630 (and window-system
|
|
631 (not paren-dont-activate-on-load)
|
|
632 (paren-activate)))))
|
|
633
|
|
634 (provide 'mic-paren)
|
|
635 (provide 'paren)
|