Escape percent character.
[org-mode/org-tableheadings.git] / lisp / org-wl.el
bloba5c82183ec13aa18f0484dddec3523317903aaef
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.36trans
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 (unless (and (wl-folder-buffer-group-p)
155 (looking-at wl-folder-group-regexp))
156 (org-store-link-props :type "wl" :description petname
157 :link link)
158 link))))
160 (defun org-wl-store-link-message ()
161 "Store a link to a WL message."
162 (save-excursion
163 (let ((buf (if (eq major-mode 'wl-summary-mode)
164 (current-buffer)
165 (and (boundp 'wl-message-buffer-cur-summary-buffer)
166 wl-message-buffer-cur-summary-buffer))))
167 (when buf
168 (with-current-buffer buf
169 (let* ((msgnum (wl-summary-message-number))
170 (mark-info (wl-summary-registered-temp-mark msgnum))
171 (folder-name
172 (if (and org-wl-link-to-refile-destination
173 mark-info
174 (equal (nth 1 mark-info) "o")) ; marked as refile
175 (nth 2 mark-info)
176 wl-summary-buffer-folder-name))
177 (folder-type (org-wl-folder-type folder-name))
178 (wl-message-entity
179 (if (fboundp 'elmo-message-entity)
180 (elmo-message-entity
181 wl-summary-buffer-elmo-folder msgnum)
182 (elmo-msgdb-overview-get-entity
183 msgnum (wl-summary-buffer-msgdb))))
184 (message-id
185 (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)
206 org-wl-shimbun-prefer-web-links xref)
207 (org-store-link-props :type "http" :link xref :description subject
208 :from from :to to :message-id message-id
209 :subject subject))
210 ((and (eq folder-type 'nntp) org-wl-nntp-prefer-web-links)
211 (setq link
212 (format
213 (if (string-match "gmane\\." folder-name)
214 "http://mid.gmane.org/%s"
215 "http://groups.google.com/groups/search?as_umsgid=%s")
216 (org-fixup-message-id-for-http message-id)))
217 (org-store-link-props :type "http" :link link :description subject
218 :from from :to to :message-id message-id
219 :subject subject))
221 (org-store-link-props :type "wl" :from from :to to
222 :subject subject :message-id message-id)
223 (setq message-id (org-remove-angle-brackets message-id))
224 (setq desc (org-email-link-description))
225 (setq link (org-make-link "wl:" folder-name "#" message-id))
226 (org-add-link-props :link link :description desc)))
227 (or link xref)))))))
229 (defun org-wl-open (path)
230 "Follow the WL message link specified by PATH.
231 When called with one prefix, open message in namazu search folder
232 with `org-wl-namazu-default-index' as search index. When called
233 with two prefixes or `org-wl-namazu-default-index' is nil, ask
234 for namazu index."
235 (require 'wl)
236 (unless wl-init (wl))
237 ;; XXX: The imap-uw's MH folder names start with "%#".
238 (if (not (string-match "\\`\\(\\(?:%#\\)?[^#]+\\)\\(#\\(.*\\)\\)?" path))
239 (error "Error in Wanderlust link"))
240 (let ((folder (match-string 1 path))
241 (article (match-string 3 path)))
242 ;; maybe open message in namazu search folder
243 (when current-prefix-arg
244 (setq folder (concat "[" article "]"
245 (if (and (equal current-prefix-arg '(4))
246 org-wl-namazu-default-index)
247 org-wl-namazu-default-index
248 (read-directory-name "Namazu index: ")))))
249 (if (not (elmo-folder-exists-p (org-no-warnings
250 (wl-folder-get-elmo-folder folder))))
251 (error "No such folder: %s" folder))
252 (let ((old-buf (current-buffer))
253 (old-point (point-marker)))
254 (wl-folder-goto-folder-subr folder)
255 (with-current-buffer old-buf
256 ;; XXX: `wl-folder-goto-folder-subr' moves point to the
257 ;; beginning of the current line. So, restore the point
258 ;; in the old buffer.
259 (goto-char old-point))
260 (and article (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets
261 article))
262 (wl-summary-redisplay)))))
264 (provide 'org-wl)
266 ;; arch-tag: 29b75a0f-ef2e-430b-8abc-acff75bde54a
268 ;;; org-wl.el ends here