org-id.el: New option org-id-uuid-program to set uuidgen program.
[org-mode.git] / lisp / org-gnus.el
blobf034f1767c172298f1b812bbad619eb122546b18
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.28trans
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-article-show-summary "gnus-art" ())
55 (declare-function gnus-summary-last-subject "gnus-sum" ())
56 (declare-function message-fetch-field "message" (header &optional not-all))
57 (declare-function message-narrow-to-head-1 "message" nil)
59 (defvar gnus-other-frame-object)
60 (defvar gnus-group-name)
61 (defvar gnus-article-current)
63 ;; Install the link type
64 (org-add-link-type "gnus" 'org-gnus-open)
65 (add-hook 'org-store-link-functions 'org-gnus-store-link)
67 ;; Implementation
69 (defun org-gnus-group-link (group)
70 "Create a link to the Gnus group GROUP.
71 If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
72 non-nil, create a link to groups.google.com or gmane.org.
73 Otherwise create a link to the group inside Gnus.
75 If `org-store-link' was called with a prefix arg the meaning of
76 `org-gnus-prefer-web-links' is reversed."
77 (let ((unprefixed-group (replace-regexp-in-string "^[^:]+:" "" group)))
78 (if (and (string-match "^nntp" group) ;; Only for nntp groups
79 (org-xor current-prefix-arg
80 org-gnus-prefer-web-links))
81 (org-make-link (if (string-match "gmane" unprefixed-group)
82 "http://news.gmane.org/"
83 "http://groups.google.com/group/")
84 unprefixed-group)
85 (org-make-link "gnus:" group))))
87 (defun org-gnus-article-link (group newsgroups message-id x-no-archive)
88 "Create a link to a Gnus article.
89 The article is specified by its MESSAGE-ID. Additional
90 parameters are the Gnus GROUP, the NEWSGROUPS the article was
91 posted to and the X-NO-ARCHIVE header value of that article.
93 If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
94 non-nil, create a link to groups.google.com or gmane.org.
95 Otherwise create a link to the article inside Gnus.
97 If `org-store-link' was called with a prefix arg the meaning of
98 `org-gnus-prefer-web-links' is reversed."
99 (if (and (org-xor current-prefix-arg org-gnus-prefer-web-links)
100 newsgroups ;; Make web links only for nntp groups
101 (not x-no-archive)) ;; and if X-No-Archive isn't set.
102 (format (if (string-match "gmane\\." newsgroups)
103 "http://mid.gmane.org/%s"
104 "http://groups.google.com/groups/search?as_umsgid=%s")
105 (org-fixup-message-id-for-http message-id))
106 (org-make-link "gnus:" group "#" message-id)))
108 (defun org-gnus-store-link ()
109 "Store a link to a Gnus folder or message."
110 (cond
111 ((eq major-mode 'gnus-group-mode)
112 (let* ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
113 (gnus-group-group-name)) ; version
114 ((fboundp 'gnus-group-name)
115 (gnus-group-name))
116 (t "???")))
117 desc link)
118 (when group
119 (org-store-link-props :type "gnus" :group group)
120 (setq desc (org-gnus-group-link group)
121 link desc)
122 (org-add-link-props :link link :description desc)
123 link)))
125 ((memq major-mode '(gnus-summary-mode gnus-article-mode))
126 (and (eq major-mode 'gnus-summary-mode) (gnus-summary-show-article))
127 (let* ((group gnus-newsgroup-name)
128 (header (with-current-buffer gnus-article-buffer
129 (gnus-summary-toggle-header 1)
130 (goto-char (point-min))
131 ;; mbox files may contain a first line starting with
132 ;; "From" followed by a space, which cannot be parsed as
133 ;; header line, so we skip it.
134 (when (looking-at "From ")
135 (beginning-of-line 2))
136 (mail-header-extract-no-properties)))
137 (from (mail-header 'from header))
138 (message-id (org-remove-angle-brackets
139 (mail-header 'message-id header)))
140 (date (mail-header 'date header))
141 (to (mail-header 'to header))
142 (newsgroups (mail-header 'newsgroups header))
143 (x-no-archive (mail-header 'x-no-archive header))
144 (subject (if (eq major-mode 'gnus-article-mode)
145 (save-restriction
146 (require 'message)
147 (message-narrow-to-head-1)
148 (message-fetch-field "subject"))
149 (gnus-summary-subject-string)))
150 desc link)
151 (org-store-link-props :type "gnus" :from from :subject subject
152 :message-id message-id :group group :to to)
153 (setq desc (org-email-link-description)
154 link (org-gnus-article-link group newsgroups message-id x-no-archive))
155 (org-add-link-props :link link :description desc)
156 (gnus-summary-toggle-header -1)
157 link))))
159 (defun org-gnus-open (path)
160 "Follow the Gnus message or folder link specified by PATH."
161 (let (group article)
162 (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
163 (error "Error in Gnus link"))
164 (setq group (match-string 1 path)
165 article (match-string 3 path))
166 (when group
167 (setq group (org-substring-no-properties group)))
168 (when article
169 (setq article (org-substring-no-properties article)))
170 (org-gnus-follow-link group article)))
172 (defun org-gnus-follow-link (&optional group article)
173 "Follow a Gnus link to GROUP and ARTICLE."
174 (require 'gnus)
175 (funcall (cdr (assq 'gnus org-link-frame-setup)))
176 (if gnus-other-frame-object (select-frame gnus-other-frame-object))
177 (when group
178 (setq group (org-substring-no-properties group)))
179 (when article
180 (setq article (org-substring-no-properties article)))
181 (cond ((and group article)
182 (gnus-activate-group group t)
183 (condition-case nil
184 (let ((articles 1)
185 group-opened)
186 (while (and (not group-opened)
187 ;; stop on integer overflows
188 (> articles 0))
189 (setq group-opened (gnus-group-read-group articles nil group)
190 articles (if (< articles 16)
191 (1+ articles)
192 (* articles 2))))
193 (if group-opened
194 (gnus-summary-goto-article article nil t)
195 (message "Couldn't follow gnus link. %s"
196 "The summary couldn't be opened.")))
197 (quit (message "Couldn't follow gnus link. %s"
198 "The linked group is empty."))))
199 (group (gnus-group-jump-to-group group))))
201 (defun org-gnus-no-new-news ()
202 "Like `M-x gnus' but doesn't check for new news."
203 (if (not (gnus-alive-p)) (gnus)))
205 (provide 'org-gnus)
207 ;; arch-tag: 512e0840-58fa-45b3-b456-71e10fa2376d
209 ;;; org-gnus.el ends here