automatic company complete in To/CC/BCC but not in message body

8 views
Skip to first unread message

Ramon Diaz-Uriarte

unread,
Apr 1, 2024, 8:46:56 AMApr 1
to mu-di...@googlegroups.com, r.d...@uam.es
Dear All,

When writing an email, and selecting the addresses, I like the automatic completion popup that company can provide. When writing the text of the email, I don't want the automatic completion popup (but I want to be able to trigger company manually).

I think the simplest way to achieve this would be to set "company-idle-delay" to nil when writing the email itself, but keep it to, say, 0.2, when adding recipients in the To/CC/BCC fields. I do not know how to do this properly, and my googling around has found nothing that would work.


Option 1: This (ugly kludge?) kind of works:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(add-hook 'mu4e-compose-mode-hook
(lambda() (setq-local company-idle-delay 0.2)))

(defun rdu-goto-message ()
"Goto message body, but first set company-idle-delay to 0"
(interactive)
(setq-local company-idle-delay nil)
(call-interactively 'message-goto-body)
)

(define-key mu4e-compose-mode-map (kbd "C-c C-b") 'rdu-goto-message)


(defun rdu/on-reply-set-company-idle-nil (&optional WIDE)
(setq-local company-idle-delay nil)
)
(advice-add 'mu4e-compose-wide-reply :before
#'rdu/on-reply-set-company-idle-nil)
(advice-add 'mu4e-compose-reply :before
#'rdu/on-reply-set-company-idle-nil)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

But there are several things I dislike:

1. company-idle-delay is still 0.2 when I reply and wide-reply. For reasons I do not understand, the advice I've tried adding does not seem to do anything.

2. Remapping C-c C-b to my function seems like an inelegant brute force approach to me (though I am an elisp newby, so elegance might not be something I can yet assess :) ).

3. If, after C-c C-b, I want to add a recipient, company automatic completion is now disabled (though this is not a big deal).




Option 2: hooks. I don't know how to make it work

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(add-hook 'message-header-hook
(lambda() (setq-local company-idle-delay 0.2)))

(add-hook 'mu4e-compose-mode-hook
(lambda() (setq-local company-idle-delay nil)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

But this does not work, because I think the mu4e-compose-mode-hook is run last on all of the buffer.



Option 3: advice message-goto-body:

I've tried adding advice to message-goto-body to set company-idle-delay to nil, as follows:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Default, so it is 0.2 in To/Cc/BCC
(add-hook 'mu4e-compose-mode-hook
(lambda() (setq-local company-idle-delay 0.2)))

;; When we move to the body, set company-idle-delay to nil
(defun rdu/after-message-goto-body (&optional interactive)
(setq-local company-idle-delay nil)
)
(advice-add 'message-goto-body :after #'rdu/after-message-goto-body)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

But that does not work. First, it just doesn't work: for reasons I do not understand, even before I move to the message body, adding the advice to message-goto-body sets the variable to nil in To/CC/BCC. And even if this worked, this would disable automatic completion when doing reply and moving back to the To/CC/BCC (though this is not such a big deal).


What is the proper way to do this?

Thanks,

R.



--
Ramon Diaz-Uriarte
Department of Biochemistry, Lab B-31
Facultad de Medicina
Universidad Autónoma de Madrid
Arzobispo Morcillo, 4
28029 Madrid
Spain

Phone: +34-91-497-2412

Email: rdi...@gmail.com
r.d...@uam.es
ramon...@iib.uam.es

https://ligarto.org/rdiaz


Dirk-Jan C. Binnema

unread,
Apr 1, 2024, 11:21:49 AMApr 1
to mu-di...@googlegroups.com
Hi,

On Monday Apr 01 2024, Ramon Diaz-Uriarte wrote:

> Dear All,
>
> When writing an email, and selecting the addresses, I like the
> automatic completion popup that company can provide. When writing the
> text of the email, I don't want the automatic completion popup (but I
> want to be able to trigger company manually).
>
> I think the simplest way to achieve this would be to set
> "company-idle-delay" to nil when writing the email itself, but keep it
> to, say, 0.2, when adding recipients in the To/CC/BCC fields. I do not
> know how to do this properly, and my googling around has found nothing
> that would work.

I don't know how company works so I'll skip that part; but what mu4e
does is add its completion function to `completion-at-point-functions'
(is company using that?).

When you try to complete (typically with TAB), mu4e's completion
function checks if we're at some contact header field (To:, Cc: etc.),
and if we are, it attempts to complete; otherwise (e.g. in the body), it
does not.

Because of that contact header check, completion in say the body won't
work without some more hacks.

Kind regards,
Dirk.

--
Dirk-Jan C. Binnema Helsinki, Finland
e:dj...@djcbsoftware.nl w:www.djcbsoftware.nl
gpg: 6987 9CED 1745 9375 0F14 DA98 11DD FEA9 DCC4 A036

Ramon Diaz-Uriarte

unread,
Apr 1, 2024, 10:45:06 PMApr 1
to Dirk-Jan C. Binnema, mu-di...@googlegroups.com
Hi Dirk,

Thanks for your reply. I think I've solved it after your answer prompted me to think about the problem differently.

On Mon, 01-April-2024, at 17:21:43, "Dirk-Jan C. Binnema" <dj...@djcbsoftware.nl> wrote:
> Hi,
>
> On Monday Apr 01 2024, Ramon Diaz-Uriarte wrote:
>
>> Dear All,
>>
>> When writing an email, and selecting the addresses, I like the
>> automatic completion popup that company can provide. When writing the
>> text of the email, I don't want the automatic completion popup (but I
>> want to be able to trigger company manually).
>>
>> I think the simplest way to achieve this would be to set
>> "company-idle-delay" to nil when writing the email itself, but keep it
>> to, say, 0.2, when adding recipients in the To/CC/BCC fields. I do not
>> know how to do this properly, and my googling around has found nothing
>> that would work.
>
> I don't know how company works so I'll skip that part; but what mu4e
> does is add its completion function to `completion-at-point-functions'
> (is company using that?).
>

Yes, company can use that: I have, as one of the default company backends company-capf, and it works perfectly on the To:, Cc:, BCC:.


> When you try to complete (typically with TAB), mu4e's completion
> function checks if we're at some contact header field (To:, Cc: etc.),
> and if we are, it attempts to complete; otherwise (e.g. in the body), it
> does not.
>
> Because of that contact header check, completion in say the body won't
> work without some more hacks.
>

Completion in the body (not of email addresses, but of file names, etc) using company can be done by having company enabled (by default, I have global-company-mode in my init file, so it is on in all buffers).

So completion works fine everywhere for me. But it is the automatic completion popup that I want to avoid in the body; this can be changed by changing the variable company-idle-delay.

What I was unable to do was to change that buffer local variable depending on whether point was initially at the header or at the body (I want automatic completion in the first case, and I don't want it on the second). Until I realized that I could use mu4e-compose-type. So now I have this

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun rdu-mu4e-company-idle-delay ()
"When replying, set company-idle-delay to 0, as we are not in
the headers. O.w. (new compose, forward), set to 0.2, as we will be in
the headers."
(if (eq mu4e-compose-type 'reply)
(setq-local company-idle-delay nil)
(setq-local company-idle-delay 0.2)))

(add-hook 'mu4e-compose-mode-hook 'rdu-mu4e-company-idle-delay)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


And this, to force the idle-delay to nil on moving to the body
when composing

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun rdu-goto-message ()
"Goto message body, and then set company-idle-delay to nil"
(interactive)
(call-interactively 'message-goto-body)
(setq-local company-idle-delay nil))

(define-key mu4e-compose-mode-map (kbd "C-c C-b") 'rdu-goto-message)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

That "rdu-goto-message" seems an ugly way to do what I want, but it works. :-)

So problem solved.

Best,

R.


> Kind regards,
> Dirk.
>
> --
> Dirk-Jan C. Binnema Helsinki, Finland
> e:dj...@djcbsoftware.nl w:www.djcbsoftware.nl
> gpg: 6987 9CED 1745 9375 0F14 DA98 11DD FEA9 DCC4 A036

Jerry Lee Daniel

unread,
May 7, 2024, 5:40:48 AMMay 7
to mu-discuss

Loans, Project and Digital Investment financing available up to $150m.
Have a Business Plan, Fundable Project and Collateral.

Contact: danielj...@gmail.com
Whatsapp: ‪+1 (918) 707‑0858
Reply all
Reply to author
Forward
0 new messages