diff options
| author | Richard M. Stallman | 1993-07-18 04:47:10 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1993-07-18 04:47:10 +0000 |
| commit | eb0d9f08616c0271f1f1ccf7b111eeb4914cf3b3 (patch) | |
| tree | a1769b894639e77cfe0512a2e8942ee202c1c3f3 | |
| parent | 11872f7e1ec1e23d95893b88c3c8f51d0581b840 (diff) | |
| download | emacs-eb0d9f08616c0271f1f1ccf7b111eeb4914cf3b3.tar.gz emacs-eb0d9f08616c0271f1f1ccf7b111eeb4914cf3b3.zip | |
Enable the hook only if window-system.
Clear blink-paren-function at the same time.
(show-paren-command-hook): If after a closeparen,
highlight that closeparen as well as matching open.
Use a different color for a mismatch, if color screen.
| -rw-r--r-- | lisp/paren.el | 115 |
1 files changed, 71 insertions, 44 deletions
diff --git a/lisp/paren.el b/lisp/paren.el index 221c6aaf5d4..c96efcd1863 100644 --- a/lisp/paren.el +++ b/lisp/paren.el | |||
| @@ -28,69 +28,96 @@ | |||
| 28 | 28 | ||
| 29 | ;;; Code: | 29 | ;;; Code: |
| 30 | 30 | ||
| 31 | ;; This is the overlay used to highlight the matching paren. | ||
| 31 | (defvar show-paren-overlay nil) | 32 | (defvar show-paren-overlay nil) |
| 33 | ;; This is the overlay used to highlight the closeparen | ||
| 34 | ;; right before point. | ||
| 35 | (defvar show-paren-overlay-1 nil) | ||
| 36 | |||
| 37 | (defvar show-paren-mismatch-face nil) | ||
| 32 | 38 | ||
| 33 | ;; Find the place to show, if there is one, | 39 | ;; Find the place to show, if there is one, |
| 34 | ;; and show it until input arrives. | 40 | ;; and show it until input arrives. |
| 35 | (defun show-paren-command-hook () | 41 | (defun show-paren-command-hook () |
| 36 | (if window-system | 42 | (if window-system |
| 37 | (let (pos dir mismatch (oldpos (point)) | 43 | (let (pos dir mismatch (oldpos (point)) |
| 38 | (face (if (face-equal 'highlight 'region) | 44 | (face 'region)) |
| 39 | 'underline 'highlight))) | ||
| 40 | (cond ((eq (char-syntax (following-char)) ?\() | 45 | (cond ((eq (char-syntax (following-char)) ?\() |
| 41 | (setq dir 1)) | 46 | (setq dir 1)) |
| 42 | ((eq (char-syntax (preceding-char)) ?\)) | 47 | ((eq (char-syntax (preceding-char)) ?\)) |
| 43 | (setq dir -1))) | 48 | (setq dir -1))) |
| 44 | (save-excursion | 49 | (if dir |
| 45 | (save-restriction | 50 | (save-excursion |
| 46 | ;; Determine the range within which to look for a match. | 51 | (save-restriction |
| 47 | (if blink-matching-paren-distance | 52 | ;; Determine the range within which to look for a match. |
| 48 | (narrow-to-region (max (point-min) | 53 | (if blink-matching-paren-distance |
| 49 | (- (point) blink-matching-paren-distance)) | 54 | (narrow-to-region (max (point-min) |
| 50 | (min (point-max) | 55 | (- (point) blink-matching-paren-distance)) |
| 51 | (+ (point) blink-matching-paren-distance)))) | 56 | (min (point-max) |
| 52 | ;; Scan across one sexp within that range. | 57 | (+ (point) blink-matching-paren-distance)))) |
| 53 | (condition-case () | 58 | ;; Scan across one sexp within that range. |
| 54 | (setq pos (scan-sexps (point) dir)) | 59 | (condition-case () |
| 55 | (error nil)) | 60 | (setq pos (scan-sexps (point) dir)) |
| 56 | ;; See if the "matching" paren is the right kind of paren | 61 | (error nil)) |
| 57 | ;; to match the one we started at. | 62 | ;; See if the "matching" paren is the right kind of paren |
| 58 | (if pos | 63 | ;; to match the one we started at. |
| 59 | (let ((beg (min pos oldpos)) (end (max pos oldpos))) | 64 | (if pos |
| 60 | (and (/= (char-syntax (char-after beg)) ?\$) | 65 | (let ((beg (min pos oldpos)) (end (max pos oldpos))) |
| 61 | (setq mismatch | 66 | (and (/= (char-syntax (char-after beg)) ?\$) |
| 62 | (/= (char-after (1- end)) | 67 | (setq mismatch |
| 63 | (logand (lsh (aref (syntax-table) | 68 | (/= (char-after (1- end)) |
| 64 | (char-after beg)) | 69 | (logand (lsh (aref (syntax-table) |
| 65 | -8) | 70 | (char-after beg)) |
| 66 | 255)))))) | 71 | -8) |
| 67 | ;; If they don't properly match, don't show. | 72 | 255)))))) |
| 68 | (if mismatch | 73 | ;; If they don't properly match, use a different face, |
| 69 | (progn | 74 | ;; or print a message. |
| 70 | (message "Paren mismatch") | 75 | (if mismatch |
| 71 | ;;; (setq pos nil) | 76 | (progn |
| 72 | )))) | 77 | (and (null show-paren-mismatch-face) |
| 78 | (x-display-color-p) | ||
| 79 | (or (setq show-paren-mismatch-face | ||
| 80 | (internal-find-face 'paren-mismatch)) | ||
| 81 | (progn | ||
| 82 | (setq show-paren-mismatch-face | ||
| 83 | (make-face 'paren-mismatch)) | ||
| 84 | (set-face-background 'paren-mismatch 'purple)))) | ||
| 85 | (if show-paren-mismatch-face | ||
| 86 | (setq face show-paren-mismatch-face) | ||
| 87 | (message "Paren mismatch")))) | ||
| 88 | ))) | ||
| 73 | (cond (pos | 89 | (cond (pos |
| 90 | (if (= dir -1) | ||
| 91 | ;; If matching backwards, highlight the closeparen | ||
| 92 | ;; before point as well as its matching open. | ||
| 93 | (progn | ||
| 94 | (if show-paren-overlay-1 | ||
| 95 | (move-overlay show-paren-overlay-1 (+ (point) dir) (point)) | ||
| 96 | (setq show-paren-overlay-1 | ||
| 97 | (make-overlay (- pos dir) pos))) | ||
| 98 | (overlay-put show-paren-overlay-1 'face face)) | ||
| 99 | ;; Otherwise, turn off any such highlighting. | ||
| 100 | (and show-paren-overlay-1 | ||
| 101 | (overlay-buffer show-paren-overlay-1) | ||
| 102 | (delete-overlay show-paren-overlay-1))) | ||
| 103 | ;; Turn on highlighting for the matching paren. | ||
| 74 | (if show-paren-overlay | 104 | (if show-paren-overlay |
| 75 | (move-overlay show-paren-overlay (- pos dir) pos) | 105 | (move-overlay show-paren-overlay (- pos dir) pos) |
| 76 | (setq show-paren-overlay | 106 | (setq show-paren-overlay |
| 77 | (make-overlay (- pos dir) pos))) | 107 | (make-overlay (- pos dir) pos))) |
| 78 | (overlay-put show-paren-overlay 'face face) | 108 | (overlay-put show-paren-overlay 'face face)) |
| 79 | ;;; This is code to blink the highlighting. | ||
| 80 | ;;; It is desirable to avoid this because | ||
| 81 | ;;; it would interfere with auto-save and gc when idle. | ||
| 82 | ;;; (while (sit-for 1) | ||
| 83 | ;;; (overlay-put show-paren-overlay | ||
| 84 | ;;; 'face | ||
| 85 | ;;; (if (overlay-get show-paren-overlay | ||
| 86 | ;;; 'face) | ||
| 87 | ;;; nil face))) | ||
| 88 | ) | ||
| 89 | (t | 109 | (t |
| 110 | ;; If not at a paren that has a match, | ||
| 111 | ;; turn off any previous paren highlighting. | ||
| 90 | (and show-paren-overlay (overlay-buffer show-paren-overlay) | 112 | (and show-paren-overlay (overlay-buffer show-paren-overlay) |
| 91 | (delete-overlay show-paren-overlay))))))) | 113 | (delete-overlay show-paren-overlay)) |
| 114 | (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1) | ||
| 115 | (delete-overlay show-paren-overlay-1))))))) | ||
| 92 | 116 | ||
| 93 | (add-hook 'post-command-hook 'show-paren-command-hook) | 117 | (if window-system |
| 118 | (progn | ||
| 119 | (setq blink-paren-function nil) | ||
| 120 | (add-hook 'post-command-hook 'show-paren-command-hook))) | ||
| 94 | 121 | ||
| 95 | (provide 'paren) | 122 | (provide 'paren) |
| 96 | 123 | ||