zap until-condition docs
[emacs.git] / lisp / org / org-mac-message.el
blob5df68f56a05986b3a17c7631f734f885f4452f68
1 ;;; org-mac-message.el --- Links to Apple Mail.app messages from within Org-mode
3 ;; Copyright (C) 2008-2013 Free Software Foundation, Inc.
5 ;; Authors: John Wiegley <johnw@gnu.org>
6 ;; Christopher Suckling <suckling at gmail dot com>
8 ;; Keywords: outlines, hypermedia, calendar, wp
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
26 ;; This file implements links to Apple Mail.app messages from within Org-mode.
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'.
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:
34 ;; (org-mac-message-insert-selected) copies a formatted list of links to
35 ;; the kill ring.
37 ;; (org-mac-message-insert-selected) inserts at point links to any
38 ;; messages selected in Mail.app.
40 ;; (org-mac-message-insert-flagged) searches within an org-mode buffer
41 ;; for a specific heading, creating it if it doesn't exist. Any
42 ;; message:// links within the first level of the heading are deleted
43 ;; and replaced with links to flagged messages.
45 ;;; Code:
47 (require 'org)
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)
54 (defcustom org-mac-mail-account "customize"
55 "The Mail.app account in which to search for flagged messages."
56 :group 'org-mac-flagged-mail
57 :type 'string)
59 (org-add-link-type "message" 'org-mac-message-open)
61 ;; In mac.c, removed in Emacs 23.
62 (declare-function do-applescript "org-mac-message" (script))
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) "\""))))
76 (defun org-mac-message-open (message-id)
77 "Visit the message with the given MESSAGE-ID.
78 This will use the command `open' with the message URL."
79 (start-process (concat "open message:" message-id) nil
80 "open" (concat "message://<" (substring message-id 2) ">")))
82 (defun as-get-selected-mail ()
83 "AppleScript to create links to selected messages in Mail.app."
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")))
98 (defun as-get-flagged-mail ()
99 "AppleScript to create links to flagged messages in Mail.app."
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"
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"
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")))
147 (defun org-mac-message-get-links (&optional select-or-flag)
148 "Create links to the messages currently selected or flagged in Mail.app.
149 This will use AppleScript to get the message-id and the subject of the
150 messages in Mail.app and make a link out of it.
151 When SELECT-OR-FLAG is \"s\", get the selected messages (this is also
152 the default). When SELECT-OR-FLAG is \"f\", get the flagged messages.
153 The 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))
179 (defun org-mac-message-insert-selected ()
180 "Insert a link to the messages currently selected in Mail.app.
181 This will use AppleScript to get the message-id and the subject of the
182 active mail in Mail.app and make a link out of it."
183 (interactive)
184 (insert (org-mac-message-get-links "s")))
186 ;; The following line is for backward compatibility
187 (defalias 'org-mac-message-insert-link 'org-mac-message-insert-selected)
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.
191 If heading exists, delete all message:// links within heading's first
192 level. If heading doesn't exist, create it at point-max. Insert
193 list of message:// links to flagged mail after heading."
194 (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
195 (with-current-buffer org-buffer
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")
211 (org-insert-heading nil t)
212 (insert org-heading "\n" (org-mac-message-get-links "f"))))))
214 (provide 'org-mac-message)
216 ;;; org-mac-message.el ends here