[PATCH] implement a capf for address completion

Subject: [PATCH] implement a capf for address completion

Date: Tue, 19 Mar 2024 15:59:15 -0400

To: notmuch@notmuchmail.org

Cc: Antoine Beaupré

From: Antoine Beaupré


I recently enabled corfu-mode everywhere, and was disappointed to find
out that I lost tab-completion on my message buffers. At most, corfu
would pop suggestions about existing words in the buffer, but no
address completion, even when I would hit TAB. I believe this is
because `message-tab' won't attempt completion if something else
already did it, and also because, somehow,
`notmuch-address-expand-name' ends up in
`message--old-style-completion-functions'.

Now, it seems to me a simple fix is to implement a proper
capf (`completion-at-point-function') for notmuch. And that, in turn,
is actually pretty simple compared to the code hidden underneath
`notmuch-address-expand-name', which not only finds completion
candidates, but also does the whole trouble of editing the buffer.

So this patch turns `notmuch-address-expand-name' into a wrapper
around the capf, and hooks the capf instead of the old function in the
message-mode completion hooks.

This ... works. Now I have popup completion, automatically (even
before hitting TAB), in my message-mode buffers. It's a bit jarring
because I'm so used to having completion in the minibuffer, but I
think I'll get used to it.

I haven't figured out how to make an escape hatch for this, to get
autocompletion in the minibuffer the same wauy way we did
before. Maybe we'd need something like the
`notmuch-address-from-minibuffer', but interactive somehow. Not
sure. But for now, this allows me to keep corfu globally active, and I
suspect will make things easier and faster going forward.
---
 emacs/notmuch-address.el | 33 +++++++++++++++++++++++----------
 1 file changed, 23 insertions(+), 10 deletions(-)

diff --git a/emacs/notmuch-address.el b/emacs/notmuch-address.el
index f756254c..0c57add8 100644
--- a/emacs/notmuch-address.el
+++ b/emacs/notmuch-address.el
@@ -171,7 +171,7 @@ matching `notmuch-address-completion-headers-regexp'."
 	       (require 'company nil t))
       (notmuch-company-setup))
     (cl-pushnew (cons notmuch-address-completion-headers-regexp
-		      #'notmuch-address-expand-name)
+		      #'notmuch-address-complete-at-point)
 		message-completion-alist :test #'equal)))
 
 (defun notmuch-address-toggle-internal-completion ()
@@ -225,15 +225,10 @@ requiring external commands."
 	 (bound-and-true-p company-mode))
     (company-manual-begin))
    (notmuch-address-command
-    (let* ((end (point))
-	   (beg (save-excursion
-		  (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
-		  (goto-char (match-end 0))
-		  (point)))
-	   (orig (buffer-substring-no-properties beg end))
-	   (completion-ignore-case t)
-	   (options (with-temp-message "Looking for completion candidates..."
-		      (notmuch-address-options orig)))
+    (let* ((capf (notmuch-address-complete-at-point))
+	   (beg (pop capf))
+	   (end (pop capf))
+	   (options (pop capf))
 	   (num-options (length options))
 	   (chosen (cond
 		    ((eq num-options 0)
@@ -256,6 +251,24 @@ requiring external commands."
 	(ding))))
    (t nil)))
 
+(defun notmuch-address-complete-at-point ()
+  "Complete the address using `notmuch-address-command'.
+
+This replaces the old `notmuch-address-expand-name' with the new
+`completion-at-point-functions' (capf) system that's compatible
+with corfu, company and friends."
+  (when notmuch-address-command
+    (let* ((end (point))
+	   (beg (save-excursion
+		  (re-search-backward "\\(\\`\\|[\n:,]\\)[ \t]*")
+		  (goto-char (match-end 0))
+		  (point)))
+	   (orig (buffer-substring-no-properties beg end))
+	   (completion-ignore-case t)
+	   (options (with-temp-message "Looking for completion candidates..."
+		      (notmuch-address-options orig))))
+      (list beg end options))))
+
 ;;; Harvest
 
 (defun notmuch-address-harvest-addr (result)
-- 
2.39.2

_______________________________________________
notmuch mailing list -- notmuch@notmuchmail.org
To unsubscribe send an email to notmuch-leave@notmuchmail.org

Thread: