Merge dired-x's dired-clean-up-after-deletion into dired's version.
[bpt/emacs.git] / lisp / dired-x.el
CommitLineData
276f1d00 1;;; dired-x.el --- extra Dired functionality
cb3fe1f0 2
73b0cd50 3;; Copyright (C) 1993-1994, 1997, 2001-2011 Free Software Foundation, Inc.
552939e4 4
cb3fe1f0
RS
5;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
6;; Lawrence R. Dodd <dodd@roebling.poly.edu>
2d29381c 7;; Maintainer: Romain Francoise <rfrancoise@gnu.org>
a443d46e 8;; Keywords: dired extensions files
bd78fa1d 9;; Package: emacs
cb3fe1f0 10
cb3fe1f0
RS
11;; This file is part of GNU Emacs.
12
eb3fa2cf 13;; GNU Emacs is free software: you can redistribute it and/or modify
cb3fe1f0 14;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
cb3fe1f0
RS
17
18;; GNU Emacs is distributed in the hope that it will be useful,
19;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21;; GNU General Public License for more details.
22
23;; You should have received a copy of the GNU General Public License
eb3fa2cf 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
cb3fe1f0 25
9572a9dd 26;;; Commentary:
cb3fe1f0 27
461b69ae
GM
28;; This is based on Sebastian Kremer's excellent dired-x.el (Dired Extra),
29;; version 1.191, adapted for GNU Emacs. See the `dired-x' info pages.
b578f267 30
461b69ae 31;; USAGE: In your ~/.emacs,
b578f267
EN
32;;
33;; (add-hook 'dired-load-hook
461b69ae 34;; (lambda ()
da5e0ce4 35;; (load "dired-x")
d7af463c 36;; ;; Set global variables here. For example:
b578f267 37;; ;; (setq dired-guess-shell-gnutar "gtar")
461b69ae 38;; ))
d7af463c 39;; (add-hook 'dired-mode-hook
461b69ae 40;; (lambda ()
d7af463c 41;; ;; Set buffer-local variables here. For example:
ce64e046 42;; ;; (dired-omit-mode 1)
461b69ae 43;; ))
b578f267
EN
44;;
45;; At load time dired-x.el will install itself, redefine some functions, and
461b69ae 46;; bind some dired keys.
b578f267 47
461b69ae 48;; User customization: M-x customize-group RET dired-x RET.
b578f267 49
461b69ae 50;; When loaded this code redefines the following functions of GNU Emacs:
42924231 51;; From dired.el: dired-find-buffer-nocreate.
30abce25 52;; From dired-aux.el: dired-read-shell-command.
b578f267 53
461b69ae 54;; *Please* see the `dired-x' info pages for more details.
cb3fe1f0
RS
55
56\f
b578f267 57;;; Code:
cb3fe1f0 58
b578f267 59;; LOAD.
cb3fe1f0 60
461b69ae
GM
61;; This is a no-op if dired-x is being loaded via `dired-load-hook',
62;; but maybe not if a dired-x function is being autoloaded.
d82cfc0c 63(require 'dired)
9572a9dd 64
461b69ae 65;; We will redefine some functions and also need some macros.
d82cfc0c 66(require 'dired-aux)
9572a9dd 67
b578f267 68;;; User-defined variables.
cb3fe1f0 69
286c247d
RS
70(defgroup dired-x nil
71 "Extended directory editing (dired-x)."
72 :group 'dired)
cb3fe1f0 73
286c247d
RS
74(defgroup dired-keys nil
75 "Dired keys customizations."
76 :prefix "dired-"
77 :group 'dired-x)
cb3fe1f0 78
286c247d 79(defcustom dired-bind-vm nil
9201cc28 80 "Non-nil means \"V\" runs `dired-vm', otherwise \"V\" runs `dired-rmail'.
3176a6a0
GM
81RMAIL files in the old Babyl format (used before before Emacs 23.1)
82contain \"-*- rmail -*-\" at the top, so `dired-find-file'
83will run `rmail' on these files. New RMAIL files use the standard
84mbox format, and so cannot be distinguished in this way."
286c247d
RS
85 :type 'boolean
86 :group 'dired-keys)
87
88(defcustom dired-bind-jump t
da5e0ce4
GM
89 "Non-nil means bind `dired-jump' to C-x C-j, otherwise do not.
90Setting this variable directly after dired-x is loaded has no effect -
91use \\[customize]."
286c247d 92 :type 'boolean
da5e0ce4
GM
93 :set (lambda (sym val)
94 (if (set sym val)
95 (progn
96 (define-key global-map "\C-x\C-j" 'dired-jump)
97 (define-key global-map "\C-x4\C-j" 'dired-jump-other-window))
98 (if (eq 'dired-jump (lookup-key global-map "\C-x\C-j"))
99 (define-key global-map "\C-x\C-j" nil))
100 (if (eq 'dired-jump-other-window (lookup-key global-map "\C-x4\C-j"))
101 (define-key global-map "\C-x4\C-j" nil))))
286c247d
RS
102 :group 'dired-keys)
103
104(defcustom dired-bind-man t
da5e0ce4
GM
105 "Non-nil means bind `dired-man' to \"N\" in dired-mode, otherwise do not.
106Setting this variable directly after dired-x is loaded has no effect -
107use \\[customize]."
286c247d 108 :type 'boolean
da5e0ce4
GM
109 :set (lambda (sym val)
110 (if (set sym val)
111 (define-key dired-mode-map "N" 'dired-man)
112 (if (eq 'dired-man (lookup-key dired-mode-map "N"))
113 (define-key dired-mode-map "N" nil))))
286c247d
RS
114 :group 'dired-keys)
115
116(defcustom dired-bind-info t
da5e0ce4
GM
117 "Non-nil means bind `dired-info' to \"I\" in dired-mode, otherwise do not.
118Setting this variable directly after dired-x is loaded has no effect -
119use \\[customize]."
286c247d 120 :type 'boolean
da5e0ce4
GM
121 :set (lambda (sym val)
122 (if (set sym val)
123 (define-key dired-mode-map "I" 'dired-info)
124 (if (eq 'dired-info (lookup-key dired-mode-map "I"))
125 (define-key dired-mode-map "I" nil))))
286c247d
RS
126 :group 'dired-keys)
127
128(defcustom dired-vm-read-only-folders nil
9201cc28 129 "If non-nil, \\[dired-vm] will visit all folders read-only.
cb3fe1f0 130If neither nil nor t, e.g. the symbol `if-file-read-only', only
da5e0ce4 131files not writable by you are visited read-only."
286c247d
RS
132 :type '(choice (const :tag "off" nil)
133 (const :tag "on" t)
e2c146c1 134 (other :tag "non-writable only" if-file-read-only))
286c247d 135 :group 'dired-x)
cb3fe1f0 136
ce64e046
LH
137(define-minor-mode dired-omit-mode
138 "Toggle Dired-Omit mode.
139With numeric ARG, enable Dired-Omit mode if ARG is positive, disable
6fbad7af 140otherwise. Enabling and disabling is buffer-local.
ce64e046 141If enabled, \"uninteresting\" files are not listed.
cb3fe1f0 142Uninteresting files are those whose filenames match regexp `dired-omit-files',
2223a1b3
JL
143plus those ending with extensions in `dired-omit-extensions'.
144
145To enable omitting in every Dired buffer, you can put in your ~/.emacs
146
147 (add-hook 'dired-mode-hook (lambda () (dired-omit-mode 1)))
148
149See Info node `(dired-x) Omitting Variables' for more information."
ce64e046
LH
150 :group 'dired-x
151 (if dired-omit-mode
152 ;; This will mention how many lines were omitted:
153 (let ((dired-omit-size-limit nil)) (dired-omit-expunge))
154 (revert-buffer)))
155
156;; For backward compatibility
cf24b82d 157(define-obsolete-variable-alias 'dired-omit-files-p 'dired-omit-mode "22.1")
cb3fe1f0 158
99358b97 159(defcustom dired-omit-files "^\\.?#\\|^\\.$\\|^\\.\\.$"
9201cc28 160 "Filenames matching this regexp will not be displayed.
ce64e046
LH
161This only has effect when `dired-omit-mode' is t. See interactive function
162`dired-omit-mode' \(\\[dired-omit-mode]\) and variable
99358b97
RS
163`dired-omit-extensions'. The default is to omit `.', `..', auto-save
164files and lock files."
286c247d
RS
165 :type 'regexp
166 :group 'dired-x)
cb3fe1f0 167
f754f898
TH
168(defcustom dired-omit-verbose t
169 "When non-nil, show messages when omitting files.
170When nil, don't show messages."
171 :type 'boolean
172 :group 'dired-x)
173
286c247d 174(defcustom dired-find-subdir nil ; t is pretty near to DWIM...
9201cc28 175 "If non-nil, Dired always finds a directory in a buffer of its own.
0a44133e
RS
176If nil, Dired finds the directory as a subdirectory in some other buffer
177if it is present as one.
cb3fe1f0 178
6fbad7af 179If there are several dired buffers for a directory, the most recently
cb3fe1f0
RS
180used is chosen.
181
182Dired avoids switching to the current buffer, so that if you have
b847eb8c 183a normal and a wildcard buffer for the same directory, \\[dired] will
286c247d
RS
184toggle between those two."
185 :type 'boolean
186 :group 'dired-x)
187
8e458944 188(defcustom dired-omit-size-limit 30000
9201cc28 189 "Maximum size for the \"omitting\" feature.
286c247d
RS
190If nil, there is no maximum size."
191 :type '(choice (const :tag "no maximum" nil) integer)
192 :group 'dired-x)
cb3fe1f0 193
286c247d 194(defcustom dired-enable-local-variables t
9201cc28 195 "Control use of local-variables lists in Dired.
cb3fe1f0
RS
196The value can be t, nil or something else.
197A value of t means local-variables lists are obeyed;
198nil means they are ignored; anything else means query.
199
5a0c3f56
JB
200This temporarily overrides the value of `enable-local-variables' when
201listing a directory. See also `dired-local-variables-file'."
286c247d
RS
202 :type 'boolean
203 :group 'dired-x)
cb3fe1f0 204
da5e0ce4
GM
205(defcustom dired-guess-shell-gnutar
206 (catch 'found
207 (dolist (exe '("tar" "gtar"))
208 (if (with-temp-buffer
209 (ignore-errors (call-process exe nil t nil "--version"))
210 (and (re-search-backward "GNU tar" nil t) t))
211 (throw 'found exe))))
9201cc28 212 "If non-nil, name of GNU tar executable.
b847eb8c
DL
213\(E.g., \"tar\" or \"gtar\"). The `z' switch will be used with it for
214compressed or gzip'ed tar files. If you don't have GNU tar, set this
286c247d 215to nil: a pipe using `zcat' or `gunzip -c' will be used."
da5e0ce4
GM
216 ;; Changed from system-type test to testing --version output.
217 ;; Maybe test --help for -z instead?
218 :version "24.1"
b847eb8c
DL
219 :type '(choice (const :tag "Not GNU tar" nil)
220 (string :tag "Command name"))
286c247d
RS
221 :group 'dired-x)
222
223(defcustom dired-guess-shell-gzip-quiet t
9201cc28 224 "Non-nil says pass -q to gzip overriding verbose GZIP environment."
286c247d
RS
225 :type 'boolean
226 :group 'dired-x)
227
228(defcustom dired-guess-shell-znew-switches nil
9201cc28 229 "If non-nil, then string of switches passed to `znew', example: \"-K\"."
b847eb8c
DL
230 :type '(choice (const :tag "None" nil)
231 (string :tag "Switches"))
286c247d
RS
232 :group 'dired-x)
233
234(defcustom dired-clean-up-buffers-too t
9201cc28 235 "Non-nil means offer to kill buffers visiting files and dirs deleted in Dired."
286c247d
RS
236 :type 'boolean
237 :group 'dired-x)
cb3fe1f0 238
b578f267 239;;; KEY BINDINGS.
cb3fe1f0 240
ce64e046 241(define-key dired-mode-map "\M-o" 'dired-omit-mode)
71326638 242(define-key dired-mode-map "*O" 'dired-mark-omitted)
cb3fe1f0 243(define-key dired-mode-map "\M-(" 'dired-mark-sexp)
582bd36a
RS
244(define-key dired-mode-map "*(" 'dired-mark-sexp)
245(define-key dired-mode-map "*." 'dired-mark-extension)
cb3fe1f0 246(define-key dired-mode-map "\M-!" 'dired-smart-shell-command)
cb3fe1f0
RS
247(define-key dired-mode-map "\M-G" 'dired-goto-subdir)
248(define-key dired-mode-map "F" 'dired-do-find-marked-files)
249(define-key dired-mode-map "Y" 'dired-do-relsymlink)
250(define-key dired-mode-map "%Y" 'dired-do-relsymlink-regexp)
251(define-key dired-mode-map "V" 'dired-do-run-mail)
252
438fc253
LH
253;;; MENU BINDINGS
254
c5ca3aa0
GM
255(require 'easymenu)
256
257(let ((menu (lookup-key dired-mode-map [menu-bar])))
258 (easy-menu-add-item menu '("Operate")
259 ["Find Files" dired-do-find-marked-files
260 :help "Find current or marked files"]
261 "Shell Command...")
262 (easy-menu-add-item menu '("Operate")
263 ["Relative Symlink to..." dired-do-relsymlink
264 :visible (fboundp 'make-symbolic-link)
265 :help "Make relative symbolic links for current or \
266marked files"]
267 "Hardlink to...")
268 (easy-menu-add-item menu '("Mark")
269 ["Flag Extension..." dired-flag-extension
270 :help "Flag files with a certain extension for deletion"]
271 "Mark Executables")
272 (easy-menu-add-item menu '("Mark")
273 ["Mark Extension..." dired-mark-extension
274 :help "Mark files with a certain extension"]
275 "Unmark All")
276 (easy-menu-add-item menu '("Mark")
277 ["Mark Omitted" dired-mark-omitted
278 :help "Mark files matching `dired-omit-files' \
279and `dired-omit-extensions'"]
280 "Unmark All")
281 (easy-menu-add-item menu '("Regexp")
282 ["Relative Symlink..." dired-do-relsymlink-regexp
283 :visible (fboundp 'make-symbolic-link)
284 :help "Make relative symbolic links for files \
285matching regexp"]
286 "Hardlink...")
287 (easy-menu-add-item menu '("Immediate")
288 ["Omit Mode" dired-omit-mode
289 :style toggle :selected dired-omit-mode
290 :help "Enable or disable omitting \"uninteresting\" \
291files"]
292 "Refresh"))
438fc253 293
cb3fe1f0 294\f
efcb74f6 295;; Install into appropriate hooks.
cb3fe1f0
RS
296
297(add-hook 'dired-mode-hook 'dired-extra-startup)
298(add-hook 'dired-after-readin-hook 'dired-omit-expunge)
299
300(defun dired-extra-startup ()
6fbad7af 301 "Automatically put on `dired-mode-hook' to get extra Dired features:
cb3fe1f0 302\\<dired-mode-map>
cb3fe1f0
RS
303 \\[dired-do-run-mail]\t-- run mail on folder (see `dired-bind-vm')
304 \\[dired-info]\t-- run info on file
305 \\[dired-man]\t-- run man on file
306 \\[dired-do-find-marked-files]\t-- visit all marked files simultaneously
ce64e046 307 \\[dired-omit-mode]\t-- toggle omitting of files
b847eb8c 308 \\[dired-mark-sexp]\t-- mark by Lisp expression
cb3fe1f0 309
da5e0ce4
GM
310To see the options you can set, use M-x customize-group RET dired-x RET.
311See also the functions:
b847eb8c
DL
312 `dired-flag-extension'
313 `dired-virtual'
314 `dired-jump'
315 `dired-man'
316 `dired-vm'
317 `dired-rmail'
318 `dired-info'
319 `dired-do-find-marked-files'"
cb3fe1f0 320 (interactive)
cb3fe1f0
RS
321 ;; These must be done in each new dired buffer.
322 (dired-hack-local-variables)
323 (dired-omit-startup))
324
325\f
b578f267 326;;; EXTENSION MARKING FUNCTIONS.
cb3fe1f0 327
efcb74f6 328;; Mark files with some extension.
cb3fe1f0 329(defun dired-mark-extension (extension &optional marker-char)
b847eb8c 330 "Mark all files with a certain EXTENSION for use in later commands.
6fbad7af 331A `.' is *not* automatically prepended to the string entered."
cb3fe1f0
RS
332 ;; EXTENSION may also be a list of extensions instead of a single one.
333 ;; Optional MARKER-CHAR is marker to use.
334 (interactive "sMarking extension: \nP")
335 (or (listp extension)
336 (setq extension (list extension)))
337 (dired-mark-files-regexp
338 (concat ".";; don't match names with nothing but an extension
339 "\\("
340 (mapconcat 'regexp-quote extension "\\|")
341 "\\)$")
342 marker-char))
343
344(defun dired-flag-extension (extension)
b847eb8c 345 "In dired, flag all files with a certain EXTENSION for deletion.
cb3fe1f0
RS
346A `.' is *not* automatically prepended to the string entered."
347 (interactive "sFlagging extension: ")
348 (dired-mark-extension extension dired-del-marker))
349
efcb74f6 350;; Define some unpopular file extensions. Used for cleaning and omitting.
cb3fe1f0
RS
351
352(defvar dired-patch-unclean-extensions
353 '(".rej" ".orig")
354 "List of extensions of dispensable files created by the `patch' program.")
355
356(defvar dired-tex-unclean-extensions
357 '(".toc" ".log" ".aux");; these are already in completion-ignored-extensions
358 "List of extensions of dispensable files created by TeX.")
359
360(defvar dired-latex-unclean-extensions
361 '(".idx" ".lof" ".lot" ".glo")
362 "List of extensions of dispensable files created by LaTeX.")
363
364(defvar dired-bibtex-unclean-extensions
365 '(".blg" ".bbl")
366 "List of extensions of dispensable files created by BibTeX.")
367
368(defvar dired-texinfo-unclean-extensions
369 '(".cp" ".cps" ".fn" ".fns" ".ky" ".kys" ".pg" ".pgs"
370 ".tp" ".tps" ".vr" ".vrs")
371 "List of extensions of dispensable files created by texinfo.")
372
373(defun dired-clean-patch ()
374 "Flag dispensable files created by patch for deletion.
375See variable `dired-patch-unclean-extensions'."
376 (interactive)
377 (dired-flag-extension dired-patch-unclean-extensions))
378
379(defun dired-clean-tex ()
9572a9dd 380 "Flag dispensable files created by [La]TeX etc. for deletion.
ca388882 381See variables `dired-tex-unclean-extensions',
9572a9dd
KH
382`dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
383`dired-texinfo-unclean-extensions'."
cb3fe1f0
RS
384 (interactive)
385 (dired-flag-extension (append dired-texinfo-unclean-extensions
386 dired-latex-unclean-extensions
387 dired-bibtex-unclean-extensions
388 dired-tex-unclean-extensions)))
389
9572a9dd
KH
390(defun dired-very-clean-tex ()
391 "Flag dispensable files created by [La]TeX *and* \".dvi\" for deletion.
392See variables `dired-texinfo-unclean-extensions',
393`dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
394`dired-texinfo-unclean-extensions'."
cb3fe1f0 395 (interactive)
9572a9dd
KH
396 (dired-flag-extension (append dired-texinfo-unclean-extensions
397 dired-latex-unclean-extensions
398 dired-bibtex-unclean-extensions
399 dired-tex-unclean-extensions
400 (list ".dvi"))))
cb3fe1f0 401\f
b578f267 402;;; JUMP.
cb3fe1f0 403
40c8b203 404;;;###autoload
f5d6548a 405(defun dired-jump (&optional other-window file-name)
cb3fe1f0
RS
406 "Jump to dired buffer corresponding to current buffer.
407If in a file, dired the current directory and move to file's line.
6fbad7af 408If in Dired already, pop up a level and goto old directory's line.
cb3fe1f0 409In case the proper dired file line cannot be found, refresh the dired
f5d6548a
JL
410buffer and try again.
411When OTHER-WINDOW is non-nil, jump to dired buffer in other window.
412Interactively with prefix argument, read FILE-NAME and
413move to its line in dired."
414 (interactive
415 (list nil (and current-prefix-arg
416 (read-file-name "Jump to dired file: "))))
417 (let* ((file (or file-name buffer-file-name))
cb3fe1f0 418 (dir (if file (file-name-directory file) default-directory)))
f5d6548a 419 (if (and (eq major-mode 'dired-mode) (null file-name))
cb3fe1f0
RS
420 (progn
421 (setq dir (dired-current-directory))
422 (dired-up-directory other-window)
da5e0ce4 423 (unless (dired-goto-file dir)
cb3fe1f0 424 ;; refresh and try again
da5e0ce4
GM
425 (dired-insert-subdir (file-name-directory dir))
426 (dired-goto-file dir)))
cb3fe1f0
RS
427 (if other-window
428 (dired-other-window dir)
429 (dired dir))
430 (if file
431 (or (dired-goto-file file)
432 ;; refresh and try again
9572a9dd 433 (progn
cb3fe1f0 434 (dired-insert-subdir (file-name-directory file))
7e90bcf5
RS
435 (dired-goto-file file))
436 ;; Toggle omitting, if it is on, and try again.
da5e0ce4
GM
437 (when dired-omit-mode
438 (dired-omit-mode)
439 (dired-goto-file file)))))))
cb3fe1f0 440
f5d6548a 441(defun dired-jump-other-window (&optional file-name)
6fbad7af 442 "Like \\[dired-jump] (`dired-jump') but in other window."
f5d6548a
JL
443 (interactive
444 (list (and current-prefix-arg
445 (read-file-name "Jump to dired file: "))))
446 (dired-jump t file-name))
cb3fe1f0 447\f
b578f267 448;;; OMITTING.
cb3fe1f0 449
efcb74f6
SM
450;; Enhanced omitting of lines from directory listings.
451;; Marked files are never omitted.
cb3fe1f0
RS
452
453;; should probably get rid of this and always use 'no-dir.
454;; sk 28-Aug-1991 09:37
455(defvar dired-omit-localp 'no-dir
b847eb8c 456 "The LOCALP argument `dired-omit-expunge' passes to `dired-get-filename'.
6fbad7af 457If it is `no-dir', omitting is much faster, but you can only match
470fa6d1
KS
458against the non-directory part of the file name. Set it to nil if you
459need to match the entire file name.")
cb3fe1f0
RS
460
461;; \017=^O for Omit - other packages can chose other control characters.
462(defvar dired-omit-marker-char ?\017
5a0c3f56 463 "Temporary marker used by Dired-Omit.
cb3fe1f0
RS
464Should never be used as marker by the user or other packages.")
465
466(defun dired-omit-startup ()
ce64e046 467 (or (assq 'dired-omit-mode minor-mode-alist)
cb3fe1f0 468 (setq minor-mode-alist
ce64e046 469 (append '((dired-omit-mode
ab606888
GM
470 (:eval (if (eq major-mode 'dired-mode)
471 " Omit" ""))))
472 minor-mode-alist))))
cb3fe1f0 473
ce64e046
LH
474(defun dired-mark-omitted ()
475 "Mark files matching `dired-omit-files' and `dired-omit-extensions'."
476 (interactive)
477 (let ((dired-omit-mode nil)) (revert-buffer)) ;; Show omitted files
478 (dired-mark-unmarked-files (dired-omit-regexp) nil nil dired-omit-localp))
cb3fe1f0 479
461b69ae 480(defcustom dired-omit-extensions
cb3fe1f0
RS
481 (append completion-ignored-extensions
482 dired-latex-unclean-extensions
483 dired-bibtex-unclean-extensions
484 dired-texinfo-unclean-extensions)
b847eb8c 485 "If non-nil, a list of extensions \(strings\) to omit from Dired listings.
685ff9f8
RS
486Defaults to elements of `completion-ignored-extensions',
487`dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions', and
b847eb8c 488`dired-texinfo-unclean-extensions'.
685ff9f8 489
ce64e046 490See interactive function `dired-omit-mode' \(\\[dired-omit-mode]\) and
461b69ae
GM
491variables `dired-omit-mode' and `dired-omit-files'."
492 :type '(repeat string)
493 :group 'dired-x)
cb3fe1f0
RS
494
495(defun dired-omit-expunge (&optional regexp)
496 "Erases all unmarked files matching REGEXP.
ce64e046 497Does nothing if global variable `dired-omit-mode' is nil, or if called
ad8c4554 498 non-interactively and buffer is bigger than `dired-omit-size-limit'.
cb3fe1f0
RS
499If REGEXP is nil or not specified, uses `dired-omit-files', and also omits
500 filenames ending in `dired-omit-extensions'.
501If REGEXP is the empty string, this function is a no-op.
502
503This functions works by temporarily binding `dired-marker-char' to
504`dired-omit-marker-char' and calling `dired-do-kill-lines'."
505 (interactive "sOmit files (regexp): ")
ce64e046 506 (if (and dired-omit-mode
32226619 507 (or (called-interactively-p 'interactive)
ad8c4554 508 (not dired-omit-size-limit)
8e458944
RS
509 (< (buffer-size) dired-omit-size-limit)
510 (progn
f754f898
TH
511 (when dired-omit-verbose
512 (message "Not omitting: directory larger than %d characters."
513 dired-omit-size-limit))
ce64e046 514 (setq dired-omit-mode nil)
8e458944 515 nil)))
cb3fe1f0 516 (let ((omit-re (or regexp (dired-omit-regexp)))
9b5ef74b 517 (old-modified-p (buffer-modified-p))
cb3fe1f0
RS
518 count)
519 (or (string= omit-re "")
520 (let ((dired-marker-char dired-omit-marker-char))
f754f898 521 (when dired-omit-verbose (message "Omitting..."))
cb3fe1f0
RS
522 (if (dired-mark-unmarked-files omit-re nil nil dired-omit-localp)
523 (progn
f754f898
TH
524 (setq count (dired-do-kill-lines
525 nil
526 (if dired-omit-verbose "Omitted %d line%s." "")))
048ea441 527 (force-mode-line-update))
f754f898 528 (when dired-omit-verbose (message "(Nothing to omit)")))))
9b5ef74b
RS
529 ;; Try to preserve modified state of buffer. So `%*' doesn't appear
530 ;; in mode-line of omitted buffers.
b847eb8c 531 (set-buffer-modified-p (and old-modified-p
9b5ef74b
RS
532 (save-excursion
533 (goto-char (point-min))
534 (re-search-forward dired-re-mark nil t))))
cb3fe1f0
RS
535 count)))
536
537(defun dired-omit-regexp ()
538 (concat (if dired-omit-files (concat "\\(" dired-omit-files "\\)") "")
539 (if (and dired-omit-files dired-omit-extensions) "\\|" "")
540 (if dired-omit-extensions
541 (concat ".";; a non-extension part should exist
542 "\\("
543 (mapconcat 'regexp-quote dired-omit-extensions "\\|")
544 "\\)$")
545 "")))
546
547;; Returns t if any work was done, nil otherwise.
548(defun dired-mark-unmarked-files (regexp msg &optional unflag-p localp)
b847eb8c 549 "Mark unmarked files matching REGEXP, displaying MSG.
470fa6d1 550REGEXP is matched against the entire file name.
cb3fe1f0
RS
551Does not re-mark files which already have a mark.
552With prefix argument, unflag all those files.
6fbad7af 553Optional fourth argument LOCALP is as in `dired-get-filename'."
cb3fe1f0 554 (interactive "P")
6fbad7af 555 (let ((dired-marker-char (if unflag-p ?\s dired-marker-char)))
cb3fe1f0
RS
556 (dired-mark-if
557 (and
558 ;; not already marked
559 (looking-at " ")
560 ;; uninteresting
561 (let ((fn (dired-get-filename localp t)))
562 (and fn (string-match regexp fn))))
563 msg)))
564
cb3fe1f0 565\f
b578f267 566;;; VIRTUAL DIRED MODE.
cb3fe1f0 567
efcb74f6 568;; For browsing `ls -lR' listings in a dired-like fashion.
cb3fe1f0 569
6fbad7af 570(defalias 'virtual-dired 'dired-virtual)
cb3fe1f0
RS
571(defun dired-virtual (dirname &optional switches)
572 "Put this buffer into Virtual Dired mode.
573
574In Virtual Dired mode, all commands that do not actually consult the
575filesystem will work.
576
577This is useful if you want to peruse and move around in an ls -lR
578output file, for example one you got from an ftp server. With
579ange-ftp, you can even dired a directory containing an ls-lR file,
580visit that file and turn on virtual dired mode. But don't try to save
581this file, as dired-virtual indents the listing and thus changes the
582buffer.
583
584If you have save a Dired buffer in a file you can use \\[dired-virtual] to
585resume it in a later session.
586
6fbad7af
JB
587Type \\<dired-mode-map>\\[revert-buffer] \
588in the Virtual Dired buffer and answer `y' to convert
589the virtual to a real dired buffer again. You don't have to do this, though:
590you can relist single subdirs using \\[dired-do-redisplay]."
cb3fe1f0
RS
591
592 ;; DIRNAME is the top level directory of the buffer. It will become
593 ;; its `default-directory'. If nil, the old value of
594 ;; default-directory is used.
595
596 ;; Optional SWITCHES are the ls switches to use.
597
598 ;; Shell wildcards will be used if there already is a `wildcard'
599 ;; line in the buffer (thus it is a saved Dired buffer), but there
600 ;; is no other way to get wildcards. Insert a `wildcard' line by
601 ;; hand if you want them.
602
603 (interactive
604 (list (read-string "Virtual Dired directory: " (dired-virtual-guess-dir))))
605 (goto-char (point-min))
606 (or (looking-at " ")
607 ;; if not already indented, do it now:
608 (indent-region (point-min) (point-max) 2))
609 (or dirname (setq dirname default-directory))
610 (setq dirname (expand-file-name (file-name-as-directory dirname)))
611 (setq default-directory dirname) ; contains no wildcards
612 (let ((wildcard (save-excursion
613 (goto-char (point-min))
614 (forward-line 1)
615 (and (looking-at "^ wildcard ")
616 (buffer-substring (match-end 0)
da5e0ce4 617 (line-end-position))))))
cb3fe1f0
RS
618 (if wildcard
619 (setq dirname (expand-file-name wildcard default-directory))))
620 ;; If raw ls listing (not a saved old dired buffer), give it a
621 ;; decent subdir headerline:
622 (goto-char (point-min))
623 (or (looking-at dired-subdir-regexp)
e8425548 624 (insert " "
fc50a3be
MY
625 (directory-file-name (file-name-directory default-directory))
626 ":\n"))
cb3fe1f0
RS
627 (dired-mode dirname (or switches dired-listing-switches))
628 (setq mode-name "Virtual Dired"
629 revert-buffer-function 'dired-virtual-revert)
630 (set (make-local-variable 'dired-subdir-alist) nil)
631 (dired-build-subdir-alist)
632 (goto-char (point-min))
633 (dired-initial-position dirname))
634
635(defun dired-virtual-guess-dir ()
b847eb8c 636 "Guess and return appropriate working directory of this buffer.
4e6ef391 637The buffer is assumed to be in Dired or ls -lR format. The guess is
b847eb8c
DL
638based upon buffer contents. If nothing could be guessed, returns
639nil."
cb3fe1f0
RS
640
641 (let ((regexp "^\\( \\)?\\([^ \n\r]*\\)\\(:\\)[\n\r]")
642 (subexpr 2))
643 (goto-char (point-min))
644 (cond ((looking-at regexp)
645 ;; If a saved dired buffer, look to which dir and
646 ;; perhaps wildcard it belongs:
647 (let ((dir (buffer-substring (match-beginning subexpr)
648 (match-end subexpr))))
649 (file-name-as-directory dir)))
650 ;; Else no match for headerline found. It's a raw ls listing.
651 ;; In raw ls listings the directory does not have a headerline
652 ;; try parent of first subdir, if any
653 ((re-search-forward regexp nil t)
654 (file-name-directory
655 (directory-file-name
656 (file-name-as-directory
657 (buffer-substring (match-beginning subexpr)
658 (match-end subexpr))))))
659 (t ; if all else fails
660 nil))))
661
662
663(defun dired-virtual-revert (&optional arg noconfirm)
664 (if (not
665 (y-or-n-p "Cannot revert a Virtual Dired buffer - switch to Real Dired mode? "))
b847eb8c 666 (error "Cannot revert a Virtual Dired buffer")
cb3fe1f0
RS
667 (setq mode-name "Dired"
668 revert-buffer-function 'dired-revert)
669 (revert-buffer)))
670
671;; A zero-arg version of dired-virtual.
cb3fe1f0 672(defun dired-virtual-mode ()
6fbad7af 673 "Put current buffer into Virtual Dired mode (see `dired-virtual').
b94d76ae 674Useful on `magic-mode-alist' with the regexp
cb3fe1f0 675
b94d76ae 676 \"^ \\\\(/[^ /]+\\\\)+/?:$\"
cb3fe1f0 677
6fbad7af 678to put saved dired buffers automatically into Virtual Dired mode.
cb3fe1f0 679
b94d76ae 680Also useful for `auto-mode-alist' like this:
cb3fe1f0 681
b94d76ae
GM
682 (add-to-list 'auto-mode-alist
683 '(\"[^/]\\\\.dired\\\\'\" . dired-virtual-mode))"
cb3fe1f0
RS
684 (interactive)
685 (dired-virtual (dired-virtual-guess-dir)))
686
687\f
b578f267 688;;; SMART SHELL.
cb3fe1f0 689
efcb74f6
SM
690;; An Emacs buffer can have but one working directory, stored in the
691;; buffer-local variable `default-directory'. A Dired buffer may have
692;; several subdirectories inserted, but still has but one working directory:
693;; that of the top level Dired directory in that buffer. For some commands
694;; it is appropriate that they use the current Dired directory instead of
695;; `default-directory', e.g., `find-file' and `compile'. This is a general
696;; mechanism is provided for special handling of the working directory in
697;; special major modes.
cb3fe1f0 698
da5e0ce4
GM
699(define-obsolete-variable-alias 'default-directory-alist
700 'dired-default-directory-alist "24.1")
701
cb3fe1f0
RS
702;; It's easier to add to this alist than redefine function
703;; default-directory while keeping the old information.
da5e0ce4 704(defconst dired-default-directory-alist
cb3fe1f0
RS
705 '((dired-mode . (if (fboundp 'dired-current-directory)
706 (dired-current-directory)
707 default-directory)))
b847eb8c
DL
708 "Alist of major modes and their opinion on `default-directory'.
709This is given as a Lisp expression to evaluate. A resulting value of
710nil is ignored in favor of `default-directory'.")
cb3fe1f0 711
b847eb8c
DL
712(defun dired-default-directory ()
713 "Usage like variable `default-directory'.
da5e0ce4
GM
714Knows about the special cases in variable `dired-default-directory-alist'."
715 (or (eval (cdr (assq major-mode dired-default-directory-alist)))
cb3fe1f0
RS
716 default-directory))
717
d3a89b9e 718(defun dired-smart-shell-command (command &optional output-buffer error-buffer)
6fbad7af 719 "Like function `shell-command', but in the current Virtual Dired directory."
d3a89b9e
JL
720 (interactive
721 (list
57199d9b
JL
722 (read-shell-command "Shell command: " nil nil
723 (cond
724 (buffer-file-name (file-relative-name buffer-file-name))
725 ((eq major-mode 'dired-mode) (dired-get-filename t t))))
d3a89b9e
JL
726 current-prefix-arg
727 shell-command-default-error-buffer))
b847eb8c 728 (let ((default-directory (dired-default-directory)))
d3a89b9e 729 (shell-command command output-buffer error-buffer)))
cb3fe1f0
RS
730
731\f
b578f267 732;;; LOCAL VARIABLES FOR DIRED BUFFERS.
cb3fe1f0 733
efcb74f6 734;; Brief Description:
9572a9dd 735;;;
efcb74f6 736;; * `dired-extra-startup' is part of the `dired-mode-hook'.
9572a9dd 737;;;
efcb74f6 738;; * `dired-extra-startup' calls `dired-hack-local-variables'
9572a9dd 739;;;
efcb74f6 740;; * `dired-hack-local-variables' checks the value of
9572a9dd
KH
741;;; `dired-local-variables-file'
742;;;
efcb74f6 743;; * Check if `dired-local-variables-file' is a non-nil string and is a
cb3fe1f0 744;;; filename found in the directory of the Dired Buffer being created.
9572a9dd 745;;;
efcb74f6 746;; * If `dired-local-variables-file' satisfies the above, then temporarily
cb3fe1f0 747;;; include it in the Dired Buffer at the bottom.
9572a9dd 748;;;
efcb74f6 749;; * Set `enable-local-variables' temporarily to the user variable
cb3fe1f0
RS
750;;; `dired-enable-local-variables' and run `hack-local-variables' on the
751;;; Dired Buffer.
752
461b69ae
GM
753;; FIXME do standard dir-locals obsolete this?
754(defcustom dired-local-variables-file (convert-standard-filename ".dired")
cb3fe1f0
RS
755 "Filename, as string, containing local dired buffer variables to be hacked.
756If this file found in current directory, then it will be inserted into dired
6fbad7af
JB
757buffer and `hack-local-variables' will be run. See Info node
758`(emacs)File Variables' for more information on local variables.
461b69ae
GM
759See also `dired-enable-local-variables'."
760 :type 'file
761 :group 'dired)
cb3fe1f0
RS
762
763(defun dired-hack-local-variables ()
764 "Evaluate local variables in `dired-local-variables-file' for dired buffer."
d1be20a1
GM
765 (and (stringp dired-local-variables-file)
766 (file-exists-p dired-local-variables-file)
767 (let ((opoint (point-max))
768 (inhibit-read-only t)
769 ;; In case user has `enable-local-variables' set to nil we
770 ;; override it locally with dired's variable.
771 (enable-local-variables dired-enable-local-variables))
772 ;; Insert 'em.
773 (save-excursion
774 (goto-char opoint)
775 (insert "\^L\n")
776 (insert-file-contents dired-local-variables-file))
777 ;; Hack 'em.
778 (let ((buffer-file-name dired-local-variables-file))
779 (hack-local-variables))
780 ;; Make sure that the modeline shows the proper information.
781 (dired-sort-set-modeline)
782 ;; Delete this stuff: `eobp' is used to find last subdir by dired.el.
783 (delete-region opoint (point-max)))))
cb3fe1f0 784
9572a9dd 785(defun dired-omit-here-always ()
b847eb8c 786 "Create `dired-local-variables-file' for omitting and reverts directory.
6fbad7af 787Sets `dired-omit-mode' to t in a local variables file that is readable by
cb3fe1f0 788dired."
9572a9dd 789 (interactive)
cb3fe1f0
RS
790 (if (file-exists-p dired-local-variables-file)
791 (message "File `./%s' already exists." dired-local-variables-file)
cb3fe1f0 792 ;; Create `dired-local-variables-file'.
efcb74f6 793 (with-current-buffer (get-buffer-create " *dot-dired*")
cb3fe1f0 794 (erase-buffer)
ce64e046 795 (insert "Local Variables:\ndired-omit-mode: t\nEnd:\n")
cb3fe1f0 796 (write-file dired-local-variables-file)
d1be20a1 797 (kill-buffer))
cb3fe1f0
RS
798 ;; Run extra-hooks and revert directory.
799 (dired-extra-startup)
800 (dired-revert)))
801
802\f
b578f267 803;;; GUESS SHELL COMMAND.
cb3fe1f0 804
efcb74f6 805;; Brief Description:
9572a9dd 806;;;
efcb74f6 807;; `dired-do-shell-command' is bound to `!' by dired.el.
9572a9dd 808;;;
efcb74f6 809;; * Redefine `dired-read-shell-command' so it calls
cb3fe1f0 810;;; `dired-guess-shell-command'.
9572a9dd 811;;;
efcb74f6 812;; * `dired-guess-shell-command' calls `dired-guess-default' with list of
cb3fe1f0 813;;; marked files.
9572a9dd 814;;;
efcb74f6 815;; * Parse `dired-guess-shell-alist-user' and
cb3fe1f0
RS
816;;; `dired-guess-shell-alist-default' (in that order) for the first REGEXP
817;;; that matches the first file in the file list.
9572a9dd 818;;;
efcb74f6 819;; * If the REGEXP matches all the entries of the file list then evaluate
c0d79871 820;;; COMMAND, which is either a string or a Lisp expression returning a
cb3fe1f0 821;;; string. COMMAND may be a list of commands.
9572a9dd 822;;;
efcb74f6 823;; * Return this command to `dired-guess-shell-command' which prompts user
747e8ee2 824;;; with it. The list of commands is put into the list of default values.
cb3fe1f0
RS
825;;; If a command is used successfully then it is stored permanently in
826;;; `dired-shell-command-history'.
827
efcb74f6 828;; Guess what shell command to apply to a file.
cb3fe1f0
RS
829(defvar dired-shell-command-history nil
830 "History list for commands that read dired-shell commands.")
831
efcb74f6 832;; Default list of shell commands.
cb3fe1f0 833
efcb74f6
SM
834;; NOTE: Use `gunzip -c' instead of `zcat' on `.gz' files. Some do not
835;; install GNU zip's version of zcat.
cb3fe1f0 836
461b69ae
GM
837(declare-function Man-support-local-filenames "man" ())
838
cb3fe1f0
RS
839(defvar dired-guess-shell-alist-default
840 (list
ecd69427 841 (list "\\.tar$"
ca136496
RF
842 '(if dired-guess-shell-gnutar
843 (concat dired-guess-shell-gnutar " xvf")
844 "tar xvf")
845 ;; Extract files into a separate subdirectory
846 '(if dired-guess-shell-gnutar
847 (concat "mkdir " (file-name-sans-extension file)
848 "; " dired-guess-shell-gnutar " -C "
849 (file-name-sans-extension file) " -xvf")
850 (concat "mkdir " (file-name-sans-extension file)
851 "; tar -C " (file-name-sans-extension file) " -xvf"))
852 ;; List archive contents.
853 '(if dired-guess-shell-gnutar
854 (concat dired-guess-shell-gnutar " tvf")
855 "tar tvf"))
cb3fe1f0
RS
856
857 ;; REGEXPS for compressed archives must come before the .Z rule to
858 ;; be recognized:
859 (list "\\.tar\\.Z$"
ca136496
RF
860 ;; Untar it.
861 '(if dired-guess-shell-gnutar
862 (concat dired-guess-shell-gnutar " zxvf")
863 (concat "zcat * | tar xvf -"))
864 ;; Optional conversion to gzip format.
865 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
866 " " dired-guess-shell-znew-switches))
cb3fe1f0
RS
867
868 ;; gzip'ed archives
2f017676 869 (list "\\.t\\(ar\\.\\)?gz$"
ca136496
RF
870 '(if dired-guess-shell-gnutar
871 (concat dired-guess-shell-gnutar " zxvf")
872 (concat "gunzip -qc * | tar xvf -"))
873 ;; Extract files into a separate subdirectory
874 '(if dired-guess-shell-gnutar
875 (concat "mkdir " (file-name-sans-extension file)
876 "; " dired-guess-shell-gnutar " -C "
877 (file-name-sans-extension file) " -zxvf")
878 (concat "mkdir " (file-name-sans-extension file)
879 "; gunzip -qc * | tar -C "
880 (file-name-sans-extension file) " -xvf -"))
881 ;; Optional decompression.
882 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q" ""))
883 ;; List archive contents.
884 '(if dired-guess-shell-gnutar
885 (concat dired-guess-shell-gnutar " ztvf")
886 (concat "gunzip -qc * | tar tvf -")))
ecd69427 887
7ba7b794 888 ;; bzip2'ed archives
ecd69427 889 (list "\\.t\\(ar\\.bz2\\|bz\\)$"
7ba7b794 890 "bunzip2 -c * | tar xvf -"
ca136496
RF
891 ;; Extract files into a separate subdirectory
892 '(concat "mkdir " (file-name-sans-extension file)
893 "; bunzip2 -c * | tar -C "
894 (file-name-sans-extension file) " -xvf -")
7ba7b794 895 ;; Optional decompression.
ca136496 896 "bunzip2")
cb3fe1f0 897
c10e0633
GM
898 ;; xz'ed archives
899 (list "\\.t\\(ar\\.\\)?xz$"
900 "unxz -c * | tar xvf -"
901 ;; Extract files into a separate subdirectory
902 '(concat "mkdir " (file-name-sans-extension file)
903 "; unxz -c * | tar -C "
904 (file-name-sans-extension file) " -xvf -")
905 ;; Optional decompression.
906 "unxz")
907
ecd69427
JL
908 '("\\.shar\\.Z$" "zcat * | unshar")
909 '("\\.shar\\.g?z$" "gunzip -qc * | unshar")
cb3fe1f0 910
85400f54 911 '("\\.e?ps$" "ghostview" "xloadimage" "lpr")
ecd69427 912 (list "\\.e?ps\\.g?z$" "gunzip -qc * | ghostview -"
ca136496
RF
913 ;; Optional decompression.
914 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
ecd69427 915 (list "\\.e?ps\\.Z$" "zcat * | ghostview -"
ca136496
RF
916 ;; Optional conversion to gzip format.
917 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
918 " " dired-guess-shell-znew-switches))
ecd69427 919
cb3fe1f0 920 '("\\.patch$" "cat * | patch")
ecd69427 921 (list "\\.patch\\.g?z$" "gunzip -qc * | patch"
ca136496
RF
922 ;; Optional decompression.
923 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
ecd69427 924 (list "\\.patch\\.Z$" "zcat * | patch"
ca136496
RF
925 ;; Optional conversion to gzip format.
926 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
927 " " dired-guess-shell-znew-switches))
cb3fe1f0 928
ecd69427 929 ;; The following four extensions are useful with dired-man ("N" key)
d2ea84be 930 (list "\\.\\(?:[0-9]\\|man\\)$" '(progn (require 'man)
ca136496
RF
931 (if (Man-support-local-filenames)
932 "man -l"
933 "cat * | tbl | nroff -man -h")))
d2ea84be 934 (list "\\.\\(?:[0-9]\\|man\\)\\.g?z$" '(progn (require 'man)
ca136496
RF
935 (if (Man-support-local-filenames)
936 "man -l"
937 "gunzip -qc * | tbl | nroff -man -h"))
938 ;; Optional decompression.
939 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
ecd69427 940 (list "\\.[0-9]\\.Z$" '(progn (require 'man)
ca136496
RF
941 (if (Man-support-local-filenames)
942 "man -l"
943 "zcat * | tbl | nroff -man -h"))
944 ;; Optional conversion to gzip format.
945 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
946 " " dired-guess-shell-znew-switches))
ecd69427
JL
947 '("\\.pod$" "perldoc" "pod2man * | nroff -man")
948
ca136496
RF
949 '("\\.dvi$" "xdvi" "dvips") ; preview and printing
950 '("\\.au$" "play") ; play Sun audiofiles
951 '("\\.mpe?g$\\|\\.avi$" "xine -p")
70ee5ed1
GM
952 '("\\.ogg$" "ogg123")
953 '("\\.mp3$" "mpg123")
ca136496
RF
954 '("\\.wav$" "play")
955 '("\\.uu$" "uudecode") ; for uudecoded files
cb3fe1f0 956 '("\\.hqx$" "mcvert")
ca136496
RF
957 '("\\.sh$" "sh") ; execute shell scripts
958 '("\\.xbm$" "bitmap") ; view X11 bitmaps
cb3fe1f0 959 '("\\.gp$" "gnuplot")
85400f54 960 '("\\.p[bgpn]m$" "xloadimage")
ca136496 961 '("\\.gif$" "xloadimage") ; view gif pictures
85400f54 962 '("\\.tif$" "xloadimage")
b847eb8c 963 '("\\.png$" "display") ; xloadimage 4.1 doesn't grok PNG
ecd69427 964 '("\\.jpe?g$" "xloadimage")
ca136496
RF
965 '("\\.fig$" "xfig") ; edit fig pictures
966 '("\\.out$" "xgraph") ; for plotting purposes.
cb3fe1f0
RS
967 '("\\.tex$" "latex" "tex")
968 '("\\.texi\\(nfo\\)?$" "makeinfo" "texi2dvi")
ca136496
RF
969 '("\\.pdf$" "xpdf")
970 '("\\.doc$" "antiword" "strings")
971 '("\\.rpm$" "rpm -qilp" "rpm -ivh")
972 '("\\.dia$" "dia")
973 '("\\.mgp$" "mgp")
cb3fe1f0
RS
974
975 ;; Some other popular archivers.
ca136496
RF
976 (list "\\.zip$" "unzip" "unzip -l"
977 ;; Extract files into a separate subdirectory
978 '(concat "unzip" (if dired-guess-shell-gzip-quiet " -q")
979 " -d " (file-name-sans-extension file)))
cb3fe1f0 980 '("\\.zoo$" "zoo x//")
cb3fe1f0
RS
981 '("\\.lzh$" "lharc x")
982 '("\\.arc$" "arc x")
983 '("\\.shar$" "unshar")
984
985 ;; Compression.
986 (list "\\.g?z$" '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
ecd69427 987 (list "\\.dz$" "dictunzip")
7ba7b794 988 (list "\\.bz2$" "bunzip2")
c10e0633 989 (list "\\.xz$" "unxz")
cb3fe1f0 990 (list "\\.Z$" "uncompress"
ca136496
RF
991 ;; Optional conversion to gzip format.
992 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
993 " " dired-guess-shell-znew-switches))
994
995 '("\\.sign?$" "gpg --verify"))
9572a9dd 996
cb3fe1f0 997 "Default alist used for shell command guessing.
b847eb8c 998See `dired-guess-shell-alist-user'.")
cb3fe1f0 999
b847eb8c
DL
1000(defcustom dired-guess-shell-alist-user nil
1001 "User-defined alist of rules for suggested commands.
1002These rules take precedence over the predefined rules in the variable
cb3fe1f0
RS
1003`dired-guess-shell-alist-default' (to which they are prepended).
1004
1005Each element of this list looks like
1006
1007 \(REGEXP COMMAND...\)
1008
6fbad7af 1009where each COMMAND can either be a string or a Lisp expression that evaluates
cb3fe1f0
RS
1010to a string. If several COMMANDs are given, the first one will be the default
1011and the rest will be added temporarily to the history and can be retrieved
1012with \\[previous-history-element] (M-p) .
1013
70ee5ed1
GM
1014The variable `dired-guess-shell-case-fold-search' controls whether
1015REGEXP is matched case-sensitively.
1016
cb3fe1f0
RS
1017You can set this variable in your ~/.emacs. For example, to add rules for
1018`.foo' and `.bar' files, write
1019
1020 \(setq dired-guess-shell-alist-user
b847eb8c 1021 (list (list \"\\\\.foo\\\\'\" \"FOO-COMMAND\");; fixed rule
cb3fe1f0 1022 ;; possibly more rules ...
de5af6bb 1023 (list \"\\\\.bar\\\\'\";; rule with condition test
cb3fe1f0
RS
1024 '(if condition
1025 \"BAR-COMMAND-1\"
b847eb8c
DL
1026 \"BAR-COMMAND-2\")))\)"
1027 :group 'dired-x
1028 :type '(alist :key-type regexp :value-type (repeat sexp)))
cb3fe1f0 1029
d7af463c
RF
1030(defcustom dired-guess-shell-case-fold-search t
1031 "If non-nil, `dired-guess-shell-alist-default' and
1032`dired-guess-shell-alist-user' are matched case-insensitively."
1033 :group 'dired-x
1034 :type 'boolean)
1035
cb3fe1f0 1036(defun dired-guess-default (files)
b847eb8c
DL
1037 "Guess a shell commands for FILES. Return command or list of commands.
1038See `dired-guess-shell-alist-user'."
cb3fe1f0 1039
d7af463c 1040 (let* ((case-fold-search dired-guess-shell-case-fold-search)
cb3fe1f0
RS
1041 ;; Prepend the user's alist to the default alist.
1042 (alist (append dired-guess-shell-alist-user
1043 dired-guess-shell-alist-default))
1044 (file (car files))
1045 (flist (cdr files))
1046 elt regexp cmds)
1047
1048 ;; Find the first match in the alist for first file in FILES.
1049 (while alist
1050 (setq elt (car alist)
1051 regexp (car elt)
1052 alist (cdr alist))
1053 (if (string-match regexp file)
1054 (setq cmds (cdr elt)
1055 alist nil)))
1056
1057 ;; If more than one file, see if all of FILES match regular expression.
1058 (while (and flist
1059 (string-match regexp (car flist)))
1060 (setq flist (cdr flist)))
9572a9dd 1061
cb3fe1f0
RS
1062 ;; If flist is still non-nil, then do not guess since this means that not
1063 ;; all the files in FILES were matched by the regexp.
1064 (setq cmds (and (not flist) cmds))
1065
9572a9dd
KH
1066 ;; Return commands or nil if flist is still non-nil.
1067 ;; Evaluate the commands in order that any logical testing will be done.
d1be20a1
GM
1068 (if (cdr cmds)
1069 (mapcar #'eval cmds)
1070 (eval (car cmds))))) ; single command
cb3fe1f0
RS
1071
1072(defun dired-guess-shell-command (prompt files)
b847eb8c 1073 "Ask user with PROMPT for a shell command, guessing a default from FILES."
cb3fe1f0 1074 (let ((default (dired-guess-default files))
5420b514 1075 default-list val)
cb3fe1f0
RS
1076 (if (null default)
1077 ;; Nothing to guess
c521381a 1078 (read-shell-command prompt nil 'dired-shell-command-history)
cb3fe1f0 1079 (if (listp default)
cb3fe1f0
RS
1080 ;; More than one guess
1081 (setq default-list default
1082 default (car default)
1083 prompt (concat
1084 prompt
1085 (format "{%d guesses} " (length default-list))))
cb3fe1f0
RS
1086 ;; Just one guess
1087 (setq default-list (list default)))
5420b514
JL
1088 ;; Put the first guess in the prompt but not in the initial value.
1089 (setq prompt (concat prompt (format "[%s] " default)))
1090 ;; All guesses can be retrieved with M-n
c521381a
JL
1091 (setq val (read-shell-command prompt nil
1092 'dired-shell-command-history
1093 default-list))
5420b514
JL
1094 ;; If we got a return, then return default.
1095 (if (equal val "") default val))))
cb3fe1f0 1096
efcb74f6
SM
1097;; REDEFINE.
1098;; Redefine dired-aux.el's version:
cb3fe1f0 1099(defun dired-read-shell-command (prompt arg files)
5a0c3f56 1100 "Read a dired shell command prompting with PROMPT (using `read-shell-command').
b847eb8c 1101ARG is the prefix arg and may be used to indicate in the prompt which
c521381a 1102FILES are affected.
b847eb8c 1103This is an extra function so that you can redefine it."
c521381a
JL
1104 (minibuffer-with-setup-hook
1105 (lambda ()
1106 (set (make-local-variable 'minibuffer-default-add-function)
1107 'minibuffer-default-add-dired-shell-commands))
1108 (dired-mark-pop-up
1109 nil 'shell files
1110 'dired-guess-shell-command
1111 (format prompt (dired-mark-prompt arg files)) ; PROMPT
1112 files))) ; FILES
cb3fe1f0
RS
1113
1114\f
b578f267 1115;;; RELATIVE SYMBOLIC LINKS.
cb3fe1f0 1116
e8425548
JB
1117(declare-function make-symbolic-link "fileio.c")
1118
cb3fe1f0
RS
1119(defvar dired-keep-marker-relsymlink ?S
1120 "See variable `dired-keep-marker-move'.")
1121
1122(defun dired-make-relative-symlink (file1 file2 &optional ok-if-already-exists)
b847eb8c 1123 "Make a symbolic link (pointing to FILE1) in FILE2.
cb3fe1f0
RS
1124The link is relative (if possible), for example
1125
1126 \"/vol/tex/bin/foo\" \"/vol/local/bin/foo\"
1127
1128results in
1129
b847eb8c 1130 \"../../tex/bin/foo\" \"/vol/local/bin/foo\""
cb3fe1f0
RS
1131 (interactive "FRelSymLink: \nFRelSymLink %s: \np")
1132 (let (name1 name2 len1 len2 (index 0) sub)
1133 (setq file1 (expand-file-name file1)
1134 file2 (expand-file-name file2)
1135 len1 (length file1)
1136 len2 (length file2))
470fa6d1 1137 ;; Find common initial file name components:
cb3fe1f0
RS
1138 (let (next)
1139 (while (and (setq next (string-match "/" file1 index))
d1be20a1 1140 (< (setq next (1+ next)) (min len1 len2))
cb3fe1f0
RS
1141 ;; For the comparison, both substrings must end in
1142 ;; `/', so NEXT is *one plus* the result of the
1143 ;; string-match.
1144 ;; E.g., consider the case of linking "/tmp/a/abc"
34ff2275 1145 ;; to "/tmp/abc" erroneously giving "/tmp/a" instead
cb3fe1f0
RS
1146 ;; of "/tmp/" as common initial component
1147 (string-equal (substring file1 0 next)
1148 (substring file2 0 next)))
1149 (setq index next))
1150 (setq name2 file2
1151 sub (substring file1 0 index)
1152 name1 (substring file1 index)))
1153 (if (string-equal sub "/")
470fa6d1 1154 ;; No common initial file name found
cb3fe1f0
RS
1155 (setq name1 file1)
1156 ;; Else they have a common parent directory
1157 (let ((tem (substring file2 index))
1158 (start 0)
1159 (count 0))
1160 ;; Count number of slashes we must compensate for ...
1161 (while (setq start (string-match "/" tem start))
1162 (setq count (1+ count)
1163 start (1+ start)))
1164 ;; ... and prepend a "../" for each slash found:
d1be20a1
GM
1165 (dotimes (n count)
1166 (setq name1 (concat "../" name1)))))
cb3fe1f0
RS
1167 (make-symbolic-link
1168 (directory-file-name name1) ; must not link to foo/
1169 ; (trailing slash!)
1170 name2 ok-if-already-exists)))
1171
5e5eaeda 1172;;;###autoload
cb3fe1f0 1173(defun dired-do-relsymlink (&optional arg)
b847eb8c
DL
1174 "Relative symlink all marked (or next ARG) files into a directory.
1175Otherwise make a relative symbolic link to the current file.
cb3fe1f0
RS
1176This creates relative symbolic links like
1177
1178 foo -> ../bar/foo
1179
1180not absolute ones like
1181
42496dc8
EZ
1182 foo -> /ugly/file/name/that/may/change/any/day/bar/foo
1183
4d4238cc 1184For absolute symlinks, use \\[dired-do-symlink]."
cb3fe1f0 1185 (interactive "P")
d1be20a1 1186 (dired-do-create-files 'relsymlink #'dired-make-relative-symlink
cb3fe1f0
RS
1187 "RelSymLink" arg dired-keep-marker-relsymlink))
1188
d8e396ce 1189(defun dired-do-relsymlink-regexp (regexp newname &optional arg whole-name)
cb3fe1f0
RS
1190 "RelSymlink all marked files containing REGEXP to NEWNAME.
1191See functions `dired-do-rename-regexp' and `dired-do-relsymlink'
1192for more info."
1193 (interactive (dired-mark-read-regexp "RelSymLink"))
1194 (dired-do-create-files-regexp
d1be20a1 1195 #'dired-make-relative-symlink
d8e396ce 1196 "RelSymLink" arg regexp newname whole-name dired-keep-marker-relsymlink))
cb3fe1f0
RS
1197
1198\f
b578f267 1199;;; VISIT ALL MARKED FILES SIMULTANEOUSLY.
cb3fe1f0 1200
efcb74f6 1201;; Brief Description:
9572a9dd 1202;;;
efcb74f6 1203;; `dired-do-find-marked-files' is bound to `F' by dired-x.el.
9572a9dd 1204;;;
efcb74f6 1205;; * Use `dired-get-marked-files' to collect the marked files in the current
9572a9dd
KH
1206;;; Dired Buffer into a list of filenames `FILE-LIST'.
1207;;;
efcb74f6 1208;; * Pass FILE-LIST to `dired-simultaneous-find-file' all with
cb3fe1f0 1209;;; `dired-do-find-marked-files''s prefix argument NOSELECT.
9572a9dd 1210;;;
efcb74f6 1211;; * `dired-simultaneous-find-file' runs through FILE-LIST decrementing the
cb3fe1f0 1212;;; list each time.
9572a9dd 1213;;;
5a0c3f56 1214;; * If NOSELECT is non-nil then just run `find-file-noselect' on each
cb3fe1f0 1215;;; element of FILE-LIST.
9572a9dd 1216;;;
efcb74f6 1217;; * If NOSELECT is nil then calculate the `size' of the window for each file
cb3fe1f0
RS
1218;;; by dividing the `window-height' by length of FILE-LIST. Thus, `size' is
1219;;; cognizant of the window-configuration.
9572a9dd 1220;;;
efcb74f6 1221;; * If `size' is too small abort, otherwise run `find-file' on each element
cb3fe1f0
RS
1222;;; of FILE-LIST giving each a window of height `size'.
1223
1224(defun dired-do-find-marked-files (&optional noselect)
1225 "Find all marked files displaying all of them simultaneously.
1226With optional NOSELECT just find files but do not select them.
1227
1228The current window is split across all files marked, as evenly as possible.
1229Remaining lines go to bottom-most window. The number of files that can be
1230displayed this way is restricted by the height of the current window and
1231`window-min-height'.
1232
9572a9dd 1233To keep dired buffer displayed, type \\[split-window-vertically] first.
cb3fe1f0 1234To display just marked files, type \\[delete-other-windows] first."
cb3fe1f0
RS
1235 (interactive "P")
1236 (dired-simultaneous-find-file (dired-get-marked-files) noselect))
1237
1238(defun dired-simultaneous-find-file (file-list noselect)
b847eb8c
DL
1239 "Visit all files in FILE-LIST and display them simultaneously.
1240The current window is split across all files in FILE-LIST, as evenly as
1241possible. Remaining lines go to the bottom-most window. The number of
1242files that can be displayed this way is restricted by the height of the
1243current window and the variable `window-min-height'. With non-nil
1244NOSELECT the files are merely found but not selected."
cb3fe1f0
RS
1245 ;; We don't make this function interactive because it is usually too clumsy
1246 ;; to specify FILE-LIST interactively unless via dired.
cb3fe1f0 1247 (let (size)
cb3fe1f0
RS
1248 (if noselect
1249 ;; Do not select the buffer.
1250 (find-file-noselect (car file-list))
cb3fe1f0
RS
1251 ;; We will have to select the buffer. Calculate and check window size.
1252 (setq size (/ (window-height) (length file-list)))
1253 (or (<= window-min-height size)
b847eb8c 1254 (error "Too many files to visit simultaneously. Try C-u prefix"))
cb3fe1f0 1255 (find-file (car file-list)))
cb3fe1f0 1256 ;; Decrement.
d1be20a1 1257 (dolist (file (cdr file-list))
cb3fe1f0
RS
1258 (if noselect
1259 ;; Do not select the buffer.
d1be20a1 1260 (find-file-noselect file)
cb3fe1f0
RS
1261 ;; Vertically split off a window of desired size. Upper window will
1262 ;; have SIZE lines. Select lower (larger) window. We split it again.
1263 (select-window (split-window nil size))
d1be20a1 1264 (find-file file)))))
cb3fe1f0
RS
1265
1266\f
b578f267 1267;;; MISCELLANEOUS COMMANDS.
cb3fe1f0 1268
efcb74f6 1269;; Run man on files.
9572a9dd 1270
461b69ae
GM
1271(declare-function Man-getpage-in-background "man" (topic))
1272
cb3fe1f0 1273(defun dired-man ()
da5e0ce4
GM
1274 "Run `man' on this file."
1275;; Used also to say: "Display old buffer if buffer name matches filename."
1276;; but I have no idea what that means.
cb3fe1f0 1277 (interactive)
685ff9f8 1278 (require 'man)
ecd69427
JL
1279 (let* ((file (dired-get-filename))
1280 (manual-program (replace-regexp-in-string "\\*" "%s"
1281 (dired-guess-shell-command
1282 "Man command: " (list file)))))
685ff9f8 1283 (Man-getpage-in-background file)))
cb3fe1f0 1284
efcb74f6 1285;; Run Info on files.
cb3fe1f0
RS
1286
1287(defun dired-info ()
da5e0ce4 1288 "Run `info' on this file."
cb3fe1f0
RS
1289 (interactive)
1290 (info (dired-get-filename)))
1291
efcb74f6 1292;; Run mail on mail folders.
cb3fe1f0 1293
461b69ae
GM
1294(declare-function vm-visit-folder "ext:vm" (folder &optional read-only))
1295(defvar vm-folder-directory)
9572a9dd 1296
cb3fe1f0
RS
1297(defun dired-vm (&optional read-only)
1298 "Run VM on this file.
da5e0ce4
GM
1299With optional prefix argument, visits the folder read-only.
1300Otherwise obeys the value of `dired-vm-read-only-folders'."
cb3fe1f0
RS
1301 (interactive "P")
1302 (let ((dir (dired-current-directory))
1303 (fil (dired-get-filename)))
da5e0ce4
GM
1304 (vm-visit-folder fil (or read-only
1305 (eq t dired-vm-read-only-folders)
1306 (and dired-vm-read-only-folders
1307 (not (file-writable-p fil)))))
1308 ;; So that pressing `v' inside VM does prompt within current directory:
cb3fe1f0
RS
1309 (set (make-local-variable 'vm-folder-directory) dir)))
1310
1311(defun dired-rmail ()
1312 "Run RMAIL on this file."
1313 (interactive)
1314 (rmail (dired-get-filename)))
1315
1316(defun dired-do-run-mail ()
fc14288b 1317 "If `dired-bind-vm' is non-nil, call `dired-vm', else call `dired-rmail'."
cb3fe1f0
RS
1318 (interactive)
1319 (if dired-bind-vm
1320 ;; Read mail folder using vm.
1321 (dired-vm)
1322 ;; Read mail folder using rmail.
1323 (dired-rmail)))
1324
1325\f
b578f267 1326;;; MISCELLANEOUS INTERNAL FUNCTIONS.
cb3fe1f0 1327
552939e4 1328(declare-function dired-old-find-buffer-nocreate "dired-x")
e637bdf8 1329
cb3fe1f0
RS
1330(or (fboundp 'dired-old-find-buffer-nocreate)
1331 (fset 'dired-old-find-buffer-nocreate
1332 (symbol-function 'dired-find-buffer-nocreate)))
1333
efcb74f6
SM
1334;; REDEFINE.
1335;; Redefines dired.el's version of `dired-find-buffer-nocreate'
ac1ce341 1336(defun dired-find-buffer-nocreate (dirname &optional mode)
10774fb5
KH
1337 (if (and dired-find-subdir
1338 ;; don't try to find a wildcard as a subdirectory
1339 (string-equal dirname (file-name-directory dirname)))
cb3fe1f0 1340 (let* ((cur-buf (current-buffer))
10774fb5 1341 (buffers (nreverse
12c38283 1342 (dired-buffers-for-dir (expand-file-name dirname))))
10774fb5
KH
1343 (cur-buf-matches (and (memq cur-buf buffers)
1344 ;; wildcards must match, too:
1345 (equal dired-directory dirname))))
1346 ;; We don't want to switch to the same buffer---
1347 (setq buffers (delq cur-buf buffers));;need setq with delq
d1be20a1 1348 (or (car (sort buffers #'dired-buffer-more-recently-used-p))
10774fb5
KH
1349 ;; ---unless it's the only possibility:
1350 (and cur-buf-matches cur-buf)))
ac1ce341 1351 (dired-old-find-buffer-nocreate dirname mode)))
cb3fe1f0
RS
1352
1353;; This should be a builtin
1354(defun dired-buffer-more-recently-used-p (buffer1 buffer2)
da5e0ce4
GM
1355 "Return t if BUFFER1 is more recently used than BUFFER2.
1356Considers buffers closer to the car of `buffer-list' to be more recent."
1357 (and (not (equal buffer1 buffer2))
1358 (memq buffer1 (buffer-list))
1359 (not (memq buffer1 (memq buffer2 (buffer-list))))))
cb3fe1f0 1360
efcb74f6
SM
1361;; Same thing as `dired-buffers-for-dir' of dired.el? - lrd 11/23/93
1362;; (defun dired-buffers-for-dir-exact (dir)
1363;; ;; Return a list of buffers that dired DIR (a directory or wildcard)
1364;; ;; at top level, or as subdirectory.
1365;; ;; Top level matches must match the wildcard part too, if any.
1366;; ;; The list is in reverse order of buffer creation, most recent last.
1367;; ;; As a side effect, killed dired buffers for DIR are removed from
1368;; ;; dired-buffers.
1369;; (let ((alist dired-buffers) result elt)
1370;; (while alist
1371;; (setq elt (car alist)
1372;; alist (cdr alist))
1373;; (let ((buf (cdr elt)))
1374;; (if (buffer-name buf)
1375;; ;; Top level must match exactly against dired-directory in
1376;; ;; case one of them is a wildcard.
1377;; (if (or (equal dir (with-current-buffer buf dired-directory))
1378;; (assoc dir (with-current-buffer buf dired-subdir-alist)))
1379;; (setq result (cons buf result)))
1380;; ;; else buffer is killed - clean up:
1381;; (setq dired-buffers (delq elt dired-buffers)))))
1382;; result))
1383
cb3fe1f0
RS
1384\f
1385;; Does anyone use this? - lrd 6/29/93.
44c816da 1386;; Apparently people do use it. - lrd 12/22/97.
cb3fe1f0
RS
1387(defun dired-mark-sexp (predicate &optional unflag-p)
1388 "Mark files for which PREDICATE returns non-nil.
1389With a prefix arg, unflag those files instead.
1390
1391PREDICATE is a lisp expression that can refer to the following symbols:
1392
1393 inode [integer] the inode of the file (only for ls -i output)
1394 s [integer] the size of the file for ls -s output
34ff2275 1395 (usually in blocks or, with -k, in KByte)
cb3fe1f0
RS
1396 mode [string] file permission bits, e.g. \"-rw-r--r--\"
1397 nlink [integer] number of links to file
1398 uid [string] owner
1399 gid [string] group (If the gid is not displayed by ls,
1400 this will still be set (to the same as uid))
1401 size [integer] file size in bytes
1402 time [string] the time that ls displays, e.g. \"Feb 12 14:17\"
1403 name [string] the name of the file
1404 sym [string] if file is a symbolic link, the linked-to name, else \"\"
1405
1406For example, use
1407
1408 (equal 0 size)
1409
1410to mark all zero length files."
1411 ;; Using sym="" instead of nil avoids the trap of
1412 ;; (string-match "foo" sym) into which a user would soon fall.
1413 ;; Give `equal' instead of `=' in the example, as this works on
1414 ;; integers and strings.
1415 (interactive "xMark if (lisp expr): \nP")
1416 (message "%s" predicate)
1417 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char))
1418 inode s mode nlink uid gid size time name sym)
1419 (dired-mark-if
1420 (save-excursion
1421 (and
1422 ;; Sets vars
1423 ;; inode s mode nlink uid gid size time name sym
1424
1425 ;; according to current file line. Returns t for success, nil if
1426 ;; there is no file line. Upon success, all variables are set, either
1427 ;; to nil or the appropriate value, so they need not be initialized.
1428 ;; Moves point within the current line.
d1be20a1
GM
1429 (dired-move-to-filename)
1430 (let (pos
1431 (mode-len 10) ; length of mode string
1432 ;; like in dired.el, but with subexpressions \1=inode, \2=s:
1433 (dired-re-inode-size "\\s *\\([0-9]*\\)\\s *\\([0-9]*\\) ?"))
1434 (beginning-of-line)
1435 (forward-char 2)
1436 (if (looking-at dired-re-inode-size)
1437 (progn
1438 (goto-char (match-end 0))
1439 (setq inode (string-to-number
1440 (buffer-substring (match-beginning 1)
1441 (match-end 1)))
1442 s (string-to-number
1443 (buffer-substring (match-beginning 2)
1444 (match-end 2)))))
1445 (setq inode nil
1446 s nil))
1447 (setq mode (buffer-substring (point) (+ mode-len (point))))
1448 (forward-char mode-len)
1449 (setq nlink (read (current-buffer)))
1450 ;; Karsten Wenger <kw@cis.uni-muenchen.de> fixed uid.
1451 (setq uid (buffer-substring (1+ (point))
1452 (progn (forward-word 1) (point))))
1453 (re-search-forward directory-listing-before-filename-regexp)
1454 (goto-char (match-beginning 1))
1455 (forward-char -1)
1456 (setq size (string-to-number
1457 (buffer-substring (save-excursion
1458 (backward-word 1)
1459 (setq pos (point)))
1460 (point))))
1461 (goto-char pos)
1462 (backward-word 1)
1463 ;; if no gid is displayed, gid will be set to uid
1464 ;; but user will then not reference it anyway in PREDICATE.
1465 (setq gid (buffer-substring (save-excursion
1466 (forward-word 1) (point))
1467 (point))
1468 time (buffer-substring (match-beginning 1)
1469 (1- (dired-move-to-filename)))
1470 name (buffer-substring (point)
1471 (or
1472 (dired-move-to-end-of-filename t)
1473 (point)))
1474 sym (if (looking-at " -> ")
1475 (buffer-substring (progn (forward-char 4) (point))
1476 (line-end-position))
1477 ""))
1478 t)
1479 (eval predicate)))
cb3fe1f0
RS
1480 (format "'%s file" predicate))))
1481
1482\f
b578f267 1483;;; FIND FILE AT POINT.
cb3fe1f0 1484
461b69ae 1485(defcustom dired-x-hands-off-my-keys t
d1be20a1
GM
1486 "Non-nil means don't remap `find-file' to `dired-x-find-file'.
1487Similarly for `find-file-other-window' and `dired-x-find-file-other-window'.
461b69ae
GM
1488If you change this variable without using \\[customize] after `dired-x.el'
1489is loaded then call \\[dired-x-bind-find-file]."
1490 :type 'boolean
1491 :initialize 'custom-initialize-default
1492 :set (lambda (sym val)
1493 (set sym val)
1494 (dired-x-bind-find-file))
1495 :group 'dired-x)
ccb1d39a 1496
ccb1d39a 1497(defun dired-x-bind-find-file ()
d1be20a1 1498 "Bind `dired-x-find-file' in place of `find-file' (or vice-versa).
ccb1d39a 1499Similarly for `dired-x-find-file-other-window' and `find-file-other-window'.
d1be20a1 1500Binding direction based on `dired-x-hands-off-my-keys'."
ccb1d39a 1501 (interactive)
32226619 1502 (if (called-interactively-p 'interactive)
ccb1d39a
RS
1503 (setq dired-x-hands-off-my-keys
1504 (not (y-or-n-p "Bind dired-x-find-file over find-file? "))))
d1be20a1
GM
1505 (define-key (current-global-map) [remap find-file]
1506 (if (not dired-x-hands-off-my-keys) 'dired-x-find-file))
1507 (define-key (current-global-map) [remap find-file-other-window]
1508 (if (not dired-x-hands-off-my-keys) 'dired-x-find-file-other-window)))
1509
1510;; Now call it so binding is correct. This could go in the :initialize
1511;; slot, but then dired-x-bind-find-file has to be defined before the
1512;; defcustom, and we get free variable warnings.
ccb1d39a 1513(dired-x-bind-find-file)
ccb1d39a
RS
1514
1515(defun dired-x-find-file (filename)
1516 "Edit file FILENAME.
d1be20a1
GM
1517Like `find-file', except that when called interactively with a
1518prefix argument, it offers the filename near point as a default."
da5e0ce4 1519 (interactive (list (dired-x-read-filename-at-point "Find file: ")))
d1be20a1 1520 (find-file filename))
ccb1d39a
RS
1521
1522(defun dired-x-find-file-other-window (filename)
1523 "Edit file FILENAME, in another window.
d1be20a1
GM
1524Like `find-file-other-window', except that when called interactively with
1525a prefix argument, when it offers the filename near point as a default."
da5e0ce4 1526 (interactive (list (dired-x-read-filename-at-point "Find file: ")))
d1be20a1 1527 (find-file-other-window filename))
ccb1d39a
RS
1528
1529;;; Internal functions.
cb3fe1f0 1530
1e56daa5 1531;; Fixme: This should probably use `thing-at-point'. -- fx
f53a06c3 1532(defun dired-filename-at-point ()
fc14288b
GM
1533 "Return the filename closest to point, expanded.
1534Point should be in or after a filename."
d1be20a1
GM
1535 (save-excursion
1536 ;; First see if just past a filename.
1537 (or (eobp) ; why?
1538 (when (looking-at "[] \t\n[{}()]") ; whitespace or some parens
1539 (skip-chars-backward " \n\t\r({[]})")
1540 (or (bobp) (backward-char 1))))
1541 (let ((filename-chars "-.[:alnum:]_/:$+@")
1542 start prefix)
fc14288b 1543 (if (looking-at (format "[%s]" filename-chars))
cb3fe1f0 1544 (progn
fc14288b
GM
1545 (skip-chars-backward filename-chars)
1546 (setq start (point)
1547 prefix
1548 ;; This is something to do with ange-ftp filenames.
1549 ;; It convert foo@bar to /foo@bar.
1550 ;; But when does the former occur in dired buffers?
1e56daa5
DL
1551 (and (string-match
1552 "^\\w+@"
8551cb32 1553 (buffer-substring start (line-end-position)))
6b622862 1554 "/"))
cb3fe1f0
RS
1555 (if (string-match "[/~]" (char-to-string (preceding-char)))
1556 (setq start (1- start)))
fc14288b 1557 (skip-chars-forward filename-chars))
cb3fe1f0 1558 (error "No file found around point!"))
9572a9dd 1559 ;; Return string.
6b622862 1560 (expand-file-name (concat prefix (buffer-substring start (point)))))))
cb3fe1f0 1561
da5e0ce4 1562(defun dired-x-read-filename-at-point (prompt)
b847eb8c
DL
1563 "Return filename prompting with PROMPT with completion.
1564If `current-prefix-arg' is non-nil, uses name at point as guess."
ccb1d39a
RS
1565 (if current-prefix-arg
1566 (let ((guess (dired-filename-at-point)))
1567 (read-file-name prompt
1568 (file-name-directory guess)
1569 guess
1570 nil (file-name-nondirectory guess)))
1571 (read-file-name prompt default-directory)))
da5e0ce4
GM
1572
1573(define-obsolete-function-alias 'read-filename-at-point
1574 'dired-x-read-filename-at-point "24.1") ; is this even needed?
cb3fe1f0 1575\f
b578f267 1576;;; BUG REPORTS
cb3fe1f0 1577
461b69ae 1578(define-obsolete-function-alias 'dired-x-submit-report 'report-emacs-bug "24.1")
cb3fe1f0
RS
1579
1580\f
1581;; As Barry Warsaw would say: "This might be useful..."
1582(provide 'dired-x)
1583
276f1d00
GM
1584;; Local Variables:
1585;; byte-compile-dynamic: t
1586;; generated-autoload-file: "dired.el"
1587;; End:
1588
cb3fe1f0 1589;;; dired-x.el ends here