Cosmetics.
[mymail.git] / mymail-vm.el
1
2 ;; Copyright (c) 2013 Francois Fleuret
3 ;; Written by Francois Fleuret <francois@fleuret.org>
4 ;;
5 ;; This file is part of mymail.
6 ;;
7 ;; mymail is free software: you can redistribute it and/or modify it
8 ;; under the terms of the GNU General Public License version 3 as
9 ;; published by the Free Software Foundation.
10 ;;
11 ;; mymail is distributed in the hope that it will be useful, but
12 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ;; General Public License for more details.
15 ;;
16 ;; You should have received a copy of the GNU General Public License
17 ;; along with mymail.  If not, see <http://www.gnu.org/licenses/>.
18
19 ;; You may want to add
20 ;;
21 ;; (add-to-list 'recentf-exclude "/tmp/mymail-vm-.*\.mbox")
22 ;;
23 ;; to your .emacs
24
25 (defgroup mymail ()
26   "Command to visit a folder build on-the-fly with mymail"
27   :version "0.9.5")
28
29 (defcustom mymail/default-additional-search-requests ""
30   "Comma-separated list of search requests to add to any search"
31   :type 'string
32   :group 'mymail)
33
34 (defcustom mymail/default-search-request nil
35   "Default request to use in place of the empty search"
36   :type 'string
37   :group 'mymail)
38
39 (defun mymail/vm-visit-folder (param)
40   "Read a comma-separated list of search requests for mymail,
41 create a temporary mbox with the resulting mails, and open it in
42 vm with `vm-visit-folder'.
43
44 If the request string is empty, use
45 `mymail/default-search-request' instead.
46
47 The string `mymail/default-additional-search-requests' is automatically
48 concatenated to the provided request, except if the request is
49 prefaced with \\."
50   (interactive
51    (list (read-string "mymail-vm: " nil 'mymail-vm-history)))
52
53   (if (string= param "")
54       (if mymail/default-search-request
55           (setq param mymail/default-search-request)
56         (error "mymail error: empty search")))
57
58   (let ((n 1)
59         (mbox-name nil)
60
61         (search-args
62          (apply 'nconc
63                 (mapcar
64                  (lambda (searche-request)
65                    (if (not (string= searche-request ""))
66                        (list "--search" searche-request)))
67                  (if (string= (substring param 0 1) "\\")
68                      (split-string (substring param 1 nil) ",")
69                    (split-string (concat param ","
70                                          mymail/default-additional-search-requests) ","))
71                  )))
72         )
73
74     (while (get-file-buffer (setq mbox-name (format "/tmp/mymail-vm-%d.mbox" n)))
75       (setq n (+ n 1)))
76
77     (if (= (apply 'call-process
78                   (nconc (list "mymail" nil nil nil
79                                "--output" mbox-name
80                                "--default-search" "p")
81                          search-args))
82            0)
83         (vm-visit-folder mbox-name t)
84       (message "mymail failed"))
85     ))