Rename comment-depth (etc.) to literal-cache (etc.). Enable it by default
[emacs.git] / lisp / gnus / gnus-dired.el
blobf7eae94a7d8be9cd1514ddb7efd92f990e060a96
1 ;;; gnus-dired.el --- utility functions where gnus and dired meet
3 ;; Copyright (C) 1996-1999, 2001-2016 Free Software Foundation, Inc.
5 ;; Authors: Benjamin Rutt <brutt@bloomington.in.us>,
6 ;; Shenghuo Zhu <zsh@cs.rochester.edu>
7 ;; Keywords: mail, news, extensions
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This package provides utility functions for intersections of gnus
27 ;; and dired. To enable the gnus-dired-mode minor mode which will
28 ;; have the effect of installing keybindings in dired-mode, place the
29 ;; following in your ~/.gnus:
31 ;; (require 'gnus-dired) ;, isn't needed due to autoload cookies
32 ;; (add-hook 'dired-mode-hook 'turn-on-gnus-dired-mode)
34 ;; Note that if you visit dired buffers before your ~/.gnus file has
35 ;; been read, those dired buffers won't have the keybindings in
36 ;; effect. To get around that problem, you may want to add the above
37 ;; statements to your ~/.emacs instead.
39 ;;; Code:
41 (require 'dired)
42 (autoload 'mml-attach-file "mml")
43 (autoload 'mm-default-file-encoding "mm-decode");; Shift this to `mailcap.el'?
44 (autoload 'mailcap-extension-to-mime "mailcap")
45 (autoload 'mailcap-mime-info "mailcap")
47 ;; Maybe shift this function to `mailcap.el'?
48 (autoload 'mm-mailcap-command "mm-decode")
50 (autoload 'ps-print-preprint "ps-print")
52 ;; Autoloads to avoid byte-compiler warnings. These are used only if the user
53 ;; customizes `gnus-dired-mail-mode' to use Message and/or Gnus.
54 (autoload 'message-buffers "message")
55 (autoload 'gnus-print-buffer "gnus-sum")
57 (defvar gnus-dired-mode-map
58 (let ((map (make-sparse-keymap)))
59 (define-key map "\C-c\C-m\C-a" 'gnus-dired-attach)
60 (define-key map "\C-c\C-m\C-l" 'gnus-dired-find-file-mailcap)
61 (define-key map "\C-c\C-m\C-p" 'gnus-dired-print)
62 map))
64 ;; FIXME: Make it customizable, change the default to `mail-user-agent' when
65 ;; this file is renamed (e.g. to `dired-mime.el').
67 (defcustom gnus-dired-mail-mode 'gnus-user-agent ;; mail-user-agent
68 "Your preference for a mail composition package.
69 See `mail-user-agent' for more information."
70 :group 'mail ;; dired?
71 :version "23.1" ;; No Gnus
72 :type '(radio (function-item :tag "Default Emacs mail"
73 :format "%t\n"
74 sendmail-user-agent)
75 (function-item :tag "Emacs interface to MH"
76 :format "%t\n"
77 mh-e-user-agent)
78 (function-item :tag "Gnus Message package"
79 :format "%t\n"
80 message-user-agent)
81 (function-item :tag "Gnus Message with full Gnus features"
82 :format "%t\n"
83 gnus-user-agent)
84 (function :tag "Other")))
86 (define-minor-mode gnus-dired-mode
87 "Minor mode for intersections of gnus and dired.
89 \\{gnus-dired-mode-map}"
90 :keymap gnus-dired-mode-map
91 (unless (derived-mode-p 'dired-mode)
92 (setq gnus-dired-mode nil)))
94 ;;;###autoload
95 (defun turn-on-gnus-dired-mode ()
96 "Convenience method to turn on gnus-dired-mode."
97 (interactive)
98 (gnus-dired-mode 1))
100 (defun gnus-dired-mail-buffers ()
101 "Return a list of active mail composition buffers."
102 (if (and (memq gnus-dired-mail-mode '(message-user-agent gnus-user-agent))
103 (require 'message)
104 (fboundp 'message-buffers))
105 (message-buffers)
106 ;; Cf. `message-buffers' in `message.el':
107 (let (buffers)
108 (save-excursion
109 (dolist (buffer (buffer-list t))
110 (set-buffer buffer)
111 (when (eq major-mode 'mail-mode)
112 (push (buffer-name buffer) buffers))))
113 (nreverse buffers))))
115 (autoload 'gnus-completing-read "gnus-util")
117 ;; Method to attach files to a mail composition.
118 (defun gnus-dired-attach (files-to-attach)
119 "Attach dired's marked files to a gnus message composition.
120 If called non-interactively, FILES-TO-ATTACH should be a list of
121 filenames."
122 (interactive
123 (list
124 (delq nil
125 (mapcar
126 ;; don't attach directories
127 (lambda (f) (if (file-directory-p f) nil f))
128 (nreverse (dired-map-over-marks (dired-get-filename) nil))))))
129 (let ((destination nil)
130 (files-str nil)
131 (bufs nil))
132 ;; warn if user tries to attach without any files marked
133 (if (null files-to-attach)
134 (error "No files to attach")
135 (setq files-str
136 (mapconcat
137 (lambda (f) (file-name-nondirectory f))
138 files-to-attach ", "))
139 (setq bufs (gnus-dired-mail-buffers))
141 ;; set up destination mail composition buffer
142 (if (and bufs
143 (y-or-n-p "Attach files to existing mail composition buffer? "))
144 (setq destination
145 (if (= (length bufs) 1)
146 (get-buffer (car bufs))
147 (gnus-completing-read "Attach to buffer"
148 bufs t nil nil (car bufs))))
149 ;; setup a new mail composition buffer
150 (let ((mail-user-agent gnus-dired-mail-mode)
151 ;; A workaround to prevent Gnus from displaying the Gnus
152 ;; logo when invoking this command without loading Gnus.
153 ;; Gnus demonstrates it when gnus.elc is being loaded if
154 ;; a command of which the name is prefixed with "gnus"
155 ;; causes that autoloading. See the code in question,
156 ;; that is the one first found in gnus.el by performing
157 ;; `C-s this-command'.
158 (this-command (if (eq gnus-dired-mail-mode 'gnus-user-agent)
159 'gnoose-dired-attach
160 this-command)))
161 (compose-mail))
162 (setq destination (current-buffer)))
164 ;; set buffer to destination buffer, and attach files
165 (set-buffer destination)
166 (goto-char (point-max)) ;attach at end of buffer
167 (while files-to-attach
168 (mml-attach-file (car files-to-attach)
169 (or (mm-default-file-encoding (car files-to-attach))
170 "application/octet-stream") nil)
171 (setq files-to-attach (cdr files-to-attach)))
172 (message "Attached file(s) %s" files-str))))
174 (autoload 'mailcap-parse-mailcaps "mailcap" "" t)
176 (defun gnus-dired-find-file-mailcap (&optional file-name arg)
177 "In dired, visit FILE-NAME according to the mailcap file.
178 If ARG is non-nil, open it in a new buffer."
179 (interactive (list
180 (file-name-sans-versions (dired-get-filename) t)
181 current-prefix-arg))
182 (mailcap-parse-mailcaps)
183 (if (file-exists-p file-name)
184 (let (mime-type method)
185 (if (and (not arg)
186 (not (file-directory-p file-name))
187 (string-match "\\.[^\\.]+$" file-name)
188 (setq mime-type
189 (mailcap-extension-to-mime
190 (match-string 0 file-name)))
191 (stringp
192 (setq method
193 (cdr (assoc 'viewer
194 (car (mailcap-mime-info mime-type
195 'all
196 'no-decode)))))))
197 (let ((view-command (mm-mailcap-command method file-name nil)))
198 (message "viewing via %s" view-command)
199 (start-process "*display*"
201 shell-file-name
202 shell-command-switch
203 view-command))
204 (find-file file-name)))
205 (if (file-symlink-p file-name)
206 (error "File is a symlink to a nonexistent target")
207 (error "File no longer exists; type `g' to update Dired buffer"))))
209 (defun gnus-dired-print (&optional file-name print-to)
210 "In dired, print FILE-NAME according to the mailcap file.
212 If there is no print command, print in a PostScript image. If the
213 optional argument PRINT-TO is nil, send the image to the printer. If
214 PRINT-TO is a string, save the PostScript image in a file with that
215 name. If PRINT-TO is a number, prompt the user for the name of the
216 file to save in."
217 (interactive (list
218 (file-name-sans-versions (dired-get-filename) t)
219 (ps-print-preprint current-prefix-arg)))
220 (mailcap-parse-mailcaps)
221 (cond
222 ((file-directory-p file-name)
223 (error "Can't print a directory"))
224 ((file-exists-p file-name)
225 (let (mime-type method)
226 (if (and (string-match "\\.[^\\.]+$" file-name)
227 (setq mime-type
228 (mailcap-extension-to-mime
229 (match-string 0 file-name)))
230 (stringp
231 (setq method (mailcap-mime-info mime-type "print"
232 'no-decode))))
233 (call-process shell-file-name nil
234 (generate-new-buffer " *mm*")
236 shell-command-switch
237 (mm-mailcap-command method file-name mime-type))
238 (with-temp-buffer
239 (insert-file-contents file-name)
240 (if (eq gnus-dired-mail-mode 'gnus-user-agent)
241 (gnus-print-buffer)
242 ;; FIXME:
243 (error "MIME print only implemented via Gnus")))
244 (ps-despool print-to))))
245 ((file-symlink-p file-name)
246 (error "File is a symlink to a nonexistent target"))
248 (error "File no longer exists; type `g' to update Dired buffer"))))
250 (provide 'gnus-dired)
252 ;;; gnus-dired.el ends here