forked from syl20bnr/spacemacs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore-keybindings.el
170 lines (150 loc) · 7.27 KB
/
core-keybindings.el
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
;;; core-keybindings.el --- Spacemacs Core File
;;
;; Copyright (c) 2012-2020 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <[email protected]>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(require 'core-funcs)
(defvar spacemacs/prefix-titles nil
"alist for mapping command prefixes to long names.")
(defvar spacemacs-default-map (make-sparse-keymap)
"Base keymap for all spacemacs leader key commands.")
(defun spacemacs/translate-C-i (_)
"If `dotspacemacs-distinguish-gui-tab' is non nil, the raw key
sequence does not include <tab> or <kp-tab>, and we are in the
gui, translate to [C-i]. Otherwise, [9] (TAB)."
(interactive)
(if (and (not (cl-position 'tab (this-single-command-raw-keys)))
(not (cl-position 'kp-tab (this-single-command-raw-keys)))
dotspacemacs-distinguish-gui-tab
(display-graphic-p))
[C-i] [?\C-i]))
(define-key key-translation-map [?\C-i] 'spacemacs/translate-C-i)
;; (defun spacemacs/translate-C-m (_)
;; "If `dotspacemacs-distinguish-gui-ret' is non nil, the raw key
;; sequence does not include <ret>, and we are in the gui, translate
;; to [C-m]. Otherwise, [9] (TAB)."
;; (interactive)
;; (if (and
;; (not (cl-position 'return (this-single-command-raw-keys)))
;; (not (cl-position 'kp-enter (this-single-command-raw-keys)))
;; dotspacemacs-distinguish-gui-ret
;; (display-graphic-p))
;; [C-m] [?\C-m]))
;; (define-key key-translation-map [?\C-m] 'spacemacs/translate-C-m)
(defun spacemacs/declare-prefix (prefix name &optional _)
"Declare a prefix PREFIX. PREFIX is a string describing a key
sequence. NAME is a string used as the prefix command."
(which-key-add-keymap-based-replacements spacemacs-default-map
prefix name))
(put 'spacemacs/declare-prefix 'lisp-indent-function 'defun)
(defun spacemacs/declare-prefix-for-mode (mode prefix name &optional _)
"Declare a prefix PREFIX. MODE is the mode in which this prefix command should
be added. PREFIX is a string describing a key sequence. NAME is a symbol name
used as the prefix command."
(let* ((is-major-mode-prefix (string-prefix-p "m" prefix))
(is-minor-mode-prefix (not is-major-mode-prefix))
(smap (intern (format "spacemacs-%s-map" mode))))
(when (spacemacs//init-leader-mode-map mode smap is-minor-mode-prefix)
(which-key-add-keymap-based-replacements (symbol-value smap)
(if is-major-mode-prefix (substring prefix 1) prefix) name))))
(put 'spacemacs/declare-prefix-for-mode 'lisp-indent-function 'defun)
(defun spacemacs/set-leader-keys (key def &rest bindings)
"Add KEY and DEF as key bindings under
`dotspacemacs-leader-key' and `dotspacemacs-emacs-leader-key'.
KEY should be a string suitable for passing to `kbd', and it
should not include the leaders. DEF is most likely a quoted
command. See `define-key' for more information about the possible
choices for DEF. This function simply uses `define-key' to add
the bindings.
For convenience, this function will accept additional KEY DEF
pairs. For example,
\(spacemacs/set-leader-keys
\"a\" 'command1
\"C-c\" 'command2
\"bb\" 'command3\)"
(while key
(define-key spacemacs-default-map (kbd key) def)
(setq key (pop bindings) def (pop bindings))))
(put 'spacemacs/set-leader-keys 'lisp-indent-function 'defun)
(defalias 'evil-leader/set-key 'spacemacs/set-leader-keys)
(defun spacemacs//acceptable-leader-p (key)
"Return t if key is a string and non-empty."
(and (stringp key) (not (string= key ""))))
(defun spacemacs//init-leader-mode-map (mode map &optional minor)
"Check for MAP-prefix. If it doesn't exist yet, use `bind-map'
to create it and bind it to `dotspacemacs-major-mode-leader-key'
and `dotspacemacs-major-mode-emacs-leader-key'. If MODE is a
minor-mode, the third argument should be non nil."
(let* ((prefix (intern (format "%s-prefix" map)))
(leader1 (when (spacemacs//acceptable-leader-p
dotspacemacs-major-mode-leader-key)
dotspacemacs-major-mode-leader-key))
(leader2 (when (spacemacs//acceptable-leader-p
dotspacemacs-leader-key)
(concat dotspacemacs-leader-key " m")))
(emacs-leader1 (when (spacemacs//acceptable-leader-p
dotspacemacs-major-mode-emacs-leader-key)
dotspacemacs-major-mode-emacs-leader-key))
(emacs-leader2 (when (spacemacs//acceptable-leader-p
dotspacemacs-emacs-leader-key)
(concat dotspacemacs-emacs-leader-key " m")))
(leaders (delq nil (list leader1 leader2)))
(emacs-leaders (delq nil (list emacs-leader1 emacs-leader2))))
(or (boundp prefix)
(progn
(eval
`(bind-map ,map
:prefix-cmd ,prefix
,(if minor :minor-modes :major-modes) (,mode)
:keys ,emacs-leaders
:evil-keys ,leaders
:evil-states (normal motion visual evilified)))
(boundp prefix)))))
(defun spacemacs/set-leader-keys-for-major-mode (mode key def &rest bindings)
"Add KEY and DEF as key bindings under
`dotspacemacs-major-mode-leader-key' and
`dotspacemacs-major-mode-emacs-leader-key' for the major-mode
MODE. MODE should be a quoted symbol corresponding to a valid
major mode. The rest of the arguments are treated exactly like
they are in `spacemacs/set-leader-keys'."
(let* ((map (intern (format "spacemacs-%s-map" mode))))
(when (spacemacs//init-leader-mode-map mode map)
(while key
(define-key (symbol-value map) (kbd key) def)
(setq key (pop bindings) def (pop bindings))))))
(put 'spacemacs/set-leader-keys-for-major-mode 'lisp-indent-function 'defun)
(defalias
'evil-leader/set-key-for-mode
'spacemacs/set-leader-keys-for-major-mode)
(defun spacemacs/set-leader-keys-for-minor-mode (mode key def &rest bindings)
"Add KEY and DEF as key bindings under
`dotspacemacs-major-mode-leader-key' and
`dotspacemacs-major-mode-emacs-leader-key' for the minor-mode
MODE. MODE should be a quoted symbol corresponding to a valid
minor mode. The rest of the arguments are treated exactly like
they are in `spacemacs/set-leader-keys'. If DEF is string, then
it is treated as a prefix not a command."
(let* ((map (intern (format "spacemacs-%s-map" mode))))
(when (spacemacs//init-leader-mode-map mode map t)
(let ((map-value (symbol-value map)))
(while key
(if (stringp def)
(which-key-add-keymap-based-replacements map-value key def)
(define-key map-value (kbd key) def))
(setq key (pop bindings) def (pop bindings)))))))
(put 'spacemacs/set-leader-keys-for-minor-mode 'lisp-indent-function 'defun)
(defun spacemacs/declare-prefix-for-minor-mode (mode prefix name)
"Declare a prefix PREFIX. MODE is the mode in which this prefix command should
be added. PREFIX is a string describing a key sequence. NAME is a symbol name
used as the prefix command.
Example:
\(spacemacs/declare-prefix-for-minor-mode 'tide-mode \"E\" \"errors\"\)"
(let* ((map (intern (format "spacemacs-%s-map" mode))))
(when (spacemacs//init-leader-mode-map mode map t)
(which-key-add-keymap-based-replacements (symbol-value map) prefix name))))
(provide 'core-keybindings)