dired-x.el 66.6 KB
Newer Older
Pavel Janík's avatar
Pavel Janík committed
1
;;; dired-x.el --- extra Dired functionality -*-byte-compile-dynamic: t;-*-
Richard M. Stallman's avatar
Richard M. Stallman committed
2 3 4

;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
;;	Lawrence R. Dodd <dodd@roebling.poly.edu>
Romain Francoise's avatar
Romain Francoise committed
5
;; Maintainer: Romain Francoise <rfrancoise@gnu.org>
6 7
;; Version: 2.37+
;; Date: 1994/08/18 19:27:42
8
;; Keywords: dired extensions files
Richard M. Stallman's avatar
Richard M. Stallman committed
9

10
;; Copyright (C) 1993, 1994, 1997, 2001, 2002, 2003, 2004,
Glenn Morris's avatar
Glenn Morris committed
11
;;   2005, 2006, 2007 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
12 13 14 15 16

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
Richard M. Stallman's avatar
Richard M. Stallman committed
17
;; the Free Software Foundation; either version 2, or (at your option)
Richard M. Stallman's avatar
Richard M. Stallman committed
18 19 20 21 22 23 24 25
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
26
;; along with GNU Emacs; see the file COPYING.  If not, write to the
Lute Kamstra's avatar
Lute Kamstra committed
27 28
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
Richard M. Stallman's avatar
Richard M. Stallman committed
29

Karl Heuer's avatar
Karl Heuer committed
30
;;; Commentary:
Richard M. Stallman's avatar
Richard M. Stallman committed
31

Erik Naggum's avatar
Erik Naggum committed
32
;; This is Sebastian Kremer's excellent dired-x.el (Dired Extra), version
33 34 35
;; 1.191, hacked up for GNU Emacs.  Redundant or conflicting material has
;; been removed or renamed in order to work properly with dired of GNU
;; Emacs.  All suggestions or comments are most welcomed.
Erik Naggum's avatar
Erik Naggum committed
36

37
;;
Erik Naggum's avatar
Erik Naggum committed
38
;; Please, PLEASE, *PLEASE* see the info pages.
39
;;
Erik Naggum's avatar
Erik Naggum committed
40 41 42 43 44 45 46 47

;; BUGS: Type M-x dired-x-submit-report and a report will be generated.

;; INSTALLATION: In your ~/.emacs,
;;
;; (add-hook 'dired-load-hook
;;           (function (lambda ()
;;                       (load "dired-x")
48
;;                       ;; Set variables here.  For example:
Erik Naggum's avatar
Erik Naggum committed
49
;;                       ;; (setq dired-guess-shell-gnutar "gtar")
50
;;                       ;; (dired-omit-mode 1)
Erik Naggum's avatar
Erik Naggum committed
51 52 53 54 55
;;                       )))
;;
;; At load time dired-x.el will install itself, redefine some functions, and
;; bind some dired keys.  *Please* see the info pages for more details.

Dave Love's avatar
Dave Love committed
56
;; *Please* see the info pages for more details.
Erik Naggum's avatar
Erik Naggum committed
57 58 59 60 61 62 63 64

;; User defined variables:
;;
;;      dired-bind-vm
;;      dired-vm-read-only-folders
;;      dired-bind-jump
;;      dired-bind-info
;;      dired-bind-man
Dave Love's avatar
Dave Love committed
65
;;      dired-x-hands-off-my-keys
Erik Naggum's avatar
Erik Naggum committed
66 67 68 69 70 71 72 73
;;      dired-find-subdir
;;      dired-enable-local-variables
;;      dired-local-variables-file
;;      dired-guess-shell-gnutar
;;      dired-guess-shell-gzip-quiet
;;      dired-guess-shell-znew-switches
;;      dired-guess-shell-alist-user
;;      dired-clean-up-buffers-too
74
;;      dired-omit-mode
Erik Naggum's avatar
Erik Naggum committed
75 76
;;      dired-omit-files
;;      dired-omit-extensions
77
;;      dired-omit-size-limit
Erik Naggum's avatar
Erik Naggum committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
;;
;; To find out more about these variables, load this file, put your cursor at
;; the end of any of the variable names, and hit C-h v [RET].  *Please* see
;; the info pages for more details.

;; When loaded this code redefines the following functions of GNU Emacs
;;
;;   Function                         Found in this file of GNU Emacs
;;   --------                         -------------------------------
;;   dired-clean-up-after-deletion    ../lisp/dired.el
;;   dired-find-buffer-nocreate       ../lisp/dired.el
;;   dired-initial-position           ../lisp/dired.el
;;
;;   dired-add-entry                  ../lisp/dired-aux.el
;;   dired-read-shell-command         ../lisp/dired-aux.el
Richard M. Stallman's avatar
Richard M. Stallman committed
93 94


Erik Naggum's avatar
Erik Naggum committed
95
;;; Code:
Richard M. Stallman's avatar
Richard M. Stallman committed
96

Erik Naggum's avatar
Erik Naggum committed
97
;; LOAD.
Richard M. Stallman's avatar
Richard M. Stallman committed
98

Erik Naggum's avatar
Erik Naggum committed
99 100 101
;; This is a no-op if dired-x is being loaded via `dired-load-hook'.  It is
;; here in case the user has autoloaded dired-x via the dired-jump key binding
;; (instead of autoloading to dired as is suggested in the info-pages).
Karl Heuer's avatar
Karl Heuer committed
102

103
(require 'dired)
Karl Heuer's avatar
Karl Heuer committed
104

Erik Naggum's avatar
Erik Naggum committed
105 106
;; We will redefine some functions and also need some macros so we need to
;; load dired stuff of GNU Emacs.
107 108

(require 'dired-aux)
Karl Heuer's avatar
Karl Heuer committed
109

110
(defvar vm-folder-directory)
111
(eval-when-compile (require 'man))
112

Erik Naggum's avatar
Erik Naggum committed
113
;;; User-defined variables.
Richard M. Stallman's avatar
Richard M. Stallman committed
114

115 116 117
(defgroup dired-x nil
  "Extended directory editing (dired-x)."
  :group 'dired)
Richard M. Stallman's avatar
Richard M. Stallman committed
118

119 120 121 122
(defgroup dired-keys nil
  "Dired keys customizations."
  :prefix "dired-"
  :group 'dired-x)
Richard M. Stallman's avatar
Richard M. Stallman committed
123

124
(defcustom dired-bind-vm nil
Dave Love's avatar
Dave Love committed
125
  "*Non-nil means \"V\" runs `dired-vm', otherwise \"V\" runs `dired-rmail'.
126 127 128 129 130 131
Also, RMAIL files contain -*- rmail -*- at the top so \"f\",
`dired-advertised-find-file', will run rmail."
  :type 'boolean
  :group 'dired-keys)

(defcustom dired-bind-jump t
Dave Love's avatar
Dave Love committed
132
  "*Non-nil means bind `dired-jump' to C-x C-j, otherwise do not."
133 134 135 136
  :type 'boolean
  :group 'dired-keys)

