1993-06-28 23:41:38 +00:00
|
|
|
;;; paren.el --- highlight matching paren.
|
1996-01-14 07:34:30 +00:00
|
|
|
|
1996-02-28 01:28:42 +00:00
|
|
|
;; Copyright (C) 1993, 1996 Free Software Foundation, Inc.
|
1993-06-28 23:41:38 +00:00
|
|
|
|
1993-07-04 17:52:52 +00:00
|
|
|
;; Author: rms@gnu.ai.mit.edu
|
|
|
|
;; Maintainer: FSF
|
|
|
|
;; Keywords: languages, faces
|
1993-07-04 17:41:56 +00:00
|
|
|
|
1993-06-28 23:41:38 +00:00
|
|
|
;; This file is part of GNU Emacs.
|
|
|
|
|
|
|
|
;; GNU Emacs 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, or (at your option)
|
|
|
|
;; any later version.
|
|
|
|
|
|
|
|
;; GNU Emacs 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
|
1996-01-14 07:34:30 +00:00
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
;; Boston, MA 02111-1307, USA.
|
1993-06-28 23:41:38 +00:00
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Load this and it will display highlighting on whatever
|
|
|
|
;; paren matches the one before or after point.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
1993-07-18 04:47:10 +00:00
|
|
|
;; This is the overlay used to highlight the matching paren.
|
1993-06-29 18:21:12 +00:00
|
|
|
(defvar show-paren-overlay nil)
|
1996-02-28 01:28:42 +00:00
|
|
|
;; This is the overlay used to highlight the closeparen right before point.
|
1993-07-18 04:47:10 +00:00
|
|
|
(defvar show-paren-overlay-1 nil)
|
|
|
|
|
1996-02-28 01:28:42 +00:00
|
|
|
(defvar show-paren-mode nil)
|
|
|
|
(defvar show-paren-idle-timer nil)
|
|
|
|
|
1993-07-18 04:47:10 +00:00
|
|
|
(defvar show-paren-mismatch-face nil)
|
1993-06-28 23:41:38 +00:00
|
|
|
|
1996-02-28 01:28:42 +00:00
|
|
|
(defvar show-paren-delay (if (featurep 'lisp-float-type) 0.125 1)
|
|
|
|
"*Time in seconds to delay before showing the matching paren.")
|
|
|
|
|
1993-07-20 05:59:51 +00:00
|
|
|
(defvar show-paren-face 'region
|
1996-02-28 01:28:42 +00:00
|
|
|
"*Name of the face to use for showing the matching paren.")
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun show-paren-mode (&optional arg)
|
|
|
|
"Toggle Show Paren mode.
|
|
|
|
With prefix ARG, turn Show Paren mode on if and only if ARG is positive.
|
|
|
|
Returns the new status of Show Paren mode (non-nil means on).
|
|
|
|
|
|
|
|
When Show Paren mode is enabled, any matching parenthesis is highlighted
|
|
|
|
after `show-paren-delay' seconds of Emacs idle time."
|
|
|
|
(interactive "P")
|
|
|
|
(if window-system
|
|
|
|
(let ((on-p (if arg
|
|
|
|
(> (prefix-numeric-value arg) 0)
|
|
|
|
(not show-paren-mode))))
|
|
|
|
(setq blink-matching-paren-on-screen (not on-p))
|
|
|
|
(and show-paren-idle-timer (cancel-timer show-paren-idle-timer))
|
|
|
|
(if on-p
|
|
|
|
(setq show-paren-idle-timer (run-with-idle-timer show-paren-delay t
|
|
|
|
'show-paren-function))
|
|
|
|
(and show-paren-overlay (overlay-buffer show-paren-overlay)
|
|
|
|
(delete-overlay show-paren-overlay))
|
|
|
|
(and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
|
|
|
|
(delete-overlay show-paren-overlay-1)))
|
|
|
|
(setq show-paren-mode on-p))))
|
1993-07-20 05:59:51 +00:00
|
|
|
|
1993-06-29 18:21:12 +00:00
|
|
|
;; Find the place to show, if there is one,
|
|
|
|
;; and show it until input arrives.
|
1996-02-28 01:28:42 +00:00
|
|
|
(defun show-paren-function ()
|
1993-08-12 05:33:16 +00:00
|
|
|
;; Do nothing if no window system to display results with.
|
1994-01-17 22:43:40 +00:00
|
|
|
;; Do nothing if executing keyboard macro.
|
1993-08-12 05:33:16 +00:00
|
|
|
;; Do nothing if input is pending.
|
1995-08-21 22:00:21 +00:00
|
|
|
(if window-system
|
1993-07-10 05:16:08 +00:00
|
|
|
(let (pos dir mismatch (oldpos (point))
|
1993-07-20 05:59:51 +00:00
|
|
|
(face show-paren-face))
|
1994-06-23 14:42:48 +00:00
|
|
|
(cond ((eq (char-syntax (preceding-char)) ?\))
|
|
|
|
(setq dir -1))
|
|
|
|
((eq (char-syntax (following-char)) ?\()
|
|
|
|
(setq dir 1)))
|
1993-07-18 04:47:10 +00:00
|
|
|
(if dir
|
|
|
|
(save-excursion
|
|
|
|
(save-restriction
|
|
|
|
;; Determine the range within which to look for a match.
|
|
|
|
(if blink-matching-paren-distance
|
|
|
|
(narrow-to-region (max (point-min)
|
|
|
|
(- (point) blink-matching-paren-distance))
|
|
|
|
(min (point-max)
|
|
|
|
(+ (point) blink-matching-paren-distance))))
|
|
|
|
;; Scan across one sexp within that range.
|
1996-09-03 17:54:23 +00:00
|
|
|
;; Errors or nil mean there is a mismatch.
|
1993-07-18 04:47:10 +00:00
|
|
|
(condition-case ()
|
|
|
|
(setq pos (scan-sexps (point) dir))
|
1996-09-03 17:54:23 +00:00
|
|
|
(error (setq pos t
|
|
|
|
mismatch t)))
|
|
|
|
;; If found a "matching" paren, see if it is the right
|
|
|
|
;; kind of paren to match the one we started at.
|
|
|
|
(if (integerp pos)
|
1993-07-18 04:47:10 +00:00
|
|
|
(let ((beg (min pos oldpos)) (end (max pos oldpos)))
|
|
|
|
(and (/= (char-syntax (char-after beg)) ?\$)
|
|
|
|
(setq mismatch
|
1994-08-11 02:42:22 +00:00
|
|
|
(not (eq (char-after (1- end))
|
|
|
|
;; This can give nil.
|
|
|
|
(matching-paren (char-after beg))))))))
|
1993-07-18 04:47:10 +00:00
|
|
|
;; If they don't properly match, use a different face,
|
|
|
|
;; or print a message.
|
|
|
|
(if mismatch
|
|
|
|
(progn
|
|
|
|
(and (null show-paren-mismatch-face)
|
|
|
|
(x-display-color-p)
|
1995-01-10 17:44:28 +00:00
|
|
|
(progn
|
1995-11-10 18:58:14 +00:00
|
|
|
(add-to-list 'facemenu-unlisted-faces
|
|
|
|
'paren-mismatch)
|
1995-01-10 17:44:28 +00:00
|
|
|
(make-face 'paren-mismatch)
|
|
|
|
(or (face-nontrivial-p 'paren-mismatch t)
|
|
|
|
(progn
|
|
|
|
(set-face-background 'paren-mismatch
|
|
|
|
"purple")
|
|
|
|
(set-face-foreground 'paren-mismatch
|
|
|
|
"white")))
|
|
|
|
(setq show-paren-mismatch-face 'paren-mismatch)))
|
1993-07-18 04:47:10 +00:00
|
|
|
(if show-paren-mismatch-face
|
|
|
|
(setq face show-paren-mismatch-face)
|
|
|
|
(message "Paren mismatch"))))
|
|
|
|
)))
|
1993-07-10 05:16:08 +00:00
|
|
|
(cond (pos
|
1996-09-03 17:54:23 +00:00
|
|
|
(if (or (= dir -1)
|
|
|
|
(not (integerp pos)))
|
1993-07-18 04:47:10 +00:00
|
|
|
;; If matching backwards, highlight the closeparen
|
|
|
|
;; before point as well as its matching open.
|
1996-09-03 17:54:23 +00:00
|
|
|
;; If matching forward, and the openparen is unbalanced,
|
|
|
|
;; highlight the paren at point to indicate misbalance.
|
|
|
|
(let ((from (if (= dir 1)
|
|
|
|
(point)
|
|
|
|
(1- (point))))
|
|
|
|
(to (if (= dir 1)
|
|
|
|
(1+ (point))
|
|
|
|
(point))))
|
1993-07-18 04:47:10 +00:00
|
|
|
(if show-paren-overlay-1
|
1993-07-30 07:50:15 +00:00
|
|
|
(move-overlay show-paren-overlay-1
|
1996-09-03 17:54:23 +00:00
|
|
|
from to
|
1993-07-30 07:50:15 +00:00
|
|
|
(current-buffer))
|
1993-07-18 04:47:10 +00:00
|
|
|
(setq show-paren-overlay-1
|
1996-09-03 17:54:23 +00:00
|
|
|
(make-overlay from to)))
|
1994-08-17 22:31:16 +00:00
|
|
|
;; Always set the overlay face, since it varies.
|
|
|
|
(overlay-put show-paren-overlay-1 'face face))
|
1993-07-18 04:47:10 +00:00
|
|
|
;; Otherwise, turn off any such highlighting.
|
|
|
|
(and show-paren-overlay-1
|
|
|
|
(overlay-buffer show-paren-overlay-1)
|
|
|
|
(delete-overlay show-paren-overlay-1)))
|
1996-09-03 17:54:23 +00:00
|
|
|
;; Turn on highlighting for the matching paren, if found.
|
|
|
|
;; If it's an unmatched paren, turn off any such highlighting.
|
|
|
|
(or (and (not (integerp pos))
|
|
|
|
(delete-overlay show-paren-overlay))
|
|
|
|
(if show-paren-overlay
|
|
|
|
(move-overlay show-paren-overlay (- pos dir) pos
|
|
|
|
(current-buffer))
|
|
|
|
(setq show-paren-overlay
|
|
|
|
(make-overlay (- pos dir) pos))))
|
1994-08-17 22:31:16 +00:00
|
|
|
;; Always set the overlay face, since it varies.
|
|
|
|
(overlay-put show-paren-overlay 'face face))
|
1993-07-10 05:16:08 +00:00
|
|
|
(t
|
1993-07-18 04:47:10 +00:00
|
|
|
;; If not at a paren that has a match,
|
|
|
|
;; turn off any previous paren highlighting.
|
1993-07-10 05:16:08 +00:00
|
|
|
(and show-paren-overlay (overlay-buffer show-paren-overlay)
|
1993-07-18 04:47:10 +00:00
|
|
|
(delete-overlay show-paren-overlay))
|
|
|
|
(and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
|
|
|
|
(delete-overlay show-paren-overlay-1)))))))
|
1993-06-28 23:41:38 +00:00
|
|
|
|
1996-02-28 01:28:42 +00:00
|
|
|
;;; For back compatibility we turn ourselves on if we're dumped or loaded.
|
1996-02-28 12:17:10 +00:00
|
|
|
(add-hook 'window-setup-hook '(lambda () (show-paren-mode t)))
|
1996-02-28 01:28:42 +00:00
|
|
|
(show-paren-mode t)
|
|
|
|
|
1993-06-29 21:27:42 +00:00
|
|
|
(provide 'paren)
|
|
|
|
|
|
|
|
;;; paren.el ends here
|