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

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

[elpa] externals/cape 68e08c2f6e 1/2: Make cape-interactive public


From: ELPA Syncer
Subject: [elpa] externals/cape 68e08c2f6e 1/2: Make cape-interactive public
Date: Fri, 27 Jan 2023 03:57:24 -0500 (EST)

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

    Make cape-interactive public
---
 README.org | 28 ++++++++++++++--------------
 cape.el    | 35 +++++++++++++++++++++--------------
 2 files changed, 35 insertions(+), 28 deletions(-)

diff --git a/README.org b/README.org
index 881c5c2c17..4817252ea5 100644
--- a/README.org
+++ b/README.org
@@ -226,7 +226,7 @@ interaction. These can either be used as advices 
(=cape-wrap-*)= or to create a
 new Capf from an existing Capf (=cape-capf-*=). You can bind the Capfs created 
by
 the Capf transformers with =defalias= to a function symbol.
 
-- ~cape-interactive-capf~: Create a Capf which can be called interactively.
+- ~cape-interactive-capf~, ~cape-interactive~: Create a Capf which can be 
called interactively.
 - ~cape-wrap-accept-all~, ~cape-capf-accept-all~: Create a Capf which accepts 
every input as valid.
 - ~cape-wrap-silent~, ~cape-capf-silent~: Wrap a chatty Capf and silence it.
 - ~cape-wrap-purify~, ~cape-capf-purify~: Purify a broken Capf and ensure that 
it does not modify the buffer.
@@ -244,10 +244,12 @@ In the following we show a few example configurations, 
which have come up on the
 personal configuration.
 
 #+begin_src emacs-lisp
