unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* [PATCH v2] emacs: use message-dont-reply-to-names when composing replies
@ 2022-06-04 20:21 jao
  2022-06-09  1:16 ` David Bremner
  0 siblings, 1 reply; 3+ messages in thread
From: jao @ 2022-06-04 20:21 UTC (permalink / raw)
  To: notmuch; +Cc: jao

notmuch-mua functions for replies now use the built-in customizable
variable message-dont-reply-to-names with the same semantics as
message-mode.

---

This version of the patch adds more testing and ensures predicate values work

Signed-off-by: jao <jao@gnu.org>
---
 doc/notmuch-emacs.rst               |  9 ++++
 emacs/notmuch-mua.el                | 13 +++++
 test/T454-emacs-dont-reply-names.sh | 76 +++++++++++++++++++++++++++++
 3 files changed, 98 insertions(+)
 create mode 100755 test/T454-emacs-dont-reply-names.sh

diff --git a/doc/notmuch-emacs.rst b/doc/notmuch-emacs.rst
index 78528785..970cd7b7 100644
--- a/doc/notmuch-emacs.rst
+++ b/doc/notmuch-emacs.rst
@@ -493,6 +493,15 @@ Sending Mail
        :code:`compose-mail`.  To use ``notmuch`` for this, customize this
        variable to the symbol :code:`notmuch-user-agent`.
 
+:index:`message-dont-reply-to-names`
+
+       When composing mail replies, Emacs's message mode uses the
+       variable :code:`message-dont-reply-to-names` to exclude
+       recipients matching a given collection of regular expressions
+       or satisfying an arbitrary predicate.  Notmuch's MUA inherits
+       this standard mechanism and will honour your customization of
+       this variable.
+
 Init File
 ---------
 
diff --git a/emacs/notmuch-mua.el b/emacs/notmuch-mua.el
index 0ae33127..865f4bb2 100644
--- a/emacs/notmuch-mua.el
+++ b/emacs/notmuch-mua.el
@@ -382,6 +382,18 @@ instead of `message-mode' and SWITCH-FUNCTION is mandatory."
     (erase-buffer)
     (notmuch-message-mode)))
 
+(defun notmuch-mua--remove-dont-reply-to-names ()
+  (when-let ((nr (message-dont-reply-to-names)))
+    (let ((nr-filter (if (functionp nr)
+			 (lambda (mail) (unless (funcall nr mail) mail))
+		       (lambda (mail) (unless (string-match-p nr mail) mail)))))
+      (dolist (header '("To" "Cc"))
+	(when-let ((v (message-fetch-field header)))
+	  (let* ((v (mapcar #'string-trim (message-tokenize-header v)))
+		 (vs (delq nil (mapcar nr-filter v)))
+		 (v (when vs (mapconcat #'identity vs ", "))))
+	    (message-replace-header header v)))))))
+
 (defun notmuch-mua-mail (&optional to subject other-headers _continue
 				   switch-function yank-action send-actions
 				   return-action &rest ignored)
@@ -422,6 +434,7 @@ moved to the \"To:\" header."
 	(message-this-is-mail t))
     (message-setup-1 headers yank-action send-actions return-action))
   (notmuch-fcc-header-setup)
+  (notmuch-mua--remove-dont-reply-to-names)
   (message-sort-headers)
   (message-hide-headers)
   (set-buffer-modified-p nil)
diff --git a/test/T454-emacs-dont-reply-names.sh b/test/T454-emacs-dont-reply-names.sh
new file mode 100755
index 00000000..e31141f9
--- /dev/null
+++ b/test/T454-emacs-dont-reply-names.sh
@@ -0,0 +1,76 @@
+#!/usr/bin/env bash
+
+test_description="emacs reply"
+. $(dirname "$0")/test-lib.sh || exit 1
+. $NOTMUCH_SRCDIR/test/test-lib-emacs.sh || exit 1
+
+EXPECTED=$NOTMUCH_SRCDIR/test/emacs-show.expected-output
+
+test_require_emacs
+
+add_email_corpus default
+
+test_begin_subtest "regular expression"
+test_emacs '(let ((message-dont-reply-to-names "notmuchmail\\|noreply\\|harvard"))
+	      (notmuch-mua-new-reply
+	        "id:20091117203301.GV3165@dottiness.seas.harvard.edu" nil t)
+	      (test-visible-output "OUTPUT-FULL.raw"))'
+
+notmuch_dir_sanitize < OUTPUT-FULL.raw > OUTPUT-FULL
+head -6 OUTPUT-FULL > OUTPUT
+
+cat <<EOF > EXPECTED
+From: Notmuch Test Suite <test_suite@notmuchmail.org>
+To: Mikhail Gusarov <dottedmag@dottedmag.net>
+Subject: Re: [notmuch] Working with Maildir storage?
+In-Reply-To: <20091117203301.GV3165@dottiness.seas.harvard.edu>
+Fcc: MAIL_DIR/sent
+--text follows this line--
+EOF
+
+test_expect_equal_file EXPECTED OUTPUT
+
+test_begin_subtest "predicate"
+test_emacs '(let ((message-dont-reply-to-names
+	           (lambda (m) (string-prefix-p "Mikhail" m))))
+	      (notmuch-mua-new-reply
+	        "id:20091117203301.GV3165@dottiness.seas.harvard.edu" nil t)
+	      (test-visible-output "OUTPUT-FULL-PRED.raw"))'
+
+notmuch_dir_sanitize < OUTPUT-FULL-PRED.raw > OUTPUT-FULL-PRED
+head -7 OUTPUT-FULL-PRED > OUTPUT-PRED
+
+cat <<EOF > EXPECTED-PRED
+From: Notmuch Test Suite <test_suite@notmuchmail.org>
+To: Lars Kellogg-Stedman <lars@seas.harvard.edu>
+Cc: notmuch@notmuchmail.org
+Subject: Re: [notmuch] Working with Maildir storage?
+In-Reply-To: <20091117203301.GV3165@dottiness.seas.harvard.edu>
+Fcc: MAIL_DIR/sent
+--text follows this line--
+EOF
+
+test_expect_equal_file EXPECTED-PRED OUTPUT-PRED
+
+test_begin_subtest "nil value"
+test_emacs '(let ((message-dont-reply-to-names nil))
+	      (notmuch-mua-new-reply
+	        "id:20091117203301.GV3165@dottiness.seas.harvard.edu" nil t)
+	      (test-visible-output "OUTPUT-FULL-NIL.raw"))'
+
+notmuch_dir_sanitize < OUTPUT-FULL-NIL.raw > OUTPUT-FULL-NIL
+head -7 OUTPUT-FULL-NIL > OUTPUT-NIL
+
+cat <<EOF > EXPECTED-NIL
+From: Notmuch Test Suite <test_suite@notmuchmail.org>
+To: Lars Kellogg-Stedman <lars@seas.harvard.edu>, Mikhail Gusarov <dottedmag@dottedmag.net>
+Cc: notmuch@notmuchmail.org
+Subject: Re: [notmuch] Working with Maildir storage?
+In-Reply-To: <20091117203301.GV3165@dottiness.seas.harvard.edu>
+Fcc: MAIL_DIR/sent
+--text follows this line--
+EOF
+
+test_expect_equal_file EXPECTED-NIL OUTPUT-NIL
+
+test_done
-- 
2.36.1

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] emacs: use message-dont-reply-to-names when composing replies
  2022-06-04 20:21 [PATCH v2] emacs: use message-dont-reply-to-names when composing replies jao
