1 ;;; gnus-dired.el --- utility functions where gnus and dired meet
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 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 3 of the License, or
15 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
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:
32 ;; (require 'gnus-dired) ;, isn't needed due to autoload cookies
33 ;; (add-hook 'dired-mode-hook 'turn-on-gnus-dired-mode)
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.
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")
48 ;; Maybe shift this function to `mailcap.el'?
49 (autoload 'mm-mailcap-command
"mm-decode")
51 (autoload 'ps-print-preprint
"ps-print")
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")
58 (defvar gnus-dired-mode-map
59 (let ((map (make-sparse-keymap)))
60 (define-key map
"\C-c\C-m\C-a" 'gnus-dired-attach
)
61 (define-key map
"\C-c\C-m\C-l" 'gnus-dired-find-file-mailcap
)
62 (define-key map
"\C-c\C-m\C-p" 'gnus-dired-print
)
65 ;; FIXME: Make it customizable, change the default to `mail-user-agent' when
66 ;; this file is renamed (e.g. to `dired-mime.el').
68 (defcustom gnus-dired-mail-mode
'gnus-user-agent
;; mail-user-agent
69 "Your preference for a mail composition package.
70 See `mail-user-agent' for more information."
71 :group
'mail
;; dired?
72 :version
"23.1" ;; No Gnus
73 :type
'(radio (function-item :tag
"Default Emacs mail"
76 (function-item :tag
"Emacs interface to MH"
79 (function-item :tag
"Gnus Message package"
82 (function-item :tag
"Gnus Message with full Gnus features"
85 (function :tag
"Other")))
87 (define-minor-mode gnus-dired-mode
88 "Minor mode for intersections of gnus and dired.
90 \\{gnus-dired-mode-map}"
91 :keymap gnus-dired-mode-map
92 (unless (derived-mode-p 'dired-mode
)
93 (setq gnus-dired-mode nil
)))
96 (defun turn-on-gnus-dired-mode ()
97 "Convenience method to turn on gnus-dired-mode."
101 (defun gnus-dired-mail-buffers ()
102 "Return a list of active mail composition buffers."
103 (if (and (memq gnus-dired-mail-mode
'(message-user-agent gnus-user-agent
))
105 (fboundp 'message-buffers
))
107 ;; Cf. `message-buffers' in `message.el':
110 (dolist (buffer (buffer-list t
))
112 (when (eq major-mode
'mail-mode
)
113 (push (buffer-name buffer
) buffers
))))
114 (nreverse buffers
))))
116 ;; Method to attach files to a mail composition.
117 (defun gnus-dired-attach (files-to-attach)
118 "Attach dired's marked files to a gnus message composition.
119 If called non-interactively, FILES-TO-ATTACH should be a list of
125 ;; don't attach directories
126 (lambda (f) (if (file-directory-p f
) nil f
))
127 (nreverse (dired-map-over-marks (dired-get-filename) nil
))))))
128 (let ((destination nil
)
131 ;; warn if user tries to attach without any files marked
132 (if (null files-to-attach
)
133 (error "No files to attach")
136 (lambda (f) (file-name-nondirectory f
))
137 files-to-attach
", "))
138 (setq bufs
(gnus-dired-mail-buffers))
140 ;; set up destination mail composition buffer
142 (y-or-n-p "Attach files to existing mail composition buffer? "))
144 (if (= (length bufs
) 1)
145 (get-buffer (car bufs
))
146 (completing-read "Attach to which mail composition buffer: "
149 (cons b
(get-buffer b
)))
152 ;; setup a new mail composition buffer
153 (let ((mail-user-agent gnus-dired-mail-mode
)
154 ;; A workaround to prevent Gnus from displaying the Gnus
155 ;; logo when invoking this command without loading Gnus.
156 ;; Gnus demonstrates it when gnus.elc is being loaded if
157 ;; a command of which the name is prefixed with "gnus"
158 ;; causes that autoloading. See the code in question,
159 ;; that is the one first found in gnus.el by performing
160 ;; `C-s this-command'.
161 (this-command (if (eq gnus-dired-mail-mode
'gnus-user-agent
)
165 (setq destination
(current-buffer)))
167 ;; set buffer to destination buffer, and attach files
168 (set-buffer destination
)
169 (goto-char (point-max)) ;attach at end of buffer
170 (while files-to-attach
171 (mml-attach-file (car files-to-attach
)
172 (or (mm-default-file-encoding (car files-to-attach
))
173 "application/octet-stream") nil
)
174 (setq files-to-attach
(cdr files-to-attach
)))
175 (message "Attached file(s) %s" files-str
))))
177 (autoload 'mailcap-parse-mailcaps
"mailcap" "" t
)
179 (defun gnus-dired-find-file-mailcap (&optional file-name arg
)
180 "In dired, visit FILE-NAME according to the mailcap file.
181 If ARG is non-nil, open it in a new buffer."
183 (file-name-sans-versions (dired-get-filename) t
)
185 (mailcap-parse-mailcaps)
186 (if (file-exists-p file-name
)
187 (let (mime-type method
)
189 (not (file-directory-p file-name
))
190 (string-match "\\.[^\\.]+$" file-name
)
192 (mailcap-extension-to-mime
193 (match-string 0 file-name
)))
197 (car (mailcap-mime-info mime-type
200 (let ((view-command (mm-mailcap-command method file-name nil
)))
201 (message "viewing via %s" view-command
)
202 (start-process "*display*"
207 (find-file file-name
)))
208 (if (file-symlink-p file-name
)
209 (error "File is a symlink to a nonexistent target")
210 (error "File no longer exists; type `g' to update Dired buffer"))))
212 (defun gnus-dired-print (&optional file-name print-to
)
213 "In dired, print FILE-NAME according to the mailcap file.
215 If there is no print command, print in a PostScript image. If the
216 optional argument PRINT-TO is nil, send the image to the printer. If
217 PRINT-TO is a string, save the PostScript image in a file with that
218 name. If PRINT-TO is a number, prompt the user for the name of the
221 (file-name-sans-versions (dired-get-filename) t
)
222 (ps-print-preprint current-prefix-arg
)))
223 (mailcap-parse-mailcaps)
225 ((file-directory-p file-name
)
226 (error "Can't print a directory"))
227 ((file-exists-p file-name
)
228 (let (mime-type method
)
229 (if (and (string-match "\\.[^\\.]+$" file-name
)
231 (mailcap-extension-to-mime
232 (match-string 0 file-name
)))
234 (setq method
(mailcap-mime-info mime-type
"print"
236 (call-process shell-file-name nil
237 (generate-new-buffer " *mm*")
240 (mm-mailcap-command method file-name mime-type
))
242 (insert-file-contents file-name
)
243 (if (eq gnus-dired-mail-mode
'gnus-user-agent
)
246 (error "MIME print only implemeted via Gnus")))
247 (ps-despool print-to
))))
248 ((file-symlink-p file-name
)
249 (error "File is a symlink to a nonexistent target"))
251 (error "File no longer exists; type `g' to update Dired buffer"))))
253 (provide 'gnus-dired
)
255 ;; arch-tag: 44737731-e445-4638-a31e-713c7590ec76
256 ;;; gnus-dired.el ends here