]> code.delx.au - gnu-emacs/blob - lisp/gnus/gnus-dired.el
bfccda422f8879b6e5449947142237ec8dd65d9a
[gnu-emacs] / lisp / gnus / gnus-dired.el
1 ;;; gnus-dired.el --- utility functions where gnus and dired meet
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5
6 ;; Authors: Benjamin Rutt <brutt@bloomington.in.us>,
7 ;; Shenghuo Zhu <zsh@cs.rochester.edu>
8 ;; Keywords: mail, news, extensions
9
10 ;; This file is part of GNU Emacs.
11
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.
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
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This package provides utility functions for intersections of gnus
28 ;; and dired. To enable the gnus-dired-mode minor mode which will
29 ;; have the effect of installing keybindings in dired-mode, place the
30 ;; following in your ~/.gnus:
31
32 ;; (require 'gnus-dired) ;, isn't needed due to autoload cookies
33 ;; (add-hook 'dired-mode-hook 'turn-on-gnus-dired-mode)
34
35 ;; Note that if you visit dired buffers before your ~/.gnus file has
36 ;; been read, those dired buffers won't have the keybindings in
37 ;; effect. To get around that problem, you may want to add the above
38 ;; statements to your ~/.emacs instead.
39
40 ;;; Code:
41
42 (eval-when-compile
43 (when (featurep 'xemacs)
44 (require 'easy-mmode))) ; for `define-minor-mode'
45 (require 'dired)
46 (autoload 'mml-attach-file "mml")
47 (autoload 'mm-default-file-encoding "mm-decode");; Shift this to `mailcap.el'?
48 (autoload 'mailcap-extension-to-mime "mailcap")
49 (autoload 'mailcap-mime-info "mailcap")
50
51 ;; Maybe shift this function to `mailcap.el'?
52 (autoload 'mm-mailcap-command "mm-decode")
53
54 (autoload 'ps-print-preprint "ps-print")
55
56 ;; Autoloads to avoid byte-compiler warnings. These are used only if the user
57 ;; customizes `gnus-dired-mail-mode' to use Message and/or Gnus.
58 (autoload 'message-buffers "message")
59 (autoload 'gnus-print-buffer "gnus-sum")
60
61 (defvar gnus-dired-mode-map
62 (let ((map (make-sparse-keymap)))
63 (define-key map "\C-c\C-m\C-a" 'gnus-dired-attach)
64 (define-key map "\C-c\C-m\C-l" 'gnus-dired-find-file-mailcap)
65 (define-key map "\C-c\C-m\C-p" 'gnus-dired-print)
66 map))
67
68 ;; FIXME: Make it customizable, change the default to `mail-user-agent' when
69 ;; this file is renamed (e.g. to `dired-mime.el').
70
71 (defcustom gnus-dired-mail-mode 'gnus-user-agent ;; mail-user-agent
72 "Your preference for a mail composition package.
73 See `mail-user-agent' for more information."
74 :group 'mail ;; dired?
75 :version "23.1" ;; No Gnus
76 :type '(radio (function-item :tag "Default Emacs mail"
77 :format "%t\n"
78 sendmail-user-agent)
79 (function-item :tag "Emacs interface to MH"
80 :format "%t\n"
81 mh-e-user-agent)
82 (function-item :tag "Gnus Message package"
83 :format "%t\n"
84 message-user-agent)
85 (function-item :tag "Gnus Message with full Gnus features"
86 :format "%t\n"
87 gnus-user-agent)
88 (function :tag "Other")))
89
90 (eval-when-compile
91 (when (featurep 'xemacs)
92 (defvar gnus-dired-mode-hook)
93 (defvar gnus-dired-mode-on-hook)
94 (defvar gnus-dired-mode-off-hook)))
95
96 (define-minor-mode gnus-dired-mode
97 "Minor mode for intersections of gnus and dired.
98
99 \\{gnus-dired-mode-map}"
100 :keymap gnus-dired-mode-map
101 (unless (derived-mode-p 'dired-mode)
102 (setq gnus-dired-mode nil)))
103
104 ;;;###autoload
105 (defun turn-on-gnus-dired-mode ()
106 "Convenience method to turn on gnus-dired-mode."
107 (interactive)
108 (gnus-dired-mode 1))
109
110 (defun gnus-dired-mail-buffers ()
111 "Return a list of active mail composition buffers."
112 (if (and (memq gnus-dired-mail-mode '(message-user-agent gnus-user-agent))
113 (require 'message)
114 (fboundp 'message-buffers))
115 (message-buffers)
116 ;; Cf. `message-buffers' in `message.el':
117 (let (buffers)
118 (save-excursion
119 (dolist (buffer (buffer-list t))
120 (set-buffer buffer)
121 (when (eq major-mode 'mail-mode)
122 (push (buffer-name buffer) buffers))))
123 (nreverse buffers))))
124
125 (autoload 'gnus-completing-read "gnus-util")
126
127 ;; Method to attach files to a mail composition.
128 (defun gnus-dired-attach (files-to-attach)
129 "Attach dired's marked files to a gnus message composition.
130 If called non-interactively, FILES-TO-ATTACH should be a list of
131 filenames."
132 (interactive
133 (list
134 (delq nil
135 (mapcar
136 ;; don't attach directories
137 (lambda (f) (if (file-directory-p f) nil f))
138 (nreverse
139 (let ((arg nil)) ;; Silence XEmacs 21.5 when compiling.
140 (dired-map-over-marks (dired-get-filename) arg)))))))
141 (let ((destination nil)
142 (files-str nil)
143 (bufs nil))
144 ;; warn if user tries to attach without any files marked
145 (if (null files-to-attach)
146 (error "No files to attach")
147 (setq files-str
148 (mapconcat
149 (lambda (f) (file-name-nondirectory f))
150 files-to-attach ", "))
151 (setq bufs (gnus-dired-mail-buffers))
152
153 ;; set up destination mail composition buffer
154 (if (and bufs
155 (y-or-n-p "Attach files to existing mail composition buffer? "))
156 (setq destination
157 (if (= (length bufs) 1)
158 (get-buffer (car bufs))
159 (gnus-completing-read "Attach to which mail composition buffer"
160 bufs t)))
161 ;; setup a new mail composition buffer
162 (let ((mail-user-agent gnus-dired-mail-mode)
163 ;; A workaround to prevent Gnus from displaying the Gnus
164 ;; logo when invoking this command without loading Gnus.
165 ;; Gnus demonstrates it when gnus.elc is being loaded if
166 ;; a command of which the name is prefixed with "gnus"
167 ;; causes that autoloading. See the code in question,
168 ;; that is the one first found in gnus.el by performing
169 ;; `C-s this-command'.
170 (this-command (if (eq gnus-dired-mail-mode 'gnus-user-agent)
171 'gnoose-dired-attach
172 this-command)))
173 (compose-mail))
174 (setq destination (current-buffer)))
175
176 ;; set buffer to destination buffer, and attach files
177 (set-buffer destination)
178 (goto-char (point-max)) ;attach at end of buffer
179 (while files-to-attach
180 (mml-attach-file (car files-to-attach)
181 (or (mm-default-file-encoding (car files-to-attach))
182 "application/octet-stream") nil)
183 (setq files-to-attach (cdr files-to-attach)))
184 (message "Attached file(s) %s" files-str))))
185
186 (autoload 'mailcap-parse-mailcaps "mailcap" "" t)
187
188 (defun gnus-dired-find-file-mailcap (&optional file-name arg)
189 "In dired, visit FILE-NAME according to the mailcap file.
190 If ARG is non-nil, open it in a new buffer."
191 (interactive (list
192 (file-name-sans-versions (dired-get-filename) t)
193 current-prefix-arg))
194 (mailcap-parse-mailcaps)
195 (if (file-exists-p file-name)
196 (let (mime-type method)
197 (if (and (not arg)
198 (not (file-directory-p file-name))
199 (string-match "\\.[^\\.]+$" file-name)
200 (setq mime-type
201 (mailcap-extension-to-mime
202 (match-string 0 file-name)))
203 (stringp
204 (setq method
205 (cdr (assoc 'viewer
206 (car (mailcap-mime-info mime-type
207 'all
208 'no-decode)))))))
209 (let ((view-command (mm-mailcap-command method file-name nil)))
210 (message "viewing via %s" view-command)
211 (start-process "*display*"
212 nil
213 shell-file-name
214 shell-command-switch
215 view-command))
216 (find-file file-name)))
217 (if (file-symlink-p file-name)
218 (error "File is a symlink to a nonexistent target")
219 (error "File no longer exists; type `g' to update Dired buffer"))))
220
221 (defun gnus-dired-print (&optional file-name print-to)
222 "In dired, print FILE-NAME according to the mailcap file.
223
224 If there is no print command, print in a PostScript image. If the
225 optional argument PRINT-TO is nil, send the image to the printer. If
226 PRINT-TO is a string, save the PostScript image in a file with that
227 name. If PRINT-TO is a number, prompt the user for the name of the
228 file to save in."
229 (interactive (list
230 (file-name-sans-versions (dired-get-filename) t)
231 (ps-print-preprint current-prefix-arg)))
232 (mailcap-parse-mailcaps)
233 (cond
234 ((file-directory-p file-name)
235 (error "Can't print a directory"))
236 ((file-exists-p file-name)
237 (let (mime-type method)
238 (if (and (string-match "\\.[^\\.]+$" file-name)
239 (setq mime-type
240 (mailcap-extension-to-mime
241 (match-string 0 file-name)))
242 (stringp
243 (setq method (mailcap-mime-info mime-type "print"
244 'no-decode))))
245 (call-process shell-file-name nil
246 (generate-new-buffer " *mm*")
247 nil
248 shell-command-switch
249 (mm-mailcap-command method file-name mime-type))
250 (with-temp-buffer
251 (insert-file-contents file-name)
252 (if (eq gnus-dired-mail-mode 'gnus-user-agent)
253 (gnus-print-buffer)
254 ;; FIXME:
255 (error "MIME print only implemeted via Gnus")))
256 (ps-despool print-to))))
257 ((file-symlink-p file-name)
258 (error "File is a symlink to a nonexistent target"))
259 (t
260 (error "File no longer exists; type `g' to update Dired buffer"))))
261
262 (provide 'gnus-dired)
263
264 ;;; gnus-dired.el ends here