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