1993-06-28 23:41:38 +00:00
|
|
|
;;; paren.el --- highlight matching paren.
|
1993-06-29 04:03:22 +00:00
|
|
|
;; Copyright (C) 1993 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
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
|
|
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
|
|
|
;;; 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)
|
1993-07-18 04:47:10 +00:00
|
|
|
;; This is the overlay used to highlight the closeparen
|
|
|
|
;; right before point.
|
|
|
|
(defvar show-paren-overlay-1 nil)
|
|
|
|
|
|
|
|
(defvar show-paren-mismatch-face nil)
|
1993-06-28 23:41:38 +00:00
|
|
|
|
1993-07-20 05:59:51 +00:00
|
|
|
(defvar show-paren-face 'region
|
|
|
|
"*Name of face to use for showing the matching paren.")
|
|
|
|
|
1993-06-29 18:21:12 +00:00
|
|
|
;; Find the place to show, if there is one,
|
|
|
|
;; and show it until input arrives.
|
|
|
|
(defun show-paren-command-hook ()
|
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.
|
1994-06-25 01:20:11 +00:00
|
|
|
(if (and window-system (not executing-kbd-macro) (sit-for 0 100))
|
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.
|
|
|
|
(condition-case ()
|
|
|
|
(setq pos (scan-sexps (point) dir))
|
|
|
|
(error nil))
|
|
|
|
;; See if the "matching" paren is the right kind of paren
|
|
|
|
;; to match the one we started at.
|
|
|
|
(if pos
|
|
|
|
(let ((beg (min pos oldpos)) (end (max pos oldpos)))
|
|
|
|
(and (/= (char-syntax (char-after beg)) ?\$)
|
|
|
|
(setq mismatch
|
|
|
|
(/= (char-after (1- end))
|
1994-06-25 01:20:11 +00:00
|
|
|
(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)
|
1994-05-23 07:45:49 +00:00
|
|
|
(or (internal-find-face 'paren-mismatch)
|
1993-07-18 04:47:10 +00:00
|
|
|
(progn
|
1993-08-11 20:18:06 +00:00
|
|
|
(make-face 'paren-mismatch)
|
1993-07-20 05:59:51 +00:00
|
|
|
(set-face-background 'paren-mismatch
|
1994-06-23 14:31:35 +00:00
|
|
|
"purple")
|
|
|
|
(set-face-foreground 'paren-mismatch
|
|
|
|
"white")))
|
1994-05-23 07:45:49 +00:00
|
|
|
(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
|
1993-07-18 04:47:10 +00:00
|
|
|
(if (= dir -1)
|
|
|
|
;; If matching backwards, highlight the closeparen
|
|
|
|
;; before point as well as its matching open.
|
|
|
|
(progn
|
|
|
|
(if show-paren-overlay-1
|
1993-07-30 07:50:15 +00:00
|
|
|
(move-overlay show-paren-overlay-1
|
|
|
|
(+ (point) dir) (point)
|
|
|
|
(current-buffer))
|
1993-07-18 04:47:10 +00:00
|
|
|
(setq show-paren-overlay-1
|
1994-06-25 01:20:11 +00:00
|
|
|
(make-overlay (- pos dir) pos))
|
|
|
|
(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)))
|
|
|
|
;; Turn on highlighting for the matching paren.
|
1993-07-10 05:16:08 +00:00
|
|
|
(if show-paren-overlay
|
1993-07-30 07:50:15 +00:00
|
|
|
(move-overlay show-paren-overlay (- pos dir) pos
|
|
|
|
(current-buffer))
|
1993-07-10 05:16:08 +00:00
|
|
|
(setq show-paren-overlay
|
1994-06-25 01:20:11 +00:00
|
|
|
(make-overlay (- pos dir) pos))
|
|
|
|
(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
|
|
|
|
1993-07-18 04:47:10 +00:00
|
|
|
(if window-system
|
|
|
|
(progn
|
|
|
|
(setq blink-paren-function nil)
|
|
|
|
(add-hook 'post-command-hook 'show-paren-command-hook)))
|
1993-08-03 17:45:17 +00:00
|
|
|
;;; This is in case paren.el is preloaded.
|
|
|
|
(add-hook 'window-setup-hook
|
|
|
|
(function (lambda ()
|
|
|
|
(if window-system
|
|
|
|
(progn
|
|
|
|
(setq blink-paren-function nil)
|
|
|
|
(add-hook 'post-command-hook
|
|
|
|
'show-paren-command-hook))))))
|
1993-06-29 21:27:42 +00:00
|
|
|
(provide 'paren)
|
|
|
|
|
|
|
|
;;; paren.el ends here
|