org-mac-message.el 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ;;; org-mac-message.el --- Support for 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.25b
  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. ;; 125 to 130.
  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 org-mac-message-insert-link ()
  65. "Insert a link to the messages currently selected in Apple Mail.
  66. This will use applescript to get the message-id and the subject of the
  67. active mail in AppleMail and make a link out of it."
  68. (interactive)
  69. (org-mac-message-get-link)
  70. (yank))
  71. (defun org-mac-message-get-link ()
  72. "Insert a link to the messages currently selected in Apple Mail.
  73. This will use applescript to get the message-id and the subject of the
  74. active mail in AppleMail and make a link out of it."
  75. (let* ((as-link-list
  76. (do-applescript
  77. (concat
  78. "tell application \"Mail\"\n"
  79. "set theLinkList to {}\n"
  80. "set theSelection to selection\n"
  81. "repeat with theMessage in theSelection\n"
  82. "set theID to message id of theMessage\n"
  83. "set theSubject to subject of theMessage\n"
  84. "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
  85. "copy theLink to end of theLinkList\n"
  86. "end repeat\n"
  87. "return theLinkList as string\n"
  88. "end tell")))
  89. (link-list
  90. (mapcar
  91. (lambda (x) (if (string-match "\\`\"\\(.*\\)\"\\'" x) (setq x (match-string 1 x))) x)
  92. (split-string as-link-list "[\r\n]+")))
  93. split-link
  94. URL
  95. description
  96. orglink
  97. orglink-insert
  98. (orglink-list nil))
  99. (while link-list
  100. (progn
  101. (setq split-link (split-string (pop link-list) "::split::"))
  102. (setq URL (car split-link))
  103. (setq description (cadr split-link))
  104. (if (not (string= URL ""))
  105. (progn
  106. (setq orglink (org-make-link-string URL description))
  107. (push orglink orglink-list)))))
  108. (with-temp-buffer
  109. (while orglink-list
  110. (insert (concat (pop orglink-list)) "\n"))
  111. (kill-region (point-min) (point-max))
  112. (current-kill 0))))
  113. (defun org-mac-create-flagged-mail ()
  114. "Create links to flagged messages in a Mail.app account and
  115. copy them to the kill ring"
  116. (interactive)
  117. (message "AppleScript: searching mailboxes...")
  118. (let* ((as-link-list
  119. (do-applescript
  120. (concat
  121. "tell application \"Mail\"\n"
  122. "set theMailboxes to every mailbox of account \"" org-mac-mail-account "\"\n"
  123. "set theLinkList to {}\n"
  124. "repeat with aMailbox in theMailboxes\n"
  125. "set theSelection to (every message in aMailbox whose flagged status = true)\n"
  126. "repeat with theMessage in theSelection\n"
  127. "set theID to message id of theMessage\n"
  128. "set theSubject to subject of theMessage\n"
  129. "set theLink to \"message://\" & theID & \"::split::\" & theSubject & \"\n\"\n"
  130. "copy theLink to end of theLinkList\n"
  131. ;; "tell application \"GrowlHelperApp\"\n"
  132. ;; "set the allNotificationsList to {\"FlaggedMail\"}\n"
  133. ;; "set the enabledNotificationsList to allNotificationsList\n"
  134. ;; "register as application \"FlaggedMail\" all notifications allNotificationsList default notifications enabledNotificationsList icon of application \"Mail\"\n"
  135. ;; "notify with name \"FlaggedMail\" title \"Importing flagged message\" description theSubject application name \"FlaggedMail\"\n"
  136. ;; "end tell\n"
  137. "end repeat\n"
  138. "end repeat\n"
  139. "return theLinkList as string\n"
  140. "end tell")))
  141. (link-list (split-string as-link-list "\n"))
  142. split-link
  143. URL
  144. description
  145. orglink
  146. (orglink-list nil))
  147. (while link-list
  148. (progn
  149. (setq split-link (split-string (pop link-list) "::split::"))
  150. (setq URL (car split-link))
  151. (setq description (cadr split-link))
  152. (if (not (string= URL ""))
  153. (progn
  154. (setq orglink (org-make-link-string URL description))
  155. (push orglink orglink-list)))))
  156. (with-temp-buffer
  157. (while orglink-list
  158. (insert (concat (pop orglink-list)) "\n"))
  159. (kill-region (point-min) (point-max))
  160. (message "Flagged messages copied to kill ring"))))
  161. (defun org-mac-insert-flagged-mail (org-buffer org-heading)
  162. "Asks for an org buffer and a heading within it. If heading
  163. exists, delete all message:// links within heading's first
  164. level. If heading doesn't exist, create it at point-max. Insert
  165. list of message:// links to flagged mail after heading."
  166. (interactive "bBuffer in which to insert links: \nsHeading after which to insert links: ")
  167. (save-excursion
  168. (set-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 message-re (save-excursion (outline-next-heading)) t)
  177. (delete-region (match-beginning 0) (match-end 0)))
  178. (org-mac-create-flagged-mail)
  179. (yank))
  180. (flush-lines "^$" (point) (outline-next-heading)))
  181. (insert "\n")
  182. (org-mac-create-flagged-mail)
  183. (yank))
  184. (goto-char (point-max))
  185. (insert "\n")
  186. (org-insert-heading)
  187. (insert (concat org-heading "\n"))
  188. (org-mac-create-flagged-mail)
  189. (yank)))))
  190. (provide 'org-mac-message)
  191. ;; arch-tag: 3806d0c1-abe1-4db6-9c31-f3ed7d4a9b32
  192. ;;; org-mac-message.el ends here