abo-abo / avy

Jump to things in Emacs tree-style
1.72k stars 110 forks source link

By default exclude current point as candidate #268

Open Luis-Henriquez-Perez opened 5 years ago

Luis-Henriquez-Perez commented 5 years ago

I noticed that in custom avy commands I made that it is possible for the candidates to include the current point I'm on.

Since that's the point I'm already at, there's no need to jump to it. I can't think of a situation in which this would be useful (though if I'm wrong and there is, please let me know).

To remedy this I added filter to all my avy commands:

`(lambda () (/= ,(1+ (point)) (point))
;; the `+1` is because my (block) cursor is behind the char I'm at, but avy goes to the 
;; the point after the char when getting candidates.

It's not a big deal, but I was wondering: should avy exclude the current point by default?

Maybe a custom variable: avy-exclude-current-point would be nice?

koddo commented 4 years ago

avy-jump now has a :pred keyword parameter, so we can write an advice that amends a passed predicate and filters out current point.

  (defun avy-jump-advice (orig-func &rest args)
    (let* ((between-inclusive (lambda (val low high) (and (<= low val) (<= val high))))
       (current-point (point))
       (oldpred (plist-get :pred args))
       (pred (lambda ()
           (and
            (let ((candidate (point))) (or (< candidate current-point) (> candidate (+ 3 current-point))))   ; +3 should be enough: with avy-goto-word-1 it's +1, with avy-goto-char-2 it's +2
            (or (null oldpred) (funcall oldpred))))))
      (apply orig-func (append args (list :pred pred)))))
  (advice-add 'avy-jump :around #'avy-jump-advice)