org-mac-message.el 8.8 KB

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