1 ;;; gnus-dired.el --- utility functions where gnus and dired meet
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002
4 ;; Free Software Foundation, Inc.
6 ;; Authors: Benjamin Rutt <brutt@bloomington.in.us>,
7 ;; Shenghuo Zhu <zsh@cs.rochester.edu>
8 ;; Keywords: mail, news, extensions
10 ;; This file is part of GNU Emacs.
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)
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.
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.
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:
34 ;; (require 'gnus-dired)
35 ;; (add-hook 'dired-mode-hook 'turn-on-gnus-dired-mode)
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.
52 (defvar gnus-dired-mode nil
53 "Minor mode for intersections of gnus and dired.")
55 (defvar gnus-dired-mode-map nil)
57 (unless gnus-dired-mode-map
58 (setq gnus-dired-mode-map (make-sparse-keymap))
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))
64 (defun gnus-dired-mode (&optional arg)
65 "Minor mode for intersections of gnus and dired.
67 \\{gnus-dired-mode-map}"
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)))
74 (gnus-add-minor-mode 'gnus-dired-mode "" gnus-dired-mode-map)
75 (gnus-run-hooks 'gnus-dired-mode-hook))))
77 ;; Convenience method to turn on gnus-dired-mode.
78 (defsubst turn-on-gnus-dired-mode ()
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
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)
96 ;; warn if user tries to attach without any files marked
97 (if (null files-to-attach)
98 (error "No files to attach")
101 (lambda (f) (file-name-nondirectory f))
102 files-to-attach ", "))
103 (setq bufs (message-buffers))
105 ;; set up destination message buffer
107 (y-or-n-p "Attach files to existing message buffer? "))
109 (if (= (length bufs) 1)
110 (get-buffer (car bufs))
111 (completing-read "Attach to which message buffer: "
114 (cons b (get-buffer b)))
117 ;; setup a new gnus message buffer
118 (gnus-setup-message 'message (message-mail))
119 (setq destination (current-buffer)))
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))))
131 (autoload 'mailcap-parse-mailcaps "mailcap" "" t)
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."
137 (file-name-sans-versions (dired-get-filename) t)
139 (mailcap-parse-mailcaps)
140 (if (and (file-exists-p file-name)
141 (not (file-directory-p file-name)))
142 (let (mime-type method)
144 (string-match "\\.[^\\.]+$" file-name)
146 (mailcap-extension-to-mime
147 (match-string 0 file-name)))
151 (car (mailcap-mime-info mime-type
153 (let ((view-command (mm-mailcap-command method file-name nil)))
154 (message "viewing via %s" view-command)
155 (start-process "*display*"
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"))))
165 (provide 'gnus-dired)
167 ;;; gnus-dired.el ends here