summaryrefslogtreecommitdiff
path: root/guile/examples/org2mu4e
blob: e7f10ad36c429e56a98d1c7d21f61bc1c6125e39 (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
#!/bin/sh
exec guile -e main -s $0 $@
!#

;;
;; Copyright (C) 2011-2012 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
;;
;; 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, 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, write to the Free Software Foundation,
;; Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

(use-modules (ice-9 getopt-long) (ice-9 format))
(use-modules (mu))

(define (display-org-header query)
  "Print the header for the org-file for QUERY."
  (format #t "* Messages matching '~a'\n\n" query))

(define (org-mu4e-link msg)
  "Create a link for this message understandable by org-mu4e."
  (let* ((subject ;; cleanup subject
	   (string-map
	     (lambda (kar)
	       (if (member kar '(#\] #\[)) #\space kar))
	     (or (mu:subject msg) "No subject"))))
    (format #f "[[mu4e:msgid:~a][~s]]"
      (mu:message-id msg) subject)))

(define (display-org-entry msg tag)
  "Write an org entry for MSG."
  (format #t "** ~a ~a\n\t~s\n\t~s\n"
    (org-mu4e-link msg)
    (if tag (string-concatenate `(":" ,tag "::")) "")
    (or (mu:from msg) "?")
    (let ((body (mu:body msg)))
      (if (not body) ;; get a 'summary' of the body text
	"<no plain-text body>"
	(string-map
	  (lambda (c)
	    (if (or (char=? c #\newline) (char=? c #\return))
		  #\space
		  c))
	  (substring body 0 (min (string-length body) 100)))))))

(define (main args)
  (let* ((optionspec   '( (muhome   (value #t))
			  (tag      (value #t))
			  (help    (single-char #\h) (value #f))))
	  (options (getopt-long args optionspec))
	  (msg (string-append
		 "usage: mu4e-org [--help] [--muhome=<muhome>] [--tag=<tag>] <query>"))
	  (help (option-ref options 'help #f))
	  (tag  (option-ref options 'tag #f))
	  (muhome (option-ref options 'muhome #f))
	  (query (string-concatenate (option-ref options '() '()))))
    (if help
      (begin (display msg) (exit 0))
      (begin
	(mu:initialize muhome)
	(display-org-header query)
	(mu:for-each-message
	    (lambda (msg) (display-org-entry msg tag))
	  query)))))

;; Local Variables:
;; mode: scheme
;; End: