org-wl.el 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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: 6.35trans
  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-namazu-default-index nil
  53. "Default namazu search index."
  54. :type 'directory
  55. :group 'org-wl)
  56. ;; Declare external functions and variables
  57. (declare-function elmo-folder-exists-p "ext:elmo" (folder) t)
  58. (declare-function elmo-message-entity-field "ext:elmo-msgdb"
  59. (entity field &optional type))
  60. (declare-function elmo-message-field "ext:elmo"
  61. (folder number field &optional type) t)
  62. (declare-function elmo-msgdb-overview-get-entity "ext:elmo" (id msgdb) t)
  63. ;; Backward compatibility to old version of wl
  64. (declare-function wl "ext:wl" () t)
  65. (declare-function wl-summary-buffer-msgdb "ext:wl-folder" () t)
  66. (declare-function wl-summary-jump-to-msg-by-message-id "ext:wl-summary"
  67. (&optional id))
  68. (declare-function wl-summary-line-from "ext:wl-summary" ())
  69. (declare-function wl-summary-line-subject "ext:wl-summary" ())
  70. (declare-function wl-summary-message-number "ext:wl-summary" ())
  71. (declare-function wl-summary-redisplay "ext:wl-summary" (&optional arg))
  72. (declare-function wl-summary-registered-temp-mark "ext:wl-action" (number))
  73. (declare-function wl-folder-goto-folder-subr "ext:wl-folder"
  74. (&optional folder sticky))
  75. (defvar wl-init)
  76. (defvar wl-summary-buffer-elmo-folder)
  77. (defvar wl-summary-buffer-folder-name)
  78. (defconst org-wl-folder-types
  79. '(("%" . imap) ("-" . nntp) ("+" . mh) ("=" . spool)
  80. ("$" . archive) ("&" . pop) ("@" . shimbun) ("[" . search)
  81. ("*" . multi) ("/" . filter) ("|" . pipe) ("'" . internal))
  82. "List of folder indicators. See Wanderlust manual, section 3.")
  83. ;; Install the link type
  84. (org-add-link-type "wl" 'org-wl-open)
  85. (add-hook 'org-store-link-functions 'org-wl-store-link)
  86. ;; Implementation
  87. (defun org-wl-folder-type (folder)
  88. "Return symbol that indicicates the type of FOLDER.
  89. FOLDER is the wanderlust folder name. The first character of the
  90. folder name determines the the folder type."
  91. (let* ((indicator (substring folder 0 1))
  92. (type (cdr (assoc indicator org-wl-folder-types))))
  93. ;; maybe access or file folder
  94. (when (not type)
  95. (setq type
  96. (cond
  97. ((and (>= (length folder) 5)
  98. (string= (substring folder 0 5) "file:"))
  99. 'file)
  100. ((and (>= (length folder) 7)
  101. (string= (substring folder 0 7) "access:"))
  102. 'access)
  103. (t
  104. nil))))
  105. type))
  106. (defun org-wl-store-link ()
  107. "Store a link to a WL folder or message."
  108. (when (eq major-mode 'wl-summary-mode)
  109. (let* ((msgnum (wl-summary-message-number))
  110. (mark-info (wl-summary-registered-temp-mark msgnum))
  111. (folder-name
  112. (if (and org-wl-link-to-refile-destination
  113. mark-info
  114. (equal (nth 1 mark-info) "o")) ; marked as refile
  115. (nth 2 mark-info)
  116. wl-summary-buffer-folder-name))
  117. (folder-type (org-wl-folder-type folder-name))
  118. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  119. msgnum 'message-id))
  120. (wl-message-entity
  121. (if (fboundp 'elmo-message-entity)
  122. (elmo-message-entity
  123. wl-summary-buffer-elmo-folder msgnum)
  124. (elmo-msgdb-overview-get-entity
  125. msgnum (wl-summary-buffer-msgdb))))
  126. (from (let ((from-field (elmo-message-entity-field wl-message-entity
  127. 'from)))
  128. (if (listp from-field)
  129. (car from-field)
  130. from-field)))
  131. (to (let ((to-field (elmo-message-entity-field wl-message-entity
  132. 'to)))
  133. (if (listp to-field)
  134. (car to-field)
  135. to-field)))
  136. (xref (let ((xref-field (elmo-message-entity-field wl-message-entity
  137. 'xref)))
  138. (if (listp xref-field)
  139. (car xref-field)
  140. xref-field)))
  141. (subject (let (wl-thr-indent-string wl-parent-message-entity)
  142. (wl-summary-line-subject)))
  143. desc link)
  144. ;; remove text properties of subject string to avoid possible bug
  145. ;; when formatting the subject
  146. ;; (Emacs bug #5306, fixed)
  147. (set-text-properties 0 (length subject) nil subject)
  148. ;; maybe remove filter condition
  149. (when (and (eq folder-type 'filter) org-wl-link-remove-filter)
  150. (while (eq (org-wl-folder-type folder-name) 'filter)
  151. (setq folder-name
  152. (replace-regexp-in-string "^/[^/]+/" "" folder-name))))
  153. ;; maybe create http link
  154. (cond
  155. ((and (eq folder-type 'shimbun) org-wl-shimbun-prefer-web-links xref)
  156. (org-store-link-props :type "http" :link xref :description subject
  157. :from from :to to :message-id message-id
  158. :subject subject))
  159. ((and (eq folder-type 'nntp) org-wl-nntp-prefer-web-links)
  160. (setq link (format
  161. (if (string-match "gmane\\." folder-name)
  162. "http://mid.gmane.org/%s"
  163. "http://groups.google.com/groups/search?as_umsgid=%s")
  164. (org-fixup-message-id-for-http message-id)))
  165. (org-store-link-props :type "http" :link link :description subject
  166. :from from :to to :message-id message-id
  167. :subject subject))
  168. (t
  169. (org-store-link-props :type "wl" :from from :to to
  170. :subject subject :message-id message-id)
  171. (setq message-id (org-remove-angle-brackets message-id))
  172. (setq desc (org-email-link-description))
  173. (setq link (org-make-link "wl:" folder-name "#" message-id))
  174. (org-add-link-props :link link :description desc)))
  175. (or link xref))))
  176. (defun org-wl-open (path)
  177. "Follow the WL message link specified by PATH.
  178. When called with one prefix, open message in namazu search folder
  179. with `org-wl-namazu-default-index' as search index. When called
  180. with two prefixes or `org-wl-namazu-default-index' is nil, ask
  181. for namazu index."
  182. (require 'wl)
  183. (unless wl-init (wl))
  184. ;; XXX: The imap-uw's MH folder names start with "%#".
  185. (if (not (string-match "\\`\\(\\(?:%#\\)?[^#]+\\)\\(#\\(.*\\)\\)?" path))
  186. (error "Error in Wanderlust link"))
  187. (let ((folder (match-string 1 path))
  188. (article (match-string 3 path)))
  189. ;; maybe open message in namazu search folder
  190. (when current-prefix-arg
  191. (setq folder (concat "[" article "]"
  192. (if (and (equal current-prefix-arg '(4))
  193. org-wl-namazu-default-index)
  194. org-wl-namazu-default-index
  195. (read-directory-name "Namazu index: ")))))
  196. (if (not (elmo-folder-exists-p (org-no-warnings
  197. (wl-folder-get-elmo-folder folder))))
  198. (error "No such folder: %s" folder))
  199. (let ((old-buf (current-buffer))
  200. (old-point (point-marker)))
  201. (wl-folder-goto-folder-subr folder)
  202. (save-excursion
  203. ;; XXX: `wl-folder-goto-folder-subr' moves point to the
  204. ;; beginning of the current line. So, restore the point
  205. ;; in the old buffer.
  206. (set-buffer old-buf)
  207. (goto-char old-point))
  208. (and (wl-summary-jump-to-msg-by-message-id (org-add-angle-brackets
  209. article))
  210. (wl-summary-redisplay)))))
  211. (provide 'org-wl)
  212. ;; arch-tag: 29b75a0f-ef2e-430b-8abc-acff75bde54a
  213. ;;; org-wl.el ends here