dired-x.el 63.6 KB
Newer Older
Dave Love's avatar
Dave Love 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>
Dave Love's avatar
Dave Love committed
5
;; Maintainer: FSF (unless Dodd can be found)
6 7
;; Version: 2.37+
;; Date: 1994/08/18 19:27:42
Richard M. Stallman's avatar
Richard M. Stallman committed
8 9
;; Keywords: dired extensions

10
;; Copyright (C) 1993, 1994, 1997 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
11 12 13 14 15

;; 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
16
;; the Free Software Foundation; either version 2, or (at your option)
Richard M. Stallman's avatar
Richard M. Stallman committed
17 18 19 20 21 22 23 24
;; 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
25 26 27
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Richard M. Stallman's avatar
Richard M. Stallman committed
28

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

Erik Naggum's avatar
Erik Naggum committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
;; This is Sebastian Kremer's excellent dired-x.el (Dired Extra), version
;; 1.191, hacked up for GNU Emacs 19.  Redundant or conflicting material
;; has been removed or renamed in order to work properly with dired of
;; GNU Emacs 19.  All suggestions or comments are most welcomed.

;;  
;; Please, PLEASE, *PLEASE* see the info pages.
;; 

;; 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")
;;                       ;; Set global variables here.  For example:
;;                       ;; (setq dired-guess-shell-gnutar "gtar")
;;                       )))
;; (add-hook 'dired-mode-hook
;;           (function (lambda ()
;;                       ;; Set buffer-local variables here.  For example:
;;                       ;; (setq dired-omit-files-p t)
;;                       )))
;;
;; 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
59
;; *Please* see the info pages for more details.
Erik Naggum's avatar
Erik Naggum committed
60 61 62 63 64 65 66 67

;; 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
68
;;      dired-x-hands-off-my-keys
Erik Naggum's avatar
Erik Naggum committed
69 70 71 72 73 74 75 76 77 78 79
;;      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
;;      dired-omit-files-p
;;      dired-omit-files
;;      dired-omit-extensions
80
;;      dired-omit-size-limit
Erik Naggum's avatar
Erik Naggum committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
;;
;; 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
96 97


Erik Naggum's avatar
Erik Naggum committed
98
;;; Code:
Richard M. Stallman's avatar
Richard M. Stallman committed
99

Erik Naggum's avatar
Erik Naggum committed
100
;; LOAD.
Richard M. Stallman's avatar
Richard M. Stallman committed
101

Erik Naggum's avatar
Erik Naggum committed
102 103 104
;; 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
105

