2008-11-23 Carsten Dominik <carsten.dominik@gmail.com>
[bpt/emacs.git] / lisp / org / org-mac-message.el
CommitLineData
20908596 1;;; org-mac-message.el --- Support for links to Apple Mail messages from within Org-mode
eb3a8c91 2
0fc0f178 3;; Copyright (C) 2008 Free Software Foundation, Inc.
eb3a8c91
GM
4
5;; Author: John Wiegley <johnw@gnu.org>
ce4fdcb9 6;; Version: 6.13
0fc0f178 7;; Keywords: outlines, hypermedia, calendar, wp
eb3a8c91 8
0fc0f178 9;; This file is part of GNU Emacs.
eb3a8c91 10
b1fc2b50 11;; GNU Emacs is free software: you can redistribute it and/or modify
0fc0f178 12;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
0fc0f178
CD
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
b1fc2b50 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
eb3a8c91 23
20908596
CD
24;;; Commentary:
25;; This file implements links to Apple Mail messages from within Org-mode.
26;; Org-mode does not load this module by default - if you would actually like
27;; this to happen then configure the variable `org-modules'.
28
eb3a8c91 29;;; Code:
0fc0f178
CD
30
31(require 'org)
32
33(org-add-link-type "message" 'org-mac-message-open)
34
46a44648 35;; In mac.c, removed in Emacs 23.
79e43d6e 36(declare-function do-applescript "org-mac-message" (script))
0fc0f178
CD
37(unless (fboundp 'do-applescript)
38 ;; Need to fake this using shell-command-to-string
39 (defun do-applescript (script)
40 (let (start cmd return)
41 (while (string-match "\n" script)
42 (setq script (replace-match "\r" t t script)))
43 (while (string-match "'" script start)
44 (setq start (+ 2 (match-beginning 0))
45 script (replace-match "\\'" t t script)))
46 (setq cmd (concat "osascript -e '" script "'"))
47 (setq return (shell-command-to-string cmd))
48 (concat "\"" (org-trim return) "\""))))
49
50(defun org-mac-message-open (message-id)
20908596
CD
51 "Visit the message with the given MESSAGE-ID.
52This will use the command `open' with the message URL."
0fc0f178
CD
53 (start-process (concat "open message:" message-id) nil
54 "open" (concat "message://<" (substring message-id 2) ">")))
55
56(defun org-mac-message-insert-link ()
57 "Insert a link to the messages currently selected in Apple Mail.
58This will use applescript to get the message-id and the subject of the
59active mail in AppleMail and make a link out of it."
60 (interactive)
61 (insert (org-mac-message-get-link)))
62
63(defun org-mac-message-get-link ()
64 "Insert a link to the messages currently selected in Apple Mail.
65This will use applescript to get the message-id and the subject of the
66active mail in AppleMail and make a link out of it."
67 (let ((subject (do-applescript "tell application \"Mail\"
68 set theMessages to selection
69 subject of beginning of theMessages
70end tell"))
71 (message-id (do-applescript "tell application \"Mail\"
72 set theMessages to selection
73 message id of beginning of theMessages
74end tell")))
75 (org-make-link-string
76 (concat "message://"
77 (substring message-id 1 (1- (length message-id))))
78 (substring subject 1 (1- (length subject))))))
79
80(provide 'org-mac-message)
81
b7b8d8ed 82;; arch-tag: 3806d0c1-abe1-4db6-9c31-f3ed7d4a9b32
b349f79f 83
0fc0f178 84;;; org-mac-message.el ends here