unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
* notmuch-show-header-line: allow format strings and functions
@ 2022-05-18 19:29 jao
  2022-05-18 19:29 ` [PATCH] emacs: " jao
  0 siblings, 1 reply; 9+ messages in thread
From: jao @ 2022-05-18 19:29 UTC (permalink / raw)
  To: notmuch

This one overrides id:20220516022039.551596-1-jao@gnu.org, hopefully
using proper indentation, and refactoring the new functionality in a
separate function for clarity.


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

* [PATCH] emacs: notmuch-show-header-line: allow format strings and functions
  2022-05-18 19:29 notmuch-show-header-line: allow format strings and functions jao
@ 2022-05-18 19:29 ` jao
  2022-05-26 23:51   ` David Bremner
  0 siblings, 1 reply; 9+ messages in thread
From: jao @ 2022-05-18 19:29 UTC (permalink / raw)
  To: notmuch; +Cc: jao

If a string value is assigned to notmuch-show-header-line, it's used
as a format string to be passed passed to format-spec with `%s`
substituted by the message's subject.  If a function is given, it's
called with the subject as argument, and its return value used as
header line.

As before, t means displaying the subject and nil not using any header
line.

Signed-off-by: jao <jao@gnu.org>
---
 emacs/notmuch-show.el | 42 +++++++++++++++++++++++++++++++++---------
 1 file changed, 33 insertions(+), 9 deletions(-)

diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el
index 69f6c845..266afbaa 100644
--- a/emacs/notmuch-show.el
+++ b/emacs/notmuch-show.el
@@ -85,8 +85,26 @@ visible for any given message."
   :group 'notmuch-show)
 
 (defcustom notmuch-show-header-line t
-  "Show a header line with the current message's subject."
-  :type 'boolean
+  "Show a header line in notmuch show buffers.
+
+If t (the default), the header line will contain the current
+message's subject.
+
+If a string, this value is interpreted as a format string to be
+passed to `format-spec` with `%s` as the substitution variable
+for the message's subject.  E.g., to display the subject trimmed
+to a maximum of 80 columns, you could use \"%>-80s\" as format.
+
+If you assign to this variable a function, the function will be
+called with the subject as argument and its return value used as
+the header line.
+
+Finally, if this variabale is set to nil, no header is
+displayed."
+  :type '(choice (const :tag "No header" ni)
+                 (const :tag "Subject" t)
+                 (string :tag "Format")
+		 (function :tag "Function"))
   :group 'notmuch-show)
 
 (defcustom notmuch-show-relative-dates t
@@ -1313,6 +1331,18 @@ fallback if the prior matches no messages."
       (push (list thread "and (" context ")") queries))
     queries))
 
+(defun notmuch-show--header-line-format ()
+  "Compute the header line format of a notmuch-show buffer."
+  (when notmuch-show-header-line
+    (let* ((s (notmuch-sanitize
+	       (notmuch-show-strip-re (notmuch-show-get-subject))))
+	   (subject (replace-regexp-in-string "%" "%%" s)))
+      (cond ((stringp notmuch-show-header-line)
+             (format-spec notmuch-show-header-line `((?s . ,subject))))
+	    ((functionp notmuch-show-header-line)
+	     (funcall notmuch-show-header-line subject))
+	    (notmuch-show-header-line subject)))))
+
 (defun notmuch-show--build-buffer (&optional state)
   "Display messages matching the current buffer context.
 
@@ -1343,13 +1373,7 @@ If no messages match the query return NIL."
       ;; display changes.
       (notmuch-show-mapc
        (lambda () (notmuch-show-set-prop :orig-tags (notmuch-show-get-tags))))
-      ;; Set the header line to the subject of the first message.
-      (when notmuch-show-header-line
-	(setq header-line-format
-	      (replace-regexp-in-string "%" "%%"
-					(notmuch-sanitize
-					 (notmuch-show-strip-re
-					  (notmuch-show-get-subject))))))
+      (setq header-line-format (notmuch-show--header-line-format))
       (run-hooks 'notmuch-show-hook)
       (if state
 	  (notmuch-show-apply-state state)
-- 
2.36.1

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

* Re: [PATCH] emacs: notmuch-show-header-line: allow format strings and functions
  2022-05-18 19:29 ` [PATCH] emacs: " jao
@ 2022-05-26 23:51   ` David Bremner
  2022-05-27  0:26     ` Jose A Ortega Ruiz
  0 siblings, 1 reply; 9+ messages in thread
From: David Bremner @ 2022-05-26 23:51 UTC (permalink / raw)
  To: jao, notmuch

jao <jao@gnu.org> writes:

> If a string value is assigned to notmuch-show-header-line, it's used
> as a format string to be passed passed to format-spec with `%s`
> substituted by the message's subject.  If a function is given, it's
> called with the subject as argument, and its return value used as
> header line.

The API defined here (function taking the subject as a string) seems a
bit limited. What if the user wants to use some other message header in
the header-line? Or list tags?

I would complain about missing test, but I think the test machinery
works via buffer-substring-no-properties, which excludes header and mode
lines.

On the other hand, it should be possible to update the manual, at least
with a copy of the docstring.

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

* Re: [PATCH] emacs: notmuch-show-header-line: allow format strings and functions
  2022-05-26 23:51   ` David Bremner
@ 2022-05-27  0:26     ` Jose A Ortega Ruiz
  2022-06-01  9:26       ` David Bremner
  0 siblings, 1 reply; 9+ messages in thread
From: Jose A Ortega Ruiz @ 2022-05-27  0:26 UTC (permalink / raw)
  To: David Bremner, notmuch

On Thu, May 26 2022, David Bremner wrote:

> jao <jao@gnu.org> writes:
>
>> If a string value is assigned to notmuch-show-header-line, it's used
>> as a format string to be passed passed to format-spec with `%s`
>> substituted by the message's subject.  If a function is given, it's
>> called with the subject as argument, and its return value used as
>> header line.
>
> The API defined here (function taking the subject as a string) seems a
> bit limited. What if the user wants to use some other message header in
> the header-line? Or list tags?

well, the function is called with the message buffer being current, so
it has access to all the message properties... we could even remove the
subject argument, but there's some convenient cleaning up on it that, i
think, makes it convenient.

> I would complain about missing test, but I think the test machinery
> works via buffer-substring-no-properties, which excludes header and
> mode lines.

yes, i didn't find an easy way of testing.

> On the other hand, it should be possible to update the manual, at
> least with a copy of the docstring.

yes, i'll add it once we settle on a final version.  one can play nice
tricks with this, such as returning a mode line format specification
from that function (since the result is assigned to the
header-line-format), and have an even more dynamic header (i put in
there an '(:eval fun-that-computes-thread-message-read/unread-counts) 
one)...

cheers,
jao
-- 
"It is easy to be blinded to the essential uselessness of computers by
the sense of accomplishment you get from getting them to work at all."
 - Douglas Adams

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

* Re: [PATCH] emacs: notmuch-show-header-line: allow format strings and functions
  2022-05-27  0:26     ` Jose A Ortega Ruiz
@ 2022-06-01  9:26       ` David Bremner
  2022-06-01  9:56         ` Jose A Ortega Ruiz
  0 siblings, 1 reply; 9+ messages in thread
From: David Bremner @ 2022-06-01  9:26 UTC (permalink / raw)
  To: Jose A Ortega Ruiz, notmuch

Jose A Ortega Ruiz <jao@gnu.org> writes:

> On Thu, May 26 2022, David Bremner wrote:

>> The API defined here (function taking the subject as a string) seems a
>> bit limited. What if the user wants to use some other message header in
>> the header-line? Or list tags?
>
> well, the function is called with the message buffer being current, so
> it has access to all the message properties... we could even remove the
> subject argument, but there's some convenient cleaning up on it that, i
> think, makes it convenient.
>

Fair enough. But since the calling is not obvious to the casual user
(*cough* or me), maybe mention the availability of message properties in
the docstring.

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

* Re: [PATCH] emacs: notmuch-show-header-line: allow format strings and functions
  2022-06-01  9:26       ` David Bremner
@ 2022-06-01  9:56         ` Jose A Ortega Ruiz
  2022-06-01 10:25           ` David Bremner
  0 siblings, 1 reply; 9+ messages in thread
From: Jose A Ortega Ruiz @ 2022-06-01  9:56 UTC (permalink / raw)
  To: David Bremner, notmuch

On Wed, Jun 01 2022, David Bremner wrote:

> Fair enough. But since the calling is not obvious to the casual user
> (*cough* or me), maybe mention the availability of message properties
> in the docstring.

Yes, good idea.  I've done just that in a new version (v4) of the patch.
I've also noticed the variable notmuch-show-header-line is already
documented in doc/notmuch-emacs.rst (by importing its docstring): do we
need to add documentation elsewhere?

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

* Re: [PATCH] emacs: notmuch-show-header-line: allow format strings and functions
  2022-06-01  9:56         ` Jose A Ortega Ruiz
@ 2022-06-01 10:25           ` David Bremner
  2022-06-01 10:34             ` Jose A Ortega Ruiz
  0 siblings, 1 reply; 9+ messages in thread
From: David Bremner @ 2022-06-01 10:25 UTC (permalink / raw)
  To: Jose A Ortega Ruiz, notmuch

Jose A Ortega Ruiz <jao@gnu.org> writes:

> On Wed, Jun 01 2022, David Bremner wrote:
>
>> Fair enough. But since the calling is not obvious to the casual user
>> (*cough* or me), maybe mention the availability of message properties
>> in the docstring.
>
> Yes, good idea.  I've done just that in a new version (v4) of the patch.
> I've also noticed the variable notmuch-show-header-line is already
> documented in doc/notmuch-emacs.rst (by importing its docstring): do we
> need to add documentation elsewhere?

As long as you are happy enough with the way it imports (e.g. run make
sphinx-html), that's fine. I did notice one typo "variabale", but I can
amend that if you are otherwise happy.

d

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

* Re: [PATCH] emacs: notmuch-show-header-line: allow format strings and functions
  2022-06-01 10:25           ` David Bremner
@ 2022-06-01 10:34             ` Jose A Ortega Ruiz
  2022-06-01 11:39               ` David Bremner
  0 siblings, 1 reply; 9+ messages in thread
From: Jose A Ortega Ruiz @ 2022-06-01 10:34 UTC (permalink / raw)
  To: David Bremner, notmuch

On Wed, Jun 01 2022, David Bremner wrote:

>> Yes, good idea.  I've done just that in a new version (v4) of the patch.
>> I've also noticed the variable notmuch-show-header-line is already
>> documented in doc/notmuch-emacs.rst (by importing its docstring): do we
>> need to add documentation elsewhere?
>
> As long as you are happy enough with the way it imports (e.g. run make
> sphinx-html), that's fine. I did notice one typo "variabale", but I can
> amend that if you are otherwise happy.

Argh, sorry about that.  Yes, i'm happy, please go ahead if you find it
fine too.

Many thanks!

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

* Re: [PATCH] emacs: notmuch-show-header-line: allow format strings and functions
  2022-06-01 10:34             ` Jose A Ortega Ruiz
@ 2022-06-01 11:39               ` David Bremner
  0 siblings, 0 replies; 9+ messages in thread
From: David Bremner @ 2022-06-01 11:39 UTC (permalink / raw)
  To: Jose A Ortega Ruiz, notmuch

Jose A Ortega Ruiz <jao@gnu.org> writes:

> On Wed, Jun 01 2022, David Bremner wrote:
>
>>> Yes, good idea.  I've done just that in a new version (v4) of the patch.
>>> I've also noticed the variable notmuch-show-header-line is already
>>> documented in doc/notmuch-emacs.rst (by importing its docstring): do we
>>> need to add documentation elsewhere?
>>
>> As long as you are happy enough with the way it imports (e.g. run make
>> sphinx-html), that's fine. I did notice one typo "variabale", but I can
>> amend that if you are otherwise happy.
>
> Argh, sorry about that.  Yes, i'm happy, please go ahead if you find it
> fine too.
>
> Many thanks!

Amended version applied to master.

d

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

end of thread, other threads:[~2022-06-01 11:39 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-18 19:29 notmuch-show-header-line: allow format strings and functions jao
2022-05-18 19:29 ` [PATCH] emacs: " jao
2022-05-26 23:51   ` David Bremner
2022-05-27  0:26     ` Jose A Ortega Ruiz
2022-06-01  9:26       ` David Bremner
2022-06-01  9:56         ` Jose A Ortega Ruiz
2022-06-01 10:25           ` David Bremner
2022-06-01 10:34             ` Jose A Ortega Ruiz
2022-06-01 11:39               ` David Bremner

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