summary refs log tree commit diff stats
path: root/lisp/+key.el
blob: 7a51be1f4b733057476a8bd070a44eaeeed473cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
;;; +key.el --- minor mode for keymaps -*- lexical-binding: t; -*-

;;; Commentary:

;; Much of the code here was cribbed from https://emacs.stackexchange.com/a/358,
;; which in turn was cribbed in part from
;; https://github.com/kaushalmodi/.emacs.d/blob/master/elisp/modi-mode.el,
;; https://github.com/jwiegley/use-package/blob/master/bind-key.el and
;; elsewhere.

;; The basic idea is to have a minor-mode for my personal key customizations,
;; especially a "leader key" set up à la vim.  In Emacs, I use `C-z' for this
;; leader key, because of its easy location and relative uselessness by default.

;;; Code:

(require 'easy-mmode)
(require 'setup nil t)

;; I need to define this map before the proper mode map.
(defvar +key-leader-map (let ((map (make-sparse-keymap))
                               (c-z (global-key-binding "\C-z")))
                           (define-key map "\C-z" c-z)
                           map)
  "A leader keymap under the \"C-z\" bind.")

(defvar +key-mode-map (let ((map (make-sparse-keymap)))
                         (define-key map "\C-z" +key-leader-map)
                         map)
  "Keymap for `+key-mode'.")

;;;###autoload
(define-minor-mode +key-mode
  "A minor mode with keybindings that will override every other mode."
  :init-value t
  :lighter " +")
(add-to-list 'emulation-mode-map-alists `((+key-mode . ,+key-mode-map)))

;;;###autoload
(define-globalized-minor-mode +key-global-mode +key-mode +key-mode)

;;;###autoload
(defun +key-setup ()
  "Setup `+key-mode' after everything else."
  (if after-init-time
      (+key-global-mode +1)
    (add-hook 'after-init-hook '+key-global-mode)))

(defun turn-off-+key-mode ()
  "Turn off `+key-mode'."
  (+key-mode -1))
(add-hook 'minibuffer-setup-hook 'turn-off-+key-mode)

;; Extras for `setup'
(with-eval-after-load 'setup
  (setup-define :+key
    (lambda (key command)
      `(define-key +key-mode-map ,key ,command))
    :documentation "Bind KEY to COMMAND in `+key-mode-map'."
    :debug '(form sexp)
    :ensure '(kbd func)
    :repeatable t)
  
  (setup-define :+leader
    (lambda (key command)
      `(define-key +key-leader-map ,key ,command))
    :documentation "Bind KEY to COMMAND in `+key-leader-map'."
    :debug '(form sexp)
    :ensure '(kbd func)
    :repeatable t))

(provide '+key)
;;; +key.el ends here