org-wl.el 9.7 KB

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