2010-04-10 Carsten Dominik <carsten.dominik@gmail.com>
[bpt/emacs.git] / lisp / org / org-mhe.el
CommitLineData
20908596
CD
1;;; org-mhe.el --- Support for links to MH-E messages from within Org-mode
2
114f9c96 3;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
1e4f816a 4;; Free Software Foundation, Inc.
20908596
CD
5
6;; Author: Thomas Baumann <thomas dot baumann at ch dot tum dot de>
7;; Keywords: outlines, hypermedia, calendar, wp
8;; Homepage: http://orgmode.org
ed21c5c8 9;; Version: 6.35i
20908596
CD
10;;
11;; This file is part of GNU Emacs.
12;;
b1fc2b50 13;; GNU Emacs is free software: you can redistribute it and/or modify
20908596 14;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
20908596
CD
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
b1fc2b50 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20908596
CD
25;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26;;
27;;; Commentary:
28
29;; This file implements links to MH-E messages from within Org-mode.
30;; Org-mode loads this module by default - if this is not what you want,
31;; configure the variable `org-modules'.
32
33;;; Code:
34
35(require 'org)
36
37;; Customization variables
38
39(defcustom org-mhe-search-all-folders nil
40 "Non-nil means the search for the mh-message may extend to all folders.
41When non-nil, the search for a message will extend to all other
42folders if it cannot be found in the folder given in the link.
43Searching all folders may be slow with the default pick based
44search but is very efficient with one of the other search engines
45supported by MH-E."
46 :group 'org-link-follow
47 :type 'boolean)
48
49;; Declare external functions and variables
50(declare-function mh-display-msg "mh-show" (msg-num folder-name))
51(declare-function mh-find-path "mh-utils" ())
52(declare-function mh-get-header-field "mh-utils" (field))
53(declare-function mh-get-msg-num "mh-utils" (error-if-no-message))
54(declare-function mh-header-display "mh-show" ())
55(declare-function mh-index-previous-folder "mh-search" ())
56(declare-function mh-normalize-folder-name "mh-utils"
57 (folder &optional empty-string-okay dont-remove-trailing-slash
58 return-nil-if-folder-empty))
59(declare-function mh-search "mh-search"
60 (folder search-regexp &optional redo-search-flag
61 window-config))
62(declare-function mh-search-choose "mh-search" (&optional searcher))
63(declare-function mh-show "mh-show" (&optional message redisplay-flag))
64(declare-function mh-show-buffer-message-number "mh-comp" (&optional buffer))
65(declare-function mh-show-header-display "mh-show" t t)
66(declare-function mh-show-msg "mh-show" (msg))
67(declare-function mh-show-show "mh-show" t t)
68(declare-function mh-visit-folder "mh-folder" (folder &optional
69 range index-data))
70(defvar mh-progs)
71(defvar mh-current-folder)
72(defvar mh-show-folder-buffer)
73(defvar mh-index-folder)
74(defvar mh-searcher)
b349f79f 75(defvar mh-search-regexp-builder)
20908596
CD
76
77;; Install the link type
78(org-add-link-type "mhe" 'org-mhe-open)
79(add-hook 'org-store-link-functions 'org-mhe-store-link)
80
81;; Implementation
82(defun org-mhe-store-link ()
83 "Store a link to an MH-E folder or message."
84 (when (or (equal major-mode 'mh-folder-mode)
85 (equal major-mode 'mh-show-mode))
86 (let ((from (org-mhe-get-header "From:"))
87 (to (org-mhe-get-header "To:"))
88 (message-id (org-mhe-get-header "Message-Id:"))
89 (subject (org-mhe-get-header "Subject:"))
90 link desc)
91 (org-store-link-props :type "mh" :from from :to to
92 :subject subject :message-id message-id)
93 (setq desc (org-email-link-description))
94 (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
95 (org-remove-angle-brackets message-id)))
96 (org-add-link-props :link link :description desc)
97 link)))
98
99(defun org-mhe-open (path)
100 "Follow an MH-E message link specified by PATH."
101 (let (folder article)
102 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
103 (error "Error in MH-E link"))
104 (setq folder (match-string 1 path)
105 article (match-string 3 path))
106 (org-mhe-follow-link folder article)))
107
108;;; mh-e integration based on planner-mode
109(defun org-mhe-get-message-real-folder ()
110 "Return the name of the real folder for the current message.
111So if you use sequences, it will now work."
112 (save-excursion
113 (let* ((folder
33306645
CD
114 (if (equal major-mode 'mh-folder-mode)
115 mh-current-folder
116 ;; Refer to the show buffer
117 mh-show-folder-buffer))
118 (end-index
119 (if (boundp 'mh-index-folder)
120 (min (length mh-index-folder) (length folder))))
121 )
20908596
CD
122 ;; a simple test on mh-index-data does not work, because
123 ;; mh-index-data is always nil in a show buffer.
124 (if (and (boundp 'mh-index-folder)
33306645
CD
125 (string= mh-index-folder (substring folder 0 end-index)))
126 (if (equal major-mode 'mh-show-mode)
127 (save-window-excursion
20908596
CD
128 (let (pop-up-frames)
129 (when (buffer-live-p (get-buffer folder))
130 (progn
131 (pop-to-buffer folder)
132 (org-mhe-get-message-folder-from-index)
133 )
134 )))
33306645
CD
135 (org-mhe-get-message-folder-from-index)
136 )
137 folder
138 )
20908596
CD
139 )))
140
141(defun org-mhe-get-message-folder-from-index ()
b349f79f 142 "Return the name of the message folder in an index folder buffer."
20908596
CD
143 (save-excursion
144 (mh-index-previous-folder)
b349f79f
CD
145 (if (re-search-forward "^\\(+.*\\)$" nil t)
146 (message "%s" (match-string 1)))))
20908596
CD
147
148(defun org-mhe-get-message-folder ()
149 "Return the name of the current message folder.
150Be careful if you use sequences."
151 (save-excursion
152 (if (equal major-mode 'mh-folder-mode)
33306645 153 mh-current-folder
20908596
CD
154 ;; Refer to the show buffer
155 mh-show-folder-buffer)))
156
157(defun org-mhe-get-message-num ()
158 "Return the number of the current message.
159Be careful if you use sequences."
160 (save-excursion
161 (if (equal major-mode 'mh-folder-mode)
33306645 162 (mh-get-msg-num nil)
20908596
CD
163 ;; Refer to the show buffer
164 (mh-show-buffer-message-number))))
165
166(defun org-mhe-get-header (header)
167 "Return the field for HEADER of the message in folder mode.
168This will create a show buffer for the corresponding message. If
169you have a better idea of how to do this then please let us know."
170 (let* ((folder (org-mhe-get-message-folder))
33306645
CD
171 (num (org-mhe-get-message-num))
172 (buffer (get-buffer-create (concat "show-" folder)))
173 (header-field))
20908596
CD
174 (with-current-buffer buffer
175 (mh-display-msg num folder)
176 (if (equal major-mode 'mh-folder-mode)
33306645 177 (mh-header-display)
20908596
CD
178 (mh-show-header-display))
179 (set-buffer buffer)
180 (setq header-field (mh-get-header-field header))
181 (if (equal major-mode 'mh-folder-mode)
33306645 182 (mh-show)
20908596
CD
183 (mh-show-show))
184 header-field)))
185
186(defun org-mhe-follow-link (folder article)
187 "Follow an MH-E link to FOLDER and ARTICLE.
188If ARTICLE is nil FOLDER is shown. If the configuration variable
189`org-mhe-search-all-folders' is t and `mh-searcher' is pick,
190ARTICLE is searched in all folders. Indexed searches (swish++,
191namazu, and others supported by MH-E) will always search in all
192folders."
193 (require 'mh-e)
194 (require 'mh-search)
195 (require 'mh-utils)
196 (mh-find-path)
197 (if (not article)
198 (mh-visit-folder (mh-normalize-folder-name folder))
20908596
CD
199 (mh-search-choose)
200 (if (equal mh-searcher 'pick)
33306645 201 (progn
b349f79f 202 (setq article (org-add-angle-brackets article))
33306645
CD
203 (mh-search folder (list "--message-id" article))
204 (when (and org-mhe-search-all-folders
205 (not (org-mhe-get-message-real-folder)))
206 (kill-this-buffer)
207 (mh-search "+" (list "--message-id" article))))
b349f79f 208 (if mh-search-regexp-builder
33306645 209 (mh-search "+" (funcall mh-search-regexp-builder
b349f79f 210 (list (cons 'message-id article))))
33306645 211 (mh-search "+" article)))
20908596 212 (if (org-mhe-get-message-real-folder)
33306645 213 (mh-show-msg 1)
20908596
CD
214 (kill-this-buffer)
215 (error "Message not found"))))
216
217(provide 'org-mhe)
218
88ac7b50 219;; arch-tag: dcb05484-8627-491d-a8c1-01dbd2bde4ae
b349f79f 220
20908596 221;;; org-mhe.el ends here