[PATCH] emacs: add notmuch-address-post-completion-hook

Mark Walters markwalters1009 at gmail.com
Thu Nov 3 02:27:22 PDT 2016


On Thu, 03 Nov 2016, David Bremner <david at tethera.net> wrote:
> This hook can be used to update the message based on the results of
> address completion. For example I use it to set the From address based
> on the To address just completed.
>
> The post-completion command is added to the notmuch-company backend to
> ensure that the hook is also called company completion is started
> without going through notmuch-address-expand-name. See the docstring of
> `company-backends' for more information.
> ---
>  emacs/notmuch-address.el | 10 ++++++++++
>  emacs/notmuch-company.el |  1 +
>  2 files changed, 11 insertions(+)
>
> diff --git a/emacs/notmuch-address.el b/emacs/notmuch-address.el
> index b2e1fba..17960bf 100644
> --- a/emacs/notmuch-address.el
> +++ b/emacs/notmuch-address.el
> @@ -98,6 +98,12 @@ to know how address selection is made by default."
>    :group 'notmuch-send
>    :group 'notmuch-external)
>  
> +(defcustom notmuch-address-completion-hook nil
> +  "Functions called after completing address. The completed address is passed as an argument to each"

Hi when testing this with company I think the following happens: if you
trigger it manually with tab then I think the hook gets passed a list of
possible addresses, not just one.

> +  :type 'hook
> +  :group 'notmuch-address
> +  :group 'notmuch-hooks)
> +
>  (defun notmuch-address-selection-function (prompt collection initial-input)
>    "Call (`completing-read'
>        PROMPT COLLECTION nil nil INITIAL-INPUT 'notmuch-address-history)"
> @@ -170,6 +176,10 @@ external commands."
>      (process-lines notmuch-address-command original))))
>  
>  (defun notmuch-address-expand-name ()
> +  (let ((return-value (notmuch-address-real-expand-name)))
> +    (run-hook-with-args 'notmuch-address-completion-hook)))

I think you need to return return-value here? But you also need to
modify notmuch-address-real-expand-name to return chosen. Around line
217 of notmuch-address.el

      (if chosen
	  (progn
	    (push chosen notmuch-address-history)
	    (delete-region beg end)
	    (insert chosen))

should become 

      (if chosen
	  (progn
	    (push chosen notmuch-address-history)
	    (delete-region beg end)
	    (insert chosen)
            ;; Return completed address 
            chosen)

I guess an alternative would to just run the hook there, and then you
wouldn't need to the notmuch-address-real-expand-name function at
all. (Sorry for misleading you about this on irc yesterday.)


> +
> +(defun notmuch-address-real-expand-name ()
>    (cond
>     ((and (eq notmuch-address-command 'internal)
>  	 notmuch-address-use-company
> diff --git a/emacs/notmuch-company.el b/emacs/notmuch-company.el
> index 168315f..91c4804 100644
> --- a/emacs/notmuch-company.el
> +++ b/emacs/notmuch-company.el
> @@ -86,6 +86,7 @@
>        (match (if (string-match notmuch-company-last-prefix arg)
>  		 (match-end 0)
>  	       0))
> +      (post-completion (run-hook-with-args 'notmuch-address-completion-hook arg))
>        (no-cache t))))

To fix the list of addresses case you might be able to just put a
(unless (listp arg) or something round the run-hook-with-args

Best wishes

Mark



More information about the notmuch mailing list