Add 2012 to FSF copyright years for Emacs files
[bpt/emacs.git] / lisp / org / org-mac-message.el
CommitLineData
c8d0cf5c 1;;; org-mac-message.el --- Links to Apple Mail.app messages from within Org-mode
eb3a8c91 2
b73f1974 3;; Copyright (C) 2008-2012 Free Software Foundation, Inc.
eb3a8c91
GM
4
5;; Author: John Wiegley <johnw@gnu.org>
c8d0cf5c
CD
6;; Christopher Suckling <suckling at gmail dot com>
7
0fc0f178 8;; Keywords: outlines, hypermedia, calendar, wp
eb3a8c91 9
0fc0f178 10;; This file is part of GNU Emacs.
eb3a8c91 11
b1fc2b50 12;; GNU Emacs is free software: you can redistribute it and/or modify
0fc0f178 13;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
0fc0f178
CD
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b1fc2b50 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
eb3a8c91 24
20908596 25;;; Commentary:
c8d0cf5c 26;; This file implements links to Apple Mail.app messages from within Org-mode.
20908596
CD
27;; Org-mode does not load this module by default - if you would actually like
28;; this to happen then configure the variable `org-modules'.
29
c8d0cf5c
CD
30;; If you would like to create links to all flagged messages in an
31;; Apple Mail.app account, please customize the variable
32;; `org-mac-mail-account' and then call one of the following functions:
33
34;; (org-mac-message-insert-selected) copies a formatted list of links to
35;; the kill ring.
36
37;; (org-mac-message-insert-selected) inserts at point links to any
38;; messages selected in Mail.app.
39
40;; (org-mac-message-insert-flagged) searches within an org-mode buffer
86fbb8ca 41;; for a specific heading, creating it if it doesn't exist. Any
c8d0cf5c
CD
42;; message:// links within the first level of the heading are deleted
43;; and replaced with links to flagged messages.
44
eb3a8c91 45;;; Code:
0fc0f178
CD
46
47(require 'org)
48
c8d0cf5c
CD
49(defgroup org-mac-flagged-mail nil
50 "Options concerning linking to flagged Mail.app messages"
51 :tag "Org Mail.app"
52 :group 'org-link)
53
54(defcustom org-mac-mail-account "customize"
86fbb8ca 55 "The Mail.app account in which to search for flagged messages."
c8d0cf5c
CD
56 :group 'org-mac-flagged-mail
57 :type 'string)
58
0fc0f178
CD
59(org-add-link-type "message" 'org-mac-message-open)
60
46a44648 61;; In mac.c, removed in Emacs 23.
79e43d6e 62(declare-function do-applescript "org-mac-message" (script))
0fc0f178
CD
63(unless (fboundp 'do-applescript)
64 ;; Need to fake this using shell-command-to-string
65 (defun do-applescript (script)
66 (let (start cmd return)
67 (while (string-match "\n" script)
68 (setq script (replace-match "\r" t t script)))
69 (while (string-match "'" script start)
70 (setq start (+ 2 (match-beginning 0))
71 script (replace-match "\\'" t t script)))
72 (setq cmd (concat "osascript -e '" script "'"))
73 (setq return (shell-command-to-string cmd))
74 (concat "\"" (org-trim return) "\""))))
75
76(defun org-mac-message-open (message-id)
20908596
CD
77 "Visit the message with the given MESSAGE-ID.
78This will use the command `open' with the message URL."
0fc0f178
CD
79 (start-process (concat "open message:" message-id) nil
80 "open" (concat "message://<" (substring message-id 2) ">")))
81
c8d0cf5c 82(defun as-get-selected-mail ()
86fbb8ca 83 "AppleScript to create links to selected messages in Mail.app."
c8d0cf5c
CD
84 (do-applescript
85 (concat
86 "tell application \"Mail\"\n"
87 "set theLinkList to {}\n"
88 "set theSelection to selection\n"
89 "repeat with theMessage in theSelection\n"
90 "set theID to message id of theMessage\n"
91 "set theSubject to subject of theMessage\n"
92 "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
93 "copy theLink to end of theLinkList\n"
94 "end repeat\n"
95 "return theLinkList as string\n"
96 "end tell")))
97
98(defun as-get-flagged-mail ()
86fbb8ca 99 "AppleScript to create links to flagged messages in Mail.app."
c8d0cf5c
CD
100 (do-applescript
101 (concat
102 ;; Is Growl installed?
103 "tell application \"System Events\"\n"
104 "set growlHelpers to the name of every process whose creator type contains \"GRRR\"\n"
105 "if (count of growlHelpers) > 0 then\n"
106 "set growlHelperApp to item 1 of growlHelpers\n"
107 "else\n"
108 "set growlHelperApp to \"\"\n"
109 "end if\n"
110 "end tell\n"
111
112 ;; Get links
113 "tell application \"Mail\"\n"
114 "set theMailboxes to every mailbox of account \"" org-mac-mail-account "\"\n"
115 "set theLinkList to {}\n"
116 "repeat with aMailbox in theMailboxes\n"
117 "set theSelection to (every message in aMailbox whose flagged status = true)\n"
118 "repeat with theMessage in theSelection\n"
119 "set theID to message id of theMessage\n"
120 "set theSubject to subject of theMessage\n"
121 "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
122 "copy theLink to end of theLinkList\n"
123
124 ;; Report progress through Growl
125 ;; This "double tell" idiom is described in detail at
126 ;; http://macscripter.net/viewtopic.php?id=24570 The
127 ;; script compiler needs static knowledge of the
128 ;; growlHelperApp. Hmm, since we're compiling
129 ;; on-the-fly here, this is likely to be way less
130 ;; portable than I'd hoped. It'll work when the name
131 ;; is still "GrowlHelperApp", though.
132 "if growlHelperApp is not \"\" then\n"
133 "tell application \"GrowlHelperApp\"\n"
134 "tell application growlHelperApp\n"
135 "set the allNotificationsList to {\"FlaggedMail\"}\n"
136 "set the enabledNotificationsList to allNotificationsList\n"
137 "register as application \"FlaggedMail\" all notifications allNotificationsList default notifications enabledNotificationsList icon of application \"Mail\"\n"
138 "notify with name \"FlaggedMail\" title \"Importing flagged message\" description theSubject application name \"FlaggedMail\"\n"
139 "end tell\n"
140 "end tell\n"
141 "end if\n"
142 "end repeat\n"
143 "end repeat\n"
144 "return theLinkList as string\n"
145 "end tell")))
146
147(defun org-mac-message-get-links (&optional select-or-flag)
148 "Create links to the messages currently selected or flagged in Mail.app.
149This will use AppleScript to get the message-id and the subject of the
150messages in Mail.app and make a link out of it.
151When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
152the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
153The Org-syntax text will be pushed to the kill ring, and also returned."
154 (interactive "sLink to (s)elected or (f)lagged messages: ")
155 (setq select-or-flag (or select-or-flag "s"))
156 (message "AppleScript: searching mailboxes...")
157 (let* ((as-link-list
158 (if (string= select-or-flag "s")
159 (as-get-selected-mail)
160 (if (string= select-or-flag "f")
161 (as-get-flagged-mail)
162 (error "Please select \"s\" or \"f\""))))
163 (link-list
164 (mapcar
165 (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
166 (split-string as-link-list "[\r\n]+")))
167 split-link URL description orglink orglink-insert rtn orglink-list)
168 (while link-list
169 (setq split-link (split-string (pop link-list) "::split::"))
170 (setq URL (car split-link))
171 (setq description (cadr split-link))
172 (when (not (string= URL ""))
173 (setq orglink (org-make-link-string URL description))
174 (push orglink orglink-list)))
175 (setq rtn (mapconcat 'identity orglink-list "\n"))
176 (kill-new rtn)
177 rtn))
178
179(defun org-mac-message-insert-selected ()
180 "Insert a link to the messages currently selected in Mail.app.
86fbb8ca 181This will use AppleScript to get the message-id and the subject of the
c8d0cf5c 182active mail in Mail.app and make a link out of it."
0fc0f178 183 (interactive)
c8d0cf5c
CD
184 (insert (org-mac-message-get-links "s")))
185
186;; The following line is for backward compatibility
187(defalias 'org-mac-message-insert-link 'org-mac-message-insert-selected)
188
189(defun org-mac-message-insert-flagged (org-buffer org-heading)
190 "Asks for an org buffer and a heading within it, and replace message links.
191If heading exists, delete all message:// links within heading's first
192level. If heading doesn't exist, create it at point-max. Insert
193list of message:// links to flagged mail after heading."
194 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
81ad75af 195 (with-current-buffer org-buffer
c8d0cf5c
CD
196 (goto-char (point-min))
197 (let ((isearch-forward t)
198 (message-re "\\[\\[\\(message:\\)\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
199 (if (org-goto-local-search-headings org-heading nil t)
200 (if (not (eobp))
201 (progn
202 (save-excursion
203 (while (re-search-forward
204 message-re (save-excursion (outline-next-heading)) t)
205 (delete-region (match-beginning 0) (match-end 0)))
206 (insert "\n" (org-mac-message-get-links "f")))
207 (flush-lines "^$" (point) (outline-next-heading)))
208 (insert "\n" (org-mac-message-get-links "f")))
209 (goto-char (point-max))
210 (insert "\n")
ed21c5c8 211 (org-insert-heading nil t)
c8d0cf5c 212 (insert org-heading "\n" (org-mac-message-get-links "f"))))))
0fc0f178
CD
213
214(provide 'org-mac-message)
215
216;;; org-mac-message.el ends here