auth-source.el (auth-source-debug): Enable by default and don't mention the obsolete...
[emacs/old-mirror.git] / lisp / dired-x.el
blob95381ccdc0cca85337d805d88366066d72db2c63
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 ;; (require '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, redefine some functions, and
46 ;; bind some dired keys.
48 ;; User customization: M-x customize-group RET dired-x RET.
50 ;; When loaded this code redefines the following functions of GNU Emacs:
51 ;; From dired.el: dired-clean-up-after-deletion, dired-find-buffer-nocreate,
52 ;; and dired-initial-position.
53 ;; From dired-aux.el: dired-add-entry and dired-read-shell-command.
55 ;; *Please* see the `dired-x' info pages for more details.
58 ;;; Code:
60 ;; LOAD.
62 ;; This is a no-op if dired-x is being loaded via `dired-load-hook',
63 ;; but maybe not if a dired-x function is being autoloaded.
64 (require 'dired)
66 ;; We will redefine some functions and also need some macros.
67 (require 'dired-aux)
69 ;;; User-defined variables.
71 (defgroup dired-x nil
72 "Extended directory editing (dired-x)."
73 :group 'dired)
75 (defgroup dired-keys nil
76 "Dired keys customizations."
77 :prefix "dired-"
78 :group 'dired-x)
80 (defcustom dired-bind-vm nil
81 "Non-nil means \"V\" runs `dired-vm', otherwise \"V\" runs `dired-rmail'.
83 RMAIL files in the old Babyl format (used before before Emacs 23.1)
84 contain \"-*- rmail -*-\" at the top, so `dired-find-file'
85 will run `rmail' on these files. New RMAIL files use the standard
86 mbox format, and so cannot be distinguished in this way."
87 :type 'boolean
88 :group 'dired-keys)
90 (defcustom dired-bind-jump t
91 "Non-nil means bind `dired-jump' to C-x C-j, otherwise do not."
92 :type 'boolean
93 :group 'dired-keys)
95 (defcustom dired-bind-man t
96 "Non-nil means bind `dired-man' to \"N\" in dired-mode, otherwise do not."
97 :type 'boolean
98 :group 'dired-keys)
100 (defcustom dired-bind-info t
101 "Non-nil means bind `dired-info' to \"I\" in dired-mode, otherwise do not."
102 :type 'boolean
103 :group 'dired-keys)
105 (defcustom dired-vm-read-only-folders nil
106 "If non-nil, \\[dired-vm] will visit all folders read-only.
107 If neither nil nor t, e.g. the symbol `if-file-read-only', only
108 files not writable by you are visited read-only.
110 Read-only folders only work in VM 5, not in VM 4."
111 :type '(choice (const :tag "off" nil)
112 (const :tag "on" t)
113 (other :tag "non-writable only" if-file-read-only))
114 :group 'dired-x)
116 (define-minor-mode dired-omit-mode
117 "Toggle Dired-Omit mode.
118 With numeric ARG, enable Dired-Omit mode if ARG is positive, disable
119 otherwise. Enabling and disabling is buffer-local.
120 If enabled, \"uninteresting\" files are not listed.
121 Uninteresting files are those whose filenames match regexp `dired-omit-files',
122 plus those ending with extensions in `dired-omit-extensions'.
124 To enable omitting in every Dired buffer, you can put in your ~/.emacs
126 (add-hook 'dired-mode-hook (lambda () (dired-omit-mode 1)))
128 See Info node `(dired-x) Omitting Variables' for more information."
129 :group 'dired-x
130 (if dired-omit-mode
131 ;; This will mention how many lines were omitted:
132 (let ((dired-omit-size-limit nil)) (dired-omit-expunge))
133 (revert-buffer)))
135 ;; For backward compatibility
136 (define-obsolete-variable-alias 'dired-omit-files-p 'dired-omit-mode "22.1")
138 (defcustom dired-omit-files "^\\.?#\\|^\\.$\\|^\\.\\.$"
139 "Filenames matching this regexp will not be displayed.
140 This only has effect when `dired-omit-mode' is t. See interactive function
141 `dired-omit-mode' \(\\[dired-omit-mode]\) and variable
142 `dired-omit-extensions'. The default is to omit `.', `..', auto-save
143 files and lock files."
144 :type 'regexp
145 :group 'dired-x)
147 (defcustom dired-omit-verbose t
148 "When non-nil, show messages when omitting files.
149 When nil, don't show messages."
150 :type 'boolean
151 :group 'dired-x)
153 (defcustom dired-find-subdir nil ; t is pretty near to DWIM...
154 "If non-nil, Dired always finds a directory in a buffer of its own.
155 If nil, Dired finds the directory as a subdirectory in some other buffer
156 if it is present as one.
158 If there are several dired buffers for a directory, the most recently
159 used is chosen.
161 Dired avoids switching to the current buffer, so that if you have
162 a normal and a wildcard buffer for the same directory, \\[dired] will
163 toggle between those two."
164 :type 'boolean
165 :group 'dired-x)
167 (defcustom dired-omit-size-limit 30000
168 "Maximum size for the \"omitting\" feature.
169 If nil, there is no maximum size."
170 :type '(choice (const :tag "no maximum" nil) integer)
171 :group 'dired-x)
173 (defcustom dired-enable-local-variables t
174 "Control use of local-variables lists in Dired.
175 The value can be t, nil or something else.
176 A value of t means local-variables lists are obeyed;
177 nil means they are ignored; anything else means query.
179 This temporarily overrides the value of `enable-local-variables' when
180 listing a directory. See also `dired-local-variables-file'."
181 :type 'boolean
182 :group 'dired-x)
184 (defcustom dired-guess-shell-gnutar (when (or (eq system-type 'gnu)
185 (eq system-type 'gnu/linux))
186 "tar")
187 "If non-nil, name of GNU tar executable.
188 \(E.g., \"tar\" or \"gtar\"). The `z' switch will be used with it for
189 compressed or gzip'ed tar files. If you don't have GNU tar, set this
190 to nil: a pipe using `zcat' or `gunzip -c' will be used."
191 :type '(choice (const :tag "Not GNU tar" nil)
192 (string :tag "Command name"))
193 :group 'dired-x)
195 (defcustom dired-guess-shell-gzip-quiet t
196 "Non-nil says pass -q to gzip overriding verbose GZIP environment."
197 :type 'boolean
198 :group 'dired-x)
200 (defcustom dired-guess-shell-znew-switches nil
201 "If non-nil, then string of switches passed to `znew', example: \"-K\"."
202 :type '(choice (const :tag "None" nil)
203 (string :tag "Switches"))
204 :group 'dired-x)
206 (defcustom dired-clean-up-buffers-too t
207 "Non-nil means offer to kill buffers visiting files and dirs deleted in Dired."
208 :type 'boolean
209 :group 'dired-x)
211 ;;; KEY BINDINGS.
213 (define-key dired-mode-map "\M-o" 'dired-omit-mode)
214 (define-key dired-mode-map "*O" 'dired-mark-omitted)
215 (define-key dired-mode-map "\M-(" 'dired-mark-sexp)
216 (define-key dired-mode-map "*(" 'dired-mark-sexp)
217 (define-key dired-mode-map "*." 'dired-mark-extension)
218 (define-key dired-mode-map "\M-!" 'dired-smart-shell-command)
219 (define-key dired-mode-map "w" 'dired-copy-filename-as-kill)
220 (define-key dired-mode-map "\M-G" 'dired-goto-subdir)
221 (define-key dired-mode-map "F" 'dired-do-find-marked-files)
222 (define-key dired-mode-map "Y" 'dired-do-relsymlink)
223 (define-key dired-mode-map "%Y" 'dired-do-relsymlink-regexp)
224 (define-key dired-mode-map "V" 'dired-do-run-mail)
226 (if dired-bind-man
227 (define-key dired-mode-map "N" 'dired-man))
229 (if dired-bind-info
230 (define-key dired-mode-map "I" 'dired-info))
232 ;;; MENU BINDINGS
234 (require 'easymenu)
236 (let ((menu (lookup-key dired-mode-map [menu-bar])))
237 (easy-menu-add-item menu '("Operate")
238 ["Find Files" dired-do-find-marked-files
239 :help "Find current or marked files"]
240 "Shell Command...")
241 (easy-menu-add-item menu '("Operate")
242 ["Relative Symlink to..." dired-do-relsymlink
243 :visible (fboundp 'make-symbolic-link)
244 :help "Make relative symbolic links for current or \
245 marked files"]
246 "Hardlink to...")
247 (easy-menu-add-item menu '("Mark")
248 ["Flag Extension..." dired-flag-extension
249 :help "Flag files with a certain extension for deletion"]
250 "Mark Executables")
251 (easy-menu-add-item menu '("Mark")
252 ["Mark Extension..." dired-mark-extension
253 :help "Mark files with a certain extension"]
254 "Unmark All")
255 (easy-menu-add-item menu '("Mark")
256 ["Mark Omitted" dired-mark-omitted
257 :help "Mark files matching `dired-omit-files' \
258 and `dired-omit-extensions'"]
259 "Unmark All")
260 (easy-menu-add-item menu '("Regexp")
261 ["Relative Symlink..." dired-do-relsymlink-regexp
262 :visible (fboundp 'make-symbolic-link)
263 :help "Make relative symbolic links for files \
264 matching regexp"]
265 "Hardlink...")
266 (easy-menu-add-item menu '("Immediate")
267 ["Omit Mode" dired-omit-mode
268 :style toggle :selected dired-omit-mode
269 :help "Enable or disable omitting \"uninteresting\" \
270 files"]
271 "Refresh"))
273 ;;; GLOBAL BINDING.
274 (when dired-bind-jump
275 (define-key global-map "\C-x\C-j" 'dired-jump)
276 (define-key global-map "\C-x4\C-j" 'dired-jump-other-window))
279 ;; Install into appropriate hooks.
281 (add-hook 'dired-mode-hook 'dired-extra-startup)
282 (add-hook 'dired-after-readin-hook 'dired-omit-expunge)
284 (defun dired-extra-startup ()
285 "Automatically put on `dired-mode-hook' to get extra Dired features:
286 \\<dired-mode-map>
287 \\[dired-do-run-mail]\t-- run mail on folder (see `dired-bind-vm')
288 \\[dired-info]\t-- run info on file
289 \\[dired-man]\t-- run man on file
290 \\[dired-do-find-marked-files]\t-- visit all marked files simultaneously
291 \\[dired-omit-mode]\t-- toggle omitting of files
292 \\[dired-mark-sexp]\t-- mark by Lisp expression
293 \\[dired-copy-filename-as-kill]\t-- copy the file or subdir names into the kill ring;
294 \t you can feed it to other commands using \\[yank]
296 For more features, see variables
298 `dired-bind-vm'
299 `dired-bind-jump'
300 `dired-bind-info'
301 `dired-bind-man'
302 `dired-vm-read-only-folders'
303 `dired-omit-mode'
304 `dired-omit-files'
305 `dired-omit-extensions'
306 `dired-omit-size-limit'
307 `dired-find-subdir'
308 `dired-enable-local-variables'
309 `dired-local-variables-file'
310 `dired-guess-shell-gnutar'
311 `dired-guess-shell-gzip-quiet'
312 `dired-guess-shell-znew-switches'
313 `dired-guess-shell-alist-user'
314 `dired-clean-up-buffers-too'
316 See also functions
318 `dired-flag-extension'
319 `dired-virtual'
320 `dired-jump'
321 `dired-man'
322 `dired-vm'
323 `dired-rmail'
324 `dired-info'
325 `dired-do-find-marked-files'"
326 (interactive)
328 ;; These must be done in each new dired buffer.
329 (dired-hack-local-variables)
330 (dired-omit-startup))
333 ;;; BUFFER CLEANING.
335 ;; REDEFINE.
336 (defun dired-clean-up-after-deletion (fn)
337 "Clean up after a deleted file or directory FN.
338 Remove expanded subdir of deleted dir, if any."
339 (save-excursion (and (cdr dired-subdir-alist)
340 (dired-goto-subdir fn)
341 (dired-kill-subdir)))
343 ;; Offer to kill buffer of deleted file FN.
344 (if dired-clean-up-buffers-too
345 (progn
346 (let ((buf (get-file-buffer fn)))
347 (and buf
348 (funcall (function y-or-n-p)
349 (format "Kill buffer of %s, too? "
350 (file-name-nondirectory fn)))
351 (save-excursion ; you never know where kill-buffer leaves you
352 (kill-buffer buf))))
353 (let ((buf-list (dired-buffers-for-dir (expand-file-name fn)))
354 (buf nil))
355 (and buf-list
356 (y-or-n-p (format "Kill dired buffer%s of %s, too? "
357 (dired-plural-s (length buf-list))
358 (file-name-nondirectory fn)))
359 (while buf-list
360 (save-excursion (kill-buffer (car buf-list)))
361 (setq buf-list (cdr buf-list)))))))
362 ;; Anything else?
366 ;;; EXTENSION MARKING FUNCTIONS.
368 ;; Mark files with some extension.
369 (defun dired-mark-extension (extension &optional marker-char)
370 "Mark all files with a certain EXTENSION for use in later commands.
371 A `.' is *not* automatically prepended to the string entered."
372 ;; EXTENSION may also be a list of extensions instead of a single one.
373 ;; Optional MARKER-CHAR is marker to use.
374 (interactive "sMarking extension: \nP")
375 (or (listp extension)
376 (setq extension (list extension)))
377 (dired-mark-files-regexp
378 (concat ".";; don't match names with nothing but an extension
379 "\\("
380 (mapconcat 'regexp-quote extension "\\|")
381 "\\)$")
382 marker-char))
384 (defun dired-flag-extension (extension)
385 "In dired, flag all files with a certain EXTENSION for deletion.
386 A `.' is *not* automatically prepended to the string entered."
387 (interactive "sFlagging extension: ")
388 (dired-mark-extension extension dired-del-marker))
390 ;; Define some unpopular file extensions. Used for cleaning and omitting.
392 (defvar dired-patch-unclean-extensions
393 '(".rej" ".orig")
394 "List of extensions of dispensable files created by the `patch' program.")
396 (defvar dired-tex-unclean-extensions
397 '(".toc" ".log" ".aux");; these are already in completion-ignored-extensions
398 "List of extensions of dispensable files created by TeX.")
400 (defvar dired-latex-unclean-extensions
401 '(".idx" ".lof" ".lot" ".glo")
402 "List of extensions of dispensable files created by LaTeX.")
404 (defvar dired-bibtex-unclean-extensions
405 '(".blg" ".bbl")
406 "List of extensions of dispensable files created by BibTeX.")
408 (defvar dired-texinfo-unclean-extensions
409 '(".cp" ".cps" ".fn" ".fns" ".ky" ".kys" ".pg" ".pgs"
410 ".tp" ".tps" ".vr" ".vrs")
411 "List of extensions of dispensable files created by texinfo.")
413 (defun dired-clean-patch ()
414 "Flag dispensable files created by patch for deletion.
415 See variable `dired-patch-unclean-extensions'."
416 (interactive)
417 (dired-flag-extension dired-patch-unclean-extensions))
419 (defun dired-clean-tex ()
420 "Flag dispensable files created by [La]TeX etc. for deletion.
421 See variables `dired-tex-unclean-extensions',
422 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
423 `dired-texinfo-unclean-extensions'."
424 (interactive)
425 (dired-flag-extension (append dired-texinfo-unclean-extensions
426 dired-latex-unclean-extensions
427 dired-bibtex-unclean-extensions
428 dired-tex-unclean-extensions)))
430 (defun dired-very-clean-tex ()
431 "Flag dispensable files created by [La]TeX *and* \".dvi\" for deletion.
432 See variables `dired-texinfo-unclean-extensions',
433 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
434 `dired-texinfo-unclean-extensions'."
435 (interactive)
436 (dired-flag-extension (append dired-texinfo-unclean-extensions
437 dired-latex-unclean-extensions
438 dired-bibtex-unclean-extensions
439 dired-tex-unclean-extensions
440 (list ".dvi"))))
442 ;;; JUMP.
444 ;;;###autoload
445 (defun dired-jump (&optional other-window file-name)
446 "Jump to dired buffer corresponding to current buffer.
447 If in a file, dired the current directory and move to file's line.
448 If in Dired already, pop up a level and goto old directory's line.
449 In case the proper dired file line cannot be found, refresh the dired
450 buffer and try again.
451 When OTHER-WINDOW is non-nil, jump to dired buffer in other window.
452 Interactively with prefix argument, read FILE-NAME and
453 move to its line in dired."
454 (interactive
455 (list nil (and current-prefix-arg
456 (read-file-name "Jump to dired file: "))))
457 (let* ((file (or file-name buffer-file-name))
458 (dir (if file (file-name-directory file) default-directory)))
459 (if (and (eq major-mode 'dired-mode) (null file-name))
460 (progn
461 (setq dir (dired-current-directory))
462 (dired-up-directory other-window)
463 (or (dired-goto-file dir)
464 ;; refresh and try again
465 (progn
466 (dired-insert-subdir (file-name-directory dir))
467 (dired-goto-file dir))))
468 (if other-window
469 (dired-other-window dir)
470 (dired dir))
471 (if file
472 (or (dired-goto-file file)
473 ;; refresh and try again
474 (progn
475 (dired-insert-subdir (file-name-directory file))
476 (dired-goto-file file))
477 ;; Toggle omitting, if it is on, and try again.
478 (if dired-omit-mode
479 (progn
480 (dired-omit-mode)
481 (dired-goto-file file))))))))
483 (defun dired-jump-other-window (&optional file-name)
484 "Like \\[dired-jump] (`dired-jump') but in other window."
485 (interactive
486 (list (and current-prefix-arg
487 (read-file-name "Jump to dired file: "))))
488 (dired-jump t file-name))
490 ;;; OMITTING.
492 ;; Enhanced omitting of lines from directory listings.
493 ;; Marked files are never omitted.
495 ;; should probably get rid of this and always use 'no-dir.
496 ;; sk 28-Aug-1991 09:37
497 (defvar dired-omit-localp 'no-dir
498 "The LOCALP argument `dired-omit-expunge' passes to `dired-get-filename'.
499 If it is `no-dir', omitting is much faster, but you can only match
500 against the non-directory part of the file name. Set it to nil if you
501 need to match the entire file name.")
503 ;; \017=^O for Omit - other packages can chose other control characters.
504 (defvar dired-omit-marker-char ?\017
505 "Temporary marker used by Dired-Omit.
506 Should never be used as marker by the user or other packages.")
508 (defun dired-omit-startup ()
509 (or (assq 'dired-omit-mode minor-mode-alist)
510 (setq minor-mode-alist
511 (append '((dired-omit-mode
512 (:eval (if (eq major-mode 'dired-mode)
513 " Omit" ""))))
514 minor-mode-alist))))
516 (defun dired-mark-omitted ()
517 "Mark files matching `dired-omit-files' and `dired-omit-extensions'."
518 (interactive)
519 (let ((dired-omit-mode nil)) (revert-buffer)) ;; Show omitted files
520 (dired-mark-unmarked-files (dired-omit-regexp) nil nil dired-omit-localp))
522 (defcustom dired-omit-extensions
523 (append completion-ignored-extensions
524 dired-latex-unclean-extensions
525 dired-bibtex-unclean-extensions
526 dired-texinfo-unclean-extensions)
527 "If non-nil, a list of extensions \(strings\) to omit from Dired listings.
528 Defaults to elements of `completion-ignored-extensions',
529 `dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions', and
530 `dired-texinfo-unclean-extensions'.
532 See interactive function `dired-omit-mode' \(\\[dired-omit-mode]\) and
533 variables `dired-omit-mode' and `dired-omit-files'."
534 :type '(repeat string)
535 :group 'dired-x)
537 (defun dired-omit-expunge (&optional regexp)
538 "Erases all unmarked files matching REGEXP.
539 Does nothing if global variable `dired-omit-mode' is nil, or if called
540 non-interactively and buffer is bigger than `dired-omit-size-limit'.
541 If REGEXP is nil or not specified, uses `dired-omit-files', and also omits
542 filenames ending in `dired-omit-extensions'.
543 If REGEXP is the empty string, this function is a no-op.
545 This functions works by temporarily binding `dired-marker-char' to
546 `dired-omit-marker-char' and calling `dired-do-kill-lines'."
547 (interactive "sOmit files (regexp): ")
548 (if (and dired-omit-mode
549 (or (called-interactively-p 'interactive)
550 (not dired-omit-size-limit)
551 (< (buffer-size) dired-omit-size-limit)
552 (progn
553 (when dired-omit-verbose
554 (message "Not omitting: directory larger than %d characters."
555 dired-omit-size-limit))
556 (setq dired-omit-mode nil)
557 nil)))
558 (let ((omit-re (or regexp (dired-omit-regexp)))
559 (old-modified-p (buffer-modified-p))
560 count)
561 (or (string= omit-re "")
562 (let ((dired-marker-char dired-omit-marker-char))
563 (when dired-omit-verbose (message "Omitting..."))
564 (if (dired-mark-unmarked-files omit-re nil nil dired-omit-localp)
565 (progn
566 (setq count (dired-do-kill-lines
568 (if dired-omit-verbose "Omitted %d line%s." "")))
569 (force-mode-line-update))
570 (when dired-omit-verbose (message "(Nothing to omit)")))))
571 ;; Try to preserve modified state of buffer. So `%*' doesn't appear
572 ;; in mode-line of omitted buffers.
573 (set-buffer-modified-p (and old-modified-p
574 (save-excursion
575 (goto-char (point-min))
576 (re-search-forward dired-re-mark nil t))))
577 count)))
579 (defun dired-omit-regexp ()
580 (concat (if dired-omit-files (concat "\\(" dired-omit-files "\\)") "")
581 (if (and dired-omit-files dired-omit-extensions) "\\|" "")
582 (if dired-omit-extensions
583 (concat ".";; a non-extension part should exist
584 "\\("
585 (mapconcat 'regexp-quote dired-omit-extensions "\\|")
586 "\\)$")
587 "")))
589 ;; Returns t if any work was done, nil otherwise.
590 (defun dired-mark-unmarked-files (regexp msg &optional unflag-p localp)
591 "Mark unmarked files matching REGEXP, displaying MSG.
592 REGEXP is matched against the entire file name.
593 Does not re-mark files which already have a mark.
594 With prefix argument, unflag all those files.
595 Optional fourth argument LOCALP is as in `dired-get-filename'."
596 (interactive "P")
597 (let ((dired-marker-char (if unflag-p ?\s dired-marker-char)))
598 (dired-mark-if
599 (and
600 ;; not already marked
601 (looking-at " ")
602 ;; uninteresting
603 (let ((fn (dired-get-filename localp t)))
604 (and fn (string-match regexp fn))))
605 msg)))
607 ;; Compiler does not get fset.
608 (declare-function dired-omit-old-add-entry "dired-x")
610 ;; REDEFINE.
611 ;; Redefine dired-aux.el's version of `dired-add-entry'
612 ;; Save old defun if not already done:
613 (or (fboundp 'dired-omit-old-add-entry)
614 (fset 'dired-omit-old-add-entry (symbol-function 'dired-add-entry)))
616 ;; REDEFINE.
617 (defun dired-omit-new-add-entry (filename &optional marker-char relative)
618 ;; This redefines dired-aux.el's dired-add-entry to avoid calling ls for
619 ;; files that are going to be omitted anyway.
620 (if dired-omit-mode
621 ;; perhaps return t without calling ls
622 (let ((omit-re (dired-omit-regexp)))
623 (if (or (string= omit-re "")
624 (not
625 (string-match omit-re
626 (cond
627 ((eq 'no-dir dired-omit-localp)
628 filename)
629 ((eq t dired-omit-localp)
630 (dired-make-relative filename))
632 (dired-make-absolute
633 filename
634 (file-name-directory filename)))))))
635 ;; if it didn't match, go ahead and add the entry
636 (dired-omit-old-add-entry filename marker-char relative)
637 ;; dired-add-entry returns t for success, perhaps we should
638 ;; return file-exists-p
640 ;; omitting is not turned on at all
641 (dired-omit-old-add-entry filename marker-char relative)))
643 ;; Redefine it.
644 (fset 'dired-add-entry 'dired-omit-new-add-entry)
647 ;;; VIRTUAL DIRED MODE.
649 ;; For browsing `ls -lR' listings in a dired-like fashion.
651 (defalias 'virtual-dired 'dired-virtual)
652 (defun dired-virtual (dirname &optional switches)
653 "Put this buffer into Virtual Dired mode.
655 In Virtual Dired mode, all commands that do not actually consult the
656 filesystem will work.
658 This is useful if you want to peruse and move around in an ls -lR
659 output file, for example one you got from an ftp server. With
660 ange-ftp, you can even dired a directory containing an ls-lR file,
661 visit that file and turn on virtual dired mode. But don't try to save
662 this file, as dired-virtual indents the listing and thus changes the
663 buffer.
665 If you have save a Dired buffer in a file you can use \\[dired-virtual] to
666 resume it in a later session.
668 Type \\<dired-mode-map>\\[revert-buffer] \
669 in the Virtual Dired buffer and answer `y' to convert
670 the virtual to a real dired buffer again. You don't have to do this, though:
671 you can relist single subdirs using \\[dired-do-redisplay]."
673 ;; DIRNAME is the top level directory of the buffer. It will become
674 ;; its `default-directory'. If nil, the old value of
675 ;; default-directory is used.
677 ;; Optional SWITCHES are the ls switches to use.
679 ;; Shell wildcards will be used if there already is a `wildcard'
680 ;; line in the buffer (thus it is a saved Dired buffer), but there
681 ;; is no other way to get wildcards. Insert a `wildcard' line by
682 ;; hand if you want them.
684 (interactive
685 (list (read-string "Virtual Dired directory: " (dired-virtual-guess-dir))))
686 (goto-char (point-min))
687 (or (looking-at " ")
688 ;; if not already indented, do it now:
689 (indent-region (point-min) (point-max) 2))
690 (or dirname (setq dirname default-directory))
691 (setq dirname (expand-file-name (file-name-as-directory dirname)))
692 (setq default-directory dirname) ; contains no wildcards
693 (let ((wildcard (save-excursion
694 (goto-char (point-min))
695 (forward-line 1)
696 (and (looking-at "^ wildcard ")
697 (buffer-substring (match-end 0)
698 (progn (end-of-line) (point)))))))
699 (if wildcard
700 (setq dirname (expand-file-name wildcard default-directory))))
701 ;; If raw ls listing (not a saved old dired buffer), give it a
702 ;; decent subdir headerline:
703 (goto-char (point-min))
704 (or (looking-at dired-subdir-regexp)
705 (insert " "
706 (directory-file-name (file-name-directory default-directory))
707 ":\n"))
708 (dired-mode dirname (or switches dired-listing-switches))
709 (setq mode-name "Virtual Dired"
710 revert-buffer-function 'dired-virtual-revert)
711 (set (make-local-variable 'dired-subdir-alist) nil)
712 (dired-build-subdir-alist)
713 (goto-char (point-min))
714 (dired-initial-position dirname))
716 (defun dired-virtual-guess-dir ()
717 "Guess and return appropriate working directory of this buffer.
718 The buffer is assumed to be in Dired or ls -lR format. The guess is
719 based upon buffer contents. If nothing could be guessed, returns
720 nil."
722 (let ((regexp "^\\( \\)?\\([^ \n\r]*\\)\\(:\\)[\n\r]")
723 (subexpr 2))
724 (goto-char (point-min))
725 (cond ((looking-at regexp)
726 ;; If a saved dired buffer, look to which dir and
727 ;; perhaps wildcard it belongs:
728 (let ((dir (buffer-substring (match-beginning subexpr)
729 (match-end subexpr))))
730 (file-name-as-directory dir)))
731 ;; Else no match for headerline found. It's a raw ls listing.
732 ;; In raw ls listings the directory does not have a headerline
733 ;; try parent of first subdir, if any
734 ((re-search-forward regexp nil t)
735 (file-name-directory
736 (directory-file-name
737 (file-name-as-directory
738 (buffer-substring (match-beginning subexpr)
739 (match-end subexpr))))))
740 (t ; if all else fails
741 nil))))
744 (defun dired-virtual-revert (&optional arg noconfirm)
745 (if (not
746 (y-or-n-p "Cannot revert a Virtual Dired buffer - switch to Real Dired mode? "))
747 (error "Cannot revert a Virtual Dired buffer")
748 (setq mode-name "Dired"
749 revert-buffer-function 'dired-revert)
750 (revert-buffer)))
752 ;; A zero-arg version of dired-virtual.
753 (defun dired-virtual-mode ()
754 "Put current buffer into Virtual Dired mode (see `dired-virtual').
755 Useful on `magic-mode-alist' with the regexp
757 \"^ \\\\(/[^ /]+\\\\)+/?:$\"
759 to put saved dired buffers automatically into Virtual Dired mode.
761 Also useful for `auto-mode-alist' like this:
763 (add-to-list 'auto-mode-alist
764 '(\"[^/]\\\\.dired\\\\'\" . dired-virtual-mode))"
765 (interactive)
766 (dired-virtual (dired-virtual-guess-dir)))
769 ;;; SMART SHELL.
771 ;; An Emacs buffer can have but one working directory, stored in the
772 ;; buffer-local variable `default-directory'. A Dired buffer may have
773 ;; several subdirectories inserted, but still has but one working directory:
774 ;; that of the top level Dired directory in that buffer. For some commands
775 ;; it is appropriate that they use the current Dired directory instead of
776 ;; `default-directory', e.g., `find-file' and `compile'. This is a general
777 ;; mechanism is provided for special handling of the working directory in
778 ;; special major modes.
780 ;; It's easier to add to this alist than redefine function
781 ;; default-directory while keeping the old information.
782 (defconst default-directory-alist
783 '((dired-mode . (if (fboundp 'dired-current-directory)
784 (dired-current-directory)
785 default-directory)))
786 "Alist of major modes and their opinion on `default-directory'.
787 This is given as a Lisp expression to evaluate. A resulting value of
788 nil is ignored in favor of `default-directory'.")
790 (defun dired-default-directory ()
791 "Usage like variable `default-directory'.
792 Knows about the special cases in variable `default-directory-alist'."
793 (or (eval (cdr (assq major-mode default-directory-alist)))
794 default-directory))
796 (defun dired-smart-shell-command (command &optional output-buffer error-buffer)
797 "Like function `shell-command', but in the current Virtual Dired directory."
798 (interactive
799 (list
800 (read-shell-command "Shell command: " nil nil
801 (cond
802 (buffer-file-name (file-relative-name buffer-file-name))
803 ((eq major-mode 'dired-mode) (dired-get-filename t t))))
804 current-prefix-arg
805 shell-command-default-error-buffer))
806 (let ((default-directory (dired-default-directory)))
807 (shell-command command output-buffer error-buffer)))
810 ;;; LOCAL VARIABLES FOR DIRED BUFFERS.
812 ;; Brief Description:
814 ;; * `dired-extra-startup' is part of the `dired-mode-hook'.
816 ;; * `dired-extra-startup' calls `dired-hack-local-variables'
818 ;; * `dired-hack-local-variables' checks the value of
819 ;;; `dired-local-variables-file'
821 ;; * Check if `dired-local-variables-file' is a non-nil string and is a
822 ;;; filename found in the directory of the Dired Buffer being created.
824 ;; * If `dired-local-variables-file' satisfies the above, then temporarily
825 ;;; include it in the Dired Buffer at the bottom.
827 ;; * Set `enable-local-variables' temporarily to the user variable
828 ;;; `dired-enable-local-variables' and run `hack-local-variables' on the
829 ;;; Dired Buffer.
831 ;; FIXME do standard dir-locals obsolete this?
832 (defcustom dired-local-variables-file (convert-standard-filename ".dired")
833 "Filename, as string, containing local dired buffer variables to be hacked.
834 If this file found in current directory, then it will be inserted into dired
835 buffer and `hack-local-variables' will be run. See Info node
836 `(emacs)File Variables' for more information on local variables.
837 See also `dired-enable-local-variables'."
838 :type 'file
839 :group 'dired)
841 (defun dired-hack-local-variables ()
842 "Evaluate local variables in `dired-local-variables-file' for dired buffer."
843 (if (and dired-local-variables-file
844 (stringp dired-local-variables-file)
845 (file-exists-p dired-local-variables-file))
846 (let ((opoint (point-max))
847 buffer-read-only
848 ;; In case user has `enable-local-variables' set to nil we
849 ;; override it locally with dired's variable.
850 (enable-local-variables dired-enable-local-variables))
851 ;; Insert 'em.
852 (save-excursion
853 (goto-char opoint)
854 (insert "\^L\n")
855 (insert-file-contents dired-local-variables-file))
856 ;; Hack 'em.
857 (let ((buffer-file-name dired-local-variables-file))
858 (hack-local-variables))
859 ;; Make sure that the modeline shows the proper information.
860 (dired-sort-set-modeline)
861 ;; Delete this stuff: `eobp' is used to find last subdir by dired.el.
862 (delete-region opoint (point-max)))))
864 (defun dired-omit-here-always ()
865 "Create `dired-local-variables-file' for omitting and reverts directory.
866 Sets `dired-omit-mode' to t in a local variables file that is readable by
867 dired."
868 (interactive)
869 (if (file-exists-p dired-local-variables-file)
870 (message "File `./%s' already exists." dired-local-variables-file)
872 ;; Create `dired-local-variables-file'.
873 (with-current-buffer (get-buffer-create " *dot-dired*")
874 (erase-buffer)
875 (insert "Local Variables:\ndired-omit-mode: t\nEnd:\n")
876 (write-file dired-local-variables-file)
877 (kill-buffer (current-buffer)))
879 ;; Run extra-hooks and revert directory.
880 (dired-extra-startup)
881 (dired-revert)))
884 ;;; GUESS SHELL COMMAND.
886 ;; Brief Description:
888 ;; `dired-do-shell-command' is bound to `!' by dired.el.
890 ;; * Redefine `dired-read-shell-command' so it calls
891 ;;; `dired-guess-shell-command'.
893 ;; * `dired-guess-shell-command' calls `dired-guess-default' with list of
894 ;;; marked files.
896 ;; * Parse `dired-guess-shell-alist-user' and
897 ;;; `dired-guess-shell-alist-default' (in that order) for the first REGEXP
898 ;;; that matches the first file in the file list.
900 ;; * If the REGEXP matches all the entries of the file list then evaluate
901 ;;; COMMAND, which is either a string or a Lisp expression returning a
902 ;;; string. COMMAND may be a list of commands.
904 ;; * Return this command to `dired-guess-shell-command' which prompts user
905 ;;; with it. The list of commands is put into the list of default values.
906 ;;; If a command is used successfully then it is stored permanently in
907 ;;; `dired-shell-command-history'.
909 ;; Guess what shell command to apply to a file.
910 (defvar dired-shell-command-history nil
911 "History list for commands that read dired-shell commands.")
913 ;; Default list of shell commands.
915 ;; NOTE: Use `gunzip -c' instead of `zcat' on `.gz' files. Some do not
916 ;; install GNU zip's version of zcat.
918 (declare-function Man-support-local-filenames "man" ())
920 (defvar dired-guess-shell-alist-default
921 (list
922 (list "\\.tar$"
923 '(if dired-guess-shell-gnutar
924 (concat dired-guess-shell-gnutar " xvf")
925 "tar xvf")
926 ;; Extract files into a separate subdirectory
927 '(if dired-guess-shell-gnutar
928 (concat "mkdir " (file-name-sans-extension file)
929 "; " dired-guess-shell-gnutar " -C "
930 (file-name-sans-extension file) " -xvf")
931 (concat "mkdir " (file-name-sans-extension file)
932 "; tar -C " (file-name-sans-extension file) " -xvf"))
933 ;; List archive contents.
934 '(if dired-guess-shell-gnutar
935 (concat dired-guess-shell-gnutar " tvf")
936 "tar tvf"))
938 ;; REGEXPS for compressed archives must come before the .Z rule to
939 ;; be recognized:
940 (list "\\.tar\\.Z$"
941 ;; Untar it.
942 '(if dired-guess-shell-gnutar
943 (concat dired-guess-shell-gnutar " zxvf")
944 (concat "zcat * | tar xvf -"))
945 ;; Optional conversion to gzip format.
946 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
947 " " dired-guess-shell-znew-switches))
949 ;; gzip'ed archives
950 (list "\\.t\\(ar\\.\\)?gz$"
951 '(if dired-guess-shell-gnutar
952 (concat dired-guess-shell-gnutar " zxvf")
953 (concat "gunzip -qc * | tar xvf -"))
954 ;; Extract files into a separate subdirectory
955 '(if dired-guess-shell-gnutar
956 (concat "mkdir " (file-name-sans-extension file)
957 "; " dired-guess-shell-gnutar " -C "
958 (file-name-sans-extension file) " -zxvf")
959 (concat "mkdir " (file-name-sans-extension file)
960 "; gunzip -qc * | tar -C "
961 (file-name-sans-extension file) " -xvf -"))
962 ;; Optional decompression.
963 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q" ""))
964 ;; List archive contents.
965 '(if dired-guess-shell-gnutar
966 (concat dired-guess-shell-gnutar " ztvf")
967 (concat "gunzip -qc * | tar tvf -")))
969 ;; bzip2'ed archives
970 (list "\\.t\\(ar\\.bz2\\|bz\\)$"
971 "bunzip2 -c * | tar xvf -"
972 ;; Extract files into a separate subdirectory
973 '(concat "mkdir " (file-name-sans-extension file)
974 "; bunzip2 -c * | tar -C "
975 (file-name-sans-extension file) " -xvf -")
976 ;; Optional decompression.
977 "bunzip2")
979 ;; xz'ed archives
980 (list "\\.t\\(ar\\.\\)?xz$"
981 "unxz -c * | tar xvf -"
982 ;; Extract files into a separate subdirectory
983 '(concat "mkdir " (file-name-sans-extension file)
984 "; unxz -c * | tar -C "
985 (file-name-sans-extension file) " -xvf -")
986 ;; Optional decompression.
987 "unxz")
989 '("\\.shar\\.Z$" "zcat * | unshar")
990 '("\\.shar\\.g?z$" "gunzip -qc * | unshar")
992 '("\\.e?ps$" "ghostview" "xloadimage" "lpr")
993 (list "\\.e?ps\\.g?z$" "gunzip -qc * | ghostview -"
994 ;; Optional decompression.
995 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
996 (list "\\.e?ps\\.Z$" "zcat * | ghostview -"
997 ;; Optional conversion to gzip format.
998 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
999 " " dired-guess-shell-znew-switches))
1001 '("\\.patch$" "cat * | patch")
1002 (list "\\.patch\\.g?z$" "gunzip -qc * | patch"
1003 ;; Optional decompression.
1004 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
1005 (list "\\.patch\\.Z$" "zcat * | patch"
1006 ;; Optional conversion to gzip format.
1007 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
1008 " " dired-guess-shell-znew-switches))
1010 ;; The following four extensions are useful with dired-man ("N" key)
1011 (list "\\.\\(?:[0-9]\\|man\\)$" '(progn (require 'man)
1012 (if (Man-support-local-filenames)
1013 "man -l"
1014 "cat * | tbl | nroff -man -h")))
1015 (list "\\.\\(?:[0-9]\\|man\\)\\.g?z$" '(progn (require 'man)
1016 (if (Man-support-local-filenames)
1017 "man -l"
1018 "gunzip -qc * | tbl | nroff -man -h"))
1019 ;; Optional decompression.
1020 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
1021 (list "\\.[0-9]\\.Z$" '(progn (require 'man)
1022 (if (Man-support-local-filenames)
1023 "man -l"
1024 "zcat * | tbl | nroff -man -h"))
1025 ;; Optional conversion to gzip format.
1026 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
1027 " " dired-guess-shell-znew-switches))
1028 '("\\.pod$" "perldoc" "pod2man * | nroff -man")
1030 '("\\.dvi$" "xdvi" "dvips") ; preview and printing
1031 '("\\.au$" "play") ; play Sun audiofiles
1032 '("\\.mpe?g$\\|\\.avi$" "xine -p")
1033 '("\\.ogg$" "ogg123")
1034 '("\\.mp3$" "mpg123")
1035 '("\\.wav$" "play")
1036 '("\\.uu$" "uudecode") ; for uudecoded files
1037 '("\\.hqx$" "mcvert")
1038 '("\\.sh$" "sh") ; execute shell scripts
1039 '("\\.xbm$" "bitmap") ; view X11 bitmaps
1040 '("\\.gp$" "gnuplot")
1041 '("\\.p[bgpn]m$" "xloadimage")
1042 '("\\.gif$" "xloadimage") ; view gif pictures
1043 '("\\.tif$" "xloadimage")
1044 '("\\.png$" "display") ; xloadimage 4.1 doesn't grok PNG
1045 '("\\.jpe?g$" "xloadimage")
1046 '("\\.fig$" "xfig") ; edit fig pictures
1047 '("\\.out$" "xgraph") ; for plotting purposes.
1048 '("\\.tex$" "latex" "tex")
1049 '("\\.texi\\(nfo\\)?$" "makeinfo" "texi2dvi")
1050 '("\\.pdf$" "xpdf")
1051 '("\\.doc$" "antiword" "strings")
1052 '("\\.rpm$" "rpm -qilp" "rpm -ivh")
1053 '("\\.dia$" "dia")
1054 '("\\.mgp$" "mgp")
1056 ;; Some other popular archivers.
1057 (list "\\.zip$" "unzip" "unzip -l"
1058 ;; Extract files into a separate subdirectory
1059 '(concat "unzip" (if dired-guess-shell-gzip-quiet " -q")
1060 " -d " (file-name-sans-extension file)))
1061 '("\\.zoo$" "zoo x//")
1062 '("\\.lzh$" "lharc x")
1063 '("\\.arc$" "arc x")
1064 '("\\.shar$" "unshar")
1066 ;; Compression.
1067 (list "\\.g?z$" '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
1068 (list "\\.dz$" "dictunzip")
1069 (list "\\.bz2$" "bunzip2")
1070 (list "\\.xz$" "unxz")
1071 (list "\\.Z$" "uncompress"
1072 ;; Optional conversion to gzip format.
1073 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
1074 " " dired-guess-shell-znew-switches))
1076 '("\\.sign?$" "gpg --verify"))
1078 "Default alist used for shell command guessing.
1079 See `dired-guess-shell-alist-user'.")
1081 (defcustom dired-guess-shell-alist-user nil
1082 "User-defined alist of rules for suggested commands.
1083 These rules take precedence over the predefined rules in the variable
1084 `dired-guess-shell-alist-default' (to which they are prepended).
1086 Each element of this list looks like
1088 \(REGEXP COMMAND...\)
1090 where each COMMAND can either be a string or a Lisp expression that evaluates
1091 to a string. If several COMMANDs are given, the first one will be the default
1092 and the rest will be added temporarily to the history and can be retrieved
1093 with \\[previous-history-element] (M-p) .
1095 The variable `dired-guess-shell-case-fold-search' controls whether
1096 REGEXP is matched case-sensitively.
1098 You can set this variable in your ~/.emacs. For example, to add rules for
1099 `.foo' and `.bar' files, write
1101 \(setq dired-guess-shell-alist-user
1102 (list (list \"\\\\.foo\\\\'\" \"FOO-COMMAND\");; fixed rule
1103 ;; possibly more rules ...
1104 (list \"\\\\.bar\\\\'\";; rule with condition test
1105 '(if condition
1106 \"BAR-COMMAND-1\"
1107 \"BAR-COMMAND-2\")))\)"
1108 :group 'dired-x
1109 :type '(alist :key-type regexp :value-type (repeat sexp)))
1111 (defcustom dired-guess-shell-case-fold-search t
1112 "If non-nil, `dired-guess-shell-alist-default' and
1113 `dired-guess-shell-alist-user' are matched case-insensitively."
1114 :group 'dired-x
1115 :type 'boolean)
1117 (defun dired-guess-default (files)
1118 "Guess a shell commands for FILES. Return command or list of commands.
1119 See `dired-guess-shell-alist-user'."
1121 (let* ((case-fold-search dired-guess-shell-case-fold-search)
1122 ;; Prepend the user's alist to the default alist.
1123 (alist (append dired-guess-shell-alist-user
1124 dired-guess-shell-alist-default))
1125 (file (car files))
1126 (flist (cdr files))
1127 elt regexp cmds)
1129 ;; Find the first match in the alist for first file in FILES.
1130 (while alist
1131 (setq elt (car alist)
1132 regexp (car elt)
1133 alist (cdr alist))
1134 (if (string-match regexp file)
1135 (setq cmds (cdr elt)
1136 alist nil)))
1138 ;; If more than one file, see if all of FILES match regular expression.
1139 (while (and flist
1140 (string-match regexp (car flist)))
1141 (setq flist (cdr flist)))
1143 ;; If flist is still non-nil, then do not guess since this means that not
1144 ;; all the files in FILES were matched by the regexp.
1145 (setq cmds (and (not flist) cmds))
1147 ;; Return commands or nil if flist is still non-nil.
1148 ;; Evaluate the commands in order that any logical testing will be done.
1149 (cond ((not (cdr cmds))
1150 (eval (car cmds))) ; single command
1152 (mapcar (function eval) cmds)))))
1154 (defun dired-guess-shell-command (prompt files)
1155 "Ask user with PROMPT for a shell command, guessing a default from FILES."
1156 (let ((default (dired-guess-default files))
1157 default-list val)
1158 (if (null default)
1159 ;; Nothing to guess
1160 (read-shell-command prompt nil 'dired-shell-command-history)
1161 (if (listp default)
1162 ;; More than one guess
1163 (setq default-list default
1164 default (car default)
1165 prompt (concat
1166 prompt
1167 (format "{%d guesses} " (length default-list))))
1168 ;; Just one guess
1169 (setq default-list (list default)))
1170 ;; Put the first guess in the prompt but not in the initial value.
1171 (setq prompt (concat prompt (format "[%s] " default)))
1172 ;; All guesses can be retrieved with M-n
1173 (setq val (read-shell-command prompt nil
1174 'dired-shell-command-history
1175 default-list))
1176 ;; If we got a return, then return default.
1177 (if (equal val "") default val))))
1179 ;; REDEFINE.
1180 ;; Redefine dired-aux.el's version:
1181 (defun dired-read-shell-command (prompt arg files)
1182 "Read a dired shell command prompting with PROMPT (using `read-shell-command').
1183 ARG is the prefix arg and may be used to indicate in the prompt which
1184 FILES are affected.
1185 This is an extra function so that you can redefine it."
1186 (minibuffer-with-setup-hook
1187 (lambda ()
1188 (set (make-local-variable 'minibuffer-default-add-function)
1189 'minibuffer-default-add-dired-shell-commands))
1190 (dired-mark-pop-up
1191 nil 'shell files
1192 'dired-guess-shell-command
1193 (format prompt (dired-mark-prompt arg files)) ; PROMPT
1194 files))) ; FILES
1197 ;;; RELATIVE SYMBOLIC LINKS.
1199 (declare-function make-symbolic-link "fileio.c")
1201 (defvar dired-keep-marker-relsymlink ?S
1202 "See variable `dired-keep-marker-move'.")
1204 (defun dired-make-relative-symlink (file1 file2 &optional ok-if-already-exists)
1205 "Make a symbolic link (pointing to FILE1) in FILE2.
1206 The link is relative (if possible), for example
1208 \"/vol/tex/bin/foo\" \"/vol/local/bin/foo\"
1210 results in
1212 \"../../tex/bin/foo\" \"/vol/local/bin/foo\""
1213 (interactive "FRelSymLink: \nFRelSymLink %s: \np")
1214 (let (name1 name2 len1 len2 (index 0) sub)
1215 (setq file1 (expand-file-name file1)
1216 file2 (expand-file-name file2)
1217 len1 (length file1)
1218 len2 (length file2))
1219 ;; Find common initial file name components:
1220 (let (next)
1221 (while (and (setq next (string-match "/" file1 index))
1222 (setq next (1+ next))
1223 (< next (min len1 len2))
1224 ;; For the comparison, both substrings must end in
1225 ;; `/', so NEXT is *one plus* the result of the
1226 ;; string-match.
1227 ;; E.g., consider the case of linking "/tmp/a/abc"
1228 ;; to "/tmp/abc" erroneously giving "/tmp/a" instead
1229 ;; of "/tmp/" as common initial component
1230 (string-equal (substring file1 0 next)
1231 (substring file2 0 next)))
1232 (setq index next))
1233 (setq name2 file2
1234 sub (substring file1 0 index)
1235 name1 (substring file1 index)))
1236 (if (string-equal sub "/")
1237 ;; No common initial file name found
1238 (setq name1 file1)
1239 ;; Else they have a common parent directory
1240 (let ((tem (substring file2 index))
1241 (start 0)
1242 (count 0))
1243 ;; Count number of slashes we must compensate for ...
1244 (while (setq start (string-match "/" tem start))
1245 (setq count (1+ count)
1246 start (1+ start)))
1247 ;; ... and prepend a "../" for each slash found:
1248 (while (> count 0)
1249 (setq count (1- count)
1250 name1 (concat "../" name1)))))
1251 (make-symbolic-link
1252 (directory-file-name name1) ; must not link to foo/
1253 ; (trailing slash!)
1254 name2 ok-if-already-exists)))
1256 ;;;###autoload
1257 (defun dired-do-relsymlink (&optional arg)
1258 "Relative symlink all marked (or next ARG) files into a directory.
1259 Otherwise make a relative symbolic link to the current file.
1260 This creates relative symbolic links like
1262 foo -> ../bar/foo
1264 not absolute ones like
1266 foo -> /ugly/file/name/that/may/change/any/day/bar/foo
1268 For absolute symlinks, use \\[dired-do-symlink]."
1269 (interactive "P")
1270 (dired-do-create-files 'relsymlink (function dired-make-relative-symlink)
1271 "RelSymLink" arg dired-keep-marker-relsymlink))
1273 (defun dired-do-relsymlink-regexp (regexp newname &optional arg whole-name)
1274 "RelSymlink all marked files containing REGEXP to NEWNAME.
1275 See functions `dired-do-rename-regexp' and `dired-do-relsymlink'
1276 for more info."
1277 (interactive (dired-mark-read-regexp "RelSymLink"))
1278 (dired-do-create-files-regexp
1279 (function dired-make-relative-symlink)
1280 "RelSymLink" arg regexp newname whole-name dired-keep-marker-relsymlink))
1283 ;;; VISIT ALL MARKED FILES SIMULTANEOUSLY.
1285 ;; Brief Description:
1287 ;; `dired-do-find-marked-files' is bound to `F' by dired-x.el.
1289 ;; * Use `dired-get-marked-files' to collect the marked files in the current
1290 ;;; Dired Buffer into a list of filenames `FILE-LIST'.
1292 ;; * Pass FILE-LIST to `dired-simultaneous-find-file' all with
1293 ;;; `dired-do-find-marked-files''s prefix argument NOSELECT.
1295 ;; * `dired-simultaneous-find-file' runs through FILE-LIST decrementing the
1296 ;;; list each time.
1298 ;; * If NOSELECT is non-nil then just run `find-file-noselect' on each
1299 ;;; element of FILE-LIST.
1301 ;; * If NOSELECT is nil then calculate the `size' of the window for each file
1302 ;;; by dividing the `window-height' by length of FILE-LIST. Thus, `size' is
1303 ;;; cognizant of the window-configuration.
1305 ;; * If `size' is too small abort, otherwise run `find-file' on each element
1306 ;;; of FILE-LIST giving each a window of height `size'.
1308 (defun dired-do-find-marked-files (&optional noselect)
1309 "Find all marked files displaying all of them simultaneously.
1310 With optional NOSELECT just find files but do not select them.
1312 The current window is split across all files marked, as evenly as possible.
1313 Remaining lines go to bottom-most window. The number of files that can be
1314 displayed this way is restricted by the height of the current window and
1315 `window-min-height'.
1317 To keep dired buffer displayed, type \\[split-window-vertically] first.
1318 To display just marked files, type \\[delete-other-windows] first."
1320 (interactive "P")
1321 (dired-simultaneous-find-file (dired-get-marked-files) noselect))
1323 (defun dired-simultaneous-find-file (file-list noselect)
1325 "Visit all files in FILE-LIST and display them simultaneously.
1326 The current window is split across all files in FILE-LIST, as evenly as
1327 possible. Remaining lines go to the bottom-most window. The number of
1328 files that can be displayed this way is restricted by the height of the
1329 current window and the variable `window-min-height'. With non-nil
1330 NOSELECT the files are merely found but not selected."
1332 ;; We don't make this function interactive because it is usually too clumsy
1333 ;; to specify FILE-LIST interactively unless via dired.
1335 (let (size)
1337 (if noselect
1338 ;; Do not select the buffer.
1339 (find-file-noselect (car file-list))
1341 ;; We will have to select the buffer. Calculate and check window size.
1342 (setq size (/ (window-height) (length file-list)))
1343 (or (<= window-min-height size)
1344 (error "Too many files to visit simultaneously. Try C-u prefix"))
1345 (find-file (car file-list)))
1347 ;; Decrement.
1348 (setq file-list (cdr file-list))
1350 (while file-list
1352 (if noselect
1353 ;; Do not select the buffer.
1354 (find-file-noselect (car file-list))
1356 ;; Vertically split off a window of desired size. Upper window will
1357 ;; have SIZE lines. Select lower (larger) window. We split it again.
1358 (select-window (split-window nil size))
1359 (find-file (car file-list)))
1361 ;; Decrement.
1362 (setq file-list (cdr file-list)))))
1365 ;;; MISCELLANEOUS COMMANDS.
1367 ;; Run man on files.
1369 (declare-function Man-getpage-in-background "man" (topic))
1371 (defun dired-man ()
1372 "Run man on this file. Display old buffer if buffer name matches filename.
1373 Uses `man.el' of \\[manual-entry] fame."
1374 (interactive)
1375 (require 'man)
1376 (let* ((file (dired-get-filename))
1377 (manual-program (replace-regexp-in-string "\\*" "%s"
1378 (dired-guess-shell-command
1379 "Man command: " (list file)))))
1380 (Man-getpage-in-background file)))
1382 ;; Run Info on files.
1384 (defun dired-info ()
1385 "Run info on this file."
1386 (interactive)
1387 (info (dired-get-filename)))
1389 ;; Run mail on mail folders.
1391 (declare-function vm-visit-folder "ext:vm" (folder &optional read-only))
1392 (defvar vm-folder-directory)
1394 (defun dired-vm (&optional read-only)
1395 "Run VM on this file.
1396 With prefix arg, visit folder read-only (this requires at least VM 5).
1397 See also variable `dired-vm-read-only-folders'."
1398 (interactive "P")
1399 (let ((dir (dired-current-directory))
1400 (fil (dired-get-filename)))
1401 ;; take care to supply 2nd arg only if requested - may still run VM 4!
1402 (cond (read-only (vm-visit-folder fil t))
1403 ((eq t dired-vm-read-only-folders) (vm-visit-folder fil t))
1404 ((null dired-vm-read-only-folders) (vm-visit-folder fil))
1405 (t (vm-visit-folder fil (not (file-writable-p fil)))))
1406 ;; so that pressing `v' inside VM does prompt within current directory:
1407 (set (make-local-variable 'vm-folder-directory) dir)))
1409 (defun dired-rmail ()
1410 "Run RMAIL on this file."
1411 (interactive)
1412 (rmail (dired-get-filename)))
1414 (defun dired-do-run-mail ()
1415 "If `dired-bind-vm' is t, then function `dired-vm', otherwise `dired-rmail'."
1416 (interactive)
1417 (if dired-bind-vm
1418 ;; Read mail folder using vm.
1419 (dired-vm)
1420 ;; Read mail folder using rmail.
1421 (dired-rmail)))
1424 ;;; MISCELLANEOUS INTERNAL FUNCTIONS.
1426 (declare-function dired-old-find-buffer-nocreate "dired-x")
1428 (or (fboundp 'dired-old-find-buffer-nocreate)
1429 (fset 'dired-old-find-buffer-nocreate
1430 (symbol-function 'dired-find-buffer-nocreate)))
1432 ;; REDEFINE.
1433 ;; Redefines dired.el's version of `dired-find-buffer-nocreate'
1434 (defun dired-find-buffer-nocreate (dirname &optional mode)
1435 (if (and dired-find-subdir
1436 ;; don't try to find a wildcard as a subdirectory
1437 (string-equal dirname (file-name-directory dirname)))
1438 (let* ((cur-buf (current-buffer))
1439 (buffers (nreverse
1440 (dired-buffers-for-dir (expand-file-name dirname))))
1441 (cur-buf-matches (and (memq cur-buf buffers)
1442 ;; wildcards must match, too:
1443 (equal dired-directory dirname))))
1444 ;; We don't want to switch to the same buffer---
1445 (setq buffers (delq cur-buf buffers));;need setq with delq
1446 (or (car (sort buffers (function dired-buffer-more-recently-used-p)))
1447 ;; ---unless it's the only possibility:
1448 (and cur-buf-matches cur-buf)))
1449 (dired-old-find-buffer-nocreate dirname mode)))
1451 ;; This should be a builtin
1452 (defun dired-buffer-more-recently-used-p (buffer1 buffer2)
1453 "Return t if BUFFER1 is more recently used than BUFFER2."
1454 (if (equal buffer1 buffer2)
1456 (let ((more-recent nil)
1457 (list (buffer-list)))
1458 (while (and list
1459 (not (setq more-recent (equal buffer1 (car list))))
1460 (not (equal buffer2 (car list))))
1461 (setq list (cdr list)))
1462 more-recent)))
1464 ;; Same thing as `dired-buffers-for-dir' of dired.el? - lrd 11/23/93
1465 ;; (defun dired-buffers-for-dir-exact (dir)
1466 ;; ;; Return a list of buffers that dired DIR (a directory or wildcard)
1467 ;; ;; at top level, or as subdirectory.
1468 ;; ;; Top level matches must match the wildcard part too, if any.
1469 ;; ;; The list is in reverse order of buffer creation, most recent last.
1470 ;; ;; As a side effect, killed dired buffers for DIR are removed from
1471 ;; ;; dired-buffers.
1472 ;; (let ((alist dired-buffers) result elt)
1473 ;; (while alist
1474 ;; (setq elt (car alist)
1475 ;; alist (cdr alist))
1476 ;; (let ((buf (cdr elt)))
1477 ;; (if (buffer-name buf)
1478 ;; ;; Top level must match exactly against dired-directory in
1479 ;; ;; case one of them is a wildcard.
1480 ;; (if (or (equal dir (with-current-buffer buf dired-directory))
1481 ;; (assoc dir (with-current-buffer buf dired-subdir-alist)))
1482 ;; (setq result (cons buf result)))
1483 ;; ;; else buffer is killed - clean up:
1484 ;; (setq dired-buffers (delq elt dired-buffers)))))
1485 ;; result))
1487 ;; REDEFINE.
1488 ;; Redefines dired.el's version of `dired-initial-position'
1489 (defun dired-initial-position (dirname)
1490 "Where point should go in a new listing of DIRNAME.
1491 Point assumed at beginning of new subdir line.
1492 You may redefine this function as you wish, e.g. like in `dired-x.el'."
1493 (end-of-line)
1494 (if dired-find-subdir (dired-goto-subdir dirname)) ; new
1495 (if dired-trivial-filenames (dired-goto-next-nontrivial-file)))
1498 ;; Does anyone use this? - lrd 6/29/93.
1499 ;; Apparently people do use it. - lrd 12/22/97.
1500 (defun dired-mark-sexp (predicate &optional unflag-p)
1501 "Mark files for which PREDICATE returns non-nil.
1502 With a prefix arg, unflag those files instead.
1504 PREDICATE is a lisp expression that can refer to the following symbols:
1506 inode [integer] the inode of the file (only for ls -i output)
1507 s [integer] the size of the file for ls -s output
1508 (usually in blocks or, with -k, in KByte)
1509 mode [string] file permission bits, e.g. \"-rw-r--r--\"
1510 nlink [integer] number of links to file
1511 uid [string] owner
1512 gid [string] group (If the gid is not displayed by ls,
1513 this will still be set (to the same as uid))
1514 size [integer] file size in bytes
1515 time [string] the time that ls displays, e.g. \"Feb 12 14:17\"
1516 name [string] the name of the file
1517 sym [string] if file is a symbolic link, the linked-to name, else \"\"
1519 For example, use
1521 (equal 0 size)
1523 to mark all zero length files."
1524 ;; Using sym="" instead of nil avoids the trap of
1525 ;; (string-match "foo" sym) into which a user would soon fall.
1526 ;; Give `equal' instead of `=' in the example, as this works on
1527 ;; integers and strings.
1528 (interactive "xMark if (lisp expr): \nP")
1529 (message "%s" predicate)
1530 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char))
1531 inode s mode nlink uid gid size time name sym)
1532 (dired-mark-if
1533 (save-excursion
1534 (and
1535 ;; Sets vars
1536 ;; inode s mode nlink uid gid size time name sym
1538 ;; according to current file line. Returns t for success, nil if
1539 ;; there is no file line. Upon success, all variables are set, either
1540 ;; to nil or the appropriate value, so they need not be initialized.
1541 ;; Moves point within the current line.
1542 (if (dired-move-to-filename)
1543 (let (pos
1544 (mode-len 10) ; length of mode string
1545 ;; like in dired.el, but with subexpressions \1=inode, \2=s:
1546 (dired-re-inode-size "\\s *\\([0-9]*\\)\\s *\\([0-9]*\\) ?"))
1547 (beginning-of-line)
1548 (forward-char 2)
1549 (if (looking-at dired-re-inode-size)
1550 (progn
1551 (goto-char (match-end 0))
1552 (setq inode (string-to-number (buffer-substring (match-beginning 1)
1553 (match-end 1)))
1554 s (string-to-number (buffer-substring (match-beginning 2)
1555 (match-end 2)))))
1556 (setq inode nil
1557 s nil))
1558 (setq mode (buffer-substring (point) (+ mode-len (point))))
1559 (forward-char mode-len)
1560 (setq nlink (read (current-buffer)))
1561 ;; Karsten Wenger <kw@cis.uni-muenchen.de> fixed uid.
1562 (setq uid (buffer-substring (+ (point) 1)
1563 (progn (forward-word 1) (point))))
1564 (re-search-forward directory-listing-before-filename-regexp)
1565 (goto-char (match-beginning 1))
1566 (forward-char -1)
1567 (setq size (string-to-number (buffer-substring (save-excursion
1568 (backward-word 1)
1569 (setq pos (point)))
1570 (point))))
1571 (goto-char pos)
1572 (backward-word 1)
1573 ;; if no gid is displayed, gid will be set to uid
1574 ;; but user will then not reference it anyway in PREDICATE.
1575 (setq gid (buffer-substring (save-excursion
1576 (forward-word 1) (point))
1577 (point))
1578 time (buffer-substring (match-beginning 1)
1579 (1- (dired-move-to-filename)))
1580 name (buffer-substring (point)
1582 (dired-move-to-end-of-filename t)
1583 (point)))
1584 sym (progn
1585 (if (looking-at " -> ")
1586 (buffer-substring
1587 (progn (forward-char 4) (point))
1588 (progn (end-of-line) (point)))
1589 "")))
1591 nil)
1592 (eval predicate)))
1593 (format "'%s file" predicate))))
1596 ;;; FIND FILE AT POINT.
1598 (defcustom dired-x-hands-off-my-keys t
1599 "Non-nil means don't bind `dired-x-find-file' over `find-file' on keyboard.
1600 Similarly for `dired-x-find-file-other-window' over `find-file-other-window'.
1601 If you change this variable without using \\[customize] after `dired-x.el'
1602 is loaded then call \\[dired-x-bind-find-file]."
1603 :type 'boolean
1604 :initialize 'custom-initialize-default
1605 :set (lambda (sym val)
1606 (set sym val)
1607 (dired-x-bind-find-file))
1608 :group 'dired-x)
1610 ;; Bind `dired-x-find-file{-other-window}' over wherever
1611 ;; `find-file{-other-window}' is bound?
1612 (defun dired-x-bind-find-file ()
1613 "Bind `dired-x-find-file' in place of `find-file' \(or reverse\).
1614 Similarly for `dired-x-find-file-other-window' and `find-file-other-window'.
1615 Binding direction based on `dired-x-hands-off-my-keys'.
1616 This function is part of `after-init-hook'."
1617 (interactive)
1618 (if (called-interactively-p 'interactive)
1619 (setq dired-x-hands-off-my-keys
1620 (not (y-or-n-p "Bind dired-x-find-file over find-file? "))))
1621 (cond ((not dired-x-hands-off-my-keys)
1622 (substitute-key-definition 'find-file
1623 'dired-x-find-file
1624 (current-global-map))
1625 (substitute-key-definition 'find-file-other-window
1626 'dired-x-find-file-other-window
1627 (current-global-map)))
1629 (substitute-key-definition 'dired-x-find-file
1630 'find-file
1631 (current-global-map))
1632 (substitute-key-definition 'dired-x-find-file-other-window
1633 'find-file-other-window
1634 (current-global-map))))
1635 ;; Clear mini-buffer.
1636 (message nil))
1638 ;; Now call it so binding is correct and put on `after-init-hook' in case
1639 ;; user changes binding.
1640 (dired-x-bind-find-file)
1641 (add-hook 'after-init-hook 'dired-x-bind-find-file)
1643 (defun dired-x-find-file (filename)
1644 "Edit file FILENAME.
1645 May create a new window, or reuse an existing one.
1646 See the function `display-buffer'.
1648 Identical to `find-file' except when called interactively, with a prefix arg
1649 \(e.g., \\[universal-argument]\), in which case it guesses filename near point.
1650 Useful for editing file mentioned in buffer you are viewing,
1651 or to test if that file exists. Use minibuffer after snatching filename."
1652 (interactive (list (read-filename-at-point "Find file: ")))
1653 (find-file (expand-file-name filename)))
1655 (defun dired-x-find-file-other-window (filename)
1656 "Edit file FILENAME, in another window.
1657 May create a new window, or reuse an existing one.
1658 See the function `display-buffer'.
1660 Identical to `find-file-other-window' except when called interactively, with
1661 a prefix arg \(e.g., \\[universal-argument]\), in which case it guesses filename near point.
1662 Useful for editing file mentioned in buffer you are viewing,
1663 or to test if that file exists. Use minibuffer after snatching filename."
1664 (interactive (list (read-filename-at-point "Find file: ")))
1665 (find-file-other-window (expand-file-name filename)))
1667 ;;; Internal functions.
1669 ;; Fixme: This should probably use `thing-at-point'. -- fx
1670 (defun dired-filename-at-point ()
1671 "Get the filename closest to point, but do not change position.
1672 Has a preference for looking backward when not directly on a symbol.
1673 Not perfect - point must be in middle of or end of filename."
1675 (let ((filename-chars "-.[:alnum:]_/:$+@")
1676 start end filename prefix)
1678 (save-excursion
1679 ;; First see if just past a filename.
1680 (if (not (eobp))
1681 (if (looking-at "[] \t\n[{}()]") ; whitespace or some parens
1682 (progn
1683 (skip-chars-backward " \n\t\r({[]})")
1684 (if (not (bobp))
1685 (backward-char 1)))))
1687 (if (string-match (concat "[" filename-chars "]")
1688 (char-to-string (following-char)))
1689 (progn
1690 (if (re-search-backward (concat "[^" filename-chars "]") nil t)
1691 (forward-char)
1692 (goto-char (point-min)))
1693 (setq start (point))
1694 (setq prefix
1695 (and (string-match
1696 "^\\w+@"
1697 (buffer-substring start (line-beginning-position)))
1698 "/"))
1699 (goto-char start)
1700 (if (string-match "[/~]" (char-to-string (preceding-char)))
1701 (setq start (1- start)))
1702 (re-search-forward (concat "\\=[" filename-chars "]*") nil t))
1704 (error "No file found around point!"))
1706 ;; Return string.
1707 (expand-file-name (concat prefix (buffer-substring start (point)))))))
1709 (defun read-filename-at-point (prompt)
1710 "Return filename prompting with PROMPT with completion.
1711 If `current-prefix-arg' is non-nil, uses name at point as guess."
1712 (if current-prefix-arg
1713 (let ((guess (dired-filename-at-point)))
1714 (read-file-name prompt
1715 (file-name-directory guess)
1716 guess
1717 nil (file-name-nondirectory guess)))
1718 (read-file-name prompt default-directory)))
1720 ;;; BUG REPORTS
1722 (define-obsolete-function-alias 'dired-x-submit-report 'report-emacs-bug "24.1")
1725 ;; As Barry Warsaw would say: "This might be useful..."
1726 (provide 'dired-x)
1728 ;; Local Variables:
1729 ;; byte-compile-dynamic: t
1730 ;; generated-autoload-file: "dired.el"
1731 ;; End:
1733 ;;; dired-x.el ends here