org-mac-message.el 7.2 KB

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