about summary refs log tree commit diff
path: root/emacs/.emacs.d/wpc/wpc-keybindings.el
blob: 2ff4fe375829ac68e17deed159ec3b9e76593ba7 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
;;; keybindings.el --- My Evil preferences -*- lexical-binding: t -*-
;; Author: William Carroll <wpcarro@gmail.com>

;;; Commentary:
;; This module hosts my Evil preferences
;;
;; Wish List:
;; - restore support for concise (n <kbd> <function>) instead of `general-mmap'
;; - restore support for `general-unbind'

;;; Code:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Dependencies
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'general)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Packages
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; This may be contraversial, but I never use the prefix key, and I'd prefer to
;; have to bound to the readline function that deletes the entire line.
(general-unbind "C-u")

(use-package evil
  :init
  ;; Should remove the warning messages on init.
  (setq evil-want-integration t)
  ;; TODO: Troubleshoot why this binding causes the following warning:
  ;; "Warning (evil-collection): `evil-want-keybinding' was set to nil but not
  ;; before loading evil."
  (setq evil-want-keybinding nil)
  (general-evil-setup)
  :config
  ;; Ensure that evil's command mode behaves with readline bindings.
  (general-define-key
   :keymaps 'evil-ex-completion-map
   "C-a" #'move-beginning-of-line
   "C-e" #'move-end-of-line
   "C-k" #'kill-line
   "C-u" #'evil-delete-whole-line
   "C-v" #'evil-paste-after
   "C-d" #'delete-char
   "C-f" #'forward-char
   "M-b" #'backward-word
   "M-f" #'forward-word
   "M-d" #'kill-word
   "M-DEL" #'backward-kill-word
   "C-b" #'backward-char)
  ;; TODO: Ensure all of my custom keybindings end up in a single map that is
  ;; easy to enable or disable.
  (general-mmap
    :keymaps 'override
    "RET" #'evil-goto-line
    "H"   #'evil-first-non-blank
    "L"   #'evil-end-of-line
    "_"   #'ranger
    "-"   #'dired-jump
    "sl"  #'wpc/evil-window-vsplit-right
    "sh"  #'evil-window-vsplit
    "sk"  #'evil-window-split
    "sj"  #'wpc/evil-window-split-down)
  (general-nmap
    :keymaps 'override
    "gd" #'xref-find-definitions
    ;; Wrapping `xref-find-references' in the `let' binding to prevent xref from
    ;; prompting.  There are other ways to handle this variable, such as setting
    ;; it globally with `setq' or buffer-locally with `setq-local'.  For now, I
    ;; prefer setting it with `let', which should bind it in the dynamic scope
    ;; for the duration of the `xref-find-references' function call.
    "gx" (lambda ()
           (interactive)
           (let ((xref-prompt-for-identifier nil))
             (call-interactively #'xref-find-references))))
  (general-unbind 'motion "M-." "C-p")
  (general-unbind 'normal "s"   "M-." "C-p" "C-n")
  (general-unbind 'insert "C-v" "C-d" "C-a" "C-e" "C-n" "C-p" "C-k")
  (setq evil-symbol-word-search t)
  (evil-mode 1))

;; TODO: Write `evil-collection' KBDs for `refine'.
;; evil keybindings
(use-package evil-collection
  :after (evil)
  :config
  (evil-collection-init))

;; `evil-collection' does not support `magit', and the preferred way to get evil
;; kbds for magit is with `evil-magit'.
(use-package evil-magit)

;; TODO: Consider moving this to another module.
(general-define-key
 :prefix "<SPC>"
 :states '(normal)
 "i" #'counsel-semantic-or-imenu
 "I" #'ibuffer
 "hk" #'helpful-callable
 "hf" #'helpful-function
 "hm" #'helpful-macro
 "hc" #'helpful-command
 "hk" #'helpful-key
 "hv" #'helpful-variable
 "hp" #'helpful-at-point
 "s" #'flyspell-mode
 "S" #'sort-lines
 "a" #'wpc-terminal/toggle
 "=" #'align
 "p" #'flycheck-previous-error
 "f" #'wpc/find-file
 "n" #'flycheck-next-error
 "N" #'smerge-next
 "W" #'balance-windows
 "gs" #'magit-status
 "E" #'refine
 "es" #'wpc/create-snippet
 ;; TODO: Replace with `macros/ilambda' when that is working again.
 "ev" (lambda () (interactive) (wpc/find-file-split "~/.config/nvim/init.vim"))
 "ee" (lambda () (interactive) (wpc/find-file-split "~/.emacs.d/init.el"))
 "ez" (lambda () (interactive) (wpc/find-file-split "~/.zshrc"))
 "ea" (lambda () (interactive) (wpc/find-file-split "~/aliases.zsh"))
 "ef" (lambda () (interactive) (wpc/find-file-split "~/functions.zsh"))
 "el" (lambda () (interactive) (wpc/find-file-split "~/variables.zsh"))
 "ex" (lambda () (interactive) (wpc/find-file-split "~/.Xresources"))
 "em" (lambda () (interactive) (wpc/find-file-split "~/.tmux.conf"))
 "l" #'locate
 "L" #'list-packages
 "B" #'magit-blame
 "w" #'save-buffer
 "r" #'wpc/evil-replace-under-point
 "R" #'deadgrep)

;; create comments easily
(use-package evil-commentary
  :after (evil)
  :config
  (evil-commentary-mode))

;; evil surround
(use-package evil-surround
  :after (evil)
  :config
  (global-evil-surround-mode 1))

;; I expect in insert mode:
;; C-a: beginning-of-line
;; C-e: end-of-line
;; C-b: backwards-char
;; C-f: forwards-char

;; TODO: Move these KBD constants to kbd.el.

(defconst wpc/up-kbds
  '("C-p" "C-k" "<backtab>" "<up>")
  "The keybindings that I expect to work for moving upwards in lists.")

(defconst wpc/down-kbds
  '("C-n" "C-j" "<tab>" "<down>")
  "The keybindings that I expect to work for moving downwards in lists.")

(defconst wpc/left-kbds
  '("C-b" "<left>")
  "The keybindings that I expect to move leftwards in insert-like modes.")

(defconst wpc/right-kbds
  '("C-f" "<right>")
  "The keybindings that I expect to move rightwards in insert-like modes.")

(defun wpc/ensure-kbds (_ignore)
  "Try to ensure that my keybindings retain priority over other minor modes."
  (unless (eq (caar minor-mode-map-alist) 'wpc/kbds-minor-mode)
    (let ((mykbds (assq 'wpc/kbds-minor-mode minor-mode-map-alist)))
      (assq-delete-all 'wpc/kbds-minor-mode minor-mode-map-alist)
      (add-to-list 'minor-mode-map-alist mykbds))))

;; Custom minor mode that ensures that my kbds are available no matter which
;; major or minor modes are active.
(add-hook 'after-load-functions #'wpc/ensure-kbds)

;; TODO: Prefer using general and 'override maps to implement this.
(defvar wpc/kbds
  (let ((map (make-sparse-keymap)))
    (bind-keys :map map
               ("M-q"            . delete-window)
               ("<s-return>"     . toggle-frame-fullscreen)
               ("M-h"            . windmove-left)
               ("M-l"            . windmove-right)
               ("M-k"            . windmove-up)
               ("M-j"            . windmove-down)
               ("M-q"            . delete-window))
    map)
  "William Carroll's keybindings that should have the highest precedence.")

;; Support pasting in M-:.
(general-define-key
 :keymaps 'read-expression-map
 "C-v"   #'clipboard-yank
 "C-S-v" #'clipboard-yank)

(define-minor-mode wpc/kbds-minor-mode
  "A minor mode so that my key settings override annoying major modes."
  :init-value t
  :lighter " wpc/kbds"
  :keymap wpc/kbds)

;; allow jk to escape
(use-package key-chord
  :after (evil)
  :config
  (key-chord-mode 1)
  (key-chord-define evil-insert-state-map "jk" 'evil-normal-state))

;; Ensure the Evil search results get centered vertically.
;; TODO: Consider packaging this up for others.
(progn
  (defadvice isearch-update
      (before advice-for-isearch-update activate)
    (evil-scroll-line-to-center (line-number-at-pos)))
  (defadvice evil-search-next
      (after advice-for-evil-search-next activate)
    (evil-scroll-line-to-center (line-number-at-pos)))
  (defadvice evil-search-previous
      (after advice-for-evil-search-previous activate)
    (evil-scroll-line-to-center (line-number-at-pos))))

(provide 'wpc-keybindings)
;;; wpc-keybindings.el ends here