Merge branch 'maint'
[org-mode.git] / lisp / org-gnus.el
blob687bc08b16ee6b17c2d82dbfcc5392471deebb3c
1 ;;; org-gnus.el --- Support for Links to Gnus Groups and Messages -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2004-2017 Free Software Foundation, Inc.
5 ;; Author: Carsten Dominik <carsten at orgmode dot org>
6 ;; Tassilo Horn <tassilo at member dot fsf dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;;
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
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.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
24 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;; Commentary:
28 ;; This file implements links to Gnus groups and messages from within Org.
29 ;; Org mode loads this module by default - if this is not what you want,
30 ;; configure the variable `org-modules'.
32 ;;; Code:
34 (require 'org)
35 (require 'gnus-util)
38 ;;; Declare external functions and variables
40 (declare-function message-fetch-field "message" (header &optional not-all))
41 (declare-function nnvirtual-map-article "nnvirtual" (article))
44 ;;; Customization variables
46 (defcustom org-gnus-prefer-web-links nil
47 "If non-nil, `org-store-link' creates web links to Google groups or Gmane.
48 \\<org-mode-map>When nil, Gnus will be used for such links.
49 Using a prefix argument to the command `\\[org-store-link]' (`org-store-link')
50 negates this setting for the duration of the command."
51 :group 'org-link-store
52 :type 'boolean)
54 (defcustom org-gnus-no-server nil
55 "Should Gnus be started using `gnus-no-server'?"
56 :group 'org-gnus
57 :version "24.4"
58 :package-version '(Org . "8.0")
59 :type 'boolean)
62 ;;; Install the link type
64 (org-link-set-parameters "gnus"
65 :follow #'org-gnus-open
66 :store #'org-gnus-store-link)
68 ;;; Implementation
70 (defun org-gnus-group-link (group)
71 "Create a link to the Gnus group GROUP.
72 If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
73 non-nil, create a link to groups.google.com or gmane.org.
74 Otherwise create a link to the group inside Gnus.
76 If `org-store-link' was called with a prefix arg the meaning of
77 `org-gnus-prefer-web-links' is reversed."
78 (let ((unprefixed-group (replace-regexp-in-string "^[^:]+:" "" group)))
79 (if (and (string-prefix-p "nntp" group) ;; Only for nntp groups
80 (org-xor current-prefix-arg
81 org-gnus-prefer-web-links))
82 (concat (if (string-match "gmane" unprefixed-group)
83 "http://news.gmane.org/"
84 "http://groups.google.com/group/")
85 unprefixed-group)
86 (concat "gnus:" group))))
88 (defun org-gnus-article-link (group newsgroups message-id x-no-archive)
89 "Create a link to a Gnus article.
90 The article is specified by its MESSAGE-ID. Additional
91 parameters are the Gnus GROUP, the NEWSGROUPS the article was
92 posted to and the X-NO-ARCHIVE header value of that article.
94 If GROUP is a newsgroup and `org-gnus-prefer-web-links' is
95 non-nil, create a link to groups.google.com or gmane.org.
96 Otherwise create a link to the article inside Gnus.
98 If `org-store-link' was called with a prefix arg the meaning of
99 `org-gnus-prefer-web-links' is reversed."
100 (if (and (org-xor current-prefix-arg org-gnus-prefer-web-links)
101 newsgroups ;; Make web links only for nntp groups
102 (not x-no-archive)) ;; and if X-No-Archive isn't set.
103 (format (if (string-match "gmane\\." newsgroups)
104 "http://mid.gmane.org/%s"
105 "http://groups.google.com/groups/search?as_umsgid=%s")
106 (org-fixup-message-id-for-http message-id))
107 (concat "gnus:" group "#" message-id)))
109 (defun org-gnus-store-link ()
110 "Store a link to a Gnus folder or message."
111 (pcase major-mode
112 (`gnus-group-mode
113 (let ((group (gnus-group-group-name)))
114 (when group
115 (org-store-link-props :type "gnus" :group group)
116 (let ((description (org-gnus-group-link group)))
117 (org-add-link-props :link description :description description)
118 description))))
119 ((or `gnus-summary-mode `gnus-article-mode)
120 (let* ((group
121 (pcase (gnus-find-method-for-group gnus-newsgroup-name)
122 (`(nnvirtual . ,_)
123 (car (nnvirtual-map-article (gnus-summary-article-number))))
124 (`(nnir . ,_)
125 (nnir-article-group (gnus-summary-article-number)))
126 (_ gnus-newsgroup-name)))
127 (header (with-current-buffer gnus-summary-buffer
128 (gnus-summary-article-header)))
129 (from (mail-header-from header))
130 (message-id (org-unbracket-string "<" ">" (mail-header-id header)))
131 (date (org-trim (mail-header-date header)))
132 ;; Remove text properties of subject string to avoid Emacs
133 ;; bug #3506.
134 (subject (org-no-properties
135 (copy-sequence (mail-header-subject header))))
136 (to (cdr (assq 'To (mail-header-extra header))))
137 newsgroups x-no-archive)
138 ;; Fetching an article is an expensive operation; newsgroup and
139 ;; x-no-archive are only needed for web links.
140 (when (org-xor current-prefix-arg org-gnus-prefer-web-links)
141 ;; Make sure the original article buffer is up-to-date.
142 (save-window-excursion (gnus-summary-select-article))
143 (setq to (or to (gnus-fetch-original-field "To")))
144 (setq newsgroups (gnus-fetch-original-field "Newsgroups"))
145 (setq x-no-archive (gnus-fetch-original-field "x-no-archive")))
146 (org-store-link-props :type "gnus" :from from :date date :subject subject
147 :message-id message-id :group group :to to)
148 (let ((link (org-gnus-article-link
149 group newsgroups message-id x-no-archive))
150 (description (org-email-link-description)))
151 (org-add-link-props :link link :description description)
152 link)))
153 (`message-mode
154 (setq org-store-link-plist nil) ;reset
155 (save-excursion
156 (save-restriction
157 (message-narrow-to-headers)
158 (unless (message-fetch-field "Message-ID")
159 (message-generate-headers '(Message-ID)))
160 (goto-char (point-min))
161 (re-search-forward "^Message-ID:" nil t)
162 (put-text-property (line-beginning-position) (line-end-position)
163 'message-deletable nil)
164 (let ((gcc (org-last (message-unquote-tokens
165 (message-tokenize-header
166 (mail-fetch-field "gcc" nil t) " ,"))))
167 (id (org-unbracket-string "<" ">"
168 (mail-fetch-field "Message-ID")))
169 (to (mail-fetch-field "To"))
170 (from (mail-fetch-field "From"))
171 (subject (mail-fetch-field "Subject"))
172 newsgroup xarchive) ;those are always nil for gcc
173 (unless gcc (error "Can not create link: No Gcc header found"))
174 (org-store-link-props :type "gnus" :from from :subject subject
175 :message-id id :group gcc :to to)
176 (let ((link (org-gnus-article-link gcc newsgroup id xarchive))
177 (description (org-email-link-description)))
178 (org-add-link-props :link link :description description)
179 link)))))))
181 (defun org-gnus-open-nntp (path)
182 "Follow the nntp: link specified by PATH."
183 (let* ((spec (split-string path "/"))
184 (server (split-string (nth 2 spec) "@"))
185 (group (nth 3 spec))
186 (article (nth 4 spec)))
187 (org-gnus-follow-link
188 (format "nntp+%s:%s" (or (cdr server) (car server)) group)
189 article)))
191 (defun org-gnus-open (path)
192 "Follow the Gnus message or folder link specified by PATH."
193 (unless (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path)
194 (error "Error in Gnus link %S" path))
195 (let ((group (match-string-no-properties 1 path))
196 (article (match-string-no-properties 3 path)))
197 (org-gnus-follow-link group article)))
199 (defun org-gnus-follow-link (&optional group article)
200 "Follow a Gnus link to GROUP and ARTICLE."
201 (require 'gnus)
202 (funcall (cdr (assq 'gnus org-link-frame-setup)))
203 (when gnus-other-frame-object (select-frame gnus-other-frame-object))
204 (let ((group (org-no-properties group))
205 (article (org-no-properties article)))
206 (cond
207 ((and group article)
208 (gnus-activate-group group)
209 (condition-case nil
210 (let ((msg "Couldn't follow Gnus link. Summary couldn't be opened."))
211 (pcase (gnus-find-method-for-group group)
212 (`(nndoc . ,_)
213 (if (gnus-group-read-group t nil group)
214 (gnus-summary-goto-article article nil t)
215 (message msg)))
217 (let ((articles 1)
218 group-opened)
219 (while (and (not group-opened)
220 ;; Stop on integer overflows.
221 (> articles 0))
222 (setq group-opened (gnus-group-read-group articles t group))
223 (setq articles (if (< articles 16)
224 (1+ articles)
225 (* articles 2))))
226 (if group-opened
227 (gnus-summary-goto-article article nil t)
228 (message msg))))))
229 (quit
230 (message "Couldn't follow Gnus link. The linked group is empty."))))
231 (group (gnus-group-jump-to-group group)))))
233 (defun org-gnus-no-new-news ()
234 "Like `\\[gnus]' but doesn't check for new news."
235 (cond ((gnus-alive-p) nil)
236 (org-gnus-no-server (gnus-no-server))
237 (t (gnus))))
239 (provide 'org-gnus)
242 ;;; org-gnus.el ends here