summaryrefslogtreecommitdiffhomepage
path: root/lib/net/jao-notmuch-gnus.el
blob: b6fafcd351f3d1300c7886277462841c50d83717 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
;;; jao-notmuch-gnus.el --- notmuch-gnus interoperability  -*- lexical-binding: t; -*-

;; Copyright (C) 2022  jao

;; Author: jao <mail@jao.io>
;; 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 <https://www.gnu.org/licenses/>.

;;; Commentary:

;;  Helper functions to work in Gnus with mail indexed by notmuch.

;;; Code:

(require 'gnus)
(require 'ol-gnus)
(require 'notmuch-show)


;;; Tagging in notmuch from Gnus buffers

(defun jao-notmuch-gnus--notmuch-id (id)
  (when id (if (string-match "<\\(.+\\)>" id) (match-string 1 id) id)))

(defun jao-notmuch-gnus-message-id (&optional no-show)
  "Find the id of currently selected message in Gnus or notmuch."
  (when (and (not no-show) (derived-mode-p 'gnus-summary-mode))
    (save-window-excursion (gnus-summary-show-article)))
  (cond (gnus-original-article-buffer
         (with-current-buffer gnus-original-article-buffer
           (jao-notmuch-gnus--notmuch-id (message-field-value "message-id"))))
        ((derived-mode-p 'notmuch-show-mode 'notmuch-tree-mode)
         (notmuch-show-get-message-id))))

(defun jao-notmuch-gnus-message-tags (id)
  "Ask notmuch for the tags of a message with the given ID."
  (let ((cmd (format "notmuch search --output=tags 'id:%s'" id)))
    (split-string (shell-command-to-string cmd))))

(defun jao-notmuch-gnus-tag-message (&optional id tags no-log)
  "Interactively add or remove tags to the current message."
  (interactive)
  (let* ((id (or id (jao-notmuch-gnus-message-id)))
         (current (unless tags (jao-notmuch-gnus-message-tags id)))
         (prompt (format "Change tags %s" (string-join current "/")))
         (tags (or tags (notmuch-read-tag-changes current prompt))))
    (notmuch-tag (concat "id:" id) tags)
    (unless no-log
      (message "%s -> %s" current (jao-notmuch-gnus-message-tags id)))))

(defun jao-notmuch-gnus-show-tags ()
  "Display in the echo area the tags of the current message."
  (interactive)
  (when-let (id (jao-notmuch-gnus-message-id))
    (message "%s" (string-join (jao-notmuch-gnus-message-tags id) " "))))

(defun jao-notmuch-gnus-toggle-tags (tags &optional id current)
  "Toggle the given TAGS list for the current Gnus message."
  (let* ((id (or id (jao-notmuch-gnus-message-id)))
         (current (or current (jao-notmuch-gnus-message-tags id)))
         (tags (mapcar (lambda (x)
                         (concat (if (member x current) "-" "+") x))
                       tags)))
    (notmuch-tag (concat "id:" id) tags)
    (message "New tags: %s" (jao-notmuch-gnus-message-tags id))))

(defun jao-notmuch-gnus-tag-mark ()
  "Remove the new tag for an article when it's marked as seen by Gnus."
  (when-let (id (jao-notmuch-gnus-message-id t))
    (jao-notmuch-gnus-tag-message id '("-new") t)))

(add-hook 'gnus-mark-article-hook #'jao-notmuch-gnus-tag-mark)

(defun jao-notmuch-gnus--group-tags (group)
  (when (string-match ".+:\\(.+\\)" group)
    (split-string (match-string 1 group) "\\.")))

(defun jao-notmuch-gnus-tag-on-move (op headers from to _d)
  (when-let* ((to-tags (when to (jao-notmuch-gnus--group-tags to)))
              (id (jao-notmuch-gnus--notmuch-id (mail-header-id headers))))
    (if (eq op 'delete)
        (let ((cur (seq-difference (jao-notmuch-gnus--group-tags from) to-tags)))
          (jao-notmuch-gnus-toggle-tags (append cur to-tags) id cur))
      (notmuch-tag (concat "id:" id)
                   (mapcar (lambda (x) (concat "+" x)) to-tags)))))

(add-hook 'gnus-summary-article-move-hook #'jao-notmuch-gnus-tag-on-move)
(add-hook 'gnus-summary-article-expire-hook #'jao-notmuch-gnus-tag-on-move)


;;;; Displaying search results in Gnus

(defvar jao-notmuch-gnus-server "nnml"
  "Name of the target Gnus server, e.g. nnml+mail.")

(defvar jao-notmuch-gnus-mail-directory (expand-file-name "~/.emacs.d/gnus/Mail")
  "Directory where Gnus stores its mail.")

(defvar jao-notmuch-gnus-leafnode-directory (expand-file-name "~/var/news")
  "Directory where leafnode stores its messages as seen by notmuch.")

(defun jao-notmuch-gnus-file-to-group (file &optional maildir newsdir)
  "Calculate the Gnus group name from the given file name.
Example:

  IN: /home/jao/var/mail/jao/foo/cur/1259184569.M4818P3384.localhost,W=6921:2,S
  OUT: nnml:jao.foo

  IN: /home/jao/.emacs.d/gnus/Mail/jao.trove/32, /home/jao/.emacs.d/gnus/Mail/
  OUT: nnml:jao.trove

  IN: /home/jao/var/mail/gmane/foo/bar/100
  OUT: nntp:gmane.foo.bar

  IN: /home/jao/var/mail/bigml/cur/1259176906.M17483P24679.localhost,W=2488:2,S
  OUT:nnimap:bigml/inbox"
  (let* ((maildir (or maildir jao-notmuch-gnus-mail-directory))
         (newsdir (or newsdir jao-notmuch-gnus-leafnode-directory))
         (g (directory-file-name (file-name-directory file)))
         (g (replace-regexp-in-string (file-name-as-directory maildir) "" g))
         (g (replace-regexp-in-string (file-name-as-directory newsdir) "" g))
         (nntp (string-match-p "^\\(gmane\\|gwene\\)/" g))
         (g (cond (nntp (concat "nntp:" g))
                  ((file-name-directory g)
                   (replace-regexp-in-string "^\\([^/]+\\)/"
                                             (concat jao-notmuch-gnus-server
                                                     ":\\1/")
                                             (file-name-directory g) t))
                  (t (concat jao-notmuch-gnus-server ":" g))))
         (g (replace-regexp-in-string "/" "." g))
         (g (replace-regexp-in-string "[/.]$" "" g)))
    (cond ((string-match ":$" g) (concat g "inbox"))
          (nntp g)
          (t (replace-regexp-in-string ":\\." ":" g)))))

(defun jao-notmuch-gnus-goto-message ()
  "Open a summary buffer containing the current notmuch article."
  (interactive)
  (let ((group (jao-notmuch-gnus-file-to-group (notmuch-show-get-filename)))
        (message-id (replace-regexp-in-string "^id:"
                                              ""
                                              (notmuch-show-get-message-id))))
    (if (and group message-id)
        (org-gnus-follow-link group message-id)
      (message "Couldn't get relevant infos for switching to Gnus."))))


;;;; Org links
(defun jao-notmuch-gnus--fname (id)
  (let ((cmd (format "notmuch search --output=files id:%s" id)))
    (car (split-string (shell-command-to-string cmd)))))

(defun jao-notmuch-gnus-org-follow (id)
  (when-let* ((fname (jao-notmuch-gnus--fname id))
              (group (jao-notmuch-gnus-file-to-group fname)))
    (org-gnus-follow-link group id)))

(defun jao-notmuch-gnus-org-store ()
  (when-let (d (or (when (derived-mode-p 'notmuch-show-mode 'notmuch-tree-mode)
                     (cons (notmuch-show-get-message-id)
                           (notmuch-show-get-subject)))
                    (when (derived-mode-p 'gnus-summary-mode 'gnus-article-mode)
                      (cons (jao-notmuch-gnus-message-id)
                            (gnus-summary-article-subject)))))
    (org-link-store-props :type "mail"
                          :link (concat "mail:" (car d))
                          :description (concat "Mail: " (cdr d)))))

(org-link-set-parameters "mail"
                         :follow #'jao-notmuch-gnus-org-follow
                         :store #'jao-notmuch-gnus-org-store)

(org-link-set-parameters "gnus" :store #'ignore)
(org-link-set-parameters "notmuch" :store #'ignore)


;;;; consult-notmuch

(with-eval-after-load "consult-notmuch"
  (defun jao-notmuch-gnus--open-candidate (candidate)
    "Open a notmuch-search completion candidate email in Gnus."
    (consult-notmuch--preview candidate nil)
    (with-current-buffer consult-notmuch--buffer-name
      (jao-notmuch-gnus-goto-message)
      (save-window-excursion (consult-notmuch--close-preview))))

  (defun jao-gnus-consult-notmuch ()
    "Run a consult-notmuch query that opens candidates in Gnus."
    (interactive)
    (jao-notmuch-gnus--open-candidate (consult-notmuch--search))))

(provide 'jao-notmuch-gnus)
;;; jao-notmuch-gnus.el ends here