From 9686a324c49c3d5ba949a22d26dfe459629a59fe Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Thu, 20 Jan 2022 11:06:42 +0100 Subject: adapter --- README.org | 13 +++++++++++++ cape.el | 11 +++++++++++ 2 files changed, 24 insertions(+) diff --git a/README.org b/README.org index 5167dd8..111f5cd 100644 --- a/README.org +++ b/README.org @@ -212,6 +212,19 @@ achieve a similarly refreshing strategy. (list (cape-capf-buster #'some-caching-capf))) #+end_src +** completing-read adapter + +/Wrap your completion command in a Cape and turn it into a Capf!/ + +Sometimes it is useful to reuse existing completion commands, which call +~completing-read~ to generate candidates for completion at point. One such example +is the command ~consult-history~. + +#+begin_src emacs-lisp + (defalias 'eshell-buffer-name-capf (cape-interactive-capf (cape-completing-read-to-capf #'consult-history))) +#+end_src + + ** Other Capf transformers - ~cape-interactive-capf~: Create a Capf which can be called interactively. diff --git a/cape.el b/cape.el index 73fde31..08b752b 100644 --- a/cape.el +++ b/cape.el @@ -950,6 +950,17 @@ This feature is experimental." (or (car (member x candidates)) x))))))))) ;;;###autoload +(defun cape-completing-read-to-capf (command) + "Convert a completion COMMAND to a Capf." + (lambda () + (catch 'cape--result + (cl-letf (((symbol-function #'completing-read) + (lambda (_prompt table &rest _) + (throw 'cape--result (list (point) (point) table :exclusive 'no))))) + (call-interactively command)) + nil))) + +;;;###autoload (defun cape-interactive-capf (capf) "Create interactive completion function from CAPF." (lambda (&optional interactive) -- cgit v1.0