-
Notifications
You must be signed in to change notification settings - Fork 0
/
hjkl.el
335 lines (271 loc) · 10.5 KB
/
hjkl.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
;;; hjkl.el --- Move in emacs using context dependent keybindings.
;; Copyright (C) 2017, Samuel Barreto
;; Author: Samuel Barreto <[email protected]>
;; Maintainer: Samuel Barreto <[email protected]>
;; Keywords: editing
;; Version: 0.1
;; URL: TODO
;; 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; `hjkl` provides an easy way to navigate source files using context dependent
;; keybindings, _ie_ keybindings that trigger an action depending on characters
;; that follow or precede the point (cursor). The idea is to move swiftly by
;; semantically relevant units in a language agnostic way. You use `hjkl-up`,
;; `hjkl-down` and other keybindings to jump to the previous or next semantic
;; unit (a sexp, a paragraph for example) when point is after or behind a
;; regular expression.
;;
;; In a way, `hjkl` was born as the result of my frustration not being able to
;; use [lispy](https://github.com/abo-abo/lispy) keybindings in all major modes.
;;
;; ![hjkl](img/hjkl.gif)
;;; Installation:
;;
;; This module is currently in beta version and not (yet) available on MELPA.
;;
;; To install use
;;
;; $ git clone https://github.com/sam217pa/emacs-hjkl
;;
;; and add the following `use-package` declaration into your `.emacs` file:
;;
;; ```elisp
;; (use-package hjkl
;; :load-path "path/to/emacs-hjkl"
;; :config
;; (setq hjkl-up (kbd "K")) ; if k does not suits you.
;; (hjkl-define-keys ...))
;; ```
;;; Usage:
;;
;; The following code tells emacs to jump to the next sentence when the cursor
;; is positioned after a period, an exclamation or question mark, or to jump to
;; the next outline heading if the cursor is before the value of
;; `outline-regexp', when the key defined by hjkl-down is pressed. Otherwise the
;; key calling hjkl-down acts normally.
;; ```elisp
;; (hjkl-define-keys
;; '((:keymap text-mode-map
;; :bind (:down (("\\.\\|?\\|!" #'forward-sentence back)
;; (outline-regexp #'outline-next-heading))))))
;; ```
;; It results into the evaluation of the following snippet:
;; ```elisp
;; (define-key text-mode-map hjkl-down
;; (lambda () (interactive)
;; (cond ((looking-back "\\.\\|?\\|!")
;; (funcall #'forward-sentence))
;; ((looking-at outline-regexp)
;; (outline-next-heading))
;; (t (self-insert-command 1)))))
;; ```
;; (More information on context-dependent keybindings can be found
;; [here](http://endlessparentheses.com/define-context-aware-keys-in-emacs.html).)
;; `hjkl` provides two way to define keybindings. The preferred way is to use
;;
;; (setq hjkl-bindings HJKL-PLIST)
;;
;; and call `M-x hjkl-update-keys` or evaluate `hjkl-update-keys` in your
;; `.emacs` ; the other way is to directly pass HJKL-PLIST to
;; `(hjkl-define-keys)`. This has the drawback of having to define all keys in
;; one place, whereas you can `append` code to `hjkl-bindings` with
;; ```elisp
;; (add-to-list
;; 'hjkl-bindings
;; '(:keymap ess-mode-map
;; :bind (:eval (")" #'ess-eval-function-or-paragraph))))
;; ```
;;; hjkl Directions
;;
;; `hjkl` provides several keybindings by default, corresponding to
;; `hjkl-directions':
;;
;; - `:up`: bound to `k` by default, useful to goes up a semantic unit. It can
;; be used to jump to the beginning of the previous paragraph, SEXP or
;; outline-heading.
;; - `:down`: bound to `j` by default, it makes sense to bind function that does
;; the opposite of those bound to `hjkl-up`.
;; - `:left` or `:right`: bound to `h` and `l` by default, useful to goes out of
;; a semantic unit. It can be used to jump to the enclosing parent delimiter
;; when the cursor is before a closing delimiter, or to hide the body of the
;; current outline heading.
;; - `:jump`: bound to `a` by default, useful to jump to a specific location
;; using [`a`vy](https://github.com/abo-abo/avy) for example.
;; - `:eval`: bound to `e` by default, used to evaluate SEXP or expression in a
;; REPL. It is useful in emacs-lisp, ESS-modes, python and languages that
;; interact with a REPL.
;; - `:mark`: bound to `m` by default, used to mark expressions, paragraphs and
;; - stuff like that.
;; - `:other`: bound to `o` by default, jump to the corresponding delimiter.
;;; Example config
;;
;;; Contributions
;;
;; I would really appreciate contributions as this is my first package. I am
;; sure it does some really wrong things, and that it can be improved a lot.
;; I'll be glad to have feedbacks on it !
;;; Disclaimer
;;
;; hjkl still feels a lot like a hack to me, but it does kinda work as I
;; expected, so I felt I would put it in the open. Maybe it will break things in
;; your config, but as the GPL says, it is distributed without any garantee … ;)
;;; Code:
;;
;; -*- lexical-binding: t -*-
(eval-when-compile
(require 'cl))
(setq hjkl-left (kbd "h"))
(setq hjkl-down (kbd "j"))
(setq hjkl-up (kbd "k"))
(setq hjkl-right (kbd "l"))
(setq hjkl-other (kbd "d"))
(setq hjkl-eval (kbd "e"))
(setq hjkl-jump (kbd "a"))
(setq hjkl-mark (kbd "m"))
(defvar hjkl-directions-binding
'(:up hjkl-up
:down hjkl-down
:left hjkl-left
:right hjkl-right
:other hjkl-other
:jump hjkl-jump
:eval hjkl-eval
:mark hjkl-mark))
(defvar hjkl-directions
(hjkl--plist-keys hjkl-directions-binding)
"The list of directions that hjkl defines.
A list of example of what each keybinding does:
:up goes up in a semantic unit.
:down goes down in a semantic unit.
:left the hjkl left direction, useful to get out of a sexp tree
when looking at an opening delimiter for example, or down
in the sexp tree when looking at a closing delimiter.
:right the hjkl right direction, that usually does the opposite
as hjkl-left.
:other goes to the corresponding delimiter, like a corresponding
opening square, round or curly bracket.
:jump jump to a specific direction using avy.
:eval evaluate last sexp, last paragraph or following sexp
:mark mark the previous or following sexp
")
;; from https://www.emacswiki.org/emacs/mon-plist-utils.el
(defun hjkl--plist-keys (in-plist)
"Extract keys from a plist."
(if (null in-plist)
in-plist
(cons (car in-plist)
(hjkl--plist-keys (cddr in-plist)))))
(defun hjkl--parse-keybinding (key-bind)
"Generate the interactive anonymous function triggered by
hjkl-* keybindings."
`(lambda ()
(interactive)
(cond ,@(mapcar
#'hjkl--parse-re-def
key-bind)
(t (self-insert-command 1)))))
(defun hjkl--parse-re-def (re-def)
"Generate the cond calls for each regular expression in RE-DEF."
(let ((back (cl-third re-def))
(re (cl-first re-def))
(fn (cl-second re-def)))
(if back
`((looking-back ,re (point-at-bol))
(funcall ,fn))
`((looking-at ,re)
(funcall ,fn)))))
(defun hjkl--define-key (keydef)
"Generate the define-key call and evaluate them for each
direction in ``hjkl-directions-binding''."
(let ((keymapvar (plist-get keydef :keymap))
(keybindvar (plist-get keydef :bind)))
(mapc #'eval
(mapcar
(lambda (dir)
`(define-key
,keymapvar
,(plist-get hjkl-directions-binding dir)
,(hjkl--parse-keybinding (plist-get keybindvar dir))))
(hjkl--plist-keys hjkl-directions-binding)))))
;;;###autoload
(defun hjkl-define-keys (key-definition)
"Call `hjkl--define-key' on each sublist of KEY-DEFINITION.
KEY-DEFINITION should be a list of the form
'((:keymap KEYMAP
:bind (:DIRECTION ((RE FUNCTION-CALL BACK)))))
:keymap should be a valid emacs keymap.
:bind is mandatory, it corresponds to the list of condition to
check for each hjkl direction.
RE is a valid regular expression, passed to `looking-at' if BACK
is nil (the default) or `looking-back' if BACK is t.
FUNCTION-CALL can be a simple function call of the form
#'function or a lambda expression.
An example usage would be the following snippet:
(hjkl-define-keys
'((:keymap text-mode-map
:bind (:up ((\"\\.\\|?\\|!\" (lambda ()
(backward-sentence)
(backward-char 1))
back))
:down ((\"\\.\\|?\\|!\" #'forward-sentence back))))))
It bind `hjkl-up' to jump to the beginning of the previous
sentence when the cursor is after a `.`, a `?` or a `!` and
`hjkl-down' to go the next sentence. The \"back\" mention is
optional but indicate that the characters that matters precedes
the cursor.
"
(mapc #'hjkl--define-key key-definition))
;;;###autoload
(defun hjkl-update-keys ()
"Update keybinding definition if ``hjkl-bindings'' is defined
by calling ``hjkl-define-keys'' on it."
(interactive)
(if (boundp 'hjkl-bindings)
(hjkl-define-keys hjkl-bindings)
(error "hjkl-bindings is not defined.")))
;; ---------- TESTS -------------------------------------------------------
(ert-deftest plist-parsing ()
(should (equal (hjkl--plist-keys '(:a a :b b)) '(:a :b)))
(should (equal (hjkl--plist-keys '(:a a :b a :b)) '(:a :b :b)))
(should (equal (hjkl--plist-keys '(:a a :b b b b)) '(:a :b b))))
(ert-deftest re-def-parsing ()
(should
(equal (hjkl--parse-re-def '(";;;" (lambda () (message "cuicui")) t))
'((looking-back ";;;" (point-at-bol)) (funcall (lambda nil (message "cuicui"))))))
(should
(equal (hjkl--parse-re-def '(";;;" (lambda () (message "cuicui"))))
'((looking-at ";;;") (funcall (lambda nil (message "cuicui")))))))
(ert-deftest generate-lambda-for-direction ()
(should
(equal
(hjkl--parse-keybinding '((")" #'forward-list t)))
'(lambda nil
(interactive)
(cond
((looking-back ")" (point-at-bol))
(funcall #'forward-list))
(t
(self-insert-command 1))))))
(should
(equal
(hjkl--parse-keybinding '(("(" #'forward-list)))
'(lambda nil
(interactive)
(cond
((looking-at "(")
(funcall #'forward-list))
(t
(self-insert-command 1)))))))
(provide 'hjkl)
;;; hjkl.el ends here.