Added ability to display avy candidates from bottom to top

And made scope limitation possible with avy-goto-line-above and below.

Fixes #236
This commit is contained in:
Sebastian Wålinder 2018-04-17 22:48:52 +02:00 committed by Oleh Krehel
parent 70e384aee5
commit 7cfe11e3c1

48
avy.el
View File

@ -1349,12 +1349,12 @@ This variable is used by `avy-goto-subword-0' and `avy-goto-subword-1'."
;;;###autoload ;;;###autoload
(defun avy-goto-subword-0 (&optional arg predicate beg end) (defun avy-goto-subword-0 (&optional arg predicate beg end)
"Jump to a word or subword start. "Jump to a word or subword start.
The window scope is determined by `avy-all-windows' (ARG negates it). The window scope is determined by `avy-all-windows' (ARG negates it).
BEG and END narrow the scope where candidates are searched.
When PREDICATE is non-nil it's a function of zero parameters that When PREDICATE is non-nil it's a function of zero parameters that
should return true." should return true.
BEG and END narrow the scope where candidates are searched."
(interactive "P") (interactive "P")
(require 'subword) (require 'subword)
(avy-with avy-goto-subword-0 (avy-with avy-goto-subword-0
@ -1411,11 +1411,12 @@ Which one depends on variable `subword-mode'."
(defvar visual-line-mode) (defvar visual-line-mode)
(defun avy--line-cands (&optional arg beg end) (defun avy--line-cands (&optional arg beg end bottom-up)
"Get candidates for selecting a line. "Get candidates for selecting a line.
The window scope is determined by `avy-all-windows'. The window scope is determined by `avy-all-windows'.
When ARG is non-nil, do the opposite of `avy-all-windows'. When ARG is non-nil, do the opposite of `avy-all-windows'.
BEG and END narrow the scope where candidates are searched." BEG and END narrow the scope where candidates are searched.
When BOTTOM-UP is non-nil, display avy candidates from top to bottom"
(let (candidates) (let (candidates)
(avy-dowindows arg (avy-dowindows arg
(let ((ws (or beg (window-start)))) (let ((ws (or beg (window-start))))
@ -1436,7 +1437,9 @@ BEG and END narrow the scope where candidates are searched."
(setq temporary-goal-column 0) (setq temporary-goal-column 0)
(line-move-visual 1 t)) (line-move-visual 1 t))
(forward-line 1))))))) (forward-line 1)))))))
(nreverse candidates))) (if bottom-up
candidates
(nreverse candidates))))
(defun avy--linum-strings () (defun avy--linum-strings ()
"Get strings for `avy-linum-mode'." "Get strings for `avy-linum-mode'."
@ -1521,14 +1524,15 @@ BEG and END narrow the scope where candidates are searched."
(frame-char-width))))) (frame-char-width)))))
(set-window-margins win width (cdr (window-margins win))))) (set-window-margins win width (cdr (window-margins win)))))
(defun avy--line (&optional arg beg end) (defun avy--line (&optional arg beg end bottom-up)
"Select a line. "Select a line.
The window scope is determined by `avy-all-windows'. The window scope is determined by `avy-all-windows'.
When ARG is non-nil, do the opposite of `avy-all-windows'. When ARG is non-nil, do the opposite of `avy-all-windows'.
BEG and END narrow the scope where candidates are searched." BEG and END narrow the scope where candidates are searched.
When BOTTOM-UP is non-nil, display avy candidates from top to bottom"
(let ((avy-action #'identity)) (let ((avy-action #'identity))
(avy--process (avy--process
(avy--line-cands arg beg end) (avy--line-cands arg beg end bottom-up)
(if avy-linum-mode (if avy-linum-mode
(progn (message "Goto line:") (progn (message "Goto line:")
'ignore) 'ignore)
@ -1572,23 +1576,35 @@ Otherwise, forward to `goto-line' with ARG."
(avy-action-goto r)))))) (avy-action-goto r))))))
;;;###autoload ;;;###autoload
(defun avy-goto-line-above () (defun avy-goto-line-above (&optional offset bottom-up)
"Goto visible line above the cursor." "Goto visible line above the cursor.
OFFSET changes the distance between the closest key to the cursor and
the cursor
When BOTTOM-UP is non-nil, display avy candidates from top to bottom"
(interactive) (interactive)
(if offset
(setq offset (+ 2 (- offset))))
(let* ((avy-all-windows nil) (let* ((avy-all-windows nil)
(r (avy--line nil (window-start) (r (avy--line nil (window-start)
(line-beginning-position)))) (line-beginning-position (or offset 1))
bottom-up)))
(unless (eq r t) (unless (eq r t)
(avy-action-goto r)))) (avy-action-goto r))))
;;;###autoload ;;;###autoload
(defun avy-goto-line-below () (defun avy-goto-line-below (&optional offset bottom-up)
"Goto visible line below the cursor." "Goto visible line below the cursor.
OFFSET changes the distance between the closest key to the cursor and
the cursor
When BOTTOM-UP is non-nil, display avy candidates from top to bottom"
(interactive) (interactive)
(if offset
(setq offset (+ offset 1)))
(let* ((avy-all-windows nil) (let* ((avy-all-windows nil)
(r (avy--line (r (avy--line
nil (line-beginning-position 2) nil (line-beginning-position (or offset 2))
(window-end (selected-window) t)))) (window-end (selected-window) t)
bottom-up)))
(unless (eq r t) (unless (eq r t)
(avy-action-goto r)))) (avy-action-goto r))))