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