summaryrefslogtreecommitdiff
path: root/helm-dabbrev.el
diff options
context:
space:
mode:
authorThierry Volpiatto <thierry.volpiatto@gmail.com>2018-07-23 06:57:33 +0200
committerThierry Volpiatto <thierry.volpiatto@gmail.com>2018-07-23 08:57:36 +0200
commit9494099ee1e03ebf56c6361bfb0cc2a5eb1c0afa (patch)
treea8b7a38b98e9d3636739c753deadc8b13fb1ef21 /helm-dabbrev.el
parent9fdbd606b782646f9df6218e49804ff5ec2bbe6f (diff)
Compute dabbrev cands async
* helm-dabbrev.el (helm-dabbrev--get-candidates): Takes one more arg limit. (helm-dabbrev--already-tried): New. (helm-dabbrev--current-thread): New. (helm-dabbrev): Compute candidates inside a thread if possible.
Diffstat (limited to 'helm-dabbrev.el')
-rw-r--r--helm-dabbrev.el59
1 files changed, 42 insertions, 17 deletions
diff --git a/helm-dabbrev.el b/helm-dabbrev.el
index 92050ed6..956297bb 100644
--- a/helm-dabbrev.el
+++ b/helm-dabbrev.el
@@ -221,12 +221,12 @@ removed."
sep-regexp ""
(match-string-no-properties 99))))))
-(defun helm-dabbrev--get-candidates (abbrev)
+(defun helm-dabbrev--get-candidates (abbrev &optional limit)
(cl-assert abbrev nil "[No Match]")
(with-current-buffer (current-buffer)
(let* ((dabbrev-get (lambda (str all-bufs)
(helm-dabbrev--collect
- str helm-dabbrev-candidates-number-limit
+ str (or limit helm-dabbrev-candidates-number-limit)
(cl-case helm-dabbrev-case-fold-search
(smart (helm-set-case-fold-search-1 abbrev))
(t helm-dabbrev-case-fold-search))
@@ -250,6 +250,8 @@ removed."
'helm-insert-completion-at-point
beg end candidate))))
+(defvar helm-dabbrev--already-tried nil)
+(defvar helm-dabbrev--current-thread nil)
;;;###autoload
(defun helm-dabbrev ()
"Preconfigured helm for dynamic abbreviations."
@@ -272,18 +274,33 @@ removed."
;; in the meaning time.
(not (eq last-command 'helm-dabbrev)))
(setq helm-dabbrev--data nil))
+ ;; When candidates are requested in helm directly without cycling,
+ ;; we need them right now before running helm, so no need to use a
+ ;; thread here.
(when cycling-disabled-p
(setq helm-dabbrev--cache (helm-dabbrev--get-candidates dabbrev)))
+ ;; The idea here is to compute only the candidates needed to cycle
+ ;; and while cycling, compute the whole list in background with a
+ ;; thread so that user doesn't have to wait candidates are ready
+ ;; at startup.
(unless (or cycling-disabled-p
(helm-dabbrev-info-p helm-dabbrev--data))
- (setq helm-dabbrev--cache (helm-dabbrev--get-candidates dabbrev))
+ (if (fboundp 'make-thread)
+ (setq helm-dabbrev--current-thread
+ (make-thread
+ (lambda ()
+ (setq helm-dabbrev--cache
+ (helm-dabbrev--get-candidates dabbrev)))))
+ (setq helm-dabbrev--cache
+ (helm-dabbrev--get-candidates dabbrev)))
(setq helm-dabbrev--data
(make-helm-dabbrev-info
:dabbrev dabbrev
:limits limits
:iterator
(helm-iter-list
- (cl-loop for i in helm-dabbrev--cache
+ (cl-loop for i in (helm-dabbrev--get-candidates
+ dabbrev helm-dabbrev-cycle-threshold)
when (and i (string-match
(concat "^" (regexp-quote dabbrev)) i))
collect i into selection
@@ -308,8 +325,11 @@ removed."
(car (helm-dabbrev-info-limits helm-dabbrev--data))
(cdr limits) it)
;; Move already tried candidates to end of list.
- (setq helm-dabbrev--cache (append (remove it helm-dabbrev--cache)
- (list it))))
+ (push it helm-dabbrev--already-tried))
+ ;; Cycling is finished, ensure helm-dabbrev--cache have
+ ;; finished to complete before continuing by blocking thread.
+ (when (fboundp 'thread-join)
+ (thread-join helm-dabbrev--current-thread))
;; If the length of candidates is only one when computed
;; that's mean the unique matched item have already been
;; inserted by the iterator, so no need to reinsert the old dabbrev,
@@ -328,17 +348,22 @@ removed."
(delete-region (car limits) (point))
(insert dabbrev))
(with-helm-show-completion (car limits) (cdr limits)
- (helm :sources (helm-build-in-buffer-source "Dabbrev Expand"
- :data helm-dabbrev--cache
- :persistent-action 'ignore
- :persistent-help "DoNothing"
- :keymap helm-dabbrev-map
- :action 'helm-dabbrev-default-action
- :group 'helm-dabbrev)
- :buffer "*helm dabbrev*"
- :input (concat "^" dabbrev " ")
- :resume 'noresume
- :allow-nest t)))))))
+ (unwind-protect
+ (helm :sources (helm-build-in-buffer-source "Dabbrev Expand"
+ :data (cl-loop for cand in helm-dabbrev--cache
+ unless (member cand helm-dabbrev--already-tried)
+ collect cand into lst
+ finally return (append lst helm-dabbrev--already-tried))
+ :persistent-action 'ignore
+ :persistent-help "DoNothing"
+ :keymap helm-dabbrev-map
+ :action 'helm-dabbrev-default-action
+ :group 'helm-dabbrev)
+ :buffer "*helm dabbrev*"
+ :input (concat "^" dabbrev " ")
+ :resume 'noresume
+ :allow-nest t)
+ (setq helm-dabbrev--already-tried nil))))))))
(provide 'helm-dabbrev)