about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/evil-leader-20140606.1243/evil-leader.el
blob: 58f8ff27e3f6e2442e2a8ab703d34b51b571ff4b (plain) (blame)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
;;; evil-leader.el --- let there be <leader>

;; Copyright (C) 2011-2013 by Michael Markert
;; Author: Michael Markert <markert.michael@googlemail.com>
;; URL: http://github.com/cofi/evil-leader
;; Package-Version: 20140606.1243
;; Git-Repository: git://github.com/cofi/evil-leader.git
;; Created: 2011-09-13
;; Version: 0.4.3
;; Keywords: evil vim-emulation leader
;; Package-Requires: ((evil "0"))

;; This file is not part of GNU Emacs.

;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;; Known Bugs:
;; See http://github.com/cofi/evil-leader/issues

;; Install:
;; (require 'evil-leader)

;; Usage:
;;
;; (global-evil-leader-mode)
;;
;;    to enable `evil-leader' in every buffer where `evil' is enabled.
;;
;;    Note: You should enable `global-evil-leader-mode' before you enable
;;          `evil-mode', otherwise `evil-leader' won't be enabled in initial
;;          buffers (*scratch*, *Messages*, ...).
;;
;;    Use `evil-leader/set-key' to bind keys in the leader map.
;;    For example:
;;
;; (evil-leader/set-key "e" 'find-file)
;;
;;    You can also bind several keys at once:
;;
;; (evil-leader/set-key
;;   "e" 'find-file
;;   "b" 'switch-to-buffer
;;   "k" 'kill-buffer)
;;
;;    The key map can of course be filled in several places.
;;
;;    After you set up the key map you can access the bindings by pressing =<leader>=
;;    (default: \) and the key(s). E.g. \ e would call `find-file' to open a file.
;;
;;    If you wish to change so you can customize =evil-leader/leader= or call
;;    `evil-leader/set-leader', e.g. (evil-leader/set-leader ",") to change it to
;;    ",".
;;    The leader has to be readable by `read-kbd-macro', so using Space as a
;;    prefix key would be (evil-leader/set-leader "<SPC>").
;;
;;    Beginning with version 0.3 evil-leader has support for mode-local bindings:
;;
;; (evil-leader/set-key-for-mode 'emacs-lisp-mode "b" 'byte-compile-file)
;;
;;    Again, you can bind several keys at once.
;;
;;    A mode-local binding shadows a normal mode-independent binding.

;;; Code:

(require 'evil)

(defvar evil-leader--default-map (make-sparse-keymap)
  "Keymap used for mode-independent leader bindings.")

(defvar evil-leader--mode-maps nil
  "Alist of mode-local leader bindings, shadows mode-independent bindings.")

;;; customization
(defgroup evil-leader nil
  "<leader> support for evil."
  :group 'evil
  :prefix 'evil-leader/)

(defcustom evil-leader/leader "\\"
  "The <leader> key, used to access keys defined by `evil-leader/set-key' in normal and visual state.
Must be readable by `read-kbd-macro'. For example: \",\"."
  :type 'string
  :group 'evil-leader)

(defcustom evil-leader/non-normal-prefix "C-"
  "Prefix for leader-map in insert- and emacs-state.
`evil-leader/in-all-states' has to be non-nil for this to be set.
The combination has to be readable by `read-kbd-macro'."
  :type 'string
  :group 'evil-leader)

(defcustom evil-leader/no-prefix-mode-rx nil
  "List of regular expressions for mode names where `evil-leader/leader' is used regardless of the state.

If the current major mode is matched by one of the regular expressions
`evil-leader/leader' is installed in emacs/insert state without
the prefix additionally to the prefixed key.

`evil-leader/in-all-states' has to be non-nil for this setting to have any effect."
  :type 'list
  :group 'evil-leader)

(defcustom evil-leader/in-all-states nil
  "If is non-nil leader-map is accessible by <prefixed-leader> in emacs/insert state.

<prefixed-leader> is `evil-leader/non-normal-prefix' + `evil-leader/leader'"
  :type 'boolean
  :group 'evil-leader)

;;;###autoload
(define-minor-mode global-evil-leader-mode
  "Global minor mode for <leader> support."
  nil nil nil
  (if global-evil-leader-mode
      (add-hook 'evil-local-mode-hook #'evil-leader-mode t)
    (remove-hook 'evil-local-mode-hook #'evil-leader-mode t)))

;;;###autoload
(define-minor-mode evil-leader-mode
  "Minor mode to enable <leader> support."
  :init-value nil
  :keymap nil
  (let* ((prefixed (read-kbd-macro (concat evil-leader/non-normal-prefix evil-leader/leader)))
         (no-prefix (read-kbd-macro evil-leader/leader))
         (mode-map (cdr (assoc major-mode evil-leader--mode-maps)))
         (map (or mode-map evil-leader--default-map))
         (no-prefix-rx (if evil-leader/no-prefix-mode-rx
                           (mapconcat #'identity evil-leader/no-prefix-mode-rx "\\|")
                         nil)))
    (if evil-leader-mode
        (progn
          (evil-normalize-keymaps)
          (define-key evil-motion-state-local-map no-prefix map)
          (define-key evil-normal-state-local-map no-prefix map)
          (when evil-leader/in-all-states
            (define-key evil-emacs-state-local-map prefixed map)
            (define-key evil-insert-state-local-map prefixed map))
          (when (and no-prefix-rx (string-match-p no-prefix-rx (symbol-name major-mode)))
            (define-key evil-emacs-state-local-map no-prefix map)
            (define-key evil-insert-state-local-map no-prefix map)))
      (define-key evil-motion-state-local-map no-prefix nil)
      (define-key evil-normal-state-local-map no-prefix nil)
      (when evil-leader/in-all-states
        (define-key evil-emacs-state-local-map prefixed nil)
        (define-key evil-insert-state-local-map prefixed nil)
        (when (and no-prefix-rx (string-match-p no-prefix-rx (symbol-name major-mode)))
          (define-key evil-emacs-state-local-map no-prefix nil)
          (define-key evil-insert-state-local-map no-prefix nil))))))

(defun evil-leader/set-leader (key &optional prefix)
  "Set leader key to `key' and non-normal-prefix to `prefix' and remove old bindings.

Passing `nil' as `prefix' leaves prefix unchanged."
  (let ((global-on global-evil-leader-mode)
        (local-on evil-leader-mode))
    (when local-on
      (evil-leader-mode -1))
    (when global-on
      (global-evil-leader-mode -1))
    (setq evil-leader/leader key)
    (when prefix
      (setq evil-leader/non-normal-prefix prefix))
    (if global-on
        (global-evil-leader-mode 1)
      (when local-on
        (evil-leader-mode 1)))))

;;;###autoload
(defun evil-leader/set-key (key def &rest bindings)
  "Bind `key' to command `def' in `evil-leader/default-map'.

Key has to be readable by `read-kbd-macro' and `def' a command.
Accepts further `key' `def' pairs."
  (interactive "kKey: \naCommand: ")
  (evil-leader--def-keys evil-leader--default-map key def bindings))
(put 'evil-leader/set-key 'lisp-indent-function 'defun)

;;;###autoload
(defun evil-leader/set-key-for-mode (mode key def &rest bindings)
  "Create keybindings for major-mode `mode' with `key' bound to command `def'.

See `evil-leader/set-key'."
  (interactive "SMode: \nkKey: \naCommand: ")
  (let ((mode-map (cdr (assoc mode evil-leader--mode-maps))))
    (unless mode-map
      (setq mode-map (make-sparse-keymap))
      (set-keymap-parent mode-map evil-leader--default-map)
      (push (cons mode mode-map) evil-leader--mode-maps))
    (evil-leader--def-keys mode-map key def bindings)))
(put 'evil-leader/set-key-for-mode 'lisp-indent-function 'defun)

(defun evil-leader--def-keys (map key def bindings)
  (while key
    (define-key map (read-kbd-macro key) def)
    (setq key (pop bindings)
          def (pop bindings))))

(provide 'evil-leader)
;;; evil-leader.el ends here