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