Release 6.02a
[org-mode.git] / lisp / org-mew.el
blob1a526ed1baebf8357e34ae23c15cf96ce7e3f9b4
1 ;;; org-mew.el --- Support for links to Mew messages from within Org-mode
3 ;; Copyright (C) 2008 Free Software Foundation, Inc.
5 ;; Author: Tokuya Kameshima <kames at fa2 dot so-net dot ne dot jp>
6 ;; Keywords: outlines, hypermedia, calendar, wp
7 ;; Homepage: http://orgmode.org
8 ;; Version: 6.02a
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
15 ;; any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;; Commentary:
30 ;; This file implements links to Mew messages from within Org-mode.
31 ;; Org-mode loads this module by default - if this is not what you want,
32 ;; configure the variable `org-modules'.
34 ;;; Code:
36 (require 'org)
38 (defgroup org-mew nil
39 "Options concerning the Mew link."
40 :tag "Org Startup"
41 :group 'org-link)
43 (defcustom org-mew-link-to-refile-destination t
44 "Create a link to the refile destination if the message is marked as refile."
45 :group 'org-mew
46 :type 'boolean)
48 ;; Declare external functions and variables
49 (declare-function mew-cache-hit "ext:mew-cache" (fld msg &optional must-hit))
50 (declare-function mew-header-get-value "ext:mew-header"
51 (field &optional as-list))
52 (declare-function mew-init "ext:mew" ())
53 (declare-function mew-refile-get "ext:mew-refile" (msg))
54 (declare-function mew-summary-display "ext:mew-summary2" (&optional redisplay))
55 (declare-function mew-summary-folder-name "ext:mew-syntax" (&optional ext))
56 (declare-function mew-summary-get-mark "ext:mew-mark" ())
57 (declare-function mew-summary-message-number2 "ext:mew-syntax" ())
58 (declare-function mew-summary-pick-with-mewl "ext:mew-pick"
59 (pattern folder src-msgs))
60 (declare-function mew-summary-search-msg "ext:mew-const" (msg))
61 (declare-function mew-summary-set-message-buffer "ext:mew-summary3" (fld msg))
62 (declare-function mew-summary-visit-folder "ext:mew-summary4"
63 (folder &optional goend no-ls))
64 (declare-function mew-window-push "ext:mew" ())
65 (defvar mew-init-p)
66 (defvar mew-summary-goto-line-then-display)
68 ;; Install the link type
69 (org-add-link-type "mew" 'org-mew-open)
70 (add-hook 'org-store-link-functions 'org-mew-store-link)
72 ;; Implementation
73 (defun org-mew-store-link ()
74 "Store a link to a MEW folder or message."
75 (when (memq major-mode '(mew-summary-mode mew-virtual-mode))
76 (let* ((msgnum (mew-summary-message-number2))
77 (mark-info (mew-summary-get-mark))
78 (folder-name
79 (if (and org-mew-link-to-refile-destination
80 (eq mark-info ?o)) ; marked as refile
81 (nth 1 (mew-refile-get msgnum))
82 (mew-summary-folder-name)))
83 message-id from to subject desc link)
84 (save-window-excursion
85 (if (fboundp 'mew-summary-set-message-buffer)
86 (mew-summary-set-message-buffer folder-name msgnum)
87 (set-buffer (mew-cache-hit folder-name msgnum t)))
88 (setq message-id (mew-header-get-value "Message-Id:"))
89 (setq from (mew-header-get-value "From:"))
90 (setq to (mew-header-get-value "To:"))
91 (setq subject (mew-header-get-value "Subject:")))
92 (org-store-link-props :type "mew" :from from :to to
93 :subject subject :message-id message-id)
94 (setq message-id (org-remove-angle-brackets message-id))
95 (setq desc (org-email-link-description))
96 (setq link (org-make-link "mew:" folder-name
97 "#" message-id))
98 (org-add-link-props :link link :description desc)
99 link)))
101 (defun org-mew-open (path)
102 "Follow the Mew message link specified by PATH."
103 (let (folder msgnum)
104 (cond ((string-match "\\`\\(+.*\\)+\\+\\([0-9]+\\)\\'" path) ; for Bastien's
105 (setq folder (match-string 1 path))
106 (setq msgnum (match-string 2 path)))
107 ((string-match "\\`\\(\\(%#\\)?[^#]+\\)\\(#\\(.*\\)\\)?" path)
108 (setq folder (match-string 1 path))
109 (setq msgnum (match-string 4 path)))
110 (t (error "Error in Mew link")))
111 (require 'mew)
112 (mew-window-push)
113 (unless mew-init-p (mew-init))
114 (mew-summary-visit-folder folder)
115 (when msgnum
116 (if (not (string-match "\\`[0-9]+\\'" msgnum))
117 (let* ((pattern (concat "message-id=" msgnum))
118 (msgs (mew-summary-pick-with-mewl pattern folder nil)))
119 (setq msgnum (car msgs))))
120 (if (mew-summary-search-msg msgnum)
121 (if mew-summary-goto-line-then-display
122 (mew-summary-display))
123 (error "Message not found")))))
125 (provide 'org-mew)
127 ;;; org-mew.el ends here