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-print-buffer
"gnus-sum")
58 (defvar gnus-dired-mode nil
59 "Minor mode for intersections of MIME mail composition and dired.")
61 (defvar gnus-dired-mode-map nil
)
63 (unless gnus-dired-mode-map
64 (setq gnus-dired-mode-map
(make-sparse-keymap))
66 (define-key gnus-dired-mode-map
"\C-c\C-m\C-a" 'gnus-dired-attach
)
67 (define-key gnus-dired-mode-map
"\C-c\C-m\C-l" 'gnus-dired-find-file-mailcap
)
68 (define-key gnus-dired-mode-map
"\C-c\C-m\C-p" 'gnus-dired-print
))
70 ;; FIXME: Make it customizable, change the default to `mail-user-agent' when
71 ;; this file is renamed (e.g. to `dired-mime.el').
73 (defcustom gnus-dired-mail-mode
'gnus-user-agent
;; mail-user-agent
74 "Your preference for a mail composition package.
75 See `mail-user-agent' for more information."
76 :group
'mail
;; dired?
77 :version
"23.1" ;; No Gnus
78 :type
'(radio (function-item :tag
"Default Emacs mail"
81 (function-item :tag
"Emacs interface to MH"
84 (function-item :tag
"Gnus Message package"
87 (function-item :tag
"Gnus Message with full Gnus features"
90 (function :tag
"Other")))
92 (defun gnus-dired-mode (&optional arg
)
93 "Minor mode for intersections of gnus and dired.
95 \\{gnus-dired-mode-map}"
97 (when (eq major-mode
'dired-mode
)
98 (set (make-local-variable 'gnus-dired-mode
)
99 (if (null arg
) (not gnus-dired-mode
)
100 (> (prefix-numeric-value arg
) 0)))
101 (when gnus-dired-mode
102 (add-minor-mode 'gnus-dired-mode
"" gnus-dired-mode-map
)
104 (run-hooks 'gnus-dired-mode-hook
)))))
107 (defun turn-on-gnus-dired-mode ()
108 "Convenience method to turn on gnus-dired-mode."
112 (defun gnus-dired-mail-buffers ()
113 "Return a list of active mail composition buffers."
114 (if (and (memq gnus-dired-mail-mode
'(message-user-agent gnus-user-agent
))
116 (fboundp 'message-buffers
))
118 ;; Cf. `message-buffers' in `message.el':
121 (dolist (buffer (buffer-list t
))
123 (when (eq major-mode
'mail-mode
)
124 (push (buffer-name buffer
) buffers
))))
125 (nreverse buffers
))))
127 ;; Method to attach files to a mail composition.
128 (defun gnus-dired-attach (files-to-attach)
129 "Attach dired's marked files to a gnus message composition.
130 If called non-interactively, FILES-TO-ATTACH should be a list of
136 ;; don't attach directories
137 (lambda (f) (if (file-directory-p f
) nil f
))
138 (nreverse (dired-map-over-marks (dired-get-filename) nil
))))))
139 (let ((destination nil
)
142 ;; warn if user tries to attach without any files marked
143 (if (null files-to-attach
)
144 (error "No files to attach")
147 (lambda (f) (file-name-nondirectory f
))
148 files-to-attach
", "))
149 (setq bufs
(gnus-dired-mail-buffers))
151 ;; set up destination mail composition buffer
153 (y-or-n-p "Attach files to existing mail composition buffer? "))
155 (if (= (length bufs
) 1)
156 (get-buffer (car bufs
))
157 (completing-read "Attach to which mail composition buffer: "
160 (cons b
(get-buffer b
)))
163 ;; setup a new mail composition buffer
164 (let ((mail-user-agent gnus-dired-mail-mode
)
165 ;; A workaround to prevent Gnus from displaying the Gnus
166 ;; logo when invoking this command without loading Gnus.
167 ;; Gnus demonstrates it when gnus.elc is being loaded if
168 ;; a command of which the name is prefixed with "gnus"
169 ;; causes that autoloading. See the code in question,
170 ;; that is the one first found in gnus.el by performing
171 ;; `C-s this-command'.
172 (this-command (if (eq gnus-dired-mail-mode
'gnus-user-agent
)
176 (setq destination
(current-buffer)))
178 ;; set buffer to destination buffer, and attach files
179 (set-buffer destination
)
180 (goto-char (point-max)) ;attach at end of buffer
181 (while files-to-attach
182 (mml-attach-file (car files-to-attach
)
183 (or (mm-default-file-encoding (car files-to-attach
))
184 "application/octet-stream") nil
)
185 (setq files-to-attach
(cdr files-to-attach
)))
186 (message "Attached file(s) %s" files-str
))))
188 (autoload 'mailcap-parse-mailcaps
"mailcap" "" t
)
190 (defun gnus-dired-find-file-mailcap (&optional file-name arg
)
191 "In dired, visit FILE-NAME according to the mailcap file.
192 If ARG is non-nil, open it in a new buffer."
194 (file-name-sans-versions (dired-get-filename) t
)
196 (mailcap-parse-mailcaps)
197 (if (file-exists-p file-name
)
198 (let (mime-type method
)
200 (not (file-directory-p file-name
))
201 (string-match "\\.[^\\.]+$" file-name
)
203 (mailcap-extension-to-mime
204 (match-string 0 file-name
)))
208 (car (mailcap-mime-info mime-type
211 (let ((view-command (mm-mailcap-command method file-name nil
)))
212 (message "viewing via %s" view-command
)
213 (start-process "*display*"
218 (find-file file-name
)))
219 (if (file-symlink-p file-name
)
220 (error "File is a symlink to a nonexistent target")
221 (error "File no longer exists; type `g' to update Dired buffer"))))
223 (defun gnus-dired-print (&optional file-name print-to
)
224 "In dired, print FILE-NAME according to the mailcap file.
226 If there is no print command, print in a PostScript image. If the
227 optional argument PRINT-TO is nil, send the image to the printer. If
228 PRINT-TO is a string, save the PostScript image in a file with that
229 name. If PRINT-TO is a number, prompt the user for the name of the
232 (file-name-sans-versions (dired-get-filename) t
)
233 (ps-print-preprint current-prefix-arg
)))
234 (mailcap-parse-mailcaps)
236 ((file-directory-p file-name
)
237 (error "Can't print a directory"))
238 ((file-exists-p file-name
)
239 (let (mime-type method
)
240 (if (and (string-match "\\.[^\\.]+$" file-name
)
242 (mailcap-extension-to-mime
243 (match-string 0 file-name
)))
245 (setq method
(mailcap-mime-info mime-type
"print"
247 (call-process shell-file-name nil
248 (generate-new-buffer " *mm*")
251 (mm-mailcap-command method file-name mime-type
))
253 (insert-file-contents file-name
)
254 (if (eq gnus-dired-mail-mode
'gnus-user-agent
)
257 (error "MIME print only implemeted via Gnus")))
258 (ps-despool print-to
))))
259 ((file-symlink-p file-name
)
260 (error "File is a symlink to a nonexistent target"))
262 (error "File no longer exists; type `g' to update Dired buffer"))))
264 (provide 'gnus-dired
)
266 ;; arch-tag: 44737731-e445-4638-a31e-713c7590ec76
267 ;;; gnus-dired.el ends here