Fix bug #9311 with loading on MS-Windows .elc files in root directories.
[emacs.git] / lisp / dired-x.el
blob0f2cfd4973fb2c99e30b809bed0028ccaed93e08
1 ;;; dired-x.el --- extra Dired functionality
3 ;; Copyright (C) 1993-1994, 1997, 2001-2011 Free Software Foundation, Inc.
5 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
6 ;; Lawrence R. Dodd <dodd@roebling.poly.edu>
7 ;; Maintainer: Romain Francoise <rfrancoise@gnu.org>
8 ;; Keywords: dired extensions files
9 ;; Package: emacs
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
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.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
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.
31 ;; USAGE: In your ~/.emacs,
33 ;; (add-hook 'dired-load-hook
34 ;; (lambda ()
35 ;; (load "dired-x")
36 ;; ;; Set global variables here. For example:
37 ;; ;; (setq dired-guess-shell-gnutar "gtar")
38 ;; ))
39 ;; (add-hook 'dired-mode-hook
40 ;; (lambda ()
41 ;; ;; Set buffer-local variables here. For example:
42 ;; ;; (dired-omit-mode 1)
43 ;; ))
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.
49 ;; User customization: M-x customize-group RET dired-x RET.
51 ;; *Please* see the `dired-x' info pages for more details.
54 ;;; Code:
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.
58 (require 'dired)
60 ;;; User-defined variables.
62 (defgroup dired-x nil
63 "Extended directory editing (dired-x)."
64 :group 'dired)
66 (defgroup dired-keys nil
67 "Dired keys customizations."
68 :prefix "dired-"
69 :group 'dired-x)
71 (defcustom dired-bind-vm nil
72 "Non-nil means \"V\" runs `dired-vm', otherwise \"V\" runs `dired-rmail'.
73 RMAIL files in the old Babyl format (used before before Emacs 23.1)
74 contain \"-*- rmail -*-\" at the top, so `dired-find-file'
75 will run `rmail' on these files. New RMAIL files use the standard
76 mbox format, and so cannot be distinguished in this way."
77 :type 'boolean
78 :group 'dired-keys)
80 (defcustom dired-bind-jump t
81 "Non-nil means bind `dired-jump' to C-x C-j, otherwise do not.
82 Setting this variable directly after dired-x is loaded has no effect -
83 use \\[customize]."
84 :type 'boolean
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))))
94 :group 'dired-keys)
96 (defcustom dired-bind-man t
97 "Non-nil means bind `dired-man' to \"N\" in dired-mode, otherwise do not.
98 Setting this variable directly after dired-x is loaded has no effect -
99 use \\[customize]."
100 :type 'boolean
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))))
106 :group 'dired-keys)
108 (defcustom dired-bind-info t
109 "Non-nil means bind `dired-info' to \"I\" in dired-mode, otherwise do not.
110 Setting this variable directly after dired-x is loaded has no effect -
111 use \\[customize]."
112 :type 'boolean
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))))
118 :group 'dired-keys)
120 (defcustom dired-vm-read-only-folders nil
121 "If non-nil, \\[dired-vm] will visit all folders read-only.
122 If neither nil nor t, e.g. the symbol `if-file-read-only', only
123 files not writable by you are visited read-only."
124 :type '(choice (const :tag "off" nil)
125 (const :tag "on" t)
126 (other :tag "non-writable only" if-file-read-only))
127 :group 'dired-x)
129 (defcustom dired-omit-size-limit 30000
130 "Maximum size for the \"omitting\" feature.
131 If nil, there is no maximum size."
132 :type '(choice (const :tag "no maximum" nil) integer)
133 :group 'dired-x)
135 (define-minor-mode dired-omit-mode
136 "Toggle Dired-Omit mode.
137 With numeric ARG, enable Dired-Omit mode if ARG is positive, disable
138 otherwise. Enabling and disabling is buffer-local.
139 If enabled, \"uninteresting\" files are not listed.
140 Uninteresting files are those whose filenames match regexp `dired-omit-files',
141 plus those ending with extensions in `dired-omit-extensions'.
143 To enable omitting in every Dired buffer, you can put in your ~/.emacs
145 (add-hook 'dired-mode-hook (lambda () (dired-omit-mode 1)))
147 See Info node `(dired-x) Omitting Variables' for more information."
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)))
154 (put 'dired-omit-mode 'safe-local-variable 'booleanp)
156 ;; For backward compatibility
157 (define-obsolete-variable-alias 'dired-omit-files-p 'dired-omit-mode "22.1")
159 (defcustom dired-omit-files "^\\.?#\\|^\\.$\\|^\\.\\.$"
160 "Filenames matching this regexp will not be displayed.
161 This only has effect when `dired-omit-mode' is t. See interactive function
162 `dired-omit-mode' \(\\[dired-omit-mode]\) and variable
163 `dired-omit-extensions'. The default is to omit `.', `..', auto-save
164 files and lock files."
165 :type 'regexp
166 :group 'dired-x)
168 (defcustom dired-omit-verbose t
169 "When non-nil, show messages when omitting files.
170 When nil, don't show messages."
171 :type 'boolean
172 :group 'dired-x)
174 (defcustom dired-find-subdir nil ; t is pretty near to DWIM...
175 "If non-nil, Dired always finds a directory in a buffer of its own.
176 If nil, Dired finds the directory as a subdirectory in some other buffer
177 if it is present as one.
179 If there are several dired buffers for a directory, the most recently
180 used is chosen.
182 Dired avoids switching to the current buffer, so that if you have
183 a normal and a wildcard buffer for the same directory, \\[dired] will
184 toggle between those two."
185 :type 'boolean
186 :group 'dired-x)
188 (defcustom dired-enable-local-variables t
189 "Control use of local-variables lists in Dired.
190 This temporarily overrides the value of `enable-local-variables' when
191 listing a directory. See also `dired-local-variables-file'."
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))
198 :group 'dired-x)
200 (make-obsolete-variable 'dired-enable-local-variables
201 "use a standard `dir-locals-file' instead." "24.1")
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))))
210 "If non-nil, name of GNU tar executable.
211 \(E.g., \"tar\" or \"gtar\"). The `z' switch will be used with it for
212 compressed or gzip'ed tar files. If you don't have GNU tar, set this
213 to nil: a pipe using `zcat' or `gunzip -c' will be used."
214 ;; Changed from system-type test to testing --version output.
215 ;; Maybe test --help for -z instead?
216 :version "24.1"
217 :type '(choice (const :tag "Not GNU tar" nil)
218 (string :tag "Command name"))
219 :group 'dired-x)
221 (defcustom dired-guess-shell-gzip-quiet t
222 "Non-nil says pass -q to gzip overriding verbose GZIP environment."
223 :type 'boolean
224 :group 'dired-x)
226 (defcustom dired-guess-shell-znew-switches nil
227 "If non-nil, then string of switches passed to `znew', example: \"-K\"."
228 :type '(choice (const :tag "None" nil)
229 (string :tag "Switches"))
230 :group 'dired-x)
232 (defcustom dired-clean-up-buffers-too t
233 "Non-nil means offer to kill buffers visiting files and dirs deleted in Dired."
234 :type 'boolean
235 :group 'dired-x)
237 ;;; KEY BINDINGS.
239 (define-key dired-mode-map "\M-o" 'dired-omit-mode)
240 (define-key dired-mode-map "*O" 'dired-mark-omitted)
241 (define-key dired-mode-map "\M-(" 'dired-mark-sexp)
242 (define-key dired-mode-map "*(" 'dired-mark-sexp)
243 (define-key dired-mode-map "*." 'dired-mark-extension)
244 (define-key dired-mode-map "\M-!" 'dired-smart-shell-command)
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)
251 ;;; MENU BINDINGS
253 (require 'easymenu)
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 \
264 marked 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' \
277 and `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 \
283 matching 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\" \
289 files"]
290 "Refresh"))
293 ;; Install into appropriate hooks.
295 (add-hook 'dired-mode-hook 'dired-extra-startup)
296 (add-hook 'dired-after-readin-hook 'dired-omit-expunge)
298 (defun dired-extra-startup ()
299 "Automatically put on `dired-mode-hook' to get extra Dired features:
300 \\<dired-mode-map>
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
305 \\[dired-omit-mode]\t-- toggle omitting of files
306 \\[dired-mark-sexp]\t-- mark by Lisp expression
308 To see the options you can set, use M-x customize-group RET dired-x RET.
309 See also the functions:
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'"
318 (interactive)
319 ;; These must be done in each new dired buffer.
320 (dired-hack-local-variables)
321 (dired-omit-startup))
324 ;;; EXTENSION MARKING FUNCTIONS.
326 ;; Mark files with some extension.
327 (defun dired-mark-extension (extension &optional marker-char)
328 "Mark all files with a certain EXTENSION for use in later commands.
329 A `.' is *not* automatically prepended to the string entered."
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))
342 (defun dired-flag-extension (extension)
343 "In dired, flag all files with a certain EXTENSION for deletion.
344 A `.' is *not* automatically prepended to the string entered."
345 (interactive "sFlagging extension: ")
346 (dired-mark-extension extension dired-del-marker))
348 ;; Define some unpopular file extensions. Used for cleaning and omitting.
350 (defvar dired-patch-unclean-extensions
351 '(".rej" ".orig")
352 "List of extensions of dispensable files created by the `patch' program.")
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.")
358 (defvar dired-latex-unclean-extensions
359 '(".idx" ".lof" ".lot" ".glo")
360 "List of extensions of dispensable files created by LaTeX.")
362 (defvar dired-bibtex-unclean-extensions
363 '(".blg" ".bbl")
364 "List of extensions of dispensable files created by BibTeX.")
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.")
371 (defun dired-clean-patch ()
372 "Flag dispensable files created by patch for deletion.
373 See variable `dired-patch-unclean-extensions'."
374 (interactive)
375 (dired-flag-extension dired-patch-unclean-extensions))
377 (defun dired-clean-tex ()
378 "Flag dispensable files created by [La]TeX etc. for deletion.
379 See variables `dired-tex-unclean-extensions',
380 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
381 `dired-texinfo-unclean-extensions'."
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)))
388 (defun dired-very-clean-tex ()
389 "Flag dispensable files created by [La]TeX *and* \".dvi\" for deletion.
390 See variables `dired-texinfo-unclean-extensions',
391 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
392 `dired-texinfo-unclean-extensions'."
393 (interactive)
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"))))
400 ;;; JUMP.
402 ;;;###autoload
403 (defun dired-jump (&optional other-window file-name)
404 "Jump to dired buffer corresponding to current buffer.
405 If in a file, dired the current directory and move to file's line.
406 If in Dired already, pop up a level and goto old directory's line.
407 In case the proper dired file line cannot be found, refresh the dired
408 buffer and try again.
409 When OTHER-WINDOW is non-nil, jump to dired buffer in other window.
410 Interactively with prefix argument, read FILE-NAME and
411 move 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))
416 (dir (if file (file-name-directory file) default-directory)))
417 (if (and (eq major-mode 'dired-mode) (null file-name))
418 (progn
419 (setq dir (dired-current-directory))
420 (dired-up-directory other-window)
421 (unless (dired-goto-file dir)
422 ;; refresh and try again
423 (dired-insert-subdir (file-name-directory dir))
424 (dired-goto-file dir)))
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
431 (progn
432 (dired-insert-subdir (file-name-directory file))
433 (dired-goto-file file))
434 ;; Toggle omitting, if it is on, and try again.
435 (when dired-omit-mode
436 (dired-omit-mode)
437 (dired-goto-file file)))))))
439 ;;;###autoload
440 (defun dired-jump-other-window (&optional file-name)
441 "Like \\[dired-jump] (`dired-jump') but in other window."
442 (interactive
443 (list (and current-prefix-arg
444 (read-file-name "Jump to dired file: "))))
445 (dired-jump t file-name))
447 ;;; OMITTING.
449 ;; Enhanced omitting of lines from directory listings.
450 ;; Marked files are never omitted.
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
455 "The LOCALP argument `dired-omit-expunge' passes to `dired-get-filename'.
456 If it is `no-dir', omitting is much faster, but you can only match
457 against the non-directory part of the file name. Set it to nil if you
458 need to match the entire file name.")
460 ;; \017=^O for Omit - other packages can chose other control characters.
461 (defvar dired-omit-marker-char ?\017
462 "Temporary marker used by Dired-Omit.
463 Should never be used as marker by the user or other packages.")
465 (defun dired-omit-startup ()
466 (or (assq 'dired-omit-mode minor-mode-alist)
467 (setq minor-mode-alist
468 (append '((dired-omit-mode
469 (:eval (if (eq major-mode 'dired-mode)
470 " Omit" ""))))
471 minor-mode-alist))))
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))
479 (defcustom dired-omit-extensions
480 (append completion-ignored-extensions
481 dired-latex-unclean-extensions
482 dired-bibtex-unclean-extensions
483 dired-texinfo-unclean-extensions)
484 "If non-nil, a list of extensions \(strings\) to omit from Dired listings.
485 Defaults to elements of `completion-ignored-extensions',
486 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions', and
487 `dired-texinfo-unclean-extensions'.
489 See interactive function `dired-omit-mode' \(\\[dired-omit-mode]\) and
490 variables `dired-omit-mode' and `dired-omit-files'."
491 :type '(repeat string)
492 :group 'dired-x)
494 (defun dired-omit-expunge (&optional regexp)
495 "Erases all unmarked files matching REGEXP.
496 Does nothing if global variable `dired-omit-mode' is nil, or if called
497 non-interactively and buffer is bigger than `dired-omit-size-limit'.
498 If REGEXP is nil or not specified, uses `dired-omit-files', and also omits
499 filenames ending in `dired-omit-extensions'.
500 If REGEXP is the empty string, this function is a no-op.
502 This 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): ")
505 (if (and dired-omit-mode
506 (or (called-interactively-p 'interactive)
507 (not dired-omit-size-limit)
508 (< (buffer-size) dired-omit-size-limit)
509 (progn
510 (when dired-omit-verbose
511 (message "Not omitting: directory larger than %d characters."
512 dired-omit-size-limit))
513 (setq dired-omit-mode nil)
514 nil)))
515 (let ((omit-re (or regexp (dired-omit-regexp)))
516 (old-modified-p (buffer-modified-p))
517 count)
518 (or (string= omit-re "")
519 (let ((dired-marker-char dired-omit-marker-char))
520 (when dired-omit-verbose (message "Omitting..."))
521 (if (dired-mark-unmarked-files omit-re nil nil dired-omit-localp)
522 (progn
523 (setq count (dired-do-kill-lines
525 (if dired-omit-verbose "Omitted %d line%s." "")))
526 (force-mode-line-update))
527 (when dired-omit-verbose (message "(Nothing to omit)")))))
528 ;; Try to preserve modified state of buffer. So `%*' doesn't appear
529 ;; in mode-line of omitted buffers.
530 (set-buffer-modified-p (and old-modified-p
531 (save-excursion
532 (goto-char (point-min))
533 (re-search-forward dired-re-mark nil t))))
534 count)))
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 "")))
546 ;; Returns t if any work was done, nil otherwise.
547 (defun dired-mark-unmarked-files (regexp msg &optional unflag-p localp)
548 "Mark unmarked files matching REGEXP, displaying MSG.
549 REGEXP is matched against the entire file name. When called
550 interactively, prompt for REGEXP.
551 With prefix argument, unflag all those files.
552 Optional fourth argument LOCALP is as in `dired-get-filename'."
553 (interactive
554 (list (dired-read-regexp
555 "Mark unmarked files matching regexp (default all): ")
556 nil current-prefix-arg nil))
557 (let ((dired-marker-char (if unflag-p ?\s dired-marker-char)))
558 (dired-mark-if
559 (and
560 ;; not already marked
561 (looking-at " ")
562 ;; uninteresting
563 (let ((fn (dired-get-filename localp t)))
564 (and fn (string-match regexp fn))))
565 msg)))
568 ;;; VIRTUAL DIRED MODE.
570 ;; For browsing `ls -lR' listings in a dired-like fashion.
572 (defalias 'virtual-dired 'dired-virtual)
573 (defun dired-virtual (dirname &optional switches)
574 "Put this buffer into Virtual Dired mode.
576 In Virtual Dired mode, all commands that do not actually consult the
577 filesystem will work.
579 This is useful if you want to peruse and move around in an ls -lR
580 output file, for example one you got from an ftp server. With
581 ange-ftp, you can even dired a directory containing an ls-lR file,
582 visit that file and turn on virtual dired mode. But don't try to save
583 this file, as dired-virtual indents the listing and thus changes the
584 buffer.
586 If you have save a Dired buffer in a file you can use \\[dired-virtual] to
587 resume it in a later session.
589 Type \\<dired-mode-map>\\[revert-buffer] \
590 in the Virtual Dired buffer and answer `y' to convert
591 the virtual to a real dired buffer again. You don't have to do this, though:
592 you can relist single subdirs using \\[dired-do-redisplay]."
594 ;; DIRNAME is the top level directory of the buffer. It will become
595 ;; its `default-directory'. If nil, the old value of
596 ;; default-directory is used.
598 ;; Optional SWITCHES are the ls switches to use.
600 ;; Shell wildcards will be used if there already is a `wildcard'
601 ;; line in the buffer (thus it is a saved Dired buffer), but there
602 ;; is no other way to get wildcards. Insert a `wildcard' line by
603 ;; hand if you want them.
605 (interactive
606 (list (read-string "Virtual Dired directory: " (dired-virtual-guess-dir))))
607 (goto-char (point-min))
608 (or (looking-at " ")
609 ;; if not already indented, do it now:
610 (indent-region (point-min) (point-max) 2))
611 (or dirname (setq dirname default-directory))
612 (setq dirname (expand-file-name (file-name-as-directory dirname)))
613 (setq default-directory dirname) ; contains no wildcards
614 (let ((wildcard (save-excursion
615 (goto-char (point-min))
616 (forward-line 1)
617 (and (looking-at "^ wildcard ")
618 (buffer-substring (match-end 0)
619 (line-end-position))))))
620 (if wildcard
621 (setq dirname (expand-file-name wildcard default-directory))))
622 ;; If raw ls listing (not a saved old dired buffer), give it a
623 ;; decent subdir headerline:
624 (goto-char (point-min))
625 (or (looking-at dired-subdir-regexp)
626 (insert " "
627 (directory-file-name (file-name-directory default-directory))
628 ":\n"))
629 (dired-mode dirname (or switches dired-listing-switches))
630 (setq mode-name "Virtual Dired"
631 revert-buffer-function 'dired-virtual-revert)
632 (set (make-local-variable 'dired-subdir-alist) nil)
633 (dired-build-subdir-alist)
634 (goto-char (point-min))
635 (dired-initial-position dirname))
637 (defun dired-virtual-guess-dir ()
638 "Guess and return appropriate working directory of this buffer.
639 The buffer is assumed to be in Dired or ls -lR format. The guess is
640 based upon buffer contents. If nothing could be guessed, returns
641 nil."
643 (let ((regexp "^\\( \\)?\\([^ \n\r]*\\)\\(:\\)[\n\r]")
644 (subexpr 2))
645 (goto-char (point-min))
646 (cond ((looking-at regexp)
647 ;; If a saved dired buffer, look to which dir and
648 ;; perhaps wildcard it belongs:
649 (let ((dir (buffer-substring (match-beginning subexpr)
650 (match-end subexpr))))
651 (file-name-as-directory dir)))
652 ;; Else no match for headerline found. It's a raw ls listing.
653 ;; In raw ls listings the directory does not have a headerline
654 ;; try parent of first subdir, if any
655 ((re-search-forward regexp nil t)
656 (file-name-directory
657 (directory-file-name
658 (file-name-as-directory
659 (buffer-substring (match-beginning subexpr)
660 (match-end subexpr))))))
661 (t ; if all else fails
662 nil))))
665 (defun dired-virtual-revert (&optional _arg _noconfirm)
666 (if (not
667 (y-or-n-p "Cannot revert a Virtual Dired buffer - switch to Real Dired mode? "))
668 (error "Cannot revert a Virtual Dired buffer")
669 (setq mode-name "Dired"
670 revert-buffer-function 'dired-revert)
671 (revert-buffer)))
673 ;; A zero-arg version of dired-virtual.
674 (defun dired-virtual-mode ()
675 "Put current buffer into Virtual Dired mode (see `dired-virtual').
676 Useful on `magic-mode-alist' with the regexp
678 \"^ \\\\(/[^ /]+\\\\)+/?:$\"
680 to put saved dired buffers automatically into Virtual Dired mode.
682 Also useful for `auto-mode-alist' like this:
684 (add-to-list 'auto-mode-alist
685 '(\"[^/]\\\\.dired\\\\'\" . dired-virtual-mode))"
686 (interactive)
687 (dired-virtual (dired-virtual-guess-dir)))
690 ;;; SMART SHELL.
692 ;; An Emacs buffer can have but one working directory, stored in the
693 ;; buffer-local variable `default-directory'. A Dired buffer may have
694 ;; several subdirectories inserted, but still has but one working directory:
695 ;; that of the top level Dired directory in that buffer. For some commands
696 ;; it is appropriate that they use the current Dired directory instead of
697 ;; `default-directory', e.g., `find-file' and `compile'. This is a general
698 ;; mechanism is provided for special handling of the working directory in
699 ;; special major modes.
701 (define-obsolete-variable-alias 'default-directory-alist
702 'dired-default-directory-alist "24.1")
704 ;; It's easier to add to this alist than redefine function
705 ;; default-directory while keeping the old information.
706 (defconst dired-default-directory-alist
707 '((dired-mode . (if (fboundp 'dired-current-directory)
708 (dired-current-directory)
709 default-directory)))
710 "Alist of major modes and their opinion on `default-directory'.
711 Each element has the form (MAJOR . EXPRESSION).
712 The function `dired-default-directory' evaluates EXPRESSION to
713 determine a default directory.")
715 (put 'dired-default-directory-alist 'risky-local-variable t) ; gets eval'd
716 (make-obsolete-variable 'dired-default-directory-alist
717 "this feature is due to be removed." "24.1")
719 (defun dired-default-directory ()
720 "Return the `dired-default-directory-alist' entry for the current major-mode.
721 If none, return `default-directory'."
722 (or (eval (cdr (assq major-mode dired-default-directory-alist)))
723 default-directory))
725 ;; It looks like this was intended to be something of a "general" feature,
726 ;; but it only ever seems to have been used in dired-smart-shell-command,
727 ;; and does not seem worth keeping around (?).
728 (make-obsolete 'dired-default-directory
729 "this feature is due to be removed." "24.1")
731 (defun dired-smart-shell-command (command &optional output-buffer error-buffer)
732 "Like function `shell-command', but in the current Virtual Dired directory."
733 (interactive
734 (list
735 (read-shell-command "Shell command: " nil nil
736 (cond
737 (buffer-file-name (file-relative-name buffer-file-name))
738 ((eq major-mode 'dired-mode) (dired-get-filename t t))))
739 current-prefix-arg
740 shell-command-default-error-buffer))
741 (let ((default-directory (or (and (eq major-mode 'dired-mode)
742 (dired-current-directory))
743 default-directory)))
744 (shell-command command output-buffer error-buffer)))
747 ;;; LOCAL VARIABLES FOR DIRED BUFFERS.
749 ;; Brief Description (This feature is obsolete as of Emacs 24.1)
751 ;; * `dired-extra-startup' is part of the `dired-mode-hook'.
753 ;; * `dired-extra-startup' calls `dired-hack-local-variables'
755 ;; * `dired-hack-local-variables' checks the value of
756 ;; `dired-local-variables-file'
758 ;; * Check if `dired-local-variables-file' is a non-nil string and is a
759 ;; filename found in the directory of the Dired Buffer being created.
761 ;; * If `dired-local-variables-file' satisfies the above, then temporarily
762 ;; include it in the Dired Buffer at the bottom.
764 ;; * Set `enable-local-variables' temporarily to the user variable
765 ;; `dired-enable-local-variables' and run `hack-local-variables' on the
766 ;; Dired Buffer.
768 (defcustom dired-local-variables-file (convert-standard-filename ".dired")
769 "Filename, as string, containing local dired buffer variables to be hacked.
770 If this file found in current directory, then it will be inserted into dired
771 buffer and `hack-local-variables' will be run. See Info node
772 `(emacs)File Variables' for more information on local variables.
773 See also `dired-enable-local-variables'."
774 :type 'file
775 :group 'dired)
777 (make-obsolete-variable 'dired-local-variables-file 'dir-locals-file "24.1")
779 (defun dired-hack-local-variables ()
780 "Evaluate local variables in `dired-local-variables-file' for dired buffer."
781 (and (stringp dired-local-variables-file)
782 (file-exists-p dired-local-variables-file)
783 (let ((opoint (point-max))
784 (inhibit-read-only t)
785 ;; In case user has `enable-local-variables' set to nil we
786 ;; override it locally with dired's variable.
787 (enable-local-variables dired-enable-local-variables))
788 ;; Insert 'em.
789 (save-excursion
790 (goto-char opoint)
791 (insert "\^L\n")
792 (insert-file-contents dired-local-variables-file))
793 ;; Hack 'em.
794 (unwind-protect
795 (let ((buffer-file-name dired-local-variables-file))
796 (hack-local-variables))
797 ;; Delete this stuff: `eobp' is used to find last subdir by dired.el.
798 (delete-region opoint (point-max)))
799 ;; Make sure that the modeline shows the proper information.
800 (dired-sort-set-modeline))))
802 (make-obsolete 'dired-hack-local-variables
803 'hack-dir-local-variables-non-file-buffer "24.1")
805 ;; Does not seem worth a dedicated command.
806 ;; See the more general features in files-x.el.
807 (defun dired-omit-here-always ()
808 "Create `dir-locals-file' setting `dired-omit-mode' to t in `dired-mode'.
809 If in a Dired buffer, reverts it."
810 (interactive)
811 (if (file-exists-p dired-local-variables-file)
812 (error "Old-style dired-local-variables-file `./%s' found;
813 replace it with a dir-locals-file `./%s'"
814 dired-local-variables-file
815 dir-locals-file))
816 (if (file-exists-p dir-locals-file)
817 (message "File `./%s' already exists." dir-locals-file)
818 (with-temp-buffer
819 (insert "\
820 \((dired-mode . ((subdirs . nil)
821 (dired-omit-mode . t))))\n")
822 (write-file dir-locals-file))
823 ;; Run extra-hooks and revert directory.
824 (when (derived-mode-p 'dired-mode)
825 (hack-dir-local-variables-non-file-buffer)
826 (dired-extra-startup)
827 (dired-revert))))
829 (make-obsolete 'dired-omit-here-always 'add-dir-local-variable "24.1")
832 ;;; GUESS SHELL COMMAND.
834 ;; Brief Description:
836 ;; * `dired-do-shell-command' is bound to `!' by dired.el.
838 ;; * `dired-guess-shell-command' provides smarter defaults for
839 ;;; dired-aux.el's `dired-read-shell-command'.
841 ;; * `dired-guess-shell-command' calls `dired-guess-default' with list of
842 ;;; marked files.
844 ;; * Parse `dired-guess-shell-alist-user' and
845 ;;; `dired-guess-shell-alist-default' (in that order) for the first REGEXP
846 ;;; that matches the first file in the file list.
848 ;; * If the REGEXP matches all the entries of the file list then evaluate
849 ;;; COMMAND, which is either a string or a Lisp expression returning a
850 ;;; string. COMMAND may be a list of commands.
852 ;; * Return this command to `dired-guess-shell-command' which prompts user
853 ;;; with it. The list of commands is put into the list of default values.
854 ;;; If a command is used successfully then it is stored permanently in
855 ;;; `dired-shell-command-history'.
857 ;; Guess what shell command to apply to a file.
858 (defvar dired-shell-command-history nil
859 "History list for commands that read dired-shell commands.")
861 ;; Default list of shell commands.
863 ;; NOTE: Use `gunzip -c' instead of `zcat' on `.gz' files. Some do not
864 ;; install GNU zip's version of zcat.
866 (autoload 'Man-support-local-filenames "man")
868 (defvar dired-guess-shell-alist-default
869 (list
870 (list "\\.tar\\'"
871 '(if dired-guess-shell-gnutar
872 (concat dired-guess-shell-gnutar " xvf")
873 "tar xvf")
874 ;; Extract files into a separate subdirectory
875 '(if dired-guess-shell-gnutar
876 (concat "mkdir " (file-name-sans-extension file)
877 "; " dired-guess-shell-gnutar " -C "
878 (file-name-sans-extension file) " -xvf")
879 (concat "mkdir " (file-name-sans-extension file)
880 "; tar -C " (file-name-sans-extension file) " -xvf"))
881 ;; List archive contents.
882 '(if dired-guess-shell-gnutar
883 (concat dired-guess-shell-gnutar " tvf")
884 "tar tvf"))
886 ;; REGEXPS for compressed archives must come before the .Z rule to
887 ;; be recognized:
888 (list "\\.tar\\.Z\\'"
889 ;; Untar it.
890 '(if dired-guess-shell-gnutar
891 (concat dired-guess-shell-gnutar " zxvf")
892 (concat "zcat * | tar xvf -"))
893 ;; Optional conversion to gzip format.
894 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
895 " " dired-guess-shell-znew-switches))
897 ;; gzip'ed archives
898 (list "\\.t\\(ar\\.\\)?gz\\'"
899 '(if dired-guess-shell-gnutar
900 (concat dired-guess-shell-gnutar " zxvf")
901 (concat "gunzip -qc * | tar xvf -"))
902 ;; Extract files into a separate subdirectory
903 '(if dired-guess-shell-gnutar
904 (concat "mkdir " (file-name-sans-extension file)
905 "; " dired-guess-shell-gnutar " -C "
906 (file-name-sans-extension file) " -zxvf")
907 (concat "mkdir " (file-name-sans-extension file)
908 "; gunzip -qc * | tar -C "
909 (file-name-sans-extension file) " -xvf -"))
910 ;; Optional decompression.
911 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q" ""))
912 ;; List archive contents.
913 '(if dired-guess-shell-gnutar
914 (concat dired-guess-shell-gnutar " ztvf")
915 (concat "gunzip -qc * | tar tvf -")))
917 ;; bzip2'ed archives
918 (list "\\.t\\(ar\\.bz2\\|bz\\)\\'"
919 "bunzip2 -c * | tar xvf -"
920 ;; Extract files into a separate subdirectory
921 '(concat "mkdir " (file-name-sans-extension file)
922 "; bunzip2 -c * | tar -C "
923 (file-name-sans-extension file) " -xvf -")
924 ;; Optional decompression.
925 "bunzip2")
927 ;; xz'ed archives
928 (list "\\.t\\(ar\\.\\)?xz\\'"
929 "unxz -c * | tar xvf -"
930 ;; Extract files into a separate subdirectory
931 '(concat "mkdir " (file-name-sans-extension file)
932 "; unxz -c * | tar -C "
933 (file-name-sans-extension file) " -xvf -")
934 ;; Optional decompression.
935 "unxz")
937 '("\\.shar\\.Z\\'" "zcat * | unshar")
938 '("\\.shar\\.g?z\\'" "gunzip -qc * | unshar")
940 '("\\.e?ps\\'" "ghostview" "xloadimage" "lpr")
941 (list "\\.e?ps\\.g?z\\'" "gunzip -qc * | ghostview -"
942 ;; Optional decompression.
943 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
944 (list "\\.e?ps\\.Z\\'" "zcat * | ghostview -"
945 ;; Optional conversion to gzip format.
946 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
947 " " dired-guess-shell-znew-switches))
949 '("\\.patch\\'" "cat * | patch")
950 (list "\\.patch\\.g?z\\'" "gunzip -qc * | patch"
951 ;; Optional decompression.
952 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
953 (list "\\.patch\\.Z\\'" "zcat * | patch"
954 ;; Optional conversion to gzip format.
955 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
956 " " dired-guess-shell-znew-switches))
958 ;; The following four extensions are useful with dired-man ("N" key)
959 ;; FIXME "man ./" does not work with dired-do-shell-command,
960 ;; because there seems to be no way for us to modify the filename,
961 ;; only the command. Hmph. `dired-man' works though.
962 (list "\\.\\(?:[0-9]\\|man\\)\\'" '(let ((loc (Man-support-local-filenames)))
963 (cond ((eq loc 'man-db) "man -l")
964 ((eq loc 'man) "man ./")
966 "cat * | tbl | nroff -man -h"))))
967 (list "\\.\\(?:[0-9]\\|man\\)\\.g?z\\'"
968 '(let ((loc (Man-support-local-filenames)))
969 (cond ((eq loc 'man-db)
970 "man -l")
971 ((eq loc 'man)
972 "man ./")
973 (t "gunzip -qc * | tbl | nroff -man -h")))
974 ;; Optional decompression.
975 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
976 (list "\\.[0-9]\\.Z\\'" '(let ((loc (Man-support-local-filenames)))
977 (cond ((eq loc 'man-db) "man -l")
978 ((eq loc 'man) "man ./")
979 (t "zcat * | tbl | nroff -man -h")))
980 ;; Optional conversion to gzip format.
981 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
982 " " dired-guess-shell-znew-switches))
983 '("\\.pod\\'" "perldoc" "pod2man * | nroff -man")
985 '("\\.dvi\\'" "xdvi" "dvips") ; preview and printing
986 '("\\.au\\'" "play") ; play Sun audiofiles
987 '("\\.mpe?g\\'\\|\\.avi\\'" "xine -p")
988 '("\\.ogg\\'" "ogg123")
989 '("\\.mp3\\'" "mpg123")
990 '("\\.wav\\'" "play")
991 '("\\.uu\\'" "uudecode") ; for uudecoded files
992 '("\\.hqx\\'" "mcvert")
993 '("\\.sh\\'" "sh") ; execute shell scripts
994 '("\\.xbm\\'" "bitmap") ; view X11 bitmaps
995 '("\\.gp\\'" "gnuplot")
996 '("\\.p[bgpn]m\\'" "xloadimage")
997 '("\\.gif\\'" "xloadimage") ; view gif pictures
998 '("\\.tif\\'" "xloadimage")
999 '("\\.png\\'" "display") ; xloadimage 4.1 doesn't grok PNG
1000 '("\\.jpe?g\\'" "xloadimage")
1001 '("\\.fig\\'" "xfig") ; edit fig pictures
1002 '("\\.out\\'" "xgraph") ; for plotting purposes.
1003 '("\\.tex\\'" "latex" "tex")
1004 '("\\.texi\\(nfo\\)?\\'" "makeinfo" "texi2dvi")
1005 '("\\.pdf\\'" "xpdf")
1006 '("\\.doc\\'" "antiword" "strings")
1007 '("\\.rpm\\'" "rpm -qilp" "rpm -ivh")
1008 '("\\.dia\\'" "dia")
1009 '("\\.mgp\\'" "mgp")
1011 ;; Some other popular archivers.
1012 (list "\\.zip\\'" "unzip" "unzip -l"
1013 ;; Extract files into a separate subdirectory
1014 '(concat "unzip" (if dired-guess-shell-gzip-quiet " -q")
1015 " -d " (file-name-sans-extension file)))
1016 '("\\.zoo\\'" "zoo x//")
1017 '("\\.lzh\\'" "lharc x")
1018 '("\\.arc\\'" "arc x")
1019 '("\\.shar\\'" "unshar")
1020 '("\\.rar\\'" "unrar x")
1021 '("\\.7z\\'" "7z x")
1023 ;; Compression.
1024 (list "\\.g?z\\'" '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
1025 (list "\\.dz\\'" "dictunzip")
1026 (list "\\.bz2\\'" "bunzip2")
1027 (list "\\.xz\\'" "unxz")
1028 (list "\\.Z\\'" "uncompress"
1029 ;; Optional conversion to gzip format.
1030 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
1031 " " dired-guess-shell-znew-switches))
1033 '("\\.sign?\\'" "gpg --verify"))
1035 "Default alist used for shell command guessing.
1036 See `dired-guess-shell-alist-user'.")
1038 (defcustom dired-guess-shell-alist-user nil
1039 "User-defined alist of rules for suggested commands.
1040 These rules take precedence over the predefined rules in the variable
1041 `dired-guess-shell-alist-default' (to which they are prepended).
1043 Each element of this list looks like
1045 \(REGEXP COMMAND...\)
1047 where each COMMAND can either be a string or a Lisp expression that evaluates
1048 to a string. If several COMMANDs are given, the first one will be the default
1049 and the rest will be added temporarily to the history and can be retrieved
1050 with \\[previous-history-element] (M-p) .
1052 The variable `dired-guess-shell-case-fold-search' controls whether
1053 REGEXP is matched case-sensitively.
1055 You can set this variable in your ~/.emacs. For example, to add rules for
1056 `.foo' and `.bar' files, write
1058 \(setq dired-guess-shell-alist-user
1059 '((\"\\\\.foo\\\\'\" \"FOO-COMMAND\")
1060 (\"\\\\.bar\\\\'\"
1061 (if condition
1062 \"BAR-COMMAND-1\"
1063 \"BAR-COMMAND-2\"))))"
1064 :group 'dired-x
1065 :type '(alist :key-type regexp :value-type (repeat sexp)))
1067 (defcustom dired-guess-shell-case-fold-search t
1068 "If non-nil, `dired-guess-shell-alist-default' and
1069 `dired-guess-shell-alist-user' are matched case-insensitively."
1070 :group 'dired-x
1071 :type 'boolean)
1073 (defun dired-guess-default (files)
1074 "Return a shell command, or a list of commands, appropriate for FILES.
1075 See `dired-guess-shell-alist-user'."
1077 (let* ((case-fold-search dired-guess-shell-case-fold-search)
1078 ;; Prepend the user's alist to the default alist.
1079 (alist (append dired-guess-shell-alist-user
1080 dired-guess-shell-alist-default))
1081 (file (car files))
1082 (flist (cdr files))
1083 elt regexp cmds)
1085 ;; Find the first match in the alist for first file in FILES.
1086 (while alist
1087 (setq elt (car alist)
1088 regexp (car elt)
1089 alist (cdr alist))
1090 (if (string-match regexp file)
1091 (setq cmds (cdr elt)
1092 alist nil)))
1094 ;; If more than one file, see if all of FILES match regular expression.
1095 (while (and flist
1096 (string-match regexp (car flist)))
1097 (setq flist (cdr flist)))
1099 ;; If flist is still non-nil, then do not guess since this means that not
1100 ;; all the files in FILES were matched by the regexp.
1101 (setq cmds (and (not flist) cmds))
1103 ;; Return commands or nil if flist is still non-nil.
1104 ;; Evaluate the commands in order that any logical testing will be done.
1105 (if (cdr cmds)
1106 (delete-dups (mapcar #'eval cmds))
1107 (eval (car cmds))))) ; single command
1109 (defun dired-guess-shell-command (prompt files)
1110 "Ask user with PROMPT for a shell command, guessing a default from FILES."
1111 (let ((default (dired-guess-default files))
1112 default-list val)
1113 (if (null default)
1114 ;; Nothing to guess
1115 (read-shell-command prompt nil 'dired-shell-command-history)
1116 (if (listp default)
1117 ;; More than one guess
1118 (setq default-list default
1119 default (car default)
1120 prompt (concat
1121 prompt
1122 (format "{%d guesses} " (length default-list))))
1123 ;; Just one guess
1124 (setq default-list (list default)))
1125 ;; Put the first guess in the prompt but not in the initial value.
1126 (setq prompt (concat prompt (format "[%s] " default)))
1127 ;; All guesses can be retrieved with M-n
1128 (setq val (read-shell-command prompt nil
1129 'dired-shell-command-history
1130 default-list))
1131 ;; If we got a return, then return default.
1132 (if (equal val "") default val))))
1135 ;;; RELATIVE SYMBOLIC LINKS.
1137 (declare-function make-symbolic-link "fileio.c")
1139 (defvar dired-keep-marker-relsymlink ?S
1140 "See variable `dired-keep-marker-move'.")
1142 (defun dired-make-relative-symlink (file1 file2 &optional ok-if-already-exists)
1143 "Make a symbolic link (pointing to FILE1) in FILE2.
1144 The link is relative (if possible), for example
1146 \"/vol/tex/bin/foo\" \"/vol/local/bin/foo\"
1148 results in
1150 \"../../tex/bin/foo\" \"/vol/local/bin/foo\""
1151 (interactive "FRelSymLink: \nFRelSymLink %s: \np")
1152 (let (name1 name2 len1 len2 (index 0) sub)
1153 (setq file1 (expand-file-name file1)
1154 file2 (expand-file-name file2)
1155 len1 (length file1)
1156 len2 (length file2))
1157 ;; Find common initial file name components:
1158 (let (next)
1159 (while (and (setq next (string-match "/" file1 index))
1160 (< (setq next (1+ next)) (min len1 len2))
1161 ;; For the comparison, both substrings must end in
1162 ;; `/', so NEXT is *one plus* the result of the
1163 ;; string-match.
1164 ;; E.g., consider the case of linking "/tmp/a/abc"
1165 ;; to "/tmp/abc" erroneously giving "/tmp/a" instead
1166 ;; of "/tmp/" as common initial component
1167 (string-equal (substring file1 0 next)
1168 (substring file2 0 next)))
1169 (setq index next))
1170 (setq name2 file2
1171 sub (substring file1 0 index)
1172 name1 (substring file1 index)))
1173 (if (string-equal sub "/")
1174 ;; No common initial file name found
1175 (setq name1 file1)
1176 ;; Else they have a common parent directory
1177 (let ((tem (substring file2 index))
1178 (start 0)
1179 (count 0))
1180 ;; Count number of slashes we must compensate for ...
1181 (while (setq start (string-match "/" tem start))
1182 (setq count (1+ count)
1183 start (1+ start)))
1184 ;; ... and prepend a "../" for each slash found:
1185 (dotimes (_n count)
1186 (setq name1 (concat "../" name1)))))
1187 (make-symbolic-link
1188 (directory-file-name name1) ; must not link to foo/
1189 ; (trailing slash!)
1190 name2 ok-if-already-exists)))
1192 (autoload 'dired-do-create-files "dired-aux")
1194 ;;;###autoload
1195 (defun dired-do-relsymlink (&optional arg)
1196 "Relative symlink all marked (or next ARG) files into a directory.
1197 Otherwise make a relative symbolic link to the current file.
1198 This creates relative symbolic links like
1200 foo -> ../bar/foo
1202 not absolute ones like
1204 foo -> /ugly/file/name/that/may/change/any/day/bar/foo
1206 For absolute symlinks, use \\[dired-do-symlink]."
1207 (interactive "P")
1208 (dired-do-create-files 'relsymlink #'dired-make-relative-symlink
1209 "RelSymLink" arg dired-keep-marker-relsymlink))
1211 (autoload 'dired-mark-read-regexp "dired-aux")
1212 (autoload 'dired-do-create-files-regexp "dired-aux")
1214 (defun dired-do-relsymlink-regexp (regexp newname &optional arg whole-name)
1215 "RelSymlink all marked files containing REGEXP to NEWNAME.
1216 See functions `dired-do-rename-regexp' and `dired-do-relsymlink'
1217 for more info."
1218 (interactive (dired-mark-read-regexp "RelSymLink"))
1219 (dired-do-create-files-regexp
1220 #'dired-make-relative-symlink
1221 "RelSymLink" arg regexp newname whole-name dired-keep-marker-relsymlink))
1224 ;;; VISIT ALL MARKED FILES SIMULTANEOUSLY.
1226 ;; Brief Description:
1228 ;; `dired-do-find-marked-files' is bound to `F' by dired-x.el.
1230 ;; * Use `dired-get-marked-files' to collect the marked files in the current
1231 ;;; Dired Buffer into a list of filenames `FILE-LIST'.
1233 ;; * Pass FILE-LIST to `dired-simultaneous-find-file' all with
1234 ;;; `dired-do-find-marked-files''s prefix argument NOSELECT.
1236 ;; * `dired-simultaneous-find-file' runs through FILE-LIST decrementing the
1237 ;;; list each time.
1239 ;; * If NOSELECT is non-nil then just run `find-file-noselect' on each
1240 ;;; element of FILE-LIST.
1242 ;; * If NOSELECT is nil then calculate the `size' of the window for each file
1243 ;;; by dividing the `window-height' by length of FILE-LIST. Thus, `size' is
1244 ;;; cognizant of the window-configuration.
1246 ;; * If `size' is too small abort, otherwise run `find-file' on each element
1247 ;;; of FILE-LIST giving each a window of height `size'.
1249 (defun dired-do-find-marked-files (&optional noselect)
1250 "Find all marked files displaying all of them simultaneously.
1251 With optional NOSELECT just find files but do not select them.
1253 The current window is split across all files marked, as evenly as possible.
1254 Remaining lines go to bottom-most window. The number of files that can be
1255 displayed this way is restricted by the height of the current window and
1256 `window-min-height'.
1258 To keep dired buffer displayed, type \\[split-window-vertically] first.
1259 To display just marked files, type \\[delete-other-windows] first."
1260 (interactive "P")
1261 (dired-simultaneous-find-file (dired-get-marked-files) noselect))
1263 (defun dired-simultaneous-find-file (file-list noselect)
1264 "Visit all files in FILE-LIST and display them simultaneously.
1265 The current window is split across all files in FILE-LIST, as evenly as
1266 possible. Remaining lines go to the bottom-most window. The number of
1267 files that can be displayed this way is restricted by the height of the
1268 current window and the variable `window-min-height'. With non-nil
1269 NOSELECT the files are merely found but not selected."
1270 ;; We don't make this function interactive because it is usually too clumsy
1271 ;; to specify FILE-LIST interactively unless via dired.
1272 (let (size)
1273 (if noselect
1274 ;; Do not select the buffer.
1275 (find-file-noselect (car file-list))
1276 ;; We will have to select the buffer. Calculate and check window size.
1277 (setq size (/ (window-height) (length file-list)))
1278 (or (<= window-min-height size)
1279 (error "Too many files to visit simultaneously. Try C-u prefix"))
1280 (find-file (car file-list)))
1281 ;; Decrement.
1282 (dolist (file (cdr file-list))
1283 (if noselect
1284 ;; Do not select the buffer.
1285 (find-file-noselect file)
1286 ;; Vertically split off a window of desired size. Upper window will
1287 ;; have SIZE lines. Select lower (larger) window. We split it again.
1288 (select-window (split-window nil size))
1289 (find-file file)))))
1292 ;;; MISCELLANEOUS COMMANDS.
1294 ;; Run man on files.
1296 (declare-function Man-getpage-in-background "man" (topic))
1298 (defvar manual-program) ; from man.el
1300 (defun dired-man ()
1301 "Run `man' on this file."
1302 ;; Used also to say: "Display old buffer if buffer name matches filename."
1303 ;; but I have no idea what that means.
1304 (interactive)
1305 (require 'man)
1306 (let* ((file (dired-get-filename))
1307 (manual-program (replace-regexp-in-string "\\*" "%s"
1308 (dired-guess-shell-command
1309 "Man command: " (list file)))))
1310 (Man-getpage-in-background file)))
1312 ;; Run Info on files.
1314 (defun dired-info ()
1315 "Run `info' on this file."
1316 (interactive)
1317 (info (dired-get-filename)))
1319 ;; Run mail on mail folders.
1321 (declare-function vm-visit-folder "ext:vm" (folder &optional read-only))
1322 (defvar vm-folder-directory)
1324 (defun dired-vm (&optional read-only)
1325 "Run VM on this file.
1326 With optional prefix argument, visits the folder read-only.
1327 Otherwise obeys the value of `dired-vm-read-only-folders'."
1328 (interactive "P")
1329 (let ((dir (dired-current-directory))
1330 (fil (dired-get-filename)))
1331 (vm-visit-folder fil (or read-only
1332 (eq t dired-vm-read-only-folders)
1333 (and dired-vm-read-only-folders
1334 (not (file-writable-p fil)))))
1335 ;; So that pressing `v' inside VM does prompt within current directory:
1336 (set (make-local-variable 'vm-folder-directory) dir)))
1338 (defun dired-rmail ()
1339 "Run RMAIL on this file."
1340 (interactive)
1341 (rmail (dired-get-filename)))
1343 (defun dired-do-run-mail ()
1344 "If `dired-bind-vm' is non-nil, call `dired-vm', else call `dired-rmail'."
1345 (interactive)
1346 (if dired-bind-vm
1347 ;; Read mail folder using vm.
1348 (dired-vm)
1349 ;; Read mail folder using rmail.
1350 (dired-rmail)))
1353 ;;; MISCELLANEOUS INTERNAL FUNCTIONS.
1355 ;; This should be a builtin
1356 (defun dired-buffer-more-recently-used-p (buffer1 buffer2)
1357 "Return t if BUFFER1 is more recently used than BUFFER2.
1358 Considers buffers closer to the car of `buffer-list' to be more recent."
1359 (and (not (equal buffer1 buffer2))
1360 (memq buffer1 (buffer-list))
1361 (not (memq buffer1 (memq buffer2 (buffer-list))))))
1363 ;; Same thing as `dired-buffers-for-dir' of dired.el? - lrd 11/23/93
1364 ;; (defun dired-buffers-for-dir-exact (dir)
1365 ;; ;; Return a list of buffers that dired DIR (a directory or wildcard)
1366 ;; ;; at top level, or as subdirectory.
1367 ;; ;; Top level matches must match the wildcard part too, if any.
1368 ;; ;; The list is in reverse order of buffer creation, most recent last.
1369 ;; ;; As a side effect, killed dired buffers for DIR are removed from
1370 ;; ;; dired-buffers.
1371 ;; (let ((alist dired-buffers) result elt)
1372 ;; (while alist
1373 ;; (setq elt (car alist)
1374 ;; alist (cdr alist))
1375 ;; (let ((buf (cdr elt)))
1376 ;; (if (buffer-name buf)
1377 ;; ;; Top level must match exactly against dired-directory in
1378 ;; ;; case one of them is a wildcard.
1379 ;; (if (or (equal dir (with-current-buffer buf dired-directory))
1380 ;; (assoc dir (with-current-buffer buf dired-subdir-alist)))
1381 ;; (setq result (cons buf result)))
1382 ;; ;; else buffer is killed - clean up:
1383 ;; (setq dired-buffers (delq elt dired-buffers)))))
1384 ;; result))
1387 ;; Does anyone use this? - lrd 6/29/93.
1388 ;; Apparently people do use it. - lrd 12/22/97.
1390 (with-no-warnings
1391 ;; Warnings are suppresed to avoid "global/dynamic var `X' lacks a prefix".
1392 ;; This is unbearably ugly, but not more than having global variables
1393 ;; named size, time, name or s, however practical it can be while writing
1394 ;; `dired-mark-sexp' predicates.
1395 (defvar inode)
1396 (defvar s)
1397 (defvar mode)
1398 (defvar nlink)
1399 (defvar uid)
1400 (defvar gid)
1401 (defvar size)
1402 (defvar time)
1403 (defvar name)
1404 (defvar sym))
1406 (defun dired-mark-sexp (predicate &optional unflag-p)
1407 "Mark files for which PREDICATE returns non-nil.
1408 With a prefix arg, unmark or unflag those files instead.
1410 PREDICATE is a lisp expression that can refer to the following symbols:
1412 inode [integer] the inode of the file (only for ls -i output)
1413 s [integer] the size of the file for ls -s output
1414 (usually in blocks or, with -k, in KByte)
1415 mode [string] file permission bits, e.g. \"-rw-r--r--\"
1416 nlink [integer] number of links to file
1417 uid [string] owner
1418 gid [string] group (If the gid is not displayed by ls,
1419 this will still be set (to the same as uid))
1420 size [integer] file size in bytes
1421 time [string] the time that ls displays, e.g. \"Feb 12 14:17\"
1422 name [string] the name of the file
1423 sym [string] if file is a symbolic link, the linked-to name, else \"\"
1425 For example, use
1427 (equal 0 size)
1429 to mark all zero length files."
1430 ;; Using sym="" instead of nil avoids the trap of
1431 ;; (string-match "foo" sym) into which a user would soon fall.
1432 ;; Give `equal' instead of `=' in the example, as this works on
1433 ;; integers and strings.
1434 (interactive "xMark if (lisp expr): \nP")
1435 (message "%s" predicate)
1436 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char))
1437 inode s mode nlink uid gid size time name sym)
1438 (dired-mark-if
1439 (save-excursion
1440 (and
1441 ;; Sets vars
1442 ;; inode s mode nlink uid gid size time name sym
1444 ;; according to current file line. Returns t for success, nil if
1445 ;; there is no file line. Upon success, all variables are set, either
1446 ;; to nil or the appropriate value, so they need not be initialized.
1447 ;; Moves point within the current line.
1448 (dired-move-to-filename)
1449 (let (pos
1450 (mode-len 10) ; length of mode string
1451 ;; like in dired.el, but with subexpressions \1=inode, \2=s:
1452 (dired-re-inode-size "\\s *\\([0-9]*\\)\\s *\\([0-9]*\\) ?"))
1453 (beginning-of-line)
1454 (forward-char 2)
1455 (if (looking-at dired-re-inode-size)
1456 (progn
1457 (goto-char (match-end 0))
1458 (setq inode (string-to-number
1459 (buffer-substring (match-beginning 1)
1460 (match-end 1)))
1461 s (string-to-number
1462 (buffer-substring (match-beginning 2)
1463 (match-end 2)))))
1464 (setq inode nil
1465 s nil))
1466 (setq mode (buffer-substring (point) (+ mode-len (point))))
1467 (forward-char mode-len)
1468 (setq nlink (read (current-buffer)))
1469 ;; Karsten Wenger <kw@cis.uni-muenchen.de> fixed uid.
1470 (setq uid (buffer-substring (1+ (point))
1471 (progn (forward-word 1) (point))))
1472 (re-search-forward directory-listing-before-filename-regexp)
1473 (goto-char (match-beginning 1))
1474 (forward-char -1)
1475 (setq size (string-to-number
1476 (buffer-substring (save-excursion
1477 (backward-word 1)
1478 (setq pos (point)))
1479 (point))))
1480 (goto-char pos)
1481 (backward-word 1)
1482 ;; if no gid is displayed, gid will be set to uid
1483 ;; but user will then not reference it anyway in PREDICATE.
1484 (setq gid (buffer-substring (save-excursion
1485 (forward-word 1) (point))
1486 (point))
1487 time (buffer-substring (match-beginning 1)
1488 (1- (dired-move-to-filename)))
1489 name (buffer-substring (point)
1491 (dired-move-to-end-of-filename t)
1492 (point)))
1493 sym (if (looking-at " -> ")
1494 (buffer-substring (progn (forward-char 4) (point))
1495 (line-end-position))
1496 ""))
1498 (eval predicate)))
1499 (format "'%s file" predicate))))
1502 ;;; FIND FILE AT POINT.
1504 (defcustom dired-x-hands-off-my-keys t
1505 "Non-nil means don't remap `find-file' to `dired-x-find-file'.
1506 Similarly for `find-file-other-window' and `dired-x-find-file-other-window'.
1507 If you change this variable without using \\[customize] after `dired-x.el'
1508 is loaded then call \\[dired-x-bind-find-file]."
1509 :type 'boolean
1510 :initialize 'custom-initialize-default
1511 :set (lambda (symbol value)
1512 (set symbol value)
1513 (dired-x-bind-find-file))
1514 :group 'dired-x)
1516 (defun dired-x-bind-find-file ()
1517 "Bind `dired-x-find-file' in place of `find-file' (or vice-versa).
1518 Similarly for `dired-x-find-file-other-window' and `find-file-other-window'.
1519 Binding direction based on `dired-x-hands-off-my-keys'."
1520 (interactive)
1521 (if (called-interactively-p 'interactive)
1522 (setq dired-x-hands-off-my-keys
1523 (not (y-or-n-p "Bind dired-x-find-file over find-file? "))))
1524 (define-key (current-global-map) [remap find-file]
1525 (if (not dired-x-hands-off-my-keys) 'dired-x-find-file))
1526 (define-key (current-global-map) [remap find-file-other-window]
1527 (if (not dired-x-hands-off-my-keys) 'dired-x-find-file-other-window)))
1529 ;; Now call it so binding is correct. This could go in the :initialize
1530 ;; slot, but then dired-x-bind-find-file has to be defined before the
1531 ;; defcustom, and we get free variable warnings.
1532 (dired-x-bind-find-file)
1534 (defun dired-x-find-file (filename)
1535 "Edit file FILENAME.
1536 Like `find-file', except that when called interactively with a
1537 prefix argument, it offers the filename near point as a default."
1538 (interactive (list (dired-x-read-filename-at-point "Find file: ")))
1539 (find-file filename))
1541 (defun dired-x-find-file-other-window (filename)
1542 "Edit file FILENAME, in another window.
1543 Like `find-file-other-window', except that when called interactively with
1544 a prefix argument, when it offers the filename near point as a default."
1545 (interactive (list (dired-x-read-filename-at-point "Find file: ")))
1546 (find-file-other-window filename))
1548 ;;; Internal functions.
1550 ;; Fixme: This should probably use `thing-at-point'. -- fx
1551 (defun dired-filename-at-point ()
1552 "Return the filename closest to point, expanded.
1553 Point should be in or after a filename."
1554 (save-excursion
1555 ;; First see if just past a filename.
1556 (or (eobp) ; why?
1557 (when (looking-at "[] \t\n[{}()]") ; whitespace or some parens
1558 (skip-chars-backward " \n\t\r({[]})")
1559 (or (bobp) (backward-char 1))))
1560 (let ((filename-chars "-.[:alnum:]_/:$+@")
1561 start prefix)
1562 (if (looking-at (format "[%s]" filename-chars))
1563 (progn
1564 (skip-chars-backward filename-chars)
1565 (setq start (point)
1566 prefix
1567 ;; This is something to do with ange-ftp filenames.
1568 ;; It convert foo@bar to /foo@bar.
1569 ;; But when does the former occur in dired buffers?
1570 (and (string-match
1571 "^\\w+@"
1572 (buffer-substring start (line-end-position)))
1573 "/"))
1574 (if (string-match "[/~]" (char-to-string (preceding-char)))
1575 (setq start (1- start)))
1576 (skip-chars-forward filename-chars))
1577 (error "No file found around point!"))
1578 ;; Return string.
1579 (expand-file-name (concat prefix (buffer-substring start (point)))))))
1581 (defun dired-x-read-filename-at-point (prompt)
1582 "Return filename prompting with PROMPT with completion.
1583 If `current-prefix-arg' is non-nil, uses name at point as guess."
1584 (if current-prefix-arg
1585 (let ((guess (dired-filename-at-point)))
1586 (read-file-name prompt
1587 (file-name-directory guess)
1588 guess
1589 nil (file-name-nondirectory guess)))
1590 (read-file-name prompt default-directory)))
1592 (define-obsolete-function-alias 'read-filename-at-point
1593 'dired-x-read-filename-at-point "24.1") ; is this even needed?
1595 ;;; BUG REPORTS
1597 (define-obsolete-function-alias 'dired-x-submit-report 'report-emacs-bug "24.1")
1600 ;; As Barry Warsaw would say: "This might be useful..."
1601 (provide 'dired-x)
1603 ;; Local Variables:
1604 ;; byte-compile-dynamic: t
1605 ;; generated-autoload-file: "dired.el"
1606 ;; End:
1608 ;;; dired-x.el ends here