Always default to avy-keys but allow for per-command overrides

Using the new `avy-keys-alist` one can override the default `avy-keys`
on a per-command basis.  That's much better than hard-coding ?a-?z for
some commands and using the defaults for some others.

Fixes #20
This commit is contained in:
Tassilo Horn 2015-05-08 22:56:33 +02:00 committed by Oleh Krehel
parent a55858540b
commit 0e049bbc44

View File

@ -36,8 +36,30 @@
:prefix "avy-") :prefix "avy-")
(defcustom avy-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l) (defcustom avy-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l)
"Keys for jumping." "Default keys for jumping."
:type '(repeat character)) :type '(repeat :tag "Keys" character))
(defcustom avy-keys-alist nil
"Alist of avy-jump commands to avy-keys overriding the default avy-keys."
:type '(alist :key-type (choice :tag "Command"
(const avy-goto-char)
(const avy-goto-char-2)
(const avy-isearch)
(const avy-goto-line)
(const avy-goto-subword-0)
(const avy-goto-subword-1)
(const avy-goto-word-0)
(const avy-goto-word-1)
(const avy-copy-line)
(const avy-copy-region)
(const avy-move-line))
:value-type (repeat :tag "Keys" character)))
(defmacro avy--with-avy-keys (command &rest body)
(declare (indent 1))
`(let ((avy-keys (or (cdr (assq ',command avy-keys-alist))
avy-keys)))
,@body))
(defcustom avy-background nil (defcustom avy-background nil
"When non-nil, a gray background will be added during the selection." "When non-nil, a gray background will be added during the selection."
@ -269,44 +291,47 @@ STYLE determines the leading char overlay style."
"Read one char and jump to it. "Read one char and jump to it.
The window scope is determined by `avy-all-windows' (ARG negates it)." The window scope is determined by `avy-all-windows' (ARG negates it)."
(interactive "P") (interactive "P")
(avy--with-avy-keys avy-goto-char
(avy--generic-jump (avy--generic-jump
(let ((c (read-char "char: "))) (let ((c (read-char "char: ")))
(if (= 13 c) (if (= 13 c)
"\n" "\n"
(regexp-quote (string c)))) (regexp-quote (string c))))
arg arg
avy-goto-char-style)) avy-goto-char-style)))
;;;###autoload ;;;###autoload
(defun avy-goto-char-2 (&optional arg) (defun avy-goto-char-2 (&optional arg)
"Read two consecutive chars and jump to the first one. "Read two consecutive chars and jump to the first one.
The window scope is determined by `avy-all-windows' (ARG negates it)." The window scope is determined by `avy-all-windows' (ARG negates it)."
(interactive "P") (interactive "P")
(avy--with-avy-keys avy-goto-char-2
(avy--generic-jump (avy--generic-jump
(regexp-quote (string (regexp-quote (string
(read-char "char 1: ") (read-char "char 1: ")
(read-char "char 2: "))) (read-char "char 2: ")))
arg arg
avy-goto-char-style)) avy-goto-char-style)))
;;;###autoload ;;;###autoload
(defun avy-isearch () (defun avy-isearch ()
"Jump to one of the current isearch candidates." "Jump to one of the current isearch candidates."
(interactive) (interactive)
(avy--with-avy-keys avy-isearch
(let* ((candidates (let* ((candidates
(avy--regex-candidates isearch-string)) (avy--regex-candidates isearch-string))
(avy-background nil) (avy-background nil)
(candidate (candidate
(avy--process candidates #'avy--overlay-post))) (avy--process candidates #'avy--overlay-post)))
(isearch-done) (isearch-done)
(avy--goto candidate))) (avy--goto candidate))))
;;;###autoload ;;;###autoload
(defun avy-goto-word-0 (arg) (defun avy-goto-word-0 (arg)
"Jump to a word start. "Jump to a word 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)."
(interactive "P") (interactive "P")
(let ((avy-keys (number-sequence ?a ?z))) (avy--with-avy-keys avy-goto-word-0
(avy--generic-jump "\\b\\sw" arg avy-goto-word-style))) (avy--generic-jump "\\b\\sw" arg avy-goto-word-style)))
;;;###autoload ;;;###autoload
@ -314,6 +339,7 @@ The window scope is determined by `avy-all-windows' (ARG negates it)."
"Read one char at word start and jump there. "Read one char at word start and jump there.
The window scope is determined by `avy-all-windows' (ARG negates it)." The window scope is determined by `avy-all-windows' (ARG negates it)."
(interactive "P") (interactive "P")
(avy--with-avy-keys avy-goto-word-1
(let* ((str (string (read-char "char: "))) (let* ((str (string (read-char "char: ")))
(regex (cond ((string= str ".") (regex (cond ((string= str ".")
"\\.") "\\.")
@ -324,7 +350,7 @@ The window scope is determined by `avy-all-windows' (ARG negates it)."
(concat (concat
"\\b" "\\b"
str))))) str)))))
(avy--generic-jump regex arg avy-goto-word-style))) (avy--generic-jump regex arg avy-goto-word-style))))
(declare-function subword-backward "subword") (declare-function subword-backward "subword")
@ -338,10 +364,8 @@ When PREDICATE is non-nil it's a function of zero parameters that
should return true." should return true."
(interactive "P") (interactive "P")
(require 'subword) (require 'subword)
(let ((avy-keys (if predicate (avy--with-avy-keys avy-goto-subword-0
avy-keys (let ((case-fold-search nil)
(number-sequence ?a ?z)))
(case-fold-search nil)
candidates) candidates)
(avy-dowindows arg (avy-dowindows arg
(let ((ws (window-start))) (let ((ws (window-start)))
@ -354,7 +378,7 @@ should return true."
(push (cons (point) (selected-window)) candidates)) (push (cons (point) (selected-window)) candidates))
(subword-backward))))) (subword-backward)))))
(avy--goto (avy--goto
(avy--process candidates (avy--style-fn avy-goto-word-style))))) (avy--process candidates (avy--style-fn avy-goto-word-style))))))
;;;###autoload ;;;###autoload
(defun avy-goto-subword-1 (&optional arg) (defun avy-goto-subword-1 (&optional arg)
@ -362,9 +386,10 @@ should return true."
The window scope is determined by `avy-all-windows' (ARG negates it). The window scope is determined by `avy-all-windows' (ARG negates it).
The case is ignored." The case is ignored."
(interactive "P") (interactive "P")
(avy--with-avy-keys avy-goto-subword-1
(let ((char (downcase (read-char "char: ")))) (let ((char (downcase (read-char "char: "))))
(avy-goto-subword-0 (avy-goto-subword-0
arg (lambda () (eq (downcase (char-after)) char))))) arg (lambda () (eq (downcase (char-after)) char))))))
(defun avy--line (&optional arg) (defun avy--line (&optional arg)
"Select line in current window." "Select line in current window."
@ -387,13 +412,15 @@ The case is ignored."
(defun avy-goto-line (&optional arg) (defun avy-goto-line (&optional arg)
"Jump to a line start in current buffer." "Jump to a line start in current buffer."
(interactive "P") (interactive "P")
(avy--goto (avy--line arg))) (avy--with-avy-keys avy-goto-line
(avy--goto (avy--line arg))))
;;;###autoload ;;;###autoload
(defun avy-copy-line (arg) (defun avy-copy-line (arg)
"Copy a selected line above the current line. "Copy a selected line above the current line.
ARG lines can be used." ARG lines can be used."
(interactive "p") (interactive "p")
(avy--with-avy-keys avy-copy-line
(let ((start (car (avy--line)))) (let ((start (car (avy--line))))
(move-beginning-of-line nil) (move-beginning-of-line nil)
(save-excursion (save-excursion
@ -404,13 +431,14 @@ ARG lines can be used."
(goto-char start) (goto-char start)
(move-end-of-line arg) (move-end-of-line arg)
(point))) (point)))
"\n")))) "\n")))))
;;;###autoload ;;;###autoload
(defun avy-move-line (arg) (defun avy-move-line (arg)
"Move a selected line above the current line. "Move a selected line above the current line.
ARG lines can be used." ARG lines can be used."
(interactive "p") (interactive "p")
(avy--with-avy-keys avy-move-line
(let ((start (car (avy--line)))) (let ((start (car (avy--line))))
(move-beginning-of-line nil) (move-beginning-of-line nil)
(save-excursion (save-excursion
@ -420,12 +448,13 @@ ARG lines can be used."
(kill-region start (point))) (kill-region start (point)))
(insert (insert
(current-kill 0) (current-kill 0)
"\n")))) "\n")))))
;;;###autoload ;;;###autoload
(defun avy-copy-region () (defun avy-copy-region ()
"Select two lines and copy the text between them here." "Select two lines and copy the text between them here."
(interactive) (interactive)
(avy--with-avy-keys avy-copy-region
(let ((beg (car (avy--line))) (let ((beg (car (avy--line)))
(end (car (avy--line))) (end (car (avy--line)))
(pad (if (bolp) "" "\n"))) (pad (if (bolp) "" "\n")))
@ -437,7 +466,7 @@ ARG lines can be used."
(save-excursion (save-excursion
(goto-char end) (goto-char end)
(line-end-position))) (line-end-position)))
pad)))) pad)))))
;;;###autoload ;;;###autoload
(defun avy-setup-default () (defun avy-setup-default ()