org-gnus.el 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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
  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: 6.30trans
  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. ;; Customization variables
  32. (when (fboundp 'defvaralias)
  33. (defvaralias 'org-usenet-links-prefer-google 'org-gnus-prefer-web-links))
  34. (defcustom org-gnus-prefer-web-links nil
  35. "Non-nil means, `org-store-link' will create web links to Google groups.
  36. When nil, Gnus will be used for such links.
  37. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  38. negates this setting for the duration of the command."
  39. :group 'org-link-store
  40. :type 'boolean)
  41. ;; Declare external functions and variables
  42. (declare-function message-fetch-field "message" (header &optional not-all))
  43. (declare-function message-narrow-to-head-1 "message" nil)
  44. (defvar gnus-other-frame-object)
  45. (defvar gnus-group-name)
  46. (defvar gnus-article-current)
  47. ;; Install the link type
  48. (org-add-link-type "gnus" 'org-gnus-open)
  49. (add-hook 'org-store-link-functions 'org-gnus-store-link)
  50. ;; Implementation
  51. (defun org-gnus-group-link (group)
  52. "Create a link to the Gnus group GROUP.
  53. If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
  54. non-nil, create a link to groups.google.com or gmane.org.
  55. Otherwise create a link to the group inside Gnus.
  56. If `org-store-link' was called with a prefix arg the meaning of
  57. `org-gnus-prefer-web-links' is reversed."
  58. (let ((unprefixed-group (replace-regexp-in-string "^[^:]+:" "" group)))
  59. (if (and (string-match "^nntp" group) ;; Only for nntp groups
  60. (org-xor current-prefix-arg
  61. org-gnus-prefer-web-links))
  62. (org-make-link (if (string-match "gmane" unprefixed-group)
  63. "http://news.gmane.org/"
  64. "http://groups.google.com/group/")
  65. unprefixed-group)
  66. (org-make-link "gnus:" group))))
  67. (defun org-gnus-article-link (group newsgroups message-id x-no-archive)
  68. "Create a link to a Gnus article.
  69. The article is specified by its MESSAGE-ID. Additional
  70. parameters are the Gnus GROUP, the NEWSGROUPS the article was
  71. posted to and the X-NO-ARCHIVE header value of that article.
  72. If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
  73. non-nil, create a link to groups.google.com or gmane.org.
  74. Otherwise create a link to the article inside Gnus.
  75. If `org-store-link' was called with a prefix arg the meaning of
  76. `org-gnus-prefer-web-links' is reversed."
  77. (if (and (org-xor current-prefix-arg org-gnus-prefer-web-links)
  78. newsgroups ;; Make web links only for nntp groups
  79. (not x-no-archive)) ;; and if X-No-Archive isn't set.
  80. (format (if (string-match "gmane\\." newsgroups)
  81. "http://mid.gmane.org/%s"
  82. "http://groups.google.com/groups/search?as_umsgid=%s")
  83. (org-fixup-message-id-for-http message-id))
  84. (org-make-link "gnus:" group "#" message-id)))
  85. (defun org-gnus-store-link ()
  86. "Store a link to a Gnus folder or message."
  87. (cond
  88. ((eq major-mode 'gnus-group-mode)
  89. (let* ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  90. (gnus-group-group-name)) ; version
  91. ((fboundp 'gnus-group-name)
  92. (gnus-group-name))
  93. (t "???")))
  94. desc link)
  95. (when group
  96. (org-store-link-props :type "gnus" :group group)
  97. (setq desc (org-gnus-group-link group)
  98. link desc)
  99. (org-add-link-props :link link :description desc)
  100. link)))
  101. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  102. (let* ((group gnus-newsgroup-name)
  103. (header (with-current-buffer gnus-summary-buffer
  104. (gnus-summary-article-header)))
  105. (from (mail-header-from header))
  106. (message-id (org-remove-angle-brackets (mail-header-id header)))
  107. (date (mail-header-date header))
  108. (subject (mail-header-subject header))
  109. (to (cdr (assq 'To (mail-header-extra header))))
  110. newsgroups x-no-archive desc link)
  111. ;; Fetching an article is an expensive operation; newsgroup and
  112. ;; x-no-archive are only needed for web links.
  113. (when (org-xor current-prefix-arg org-gnus-prefer-web-links)
  114. ;; Make sure the original article buffer is up-to-date
  115. (save-window-excursion (gnus-summary-select-article))
  116. (setq to (or to (gnus-fetch-original-field "To"))
  117. newsgroups (gnus-fetch-original-field "Newsgroups")
  118. x-no-archive (gnus-fetch-original-field "x-no-archive")))
  119. (org-store-link-props :type "gnus" :from from :subject subject
  120. :message-id message-id :group group :to to)
  121. (setq desc (org-email-link-description)
  122. link (org-gnus-article-link
  123. group newsgroups message-id x-no-archive))
  124. (org-add-link-props :link link :description desc)
  125. link))))
  126. (defun org-gnus-open (path)
  127. "Follow the Gnus message or folder link specified by PATH."
  128. (let (group article)
  129. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  130. (error "Error in Gnus link"))
  131. (setq group (match-string 1 path)
  132. article (match-string 3 path))
  133. (when group
  134. (setq group (org-substring-no-properties group)))
  135. (when article
  136. (setq article (org-substring-no-properties article)))
  137. (org-gnus-follow-link group article)))
  138. (defun org-gnus-follow-link (&optional group article)
  139. "Follow a Gnus link to GROUP and ARTICLE."
  140. (require 'gnus)
  141. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  142. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  143. (when group
  144. (setq group (org-substring-no-properties group)))
  145. (when article
  146. (setq article (org-substring-no-properties article)))
  147. (cond ((and group article)
  148. (gnus-activate-group group t)
  149. (condition-case nil
  150. (let ((articles 1)
  151. group-opened)
  152. (while (and (not group-opened)
  153. ;; stop on integer overflows
  154. (> articles 0))
  155. (setq group-opened (gnus-group-read-group articles nil group)
  156. articles (if (< articles 16)
  157. (1+ articles)
  158. (* articles 2))))
  159. (if group-opened
  160. (gnus-summary-goto-article article nil t)
  161. (message "Couldn't follow gnus link. %s"
  162. "The summary couldn't be opened.")))
  163. (quit (message "Couldn't follow gnus link. %s"
  164. "The linked group is empty."))))
  165. (group (gnus-group-jump-to-group group))))
  166. (defun org-gnus-no-new-news ()
  167. "Like `M-x gnus' but doesn't check for new news."
  168. (if (not (gnus-alive-p)) (gnus)))
  169. (provide 'org-gnus)
  170. ;; arch-tag: 512e0840-58fa-45b3-b456-71e10fa2376d
  171. ;;; org-gnus.el ends here