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