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 (when (featurep 'xemacs
)
44 (require 'easy-mmode
))) ; for `define-minor-mode'
46 (autoload 'mml-attach-file
"mml")
47 (autoload 'mm-default-file-encoding
"mm-decode");; Shift this to `mailcap.el'?
48 (autoload 'mailcap-extension-to-mime
"mailcap")
49 (autoload 'mailcap-mime-info
"mailcap")
51 ;; Maybe shift this function to `mailcap.el'?
52 (autoload 'mm-mailcap-command
"mm-decode")
54 (autoload 'ps-print-preprint
"ps-print")
56 ;; Autoloads to avoid byte-compiler warnings. These are used only if the user
57 ;; customizes `gnus-dired-mail-mode' to use Message and/or Gnus.
58 (autoload 'message-buffers
"message")
59 (autoload 'gnus-print-buffer
"gnus-sum")
61 (defvar gnus-dired-mode-map
62 (let ((map (make-sparse-keymap)))
63 (define-key map
"\C-c\C-m\C-a" 'gnus-dired-attach
)
64 (define-key map
"\C-c\C-m\C-l" 'gnus-dired-find-file-mailcap
)
65 (define-key map
"\C-c\C-m\C-p" 'gnus-dired-print
)
68 ;; FIXME: Make it customizable, change the default to `mail-user-agent' when
69 ;; this file is renamed (e.g. to `dired-mime.el').
71 (defcustom gnus-dired-mail-mode
'gnus-user-agent
;; mail-user-agent
72 "Your preference for a mail composition package.
73 See `mail-user-agent' for more information."
74 :group
'mail
;; dired?
75 :version
"23.1" ;; No Gnus
76 :type
'(radio (function-item :tag
"Default Emacs mail"
79 (function-item :tag
"Emacs interface to MH"
82 (function-item :tag
"Gnus Message package"
85 (function-item :tag
"Gnus Message with full Gnus features"
88 (function :tag
"Other")))
91 (when (featurep 'xemacs
)
92 (defvar gnus-dired-mode-hook
)
93 (defvar gnus-dired-mode-on-hook
)
94 (defvar gnus-dired-mode-off-hook
)))
96 (define-minor-mode gnus-dired-mode
97 "Minor mode for intersections of gnus and dired.
99 \\{gnus-dired-mode-map}"
100 :keymap gnus-dired-mode-map
101 (unless (derived-mode-p 'dired-mode
)
102 (setq gnus-dired-mode nil
)))
105 (defun turn-on-gnus-dired-mode ()
106 "Convenience method to turn on gnus-dired-mode."
110 (defun gnus-dired-mail-buffers ()
111 "Return a list of active mail composition buffers."
112 (if (and (memq gnus-dired-mail-mode
'(message-user-agent gnus-user-agent
))
114 (fboundp 'message-buffers
))
116 ;; Cf. `message-buffers' in `message.el':
119 (dolist (buffer (buffer-list t
))
121 (when (eq major-mode
'mail-mode
)
122 (push (buffer-name buffer
) buffers
))))
123 (nreverse buffers
))))
125 ;; Method to attach files to a mail composition.
126 (defun gnus-dired-attach (files-to-attach)
127 "Attach dired's marked files to a gnus message composition.
128 If called non-interactively, FILES-TO-ATTACH should be a list of
134 ;; don't attach directories
135 (lambda (f) (if (file-directory-p f
) nil f
))
136 (nreverse (dired-map-over-marks (dired-get-filename) nil
))))))
137 (let ((destination nil
)
140 ;; warn if user tries to attach without any files marked
141 (if (null files-to-attach
)
142 (error "No files to attach")
145 (lambda (f) (file-name-nondirectory f
))
146 files-to-attach
", "))
147 (setq bufs
(gnus-dired-mail-buffers))
149 ;; set up destination mail composition buffer
151 (y-or-n-p "Attach files to existing mail composition buffer? "))
153 (if (= (length bufs
) 1)
154 (get-buffer (car bufs
))
155 (completing-read "Attach to which mail composition buffer: "
158 (cons b
(get-buffer b
)))
161 ;; setup a new mail composition buffer
162 (let ((mail-user-agent gnus-dired-mail-mode
)
163 ;; A workaround to prevent Gnus from displaying the Gnus
164 ;; logo when invoking this command without loading Gnus.
165 ;; Gnus demonstrates it when gnus.elc is being loaded if
166 ;; a command of which the name is prefixed with "gnus"
167 ;; causes that autoloading. See the code in question,
168 ;; that is the one first found in gnus.el by performing
169 ;; `C-s this-command'.
170 (this-command (if (eq gnus-dired-mail-mode
'gnus-user-agent
)
174 (setq destination
(current-buffer)))
176 ;; set buffer to destination buffer, and attach files
177 (set-buffer destination
)
178 (goto-char (point-max)) ;attach at end of buffer
179 (while files-to-attach
180 (mml-attach-file (car files-to-attach
)
181 (or (mm-default-file-encoding (car files-to-attach
))
182 "application/octet-stream") nil
)
183 (setq files-to-attach
(cdr files-to-attach
)))
184 (message "Attached file(s) %s" files-str
))))
186 (autoload 'mailcap-parse-mailcaps
"mailcap" "" t
)
188 (defun gnus-dired-find-file-mailcap (&optional file-name arg
)
189 "In dired, visit FILE-NAME according to the mailcap file.
190 If ARG is non-nil, open it in a new buffer."
192 (file-name-sans-versions (dired-get-filename) t
)
194 (mailcap-parse-mailcaps)
195 (if (file-exists-p file-name
)
196 (let (mime-type method
)
198 (not (file-directory-p file-name
))
199 (string-match "\\.[^\\.]+$" file-name
)
201 (mailcap-extension-to-mime
202 (match-string 0 file-name
)))
206 (car (mailcap-mime-info mime-type
209 (let ((view-command (mm-mailcap-command method file-name nil
)))
210 (message "viewing via %s" view-command
)
211 (start-process "*display*"
216 (find-file file-name
)))
217 (if (file-symlink-p file-name
)
218 (error "File is a symlink to a nonexistent target")
219 (error "File no longer exists; type `g' to update Dired buffer"))))
221 (defun gnus-dired-print (&optional file-name print-to
)
222 "In dired, print FILE-NAME according to the mailcap file.
224 If there is no print command, print in a PostScript image. If the
225 optional argument PRINT-TO is nil, send the image to the printer. If
226 PRINT-TO is a string, save the PostScript image in a file with that
227 name. If PRINT-TO is a number, prompt the user for the name of the
230 (file-name-sans-versions (dired-get-filename) t
)
231 (ps-print-preprint current-prefix-arg
)))
232 (mailcap-parse-mailcaps)
234 ((file-directory-p file-name
)
235 (error "Can't print a directory"))
236 ((file-exists-p file-name
)
237 (let (mime-type method
)
238 (if (and (string-match "\\.[^\\.]+$" file-name
)
240 (mailcap-extension-to-mime
241 (match-string 0 file-name
)))
243 (setq method
(mailcap-mime-info mime-type
"print"
245 (call-process shell-file-name nil
246 (generate-new-buffer " *mm*")
249 (mm-mailcap-command method file-name mime-type
))
251 (insert-file-contents file-name
)
252 (if (eq gnus-dired-mail-mode
'gnus-user-agent
)
255 (error "MIME print only implemeted via Gnus")))
256 (ps-despool print-to
))))
257 ((file-symlink-p file-name
)
258 (error "File is a symlink to a nonexistent target"))
260 (error "File no longer exists; type `g' to update Dired buffer"))))
262 (provide 'gnus-dired
)
264 ;; arch-tag: 44737731-e445-4638-a31e-713c7590ec76
265 ;;; gnus-dired.el ends here