* [PATCH] implement a capf for address completion
@ 2024-03-19 19:59 Antoine Beaupré
2024-03-19 20:12 ` Antoine Beaupré
0 siblings, 1 reply; 3+ messages in thread
From: Antoine Beaupré @ 2024-03-19 19:59 UTC (permalink / raw)
To: notmuch; +Cc: 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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] implement a capf for address completion
2024-03-19 19:59 [PATCH] implement a capf for address completion Antoine Beaupré
@ 2024-03-19 20:12 ` Antoine Beaupré
2024-03-19 20:34 ` Antoine Beaupré
0 siblings, 1 reply; 3+ messages in thread
From: Antoine Beaupré @ 2024-03-19 20:12 UTC (permalink / raw)
To: notmuch
I have *just* realized, after writing all this code and sending the
patch, that someone else might have discussed this here. Oops.
So, turns out, it *has* been discussed, but it doesn't look like anyone
tackled that problem fully just yet.
The closest we have in the archive is (author in CC):
<71a3382b-3f1b-4b81-883c-b4a8bd710888%40picnicpark.org>
... which writes a new `kea/notmuch-address-message-capf' function from
scratch. It might, however, do a better job than i do at taking into
account the "harvest" process, which I only discovered after writing the
patch and decided I would pretend it doesn't exist (and is probably
inaccurate). So that might be a flaw with my patch already, but in my
defense, WTF is that harvest thing!? :)
Another discussion about capf was about integration with EUDC:
<8a437e3f646f7972c86c4aae57ae7452%40condition-alpha.com>
But I don't quite understand what EUDC is or why it matters here. Might
be *some* overlap, not sure.
Finally, there's this whole other thread from 2022 about capf, but it's
quite diffuse and I couldn't make heads or tails of it. The head of the
thread according to the online archives is:
<m2v8xnn1l3.fsf%40guru.guru-group.fi>
... but that's a reply to... something else, so I'm not sure.
As usual, your mileage may vary. :) To test my patch, you apply it,
restart Emacs (or rewire `message-completion-alist' like Keith Amidon
did), fire off a new email, start entering an address, and hit TAB (or,
if you have corfu, just wait for the timeout).
a.
--
We don't need any more heroes.
We just need someone to take out recycling.
- Banksy
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] implement a capf for address completion
2024-03-19 20:12 ` Antoine Beaupré
@ 2024-03-19 20:34 ` Antoine Beaupré
0 siblings, 0 replies; 3+ messages in thread
From: Antoine Beaupré @ 2024-03-19 20:34 UTC (permalink / raw)
To: notmuch
On 2024-03-19 16:12:04, Antoine Beaupré wrote:
[...]
> The closest we have in the archive is (author in CC):
>
> <71a3382b-3f1b-4b81-883c-b4a8bd710888%40picnicpark.org>
>
> ... which writes a new `kea/notmuch-address-message-capf' function from
> scratch. It might, however, do a better job than i do at taking into
> account the "harvest" process, which I only discovered after writing the
> patch and decided I would pretend it doesn't exist (and is probably
> inaccurate). So that might be a flaw with my patch already, but in my
> defense, WTF is that harvest thing!? :)
Thinking about this more, I *think* the capf *can* return a *function*
instead of a list of strings, so we might be able to refactor this a bit
better and send a function that waits for the harvesting to complete, or
stream the results or something.
Above my pay grade, at this point.
--
We have no friends but the mountains.
- Kurdish saying\r
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-03-19 20:44 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-19 19:59 [PATCH] implement a capf for address completion Antoine Beaupré
2024-03-19 20:12 ` Antoine Beaupré
2024-03-19 20:34 ` Antoine Beaupré
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).