org-mac-message.el 8.8 KB

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