[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 95008ea 30/36: hydra.el: Add automatic lookup for remaped
From: |
Oleh Krehel |
Subject: |
[elpa] master 95008ea 30/36: hydra.el: Add automatic lookup for remaped cmd |
Date: |
Sat, 22 Jul 2017 11:22:25 -0400 (EDT) |
branch: master
commit 95008ea38bc2b6420ba48d250b8a2bf6070a49e7
Author: David AMAR <address@hidden>
Commit: Oleh Krehel <address@hidden>
hydra.el: Add automatic lookup for remaped cmd
* hydra-test.el (hydra-remap-lookup-1):
(hydra-remap-lookup-2): New tests.
* hydra.el (hydra-look-for-remap): New defcustom.
(hydra--call-interactively-remap-maybe): New defun.
(hydra--call-interactively): Update.
Fixes #222
Fixes #221
---
hydra-test.el | 52 ++++++++++++++++++++++++++++++++++++++++------------
hydra.el | 20 ++++++++++++++++++--
2 files changed, 58 insertions(+), 14 deletions(-)
diff --git a/hydra-test.el b/hydra-test.el
index 5181cfb..8057d80 100644
--- a/hydra-test.el
+++ b/hydra-test.el
@@ -124,7 +124,7 @@ Call the head: `first-error'."
(progn
(setq this-command
(quote first-error))
- (call-interactively
+ (hydra--call-interactively-remap-maybe
(function first-error)))
((quit error)
(message
@@ -160,7 +160,7 @@ Call the head: `next-error'."
(progn
(setq this-command
(quote next-error))
- (call-interactively
+ (hydra--call-interactively-remap-maybe
(function next-error)))
((quit error)
(message
@@ -196,7 +196,7 @@ Call the head: `previous-error'."
(progn
(setq this-command
(quote previous-error))
- (call-interactively
+ (hydra--call-interactively-remap-maybe
(function previous-error)))
((quit error)
(message
@@ -340,7 +340,7 @@ Call the head: `toggle-truncate-lines'."
(progn
(setq this-command
(quote toggle-truncate-lines))
- (call-interactively
+ (hydra--call-interactively-remap-maybe
(function
toggle-truncate-lines))))
(defun hydra-toggle/auto-fill-mode-and-exit nil
@@ -362,7 +362,7 @@ Call the head: `auto-fill-mode'."
(progn
(setq this-command
(quote auto-fill-mode))
- (call-interactively
+ (hydra--call-interactively-remap-maybe
(function auto-fill-mode))))
(defun hydra-toggle/abbrev-mode-and-exit nil
"Create a hydra with no body and the heads:
@@ -383,7 +383,7 @@ Call the head: `abbrev-mode'."
(progn
(setq this-command
(quote abbrev-mode))
- (call-interactively
+ (hydra--call-interactively-remap-maybe
(function abbrev-mode))))
(defun hydra-toggle/nil nil
"Create a hydra with no body and the heads:
@@ -514,7 +514,7 @@ Call the head: `next-line'."
(progn
(setq this-command
(quote next-line))
- (call-interactively
+ (hydra--call-interactively-remap-maybe
(function next-line)))
((quit error)
(message
@@ -550,7 +550,7 @@ Call the head: `previous-line'."
(progn
(setq this-command
(quote previous-line))
- (call-interactively
+ (hydra--call-interactively-remap-maybe
(function previous-line)))
((quit error)
(message
@@ -690,7 +690,7 @@ Call the head: `(text-scale-set 0)'."
(setq hydra-curr-body-fn
(quote hydra-zoom/body)))
(condition-case err
- (call-interactively
+ (hydra--call-interactively-remap-maybe
(function
(lambda nil
(interactive)
@@ -723,7 +723,7 @@ Call the head: `(text-scale-set 0)'."
(hydra-keyboard-quit)
(setq hydra-curr-body-fn
(quote hydra-zoom/body))
- (call-interactively
+ (hydra--call-interactively-remap-maybe
(function
(lambda nil
(interactive)
@@ -836,7 +836,7 @@ Call the head: `(text-scale-set 0)'."
(setq hydra-curr-body-fn
(quote hydra-zoom/body)))
(condition-case err
- (call-interactively
+ (hydra--call-interactively-remap-maybe
(function
(lambda nil
(interactive)
@@ -869,7 +869,7 @@ Call the head: `(text-scale-set 0)'."
(hydra-keyboard-quit)
(setq hydra-curr-body-fn
(quote hydra-zoom/body))
- (call-interactively
+ (hydra--call-interactively-remap-maybe
(function
(lambda nil
(interactive)
@@ -1290,6 +1290,19 @@ _w_ Worf: % -8`hydra-tng/worf^^
_h_ Set phasers to
("1" find-file)
("q" nil))
+(defun remapable-print ()
+ (interactive)
+ (insert "remapable print was called"))
+(defun remaped-print ()
+ (interactive)
+ (insert "*remaped* print was called"))
+(define-key global-map (kbd "C-=") 'remapable-print)
+(define-key global-map [remap remapable-print] 'remaped-print)
+
+(defhydra hydra-simple-with-remap (global-map "C-c")
+ ("r" remapable-print)
+ ("q" nil))
+
(defmacro hydra-with (in &rest body)
`(let ((temp-buffer (generate-new-buffer " *temp*")))
(save-window-excursion
@@ -1346,6 +1359,21 @@ _w_ Worf: % -8`hydra-tng/worf^^
_h_ Set phasers to
(kbd "C-c g 1 RET q")))
"|foo\nbar")))
+(ert-deftest hydra-remap-lookup-1 ()
+ "try calling a remapped command while option is disabled "
+ (setq hydra-look-for-remap nil)
+ (should (string= (hydra-with "|"
+ (execute-kbd-macro
+ (kbd "C-c rq")))
+ "remapable print was called|")))
+(ert-deftest hydra-remap-lookup-2 ()
+ "try calling a remapped command while option is enabled"
+ (setq hydra-look-for-remap t)
+ (should (string= (hydra-with "|"
+ (execute-kbd-macro
+ (kbd "C-c rq")))
+ "*remaped* print was called|")))
+
(ert-deftest hydra-columns-1 ()
(should (equal (eval
(cadr
diff --git a/hydra.el b/hydra.el
index 992f9db..830ac5a 100644
--- a/hydra.el
+++ b/hydra.el
@@ -224,6 +224,12 @@ When nil, you can specify your own at each location like
this: _ 5a_."
"Default `format'-style specifier for ?a? syntax in docstrings."
:type 'string)
+(defcustom hydra-look-for-remap nil
+ "When non-nil and when calling a head with a simple command, hydra will
lookup
+for a potential remap command according to the current active keymap and call
it
+instead if found"
+ :type 'boolean)
+
(make-obsolete-variable
'hydra-key-format-spec
"Since the docstrings are aligned by hand anyway, this isn't very useful."
@@ -740,6 +746,16 @@ HEADS is a list of heads."
heads ",\n")
(format "The body can be accessed via `%S'." body-name)))
+(defun hydra--call-interactively-remap-maybe (cmd)
+ "`call-interactively' the given CMD or its remapped equivalent according to
+current active keymap if applicable and if `hydra-look-for-remap' is non nil"
+ (let ((remapped-cmd (if hydra-look-for-remap
+ (command-remapping `,cmd)
+ nil)))
+ (if remapped-cmd
+ (call-interactively `,remapped-cmd)
+ (call-interactively `,cmd))))
+
(defun hydra--call-interactively (cmd name)
"Generate a `call-interactively' statement for CMD.
Set `this-command' to NAME."
@@ -747,8 +763,8 @@ Set `this-command' to NAME."
(not (memq name '(nil body))))
`(progn
(setq this-command ',name)
- (call-interactively #',cmd))
- `(call-interactively #',cmd)))
+ (hydra--call-interactively-remap-maybe #',cmd))
+ `(hydra--call-interactively-remap-maybe #',cmd)))
(defun hydra--make-defun (name body doc head
keymap body-pre body-before-exit
- [elpa] master 5be27f4 02/36: Add a shortcut to supply a head's eval-able docstring, (continued)
- [elpa] master 5be27f4 02/36: Add a shortcut to supply a head's eval-able docstring, Oleh Krehel, 2017/07/22
- [elpa] master 585db09 17/36: hydra-examples.el: Add example of setting verbosity, Oleh Krehel, 2017/07/22
- [elpa] master 6d5bdf7 11/36: Introduce (:hint none), Oleh Krehel, 2017/07/22
- [elpa] master 76d51ec 23/36: Fix compile warnings, Oleh Krehel, 2017/07/22
- [elpa] master dd5f703 27/36: hydra.el: Use error-message-string, not message, Oleh Krehel, 2017/07/22
- [elpa] master d2aaf86 20/36: Fix e.g. _f_(foo) in format string, Oleh Krehel, 2017/07/22
- [elpa] master a72d68a 28/36: hydra.el (hydra-fontify-head-default): Fix head keys as "%f", Oleh Krehel, 2017/07/22
- [elpa] master 3527b32 24/36: Fix byte compiler warnings, Oleh Krehel, 2017/07/22
- [elpa] master 943636f 35/36: hydra.el: Bump version, Oleh Krehel, 2017/07/22
- [elpa] master 3db82e5 29/36: Implement named columns, Oleh Krehel, 2017/07/22
- [elpa] master 95008ea 30/36: hydra.el: Add automatic lookup for remaped cmd,
Oleh Krehel <=
- [elpa] master 32b8352 36/36: Merge commit '943636fe4a35298d9d234222bc4520dec9ef2305' from hydra, Oleh Krehel, 2017/07/22
- [elpa] master 91f8e7c 22/36: hydra.el: Bump version, Oleh Krehel, 2017/07/22
- [elpa] master a85a617 03/36: Allow to use e.g. "↑" in place of "<up>" in the docstring, Oleh Krehel, 2017/07/22
- [elpa] master 9c2589f 19/36: Update the package description, Oleh Krehel, 2017/07/22
- [elpa] master 63de503 21/36: hydra.el (hydra-key-regex): Add "$", Oleh Krehel, 2017/07/22
- [elpa] master 81d88e4 15/36: hydra.el (hydra-show-hint): Extract from defhydra, Oleh Krehel, 2017/07/22
- [elpa] master 1d378c6 16/36: Allow to set hydra verbosity, Oleh Krehel, 2017/07/22
- [elpa] master 2ebf862 25/36: Move Amaranth warning message to a defvar, Oleh Krehel, 2017/07/22
- [elpa] master a07b92a 26/36: lv.el (lv-window): Rename to " *LV*", Oleh Krehel, 2017/07/22
- [elpa] master 36fb5e0 32/36: hydra.el (defhydra): Use copy-tree on heads, Oleh Krehel, 2017/07/22