Changed indentation to avoid a gcc warning.
[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 ;; I also use
26 ;;
27 ;; (define-key vm-summary-mode-map "\\" 'mymail/vm-visit-folder)
28 ;;
29 ;; So that pressing "\" in the summary start a search with mymail
30
31 (defgroup mymail ()
32   "Command to visit a folder built on-the-fly with mymail"
33   :version "0.9.10")
34
35 (defcustom mymail/default-additional-search-requests ""
36   "Comma-separated list of search requests to add to any search"
37   :type 'string
38   :group 'mymail)
39
40 (defcustom mymail/default-search-request nil
41   "Default request to use in place of the empty search"
42   :type 'string
43   :group 'mymail)
44
45 (defun mymail/vm-visit-folder (param)
46   "Read a comma-separated list of search requests for mymail,
47 create a temporary mbox with the resulting mails, and open it in
48 vm with `vm-visit-folder'.
49
50 If the request string is empty, use
51 `mymail/default-search-request' instead.
52
53 The string `mymail/default-additional-search-requests' is automatically
54 concatenated to the provided request, except if the request is
55 prefaced with `\\'."
56   (interactive
57    (list (read-string "mymail-vm: " nil 'mymail-vm-history)))
58
59   (if (string= param "")
60       (if mymail/default-search-request
61           (setq param mymail/default-search-request)
62         (error "mymail error: empty search")))
63
64   (let ((n 1)
65         (mbox-name nil)
66
67         (search-args
68          (apply 'nconc
69                 (mapcar
70                  (lambda (searche-request)
71                    (if (not (string= searche-request ""))
72                        (list "--search" searche-request)))
73                  (if (string= (substring param 0 1) "\\")
74                      (split-string (substring param 1 nil) ",")
75                    (split-string (concat param ","
76                                          mymail/default-additional-search-requests) ","))
77                  )))
78         )
79
80     (while (get-file-buffer (setq mbox-name (format "/tmp/mymail-vm-%d.mbox" n)))
81       (setq n (+ n 1)))
82
83     ;; (message (prin1-to-string
84               ;; (nconc (list "mymail"
85                            ;; "--output" mbox-name
86                            ;; "--default-search" "p"
87                            ;; "--nb-mails-max" "500")
88                      ;; search-args))
89              ;; )
90
91     (if (= (apply 'call-process
92                   (nconc (list "mymail" nil '(nil "/tmp/mymail.err") nil
93                                "--output" mbox-name
94                                "--default-search" "p"
95                                "--nb-mails-max" "500")
96                          search-args))
97            0)
98         (vm-visit-folder mbox-name t)
99
100       ;; (message "mymail failed. See /tmp/mymail.err"))
101
102       (message (with-temp-buffer
103                  (insert-file-contents "/tmp/mymail.err")
104                  (replace-string "\n" " ")
105                  (buffer-string)))
106       )
107     ))