Merge from emacs--devo--0
[gnus] / lisp / 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,
4 ;;   2005, 2006, 2007, 2008 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, or (at your option)
15 ;; 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; see the file COPYING.  If not, write to
24 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;; This package provides utility functions for intersections of gnus
30 ;; and dired.  To enable the gnus-dired-mode minor mode which will
31 ;; have the effect of installing keybindings in dired-mode, place the
32 ;; following in your ~/.gnus:
33
34 ;; (require 'gnus-dired) ;, isn't needed due to autoload cookies
35 ;; (add-hook 'dired-mode-hook 'turn-on-gnus-dired-mode)
36
37 ;; Note that if you visit dired buffers before your ~/.gnus file has
38 ;; been read, those dired buffers won't have the keybindings in
39 ;; effect.  To get around that problem, you may want to add the above
40 ;; statements to your ~/.emacs instead.
41
42 ;;; Code:
43
44 (require 'dired)
45 (autoload 'mml-attach-file "mml")
46 (autoload 'mm-default-file-encoding "mm-decode");; Shift this to `mailcap.el'?
47 (autoload 'mailcap-extension-to-mime "mailcap")
48 (autoload 'mailcap-mime-info "mailcap")
49
50 ;; Maybe shift this function to `mailcap.el'?
51 (autoload 'mm-mailcap-command "mm-decode")
52
53 (autoload 'ps-print-preprint "ps-print")
54
55 ;; Autoloads to avoid byte-compiler warnings.  These are used only if the user
56 ;; customizes `gnus-dired-mail-mode' to use Message and/or Gnus.
57 (autoload 'message-buffers "message")
58 (autoload 'gnus-setup-message "gnus-msg")
59 (autoload 'gnus-print-buffer "gnus-sum")
60
61 (defvar gnus-dired-mode nil
62   "Minor mode for intersections of MIME mail composition and dired.")
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
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
74 ;; this file if renamed (e.g. to `dired-mime.el').
75
76 (defcustom gnus-dired-mail-mode 'gnus-user-agent ;; mail-user-agent
77   "Your preference for a mail composition package.
78 See `mail-user-agent' for more information."
79   :group 'mail ;; dired?
80   :version "23.0" ;; No Gnus
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")))
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
105       (add-minor-mode 'gnus-dired-mode "" gnus-dired-mode-map)
106       (save-current-buffer
107         (run-hooks 'gnus-dired-mode-hook)))))
108
109 ;;;###autoload
110 (defun turn-on-gnus-dired-mode ()
111   "Convenience method to turn on gnus-dired-mode."
112   (interactive)
113   (gnus-dired-mode 1))
114
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.
131 (defun gnus-dired-attach (files-to-attach)
132   "Attach dired's marked files to a gnus message composition.
133 If called non-interactively, FILES-TO-ATTACH should be a list of
134 filenames."
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 ", "))
152       (setq bufs (gnus-dired-mail-buffers))
153
154       ;; set up destination mail composition buffer
155       (if (and bufs
156                (y-or-n-p "Attach files to existing mail composition buffer? "))
157           (setq destination
158                 (if (= (length bufs) 1)
159                     (get-buffer (car bufs))
160                   (completing-read "Attach to which mail composition buffer: "
161                                    (mapcar
162                                     (lambda (b)
163                                       (cons b (get-buffer b)))
164                                     bufs)
165                                    nil t)))
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))
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.
187 If 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
204                                                             'all 
205                                                             'no-decode)))))))
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
221 If there is no print command, print in a PostScript image. If the
222 optional argument PRINT-TO is nil, send the image to the printer. If
223 PRINT-TO is a string, save the PostScript image in a file with that
224 name.  If PRINT-TO is a number, prompt the user for the name of the
225 file 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
240                 (setq method (mailcap-mime-info mime-type "print"
241                                                 'no-decode))))
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)
249           (if (eq gnus-dired-mail-mode 'gnus-user-agent)
250               (gnus-print-buffer)
251             ;; FIXME:
252             (error "MIME print only implemeted via Gnus")))
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
261 ;;; arch-tag: 44737731-e445-4638-a31e-713c7590ec76
262 ;;; gnus-dired.el ends here