diff options
| author | Simon Marshall | 1997-06-19 08:37:05 +0000 |
|---|---|---|
| committer | Simon Marshall | 1997-06-19 08:37:05 +0000 |
| commit | 173f234148593fc453638d8490b90390d4945e65 (patch) | |
| tree | 6d1772d3b4ec8f2bb84f32cf8beb06da648bcd54 /lisp | |
| parent | 93853f3d0c280e3465dfd1fe52019e4eeb8b0f72 (diff) | |
| download | emacs-173f234148593fc453638d8490b90390d4945e65.tar.gz emacs-173f234148593fc453638d8490b90390d4945e65.zip | |
customise, rewrite and extend.
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/paren.el | 278 |
1 files changed, 155 insertions, 123 deletions
diff --git a/lisp/paren.el b/lisp/paren.el index f76037c919c..38e738c57cb 100644 --- a/lisp/paren.el +++ b/lisp/paren.el | |||
| @@ -30,22 +30,18 @@ | |||
| 30 | 30 | ||
| 31 | ;;; Code: | 31 | ;;; Code: |
| 32 | 32 | ||
| 33 | (defgroup paren-showing nil | ||
| 34 | "Showing (un)matching of parens and expressions." | ||
| 35 | :prefix "show-paren-" | ||
| 36 | :group 'paren-matching) | ||
| 37 | |||
| 33 | ;; This is the overlay used to highlight the matching paren. | 38 | ;; This is the overlay used to highlight the matching paren. |
| 34 | (defvar show-paren-overlay nil) | 39 | (defvar show-paren-overlay nil) |
| 35 | ;; This is the overlay used to highlight the closeparen right before point. | 40 | ;; This is the overlay used to highlight the closeparen right before point. |
| 36 | (defvar show-paren-overlay-1 nil) | 41 | (defvar show-paren-overlay-1 nil) |
| 37 | 42 | ||
| 38 | (defvar show-paren-mode nil) | ||
| 39 | (defvar show-paren-idle-timer nil) | 43 | (defvar show-paren-idle-timer nil) |
| 40 | 44 | ||
| 41 | (defvar show-paren-mismatch-face nil) | ||
| 42 | |||
| 43 | (defvar show-paren-delay (if (featurep 'lisp-float-type) 0.125 1) | ||
| 44 | "*Time in seconds to delay before showing the matching paren.") | ||
| 45 | |||
| 46 | (defvar show-paren-face 'region | ||
| 47 | "*Name of the face to use for showing the matching paren.") | ||
| 48 | |||
| 49 | ;;;###autoload | 45 | ;;;###autoload |
| 50 | (defun show-paren-mode (&optional arg) | 46 | (defun show-paren-mode (&optional arg) |
| 51 | "Toggle Show Paren mode. | 47 | "Toggle Show Paren mode. |
| @@ -53,22 +49,68 @@ With prefix ARG, turn Show Paren mode on if and only if ARG is positive. | |||
| 53 | Returns the new status of Show Paren mode (non-nil means on). | 49 | Returns the new status of Show Paren mode (non-nil means on). |
| 54 | 50 | ||
| 55 | When Show Paren mode is enabled, any matching parenthesis is highlighted | 51 | When Show Paren mode is enabled, any matching parenthesis is highlighted |
| 56 | after `show-paren-delay' seconds of Emacs idle time." | 52 | in `show-paren-style' after `show-paren-delay' seconds of Emacs idle time." |
| 57 | (interactive "P") | 53 | (interactive "P") |
| 58 | (if window-system | 54 | (when window-system |
| 59 | (let ((on-p (if arg | 55 | (let ((on-p (if arg |
| 60 | (> (prefix-numeric-value arg) 0) | 56 | (> (prefix-numeric-value arg) 0) |
| 61 | (not show-paren-mode)))) | 57 | (not show-paren-mode)))) |
| 62 | (setq blink-matching-paren-on-screen (not on-p)) | 58 | (setq blink-matching-paren-on-screen (not on-p)) |
| 63 | (and show-paren-idle-timer (cancel-timer show-paren-idle-timer)) | 59 | (when show-paren-idle-timer |
| 64 | (if on-p | 60 | (cancel-timer show-paren-idle-timer)) |
| 65 | (setq show-paren-idle-timer (run-with-idle-timer show-paren-delay t | 61 | (if on-p |
| 66 | 'show-paren-function)) | 62 | (setq show-paren-idle-timer (run-with-idle-timer |
| 67 | (and show-paren-overlay (overlay-buffer show-paren-overlay) | 63 | show-paren-delay t |
| 68 | (delete-overlay show-paren-overlay)) | 64 | 'show-paren-function)) |
| 69 | (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1) | 65 | (and show-paren-overlay (overlay-buffer show-paren-overlay) |
| 70 | (delete-overlay show-paren-overlay-1))) | 66 | (delete-overlay show-paren-overlay)) |
| 71 | (setq show-paren-mode on-p)))) | 67 | (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1) |
| 68 | (delete-overlay show-paren-overlay-1))) | ||
| 69 | (setq show-paren-mode on-p)))) | ||
| 70 | |||
| 71 | ;; Naughty hack. This variable was originally a `defvar' to keep track of | ||
| 72 | ;; whether Show Paren mode was turned on or not. As a `defcustom' with | ||
| 73 | ;; special `:set' and `:require' forms, we can provide custom mode control. | ||
| 74 | (defcustom show-paren-mode nil | ||
| 75 | "Toggle Show Paren mode. | ||
| 76 | When Show Paren mode is enabled, any matching parenthesis is highlighted | ||
| 77 | after `show-paren-delay' seconds of Emacs idle time. | ||
| 78 | You must modify via \\[customize] for this variable to have an effect." | ||
| 79 | :set (lambda (symbol value) | ||
| 80 | (show-paren-mode (or value 0))) | ||
| 81 | :type 'boolean | ||
| 82 | :group 'paren-showing | ||
| 83 | :require 'paren) | ||
| 84 | |||
| 85 | (defcustom show-paren-style 'parenthesis | ||
| 86 | "*Style used when showing a matching paren. | ||
| 87 | Valid styles are `parenthesis' (meaning show the matching paren), | ||
| 88 | `expression' (meaning show the entire expression enclosed by the paren) and | ||
| 89 | `mixed' (meaning show the matching paren if it is visible, and the expression | ||
| 90 | otherwise)." | ||
| 91 | :type '(choice (const parenthesis) (const expression) (const mixed)) | ||
| 92 | :group 'paren-showing) | ||
| 93 | |||
| 94 | (defcustom show-paren-delay | ||
| 95 | (if (featurep 'lisp-float-type) (/ (float 1) (float 8)) 1) | ||
| 96 | "*Time in seconds to delay before showing a matching paren." | ||
| 97 | :type '(number :tag "seconds") | ||
| 98 | :group 'paren-showing) | ||
| 99 | |||
| 100 | (defface show-paren-match-face | ||
| 101 | '((((class color)) (:background "turquoise")) | ||
| 102 | (((class grayscale)) (:background "gray")) | ||
| 103 | (t (:reverse-video t))) | ||
| 104 | "Show Paren mode face used for a matching paren." | ||
| 105 | :group 'faces | ||
| 106 | :group 'paren-showing) | ||
| 107 | |||
| 108 | (defface show-paren-mismatch-face | ||
| 109 | '((((class color)) (:foreground "white" :background "purple")) | ||
| 110 | (t (:reverse-video t))) | ||
| 111 | "Show Paren mode face used for a mismatching paren." | ||
| 112 | :group 'faces | ||
| 113 | :group 'paren-showing) | ||
| 72 | 114 | ||
| 73 | ;; Find the place to show, if there is one, | 115 | ;; Find the place to show, if there is one, |
| 74 | ;; and show it until input arrives. | 116 | ;; and show it until input arrives. |
| @@ -76,105 +118,95 @@ after `show-paren-delay' seconds of Emacs idle time." | |||
| 76 | ;; Do nothing if no window system to display results with. | 118 | ;; Do nothing if no window system to display results with. |
| 77 | ;; Do nothing if executing keyboard macro. | 119 | ;; Do nothing if executing keyboard macro. |
| 78 | ;; Do nothing if input is pending. | 120 | ;; Do nothing if input is pending. |
| 79 | (if window-system | 121 | (when window-system |
| 80 | (let (pos dir mismatch (oldpos (point)) | 122 | (let (pos dir mismatch face (oldpos (point))) |
| 81 | (face show-paren-face)) | 123 | (cond ((eq (char-syntax (preceding-char)) ?\)) |
| 82 | (cond ((eq (char-syntax (preceding-char)) ?\)) | 124 | (setq dir -1)) |
| 83 | (setq dir -1)) | 125 | ((eq (char-syntax (following-char)) ?\() |
| 84 | ((eq (char-syntax (following-char)) ?\() | 126 | (setq dir 1))) |
| 85 | (setq dir 1))) | 127 | ;; |
| 86 | (if dir | 128 | ;; Find the other end of the sexp. |
| 87 | (save-excursion | 129 | (when dir |
| 88 | (save-restriction | 130 | (save-excursion |
| 89 | ;; Determine the range within which to look for a match. | 131 | (save-restriction |
| 90 | (if blink-matching-paren-distance | 132 | ;; Determine the range within which to look for a match. |
| 91 | (narrow-to-region (max (point-min) | 133 | (when blink-matching-paren-distance |
| 92 | (- (point) blink-matching-paren-distance)) | 134 | (narrow-to-region |
| 93 | (min (point-max) | 135 | (max (point-min) (- (point) blink-matching-paren-distance)) |
| 94 | (+ (point) blink-matching-paren-distance)))) | 136 | (min (point-max) (+ (point) blink-matching-paren-distance)))) |
| 95 | ;; Scan across one sexp within that range. | 137 | ;; Scan across one sexp within that range. |
| 96 | ;; Errors or nil mean there is a mismatch. | 138 | ;; Errors or nil mean there is a mismatch. |
| 97 | (condition-case () | 139 | (condition-case () |
| 98 | (setq pos (scan-sexps (point) dir)) | 140 | (setq pos (scan-sexps (point) dir)) |
| 99 | (error (setq pos t | 141 | (error (setq pos t mismatch t))) |
| 100 | mismatch t))) | 142 | ;; If found a "matching" paren, see if it is the right |
| 101 | ;; If found a "matching" paren, see if it is the right | 143 | ;; kind of paren to match the one we started at. |
| 102 | ;; kind of paren to match the one we started at. | 144 | (when (integerp pos) |
| 103 | (if (integerp pos) | 145 | (let ((beg (min pos oldpos)) (end (max pos oldpos))) |
| 104 | (let ((beg (min pos oldpos)) (end (max pos oldpos))) | 146 | (when (/= (char-syntax (char-after beg)) ?\$) |
| 105 | (and (/= (char-syntax (char-after beg)) ?\$) | 147 | (setq mismatch |
| 106 | (setq mismatch | 148 | (not (eq (char-before end) |
| 107 | (not (eq (char-before end) | 149 | ;; This can give nil. |
| 108 | ;; This can give nil. | 150 | (matching-paren (char-after beg))))))))))) |
| 109 | (matching-paren (char-after beg)))))))) | 151 | ;; |
| 110 | ;; If they don't properly match, use a different face, | 152 | ;; Highlight the other end of the sexp, or unhighlight if none. |
| 111 | ;; or print a message. | 153 | (if (not pos) |
| 112 | (if mismatch | 154 | (progn |
| 113 | (progn | 155 | ;; If not at a paren that has a match, |
| 114 | (and (null show-paren-mismatch-face) | 156 | ;; turn off any previous paren highlighting. |
| 115 | (x-display-color-p) | 157 | (and show-paren-overlay (overlay-buffer show-paren-overlay) |
| 116 | (progn | 158 | (delete-overlay show-paren-overlay)) |
| 117 | (add-to-list 'facemenu-unlisted-faces | 159 | (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1) |
| 118 | 'paren-mismatch) | 160 | (delete-overlay show-paren-overlay-1))) |
| 119 | (make-face 'paren-mismatch) | 161 | ;; |
| 120 | (or (face-nontrivial-p 'paren-mismatch t) | 162 | ;; Use the correct face. |
| 121 | (progn | 163 | (if mismatch |
| 122 | (set-face-background 'paren-mismatch | 164 | (setq face 'show-paren-mismatch-face) |
| 123 | "purple") | 165 | (setq face 'show-paren-match-face)) |
| 124 | (set-face-foreground 'paren-mismatch | 166 | ;; |
| 125 | "white"))) | 167 | ;; If matching backwards, highlight the closeparen |
| 126 | (setq show-paren-mismatch-face 'paren-mismatch))) | 168 | ;; before point as well as its matching open. |
| 127 | (if show-paren-mismatch-face | 169 | ;; If matching forward, and the openparen is unbalanced, |
| 128 | (setq face show-paren-mismatch-face) | 170 | ;; highlight the paren at point to indicate misbalance. |
| 129 | (message "Paren mismatch")))) | 171 | ;; Otherwise, turn off any such highlighting. |
| 130 | ))) | 172 | (if (and (= dir 1) (integerp pos)) |
| 131 | (cond (pos | 173 | (when (and show-paren-overlay-1 |
| 132 | (if (or (= dir -1) | 174 | (overlay-buffer show-paren-overlay-1)) |
| 133 | (not (integerp pos))) | 175 | (delete-overlay show-paren-overlay-1)) |
| 134 | ;; If matching backwards, highlight the closeparen | 176 | (let ((from (if (= dir 1) |
| 135 | ;; before point as well as its matching open. | 177 | (point) |
| 136 | ;; If matching forward, and the openparen is unbalanced, | 178 | (forward-point -1))) |
| 137 | ;; highlight the paren at point to indicate misbalance. | 179 | (to (if (= dir 1) |
| 138 | (let ((from (if (= dir 1) | 180 | (forward-point 1) |
| 139 | (point) | 181 | (point)))) |
| 140 | (forward-point -1))) | 182 | (if show-paren-overlay-1 |
| 141 | (to (if (= dir 1) | 183 | (move-overlay show-paren-overlay-1 from to (current-buffer)) |
| 142 | (forward-point 1) | 184 | (setq show-paren-overlay-1 (make-overlay from to))) |
| 143 | (point)))) | 185 | ;; Always set the overlay face, since it varies. |
| 144 | (if show-paren-overlay-1 | 186 | (overlay-put show-paren-overlay-1 'face face))) |
| 145 | (move-overlay show-paren-overlay-1 | 187 | ;; |
| 146 | from to | 188 | ;; Turn on highlighting for the matching paren, if found. |
| 147 | (current-buffer)) | 189 | ;; If it's an unmatched paren, turn off any such highlighting. |
| 148 | (setq show-paren-overlay-1 | 190 | (unless (integerp pos) |
| 149 | (make-overlay from to))) | 191 | (delete-overlay show-paren-overlay)) |
| 150 | ;; Always set the overlay face, since it varies. | 192 | (let ((to (if (or (eq show-paren-style 'expression) |
| 151 | (overlay-put show-paren-overlay-1 'face face)) | 193 | (and (eq show-paren-style 'mixed) |
| 152 | ;; Otherwise, turn off any such highlighting. | 194 | (not (pos-visible-in-window-p pos)))) |
| 153 | (and show-paren-overlay-1 | 195 | (point) |
| 154 | (overlay-buffer show-paren-overlay-1) | 196 | pos)) |
| 155 | (delete-overlay show-paren-overlay-1))) | 197 | (from (if (or (eq show-paren-style 'expression) |
| 156 | ;; Turn on highlighting for the matching paren, if found. | 198 | (and (eq show-paren-style 'mixed) |
| 157 | ;; If it's an unmatched paren, turn off any such highlighting. | 199 | (not (pos-visible-in-window-p pos)))) |
| 158 | (or (and (not (integerp pos)) | 200 | pos |
| 159 | (delete-overlay show-paren-overlay)) | 201 | (save-excursion |
| 160 | (save-excursion | 202 | (goto-char pos) |
| 161 | (goto-char pos) | 203 | (forward-point (- dir)))))) |
| 162 | (if show-paren-overlay | 204 | (if show-paren-overlay |
| 163 | (move-overlay show-paren-overlay | 205 | (move-overlay show-paren-overlay from to (current-buffer)) |
| 164 | (forward-point (- dir)) | 206 | (setq show-paren-overlay (make-overlay from to)))) |
| 165 | pos | 207 | ;; |
| 166 | (current-buffer)) | 208 | ;; Always set the overlay face, since it varies. |
| 167 | (setq show-paren-overlay | 209 | (overlay-put show-paren-overlay 'face face))))) |
| 168 | (make-overlay (forward-point (- dir)) pos))))) | ||
| 169 | ;; Always set the overlay face, since it varies. | ||
| 170 | (overlay-put show-paren-overlay 'face face)) | ||
| 171 | (t | ||
| 172 | ;; If not at a paren that has a match, | ||
| 173 | ;; turn off any previous paren highlighting. | ||
| 174 | (and show-paren-overlay (overlay-buffer show-paren-overlay) | ||
| 175 | (delete-overlay show-paren-overlay)) | ||
| 176 | (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1) | ||
| 177 | (delete-overlay show-paren-overlay-1))))))) | ||
| 178 | 210 | ||
| 179 | (provide 'paren) | 211 | (provide 'paren) |
| 180 | 212 | ||