2009-01-26 Carsten Dominik <dominik@science.uva.nl>
[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
1e4f816a
CD
3;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
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
0bd48b37 10;; Version: 6.19a
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)
37(eval-when-compile
38 (require 'gnus-sum))
39
40;; Customization variables
41
ce4fdcb9 42(when (fboundp 'defvaralias)
ff4be292 43 (defvaralias 'org-usenet-links-prefer-google 'org-gnus-prefer-web-links))
ce4fdcb9
CD
44
45(defcustom org-gnus-prefer-web-links nil
20908596
CD
46 "Non-nil means, `org-store-link' will create web links to Google groups.
47When nil, Gnus will be used for such links.
48Using a prefix arg to the command \\[org-store-link] (`org-store-link')
49negates this setting for the duration of the command."
50 :group 'org-link-store
51 :type 'boolean)
52
53;; Declare external functions and variables
54(declare-function gnus-article-show-summary "gnus-art" ())
55(declare-function gnus-summary-last-subject "gnus-sum" ())
56(defvar gnus-other-frame-object)
57(defvar gnus-group-name)
58(defvar gnus-article-current)
59
60;; Install the link type
61(org-add-link-type "gnus" 'org-gnus-open)
62(add-hook 'org-store-link-functions 'org-gnus-store-link)
63
64;; Implementation
ce4fdcb9
CD
65
66(defun org-gnus-group-link (group)
67 "Create a link to the Gnus group GROUP.
68If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
69non-nil, create a link to groups.google.com or gmane.org.
70Otherwise create a link to the group inside Gnus.
71
72If `org-store-link' was called with a prefix arg the meaning of
73`org-gnus-prefer-web-links' is reversed."
74 (let ((unprefixed-group (replace-regexp-in-string "^[^:]+:" "" group)))
75 (if (and (string-match "^nntp" group) ;; Only for nntp groups
76 (org-xor current-prefix-arg
77 org-gnus-prefer-web-links))
db55f368
CD
78 (org-make-link (if (string-match "gmane" unprefixed-group)
79 "http://news.gmane.org/"
80 "http://groups.google.com/group/")
81 unprefixed-group)
82 (org-make-link "gnus:" group))))
ce4fdcb9
CD
83
84(defun org-gnus-article-link (group newsgroups message-id x-no-archive)
85 "Create a link to a Gnus article.
86The article is specified by its MESSAGE-ID. Additional
87parameters are the Gnus GROUP, the NEWSGROUPS the article was
88posted to and the X-NO-ARCHIVE header value of that article.
89
90If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
91non-nil, create a link to groups.google.com or gmane.org.
92Otherwise create a link to the article inside Gnus.
93
94If `org-store-link' was called with a prefix arg the meaning of
95`org-gnus-prefer-web-links' is reversed."
96 (if (and (org-xor current-prefix-arg org-gnus-prefer-web-links)
97 newsgroups ;; Make web links only for nntp groups
98 (not x-no-archive)) ;; and if X-No-Archive isn't set.
99 (format (if (string-match "gmane\\." newsgroups)
100 "http://mid.gmane.org/%s"
101 "http://groups.google.com/groups/search?as_umsgid=%s")
db55f368 102 (org-fixup-message-id-for-http message-id))
ce4fdcb9
CD
103 (org-make-link "gnus:" group "#" message-id)))
104
20908596
CD
105(defun org-gnus-store-link ()
106 "Store a link to a Gnus folder or message."
107 (cond
108 ((eq major-mode 'gnus-group-mode)
ce4fdcb9
CD
109 (let* ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
110 (gnus-group-group-name)) ; version
111 ((fboundp 'gnus-group-name)
112 (gnus-group-name))
113 (t "???")))
114 desc link)
0bd48b37
CD
115 (when group
116 (org-store-link-props :type "gnus" :group group)
117 (setq desc (org-gnus-group-link group)
118 link desc)
119 (org-add-link-props :link link :description desc)
120 link)))
20908596
CD
121
122 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
ce4fdcb9 123 (and (eq major-mode 'gnus-summary-mode) (gnus-summary-show-article))
20908596 124 (let* ((group gnus-newsgroup-name)
ce4fdcb9
CD
125 (header (with-current-buffer gnus-article-buffer
126 (gnus-summary-toggle-header 1)
127 (goto-char (point-min))
128 (mail-header-extract-no-properties)))
129 (from (mail-header 'from header))
db55f368
CD
130 (message-id (org-remove-angle-brackets
131 (mail-header 'message-id header)))
ce4fdcb9
CD
132 (date (mail-header 'date header))
133 (to (mail-header 'to header))
134 (newsgroups (mail-header 'newsgroups header))
135 (x-no-archive (mail-header 'x-no-archive header))
20908596
CD
136 (subject (gnus-summary-subject-string))
137 desc link)
138 (org-store-link-props :type "gnus" :from from :subject subject
621f83e4 139 :message-id message-id :group group :to to)
ce4fdcb9
CD
140 (setq desc (org-email-link-description)
141 link (org-gnus-article-link group newsgroups message-id x-no-archive))
20908596 142 (org-add-link-props :link link :description desc)
ff4be292 143 (gnus-summary-toggle-header -1)
20908596
CD
144 link))))
145
146(defun org-gnus-open (path)
147 "Follow the Gnus message or folder link specified by PATH."
148 (let (group article)
149 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
150 (error "Error in Gnus link"))
151 (setq group (match-string 1 path)
152 article (match-string 3 path))
db55f368
CD
153 (when group
154 (setq group (org-substring-no-properties group)))
155 (when article
156 (setq article (org-substring-no-properties article)))
20908596
CD
157 (org-gnus-follow-link group article)))
158
159(defun org-gnus-follow-link (&optional group article)
160 "Follow a Gnus link to GROUP and ARTICLE."
161 (require 'gnus)
162 (funcall (cdr (assq 'gnus org-link-frame-setup)))
163 (if gnus-other-frame-object (select-frame gnus-other-frame-object))
db55f368
CD
164 (when group
165 (setq group (org-substring-no-properties group)))
166 (when article
167 (setq article (org-substring-no-properties article)))
20908596 168 (cond ((and group article)
db55f368
CD
169 (gnus-activate-group group t)
170 (condition-case nil
171 (let ((articles 1)
172 group-opened)
173 (while (and (not group-opened)
174 ;; stop on integer overflows
175 (> articles 0))
176 (setq group-opened (gnus-group-read-group articles nil group)
177 articles (if (< articles 16)
178 (1+ articles)
179 (* articles 2))))
180 (if group-opened
181 (gnus-summary-goto-article article nil t)
182 (message "Couldn't follow gnus link. %s"
183 "The summary couldn't be opened.")))
184 (quit (message "Couldn't follow gnus link. %s"
185 "The linked group is empty."))))
20908596
CD
186 (group (gnus-group-jump-to-group group))))
187
93b62de8
CD
188(defun org-gnus-no-new-news ()
189 "Like `M-x gnus' but doesn't check for new news."
190 (if (not (gnus-alive-p)) (gnus)))
191
20908596
CD
192(provide 'org-gnus)
193
88ac7b50 194;; arch-tag: 512e0840-58fa-45b3-b456-71e10fa2376d
b349f79f 195
20908596 196;;; org-gnus.el ends here