[PATCH] emacs: mua: add a pre-send-check-hook
Matt Armstrong
marmstrong at google.com
Mon Nov 7 13:12:22 PST 2016
Hey Mark,
For consistency with Emacs' own elisp, perhaps rename
notmuch-mua-pre-send-check-hooks to
notmuch-mua-pre-send-check-functions?
This patch reminded me of a recent discussion on emacs-devel about the
definition of "hook" in Emacs code and documentation. There is the
broad meaning of "arbitrary extension point", and the narrow meaning of
"list of functions run by `run-hook'". This is "hook" in the former
sense.
>From the Elisp manual's Modes > Hooks section:
If the hook variable’s name does not end with ‘-hook’, that indicates
it is probably an "abnormal hook". That means the hook functions are
called with arguments, or their return values are used in some way. The
hook’s documentation says how the functions are called. You can use
‘add-hook’ to add a function to an abnormal hook, but you must write the
function to follow the hook’s calling convention. By convention,
abnormal hook names end in ‘-functions’.
[...]
-- Function: run-hook-with-args-until-failure hook &rest args
This function runs an abnormal hook by calling each hook function
in turn, stopping if one of them fails by returning ‘nil’. Each
hook function is passed the arguments ARGS. If this function stops
because one of the hook functions fails, it returns ‘nil’;
otherwise it returns a non-‘nil’ value.
When calling hooks by run-hook-with-args-until-failure/-success, Emacs'
own elisp code follows the -functions convention for the most part.
Mark Walters <markwalters1009 at gmail.com> writes:
> This add a pre-send hook for any checks the user wants to run before
> sending the message. If any function in the hook returns nil then the
> send will abort.
>
> One use would be to check that the from address is appropriate for the
> recipients (i.e., test From: based on To: and Cc:), but many other
> uses are possible: checking spelling, checking that the message is set
> to be encrypted etc.
> ---
>
> bremner and I were discussing the address-completion-hook on irc, and
> before he implemented that I suggested an alternative of adding a
> pre-send-check-hook. The idea is that functions in this hook can force
> abort sending (or at least get confirmation from the user) based on
> the message.
>
> For example I would be quite likely to use something like the
> following in the hook.
>
> (lambda ()
> (save-excursion
> (save-restriction
> (let ((to (message-fetch-field "To"))
> (from (message-fetch-field "From"))
> (case-fold-search t))
> (or (not (string-match "work-domain-address" to))
> (string= from "my-work-address")
> (yes-or-no-p "Message to work but not from work address. Really send? "))))))
>
> I think this is reasonably orthogonal to the
> notmuch-address-completion-hook. Setting the from address based on the
> to addresses makes a lot of sense, but checking on send also does --
> if the from is correct then the check is silent, and if the user types
> in the To: without using completion then the check will catch it.
>
>
> Best wishes
>
> Mark
>
>
> emacs/notmuch-mua.el | 12 +++++++++++-
> 1 file changed, 11 insertions(+), 1 deletion(-)
>
> diff --git a/emacs/notmuch-mua.el b/emacs/notmuch-mua.el
> index f333655..78130e6 100644
> --- a/emacs/notmuch-mua.el
> +++ b/emacs/notmuch-mua.el
> @@ -36,6 +36,15 @@
>
> ;;
>
> +(defcustom notmuch-mua-pre-send-check-hook nil
> + "Hook of checks run before sending messages.
> +
> +If any of the functions in the hook return nil then the send will
> +be aborted."
> + :type 'hook
> + :group 'notmuch-send
> + :group 'notmuch-hooks)
> +
> (defcustom notmuch-mua-send-hook '(notmuch-mua-message-send-hook)
> "Hook run before sending messages."
> :type 'hook
> @@ -538,7 +547,8 @@ unencrypted. Really send? "))))
>
> (defun notmuch-mua-send-common (arg &optional exit)
> (interactive "P")
> - (when (and (notmuch-mua-check-no-misplaced-secure-tag)
> + (when (and (run-hook-with-args-until-failure 'notmuch-mua-pre-send-check-hook)
> + (notmuch-mua-check-no-misplaced-secure-tag)
> (notmuch-mua-check-secure-tag-has-newline))
> (letf (((symbol-function 'message-do-fcc) #'notmuch-maildir-message-do-fcc))
> (if exit
> --
> 2.1.4
>
> _______________________________________________
> notmuch mailing list
> notmuch at notmuchmail.org
> https://notmuchmail.org/mailman/listinfo/notmuch
More information about the notmuch
mailing list