avy.el (avy-org-goto-heading-timer): Simplify

Re #214
This commit is contained in:
Oleh Krehel 2017-08-13 11:54:44 +02:00
parent f4c45d329a
commit 8556274978

55
avy.el
View File

@ -560,33 +560,16 @@ multiple DISPLAY-FN invokations."
"Read one or many characters and jump to matching Org headings. "Read one or many characters and jump to matching Org headings.
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-all-windows (if arg (let ((avy-all-windows (if arg
(not avy-all-windows) (not avy-all-windows)
avy-all-windows)) avy-all-windows)))
(input (avy--read-string-with-timeout))
(regexp (rx-to-string `(seq bol (1+ "*") (1+ space) (0+ not-newline)
,input (0+ not-newline) eol))))
(avy-with avy-goto-char-timer (avy-with avy-goto-char-timer
(avy--process (avy--regex-candidates regexp) (avy--process
(avy--style-fn avy-style))))) (avy--read-candidates
(lambda (input)
(defun avy--read-string-with-timeout () (format "^\\*+ .*\\(%s\\)" input)))
"Read string from minibuffer with a timeout." (avy--style-fn avy-style))
;; It's a shame that only `read-char' has the timeout option, so we (org-back-to-heading))))
;; have to do this funky loop ourselves, instead of
;; e.g. `read-string' with a timeout.
(cl-loop with charnum
with string = ""
while (not (equal 13 charnum))
for charnum = (read-char (format "Prompt: %s" string )
t
(unless (string-empty-p string)
avy-timeout-seconds))
if (and charnum
(not (equal 13 charnum)))
concat (make-string 1 charnum) into string
else do (setq charnum 13)
finally return string))
;;** Rest ;;** Rest
(defun avy-window-list () (defun avy-window-list ()
@ -1809,7 +1792,7 @@ newline."
"How many seconds to wait for the second char." "How many seconds to wait for the second char."
:type 'float) :type 'float)
(defun avy--read-candidates () (defun avy--read-candidates (&optional re-builder)
"Read as many chars as possible and return their occurences. "Read as many chars as possible and return their occurences.
At least one char must be read, and then repeatedly one next char At least one char must be read, and then repeatedly one next char
may be read if it is entered before `avy-timeout-seconds'. `C-h' may be read if it is entered before `avy-timeout-seconds'. `C-h'
@ -1817,8 +1800,14 @@ or `DEL' deletes the last char entered, and `RET' exits with the
currently read string immediately instead of waiting for another currently read string immediately instead of waiting for another
char for `avy-timeout-seconds'. char for `avy-timeout-seconds'.
The format of the result is the same as that of `avy--regex-candidates'. The format of the result is the same as that of `avy--regex-candidates'.
This function obeys `avy-all-windows' setting." This function obeys `avy-all-windows' setting.
(let ((str "") char break overlays regex) RE-BUILDER is a function that takes a string and returns a regex.
When nil, `regexp-quote' is used.
If a group is captured, the first group is highlighted.
Otherwise, the whole regex is highlighted."
(let ((str "")
(re-builder (or re-builder #'regexp-quote))
char break overlays regex)
(unwind-protect (unwind-protect
(progn (progn
(while (and (not break) (while (and (not break)
@ -1856,12 +1845,12 @@ This function obeys `avy-all-windows' setting."
(window-end (selected-window) t))) (window-end (selected-window) t)))
(save-excursion (save-excursion
(goto-char (car pair)) (goto-char (car pair))
(setq regex (regexp-quote str)) (setq regex (funcall re-builder str))
(while (re-search-forward regex (cdr pair) t) (while (re-search-forward regex (cdr pair) t)
(unless (get-char-property (1- (point)) 'invisible) (unless (get-char-property (1- (point)) 'invisible)
(let ((ov (make-overlay (let* ((idx (if (= (length (match-data)) 4) 1 0))
(match-beginning 0) (ov (make-overlay
(match-end 0)))) (match-beginning idx) (match-end idx))))
(setq found t) (setq found t)
(push ov overlays) (push ov overlays)
(overlay-put (overlay-put