Add 2010 to copyright years.
[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, 2009, 2010 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 (require 'dired)
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")
56 (autoload 'gnus-print-buffer "gnus-sum")
57
58 (defvar gnus-dired-mode nil
59   "Minor mode for intersections of MIME mail composition and dired.")
60
61 (defvar gnus-dired-mode-map nil)
62
63 (unless gnus-dired-mode-map
64   (setq gnus-dired-mode-map (make-sparse-keymap))
65
66   (define-key gnus-dired-mode-map "\C-c\C-m\C-a" 'gnus-dired-attach)
67   (define-key gnus-dired-mode-map "\C-c\C-m\C-l" 'gnus-dired-find-file-mailcap)
68   (define-key gnus-dired-mode-map "\C-c\C-m\C-p" 'gnus-dired-print))
69
70 ;; FIXME: Make it customizable, change the default to `mail-user-agent' when
71 ;; this file is renamed (e.g. to `dired-mime.el').
72
73 (defcustom gnus-dired-mail-mode 'gnus-user-agent ;; mail-user-agent
74   "Your preference for a mail composition package.
75 See `mail-user-agent' for more information."
76   :group 'mail ;; dired?
77   :version "23.1" ;; No Gnus
78   :type '(radio (function-item :tag "Default Emacs mail"
79                                :format "%t\n"
80                                sendmail-user-agent)
81                 (function-item :tag "Emacs interface to MH"
82                                :format "%t\n"
83                                mh-e-user-agent)
84                 (function-item :tag "Gnus Message package"
85                                :format "%t\n"
86                                message-user-agent)
87                 (function-item :tag "Gnus Message with full Gnus features"
88                                :format "%t\n"
89                                gnus-user-agent)
90                 (function :tag "Other")))
91
92 (defun gnus-dired-mode (&optional arg)
93   "Minor mode for intersections of gnus and dired.
94
95 \\{gnus-dired-mode-map}"
96   (interactive "P")
97   (when (eq major-mode 'dired-mode)
98     (set (make-local-variable 'gnus-dired-mode)
99          (if (null arg) (not gnus-dired-mode)
100            (> (prefix-numeric-value arg) 0)))
101     (when gnus-dired-mode
102       (add-minor-mode 'gnus-dired-mode "" gnus-dired-mode-map)
103       (save-current-buffer
104         (run-hooks 'gnus-dired-mode-hook)))))
105
106 ;;;###autoload
107 (defun turn-on-gnus-dired-mode ()
108   "Convenience method to turn on gnus-dired-mode."
109   (interactive)
110   (gnus-dired-mode 1))
111
112 (defun gnus-dired-mail-buffers ()
113   "Return a list of active mail composition buffers."
114   (if (and (memq gnus-dired-mail-mode '(message-user-agent gnus-user-agent))
115            (require 'message)
116            (fboundp 'message-buffers))
117       (message-buffers)
118     ;; Cf. `message-buffers' in `message.el':
119     (let (buffers)
120       (save-excursion
121         (dolist (buffer (buffer-list t))
122           (set-buffer buffer)
123           (when (eq major-mode 'mail-mode)
124             (push (buffer-name buffer) buffers))))
125       (nreverse buffers))))
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 (dired-map-over-marks (dired-get-filename) nil))))))
139   (let ((destination nil)
140         (files-str nil)
141         (bufs nil))
142     ;; warn if user tries to attach without any files marked
143     (if (null files-to-attach)
144         (error "No files to attach")
145       (setq files-str
146             (mapconcat
147              (lambda (f) (file-name-nondirectory f))
148              files-to-attach ", "))
149       (setq bufs (gnus-dired-mail-buffers))
150
151       ;; set up destination mail composition buffer
152       (if (and bufs
153                (y-or-n-p "Attach files to existing mail composition buffer? "))
154           (setq destination
155                 (if (= (length bufs) 1)
156                     (get-buffer (car bufs))
157                   (completing-read "Attach to which mail composition buffer: "
158                                    (mapcar
159                                     (lambda (b)
160                                       (cons b (get-buffer b)))
161                                     bufs)
162                                    nil t)))
163         ;; setup a new mail composition buffer
164         (let ((mail-user-agent gnus-dired-mail-mode)
165               ;; A workaround to prevent Gnus from displaying the Gnus
166               ;; logo when invoking this command without loading Gnus.
167               ;; Gnus demonstrates it when gnus.elc is being loaded if
168               ;; a command of which the name is prefixed with "gnus"
169               ;; causes that autoloading.  See the code in question,
170               ;; that is the one first found in gnus.el by performing
171               ;; `C-s this-command'.
172               (this-command (if (eq gnus-dired-mail-mode 'gnus-user-agent)
173                                 'gnoose-dired-attach
174                               this-command)))
175           (compose-mail))
176         (setq destination (current-buffer)))
177
178       ;; set buffer to destination buffer, and attach files
179       (set-buffer destination)
180       (goto-char (point-max))           ;attach at end of buffer
181       (while files-to-attach
182         (mml-attach-file (car files-to-attach)
183                          (or (mm-default-file-encoding (car files-to-attach))
184                              "application/octet-stream") nil)
185         (setq files-to-attach (cdr files-to-attach)))
186       (message "Attached file(s) %s" files-str))))
187
188 (autoload 'mailcap-parse-mailcaps "mailcap" "" t)
189
190 (defun gnus-dired-find-file-mailcap (&optional file-name arg)
191   "In dired, visit FILE-NAME according to the mailcap file.
192 If ARG is non-nil, open it in a new buffer."
193   (interactive (list
194                 (file-name-sans-versions (dired-get-filename) t)
195                 current-prefix-arg))
196   (mailcap-parse-mailcaps)
197   (if (file-exists-p file-name)
198       (let (mime-type method)
199         (if (and (not arg)
200                  (not (file-directory-p file-name))
201                  (string-match "\\.[^\\.]+$" file-name)
202                  (setq mime-type
203                        (mailcap-extension-to-mime
204                         (match-string 0 file-name)))
205                  (stringp
206                   (setq method
207                         (cdr (assoc 'viewer
208                                     (car (mailcap-mime-info mime-type
209                                                             'all 
210                                                             'no-decode)))))))
211             (let ((view-command (mm-mailcap-command method file-name nil)))
212               (message "viewing via %s" view-command)
213               (start-process "*display*"
214                              nil
215                              shell-file-name
216                              shell-command-switch
217                              view-command))
218           (find-file file-name)))
219     (if (file-symlink-p file-name)
220         (error "File is a symlink to a nonexistent target")
221       (error "File no longer exists; type `g' to update Dired buffer"))))
222
223 (defun gnus-dired-print (&optional file-name print-to)
224   "In dired, print FILE-NAME according to the mailcap file.
225
226 If there is no print command, print in a PostScript image. If the
227 optional argument PRINT-TO is nil, send the image to the printer. If
228 PRINT-TO is a string, save the PostScript image in a file with that
229 name.  If PRINT-TO is a number, prompt the user for the name of the
230 file to save in."
231   (interactive (list
232                 (file-name-sans-versions (dired-get-filename) t)
233                 (ps-print-preprint current-prefix-arg)))
234   (mailcap-parse-mailcaps)
235   (cond
236    ((file-directory-p file-name)
237     (error "Can't print a directory"))
238    ((file-exists-p file-name)
239     (let (mime-type method)
240       (if (and (string-match "\\.[^\\.]+$" file-name)
241                (setq mime-type
242                      (mailcap-extension-to-mime
243                       (match-string 0 file-name)))
244                (stringp
245                 (setq method (mailcap-mime-info mime-type "print"
246                                                 'no-decode))))
247           (call-process shell-file-name nil
248                         (generate-new-buffer " *mm*")
249                         nil
250                         shell-command-switch
251                         (mm-mailcap-command method file-name mime-type))
252         (with-temp-buffer
253           (insert-file-contents file-name)
254           (if (eq gnus-dired-mail-mode 'gnus-user-agent)
255               (gnus-print-buffer)
256             ;; FIXME:
257             (error "MIME print only implemeted via Gnus")))
258         (ps-despool print-to))))
259    ((file-symlink-p file-name)
260      (error "File is a symlink to a nonexistent target"))
261     (t
262      (error "File no longer exists; type `g' to update Dired buffer"))))
263
264 (provide 'gnus-dired)
265
266 ;; arch-tag: 44737731-e445-4638-a31e-713c7590ec76
267 ;;; gnus-dired.el ends here