* [PATCH] emacs: mua: add a pre-send-check-hook
@ 2016-11-04 18:00 Mark Walters
2016-11-07 21:12 ` Matt Armstrong
0 siblings, 1 reply; 3+ messages in thread
From: Mark Walters @ 2016-11-04 18:00 UTC (permalink / raw)
To: notmuch
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
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] emacs: mua: add a pre-send-check-hook
2016-11-04 18:00 [PATCH] emacs: mua: add a pre-send-check-hook Mark Walters
@ 2016-11-07 21:12 ` Matt Armstrong
2016-11-13 7:36 ` Mark Walters
0 siblings, 1 reply; 3+ messages in thread
From: Matt Armstrong @ 2016-11-07 21:12 UTC (permalink / raw)
To: Mark Walters, notmuch
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@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@notmuchmail.org
> https://notmuchmail.org/mailman/listinfo/notmuch
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] emacs: mua: add a pre-send-check-hook
2016-11-07 21:12 ` Matt Armstrong
@ 2016-11-13 7:36 ` Mark Walters
0 siblings, 0 replies; 3+ messages in thread
From: Mark Walters @ 2016-11-13 7:36 UTC (permalink / raw)
To: Matt Armstrong, notmuch
> For consistency with Emacs' own elisp, perhaps rename
> notmuch-mua-pre-send-check-hooks to
> notmuch-mua-pre-send-check-functions?
Hi
Yes you are correct. (Annoyingly I had thought about this and thought
it was OK since I didn't need to pass an argument. I had missed the
return value case).
Best wishes
Mark
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2016-11-13 7:36 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-11-04 18:00 [PATCH] emacs: mua: add a pre-send-check-hook Mark Walters
2016-11-07 21:12 ` Matt Armstrong
2016-11-13 7:36 ` Mark Walters
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).