aboutsummaryrefslogtreecommitdiff
path: root/.emacs.local.d/modes/notmuch.el
blob: f4ed1277885b067406da2a04b893da1e3e713b15 (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
;; --------
;; notmuch mode
;; --------
(require 'notmuch)

;; This should be upstream
(require 'notmuch-show)
(require 'notmuch-tag)
(defun notmuch-tree-show-message-in ()
  "Show the current message (in split-pane)."
  (interactive)
  (let ((id (notmuch-tree-get-message-id))
        (inhibit-read-only t)
        buffer)
    (when id
      ;; We close and reopen the window to kill off un-needed buffers
      ;; this might cause flickering but seems ok.
      (notmuch-tree-close-message-window)
      (setq notmuch-tree-message-window
            (split-window-vertically (/ (window-height) 4)))
      (with-selected-window notmuch-tree-message-window
        ;; Since we are only displaying one message do not indent.
        (let ((notmuch-show-indent-messages-width 0)
              (notmuch-show-only-matching-messages t))
          (setq buffer (notmuch-show id))))
      ;; We need the `let' as notmuch-tree-message-window is buffer local.
      (let ((window notmuch-tree-message-window))
        (with-current-buffer buffer
          (setq notmuch-tree-message-window window)
          (add-hook 'kill-buffer-hook 'notmuch-tree-message-window-kill-hook)))
      (when notmuch-show-mark-read-tags
        (notmuch-tree-tag-update-display notmuch-show-mark-read-tags)
        (notmuch-tree-tag notmuch-show-mark-read-tags))
      (setq notmuch-tree-message-buffer buffer))))
;; End upstream

;; Keymaps
(defun rul/capture-mail()
  "Capture mail to org mode."
  (interactive)
  (org-store-link nil)
  (org-capture nil "m")
  )

(bind-key "c" 'rul/capture-mail notmuch-show-mode-map)

(define-key notmuch-show-mode-map "R" 'notmuch-show-reply)
(define-key notmuch-search-mode-map "R" 'notmuch-search-reply-to-thread)

; Spam
(define-key notmuch-show-mode-map "S"
  (lambda ()
    "mark message as spam"
    (interactive)
    (notmuch-show-tag (list "+spam" "-inbox" "-unread"))))

(define-key notmuch-search-mode-map "S"
  (lambda (&optional beg end)
    "mark thread as spam"
    (interactive (notmuch-search-interactive-region))
    (notmuch-search-tag (list "+spam" "-inbox" "-unread") beg end)))

; Archive
(define-key notmuch-show-mode-map "A"
  (lambda ()
    "archive"
    (interactive)
    (notmuch-show-tag (list "+archive" "-inbox" "-unread"))
    (notmuch-refresh-this-buffer)))

(define-key notmuch-search-mode-map "A"
  (lambda (&optional beg end)
    "mark thread as spam"
    (interactive (notmuch-search-interactive-region))
    (notmuch-search-tag (list "+archive" "-inbox" "-unread") beg end)
    (notmuch-refresh-this-buffer)))

; Mark as read
(define-key notmuch-search-mode-map "r"
  (lambda (&optional beg end)
    "mark thread as read"
    (interactive (notmuch-search-interactive-region))
    (notmuch-search-tag (list "-unread") beg end)
    (notmuch-search-next-thread)))

(define-key notmuch-search-mode-map (kbd "RET")
  (lambda ()
    "Show the selected thread with notmuch-tree if it has more
than one email. Use notmuch-show otherwise."
    (interactive)
    (if (= (plist-get (notmuch-search-get-result) :total) 1)
        (notmuch-search-show-thread)
      (notmuch-tree (notmuch-search-find-thread-id)
                    notmuch-search-query-string
                    nil
                    (notmuch-prettify-subject (notmuch-search-find-subject))))))

(defun color-inbox-if-unread () (interactive)
       (save-excursion
         (goto-char (point-min))
         (let ((cnt (car (process-lines "notmuch" "count" "tag:inbox and tag:unread"))))
           (when (> (string-to-number cnt) 0)
             (save-excursion
               (when (search-forward "inbox" (point-max) t)
                 (let* ((overlays (overlays-in (match-beginning 0) (match-end 0)))
                        (overlay (car overlays)))
                   (when overlay
                     (overlay-put overlay 'face '((:inherit bold) (:foreground "green")))))))))))

(defvar notmuch-hello-refresh-count 0)
(defun notmuch-hello-refresh-status-message ()
  (let* ((new-count
          (string-to-number
           (car (process-lines notmuch-command "count"))))
         (diff-count (- new-count notmuch-hello-refresh-count)))
    (cond
     ((= notmuch-hello-refresh-count 0)
      (message "You have %s messages."
               (notmuch-hello-nice-number new-count)))
     ((> diff-count 0)
      (message "You have %s more messages since last refresh."
               (notmuch-hello-nice-number diff-count)))
     ((< diff-count 0)
      (message "You have %s fewer messages since last refresh."
               (notmuch-hello-nice-number (- diff-count)))))
    (setq notmuch-hello-refresh-count new-count)))

(add-hook 'notmuch-hello-refresh-hook 'color-inbox-if-unread)
(add-hook 'notmuch-hello-refresh-hook 'notmuch-hello-refresh-status-message)
nihil fit ex nihilo