1 ;;; gnus-dired.el --- utility functions where gnus and dired meet
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002
4 ;; Free Software Foundation, Inc.
6 ;; Authors: Benjamin Rutt <brutt@bloomington.in.us>,
7 ;; Shenghuo Zhu <zsh@cs.rochester.edu>
8 ;; Keywords: mail, news, extensions
10 ;; This file is part of GNU Emacs.
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)
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.
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.
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:
34 ;; (require 'gnus-dired) ;, isn't needed due to autoload cookies
35 ;; (add-hook 'dired-mode-hook 'turn-on-gnus-dired-mode)
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.
52 (defvar gnus-dired-mode nil
53 "Minor mode for intersections of gnus and dired.")
55 (defvar gnus-dired-mode-map nil)
57 (unless gnus-dired-mode-map
58 (setq gnus-dired-mode-map (make-sparse-keymap))
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
66 (defun gnus-dired-mode (&optional arg)
67 "Minor mode for intersections of gnus and dired.
69 \\{gnus-dired-mode-map}"
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)))
76 (gnus-add-minor-mode 'gnus-dired-mode "" gnus-dired-mode-map)
77 (gnus-run-hooks 'gnus-dired-mode-hook))))
80 (defun turn-on-gnus-dired-mode ()
81 "Convenience method to turn on gnus-dired-mode."
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
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)
99 ;; warn if user tries to attach without any files marked
100 (if (null files-to-attach)
101 (error "No files to attach")
104 (lambda (f) (file-name-nondirectory f))
105 files-to-attach ", "))
106 (setq bufs (message-buffers))
108 ;; set up destination message buffer
110 (y-or-n-p "Attach files to existing message buffer? "))
112 (if (= (length bufs) 1)
113 (get-buffer (car bufs))
114 (completing-read "Attach to which message buffer: "
117 (cons b (get-buffer b)))
120 ;; setup a new gnus message buffer
121 (gnus-setup-message 'message (message-mail))
122 (setq destination (current-buffer)))
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))))
134 (autoload 'mailcap-parse-mailcaps "mailcap" "" t)
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."
140 (file-name-sans-versions (dired-get-filename) t)
142 (mailcap-parse-mailcaps)
143 (if (file-exists-p file-name)
144 (let (mime-type method)
146 (not (file-directory-p file-name))
147 (string-match "\\.[^\\.]+$" file-name)
149 (mailcap-extension-to-mime
150 (match-string 0 file-name)))
154 (car (mailcap-mime-info mime-type
156 (let ((view-command (mm-mailcap-command method file-name nil)))
157 (message "viewing via %s" view-command)
158 (start-process "*display*"
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"))))
168 (defun gnus-dired-print (&optional file-name print-to)
169 "In dired, print FILE-NAME according to the mailcap file.
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
177 (file-name-sans-versions (dired-get-filename) t)
178 (ps-print-preprint current-prefix-arg)))
179 (mailcap-parse-mailcaps)
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)
187 (mailcap-extension-to-mime
188 (match-string 0 file-name)))
190 (setq method (mailcap-mime-info mime-type "print"))))
191 (call-process shell-file-name nil
192 (generate-new-buffer " *mm*")
195 (mm-mailcap-command method file-name mime-type))
197 (insert-file-contents file-name)
199 (ps-despool print-to))))
200 ((file-symlink-p file-name)
201 (error "File is a symlink to a nonexistent target"))
203 (error "File no longer exists; type `g' to update Dired buffer"))))
205 (provide 'gnus-dired)
207 ;;; gnus-dired.el ends here