branch: externals/cape
commit 30133e41ccc5a4bb72998b19c26a664e3c3bfc65
Author: Daniel Mendler <m...@daniel-mendler.de>
Commit: Daniel Mendler <m...@daniel-mendler.de>

    Add cape-file-prefix customization variable
---
 CHANGELOG.org |  5 +++++
 cape.el       | 27 ++++++++++++++++++++-------
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/CHANGELOG.org b/CHANGELOG.org
index e74f68aa01..f3738fb79b 100644
--- a/CHANGELOG.org
+++ b/CHANGELOG.org
@@ -2,6 +2,11 @@
 #+author: Daniel Mendler
 #+language: en
 
+* Development
+
+- ~cape-file-prefix~: New variable to customize file name completion prefix. If
+  the input matches one of the configured prefixes, file completion is started.
+
 * Version 1.4 (2024-03-08)
 
 - =cape-char=: Look back from point, instead of using the match at point. This
diff --git a/cape.el b/cape.el
index 71fdaffbc3..caa9d58102 100644
--- a/cape.el
+++ b/cape.el
@@ -108,6 +108,13 @@ Any other non-nil value only checks some other buffers, as 
per
   "Base directory used by `cape-file."
   :type '(choice (const nil) string function))
 
+(defcustom cape-file-prefix "file:"
+  "File completion trigger prefixes.
+The value can be a string or a list of strings.  The default
+`file:' is the prefix of Org file links which work in arbitrary
+buffers via `org-open-at-point-global'."
+  :type '(choice string (repeat string)))
+
 (defcustom cape-file-directory-must-exist t
   "The parent directory must exist for file completion."
   :type 'boolean)
@@ -402,13 +409,19 @@ If INTERACTIVE is nil the function acts like a Capf."
                                       ('nil default-directory)
                                       ((pred stringp) cape-file-directory)
                                       (_ (funcall cape-file-directory))))
-                 (`(,beg . ,end) (cape--bounds 'filename))
+                 (prefix (and cape-file-prefix
+                              (looking-back
+                               (concat
+                                (regexp-opt (ensure-list cape-file-prefix) t)
+                                "[^ \n\t]*")
+                               (pos-bol))
+                              (match-end 1)))
+                 (`(,beg . ,end) (if prefix
+                                     (cons prefix (point))
+                                   (cape--bounds 'filename)))
                  (non-essential t)
-                 (file (buffer-substring-no-properties beg end))
-                 ;; Support org links globally, see `org-open-at-point-global'.
-                 (org (string-prefix-p "file:" file)))
-      (when org (setq beg (+ 5 beg)))
-      (when (or org
+                 (file (buffer-substring-no-properties beg end)))
+      (when (or prefix
                 (not cape-file-directory-must-exist)
                 (and (string-search "/" file)
                      (file-exists-p (file-name-directory file))))
@@ -420,7 +433,7 @@ If INTERACTIVE is nil the function acts like a Capf."
                  comint-unquote-function
                  comint-requote-function)
               #'read-file-name-internal))
-          ,@(when (or org (string-match-p "./" file))
+          ,@(when (or prefix (string-match-p "./" file))
               '(:company-prefix-length t))
           ,@cape--file-properties)))))
 

Reply via email to