@ 2022-06-09  1:16 ` David Bremner
  2022-06-09  2:12   ` Jose A Ortega Ruiz
  0 siblings, 1 reply; 3+ messages in thread
From: David Bremner @ 2022-06-09  1:16 UTC (permalink / raw)
  To: jao, notmuch; +Cc: jao

jao <jao@gnu.org> writes:

>
>  
> +(defun notmuch-mua--remove-dont-reply-to-names ()
> +  (when-let ((nr (message-dont-reply-to-names)))

Using when-let is fine with me, but I wonder if we should follow the
advice in subr-x to do

       (eval-when-compile (require 'subr-x))

It's autoloaded, but maybe we can improve loading time a little?
I noticed the other places we just do (require 'subr-x), but we're not
always the most *cough* sophisticated elisp programmers.

I'm a bit nervous about calling an undocumented defsubst here. It might
be better to inline the 3 line definition. What do you think?

> +    (let ((nr-filter (if (functionp nr)
> +			 (lambda (mail) (unless (funcall nr mail) mail))
> +		       (lambda (mail) (unless (string-match-p nr mail)  mail)))))

Inlining the definition would also make it more clear that the above
code handles the case where message-dont-reply-to-names is list of
regexps. Failing that maybe a comment.

> +      (dolist (header '("To" "Cc"))

I found the re-use of v confusing in this 'when-let'. It reminds me of
an exam question about scope :).

> +	(when-let ((v (message-fetch-field header)))
> +	  (let* ((v (mapcar #'string-trim (message-tokenize-header v)))
> +		 (vs (delq nil (mapcar nr-filter v)))
> +		 (v (when vs (mapconcat #'identity vs ", "))))

I think 'and' is conventional for places where we care about what the
expression evaluates to, and 'when' preferred when not.

> +	    (message-replace-header header v)))))))
> +
>  (defun notmuch-mua-mail (&optional to subject other-headers _continue
>  				   switch-function yank-action send-actions
>  				   return-action &rest ignored)
> @@ -422,6 +434,7 @@ moved to the \"To:\" header."
>  	(message-this-is-mail t))
>      (message-setup-1 headers yank-action send-actions return-action))
>    (notmuch-fcc-header-setup)
> +  (notmuch-mua--remove-dont-reply-to-names)
>    (message-sort-headers)
>    (message-hide-headers)
>    (set-buffer-modified-p nil)
> diff --git a/test/T454-emacs-dont-reply-names.sh b/test/T454-emacs-dont-reply-names.sh
> new file mode 100755
> index 00000000..e31141f9
> --- /dev/null
> +++ b/test/T454-emacs-dont-reply-names.sh
> @@ -0,0 +1,76 @@
> +#!/usr/bin/env bash
> +
> +test_description="emacs reply"

I guess this description should match the name of the file and mention
something like "don't reply"; this will make the individual test
descriptions make more sense

> +. $(dirname "$0")/test-lib.sh || exit 1
> +. $NOTMUCH_SRCDIR/test/test-lib-emacs.sh || exit 1
> +
> +EXPECTED=$NOTMUCH_SRCDIR/test/emacs-show.expected-output
> +
> +test_require_emacs
> +
> +add_email_corpus default
> +
> +test_begin_subtest "regular expression"
> +test_emacs '(let ((message-dont-reply-to-names "notmuchmail\\|noreply\\|harvard"))
> +	      (notmuch-mua-new-reply
> +	        "id:20091117203301.GV3165@dottiness.seas.harvard.edu" nil t)
> +	      (test-visible-output "OUTPUT-FULL.raw"))'
> +

Maybe mention this tests Cc; I don't _think_ the other tests do?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] emacs: use message-dont-reply-to-names when composing replies
  2022-06-09  1:16 ` David Bremner
@ 2022-06-09  2:12   ` Jose A Ortega Ruiz
  0 siblings, 0 replies; 3+ messages in thread
From: Jose A Ortega Ruiz @ 2022-06-09  2:12 UTC (permalink / raw)
  To: David Bremner, notmuch

On Wed, Jun 08 2022, David Bremner wrote:

> jao <jao@gnu.org> writes:
>
>>
>>  
>> +(defun notmuch-mua--remove-dont-reply-to-names ()
>> +  (when-let ((nr (message-dont-reply-to-names)))
>
> Using when-let is fine with me, but I wonder if we should follow the
> advice in subr-x to do
>
>        (eval-when-compile (require 'subr-x))
>
> It's autoloaded, but maybe we can improve loading time a little?
> I noticed the other places we just do (require 'subr-x), but we're not
> always the most *cough* sophisticated elisp programmers.

well, i'd be surprised if it made any noticeable difference, but it
surely won't hurt.  added.

> I'm a bit nervous about calling an undocumented defsubst here. It might
> be better to inline the 3 line definition. What do you think?

in all fairness, its name doesn't include a double dash, so that makes
it theoretically public.  but it's true it's undocumented, and, if
inlining makes the code clearer without having to add explanatory
messages, i think inlining is a net win. done.

>> +      (dolist (header '("To" "Cc"))
>
> I found the re-use of v confusing in this 'when-let'. It reminds me of
> an exam question about scope :).

i'll change it, no problem.

>
>> +	(when-let ((v (message-fetch-field header)))
>> +	  (let* ((v (mapcar #'string-trim (message-tokenize-header v)))
>> +		 (vs (delq nil (mapcar nr-filter v)))
>> +		 (v (when vs (mapconcat #'identity vs ", "))))
>
> I think 'and' is conventional for places where we care about what the
> expression evaluates to, and 'when' preferred when not.

fair enough.  i've removed also similar usages of 'unless'.

[...]

>> @@ -0,0 +1,76 @@
>> +#!/usr/bin/env bash
>> +
>> +test_description="emacs reply"
>
> I guess this description should match the name of the file and mention
> something like "don't reply"; this will make the individual test
> descriptions make more sense

oops yes, forgot to update it.

>> +. $(dirname "$0")/test-lib.sh || exit 1
>> +. $NOTMUCH_SRCDIR/test/test-lib-emacs.sh || exit 1
>> +
>> +EXPECTED=$NOTMUCH_SRCDIR/test/emacs-show.expected-output
>> +
>> +test_require_emacs
>> +
>> +add_email_corpus default
>> +
>> +test_begin_subtest "regular expression"
>> +test_emacs '(let ((message-dont-reply-to-names "notmuchmail\\|noreply\\|harvard"))
>> +	      (notmuch-mua-new-reply
>> +	        "id:20091117203301.GV3165@dottiness.seas.harvard.edu" nil t)
>> +	      (test-visible-output "OUTPUT-FULL.raw"))'
>> +
>
> Maybe mention this tests Cc; I don't _think_ the other tests do?

they actually do: they make it disappear!

i'm submitting a new version with the changes above: let me know how it
looks.

jao

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-06-09  2:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-04 20:21 [PATCH v2] emacs: use message-dont-reply-to-names when composing replies jao
2022-06-09  1:16 ` David Bremner
2022-06-09  2:12   ` Jose A Ortega Ruiz

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).