Add :version tag to new Org options in Emacs 24.1
[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
b73f1974 3;; Copyright (C) 2004-2012 Free Software Foundation, Inc.
20908596
CD
4
5;; Author: Carsten Dominik <carsten at orgmode dot org>
ce4fdcb9 6;; Tassilo Horn <tassilo at member dot fsf dot org>
20908596
CD
7;; Keywords: outlines, hypermedia, calendar, wp
8;; Homepage: http://orgmode.org
20908596
CD
9;;
10;; This file is part of GNU Emacs.
11;;
b1fc2b50 12;; GNU Emacs is free software: you can redistribute it and/or modify
20908596 13;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
20908596
CD
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b1fc2b50 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
20908596
CD
24;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
25;;
26;;; Commentary:
27
28;; This file implements links to Gnus groups and messages from within Org-mode.
29;; Org-mode loads this module by default - if this is not what you want,
30;; configure the variable `org-modules'.
31
32;;; Code:
33
34(require 'org)
21ee034d 35(eval-when-compile (require 'gnus-sum))
20908596 36
8d642074
CD
37;; Declare external functions and variables
38(declare-function message-fetch-field "message" (header &optional not-all))
39(declare-function message-narrow-to-head-1 "message" nil)
afe98dfa 40(declare-function nnimap-group-overview-filename "nnimap" (group server))
8d642074
CD
41;; The following line suppresses a compiler warning stemming from gnus-sum.el
42(declare-function gnus-summary-last-subject "gnus-sum" nil)
20908596
CD
43;; Customization variables
44
ce4fdcb9 45(when (fboundp 'defvaralias)
ff4be292 46 (defvaralias 'org-usenet-links-prefer-google 'org-gnus-prefer-web-links))
ce4fdcb9
CD
47
48(defcustom org-gnus-prefer-web-links nil
86fbb8ca 49 "If non-nil, `org-store-link' creates web links to Google groups or Gmane.
20908596
CD
50When nil, Gnus will be used for such links.
51Using a prefix arg to the command \\[org-store-link] (`org-store-link')
52negates this setting for the duration of the command."
53 :group 'org-link-store
54 :type 'boolean)
55
afe98dfa
CD
56(defcustom org-gnus-nnimap-query-article-no-from-file nil
57 "If non-nil, `org-gnus-follow-link' will try to translate
58Message-Ids to article numbers by querying the .overview file.
59Normally, this translation is done by querying the IMAP server,
60which is usually very fast. Unfortunately, some (maybe badly
61configured) IMAP servers don't support this operation quickly.
62So if following a link to a Gnus article takes ages, try setting
63this variable to `t'."
64 :group 'org-link-store
372d7b21 65 :version "24.1"
afe98dfa
CD
66 :type 'boolean)
67
20908596
CD
68
69;; Install the link type
70(org-add-link-type "gnus" 'org-gnus-open)
71(add-hook 'org-store-link-functions 'org-gnus-store-link)
72
73;; Implementation
ce4fdcb9 74
afe98dfa
CD
75(defun org-gnus-nnimap-cached-article-number (group server message-id)
76 "Return cached article number (uid) of message in GROUP on SERVER.
77MESSAGE-ID is the message-id header field that identifies the
78message. If the uid is not cached, return nil."
79 (with-temp-buffer
80 (let ((nov (nnimap-group-overview-filename group server)))
81 (when (file-exists-p nov)
82 (mm-insert-file-contents nov)
83 (set-buffer-modified-p nil)
84 (goto-char (point-min))
85 (catch 'found
86 (while (search-forward message-id nil t)
87 (let ((hdr (split-string (thing-at-point 'line) "\t")))
88 (if (string= (nth 4 hdr) message-id)
89 (throw 'found (nth 0 hdr))))))))))
90
ce4fdcb9
CD
91(defun org-gnus-group-link (group)
92 "Create a link to the Gnus group GROUP.
93If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
94non-nil, create a link to groups.google.com or gmane.org.
95Otherwise create a link to the group inside Gnus.
96
97If `org-store-link' was called with a prefix arg the meaning of
98`org-gnus-prefer-web-links' is reversed."
99 (let ((unprefixed-group (replace-regexp-in-string "^[^:]+:" "" group)))
100 (if (and (string-match "^nntp" group) ;; Only for nntp groups
101 (org-xor current-prefix-arg
102 org-gnus-prefer-web-links))
db55f368
CD
103 (org-make-link (if (string-match "gmane" unprefixed-group)
104 "http://news.gmane.org/"
105 "http://groups.google.com/group/")
106 unprefixed-group)
107 (org-make-link "gnus:" group))))
ce4fdcb9
CD
108
109(defun org-gnus-article-link (group newsgroups message-id x-no-archive)
110 "Create a link to a Gnus article.
111The article is specified by its MESSAGE-ID. Additional
112parameters are the Gnus GROUP, the NEWSGROUPS the article was
113posted to and the X-NO-ARCHIVE header value of that article.
114
115If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
116non-nil, create a link to groups.google.com or gmane.org.
117Otherwise create a link to the article inside Gnus.
118
119If `org-store-link' was called with a prefix arg the meaning of
120`org-gnus-prefer-web-links' is reversed."
121 (if (and (org-xor current-prefix-arg org-gnus-prefer-web-links)
122 newsgroups ;; Make web links only for nntp groups
123 (not x-no-archive)) ;; and if X-No-Archive isn't set.
124 (format (if (string-match "gmane\\." newsgroups)
125 "http://mid.gmane.org/%s"
126 "http://groups.google.com/groups/search?as_umsgid=%s")
db55f368 127 (org-fixup-message-id-for-http message-id))
ce4fdcb9
CD
128 (org-make-link "gnus:" group "#" message-id)))
129
20908596
CD
130(defun org-gnus-store-link ()
131 "Store a link to a Gnus folder or message."
132 (cond
133 ((eq major-mode 'gnus-group-mode)
ce4fdcb9
CD
134 (let* ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
135 (gnus-group-group-name)) ; version
136 ((fboundp 'gnus-group-name)
137 (gnus-group-name))
138 (t "???")))
139 desc link)
0bd48b37
CD
140 (when group
141 (org-store-link-props :type "gnus" :group group)
142 (setq desc (org-gnus-group-link group)
143 link desc)
144 (org-add-link-props :link link :description desc)
145 link)))
20908596
CD
146
147 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
20908596 148 (let* ((group gnus-newsgroup-name)
86fbb8ca 149 (header (with-current-buffer gnus-summary-buffer
54a0dee5
CD
150 (gnus-summary-article-header)))
151 (from (mail-header-from header))
152 (message-id (org-remove-angle-brackets (mail-header-id header)))
3ab2c837
BG
153 (date (org-trim (mail-header-date header)))
154 (date-ts (and date
155 (ignore-errors
156 (format-time-string
157 (org-time-stamp-format t)
158 (date-to-time date)))))
159 (date-ts-ia (and date
160 (ignore-errors
161 (format-time-string
162 (org-time-stamp-format t t)
163 (date-to-time date)))))
86fbb8ca
CD
164 (subject (copy-sequence (mail-header-subject header)))
165 (to (cdr (assq 'To (mail-header-extra header))))
166 newsgroups x-no-archive desc link)
167 ;; Remove text properties of subject string to avoid Emacs bug
168 ;; #3506
169 (set-text-properties 0 (length subject) nil subject)
170
54a0dee5
CD
171 ;; Fetching an article is an expensive operation; newsgroup and
172 ;; x-no-archive are only needed for web links.
173 (when (org-xor current-prefix-arg org-gnus-prefer-web-links)
86fbb8ca
CD
174 ;; Make sure the original article buffer is up-to-date
175 (save-window-excursion (gnus-summary-select-article))
176 (setq to (or to (gnus-fetch-original-field "To"))
177 newsgroups (gnus-fetch-original-field "Newsgroups")
178 x-no-archive (gnus-fetch-original-field "x-no-archive")))
14e1337f 179 (org-store-link-props :type "gnus" :from from :subject subject
621f83e4 180 :message-id message-id :group group :to to)
afe98dfa
CD
181 (when date
182 (org-add-link-props :date date :date-timestamp date-ts
183 :date-timestamp-inactive date-ts-ia))
ce4fdcb9 184 (setq desc (org-email-link-description)
54a0dee5
CD
185 link (org-gnus-article-link
186 group newsgroups message-id x-no-archive))
20908596 187 (org-add-link-props :link link :description desc)
3ab2c837
BG
188 link))
189 ((eq major-mode 'message-mode)
190 (setq org-store-link-plist nil) ; reset
191 (save-excursion
192 (save-restriction
193 (message-narrow-to-headers)
194 (and (not (message-fetch-field "Message-ID"))
195 (message-generate-headers '(Message-ID)))
196 (goto-char (point-min))
197 (re-search-forward "^Message-ID: *.*$" nil t)
198 (put-text-property (match-beginning 0) (match-end 0) 'message-deletable nil)
199 (let ((gcc (car (last
200 (message-unquote-tokens
201 (message-tokenize-header (mail-fetch-field "gcc" nil t) " ,")))))
202 (id (org-remove-angle-brackets (mail-fetch-field "Message-ID")))
203 (to (mail-fetch-field "To"))
204 (from (mail-fetch-field "From"))
205 (subject (mail-fetch-field "Subject"))
206 desc link
207 newsgroup xarchive) ; those are always nil for gcc
208 (and (not gcc)
209 (error "Can not create link: No Gcc header found."))
210 (org-store-link-props :type "gnus" :from from :subject subject
211 :message-id id :group gcc :to to)
212 (setq desc (org-email-link-description)
213 link (org-gnus-article-link
214 gcc newsgroup id xarchive))
215 (org-add-link-props :link link :description desc)
216 link))))))
20908596 217
afe98dfa
CD
218(defun org-gnus-open-nntp (path)
219 "Follow the nntp: link specified by PATH."
220 (let* ((spec (split-string path "/"))
221 (server (split-string (nth 2 spec) "@"))
222 (group (nth 3 spec))
223 (article (nth 4 spec)))
224 (org-gnus-follow-link
225 (format "nntp+%s:%s" (or (cdr server) (car server)) group)
226 article)))
227
20908596
CD
228(defun org-gnus-open (path)
229 "Follow the Gnus message or folder link specified by PATH."
230 (let (group article)
231 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
232 (error "Error in Gnus link"))
233 (setq group (match-string 1 path)
234 article (match-string 3 path))
db55f368
CD
235 (when group
236 (setq group (org-substring-no-properties group)))
237 (when article
238 (setq article (org-substring-no-properties article)))
20908596
CD
239 (org-gnus-follow-link group article)))
240
241(defun org-gnus-follow-link (&optional group article)
242 "Follow a Gnus link to GROUP and ARTICLE."
243 (require 'gnus)
244 (funcall (cdr (assq 'gnus org-link-frame-setup)))
245 (if gnus-other-frame-object (select-frame gnus-other-frame-object))
db55f368
CD
246 (when group
247 (setq group (org-substring-no-properties group)))
248 (when article
249 (setq article (org-substring-no-properties article)))
20908596 250 (cond ((and group article)
3ab2c837 251 (gnus-activate-group group)
db55f368 252 (condition-case nil
afe98dfa
CD
253 (let* ((method (gnus-find-method-for-group group))
254 (backend (car method))
255 (server (cadr method)))
ed21c5c8
CD
256 (cond
257 ((eq backend 'nndoc)
258 (if (gnus-group-read-group t nil group)
259 (gnus-summary-goto-article article nil t)
260 (message "Couldn't follow gnus link. %s"
261 "The summary couldn't be opened.")))
262 (t
263 (let ((articles 1)
264 group-opened)
afe98dfa
CD
265 (when (and (eq backend 'nnimap)
266 org-gnus-nnimap-query-article-no-from-file)
267 (setq article
268 (or (org-gnus-nnimap-cached-article-number
269 (nth 1 (split-string group ":"))
270 server (concat "<" article ">")) article)))
ed21c5c8
CD
271 (while (and (not group-opened)
272 ;; stop on integer overflows
273 (> articles 0))
274 (setq group-opened (gnus-group-read-group
275 articles nil group)
276 articles (if (< articles 16)
277 (1+ articles)
278 (* articles 2))))
279 (if group-opened
280 (gnus-summary-goto-article article nil t)
281 (message "Couldn't follow gnus link. %s"
282 "The summary couldn't be opened."))))))
db55f368
CD
283 (quit (message "Couldn't follow gnus link. %s"
284 "The linked group is empty."))))
20908596
CD
285 (group (gnus-group-jump-to-group group))))
286
93b62de8
CD
287(defun org-gnus-no-new-news ()
288 "Like `M-x gnus' but doesn't check for new news."
289 (if (not (gnus-alive-p)) (gnus)))
290
20908596
CD
291(provide 'org-gnus)
292
5b409b39 293
20908596 294;;; org-gnus.el ends here