Source

edit-utils / mic-paren.el

Full commit
  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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
;;; mic-paren.el --- highlight matching parenthesises.
;;; Version 1.3.1 - 97-02-27
;;; Copyright (C) 1997 Mikael Sjödin (mic@docs.uu.se)
;;;
;;; Author: Mikael Sjödin  --  mic@docs.uu.se
;;; Additional code by: Vinicius Jose Latorre <vinicius@cpqd.br>
;;;                     Steven L Baur <steve@xemacs.org>
;;;                      
;;; Keywords: languages, faces
;;;
;;; This file is NOT part of GNU Emacs.
;;; You may however 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, or (at your option) any later version.
;;;
;;; mic-paren 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.

;;; ----------------------------------------------------------------------
;;; Short Description:
;;;
;;; Load this file and Emacs will display highlighting on whatever
;;; parenthesis matches the one before or after point.  This is an extension to
;;; the paren.el file distributed with Emacs.  The default behaviour is similar
;;; to paren.el but try the authors favourite options:
;;;   (setq paren-face 'bold)
;;;   (setq paren-sexp-mode t)

;;; ----------------------------------------------------------------------
;;; Installation:
;;;
;;; o Place this file in a directory in your 'load-path.
;;; o Put the following in your .emacs file:
;;;     (if window-system
;;;         (require 'mic-paren))
;;; o Restart your Emacs. mic-paren is now installed and activated!
;;; o To list the possible customisation enter `C-h f paren-activate'
;;;

;;; ----------------------------------------------------------------------
;;; Long Description:
;;;
;;; mic-paren.el is an extension to the packages paren.el and stig-paren.el for
;;; Emacs.  When mic-paren is active (it is activated when loaded) Emacs normal
;;; parenthesis matching is deactivated.  Instead parenthesis matching will be
;;; performed as soon as the cursor is positioned at a parenthesis.  The
;;; matching parenthesis (or the entire expression between the parenthesises)
;;; is highlighted until the cursor is moved away from the parenthesis.
;;; Features include:
;;; o Both forward and backward parenthesis matching (simultaneously if
;;;   cursor is between two expressions).
;;; o Indication of mismatched parenthesises.
;;; o Recognition of "escaped" parenthesises.
;;; o Option to select if only the matching parenthesis or the entire
;;;   expression should be highlighted.
;;; o Message describing the match when the matching parenthesis is
;;;   off-screen. 
;;; o Optional delayed highlighting (useful on slow systems), 
;;; o Functions to activate/deactivate mic-paren.el is provided.
;;; o Numerous options to control the behaviour and appearance of
;;;   mic-paren.el. 
;;;
;;; mic-paren.el is developed and tested under Emacs 19.28 - 19.34.  It should
;;; work on earlier and forthcoming Emacs versions.  XEmacs compatibility has
;;; been provided by Steven L Baur <steve@xemacs.org>.  Jan Dubois
;;; (jaduboi@ibm.net) provided help to get mic-paren to work in OS/2.
;;;
;;; This file can be obtained from http://www.docs.uu.se/~mic/emacs.html

;;; ----------------------------------------------------------------------
;;; Versions:
;;;
;;; v1.3.1  Some spelling corrected (from Vinicius Jose Latorre
;;;	    <vinicius@cpqd.br> and Steven L Baur <steve@xemacs.org>)
;;;
;;; v1.3    Added code from Vinicius Jose Latorre <vinicius@cpqd.br> to
;;;	    highlight unmathced parenthesises (useful in minibuffer)
;;;
;;; v1.2.1  Fixed stuff to work with OS/2 emx-emacs
;;;           - checks if x-display-color-p is bound before calling it
;;;           - changed how X/Lucid Emacs is deteced
;;;         Added automatic load of the timer-feature (+ variable to disable
;;;         the loading) 

;;; ======================================================================
;;; User Options:

(defvar paren-priority nil
  "*Defines the behaviour of mic-paren when point is between a closing and an
  opening parenthesis.

A value of 'close means highlight the parenthesis matching the
close-parenthesis before the point.

A value of 'open means highlight the parenthesis matching the open-parenthesis
after the point.

Any other value means highlight both parenthesis matching the parenthesis
beside the point.")


;;; ------------------------------

(defvar paren-sexp-mode nil
  "*If nil only the matching parenthesis is highlighted.
If non-nil the whole s-expression between the matching parenthesis is
highlighted.")

;;; ------------------------------

(defvar paren-highlight-at-point t
  "*If non-nil and point is after a close parenthesis, both the close and
open parenthesis is highlighted. If nil, only the open parenthesis is
highlighted.")

;;; ------------------------------

(defvar paren-highlight-offscreen nil
  "*If non-nil stig-paren will highlight text which is not visible in the
current buffer.  

This is useful if you regularly display the current buffer in multiple windows
or frames. For instance if you use follow-mode (by andersl@csd.uu.se), however
it may slow down your Emacs.

(This variable is ignored (treated as non-nil) if you set paren-sexp-mode to
non-nil.)")

;;; ------------------------------

(defvar paren-message-offscreen t
  "*Display message if matching parenthesis is off-screen.")

;;; ------------------------------

(defvar paren-message-no-match t
  "*Display message if no matching parenthesis is found.")

;;; ------------------------------

(defvar paren-ding-unmatched nil
  "*Make noise if the cursor is at an unmatched parenthesis or no matching
parenthesis is found.

Even if nil, typing an unmatched parenthesis produces a ding.")

;;; ------------------------------

(defvar paren-delay nil
  "*This variable controls when highlighting is done.  The variable has
different meaning in different versions of Emacs.

In Emacs 19.29 and below: 
  This variable is ignored.

In Emacs 19.30:
  A value of nil will make highlighting happen immediately (this may slow down
  your Emacs if running on a slow system).  Any non-nil value will delay
  highlighting for the time specified by post-command-idle-delay.  

In Emacs 19.31 and above:
  A value of nil will make highlighting happen immediately (this may slow down
  your Emacs if running on a slow system).  If not nil, the value should be a
  number (possible a floating point number if your Emacs support floating point
  numbers).  The number is the delay before mic-paren performs highlighting.

If you change this variable when mic-paren is active you have to re-activate
(with M-x paren-activate) mic-paren for the change to take effect.")


;;; ------------------------------

(defvar paren-dont-touch-blink nil
  "*If non-nil mic-paren will not change the value of blink-matching-paren when
activated of deactivated.

If nil mic-paren turns of blinking when activated and turns on blinking when
deactivated.")

;;; ------------------------------

(defvar paren-dont-activate-on-load t
 "*If non-nil mic-paren will not activate itself when loaded.")

;;; ------------------------------

(defvar paren-dont-load-timer (not (string-match "XEmacs\\|Lucid"
						 emacs-version))
  "*If non-nil mic-paren will not try to load the timer-feature when loaded.

(I have no idea why Emacs user ever want to set this to non-nil but I hate
packages which loads/activates stuff I don't want to use so I provide this way
to prevent the loading if someone doesn't want timers to be loaded.)")

;;; ------------------------------

(defgroup mic-paren nil
  "Highlighting of (non)matching parentheses and expressions."
  :group 'matching)

(defface paren-face '((((class color)
			(background light))
		       (:background "lightgoldenrod"))
		      (((class color)
			(background dark))
		       (:background "darkolivegreen"))
		      (t
		       (:underline t)))
  "*Face to use for showing the matching parenthesis."
  :group 'mic-paren)

(defvar paren-face 'paren-face
  "Don't even think of using this.
The variable is obsolete, use the `paren-face' face instead.")

(defface paren-mismatch-face '((((class color)
				 (background light))
				(:background "DeepPink"))
			       (((class color)
				 (background dark))
				(:background "DeepPink3"))
			       (t
				(:inverse-video t)))
  "*Face to use when highlighting a mismatched parenthesis."
  :group 'mic-paren)

(defvar paren-mismatch-face 'paren-mismatch-face
  "Don't even think of using this.
The variable is obsolete, use the `paren-mismatch-face' face instead.")

(defface paren-no-match-face '((((class color)
				 (background light))
				(:background "yellow"))
			       (((class color)
				 (background dark))
				(:background "yellow4"))
			       (t nil))
  "*Face to use when highlighting an unmatched parenthesis."
  :group 'mic-paren)

(defvar paren-no-match-face 'paren-no-match-face
  "Don't even think of using this.
The variable is obsolete, use the `paren-no-match-face' face instead.")

;;; ======================================================================
;;; User Functions:

;; XEmacs compatibility (mainly by Steven L Baur <steve@xemacs.org>)
(eval-and-compile
  (if (string-match "\\(Lucid\\|XEmacs\\)" emacs-version)
      (progn
	(fset 'mic-make-overlay 'make-extent)
	(fset 'mic-delete-overlay 'delete-extent)
	(fset 'mic-overlay-put 'set-extent-property)
	(defun mic-cancel-timer (timer) (delete-itimer timer))
	(fset 'mic-run-with-idle-timer 'start-itimer)
	)
    (fset 'mic-make-overlay 'make-overlay)
    (fset 'mic-delete-overlay 'delete-overlay)
    (fset 'mic-overlay-put 'overlay-put)
    (fset 'mic-cancel-timer 'cancel-timer)
    (fset 'mic-run-with-idle-timer 'run-with-idle-timer)
    ))


(defun paren-activate ()
  "Activates mic-paren parenthesis highlighting.
paren-activate deactivates the paren.el and stig-paren.el packages if they are
active 
Options:
  paren-priority
  paren-sexp-mode
  paren-highlight-at-point
  paren-highlight-offscreen
  paren-message-offscreen
  paren-message-no-match
  paren-ding-unmatched
  paren-delay
  paren-dont-touch-blink
  paren-dont-activate-on-load
  paren-face
  paren-mismatch-face"
  (interactive)
  ;; Deactivate mic-paren.el (To remove redundant hooks)
  (paren-deactivate)
  ;; Deactivate paren.el if loaded
  (if (boundp 'post-command-idle-hook)
      (remove-hook 'post-command-idle-hook 'show-paren-command-hook))
  (remove-hook 'post-command-hook 'show-paren-command-hook)
  (and (boundp 'show-paren-overlay)
       show-paren-overlay
       (mic-delete-overlay show-paren-overlay))
  (and (boundp 'show-paren-overlay-1)
       show-paren-overlay-1
       (mic-delete-overlay show-paren-overlay-1))
  ;; Deactivate stig-paren.el if loaded
  (if (boundp 'post-command-idle-hook)
      (remove-hook 'post-command-idle-hook 'stig-paren-command-hook))
  (remove-hook 'post-command-hook 'stig-paren-command-hook)
  (remove-hook 'post-command-hook 'stig-paren-safe-command-hook)
  (remove-hook 'pre-command-hook 'stig-paren-delete-overlay)
  ;; Deactivate Emacs standard parenthesis blinking
  (or paren-dont-touch-blink
      (setq blink-matching-paren nil))

  (cond(
	;; If timers are available use them
	;; (Emacs 19.31 and above)
	(featurep 'timer)
	(if (numberp paren-delay)
	    (setq mic-paren-idle-timer 
		  (mic-run-with-idle-timer paren-delay t
					   'mic-paren-command-idle-hook))
	  (add-hook 'post-command-hook 'mic-paren-command-hook)))
       ;; If the idle hook exists assume it is functioning and use it 
       ;; (Emacs 19.30)
       ((and (boundp 'post-command-idle-hook) 
	     (boundp 'post-command-idle-delay))
	(if paren-delay
	    (add-hook 'post-command-idle-hook 'mic-paren-command-idle-hook)
	  (add-hook 'post-command-hook 'mic-paren-command-hook)))
       ;; Check if we (at least) have a post-command-hook, and use it
       ;; (Emacs 19.29 and below)
       ((boundp 'post-command-hook) 
	(add-hook 'post-command-hook 'mic-paren-command-hook))
       ;; Not possible to install mic-paren hooks
       (t (error "Cannot activate mic-paren in this Emacs version"))))



(defun paren-deactivate ()
  "Deactivates mic-paren parenthesis highlighting"
  (interactive)
  ;; Deactivate (don't bother to check where/if mic-paren is acivte, just
  ;; delete all possible hooks and timers)
  (if (boundp 'post-command-idle-hook)
      (remove-hook 'post-command-idle-hook 'mic-paren-command-idle-hook))
  (if mic-paren-idle-timer
      (mic-cancel-timer mic-paren-idle-timer))
  (remove-hook 'post-command-hook 'mic-paren-command-hook)

  ;; Remove any old highlighs
  (mic-delete-overlay mic-paren-backw-overlay)
  (mic-delete-overlay mic-paren-point-overlay)
  (mic-delete-overlay mic-paren-forw-overlay)

  ;; Reactivate Emacs standard parenthesis blinking
  (or paren-dont-touch-blink
      (setq blink-matching-paren t))
  )

;;; ======================================================================
;;; Internal variables:

(defvar mic-paren-backw-overlay (mic-make-overlay (point-min) (point-min))
  "Overlay for the open-paren which matches the close-paren before
point. When in sexp-mode this is the overlay for the expression before point.")

(defvar mic-paren-point-overlay (mic-make-overlay (point-min) (point-min))
  "Overlay for the close-paren before point.
(Not used when is sexp-mode.)")

(defvar mic-paren-forw-overlay (mic-make-overlay (point-min) (point-min))
  "Overlay for the close-paren which matches the open-paren after
point. When in sexp-mode this is the overlay for the expression after point.")

(defvar mic-paren-idle-timer nil
  "Idle-timer.  Used only in Emacs 19.31 and above (and if paren-delay is nil)")




;;; ======================================================================
;;; Internal function:



(defun mic-paren-command-hook ()
  (or executing-kbd-macro
      (input-pending-p)			;[This might cause trouble since the
                                        ; function is unreliable]
      (condition-case paren-error
	  (mic-paren-highlight)
	(error 
	 (if (not (window-minibuffer-p (selected-window)))
	     (message "mic-paren caught an error (please report): %s"
		      paren-error))))))

(defun mic-paren-command-idle-hook ()
  (condition-case paren-error
      (mic-paren-highlight)
    (error 
     (if (not (window-minibuffer-p (selected-window)))
	 (message "mic-paren caught an error (please report): %s" 
		  paren-error)))))


(defun mic-paren-highlight ()
  "The main-function of mic-paren. Does all highlighting, dinging, messages,
cleaning-up."
  ;; Remove any old highlighting
  (mic-delete-overlay mic-paren-forw-overlay)
  (mic-delete-overlay mic-paren-point-overlay)
  (mic-delete-overlay mic-paren-backw-overlay)

  ;; Handle backward highlighting (when after a close-paren):
  ;; If positioned after a close-paren, and
  ;;    not before an open-paren when priority=open, and
  ;;    the close-paren is not escaped then
  ;;      perform highlighting
  ;; else
  ;;      remove any old backward highlights
  (if (and (eq (char-syntax (preceding-char)) ?\))
	   (not (and (eq (char-syntax (following-char)) ?\()
		     (eq paren-priority 'open)))
	   (paren-evenp (paren-backslashes-before-char (1- (point)))))
       (let (open)
	 ;; Find the position for the open-paren
	 (save-excursion
	   (save-restriction
	     (if blink-matching-paren-distance
		 (narrow-to-region 
		  (max (point-min)
		       (- (point) blink-matching-paren-distance))
		  (point-max)))
	     (condition-case ()
		 (setq open (scan-sexps (point) -1))
	       (error nil))))

	 ;; If match found
	 ;;    highlight expression and/or print messages
	 ;; else
	 ;;    highlight unmatched paren
	 ;;    print no-match message
	 (if open
	     (let ((mismatch (not (eq (matching-paren (preceding-char)) 
				      (char-after open))))
		   (visible (pos-visible-in-window-p open)))
	       ;; If highlight is appropriate
	       ;;    highlight
	       ;; else
	       ;;    remove any old highlight
	       (if (or visible paren-highlight-offscreen paren-sexp-mode)
		   ;; If sexp-mode
		   ;;    highlight sexp
		   ;; else
		   ;;    highlight the two parens
		   (if paren-sexp-mode
		       (progn
			 (setq mic-paren-backw-overlay
			       (mic-make-overlay open (point)))
			 (if mismatch
			     (mic-overlay-put mic-paren-backw-overlay 
					      'face paren-mismatch-face)
			   (mic-overlay-put mic-paren-backw-overlay 
					    'face paren-face)))
		     (setq mic-paren-backw-overlay
			   (mic-make-overlay open (1+ open)))
		     (and paren-highlight-at-point
			  (setq mic-paren-point-overlay
				(mic-make-overlay (1- (point)) (point))))
		     (if mismatch
			 (progn
			   (mic-overlay-put mic-paren-backw-overlay 
					    'face paren-mismatch-face)
			   (and paren-highlight-at-point
				(mic-overlay-put mic-paren-point-overlay 
						 'face paren-mismatch-face)))
		       (mic-overlay-put mic-paren-backw-overlay 
					'face paren-face)
		       (and paren-highlight-at-point 
			    (mic-overlay-put mic-paren-point-overlay 
					     'face paren-face)))))
	       ;; Print messages if match is offscreen
	       (and paren-message-offscreen
		    (not visible)
		    (not (window-minibuffer-p (selected-window)))
		    (message "%s %s" 
			     (if mismatch "MISMATCH:" "Matches")
			     (mic-paren-get-matching-open-text open)))
	       ;; Ding if mismatch
	       (and mismatch
		    paren-ding-unmatched
		    (ding)))
	   (setq mic-paren-backw-overlay
		 (mic-make-overlay (1- (point)) (point)))
	   (mic-overlay-put mic-paren-backw-overlay
			    'face paren-no-match-face)
	   (and paren-message-no-match
		(not (window-minibuffer-p (selected-window)))
		(message "No opening parenthesis found"))
	   (and paren-message-no-match
		paren-ding-unmatched
		(ding)))))

  ;; Handle forward highlighting (when before an open-paren):
  ;; If positioned before an open-paren, and
  ;;    not after a close-paren when priority=close, and
  ;;    the open-paren is not escaped then
  ;;      perform highlighting
  ;; else
  ;;      remove any old forward highlights
  (if (and (eq (char-syntax (following-char)) ?\()
	   (not (and (eq (char-syntax (preceding-char)) ?\))
		     (eq paren-priority 'close)))
	   (paren-evenp (paren-backslashes-before-char (point))))
       (let (close)
	 ;; Find the position for the close-paren
	 (save-excursion
	   (save-restriction
	     (if blink-matching-paren-distance
		 (narrow-to-region 
		  (point-min)
		  (min (point-max)
		       (+ (point) blink-matching-paren-distance))))
      	     (condition-case ()
		 (setq close (scan-sexps (point) 1))
	       (error nil))))
	 ;; If match found
	 ;;    highlight expression and/or print messages
	 ;; else
	 ;;    highlight unmatched paren
	 ;;    print no-match message
	 (if close
	     (let ((mismatch (not (eq (matching-paren (following-char))
				      (char-after (1- close)))))
		   (visible (pos-visible-in-window-p close)))
	       ;; If highlight is appropriate
	       ;;    highlight
	       ;; else
	       ;;    remove any old highlight
	       (if (or visible paren-highlight-offscreen paren-sexp-mode)
		   ;; If sexp-mode
		   ;;    highlight sexp
		   ;; else
		   ;;    highlight the two parens
		   (if paren-sexp-mode
		       (progn
			 (setq mic-paren-forw-overlay
			       (mic-make-overlay (point) close))
			 (if mismatch
			     (mic-overlay-put mic-paren-forw-overlay 
					      'face paren-mismatch-face)
			   (mic-overlay-put mic-paren-forw-overlay 
					    'face paren-face)))
		     (setq mic-paren-forw-overlay
			   (mic-make-overlay (1- close) close))
		     (if mismatch
			 (mic-overlay-put mic-paren-forw-overlay 
					  'face paren-mismatch-face)
		       (mic-overlay-put mic-paren-forw-overlay 
					'face paren-face))))

	       ;; Print messages if match is offscreen
	       (and paren-message-offscreen
		    (not visible)
		    (not (window-minibuffer-p (selected-window)))
		    (message "%s %s" 
			     (if mismatch "MISMATCH:" "Matches")
			     (mic-paren-get-matching-close-text close)))
	       ;; Ding if mismatch
	       (and mismatch
		    paren-ding-unmatched
		    (ding)))
	   (setq mic-paren-forw-overlay
		 (mic-make-overlay (point) (1+ (point))))
	   (mic-overlay-put mic-paren-forw-overlay
			    'face paren-no-match-face)
	   (and paren-message-no-match
		(not (window-minibuffer-p (selected-window)))
		(message "No closing parenthesis found"))
	   (and paren-message-no-match
		paren-ding-unmatched
		(ding))))))

;;; --------------------------------------------------

(defun mic-paren-get-matching-open-text (open)
  "Returns a string with the context around OPEN-paren."
  ;; If there's stuff on this line preceding the paren, then display text from
  ;; beginning of line to paren.
  ;;
  ;; If, however, the paren is at the beginning of a line, then skip whitespace
  ;; forward and display text from paren to end of the next line containing
  ;; non-space text.
  ;;
  ;; (Same as in stig-paren.el)
  (save-excursion
    (goto-char open)
    (if (save-excursion
	  (skip-chars-backward " \t")
	  (not (bolp)))
	(progn
	  (beginning-of-line)
	  (concat (buffer-substring (point) (1+ open)) "..."))
      (forward-char 1)			;From the beginning-of-line
      (skip-chars-forward "\n \t")
      (end-of-line)
      (buffer-substring open (point)))))


(defun mic-paren-get-matching-close-text (close)
  "Returns a string with the context around CLOSE-paren."
  ;; The whole line up until the close-paren with "..." appended if there are
  ;; more text after the close-paren
  (save-excursion
    (goto-char close)
    (beginning-of-line)
    (concat
     (buffer-substring (point) close)
     (progn 
       (goto-char close)
       (if (looking-at "[ \t]*$")
	   ""
	 "...")))))
  

(defun paren-evenp (number)
  "Returns t if NUMBER is an even number, nil otherwise"
  (eq 0 (% number 2)))

(defun paren-backslashes-before-char (pnt)
  (setq pnt (1- pnt))
  (let ((n 0))
    (while (and (>= pnt (point-min))
		(eq (char-syntax (char-after pnt)) ?\\))
      (setq n (1+ n))
      (setq pnt (1- pnt)))
    n))

    

;;; ======================================================================
;;; Initialisation when loading:

;;; Try to load the timer feature if its not already loaded
(or paren-dont-load-timer
    (featurep 'timer)
    (condition-case ()
	(require 'timer)
      (error nil)))


(or paren-dont-activate-on-load
    (paren-activate))

;;; This is in case mic-paren.el is preloaded. [Does this work? /Mic]
(add-hook 'window-setup-hook
	  (function (lambda ()
		      (and window-system
			   (not paren-dont-activate-on-load)
			   (paren-activate)))))

(provide 'mic-paren)
(provide 'paren)