avy-jump.el: Major update

* avy-jump.el (avy-jump): Change prefix to "avi-".
(avi-keys): Rename from `avy-keys'.
(avi-background): New defcustom.
(avi-lead-face): New defface.
(avi--goto): Update.
(avi--process): Update signature.
(avi--regex-candidates): Update format.
(avi--overlay): New defun.
(avi--overlay-pre): New defun.
(avi--overlay-post): New defun.
(avi-goto-char): Rename from `avy-jump-char'.
(avi-goto-char-2): Rename from `avy-jump-double-char'.
(avi-isearch): Rename from `avy-jump-isearch'.
(avi-goto-word-0): Rename from `avy-jump-zero-word'.
(avi-goto-word-1): Rename from `avy-jump-one-word'.
(avi-goto-line): Rename from `avy-jump-line'.
(avi--line): New defun.
(avi-copy-line): New defun.
(avi-move-line): New defun.
(avi-copy-region): New defun.

Fixes #21.
old-master
Oleh Krehel 11 years ago
parent 574b3bddfc
commit 4e5ca87988
  1. 257
      avy-jump.el

@ -21,92 +21,154 @@
;;; Commentary: ;;; Commentary:
;; ;;
;; This package offers various commands for navigating to things using `avy', such as ;; This package offers various commands for navigating to things using `avy'.
;; `avy-jump-char', `avy-double-char', and `avy-jump-line'. ;; They are in the "Commands" outline.
;;; Code: ;;; Code:
;;* Requires
(require 'avy) (require 'avy)
(require 'ace-window) (require 'ace-window)
;;* Customization
(defgroup avy-jump nil (defgroup avy-jump nil
"Jump to things tree-style." "Jump to things tree-style."
:group 'convenience :group 'convenience
:prefix "avy-") :prefix "avi-")
(defcustom avy-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l) (defcustom avi-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l)
"Keys for jumping.") "Keys for jumping.")
(defun avy--goto (x) (defcustom avi-background nil
"When non-nil, a gray background will be added during the selection."
:type 'boolean)
(defface avi-lead-face
'((t (:foreground "white" :background "#e52b50")))
"Face used for the leading chars.")
;;* Internals
(defun avi--goto (x)
"Goto X. "Goto X.
X is (POSITION . WINDOW)." X is (POS . WND)
(select-window (cdr x)) POS is either a position or (BEG . END)."
(goto-char (car x))) (if (null x)
(message "zero candidates")
(select-window (cdr x))
(let ((pt (car x)))
(when (consp pt)
(setq pt (car pt)))
(goto-char pt))))
(defun avy--process (candidates action &optional nobackground) (defun avi--process (candidates overlay-fn)
"Select one of CANDIDATES using `avy-read'. "Select one of CANDIDATES using `avy-read'."
Call ACTION on that candidate."
(unwind-protect (unwind-protect
(let ((aw-background (not nobackground))) (let ((aw-background avi-background))
(aw--make-backgrounds (list (selected-window)))
(cl-case (length candidates) (cl-case (length candidates)
(0 (0
(message "zero candidates")) nil)
(1 (1
(funcall action (car candidates))) (car candidates))
(t (t
(funcall (aw--make-backgrounds (list (selected-window)))
action (avy-read (avy-tree candidates avi-keys)
(avy-read (avy-tree candidates avy-keys) overlay-fn
#'aw--lead-overlay #'aw--remove-leading-chars))))
#'aw--remove-leading-chars)))))
(aw--done))) (aw--done)))
(defun avy--regex-candidates (regex &optional wnd) (defun avi--regex-candidates (regex &optional wnd)
"Return all elements that match REGEX in WND. "Return all elements that match REGEX in WND.
Each element of the list is (POSITION . WND)." Each element of the list is ((BEG . END) . WND)."
(let ((we (window-end)) (setq wnd (or wnd (selected-window)))
(let ((we (window-end (selected-window) t))
candidates) candidates)
(setq wnd (or wnd (selected-window)))
(save-window-excursion (save-window-excursion
(select-window wnd) (select-window wnd)
(save-excursion (save-excursion
(goto-char (window-start)) (goto-char (window-start))
(while (re-search-forward regex we t) (while (re-search-forward regex we t)
(push (cons (1- (point)) wnd) candidates))) (push (cons (cons (match-beginning 0)
(match-end 0))
wnd) candidates)))
(nreverse candidates)))) (nreverse candidates))))
(defun avy-jump-char () (defun avi--overlay (str pt wnd)
"Create an overlay with STR at PT in WND."
(let ((ol (make-overlay pt (1+ pt) (window-buffer wnd)))
(old-str (with-selected-window wnd
(buffer-substring pt (1+ pt)))))
(when avi-background
(propertize old-str 'face 'aw-background-face))
(overlay-put ol 'window wnd)
(overlay-put ol 'display (concat str old-str))
(push ol aw-overlays-lead)))
(defun avi--overlay-pre (path leaf)
"Create an overlay with STR at LEAF.
PATH is a list of keys from tree root to LEAF.
LEAF is ((BEG . END) . WND)."
(avi--overlay
(propertize (apply #'string (reverse path))
'face 'avi-lead-face)
(if (consp (car leaf))
(caar leaf)
(car leaf))
(cdr leaf)))
(defun avi--overlay-post (path leaf)
"Create an overlay with STR at LEAF.
PATH is a list of keys from tree root to LEAF.
LEAF is ((BEG . END) . WND)."
(avi--overlay
(propertize (apply #'string (reverse path))
'face 'avi-lead-face)
(if (consp (car leaf))
(cdar leaf)
(car leaf))
(cdr leaf)))
;;* Commands
;;;###autoload
(defun avi-goto-char ()
"Read one char and jump to it in current window." "Read one char and jump to it in current window."
(interactive) (interactive)
(avy--process (avy--regex-candidates (avi--goto
(string (read-char "char: ")) (avi--process
(selected-window)) (avi--regex-candidates
#'avy--goto)) (string (read-char "char: "))
(selected-window))
#'avi--overlay-post)))
(defun avy-jump-double-char () ;;;###autoload
(defun avi-goto-char-2 ()
"Read two chars and jump to them in current window." "Read two chars and jump to them in current window."
(interactive) (interactive)
(avy--process (avy--regex-candidates (avi--goto
(string (avi--process
(read-char "char 1: ") (avi--regex-candidates
(read-char "char 2: ")) (string
(selected-window)) (read-char "char 1: ")
#'avy--goto)) (read-char "char 2: "))
(selected-window))
(defun avy-jump-isearch () #'avi--overlay-post)))
;;;###autoload
(defun avi-isearch ()
"Jump to one of the current isearch candidates." "Jump to one of the current isearch candidates."
(interactive) (interactive)
(let ((candidates (let* ((candidates
(mapcar (lambda (x) (cons (1+ (car x)) (avi--regex-candidates isearch-string))
(cdr x))) (avi-background nil)
(avy--regex-candidates isearch-string)))) (candidate
(avy--process candidates #'avy--goto t) (avi--process candidates #'avi--overlay-post)))
(isearch-done))) (isearch-done)
(avi--goto candidate)))
(defun avy-jump-zero-word ()
"Jump to a word start in current buffer" ;;;###autoload
(defun avi-goto-word-0 ()
"Jump to a word start in current window."
(interactive) (interactive)
(let ((we (window-end)) (let ((we (window-end (selected-window) t))
(avi-keys (number-sequence ?a ?z))
candidates) candidates)
(save-excursion (save-excursion
(goto-char (window-start)) (goto-char (window-start))
@ -115,38 +177,97 @@ Each element of the list is (POSITION . WND)."
(forward-word -1) (forward-word -1)
(push (cons (point) (selected-window)) (push (cons (point) (selected-window))
candidates))) candidates)))
(avy--process (nreverse candidates) (avi--goto
#'avy--goto))) (avi--process (nreverse candidates)
#'avi--overlay-pre))))
(defun avy-jump-one-word () ;;;###autoload
"Jump to a word start in current buffer. (defun avi-goto-word-1 ()
"Jump to a word start in current window.
Read one char with which the word should start." Read one char with which the word should start."
(interactive) (interactive)
(let ((candidates (avy--regex-candidates (let ((candidates (avi--regex-candidates
(string (read-char "char: ")) (string (read-char "char: "))
(selected-window)))) (selected-window))))
(save-excursion (save-excursion
(setq candidates (cl-remove-if-not (setq candidates (cl-remove-if-not
(lambda (x) (lambda (x)
(goto-char (car x)) (goto-char (caar x))
(looking-at "\\b")) (looking-at "\\b"))
(nreverse candidates)))) candidates)))
(avy--process candidates #'avy--goto))) (avi--goto
(avi--process
candidates
#'avi--overlay-pre))))
(defun avy-jump-line () (defun avi--line ()
"Select line in current window."
(let ((avi-background nil)
candidates)
(save-excursion
(save-restriction
(narrow-to-region (window-start) (window-end (selected-window) t))
(goto-char (point-min))
(while (< (point) (point-max))
(push (cons (point) (selected-window))
candidates)
(forward-line 1))))
(avi--process (nreverse candidates) #'avi--overlay-pre)))
;;;###autoload
(defun avi-goto-line ()
"Jump to a line start in current buffer." "Jump to a line start in current buffer."
(interactive) (interactive)
(let ((we (window-end)) (avi--goto (avi--line)))
candidates)
;;;###autoload
(defun avi-copy-line (arg)
"Copy a selected line above the current line.
ARG lines can be used."
(interactive "p")
(let ((start (car (avi--line))))
(move-beginning-of-line nil)
(save-excursion (save-excursion
(goto-char (window-start)) (insert
(while (< (point) we) (buffer-substring-no-properties
(push (cons (point) (selected-window)) start
candidates) (save-excursion
(forward-line 1))) (goto-char start)
(avy--process (nreverse candidates) (move-end-of-line arg)
#'avy--goto (point)))
t))) "\n"))))
;;;###autoload
(defun avi-move-line (arg)
"Move a selected line above the current line.
ARG lines can be used."
(interactive "p")
(let ((start (car (avi--line))))
(move-beginning-of-line nil)
(save-excursion
(save-excursion
(goto-char start)
(move-end-of-line arg)
(kill-region start (point)))
(insert
(current-kill 0)))))
;;;###autoload
(defun avi-copy-region ()
"Select two lines and copy the text between them here."
(interactive)
(let ((beg (car (avi--line)))
(end (car (avi--line)))
(pad (if (bolp) "" "\n")))
(move-beginning-of-line nil)
(save-excursion
(insert
(buffer-substring-no-properties
beg
(save-excursion
(goto-char end)
(line-end-position)))
pad))))
(provide 'avy-jump) (provide 'avy-jump)

Loading…
Cancel
Save