diff options
| author | Tassilo Horn | 2015-09-29 21:34:18 +0200 |
|---|---|---|
| committer | Tassilo Horn | 2015-09-30 12:30:45 +0200 |
| commit | e73b0d6f03ffdbc1ec40636c662f010083af0b98 (patch) | |
| tree | 5fc55fae55e25d7b9316b3b00f861d29196edde0 | |
| parent | ecedfd66fc18e9db206c9f9a49fd5226561f94a9 (diff) | |
| download | emacs-e73b0d6f03ffdbc1ec40636c662f010083af0b98.tar.gz emacs-e73b0d6f03ffdbc1ec40636c662f010083af0b98.zip | |
Implement unprettification of symbol at point
* lisp/progmodes/prog-mode.el: Implement feature for unprettifying the
symbol at point.
(prettify-symbols--current-symbol-bounds): New variable.
(prettify-symbols--post-command-hook): New function.
(prettify-symbols-unprettify-at-point): New defcustom.
(prettify-symbols-mode): Use it.
(prettify-symbols--compose-symbol): Use them.
| -rw-r--r-- | lisp/progmodes/prog-mode.el | 43 |
1 files changed, 39 insertions, 4 deletions
diff --git a/lisp/progmodes/prog-mode.el b/lisp/progmodes/prog-mode.el index f1aa35f2871..b4085093c1b 100644 --- a/lisp/progmodes/prog-mode.el +++ b/lisp/progmodes/prog-mode.el | |||
| @@ -29,7 +29,8 @@ | |||
| 29 | 29 | ||
| 30 | ;;; Code: | 30 | ;;; Code: |
| 31 | 31 | ||
| 32 | (eval-when-compile (require 'cl-lib)) | 32 | (eval-when-compile (require 'cl-lib) |
| 33 | (require 'subr-x)) | ||
| 33 | 34 | ||
| 34 | (defgroup prog-mode nil | 35 | (defgroup prog-mode nil |
| 35 | "Generic programming mode, from which others derive." | 36 | "Generic programming mode, from which others derive." |
| @@ -161,13 +162,20 @@ Regexp match data 0 points to the chars." | |||
| 161 | (let ((start (match-beginning 0)) | 162 | (let ((start (match-beginning 0)) |
| 162 | (end (match-end 0)) | 163 | (end (match-end 0)) |
| 163 | (match (match-string 0))) | 164 | (match (match-string 0))) |
| 164 | (if (funcall prettify-symbols-compose-predicate start end match) | 165 | (if (and (not (equal prettify-symbols--current-symbol-bounds (list start end))) |
| 166 | (funcall prettify-symbols-compose-predicate start end match)) | ||
| 165 | ;; That's a symbol alright, so add the composition. | 167 | ;; That's a symbol alright, so add the composition. |
| 166 | (compose-region start end (cdr (assoc match alist))) | 168 | (progn |
| 169 | (compose-region start end (cdr (assoc match alist))) | ||
| 170 | (add-text-properties | ||
| 171 | start end | ||
| 172 | `(prettify-symbols-start ,start prettify-symbols-end ,end))) | ||
| 167 | ;; No composition for you. Let's actually remove any | 173 | ;; No composition for you. Let's actually remove any |
| 168 | ;; composition we may have added earlier and which is now | 174 | ;; composition we may have added earlier and which is now |
| 169 | ;; incorrect. | 175 | ;; incorrect. |
| 170 | (remove-text-properties start end '(composition)))) | 176 | (remove-text-properties start end '(composition |
| 177 | prettify-symbols-start | ||
| 178 | prettify-symbols-end)))) | ||
| 171 | ;; Return nil because we're not adding any face property. | 179 | ;; Return nil because we're not adding any face property. |
| 172 | nil) | 180 | nil) |
| 173 | 181 | ||
| @@ -179,6 +187,29 @@ Regexp match data 0 points to the chars." | |||
| 179 | 187 | ||
| 180 | (defvar-local prettify-symbols--keywords nil) | 188 | (defvar-local prettify-symbols--keywords nil) |
| 181 | 189 | ||
| 190 | (defvar-local prettify-symbols--current-symbol-bounds nil) | ||
| 191 | |||
| 192 | (defun prettify-symbols--post-command-hook () | ||
| 193 | (if-let ((c (get-text-property (point) 'composition)) | ||
| 194 | (s (get-text-property (point) 'prettify-symbols-start)) | ||
| 195 | (e (get-text-property (point) 'prettify-symbols-end))) | ||
| 196 | (progn | ||
| 197 | (setq prettify-symbols--current-symbol-bounds (list s e)) | ||
| 198 | (remove-text-properties s e '(composition))) | ||
| 199 | (when (and prettify-symbols--current-symbol-bounds | ||
| 200 | (or (< (point) (car prettify-symbols--current-symbol-bounds)) | ||
| 201 | (>= (point) (cadr prettify-symbols--current-symbol-bounds)))) | ||
| 202 | (apply #'font-lock-flush prettify-symbols--current-symbol-bounds) | ||
| 203 | (setq prettify-symbols--current-symbol-bounds nil)))) | ||
| 204 | |||
| 205 | (defcustom prettify-symbols-unprettify-at-point t | ||
| 206 | "If non-nil, show the non-prettified version of a symbol when point is on it. | ||
| 207 | The prettification will be reapplied as soon as point moves away | ||
| 208 | from the symbol. If set to nil, the prettification persists even | ||
| 209 | when point is on the symbol." | ||
| 210 | :type 'boolean | ||
| 211 | :group 'prog-mode) | ||
| 212 | |||
| 182 | ;;;###autoload | 213 | ;;;###autoload |
| 183 | (define-minor-mode prettify-symbols-mode | 214 | (define-minor-mode prettify-symbols-mode |
| 184 | "Toggle Prettify Symbols mode. | 215 | "Toggle Prettify Symbols mode. |
| @@ -206,8 +237,12 @@ support it." | |||
| 206 | (font-lock-add-keywords nil prettify-symbols--keywords) | 237 | (font-lock-add-keywords nil prettify-symbols--keywords) |
| 207 | (setq-local font-lock-extra-managed-props | 238 | (setq-local font-lock-extra-managed-props |
| 208 | (cons 'composition font-lock-extra-managed-props)) | 239 | (cons 'composition font-lock-extra-managed-props)) |
| 240 | (when prettify-symbols-unprettify-at-point | ||
| 241 | (add-hook 'post-command-hook | ||
| 242 | #'prettify-symbols--post-command-hook nil t)) | ||
| 209 | (font-lock-flush)) | 243 | (font-lock-flush)) |
| 210 | ;; Turn off | 244 | ;; Turn off |
| 245 | (remove-hook 'post-command-hook #'prettify-symbols--post-command-hook t) | ||
| 211 | (when prettify-symbols--keywords | 246 | (when prettify-symbols--keywords |
| 212 | (font-lock-remove-keywords nil prettify-symbols--keywords) | 247 | (font-lock-remove-keywords nil prettify-symbols--keywords) |
| 213 | (setq prettify-symbols--keywords nil)) | 248 | (setq prettify-symbols--keywords nil)) |