org-mac-message.el 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. ;;; org-mac-message.el --- Links to Apple Mail 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.25c
  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 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 account, please customize the variable
  24. ;; org-mac-mail-account and then call one of the following functions:
  25. ;; (org-mac-create-flagged-mail) copies a formatted list of links to
  26. ;; the kill ring.
  27. ;; (org-mac-insert-flagged-mail) searches within an org-mode buffer
  28. ;; for a specific heading, creating it if it doesn't exist. Any
  29. ;; message:// links within the first level of the heading are deleted
  30. ;; and replaced with links to flagged messages.
  31. ;; If you have Growl installed and would like more visual feedback
  32. ;; whilst AppleScript searches for messages, please uncomment lines
  33. ;; 114 to 119.
  34. ;;; Code:
  35. (require 'org)
  36. (defgroup org-mac-flagged-mail nil
  37. "Options concerning linking to flagged Mail.app messages"
  38. :tag "Org Mail.app"
  39. :group 'org-link)
  40. (defcustom org-mac-mail-account "customize"
  41. "The Mail.app account in which to search for flagged messages"
  42. :group 'org-mac-flagged-mail
  43. :type 'string)
  44. (org-add-link-type "message" 'org-mac-message-open)
  45. ;; In mac.c, removed in Emacs 23.
  46. (declare-function do-applescript "org-mac-message" (script))
  47. (unless (fboundp 'do-applescript)
  48. ;; Need to fake this using shell-command-to-string
  49. (defun do-applescript (script)
  50. (let (start cmd return)
  51. (while (string-match "\n" script)
  52. (setq script (replace-match "\r" t t script)))
  53. (while (string-match "'" script start)
  54. (setq start (+ 2 (match-beginning 0))
  55. script (replace-match "\\'" t t script)))
  56. (setq cmd (concat "osascript -e '" script "'"))
  57. (setq return (shell-command-to-string cmd))
  58. (concat "\"" (org-trim return) "\""))))
  59. (defun org-mac-message-open (message-id)
  60. "Visit the message with the given MESSAGE-ID.
  61. This will use the command `open' with the message URL."
  62. (start-process (concat "open message:" message-id) nil
  63. "open" (concat "message://<" (substring message-id 2) ">")))
  64. (defun as-get-selected-mail ()
  65. "AppleScript to create links to selected messages in Mail.app"
  66. (do-applescript
  67. (concat
  68. "tell application \"Mail\"\n"
  69. "set theLinkList to {}\n"
  70. "set theSelection to selection\n"
  71. "repeat with theMessage in theSelection\n"
  72. "set theID to message id of theMessage\n"
  73. "set theSubject to subject of theMessage\n"
  74. "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
  75. "copy theLink to end of theLinkList\n"
  76. "end repeat\n"
  77. "return theLinkList as string\n"
  78. "end tell")))
  79. (defun as-get-flagged-mail ()
  80. "AppleScript to create links to flagged messages in Mail.app"
  81. (do-applescript
  82. (concat
  83. "tell application \"Mail\"\n"
  84. "set theMailboxes to every mailbox of account \"" org-mac-mail-account "\"\n"
  85. "set theLinkList to {}\n"
  86. "repeat with aMailbox in theMailboxes\n"
  87. "set theSelection to (every message in aMailbox whose flagged status = true)\n"
  88. "repeat with theMessage in theSelection\n"
  89. "set theID to message id of theMessage\n"
  90. "set theSubject to subject of theMessage\n"
  91. "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
  92. "copy theLink to end of theLinkList\n"
  93. ;; "tell application \"GrowlHelperApp\"\n"
  94. ;; "set the allNotificationsList to {\"FlaggedMail\"}\n"
  95. ;; "set the enabledNotificationsList to allNotificationsList\n"
  96. ;; "register as application \"FlaggedMail\" all notifications allNotificationsList default notifications enabledNotificationsList icon of application \"Mail\"\n"
  97. ;; "notify with name \"FlaggedMail\" title \"Importing flagged message\" description theSubject application name \"FlaggedMail\"\n"
  98. ;; "end tell\n"
  99. "end repeat\n"
  100. "end repeat\n"
  101. "return theLinkList as string\n"
  102. "end tell")))
  103. (defun org-mac-message-get-links (select-or-flag)
  104. "Create links to the messages currently selected or flagged in
  105. Mail.app. This will use AppleScript to get the message-id and
  106. the subject of the message in Mail.app and make a link out
  107. of it."
  108. (interactive "sLink to (s)elected or (f)lagged messages: ")
  109. (message "AppleScript: searching mailboxes...")
  110. (let* ((as-link-list
  111. (if (string= select-or-flag "s")
  112. (as-get-selected-mail)
  113. (if (string= select-or-flag "f")
  114. (as-get-flagged-mail)
  115. (error "Please select \"s\" or \"f\""))))
  116. (link-list
  117. (mapcar
  118. (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
  119. (split-string as-link-list "[\r\n]+")))
  120. split-link
  121. URL
  122. description
  123. orglink
  124. orglink-insert
  125. (orglink-list nil))
  126. (while link-list
  127. (setq split-link (split-string (pop link-list) "::split::"))
  128. (setq URL (car split-link))
  129. (setq description (cadr split-link))
  130. (when (not (string= URL ""))
  131. (setq orglink (org-make-link-string URL description))
  132. (push orglink orglink-list)))
  133. (with-temp-buffer
  134. (while orglink-list
  135. (insert (concat (pop orglink-list)) "\n"))
  136. (kill-region (point-min) (point-max))
  137. (current-kill 0)))
  138. (message "Messages copied to kill-ring"))
  139. (defun org-mac-message-insert-selected ()
  140. "Insert a link to the messages currently selected in Apple Mail.
  141. This will use applescript to get the message-id and the subject of the
  142. active mail in AppleMail and make a link out of it."
  143. (interactive)
  144. (org-mac-message-get-links "s")
  145. (yank))
  146. ;; The following line is for backward compatibility
  147. (defalias 'org-mac-message-insert-link 'org-mac-message-insert-selected)
  148. (defun org-mac-message-insert-flagged (org-buffer org-heading)
  149. "Asks for an org buffer and a heading within it. If heading
  150. exists, delete all message:// links within heading's first
  151. level. If heading doesn't exist, create it at point-max. Insert
  152. list of message:// links to flagged mail after heading."
  153. (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
  154. (save-excursion
  155. (set-buffer org-buffer)
  156. (goto-char (point-min))
  157. (let ((isearch-forward t)
  158. (message-re "\\[\\[\\(message:\\)?\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"))
  159. (if (org-goto-local-search-headings org-heading nil t)
  160. (if (not (eobp))
  161. (progn
  162. (save-excursion
  163. (while (re-search-forward message-re (save-excursion (outline-next-heading)) t)
  164. (delete-region (match-beginning 0) (match-end 0)))
  165. (org-mac-message-get-links "f")
  166. (yank))
  167. (flush-lines "^$" (point) (outline-next-heading)))
  168. (insert "\n")
  169. (org-mac-message-get-links "f")
  170. (yank))
  171. (goto-char (point-max))
  172. (insert "\n")
  173. (org-insert-heading)
  174. (insert (concat org-heading "\n"))
  175. (org-mac-message-get-links "f")
  176. (yank)))))
  177. (provide 'org-mac-message)
  178. ;; arch-tag: 3806d0c1-abe1-4db6-9c31-f3ed7d4a9b32
  179. ;;; org-mac-message.el ends here