[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master 11323ec 06/27: Allow to use minor-mode-maps and more
From: |
Oleh Krehel |
Subject: |
[elpa] master 11323ec 06/27: Allow to use minor-mode-maps and more |
Date: |
Sat, 24 Jan 2015 20:36:30 +0000 |
branch: master
commit 11323ece73ecbf9e7fea3e20a26d3c0aeb41e01f
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>
Allow to use minor-mode-maps and more
* hydra.el (hydra-create): Add a third optional argument. When it's not
supplied, the behavior should remain the same. Otherwise, it's a
lambda that's used instead of `global-set-key', with the same semantics.
* README.md: Update.
re #1
---
README.md | 29 +++++++++++++++++++++++++++++
hydra.el | 26 ++++++++++++++++++++------
2 files changed, 49 insertions(+), 6 deletions(-)
diff --git a/README.md b/README.md
index 3e76618..928983f 100644
--- a/README.md
+++ b/README.md
@@ -26,3 +26,32 @@ You can expand the examples in-place, it still looks elegant:
See the [introductory blog
post](http://oremacs.com/2015/01/20/introducing-hydra/) for more information.
![hydra](http://oremacs.com/download/Hydra.png)
+
+## Using Hydra to define bindings other than global ones
+
+You can use the third optional argument of `hydra-create` for this (it
defaults to `global-set-key`).
+Here's an example:
+
+```cl
+(hydra-create "z"
+ '(("l" . forward-char)
+ ("h" . backward-char)
+ ("j" . next-line)
+ ("k" . previous-line))
+ (lambda (key command)
+ (define-key lispy-mode-map key command)))
+```
+
+For this simple case, there's even a shortcut: if you give a keymap as the
third argument,
+the lambda will be generated for you:
+
+```cl
+(hydra-create "z"
+ '(("l" . forward-char)
+ ("h" . backward-char)
+ ("j" . next-line)
+ ("k" . previous-line))
+ lispy-mode-map)
+```
+
+
diff --git a/hydra.el b/hydra.el
index d2886df..9479817 100644
--- a/hydra.el
+++ b/hydra.el
@@ -51,15 +51,21 @@
(require 'cl-lib)
;;;###autoload
-(defmacro hydra-create (body heads)
- "Create a hydra with a BODY prefix and HEADS.
+(defmacro hydra-create (body heads &optional method)
+ "Create a hydra with a BODY prefix and HEADS with METHOD.
This will result in `global-set-key' statements with the keys
being the concatenation of BODY and each head in HEADS. HEADS is
an alist of (KEY . FUNCTION).
After one of the HEADS is called via BODY+KEY, it and the other
HEADS can be called with only KEY (no need for BODY). This state
-is broken once any key binding that is not in HEADS is called."
+is broken once any key binding that is not in HEADS is called.
+
+METHOD is a lambda takes two arguments: a KEY and a COMMAND.
+It defaults to `global-set-key'.
+When `(keymapp METHOD)`, it becomes:
+
+ (lambda (key command) (define-key METHOD key command))"
(declare (indent 1))
(let* ((keymap (make-sparse-keymap))
(heads (eval heads))
@@ -67,9 +73,17 @@ is broken once any key binding that is not in HEADS is
called."
(lambda (x)
(define-key keymap (car x)
(intern (format "hydra-%s-%S" body (cdr x)))))
- heads)))
+ heads))
+ (method (cond ((null method)
+ 'global-set-key)
+
+ ((keymapp (eval method))
+ `(lambda (key command) (define-key ,method key
command)))
+
+ (t
+ method))))
`(progn
- (global-set-key ,(kbd body) nil)
+ (,method ,(kbd body) nil)
,@(cl-mapcar
(lambda (head name)
`(defun ,name ()
@@ -91,7 +105,7 @@ Call the head: `%S'."
heads names)
,@(cl-mapcar
(lambda (head name)
- `(global-set-key ,(kbd (concat body " " (car head))) #',name))
+ `(,method ,(kbd (concat body " " (car head))) #',name))
heads names))))
(provide 'hydra)
- [elpa] master updated (c7f675e -> 7b78b37), Oleh Krehel, 2015/01/24
- [elpa] master 916a130 05/27: Depend on cl-lib v0.5 for `cl-mapcar', Oleh Krehel, 2015/01/24
- [elpa] master a179f31 04/27: Unbind hydra's body unconditionally to avoid error, Oleh Krehel, 2015/01/24
- [elpa] master abef4bf 02/27: Split away the examples, Oleh Krehel, 2015/01/24
- [elpa] master 2a367fa 03/27: README.md: update doc, Oleh Krehel, 2015/01/24
- [elpa] master f55c1ff 07/27: Add `hydra-is-helpful' custom var, Oleh Krehel, 2015/01/24
- [elpa] master 9b86656 01/27: Initial import, Oleh Krehel, 2015/01/24
- [elpa] master 6966e14 11/27: Fix the bugged `global-set-key' version, Oleh Krehel, 2015/01/24
- [elpa] master 11323ec 06/27: Allow to use minor-mode-maps and more,
Oleh Krehel <=
- [elpa] master dbd77f0 09/27: Don't unbind hydra's body, unless using global-set-key, Oleh Krehel, 2015/01/24
- [elpa] master 998b533 10/27: Make sure `windmove-wrap-around' is nil during tests, Oleh Krehel, 2015/01/24
- [elpa] master 5318fc3 08/27: Update `hydra-create' format, Oleh Krehel, 2015/01/24
- [elpa] master e156bf2 12/27: Require cl at compile time, Oleh Krehel, 2015/01/24
- [elpa] master 69bb4a4 13/27: Fix `kbd' interpretting "<tab>" char-by-char, Oleh Krehel, 2015/01/24
- [elpa] master b982fe5 14/27: Allow to add one head that disables the Hydra, Oleh Krehel, 2015/01/24
- [elpa] master c3ce0a9 15/27: `hydra-create' will also define `hydra-...-body', Oleh Krehel, 2015/01/24
- [elpa] master 8884698 17/27: Don't undefine BODY's map binding when it's a prefix, Oleh Krehel, 2015/01/24
- [elpa] master dfe1273 19/27: Simplify and improve the generated defuns, Oleh Krehel, 2015/01/24
- [elpa] master 301b500 16/27: Don't undefine BODY's global binding when it's a prefix, Oleh Krehel, 2015/01/24