org-gnus.el 7.7 KB

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