* gnus-start.el (gnus-1): Call gnus-agentize if gnus-agent is
[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)
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 ;; Convenience method to turn on gnus-dired-mode.
78 (defsubst turn-on-gnus-dired-mode ()
79   (gnus-dired-mode 1))
80
81 ;; Method to attach files to a gnus composition.
82 (defun gnus-dired-attach (files-to-attach)
83   "Attach dired's marked files to a gnus message composition.
84 If called non-interactively, FILES-TO-ATTACH should be a list of
85 filenames."
86   (interactive
87    (list
88     (delq nil
89           (mapcar
90            ;; don't attach directories
91            (lambda (f) (if (file-directory-p f) nil f))
92            (nreverse (dired-map-over-marks (dired-get-filename) nil))))))
93   (let ((destination nil)
94         (files-str nil)
95         (bufs nil))
96     ;; warn if user tries to attach without any files marked 
97     (if (null files-to-attach)
98         (error "No files to attach")
99       (setq files-str
100             (mapconcat
101              (lambda (f) (file-name-nondirectory f))
102              files-to-attach ", "))
103       (setq bufs (message-buffers))
104     
105       ;; set up destination message buffer
106       (if (and bufs
107                (y-or-n-p "Attach files to existing message buffer? "))
108           (setq destination
109                 (if (= (length bufs) 1)
110                     (get-buffer (car bufs))
111                   (completing-read "Attach to which message buffer: "
112                                    (mapcar
113                                     (lambda (b)
114                                       (cons b (get-buffer b)))
115                                     bufs)
116                                    nil t)))
117         ;; setup a new gnus message buffer
118         (gnus-setup-message 'message (message-mail))
119         (setq destination (current-buffer)))
120
121       ;; set buffer to destination buffer, and attach files
122       (set-buffer destination)
123       (goto-char (point-max))           ;attach at end of buffer
124       (while files-to-attach
125         (mml-attach-file (car files-to-attach)
126                          (or (mm-default-file-encoding (car files-to-attach))
127                              "application/octet-stream") nil)
128         (setq files-to-attach (cdr files-to-attach)))
129       (message "Attached file(s) %s" files-str))))
130
131 (autoload 'mailcap-parse-mailcaps "mailcap" "" t)
132
133 (defun gnus-dired-find-file-mailcap (&optional file-name arg)
134   "In dired, visit FILE-NAME according to the mailcap file.
135 If ARG is non-nil, open it in a new buffer."
136   (interactive (list
137                 (file-name-sans-versions (dired-get-filename) t)
138                 current-prefix-arg))
139   (mailcap-parse-mailcaps)
140   (if (and (file-exists-p file-name)
141            (not (file-directory-p file-name)))
142       (let (mime-type method)
143         (if (and (not arg)
144                  (string-match "\\.[^\\.]+$" file-name)
145                  (setq mime-type
146                        (mailcap-extension-to-mime 
147                         (match-string 0 file-name)))
148                  (stringp 
149                   (setq method
150                         (cdr (assoc 'viewer 
151                                     (car (mailcap-mime-info mime-type 
152                                                             'all)))))))
153             (let ((view-command (mm-mailcap-command method file-name nil)))
154               (message "viewing via %s" view-command)
155               (start-process "*display*"
156                              nil
157                              shell-file-name
158                              shell-command-switch
159                              view-command))
160           (find-file file-name)))
161     (if (file-symlink-p file-name)
162         (error "File is a symlink to a nonexistent target")
163       (error "File no longer exists; type `g' to update Dired buffer"))))
164
165 (provide 'gnus-dired)
166
167 ;;; gnus-dired.el ends here