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