Refactor unescaping functions
[org-mode.git] / lisp / org-mhe.el
blobc9b04811a1a29041d75535f144e2069189c1b1a2
1 ;;; org-mhe.el --- Support for links to MH-E messages from within Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Thomas Baumann <thomas dot baumann at ch dot tum dot de>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 7.4
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
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'.
33 ;;; Code:
35 (require 'org)
37 ;; Customization variables
39 (defcustom org-mhe-search-all-folders nil
40 "Non-nil means the search for the mh-message may extend to all folders.
41 When non-nil, the search for a message will extend to all other
42 folders if it cannot be found in the folder given in the link.
43 Searching all folders may be slow with the default pick based
44 search but is very efficient with one of the other search engines
45 supported by MH-E."
46 :group 'org-link-follow
47 :type 'boolean)
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)
75 (defvar mh-search-regexp-builder)
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)
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 (save-window-excursion
87 (let* ((from (org-mhe-get-header "From:"))
88 (to (org-mhe-get-header "To:"))
89 (message-id (org-mhe-get-header "Message-Id:"))
90 (subject (org-mhe-get-header "Subject:"))
91 (date (org-mhe-get-header "Date:"))
92 (date-ts (and date (format-time-string
93 (org-time-stamp-format t) (date-to-time date))))
94 (date-ts-ia (and date (format-time-string
95 (org-time-stamp-format t t)
96 (date-to-time date))))
97 link desc)
98 (org-store-link-props :type "mh" :from from :to to
99 :subject subject :message-id message-id)
100 (when date
101 (org-add-link-props :date date :date-timestamp date-ts
102 :date-timestamp-inactive date-ts-ia))
103 (setq desc (org-email-link-description))
104 (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
105 (org-remove-angle-brackets message-id)))
106 (org-add-link-props :link link :description desc)
107 link))))
109 (defun org-mhe-open (path)
110 "Follow an MH-E message link specified by PATH."
111 (let (folder article)
112 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
113 (error "Error in MH-E link"))
114 (setq folder (match-string 1 path)
115 article (match-string 3 path))
116 (org-mhe-follow-link folder article)))
118 ;;; mh-e integration based on planner-mode
119 (defun org-mhe-get-message-real-folder ()
120 "Return the name of the real folder for the current message.
121 So if you use sequences, it will now work."
122 (save-excursion
123 (let* ((folder
124 (if (equal major-mode 'mh-folder-mode)
125 mh-current-folder
126 ;; Refer to the show buffer
127 mh-show-folder-buffer))
128 (end-index
129 (if (boundp 'mh-index-folder)
130 (min (length mh-index-folder) (length folder))))
132 ;; a simple test on mh-index-data does not work, because
133 ;; mh-index-data is always nil in a show buffer.
134 (if (and (boundp 'mh-index-folder)
135 (string= mh-index-folder (substring folder 0 end-index)))
136 (if (equal major-mode 'mh-show-mode)
137 (save-window-excursion
138 (let (pop-up-frames)
139 (when (buffer-live-p (get-buffer folder))
140 (progn
141 (pop-to-buffer folder)
142 (org-mhe-get-message-folder-from-index)
145 (org-mhe-get-message-folder-from-index)
147 folder
151 (defun org-mhe-get-message-folder-from-index ()
152 "Return the name of the message folder in an index folder buffer."
153 (save-excursion
154 (mh-index-previous-folder)
155 (if (re-search-forward "^\\(+.*\\)$" nil t)
156 (message "%s" (match-string 1)))))
158 (defun org-mhe-get-message-folder ()
159 "Return the name of the current message folder.
160 Be careful if you use sequences."
161 (save-excursion
162 (if (equal major-mode 'mh-folder-mode)
163 mh-current-folder
164 ;; Refer to the show buffer
165 mh-show-folder-buffer)))
167 (defun org-mhe-get-message-num ()
168 "Return the number of the current message.
169 Be careful if you use sequences."
170 (save-excursion
171 (if (equal major-mode 'mh-folder-mode)
172 (mh-get-msg-num nil)
173 ;; Refer to the show buffer
174 (mh-show-buffer-message-number))))
176 (defun org-mhe-get-header (header)
177 "Return the field for HEADER of the message in folder mode.
178 This will create a show buffer for the corresponding message. If
179 you have a better idea of how to do this then please let us know."
180 (let* ((folder (org-mhe-get-message-folder))
181 (num (org-mhe-get-message-num))
182 (buffer (get-buffer-create (concat "show-" folder)))
183 (header-field))
184 (with-current-buffer buffer
185 (mh-display-msg num folder)
186 (if (equal major-mode 'mh-folder-mode)
187 (mh-header-display)
188 (mh-show-header-display))
189 (set-buffer buffer)
190 (setq header-field (mh-get-header-field header))
191 (if (equal major-mode 'mh-folder-mode)
192 (mh-show)
193 (mh-show-show))
194 (org-trim header-field))))
196 (defun org-mhe-follow-link (folder article)
197 "Follow an MH-E link to FOLDER and ARTICLE.
198 If ARTICLE is nil FOLDER is shown. If the configuration variable
199 `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
200 ARTICLE is searched in all folders. Indexed searches (swish++,
201 namazu, and others supported by MH-E) will always search in all
202 folders."
203 (require 'mh-e)
204 (require 'mh-search)
205 (require 'mh-utils)
206 (mh-find-path)
207 (if (not article)
208 (mh-visit-folder (mh-normalize-folder-name folder))
209 (mh-search-choose)
210 (if (equal mh-searcher 'pick)
211 (progn
212 (setq article (org-add-angle-brackets article))
213 (mh-search folder (list "--message-id" article))
214 (when (and org-mhe-search-all-folders
215 (not (org-mhe-get-message-real-folder)))
216 (kill-this-buffer)
217 (mh-search "+" (list "--message-id" article))))
218 (if mh-search-regexp-builder
219 (mh-search "+" (funcall mh-search-regexp-builder
220 (list (cons 'message-id article))))
221 (mh-search "+" article)))
222 (if (org-mhe-get-message-real-folder)
223 (mh-show-msg 1)
224 (kill-this-buffer)
225 (error "Message not found"))))
227 (provide 'org-mhe)
229 ;; arch-tag: dcb05484-8627-491d-a8c1-01dbd2bde4ae
231 ;;; org-mhe.el ends here