diff options
| author | Daniel Mendler | 2021-02-10 14:13:36 +0100 |
|---|---|---|
| committer | Daniel Mendler | 2021-02-10 15:34:12 +0100 |
| commit | 5ca7bc321d1684cf718c59088b785a5fc00fc276 (patch) | |
| tree | ea1363c97dfe81dce23507a47b9c72950fb4bd03 | |
| parent | ec750952f4cde13023554da3a94964f024ee4f71 (diff) | |
| download | emacs-5ca7bc321d1684cf718c59088b785a5fc00fc276.tar.gz emacs-5ca7bc321d1684cf718c59088b785a5fc00fc276.zip | |
unbind-key: Ensure that keys are removed from the keymap
* The removal from the keymap is performed by bind-key--remove
* Use the same argument normalization as bind-key
| -rw-r--r-- | lisp/use-package/bind-key.el | 52 |
1 files changed, 41 insertions, 11 deletions
diff --git a/lisp/use-package/bind-key.el b/lisp/use-package/bind-key.el index 13fd02ab89b..1d611c2933c 100644 --- a/lisp/use-package/bind-key.el +++ b/lisp/use-package/bind-key.el | |||
| @@ -199,17 +199,47 @@ can safely be called at any time." | |||
| 199 | (defmacro unbind-key (key-name &optional keymap) | 199 | (defmacro unbind-key (key-name &optional keymap) |
| 200 | "Unbind the given KEY-NAME, within the KEYMAP (if specified). | 200 | "Unbind the given KEY-NAME, within the KEYMAP (if specified). |
| 201 | See `bind-key' for more details." | 201 | See `bind-key' for more details." |
| 202 | `(progn | 202 | (let ((namevar (make-symbol "name")) |
| 203 | (bind-key ,key-name nil ,keymap) | 203 | (kdescvar (make-symbol "kdesc"))) |
| 204 | (setq personal-keybindings | 204 | `(let* ((,namevar ,key-name) |
| 205 | (cl-delete-if #'(lambda (k) | 205 | (,kdescvar (cons (if (stringp ,namevar) ,namevar |
| 206 | ,(if keymap | 206 | (key-description ,namevar)) |
| 207 | `(and (consp (car k)) | 207 | (if (symbolp ,keymap) ,keymap (quote ,keymap))))) |
| 208 | (string= (caar k) ,key-name) | 208 | (bind-key--remove (if (vectorp ,namevar) ,namevar |
| 209 | (eq (cdar k) ',keymap)) | 209 | (read-kbd-macro ,namevar)) |
| 210 | `(and (stringp (car k)) | 210 | (or (if (and ,keymap (symbolp ,keymap)) |
| 211 | (string= (car k) ,key-name)))) | 211 | (symbol-value ,keymap) ,keymap) |
| 212 | personal-keybindings)))) | 212 | global-map)) |
| 213 | (setq personal-keybindings | ||
| 214 | (cl-delete-if (lambda (k) (equal (car k) ,kdescvar)) | ||
| 215 | personal-keybindings)) | ||
| 216 | nil))) | ||
| 217 | |||
| 218 | (defun bind-key--remove (key keymap) | ||
| 219 | "Remove KEY from KEYMAP. | ||
| 220 | |||
| 221 | In contrast to `define-key', this function removes the binding from the keymap." | ||
| 222 | (define-key keymap key nil) | ||
| 223 | ;; Split M-key in ESC key | ||
| 224 | (setq key (mapcan (lambda (k) | ||
| 225 | (if (and (integerp k) (/= (logand k ?\M-\0) 0)) | ||
| 226 | (list ?\e (logxor k ?\M-\0)) | ||
| 227 | (list k))) | ||
| 228 | key)) | ||
| 229 | ;; Delete single keys directly | ||
| 230 | (if (= (length key) 1) | ||
| 231 | (delete key keymap) | ||
| 232 | ;; Lookup submap and delete key from there | ||
| 233 | (let* ((prefix (vconcat (butlast key))) | ||
| 234 | (submap (lookup-key keymap prefix))) | ||
| 235 | (unless (keymapp submap) | ||
| 236 | (error "Not a keymap for %s" key)) | ||
| 237 | (when (symbolp submap) | ||
| 238 | (setq submap (symbol-function submap))) | ||
| 239 | (delete (last key) submap) | ||
| 240 | ;; Delete submap if it is empty | ||
| 241 | (when (= 1 (length submap)) | ||
| 242 | (bind-key--remove prefix keymap))))) | ||
| 213 | 243 | ||
| 214 | ;;;###autoload | 244 | ;;;###autoload |
| 215 | (defmacro bind-key* (key-name command &optional predicate) | 245 | (defmacro bind-key* (key-name command &optional predicate) |