summaryrefslogtreecommitdiffhomepage
path: root/attic/elisp/jao-notmuch-move.el
blob: d4626bf11d7ef7af18f4a9649e265c97efe7f13c (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
;;; jao-notmuch-move.el --- Move messages around in notmuch  -*- lexical-binding: t; -*-

;; Copyright (C) 2021, 2024  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:

;; Moving messages around

;;; Code:

(require 'notmuch)

(defvar jao-notmuch-mailboxes nil)
(defvar jao-notmuch-mailboxes-rx nil)

(defun jao-notmuch--path-to-mailbox (full-path)
  (unless jao-notmuch-mailboxes-rx
    (setq jao-notmuch-mailboxes-rx (regexp-opt jao-notmuch-mailboxes)))
  (if (string-match jao-notmuch-mailboxes-rx full-path)
      (match-string 0 full-path)
    (user-error "Message not in any registered mailbox!")))

(defun jao-notmuch--msg-props ()
  (if-let* ((p (save-excursion
                 (beginning-of-line)
                 (text-property-search-forward :notmuch-message-properties))))
      (prop-match-value p)
    (user-error "No message at point")))

(defun jao-notmuch--full-path ()
  (seq-find #'file-exists-p (plist-get (jao-notmuch--msg-props) :filename)))

(defun jao-notmuch--move (&optional full-path d)
  (let* ((full-path (or full-path (jao-notmuch--full-path)))
         (ff (jao-notmuch--path-to-mailbox full-path))
         (d (or d (completing-read (format "From %s to: " ff)
                                   (remove ff jao-notmuch-mailboxes) nil t)))
         (dest (string-replace ff d full-path))
         (dest (replace-regexp-in-string ",U=.+$" "m:2,S" dest))
         (ftags (split-string ff "/"))
         (ttags (split-string d "/")))
    (when (y-or-n-p (format "%s -> %s? " ftags ttags))
      (notmuch-tree-close-message-window)
      (notmuch-tree-tag (append (notmuch-tag-change-list ftags t)
                                (notmuch-tag-change-list ttags)))
      (rename-file (jao-notmuch--full-path) dest)
      (shell-command-to-string "notmuch new")
      (notmuch-refresh-this-buffer))))

(defun jao-notmuch-move-message ()
  "Move message at point to another folder."
  (interactive)
  (jao-notmuch--move))




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