org-gnus.el 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. ;;; org-gnus.el --- Support for links to Gnus groups and messages from within Org-mode
  2. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  3. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  4. ;; Tassilo Horn <tassilo at member dot fsf dot org>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 6.13pre02
  8. ;;
  9. ;; This file is part of GNU Emacs.
  10. ;;
  11. ;; GNU Emacs is free software: you can redistribute it and/or modify
  12. ;; it under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation, either version 3 of the License, or
  14. ;; (at your option) any later version.
  15. ;; GNU Emacs is distributed in the hope that it will be useful,
  16. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. ;; GNU General Public License for more details.
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  21. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  22. ;;
  23. ;;; Commentary:
  24. ;; This file implements links to Gnus groups and messages from within Org-mode.
  25. ;; Org-mode loads this module by default - if this is not what you want,
  26. ;; configure the variable `org-modules'.
  27. ;;; Code:
  28. (require 'org)
  29. (eval-when-compile
  30. (require 'gnus-sum))
  31. ;; Customization variables
  32. (when (fboundp 'defvaralias)
  33. (defvaralias 'org-usenet-links-prefer-google 'org-gnus-prefer-web-links
  34. "Deprecated name for `org-gnus-prefer-web-links'."))
  35. (defcustom org-gnus-prefer-web-links nil
  36. "Non-nil means, `org-store-link' will create web links to Google groups.
  37. When nil, Gnus will be used for such links.
  38. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  39. negates this setting for the duration of the command."
  40. :group 'org-link-store
  41. :type 'boolean)
  42. ;; Declare external functions and variables
  43. (declare-function gnus-article-show-summary "gnus-art" ())
  44. (declare-function gnus-summary-last-subject "gnus-sum" ())
  45. (defvar gnus-other-frame-object)
  46. (defvar gnus-group-name)
  47. (defvar gnus-article-current)
  48. ;; Install the link type
  49. (org-add-link-type "gnus" 'org-gnus-open)
  50. (add-hook 'org-store-link-functions 'org-gnus-store-link)
  51. ;; Implementation
  52. (defun org-gnus-group-link (group)
  53. "Create a link to the Gnus group GROUP.
  54. If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
  55. non-nil, create a link to groups.google.com or gmane.org.
  56. Otherwise create a link to the group inside Gnus.
  57. If `org-store-link' was called with a prefix arg the meaning of
  58. `org-gnus-prefer-web-links' is reversed."
  59. (let ((unprefixed-group (replace-regexp-in-string "^[^:]+:" "" group)))
  60. (if (and (string-match "^nntp" group) ;; Only for nntp groups
  61. (org-xor current-prefix-arg
  62. org-gnus-prefer-web-links))
  63. (concat (if (string-match "gmane" unprefixed-group)
  64. "http://news.gmane.org/"
  65. "http://groups.google.com/group/")
  66. unprefixed-group)
  67. (concat "gnus:" group))))
  68. (defun org-gnus-article-link (group newsgroups message-id x-no-archive)
  69. "Create a link to a Gnus article.
  70. The article is specified by its MESSAGE-ID. Additional
  71. parameters are the Gnus GROUP, the NEWSGROUPS the article was
  72. posted to and the X-NO-ARCHIVE header value of that article.
  73. If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
  74. non-nil, create a link to groups.google.com or gmane.org.
  75. Otherwise create a link to the article inside Gnus.
  76. If `org-store-link' was called with a prefix arg the meaning of
  77. `org-gnus-prefer-web-links' is reversed."
  78. (if (and (org-xor current-prefix-arg org-gnus-prefer-web-links)
  79. newsgroups ;; Make web links only for nntp groups
  80. (not x-no-archive)) ;; and if X-No-Archive isn't set.
  81. (format (if (string-match "gmane\\." newsgroups)
  82. "http://mid.gmane.org/%s"
  83. "http://groups.google.com/groups/search?as_umsgid=%s")
  84. (org-fixup-message-id-for-http
  85. (replace-regexp-in-string "[<>]" "" message-id)))
  86. (org-make-link "gnus:" group "#" message-id)))
  87. (defun org-gnus-store-link ()
  88. "Store a link to a Gnus folder or message."
  89. (cond
  90. ((eq major-mode 'gnus-group-mode)
  91. (let* ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  92. (gnus-group-group-name)) ; version
  93. ((fboundp 'gnus-group-name)
  94. (gnus-group-name))
  95. (t "???")))
  96. desc link)
  97. (unless group (error "Not on a group"))
  98. (org-store-link-props :type "gnus" :group group)
  99. (setq desc (org-gnus-group-link group)
  100. link (org-make-link desc))
  101. (org-add-link-props :link link :description desc)
  102. link))
  103. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  104. (and (eq major-mode 'gnus-summary-mode) (gnus-summary-show-article))
  105. (let* ((group gnus-newsgroup-name)
  106. (header (with-current-buffer gnus-article-buffer
  107. (gnus-summary-toggle-header 1)
  108. (goto-char (point-min))
  109. (mail-header-extract-no-properties)))
  110. (from (mail-header 'from header))
  111. (message-id (mail-header 'message-id header))
  112. (date (mail-header 'date header))
  113. (to (mail-header 'to header))
  114. (newsgroups (mail-header 'newsgroups header))
  115. (x-no-archive (mail-header 'x-no-archive header))
  116. (subject (gnus-summary-subject-string))
  117. desc link)
  118. (org-store-link-props :type "gnus" :from from :subject subject
  119. :message-id message-id :group group :to to)
  120. (setq desc (org-email-link-description)
  121. link (org-gnus-article-link group newsgroups message-id x-no-archive))
  122. (org-add-link-props :link link :description desc)
  123. link))))
  124. (defun org-gnus-open (path)
  125. "Follow the Gnus message or folder link specified by PATH."
  126. (let (group article)
  127. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  128. (error "Error in Gnus link"))
  129. (setq group (match-string 1 path)
  130. article (match-string 3 path))
  131. (org-gnus-follow-link group article)))
  132. (defun org-gnus-follow-link (&optional group article)
  133. "Follow a Gnus link to GROUP and ARTICLE."
  134. (require 'gnus)
  135. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  136. (if gnus-other-frame-object (select-frame gnus-other-frame-object))
  137. (cond ((and group article)
  138. (gnus-group-read-group 1 nil group)
  139. (gnus-summary-goto-article
  140. (if (string-match "[^0-9]" article)
  141. article
  142. (string-to-number article))
  143. nil t))
  144. (group (gnus-group-jump-to-group group))))
  145. (defun org-gnus-no-new-news ()
  146. "Like `M-x gnus' but doesn't check for new news."
  147. (if (not (gnus-alive-p)) (gnus)))
  148. (provide 'org-gnus)
  149. ;; arch-tag: 512e0840-58fa-45b3-b456-71e10fa2376d
  150. ;;; org-gnus.el ends here