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