Get rid of RCS id.
[bpt/emacs.git] / lisp / dired-x.el
CommitLineData
cb3fe1f0
RS
1;;; dired-x.el --- Sebastian Kremer's Extra DIRED hacked up for GNU Emacs19
2
3;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
4;; Lawrence R. Dodd <dodd@roebling.poly.edu>
5;; Maintainer: Lawrence R. Dodd <dodd@roebling.poly.edu>
9b5ef74b
RS
6;; Version: 2.37+
7;; Date: 1994/08/18 19:27:42
cb3fe1f0
RS
8;; Keywords: dired extensions
9
10;; Copyright (C) 1993, 1994 Free Software Foundation
11
12;; This file is part of GNU Emacs.
13
14;; GNU Emacs is free software; you can redistribute it and/or modify
15;; it under the terms of the GNU General Public License as published by
de3b6439 16;; the Free Software Foundation; either version 2, or (at your option)
cb3fe1f0
RS
17;; any later version.
18
19;; GNU Emacs is distributed in the hope that it will be useful,
20;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22;; GNU General Public License for more details.
23
24;; You should have received a copy of the GNU General Public License
b578f267
EN
25;; along with GNU Emacs; see the file COPYING. If not, write to the
26;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
27;; Boston, MA 02111-1307, USA.
cb3fe1f0 28
9572a9dd 29;;; Commentary:
cb3fe1f0 30
b578f267
EN
31;; This is Sebastian Kremer's excellent dired-x.el (Dired Extra), version
32;; 1.191, hacked up for GNU Emacs 19. Redundant or conflicting material
33;; has been removed or renamed in order to work properly with dired of
34;; GNU Emacs 19. All suggestions or comments are most welcomed.
35
36;;
37;; Please, PLEASE, *PLEASE* see the info pages.
38;;
39
40;; BUGS: Type M-x dired-x-submit-report and a report will be generated.
41
42;; INSTALLATION: In your ~/.emacs,
43;;
44;; (add-hook 'dired-load-hook
45;; (function (lambda ()
46;; (load "dired-x")
47;; ;; Set global variables here. For example:
48;; ;; (setq dired-guess-shell-gnutar "gtar")
49;; )))
50;; (add-hook 'dired-mode-hook
51;; (function (lambda ()
52;; ;; Set buffer-local variables here. For example:
53;; ;; (setq dired-omit-files-p t)
54;; )))
55;;
56;; At load time dired-x.el will install itself, redefine some functions, and
57;; bind some dired keys. *Please* see the info pages for more details.
58
59;; CAUTION: If you are using a version of GNU Emacs earlier than 19.20 than
60;; you may have to edit dired.el. The copy of dired.el in GNU Emacs versions
61;; earlier than 19.20 incorrectly had the call to run-hooks *before* the call
62;; to provide. In such a case, it is possible that byte-compiling and/or
63;; loading dired can cause an infinite loop. To prevent this, make sure the
64;; line of code
65;;
66;; (run-hooks 'dired-load-hook)
67;;
68;; is the *last* executable line in the file dired.el. That is, make sure it
69;; comes *after* the line
70;;
71;; (provide 'dired)
72;;
73;; *Please* see the info pages for more details.
74
75;; User defined variables:
76;;
77;; dired-bind-vm
78;; dired-vm-read-only-folders
79;; dired-bind-jump
80;; dired-bind-info
81;; dired-bind-man
82;; dired-x-hands-off-my-keys
83;; dired-find-subdir
84;; dired-enable-local-variables
85;; dired-local-variables-file
86;; dired-guess-shell-gnutar
87;; dired-guess-shell-gzip-quiet
88;; dired-guess-shell-znew-switches
89;; dired-guess-shell-alist-user
90;; dired-clean-up-buffers-too
91;; dired-omit-files-p
92;; dired-omit-files
93;; dired-omit-extensions
94;;
95;; To find out more about these variables, load this file, put your cursor at
96;; the end of any of the variable names, and hit C-h v [RET]. *Please* see
97;; the info pages for more details.
98
99;; When loaded this code redefines the following functions of GNU Emacs
100;;
101;; Function Found in this file of GNU Emacs
102;; -------- -------------------------------
103;; dired-clean-up-after-deletion ../lisp/dired.el
104;; dired-find-buffer-nocreate ../lisp/dired.el
105;; dired-initial-position ../lisp/dired.el
106;;
107;; dired-add-entry ../lisp/dired-aux.el
108;; dired-read-shell-command ../lisp/dired-aux.el
109;;
110;; One drawback is that dired-x.el will load dired-aux.el as soon as dired is
111;; loaded. Thus, the advantage of separating out non-essential dired stuff
112;; into dired-aux.el and only loading when necessary will be lost. Please
113;; note also that some of the comments in dired.el and dired-aux.el are
114;; Kremer's that referred to the old dired-x.el. This now should be referring
115;; to this program. (This is also a good reason to call this dired-x.el
116;; instead of dired-x19.el.)
cb3fe1f0
RS
117
118\f
b578f267 119;;; Code:
cb3fe1f0 120
b578f267 121;; LOAD.
cb3fe1f0 122
b578f267
EN
123;; This is a no-op if dired-x is being loaded via `dired-load-hook'. It is
124;; here in case the user has autoloaded dired-x via the dired-jump key binding
125;; (instead of autoloading to dired as is suggested in the info-pages).
9572a9dd 126
d82cfc0c 127(require 'dired)
9572a9dd 128
b578f267
EN
129;; We will redefine some functions and also need some macros so we need to
130;; load dired stuff of GNU Emacs.
d82cfc0c
RS
131
132(require 'dired-aux)
9572a9dd 133
b578f267 134;;; User-defined variables.
cb3fe1f0
RS
135
136(defvar dired-bind-vm nil
137 "*t says \"V\" in dired-mode will `dired-vm', otherwise \"V\" is `dired-rmail'.
138Also, RMAIL files contain -*- rmail -*- at the top so \"f\",
139`dired-advertised-find-file', will run rmail.")
140
141(defvar dired-bind-jump t
142 "*t says bind `dired-jump' to C-x C-j, otherwise do not.")
143
144(defvar dired-bind-man t
145 "*t says bind `dired-man' to \"N\" in dired-mode, otherwise do not.")
146
147(defvar dired-bind-info t
148 "*t says bind `dired-info' to \"I\" in dired-mode, otherwise do not.")
149
150(defvar dired-vm-read-only-folders nil
151 "*If t, \\[dired-vm] will visit all folders read-only.
152If neither nil nor t, e.g. the symbol `if-file-read-only', only
153files not writable by you are visited read-only.
154
155Read-only folders only work in VM 5, not in VM 4.")
156
157(defvar dired-omit-files-p nil
158 "*If non-nil, \"uninteresting\" files are not listed (buffer-local).
159Use \\[dired-omit-toggle] to toggle its value.
160Uninteresting files are those whose filenames match regexp `dired-omit-files',
161plus those ending with extensions in `dired-omit-extensions'.")
e2439696 162(make-variable-buffer-local 'dired-omit-files-p)
cb3fe1f0 163
685ff9f8 164(defvar dired-omit-files "^#\\|^\\.$\\|^\\.\\.$"
e2439696 165 "*Filenames matching this regexp will not be displayed.
685ff9f8
RS
166This only has effect when `dired-omit-files-p' is t. See interactive function
167`dired-omit-toggle' \(\\[dired-omit-toggle]\) and variable
168`dired-omit-extensions'. The default is to omit `.', `..', and auto-save
169files.")
cb3fe1f0
RS
170
171(defvar dired-find-subdir nil ; t is pretty near to DWIM...
0a44133e
RS
172 "*If non-nil, Dired always finds a directory in a buffer of its own.
173If nil, Dired finds the directory as a subdirectory in some other buffer
174if it is present as one.
cb3fe1f0
RS
175
176If there are several Dired buffers for a directory, the most recently
177used is chosen.
178
179Dired avoids switching to the current buffer, so that if you have
180a normal and a wildcard buffer for the same directory, C-x d RET will
181toggle between those two.")
182
183(defvar dired-enable-local-variables t
184 "*Control use of local-variables lists in dired.
185The value can be t, nil or something else.
186A value of t means local-variables lists are obeyed;
187nil means they are ignored; anything else means query.
188
189This temporarily overrides the value of `enable-local-variables' when listing
190a directory. See also `dired-local-variables-file'.")
191
192(defvar dired-guess-shell-gnutar nil
193 "*If non-nil, name of GNU tar executable (e.g., \"tar\" or \"gtar\") and `z'
194switch will be used for compressed or gzip'ed tar files. If no GNU tar, set
195to nil: a pipe using `zcat' or `gunzip -c' will be used.")
196
197(defvar dired-guess-shell-gzip-quiet t
198 "*non-nil says pass -q to gzip overriding verbose GZIP environment.")
199
200(defvar dired-guess-shell-znew-switches nil
201 "*If non-nil, then string of switches passed to `znew', example: \"-K\"")
202
203(defvar dired-clean-up-buffers-too t
204 "*t says offer to kill buffers visiting files and dirs deleted in dired.")
205
b578f267 206;;; KEY BINDINGS.
cb3fe1f0
RS
207
208(define-key dired-mode-map "\M-o" 'dired-omit-toggle)
209(define-key dired-mode-map "\M-(" 'dired-mark-sexp)
582bd36a
RS
210(define-key dired-mode-map "*(" 'dired-mark-sexp)
211(define-key dired-mode-map "*." 'dired-mark-extension)
cb3fe1f0
RS
212(define-key dired-mode-map "\M-!" 'dired-smart-shell-command)
213(define-key dired-mode-map "T" 'dired-do-toggle)
582bd36a 214(define-key dired-mode-map "*t" 'dired-do-toggle)
cb3fe1f0
RS
215(define-key dired-mode-map "w" 'dired-copy-filename-as-kill)
216(define-key dired-mode-map "\M-g" 'dired-goto-file)
217(define-key dired-mode-map "\M-G" 'dired-goto-subdir)
218(define-key dired-mode-map "F" 'dired-do-find-marked-files)
219(define-key dired-mode-map "Y" 'dired-do-relsymlink)
220(define-key dired-mode-map "%Y" 'dired-do-relsymlink-regexp)
221(define-key dired-mode-map "V" 'dired-do-run-mail)
222
223(if dired-bind-man
224 (define-key dired-mode-map "N" 'dired-man))
225
226(if dired-bind-info
227 (define-key dired-mode-map "I" 'dired-info))
228
9572a9dd 229;;; GLOBAL BINDING.
cb3fe1f0
RS
230(if dired-bind-jump
231 (progn
232 (define-key global-map "\C-x\C-j" 'dired-jump)
233 (define-key global-map "\C-x4\C-j" 'dired-jump-other-window)))
234
235\f
b578f267 236;;; Install into appropriate hooks.
cb3fe1f0
RS
237
238(add-hook 'dired-mode-hook 'dired-extra-startup)
239(add-hook 'dired-after-readin-hook 'dired-omit-expunge)
240
241(defun dired-extra-startup ()
242 "Automatically put on dired-mode-hook to get extra dired features:
243\\<dired-mode-map>
244
245 \\[dired-do-run-mail]\t-- run mail on folder (see `dired-bind-vm')
246 \\[dired-info]\t-- run info on file
247 \\[dired-man]\t-- run man on file
248 \\[dired-do-find-marked-files]\t-- visit all marked files simultaneously
249 \\[dired-omit-toggle]\t-- toggle omitting of files
250 \\[dired-do-toggle]\t-- toggle marks
251 \\[dired-mark-sexp]\t-- mark by lisp expression
252 \\[dired-copy-filename-as-kill]\t-- copy the file or subdir names into the kill ring.
253 \t You can feed it to other commands using \\[yank].
254
255For more features, see variables
256
257 dired-bind-vm
258 dired-bind-jump
259 dired-bind-info
260 dired-bind-man
261 dired-vm-read-only-folders
262 dired-omit-files-p
263 dired-omit-files
264 dired-omit-extensions
265 dired-find-subdir
266 dired-enable-local-variables
267 dired-local-variables-file
268 dired-guess-shell-gnutar
269 dired-guess-shell-gzip-quiet
270 dired-guess-shell-znew-switches
271 dired-guess-shell-alist-user
9572a9dd 272 dired-clean-up-buffers-too
cb3fe1f0
RS
273
274See also functions
275
276 dired-flag-extension
277 dired-virtual
278 dired-jump
279 dired-man
280 dired-vm
281 dired-rmail
282 dired-info
283 dired-do-find-marked-files
284"
285 (interactive)
286
287 ;; These must be done in each new dired buffer.
288 (dired-hack-local-variables)
289 (dired-omit-startup))
290
291\f
b578f267 292;;; BUFFER CLEANING.
cb3fe1f0 293
b578f267 294;; REDEFINE.
cb3fe1f0
RS
295(defun dired-clean-up-after-deletion (fn)
296
297 ;; Clean up after a deleted file or directory FN.
298 ;; Remove expanded subdir of deleted dir, if any.
299 (save-excursion (and (cdr dired-subdir-alist)
300 (dired-goto-subdir fn)
301 (dired-kill-subdir)))
302
303 ;; Offer to kill buffer of deleted file FN.
304 (if dired-clean-up-buffers-too
305 (progn
306 (let ((buf (get-file-buffer fn)))
307 (and buf
308 (funcall (function y-or-n-p)
309 (format "Kill buffer of %s, too? "
310 (file-name-nondirectory fn)))
311 (save-excursion ; you never know where kill-buffer leaves you
312 (kill-buffer buf))))
12c38283 313 (let ((buf-list (dired-buffers-for-dir (expand-file-name fn)))
cb3fe1f0
RS
314 (buf nil))
315 (and buf-list
316 (y-or-n-p (format "Kill dired buffer%s of %s, too? "
317 (dired-plural-s (length buf-list))
318 (file-name-nondirectory fn)))
319 (while buf-list
320 (save-excursion (kill-buffer (car buf-list)))
321 (setq buf-list (cdr buf-list)))))))
9572a9dd 322 ;; Anything else?
cb3fe1f0
RS
323 )
324
325\f
b578f267 326;;; EXTENSION MARKING FUNCTIONS.
cb3fe1f0 327
9572a9dd 328;;; Mark files with some extension.
cb3fe1f0
RS
329(defun dired-mark-extension (extension &optional marker-char)
330 "Mark all files with a certain extension for use in later commands.
331A `.' is not automatically prepended to the string entered."
332 ;; EXTENSION may also be a list of extensions instead of a single one.
333 ;; Optional MARKER-CHAR is marker to use.
334 (interactive "sMarking extension: \nP")
335 (or (listp extension)
336 (setq extension (list extension)))
337 (dired-mark-files-regexp
338 (concat ".";; don't match names with nothing but an extension
339 "\\("
340 (mapconcat 'regexp-quote extension "\\|")
341 "\\)$")
342 marker-char))
343
344(defun dired-flag-extension (extension)
345 "In dired, flag all files with a certain extension for deletion.
346A `.' is *not* automatically prepended to the string entered."
347 (interactive "sFlagging extension: ")
348 (dired-mark-extension extension dired-del-marker))
349
350;;; Define some unpopular file extensions. Used for cleaning and omitting.
351
352(defvar dired-patch-unclean-extensions
353 '(".rej" ".orig")
354 "List of extensions of dispensable files created by the `patch' program.")
355
356(defvar dired-tex-unclean-extensions
357 '(".toc" ".log" ".aux");; these are already in completion-ignored-extensions
358 "List of extensions of dispensable files created by TeX.")
359
360(defvar dired-latex-unclean-extensions
361 '(".idx" ".lof" ".lot" ".glo")
362 "List of extensions of dispensable files created by LaTeX.")
363
364(defvar dired-bibtex-unclean-extensions
365 '(".blg" ".bbl")
366 "List of extensions of dispensable files created by BibTeX.")
367
368(defvar dired-texinfo-unclean-extensions
369 '(".cp" ".cps" ".fn" ".fns" ".ky" ".kys" ".pg" ".pgs"
370 ".tp" ".tps" ".vr" ".vrs")
371 "List of extensions of dispensable files created by texinfo.")
372
373(defun dired-clean-patch ()
374 "Flag dispensable files created by patch for deletion.
375See variable `dired-patch-unclean-extensions'."
376 (interactive)
377 (dired-flag-extension dired-patch-unclean-extensions))
378
379(defun dired-clean-tex ()
9572a9dd
KH
380 "Flag dispensable files created by [La]TeX etc. for deletion.
381See variables `dired-texinfo-unclean-extensions',
382`dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
383`dired-texinfo-unclean-extensions'."
cb3fe1f0
RS
384 (interactive)
385 (dired-flag-extension (append dired-texinfo-unclean-extensions
386 dired-latex-unclean-extensions
387 dired-bibtex-unclean-extensions
388 dired-tex-unclean-extensions)))
389
9572a9dd
KH
390(defun dired-very-clean-tex ()
391 "Flag dispensable files created by [La]TeX *and* \".dvi\" for deletion.
392See variables `dired-texinfo-unclean-extensions',
393`dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions' and
394`dired-texinfo-unclean-extensions'."
cb3fe1f0 395 (interactive)
9572a9dd
KH
396 (dired-flag-extension (append dired-texinfo-unclean-extensions
397 dired-latex-unclean-extensions
398 dired-bibtex-unclean-extensions
399 dired-tex-unclean-extensions
400 (list ".dvi"))))
cb3fe1f0 401\f
b578f267 402;;; JUMP.
cb3fe1f0 403
40c8b203 404;;;###autoload
cb3fe1f0
RS
405(defun dired-jump (&optional other-window)
406 "Jump to dired buffer corresponding to current buffer.
407If in a file, dired the current directory and move to file's line.
408If in dired already, pop up a level and goto old directory's line.
409In case the proper dired file line cannot be found, refresh the dired
410buffer and try again."
411 (interactive "P")
412 (let* ((file buffer-file-name)
413 (dir (if file (file-name-directory file) default-directory)))
414 (if (eq major-mode 'dired-mode)
415 (progn
416 (setq dir (dired-current-directory))
417 (dired-up-directory other-window)
418 (or (dired-goto-file dir)
419 ;; refresh and try again
9572a9dd 420 (progn
cb3fe1f0
RS
421 (dired-insert-subdir (file-name-directory dir))
422 (dired-goto-file dir))))
423 (if other-window
424 (dired-other-window dir)
425 (dired dir))
426 (if file
427 (or (dired-goto-file file)
428 ;; refresh and try again
9572a9dd 429 (progn
cb3fe1f0 430 (dired-insert-subdir (file-name-directory file))
7e90bcf5
RS
431 (dired-goto-file file))
432 ;; Toggle omitting, if it is on, and try again.
433 (if dired-omit-files-p
434 (progn
435 (dired-omit-toggle)
436 (dired-goto-file file))))))))
cb3fe1f0
RS
437
438(defun dired-jump-other-window ()
439 "Like \\[dired-jump] (dired-jump) but in other window."
440 (interactive)
441 (dired-jump t))
cb3fe1f0 442\f
b578f267 443;;; TOGGLE.
cb3fe1f0
RS
444;;; Toggle marked files with unmarked files.
445
446(defun dired-do-toggle ()
447 "Toggle marks.
448That is, currently marked files become unmarked and vice versa.
449Files marked with other flags (such as `D') are not affected.
450`.' and `..' are never toggled.
451As always, hidden subdirs are not affected."
452 (interactive)
453 (save-excursion
454 (goto-char (point-min))
455 (let (buffer-read-only)
456 (while (not (eobp))
457 (or (dired-between-files)
458 (looking-at dired-re-dot)
459 ;; use subst instead of insdel because it does not move
460 ;; the gap and thus should be faster and because
461 ;; other characters are left alone automatically
462 (apply 'subst-char-in-region
463 (point) (1+ (point))
464 (if (eq ?\040 (following-char)) ; SPC
465 (list ?\040 dired-marker-char)
466 (list dired-marker-char ?\040))))
467 (forward-line 1)))))
468
469\f
b578f267 470;;; COPY NAMES OF MARKED FILES INTO KILL-RING.
9572a9dd 471
cb3fe1f0
RS
472(defun dired-copy-filename-as-kill (&optional arg)
473 "Copy names of marked (or next ARG) files into the kill ring.
474The names are separated by a space.
475With a zero prefix arg, use the complete pathname of each marked file.
476With \\[universal-argument], use the relative pathname of each marked file.
477
478If on a subdir headerline, use subdirname instead; prefix arg is ignored
479in this case.
480
481You can then feed the file name(s) to other commands with \\[yank]."
482 (interactive "P")
483 (let ((string
484 (or (dired-get-subdir)
485 (mapconcat (function identity)
486 (if arg
487 (cond ((zerop (prefix-numeric-value arg))
488 (dired-get-marked-files))
489 ((integerp arg)
490 (dired-get-marked-files 'no-dir arg))
491 (t ; else a raw arg
492 (dired-get-marked-files t)))
493 (dired-get-marked-files 'no-dir))
494 " "))))
495 (kill-new string)
496 (message "%s" string)))
497
498\f
b578f267 499;;; OMITTING.
cb3fe1f0
RS
500
501;;; Enhanced omitting of lines from directory listings.
502;;; Marked files are never omitted.
503
504;; should probably get rid of this and always use 'no-dir.
505;; sk 28-Aug-1991 09:37
506(defvar dired-omit-localp 'no-dir
507 "The LOCALP argument dired-omit-expunge passes to dired-get-filename.
508If it is 'no-dir, omitting is much faster, but you can only match
509against the basename of the file. Set it to nil if you need to match the
510whole pathname.")
511
512;; \017=^O for Omit - other packages can chose other control characters.
513(defvar dired-omit-marker-char ?\017
514 "Temporary marker used by by dired-omit.
515Should never be used as marker by the user or other packages.")
516
517(defun dired-omit-startup ()
cb3fe1f0
RS
518 (or (assq 'dired-omit-files-p minor-mode-alist)
519 (setq minor-mode-alist
520 (append '((dired-omit-files-p " Omit")) minor-mode-alist))))
521
522(defun dired-omit-toggle (&optional flag)
685ff9f8 523 "Toggle omitting files matching `dired-omit-files' and `dired-omit-extensions'.
cb3fe1f0
RS
524With an arg, and if omitting was off, don't toggle and just mark the
525 files but don't actually omit them.
526With an arg, and if omitting was on, turn it off but don't refresh the buffer."
527 (interactive "P")
528 (if flag
529 (if dired-omit-files-p
530 (setq dired-omit-files-p (not dired-omit-files-p))
531 (dired-mark-unmarked-files (dired-omit-regexp) nil nil
532 dired-omit-localp))
533 ;; no FLAG
534 (setq dired-omit-files-p (not dired-omit-files-p))
535 (if (not dired-omit-files-p)
536 (revert-buffer)
537 ;; this will mention how many were omitted:
538 (dired-omit-expunge))))
539
540(defvar dired-omit-extensions
541 (append completion-ignored-extensions
542 dired-latex-unclean-extensions
543 dired-bibtex-unclean-extensions
544 dired-texinfo-unclean-extensions)
685ff9f8
RS
545 "If non-nil, a list of extensions \(strings\) to omit from Dired listings.
546Defaults to elements of `completion-ignored-extensions',
547`dired-latex-unclean-extensions', `dired-bibtex-unclean-extensions', and
548`dired-texinfo-unclean-extensions'.
549
550See interactive function `dired-omit-toggle' \(\\[dired-omit-toggle]\) and
551variables `dired-omit-files-p' and `dired-omit-files'.")
cb3fe1f0
RS
552
553(defun dired-omit-expunge (&optional regexp)
554 "Erases all unmarked files matching REGEXP.
555Does nothing if global variable `dired-omit-files-p' is nil.
556If REGEXP is nil or not specified, uses `dired-omit-files', and also omits
557 filenames ending in `dired-omit-extensions'.
558If REGEXP is the empty string, this function is a no-op.
559
560This functions works by temporarily binding `dired-marker-char' to
561`dired-omit-marker-char' and calling `dired-do-kill-lines'."
562 (interactive "sOmit files (regexp): ")
563 (if dired-omit-files-p
564 (let ((omit-re (or regexp (dired-omit-regexp)))
9b5ef74b 565 (old-modified-p (buffer-modified-p))
cb3fe1f0
RS
566 count)
567 (or (string= omit-re "")
568 (let ((dired-marker-char dired-omit-marker-char))
569 (message "Omitting...")
570 (if (dired-mark-unmarked-files omit-re nil nil dired-omit-localp)
571 (progn
572 (setq count (dired-do-kill-lines nil "Omitted %d line%s."))
048ea441 573 (force-mode-line-update))
cb3fe1f0 574 (message "(Nothing to omit)"))))
9b5ef74b
RS
575 ;; Try to preserve modified state of buffer. So `%*' doesn't appear
576 ;; in mode-line of omitted buffers.
577 (set-buffer-modified-p (and old-modified-p
578 (save-excursion
579 (goto-char (point-min))
580 (re-search-forward dired-re-mark nil t))))
cb3fe1f0
RS
581 count)))
582
583(defun dired-omit-regexp ()
584 (concat (if dired-omit-files (concat "\\(" dired-omit-files "\\)") "")
585 (if (and dired-omit-files dired-omit-extensions) "\\|" "")
586 (if dired-omit-extensions
587 (concat ".";; a non-extension part should exist
588 "\\("
589 (mapconcat 'regexp-quote dired-omit-extensions "\\|")
590 "\\)$")
591 "")))
592
593;; Returns t if any work was done, nil otherwise.
594(defun dired-mark-unmarked-files (regexp msg &optional unflag-p localp)
595 "Marks unmarked files matching REGEXP, displaying MSG.
596REGEXP is matched against the complete pathname.
597Does not re-mark files which already have a mark.
598With prefix argument, unflag all those files.
599Second optional argument LOCALP is as in `dired-get-filename'."
600 (interactive "P")
601 (let ((dired-marker-char (if unflag-p ?\ dired-marker-char)))
602 (dired-mark-if
603 (and
604 ;; not already marked
605 (looking-at " ")
606 ;; uninteresting
607 (let ((fn (dired-get-filename localp t)))
608 (and fn (string-match regexp fn))))
609 msg)))
610
9572a9dd 611;;; REDEFINE.
cb3fe1f0
RS
612(defun dired-omit-new-add-entry (filename &optional marker-char)
613 ;; This redefines dired-aux.el's dired-add-entry to avoid calling ls for
614 ;; files that are going to be omitted anyway.
615 (if dired-omit-files-p
616 ;; perhaps return t without calling ls
617 (let ((omit-re (dired-omit-regexp)))
618 (if (or (string= omit-re "")
619 (not
620 (string-match omit-re
621 (cond
622 ((eq 'no-dir dired-omit-localp)
623 filename)
624 ((eq t dired-omit-localp)
625 (dired-make-relative filename))
626 (t
627 (dired-make-absolute
628 filename
629 (file-name-directory filename)))))))
630 ;; if it didn't match, go ahead and add the entry
631 (dired-omit-old-add-entry filename marker-char)
632 ;; dired-add-entry returns t for success, perhaps we should
633 ;; return file-exists-p
634 t))
635 ;; omitting is not turned on at all
636 (dired-omit-old-add-entry filename marker-char)))
637
9572a9dd 638;;; REDEFINE.
cb3fe1f0
RS
639;;; Redefine dired-aux.el's version of `dired-add-entry'
640;;; Save old defun if not already done:
641(or (fboundp 'dired-omit-old-add-entry)
642 (fset 'dired-omit-old-add-entry (symbol-function 'dired-add-entry)))
643;; Redefine it.
644(fset 'dired-add-entry 'dired-omit-new-add-entry)
645
646\f
b578f267 647;;; VIRTUAL DIRED MODE.
cb3fe1f0
RS
648
649;;; For browsing `ls -lR' listings in a dired-like fashion.
650
651(fset 'virtual-dired 'dired-virtual)
652(defun dired-virtual (dirname &optional switches)
653 "Put this buffer into Virtual Dired mode.
654
655In Virtual Dired mode, all commands that do not actually consult the
656filesystem will work.
657
658This is useful if you want to peruse and move around in an ls -lR
659output file, for example one you got from an ftp server. With
660ange-ftp, you can even dired a directory containing an ls-lR file,
661visit that file and turn on virtual dired mode. But don't try to save
662this file, as dired-virtual indents the listing and thus changes the
663buffer.
664
665If you have save a Dired buffer in a file you can use \\[dired-virtual] to
666resume it in a later session.
667
668Type \\<dired-mode-map>\\[revert-buffer] in the
669Virtual Dired buffer and answer `y' to convert the virtual to a real
670dired buffer again. You don't have to do this, though: you can relist
671single subdirs using \\[dired-do-redisplay].
672"
673
674 ;; DIRNAME is the top level directory of the buffer. It will become
675 ;; its `default-directory'. If nil, the old value of
676 ;; default-directory is used.
677
678 ;; Optional SWITCHES are the ls switches to use.
679
680 ;; Shell wildcards will be used if there already is a `wildcard'
681 ;; line in the buffer (thus it is a saved Dired buffer), but there
682 ;; is no other way to get wildcards. Insert a `wildcard' line by
683 ;; hand if you want them.
684
685 (interactive
686 (list (read-string "Virtual Dired directory: " (dired-virtual-guess-dir))))
687 (goto-char (point-min))
688 (or (looking-at " ")
689 ;; if not already indented, do it now:
690 (indent-region (point-min) (point-max) 2))
691 (or dirname (setq dirname default-directory))
692 (setq dirname (expand-file-name (file-name-as-directory dirname)))
693 (setq default-directory dirname) ; contains no wildcards
694 (let ((wildcard (save-excursion
695 (goto-char (point-min))
696 (forward-line 1)
697 (and (looking-at "^ wildcard ")
698 (buffer-substring (match-end 0)
699 (progn (end-of-line) (point)))))))
700 (if wildcard
701 (setq dirname (expand-file-name wildcard default-directory))))
702 ;; If raw ls listing (not a saved old dired buffer), give it a
703 ;; decent subdir headerline:
704 (goto-char (point-min))
705 (or (looking-at dired-subdir-regexp)
706 (dired-insert-headerline default-directory))
707 (dired-mode dirname (or switches dired-listing-switches))
708 (setq mode-name "Virtual Dired"
709 revert-buffer-function 'dired-virtual-revert)
710 (set (make-local-variable 'dired-subdir-alist) nil)
711 (dired-build-subdir-alist)
712 (goto-char (point-min))
713 (dired-initial-position dirname))
714
715(defun dired-virtual-guess-dir ()
716
717 ;; Guess and return appropriate working directory of this buffer,
718 ;; assumed to be in Dired or ls -lR format.
719 ;; The guess is based upon buffer contents.
720 ;; If nothing could be guessed, returns nil.
721
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))))
742
743
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)))
751
752;; A zero-arg version of dired-virtual.
753;; You need my modified version of set-auto-mode for the
754;; `buffer-contents-mode-alist'.
755;; Or you use infer-mode.el and infer-mode-alist, same syntax.
756(defun dired-virtual-mode ()
757 "Put current buffer into virtual dired mode (see `dired-virtual').
758Useful on `buffer-contents-mode-alist' (which see) with the regexp
759
760 \"^ \\(/[^ /]+\\)/?+:$\"
761
762to put saved dired buffers automatically into virtual dired mode.
763
764Also useful for `auto-mode-alist' (which see) like this:
765
e7070c01 766 \(setq auto-mode-alist (cons '(\"[^/]\\.dired\\'\" . dired-virtual-mode)
cb3fe1f0
RS
767 auto-mode-alist)\)"
768 (interactive)
769 (dired-virtual (dired-virtual-guess-dir)))
770
771\f
b578f267 772;;; SMART SHELL.
cb3fe1f0
RS
773
774;;; An Emacs buffer can have but one working directory, stored in the
775;;; buffer-local variable `default-directory'. A Dired buffer may have
776;;; several subdirectories inserted, but still has but one working directory:
777;;; that of the top level Dired directory in that buffer. For some commands
778;;; it is appropriate that they use the current Dired directory instead of
779;;; `default-directory', e.g., `find-file' and `compile'. This is a general
780;;; mechanism is provided for special handling of the working directory in
781;;; special major modes.
782
783;; It's easier to add to this alist than redefine function
784;; default-directory while keeping the old information.
785(defconst default-directory-alist
786 '((dired-mode . (if (fboundp 'dired-current-directory)
787 (dired-current-directory)
788 default-directory)))
789 "Alist of major modes and their opinion on default-directory, as a
790lisp expression to evaluate. A resulting value of nil is ignored in
791favor of default-directory.")
792
793(defun default-directory ()
794 "Usage like variable `default-directory', but knows about the special
795cases in variable `default-directory-alist' (which see)."
796 (or (eval (cdr (assq major-mode default-directory-alist)))
797 default-directory))
798
799(defun dired-smart-shell-command (cmd &optional insert)
800 "Like function `shell-command', but in the current Tree Dired directory."
801 (interactive "sShell command: \nP")
802 (let ((default-directory (default-directory)))
803 (shell-command cmd insert)))
804
805\f
b578f267 806;;; LOCAL VARIABLES FOR DIRED BUFFERS.
cb3fe1f0 807
9572a9dd
KH
808;;; Brief Description:
809;;;
810;;; * `dired-extra-startup' is part of the `dired-mode-hook'.
811;;;
cb3fe1f0 812;;; * `dired-extra-startup' calls `dired-hack-local-variables'
9572a9dd 813;;;
cb3fe1f0 814;;; * `dired-hack-local-variables' checks the value of
9572a9dd
KH
815;;; `dired-local-variables-file'
816;;;
cb3fe1f0
RS
817;;; * Check if `dired-local-variables-file' is a non-nil string and is a
818;;; filename found in the directory of the Dired Buffer being created.
9572a9dd 819;;;
cb3fe1f0
RS
820;;; * If `dired-local-variables-file' satisfies the above, then temporarily
821;;; include it in the Dired Buffer at the bottom.
9572a9dd 822;;;
cb3fe1f0
RS
823;;; * Set `enable-local-variables' temporarily to the user variable
824;;; `dired-enable-local-variables' and run `hack-local-variables' on the
825;;; Dired Buffer.
826
db8f8812 827(defvar dired-local-variables-file (convert-standard-filename ".dired")
cb3fe1f0
RS
828 "Filename, as string, containing local dired buffer variables to be hacked.
829If this file found in current directory, then it will be inserted into dired
830buffer and `hack-local-variables' will be run. See Emacs Info pages for more
831information on local variables. See also `dired-enable-local-variables'.")
832
833(defun dired-hack-local-variables ()
834 "Evaluate local variables in `dired-local-variables-file' for dired buffer."
835 (if (and dired-local-variables-file
836 (stringp dired-local-variables-file)
837 (file-exists-p dired-local-variables-file))
838 (let ((opoint (point-max))
839 buffer-read-only
840 ;; In case user has `enable-local-variables' set to nil we
841 ;; override it locally with dired's variable.
842 (enable-local-variables dired-enable-local-variables))
843 ;; Insert 'em.
844 (save-excursion
845 (goto-char opoint)
846 (insert "\^L\n")
847 (insert-file-contents dired-local-variables-file))
848 ;; Hack 'em.
849 (let ((buffer-file-name dired-local-variables-file))
850 (hack-local-variables))
851 ;; Make sure that the modeline shows the proper information.
852 (dired-sort-set-modeline)
853 ;; Delete this stuff: `eobp' is used to find last subdir by dired.el.
9572a9dd 854 (delete-region opoint (point-max)))))
cb3fe1f0 855
9572a9dd 856(defun dired-omit-here-always ()
cb3fe1f0
RS
857 "Creates `dired-local-variables-file' for omitting and reverts directory.
858Sets dired-omit-file-p to t in a local variables file that is readable by
859dired."
9572a9dd 860 (interactive)
cb3fe1f0
RS
861 (if (file-exists-p dired-local-variables-file)
862 (message "File `./%s' already exists." dired-local-variables-file)
863
864 ;; Create `dired-local-variables-file'.
865 (save-excursion
866 (set-buffer (get-buffer-create " *dot-dired*"))
867 (erase-buffer)
868 (insert "Local Variables:\ndired-omit-files-p: t\nEnd:\n")
869 (write-file dired-local-variables-file)
870 (kill-buffer (current-buffer)))
871
872 ;; Run extra-hooks and revert directory.
873 (dired-extra-startup)
874 (dired-revert)))
875
876\f
b578f267 877;;; GUESS SHELL COMMAND.
cb3fe1f0 878
9572a9dd
KH
879;;; Brief Description:
880;;;
881;;; `dired-do-shell-command' is bound to `!' by dired.el.
882;;;
cb3fe1f0
RS
883;;; * Redefine `dired-do-shell-command' so it calls
884;;; `dired-guess-shell-command'.
9572a9dd 885;;;
cb3fe1f0
RS
886;;; * `dired-guess-shell-command' calls `dired-guess-default' with list of
887;;; marked files.
9572a9dd 888;;;
cb3fe1f0
RS
889;;; * Parse `dired-guess-shell-alist-user' and
890;;; `dired-guess-shell-alist-default' (in that order) for the first REGEXP
891;;; that matches the first file in the file list.
9572a9dd 892;;;
cb3fe1f0 893;;; * If the REGEXP matches all the entries of the file list then evaluate
c0d79871 894;;; COMMAND, which is either a string or a Lisp expression returning a
cb3fe1f0 895;;; string. COMMAND may be a list of commands.
9572a9dd 896;;;
cb3fe1f0 897;;; * Return this command to `dired-guess-shell-command' which prompts user
34ff2275 898;;; with it. The list of commands is temporarily put into the history list.
cb3fe1f0
RS
899;;; If a command is used successfully then it is stored permanently in
900;;; `dired-shell-command-history'.
901
902;;; Guess what shell command to apply to a file.
903(defvar dired-shell-command-history nil
904 "History list for commands that read dired-shell commands.")
905
9572a9dd 906;;; Default list of shell commands.
cb3fe1f0
RS
907
908;;; NOTE: Use `gunzip -c' instead of `zcat' on `.gz' files. Some do not
909;;; install GNU zip's version of zcat.
910
911(defvar dired-guess-shell-alist-default
912 (list
913 (list "\\.tar$" '(if dired-guess-shell-gnutar
914 (concat dired-guess-shell-gnutar " xvf")
915 "tar xvf"))
916
917 ;; REGEXPS for compressed archives must come before the .Z rule to
918 ;; be recognized:
919 (list "\\.tar\\.Z$"
920 ;; Untar it.
921 '(if dired-guess-shell-gnutar
922 (concat dired-guess-shell-gnutar " zxvf")
923 (concat "zcat * | tar xvf -"))
924 ;; Optional conversion to gzip format.
925 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
926 " " dired-guess-shell-znew-switches))
927
928 ;; gzip'ed archives
929 (list "\\.tar\\.g?z$"
930 '(if dired-guess-shell-gnutar
931 (concat dired-guess-shell-gnutar " zxvf")
932 (concat "gunzip -qc * | tar xvf -"))
933 ;; Optional decompression.
934 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q" "")))
935
936 '("\\.shar.Z$" "zcat * | unshar")
937 '("\\.shar.g?z$" "gunzip -qc * | unshar")
938
939 '("\\.ps$" "ghostview" "xv" "lpr")
940 (list "\\.ps.g?z$" "gunzip -qc * | ghostview -"
941 ;; Optional decompression.
942 '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
9572a9dd 943 (list "\\.ps.Z$" "zcat * | ghostview -"
cb3fe1f0
RS
944 ;; Optional conversion to gzip format.
945 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
946 " " dired-guess-shell-znew-switches))
947 '("\\.patch$" "cat * | patch")
948 '("\\.patch.g?z$" "gunzip -qc * | patch")
949 (list "\\.patch.Z$" "zcat * | patch"
950 ;; Optional conversion to gzip format.
951 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
952 " " dired-guess-shell-znew-switches))
953
954 '("\\.dvi$" "xdvi" "dvips") ; preview and printing
9572a9dd 955 '("\\.au$" "play") ; play Sun audiofiles
cb3fe1f0
RS
956 '("\\.mpg$" "mpeg_play")
957 '("\\.uu$" "uudecode") ; for uudecoded files
958 '("\\.hqx$" "mcvert")
959 '("\\.sh$" "sh") ; execute shell scripts
960 '("\\.xbm$" "bitmap") ; view X11 bitmaps
961 '("\\.gp$" "gnuplot")
962 '("\\.p[bgpn]m$" "xv")
963 '("\\.gif$" "xv") ; view gif pictures
964 '("\\.tif$" "xv")
965 '("\\.jpg$" "xv")
966 '("\\.fig$" "xfig") ; edit fig pictures
967 '("\\.out$" "xgraph") ; for plotting purposes.
968 '("\\.tex$" "latex" "tex")
969 '("\\.texi\\(nfo\\)?$" "makeinfo" "texi2dvi")
970
971 ;; Some other popular archivers.
972 '("\\.zoo$" "zoo x//")
973 '("\\.zip$" "unzip")
974 '("\\.lzh$" "lharc x")
975 '("\\.arc$" "arc x")
976 '("\\.shar$" "unshar")
977
978 ;; Compression.
979 (list "\\.g?z$" '(concat "gunzip" (if dired-guess-shell-gzip-quiet " -q")))
980 (list "\\.Z$" "uncompress"
981 ;; Optional conversion to gzip format.
982 '(concat "znew" (if dired-guess-shell-gzip-quiet " -q")
983 " " dired-guess-shell-znew-switches))
984 )
9572a9dd 985
cb3fe1f0
RS
986 "Default alist used for shell command guessing.
987See `dired-guess-shell-alist-user'")
988
989(defvar dired-guess-shell-alist-user nil
990 "User-defined alist of rules for suggested commands. These rules take
991precedence over the predefined rules in the variable
992`dired-guess-shell-alist-default' (to which they are prepended).
993
994Each element of this list looks like
995
996 \(REGEXP COMMAND...\)
997
998where each COMMAND can either be a string or a lisp expression that evaluates
999to a string. If several COMMANDs are given, the first one will be the default
1000and the rest will be added temporarily to the history and can be retrieved
1001with \\[previous-history-element] (M-p) .
1002
1003You can set this variable in your ~/.emacs. For example, to add rules for
1004`.foo' and `.bar' files, write
1005
1006 \(setq dired-guess-shell-alist-user
1007 (list (list \"\\\\.foo$\" \"FOO-COMMAND\");; fixed rule
1008 ;; possibly more rules ...
1009 (list \"\\\\.bar$\";; rule with condition test
1010 '(if condition
1011 \"BAR-COMMAND-1\"
1012 \"BAR-COMMAND-2\")))\)
1013")
1014
1015(defun dired-guess-default (files)
1016
9572a9dd
KH
1017 ;; Guess a shell commands for FILES. Return command or list of commands.
1018 ;; See `dired-guess-shell-alist-user'.
cb3fe1f0
RS
1019
1020 (let* ((case-fold-search nil) ; case-sensitive matching
1021 ;; Prepend the user's alist to the default alist.
1022 (alist (append dired-guess-shell-alist-user
1023 dired-guess-shell-alist-default))
1024 (file (car files))
1025 (flist (cdr files))
1026 elt regexp cmds)
1027
1028 ;; Find the first match in the alist for first file in FILES.
1029 (while alist
1030 (setq elt (car alist)
1031 regexp (car elt)
1032 alist (cdr alist))
1033 (if (string-match regexp file)
1034 (setq cmds (cdr elt)
1035 alist nil)))
1036
1037 ;; If more than one file, see if all of FILES match regular expression.
1038 (while (and flist
1039 (string-match regexp (car flist)))
1040 (setq flist (cdr flist)))
9572a9dd 1041
cb3fe1f0
RS
1042 ;; If flist is still non-nil, then do not guess since this means that not
1043 ;; all the files in FILES were matched by the regexp.
1044 (setq cmds (and (not flist) cmds))
1045
9572a9dd
KH
1046 ;; Return commands or nil if flist is still non-nil.
1047 ;; Evaluate the commands in order that any logical testing will be done.
cb3fe1f0
RS
1048 (cond ((not (cdr cmds))
1049 (eval (car cmds))) ; single command
1050 (t
1051 (mapcar (function eval) cmds)))))
1052
1053(defun dired-guess-shell-command (prompt files)
1054
1055 ;; Ask user with PROMPT for a shell command, guessing a default from FILES.
1056
1057 (let ((default (dired-guess-default files))
1058 default-list old-history val (failed t))
1059
1060 (if (null default)
1061 ;; Nothing to guess
1062 (read-from-minibuffer prompt nil nil nil 'dired-shell-command-history)
1063
1064 ;; Save current history list
1065 (setq old-history dired-shell-command-history)
1066
1067 (if (listp default)
1068
1069 ;; More than one guess
1070 (setq default-list default
1071 default (car default)
1072 prompt (concat
1073 prompt
1074 (format "{%d guesses} " (length default-list))))
1075
1076 ;; Just one guess
1077 (setq default-list (list default)))
1078
1079 ;; Push all guesses onto history so that they can be retrieved with M-p
1080 ;; and put the first guess in the prompt but not in the initial value.
1081 (setq dired-shell-command-history
1082 (append default-list dired-shell-command-history)
1083 prompt (concat prompt (format "[%s] " default)))
1084
1085 ;; The unwind-protect returns VAL, and we too.
1086 (unwind-protect
1087 ;; BODYFORM
1088 (progn
1089 (setq val (read-from-minibuffer prompt nil nil nil
1090 'dired-shell-command-history)
1091 failed nil)
1092 ;; If we got a return, then use default.
1093 (if (equal val "")
1094 (setq val default))
1095 val)
1096
1097 ;; UNWINDFORMS
1098 ;; Undo pushing onto the history list so that an aborted
1099 ;; command doesn't get the default in the next command.
1100 (setq dired-shell-command-history old-history)
1101 (if (not failed)
1102 (or (equal val (car-safe dired-shell-command-history))
1103 (setq dired-shell-command-history
1104 (cons val dired-shell-command-history))))))))
1105
1106
9572a9dd 1107;;; REDEFINE.
cb3fe1f0
RS
1108;;; Redefine dired-aux.el's version:
1109(defun dired-read-shell-command (prompt arg files)
1110;; "Read a dired shell command prompting with PROMPT (using read-string).
1111;;ARG is the prefix arg and may be used to indicate in the prompt which
1112;; files are affected.
1113;;This is an extra function so that you can redefine it, e.g., to use gmhist."
1114 (dired-mark-pop-up
1115 nil 'shell files
1116 'dired-guess-shell-command
1117 (format prompt (dired-mark-prompt arg files)) ; PROMPT
1118 files)) ; FILES
1119
1120\f
b578f267 1121;;; RELATIVE SYMBOLIC LINKS.
cb3fe1f0
RS
1122
1123(defvar dired-keep-marker-relsymlink ?S
1124 "See variable `dired-keep-marker-move'.")
1125
1126(defun dired-make-relative-symlink (file1 file2 &optional ok-if-already-exists)
1127 "Three arguments: FILE1 FILE2 &optional OK-IF-ALREADY-EXISTS
1128Make a symbolic link (pointing to FILE1) in FILE2.
1129The link is relative (if possible), for example
1130
1131 \"/vol/tex/bin/foo\" \"/vol/local/bin/foo\"
1132
1133results in
1134
1135 \"../../tex/bin/foo\" \"/vol/local/bin/foo\"
1136"
1137 (interactive "FRelSymLink: \nFRelSymLink %s: \np")
1138 (let (name1 name2 len1 len2 (index 0) sub)
1139 (setq file1 (expand-file-name file1)
1140 file2 (expand-file-name file2)
1141 len1 (length file1)
1142 len2 (length file2))
1143 ;; Find common initial pathname components:
1144 (let (next)
1145 (while (and (setq next (string-match "/" file1 index))
1146 (setq next (1+ next))
1147 (< next (min len1 len2))
1148 ;; For the comparison, both substrings must end in
1149 ;; `/', so NEXT is *one plus* the result of the
1150 ;; string-match.
1151 ;; E.g., consider the case of linking "/tmp/a/abc"
34ff2275 1152 ;; to "/tmp/abc" erroneously giving "/tmp/a" instead
cb3fe1f0
RS
1153 ;; of "/tmp/" as common initial component
1154 (string-equal (substring file1 0 next)
1155 (substring file2 0 next)))
1156 (setq index next))
1157 (setq name2 file2
1158 sub (substring file1 0 index)
1159 name1 (substring file1 index)))
1160 (if (string-equal sub "/")
1161 ;; No common initial pathname found
1162 (setq name1 file1)
1163 ;; Else they have a common parent directory
1164 (let ((tem (substring file2 index))
1165 (start 0)
1166 (count 0))
1167 ;; Count number of slashes we must compensate for ...
1168 (while (setq start (string-match "/" tem start))
1169 (setq count (1+ count)
1170 start (1+ start)))
1171 ;; ... and prepend a "../" for each slash found:
1172 (while (> count 0)
1173 (setq count (1- count)
1174 name1 (concat "../" name1)))))
1175 (make-symbolic-link
1176 (directory-file-name name1) ; must not link to foo/
1177 ; (trailing slash!)
1178 name2 ok-if-already-exists)))
1179
1180(defun dired-do-relsymlink (&optional arg)
1181 "Relative symlink all marked (or next ARG) files into a directory,
1182or make a relative symbolic link to the current file.
1183This creates relative symbolic links like
1184
1185 foo -> ../bar/foo
1186
1187not absolute ones like
1188
1189 foo -> /ugly/path/that/may/change/any/day/bar/foo"
1190 (interactive "P")
1191 (dired-do-create-files 'relsymlink (function dired-make-relative-symlink)
1192 "RelSymLink" arg dired-keep-marker-relsymlink))
1193
1194(defun dired-do-relsymlink-regexp (regexp newname &optional whole-path)
1195 "RelSymlink all marked files containing REGEXP to NEWNAME.
1196See functions `dired-do-rename-regexp' and `dired-do-relsymlink'
1197for more info."
1198 (interactive (dired-mark-read-regexp "RelSymLink"))
1199 (dired-do-create-files-regexp
1200 (function dired-make-relative-symlink)
1201 "RelSymLink" nil regexp newname whole-path dired-keep-marker-relsymlink))
1202
1203\f
b578f267 1204;;; VISIT ALL MARKED FILES SIMULTANEOUSLY.
cb3fe1f0 1205
9572a9dd
KH
1206;;; Brief Description:
1207;;;
1208;;; `dired-do-find-marked-files' is bound to `F' by dired-x.el.
1209;;;
cb3fe1f0 1210;;; * Use `dired-get-marked-files' to collect the marked files in the current
9572a9dd
KH
1211;;; Dired Buffer into a list of filenames `FILE-LIST'.
1212;;;
cb3fe1f0
RS
1213;;; * Pass FILE-LIST to `dired-simultaneous-find-file' all with
1214;;; `dired-do-find-marked-files''s prefix argument NOSELECT.
9572a9dd 1215;;;
cb3fe1f0
RS
1216;;; * `dired-simultaneous-find-file' runs through FILE-LIST decrementing the
1217;;; list each time.
9572a9dd 1218;;;
cb3fe1f0
RS
1219;;; * If NOSELECT is non-nil then just run `find-file-noselect' on each
1220;;; element of FILE-LIST.
9572a9dd 1221;;;
cb3fe1f0
RS
1222;;; * If NOSELECT is nil then calculate the `size' of the window for each file
1223;;; by dividing the `window-height' by length of FILE-LIST. Thus, `size' is
1224;;; cognizant of the window-configuration.
9572a9dd 1225;;;
cb3fe1f0
RS
1226;;; * If `size' is too small abort, otherwise run `find-file' on each element
1227;;; of FILE-LIST giving each a window of height `size'.
1228
1229(defun dired-do-find-marked-files (&optional noselect)
1230 "Find all marked files displaying all of them simultaneously.
1231With optional NOSELECT just find files but do not select them.
1232
1233The current window is split across all files marked, as evenly as possible.
1234Remaining lines go to bottom-most window. The number of files that can be
1235displayed this way is restricted by the height of the current window and
1236`window-min-height'.
1237
9572a9dd 1238To keep dired buffer displayed, type \\[split-window-vertically] first.
cb3fe1f0
RS
1239To display just marked files, type \\[delete-other-windows] first."
1240
1241 (interactive "P")
1242 (dired-simultaneous-find-file (dired-get-marked-files) noselect))
1243
1244(defun dired-simultaneous-find-file (file-list noselect)
1245
1246 ;; Visit all files in FILE-LIST and display them simultaneously. The
1247 ;; current window is split across all files in FILE-LIST, as evenly as
1248 ;; possible. Remaining lines go to the bottom-most window. The number of
1249 ;; files that can be displayed this way is restricted by the height of the
1250 ;; current window and the variable `window-min-height'. With non-nil
1251 ;; NOSELECT the files are merely found but not selected.
9572a9dd 1252
cb3fe1f0
RS
1253 ;; We don't make this function interactive because it is usually too clumsy
1254 ;; to specify FILE-LIST interactively unless via dired.
1255
1256 (let (size)
1257
1258 (if noselect
1259 ;; Do not select the buffer.
1260 (find-file-noselect (car file-list))
1261
1262 ;; We will have to select the buffer. Calculate and check window size.
1263 (setq size (/ (window-height) (length file-list)))
1264 (or (<= window-min-height size)
1265 (error "Too many files to visit simultaneously. Try C-u prefix."))
1266 (find-file (car file-list)))
1267
1268 ;; Decrement.
1269 (setq file-list (cdr file-list))
1270
1271 (while file-list
1272
1273 (if noselect
1274 ;; Do not select the buffer.
1275 (find-file-noselect (car file-list))
1276
1277 ;; Vertically split off a window of desired size. Upper window will
1278 ;; have SIZE lines. Select lower (larger) window. We split it again.
1279 (select-window (split-window nil size))
1280 (find-file (car file-list)))
1281
1282 ;; Decrement.
1283 (setq file-list (cdr file-list)))))
1284
1285\f
b578f267 1286;;; MISCELLANEOUS COMMANDS.
cb3fe1f0 1287
9572a9dd
KH
1288;;; Run man on files.
1289
cb3fe1f0 1290(defun dired-man ()
9572a9dd 1291 "Run man on this file. Display old buffer if buffer name matches filename.
685ff9f8 1292Uses ../lisp/man.el of \\[manual-entry] fame."
cb3fe1f0 1293 (interactive)
685ff9f8
RS
1294 (require 'man)
1295 (let ((file (dired-get-filename))
9b5ef74b 1296 (manual-program "nroff -man -h"))
685ff9f8 1297 (Man-getpage-in-background file)))
cb3fe1f0
RS
1298
1299;;; Run Info on files.
1300
1301(defun dired-info ()
1302 "Run info on this file."
1303 (interactive)
1304 (info (dired-get-filename)))
1305
1306;;; Run mail on mail folders.
1307
9572a9dd
KH
1308;;; (and (not (fboundp 'vm-visit-folder))
1309;;; (defun vm-visit-folder (file &optional arg)
1310;;; nil))
1311
cb3fe1f0
RS
1312(defun dired-vm (&optional read-only)
1313 "Run VM on this file.
1314With prefix arg, visit folder read-only (this requires at least VM 5).
1315See also variable `dired-vm-read-only-folders'."
1316 (interactive "P")
1317 (let ((dir (dired-current-directory))
1318 (fil (dired-get-filename)))
1319 ;; take care to supply 2nd arg only if requested - may still run VM 4!
1320 (cond (read-only (vm-visit-folder fil t))
1321 ((eq t dired-vm-read-only-folders) (vm-visit-folder fil t))
1322 ((null dired-vm-read-only-folders) (vm-visit-folder fil))
1323 (t (vm-visit-folder fil (not (file-writable-p fil)))))
1324 ;; so that pressing `v' inside VM does prompt within current directory:
1325 (set (make-local-variable 'vm-folder-directory) dir)))
1326
1327(defun dired-rmail ()
1328 "Run RMAIL on this file."
1329 (interactive)
1330 (rmail (dired-get-filename)))
1331
1332(defun dired-do-run-mail ()
1333 "If `dired-bind-vm' is t, then function `dired-vm', otherwise `dired-rmail'."
1334 (interactive)
1335 (if dired-bind-vm
1336 ;; Read mail folder using vm.
1337 (dired-vm)
1338 ;; Read mail folder using rmail.
1339 (dired-rmail)))
1340
1341\f
b578f267 1342;;; MISCELLANEOUS INTERNAL FUNCTIONS.
cb3fe1f0
RS
1343
1344(or (fboundp 'dired-old-find-buffer-nocreate)
1345 (fset 'dired-old-find-buffer-nocreate
1346 (symbol-function 'dired-find-buffer-nocreate)))
1347
9572a9dd 1348;;; REDEFINE.
cb3fe1f0 1349;;; Redefines dired.el's version of `dired-find-buffer-nocreate'
ac1ce341 1350(defun dired-find-buffer-nocreate (dirname &optional mode)
10774fb5
KH
1351 (if (and dired-find-subdir
1352 ;; don't try to find a wildcard as a subdirectory
1353 (string-equal dirname (file-name-directory dirname)))
cb3fe1f0 1354 (let* ((cur-buf (current-buffer))
10774fb5 1355 (buffers (nreverse
12c38283 1356 (dired-buffers-for-dir (expand-file-name dirname))))
10774fb5
KH
1357 (cur-buf-matches (and (memq cur-buf buffers)
1358 ;; wildcards must match, too:
1359 (equal dired-directory dirname))))
1360 ;; We don't want to switch to the same buffer---
1361 (setq buffers (delq cur-buf buffers));;need setq with delq
1362 (or (car (sort buffers (function dired-buffer-more-recently-used-p)))
1363 ;; ---unless it's the only possibility:
1364 (and cur-buf-matches cur-buf)))
ac1ce341 1365 (dired-old-find-buffer-nocreate dirname mode)))
cb3fe1f0
RS
1366
1367;; This should be a builtin
1368(defun dired-buffer-more-recently-used-p (buffer1 buffer2)
1369 "Return t if BUFFER1 is more recently used than BUFFER2."
1370 (if (equal buffer1 buffer2)
1371 nil
1372 (let ((more-recent nil)
1373 (list (buffer-list)))
1374 (while (and list
1375 (not (setq more-recent (equal buffer1 (car list))))
1376 (not (equal buffer2 (car list))))
1377 (setq list (cdr list)))
1378 more-recent)))
1379
1380;;; Same thing as `dired-buffers-for-dir' of dired.el? - lrd 11/23/93
1381;;; (defun dired-buffers-for-dir-exact (dir)
1382;;; ;; Return a list of buffers that dired DIR (a directory or wildcard)
1383;;; ;; at top level, or as subdirectory.
1384;;; ;; Top level matches must match the wildcard part too, if any.
1385;;; ;; The list is in reverse order of buffer creation, most recent last.
1386;;; ;; As a side effect, killed dired buffers for DIR are removed from
1387;;; ;; dired-buffers.
1388;;; (let ((alist dired-buffers) result elt)
1389;;; (while alist
1390;;; (setq elt (car alist)
1391;;; alist (cdr alist))
1392;;; (let ((buf (cdr elt)))
1393;;; (if (buffer-name buf)
1394;;; ;; Top level must match exactly against dired-directory in
1395;;; ;; case one of them is a wildcard.
1396;;; (if (or (equal dir (save-excursion (set-buffer buf)
1397;;; dired-directory))
1398;;; (assoc dir (save-excursion (set-buffer buf)
1399;;; dired-subdir-alist)))
1400;;; (setq result (cons buf result)))
1401;;; ;; else buffer is killed - clean up:
1402;;; (setq dired-buffers (delq elt dired-buffers)))))
1403;;; result))
1404
9572a9dd 1405;;; REDEFINE.
cb3fe1f0
RS
1406;;; Redefines dired.el's version of `dired-initial-position'
1407(defun dired-initial-position (dirname)
1408 (end-of-line)
1409 (if dired-find-subdir (dired-goto-subdir dirname)) ; new
1410 (if dired-trivial-filenames (dired-goto-next-nontrivial-file)))
1411
1412\f
1413;; Does anyone use this? - lrd 6/29/93.
1414(defun dired-mark-sexp (predicate &optional unflag-p)
1415 "Mark files for which PREDICATE returns non-nil.
1416With a prefix arg, unflag those files instead.
1417
1418PREDICATE is a lisp expression that can refer to the following symbols:
1419
1420 inode [integer] the inode of the file (only for ls -i output)
1421 s [integer] the size of the file for ls -s output
34ff2275 1422 (usually in blocks or, with -k, in KByte)
cb3fe1f0
RS
1423 mode [string] file permission bits, e.g. \"-rw-r--r--\"
1424 nlink [integer] number of links to file
1425 uid [string] owner
1426 gid [string] group (If the gid is not displayed by ls,
1427 this will still be set (to the same as uid))
1428 size [integer] file size in bytes
1429 time [string] the time that ls displays, e.g. \"Feb 12 14:17\"
1430 name [string] the name of the file
1431 sym [string] if file is a symbolic link, the linked-to name, else \"\"
1432
1433For example, use
1434
1435 (equal 0 size)
1436
1437to mark all zero length files."
1438 ;; Using sym="" instead of nil avoids the trap of
1439 ;; (string-match "foo" sym) into which a user would soon fall.
1440 ;; Give `equal' instead of `=' in the example, as this works on
1441 ;; integers and strings.
1442 (interactive "xMark if (lisp expr): \nP")
1443 (message "%s" predicate)
1444 (let ((dired-marker-char (if unflag-p ?\040 dired-marker-char))
1445 inode s mode nlink uid gid size time name sym)
1446 (dired-mark-if
1447 (save-excursion
1448 (and
1449 ;; Sets vars
1450 ;; inode s mode nlink uid gid size time name sym
1451
1452 ;; according to current file line. Returns t for success, nil if
1453 ;; there is no file line. Upon success, all variables are set, either
1454 ;; to nil or the appropriate value, so they need not be initialized.
1455 ;; Moves point within the current line.
1456 (if (dired-move-to-filename)
1457 (let (pos
1458 (mode-len 10) ; length of mode string
1459 ;; like in dired.el, but with subexpressions \1=inode, \2=s:
1460 (dired-re-inode-size "\\s *\\([0-9]*\\)\\s *\\([0-9]*\\) ?"))
1461 (beginning-of-line)
1462 (forward-char 2)
1463 (if (looking-at dired-re-inode-size)
1464 (progn
1465 (goto-char (match-end 0))
1466 (setq inode (string-to-int (buffer-substring (match-beginning 1)
1467 (match-end 1)))
1468 s (string-to-int (buffer-substring (match-beginning 2)
1469 (match-end 2)))))
1470 (setq inode nil
1471 s nil))
1472 (setq mode (buffer-substring (point) (+ mode-len (point))))
1473 (forward-char mode-len)
1474 (setq nlink (read (current-buffer)))
1475 (setq uid (buffer-substring (point) (progn (forward-word 1) (point))))
1476 (re-search-forward "\\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\)")
1477 (goto-char (match-beginning 1))
1478 (forward-char -1)
1479 (setq size (string-to-int (buffer-substring (save-excursion
1480 (backward-word 1)
1481 (setq pos (point)))
1482 (point))))
1483 (goto-char pos)
1484 (backward-word 1)
1485 ;; if no gid is displayed, gid will be set to uid
1486 ;; but user will then not reference it anyway in PREDICATE.
1487 (setq gid (buffer-substring (save-excursion (forward-word 1) (point))
1488 (point))
1489 time (buffer-substring (match-beginning 1)
1490 (1- (dired-move-to-filename)))
1491 name (buffer-substring (point)
1492 (or (dired-move-to-end-of-filename t)
1493 (point)))
1494 sym (progn
1495 (if (looking-at " -> ")
1496 (buffer-substring (progn (forward-char 4) (point))
1497 (progn (end-of-line) (point)))
1498 "")))
1499 t)
1500 nil)
1501 (eval predicate)))
1502 (format "'%s file" predicate))))
1503
1504\f
b578f267 1505;;; FIND FILE AT POINT.
cb3fe1f0 1506
ccb1d39a
RS
1507(defvar dired-x-hands-off-my-keys t
1508 "*t means don't bind `dired-x-find-file' over `find-file' on keyboard.
1509Similarly for `dired-x-find-file-other-window' over `find-file-other-window'.
1510If you change this variable after dired-x.el is loaded then do
1511\\[dired-x-bind-find-file].")
1512
1513;;; Bind `dired-x-find-file{-other-window}' over wherever
1514;;; `find-file{-other-window}' is bound?
1515(defun dired-x-bind-find-file ()
1516 "Bind `dired-x-find-file' in place of `find-file' \(or reverse\).
1517Similarly for `dired-x-find-file-other-window' and `find-file-other-window'.
1518Binding direction based on `dired-x-hands-off-my-keys'.
1519This function part of `after-init-hook'."
1520 (interactive)
1521 (if (interactive-p)
1522 (setq dired-x-hands-off-my-keys
1523 (not (y-or-n-p "Bind dired-x-find-file over find-file? "))))
1524 (cond ((not dired-x-hands-off-my-keys)
1525 (substitute-key-definition 'find-file
1526 'dired-x-find-file
1527 (current-global-map))
1528 (substitute-key-definition 'find-file-other-window
1529 'dired-x-find-file-other-window
1530 (current-global-map)))
1531 (t
1532 (substitute-key-definition 'dired-x-find-file
1533 'find-file
1534 (current-global-map))
1535 (substitute-key-definition 'dired-x-find-file-other-window
1536 'find-file-other-window
1537 (current-global-map))))
1538 ;; Clear mini-buffer.
1539 (message nil))
1540
1541;;; Now call it so binding is correct and put on `after-init-hook' in case
1542;;; user changes binding.
1543(dired-x-bind-find-file)
1544(add-hook 'after-init-hook 'dired-x-bind-find-file)
1545
1546(defun dired-x-find-file (filename)
1547 "Edit file FILENAME.
1548May create a new window, or reuse an existing one.
1549See the function `display-buffer'.
1550
1551Identical to `find-file' except when called interactively, with a prefix arg
1552\(e.g., \\[universal-argument]\), in which case it guesses filename near
1553point. Useful for editing file mentioned in buffer you are viewing, or to
1554test if that file exists. Use minibuffer after snatching filename."
1555 (interactive (list (read-filename-at-point "Find file: ")))
1556 (find-file (expand-file-name filename)))
1557
1558(defun dired-x-find-file-other-window (filename)
1559 "Edit file FILENAME, in another window.
1560May create a new window, or reuse an existing one.
1561See the function `display-buffer'.
1562
1563Identical to `find-file-other-window' except when called interactively, with a
1564prefix arg \(e.g., \\[universal-argument]\), in which case it guesses filename
1565near point. Useful for editing file mentioned in buffer you are viewing, or
1566to test if that file exists. Use minibuffer after snatching filename."
1567 (interactive (list (read-filename-at-point "Find file: ")))
1568 (find-file-other-window (expand-file-name filename)))
1569
1570;;; Internal functions.
cb3fe1f0
RS
1571(defun dired-filename-at-point ()
1572
1573 ;; Get the filename closest to point, but do not change position. Has a
1574 ;; preference for looking backward when not directly on a symbol. Not
1575 ;; perfect - point must be in middle of or end of filename.
1576
9572a9dd 1577 (let ((filename-chars ".a-zA-Z0-9---_/:$+")
cb3fe1f0
RS
1578 (bol (save-excursion (beginning-of-line) (point)))
1579 (eol (save-excursion (end-of-line) (point)))
1580 start end filename)
1581
1582 (save-excursion
1583 ;; First see if just past a filename.
1584 (if (not (eobp))
1585 (if (looking-at "[] \t\n[{}()]") ; whitespace or some parens
1586 (progn
1587 (skip-chars-backward " \n\t\r({[]})")
1588 (if (not (bobp))
1589 (backward-char 1)))))
9572a9dd 1590
cb3fe1f0
RS
1591 (if (string-match (concat "[" filename-chars "]")
1592 (char-to-string (following-char)))
1593 (progn
1594 (skip-chars-backward filename-chars)
1595 (setq start (point))
1596 (if (string-match "[/~]" (char-to-string (preceding-char)))
1597 (setq start (1- start)))
1598 (skip-chars-forward filename-chars))
1599
1600 (error "No file found around point!"))
1601
9572a9dd 1602 ;; Return string.
cb3fe1f0
RS
1603 (expand-file-name (buffer-substring start (point))))))
1604
ccb1d39a
RS
1605(defun read-filename-at-point (prompt)
1606 ;;; Returns filename prompting with PROMPT with completion. If
1607 ;;; `current-prefix-arg' is non-nil, uses name at point as guess.
1608 (if current-prefix-arg
1609 (let ((guess (dired-filename-at-point)))
1610 (read-file-name prompt
1611 (file-name-directory guess)
1612 guess
1613 nil (file-name-nondirectory guess)))
1614 (read-file-name prompt default-directory)))
1615
cb3fe1f0 1616\f
b578f267 1617;;; BUG REPORTS
cb3fe1f0
RS
1618
1619;;; This section is provided for reports. It uses Barry A. Warsaw's
1620;;; reporter.el which is bundled with GNU Emacs v19.
1621
ccb1d39a 1622(defconst dired-x-version "2.37"
cb3fe1f0 1623 "Revision number of dired-x.el -- dired extra for GNU Emacs v19.
9572a9dd 1624Type \\[dired-x-submit-report] to send a bug report. Available via anonymous
cb3fe1f0
RS
1625ftp in
1626
1627 /roebling.poly.edu:/pub/packages/dired-x.tar.gz")
1628
1629(defconst dired-x-help-address "dodd@roebling.poly.edu"
1630 "Address(es) accepting submission of reports on dired-x.el.")
1631
1632(defconst dired-x-maintainer "Larry"
1633 "First name(s) of people accepting submission of reports on dired-x.el.")
1634
1635(defconst dired-x-file "dired-x.el"
1636 "Name of file containing emacs lisp code.")
1637
1638(defconst dired-x-variable-list
9572a9dd 1639 (list
cb3fe1f0
RS
1640 'dired-bind-vm
1641 'dired-vm-read-only-folders
1642 'dired-bind-jump
1643 'dired-bind-info
1644 'dired-bind-man
1645 'dired-find-subdir
1646 'dired-enable-local-variables
1647 'dired-local-variables-file
1648 'dired-guess-shell-gnutar
1649 'dired-guess-shell-gzip-quiet
1650 'dired-guess-shell-znew-switches
1651 'dired-guess-shell-alist-user
1652 'dired-clean-up-buffers-too
1653 'dired-omit-files-p
1654 'dired-omit-files
1655 'dired-omit-extensions
1656 )
1657 "List of variables to be appended to reports sent by `dired-x-submit-report.'")
1658
1659(defun dired-x-submit-report ()
1660 "Submit via reporter.el a bug report on program. Send report on `dired-x-file'
1661version `dired-x-version,' to `dired-x-maintainer' at address `dired-x-help-address'
1662listing variables `dired-x-variable-list' in the message."
1663 (interactive)
1664
1665 ;; In case we can't find reporter...
1666 (condition-case err
1667 (progn
1668 ;; Get it if we can.
1669 (require 'reporter)
1670
1671 (reporter-submit-bug-report
1672 dired-x-help-address ; address
1673 (concat dired-x-file " (" dired-x-version ")") ; pkgname
1674 dired-x-variable-list ; varlist
1675 nil nil ; pre-/post-hooks
1676 (concat dired-x-maintainer ","))) ; salutation
1677
1678 ;; ...fail gracefully.
9572a9dd 1679 (error
cb3fe1f0
RS
1680 (beep)
1681 (message "Sorry, reporter.el not found."))))
1682
1683\f
1684;; As Barry Warsaw would say: "This might be useful..."
1685(provide 'dired-x)
1686
1687;;; dired-x.el ends here