-;;; avy.el --- set-based completion -*- lexical-binding: t -*-
+;;; avy.el --- tree-based completion -*- lexical-binding: t -*-
;; Copyright (C) 2015 Free Software Foundation, Inc.
;; possible that the path-len must be incremented, e.g., if we're matching
;; for x and a buffer contains xaxbxcx only every second subsequence is
;; usable for the four matches.
- (let* ((path-len (ceiling (log (length lst) (length keys))))
- (alist (avy--path-alist-1 lst path-len keys)))
- (while (not alist)
- (cl-incf path-len)
- (setq alist (avy--path-alist-1 lst path-len keys)))
- (let* ((len (length (caar alist)))
- (i 0))
- (setq avy-current-path "")
- (while (< i len)
- (dolist (x (reverse alist))
- (avy--overlay-at-full (reverse (car x)) (cdr x)))
- (let ((char (funcall avy-translate-char-function (read-key))))
- (avy--remove-leading-chars)
- (setq alist
- (delq nil
- (mapcar (lambda (x)
- (when (eq (caar x) char)
- (cons (cdr (car x)) (cdr x))))
- alist)))
- (setq avy-current-path
- (concat avy-current-path (string (avy--key-to-char char))))
- (cl-incf i)
- (unless alist
- (funcall avy-handler-function char))))
- (cdar alist))))
+ (catch 'done
+ (let* ((path-len (ceiling (log (length lst) (length keys))))
+ (alist (avy--path-alist-1 lst path-len keys)))
+ (while (not alist)
+ (cl-incf path-len)
+ (setq alist (avy--path-alist-1 lst path-len keys)))
+ (let* ((len (length (caar alist)))
+ (i 0))
+ (setq avy-current-path "")
+ (while (< i len)
+ (dolist (x (reverse alist))
+ (avy--overlay-at-full (reverse (car x)) (cdr x)))
+ (let ((char (funcall avy-translate-char-function (read-key))))
+ (avy--remove-leading-chars)
+ (setq alist
+ (delq nil
+ (mapcar (lambda (x)
+ (when (eq (caar x) char)
+ (cons (cdr (car x)) (cdr x))))
+ alist)))
+ (setq avy-current-path
+ (concat avy-current-path (string (avy--key-to-char char))))
+ (cl-incf i)
+ (unless alist
+ (funcall avy-handler-function char))))
+ (cdar alist)))))
;;** Rest
(defun avy-window-list ()
(t
(error "Unrecognized option: %S" avy-all-windows))))
+(defcustom avy-all-windows-alt t
+ "The alternative `avy-all-windows' for use with \\[universal-argument]."
+ :type '(choice
+ (const :tag "All windows on the current frame" t)
+ (const :tag "All windows on all frames" all-frames)))
+
(defmacro avy-dowindows (flip &rest body)
"Depending on FLIP and `avy-all-windows' run BODY in each or selected window."
(declare (indent 1)
(debug (form body)))
`(let ((avy-all-windows (if ,flip
- (not avy-all-windows)
+ avy-all-windows-alt
avy-all-windows)))
(dolist (wnd (avy-window-list))
(with-selected-window wnd
When GROUP is non-nil, (BEG . END) should delimit that regex group."
(setq group (or group 0))
(let ((case-fold-search (or avy-case-fold-search
- (not (string= regex (upcase regex)))))
+ (string= regex (downcase regex))))
candidates)
- (avy-dowindows nil
+ (avy-dowindows current-prefix-arg
(dolist (pair (avy--find-visible-regions
(or beg (window-start))
(or end (window-end (selected-window) t))))
(defun avy--overlay (str beg end wnd &optional compose-fn)
"Create an overlay with STR from BEG to END in WND.
COMPOSE-FN is a lambda that concatenates the old string at BEG with STR."
- (when (<= (1+ beg) (with-selected-window wnd (point-max)))
- (let* ((beg (+ beg avy--overlay-offset))
- (ol (make-overlay beg (or end (1+ beg)) (window-buffer wnd)))
- (old-str (avy--old-str beg wnd))
- (os-line-prefix (get-text-property 0 'line-prefix old-str))
- (os-wrap-prefix (get-text-property 0 'wrap-prefix old-str)))
- (when os-line-prefix
- (add-text-properties 0 1 `(line-prefix ,os-line-prefix) str))
- (when os-wrap-prefix
- (add-text-properties 0 1 `(wrap-prefix ,os-wrap-prefix) str))
- (overlay-put ol 'window wnd)
- (overlay-put ol 'category 'avy)
- (overlay-put ol 'display (funcall
- (or compose-fn #'concat)
- str old-str))
- (push ol avy--overlays-lead))))
+ (let ((eob (with-selected-window wnd (point-max))))
+ (when (<= beg eob)
+ (let* ((beg (+ beg avy--overlay-offset))
+ (ol (make-overlay beg (or end (1+ beg)) (window-buffer wnd)))
+ (old-str (if (eq beg eob) "" (avy--old-str beg wnd)))
+ (os-line-prefix (get-text-property 0 'line-prefix old-str))
+ (os-wrap-prefix (get-text-property 0 'wrap-prefix old-str))
+ other-ol)
+ (when os-line-prefix
+ (add-text-properties 0 1 `(line-prefix ,os-line-prefix) str))
+ (when os-wrap-prefix
+ (add-text-properties 0 1 `(wrap-prefix ,os-wrap-prefix) str))
+ (when (setq other-ol (cl-find-if
+ (lambda (o) (overlay-get o 'goto-address))
+ (overlays-at beg)))
+ (add-text-properties
+ 0 (length old-str)
+ `(face ,(overlay-get other-ol 'face)) old-str))
+ (overlay-put ol 'window wnd)
+ (overlay-put ol 'category 'avy)
+ (overlay-put ol (if (eq beg eob)
+ 'after-string
+ 'display)
+ (funcall
+ (or compose-fn #'concat)
+ str old-str))
+ (push ol avy--overlays-lead)))))
(defcustom avy-highlight-first nil
"When non-nil highlight the first decision char with `avy-lead-face-0'.
len))
lep)))
(when (and (bound-and-true-p visual-line-mode)
- (> len (- end beg)))
+ (> len (- end beg))
+ (not (eq lep beg)))
(setq len (- end beg))
(let ((old-str (apply #'string (reverse path))))
(setq str
(declare-function subword-backward "subword")
(defvar subword-backward-regexp)
+(defcustom avy-subword-extra-word-chars '(?{ ?= ?} ?* ?: ?> ?<)
+ "A list of characters that should temporarily match \"\\w\".
+This variable is used by `avy-goto-subword-0' and `avy-goto-subword-1'."
+ :type '(repeat character))
+
;;;###autoload
(defun avy-goto-subword-0 (&optional arg predicate)
"Jump to a word or subword start.
"\\(\\(\\W\\|[[:lower:][:digit:]]\\)\\([!-/:@`~[:upper:]]+\\W*\\)\\|\\W\\w+\\)")
candidates)
(avy-dowindows arg
- (let ((ws (window-start))
- window-cands)
- (save-excursion
- (goto-char (window-end (selected-window) t))
- (subword-backward)
- (while (> (point) ws)
- (when (or (null predicate)
- (and predicate (funcall predicate)))
- (unless (get-char-property (point) 'invisible)
- (push (cons (point) (selected-window)) window-cands)))
- (subword-backward)))
- (setq candidates (nconc candidates window-cands))))
+ (let ((syn-tbl (copy-syntax-table)))
+ (dolist (char avy-subword-extra-word-chars)
+ (modify-syntax-entry char "w" syn-tbl))
+ (with-syntax-table syn-tbl
+ (let ((ws (window-start))
+ window-cands)
+ (save-excursion
+ (goto-char (window-end (selected-window) t))
+ (subword-backward)
+ (while (> (point) ws)
+ (when (or (null predicate)
+ (and predicate (funcall predicate)))
+ (unless (get-char-property (point) 'invisible)
+ (push (cons (point) (selected-window)) window-cands)))
+ (subword-backward)))
+ (setq candidates (nconc candidates window-cands))))))
(avy--process candidates (avy--style-fn avy-style)))))
;;;###autoload
(setq temporary-goal-column 0)
(line-move-visual 1 t))
(forward-line 1)))))))
- (setq avy-action #'identity)
- (avy--process (nreverse candidates) (avy--style-fn avy-style))))
+ (let ((avy-action #'identity))
+ (avy--process (nreverse candidates) (avy--style-fn avy-style)))))
;;;###autoload
(defun avy-goto-line (&optional arg)
Otherwise, forward to `goto-line' with ARG."
(interactive "p")
+ (setq arg (or arg 1))
(if (not (memq arg '(1 4)))
(progn
(goto-char (point-min))
"Goto line: " (string char))))
(when line
(avy-push-mark)
- (goto-char (point-min))
- (forward-line (1- (string-to-number line)))
+ (save-restriction
+ (widen)
+ (goto-char (point-min))
+ (forward-line (1- (string-to-number line))))
(throw 'done 'exit))))))
(r (avy--line (eq arg 4))))
(unless (eq r t)
(defun avy-goto-line-above ()
"Goto visible line above the cursor."
(interactive)
- (let ((r (avy--line nil (window-start) (point))))
+ (let* ((avy-all-windows nil)
+ (r (avy--line nil (window-start)
+ (line-beginning-position))))
(unless (eq r t)
(avy-action-goto r))))
(defun avy-goto-line-below ()
"Goto visible line below the cursor."
(interactive)
- (let ((r (avy--line
- nil (point)
- (window-end (selected-window) t))))
+ (let* ((avy-all-windows nil)
+ (r (avy--line
+ nil (line-beginning-position 2)
+ (window-end (selected-window) t))))
(unless (eq r t)
(avy-action-goto r))))
+(defcustom avy-line-insert-style 'above
+ "How to insert the newly copied/cut line."
+ :type '(choice
+ (const :tag "Above" above)
+ (const :tag "Below" below)))
+
;;;###autoload
(defun avy-copy-line (arg)
"Copy a selected line above the current line.
ARG lines can be used."
(interactive "p")
- (avy-with avy-copy-line
- (let ((start (avy--line)))
- (move-beginning-of-line nil)
- (save-excursion
- (insert
- (buffer-substring-no-properties
- start
- (save-excursion
- (goto-char start)
- (move-end-of-line arg)
- (point)))
- "\n")))))
+ (let ((initial-window (selected-window)))
+ (avy-with avy-copy-line
+ (let* ((start (avy--line))
+ (str (buffer-substring-no-properties
+ start
+ (save-excursion
+ (goto-char start)
+ (move-end-of-line arg)
+ (point)))))
+ (select-window initial-window)
+ (cond ((eq avy-line-insert-style 'above)
+ (beginning-of-line)
+ (save-excursion
+ (insert str "\n")))
+ ((eq avy-line-insert-style 'below)
+ (end-of-line)
+ (insert "\n" str)
+ (beginning-of-line))
+ (t
+ (user-error "Unexpected `avy-line-insert-style'")))))))
;;;###autoload
(defun avy-move-line (arg)
"Move a selected line above the current line.
ARG lines can be used."
(interactive "p")
- (avy-with avy-move-line
- (let ((start (avy--line)))
- (move-beginning-of-line nil)
- (save-excursion
+ (let ((initial-window (selected-window)))
+ (avy-with avy-move-line
+ (let ((start (avy--line)))
(save-excursion
(goto-char start)
(kill-whole-line arg))
- (insert
- (current-kill 0))))))
+ (select-window initial-window)
+ (cond ((eq avy-line-insert-style 'above)
+ (beginning-of-line)
+ (save-excursion
+ (insert
+ (current-kill 0))))
+ ((eq avy-line-insert-style 'below)
+ (end-of-line)
+ (newline)
+ (save-excursion
+ (insert (substring (current-kill 0) 0 -1))))
+ (t
+ (user-error "Unexpected `avy-line-insert-style'")))))))
;;;###autoload
-(defun avy-copy-region ()
- "Select two lines and copy the text between them here."
- (interactive)
- (avy-with avy-copy-region
- (let ((beg (avy--line))
- (end (avy--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)))))
+(defun avy-copy-region (arg)
+ "Select two lines and copy the text between them to point.
+
+The window scope is determined by `avy-all-windows' or
+`avy-all-windows-alt' when ARG is non-nil."
+ (interactive "P")
+ (let ((initial-window (selected-window)))
+ (avy-with avy-copy-region
+ (let* ((beg (save-selected-window
+ (avy--line arg)))
+ (end (avy--line arg))
+ (str (buffer-substring-no-properties
+ beg
+ (save-excursion
+ (goto-char end)
+ (line-end-position)))))
+ (select-window initial-window)
+ (cond ((eq avy-line-insert-style 'above)
+ (beginning-of-line)
+ (save-excursion
+ (insert str "\n")))
+ ((eq avy-line-insert-style 'below)
+ (end-of-line)
+ (newline)
+ (save-excursion
+ (insert str)))
+ (t
+ (user-error "Unexpected `avy-line-insert-style'")))))))
;;;###autoload
(defun avy-setup-default ()
(setq str (concat str (list char)))))
;; Highlight
(when (>= (length str) 1)
- (let (found)
- (avy-dowindows nil
+ (let ((case-fold-search
+ (or avy-case-fold-search (string= str (downcase str))))
+ found)
+ (avy-dowindows current-prefix-arg
(dolist (pair (avy--find-visible-regions
(window-start)
(window-end (selected-window) t)))