forked from emacsorphanage/god-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgod-mode.el
285 lines (243 loc) · 8.91 KB
/
god-mode.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
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
;;; god-mode.el --- God-like command entering minor mode
;; Copyright (C) 2013 Chris Done
;; Copyright (C) 2013 Magnar Sveen
;; Copyright (C) 2013 Rüdiger Sonderfeld
;; Copyright (C) 2013 Dillon Kearns
;; Copyright (C) 2013 Fabián Ezequiel Gallina
;; Author: Chris Done <[email protected]>
;; URL: https://github.com/chrisdone/god-mode
;; Version: 2.13.0
;; This file is not part of GNU Emacs.
;; This file 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, or (at your option)
;; any later version.
;; This file 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 GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; See README.md.
;;; Code:
(require 'cl-lib)
(add-hook 'after-change-major-mode-hook 'god-mode-maybe-activate)
(defvar god-local-mode-paused nil)
(make-variable-buffer-local 'god-local-mode-paused)
(defcustom god-mod-alist
'((nil . "C-")
("g" . "M-")
("G" . "C-M-"))
"List of keys and their associated modifer."
:group 'god
:type '(alist))
(defcustom god-literal-key
" "
"The key used for literal interpretation."
:group 'god
:type 'string)
(defcustom god-exempt-major-modes
'(dired-mode
git-commit-mode
grep-mode
magit-status-mode
magit-log-edit-mode
vc-annotate-mode)
"List of major modes that should not start in god-local-mode."
:group 'god
:type '(function))
(defcustom god-exempt-predicates
(list #'god-exempt-mode-p
#'god-comint-mode-p
#'god-view-mode-p
#'god-special-mode-p)
"List of predicates checked before enabling god-local-mode.
All predicates must return nil for god-local-mode to start."
:group 'god
:type '(repeat function))
(defvar god-local-mode-map
(let ((map (make-sparse-keymap)))
(suppress-keymap map t)
(define-key map [remap self-insert-command] 'god-mode-self-insert)
map))
(defvar god-mode-universal-argument-map
(let ((map (copy-keymap universal-argument-map)))
(define-key map (kbd "u") 'universal-argument-more)
map)
"Keymap used while processing \\[universal-argument] with god-mode on.")
;;;###autoload
(define-minor-mode god-local-mode
"Minor mode for running commands."
nil " God" god-local-mode-map
(if god-local-mode
(run-hooks 'god-mode-enabled-hook)
(run-hooks 'god-mode-disabled-hook)))
(defun god-local-mode-pause ()
"Pause god-mode local to the buffer, if it's
enabled. See also `god-local-mode-resume'."
(when god-local-mode
(god-local-mode -1)
(setq god-local-mode-paused t)))
(defun god-local-mode-resume ()
"Will re-enable god-mode, if it was active when
`god-local-mode-pause' was called. If not, nothing happens."
(when (bound-and-true-p god-local-mode-paused)
(setq god-local-mode-paused nil)
(god-local-mode 1)))
(defvar god-global-mode nil
"Activate God mode on all buffers?")
(defvar god-literal-sequence nil
"Activated after space is pressed in a command sequence.")
;;;###autoload
(defun god-mode ()
"Toggle global God mode."
(interactive)
(setq god-global-mode (not god-global-mode))
(if god-global-mode
(god-local-mode 1)
(god-local-mode -1)))
;;;###autoload
(defun god-mode-all ()
"Toggle God mode in all buffers."
(interactive)
(let ((new-status (if (bound-and-true-p god-local-mode) -1 1)))
(setq god-global-mode t)
(mapc (lambda (buffer)
(with-current-buffer buffer
(god-mode-maybe-activate new-status)))
(buffer-list))
(setq god-global-mode (= new-status 1))))
(defadvice save&set-overriding-map
(before god-mode-add-to-universal-argument-map (map) activate compile)
"This is used to set special keybindings after C-u is
pressed. When god-mode is active, intercept the call to add in
our own keybindings."
(if (and god-local-mode (equal universal-argument-map map))
(setq map god-mode-universal-argument-map)))
(defun god-mode-self-insert ()
"Handle self-insert keys."
(interactive)
(let* ((initial-key (aref (this-command-keys-vector)
(- (length (this-command-keys-vector)) 1)))
(binding (god-mode-lookup-key-sequence initial-key)))
(when (god-mode-upper-p initial-key)
(setq this-command-keys-shift-translated t))
(setq this-original-command binding)
(setq this-command binding)
;; `real-this-command' is used by emacs to populate
;; `last-repeatable-command', which is used by `repeat'.
(setq real-this-command binding)
(setq god-literal-sequence nil)
(if (commandp binding t)
(call-interactively binding)
(execute-kbd-macro binding))))
(defun god-mode-upper-p (char)
"Is the given char upper case?"
(and (>= char ?A)
(<= char ?Z)
(/= char ?G)))
(defun god-mode-lookup-key-sequence (&optional key key-string-so-far)
"Lookup the command for the given `key' (or the next keypress,
if `key' is nil). This function sometimes
recurses. `key-string-so-far' should be nil for the first call in
the sequence."
(interactive)
(let ((sanitized-key
(if key-string-so-far (char-to-string (or key (read-event key-string-so-far)))
(god-mode-sanitized-key-string (or key (read-event key-string-so-far))))))
(god-mode-lookup-command
(key-string-after-consuming-key sanitized-key key-string-so-far))))
(defun god-mode-sanitized-key-string (key)
"Convert any special events to textual."
(cl-case key
(tab "TAB")
(?\ "SPC")
(left "<left>")
(right "<right>")
(prior "<prior>")
(next "<next>")
(backspace "DEL")
(return "RET")
(t (char-to-string key))))
(defun key-string-after-consuming-key (key key-string-so-far)
"Interpret god-mode special keys for key (consumes more keys if
appropriate). Append to keysequence."
(let ((key-consumed t) next-modifier next-key)
(message key-string-so-far)
(setq next-modifier
(cond
((string= key god-literal-key)
(setq god-literal-sequence t)
"")
(god-literal-sequence
(setq key-consumed nil)
"")
((and
(stringp key)
(not (eq nil (assoc key god-mod-alist)))
(not (eq nil key)))
(cdr (assoc key god-mod-alist)))
(t
(setq key-consumed nil)
(cdr (assoc nil god-mod-alist))
)))
(setq next-key
(if key-consumed
(god-mode-sanitized-key-string (read-event key-string-so-far))
key))
(if key-string-so-far
(concat key-string-so-far " " next-modifier next-key)
(concat next-modifier next-key))))
(defun god-mode-lookup-command (key-string)
"Execute extended keymaps such as C-c, or if it is a command,
call it."
(let* ((key-vector (read-kbd-macro key-string t))
(binding (key-binding key-vector)))
(cond ((commandp binding)
(setq last-command-event (aref key-vector (- (length key-vector) 1)))
binding)
((keymapp binding)
(god-mode-lookup-key-sequence nil key-string))
(:else
(error "God: Unknown key binding for `%s`" key-string)))))
;;;###autoload
(defun god-mode-maybe-activate (&optional status)
"Activate God mode locally on individual buffers when appropriate."
(when (and god-global-mode
(not (minibufferp))
(god-passes-predicates-p))
(god-local-mode (if status status 1))))
(defun god-exempt-mode-p ()
"Return non-nil if major-mode is exempt.
Members of the `god-exempt-major-modes' list are exempt."
(memq major-mode god-exempt-major-modes))
(defun god-mode-child-of-p (major-mode parent-mode)
"Return non-nil if MAJOR-MODE is derived from PARENT-MODE."
(let ((parent (get major-mode 'derived-mode-parent)))
(cond ((eq parent parent-mode))
((not (null parent))
(god-mode-child-of-p parent parent-mode))
(t nil))))
(defun god-comint-mode-p ()
"Return non-nil if major-mode is child of comint-mode."
(god-mode-child-of-p major-mode 'comint-mode))
(defun god-special-mode-p ()
"Return non-nil if major-mode is child of special-mode."
(god-mode-child-of-p major-mode 'special-mode))
(defun god-view-mode-p ()
"Return non-nil if view-mode is enabled in current buffer."
view-mode)
(defun god-passes-predicates-p ()
"Return non-nil if all `god-exempt-predicates' return nil."
(not
(catch 'disable
(let ((preds god-exempt-predicates))
(while preds
(when (funcall (car preds))
(throw 'disable t))
(setq preds (cdr preds)))))))
(provide 'god-mode)
;;; god-mode.el ends here