-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdwcB.el
More file actions
290 lines (256 loc) · 10.9 KB
/
dwcB.el
File metadata and controls
290 lines (256 loc) · 10.9 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
;;; Code:
(require 'cl)
;; temp
(add-to-list 'load-path "~/workspace/dwcB")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;; KEY GENERICS ;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Direction
(defconst dwcB-backward-key "j")
(defconst dwcB-forward-key "l")
(defconst dwcB-downward-key "k")
(defconst dwcB-upward-key "i")
;; Beginning/End
(defconst dwcB-beginning-key "u")
(defconst dwcB-end-key "o")
;; Search
(defconst dwcB-search-alpha-key "p")
(defconst dwcB-search-beta-key "y")
;; Bigger/Smaller
(defconst dwcB-bigger-key ">")
(defconst dwcB-smaller-key "<")
;; Add/Subtract
(defconst dwcB-shift-left-key "n") ;; I dont think this is needed
(defconst dwcB-shift-right-key "m") ;; ditto
;; Kill
(defconst dwcB-kill-element-key "d") ;; should be delete alpha
(defconst dwcB-kill-big-key "a") ;; should be delete beta
(defconst dwcB-kill-or-save-key "s")
(defconst dwcB-remove-key "z")
;; Create
(defconst dwcB-yank-key "f") ;; should be insert alpha
(defconst dwcB-insert-key "v") ;; shoulb be insert beta
(defconst dwcB-note-key "+")
;; Transpose
(defconst dwcB-transpose-key "t") ;; should be switch
;; Undo/Redo
(defconst dwcB-undo-key "r")
(defconst dwcB-redo-key "t")
;; Query
(defconst dwcB-query-key "?")
(defconst dwcB-description-key "/")
;; Compile/Interpret
(defconst dwcB-evaluate-key ":")
(defconst dwcB-compile-key ";")
;; Present
(defconst dwcB-present-key "/")
(defconst dwcB-enter-key "RET")
(defconst dwcB-edit-key "*")
(defconst dwcB-mark-key "&")
(defconst dwcB-sort-key "#")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;; MAPS/PREFIXES ;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar dwcB--global-map (make-sparse-keymap)
"alist for tracking dwcB versions of general keymaps")
(defvar dwcB--inter-buffer-map (make-sparse-keymap)
"Prefix map for window, buffer, and frame commands.")
(defconst dwcB-major-prefix "C-e"
"dwc-binding prefix used as default for major modes. Major mode commands may be
bound to keys outside of prefix (see dwcB-add-major-mode-map).")
(defconst dwcB-inter-buffer-prefix "C-w"
"dwc-binding prefix used as default for inter-buffer navigation and editing.")
(define-key dwcB--global-map (kbd dwcB-inter-buffer-prefix) dwcB--inter-buffer-map)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;; SAVES ;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar dwcB--primary-alist '()
"first searched when setting local-map. Then looks outside")
(defvar dwcB--minor-alist '())
(defvar dwcB--minor-alist--saved '() "list of keymaps to be appended to minor-mode-map-alist")
(defvar dwcB--primary-alist--saved '())
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;; DWCB ;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-minor-mode dwcB-mode
"Organized alternative bindings to Emacs defaults."
:lighter " dwcB"
:global t
(if dwcB-mode
(dwcB--setup)
(dwcB--teardown))
)
(defun dwcB--setup ()
(progn
(keyboard-translate ?\C-i ?\H-i) ; this is needed because emacs wont let you bind C-i naturally
(dwcB--set-global-map)
(add-hook 'after-change-major-mode-hook 'dwcB--major-mode-update)
(dwcB--major-mode-update)
;; added as workaround to helm's wild and whacky handing of keymaps. Oh well.
(add-hook 'post-command-hook 'dwcB--setup-minor-maps)
))
(defun dwcB--teardown ()
(progn
(keyboard-translate ?\C-i ?\C-i)
(dwcB--reset-global-map)
(remove-hook 'after-change-major-mode-hook 'dwcB--major-mode-update)
(dwcB--major-mode-clear)
(remove-hook 'post-command-hook 'dwcB--setup-minor-maps)
(dwcB--teardown-minor-maps)
))
(defun dwcB--set-global-map ()
"Set the current global map to dwcB global map."
(when (eq (current-global-map) global-map)
(use-global-map (make-composed-keymap dwcB--global-map global-map)))
)
(defun dwcB--reset-global-map ()
"Set the current global map to Emacs default, global-map."
(when (eq (current-global-map) dwcB--global-map)
(use-global-map global-map))
)
(defun dwcB--setup-minor-maps ()
(setq dwcB--minor-alist--saved minor-mode-overriding-map-alist)
(setq minor-mode-overriding-map-alist dwcB--minor-alist)
)
(defun dwcB--teardown-minor-maps ()
(setq minor-mode-overriding-map-alist dwcB--minor-alist--saved)
)
(defun dwcB--major-mode-update ()
"Switch to corresponding dwcB keymap for the current major mode."
(let ((mode-map (assoc major-mode dwcB--primary-alist)))
(when mode-map
(dwcB--save (cons major-mode (current-local-map)) 'dwcB--primary-alist--saved)
(use-local-map (cdr mode-map))
))
)
(defun dwcB--major-mode-clear ()
(let ((mode-map (assoc major-mode dwcB--primary-alist--saved)))
(when mode-map
(use-local-map (cdr mode-map))
)
)
)
(defun dwcB--save (MODE-MAP ALIST)
(let ((entry-mode-map (assoc (car MODE-MAP) (symbol-value ALIST))))
(if entry-mode-map
;; amend set value in place. Don't want this to just add to list.
;; this can always just go if it causes problems
;; (setf (cdr (assoc s(car MODE-MAP) ALIST))
;; (make-composed-keymap `(,(cdr MODE-MAP)
;; ,(cdr entry-mode-map)))
;; )
(add-to-list ALIST
(cons (car MODE-MAP)
(make-composed-keymap `(,(cdr MODE-MAP)
,(cdr entry-mode-map)))
))
(add-to-list ALIST MODE-MAP)
)
)
)
(defun read-binds (binding-config)
"Accepts a binding configuration and converts it into a key/command alist"
;; needs to handle the H-i problem (peculiarity with emacs key binding architecture leaves C-i broken)
;; HYDRA - needs to append C-modifier set into no-modifier set for hydra
(let ((modifiers '(("" no-modifier)
("C-" C-modifier)
("M-" M-modifier)
("C-S-" C-S-modifier)
("M-S-" M-S-modifier)
("C-M-" C-M-modifier)
("C-M-S-" C-M-S-modifier))))
(apply 'append
(mapcar (apply-partially 'apply
(lambda (modifier bindings)
"Add a modifier to all in a binding-list"
(mapcar (lambda (bind) (cons (concat modifier (car bind))
(cdr bind)))
bindings)))
(remove-if-not #'cdr (mapcar (lambda (modifier)
"Produce a modifier/binding-list association"
`(,(car modifier)
,(plist-get binding-config (cadr modifier))))
modifiers)
)))
)
)
(defun dwcB-configure (&rest args)
"Configures dwcB binding.
:key - A minor mode or major mode name to configure bindings to.
A keymap name to which to correlate a dwcB keymap. (useful
if you want to build map hierarchies using :parent).
Ommit to bind globally.
:base - A base map with which to compose the provided bindings.
:parent - A parent map from which the produces bindings should inherit.
:gen-binds - Bindings in the general namespace (i.e., not necessarily
under a prefix key)
:env-binds - Bindings under the major prefix
:wnd-binds - Bindings under the window prefix"
(let* ((key (plist-get args :key))
(base-map (plist-get args :base))
(parent-map (plist-get args :parent))
(clean-bind-conf (lambda (config)
"Read a binding config (normal and C-x subprefix) and return results"
`(,(read-binds (cdr config)) ,(read-binds (cdr config)))))
(gen-binds (funcall clean-bind-conf (plist-get args :gen-binds)))
(env-binds (funcall clean-bind-conf (plist-get args :env-binds)))
(wnd-binds (funcall clean-bind-conf (plist-get args :wnd-binds))))
(unless (symbolp key)
(error ":key must be a symbol"))
(unless (symbolp parent-map)
(error ":key must be a symbol"))
(unless (or (or gen-binds env-binds wnd-binds) (and key (or parent-map base-map)))
(error
"Must provide general (:gen-binds), environment (:env-binds) or
window (:wnd-binds) bindings."))
(let* ((dwcB-map (make-sparse-keymap))
(build-map (lambda (BINDS)
"Create a map from binding configuration BINDS and return it"
(let ((map (make-sparse-keymap)))
(mapc (lambda (binding)
(define-key map (kbd (car binding)) (cdr binding)))
BINDS)
map))))
;; Add major, window, and general binds into dwcB-map
(mapc (apply-partially 'apply (lambda (BINDS PREFIX)
"Add BINDS to dwcB-map. If PREFIX non-nil, add under PREFIX"
(when BINDS
(let* ((no-prefix-binds (car BINDS))
(ctrl-x-binds (cadr BINDS))
(map (funcall build-map no-prefix-binds)))
(define-key map (kbd "C-x") (funcall build-map ctrl-x-binds))
(if PREFIX
(define-key dwcB-map (kbd PREFIX) map)
(setq dwcB-map (make-composed-keymap dwcB-map map)))
))))
`((,gen-binds nil)
(,env-binds ,dwcB-major-prefix)
(,wnd-binds ,dwcB-inter-buffer-prefix)))
;; If a base map, add those binds to dwcB-map
(when base-map
(unless (keymapp base-map)
(error ":base must be a keymap"))
(setq dwcB-map (make-composed-keymap dwcB-map base-map))
)
;; Inherit dwcB-map from parent-map if it exists
(when parent-map
(if (and (boundp parent-map) (keymapp (symbol-value parent-map)))
(set-keymap-parent dwcB-map (symbol-value parent-map))
(let ((entry (assoc parent-map dwcB--primary-alist)))
(if entry
(set-keymap-parent dwcB-map (cdr entry))
(error "parent-map must be a keymap or a known dwcB key")))
))
;; Decide when this dwcB-map goes into effect (globally, during a certain mode, etc)
(if key
(let ((mode-map-assoc (cons key dwcB-map)))
(if (member key minor-mode-list)
(dwcB--save mode-map-assoc 'dwcB--minor-alist)
(dwcB--save mode-map-assoc 'dwcB--primary-alist)))
(setq dwcB--global-map (make-composed-keymap dwcB-map dwcB--global-map))
)
))
)
;(require 'default-bindings)
(provide 'dwcB)
;;; dwcB.el ends here