106
(require 'dired)
Karl Heuer's avatar
Karl Heuer committed
107

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

(require 'dired-aux)
Karl Heuer's avatar
Karl Heuer committed
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
(defcustom dired-omit-files-p nil
Richard M. Stallman's avatar
Richard M. Stallman committed
158 159 160
  "*If non-nil, \"uninteresting\" files are not listed (buffer-local).
Use \\[dired-omit-toggle] to toggle its value.
Uninteresting files are those whose filenames match regexp `dired-omit-files',
161 162 163
plus those ending with extensions in `dired-omit-extensions'."
  :type 'boolean
  :group 'dired-x)
164
(make-variable-buffer-local 'dired-omit-files-p)
Richard M. Stallman's avatar
Richard M. Stallman committed
165

166
(defcustom dired-omit-files "^\\.?#\\|^\\.$\\|^\\.\\.$"
167
  "*Filenames matching this regexp will not be displayed.
168 169
This only has effect when `dired-omit-files-p' is t.  See interactive function
`dired-omit-toggle' \(\\[dired-omit-toggle]\) and variable
170 171
`dired-omit-extensions'.  The default is to omit  `.', `..', auto-save
files and lock files."
172 173
  :type 'regexp
  :group 'dired-x)
Richard M. Stallman's avatar
Richard M. Stallman committed
174

175
(defcustom dired-find-subdir nil           ; t is pretty near to DWIM...
176 177 178
  "*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
179 180 181 182 183

If there are several Dired buffers for a directory, the most recently
used is chosen.

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

189
(defcustom dired-omit-size-limit 30000
190 191 192 193
  "*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
194

195
(defcustom dired-enable-local-variables t
Richard M. Stallman's avatar
Richard M. Stallman committed
196 197 198 199 200 201
  "*Control use of local-variables lists in dired.
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
202 203 204
a directory.  See also `dired-local-variables-file'."
  :type 'boolean
  :group 'dired-x)
Richard M. Stallman's avatar
Richard M. Stallman committed
205

206
(defcustom dired-guess-shell-gnutar nil
Dave Love's avatar
Dave Love committed
207 208 209
  "*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
210
to nil: a pipe using `zcat' or `gunzip -c' will be used."
Dave Love's avatar
Dave Love committed
211 212
  :type '(choice (const :tag "Not GNU tar" nil)
		 (string :tag "Command name"))
213 214 215
  :group 'dired-x)

(defcustom dired-guess-shell-gzip-quiet t
Dave Love's avatar
Dave Love committed
216
  "*Non-nil says pass -q to gzip overriding verbose GZIP environment."
217 218 219 220
  :type 'boolean
  :group 'dired-x)

(defcustom dired-guess-shell-znew-switches nil
Dave Love's avatar
Dave Love committed
221 222 223
  "*If non-nil, then string of switches passed to `znew', example: \"-K\"."
  :type '(choice (const :tag "None" nil)
		 (string :tag "Switches"))
224 225 226
  :group 'dired-x)

(defcustom dired-clean-up-buffers-too t
Dave Love's avatar
Dave Love committed
227
  "*Non-nil means offer to kill buffers visiting files and dirs deleted in dired."
228 229
  :type 'boolean
  :group 'dired-x)
Richard M. Stallman's avatar
Richard M. Stallman committed
230

Erik Naggum's avatar
Erik Naggum committed
231
;;; KEY BINDINGS.
Richard M. Stallman's avatar
Richard M. Stallman committed
232 233 234

(define-key dired-mode-map "\M-o" 'dired-omit-toggle)
(define-key dired-mode-map "\M-(" 'dired-mark-sexp)
235 236
(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
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
(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-file)
(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))

Karl Heuer's avatar
Karl Heuer committed
252
;;; GLOBAL BINDING.
Richard M. Stallman's avatar
Richard M. Stallman committed
253 254 255 256 257 258
(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
259
;;; Install into appropriate hooks.
Richard M. Stallman's avatar
Richard M. Stallman committed
260 261 262 263 264

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

(defun dired-extra-startup ()
Dave Love's avatar
Dave Love committed
265
  "Automatically put on `dired-mode-hook' to get extra dired features:
Richard M. Stallman's avatar
Richard M. Stallman committed
266 267 268 269 270 271
\\<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
  \\[dired-omit-toggle]\t-- toggle omitting of files
Dave Love's avatar
Dave Love committed
272
  \\[dired-mark-sexp]\t-- mark by Lisp expression
Richard M. Stallman's avatar
Richard M. Stallman committed
273 274 275 276 277
  \\[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].

For more features, see variables

Dave Love's avatar
Dave Love committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
  `dired-bind-vm'
  `dired-bind-jump'
  `dired-bind-info'
  `dired-bind-man'
  `dired-vm-read-only-folders'
  `dired-omit-files-p'
  `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
295 296 297

See also functions

Dave Love's avatar
Dave Love committed
298 299 300 301 302 303 304 305
  `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
306 307 308 309 310 311 312
  (interactive)

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


Erik Naggum's avatar
Erik Naggum committed
313
;;; BUFFER CLEANING.
Richard M. Stallman's avatar
Richard M. Stallman committed
314

Erik Naggum's avatar
Erik Naggum committed
315
;; REDEFINE.
Richard M. Stallman's avatar
Richard M. Stallman committed
316
(defun dired-clean-up-after-deletion (fn)
Dave Love's avatar
Dave Love committed
317 318
  "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
319 320 321 322 323 324 325 326 327 328 329 330 331 332
  (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))))
333
        (let ((buf-list (dired-buffers-for-dir (expand-file-name fn)))
Richard M. Stallman's avatar
Richard M. Stallman committed
334 335 336 337 338 339 340 341
              (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
342
  ;; Anything else?
Richard M. Stallman's avatar
Richard M. Stallman committed
343 344 345
  )


Erik Naggum's avatar
Erik Naggum committed
346
;;; EXTENSION MARKING FUNCTIONS.
Richard M. Stallman's avatar
Richard M. Stallman committed
347

Karl Heuer's avatar
Karl Heuer committed
348
;;; Mark files with some extension.
Richard M. Stallman's avatar
Richard M. Stallman committed
349
(defun dired-mark-extension (extension &optional marker-char)
Dave Love's avatar
Dave Love committed
350
  "Mark all files with a certain EXTENSION for use in later commands.
Richard M. Stallman's avatar
Richard M. Stallman committed
351 352 353 354 355 356 357 358 359 360 361 362 363 364
A `.' is not automatically prepended to the string entered."
  ;; 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
365
  "In dired, flag all files with a certain EXTENSION for deletion.
Richard M. Stallman's avatar
Richard M. Stallman committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
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
400 401 402 403
  "Flag dispensable files created by [La]TeX etc. 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
404 405 406 407 408 409
  (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
410 411 412 413 414
(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
415
  (interactive)
Karl Heuer's avatar
Karl Heuer committed
416 417 418 419 420
  (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
421

Erik Naggum's avatar
Erik Naggum committed
422
;;; JUMP.
Richard M. Stallman's avatar
Richard M. Stallman committed
423

424
;;;###autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
(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.
If in dired already, pop up a level and goto old directory's line.
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
440
              (progn
Richard M. Stallman's avatar
Richard M. Stallman committed
441 442 443 444 445 446 447 448
                (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
449
              (progn
Richard M. Stallman's avatar
Richard M. Stallman committed
450
                (dired-insert-subdir (file-name-directory file))
451 452 453 454 455 456
                (dired-goto-file file))
              ;; Toggle omitting, if it is on, and try again.
	      (if dired-omit-files-p
		  (progn
		    (dired-omit-toggle)
		    (dired-goto-file file))))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
457 458 459 460 461 462

(defun dired-jump-other-window ()
  "Like \\[dired-jump] (dired-jump) but in other window."
  (interactive)
  (dired-jump t))

Erik Naggum's avatar
Erik Naggum committed
463
;;; COPY NAMES OF MARKED FILES INTO KILL-RING.
Karl Heuer's avatar
Karl Heuer committed
464

Richard M. Stallman's avatar
Richard M. Stallman committed
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
(defun dired-copy-filename-as-kill (&optional arg)
  "Copy names of marked (or next ARG) files into the kill ring.
The names are separated by a space.
With a zero prefix arg, use the complete pathname of each marked file.
With \\[universal-argument], use the relative pathname of each marked file.

If on a subdir headerline, use subdirname instead; prefix arg is ignored
in this case.

You can then feed the file name(s) to other commands with \\[yank]."
  (interactive "P")
  (let ((string
         (or (dired-get-subdir)
             (mapconcat (function identity)
                        (if arg
                            (cond ((zerop (prefix-numeric-value arg))
                                   (dired-get-marked-files))
                                  ((integerp arg)
                                   (dired-get-marked-files 'no-dir arg))
                                  (t    ; else a raw arg
                                   (dired-get-marked-files t)))
                          (dired-get-marked-files 'no-dir))
                        " "))))
    (kill-new string)
    (message "%s" string)))


Erik Naggum's avatar
Erik Naggum committed
492
;;; OMITTING.
Richard M. Stallman's avatar
Richard M. Stallman committed
493 494 495 496 497 498 499

;;; 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
500
  "The LOCALP argument `dired-omit-expunge' passes to `dired-get-filename'.
Richard M. Stallman's avatar
Richard M. Stallman committed
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
If it is 'no-dir, omitting is much faster, but you can only match
against the basename of the file.  Set it to nil if you need to match the
whole pathname.")

;; \017=^O for Omit - other packages can chose other control characters.
(defvar dired-omit-marker-char ?\017
  "Temporary marker used by by dired-omit.
Should never be used as marker by the user or other packages.")

(defun dired-omit-startup ()
  (or (assq 'dired-omit-files-p minor-mode-alist)
      (setq minor-mode-alist
            (append '((dired-omit-files-p " Omit")) minor-mode-alist))))

(defun dired-omit-toggle (&optional flag)
516
  "Toggle omitting files matching `dired-omit-files' and `dired-omit-extensions'.
Richard M. Stallman's avatar
Richard M. Stallman committed
517 518 519 520 521 522 523 524 525 526 527 528 529 530
With an arg, and if omitting was off, don't toggle and just mark the
  files but don't actually omit them.
With an arg, and if omitting was on, turn it off but don't refresh the buffer."
  (interactive "P")
  (if flag
      (if dired-omit-files-p
          (setq dired-omit-files-p (not dired-omit-files-p))
        (dired-mark-unmarked-files (dired-omit-regexp) nil nil
                                   dired-omit-localp))
    ;; no FLAG
    (setq dired-omit-files-p (not dired-omit-files-p))
    (if (not dired-omit-files-p)
        (revert-buffer)
      ;; this will mention how many were omitted:
531 532
      (let ((dired-omit-size-limit nil))
        (dired-omit-expunge)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
533 534 535 536 537 538

(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
539
  "If non-nil, a list of extensions \(strings\) to omit from Dired listings.
540 541
Defaults to elements of `completion-ignored-extensions',
`dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions', and
Dave Love's avatar
Dave Love committed
542
`dired-texinfo-unclean-extensions'.
543 544 545

See interactive function `dired-omit-toggle' \(\\[dired-omit-toggle]\) and
variables `dired-omit-files-p' and `dired-omit-files'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
546 547 548

(defun dired-omit-expunge (&optional regexp)
  "Erases all unmarked files matching REGEXP.
549 550
Does nothing if global variable `dired-omit-files-p' is nil, or if called
  non-interactively and buffer is bigger than `dired-omit-size-limit'.
Richard M. Stallman's avatar
Richard M. Stallman committed
551 552 553 554 555 556 557
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): ")
558 559 560
  (if (and dired-omit-files-p
           (or (interactive-p)
               (not dired-omit-size-limit)
561 562 563 564 565 566
               (< (buffer-size) dired-omit-size-limit)
	       (progn
		 (message "Not omitting: directory larger than %d characters."
			  dired-omit-size-limit)
		 (setq dired-omit-files-p nil)
		 nil)))
Richard M. Stallman's avatar
Richard M. Stallman committed
567
      (let ((omit-re (or regexp (dired-omit-regexp)))
568
            (old-modified-p (buffer-modified-p))
Richard M. Stallman's avatar
Richard M. Stallman committed
569 570 571 572 573 574 575
            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."))
576
                    (force-mode-line-update))
Richard M. Stallman's avatar
Richard M. Stallman committed
577
                (message "(Nothing to omit)"))))
578 579
        ;; Try to preserve modified state of buffer.  So `%*' doesn't appear
        ;; in mode-line of omitted buffers.
Dave Love's avatar
Dave Love committed
580
        (set-buffer-modified-p (and old-modified-p
581 582 583
                                    (save-excursion
                                      (goto-char (point-min))
                                      (re-search-forward dired-re-mark nil t))))
Richard M. Stallman's avatar
Richard M. Stallman committed
584 585 586 587 588 589 590 591 592 593 594 595 596 597
        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
598
  "Mark unmarked files matching REGEXP, displaying MSG.
Richard M. Stallman's avatar
Richard M. Stallman committed
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
REGEXP is matched against the complete pathname.
Does not re-mark files which already have a mark.
With prefix argument, unflag all those files.
Second optional argument LOCALP is as in `dired-get-filename'."
  (interactive "P")
  (let ((dired-marker-char (if unflag-p ?\  dired-marker-char)))
    (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
614
;;; REDEFINE.
615
(defun dired-omit-new-add-entry (filename &optional marker-char relative)
Richard M. Stallman's avatar
Richard M. Stallman committed
616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633
  ;; This redefines dired-aux.el's dired-add-entry to avoid calling ls for
  ;; files that are going to be omitted anyway.
  (if dired-omit-files-p
      ;; 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
634
            (dired-omit-old-add-entry filename marker-char relative)
Richard M. Stallman's avatar
Richard M. Stallman committed
635 636 637 638
          ;; dired-add-entry returns t for success, perhaps we should
          ;; return file-exists-p
          t))
    ;; omitting is not turned on at all
639
    (dired-omit-old-add-entry filename marker-char relative)))
Richard M. Stallman's avatar
Richard M. Stallman committed
640

Karl Heuer's avatar
Karl Heuer committed
641
;;; REDEFINE.
Richard M. Stallman's avatar
Richard M. Stallman committed
642 643 644 645 646 647 648 649
;;; 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
650
;;; VIRTUAL DIRED MODE.
Richard M. Stallman's avatar
Richard M. Stallman committed
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673

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

(fset 'virtual-dired 'dired-virtual)
(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.

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
Dave Love's avatar
Dave Love committed
674
single subdirs using \\[dired-do-redisplay]."
Richard M. Stallman's avatar
Richard M. Stallman committed
675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717

  ;; 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)
      (dired-insert-headerline default-directory))
  (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
718 719 720 721
  "Guess and return appropriate working directory of this buffer.
Th ebuffer is assumed to be in Dired or ls -lR format.  The guess is
based upon buffer contents.  If nothing could be guessed, returns
nil."
Richard M. Stallman's avatar
Richard M. Stallman committed
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747

  (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
748
      (error "Cannot revert a Virtual Dired buffer")
Richard M. Stallman's avatar
Richard M. Stallman committed
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
    (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 ()
  "Put current buffer into virtual dired mode (see `dired-virtual').
Useful on `buffer-contents-mode-alist' (which see) with the regexp

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

to put saved dired buffers automatically into virtual dired mode.

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

767
  \(setq auto-mode-alist (cons '(\"[^/]\\.dired\\'\" . dired-virtual-mode)
Richard M. Stallman's avatar
Richard M. Stallman committed
768 769 770 771 772
                              auto-mode-alist)\)"
  (interactive)
  (dired-virtual (dired-virtual-guess-dir)))


Erik Naggum's avatar
Erik Naggum committed
773
;;; SMART SHELL.
Richard M. Stallman's avatar
Richard M. Stallman committed
774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789

;;; 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
790 791 792
  "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
793

Dave Love's avatar
Dave Love committed
794 795 796
(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
797 798 799 800 801
  (or (eval (cdr (assq major-mode default-directory-alist)))
      default-directory))

(defun dired-smart-shell-command (cmd &optional insert)
  "Like function `shell-command', but in the current Tree Dired directory."
802 803 804
  (interactive (list (read-from-minibuffer "Shell command: "
					   nil nil nil 'shell-command-history)
		     current-prefix-arg))
Dave Love's avatar
Dave Love committed
805
  (let ((default-directory (dired-default-directory)))
Richard M. Stallman's avatar
Richard M. Stallman committed
806 807 808
    (shell-command cmd insert)))

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

Karl Heuer's avatar
Karl Heuer committed
811 812 813 814
;;; Brief Description:
;;;
;;; * `dired-extra-startup' is part of the `dired-mode-hook'.
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
815
;;; * `dired-extra-startup' calls `dired-hack-local-variables'
Karl Heuer's avatar
Karl Heuer committed
816
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
817
;;; * `dired-hack-local-variables' checks the value of
Karl Heuer's avatar
Karl Heuer committed
818 819
;;;   `dired-local-variables-file'
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
820 821
;;; * 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
822
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
823 824
;;; * 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
825
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
826 827 828 829
;;; * Set `enable-local-variables' temporarily to the user variable
;;;   `dired-enable-local-variables' and run `hack-local-variables' on the
;;;   Dired Buffer.

830
(defvar dired-local-variables-file (convert-standard-filename ".dired")
Richard M. Stallman's avatar
Richard M. Stallman committed
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856
  "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
buffer and `hack-local-variables' will be run.  See Emacs Info pages for more
information on local variables.  See also `dired-enable-local-variables'.")

(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
857
        (delete-region opoint (point-max)))))
Richard M. Stallman's avatar
Richard M. Stallman committed
858

Karl Heuer's avatar
Karl Heuer committed
859
(defun dired-omit-here-always ()
Dave Love's avatar
Dave Love committed
860
  "Create `dired-local-variables-file' for omitting and reverts directory.
Richard M. Stallman's avatar
Richard M. Stallman committed
861 862
Sets dired-omit-file-p to t in a local variables file that is readable by
dired."
Karl Heuer's avatar
Karl Heuer committed
863
  (interactive)
Richard M. Stallman's avatar
Richard M. Stallman committed
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
  (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)
      (insert "Local Variables:\ndired-omit-files-p: t\nEnd:\n")
      (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
880
;;; GUESS SHELL COMMAND.
Richard M. Stallman's avatar
Richard M. Stallman committed
881

Karl Heuer's avatar
Karl Heuer committed
882 883 884 885
;;; Brief Description:
;;;
;;; `dired-do-shell-command' is bound to `!' by dired.el.
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
886 887
;;; * Redefine `dired-do-shell-command' so it calls
;;;   `dired-guess-shell-command'.
Karl Heuer's avatar
Karl Heuer committed
888
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
889 890
;;; * `dired-guess-shell-command' calls `dired-guess-default' with list of
;;;    marked files.
Karl Heuer's avatar
Karl Heuer committed
891
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
892 893 894
;;; * 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
895
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
896
;;; * If the REGEXP matches all the entries of the file list then evaluate
Richard M. Stallman's avatar
Richard M. Stallman committed
897
;;;   COMMAND, which is either a string or a Lisp expression returning a
Richard M. Stallman's avatar
Richard M. Stallman committed
898
;;;   string.  COMMAND may be a list of commands.
Karl Heuer's avatar
Karl Heuer committed
899
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
900
;;; * Return this command to `dired-guess-shell-command' which prompts user
Karl Heuer's avatar
Karl Heuer committed
901
;;;   with it.  The list of commands is temporarily put into the history list.
Richard M. Stallman's avatar
Richard M. Stallman committed
902 903 904 905 906 907 908
;;;   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
909
;;; Default list of shell commands.
Richard M. Stallman's avatar
Richard M. Stallman committed
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931

;;; 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
   (list "\\.tar$" '(if dired-guess-shell-gnutar
                        (concat dired-guess-shell-gnutar " xvf")
                      "tar xvf"))

   ;; REGEXPS for compressed archives must come before the .Z rule to
   ;; be recognized:
   (list "\\.tar\\.Z$"
         ;; 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))

   ;; gzip'ed archives
932
   (list "\\.t\\(ar\\.\\)?gz$"
Richard M. Stallman's avatar
Richard M. Stallman committed
933 934 935 936 937
         '(if dired-guess-shell-gnutar
              (concat dired-guess-shell-gnutar " zxvf")
            (concat "gunzip -qc * | tar xvf -"))
         ;; Optional decompression.
         '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q" "")))
938 939 940 941 942
   ;; bzip2'ed archives
   (list "\\.tar\\.bz2$"
	 "bunzip2 -c * | tar xvf -"
	 ;; Optional decompression.
         "bunzip2")
Richard M. Stallman's avatar
Richard M. Stallman committed
943 944 945 946

   '("\\.shar.Z$" "zcat * | unshar")
   '("\\.shar.g?z$" "gunzip -qc * | unshar")

947
   '("\\.e?ps$" "ghostview" "xloadimage" "lpr")
948
   (list "\\.e?ps.g?z$" "gunzip -qc * | ghostview -"
Richard M. Stallman's avatar
Richard M. Stallman committed
949 950
         ;; Optional decompression.
         '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
951
   (list "\\.e?ps.Z$" "zcat * | ghostview -"
Richard M. Stallman's avatar
Richard M. Stallman committed
952 953 954 955 956 957 958 959 960 961 962
         ;; Optional conversion to gzip format.
         '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
                  " " dired-guess-shell-znew-switches))
   '("\\.patch$" "cat * | patch")
   '("\\.patch.g?z$" "gunzip -qc * | patch")
   (list "\\.patch.Z$" "zcat * | patch"
         ;; Optional conversion to gzip format.
         '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
                  " " dired-guess-shell-znew-switches))

   '("\\.dvi$" "xdvi" "dvips")          ; preview and printing
Karl Heuer's avatar
Karl Heuer committed
963
   '("\\.au$" "play")                   ; play Sun audiofiles
Richard M. Stallman's avatar
Richard M. Stallman committed
964 965 966 967 968 969
   '("\\.mpg$" "mpeg_play")
   '("\\.uu$" "uudecode")               ; for uudecoded files
   '("\\.hqx$" "mcvert")
   '("\\.sh$" "sh")                     ; execute shell scripts
   '("\\.xbm$" "bitmap")                ; view X11 bitmaps
   '("\\.gp$" "gnuplot")
970 971 972
   '("\\.p[bgpn]m$" "xloadimage")
   '("\\.gif$" "xloadimage")                    ; view gif pictures
   '("\\.tif$" "xloadimage")
Dave Love's avatar
Dave Love committed
973
   '("\\.png$" "display")		; xloadimage 4.1 doesn't grok PNG
974
   '("\\.jpg$" "xloadimage")
Richard M. Stallman's avatar
Richard M. Stallman committed
975 976 977 978
   '("\\.fig$" "xfig")                  ; edit fig pictures
   '("\\.out$" "xgraph")                ; for plotting purposes.
   '("\\.tex$" "latex" "tex")
   '("\\.texi\\(nfo\\)?$" "makeinfo" "texi2dvi")
979
   '("\\.pdf$" "xpdf")              ; edit PDF files
Richard M. Stallman's avatar
Richard M. Stallman committed
980 981 982 983 984 985 986 987 988 989

   ;; Some other popular archivers.
   '("\\.zoo$" "zoo x//")
   '("\\.zip$" "unzip")
   '("\\.lzh$" "lharc x")
   '("\\.arc$" "arc x")
   '("\\.shar$" "unshar")

   ;; Compression.
   (list "\\.g?z$" '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
990
   (list "\\.bz2$" "bunzip2")
Richard M. Stallman's avatar
Richard M. Stallman committed
991 992 993 994 995
   (list "\\.Z$" "uncompress"
         ;; Optional conversion to gzip format.
         '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
                  " " dired-guess-shell-znew-switches))
   )
Karl Heuer's avatar
Karl Heuer committed
996

Richard M. Stallman's avatar
Richard M. Stallman committed
997
  "Default alist used for shell command guessing.
Dave Love's avatar
Dave Love committed
998
See `dired-guess-shell-alist-user'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
999

Dave Love's avatar
Dave Love committed
1000 1001 1002
(defcustom dired-guess-shell-alist-user nil
  "User-defined alist of rules for suggested commands.
These rules take precedence over the predefined rules in the variable
Richard M. Stallman's avatar
Richard M. Stallman committed
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
`dired-guess-shell-alist-default' (to which they are prepended).

Each element of this list looks like

    \(REGEXP COMMAND...\)

where each COMMAND can either be a string or a lisp expression that evaluates
to a string.  If several COMMANDs are given, the first one will be the default
and the rest will be added temporarily to the history and can be retrieved
with \\[previous-history-element] (M-p) .

You can set this variable in your ~/.emacs.  For example, to add rules for
`.foo' and `.bar' files, write

 \(setq dired-guess-shell-alist-user
Dave Love's avatar
Dave Love committed
1018
       (list (list \"\\\\.foo\\\\'\" \"FOO-COMMAND\");; fixed rule
Richard M. Stallman's avatar
Richard M. Stallman committed
1019
              ;; possibly more rules ...
Dave Love's avatar
Dave Love committed
1020
              (list \"\\\\.bar\\\'\";; rule with condition test
Richard M. Stallman's avatar
Richard M. Stallman committed
1021 1022
                    '(if condition
                          \"BAR-COMMAND-1\"
Dave Love's avatar
Dave Love committed
1023 1024 1025
                        \"BAR-COMMAND-2\")))\)"
  :group 'dired-x
  :type '(alist :key-type regexp :value-type (repeat sexp)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1026 1027

(defun dired-guess-default (files)
Dave Love's avatar
Dave Love committed
1028 1029
  "Guess a shell commands for FILES.  Return command or list of commands.
See `dired-guess-shell-alist-user'."
Richard M. Stallman's avatar
Richard M. Stallman committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051

  (let* ((case-fold-search nil) ; case-sensitive matching
         ;; Prepend the user's alist to the default alist.
         (alist (append dired-guess-shell-alist-user
                        dired-guess-shell-alist-default))
         (file (car files))
         (flist (cdr files))
         elt regexp cmds)

    ;; Find the first match in the alist for first file in FILES.
    (while alist
      (setq elt (car alist)
            regexp (car elt)
            alist (cdr alist))
      (if (string-match regexp file)
          (setq cmds (cdr elt)
                alist nil)))

    ;; If more than one file, see if all of FILES match regular expression.
    (while (and flist
                (string-match regexp (car flist)))
      (setq flist (cdr flist)))
Karl Heuer's avatar
Karl Heuer committed
1052

Richard M. Stallman's avatar
Richard M. Stallman committed
1053 1054 1055 1056
    ;; If flist is still non-nil, then do not guess since this means that not
    ;; all the files in FILES were matched by the regexp.
    (setq cmds (and (not flist) cmds))

Karl Heuer's avatar
Karl Heuer committed
1057 1058
    ;; Return commands or nil if flist is still non-nil.
    ;; Evaluate the commands in order that any logical testing will be done.
Richard M. Stallman's avatar
Richard M. Stallman committed
1059 1060 1061 1062 1063 1064
    (cond ((not (cdr cmds))
           (eval (car cmds))) ; single command
          (t
           (mapcar (function eval) cmds)))))

(defun dired-guess-shell-command (prompt files)
Dave Love's avatar
Dave Love committed
1065
  "Ask user with PROMPT for a shell command, guessing a default from FILES."
Richard M. Stallman's avatar
Richard M. Stallman committed
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116

  (let ((default (dired-guess-default files))
        default-list old-history val (failed t))

    (if (null default)
        ;; Nothing to guess
        (read-from-minibuffer prompt nil nil nil 'dired-shell-command-history)

      ;; Save current history list
      (setq old-history dired-shell-command-history)

      (if (listp default)

          ;; More than one guess
          (setq default-list default
                default (car default)
                prompt (concat
                        prompt
                        (format "{%d guesses} " (length default-list))))

        ;; Just one guess
        (setq default-list (list default)))

      ;; Push all guesses onto history so that they can be retrieved with M-p
      ;; and put the first guess in the prompt but not in the initial value.
      (setq dired-shell-command-history
            (append default-list dired-shell-command-history)
            prompt (concat prompt (format "[%s] " default)))

      ;; The unwind-protect returns VAL, and we too.
      (unwind-protect
          ;; BODYFORM
          (progn
            (setq val (read-from-minibuffer prompt nil nil nil
                                            'dired-shell-command-history)
                  failed nil)
            ;; If we got a return, then use default.
            (if (equal val "")
                (setq val default))
            val)

        ;; UNWINDFORMS
        ;; Undo pushing onto the history list so that an aborted
        ;; command doesn't get the default in the next command.
        (setq dired-shell-command-history old-history)
        (if (not failed)
            (or (equal val (car-safe dired-shell-command-history))
                (setq dired-shell-command-history
                      (cons val dired-shell-command-history))))))))


Karl Heuer's avatar
Karl Heuer committed
1117
;;; REDEFINE.
Richard M. Stallman's avatar
Richard M. Stallman committed
1118 1119
;;; Redefine dired-aux.el's version:
(defun dired-read-shell-command (prompt arg files)
Dave Love's avatar
Dave Love committed
1120 1121 1122 1123
  "Read a dired shell command prompting with PROMPT (using read-string).
ARG is the prefix arg and may be used to indicate in the prompt which
  files are affected.
This is an extra function so that you can redefine it."
Richard M. Stallman's avatar
Richard M. Stallman committed
1124 1125 1126 1127 1128 1129 1130
  (dired-mark-pop-up
   nil 'shell files
   'dired-guess-shell-command
   (format prompt (dired-mark-prompt arg files)) ; PROMPT
   files))                                       ; FILES


Erik Naggum's avatar
Erik Naggum committed
1131
;;; RELATIVE SYMBOLIC LINKS.
Richard M. Stallman's avatar
Richard M. Stallman committed
1132 1133 1134 1135 1136

(defvar dired-keep-marker-relsymlink ?S
  "See variable `dired-keep-marker-move'.")

(defun dired-make-relative-symlink (file1 file2 &optional ok-if-already-exists)
Dave Love's avatar
Dave Love committed
1137
  "Make a symbolic link (pointing to FILE1) in FILE2.
Richard M. Stallman's avatar
Richard M. Stallman committed
1138 1139 1140 1141 1142 1143
The link is relative (if possible), for example

    \"/vol/tex/bin/foo\" \"/vol/local/bin/foo\"

results in

Dave Love's avatar
Dave Love committed
1144
    \"../../tex/bin/foo\" \"/vol/local/bin/foo\""
Richard M. Stallman's avatar
Richard M. Stallman committed
1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
  (interactive "FRelSymLink: \nFRelSymLink %s: \np")
  (let (name1 name2 len1 len2 (index 0) sub)
    (setq file1 (expand-file-name file1)
          file2 (expand-file-name file2)
          len1 (length file1)
          len2 (length file2))
    ;; Find common initial pathname components:
    (let (next)
      (while (and (setq next (string-match "/" file1 index))
                  (setq next (1+ next))
                  (< next (min len1 len2))
                  ;; For the comparison, both substrings must end in
                  ;; `/', so NEXT is *one plus* the result of the
                  ;; string-match.
                  ;; E.g., consider the case of linking "/tmp/a/abc"
Karl Heuer's avatar
Karl Heuer committed
1160
                  ;; to "/tmp/abc" erroneously giving "/tmp/a" instead
Richard M. Stallman's avatar
Richard M. Stallman committed
1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
                  ;; of "/tmp/" as common initial component
                  (string-equal (substring file1 0 next)
                                (substring file2 0 next)))
        (setq index next))
      (setq name2 file2
            sub (substring file1 0 index)
            name1 (substring file1 index)))
    (if (string-equal sub "/")
        ;; No common initial pathname found
        (setq name1 file1)
      ;; Else they have a common parent directory
      (let ((tem (substring file2 index))
            (start 0)
            (count 0))
        ;; Count number of slashes we must compensate for ...
        (while (setq start (string-match "/" tem start))
          (setq count (1+ count)
                start (1+ start)))
        ;; ... and prepend a "../" for each slash found:
        (while (> count 0)
          (setq count (1- count)
                name1 (concat "../" name1)))))
    (make-symbolic-link
     (directory-file-name name1)        ; must not link to foo/
                                        ; (trailing slash!)
     name2 ok-if-already-exists)))

(defun dired-do-relsymlink (&optional arg)
Dave Love's avatar
Dave Love committed
1189 1190
   "Relative symlink all marked (or next ARG) files into a directory.
Otherwise make a relative symbolic link to the current file.
Richard M. Stallman's avatar
Richard M. Stallman committed
1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
This creates relative symbolic links like

    foo -> ../bar/foo

not absolute ones like

    foo -> /ugly/path/that/may/change/any/day/bar/foo"
  (interactive "P")
  (dired-do-create-files 'relsymlink (function dired-make-relative-symlink)
                           "RelSymLink" arg dired-keep-marker-relsymlink))

(defun dired-do-relsymlink-regexp (regexp newname &optional whole-path)
  "RelSymlink all marked files containing REGEXP to NEWNAME.
See functions `dired-do-rename-regexp' and `dired-do-relsymlink'
for more info."
  (interactive (dired-mark-read-regexp "RelSymLink"))
  (dired-do-create-files-regexp
   (function dired-make-relative-symlink)
   "RelSymLink" nil regexp newname whole-path dired-keep-marker-relsymlink))


Erik Naggum's avatar
Erik Naggum committed
1212
;;; VISIT ALL MARKED FILES SIMULTANEOUSLY.
Richard M. Stallman's avatar
Richard M. Stallman committed
1213

Karl Heuer's avatar
Karl Heuer committed
1214 1215 1216 1217
;;; Brief Description:
;;;
;;; `dired-do-find-marked-files' is bound to `F' by dired-x.el.
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
1218
;;; * Use `dired-get-marked-files' to collect the marked files in the current
Karl Heuer's avatar
Karl Heuer committed
1219 1220
;;;   Dired Buffer into a list of filenames `FILE-LIST'.
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
1221 1222
;;; * Pass FILE-LIST to `dired-simultaneous-find-file' all with
;;;   `dired-do-find-marked-files''s prefix argument NOSELECT.
Karl Heuer's avatar
Karl Heuer committed
1223
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
1224 1225
;;; * `dired-simultaneous-find-file' runs through FILE-LIST decrementing the
;;;   list each time.
Karl Heuer's avatar
Karl Heuer committed
1226
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
1227 1228
;;; * If NOSELECT is non-nil then just run `find-file-noselect'  on each
;;;   element of FILE-LIST.
Karl Heuer's avatar
Karl Heuer committed
1229
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
1230 1231 1232
;;; * If NOSELECT is nil then calculate the `size' of the window for each file
;;;   by dividing the `window-height' by length of FILE-LIST.  Thus, `size' is
;;;   cognizant of the window-configuration.
Karl Heuer's avatar
Karl Heuer committed
1233
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
;;; * If `size' is too small abort, otherwise run `find-file' on each element
;;;   of FILE-LIST giving each a window of height `size'.

(defun dired-do-find-marked-files (&optional noselect)
  "Find all marked files displaying all of them simultaneously.
With optional NOSELECT just find files but do not select them.

The current window is split across all files marked, as evenly as possible.
Remaining lines go to bottom-most window.  The number of files that can be
displayed this way is restricted by the height of the current window and
`window-min-height'.

Karl Heuer's avatar
Karl Heuer committed