ab5298a8777f5c721e305437bd0131bb61d00f28
[gnus] / lisp / gnus-dired.el
1 ;;; gnus-dired.el --- utility functions where gnus and dired meet
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002
4 ;;        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 2, or (at your option)
15 ;; 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; see the file COPYING.  If not, write to
24 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
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:
33
34 ;; (require 'gnus-dired) ;, isn't needed due to autoload cookies
35 ;; (add-hook 'dired-mode-hook 'turn-on-gnus-dired-mode)
36
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.
41
42 ;;; Code:
43
44 (require 'dired)
45 (require 'gnus-ems)
46 (require 'gnus-msg)
47 (require 'gnus-util)
48 (require 'message)
49 (require 'mm-encode)
50 (require 'mml)
51
52 (defvar gnus-dired-mode nil
53   "Minor mode for intersections of gnus and dired.")
54
55 (defvar gnus-dired-mode-map nil)
56
57 (unless gnus-dired-mode-map
58   (setq gnus-dired-mode-map (make-sparse-keymap))
59
60   (gnus-define-keys gnus-dired-mode-map
61     "\C-c\C-a" gnus-dired-attach
62     "\C-c\C-f" gnus-dired-find-file-mailcap))
63
64 (defun gnus-dired-mode (&optional arg)
65   "Minor mode for intersections of gnus and dired.
66
67 \\{gnus-dired-mode-map}"
68   (interactive "P")
69   (when (eq major-mode 'dired-mode)
70     (set (make-local-variable 'gnus-dired-mode)
71          (if (null arg) (not gnus-dired-mode)
72            (> (prefix-numeric-value arg) 0)))
73     (when gnus-dired-mode
74       (gnus-add-minor-mode 'gnus-dired-mode "" gnus-dired-mode-map)
75       (gnus-run-hooks 'gnus-dired-mode-hook))))
76
77 ;;;###autoload
78 (defun turn-on-gnus-dired-mode ()
79   "Convenience method to turn on gnus-dired-mode."
80   (gnus-dired-mode 1))
81
82 ;; Method to attach files to a gnus composition.
83 (defun gnus-dired-attach (files-to-attach)
84   "Attach dired's marked files to a gnus message composition.
85 If called non-interactively, FILES-TO-ATTACH should be a list of
86 filenames."
87   (interactive
88    (list
89     (delq nil
90           (mapcar
91            ;; don't attach directories
92            (lambda (f) (if (file-directory-p f) nil f))
93            (nreverse (dired-map-over-marks (dired-get-filename) nil))))))
94   (let ((destination nil)
95         (files-str nil)
96         (bufs nil))
97     ;; warn if user tries to attach without any files marked 
98     (if (null files-to-attach)
99         (error "No files to attach")
100       (setq files-str
101             (mapconcat
102              (lambda (f) (file-name-nondirectory f))
103              files-to-attach ", "))
104       (setq bufs (message-buffers))
105     
106       ;; set up destination message buffer
107       (if (and bufs
108                (y-or-n-p "Attach files to existing message buffer? "))
109           (setq destination
110                 (if (= (length bufs) 1)
111                     (get-buffer (car bufs))
112                   (completing-read "Attach to which message buffer: "
113                                    (mapcar
114                                     (lambda (b)
115                                       (cons b (get-buffer b)))
116                                     bufs)
117                                    nil t)))
118         ;; setup a new gnus message buffer
119         (gnus-setup-message 'message (message-mail))
120         (setq destination (current-buffer)))
121
122       ;; set buffer to destination buffer, and attach files
123       (set-buffer destination)
124       (goto-char (point-max))           ;attach at end of buffer
125       (while files-to-attach
126         (mml-attach-file (car files-to-attach)
127                          (or (mm-default-file-encoding (car files-to-attach))
128                              "application/octet-stream") nil)
129         (setq files-to-attach (cdr files-to-attach)))
130       (message "Attached file(s) %s" files-str))))
131
132 (autoload 'mailcap-parse-mailcaps "mailcap" "" t)
133
134 (defun gnus-dired-find-file-mailcap (&optional file-name arg)
135   "In dired, visit FILE-NAME according to the mailcap file.
136 If ARG is non-nil, open it in a new buffer."
137   (interactive (list
138                 (file-name-sans-versions (dired-get-filename) t)
139                 current-prefix-arg))
140   (mailcap-parse-mailcaps)
141   (if (and (file-exists-p file-name)
142            (not (file-directory-p file-name)))
143       (let (mime-type method)
144         (if (and (not arg)
145                  (string-match "\\.[^\\.]+$" file-name)
146                  (setq mime-type
147                        (mailcap-extension-to-mime 
148                         (match-string 0 file-name)))
149                  (stringp 
150                   (setq method
151                         (cdr (assoc 'viewer 
152                                     (car (mailcap-mime-info mime-type 
153                                                             'all)))))))
154             (let ((view-command (mm-mailcap-command method file-name nil)))
155               (message "viewing via %s" view-command)
156               (start-process "*display*"
157                              nil
158                              shell-file-name
159                              shell-command-switch
160                              view-command))
161           (find-file file-name)))
162     (if (file-symlink-p file-name)
163         (error "File is a symlink to a nonexistent target")
164       (error "File no longer exists; type `g' to update Dired buffer"))))
165
166 (provide 'gnus-dired)
167
168 ;;; gnus-dired.el ends here