Add 2011 to FSF/AIST copyright years.
[bpt/emacs.git] / lisp / org / org-gnus.el
CommitLineData
20908596
CD
1;;; org-gnus.el --- Support for links to Gnus groups and messages from within Org-mode
2
5df4f04c 3;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
1e4f816a 4;; Free Software Foundation, Inc.
20908596
CD
5
6;; Author: Carsten Dominik <carsten at orgmode dot org>
ce4fdcb9 7;; Tassilo Horn <tassilo at member dot fsf dot org>
20908596
CD
8;; Keywords: outlines, hypermedia, calendar, wp
9;; Homepage: http://orgmode.org
5dec9555 10;; Version: 6.33x
20908596
CD
11;;
12;; This file is part of GNU Emacs.
13;;
b1fc2b50 14;; GNU Emacs is free software: you can redistribute it and/or modify
20908596 15;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
16;; the Free Software Foundation, either version 3 of the License, or
17;; (at your option) any later version.
20908596
CD
18
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
b1fc2b50 25;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20908596
CD
26;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27;;
28;;; Commentary:
29
30;; This file implements links to Gnus groups and messages from within Org-mode.
31;; Org-mode loads this module by default - if this is not what you want,
32;; configure the variable `org-modules'.
33
34;;; Code:
35
36(require 'org)
21ee034d 37(eval-when-compile (require 'gnus-sum))
20908596 38
8d642074
CD
39;; Declare external functions and variables
40(declare-function message-fetch-field "message" (header &optional not-all))
41(declare-function message-narrow-to-head-1 "message" nil)
42;; The following line suppresses a compiler warning stemming from gnus-sum.el
43(declare-function gnus-summary-last-subject "gnus-sum" nil)
44
20908596
CD
45;; Customization variables
46
ce4fdcb9 47(when (fboundp 'defvaralias)
ff4be292 48 (defvaralias 'org-usenet-links-prefer-google 'org-gnus-prefer-web-links))
ce4fdcb9
CD
49
50(defcustom org-gnus-prefer-web-links nil
20908596
CD
51 "Non-nil means, `org-store-link' will create web links to Google groups.
52When nil, Gnus will be used for such links.
53Using a prefix arg to the command \\[org-store-link] (`org-store-link')
54negates this setting for the duration of the command."
55 :group 'org-link-store
56 :type 'boolean)
57
20908596
CD
58
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)
62
63;; Implementation
ce4fdcb9
CD
64
65(defun org-gnus-group-link (group)
66 "Create a link to the Gnus group GROUP.
67If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
68non-nil, create a link to groups.google.com or gmane.org.
69Otherwise create a link to the group inside Gnus.
70
71If `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))
db55f368
CD
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))))
ce4fdcb9
CD
82
83(defun org-gnus-article-link (group newsgroups message-id x-no-archive)
84 "Create a link to a Gnus article.
85The article is specified by its MESSAGE-ID. Additional
86parameters are the Gnus GROUP, the NEWSGROUPS the article was
87posted to and the X-NO-ARCHIVE header value of that article.
88
89If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
90non-nil, create a link to groups.google.com or gmane.org.
91Otherwise create a link to the article inside Gnus.
92
93If `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")
db55f368 101 (org-fixup-message-id-for-http message-id))
ce4fdcb9
CD
102 (org-make-link "gnus:" group "#" message-id)))
103
20908596
CD
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)
ce4fdcb9
CD
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)
0bd48b37
CD
114 (when 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)))
20908596
CD
120
121 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
20908596 122 (let* ((group gnus-newsgroup-name)
54a0dee5
CD
123 (header (with-current-buffer gnus-summary-buffer
124 (gnus-summary-article-header)))
125 (from (mail-header-from header))
126 (message-id (org-remove-angle-brackets (mail-header-id header)))
127 (date (mail-header-date header))
128 (subject (mail-header-subject header))
129 (to (cdr (assq 'To (mail-header-extra header))))
130 newsgroups x-no-archive desc link)
131 ;; Fetching an article is an expensive operation; newsgroup and
132 ;; x-no-archive are only needed for web links.
133 (when (org-xor current-prefix-arg org-gnus-prefer-web-links)
134 ;; Make sure the original article buffer is up-to-date
135 (save-window-excursion (gnus-summary-select-article))
136 (setq to (or to (gnus-fetch-original-field "To"))
137 newsgroups (gnus-fetch-original-field "Newsgroups")
138 x-no-archive (gnus-fetch-original-field "x-no-archive")))
20908596 139 (org-store-link-props :type "gnus" :from from :subject subject
621f83e4 140 :message-id message-id :group group :to to)
ce4fdcb9 141 (setq desc (org-email-link-description)
54a0dee5
CD
142 link (org-gnus-article-link
143 group newsgroups message-id x-no-archive))
20908596
CD
144 (org-add-link-props :link link :description desc)
145 link))))
146
147(defun org-gnus-open (path)
148 "Follow the Gnus message or folder link specified by PATH."
149 (let (group article)
150 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
151 (error "Error in Gnus link"))
152 (setq group (match-string 1 path)
153 article (match-string 3 path))
db55f368
CD
154 (when group
155 (setq group (org-substring-no-properties group)))
156 (when article
157 (setq article (org-substring-no-properties article)))
20908596
CD
158 (org-gnus-follow-link group article)))
159
160(defun org-gnus-follow-link (&optional group article)
161 "Follow a Gnus link to GROUP and ARTICLE."
162 (require 'gnus)
163 (funcall (cdr (assq 'gnus org-link-frame-setup)))
164 (if gnus-other-frame-object (select-frame gnus-other-frame-object))
db55f368
CD
165 (when group
166 (setq group (org-substring-no-properties group)))
167 (when article
168 (setq article (org-substring-no-properties article)))
20908596 169 (cond ((and group article)
db55f368
CD
170 (gnus-activate-group group t)
171 (condition-case nil
172 (let ((articles 1)
173 group-opened)
174 (while (and (not group-opened)
175 ;; stop on integer overflows
176 (> articles 0))
177 (setq group-opened (gnus-group-read-group articles nil group)
178 articles (if (< articles 16)
179 (1+ articles)
180 (* articles 2))))
181 (if group-opened
182 (gnus-summary-goto-article article nil t)
183 (message "Couldn't follow gnus link. %s"
184 "The summary couldn't be opened.")))
185 (quit (message "Couldn't follow gnus link. %s"
186 "The linked group is empty."))))
20908596
CD
187 (group (gnus-group-jump-to-group group))))
188
93b62de8
CD
189(defun org-gnus-no-new-news ()
190 "Like `M-x gnus' but doesn't check for new news."
191 (if (not (gnus-alive-p)) (gnus)))
192
20908596
CD
193(provide 'org-gnus)
194
88ac7b50 195;; arch-tag: 512e0840-58fa-45b3-b456-71e10fa2376d
b349f79f 196
20908596 197;;; org-gnus.el ends here