Skip to content

Commit 8515834

Browse files
committed
Add :collect-postprocess parameter, closes #34
1 parent 1facf7e commit 8515834

File tree

2 files changed

+66
-36
lines changed

2 files changed

+66
-36
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ If `expr` is non-`nil`, the motion will be executed in all visible windows simul
9393

9494
When specified, `(goto-char (funcall callable))` is run before the motion is executed. For example, use this to jump to the BOL of each line as in easymotion with `:initial-position #'point-at-bol`. Unlike in `:pre-hook`, `callable` is run once per window when `:all-windows` is specified.
9595

96+
* `:collect-postprocess callable`
97+
98+
When specified, `callable` is called on the collected list of points (which is of the form `((point window)...)`). Otherwise, the default function, which sorts the points in order of increasing distance from `(point)`, is used.
99+
96100
Credits
97101
=======
98102
I'm deeply indebted to:

evil-easymotion.el

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,31 @@
107107
(avy--process points
108108
(avy--style-fn avy-style))))
109109

110-
(defun evilem--collect (func &optional scope all-windows initial-point)
110+
(defun evilem--default-collect-postprocess (points)
111+
(cl-stable-sort
112+
points
113+
#'<
114+
:key (lambda (pt)
115+
(if (equal (selected-window) (cdr pt))
116+
(abs (- (point) (car pt)))
117+
most-positive-fixnum))))
118+
119+
(defun evilem--collect (func &optional
120+
scope
121+
all-windows
122+
initial-point
123+
sort-key
124+
collect-postprocess)
111125
"Repeatedly execute func, and collect the cursor positions into a list"
112126
(require 'avy)
113-
(if (functionp func)
114-
(cl-letf ((points nil)
115-
(point nil)
116-
(avy-all-windows all-windows)
117-
;; make sure the motion doesn't move the window
118-
(scroll-conservatively 101)
119-
(smooth-scrolling-mode nil)
120-
(scroll-margin 0))
127+
(cl-letf ((points nil)
128+
(point nil)
129+
(avy-all-windows all-windows)
130+
;; make sure the motion doesn't move the window
131+
(scroll-conservatively 101)
132+
(smooth-scrolling-mode nil)
133+
(scroll-margin 0))
134+
(if (functionp func)
121135
(avy-dowindows current-prefix-arg
122136
(save-excursion
123137
(save-restriction
@@ -143,11 +157,13 @@
143157
(setq point (cons (point) (get-buffer-window)))
144158
(not (member point points))
145159
(push point points))))))
146-
(nreverse points))
147-
(cl-remove-duplicates
148-
(cl-mapcan (lambda (f)
149-
(evilem--collect f scope all-windows))
150-
func))))
160+
(setq points (cl-remove-duplicates
161+
(cl-mapcan (lambda (f)
162+
(evilem--collect f scope all-windows))
163+
func))))
164+
(funcall (or collect-postprocess
165+
#'evilem--default-collect-postprocess)
166+
points)))
151167

152168
(cl-defmacro evilem-make-motion (name
153169
funcs
@@ -157,25 +173,27 @@
157173
bind
158174
scope
159175
all-windows
160-
initial-point)
176+
initial-point
177+
collect-postprocess)
161178
"Automatically define an evil easymotion for `func', naming it `name'"
162179
`(,(if all-windows
163180
'evil-define-command
164181
'evil-define-motion)
165182
,name (&optional _count)
166-
(evil-without-repeat
167-
(setq evil-this-type 'inclusive)
168-
(cl-letf* ,bind
169-
,(when pre-hook `(funcall ,(if (functionp pre-hook)
170-
pre-hook
171-
`(lambda () ,pre-hook))))
172-
(evilem--jump (evilem--collect ,funcs
173-
,scope
174-
,all-windows
175-
,initial-point))
176-
,(when post-hook `(funcall ,(if (functionp post-hook)
177-
post-hook
178-
`(lambda () ,post-hook))))))))
183+
(evil-without-repeat
184+
(setq evil-this-type 'inclusive)
185+
(cl-letf* ,bind
186+
,(when pre-hook `(funcall ,(if (functionp pre-hook)
187+
pre-hook
188+
`(lambda () ,pre-hook))))
189+
(evilem--jump (evilem--collect ,funcs
190+
,scope
191+
,all-windows
192+
,initial-point
193+
,collect-postprocess))
194+
,(when post-hook `(funcall ,(if (functionp post-hook)
195+
post-hook
196+
`(lambda () ,post-hook))))))))
179197

180198
(cl-defmacro evilem-make-motion-plain (name
181199
funcs
@@ -185,7 +203,8 @@
185203
bind
186204
scope
187205
all-windows
188-
initial-point)
206+
initial-point
207+
collect-postprocess)
189208
"Automatically define a plain easymotion for `func', naming it `name'"
190209
`(defun ,name ()
191210
(interactive)
@@ -196,7 +215,8 @@
196215
(evilem--jump (evilem--collect ,funcs
197216
,scope
198217
,all-windows
199-
,initial-point))
218+
,initial-point
219+
,collect-postprocess))
200220
,(when post-hook `(funcall ,(if (functionp post-hook)
201221
post-hook
202222
`(lambda () ,post-hook)))))))
@@ -208,7 +228,8 @@
208228
bind
209229
scope
210230
all-windows
211-
initial-point)
231+
initial-point
232+
collect-postprocess)
212233
`(evilem-make-motion
213234
,(intern (evilem--make-name motions))
214235
,motions
@@ -217,7 +238,8 @@
217238
:bind ,bind
218239
:scope ,scope
219240
:all-windows ,all-windows
220-
:initial-point ,initial-point))
241+
:initial-point ,initial-point
242+
:collect-postprocess ,collect-postprocess))
221243

222244
(cl-defmacro evilem-create-plain (motions
223245
&key
@@ -226,7 +248,8 @@
226248
bind
227249
scope
228250
all-windows
229-
initial-point)
251+
initial-point
252+
collect-postprocess)
230253
`(evilem-make-motion-plain
231254
,(intern (evilem--make-name motions))
232255
,motions
@@ -235,7 +258,8 @@
235258
:bind ,bind
236259
:scope ,scope
237260
:all-windows ,all-windows
238-
:initial-point ,initial-point))
261+
:initial-point ,initial-point
262+
:collect-postprocess ,collect-postprocess))
239263

240264
(cl-defmacro evilem-define (key
241265
motions
@@ -245,7 +269,8 @@
245269
bind
246270
scope
247271
all-windows
248-
initial-point)
272+
initial-point
273+
collect-postprocess)
249274
"Automatically create and bind an evil motion"
250275
`(define-key ,(if all-windows
251276
'evil-normal-state-map
@@ -257,7 +282,8 @@
257282
:bind ,bind
258283
:scope ,scope
259284
:all-windows ,all-windows
260-
:initial-point ,initial-point)))
285+
:initial-point ,initial-point
286+
:collect-postprocess ,collect-postprocess)))
261287

262288
;;;###autoload
263289
(defun evilem-default-keybindings (prefix)

0 commit comments

Comments
 (0)