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'."
:type '(repeat character)) :type '(repeat character))
;;;###autoload ;;;###autoload
(defun avy-goto-subword-0 (&optional arg predicate beg end)
(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).
When PREDICATE is non-nil it's a function of zero parameters that
should return true.
BEG and END narrow the scope where candidates are searched." BEG and END narrow the scope where candidates are searched."
(interactive "P") (interactive "P")
@@ -1411,11 +1411,12 @@ Which one depends on variable `subword-mode'."
(call-interactively #'avy-goto-subword-1) (call-interactively #'avy-goto-subword-1)
(call-interactively #'avy-goto-word-1))) (call-interactively #'avy-goto-word-1)))
(defvar visual-line-mode) (defvar visual-line-mode)
(defun avy--line-cands (&optional arg beg end bottom-up) (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'.
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" When BOTTOM-UP is non-nil, display avy candidates from top to bottom"
(let (candidates) (let (candidates)
@@ -1436,7 +1437,9 @@ BEG and END narrow the scope where candidates are searched."
(if visual-line-mode (if visual-line-mode
(progn (progn
(setq temporary-goal-column 0) (setq temporary-goal-column 0)
(line-move-visual 1 t)) (line-move-visual 1 t))
(forward-line 1)))))))
(if bottom-up
candidates candidates
(nreverse candidates)))) (nreverse candidates))))
@@ -1521,14 +1524,15 @@ BEG and END narrow the scope where candidates are searched."
(setq width (ceiling (setq width (ceiling
(/ (* width 1.0 (linum--face-width 'linum)) (/ (* width 1.0 (linum--face-width 'linum))
(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 bottom-up) (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'.
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" 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 bottom-up) (avy--line-cands arg beg end bottom-up)
(if avy-linum-mode (if avy-linum-mode
@@ -1572,23 +1576,35 @@ Otherwise, forward to `goto-line' with ARG."
(r (avy--line (eq arg 4)))) (r (avy--line (eq arg 4))))
(unless (eq r t) (unless (eq r t)
(avy-action-goto r)))))) (avy-action-goto r))))))
;;;###autoload ;;;###autoload
(defun avy-goto-line-above (&optional offset bottom-up)
"Goto visible line above the cursor.
OFFSET changes the distance between the closest key to the cursor and
the cursor the cursor
When BOTTOM-UP is non-nil, display avy candidates from top to bottom"
(interactive)
(if offset (if offset
(setq offset (+ 2 (- offset)))) (setq offset (+ 2 (- offset))))
(let* ((avy-all-windows nil) (let* ((avy-all-windows nil)
(r (avy--line nil (window-start)
(line-beginning-position (or offset 1)) (line-beginning-position (or offset 1))
bottom-up))) 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 (&optional offset bottom-up)
"Goto visible line below the cursor.
OFFSET changes the distance between the closest key to the cursor and
the cursor the cursor
When BOTTOM-UP is non-nil, display avy candidates from top to bottom"
(interactive)
(if offset (if offset
(setq offset (+ offset 1))) (setq offset (+ offset 1)))
(let* ((avy-all-windows nil) (let* ((avy-all-windows nil)
(r (avy--line (r (avy--line
nil (line-beginning-position (or offset 2))
(window-end (selected-window) t) (window-end (selected-window) t)
bottom-up))) bottom-up)))
(unless (eq r t) (unless (eq r t)