This patch extracts the rendering of tags in notmuch-show to a dedicated notmuch-tagger file.
This new file introduces a `notmuch-tagger-formats' variable that associates each tag to a particular format. For example, (("unread" (:propertize "unread" face (:foreground "red"))) ("flagged" (:propertize "flagged" display (image :type svg :file "~/notmuch/emacs/resources/star.svg" :ascent center :mask heuristic)))) associates a red font to the "unread" tag and a star picture to the "flagged" tag. In the future, I would like to use the Customization interface of Emacs to edit this variable and also to provide high-lever functions to manipulate it such that (notmuch-tagger-propertize "unread" :foreground "red") and (notmuch-tagger-picture "flagged" "star.svg"). `mode-line-format' templates are used to represent the format for each tag. This is a concize format that can also be used in `header-line-format' if later desired. Signed-off-by: Damien Cassou <damien.cas...@gmail.com> --- emacs/notmuch-show.el | 7 ++--- emacs/notmuch-tagger.el | 75 +++++++++++++++++++++++++++++++++++++++++++++++ emacs/notmuch.el | 5 ++-- 3 files changed, 80 insertions(+), 7 deletions(-) create mode 100644 emacs/notmuch-tagger.el diff --git a/emacs/notmuch-show.el b/emacs/notmuch-show.el index 1864dd1..7bf9f3c 100644 --- a/emacs/notmuch-show.el +++ b/emacs/notmuch-show.el @@ -36,6 +36,7 @@ (require 'notmuch-mua) (require 'notmuch-crypto) (require 'notmuch-print) +(require 'notmuch-tagger) (declare-function notmuch-call-notmuch-process "notmuch" (&rest args)) (declare-function notmuch-fontify-headers "notmuch" nil) @@ -362,8 +363,7 @@ operation on the contents of the current buffer." (if (re-search-forward "(\\([^()]*\\))$" (line-end-position) t) (let ((inhibit-read-only t)) (replace-match (concat "(" - (propertize (mapconcat 'identity tags " ") - 'face 'notmuch-tag-face) + (notmuch-tagger-present-tags tags) ")")))))) (defun notmuch-clean-address (address) @@ -441,8 +441,7 @@ message at DEPTH in the current thread." " (" date ") (" - (propertize (mapconcat 'identity tags " ") - 'face 'notmuch-tag-face) + (notmuch-tagger-present-tags tags) ")\n") (overlay-put (make-overlay start (point)) 'face 'notmuch-message-summary-face))) diff --git a/emacs/notmuch-tagger.el b/emacs/notmuch-tagger.el new file mode 100644 index 0000000..90730f6 --- /dev/null +++ b/emacs/notmuch-tagger.el @@ -0,0 +1,75 @@ +;; notmuch-tagger.el --- Library to improve the way tags are displayed +;; +;; Copyright © Damien Cassou +;; +;; This file is part of Notmuch. +;; +;; Notmuch is free software: you can redistribute it and/or modify it +;; under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. +;; +;; Notmuch is distributed in the hope that it will be useful, but +;; WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;; General Public License for more details. +;; +;; You should have received a copy of the GNU General Public License +;; along with Notmuch. If not, see <http://www.gnu.org/licenses/>. +;; +;; Authors: Damien Cassou <damien.cas...@gmail.com> +;;; Commentary: +;; +;;; Comments +;; +;;; Code: +;; + +(require 'cl) + +(defvar notmuch-tagger-formats + `(("unread" + (:propertize "unread" face + (:foreground "red"))) + ("flagged" + (:propertize "flagged" display + (image :type svg :file + ,(expand-file-name + "resources/star.svg" + (file-name-directory + (or + (locate-library "notmuch-tagger") + (buffer-file-name)))) + :ascent center :mask heuristic)))) + "Contains pairs of (KEY FORMAT) to format a tag matching KEY. + +KEY must be a string with a tag name. In the future, KEY could +also be a regexp or list of keys to be matched against tags. + +The default value set the unread tag to be red and the flagged +tag to have a star picture attached. Those are just examples so +you get an idea of what you can do.") + +(defun notmuch-tagger-tag-format (tag) + "Format TAG as a `mode-line-format' template. + +The format to apply to TAG is searched in +`notmuch-tagger-formats'. If not found, the default +`notmuch-tag-face' is used." + (let ((match (assoc tag notmuch-tagger-formats))) + (if match + (cadr match) + `(:propertize ,tag face notmuch-tag-face)))) + +(defun notmuch-tagger-tags-format (tags) + "Format TAGS as a `mode-line-format' template." + (notmuch-intersperse + (remove nil (mapcar #'notmuch-tagger-tag-format tags)) + " ")) + +(defun notmuch-tagger-present-tags (tags) + "Return a string that represent TAGS with their format." + (format-mode-line (notmuch-tagger-tags-format tags))) + +(provide 'notmuch-tagger) +;;; notmuch-tagger.el ends here diff --git a/emacs/notmuch.el b/emacs/notmuch.el index c98a4fe..c607905 100644 --- a/emacs/notmuch.el +++ b/emacs/notmuch.el @@ -797,9 +797,8 @@ non-authors is found, assume that all of the authors match." (notmuch-search-insert-authors format-string (plist-get result :authors))) ((string-equal field "tags") - (let ((tags-str (mapconcat 'identity (plist-get result :tags) " "))) - (insert (propertize (format format-string tags-str) - 'face 'notmuch-tag-face)))))) + (let ((tags (plist-get result :tags))) + (insert (format format-string (notmuch-tagger-present-tags tags))))))) (defun notmuch-search-show-result (result &optional pos) "Insert RESULT at POS or the end of the buffer if POS is null." -- 1.7.10.4 _______________________________________________ notmuch mailing list notmuch@notmuchmail.org http://notmuchmail.org/mailman/listinfo/notmuch