branch: externals/cape
commit 8d6f43b2876a284b748a645e24a30fc912921d2e
Author: Daniel Mendler <[email protected]>
Commit: Daniel Mendler <[email protected]>

    Improve cape-wrap-trigger
---
 README.org | 10 +++++-----
 cape.el    | 35 ++++++++++++++++++++---------------
 2 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/README.org b/README.org
index d02571a72c..a895269da9 100644
--- a/README.org
+++ b/README.org
@@ -259,14 +259,14 @@ the Capf transformers with =defalias= to a function 
symbol.
 - ~cape-capf-silent~, ~cape-wrap-silent~: Silence Capf messages and errors.
 - ~cape-capf-sort~, ~cape-wrap-sort~: Add sort function to a Capf.
 - ~cape-capf-super~, ~cape-wrap-super~: Merge multiple Capfs into a Super-Capf.
-- ~cape-capf-trigger~, ~cape-wrap-trigger~: Create a Capf with a trigger 
character or string.
+- ~cape-capf-trigger~, ~cape-wrap-trigger~: Create a Capf with a trigger 
prefix character.
 
 In the following we show a few example configurations, which have come up on 
the
 [[https://github.com/minad/cape/issues][Cape]] or 
[[https://github.com/minad/corfu/issues][Corfu issue tracker]] or the 
[[https://github.com/minad/corfu/wiki][Corfu wiki.]] I use some of these tweaks 
in my
 personal configuration.
 
 #+begin_src emacs-lisp
-;; Example 1: Configure a merged Capf with a trigger prefix
+;; Example 1: Configure a merged Capf with a trigger prefix character.
 (setq-local completion-at-point-functions
             (list (cape-capf-trigger
                    (cape-capf-super #'cape-abbrev
@@ -274,14 +274,14 @@ personal configuration.
                                     #'tempel-complete)
                    ?/)))
 
-;; Example 2: Configure a Capf with a specific auto completion prefix length
+;; Example 2: Configure a Capf with a specific auto completion prefix length.
 (setq-local completion-at-point-functions
             (list (cape-capf-prefix-length #'cape-dabbrev 2)))
 
-;; Example 3: Create a Capf with debugging messages
+;; Example 3: Create a Capf with debugging messages.
 (setq-local completion-at-point-functions (list (cape-capf-debug #'cape-dict)))
 
-;; Example 4: Named Capf
+;; Example 4: Named Capf.
 (defalias 'cape-dabbrev-min-2 (cape-capf-prefix-length #'cape-dabbrev 2))
 (setq-local completion-at-point-functions (list #'cape-dabbrev-min-2))
 
diff --git a/cape.el b/cape.el
index 29fdf489c2..288ae05b2c 100644
--- a/cape.el
+++ b/cape.el
@@ -1216,29 +1216,34 @@ This function can be used as an advice around an 
existing Capf."
     (`(,beg ,end ,table . ,plist)
      `(,beg ,end ,(cape--accept-all-table table) . ,plist))))
 
+(defvar cape--trigger-syntax-table (make-syntax-table (syntax-table))
+  "Syntax table used for the trigger character.")
+
 ;;;###autoload
 (defun cape-wrap-trigger (capf trigger)
-  "Ensure that TRIGGER string or character occurs before point and then call 
CAPF.
+  "Ensure that TRIGGER character occurs before point and then call CAPF.
 Example:
   (setq completion-at-point-functions
       (list (cape-capf-trigger \\='cape-abbrev ?/)))"
-  (unless (stringp trigger) (setq trigger (char-to-string trigger)))
-  (when-let ((tbeg (save-excursion (search-backward trigger (pos-bol) 
'noerror)))
-             (tend (+ tbeg (length trigger)))
-             ((save-excursion (not (re-search-backward "\\s-" tbeg 
'noerror)))))
-    (pcase (funcall capf)
+  (when-let ((pos (save-excursion (search-backward (char-to-string trigger) 
(pos-bol) 'noerror)))
+             ((save-excursion (not (re-search-backward "\\s-" pos 'noerror)))))
+    (pcase
+        ;; Treat the trigger character as punctuation.
+        (with-syntax-table cape--trigger-syntax-table
+          (unless (eq (char-syntax trigger) ?.)
+            (modify-syntax-entry trigger "."))
+          (funcall capf))
       (`(,beg ,end ,table . ,plist)
-       (when (<= tbeg beg tend)
-         (setq beg tend
-               tbeg (copy-marker tbeg)
-               tend (copy-marker tend))
-         `( ,beg ,end ,table
+       (when (<= pos beg (1+ pos))
+         `( ,(1+ pos) ,end ,table
             :company-prefix-length t
             :exit-function
-            ,(lambda (str status)
-               (delete-region tbeg tend)
-               (when-let ((exit (plist-get plist :exit-function)))
-                 (funcall exit str status)))
+            ,(let ((pos (copy-marker pos))
+                   (end (copy-marker (1+ pos))))
+               (lambda (str status)
+                 (delete-region pos end)
+                 (when-let ((exit (plist-get plist :exit-function)))
+                   (funcall exit str status))))
             . ,plist))))))
 
 ;;;###autoload (autoload 'cape-capf-purify "cape")

Reply via email to