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 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, 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., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, 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.
45 (autoload 'mml-attach-file
"mml")
46 (autoload 'mm-default-file-encoding
"mm-decode");; Shift this to `mailcap.el'?
47 (autoload 'mailcap-extension-to-mime
"mailcap")
48 (autoload 'mailcap-mime-info
"mailcap")
50 ;; Maybe shift this function to `mailcap.el'?
51 (autoload 'mm-mailcap-command
"mm-decode")
53 (autoload 'ps-print-preprint
"ps-print")
55 ;; Autoloads to avoid byte-compiler warnings. These are used only if the user
56 ;; customizes `gnus-dired-mail-mode' to use Message and/or Gnus.
57 (autoload 'message-buffers
"message")
58 (autoload 'gnus-setup-message
"gnus-msg")
59 (autoload 'gnus-print-buffer
"gnus-sum")
61 (defvar gnus-dired-mode nil
62 "Minor mode for intersections of MIME mail composition and dired.")
64 (defvar gnus-dired-mode-map nil
)
66 (unless gnus-dired-mode-map
67 (setq gnus-dired-mode-map
(make-sparse-keymap))
69 (define-key gnus-dired-mode-map
"\C-c\C-m\C-a" 'gnus-dired-attach
)
70 (define-key gnus-dired-mode-map
"\C-c\C-m\C-l" 'gnus-dired-find-file-mailcap
)
71 (define-key gnus-dired-mode-map
"\C-c\C-m\C-p" 'gnus-dired-print
))
73 ;; FIXME: Make it customizable, change the default to `mail-user-agent' when
74 ;; this file if renamed (e.g. to `dired-mime.el').
76 (defcustom gnus-dired-mail-mode
'gnus-user-agent
;; mail-user-agent
77 "Your preference for a mail composition package.
78 See `mail-user-agent' for more information."
79 :group
'mail
;; dired?
80 :version
"23.1" ;; No Gnus
81 :type
'(radio (function-item :tag
"Default Emacs mail"
84 (function-item :tag
"Emacs interface to MH"
87 (function-item :tag
"Gnus Message package"
90 (function-item :tag
"Gnus Message with full Gnus features"
93 (function :tag
"Other")))
95 (defun gnus-dired-mode (&optional arg
)
96 "Minor mode for intersections of gnus and dired.
98 \\{gnus-dired-mode-map}"
100 (when (eq major-mode
'dired-mode
)
101 (set (make-local-variable 'gnus-dired-mode
)
102 (if (null arg
) (not gnus-dired-mode
)
103 (> (prefix-numeric-value arg
) 0)))
104 (when gnus-dired-mode
105 (add-minor-mode 'gnus-dired-mode
"" gnus-dired-mode-map
)
107 (run-hooks 'gnus-dired-mode-hook
)))))
110 (defun turn-on-gnus-dired-mode ()
111 "Convenience method to turn on gnus-dired-mode."
115 (defun gnus-dired-mail-buffers ()
116 "Return a list of active mail composition buffers."
117 (if (and (memq gnus-dired-mail-mode
'(message-user-agent gnus-user-agent
))
119 (fboundp 'message-buffers
))
121 ;; Cf. `message-buffers' in `message.el':
124 (dolist (buffer (buffer-list t
))
126 (when (eq major-mode
'mail-mode
)
127 (push (buffer-name buffer
) buffers
))))
128 (nreverse buffers
))))
130 ;; Method to attach files to a mail composition.
131 (defun gnus-dired-attach (files-to-attach)
132 "Attach dired's marked files to a gnus message composition.
133 If called non-interactively, FILES-TO-ATTACH should be a list of
139 ;; don't attach directories
140 (lambda (f) (if (file-directory-p f
) nil f
))
141 (nreverse (dired-map-over-marks (dired-get-filename) nil
))))))
142 (let ((destination nil
)
145 ;; warn if user tries to attach without any files marked
146 (if (null files-to-attach
)
147 (error "No files to attach")
150 (lambda (f) (file-name-nondirectory f
))
151 files-to-attach
", "))
152 (setq bufs
(gnus-dired-mail-buffers))
154 ;; set up destination mail composition buffer
156 (y-or-n-p "Attach files to existing mail composition buffer? "))
158 (if (= (length bufs
) 1)
159 (get-buffer (car bufs
))
160 (completing-read "Attach to which mail composition buffer: "
163 (cons b
(get-buffer b
)))
166 ;; setup a new mail composition buffer
167 (if (eq gnus-dired-mail-mode
'gnus-user-agent
)
168 (gnus-setup-message 'message
(message-mail))
169 ;; FIXME: Is this the right thing?
171 (setq destination
(current-buffer)))
173 ;; set buffer to destination buffer, and attach files
174 (set-buffer destination
)
175 (goto-char (point-max)) ;attach at end of buffer
176 (while files-to-attach
177 (mml-attach-file (car files-to-attach
)
178 (or (mm-default-file-encoding (car files-to-attach
))
179 "application/octet-stream") nil
)
180 (setq files-to-attach
(cdr files-to-attach
)))
181 (message "Attached file(s) %s" files-str
))))
183 (autoload 'mailcap-parse-mailcaps
"mailcap" "" t
)
185 (defun gnus-dired-find-file-mailcap (&optional file-name arg
)
186 "In dired, visit FILE-NAME according to the mailcap file.
187 If ARG is non-nil, open it in a new buffer."
189 (file-name-sans-versions (dired-get-filename) t
)
191 (mailcap-parse-mailcaps)
192 (if (file-exists-p file-name
)
193 (let (mime-type method
)
195 (not (file-directory-p file-name
))
196 (string-match "\\.[^\\.]+$" file-name
)
198 (mailcap-extension-to-mime
199 (match-string 0 file-name
)))
203 (car (mailcap-mime-info mime-type
206 (let ((view-command (mm-mailcap-command method file-name nil
)))
207 (message "viewing via %s" view-command
)
208 (start-process "*display*"
213 (find-file file-name
)))
214 (if (file-symlink-p file-name
)
215 (error "File is a symlink to a nonexistent target")
216 (error "File no longer exists; type `g' to update Dired buffer"))))
218 (defun gnus-dired-print (&optional file-name print-to
)
219 "In dired, print FILE-NAME according to the mailcap file.
221 If there is no print command, print in a PostScript image. If the
222 optional argument PRINT-TO is nil, send the image to the printer. If
223 PRINT-TO is a string, save the PostScript image in a file with that
224 name. If PRINT-TO is a number, prompt the user for the name of the
227 (file-name-sans-versions (dired-get-filename) t
)
228 (ps-print-preprint current-prefix-arg
)))
229 (mailcap-parse-mailcaps)
231 ((file-directory-p file-name
)
232 (error "Can't print a directory"))
233 ((file-exists-p file-name
)
234 (let (mime-type method
)
235 (if (and (string-match "\\.[^\\.]+$" file-name
)
237 (mailcap-extension-to-mime
238 (match-string 0 file-name
)))
240 (setq method
(mailcap-mime-info mime-type
"print"
242 (call-process shell-file-name nil
243 (generate-new-buffer " *mm*")
246 (mm-mailcap-command method file-name mime-type
))
248 (insert-file-contents file-name
)
249 (if (eq gnus-dired-mail-mode
'gnus-user-agent
)
252 (error "MIME print only implemeted via Gnus")))
253 (ps-despool print-to
))))
254 ((file-symlink-p file-name
)
255 (error "File is a symlink to a nonexistent target"))
257 (error "File no longer exists; type `g' to update Dired buffer"))))
259 (provide 'gnus-dired
)
261 ;; arch-tag: 44737731-e445-4638-a31e-713c7590ec76
262 ;;; gnus-dired.el ends here