(defcustom dired-bind-man t
Dave Love's avatar
Dave Love committed
137
  "*Non-nil means bind `dired-man' to \"N\" in dired-mode, otherwise do not."
138 139 140 141
  :type 'boolean
  :group 'dired-keys)

(defcustom dired-bind-info t
Dave Love's avatar
Dave Love committed
142
  "*Non-nil means bind `dired-info' to \"I\" in dired-mode, otherwise do not."
143 144 145 146
  :type 'boolean
  :group 'dired-keys)

(defcustom dired-vm-read-only-folders nil
Dave Love's avatar
Dave Love committed
147
  "*If non-nil, \\[dired-vm] will visit all folders read-only.
Richard M. Stallman's avatar
Richard M. Stallman committed
148 149 150
If neither nil nor t, e.g. the symbol `if-file-read-only', only
files not writable by you are visited read-only.

151 152 153
Read-only folders only work in VM 5, not in VM 4."
  :type '(choice (const :tag "off" nil)
		 (const :tag "on" t)
154
		 (other :tag "non-writable only" if-file-read-only))
155
  :group 'dired-x)
Richard M. Stallman's avatar
Richard M. Stallman committed
156

157 158 159
(define-minor-mode dired-omit-mode
  "Toggle Dired-Omit mode.
With numeric ARG, enable Dired-Omit mode if ARG is positive, disable
160
otherwise.  Enabling and disabling is buffer-local.
161
If enabled, \"uninteresting\" files are not listed.
Richard M. Stallman's avatar
Richard M. Stallman committed
162
Uninteresting files are those whose filenames match regexp `dired-omit-files',
163
plus those ending with extensions in `dired-omit-extensions'."
164 165 166 167 168 169 170 171 172
  :group 'dired-x
  (if dired-omit-mode
      ;; This will mention how many lines were omitted:
      (let ((dired-omit-size-limit nil)) (dired-omit-expunge))
    (revert-buffer)))

;; For backward compatibility
(defvaralias 'dired-omit-files-p 'dired-omit-mode)
(make-obsolete-variable 'dired-omit-files-p 'dired-omit-mode)
Richard M. Stallman's avatar
Richard M. Stallman committed
173

174
(defcustom dired-omit-files "^\\.?#\\|^\\.$\\|^\\.\\.$"
175
  "*Filenames matching this regexp will not be displayed.
176 177
This only has effect when `dired-omit-mode' is t.  See interactive function
`dired-omit-mode' \(\\[dired-omit-mode]\) and variable
178 179
`dired-omit-extensions'.  The default is to omit  `.', `..', auto-save
files and lock files."
180 181
  :type 'regexp
  :group 'dired-x)
Richard M. Stallman's avatar
Richard M. Stallman committed
182

183
(defcustom dired-find-subdir nil           ; t is pretty near to DWIM...
184 185 186
  "*If non-nil, Dired always finds a directory in a buffer of its own.
If nil, Dired finds the directory as a subdirectory in some other buffer
if it is present as one.
Richard M. Stallman's avatar
Richard M. Stallman committed
187

188
If there are several dired buffers for a directory, the most recently
Richard M. Stallman's avatar
Richard M. Stallman committed
189 190 191
used is chosen.

Dired avoids switching to the current buffer, so that if you have
Dave Love's avatar
Dave Love committed
192
a normal and a wildcard buffer for the same directory, \\[dired] will
193 194 195 196
toggle between those two."
  :type 'boolean
  :group 'dired-x)

197
(defcustom dired-omit-size-limit 30000
198 199 200 201
  "*Maximum size for the \"omitting\" feature.
If nil, there is no maximum size."
  :type '(choice (const :tag "no maximum" nil) integer)
  :group 'dired-x)
Richard M. Stallman's avatar
Richard M. Stallman committed
202

203
(defcustom dired-enable-local-variables t
204
  "*Control use of local-variables lists in Dired.
Richard M. Stallman's avatar
Richard M. Stallman committed
205 206 207 208 209
The value can be t, nil or something else.
A value of t means local-variables lists are obeyed;
nil means they are ignored; anything else means query.

This temporarily overrides the value of `enable-local-variables' when listing
210 211 212
a directory.  See also `dired-local-variables-file'."
  :type 'boolean
  :group 'dired-x)
Richard M. Stallman's avatar
Richard M. Stallman committed
213

214 215 216
(defcustom dired-guess-shell-gnutar (when (or (eq system-type 'gnu)
					      (eq system-type 'gnu/linux))
				      "tar")
Dave Love's avatar
Dave Love committed
217 218 219
  "*If non-nil, name of GNU tar executable.
\(E.g., \"tar\" or \"gtar\").  The `z' switch will be used with it for
compressed or gzip'ed tar files.  If you don't have GNU tar, set this
220
to nil: a pipe using `zcat' or `gunzip -c' will be used."
Dave Love's avatar
Dave Love committed
221 222
  :type '(choice (const :tag "Not GNU tar" nil)
		 (string :tag "Command name"))
223 224 225
  :group 'dired-x)

(defcustom dired-guess-shell-gzip-quiet t
Dave Love's avatar
Dave Love committed
226
  "*Non-nil says pass -q to gzip overriding verbose GZIP environment."
227 228 229 230
  :type 'boolean
  :group 'dired-x)

(defcustom dired-guess-shell-znew-switches nil
Dave Love's avatar
Dave Love committed
231 232 233
  "*If non-nil, then string of switches passed to `znew', example: \"-K\"."
  :type '(choice (const :tag "None" nil)
		 (string :tag "Switches"))
234 235 236
  :group 'dired-x)

(defcustom dired-clean-up-buffers-too t
237
  "*Non-nil means offer to kill buffers visiting files and dirs deleted in Dired."
238 239
  :type 'boolean
  :group 'dired-x)
Richard M. Stallman's avatar
Richard M. Stallman committed
240

Erik Naggum's avatar
Erik Naggum committed
241
;;; KEY BINDINGS.
Richard M. Stallman's avatar
Richard M. Stallman committed
242

243
(define-key dired-mode-map "\M-o" 'dired-omit-mode)
244
(define-key dired-mode-map "*O" 'dired-mark-omitted)
Richard M. Stallman's avatar
Richard M. Stallman committed
245
(define-key dired-mode-map "\M-(" 'dired-mark-sexp)
246 247
(define-key dired-mode-map "*(" 'dired-mark-sexp)
(define-key dired-mode-map "*." 'dired-mark-extension)
Richard M. Stallman's avatar
Richard M. Stallman committed
248 249 250 251 252 253 254 255 256 257 258 259 260 261
(define-key dired-mode-map "\M-!" 'dired-smart-shell-command)
(define-key dired-mode-map "w" 'dired-copy-filename-as-kill)
(define-key dired-mode-map "\M-G" 'dired-goto-subdir)
(define-key dired-mode-map "F" 'dired-do-find-marked-files)
(define-key dired-mode-map "Y"  'dired-do-relsymlink)
(define-key dired-mode-map "%Y" 'dired-do-relsymlink-regexp)
(define-key dired-mode-map "V" 'dired-do-run-mail)

(if dired-bind-man
    (define-key dired-mode-map "N" 'dired-man))

(if dired-bind-info
    (define-key dired-mode-map "I" 'dired-info))

262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
;;; MENU BINDINGS

(let ((menu-bar (lookup-key dired-mode-map [menu-bar])))
  (let ((menu (lookup-key menu-bar [operate])))
    (define-key-after
      menu
      [find-files]
      '(menu-item
        "Find files"
        dired-do-find-marked-files
        :help "Find current or marked files")
      'delete)
    (define-key-after
      menu
      [relsymlink]
      '(menu-item
        "Relative symlink to..."
        dired-do-relsymlink
        :visible (fboundp 'make-symbolic-link)
        :help "Make relative symbolic links for current or marked files")
      'symlink))
  (let ((menu (lookup-key menu-bar [mark])))
    (define-key-after
      menu
      [flag-extension]
      '(menu-item
        "Flag extension..."
        dired-flag-extension
        :help "Flag files with a certain extension for deletion")
      'garbage-files)
    (define-key-after
      menu
      [mark-extension]
      '(menu-item
        "Mark extension..."
        dired-mark-extension
        :help "Mark files with a certain extension")
      'symlinks)
    (define-key-after
      menu
      [mark-omitted]
      '(menu-item
        "Mark omitted"
        dired-mark-omitted
        :help "Mark files matching `dired-omit-files' and `dired-omit-extensions'")
      'mark-extension))
  (let ((menu (lookup-key menu-bar [regexp])))
    (define-key-after
      menu
      [relsymlink-regexp]
      '(menu-item
        "Relative symlink..."
        dired-do-relsymlink-regexp
        :visible (fboundp 'make-symbolic-link)
        :help "Make relative symbolic links for files matching regexp")
      'symlink))
  (let ((menu (lookup-key menu-bar [immediate])))
    (define-key-after
      menu
      [omit-mode]
      '(menu-item
        "Omit mode" dired-omit-mode
        :button (:toggle . dired-omit-mode)
        :help "Enable or disable omitting \"uninteresting\" files")
      'dashes)))

Karl Heuer's avatar
Karl Heuer committed
328
;;; GLOBAL BINDING.
Richard M. Stallman's avatar
Richard M. Stallman committed
329 330 331 332 333 334
(if dired-bind-jump
    (progn
      (define-key global-map "\C-x\C-j" 'dired-jump)
      (define-key global-map "\C-x4\C-j" 'dired-jump-other-window)))


Erik Naggum's avatar
Erik Naggum committed
335
;;; Install into appropriate hooks.
Richard M. Stallman's avatar
Richard M. Stallman committed
336 337 338 339 340

(add-hook 'dired-mode-hook 'dired-extra-startup)
(add-hook 'dired-after-readin-hook 'dired-omit-expunge)

(defun dired-extra-startup ()
341
  "Automatically put on `dired-mode-hook' to get extra Dired features:
Richard M. Stallman's avatar
Richard M. Stallman committed
342 343 344 345 346
\\<dired-mode-map>
  \\[dired-do-run-mail]\t-- run mail on folder (see `dired-bind-vm')
  \\[dired-info]\t-- run info on file
  \\[dired-man]\t-- run man on file
  \\[dired-do-find-marked-files]\t-- visit all marked files simultaneously
347
  \\[dired-omit-mode]\t-- toggle omitting of files
Dave Love's avatar
Dave Love committed
348
  \\[dired-mark-sexp]\t-- mark by Lisp expression
349 350
  \\[dired-copy-filename-as-kill]\t-- copy the file or subdir names into the kill ring;
  \t   you can feed it to other commands using \\[yank]
Richard M. Stallman's avatar
Richard M. Stallman committed
351 352 353

For more features, see variables

Dave Love's avatar
Dave Love committed
354 355 356 357 358
  `dired-bind-vm'
  `dired-bind-jump'
  `dired-bind-info'
  `dired-bind-man'
  `dired-vm-read-only-folders'
359
  `dired-omit-mode'
Dave Love's avatar
Dave Love committed
360 361 362 363 364 365 366 367 368 369 370
  `dired-omit-files'
  `dired-omit-extensions'
  `dired-omit-size-limit'
  `dired-find-subdir'
  `dired-enable-local-variables'
  `dired-local-variables-file'
  `dired-guess-shell-gnutar'
  `dired-guess-shell-gzip-quiet'
  `dired-guess-shell-znew-switches'
  `dired-guess-shell-alist-user'
  `dired-clean-up-buffers-too'
Richard M. Stallman's avatar
Richard M. Stallman committed
371 372 373

See also functions

Dave Love's avatar
Dave Love committed
374 375 376 377 378 379 380 381
  `dired-flag-extension'
  `dired-virtual'
  `dired-jump'
  `dired-man'
  `dired-vm'
  `dired-rmail'
  `dired-info'
  `dired-do-find-marked-files'"
Richard M. Stallman's avatar
Richard M. Stallman committed
382 383 384 385 386 387 388
  (interactive)

  ;; These must be done in each new dired buffer.
  (dired-hack-local-variables)
  (dired-omit-startup))


Erik Naggum's avatar
Erik Naggum committed
389
;;; BUFFER CLEANING.
Richard M. Stallman's avatar
Richard M. Stallman committed
390

Erik Naggum's avatar
Erik Naggum committed
391
;; REDEFINE.
Richard M. Stallman's avatar
Richard M. Stallman committed
392
(defun dired-clean-up-after-deletion (fn)
Dave Love's avatar
Dave Love committed
393 394
  "Clean up after a deleted file or directory FN.
Remove expanded subdir of deleted dir, if any."
Richard M. Stallman's avatar
Richard M. Stallman committed
395 396 397 398 399 400 401 402 403 404 405 406 407 408
  (save-excursion (and (cdr dired-subdir-alist)
                       (dired-goto-subdir fn)
                       (dired-kill-subdir)))

  ;; Offer to kill buffer of deleted file FN.
  (if dired-clean-up-buffers-too
      (progn
        (let ((buf (get-file-buffer fn)))
          (and buf
               (funcall (function y-or-n-p)
                        (format "Kill buffer of %s, too? "
                                (file-name-nondirectory fn)))
               (save-excursion ; you never know where kill-buffer leaves you
                 (kill-buffer buf))))
409
        (let ((buf-list (dired-buffers-for-dir (expand-file-name fn)))
Richard M. Stallman's avatar
Richard M. Stallman committed
410 411 412 413 414 415 416 417
              (buf nil))
          (and buf-list
               (y-or-n-p (format "Kill dired buffer%s of %s, too? "
                                 (dired-plural-s (length buf-list))
                                 (file-name-nondirectory fn)))
               (while buf-list
                 (save-excursion (kill-buffer (car buf-list)))
                 (setq buf-list (cdr buf-list)))))))
Karl Heuer's avatar
Karl Heuer committed
418
  ;; Anything else?
Richard M. Stallman's avatar
Richard M. Stallman committed
419 420 421
  )


Erik Naggum's avatar
Erik Naggum committed
422
;;; EXTENSION MARKING FUNCTIONS.
Richard M. Stallman's avatar
Richard M. Stallman committed
423

Karl Heuer's avatar
Karl Heuer committed
424
;;; Mark files with some extension.
Richard M. Stallman's avatar
Richard M. Stallman committed
425
(defun dired-mark-extension (extension &optional marker-char)
Dave Love's avatar
Dave Love committed
426
  "Mark all files with a certain EXTENSION for use in later commands.
427
A `.' is *not* automatically prepended to the string entered."
Richard M. Stallman's avatar
Richard M. Stallman committed
428 429 430 431 432 433 434 435 436 437 438 439 440
  ;; EXTENSION may also be a list of extensions instead of a single one.
  ;; Optional MARKER-CHAR is marker to use.
  (interactive "sMarking extension: \nP")
  (or (listp extension)
      (setq extension (list extension)))
  (dired-mark-files-regexp
   (concat ".";; don't match names with nothing but an extension
           "\\("
           (mapconcat 'regexp-quote extension "\\|")
           "\\)$")
   marker-char))

(defun dired-flag-extension (extension)
Dave Love's avatar
Dave Love committed
441
  "In dired, flag all files with a certain EXTENSION for deletion.
Richard M. Stallman's avatar
Richard M. Stallman committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
A `.' is *not* automatically prepended to the string entered."
  (interactive "sFlagging extension: ")
  (dired-mark-extension extension dired-del-marker))

;;; Define some unpopular file extensions.  Used for cleaning and omitting.

(defvar dired-patch-unclean-extensions
  '(".rej" ".orig")
  "List of extensions of dispensable files created by the `patch' program.")

(defvar dired-tex-unclean-extensions
  '(".toc" ".log" ".aux");; these are already in completion-ignored-extensions
  "List of extensions of dispensable files created by TeX.")

(defvar dired-latex-unclean-extensions
  '(".idx" ".lof" ".lot" ".glo")
  "List of extensions of dispensable files created by LaTeX.")

(defvar dired-bibtex-unclean-extensions
  '(".blg" ".bbl")
  "List of extensions of dispensable files created by BibTeX.")

(defvar dired-texinfo-unclean-extensions
  '(".cp" ".cps" ".fn" ".fns" ".ky" ".kys" ".pg" ".pgs"
    ".tp" ".tps" ".vr" ".vrs")
  "List of extensions of dispensable files created by texinfo.")

(defun dired-clean-patch ()
  "Flag dispensable files created by patch for deletion.
See variable `dired-patch-unclean-extensions'."
  (interactive)
  (dired-flag-extension dired-patch-unclean-extensions))

(defun dired-clean-tex ()
Karl Heuer's avatar
Karl Heuer committed
476
  "Flag dispensable files created by [La]TeX etc. for deletion.
Eli Zaretskii's avatar
Eli Zaretskii committed
477
See variables `dired-tex-unclean-extensions',
Karl Heuer's avatar
Karl Heuer committed
478 479
`dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
`dired-texinfo-unclean-extensions'."
Richard M. Stallman's avatar
Richard M. Stallman committed
480 481 482 483 484 485
  (interactive)
  (dired-flag-extension (append dired-texinfo-unclean-extensions
                                dired-latex-unclean-extensions
                                dired-bibtex-unclean-extensions
                                dired-tex-unclean-extensions)))

Karl Heuer's avatar
Karl Heuer committed
486 487 488 489 490
(defun dired-very-clean-tex ()
  "Flag dispensable files created by [La]TeX *and* \".dvi\" for deletion.
See variables `dired-texinfo-unclean-extensions',
`dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
`dired-texinfo-unclean-extensions'."
Richard M. Stallman's avatar
Richard M. Stallman committed
491
  (interactive)
Karl Heuer's avatar
Karl Heuer committed
492 493 494 495 496
  (dired-flag-extension (append dired-texinfo-unclean-extensions
                                dired-latex-unclean-extensions
                                dired-bibtex-unclean-extensions
                                dired-tex-unclean-extensions
                                (list ".dvi"))))
Richard M. Stallman's avatar
Richard M. Stallman committed
497

Erik Naggum's avatar
Erik Naggum committed
498
;;; JUMP.
Richard M. Stallman's avatar
Richard M. Stallman committed
499

500
;;;###autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
501 502 503
(defun dired-jump (&optional other-window)
  "Jump to dired buffer corresponding to current buffer.
If in a file, dired the current directory and move to file's line.
504
If in Dired already, pop up a level and goto old directory's line.
Richard M. Stallman's avatar
Richard M. Stallman committed
505 506 507 508 509 510 511 512 513 514 515
In case the proper dired file line cannot be found, refresh the dired
buffer and try again."
  (interactive "P")
  (let* ((file buffer-file-name)
         (dir (if file (file-name-directory file) default-directory)))
    (if (eq major-mode 'dired-mode)
        (progn
          (setq dir (dired-current-directory))
          (dired-up-directory other-window)
          (or (dired-goto-file dir)
              ;; refresh and try again
Karl Heuer's avatar
Karl Heuer committed
516
              (progn
Richard M. Stallman's avatar
Richard M. Stallman committed
517 518 519 520 521 522 523 524
                (dired-insert-subdir (file-name-directory dir))
                (dired-goto-file dir))))
      (if other-window
          (dired-other-window dir)
        (dired dir))
      (if file
          (or (dired-goto-file file)
              ;; refresh and try again
Karl Heuer's avatar
Karl Heuer committed
525
              (progn
Richard M. Stallman's avatar
Richard M. Stallman committed
526
                (dired-insert-subdir (file-name-directory file))
527 528
                (dired-goto-file file))
              ;; Toggle omitting, if it is on, and try again.
529
	      (if dired-omit-mode
530
		  (progn
531
		    (dired-omit-mode)
532
		    (dired-goto-file file))))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
533 534

(defun dired-jump-other-window ()
535
  "Like \\[dired-jump] (`dired-jump') but in other window."
Richard M. Stallman's avatar
Richard M. Stallman committed
536 537 538
  (interactive)
  (dired-jump t))

Erik Naggum's avatar
Erik Naggum committed
539
;;; OMITTING.
Richard M. Stallman's avatar
Richard M. Stallman committed
540 541 542 543 544 545 546

;;; Enhanced omitting of lines from directory listings.
;;; Marked files are never omitted.

;; should probably get rid of this and always use 'no-dir.
;; sk 28-Aug-1991 09:37
(defvar dired-omit-localp 'no-dir
Dave Love's avatar
Dave Love committed
547
  "The LOCALP argument `dired-omit-expunge' passes to `dired-get-filename'.
548
If it is `no-dir', omitting is much faster, but you can only match
Kim F. Storm's avatar
Kim F. Storm committed
549 550
against the non-directory part of the file name.  Set it to nil if you
need to match the entire file name.")
Richard M. Stallman's avatar
Richard M. Stallman committed
551 552 553

;; \017=^O for Omit - other packages can chose other control characters.
(defvar dired-omit-marker-char ?\017
554
  "Temporary marker used by dired-omit.
Richard M. Stallman's avatar
Richard M. Stallman committed
555 556 557
Should never be used as marker by the user or other packages.")

(defun dired-omit-startup ()
558
  (or (assq 'dired-omit-mode minor-mode-alist)
Richard M. Stallman's avatar
Richard M. Stallman committed
559
      (setq minor-mode-alist
560
            (append '((dired-omit-mode
561 562 563
		       (:eval (if (eq major-mode 'dired-mode)
				  " Omit" ""))))
		    minor-mode-alist))))
Richard M. Stallman's avatar
Richard M. Stallman committed
564

565 566 567 568 569
(defun dired-mark-omitted ()
  "Mark files matching `dired-omit-files' and `dired-omit-extensions'."
  (interactive)
  (let ((dired-omit-mode nil)) (revert-buffer)) ;; Show omitted files
  (dired-mark-unmarked-files (dired-omit-regexp) nil nil dired-omit-localp))
Richard M. Stallman's avatar
Richard M. Stallman committed
570 571 572 573 574 575

(defvar dired-omit-extensions
  (append completion-ignored-extensions
          dired-latex-unclean-extensions
          dired-bibtex-unclean-extensions
          dired-texinfo-unclean-extensions)
Dave Love's avatar
Dave Love committed
576
  "If non-nil, a list of extensions \(strings\) to omit from Dired listings.
577 578
Defaults to elements of `completion-ignored-extensions',
`dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions', and
Dave Love's avatar
Dave Love committed
579
`dired-texinfo-unclean-extensions'.
580

581 582
See interactive function `dired-omit-mode' \(\\[dired-omit-mode]\) and
variables `dired-omit-mode' and `dired-omit-files'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
583 584 585

(defun dired-omit-expunge (&optional regexp)
  "Erases all unmarked files matching REGEXP.
586
Does nothing if global variable `dired-omit-mode' is nil, or if called
587
  non-interactively and buffer is bigger than `dired-omit-size-limit'.
Richard M. Stallman's avatar
Richard M. Stallman committed
588 589 590 591 592 593 594
If REGEXP is nil or not specified, uses `dired-omit-files', and also omits
  filenames ending in `dired-omit-extensions'.
If REGEXP is the empty string, this function is a no-op.

This functions works by temporarily binding `dired-marker-char' to
`dired-omit-marker-char' and calling `dired-do-kill-lines'."
  (interactive "sOmit files (regexp): ")
595
  (if (and dired-omit-mode
596 597
           (or (interactive-p)
               (not dired-omit-size-limit)
598 599 600 601
               (< (buffer-size) dired-omit-size-limit)
	       (progn
		 (message "Not omitting: directory larger than %d characters."
			  dired-omit-size-limit)
602
		 (setq dired-omit-mode nil)
603
		 nil)))
Richard M. Stallman's avatar
Richard M. Stallman committed
604
      (let ((omit-re (or regexp (dired-omit-regexp)))
605
            (old-modified-p (buffer-modified-p))
Richard M. Stallman's avatar
Richard M. Stallman committed
606 607 608 609 610 611 612
            count)
        (or (string= omit-re "")
            (let ((dired-marker-char dired-omit-marker-char))
              (message "Omitting...")
              (if (dired-mark-unmarked-files omit-re nil nil dired-omit-localp)
                  (progn
                    (setq count (dired-do-kill-lines nil "Omitted %d line%s."))
613
                    (force-mode-line-update))
Richard M. Stallman's avatar
Richard M. Stallman committed
614
                (message "(Nothing to omit)"))))
615 616
        ;; Try to preserve modified state of buffer.  So `%*' doesn't appear
        ;; in mode-line of omitted buffers.
Dave Love's avatar
Dave Love committed
617
        (set-buffer-modified-p (and old-modified-p
618 619 620
                                    (save-excursion
                                      (goto-char (point-min))
                                      (re-search-forward dired-re-mark nil t))))
Richard M. Stallman's avatar
Richard M. Stallman committed
621 622 623 624 625 626 627 628 629 630 631 632 633 634
        count)))

(defun dired-omit-regexp ()
  (concat (if dired-omit-files (concat "\\(" dired-omit-files "\\)") "")
          (if (and dired-omit-files dired-omit-extensions) "\\|" "")
          (if dired-omit-extensions
              (concat ".";; a non-extension part should exist
                      "\\("
                      (mapconcat 'regexp-quote dired-omit-extensions "\\|")
                      "\\)$")
            "")))

;; Returns t if any work was done, nil otherwise.
(defun dired-mark-unmarked-files (regexp msg &optional unflag-p localp)
Dave Love's avatar
Dave Love committed
635
  "Mark unmarked files matching REGEXP, displaying MSG.
Kim F. Storm's avatar
Kim F. Storm committed
636
REGEXP is matched against the entire file name.
Richard M. Stallman's avatar
Richard M. Stallman committed
637 638
Does not re-mark files which already have a mark.
With prefix argument, unflag all those files.
639
Optional fourth argument LOCALP is as in `dired-get-filename'."
Richard M. Stallman's avatar
Richard M. Stallman committed
640
  (interactive "P")
641
  (let ((dired-marker-char (if unflag-p ?\s dired-marker-char)))
Richard M. Stallman's avatar
Richard M. Stallman committed
642 643 644 645 646 647 648 649 650
    (dired-mark-if
     (and
      ;; not already marked
      (looking-at " ")
      ;; uninteresting
      (let ((fn (dired-get-filename localp t)))
        (and fn (string-match regexp fn))))
     msg)))

Karl Heuer's avatar
Karl Heuer committed
651
;;; REDEFINE.
652
(defun dired-omit-new-add-entry (filename &optional marker-char relative)
Richard M. Stallman's avatar
Richard M. Stallman committed
653 654
  ;; This redefines dired-aux.el's dired-add-entry to avoid calling ls for
  ;; files that are going to be omitted anyway.
655
  (if dired-omit-mode
Richard M. Stallman's avatar
Richard M. Stallman committed
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
      ;; perhaps return t without calling ls
      (let ((omit-re (dired-omit-regexp)))
        (if (or (string= omit-re "")
                (not
                 (string-match omit-re
                               (cond
                                ((eq 'no-dir dired-omit-localp)
                                 filename)
                                ((eq t dired-omit-localp)
                                 (dired-make-relative filename))
                                (t
                                 (dired-make-absolute
                                  filename
                                  (file-name-directory filename)))))))
            ;; if it didn't match, go ahead and add the entry
671
            (dired-omit-old-add-entry filename marker-char relative)
Richard M. Stallman's avatar
Richard M. Stallman committed
672 673 674 675
          ;; dired-add-entry returns t for success, perhaps we should
          ;; return file-exists-p
          t))
    ;; omitting is not turned on at all
676
    (dired-omit-old-add-entry filename marker-char relative)))
Richard M. Stallman's avatar
Richard M. Stallman committed
677

Karl Heuer's avatar
Karl Heuer committed
678
;;; REDEFINE.
Richard M. Stallman's avatar
Richard M. Stallman committed
679 680 681 682 683 684 685 686
;;; Redefine dired-aux.el's version of `dired-add-entry'
;;; Save old defun if not already done:
(or (fboundp 'dired-omit-old-add-entry)
    (fset 'dired-omit-old-add-entry (symbol-function 'dired-add-entry)))
;; Redefine it.
(fset 'dired-add-entry 'dired-omit-new-add-entry)


Erik Naggum's avatar
Erik Naggum committed
687
;;; VIRTUAL DIRED MODE.
Richard M. Stallman's avatar
Richard M. Stallman committed
688 689 690

;;; For browsing `ls -lR' listings in a dired-like fashion.

691
(defalias 'virtual-dired 'dired-virtual)
Richard M. Stallman's avatar
Richard M. Stallman committed
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
(defun dired-virtual (dirname &optional switches)
  "Put this buffer into Virtual Dired mode.

In Virtual Dired mode, all commands that do not actually consult the
filesystem will work.

This is useful if you want to peruse and move around in an ls -lR
output file, for example one you got from an ftp server.  With
ange-ftp, you can even dired a directory containing an ls-lR file,
visit that file and turn on virtual dired mode.  But don't try to save
this file, as dired-virtual indents the listing and thus changes the
buffer.

If you have save a Dired buffer in a file you can use \\[dired-virtual] to
resume it in a later session.

708 709 710 711
Type \\<dired-mode-map>\\[revert-buffer] \
in the Virtual Dired buffer and answer `y' to convert
the virtual to a real dired buffer again.  You don't have to do this, though:
you can relist single subdirs using \\[dired-do-redisplay]."
Richard M. Stallman's avatar
Richard M. Stallman committed
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744

  ;; DIRNAME is the top level directory of the buffer.  It will become
  ;; its `default-directory'.  If nil, the old value of
  ;; default-directory is used.

  ;; Optional SWITCHES are the ls switches to use.

  ;; Shell wildcards will be used if there already is a `wildcard'
  ;; line in the buffer (thus it is a saved Dired buffer), but there
  ;; is no other way to get wildcards.  Insert a `wildcard' line by
  ;; hand if you want them.

  (interactive
   (list (read-string "Virtual Dired directory: " (dired-virtual-guess-dir))))
  (goto-char (point-min))
  (or (looking-at "  ")
      ;; if not already indented, do it now:
      (indent-region (point-min) (point-max) 2))
  (or dirname (setq dirname default-directory))
  (setq dirname (expand-file-name (file-name-as-directory dirname)))
  (setq default-directory dirname)      ; contains no wildcards
  (let ((wildcard (save-excursion
                    (goto-char (point-min))
                    (forward-line 1)
                    (and (looking-at "^  wildcard ")
                         (buffer-substring (match-end 0)
                                           (progn (end-of-line) (point)))))))
  (if wildcard
        (setq dirname (expand-file-name wildcard default-directory))))
  ;; If raw ls listing (not a saved old dired buffer), give it a
  ;; decent subdir headerline:
  (goto-char (point-min))
  (or (looking-at dired-subdir-regexp)
745 746 747
      (insert "  " 
	      (directory-file-name (file-name-directory default-directory))
	      ":\n"))
Richard M. Stallman's avatar
Richard M. Stallman committed
748 749 750 751 752 753 754 755 756
  (dired-mode dirname (or switches dired-listing-switches))
  (setq mode-name "Virtual Dired"
        revert-buffer-function 'dired-virtual-revert)
  (set (make-local-variable 'dired-subdir-alist) nil)
  (dired-build-subdir-alist)
  (goto-char (point-min))
  (dired-initial-position dirname))

(defun dired-virtual-guess-dir ()
Dave Love's avatar
Dave Love committed
757
  "Guess and return appropriate working directory of this buffer.
758
The buffer is assumed to be in Dired or ls -lR format.  The guess is
Dave Love's avatar
Dave Love committed
759 760
based upon buffer contents.  If nothing could be guessed, returns
nil."
Richard M. Stallman's avatar
Richard M. Stallman committed
761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786

  (let ((regexp "^\\(  \\)?\\([^ \n\r]*\\)\\(:\\)[\n\r]")
        (subexpr 2))
    (goto-char (point-min))
    (cond ((looking-at regexp)
           ;; If a saved dired buffer, look to which dir and
           ;; perhaps wildcard it belongs:
           (let ((dir (buffer-substring (match-beginning subexpr)
                                        (match-end subexpr))))
             (file-name-as-directory dir)))
          ;; Else no match for headerline found.  It's a raw ls listing.
          ;; In raw ls listings the directory does not have a headerline
          ;; try parent of first subdir, if any
          ((re-search-forward regexp nil t)
           (file-name-directory
            (directory-file-name
             (file-name-as-directory
              (buffer-substring (match-beginning subexpr)
                                (match-end subexpr))))))
          (t                            ; if all else fails
           nil))))


(defun dired-virtual-revert (&optional arg noconfirm)
  (if (not
       (y-or-n-p "Cannot revert a Virtual Dired buffer - switch to Real Dired mode? "))
Dave Love's avatar
Dave Love committed
787
      (error "Cannot revert a Virtual Dired buffer")
Richard M. Stallman's avatar
Richard M. Stallman committed
788 789 790 791 792 793 794 795 796
    (setq mode-name "Dired"
          revert-buffer-function 'dired-revert)
    (revert-buffer)))

;; A zero-arg version of dired-virtual.
;; You need my modified version of set-auto-mode for the
;; `buffer-contents-mode-alist'.
;; Or you use infer-mode.el and infer-mode-alist, same syntax.
(defun dired-virtual-mode ()
797
  "Put current buffer into Virtual Dired mode (see `dired-virtual').
Richard M. Stallman's avatar
Richard M. Stallman committed
798 799 800 801
Useful on `buffer-contents-mode-alist' (which see) with the regexp

    \"^  \\(/[^ /]+\\)/?+:$\"

802
to put saved dired buffers automatically into Virtual Dired mode.
Richard M. Stallman's avatar
Richard M. Stallman committed
803 804 805

Also useful for `auto-mode-alist' (which see) like this:

806
  \(setq auto-mode-alist (cons '(\"[^/]\\.dired\\'\" . dired-virtual-mode)
Richard M. Stallman's avatar
Richard M. Stallman committed
807 808 809 810 811
                              auto-mode-alist)\)"
  (interactive)
  (dired-virtual (dired-virtual-guess-dir)))


Erik Naggum's avatar
Erik Naggum committed
812
;;; SMART SHELL.
Richard M. Stallman's avatar
Richard M. Stallman committed
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828

;;; An Emacs buffer can have but one working directory, stored in the
;;; buffer-local variable `default-directory'.  A Dired buffer may have
;;; several subdirectories inserted, but still has but one working directory:
;;; that of the top level Dired directory in that buffer.  For some commands
;;; it is appropriate that they use the current Dired directory instead of
;;; `default-directory', e.g., `find-file' and `compile'.  This is a general
;;; mechanism is provided for special handling of the working directory in
;;; special major modes.

;; It's easier to add to this alist than redefine function
;; default-directory while keeping the old information.
(defconst default-directory-alist
  '((dired-mode . (if (fboundp 'dired-current-directory)
                      (dired-current-directory)
                    default-directory)))
Dave Love's avatar
Dave Love committed
829 830 831
  "Alist of major modes and their opinion on `default-directory'.
This is given as a Lisp expression to evaluate.  A resulting value of
nil is ignored in favor of `default-directory'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
832

Dave Love's avatar
Dave Love committed
833 834 835
(defun dired-default-directory ()
  "Usage like variable `default-directory'.
Knows about the special cases in variable `default-directory-alist'."
Richard M. Stallman's avatar
Richard M. Stallman committed
836 837 838 839
  (or (eval (cdr (assq major-mode default-directory-alist)))
      default-directory))

(defun dired-smart-shell-command (cmd &optional insert)
840
  "Like function `shell-command', but in the current Virtual Dired directory."
841 842 843
  (interactive (list (read-from-minibuffer "Shell command: "
					   nil nil nil 'shell-command-history)
		     current-prefix-arg))
Dave Love's avatar
Dave Love committed
844
  (let ((default-directory (dired-default-directory)))
Richard M. Stallman's avatar
Richard M. Stallman committed
845 846 847
    (shell-command cmd insert)))


Erik Naggum's avatar
Erik Naggum committed
848
;;; LOCAL VARIABLES FOR DIRED BUFFERS.
Richard M. Stallman's avatar
Richard M. Stallman committed
849

Karl Heuer's avatar
Karl Heuer committed
850 851 852 853
;;; Brief Description:
;;;
;;; * `dired-extra-startup' is part of the `dired-mode-hook'.
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
854
;;; * `dired-extra-startup' calls `dired-hack-local-variables'
Karl Heuer's avatar
Karl Heuer committed
855
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
856
;;; * `dired-hack-local-variables' checks the value of
Karl Heuer's avatar
Karl Heuer committed
857 858
;;;   `dired-local-variables-file'
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
859 860
;;; * Check if `dired-local-variables-file' is a non-nil string and is a
;;;   filename found in the directory of the Dired Buffer being created.
Karl Heuer's avatar
Karl Heuer committed
861
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
862 863
;;; * If `dired-local-variables-file' satisfies the above, then temporarily
;;;   include it in the Dired Buffer at the bottom.
Karl Heuer's avatar
Karl Heuer committed
864
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
865 866 867 868
;;; * Set `enable-local-variables' temporarily to the user variable
;;;   `dired-enable-local-variables' and run `hack-local-variables' on the
;;;   Dired Buffer.

869
(defvar dired-local-variables-file (convert-standard-filename ".dired")
Richard M. Stallman's avatar
Richard M. Stallman committed
870 871
  "Filename, as string, containing local dired buffer variables to be hacked.
If this file found in current directory, then it will be inserted into dired
872 873 874
buffer and `hack-local-variables' will be run.  See Info node
`(emacs)File Variables' for more information on local variables.
See also `dired-enable-local-variables'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896

(defun dired-hack-local-variables ()
  "Evaluate local variables in `dired-local-variables-file' for dired buffer."
  (if (and dired-local-variables-file
           (stringp dired-local-variables-file)
           (file-exists-p dired-local-variables-file))
      (let ((opoint (point-max))
            buffer-read-only
            ;; In case user has `enable-local-variables' set to nil we
            ;; override it locally with dired's variable.
            (enable-local-variables dired-enable-local-variables))
        ;; Insert 'em.
        (save-excursion
          (goto-char opoint)
          (insert "\^L\n")
          (insert-file-contents dired-local-variables-file))
        ;; Hack 'em.
        (let ((buffer-file-name dired-local-variables-file))
          (hack-local-variables))
        ;; Make sure that the modeline shows the proper information.
        (dired-sort-set-modeline)
        ;; Delete this stuff: `eobp' is used to find last subdir by dired.el.
Karl Heuer's avatar
Karl Heuer committed
897
        (delete-region opoint (point-max)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
898

Karl Heuer's avatar
Karl Heuer committed
899
(defun dired-omit-here-always ()
Dave Love's avatar
Dave Love committed
900
  "Create `dired-local-variables-file' for omitting and reverts directory.
901
Sets `dired-omit-mode' to t in a local variables file that is readable by
Richard M. Stallman's avatar
Richard M. Stallman committed
902
dired."
Karl Heuer's avatar
Karl Heuer committed
903
  (interactive)
Richard M. Stallman's avatar
Richard M. Stallman committed
904 905 906 907 908 909 910
  (if (file-exists-p dired-local-variables-file)
      (message "File `./%s' already exists." dired-local-variables-file)

    ;; Create `dired-local-variables-file'.
    (save-excursion
      (set-buffer (get-buffer-create " *dot-dired*"))
      (erase-buffer)
911
      (insert "Local Variables:\ndired-omit-mode: t\nEnd:\n")
Richard M. Stallman's avatar
Richard M. Stallman committed
912 913 914 915 916 917 918 919
      (write-file dired-local-variables-file)
      (kill-buffer (current-buffer)))

    ;; Run extra-hooks and revert directory.
    (dired-extra-startup)
    (dired-revert)))


Erik Naggum's avatar
Erik Naggum committed
920
;;; GUESS SHELL COMMAND.
Richard M. Stallman's avatar
Richard M. Stallman committed
921

Karl Heuer's avatar
Karl Heuer committed
922 923 924 925
;;; Brief Description:
;;;
;;; `dired-do-shell-command' is bound to `!' by dired.el.
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
926 927
;;; * Redefine `dired-do-shell-command' so it calls
;;;   `dired-guess-shell-command'.
Karl Heuer's avatar
Karl Heuer committed
928
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
929 930
;;; * `dired-guess-shell-command' calls `dired-guess-default' with list of
;;;    marked files.
Karl Heuer's avatar
Karl Heuer committed
931
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
932 933 934
;;; * Parse `dired-guess-shell-alist-user' and
;;;   `dired-guess-shell-alist-default' (in that order) for the first REGEXP
;;;   that matches the first file in the file list.
Karl Heuer's avatar
Karl Heuer committed
935
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
936
;;; * If the REGEXP matches all the entries of the file list then evaluate
Richard M. Stallman's avatar
Richard M. Stallman committed
937
;;;   COMMAND, which is either a string or a Lisp expression returning a
Richard M. Stallman's avatar
Richard M. Stallman committed
938
;;;   string.  COMMAND may be a list of commands.
Karl Heuer's avatar
Karl Heuer committed
939
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
940
;;; * Return this command to `dired-guess-shell-command' which prompts user
Karl Heuer's avatar
Karl Heuer committed
941
;;;   with it.  The list of commands is temporarily put into the history list.
Richard M. Stallman's avatar
Richard M. Stallman committed
942 943 944 945 946 947 948
;;;   If a command is used successfully then it is stored permanently in
;;;   `dired-shell-command-history'.

;;; Guess what shell command to apply to a file.
(defvar dired-shell-command-history nil
  "History list for commands that read dired-shell commands.")

Karl Heuer's avatar
Karl Heuer committed
949
;;; Default list of shell commands.
Richard M. Stallman's avatar
Richard M. Stallman committed
950 951 952 953 954 955

;;; NOTE: Use `gunzip -c' instead of `zcat' on `.gz' files.  Some do not
;;; install GNU zip's version of zcat.

(defvar dired-guess-shell-alist-default
  (list
956
   (list "\\.tar$"
957 958 959 960 961 962 963 964 965 966 967 968 969 970
	 '(if dired-guess-shell-gnutar
	      (concat dired-guess-shell-gnutar " xvf")
	    "tar xvf")
	 ;; Extract files into a separate subdirectory
	 '(if dired-guess-shell-gnutar
	      (concat "mkdir " (file-name-sans-extension file)
		      "; " dired-guess-shell-gnutar " -C "
		      (file-name-sans-extension file) " -xvf")
	    (concat "mkdir " (file-name-sans-extension file)
		    "; tar -C " (file-name-sans-extension file) " -xvf"))
	 ;; List archive contents.
	 '(if dired-guess-shell-gnutar
	      (concat dired-guess-shell-gnutar " tvf")
	    "tar tvf"))
Richard M. Stallman's avatar
Richard M. Stallman committed
971 972 973 974

   ;; REGEXPS for compressed archives must come before the .Z rule to
   ;; be recognized:
   (list "\\.tar\\.Z$"
975 976 977 978 979 980 981
	 ;; Untar it.
	 '(if dired-guess-shell-gnutar
	      (concat dired-guess-shell-gnutar " zxvf")
	    (concat "zcat * | tar xvf -"))
	 ;; Optional conversion to gzip format.
	 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
		  " " dired-guess-shell-znew-switches))
Richard M. Stallman's avatar
Richard M. Stallman committed
982 983

   ;; gzip'ed archives
984
   (list "\\.t\\(ar\\.\\)?gz$"
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
	 '(if dired-guess-shell-gnutar
	      (concat dired-guess-shell-gnutar " zxvf")
	    (concat "gunzip -qc * | tar xvf -"))
	 ;; Extract files into a separate subdirectory
	 '(if dired-guess-shell-gnutar
	      (concat "mkdir " (file-name-sans-extension file)
		      "; " dired-guess-shell-gnutar " -C "
		      (file-name-sans-extension file) " -zxvf")
	    (concat "mkdir " (file-name-sans-extension file)
		    "; gunzip -qc * | tar -C "
		    (file-name-sans-extension file) " -xvf -"))
	 ;; Optional decompression.
	 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q" ""))
	 ;; List archive contents.
	 '(if dired-guess-shell-gnutar
	      (concat dired-guess-shell-gnutar " ztvf")
	    (concat "gunzip -qc * | tar tvf -")))
1002

1003
   ;; bzip2'ed archives
1004
   (list "\\.t\\(ar\\.bz2\\|bz\\)$"
1005
	 "bunzip2 -c * | tar xvf -"
1006 1007 1008 1009
	 ;; Extract files into a separate subdirectory
	 '(concat "mkdir " (file-name-sans-extension file)
		  "; bunzip2 -c * | tar -C "
		  (file-name-sans-extension file) " -xvf -")
1010
	 ;; Optional decompression.
1011
	 "bunzip2")
Richard M. Stallman's avatar
Richard M. Stallman committed
1012

1013 1014
   '("\\.shar\\.Z$" "zcat * | unshar")
   '("\\.shar\\.g?z$" "gunzip -qc * | unshar")
Richard M. Stallman's avatar
Richard M. Stallman committed
1015

1016
   '("\\.e?ps$" "ghostview" "xloadimage" "lpr")
1017
   (list "\\.e?ps\\.g?z$" "gunzip -qc * | ghostview -"
1018 1019
	 ;; Optional decompression.
	 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
1020
   (list "\\.e?ps\\.Z$" "zcat * | ghostview -"
1021 1022 1023
	 ;; Optional conversion to gzip format.
	 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
		  " " dired-guess-shell-znew-switches))
1024

Richard M. Stallman's avatar
Richard M. Stallman committed
1025
   '("\\.patch$" "cat * | patch")
1026
   (list "\\.patch\\.g?z$" "gunzip -qc * | patch"
1027 1028
	 ;; Optional decompression.
	 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
1029
   (list "\\.patch\\.Z$" "zcat * | patch"
1030 1031 1032
	 ;; Optional conversion to gzip format.
	 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
		  " " dired-guess-shell-znew-switches))
Richard M. Stallman's avatar
Richard M. Stallman committed
1033

1034
   ;; The following four extensions are useful with dired-man ("N" key)
1035
   (list "\\.\\(?:[0-9]\\|man\\)$" '(progn (require 'man)
1036 1037 1038
					   (if (Man-support-local-filenames)
					       "man -l"
					     "cat * | tbl | nroff -man -h")))
1039
   (list "\\.\\(?:[0-9]\\|man\\)\\.g?z$" '(progn (require 'man)
1040 1041 1042 1043 1044
						 (if (Man-support-local-filenames)
						     "man -l"
						   "gunzip -qc * | tbl | nroff -man -h"))
	 ;; Optional decompression.
	 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
1045
   (list "\\.[0-9]\\.Z$" '(progn (require 'man)
1046 1047 1048 1049 1050 1051
				 (if (Man-support-local-filenames)
				     "man -l"
				   "zcat * | tbl | nroff -man -h"))
	 ;; Optional conversion to gzip format.
	 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
		  " " dired-guess-shell-znew-switches))
1052 1053
   '("\\.pod$" "perldoc" "pod2man * | nroff -man")

1054 1055 1056 1057 1058
   '("\\.dvi$" "xdvi" "dvips")		; preview and printing
   '("\\.au$" "play")			; play Sun audiofiles
   '("\\.mpe?g$\\|\\.avi$" "xine -p")
   '("\\.wav$" "play")
   '("\\.uu$" "uudecode")		; for uudecoded files
Richard M. Stallman's avatar
Richard M. Stallman committed
1059
   '("\\.hqx$" "mcvert")
1060 1061
   '("\\.sh$" "sh")			; execute shell scripts
   '("\\.xbm$" "bitmap")		; view X11 bitmaps
Richard M. Stallman's avatar
Richard M. Stallman committed
1062
   '("\\.gp$" "gnuplot")
1063
   '("\\.p[bgpn]m$" "xloadimage")
1064
   '("\\.gif$" "xloadimage")		; view gif pictures
1065
   '("\\.tif$" "xloadimage")
Dave Love's avatar
Dave Love committed
1066