Require gnus-msg instead of autoloading it for the gnus-setup-message macro.
[bpt/emacs.git] / lisp / gnus / gnus-dired.el
CommitLineData
23f87bed
MB
1;;; gnus-dired.el --- utility functions where gnus and dired meet
2
e84b4b86 3;; Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004,
ae940284 4;; 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
23f87bed
MB
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
5e809f55 12;; GNU Emacs is free software: you can redistribute it and/or modify
23f87bed 13;; it under the terms of the GNU General Public License as published by
5e809f55
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
23f87bed
MB
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
5e809f55 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23f87bed
MB
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(require 'dired)
b890d447
MB
43(autoload 'mml-attach-file "mml")
44(autoload 'mm-default-file-encoding "mm-decode");; Shift this to `mailcap.el'?
45(autoload 'mailcap-extension-to-mime "mailcap")
46(autoload 'mailcap-mime-info "mailcap")
47
48;; Maybe shift this function to `mailcap.el'?
49(autoload 'mm-mailcap-command "mm-decode")
50
51(autoload 'ps-print-preprint "ps-print")
52
53;; Autoloads to avoid byte-compiler warnings. These are used only if the user
54;; customizes `gnus-dired-mail-mode' to use Message and/or Gnus.
55(autoload 'message-buffers "message")
b890d447 56(autoload 'gnus-print-buffer "gnus-sum")
23f87bed 57
920b3f72
KY
58;; Load the `gnus-setup-message' macro that `gnus-dired-attach' uses.
59(require 'gnus-msg)
60
23f87bed 61(defvar gnus-dired-mode nil
b890d447 62 "Minor mode for intersections of MIME mail composition and dired.")
23f87bed
MB
63
64(defvar gnus-dired-mode-map nil)
65
66(unless gnus-dired-mode-map
67 (setq gnus-dired-mode-map (make-sparse-keymap))
68
b890d447
MB
69 (define-key gnus-dired-mode-map "\C-c\C-m\C-a" 'gnus-dired-attach)
70 (define-key gnus-dired-mode-map "\C-c\C-m\C-l" 'gnus-dired-find-file-mailcap)
71 (define-key gnus-dired-mode-map "\C-c\C-m\C-p" 'gnus-dired-print))
72
73;; FIXME: Make it customizable, change the default to `mail-user-agent' when
3a0f6aac 74;; this file is renamed (e.g. to `dired-mime.el').
b890d447
MB
75
76(defcustom gnus-dired-mail-mode 'gnus-user-agent ;; mail-user-agent
77 "Your preference for a mail composition package.
78See `mail-user-agent' for more information."
79 :group 'mail ;; dired?
330f707b 80 :version "23.1" ;; No Gnus
b890d447
MB
81 :type '(radio (function-item :tag "Default Emacs mail"
82 :format "%t\n"
83 sendmail-user-agent)
84 (function-item :tag "Emacs interface to MH"
85 :format "%t\n"
86 mh-e-user-agent)
87 (function-item :tag "Gnus Message package"
88 :format "%t\n"
89 message-user-agent)
90 (function-item :tag "Gnus Message with full Gnus features"
91 :format "%t\n"
92 gnus-user-agent)
93 (function :tag "Other")))
23f87bed
MB
94
95(defun gnus-dired-mode (&optional arg)
96 "Minor mode for intersections of gnus and dired.
97
98\\{gnus-dired-mode-map}"
99 (interactive "P")
100 (when (eq major-mode 'dired-mode)
101 (set (make-local-variable 'gnus-dired-mode)
102 (if (null arg) (not gnus-dired-mode)
103 (> (prefix-numeric-value arg) 0)))
104 (when gnus-dired-mode
01c52d31 105 (add-minor-mode 'gnus-dired-mode "" gnus-dired-mode-map)
b890d447
MB
106 (save-current-buffer
107 (run-hooks 'gnus-dired-mode-hook)))))
23f87bed
MB
108
109;;;###autoload
110(defun turn-on-gnus-dired-mode ()
111 "Convenience method to turn on gnus-dired-mode."
b890d447 112 (interactive)
23f87bed
MB
113 (gnus-dired-mode 1))
114
b890d447
MB
115(defun gnus-dired-mail-buffers ()
116 "Return a list of active mail composition buffers."
117 (if (and (memq gnus-dired-mail-mode '(message-user-agent gnus-user-agent))
118 (require 'message)
119 (fboundp 'message-buffers))
120 (message-buffers)
121 ;; Cf. `message-buffers' in `message.el':
122 (let (buffers)
123 (save-excursion
124 (dolist (buffer (buffer-list t))
125 (set-buffer buffer)
126 (when (eq major-mode 'mail-mode)
127 (push (buffer-name buffer) buffers))))
128 (nreverse buffers))))
129
130;; Method to attach files to a mail composition.
23f87bed
MB
131(defun gnus-dired-attach (files-to-attach)
132 "Attach dired's marked files to a gnus message composition.
133If called non-interactively, FILES-TO-ATTACH should be a list of
134filenames."
135 (interactive
136 (list
137 (delq nil
138 (mapcar
139 ;; don't attach directories
140 (lambda (f) (if (file-directory-p f) nil f))
141 (nreverse (dired-map-over-marks (dired-get-filename) nil))))))
142 (let ((destination nil)
143 (files-str nil)
144 (bufs nil))
145 ;; warn if user tries to attach without any files marked
146 (if (null files-to-attach)
147 (error "No files to attach")
148 (setq files-str
149 (mapconcat
150 (lambda (f) (file-name-nondirectory f))
151 files-to-attach ", "))
b890d447 152 (setq bufs (gnus-dired-mail-buffers))
23f87bed 153
b890d447 154 ;; set up destination mail composition buffer
23f87bed 155 (if (and bufs
b890d447 156 (y-or-n-p "Attach files to existing mail composition buffer? "))
23f87bed
MB
157 (setq destination
158 (if (= (length bufs) 1)
159 (get-buffer (car bufs))
b890d447 160 (completing-read "Attach to which mail composition buffer: "
23f87bed
MB
161 (mapcar
162 (lambda (b)
163 (cons b (get-buffer b)))
164 bufs)
165 nil t)))
b890d447
MB
166 ;; setup a new mail composition buffer
167 (if (eq gnus-dired-mail-mode 'gnus-user-agent)
168 (gnus-setup-message 'message (message-mail))
169 ;; FIXME: Is this the right thing?
170 (compose-mail))
23f87bed
MB
171 (setq destination (current-buffer)))
172
173 ;; set buffer to destination buffer, and attach files
174 (set-buffer destination)
175 (goto-char (point-max)) ;attach at end of buffer
176 (while files-to-attach
177 (mml-attach-file (car files-to-attach)
178 (or (mm-default-file-encoding (car files-to-attach))
179 "application/octet-stream") nil)
180 (setq files-to-attach (cdr files-to-attach)))
181 (message "Attached file(s) %s" files-str))))
182
183(autoload 'mailcap-parse-mailcaps "mailcap" "" t)
184
185(defun gnus-dired-find-file-mailcap (&optional file-name arg)
186 "In dired, visit FILE-NAME according to the mailcap file.
187If ARG is non-nil, open it in a new buffer."
188 (interactive (list
189 (file-name-sans-versions (dired-get-filename) t)
190 current-prefix-arg))
191 (mailcap-parse-mailcaps)
192 (if (file-exists-p file-name)
193 (let (mime-type method)
194 (if (and (not arg)
195 (not (file-directory-p file-name))
196 (string-match "\\.[^\\.]+$" file-name)
197 (setq mime-type
198 (mailcap-extension-to-mime
199 (match-string 0 file-name)))
200 (stringp
201 (setq method
202 (cdr (assoc 'viewer
203 (car (mailcap-mime-info mime-type
b890d447
MB
204 'all
205 'no-decode)))))))
23f87bed
MB
206 (let ((view-command (mm-mailcap-command method file-name nil)))
207 (message "viewing via %s" view-command)
208 (start-process "*display*"
209 nil
210 shell-file-name
211 shell-command-switch
212 view-command))
213 (find-file file-name)))
214 (if (file-symlink-p file-name)
215 (error "File is a symlink to a nonexistent target")
216 (error "File no longer exists; type `g' to update Dired buffer"))))
217
218(defun gnus-dired-print (&optional file-name print-to)
219 "In dired, print FILE-NAME according to the mailcap file.
220
221If there is no print command, print in a PostScript image. If the
222optional argument PRINT-TO is nil, send the image to the printer. If
223PRINT-TO is a string, save the PostScript image in a file with that
224name. If PRINT-TO is a number, prompt the user for the name of the
225file to save in."
226 (interactive (list
227 (file-name-sans-versions (dired-get-filename) t)
228 (ps-print-preprint current-prefix-arg)))
229 (mailcap-parse-mailcaps)
230 (cond
231 ((file-directory-p file-name)
232 (error "Can't print a directory"))
233 ((file-exists-p file-name)
234 (let (mime-type method)
235 (if (and (string-match "\\.[^\\.]+$" file-name)
236 (setq mime-type
237 (mailcap-extension-to-mime
238 (match-string 0 file-name)))
239 (stringp
b890d447
MB
240 (setq method (mailcap-mime-info mime-type "print"
241 'no-decode))))
23f87bed
MB
242 (call-process shell-file-name nil
243 (generate-new-buffer " *mm*")
244 nil
245 shell-command-switch
246 (mm-mailcap-command method file-name mime-type))
247 (with-temp-buffer
248 (insert-file-contents file-name)
b890d447
MB
249 (if (eq gnus-dired-mail-mode 'gnus-user-agent)
250 (gnus-print-buffer)
251 ;; FIXME:
252 (error "MIME print only implemeted via Gnus")))
23f87bed
MB
253 (ps-despool print-to))))
254 ((file-symlink-p file-name)
255 (error "File is a symlink to a nonexistent target"))
256 (t
257 (error "File no longer exists; type `g' to update Dired buffer"))))
258
259(provide 'gnus-dired)
260
cbee283d 261;; arch-tag: 44737731-e445-4638-a31e-713c7590ec76
23f87bed 262;;; gnus-dired.el ends here