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
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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'.
37 (eval-when-compile (require 'gnus-sum
))
39 ;; Customization variables
41 (when (fboundp 'defvaralias
)
42 (defvaralias 'org-usenet-links-prefer-google
'org-gnus-prefer-web-links
))
44 (defcustom org-gnus-prefer-web-links nil
45 "Non-nil means, `org-store-link' will create web links to Google groups.
46 When nil, Gnus will be used for such links.
47 Using a prefix arg to the command \\[org-store-link] (`org-store-link')
48 negates this setting for the duration of the command."
49 :group
'org-link-store
52 ;; Declare external functions and variables
54 (defvar gnus-other-frame-object
)
55 (defvar gnus-group-name
)
56 (defvar gnus-article-current
)
58 ;; Install the link type
59 (org-add-link-type "gnus" 'org-gnus-open
)
60 (add-hook 'org-store-link-functions
'org-gnus-store-link
)
64 (defun org-gnus-group-link (group)
65 "Create a link to the Gnus group GROUP.
66 If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
67 non-nil, create a link to groups.google.com or gmane.org.
68 Otherwise create a link to the group inside Gnus.
70 If `org-store-link' was called with a prefix arg the meaning of
71 `org-gnus-prefer-web-links' is reversed."
72 (let ((unprefixed-group (replace-regexp-in-string "^[^:]+:" "" group
)))
73 (if (and (string-match "^nntp" group
) ;; Only for nntp groups
74 (org-xor current-prefix-arg
75 org-gnus-prefer-web-links
))
76 (org-make-link (if (string-match "gmane" unprefixed-group
)
77 "http://news.gmane.org/"
78 "http://groups.google.com/group/")
80 (org-make-link "gnus:" group
))))
82 (defun org-gnus-article-link (group newsgroups message-id x-no-archive
)
83 "Create a link to a Gnus article.
84 The article is specified by its MESSAGE-ID. Additional
85 parameters are the Gnus GROUP, the NEWSGROUPS the article was
86 posted to and the X-NO-ARCHIVE header value of that article.
88 If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
89 non-nil, create a link to groups.google.com or gmane.org.
90 Otherwise create a link to the article inside Gnus.
92 If `org-store-link' was called with a prefix arg the meaning of
93 `org-gnus-prefer-web-links' is reversed."
94 (if (and (org-xor current-prefix-arg org-gnus-prefer-web-links
)
95 newsgroups
;; Make web links only for nntp groups
96 (not x-no-archive
)) ;; and if X-No-Archive isn't set.
97 (format (if (string-match "gmane\\." newsgroups
)
98 "http://mid.gmane.org/%s"
99 "http://groups.google.com/groups/search?as_umsgid=%s")
100 (org-fixup-message-id-for-http message-id
))
101 (org-make-link "gnus:" group
"#" message-id
)))
103 (defun org-gnus-store-link ()
104 "Store a link to a Gnus folder or message."
106 ((eq major-mode
'gnus-group-mode
)
107 (let* ((group (cond ((fboundp 'gnus-group-group-name
) ; depending on Gnus
108 (gnus-group-group-name)) ; version
109 ((fboundp 'gnus-group-name
)
114 (org-store-link-props :type
"gnus" :group group
)
115 (setq desc
(org-gnus-group-link group
)
117 (org-add-link-props :link link
:description desc
)
120 ((memq major-mode
'(gnus-summary-mode gnus-article-mode
))
121 (let* ((group gnus-newsgroup-name
)
122 (header (with-current-buffer gnus-summary-buffer
123 (gnus-summary-article-header)))
124 (from (mail-header-from header
))
125 (message-id (org-remove-angle-brackets (mail-header-id header
)))
126 (date (mail-header-date header
))
127 (subject (mail-header-subject header
))
128 (to (cdr (assq 'To
(mail-header-extra header
))))
129 newsgroups x-no-archive desc link
)
130 ;; Fetching an article is an expensive operation; newsgroup and
131 ;; x-no-archive are only needed for web links.
132 (when (org-xor current-prefix-arg org-gnus-prefer-web-links
)
133 ;; Make sure the original article buffer is up-to-date
134 (save-window-excursion (gnus-summary-select-article))
135 (setq to
(or to
(gnus-fetch-original-field "To"))
136 newsgroups
(gnus-fetch-original-field "Newsgroups")
137 x-no-archive
(gnus-fetch-original-field "x-no-archive")))
138 (org-store-link-props :type
"gnus" :from from
:subject subject
139 :message-id message-id
:group group
:to to
)
140 (setq desc
(org-email-link-description)
141 link
(org-gnus-article-link
142 group newsgroups message-id x-no-archive
))
143 (org-add-link-props :link link
:description desc
)
146 (defun org-gnus-open (path)
147 "Follow the Gnus message or folder link specified by PATH."
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
))
154 (setq group
(org-substring-no-properties group
)))
156 (setq article
(org-substring-no-properties article
)))
157 (org-gnus-follow-link group article
)))
159 (defun org-gnus-follow-link (&optional group article
)
160 "Follow a Gnus link to GROUP and ARTICLE."
162 (funcall (cdr (assq 'gnus org-link-frame-setup
)))
163 (if gnus-other-frame-object
(select-frame gnus-other-frame-object
))
165 (setq group
(org-substring-no-properties group
)))
167 (setq article
(org-substring-no-properties article
)))
168 (cond ((and group article
)
169 (gnus-activate-group group t
)
173 (while (and (not group-opened
)
174 ;; stop on integer overflows
176 (setq group-opened
(gnus-group-read-group articles nil group
)
177 articles
(if (< articles
16)
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."))))
186 (group (gnus-group-jump-to-group group
))))
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)))
194 ;; arch-tag: 512e0840-58fa-45b3-b456-71e10fa2376d
196 ;;; org-gnus.el ends here