org-agenda.el: Fixed a problem when computing time-grid.
[org-mode.git] / lisp / org-wl.el
blobe1222ed821c3e04045741111af3deb3da91eb8c0
1 ;;; org-wl.el --- Support for links to Wanderlust messages from within Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 ;; Free Software Foundation, Inc.
6 ;; Author: Tokuya Kameshima <kames at fa2 dot so-net dot ne dot jp>
7 ;; David Maus <dmaus at ictsoc dot de>
8 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; Homepage: http://orgmode.org
10 ;; Version: 7.01trans
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;; Commentary:
30 ;; This file implements links to Wanderlust 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-wl nil
39 "Options concerning the Wanderlust link."
40 :tag "Org Startup"
41 :group 'org-link)
43 (defcustom org-wl-link-to-refile-destination t
44 "Create a link to the refile destination if the message is marked as refile."
45 :group 'org-wl
46 :type 'boolean)
48 (defcustom org-wl-link-remove-filter nil
49 "Remove filter condition if message is filter folder."
50 :group 'org-wl
51 :type 'boolean)
53 (defcustom org-wl-shimbun-prefer-web-links nil
54 "If non-nil create web links for shimbun messages."
55 :group 'org-wl
56 :type 'boolean)
58 (defcustom org-wl-nntp-prefer-web-links nil
59 "If non-nil create web links for nntp messages.
60 When folder name contains string \"gmane\" link to gmane,
61 googlegroups otherwise."
62 :type 'boolean
63 :group 'org-wl)
65 (defcustom org-wl-disable-folder-check t
66 "Disable check for new messages when open a link."
67 :type 'boolean
68 :group 'org-wl)
70 (defcustom org-wl-namazu-default-index nil
71 "Default namazu search index."
72 :type 'directory
73 :group 'org-wl)
75 ;; Declare external functions and variables
76 (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
77 (declare-function elmo-message-entity-field "ext:elmo-msgdb"
78 (entity field &optional type))
79 (declare-function elmo-message-field "ext:elmo"
80 (folder number field &optional type) t)
81 (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (id msgdb) t)
82 ;; Backward compatibility to old version of wl
83 (declare-function wl "ext:wl" () t)
84 (declare-function wl-summary-buffer-msgdb "ext:wl-folder" () t)
85 (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary"
86 (&optional id))
87 (declare-function wl-summary-line-from "ext:wl-summary" ())
88 (declare-function wl-summary-line-subject "ext:wl-summary" ())
89 (declare-function wl-summary-message-number "ext:wl-summary" ())
90 (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
91 (declare-function wl-summary-registered-temp-mark "ext:wl-action" (number))
92 (declare-function wl-folder-goto-folder-subr "ext:wl-folder"
93 (&optional folder sticky))
94 (declare-function wl-folder-get-petname "ext:wl-folder" (name))
95 (declare-function wl-folder-get-entity-from-buffer "ext:wl-folder"
96 (&optional getid))
97 (declare-function wl-folder-buffer-group-p "ext:wl-folder")
98 (defvar wl-init)
99 (defvar wl-summary-buffer-elmo-folder)
100 (defvar wl-summary-buffer-folder-name)
101 (defvar wl-folder-group-regexp)
102 (defvar wl-auto-check-folder-name)
104 (defconst org-wl-folder-types
105 '(("%" . imap) ("-" . nntp) ("+" . mh) ("=" . spool)
106 ("$" . archive) ("&" . pop) ("@" . shimbun) ("[" . search)
107 ("*" . multi) ("/" . filter) ("|" . pipe) ("'" . internal))
108 "List of folder indicators. See Wanderlust manual, section 3.")
110 ;; Install the link type
111 (org-add-link-type "wl" 'org-wl-open)
112 (add-hook 'org-store-link-functions 'org-wl-store-link)
114 ;; Implementation
116 (defun org-wl-folder-type (folder)
117 "Return symbol that indicates the type of FOLDER.
118 FOLDER is the wanderlust folder name. The first character of the
119 folder name determines the the folder type."
120 (let* ((indicator (substring folder 0 1))
121 (type (cdr (assoc indicator org-wl-folder-types))))
122 ;; maybe access or file folder
123 (when (not type)
124 (setq type
125 (cond
126 ((and (>= (length folder) 5)
127 (string= (substring folder 0 5) "file:"))
128 'file)
129 ((and (>= (length folder) 7)
130 (string= (substring folder 0 7) "access:"))
131 'access)
133 nil))))
134 type))
136 (defun org-wl-message-field (field entity)
137 "Return content of FIELD in ENTITY.
138 FIELD is a symbol of a rfc822 message header field.
139 ENTITY is a message entity."
140 (let ((content (elmo-message-entity-field entity field)))
141 (if (listp content) (car content) content)))
143 (defun org-wl-store-link ()
144 "Store a link to a WL message or folder."
145 (unless (eobp)
146 (cond
147 ((memq major-mode '(wl-summary-mode mime-view-mode))
148 (org-wl-store-link-message))
149 ((eq major-mode 'wl-folder-mode)
150 (org-wl-store-link-folder))
152 nil))))
154 (defun org-wl-store-link-folder ()
155 "Store a link to a WL folder."
156 (let* ((folder (wl-folder-get-entity-from-buffer))
157 (petname (wl-folder-get-petname folder))
158 (link (org-make-link "wl:" folder)))
159 (save-excursion
160 (beginning-of-line)
161 (unless (and (wl-folder-buffer-group-p)
162 (looking-at wl-folder-group-regexp))
163 (org-store-link-props :type "wl" :description petname
164 :link link)
165 link))))
167 (defun org-wl-store-link-message ()
168 "Store a link to a WL message."
169 (save-excursion
170 (let ((buf (if (eq major-mode 'wl-summary-mode)
171 (current-buffer)
172 (and (boundp 'wl-message-buffer-cur-summary-buffer)
173 wl-message-buffer-cur-summary-buffer))))
174 (when buf
175 (with-current-buffer buf
176 (let* ((msgnum (wl-summary-message-number))
177 (mark-info (wl-summary-registered-temp-mark msgnum))
178 (folder-name
179 (if (and org-wl-link-to-refile-destination
180 mark-info
181 (equal (nth 1 mark-info) "o")) ; marked as refile
182 (nth 2 mark-info)
183 wl-summary-buffer-folder-name))
184 (folder-type (org-wl-folder-type folder-name))
185 (wl-message-entity
186 (if (fboundp 'elmo-message-entity)
187 (elmo-message-entity
188 wl-summary-buffer-elmo-folder msgnum)
189 (elmo-msgdb-overview-get-entity
190 msgnum (wl-summary-buffer-msgdb))))
191 (message-id
192 (org-wl-message-field 'message-id wl-message-entity))
193 (message-id-no-brackets
194 (org-remove-angle-brackets message-id))
195 (from (org-wl-message-field 'from wl-message-entity))
196 (to (org-wl-message-field 'to wl-message-entity))
197 (xref (org-wl-message-field 'xref wl-message-entity))
198 (subject (org-wl-message-field 'subject wl-message-entity))
199 desc link)
201 ;; remove text properties of subject string to avoid possible bug
202 ;; when formatting the subject
203 ;; (Emacs bug #5306, fixed)
204 (set-text-properties 0 (length subject) nil subject)
206 ;; maybe remove filter condition
207 (when (and (eq folder-type 'filter) org-wl-link-remove-filter)
208 (while (eq (org-wl-folder-type folder-name) 'filter)
209 (setq folder-name
210 (replace-regexp-in-string "^/[^/]+/" "" folder-name))))
212 ;; maybe create http link
213 (cond
214 ((and (eq folder-type 'shimbun)
215 org-wl-shimbun-prefer-web-links xref)
216 (org-store-link-props :type "http" :link xref :description subject
217 :from from :to to :message-id message-id
218 :message-id-no-brackets message-id-no-brackets
219 :subject subject))
220 ((and (eq folder-type 'nntp) org-wl-nntp-prefer-web-links)
221 (setq link
222 (format
223 (if (string-match "gmane\\." folder-name)
224 "http://mid.gmane.org/%s"
225 "http://groups.google.com/groups/search?as_umsgid=%s")
226 (org-fixup-message-id-for-http message-id)))
227 (org-store-link-props :type "http" :link link :description subject
228 :from from :to to :message-id message-id
229 :message-id-no-brackets message-id-no-brackets
230 :subject subject))
232 (org-store-link-props :type "wl" :from from :to to
233 :subject subject :message-id message-id
234 :message-id-no-brackets message-id-no-brackets)
235 (setq desc (org-email-link-description))
236 (setq link (org-make-link "wl:" folder-name "#" message-id-no-brackets))
237 (org-add-link-props :link link :description desc)))
238 (or link xref)))))))
240 (defun org-wl-open (path)
241 "Follow the WL message link specified by PATH.
242 When called with one prefix, open message in namazu search folder
243 with `org-wl-namazu-default-index' as search index. When called
244 with two prefixes or `org-wl-namazu-default-index' is nil, ask
245 for namazu index."
246 (require 'wl)
247 (let ((wl-auto-check-folder-name
248 (if org-wl-disable-folder-check
249 'none
250 wl-auto-check-folder-name)))
251 (unless wl-init (wl))
252 ;; XXX: The imap-uw's MH folder names start with "%#".
253 (if (not (string-match "\\`\\(\\(?:%#\\)?[^#]+\\)\\(#\\(.*\\)\\)?" path))
254 (error "Error in Wanderlust link"))
255 (let ((folder (match-string 1 path))
256 (article (match-string 3 path)))
257 ;; maybe open message in namazu search folder
258 (when current-prefix-arg
259 (setq folder (concat "[" article "]"
260 (if (and (equal current-prefix-arg '(4))
261 org-wl-namazu-default-index)
262 org-wl-namazu-default-index
263 (read-directory-name "Namazu index: ")))))
264 (if (not (elmo-folder-exists-p (org-no-warnings
265 (wl-folder-get-elmo-folder folder))))
266 (error "No such folder: %s" folder))
267 (let ((old-buf (current-buffer))
268 (old-point (point-marker)))
269 (wl-folder-goto-folder-subr folder)
270 (with-current-buffer old-buf
271 ;; XXX: `wl-folder-goto-folder-subr' moves point to the
272 ;; beginning of the current line. So, restore the point
273 ;; in the old buffer.
274 (goto-char old-point))
275 (and article (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets
276 article))
277 (wl-summary-redisplay))))))
279 (provide 'org-wl)
281 ;; arch-tag: 29b75a0f-ef2e-430b-8abc-acff75bde54a
283 ;;; org-wl.el ends here