Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve semantics of :body-pre hook #368

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions hydra-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,10 @@ _w_ Worf: % -8`hydra-tng/worf^^ _h_ Set phasers to
("1" find-file)
("q" nil))

(defhydra hydra-body-pre (global-map "C-c" :body-pre (insert ":pre:"))
("x" (insert "X"))
("q" nil))

(defun remapable-print ()
(interactive)
(insert "remapable print was called"))
Expand Down Expand Up @@ -1461,6 +1465,18 @@ _w_ Worf: % -8`hydra-tng/worf^^ _h_ Set phasers to
(kbd "C-c rq")))
"*remaped* print was called|")))

(ert-deftest hydra-body-pre ()
":body-pre callback must be called only once per hydra session"
(should (string= (hydra-with "|"
(execute-kbd-macro
(kbd "C-c xx")))
":pre:XX|"))
(should (string= (hydra-with "|"
(hydra-body-pre/body)
(execute-kbd-macro
(kbd "xx")))
":pre:XX|")))

(ert-deftest hydra-columns-1 ()
(should (equal (eval
(cadr
Expand Down
25 changes: 20 additions & 5 deletions hydra.el
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ warn: keep KEYMAP and issue a warning instead of running the command."
(t nil)))))
(hydra-disable))))

(defvar hydra--body-body-pre-called-p nil
"Guard for ensuring :body-pre callback is called only once.")

(defvar hydra--ignore nil
"When non-nil, don't call `hydra-curr-on-exit'.")

Expand All @@ -150,6 +153,7 @@ warn: keep KEYMAP and issue a warning instead of running the command."
(setq hydra-deactivate nil)
(remove-hook 'pre-command-hook 'hydra--clearfun)
(unless hydra--ignore
(setq hydra--body-body-pre-called-p nil)
(if (fboundp 'remove-function)
(remove-function input-method-function #'hydra--imf)
(when hydra--input-method-function
Expand Down Expand Up @@ -1410,10 +1414,19 @@ result of `defhydra'."
;; create defuns
,@(mapcar
(lambda (head)
(hydra--make-defun name body doc head keymap-name
body-pre
body-before-exit
body-after-exit))
;; Ensure that :body-pre is called once when hydra gets
;; activated by directly calling its head.
(let ((body-pre (if body-body-pre
`(progn
(unless hydra--body-body-pre-called-p
(setq hydra--body-body-pre-called-p t)
,body-body-pre)
,body-pre)
body-pre)))
(hydra--make-defun name body doc head keymap-name
body-pre
body-before-exit
body-after-exit)))
heads-nodup)
;; free up keymap prefix
,@(unless (or (null body-key)
Expand Down Expand Up @@ -1448,7 +1461,9 @@ result of `defhydra'."
name body doc '(nil body)
keymap-name
(or body-body-pre body-pre) body-before-exit
'(setq prefix-arg current-prefix-arg)))))
`(setq prefix-arg current-prefix-arg
,@(when body-body-pre
'(hydra--body-body-pre-called-p t)))))))
(error
(hydra--complain "Error in defhydra %S: %s" name (cdr err))
nil)))
Expand Down