Install org-mode version 7.3
[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
114f9c96 3;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
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
afe98dfa 10;; Version: 7.3
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)
afe98dfa 42(declare-function nnimap-group-overview-filename "nnimap" (group server))
8d642074
CD
43;; The following line suppresses a compiler warning stemming from gnus-sum.el
44(declare-function gnus-summary-last-subject "gnus-sum" nil)
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
86fbb8ca 51 "If non-nil, `org-store-link' creates web links to Google groups or Gmane.
20908596
CD
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
afe98dfa
CD
58(defcustom org-gnus-nnimap-query-article-no-from-file nil
59 "If non-nil, `org-gnus-follow-link' will try to translate
60Message-Ids to article numbers by querying the .overview file.
61Normally, this translation is done by querying the IMAP server,
62which is usually very fast. Unfortunately, some (maybe badly
63configured) IMAP servers don't support this operation quickly.
64So if following a link to a Gnus article takes ages, try setting
65this variable to `t'."
66 :group 'org-link-store
67 :type 'boolean)
68
20908596
CD
69
70;; Install the link type
71(org-add-link-type "gnus" 'org-gnus-open)
72(add-hook 'org-store-link-functions 'org-gnus-store-link)
73
74;; Implementation
ce4fdcb9 75
afe98dfa
CD
76(defun org-gnus-nnimap-cached-article-number (group server message-id)
77 "Return cached article number (uid) of message in GROUP on SERVER.
78MESSAGE-ID is the message-id header field that identifies the
79message. If the uid is not cached, return nil."
80 (with-temp-buffer
81 (let ((nov (nnimap-group-overview-filename group server)))
82 (when (file-exists-p nov)
83 (mm-insert-file-contents nov)
84 (set-buffer-modified-p nil)
85 (goto-char (point-min))
86 (catch 'found
87 (while (search-forward message-id nil t)
88 (let ((hdr (split-string (thing-at-point 'line) "\t")))
89 (if (string= (nth 4 hdr) message-id)
90 (throw 'found (nth 0 hdr))))))))))
91
ce4fdcb9
CD
92(defun org-gnus-group-link (group)
93 "Create a link to the Gnus group GROUP.
94If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
95non-nil, create a link to groups.google.com or gmane.org.
96Otherwise create a link to the group inside Gnus.
97
98If `org-store-link' was called with a prefix arg the meaning of
99`org-gnus-prefer-web-links' is reversed."
100 (let ((unprefixed-group (replace-regexp-in-string "^[^:]+:" "" group)))
101 (if (and (string-match "^nntp" group) ;; Only for nntp groups
102 (org-xor current-prefix-arg
103 org-gnus-prefer-web-links))
db55f368
CD
104 (org-make-link (if (string-match "gmane" unprefixed-group)
105 "http://news.gmane.org/"
106 "http://groups.google.com/group/")
107 unprefixed-group)
108 (org-make-link "gnus:" group))))
ce4fdcb9
CD
109
110(defun org-gnus-article-link (group newsgroups message-id x-no-archive)
111 "Create a link to a Gnus article.
112The article is specified by its MESSAGE-ID. Additional
113parameters are the Gnus GROUP, the NEWSGROUPS the article was
114posted to and the X-NO-ARCHIVE header value of that article.
115
116If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
117non-nil, create a link to groups.google.com or gmane.org.
118Otherwise create a link to the article inside Gnus.
119
120If `org-store-link' was called with a prefix arg the meaning of
121`org-gnus-prefer-web-links' is reversed."
122 (if (and (org-xor current-prefix-arg org-gnus-prefer-web-links)
123 newsgroups ;; Make web links only for nntp groups
124 (not x-no-archive)) ;; and if X-No-Archive isn't set.
125 (format (if (string-match "gmane\\." newsgroups)
126 "http://mid.gmane.org/%s"
127 "http://groups.google.com/groups/search?as_umsgid=%s")
db55f368 128 (org-fixup-message-id-for-http message-id))
ce4fdcb9
CD
129 (org-make-link "gnus:" group "#" message-id)))
130
20908596
CD
131(defun org-gnus-store-link ()
132 "Store a link to a Gnus folder or message."
133 (cond
134 ((eq major-mode 'gnus-group-mode)
ce4fdcb9
CD
135 (let* ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
136 (gnus-group-group-name)) ; version
137 ((fboundp 'gnus-group-name)
138 (gnus-group-name))
139 (t "???")))
140 desc link)
0bd48b37
CD
141 (when group
142 (org-store-link-props :type "gnus" :group group)
143 (setq desc (org-gnus-group-link group)
144 link desc)
145 (org-add-link-props :link link :description desc)
146 link)))
20908596
CD
147
148 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
20908596 149 (let* ((group gnus-newsgroup-name)
86fbb8ca 150 (header (with-current-buffer gnus-summary-buffer
54a0dee5
CD
151 (gnus-summary-article-header)))
152 (from (mail-header-from header))
153 (message-id (org-remove-angle-brackets (mail-header-id header)))
154 (date (mail-header-date header))
afe98dfa
CD
155 (date-ts (and date (format-time-string
156 (org-time-stamp-format t) (date-to-time date))))
157 (date-ts-ia (and date (format-time-string
158 (org-time-stamp-format t t)
159 (date-to-time date))))
86fbb8ca
CD
160 (subject (copy-sequence (mail-header-subject header)))
161 (to (cdr (assq 'To (mail-header-extra header))))
162 newsgroups x-no-archive desc link)
163 ;; Remove text properties of subject string to avoid Emacs bug
164 ;; #3506
165 (set-text-properties 0 (length subject) nil subject)
166
54a0dee5
CD
167 ;; Fetching an article is an expensive operation; newsgroup and
168 ;; x-no-archive are only needed for web links.
169 (when (org-xor current-prefix-arg org-gnus-prefer-web-links)
86fbb8ca
CD
170 ;; Make sure the original article buffer is up-to-date
171 (save-window-excursion (gnus-summary-select-article))
172 (setq to (or to (gnus-fetch-original-field "To"))
173 newsgroups (gnus-fetch-original-field "Newsgroups")
174 x-no-archive (gnus-fetch-original-field "x-no-archive")))
afe98dfa 175 (org-store-link-props :type "gnus" :from from :subject subject
621f83e4 176 :message-id message-id :group group :to to)
afe98dfa
CD
177 (when date
178 (org-add-link-props :date date :date-timestamp date-ts
179 :date-timestamp-inactive date-ts-ia))
ce4fdcb9 180 (setq desc (org-email-link-description)
54a0dee5
CD
181 link (org-gnus-article-link
182 group newsgroups message-id x-no-archive))
20908596
CD
183 (org-add-link-props :link link :description desc)
184 link))))
185
afe98dfa
CD
186(defun org-gnus-open-nntp (path)
187 "Follow the nntp: link specified by PATH."
188 (let* ((spec (split-string path "/"))
189 (server (split-string (nth 2 spec) "@"))
190 (group (nth 3 spec))
191 (article (nth 4 spec)))
192 (org-gnus-follow-link
193 (format "nntp+%s:%s" (or (cdr server) (car server)) group)
194 article)))
195
20908596
CD
196(defun org-gnus-open (path)
197 "Follow the Gnus message or folder link specified by PATH."
198 (let (group article)
199 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
200 (error "Error in Gnus link"))
201 (setq group (match-string 1 path)
202 article (match-string 3 path))
db55f368
CD
203 (when group
204 (setq group (org-substring-no-properties group)))
205 (when article
206 (setq article (org-substring-no-properties article)))
20908596
CD
207 (org-gnus-follow-link group article)))
208
209(defun org-gnus-follow-link (&optional group article)
210 "Follow a Gnus link to GROUP and ARTICLE."
211 (require 'gnus)
212 (funcall (cdr (assq 'gnus org-link-frame-setup)))
213 (if gnus-other-frame-object (select-frame gnus-other-frame-object))
db55f368
CD
214 (when group
215 (setq group (org-substring-no-properties group)))
216 (when article
217 (setq article (org-substring-no-properties article)))
20908596 218 (cond ((and group article)
db55f368
CD
219 (gnus-activate-group group t)
220 (condition-case nil
afe98dfa
CD
221 (let* ((method (gnus-find-method-for-group group))
222 (backend (car method))
223 (server (cadr method)))
ed21c5c8
CD
224 (cond
225 ((eq backend 'nndoc)
226 (if (gnus-group-read-group t nil group)
227 (gnus-summary-goto-article article nil t)
228 (message "Couldn't follow gnus link. %s"
229 "The summary couldn't be opened.")))
230 (t
231 (let ((articles 1)
232 group-opened)
afe98dfa
CD
233 (when (and (eq backend 'nnimap)
234 org-gnus-nnimap-query-article-no-from-file)
235 (setq article
236 (or (org-gnus-nnimap-cached-article-number
237 (nth 1 (split-string group ":"))
238 server (concat "<" article ">")) article)))
ed21c5c8
CD
239 (while (and (not group-opened)
240 ;; stop on integer overflows
241 (> articles 0))
242 (setq group-opened (gnus-group-read-group
243 articles nil group)
244 articles (if (< articles 16)
245 (1+ articles)
246 (* articles 2))))
247 (if group-opened
248 (gnus-summary-goto-article article nil t)
249 (message "Couldn't follow gnus link. %s"
250 "The summary couldn't be opened."))))))
db55f368
CD
251 (quit (message "Couldn't follow gnus link. %s"
252 "The linked group is empty."))))
20908596
CD
253 (group (gnus-group-jump-to-group group))))
254
93b62de8
CD
255(defun org-gnus-no-new-news ()
256 "Like `M-x gnus' but doesn't check for new news."
257 (if (not (gnus-alive-p)) (gnus)))
258
20908596
CD
259(provide 'org-gnus)
260
88ac7b50 261;; arch-tag: 512e0840-58fa-45b3-b456-71e10fa2376d
b349f79f 262
20908596 263;;; org-gnus.el ends here