Add auto-completion in mu4e search query for fragments
I like this package but I was annoyed that I don't have auto-completion for the fragments. I looked into the documentation of completion-at-point-function
s (capf's), the emacs manual and some online blogs and found that writing my own completion function is not too difficult:
(defun my-mu4e-query-fragments-complete ()
(let ((bounds (bounds-of-thing-at-point 'symbol)))
(when (and bounds
(char-equal (char-after (car bounds)) '?%))
(list
(car bounds)
(cdr bounds)
(completion-table-dynamic
(lambda (_)
(mapcar #'car mu4e-query-fragments-list)))
:exclusive 'no))))
This is a very early draft of me learning how to use completions-at-point functions. Maybe it would be possible to be more sophisticated and create a completion with the information what each snippet expands to, so that completion backends like company-quickhelp
or corfu-docs
might preview that information, but I didn't look into that yet.
The only difficulty is adding the this completion function to be used in mu4e-search-read-query
, because that function currently overrides the global completion-at-point-functions
with its own completion function and I see no elegant way to use hooks or something like that to add functions to that, except overriding mu4e-search-read-query
, which is what I did for now:
(defcustom my-mu4e-search-query-capfs nil
"Completion-at-point functions for mu4e search queries."
:group 'mu4e
:type 'hook)
(dolist (list #'mu4e--search-query-completion-at-point #'my-mu4e-query-fragments-complete)
(add-hook 'my-mu4e-search-query-capfs))
(defun my-mu4e-search-read-query (prompt &optional initial-input)
"Read a query with completion using PROMPT and INITIAL-INPUT."
(minibuffer-with-setup-hook
(lambda ()
(setq-local completion-at-point-functions my-mu4e-search-query-capfs)
(use-local-map mu4e-minibuffer-search-query-map))
(read-string prompt initial-input 'mu4e--search-hist)))
(advice-add 'mu4e-search-read-query :override #'my-mu4e-search-read-query)
I would not include an override in the package by default, but I think it would be possible to put the alternative query function with query completion support into the package and then just add an entry to the readme asking users to add that override advice to their config if they want completion support.
Also I opened an mu4e issue here to add more extensibility for the capfs used by mu4e-search-read-query
.