emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] externals/cape 84c128ae9b 013/146: Add cape-merge-capfs (Fix #2)


From: ELPA Syncer
Subject: [elpa] externals/cape 84c128ae9b 013/146: Add cape-merge-capfs (Fix #2)
Date: Sun, 9 Jan 2022 20:57:38 -0500 (EST)

branch: externals/cape
commit 84c128ae9bbc80a5e890f4edf16e0c112f9374da
Author: Daniel Mendler <mail@daniel-mendler.de>
Commit: Daniel Mendler <mail@daniel-mendler.de>

    Add cape-merge-capfs (Fix #2)
---
 README.org | 14 +++++++++++++-
 cape.el    | 36 ++++++++++++++++++++++++++++++++++++
 2 files changed, 49 insertions(+), 1 deletion(-)

diff --git a/README.org b/README.org
index f35596c31a..90d0b96180 100644
--- a/README.org
+++ b/README.org
@@ -62,8 +62,20 @@ completion mechanism. The adapter is still experimental and 
may have certain edg
 cases. It can be used as follows:
 
 #+begin_src emacs-lisp
-  ;; Use Company backends as Capfs!
+  ;; Use Company backends as Capfs.
   (setq-local completion-at-point-functions
     (mapcar #'cape-company-to-capf
       (list #'company-files #'company-ispell #'company-dabbrev)))
 #+end_src
+
+* Merging Capf
+
+Cape supports merging multiple Capfs using the function ~cape-merge-capfs~. 
This
+feature is experimental and works only for well-behaved Capfs and simple
+completion tables. But it is a start to emulate the corresponding Company 
feature.
+
+#+begin_src emacs-lisp
+  ;; Merge the dabbrev and dict capfs, display candidates together.
+  (setq-local completion-at-point-functions
+              (list (cape-merge-capfs #'cape-dabbrev-capf #'cape-dict-capf)))
+#+end_src
diff --git a/cape.el b/cape.el
index 7ab68b3627..fb4507e0d6 100644
--- a/cape.el
+++ b/cape.el
@@ -29,6 +29,8 @@
 
 ;;; Code:
 
+(eval-when-compile (require 'cl-lib))
+
 (defgroup cape nil
   "Completion At Point Extensions."
   :group 'convenience
@@ -464,6 +466,40 @@
                                 (user-error "No keywords for %s" major-mode))
                             cape--keyword-properties))
 
+(defun cape--merged-function (ht prop)
+  "Return merged function for PROP given HT."
+  (lambda (x)
+    (when-let (fun (plist-get (gethash x ht) prop))
+      (funcall fun x))))
+
+(defun cape-merge-capfs (&rest capfs)
+  "Merge CAPFS and return new Capf which includes all candidates."
+  (lambda ()
+    (when-let (results (delq nil (mapcar #'funcall capfs)))
+      (pcase-let ((`((,beg ,end . ,_)) results)
+                  (candidates nil)
+                  (ht (make-hash-table :test #'equal)))
+        (cl-loop for (beg2 end2 table . plist) in results do
+                 (when (and (= beg beg2) (= end end2))
+                   (setq table (all-completions "" table (plist-get plist 
:predicate))
+                         candidates (nconc candidates table))
+                   (cl-loop for cand in table do (puthash cand plist ht))))
+        (list beg end
+              (lambda (str pred action)
+                (if (eq action 'metadata)
+                    '(metadata
+                      (display-sort-function . identity)
+                      (cycle-sort-function . identity))
+                  (complete-with-action action candidates str pred)))
+              :exclusive 'no
+              :company-doc-buffer (cape--merged-function ht 
:company-doc-buffer)
+              :company-location (cape--merged-function ht :company-location)
+              :company-docsig (cape--merged-function ht :company-docsig)
+              :company-deprecated (cape--merged-function ht 
:company-deprecated)
+              :company-kind (cape--merged-function ht :company-kind)
+              :annotation-function (cape--merged-function ht 
:annotation-function)
+              :exit-function (lambda (x _status) (funcall 
(cape--merged-function ht :exit-function) x)))))))
+
 (defun cape--company-call (backend &rest args)
   "Call Company BACKEND with ARGS."
   (pcase (apply backend args)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]