customizable processing of Org-mode text used as comments in tangled source-code...
[org-mode.git] / lisp / org-rmail.el
blob0437ce32ff19b0da5316ed3a20e1a36d502a8470
1 ;;; org-rmail.el --- Support for links to Rmail messages from within Org-mode
3 ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;;
9 ;; This file is part of GNU Emacs.
11 ;; GNU 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 of the License, or
14 ;; (at your option) any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25 ;;; Commentary:
27 ;; This file implements links to Rmail messages from within Org-mode.
28 ;; Org-mode loads this module by default - if this is not what you want,
29 ;; configure the variable `org-modules'.
31 ;;; Code:
33 (require 'org)
35 ;; Declare external functions and variables
36 (declare-function rmail-show-message "rmail" (&optional n no-summary))
37 (declare-function rmail-what-message "rmail" ())
38 (defvar rmail-current-message)
40 ;; Install the link type
41 (org-add-link-type "rmail" 'org-rmail-open)
42 (add-hook 'org-store-link-functions 'org-rmail-store-link)
44 ;; Implementation
45 (defun org-rmail-store-link ()
46 "Store a link to an Rmail folder or message."
47 (when (or (eq major-mode 'rmail-mode)
48 (eq major-mode 'rmail-summary-mode))
49 (save-window-excursion
50 (save-restriction
51 (when (eq major-mode 'rmail-summary-mode)
52 (rmail-show-message rmail-current-message))
53 (when (fboundp 'rmail-narrow-to-non-pruned-header)
54 (rmail-narrow-to-non-pruned-header))
55 (let* ((folder buffer-file-name)
56 (message-id (mail-fetch-field "message-id"))
57 (from (mail-fetch-field "from"))
58 (to (mail-fetch-field "to"))
59 (subject (mail-fetch-field "subject"))
60 (date (mail-fetch-field "date"))
61 (date-ts (and date (format-time-string
62 (org-time-stamp-format t)
63 (date-to-time date))))
64 (date-ts-ia (and date (format-time-string
65 (org-time-stamp-format t t)
66 (date-to-time date))))
67 desc link)
68 (org-store-link-props
69 :type "rmail" :from from :to to
70 :subject subject :message-id message-id)
71 (when date
72 (org-add-link-props :date date :date-timestamp date-ts
73 :date-timestamp-inactive date-ts-ia))
74 (setq message-id (org-remove-angle-brackets message-id))
75 (setq desc (org-email-link-description))
76 (setq link (org-make-link "rmail:" folder "#" message-id))
77 (org-add-link-props :link link :description desc)
78 (rmail-show-message rmail-current-message)
79 link)))))
81 (defun org-rmail-open (path)
82 "Follow an Rmail message link to the specified PATH."
83 (let (folder article)
84 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
85 (error "Error in Rmail link"))
86 (setq folder (match-string 1 path)
87 article (match-string 3 path))
88 (org-rmail-follow-link folder article)))
90 (defun org-rmail-follow-link (folder article)
91 "Follow an Rmail link to FOLDER and ARTICLE."
92 (require 'rmail)
93 (setq article (org-add-angle-brackets article))
94 (let (message-number)
95 (save-excursion
96 (save-window-excursion
97 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
98 (setq message-number
99 (save-restriction
100 (widen)
101 (goto-char (point-max))
102 (if (re-search-backward
103 (concat "^Message-ID:\\s-+" (regexp-quote
104 (or article "")))
105 nil t)
106 (rmail-what-message))))))
107 (if message-number
108 (progn
109 (rmail (if (string= folder "RMAIL") rmail-file-name folder))
110 (rmail-show-message message-number)
111 message-number)
112 (error "Message not found"))))
114 (provide 'org-rmail)
116 ;;; org-rmail.el ends here