summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjao <jao@gnu.org>2021-08-22 03:58:32 +0100
committerjao <jao@gnu.org>2021-08-23 01:01:51 +0100
commit0d7d909aa8bc4fb8e5ab80f93bc397fbed666761 (patch)
treeb72398741ece2c7f75890eb32575b501990db68a
parent0bc848869b70e504bcdc0f6dacd43bc8dc757608 (diff)
downloadnotmuch-0d7d909aa8bc4fb8e5ab80f93bc397fbed666761.tar.gz
notmuch-0d7d909aa8bc4fb8e5ab80f93bc397fbed666761.tar.bz2
emacs: extensions to search result format specificationstree-result-format
Allow arbitrary formatting functions as well as user-defined field-extraction functions in elements of notmuch-{search, tree, unthreaded}-result-format
-rw-r--r--emacs/notmuch-tree.el75
-rw-r--r--emacs/notmuch.el20
2 files changed, 78 insertions, 17 deletions
diff --git a/emacs/notmuch-tree.el b/emacs/notmuch-tree.el
index a1dfa7b1..d6c67df2 100644
--- a/emacs/notmuch-tree.el
+++ b/emacs/notmuch-tree.el
@@ -93,41 +93,78 @@ empty string or to a different kind of arrow point."
(defcustom notmuch-tree-result-format
`(("date" . "%12s ")
("authors" . "%-20s")
- ((("tree" . "%s")("subject" . "%s")) ." %-54s ")
+ ((("tree" . "%s") ("subject" . "%s")) . " %-54s ")
("tags" . "(%s)"))
- "Result formatting for tree view. Supported fields are: date,
-authors, subject, tree, tags. Tree means the thread tree
-box graphics. The field may also be a list in which case
-the formatting rules are applied recursively and then the
-output of all the fields in the list is inserted
-according to format-string.
+ "Result formatting for tree view.
+
+Supported fields are: date, authors, subject, tree, tags or a
+symbol denoting an elisp function. Tree means the thread tree
+box graphics. The format can be either a format string or a
+function name, which is called with the string resulting from
+evaluating the field with format \"%s\".
+
+For example, the spec:
+
+ (\"subject\" . downcase)
+
+will print the subject in lower case (i.e., as returned by `downcase').
+
+When the field is a function instead of a string, the function is
+called with the current message properties as its first argument.
+For instance, if we define:
+
+ (defun downcase-subject (msg)
+ (downcase (plist-get msg :subject)))
+
+the spec
+
+ (downcase-subject)
+
+will print the subject in downcase (i.e., the result of calling
+`downcase-subject' with a plist of message properties).
+
+The field may also be a list in which case the formatting rules
+are applied recursively and then the output of all the fields in
+the list is inserted according to format-string.
Note the author string should not contain
whitespace (put it in the neighbouring fields instead).
For example:
(setq notmuch-tree-result-format \(\(\"authors\" . \"%-40s\"\)
\(\"subject\" . \"%s\"\)\)\)"
- :type '(alist :key-type (string) :value-type (string))
+ :type '(alist :key-type (choice (string :tag "Field name")
+ (symbol :tag "Function name"))
+ :value-type (choice (string :tag "Format string")
+ (const :tag "Message properties" nil)
+ (symbol :tag "Formatting function")))
:group 'notmuch-tree)
(defcustom notmuch-unthreaded-result-format
`(("date" . "%12s ")
("authors" . "%-20s")
- ((("subject" . "%s")) ." %-54s ")
+ ("subject" . " %-54s ")
("tags" . "(%s)"))
- "Result formatting for unthreaded tree view. Supported fields are: date,
-authors, subject, tree, tags. Tree means the thread tree
-box graphics. The field may also be a list in which case
-the formatting rules are applied recursively and then the
-output of all the fields in the list is inserted
-according to format-string.
+ "Result formatting for unthreaded tree view.
+
+Supported fields are: date, authors, subject, tree, tags or a
+symbol denoting an elisp function. The format can be either a
+format string or a function taking the field's value and
+formatting it. The field may also be a list of other field
+specifications.
+
+Please see the docstring of `notmuch-tree-result-format' for more
+details and examples.
Note the author string should not contain
whitespace (put it in the neighbouring fields instead).
For example:
(setq notmuch-tree-result-format \(\(\"authors\" . \"%-40s\"\)
\(\"subject\" . \"%s\"\)\)\)"
- :type '(alist :key-type (string) :value-type (string))
+ :type '(alist :key-type (choice (string :tag "Field name")
+ (symbol :tag "Function name"))
+ :value-type (choice (string :tag "Format string")
+ (const :tag "Message properties" nil)
+ (symbol :tag "Formatting function")))
:group 'notmuch-tree)
(defun notmuch-tree-result-format ()
@@ -886,6 +923,12 @@ unchanged ADDRESS if parsing fails."
(let* ((headers (plist-get msg :headers))
(match (plist-get msg :match)))
(cond
+ ((symbolp field)
+ (apply field msg format-string))
+
+ ((symbolp format-string)
+ (funcall format-string (notmuch-tree-format-field field "%s" msg)))
+
((listp field)
(format format-string (notmuch-tree-format-field-list field msg)))
diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index 739cb93b..e73ec5f4 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -98,7 +98,21 @@ currently experimental). Note that a line break at the end of an
\"authors\" field will get elided if the authors list is long;
place it instead at the beginning of the following field. To
enter a line break when setting this variable with setq, use \\n.
-To enter a line break in customize, press \\[quoted-insert] C-j."
+To enter a line break in customize, press \\[quoted-insert] C-j.
+
+It is also possible to specify a formatting function instead of a
+format string, for instance
+
+ (\"subject\" . downcase)
+
+or a symbol instead of a field name, in which case the function
+denoted by the symbol is called with the properties of the search
+result. E.g., if we define the function
+
+ (defun downcase-subject (msg)
+ (downcase (plist-get msg :subject)))
+
+the spec `(downcase-subject)' will also print a downcased subject."
:type '(alist :key-type (string) :value-type (string))
:group 'notmuch-search)
@@ -831,6 +845,10 @@ non-authors is found, assume that all of the authors match."
(defun notmuch-search-insert-field (field format-string result)
(cond
+ ((symbolp field)
+ (apply field result format-string))
+ ((symbolp format-string)
+ (funcall format-string (notmuch-tree-format-field field "%s" result)))
((string-equal field "date")
(insert (propertize (format format-string (plist-get result :date_relative))
'face 'notmuch-search-date)))