org-wl.el 10 KB

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