-  ;; Example 1: Sanitize the `pcomplete-completions-at-point' Capf.
-  ;; The Capf has undesired side effects on Emacs 28 and earlier.
-  (advice-add 'pcomplete-completions-at-point :around #'cape-wrap-silent)
-  (advice-add 'pcomplete-completions-at-point :around #'cape-wrap-purify)
+  ;; Example 1: Sanitize the `pcomplete-completions-at-point' Capf.  The Capf 
has
+  ;; undesired side effects on Emacs 28 and earlier.  These advices are not 
needed
+  ;; on Emacs 29 and newer.
+  (when (< emacs-major-version 29)
+    (advice-add 'pcomplete-completions-at-point :around #'cape-wrap-silent)
+    (advice-add 'pcomplete-completions-at-point :around #'cape-wrap-purify))
 
   ;; Example 2: Configure a Capf with a specific auto completion prefix length
   (setq-local completion-at-point-functions
@@ -257,19 +259,17 @@ personal configuration.
   (defalias 'cape-dabbrev-min-2 (cape-capf-prefix-length #'cape-dabbrev 2))
   (setq-local completion-at-point-functions (list #'cape-dabbrev-min-2))
 
-  ;; Example 4: Define a defensive Dabbrev Capf, which accepts all inputs.
-  ;; If you use Corfu and `corfu-auto=t', the first candidate won't be auto
-  ;; selected even if `corfu-preselect-first=t'! You can use this instead of
-  ;; `cape-dabbrev'.
+  ;; Example 4: Define a defensive Dabbrev Capf, which accepts all inputs.  If 
you
+  ;; use Corfu and `corfu-auto=t', the first candidate won't be auto selected 
even
+  ;; if `corfu-preselect=first'. You can use this instead of `cape-dabbrev'.
   (defun my-cape-dabbrev-accept-all ()
     (cape-wrap-accept-all #'cape-dabbrev))
   (add-to-list 'completion-at-point-functions #'my-cape-dabbrev-accept-all)
 
-  ;; Example 5: Define interactive Capf which can be bound to a key.
-  ;; Here we wrap the `elisp-completion-at-point' such that we can
-  ;; complete Elisp code explicitly in arbitrary buffers.
-  (global-set-key (kbd "C-c p e")
-                  (cape-interactive-capf #'elisp-completion-at-point))
+  ;; Example 5: Define interactive Capf which can be bound to a key.  Here we 
wrap
+  ;; the `elisp-completion-at-point' such that we can complete Elisp code
+  ;; explicitly in arbitrary buffers.
+  (keymap-global-set "C-c p e" (cape-interactive-capf 
#'elisp-completion-at-point))
 
   ;; Example 6: Ignore :keywords in Elisp completion.
   (defun ignore-elisp-keywords (sym)
diff --git a/cape.el b/cape.el
index c2aabb5be2..832a321cea 100644
--- a/cape.el
+++ b/cape.el
@@ -125,11 +125,6 @@ The buffers are scanned for completion candidates by 
`cape-line'."
   "Return bounds of THING."
   (or (bounds-of-thing-at-point thing) (cons (point) (point))))
 
-(defun cape--interactive (capf)
-  "Complete with CAPF."
-  (let ((completion-at-point-functions (list capf)))
-    (or (completion-at-point) (user-error "%s: No completions" capf))))
-
 (defmacro cape--wrapped-table (wrap body)
   "Create wrapped completion table, handle `completion--unquote'.
 WRAP is the wrapper function.
@@ -243,7 +238,7 @@ See also `consult-history' for a more flexible variant 
based on
 `completing-read'. If INTERACTIVE is nil the function acts like a Capf."
   (interactive (list t))
   (if interactive
-      (cape--interactive #'cape-history)
+      (cape-interactive #'cape-history)
     (let (history bol)
       (cond
        ((derived-mode-p 'eshell-mode)
@@ -278,7 +273,7 @@ If INTERACTIVE is nil the function acts like a Capf."
   (interactive (list t))
   (if interactive
       (let (cape-file-directory-must-exist)
-        (cape--interactive #'cape-file))
+        (cape-interactive #'cape-file))
     (let* ((default-directory (pcase cape-file-directory
                                 ('nil default-directory)
                                 ((pred stringp) cape-file-directory)
@@ -353,7 +348,7 @@ STATUS is the exit status."
 If INTERACTIVE is nil the function acts like a Capf."
   (interactive (list t))
   (if interactive
-      (cape--interactive #'cape-symbol)
+      (cape-interactive #'cape-symbol)
     (pcase-let ((`(,beg . ,end) (cape--bounds 'symbol)))
       (when (eq (char-after beg) ?')
         (setq beg (1+ beg) end (max beg end)))
@@ -387,7 +382,7 @@ See the user options `cape-dabbrev-min-length' and
   (interactive (list t))
   (if interactive
       (let ((cape-dabbrev-min-length 0))
-        (cape--interactive #'cape-dabbrev))
+        (cape-interactive #'cape-dabbrev))
     (when (thing-at-point-looking-at "\\(?:\\sw\\|\\s_\\)+")
       (let ((beg (match-beginning 0))
             (end (match-end 0)))
@@ -434,7 +429,7 @@ See the user options `cape-dabbrev-min-length' and
 If INTERACTIVE is nil the function acts like a Capf."
   (interactive (list t))
   (if interactive
-      (cape--interactive #'cape-ispell)
+      (cape-interactive #'cape-ispell)
     (let ((bounds (cape--bounds 'word)))
       `(,(car bounds) ,(cdr bounds)
         ,(cape--table-with-properties
@@ -467,7 +462,7 @@ See the custom option `cape-dict-file'.
 If INTERACTIVE is nil the function acts like a Capf."
   (interactive (list t))
   (if interactive
-      (cape--interactive #'cape-dict)
+      (cape-interactive #'cape-dict)
     (let ((bounds (cape--bounds 'word)))
       `(,(car bounds) ,(cdr bounds)
         ,(cape--table-with-properties (cape--dict-words) :category 'cape-dict)
@@ -518,7 +513,7 @@ If INTERACTIVE is nil the function acts like a Capf."
   (if interactive
       ;; NOTE: Disable cycling since abbreviation replacement breaks it.
       (let (completion-cycle-threshold)
-        (cape--interactive #'cape-abbrev))
+        (cape-interactive #'cape-abbrev))
     (when-let (abbrevs (cape--abbrev-list))
       (let ((bounds (cape--bounds 'symbol)))
         `(,(car bounds) ,(cdr bounds)
@@ -567,7 +562,7 @@ The buffers returned by `cape-line-buffer-function' are 
scanned for lines.
 If INTERACTIVE is nil the function acts like a Capf."
   (interactive (list t))
   (if interactive
-      (cape--interactive #'cape-line)
+      (cape-interactive #'cape-line)
     `(,(pos-bol) ,(point)
       ,(cape--table-with-properties (cape--line-list) :sort nil)
       ,@cape--line-properties)))
@@ -737,12 +732,24 @@ This feature is experimental."
                   (cape--company-call backend 'post-completion
                                       (or (car (member x candidates)) 
x)))))))))
 
+;;;###autoload
+(defun cape-interactive (&rest capfs)
+  "Complete interactively with the given CAPFS."
+  (let ((completion-at-point-functions capfs))
+    (unless (completion-at-point)
+      (user-error "%s: No completions"
+                  (mapconcat (lambda (fun)
+                               (if (symbolp fun)
+                                   (symbol-name fun)
+                                 "anonymous-capf"))
+                             capfs ", ")))))
+
 ;;;###autoload
 (defun cape-interactive-capf (capf)
   "Create interactive completion function from CAPF."
   (lambda (&optional interactive)
     (interactive (list t))
-    (if interactive (cape--interactive capf) (funcall capf))))
+    (if interactive (cape-interactive capf) (funcall capf))))
 
 ;;;###autoload
 (defun cape-wrap-buster (capf &optional valid)



reply via email to

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