Release 6.30c
[org-mode/org-tableheadings.git] / lisp / org-gnus.el
blob5c5bc6c07d4358b20ba35cc6514d3db3290b367f
1 ;;; org-gnus.el --- Support for links to Gnus groups and messages from within Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Tassilo Horn <tassilo at member dot fsf dot org>
8 ;; Keywords: outlines, hypermedia, calendar, wp
9 ;; Homepage: http://orgmode.org
10 ;; Version: 6.30c
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
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.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
28 ;;; Commentary:
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'.
34 ;;; Code:
36 (require 'org)
37 (eval-when-compile
38 (require 'gnus-sum))
40 ;; Customization variables
42 (when (fboundp 'defvaralias)
43 (defvaralias 'org-usenet-links-prefer-google 'org-gnus-prefer-web-links))
45 (defcustom org-gnus-prefer-web-links nil
46 "Non-nil means, `org-store-link' will create web links to Google groups.
47 When nil, Gnus will be used for such links.
48 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
49 negates this setting for the duration of the command."
50 :group 'org-link-store
51 :type 'boolean)
53 ;; Declare external functions and variables
54 (declare-function gnus-summary-article-header "gnus-sum" (&optional number))
55 (declare-function message-fetch-field "message" (header &optional not-all))
56 (declare-function message-narrow-to-head-1 "message" nil)
58 (defvar gnus-other-frame-object)
59 (defvar gnus-group-name)
60 (defvar gnus-article-current)
62 ;; Install the link type
63 (org-add-link-type "gnus" 'org-gnus-open)
64 (add-hook 'org-store-link-functions 'org-gnus-store-link)
66 ;; Implementation
68 (defun org-gnus-group-link (group)
69 "Create a link to the Gnus group GROUP.
70 If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
71 non-nil, create a link to groups.google.com or gmane.org.
72 Otherwise create a link to the group inside Gnus.
74 If `org-store-link' was called with a prefix arg the meaning of
75 `org-gnus-prefer-web-links' is reversed."
76 (let ((unprefixed-group (replace-regexp-in-string "^[^:]+:" "" group)))
77 (if (and (string-match "^nntp" group) ;; Only for nntp groups
78 (org-xor current-prefix-arg
79 org-gnus-prefer-web-links))
80 (org-make-link (if (string-match "gmane" unprefixed-group)
81 "http://news.gmane.org/"
82 "http://groups.google.com/group/")
83 unprefixed-group)
84 (org-make-link "gnus:" group))))
86 (defun org-gnus-article-link (group newsgroups message-id x-no-archive)
87 "Create a link to a Gnus article.
88 The article is specified by its MESSAGE-ID. Additional
89 parameters are the Gnus GROUP, the NEWSGROUPS the article was
90 posted to and the X-NO-ARCHIVE header value of that article.
92 If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
93 non-nil, create a link to groups.google.com or gmane.org.
94 Otherwise create a link to the article inside Gnus.
96 If `org-store-link' was called with a prefix arg the meaning of
97 `org-gnus-prefer-web-links' is reversed."
98 (if (and (org-xor current-prefix-arg org-gnus-prefer-web-links)
99 newsgroups ;; Make web links only for nntp groups
100 (not x-no-archive)) ;; and if X-No-Archive isn't set.
101 (format (if (string-match "gmane\\." newsgroups)
102 "http://mid.gmane.org/%s"
103 "http://groups.google.com/groups/search?as_umsgid=%s")
104 (org-fixup-message-id-for-http message-id))
105 (org-make-link "gnus:" group "#" message-id)))
107 (defun org-gnus-store-link ()
108 "Store a link to a Gnus folder or message."
109 (cond
110 ((eq major-mode 'gnus-group-mode)
111 (let* ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
112 (gnus-group-group-name)) ; version
113 ((fboundp 'gnus-group-name)
114 (gnus-group-name))
115 (t "???")))
116 desc link)
117 (when group
118 (org-store-link-props :type "gnus" :group group)
119 (setq desc (org-gnus-group-link group)
120 link desc)
121 (org-add-link-props :link link :description desc)
122 link)))
124 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
125 (let* ((group gnus-newsgroup-name)
126 (header (with-current-buffer gnus-summary-buffer
127 (gnus-summary-article-header)))
128 (from (mail-header-from header))
129 (message-id (org-remove-angle-brackets (mail-header-id header)))
130 (date (mail-header-date header))
131 (subject (mail-header-subject header))
132 (to (cdr (assq 'To (mail-header-extra header))))
133 newsgroups x-no-archive desc link)
134 ;; Fetching an article is an expensive operation; newsgroup and
135 ;; x-no-archive are only needed for web links.
136 (when (org-xor current-prefix-arg org-gnus-prefer-web-links)
137 ;; Make sure the original article buffer is up-to-date
138 (save-window-excursion (gnus-summary-select-article))
139 (setq to (or to (gnus-fetch-original-field "To"))
140 newsgroups (gnus-fetch-original-field "Newsgroups")
141 x-no-archive (gnus-fetch-original-field "x-no-archive")))
142 (org-store-link-props :type "gnus" :from from :subject subject
143 :message-id message-id :group group :to to)
144 (setq desc (org-email-link-description)
145 link (org-gnus-article-link
146 group newsgroups message-id x-no-archive))
147 (org-add-link-props :link link :description desc)
148 link))))
150 (defun org-gnus-open (path)
151 "Follow the Gnus message or folder link specified by PATH."
152 (let (group article)
153 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
154 (error "Error in Gnus link"))
155 (setq group (match-string 1 path)
156 article (match-string 3 path))
157 (when group
158 (setq group (org-substring-no-properties group)))
159 (when article
160 (setq article (org-substring-no-properties article)))
161 (org-gnus-follow-link group article)))
163 (defun org-gnus-follow-link (&optional group article)
164 "Follow a Gnus link to GROUP and ARTICLE."
165 (require 'gnus)
166 (funcall (cdr (assq 'gnus org-link-frame-setup)))
167 (if gnus-other-frame-object (select-frame gnus-other-frame-object))
168 (when group
169 (setq group (org-substring-no-properties group)))
170 (when article
171 (setq article (org-substring-no-properties article)))
172 (cond ((and group article)
173 (gnus-activate-group group t)
174 (condition-case nil
175 (let ((articles 1)
176 group-opened)
177 (while (and (not group-opened)
178 ;; stop on integer overflows
179 (> articles 0))
180 (setq group-opened (gnus-group-read-group articles nil group)
181 articles (if (< articles 16)
182 (1+ articles)
183 (* articles 2))))
184 (if group-opened
185 (gnus-summary-goto-article article nil t)
186 (message "Couldn't follow gnus link. %s"
187 "The summary couldn't be opened.")))
188 (quit (message "Couldn't follow gnus link. %s"
189 "The linked group is empty."))))
190 (group (gnus-group-jump-to-group group))))
192 (defun org-gnus-no-new-news ()
193 "Like `M-x gnus' but doesn't check for new news."
194 (if (not (gnus-alive-p)) (gnus)))
196 (provide 'org-gnus)
198 ;; arch-tag: 512e0840-58fa-45b3-b456-71e10fa2376d
200 ;;; org-gnus.el ends here