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