-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathah.el
263 lines (226 loc) · 8.6 KB
/
ah.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
;;; ah.el --- Additional hooks -*- lexical-binding: t; -*-
;; Copyright (C) 2019-2022 Takaaki ISHIKAWA
;; Author: Takaaki ISHIKAWA <takaxp at ieee dot org>
;; Keywords: convenience
;; Version: 0.9.3
;; Maintainer: Takaaki ISHIKAWA <takaxp at ieee dot org>
;; URL: https://github.com/takaxp/ah
;; Package-Requires: ((emacs "25.1"))
;; Twitter: @takaxp
;; 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 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:
;; This package provides a set of additional hooks.
;; - ah-before-move-cursor-hook
;; - ah-after-move-cursor-hook
;; - ah-before-c-g-hook
;; - ah-after-c-g-hook
;; - ah-before-enable-theme-hook
;; - ah-after-enable-theme-hook
;;; Change Log:
;;; Code:
;; `ah-before-move-cursor-hook' and `ah-after-move-cursor-hook'
(defgroup ah nil
"Additional hooks."
:group 'convenience)
(defcustom ah-lighter " Hooks"
"Lighter for this."
:type 'string
:group 'ah)
(defcustom ah-before-move-cursor-hook nil
"Hook runs before moving the cursor."
:type 'hook
:group 'ah)
(defcustom ah-after-move-cursor-hook nil
"Hook runs after moving the cursor."
:type 'hook
:group 'ah)
(defcustom ah-before-c-g-hook nil
"Hook runs before \\[keyboard-quit] and related commands."
:type 'hook
:group 'ah)
(defcustom ah-after-c-g-hook nil
"Hook runs after \\[keyboard-quit] and related commands."
:type 'hook
:group 'ah)
(defcustom ah-before-enable-theme-hook nil
"Hook runs before \\[enable-theme]."
:type 'hook
:group 'ah)
(defcustom ah-after-enable-theme-hook nil
"Hook runs after \\[enable-theme]."
:type 'hook
:group 'ah)
(defun ah--cur-next-line (f &optional arg try-vscroll)
"Extend `next-line'.
F is the original function.
ARG and TRY-VSCROLL are identical to the original arguments."
(when (called-interactively-p 'any)
(run-hooks 'ah-before-move-cursor-hook))
(funcall f arg try-vscroll)
(when (called-interactively-p 'any)
(run-hooks 'ah-after-move-cursor-hook)))
(defun ah--cur-previous-line (f &optional arg try-vscroll)
"Extend `previous-line'.
F is the original function.
ARG and TRY-VSCROLL are identical to the original arguments."
(when (called-interactively-p 'any)
(run-hooks 'ah-before-move-cursor-hook))
(funcall f arg try-vscroll)
(when (called-interactively-p 'any)
(run-hooks 'ah-after-move-cursor-hook)))
(defun ah--cur-forward-char (f &optional N)
"Extend `forward-char'.
F is the original function.
N is identical to the original arguments."
(when (called-interactively-p 'any)
(run-hooks 'ah-before-move-cursor-hook))
(funcall f N)
(when (called-interactively-p 'any)
(run-hooks 'ah-after-move-cursor-hook)))
(defun ah--cur-backward-char (f &optional N)
"Extend `backward-char'.
F is the original function.
N is identical to the original arguments."
(when (called-interactively-p 'any)
(run-hooks 'ah-before-move-cursor-hook))
(funcall f N)
(when (called-interactively-p 'any)
(run-hooks 'ah-after-move-cursor-hook)))
(defun ah--cur-syntax-subword-forward (f &optional N)
"Extend `syntax-subword-forward'.
F is the original function.
N is identical to the original arguments."
(when (called-interactively-p 'any)
(run-hooks 'ah-before-move-cursor-hook))
(funcall f N)
(when (called-interactively-p 'any)
(run-hooks 'ah-after-move-cursor-hook)))
(defun ah--cur-syntax-subword-backward (f &optional N)
"Extend `syntax-subword-backward'.
F is the original function.
N is identical to the original arguments."
(when (called-interactively-p 'any)
(run-hooks 'ah-before-move-cursor-hook))
(funcall f N)
(when (called-interactively-p 'any)
(run-hooks 'ah-after-move-cursor-hook)))
(defun ah--cur-move-beginning-of-line (f ARG)
"Extend `move-beginning-of-line'.
F is the original function.
ARG is identical to the original arguments."
(when (called-interactively-p 'any)
(run-hooks 'ah-before-move-cursor-hook))
(funcall f ARG)
(when (called-interactively-p 'any)
(run-hooks 'ah-after-move-cursor-hook)))
(defun ah--cur-move-end-of-line (f ARG)
"Extend `move-end-of-line'.
F is the original function.
ARG is identical to the original arguments."
(when (called-interactively-p 'any)
(run-hooks 'ah-before-move-cursor-hook))
(funcall f ARG)
(when (called-interactively-p 'any)
(run-hooks 'ah-after-move-cursor-hook)))
(defun ah--cur-beginning-of-buffer (f &optional ARG)
"Extend `beginning-of-buffer'.
F is the original function.
ARG is identical to the original arguments."
(when (called-interactively-p 'any)
(run-hooks 'ah-before-move-cursor-hook))
(funcall f ARG)
(when (called-interactively-p 'any)
(run-hooks 'ah-after-move-cursor-hook)))
(defun ah--cur-end-of-buffer (f &optional ARG)
"Extend `end-of-buffer'.
F is the original function.
ARG is identical to the original arguments."
(when (called-interactively-p 'any)
(run-hooks 'ah-before-move-cursor-hook))
(funcall f ARG)
(when (called-interactively-p 'any)
(run-hooks 'ah-after-move-cursor-hook)))
(defun ah--cg-post-processing ()
"Post processing for aborting of the target command."
(when (memq this-command '(keyboard-quit isearch-abort))
(run-hooks 'ah-after-c-g-hook))
(remove-hook 'post-command-hook #'ah--cg-post-processing))
(defun ah--cg-keyboard-quit (f)
"Extend `keyboard-quit'.
F is the original function."
(when ah-after-c-g-hook
(add-hook 'post-command-hook #'ah--cg-post-processing))
(run-hooks 'ah-before-c-g-hook)
(funcall f))
(defun ah--cg-isearch-abort (f)
"Extend `isearch-abort'.
F is the original function."
(when ah-after-c-g-hook
(add-hook 'post-command-hook #'ah--cg-post-processing))
(run-hooks 'ah-before-c-g-hook)
(funcall f))
(defun ah--enable-theme (f theme)
"Extend `enable-theme'.
F is the original function.
THEME is identical to the original arguments."
(when (eq theme 'user)
(run-hooks 'ah-before-enable-theme-hook))
(funcall f theme)
(when (eq theme 'user)
(run-hooks 'ah-after-enable-theme-hook)))
(defun ah--setup ()
"Setup."
;; For `ah-before-move-cursor-hook' and `ah-after-move-cursor-hook'
(advice-add 'next-line :around #'ah--cur-next-line)
(advice-add 'previous-line :around #'ah--cur-previous-line)
(advice-add 'forward-char :around #'ah--cur-forward-char)
(advice-add 'backward-char :around #'ah--cur-backward-char)
(advice-add 'syntax-subword-forward :around #'ah--cur-syntax-subword-forward)
(advice-add 'syntax-subword-backward :around
#'ah--cur-syntax-subword-backward)
(advice-add 'move-beginning-of-line :around #'ah--cur-move-beginning-of-line)
(advice-add 'move-end-of-line :around #'ah--cur-move-end-of-line)
(advice-add 'beginning-of-buffer :around #'ah--cur-beginning-of-buffer)
(advice-add 'end-of-buffer :around #'ah--cur-end-of-buffer)
(advice-add 'keyboard-quit :around #'ah--cg-keyboard-quit)
(advice-add 'isearch-abort :around #'ah--cg-isearch-abort)
(advice-add 'enable-theme :around #'ah--enable-theme))
(defun ah--abort ()
"Abort."
;; For `ah-before-move-cursor-hook' and `ah-after-move-cursor-hook'
(advice-remove 'next-line #'ah--cur-next-line)
(advice-remove 'previous-line #'ah--cur-previous-line)
(advice-remove 'forward-char #'ah--cur-forward-char)
(advice-remove 'backward-char #'ah--cur-backward-char)
(advice-remove 'syntax-subword-forward #'ah--cur-syntax-subword-forward)
(advice-remove 'syntax-subword-backward #'ah--cur-syntax-subword-backward)
(advice-remove 'move-beginning-of-line #'ah--cur-move-beginning-of-line)
(advice-remove 'move-end-of-line #'ah--cur-move-end-of-line)
(advice-remove 'beginning-of-buffer #'ah--cur-beginning-of-buffer)
(advice-remove 'end-of-buffer #'ah--cur-end-of-buffer)
(advice-remove 'keyboard-quit #'ah--cg-keyboard-quit)
(advice-remove 'isearch-abort #'ah--cg-isearch-abort)
(advice-remove 'enable-theme #'ah--enable-theme))
;;;###autoload
(define-minor-mode ah-mode
"Toggle the minor mode `ah-mode'."
:init-value nil
:lighter (:eval (format "%s" ah-lighter))
:global t
:group 'ah
(if ah-mode
(ah--setup)
(ah--abort)))
(provide 'ah)
;;; ah.el ends here