* progmodes/cperl-mode.el (cperl-mode-unload-function): New function.
[bpt/emacs.git] / lisp / gnus / gnus-dired.el
CommitLineData
23f87bed
MB
1;;; gnus-dired.el --- utility functions where gnus and dired meet
2
e84b4b86 3;; Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004,
114f9c96 4;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
23f87bed
MB
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
5e809f55 12;; GNU Emacs is free software: you can redistribute it and/or modify
23f87bed 13;; it under the terms of the GNU General Public License as published by
5e809f55
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
23f87bed
MB
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
5e809f55 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23f87bed
MB
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
d029b5d2
KY
42(eval-when-compile
43 (when (featurep 'xemacs)
44 (require 'easy-mmode))) ; for `define-minor-mode'
23f87bed 45(require 'dired)
b890d447
MB
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")
50
51;; Maybe shift this function to `mailcap.el'?
52(autoload 'mm-mailcap-command "mm-decode")
53
54(autoload 'ps-print-preprint "ps-print")
55
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")
b890d447 59(autoload 'gnus-print-buffer "gnus-sum")
23f87bed 60
bbf52f1e
SM
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)
66 map))
b890d447
MB
67
68;; FIXME: Make it customizable, change the default to `mail-user-agent' when
3a0f6aac 69;; this file is renamed (e.g. to `dired-mime.el').
b890d447
MB
70
71(defcustom gnus-dired-mail-mode 'gnus-user-agent ;; mail-user-agent
72 "Your preference for a mail composition package.
73See `mail-user-agent' for more information."
74 :group 'mail ;; dired?
330f707b 75 :version "23.1" ;; No Gnus
b890d447
MB
76 :type '(radio (function-item :tag "Default Emacs mail"
77 :format "%t\n"
78 sendmail-user-agent)
79 (function-item :tag "Emacs interface to MH"
80 :format "%t\n"
81 mh-e-user-agent)
82 (function-item :tag "Gnus Message package"
83 :format "%t\n"
84 message-user-agent)
85 (function-item :tag "Gnus Message with full Gnus features"
86 :format "%t\n"
87 gnus-user-agent)
88 (function :tag "Other")))
23f87bed 89
bbf52f1e 90(define-minor-mode gnus-dired-mode
23f87bed
MB
91 "Minor mode for intersections of gnus and dired.
92
93\\{gnus-dired-mode-map}"
bbf52f1e
SM
94 :keymap gnus-dired-mode-map
95 (unless (derived-mode-p 'dired-mode)
96 (setq gnus-dired-mode nil)))
23f87bed
MB
97
98;;;###autoload
99(defun turn-on-gnus-dired-mode ()
100 "Convenience method to turn on gnus-dired-mode."
b890d447 101 (interactive)
23f87bed
MB
102 (gnus-dired-mode 1))
103
b890d447
MB
104(defun gnus-dired-mail-buffers ()
105 "Return a list of active mail composition buffers."
106 (if (and (memq gnus-dired-mail-mode '(message-user-agent gnus-user-agent))
107 (require 'message)
108 (fboundp 'message-buffers))
109 (message-buffers)
110 ;; Cf. `message-buffers' in `message.el':
111 (let (buffers)
112 (save-excursion
113 (dolist (buffer (buffer-list t))
114 (set-buffer buffer)
115 (when (eq major-mode 'mail-mode)
116 (push (buffer-name buffer) buffers))))
117 (nreverse buffers))))
118
119;; Method to attach files to a mail composition.
23f87bed
MB
120(defun gnus-dired-attach (files-to-attach)
121 "Attach dired's marked files to a gnus message composition.
122If called non-interactively, FILES-TO-ATTACH should be a list of
123filenames."
124 (interactive
125 (list
126 (delq nil
127 (mapcar
128 ;; don't attach directories
129 (lambda (f) (if (file-directory-p f) nil f))
130 (nreverse (dired-map-over-marks (dired-get-filename) nil))))))
131 (let ((destination nil)
132 (files-str nil)
133 (bufs nil))
134 ;; warn if user tries to attach without any files marked
135 (if (null files-to-attach)
136 (error "No files to attach")
137 (setq files-str
138 (mapconcat
139 (lambda (f) (file-name-nondirectory f))
140 files-to-attach ", "))
b890d447 141 (setq bufs (gnus-dired-mail-buffers))
23f87bed 142
b890d447 143 ;; set up destination mail composition buffer
23f87bed 144 (if (and bufs
b890d447 145 (y-or-n-p "Attach files to existing mail composition buffer? "))
23f87bed
MB
146 (setq destination
147 (if (= (length bufs) 1)
148 (get-buffer (car bufs))
b890d447 149 (completing-read "Attach to which mail composition buffer: "
23f87bed
MB
150 (mapcar
151 (lambda (b)
152 (cons b (get-buffer b)))
153 bufs)
154 nil t)))
b890d447 155 ;; setup a new mail composition buffer
87202be1
KY
156 (let ((mail-user-agent gnus-dired-mail-mode)
157 ;; A workaround to prevent Gnus from displaying the Gnus
158 ;; logo when invoking this command without loading Gnus.
159 ;; Gnus demonstrates it when gnus.elc is being loaded if
160 ;; a command of which the name is prefixed with "gnus"
161 ;; causes that autoloading. See the code in question,
162 ;; that is the one first found in gnus.el by performing
163 ;; `C-s this-command'.
164 (this-command (if (eq gnus-dired-mail-mode 'gnus-user-agent)
165 'gnoose-dired-attach
166 this-command)))
b890d447 167 (compose-mail))
23f87bed
MB
168 (setq destination (current-buffer)))
169
170 ;; set buffer to destination buffer, and attach files
171 (set-buffer destination)
172 (goto-char (point-max)) ;attach at end of buffer
173 (while files-to-attach
174 (mml-attach-file (car files-to-attach)
175 (or (mm-default-file-encoding (car files-to-attach))
176 "application/octet-stream") nil)
177 (setq files-to-attach (cdr files-to-attach)))
178 (message "Attached file(s) %s" files-str))))
179
180(autoload 'mailcap-parse-mailcaps "mailcap" "" t)
181
182(defun gnus-dired-find-file-mailcap (&optional file-name arg)
183 "In dired, visit FILE-NAME according to the mailcap file.
184If ARG is non-nil, open it in a new buffer."
185 (interactive (list
186 (file-name-sans-versions (dired-get-filename) t)
187 current-prefix-arg))
188 (mailcap-parse-mailcaps)
189 (if (file-exists-p file-name)
190 (let (mime-type method)
191 (if (and (not arg)
192 (not (file-directory-p file-name))
193 (string-match "\\.[^\\.]+$" file-name)
194 (setq mime-type
195 (mailcap-extension-to-mime
196 (match-string 0 file-name)))
197 (stringp
198 (setq method
199 (cdr (assoc 'viewer
200 (car (mailcap-mime-info mime-type
b890d447
MB
201 'all
202 'no-decode)))))))
23f87bed
MB
203 (let ((view-command (mm-mailcap-command method file-name nil)))
204 (message "viewing via %s" view-command)
205 (start-process "*display*"
206 nil
207 shell-file-name
208 shell-command-switch
209 view-command))
210 (find-file file-name)))
211 (if (file-symlink-p file-name)
212 (error "File is a symlink to a nonexistent target")
213 (error "File no longer exists; type `g' to update Dired buffer"))))
214
215(defun gnus-dired-print (&optional file-name print-to)
216 "In dired, print FILE-NAME according to the mailcap file.
217
218If there is no print command, print in a PostScript image. If the
219optional argument PRINT-TO is nil, send the image to the printer. If
220PRINT-TO is a string, save the PostScript image in a file with that
221name. If PRINT-TO is a number, prompt the user for the name of the
222file to save in."
223 (interactive (list
224 (file-name-sans-versions (dired-get-filename) t)
225 (ps-print-preprint current-prefix-arg)))
226 (mailcap-parse-mailcaps)
227 (cond
228 ((file-directory-p file-name)
229 (error "Can't print a directory"))
230 ((file-exists-p file-name)
231 (let (mime-type method)
232 (if (and (string-match "\\.[^\\.]+$" file-name)
233 (setq mime-type
234 (mailcap-extension-to-mime
235 (match-string 0 file-name)))
236 (stringp
b890d447
MB
237 (setq method (mailcap-mime-info mime-type "print"
238 'no-decode))))
23f87bed
MB
239 (call-process shell-file-name nil
240 (generate-new-buffer " *mm*")
241 nil
242 shell-command-switch
243 (mm-mailcap-command method file-name mime-type))
244 (with-temp-buffer
245 (insert-file-contents file-name)
b890d447
MB
246 (if (eq gnus-dired-mail-mode 'gnus-user-agent)
247 (gnus-print-buffer)
248 ;; FIXME:
249 (error "MIME print only implemeted via Gnus")))
23f87bed
MB
250 (ps-despool print-to))))
251 ((file-symlink-p file-name)
252 (error "File is a symlink to a nonexistent target"))
253 (t
254 (error "File no longer exists; type `g' to update Dired buffer"))))
255
256(provide 'gnus-dired)
257
cbee283d 258;; arch-tag: 44737731-e445-4638-a31e-713c7590ec76
23f87bed 259;;; gnus-dired.el ends here