org-gnus.el 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. ;;; org-gnus.el --- Support for links to Gnus groups and messages from within Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  5. ;; Tassilo Horn <tassilo at member dot fsf dot org>
  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 Gnus groups and 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. (eval-when-compile (require 'gnus-sum))
  31. ;; Declare external functions and variables
  32. (declare-function message-fetch-field "message" (header &optional not-all))
  33. (declare-function message-narrow-to-head-1 "message" nil)
  34. ;; The following line suppresses a compiler warning stemming from gnus-sum.el
  35. (declare-function gnus-summary-last-subject "gnus-sum" nil)
  36. ;; Customization variables
  37. (when (fboundp 'defvaralias)
  38. (defvaralias 'org-usenet-links-prefer-google 'org-gnus-prefer-web-links))
  39. (defcustom org-gnus-prefer-web-links nil
  40. "If non-nil, `org-store-link' creates web links to Google groups or Gmane.
  41. When nil, Gnus will be used for such links.
  42. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  43. negates this setting for the duration of the command."
  44. :group 'org-link-store
  45. :type 'boolean)
  46. (defcustom org-gnus-nnimap-query-article-no-from-file t
  47. "If non-nil, `org-gnus-follow-link' will try to translate
  48. Message-Ids to article numbers by querying the .overview file.
  49. Normally, this translation is done by querying the IMAP server,
  50. which is usually very fast. Unfortunately, some (maybe badly
  51. configured) IMAP servers don't support this operation quickly.
  52. So if following a link to a Gnus article takes ages, try setting
  53. this variable to `t'."
  54. :group 'org-link-store
  55. :type 'boolean)
  56. ;; Install the link type
  57. (org-add-link-type "gnus" 'org-gnus-open)
  58. (add-hook 'org-store-link-functions 'org-gnus-store-link)
  59. ;; Implementation
  60. (defun org-gnus-nnimap-cached-article-number (group server message-id)
  61. "Return cached article number (uid) of message in GROUP on SERVER.
  62. MESSAGE-ID is the message-id header field that identifies the
  63. message. If the uid is not cached, return nil."
  64. (with-temp-buffer
  65. (let ((nov (nnimap-group-overview-filename group server)))
  66. (when (file-exists-p nov)
  67. (mm-insert-file-contents nov)
  68. (set-buffer-modified-p nil)
  69. (goto-char (point-min))
  70. (catch 'found
  71. (while (search-forward message-id nil t)
  72. (let ((hdr (split-string (thing-at-point 'line) "\t")))
  73. (if (string= (nth 4 hdr) message-id)
  74. (throw 'found (nth 0 hdr))))))))))
  75. (defun org-gnus-group-link (group)
  76. "Create a link to the Gnus group GROUP.
  77. If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
  78. non-nil, create a link to groups.google.com or gmane.org.
  79. Otherwise create a link to the group inside Gnus.
  80. If `org-store-link' was called with a prefix arg the meaning of
  81. `org-gnus-prefer-web-links' is reversed."
  82. (let ((unprefixed-group (replace-regexp-in-string "^[^:]+:" "" group)))
  83. (if (and (string-match "^nntp" group) ;; Only for nntp groups
  84. (org-xor current-prefix-arg
  85. org-gnus-prefer-web-links))
  86. (org-make-link (if (string-match "gmane" unprefixed-group)
  87. "http://news.gmane.org/"
  88. "http://groups.google.com/group/")
  89. unprefixed-group)
  90. (org-make-link "gnus:" group))))
  91. (defun org-gnus-article-link (group newsgroups message-id x-no-archive)
  92. "Create a link to a Gnus article.
  93. The article is specified by its MESSAGE-ID. Additional
  94. parameters are the Gnus GROUP, the NEWSGROUPS the article was
  95. posted to and the X-NO-ARCHIVE header value of that article.
  96. If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
  97. non-nil, create a link to groups.google.com or gmane.org.
  98. Otherwise create a link to the article inside Gnus.
  99. If `org-store-link' was called with a prefix arg the meaning of
  100. `org-gnus-prefer-web-links' is reversed."
  101. (if (and (org-xor current-prefix-arg org-gnus-prefer-web-links)
  102. newsgroups ;; Make web links only for nntp groups
  103. (not x-no-archive)) ;; and if X-No-Archive isn't set.
  104. (format (if (string-match "gmane\\." newsgroups)
  105. "http://mid.gmane.org/%s"
  106. "http://groups.google.com/groups/search?as_umsgid=%s")
  107. (org-fixup-message-id-for-http message-id))
  108. (org-make-link "gnus:" group "#" message-id)))
  109. (defun org-gnus-store-link ()
  110. "Store a link to a Gnus folder or message."
  111. (cond
  112. ((eq major-mode 'gnus-group-mode)
  113. (let* ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  114. (gnus-group-group-name)) ; version
  115. ((fboundp 'gnus-group-name)
  116. (gnus-group-name))
  117. (t "???")))
  118. desc link)
  119. (when group
  120. (org-store-link-props :type "gnus" :group group)
  121. (setq desc (org-gnus-group-link group)
  122. link desc)
  123. (org-add-link-props :link link :description desc)
  124. link)))
  125. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  126. (let* ((group gnus-newsgroup-name)
  127. (header (with-current-buffer gnus-summary-buffer
  128. (gnus-summary-article-header)))
  129. (from (mail-header-from header))
  130. (message-id (org-remove-angle-brackets (mail-header-id header)))
  131. (date (mail-header-date header))
  132. (subject (copy-sequence (mail-header-subject header)))
  133. (to (cdr (assq 'To (mail-header-extra header))))
  134. newsgroups x-no-archive desc link)
  135. ;; Remove text properties of subject string to avoid Emacs bug
  136. ;; #3506
  137. (set-text-properties 0 (length subject) nil subject)
  138. ;; Fetching an article is an expensive operation; newsgroup and
  139. ;; x-no-archive are only needed for web links.
  140. (when (org-xor current-prefix-arg org-gnus-prefer-web-links)
  141. ;; Make sure the original article buffer is up-to-date
  142. (save-window-excursion (gnus-summary-select-article))
  143. (setq to (or to (gnus-fetch-original-field "To"))
  144. newsgroups (gnus-fetch-original-field "Newsgroups")
  145. x-no-archive (gnus-fetch-original-field "x-no-archive")))
  146. (org-store-link-props :type "gnus" :from from :subject subject
  147. :message-id message-id :group group :to to)
  148. (setq desc (org-email-link-description)
  149. link (org-gnus-article-link
  150. group newsgroups message-id x-no-archive))
  151. (org-add-link-props :link link :description desc)
  152. link))))
  153. (defun org-gnus-open (path)
  154. "Follow the Gnus message or folder link specified by PATH."
  155. (let (group article)
  156. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  157. (error "Error in Gnus link"))
  158. (setq group (match-string 1 path)
  159. article (match-string 3 path))
  160. (when group
  161. (setq group (org-substring-no-properties group)))
  162. (when article
  163. (setq article (org-substring-no-properties article)))
  164. (org-gnus-follow-link group article)))
  165. (defun org-gnus-follow-link (&optional group article)
  166. "Follow a Gnus link to GROUP and ARTICLE."
  167. (require 'gnus)
  168. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  169. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  170. (when group
  171. (setq group (org-substring-no-properties group)))
  172. (when article
  173. (setq article (org-substring-no-properties article)))
  174. (cond ((and group article)
  175. (gnus-activate-group group t)
  176. (condition-case nil
  177. (let* ((method (gnus-find-method-for-group group))
  178. (backend (car method))
  179. (server (cadr method)))
  180. (cond
  181. ((eq backend 'nndoc)
  182. (if (gnus-group-read-group t nil group)
  183. (gnus-summary-goto-article article nil t)
  184. (message "Couldn't follow gnus link. %s"
  185. "The summary couldn't be opened.")))
  186. (t
  187. (let ((articles 1)
  188. group-opened)
  189. (when (and (eq backend 'nnimap)
  190. org-gnus-nnimap-query-article-no-from-file)
  191. (setq article
  192. (or (org-gnus-nnimap-cached-article-number
  193. (nth 1 (split-string group ":"))
  194. server (concat "<" article ">")) article)))
  195. (while (and (not group-opened)
  196. ;; stop on integer overflows
  197. (> articles 0))
  198. (setq group-opened (gnus-group-read-group
  199. articles nil group)
  200. articles (if (< articles 16)
  201. (1+ articles)
  202. (* articles 2))))
  203. (if group-opened
  204. (gnus-summary-goto-article article nil t)
  205. (message "Couldn't follow gnus link. %s"
  206. "The summary couldn't be opened."))))))
  207. (quit (message "Couldn't follow gnus link. %s"
  208. "The linked group is empty."))))
  209. (group (gnus-group-jump-to-group group))))
  210. (defun org-gnus-no-new-news ()
  211. "Like `M-x gnus' but doesn't check for new news."
  212. (if (not (gnus-alive-p)) (gnus)))
  213. (provide 'org-gnus)
  214. ;; arch-tag: 512e0840-58fa-45b3-b456-71e10fa2376d
  215. ;;; org-gnus.el ends here