Code cleanup and two enhancements for org-wl.el
[org-mode.git] / lisp / org-wl.el
blob4a769046f24286d6862fede4e4756dcbe5490eb6
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: 6.35trans
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-namazu-default-index nil
66 "Default namazu search index."
67 :type 'directory
68 :group 'org-wl)
70 ;; Declare external functions and variables
71 (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
72 (declare-function elmo-message-entity-field "ext:elmo-msgdb"
73 (entity field &optional type))
74 (declare-function elmo-message-field "ext:elmo"
75 (folder number field &optional type) t)
76 (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (id msgdb) t)
77 ;; Backward compatibility to old version of wl
78 (declare-function wl "ext:wl" () t)
79 (declare-function wl-summary-buffer-msgdb "ext:wl-folder" () t)
80 (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary"
81 (&optional id))
82 (declare-function wl-summary-line-from "ext:wl-summary" ())
83 (declare-function wl-summary-line-subject "ext:wl-summary" ())
84 (declare-function wl-summary-message-number "ext:wl-summary" ())
85 (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
86 (declare-function wl-summary-registered-temp-mark "ext:wl-action" (number))
87 (declare-function wl-folder-goto-folder-subr "ext:wl-folder"
88 (&optional folder sticky))
89 (declare-function wl-folder-get-petname "ext:wl-folder" (name))
90 (declare-function wl-folder-get-entity-from-buffer "ext:wl-folder"
91 (&optional getid))
92 (declare-function wl-folder-buffer-group-p "ext:wl-folder")
93 (defvar wl-init)
94 (defvar wl-summary-buffer-elmo-folder)
95 (defvar wl-summary-buffer-folder-name)
96 (defvar wl-folder-group-regexp)
98 (defconst org-wl-folder-types
99 '(("%" . imap) ("-" . nntp) ("+" . mh) ("=" . spool)
100 ("$" . archive) ("&" . pop) ("@" . shimbun) ("[" . search)
101 ("*" . multi) ("/" . filter) ("|" . pipe) ("'" . internal))
102 "List of folder indicators. See Wanderlust manual, section 3.")
104 ;; Install the link type
105 (org-add-link-type "wl" 'org-wl-open)
106 (add-hook 'org-store-link-functions 'org-wl-store-link)
108 ;; Implementation
110 (defun org-wl-folder-type (folder)
111 "Return symbol that indicicates the type of FOLDER.
112 FOLDER is the wanderlust folder name. The first character of the
113 folder name determines the the folder type."
114 (let* ((indicator (substring folder 0 1))
115 (type (cdr (assoc indicator org-wl-folder-types))))
116 ;; maybe access or file folder
117 (when (not type)
118 (setq type
119 (cond
120 ((and (>= (length folder) 5)
121 (string= (substring folder 0 5) "file:"))
122 'file)
123 ((and (>= (length folder) 7)
124 (string= (substring folder 0 7) "access:"))
125 'access)
127 nil))))
128 type))
130 (defun org-wl-message-field (field entity)
131 "Return content of FIELD in ENTITY.
132 FIELD is a symbol of a rfc822 message header field.
133 ENTITY is a message entity."
134 (let ((content (elmo-message-entity-field entity field)))
135 (if (listp content) (car content) content)))
137 (defun org-wl-store-link ()
138 "Store a link to a WL message or folder."
139 (cond
140 ((memq major-mode '(wl-summary-mode mime-view-mode))
141 (org-wl-store-link-message))
142 ((eq major-mode 'wl-folder-mode)
143 (org-wl-store-link-folder))
145 nil)))
147 (defun org-wl-store-link-folder ()
148 "Store a link to a WL folder."
149 (let* ((folder (wl-folder-get-entity-from-buffer))
150 (petname (wl-folder-get-petname folder))
151 (link (org-make-link "wl:" folder)))
152 (save-excursion
153 (beginning-of-line)
154 (if (and (wl-folder-buffer-group-p)
155 (looking-at wl-folder-group-regexp))
156 (error "Cannot store link to folder group: %s" folder))
157 (org-store-link-props :type "wl" :description petname
158 :link link)
159 link)))
161 (defun org-wl-store-link-message ()
162 "Store a link to a WL message."
163 (save-excursion
164 (let ((buf (if (eq major-mode 'wl-summary-mode)
165 (current-buffer)
166 (and (boundp 'wl-message-buffer-cur-summary-buffer)
167 wl-message-buffer-cur-summary-buffer))))
168 (when buf
169 (with-current-buffer buf
170 (let* ((msgnum (wl-summary-message-number))
171 (mark-info (wl-summary-registered-temp-mark msgnum))
172 (folder-name
173 (if (and org-wl-link-to-refile-destination
174 mark-info
175 (equal (nth 1 mark-info) "o")) ; marked as refile
176 (nth 2 mark-info)
177 wl-summary-buffer-folder-name))
178 (folder-type (org-wl-folder-type folder-name))
179 (wl-message-entity
180 (if (fboundp 'elmo-message-entity)
181 (elmo-message-entity
182 wl-summary-buffer-elmo-folder msgnum)
183 (elmo-msgdb-overview-get-entity
184 msgnum (wl-summary-buffer-msgdb))))
185 (message-id (org-wl-message-field 'message-id wl-message-entity))
186 (from (org-wl-message-field 'from wl-message-entity))
187 (to (org-wl-message-field 'to wl-message-entity))
188 (xref (org-wl-message-field 'xref wl-message-entity))
189 (subject (org-wl-message-field 'subject wl-message-entity))
190 desc link)
192 ;; remove text properties of subject string to avoid possible bug
193 ;; when formatting the subject
194 ;; (Emacs bug #5306, fixed)
195 (set-text-properties 0 (length subject) nil subject)
197 ;; maybe remove filter condition
198 (when (and (eq folder-type 'filter) org-wl-link-remove-filter)
199 (while (eq (org-wl-folder-type folder-name) 'filter)
200 (setq folder-name
201 (replace-regexp-in-string "^/[^/]+/" "" folder-name))))
203 ;; maybe create http link
204 (cond
205 ((and (eq folder-type 'shimbun) org-wl-shimbun-prefer-web-links xref)
206 (org-store-link-props :type "http" :link xref :description subject
207 :from from :to to :message-id message-id
208 :subject subject))
209 ((and (eq folder-type 'nntp) org-wl-nntp-prefer-web-links)
210 (setq link (format
211 (if (string-match "gmane\\." folder-name)
212 "http://mid.gmane.org/%s"
213 "http://groups.google.com/groups/search?as_umsgid=%s")
214 (org-fixup-message-id-for-http message-id)))
215 (org-store-link-props :type "http" :link link :description subject
216 :from from :to to :message-id message-id
217 :subject subject))
219 (org-store-link-props :type "wl" :from from :to to
220 :subject subject :message-id message-id)
221 (setq message-id (org-remove-angle-brackets message-id))
222 (setq desc (org-email-link-description))
223 (setq link (org-make-link "wl:" folder-name "#" message-id))
224 (org-add-link-props :link link :description desc)))
225 (or link xref)))))))
227 (defun org-wl-open (path)
228 "Follow the WL message link specified by PATH.
229 When called with one prefix, open message in namazu search folder
230 with `org-wl-namazu-default-index' as search index. When called
231 with two prefixes or `org-wl-namazu-default-index' is nil, ask
232 for namazu index."
233 (require 'wl)
234 (unless wl-init (wl))
235 ;; XXX: The imap-uw's MH folder names start with "%#".
236 (if (not (string-match "\\`\\(\\(?:%#\\)?[^#]+\\)\\(#\\(.*\\)\\)?" path))
237 (error "Error in Wanderlust link"))
238 (let ((folder (match-string 1 path))
239 (article (match-string 3 path)))
240 ;; maybe open message in namazu search folder
241 (when current-prefix-arg
242 (setq folder (concat "[" article "]"
243 (if (and (equal current-prefix-arg '(4))
244 org-wl-namazu-default-index)
245 org-wl-namazu-default-index
246 (read-directory-name "Namazu index: ")))))
247 (if (not (elmo-folder-exists-p (org-no-warnings
248 (wl-folder-get-elmo-folder folder))))
249 (error "No such folder: %s" folder))
250 (let ((old-buf (current-buffer))
251 (old-point (point-marker)))
252 (wl-folder-goto-folder-subr folder)
253 (with-current-buffer old-buf
254 ;; XXX: `wl-folder-goto-folder-subr' moves point to the
255 ;; beginning of the current line. So, restore the point
256 ;; in the old buffer.
257 (goto-char old-point))
258 (and article (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets
259 article))
260 (wl-summary-redisplay)))))
262 (provide 'org-wl)
264 ;; arch-tag: 29b75a0f-ef2e-430b-8abc-acff75bde54a
266 ;;; org-wl.el ends here