summaryrefslogtreecommitdiff
path: root/lisp/km-mail.el
blob: 772b3a432e9a8778c5b8ee4eaf38096da24a3d14 (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
;;; km-mail.el --- Mail-related extensions

;; Copyright (C) 2012-2016 Kyle Meyer <kyle@kyleam.com>

;; Author: Kyle Meyer <kyle@kyleam.com>
;; URL: https://github.com/kyleam/emacs.d

;; 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 <http://www.gnu.org/licenses/>.

;;; Code:

(require 'compile)
(require 'notmuch)

(require 'km-util)

;;;###autoload
(defun km/notmuch-show-open-github-patch ()
  "Open patch from GitHub email."
  (interactive)
  (with-current-notmuch-show-message
   (km/open-github-patch (current-buffer))))

;;;###autoload
(defun km/notmuch-show-pipe-message-to-project (project)
  (interactive
   (list (expand-file-name
          (completing-read "Project: "
                           (projectile-relevant-known-projects)))))
  (let ((default-directory project))
    (call-interactively #'notmuch-show-pipe-message)))

;;;###autoload
(defun km/notmuch-show-pipe-part-to-project (project)
  (interactive
   (list (expand-file-name
          (completing-read "Project: "
                           (projectile-relevant-known-projects)))))
  (let ((default-directory project))
    (call-interactively #'notmuch-show-pipe-part)))

;;;###autoload
(defun km/notmuch-archive-all ()
  "Call `notmuch-search-archive-thread' with whole-buffer region."
  (interactive)
  (mark-whole-buffer)
  (call-interactively #'notmuch-search-archive-thread))

;;;###autoload
(defun km/notmuch-tree-from-show-current-query (&optional ignore-context)
  (interactive "P")
  (let ((notmuch-show-query-context (and (not ignore-context)
                                         notmuch-show-query-context)))
    (call-interactively #'notmuch-tree-from-show-current-query)))

;;;###autoload
(defun km/notmuch-show-at-point ()
  "Call `notmuch-show' with message or thread ID at point."
  (interactive)
  (let ((id (save-excursion
              (skip-syntax-backward "^\\s-")
              (and (looking-at
                    (rx (group (zero-or-one (or "id:" "thread:")))
                        (one-or-more (any "-" "_" "." "@" "/" alnum))))
                   (concat (and (string= (match-string 1) "") "id:")
                           (match-string-no-properties 0))))))
    (if id
        (notmuch-show id)
      (call-interactively #'notmuch-show))))


;;; Mail sync

(defvar mail-sync-log-file "/var/log/mail-sync/mail-sync")

(defun mail-sync-log-to-file (buf _)
  (with-temp-buffer
    (insert "\n")
    (insert (with-current-buffer buf (buffer-string)))
    (write-region nil nil mail-sync-log-file 'append 'no-msg)))

(defvar mail-sync-calling-buffer nil)
(defun mail-sync-refresh-caller (_ exit)
  (when (equal exit "finished\n")
    (when (and mail-sync-calling-buffer
               (buffer-live-p mail-sync-calling-buffer))
      (with-current-buffer mail-sync-calling-buffer
        (notmuch-refresh-this-buffer))))
  (setq mail-sync-calling-buffer nil))

;;;###autoload
(define-compilation-mode mail-sync-mode "Mail-sync"
  "Sync mail, logging output to *mail-sync-log*."
  (set (make-local-variable 'compilation-finish-functions)
       '(mail-sync-log-to-file mail-sync-refresh-caller)))

;;;###autoload
(defun km/notmuch-sync-mail (&optional cmd-append)
  (interactive (list (and current-prefix-arg
                          (read-string "sync-mail args: "))))
  (setq mail-sync-calling-buffer (current-buffer))
  (let ((default-directory (expand-file-name "~/"))
        (display-buffer-overriding-action
         '(display-buffer-below-selected)))
    (compilation-start (concat "sync-mail"
                               (and cmd-append " ")
                               cmd-append)
                       'mail-sync-mode)))

;;;###autoload
(defun km/notmuch-sync-mail-fast ()
  (interactive)
  (km/notmuch-sync-mail "--fast"))

(provide 'km-mail)
;;; km-mail.el ends here