Skip to content

Instantly share code, notes, and snippets.

@kaushalmodi kaushalmodi/test-hydra.el
Last active Aug 29, 2015

What would you like to do?
;; Time-stamp: <2015-01-20 13:10:26 kmodi>
;; Code to demonstrate that hydra is wiping out my predefined bindings to my
;; minor mode map
(require 'hydra)
(require 'bind-key)
(defvar xyz-mode-map (make-sparse-keymap)
"Keymap while xyz-mode is active.")
(defvar xyz/pseudo-map-prefix "C-M-o"
;; overrides the default binding to `split-line'
"Prefix key binding for a `pseudo' keymap.")
(define-minor-mode xyz-mode
"A minor mode so that my key settings override annoying major modes."
:lighter " xyz"
(defadvice load (after give-my-keybindings-priority)
"Try to ensure that my keybindings always have priority."
(if (not (eq (car (car minor-mode-map-alist)) 'xyz-mode))
(let ((mykeys (assq 'xyz-mode minor-mode-map-alist)))
(assq-delete-all 'xyz-mode minor-mode-map-alist)
(add-to-list 'minor-mode-map-alist mykeys))))
(ad-activate 'load)
(defun turn-on-xyz-mode ()
"Turns on xyz-mode."
(xyz-mode t))
(defun turn-off-xyz-mode ()
"Turns off xyz-mode."
(xyz-mode -1))
(define-globalized-minor-mode global-xyz-mode xyz-mode turn-on-xyz-mode)
;; Turn off the minor mode in the minibuffer
(add-hook 'minibuffer-setup-hook 'turn-off-xyz-mode)
;; ###autoload
;; (add-hook 'text-mode-hook 'xyz-mode)
(defmacro bind-to-xyz-map (key fn)
"Bind a function to the `xyz-mode-map'
USAGE: (bind-to-xyz-map \"f\" full-screen-center)
`(bind-key (concat ,xyz/pseudo-map-prefix " " ,key) ',fn xyz-mode-map))
(bind-to-xyz-map "p" previous-line)
(bind-to-xyz-map "n" next-line)
;; 1. First load this file without uncommenting below
;; 2. Do `C-h c C-M-o p`.. it will show that that is bound to `previous-line`
;; 3. Now uncomment and evaluate the below `hydra-create' call
;; 4. Do `C-h c C-M-o p`.. it will show that that is undefined!
;; (hydra-create "C-M-o"
;; '(("h" . hydra-move-splitter-left)
;; ("j" . hydra-move-splitter-down)
;; ("k" . hydra-move-splitter-up)
;; ("l" . hydra-move-splitter-right))
;; xyz-mode-map)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.