;;; jao-notmuch.el --- Extensions for notmuch -*- lexical-binding: t; -*- ;; Copyright (C) 2021 jao ;; Author: jao ;; Keywords: mail ;; This program 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. ;; This program 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 this program. If not, see . ;;; Commentary: ;; Extensions to vanilla notmuch, mostly for tree view ;;; Code: (require 'outline) (require 'mm-decode) (require 'mm-view) (require 'notmuch) (require 'notmuch-tree) (require 'notmuch-show) ;;;; Targetting the displayed message from the tree view (defvar-local jao-notmuch--tree-buffer nil) (declare eww--url-at-point "eww") (defun jao-notmuch-goto-message-buffer (&optional and-click no-record) (interactive "P") (when (window-live-p notmuch-tree-message-window) (let ((b (current-buffer))) (select-window notmuch-tree-message-window no-record) (setq-local jao-notmuch--tree-buffer b) (or (not and-click) (cond ((eww--url-at-point) (shr-browse-url) 'url) ((button-at (point)) (push-button) 'button)))))) (defun jao-notmuch-tree-toggle-message () (interactive) (if (window-live-p notmuch-tree-message-window) (notmuch-tree-close-message-window) (notmuch-tree-show-message nil))) (defun jao-notmuch-click-message-buffer () (interactive) (let ((b (current-buffer))) (unless (eq 'url (jao-notmuch-goto-message-buffer t t)) (pop-to-buffer b)))) (defun jao-notmuch-goto-tree-buffer (&optional no-record) (interactive) (if (buffer-live-p jao-notmuch--tree-buffer) (pop-to-buffer jao-notmuch--tree-buffer nil no-record) (user-error "No tree view for this buffer"))) (defun jao-notmuch-tree--looking-at-message () (when-let (id (plist-get (notmuch-tree-get-message-properties) :id)) (equal (concat "*notmuch-id:" id "*") (buffer-name (window-buffer notmuch-tree-message-window))))) (defun jao-notmuch-tree-scroll-or-next () "Scroll or next message in forest or exit if none." (interactive) (if (not (jao-notmuch-tree--looking-at-message)) (jao-notmuch-tree-show-or-scroll t) (if (notmuch-tree-scroll-message-window) (jao-notmuch-tree-next nil) (when (not (window-live-p notmuch-tree-message-window)) (notmuch-tree-show-message nil))) (jao-notmuch--tree-update-buffer-name))) (defun jao-notmuch-tree-show-or-scroll (force) "Show current message, or scroll it if visible." (interactive "P") (if (and (not force) (window-live-p notmuch-tree-message-window) (jao-notmuch-tree--looking-at-message)) (scroll-other-window 1) (notmuch-tree-show-message nil) (jao-notmuch--tree-update-buffer-name))) (notmuch-tree--define-do-in-message-window jao-notmuch-tree-end-of-buffer end-of-buffer) (notmuch-tree--define-do-in-message-window jao-notmuch-tree-beginning-of-buffer beginning-of-buffer) (defun jao-notmuch--page-urls (res) (save-excursion (goto-char (point-min)) (let ((pos (point))) (while (setq pos (next-single-property-change pos 'w3m-href-anchor)) (when-let ((url (get-text-property pos 'w3m-href-anchor))) (when (stringp url) (cl-pushnew url res :test #'string=)))) (seq-uniq res #'string=)))) (defun jao-notmuch-browse-urls (&optional external) (interactive "P") (when (or (derived-mode-p 'notmuch-show-mode) (jao-notmuch-goto-message-buffer)) (let ((urls (jao-notmuch--page-urls (notmuch-show--gather-urls))) (fn (if external browse-url-secondary-browser-function #'browse-url))) (if urls (funcall fn (completing-read "Browse URL: " urls)) (message "No URLs in this message"))))) ;;;; Navigating URLs (defun jao-notmuch-show-next-button () (interactive) (when (get-text-property (point) 'w3m-href-anchor) (goto-char (next-single-property-change (point) 'w3m-href-anchor))) (if-let (pos (next-single-property-change (point) 'w3m-href-anchor)) (goto-char pos) (forward-button 1))) (defun jao-notmuch-show-previous-button () (interactive) (if-let (pos (previous-single-property-change (point) 'w3m-href-anchor)) (goto-char (previous-single-property-change pos 'w3m-href-anchor)) (backward-button 1))) (defun jao-notmuch-show-ret () (interactive) (when-let (url (or (get-text-property (point) 'w3m-href-anchor) (thing-at-point-url-at-point))) (browse-url url))) ;;;; Toggling mime parts and images (defun jao-notmuch--toggle-mime () (save-excursion (goto-char (point-min)) (while (and (re-search-forward "^\\[ text/\\(html\\|plain\\) " nil t) (button-at (point))) (notmuch-show-toggle-part-invisibility)))) (defun jao-notmuch--show-hidden-html () (when (save-excursion (goto-char (point-min)) (re-search-forward "^\\[ text/html (hidden) \\]" nil t)) (jao-notmuch--toggle-mime))) (defun jao-notmuch-show-prefer-html () (add-hook 'notmuch-show-hook #'jao-notmuch--show-hidden-html)) (defun jao-notmuch-toggle-mime-parts () (interactive) (when (jao-notmuch-goto-message-buffer nil t) (goto-char (point-min)) (jao-notmuch--toggle-mime) (jao-notmuch-goto-tree-buffer t) (jao-notmuch--tree-update-buffer-name))) (defvar-local jao-notmuch--showing-images nil) (defun jao-notmuch--setup-w3m-images (&optional activate) (setq-local w3m-ignored-image-url-regexp (unless jao-notmuch--showing-images notmuch-show-text/html-blocked-images)) (when activate (w3m-toggle-inline-images (if jao-notmuch--showing-images t 'turnoff)))) (defun jao-notmuch-show-images () (interactive) (save-window-excursion (when (or (derived-mode-p 'notmuch-show-mode) (jao-notmuch-goto-message-buffer nil t)) (goto-char (point-min)) (when (re-search-forward "^\\[ text/html " nil t) (when (looking-at-p "(hidden)") (notmuch-show-toggle-part-invisibility)) (forward-line 1) (setq jao-notmuch--showing-images (not jao-notmuch--showing-images)) (jao-notmuch--setup-w3m-images t))))) (add-hook 'notmuch-show-mode-hook #'jao-notmuch--setup-w3m-images) ;;;; w3m renderer (defun jao-w3m-gnus-html-renderer (handle) (let ((w3m-message-silent t) (mm-w3m-safe-url-regexp nil)) (condition-case nil (mm-inline-text-html-render-with-w3m handle) (error (delete-region (point) (point-max)) (let ((shr-use-fonts nil) (shr-use-colors nil)) (mm-shr handle)))))) (setq mm-text-html-renderer #'jao-w3m-gnus-html-renderer) ;;;; Keeping track of unread messages in current tree view (defvar-local jao-notmuch--query-name nil) (defun jao-notmuch--query-name () (when notmuch-tree-basic-query (or jao-notmuch--query-name (setq jao-notmuch--query-name (let ((q (seq-find (lambda (q) (string= (or (plist-get q :query) "") notmuch-tree-basic-query)) notmuch-saved-searches))) (or (plist-get q :name) notmuch-tree-basic-query)))))) (defun jao-notmuch--looking-at-new-p (&optional p) (when-let (ts (if p (plist-get p :tags) (notmuch-show-get-tags))) (or (member "unread" ts) (member "new" ts)))) (defsubst jao-notmuch-tree--first-p (&optional msg) (plist-get (or msg (notmuch-tree-get-message-properties)) :first)) (defun jao-notmuch--unread-counts (&optional thread) (let ((cnt) (total 0) (match 0) (msg)) (save-excursion (if thread (while (and (not (jao-notmuch-tree--first-p)) (zerop (forward-line -1)))) (goto-char (point-min))) (while (and (setq msg (notmuch-tree-get-message-properties)) (or (not cnt) (not thread) (not (jao-notmuch-tree--first-p msg)))) (unless cnt (setq cnt 0)) (setq total (1+ total)) (when (plist-get msg :match) (setq match (1+ match))) (when (jao-notmuch--looking-at-new-p msg) (setq cnt (1+ cnt))) (forward-line 1))) (when cnt (list total match cnt)))) (defvar jao-notmuch-tree-buffer-name-format "%Q [%N / %M] %n / %m") (defun jao-notmuch-tree--format-name (query total match new ttotal tmatch tnew) (format-spec jao-notmuch-tree-buffer-name-format `((?Q . ,query) (?T . ,total) (?N . ,new) (?M . ,match) (?t . ,ttotal) (?n . ,tnew) (?m . ,tmatch)))) (defun jao-notmuch--tree-update-buffer-name () (when-let* ((n (jao-notmuch--unread-counts)) (nc (append n (jao-notmuch--unread-counts t))) (q (jao-notmuch--query-name))) (prog1 (rename-buffer (apply #'jao-notmuch-tree--format-name q nc)) (when (fboundp 'jao-minibuffer-refresh) (jao-minibuffer-refresh))))) (defun jao-notmuch-tree--find-update-buffer-name (&rest _args) (when-let ((mb (window-buffer notmuch-tree-message-window))) (seq-find (lambda (b) (with-current-buffer b (and (derived-mode-p 'notmuch-tree-mode) (eq notmuch-tree-message-buffer mb) (jao-notmuch--tree-update-buffer-name)))) (buffer-list)))) (add-hook 'notmuch-after-tag-hook #'jao-notmuch-tree--find-update-buffer-name) (defun jao-notmuch-echo-count () "Show the number of unread messages left in this tree view." (interactive) (message (jao-notmuch--tree-update-buffer-name))) ;;;; Outline mode for tree view (defun jao-notmuch-tree--msg-prefix (msg) (insert (propertize (if (plist-get msg :first) "> " " ") 'display ""))) (defun jao-notmuch-tree--mode-setup () (setq-local outline-regexp "^> \\|^En") (outline-minor-mode t)) (defun jao-notmuch-tree-hide-others (&optional and-show) (interactive) (outline-hide-body) (outline-show-entry) (when and-show (notmuch-tree-show-message nil))) (defun jao-notmuch-tree--next (prev thread no-exit) (let ((line-move-ignore-invisible nil)) (cond ((jao-notmuch--looking-at-new-p)) (thread (notmuch-tree-next-thread prev) (unless (or (not (notmuch-tree-get-message-properties)) (jao-notmuch--looking-at-new-p)) (notmuch-tree-matching-message prev (not no-exit)))) (t (notmuch-tree-matching-message prev (not no-exit))))) (when (notmuch-tree-get-message-id) (jao-notmuch-tree-hide-others t)) (when prev (forward-char 2))) (defvar jao-notmuch-tree--prefix-map (let ((m (make-keymap "Thread operations"))) (define-key m (kbd "TAB") #'outline-cycle) (define-key m (kbd "t") #'outline-toggle-children) (define-key m (kbd "s") #'outline-show-entry) (define-key m (kbd "S") #'outline-show-all) (define-key m (kbd "h") #'outline-hide-entry) (define-key m (kbd "H") #'outline-hide-body) (define-key m (kbd "o") #'jao-notmuch-tree-hide-others) (define-key m (kbd "n") #'outline-hide-other) m)) (defun jao-notmuch-tree-outline-setup (&optional prefix) (define-key notmuch-tree-mode-map (kbd (or prefix "T")) jao-notmuch-tree--prefix-map) (define-key notmuch-tree-mode-map (kbd "TAB") #'outline-cycle) (define-key notmuch-tree-mode-map (kbd "M-TAB") #'outline-cycle-buffer) (add-hook 'notmuch-tree-mode-hook #'jao-notmuch-tree--mode-setup) (advice-add 'notmuch-tree-insert-msg :before #'jao-notmuch-tree--msg-prefix)) (defun jao-notmuch-tree-next (thread &optional no-exit) "Next message or thread in forest, taking care of thread visibility." (interactive "P") (jao-notmuch-tree--next nil thread no-exit)) (defun jao-notmuch-tree-next-thread (&optional exit) "Next thread in forest, taking care of thread visibility." (interactive "P") (jao-notmuch-tree--next nil t exit)) (defun jao-notmuch-tree-previous (thread) "Previous message or thread in forest, taking care of thread visibility." (interactive "P") (jao-notmuch-tree--next t thread t)) (defun jao-notmuch-tree-previous-thread (&optional exit) "Previous thread in forest, taking care of thread visibility." (interactive "P") (jao-notmuch-tree--next t t exit)) ;;;; Updating the tree window after insertion (defun jao-notmuch--tree-sentinel (proc &rest _) (when (eq (process-status proc) 'exit) (jao-notmuch--tree-update-buffer-name) (jao-notmuch-tree-hide-others))) (defun jao-notmuch-tree-setup (&optional prefix) "Set up display of trees, with PREFIX key for outline commands." (jao-notmuch-tree-outline-setup prefix) (advice-add 'notmuch-tree-process-sentinel :after #'jao-notmuch--tree-sentinel)) ;;;; Tagging (defun jao-notmuch-tag-jump-and-next (reverse) (interactive "P") (notmuch-tag-jump reverse) (jao-notmuch-tree-next nil t)) (defun jao-notmuch-tree--tag-and-next (tags reverse whole-thread) (let ((c (notmuch-tag-change-list tags reverse))) (if whole-thread (notmuch-tree-tag-thread c) (notmuch-tree-tag c))) (jao-notmuch-tree-next whole-thread t)) (defun jao-notmuch-tree-delete-message (undelete) (interactive "P") (jao-notmuch-tree--tag-and-next '("+deleted" "-new" "-unread") undelete nil)) (defun jao-notmuch-tree-delete-thread () (interactive) (jao-notmuch-tree--tag-and-next '("+deleted" "-new" "-unread") nil t)) (defun jao-notmuch-tree-read-thread () (interactive) (jao-notmuch-tree--tag-and-next '("-unread" "-new") nil t)) (defun jao-notmuch-tree-flag (unmark) (interactive "P") (let ((tags (if unmark '("-flagged") '("-unread" "-new" "-deleted" "+flagged")))) (jao-notmuch-tree--tag-and-next tags nil nil))) (defun jao-notmuch-tree-spam (unmark) (interactive "P") (let ((tags (if unmark '("-spam") '("-unread" "-new" "+spam")))) (jao-notmuch-tree--tag-and-next tags nil nil))) ;;;; Results formatters (defun jao-notmuch-format-tags (fmt msg) (let ((ts (thread-last (notmuch-tree-format-field "tags" "%s" msg) (split-string) (seq-sort-by #'length #'<)))) (format-spec fmt `((?s . ,(mapconcat #'identity ts " ")))))) (defun jao-notmuch-tree-and-subject (fmt msg) (let ((tr (notmuch-tree-format-field "tree" " %s" msg)) (sb (notmuch-tree-format-field "subject" " %s" msg))) (format-spec fmt `((?s . ,(concat tr sb)))))) (defun jao-notmuch-msg-ticks (mails-rx msg) (let ((headers (plist-get msg :headers))) (cond ((string-match-p mails-rx (or (plist-get headers :To) "")) (propertize " »" 'face 'notmuch-tree-match-tree-face)) ((string-match-p mails-rx (or (plist-get headers :Cc) "")) (propertize " ¬" 'face 'notmuch-tree-match-tree-face)) (t " ")))) (provide 'jao-notmuch) ;;; jao-notmuch.el ends here