Merge from emacs--rel--22
[bpt/emacs.git] / lisp / org / org-mac-message.el
1 ;;; org-mac-message.el --- Support for links to Apple Mail messages from within Org-mode
2
3 ;; Copyright (C) 2008 Free Software Foundation, Inc.
4
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Version: 6.02b
7 ;; Keywords: outlines, hypermedia, calendar, wp
8
9 ;; This file is part of GNU Emacs.
10
11 ;; Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27 ;; This file implements links to Apple Mail messages from within Org-mode.
28 ;; Org-mode does not load this module by default - if you would actually like
29 ;; this to happen then configure the variable `org-modules'.
30
31 ;;; Code:
32
33 (require 'org)
34
35 (org-add-link-type "message" 'org-mac-message-open)
36
37 (declare-function do-applescript "mac.c" (string))
38 (unless (fboundp 'do-applescript)
39 ;; Need to fake this using shell-command-to-string
40 (defun do-applescript (script)
41 (let (start cmd return)
42 (while (string-match "\n" script)
43 (setq script (replace-match "\r" t t script)))
44 (while (string-match "'" script start)
45 (setq start (+ 2 (match-beginning 0))
46 script (replace-match "\\'" t t script)))
47 (setq cmd (concat "osascript -e '" script "'"))
48 (setq return (shell-command-to-string cmd))
49 (concat "\"" (org-trim return) "\""))))
50
51 (defun org-mac-message-open (message-id)
52 "Visit the message with the given MESSAGE-ID.
53 This will use the command `open' with the message URL."
54 (start-process (concat "open message:" message-id) nil
55 "open" (concat "message://<" (substring message-id 2) ">")))
56
57 (defun org-mac-message-insert-link ()
58 "Insert a link to the messages currently selected in Apple Mail.
59 This will use applescript to get the message-id and the subject of the
60 active mail in AppleMail and make a link out of it."
61 (interactive)
62 (insert (org-mac-message-get-link)))
63
64 (defun org-mac-message-get-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 (let ((subject (do-applescript "tell application \"Mail\"
69 set theMessages to selection
70 subject of beginning of theMessages
71 end tell"))
72 (message-id (do-applescript "tell application \"Mail\"
73 set theMessages to selection
74 message id of beginning of theMessages
75 end tell")))
76 (org-make-link-string
77 (concat "message://"
78 (substring message-id 1 (1- (length message-id))))
79 (substring subject 1 (1- (length subject))))))
80
81 (provide 'org-mac-message)
82
83 ;; arch-tag: 3806d0c1-abe1-4db6-9c31-f3ed7d4a9b32
84 ;;; org-mac-message.el ends here