Indent.
[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
4 ;;        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 2, 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., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, 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 (require 'gnus-ems)
46 (require 'gnus-msg)
47 (require 'gnus-util)
48 (require 'message)
49 (require 'mm-encode)
50 (require 'mml)
51
52 (defvar gnus-dired-mode nil
53   "Minor mode for intersections of gnus and dired.")
54
55 (defvar gnus-dired-mode-map nil)
56
57 (unless gnus-dired-mode-map
58   (setq gnus-dired-mode-map (make-sparse-keymap))
59
60   (gnus-define-keys gnus-dired-mode-map
61     "\C-c\C-a" gnus-dired-attach
62     "\C-c\C-f" gnus-dired-find-file-mailcap
63     "\C-cP" gnus-dired-print
64     ))
65
66 (defun gnus-dired-mode (&optional arg)
67   "Minor mode for intersections of gnus and dired.
68
69 \\{gnus-dired-mode-map}"
70   (interactive "P")
71   (when (eq major-mode 'dired-mode)
72     (set (make-local-variable 'gnus-dired-mode)
73          (if (null arg) (not gnus-dired-mode)
74            (> (prefix-numeric-value arg) 0)))
75     (when gnus-dired-mode
76       (gnus-add-minor-mode 'gnus-dired-mode "" gnus-dired-mode-map)
77       (gnus-run-hooks 'gnus-dired-mode-hook))))
78
79 ;;;###autoload
80 (defun turn-on-gnus-dired-mode ()
81   "Convenience method to turn on gnus-dired-mode."
82   (gnus-dired-mode 1))
83
84 ;; Method to attach files to a gnus composition.
85 (defun gnus-dired-attach (files-to-attach)
86   "Attach dired's marked files to a gnus message composition.
87 If called non-interactively, FILES-TO-ATTACH should be a list of
88 filenames."
89   (interactive
90    (list
91     (delq nil
92           (mapcar
93            ;; don't attach directories
94            (lambda (f) (if (file-directory-p f) nil f))
95            (nreverse (dired-map-over-marks (dired-get-filename) nil))))))
96   (let ((destination nil)
97         (files-str nil)
98         (bufs nil))
99     ;; warn if user tries to attach without any files marked
100     (if (null files-to-attach)
101         (error "No files to attach")
102       (setq files-str
103             (mapconcat
104              (lambda (f) (file-name-nondirectory f))
105              files-to-attach ", "))
106       (setq bufs (message-buffers))
107
108       ;; set up destination message buffer
109       (if (and bufs
110                (y-or-n-p "Attach files to existing message buffer? "))
111           (setq destination
112                 (if (= (length bufs) 1)
113                     (get-buffer (car bufs))
114                   (completing-read "Attach to which message buffer: "
115                                    (mapcar
116                                     (lambda (b)
117                                       (cons b (get-buffer b)))
118                                     bufs)
119                                    nil t)))
120         ;; setup a new gnus message buffer
121         (gnus-setup-message 'message (message-mail))
122         (setq destination (current-buffer)))
123
124       ;; set buffer to destination buffer, and attach files
125       (set-buffer destination)
126       (goto-char (point-max))           ;attach at end of buffer
127       (while files-to-attach
128         (mml-attach-file (car files-to-attach)
129                          (or (mm-default-file-encoding (car files-to-attach))
130                              "application/octet-stream") nil)
131         (setq files-to-attach (cdr files-to-attach)))
132       (message "Attached file(s) %s" files-str))))
133
134 (autoload 'mailcap-parse-mailcaps "mailcap" "" t)
135
136 (defun gnus-dired-find-file-mailcap (&optional file-name arg)
137   "In dired, visit FILE-NAME according to the mailcap file.
138 If ARG is non-nil, open it in a new buffer."
139   (interactive (list
140                 (file-name-sans-versions (dired-get-filename) t)
141                 current-prefix-arg))
142   (mailcap-parse-mailcaps)
143   (if (file-exists-p file-name)
144       (let (mime-type method)
145         (if (and (not arg)
146                  (not (file-directory-p file-name))
147                  (string-match "\\.[^\\.]+$" file-name)
148                  (setq mime-type
149                        (mailcap-extension-to-mime
150                         (match-string 0 file-name)))
151                  (stringp
152                   (setq method
153                         (cdr (assoc 'viewer
154                                     (car (mailcap-mime-info mime-type
155                                                             'all)))))))
156             (let ((view-command (mm-mailcap-command method file-name nil)))
157               (message "viewing via %s" view-command)
158               (start-process "*display*"
159                              nil
160                              shell-file-name
161                              shell-command-switch
162                              view-command))
163           (find-file file-name)))
164     (if (file-symlink-p file-name)
165         (error "File is a symlink to a nonexistent target")
166       (error "File no longer exists; type `g' to update Dired buffer"))))
167
168 (defun gnus-dired-print (&optional file-name print-to)
169   "In dired, print FILE-NAME according to the mailcap file.
170
171 If there is no print command, print in a PostScript image. If the
172 optional argument PRINT-TO is nil, send the image to the printer. If
173 PRINT-TO is a string, save the PostScript image in a file with that
174 name.  If PRINT-TO is a number, prompt the user for the name of the
175 file to save in."
176   (interactive (list
177                 (file-name-sans-versions (dired-get-filename) t)
178                 (ps-print-preprint current-prefix-arg)))
179   (mailcap-parse-mailcaps)
180   (cond
181    ((file-directory-p file-name)
182     (error "Can't print a directory"))
183    ((file-exists-p file-name)
184     (let (mime-type method)
185       (if (and (string-match "\\.[^\\.]+$" file-name)
186                (setq mime-type
187                      (mailcap-extension-to-mime
188                       (match-string 0 file-name)))
189                (stringp
190                 (setq method (mailcap-mime-info mime-type "print"))))
191           (call-process shell-file-name nil
192                         (generate-new-buffer " *mm*")
193                         nil
194                         shell-command-switch
195                         (mm-mailcap-command method file-name mime-type))
196         (with-temp-buffer
197           (insert-file-contents file-name)
198           (gnus-print-buffer))
199         (ps-despool print-to))))
200    ((file-symlink-p file-name)
201      (error "File is a symlink to a nonexistent target"))
202     (t
203      (error "File no longer exists; type `g' to update Dired buffer"))))
204
205 (provide 'gnus-dired)
206
207 ;;; gnus-dired.el ends here