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 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-setup-message
"gnus-msg")
57 (autoload 'gnus-print-buffer
"gnus-sum")
59 (defvar gnus-dired-mode nil
60 "Minor mode for intersections of MIME mail composition and dired.")
62 (defvar gnus-dired-mode-map nil
)
64 (unless gnus-dired-mode-map
65 (setq gnus-dired-mode-map
(make-sparse-keymap))
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
))
71 ;; FIXME: Make it customizable, change the default to `mail-user-agent' when
72 ;; this file is renamed (e.g. to `dired-mime.el').
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"
82 (function-item :tag
"Emacs interface to MH"
85 (function-item :tag
"Gnus Message package"
88 (function-item :tag
"Gnus Message with full Gnus features"
91 (function :tag
"Other")))
93 (defun gnus-dired-mode (&optional arg
)
94 "Minor mode for intersections of gnus and dired.
96 \\{gnus-dired-mode-map}"
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
)
105 (run-hooks 'gnus-dired-mode-hook
)))))
108 (defun turn-on-gnus-dired-mode ()
109 "Convenience method to turn on gnus-dired-mode."
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
))
117 (fboundp 'message-buffers
))
119 ;; Cf. `message-buffers' in `message.el':
122 (dolist (buffer (buffer-list t
))
124 (when (eq major-mode
'mail-mode
)
125 (push (buffer-name buffer
) buffers
))))
126 (nreverse buffers
))))
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
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
)
143 ;; warn if user tries to attach without any files marked
144 (if (null files-to-attach
)
145 (error "No files to attach")
148 (lambda (f) (file-name-nondirectory f
))
149 files-to-attach
", "))
150 (setq bufs
(gnus-dired-mail-buffers))
152 ;; set up destination mail composition buffer
154 (y-or-n-p "Attach files to existing mail composition buffer? "))
156 (if (= (length bufs
) 1)
157 (get-buffer (car bufs
))
158 (completing-read "Attach to which mail composition buffer: "
161 (cons b
(get-buffer b
)))
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?
169 (setq destination
(current-buffer)))
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
))))
181 (autoload 'mailcap-parse-mailcaps
"mailcap" "" t
)
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."
187 (file-name-sans-versions (dired-get-filename) t
)
189 (mailcap-parse-mailcaps)
190 (if (file-exists-p file-name
)
191 (let (mime-type method
)
193 (not (file-directory-p file-name
))
194 (string-match "\\.[^\\.]+$" file-name
)
196 (mailcap-extension-to-mime
197 (match-string 0 file-name
)))
201 (car (mailcap-mime-info mime-type
204 (let ((view-command (mm-mailcap-command method file-name nil
)))
205 (message "viewing via %s" view-command
)
206 (start-process "*display*"
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"))))
216 (defun gnus-dired-print (&optional file-name print-to
)
217 "In dired, print FILE-NAME according to the mailcap file.
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
225 (file-name-sans-versions (dired-get-filename) t
)
226 (ps-print-preprint current-prefix-arg
)))
227 (mailcap-parse-mailcaps)
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
)
235 (mailcap-extension-to-mime
236 (match-string 0 file-name
)))
238 (setq method
(mailcap-mime-info mime-type
"print"
240 (call-process shell-file-name nil
241 (generate-new-buffer " *mm*")
244 (mm-mailcap-command method file-name mime-type
))
246 (insert-file-contents file-name
)
247 (if (eq gnus-dired-mail-mode
'gnus-user-agent
)
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"))
255 (error "File no longer exists; type `g' to update Dired buffer"))))
257 (provide 'gnus-dired
)
259 ;; arch-tag: 44737731-e445-4638-a31e-713c7590ec76
260 ;;; gnus-dired.el ends here