* admin/notes/copyright: Remove comments about deleted files in src/{m,s}.
[emacs.git] / lisp / org / org-wl.el
blob2a16d047b3316448ec0d0064b2ff10d6a69e7dd4
1 ;;; org-wl.el --- Support for links to Wanderlust messages from within Org-mode
3 ;; Copyright (C) 2004-2011 Free Software Foundation, Inc.
5 ;; Author: Tokuya Kameshima <kames at fa2 dot so-net dot ne dot jp>
6 ;; David Maus <dmaus at ictsoc 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 Wanderlust 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 (defgroup org-wl nil
38 "Options concerning the Wanderlust link."
39 :tag "Org Startup"
40 :group 'org-link)
42 (defcustom org-wl-link-to-refile-destination t
43 "Create a link to the refile destination if the message is marked as refile."
44 :group 'org-wl
45 :type 'boolean)
47 (defcustom org-wl-link-remove-filter nil
48 "Remove filter condition if message is filter folder."
49 :group 'org-wl
50 :type 'boolean)
52 (defcustom org-wl-shimbun-prefer-web-links nil
53 "If non-nil create web links for shimbun messages."
54 :group 'org-wl
55 :type 'boolean)
57 (defcustom org-wl-nntp-prefer-web-links nil
58 "If non-nil create web links for nntp messages.
59 When folder name contains string \"gmane\" link to gmane,
60 googlegroups otherwise."
61 :type 'boolean
62 :group 'org-wl)
64 (defcustom org-wl-disable-folder-check t
65 "Disable check for new messages when open a link."
66 :type 'boolean
67 :group 'org-wl)
69 (defcustom org-wl-namazu-default-index nil
70 "Default namazu search index."
71 :type 'directory
72 :group 'org-wl)
74 ;; Declare external functions and variables
75 (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
76 (declare-function elmo-message-entity-field "ext:elmo-msgdb"
77 (entity field &optional type))
78 (declare-function elmo-message-field "ext:elmo"
79 (folder number field &optional type) t)
80 (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (id msgdb) t)
81 ;; Backward compatibility to old version of wl
82 (declare-function wl "ext:wl" () t)
83 (declare-function wl-summary-buffer-msgdb "ext:wl-folder" () t)
84 (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary"
85 (&optional id))
86 (declare-function wl-summary-jump-to-msg "ext:wl-summary"
87 (&optional number beg end))
88 (declare-function wl-summary-line-from "ext:wl-summary" ())
89 (declare-function wl-summary-line-subject "ext:wl-summary" ())
90 (declare-function wl-summary-message-number "ext:wl-summary" ())
91 (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
92 (declare-function wl-summary-registered-temp-mark "ext:wl-action" (number))
93 (declare-function wl-folder-goto-folder-subr "ext:wl-folder"
94 (&optional folder sticky))
95 (declare-function wl-folder-get-petname "ext:wl-folder" (name))
96 (declare-function wl-folder-get-entity-from-buffer "ext:wl-folder"
97 (&optional getid))
98 (declare-function wl-folder-buffer-group-p "ext:wl-folder")
99 (defvar wl-init)
100 (defvar wl-summary-buffer-elmo-folder)
101 (defvar wl-summary-buffer-folder-name)
102 (defvar wl-folder-group-regexp)
103 (defvar wl-auto-check-folder-name)
104 (defvar elmo-nntp-default-server)
106 (defconst org-wl-folder-types
107 '(("%" . imap) ("-" . nntp) ("+" . mh) ("=" . spool)
108 ("$" . archive) ("&" . pop) ("@" . shimbun) ("[" . search)
109 ("*" . multi) ("/" . filter) ("|" . pipe) ("'" . internal))
110 "List of folder indicators. See Wanderlust manual, section 3.")
112 ;; Install the link type
113 (org-add-link-type "wl" 'org-wl-open)
114 (add-hook 'org-store-link-functions 'org-wl-store-link)
116 ;; Implementation
118 (defun org-wl-folder-type (folder)
119 "Return symbol that indicates the type of FOLDER.
120 FOLDER is the wanderlust folder name. The first character of the
121 folder name determines the the folder type."
122 (let* ((indicator (substring folder 0 1))
123 (type (cdr (assoc indicator org-wl-folder-types))))
124 ;; maybe access or file folder
125 (when (not type)
126 (setq type
127 (cond
128 ((and (>= (length folder) 5)
129 (string= (substring folder 0 5) "file:"))
130 'file)
131 ((and (>= (length folder) 7)
132 (string= (substring folder 0 7) "access:"))
133 'access)
135 nil))))
136 type))
138 (defun org-wl-message-field (field entity)
139 "Return content of FIELD in ENTITY.
140 FIELD is a symbol of a rfc822 message header field.
141 ENTITY is a message entity."
142 (let ((content (elmo-message-entity-field entity field 'string)))
143 (if (listp content) (car content) content)))
145 (defun org-wl-store-link ()
146 "Store a link to a WL message or folder."
147 (unless (eobp)
148 (cond
149 ((memq major-mode '(wl-summary-mode mime-view-mode))
150 (org-wl-store-link-message))
151 ((eq major-mode 'wl-folder-mode)
152 (org-wl-store-link-folder))
154 nil))))
156 (defun org-wl-store-link-folder ()
157 "Store a link to a WL folder."
158 (let* ((folder (wl-folder-get-entity-from-buffer))
159 (petname (wl-folder-get-petname folder))
160 (link (org-make-link "wl:" folder)))
161 (save-excursion
162 (beginning-of-line)
163 (unless (and (wl-folder-buffer-group-p)
164 (looking-at wl-folder-group-regexp))
165 (org-store-link-props :type "wl" :description petname
166 :link link)
167 link))))
169 (defun org-wl-store-link-message ()
170 "Store a link to a WL message."
171 (save-excursion
172 (let ((buf (if (eq major-mode 'wl-summary-mode)
173 (current-buffer)
174 (and (boundp 'wl-message-buffer-cur-summary-buffer)
175 wl-message-buffer-cur-summary-buffer))))
176 (when buf
177 (with-current-buffer buf
178 (let* ((msgnum (wl-summary-message-number))
179 (mark-info (wl-summary-registered-temp-mark msgnum))
180 (folder-name
181 (if (and org-wl-link-to-refile-destination
182 mark-info
183 (equal (nth 1 mark-info) "o")) ; marked as refile
184 (nth 2 mark-info)
185 wl-summary-buffer-folder-name))
186 (folder-type (org-wl-folder-type folder-name))
187 (wl-message-entity
188 (if (fboundp 'elmo-message-entity)
189 (elmo-message-entity
190 wl-summary-buffer-elmo-folder msgnum)
191 (elmo-msgdb-overview-get-entity
192 msgnum (wl-summary-buffer-msgdb))))
193 (message-id
194 (org-wl-message-field 'message-id wl-message-entity))
195 (message-id-no-brackets
196 (org-remove-angle-brackets message-id))
197 (from (org-wl-message-field 'from wl-message-entity))
198 (to (org-wl-message-field 'to wl-message-entity))
199 (xref (org-wl-message-field 'xref wl-message-entity))
200 (subject (org-wl-message-field 'subject wl-message-entity))
201 (date (org-wl-message-field 'date wl-message-entity))
202 (date-ts (and date (format-time-string
203 (org-time-stamp-format t)
204 (date-to-time date))))
205 (date-ts-ia (and date (format-time-string
206 (org-time-stamp-format t t)
207 (date-to-time date))))
208 desc link)
210 ;; remove text properties of subject string to avoid possible bug
211 ;; when formatting the subject
212 ;; (Emacs bug #5306, fixed)
213 (set-text-properties 0 (length subject) nil subject)
215 ;; maybe remove filter condition
216 (when (and (eq folder-type 'filter) org-wl-link-remove-filter)
217 (while (eq (org-wl-folder-type folder-name) 'filter)
218 (setq folder-name
219 (replace-regexp-in-string "^/[^/]+/" "" folder-name))))
221 ;; maybe create http link
222 (cond
223 ((and (eq folder-type 'shimbun)
224 org-wl-shimbun-prefer-web-links xref)
225 (org-store-link-props :type "http" :link xref :description subject
226 :from from :to to :message-id message-id
227 :message-id-no-brackets message-id-no-brackets
228 :subject subject))
229 ((and (eq folder-type 'nntp) org-wl-nntp-prefer-web-links)
230 (setq link
231 (format
232 (if (string-match "gmane\\." folder-name)
233 "http://mid.gmane.org/%s"
234 "http://groups.google.com/groups/search?as_umsgid=%s")
235 (org-fixup-message-id-for-http message-id)))
236 (org-store-link-props :type "http" :link link :description subject
237 :from from :to to :message-id message-id
238 :message-id-no-brackets message-id-no-brackets
239 :subject subject))
241 (org-store-link-props :type "wl" :from from :to to
242 :subject subject :message-id message-id
243 :message-id-no-brackets message-id-no-brackets)
244 (setq desc (org-email-link-description))
245 (setq link (org-make-link "wl:" folder-name "#" message-id-no-brackets))
246 (org-add-link-props :link link :description desc)))
247 (when date
248 (org-add-link-props :date date :date-timestamp date-ts
249 :date-timestamp-inactive date-ts-ia))
250 (or link xref)))))))
252 (defun org-wl-open-nntp (path)
253 "Follow the nntp: link specified by PATH."
254 (let* ((spec (split-string path "/"))
255 (server (split-string (nth 2 spec) "@"))
256 (group (nth 3 spec))
257 (article (nth 4 spec)))
258 (org-wl-open
259 (concat "-" group ":" (if (cdr server)
260 (car (split-string (car server) ":"))
262 (if (string= elmo-nntp-default-server (nth 2 spec))
264 (concat "@" (or (cdr server) (car server))))
265 (if article (concat "#" article) "")))))
267 (defun org-wl-open (path)
268 "Follow the WL message link specified by PATH.
269 When called with one prefix, open message in namazu search folder
270 with `org-wl-namazu-default-index' as search index. When called
271 with two prefixes or `org-wl-namazu-default-index' is nil, ask
272 for namazu index."
273 (require 'wl)
274 (let ((wl-auto-check-folder-name
275 (if org-wl-disable-folder-check
276 'none
277 wl-auto-check-folder-name)))
278 (unless wl-init (wl))
279 ;; XXX: The imap-uw's MH folder names start with "%#".
280 (if (not (string-match "\\`\\(\\(?:%#\\)?[^#]+\\)\\(#\\(.*\\)\\)?" path))
281 (error "Error in Wanderlust link"))
282 (let ((folder (match-string 1 path))
283 (article (match-string 3 path)))
284 ;; maybe open message in namazu search folder
285 (when current-prefix-arg
286 (setq folder (concat "[" article "]"
287 (if (and (equal current-prefix-arg '(4))
288 org-wl-namazu-default-index)
289 org-wl-namazu-default-index
290 (read-directory-name "Namazu index: ")))))
291 (if (not (elmo-folder-exists-p (org-no-warnings
292 (wl-folder-get-elmo-folder folder))))
293 (error "No such folder: %s" folder))
294 (let ((old-buf (current-buffer))
295 (old-point (point-marker)))
296 (wl-folder-goto-folder-subr folder)
297 (with-current-buffer old-buf
298 ;; XXX: `wl-folder-goto-folder-subr' moves point to the
299 ;; beginning of the current line. So, restore the point
300 ;; in the old buffer.
301 (goto-char old-point))
302 (when article
303 (if (org-string-match-p "@" article)
304 (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets
305 article))
306 (or (wl-summary-jump-to-msg (string-to-number article))
307 (error "No such message: %s" article)))
308 (wl-summary-redisplay))))))
310 (provide 'org-wl)
313 ;;; org-wl.el ends here