Skip to content

Commit

Permalink
Merge pull request from wyuenho/patch-1
Browse files Browse the repository at this point in the history
GitHub-reference: jwiegley/use-package#846
  • Loading branch information
jwiegley committed Aug 5, 2020
2 parents b00b2ae + 4dfa484 commit 6fa72ab
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
13 changes: 8 additions & 5 deletions lisp/use-package/bind-key.el
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,13 @@ spelled-out keystrokes, e.g., \"C-c C-z\". See documentation of
COMMAND must be an interactive function or lambda form.
KEYMAP, if present, should be a keymap and not a quoted symbol.
KEYMAP, if present, should be a keymap variable or symbol.
For example:
(bind-key \"M-h\" #'some-interactive-function my-mode-map)
(bind-key \"M-h\" #'some-interactive-function 'my-mode-map)
If PREDICATE is non-nil, it is a form evaluated to determine when
a key should be bound. It must return non-nil in such cases.
Emacs can evaluate this form at any time that it does redisplay
Expand All @@ -171,10 +173,11 @@ can safely be called at any time."
`(let* ((,namevar ,key-name)
(,keyvar (if (vectorp ,namevar) ,namevar
(read-kbd-macro ,namevar)))
(kmap (if (and ,keymap (symbolp ,keymap)) (symbol-value ,keymap) ,keymap))
(,kdescvar (cons (if (stringp ,namevar) ,namevar
(key-description ,namevar))
(quote ,keymap)))
(,bindingvar (lookup-key (or ,keymap global-map) ,keyvar)))
(if (symbolp ,keymap) ,keymap (quote ,keymap))))
(,bindingvar (lookup-key (or kmap global-map) ,keyvar)))
(let ((entry (assoc ,kdescvar personal-keybindings))
(details (list ,command
(unless (numberp ,bindingvar)
Expand All @@ -183,11 +186,11 @@ can safely be called at any time."
(setcdr entry details)
(add-to-list 'personal-keybindings (cons ,kdescvar details))))
,(if predicate
`(define-key (or ,keymap global-map) ,keyvar
`(define-key (or kmap global-map) ,keyvar
'(menu-item "" nil :filter (lambda (&optional _)
(when ,predicate
,command))))
`(define-key (or ,keymap global-map) ,keyvar ,command)))))
`(define-key (or kmap global-map) ,keyvar ,command)))))

;;;###autoload
(defmacro unbind-key (key-name &optional keymap)
Expand Down
12 changes: 12 additions & 0 deletions test/lisp/use-package/use-package-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -1940,6 +1940,18 @@
(define-prefix-command 'my/map)
(bind-key "<f1>" 'my/map nil nil))))


(ert-deftest bind-key/845 ()
(defvar test-map (make-keymap))
(bind-key "<f1>" 'ignore 'test-map)
(should (eq (lookup-key test-map (kbd "<f1>")) 'ignore))
(let ((binding (cl-find "<f1>" personal-keybindings :test 'string= :key 'caar)))
(message "test-map %s" test-map)
(message "binding %s" binding)
(should (eq (cdar binding) 'test-map))
(should (eq (nth 1 binding) 'ignore))
(should (eq (nth 2 binding) nil))))

;; Local Variables:
;; indent-tabs-mode: nil
;; no-byte-compile: t
Expand Down

0 comments on commit 6fa72ab

Please sign in to comment.