branch: externals/capf-autosuggest
commit 8507236eab1a57969fa28a9858a7ade5e6de0eff
Author: jakanakaevangeli <[email protected]>
Commit: jakanakaevangeli <[email protected]>
Generalize capf-autosuggest-capf-functions
Also, by default, only consider auto-suggestions if at eol.
---
capf-autosuggest.el | 32 +++++++++++++++++++++++---------
history-capf.el | 6 +++---
2 files changed, 26 insertions(+), 12 deletions(-)
diff --git a/capf-autosuggest.el b/capf-autosuggest.el
index 4a2c275..a61d976 100644
--- a/capf-autosuggest.el
+++ b/capf-autosuggest.el
@@ -39,11 +39,12 @@
"Face used for auto suggestions."
:group 'completion)
-(defvar-local capf-autosuggest-capf nil
+(defvar capf-autosuggest-capf-functions '(capf-autosuggest-orig-if-at-eol-capf)
"`completion-at-point-functions', used by capf-autossugest.
-If nil, capf-autosuggest will use
-`completion-at-point-functions', otherwise it will use this hook
-variable.")
+It is used instead of the standard
+`completion-at-point-functions', but the default value contains
+`capf-autosuggest-orig-if-at-eol-capf' which searches the
+standard capf functions, if point is at the end of line.")
(defvar capf-autosuggest-all-completions-only-one nil
"Non-nil if only the first result of `all-completions' is of interest.
@@ -58,6 +59,22 @@ hint to only return a list of one element for optimization.")
(defvar capf-autosuggest-active-mode)
+(defun capf-autosuggest-orig-capf (&optional capf-functions)
+ "A capf that chooses from hook variable CAPF-FUNCTIONS.
+CAPF-FUNCTIONS defaults to `completion-at-point-functions'.
+Don't add this function to `completion-at-point-functions', as
+this will result in an infinite loop. Useful for adding to
+`capf-autosuggest-capf-functions', making it search the standard
+capf functions."
+ (cdr (run-hook-wrapped (or capf-functions 'completion-at-point-functions)
+ #'completion--capf-wrapper 'all)))
+
+(defun capf-autosuggest-orig-if-at-eol-capf ()
+ "`capf-autosuggest-orig-capf' if at the end of buffer.
+Otherwise, return nil."
+ (when (eolp)
+ (capf-autosuggest-orig-capf)))
+
(defun capf-autosuggest--post-h ()
"Create an auto-suggest overlay."
(if completion-in-region-mode
@@ -69,11 +86,8 @@ hint to only return a list of one element for optimization.")
(capf-autosuggest-active-mode -1))))
(unless capf-autosuggest-active-mode
- (pcase (run-hook-wrapped (if capf-autosuggest-capf
- 'capf-autosuggest-capf
- 'completion-at-point-functions)
- #'completion--capf-wrapper 'all)
- (`(,_fun ,beg ,end ,table . ,plist)
+ (pcase (capf-autosuggest-orig-capf 'capf-autosuggest-capf-functions)
+ (`(,beg ,end ,table . ,plist)
(let* ((pred (plist-get plist :predicate))
(string (buffer-substring-no-properties beg end))
;; See `completion-emacs21-all-completions'
diff --git a/history-capf.el b/history-capf.el
index df4a71b..f86a299 100644
--- a/history-capf.el
+++ b/history-capf.el
@@ -36,7 +36,7 @@
(defvar comint-use-prompt-regexp)
(defvar eshell-history-ring)
(defvar eshell-last-output-end)
-(declare-function capf-autosuggest-mode "capf-autosuggest")
+(autoload 'capf-autosuggest-mode "capf-autosuggest")
(declare-function eshell-bol "esh-mode")
(declare-function comint-previous-matching-input-from-input "comint")
(declare-function eshell-previous-matching-input-from-input "em-hist")
@@ -111,13 +111,13 @@ Is only applicable if point is after the last prompt."
(defun history-capf-setup-autosuggest-comint ()
"Setup capf-autosuggest for history suggestion in comint."
(capf-autosuggest-mode)
- (add-hook 'capf-autosuggest-capf #'history-capf-comint))
+ (add-hook 'capf-autosuggest-capf-functions #'history-capf-comint nil t))
;;;###autoload
(defun history-capf-setup-autosuggest-eshell ()
"Setup capf-autosuggest for history suggestion in eshell."
(capf-autosuggest-mode)
- (add-hook 'capf-autosuggest-capf #'history-capf-eshell))
+ (add-hook 'capf-autosuggest-capf-functions #'history-capf-eshell nil t))
(with-eval-after-load 'capf-autosuggest
(define-key capf-autosuggest-active-mode-map