Use define-minor-mode in Gnus where applicable.
[bpt/emacs.git] / lisp / gnus / gnus-dired.el
1 ;;; gnus-dired.el --- utility functions where gnus and dired meet
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; Authors: Benjamin Rutt <brutt@bloomington.in.us>,
7 ;; Shenghuo Zhu <zsh@cs.rochester.edu>
8 ;; Keywords: mail, news, extensions
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25 ;;; Commentary:
26
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:
31
32 ;; (require 'gnus-dired) ;, isn't needed due to autoload cookies
33 ;; (add-hook 'dired-mode-hook 'turn-on-gnus-dired-mode)
34
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.
39
40 ;;; Code:
41
42 (require 'dired)
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")
47
48 ;; Maybe shift this function to `mailcap.el'?
49 (autoload 'mm-mailcap-command "mm-decode")
50
51 (autoload 'ps-print-preprint "ps-print")
52
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")
57
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)
63 map))
64
65 ;; FIXME: Make it customizable, change the default to `mail-user-agent' when
66 ;; this file is renamed (e.g. to `dired-mime.el').
67
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"
74 :format "%t\n"
75 sendmail-user-agent)
76 (function-item :tag "Emacs interface to MH"
77 :format "%t\n"
78 mh-e-user-agent)
79 (function-item :tag "Gnus Message package"
80 :format "%t\n"
81 message-user-agent)
82 (function-item :tag "Gnus Message with full Gnus features"
83 :format "%t\n"
84 gnus-user-agent)
85 (function :tag "Other")))
86
87 (define-minor-mode gnus-dired-mode
88 "Minor mode for intersections of gnus and dired.
89
90 \\{gnus-dired-mode-map}"
91 :keymap gnus-dired-mode-map
92 (unless (derived-mode-p 'dired-mode)
93 (setq gnus-dired-mode nil)))
94
95 ;;;###autoload
96 (defun turn-on-gnus-dired-mode ()
97 "Convenience method to turn on gnus-dired-mode."
98 (interactive)
99 (gnus-dired-mode 1))
100
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))
104 (require 'message)
105 (fboundp 'message-buffers))
106 (message-buffers)
107 ;; Cf. `message-buffers' in `message.el':
108 (let (buffers)
109 (save-excursion
110 (dolist (buffer (buffer-list t))
111 (set-buffer buffer)
112 (when (eq major-mode 'mail-mode)
113 (push (buffer-name buffer) buffers))))
114 (nreverse buffers))))
115
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
120 filenames."
121 (interactive
122 (list
123 (delq nil
124 (mapcar
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)
129 (files-str nil)
130 (bufs nil))
131 ;; warn if user tries to attach without any files marked
132 (if (null files-to-attach)
133 (error "No files to attach")
134 (setq files-str
135 (mapconcat
136 (lambda (f) (file-name-nondirectory f))
137 files-to-attach ", "))
138 (setq bufs (gnus-dired-mail-buffers))
139
140 ;; set up destination mail composition buffer
141 (if (and bufs
142 (y-or-n-p "Attach files to existing mail composition buffer? "))
143 (setq destination
144 (if (= (length bufs) 1)
145 (get-buffer (car bufs))
146 (completing-read "Attach to which mail composition buffer: "
147 (mapcar
148 (lambda (b)
149 (cons b (get-buffer b)))
150 bufs)
151 nil t)))
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)
162 'gnoose-dired-attach
163 this-command)))
164 (compose-mail))
165 (setq destination (current-buffer)))
166
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))))
176
177 (autoload 'mailcap-parse-mailcaps "mailcap" "" t)
178
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."
182 (interactive (list
183 (file-name-sans-versions (dired-get-filename) t)
184 current-prefix-arg))
185 (mailcap-parse-mailcaps)
186 (if (file-exists-p file-name)
187 (let (mime-type method)
188 (if (and (not arg)
189 (not (file-directory-p file-name))
190 (string-match "\\.[^\\.]+$" file-name)
191 (setq mime-type
192 (mailcap-extension-to-mime
193 (match-string 0 file-name)))
194 (stringp
195 (setq method
196 (cdr (assoc 'viewer
197 (car (mailcap-mime-info mime-type
198 'all
199 'no-decode)))))))
200 (let ((view-command (mm-mailcap-command method file-name nil)))
201 (message "viewing via %s" view-command)
202 (start-process "*display*"
203 nil
204 shell-file-name
205 shell-command-switch
206 view-command))
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"))))
211
212 (defun gnus-dired-print (&optional file-name print-to)
213 "In dired, print FILE-NAME according to the mailcap file.
214
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
219 file to save in."
220 (interactive (list
221 (file-name-sans-versions (dired-get-filename) t)
222 (ps-print-preprint current-prefix-arg)))
223 (mailcap-parse-mailcaps)
224 (cond
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)
230 (setq mime-type
231 (mailcap-extension-to-mime
232 (match-string 0 file-name)))
233 (stringp
234 (setq method (mailcap-mime-info mime-type "print"
235 'no-decode))))
236 (call-process shell-file-name nil
237 (generate-new-buffer " *mm*")
238 nil
239 shell-command-switch
240 (mm-mailcap-command method file-name mime-type))
241 (with-temp-buffer
242 (insert-file-contents file-name)
243 (if (eq gnus-dired-mail-mode 'gnus-user-agent)
244 (gnus-print-buffer)
245 ;; FIXME:
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"))
250 (t
251 (error "File no longer exists; type `g' to update Dired buffer"))))
252
253 (provide 'gnus-dired)
254
255 ;; arch-tag: 44737731-e445-4638-a31e-713c7590ec76
256 ;;; gnus-dired.el ends here