1 ;;; dired.el --- directory-browsing commands
3 ;; Copyright (C) 1985, 1986, 1992, 1993 Free Software Foundation, Inc.
5 ;; Author: Sebastian Kremer <sk@thp.uni-koeln.de>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; This is a major mode for directory browsing and editing. It is
27 ;; documented in the Emacs manual.
29 ;; Rewritten in 1990/1991 to add tree features, file marking and
30 ;; sorting by Sebastian Kremer <sk@thp.uni-koeln.de>.
31 ;; Finished up by rms in 1992.
35 ;;; Customizable variables
38 (defvar dired-listing-switches
"-al"
39 "*Switches passed to `ls' for dired. MUST contain the `l' option.
40 May contain all other options that don't contradict `-l';
41 may contain even `F', `b', `i' and `s'. See also the variable
42 `dired-ls-F-marks-symlinks' concerning the `F' switch.")
44 ; Don't use absolute paths as /bin should be in any PATH and people
45 ; may prefer /usr/local/gnu/bin or whatever. However, chown is
46 ; usually not in PATH.
49 (defvar dired-chown-program
50 (if (memq system-type
'(hpux dgux usg-unix-v irix
))
52 "Name of chown command (usually `chown' or `/etc/chown').")
55 (defvar dired-ls-F-marks-symlinks nil
56 "*Informs dired about how `ls -lF' marks symbolic links.
57 Set this to t if `insert-directory-program' with `-lF' marks the symbolic link
58 itself with a trailing @ (usually the case under Ultrix).
60 Example: if `ln -s foo bar; ls -F bar' gives `bar -> foo', set it to
61 nil (the default), if it gives `bar@ -> foo', set it to t.
63 Dired checks if there is really a @ appended. Thus, if you have a
64 marking `ls' program on one host and a non-marking on another host, and
65 don't care about symbolic links which really end in a @, you can
66 always set this variable to t.")
69 (defvar dired-trivial-filenames
"^\\.\\.?$\\|^#"
70 "*Regexp of files to skip when finding first file of a directory.
71 A value of nil means move to the subdir line.
72 A value of t means move to first file.")
75 (defvar dired-keep-marker-rename t
76 ;; Use t as default so that moved files "take their markers with them".
77 "*Controls marking of renamed files.
78 If t, files keep their previous marks when they are renamed.
79 If a character, renamed files (whether previously marked or not)
80 are afterward marked with that character.")
83 (defvar dired-keep-marker-copy ?C
84 "*Controls marking of copied files.
85 If t, copied files are marked if and as the corresponding original files were.
86 If a character, copied files are unconditionally marked with that character.")
89 (defvar dired-keep-marker-hardlink ?H
90 "*Controls marking of newly made hard links.
91 If t, they are marked if and as the files linked to were marked.
92 If a character, new links are unconditionally marked with that character.")
95 (defvar dired-keep-marker-symlink ?Y
96 "*Controls marking of newly made symbolic links.
97 If t, they are marked if and as the files linked to were marked.
98 If a character, new links are unconditionally marked with that character.")
101 (defvar dired-dwim-target nil
102 "*If non-nil, dired tries to guess a default target directory.
103 This means: if there is a dired buffer displayed in the next window,
104 use its current subdir, instead of the current subdir of this dired buffer.
106 The target is used in the prompt for file copy, rename etc.")
109 (defvar dired-copy-preserve-time t
110 "*If non-nil, Dired preserves the last-modified time in a file copy.
111 \(This works on only some systems.)")
115 (defvar dired-load-hook nil
116 "Run after loading dired.
117 You can customize key bindings or load extensions with this.")
119 (defvar dired-mode-hook nil
120 "Run at the very end of dired-mode.")
122 (defvar dired-before-readin-hook nil
123 "This hook is run before a dired buffer is read in (created or reverted).")
125 (defvar dired-after-readin-hook nil
126 "Hook run after each time a file or directory is read by Dired.
127 After each listing of a file or directory, this hook is run
128 with the buffer narrowed to the listing.")
129 ;; Note this can't simply be run inside function `dired-ls' as the hook
130 ;; functions probably depend on the dired-subdir-alist to be OK.
132 ;;; Internal variables
134 (defvar dired-marker-char ?
* ; the answer is 42
135 ;; so that you can write things like
136 ;; (let ((dired-marker-char ?X))
137 ;; ;; great code using X markers ...
139 ;; For example, commands operating on two sets of files, A and B.
140 ;; Or marking files with digits 0-9. This could implicate
141 ;; concentric sets or an order for the marked files.
142 ;; The code depends on dynamic scoping on the marker char.
143 "In Dired, the current mark character.
144 This is what the `do' commands look for and what the `mark' commands store.")
146 (defvar dired-del-marker ?D
147 "Character used to flag files for deletion.")
149 (defvar dired-shrink-to-fit
151 ;; I see no reason ever to make this nil -- rms.
152 ;; (> baud-rate search-slow-speed)
153 "Non-nil means Dired shrinks the display buffer to fit the marked files.")
155 (defvar dired-flagging-regexp nil
);; Last regexp used to flag files.
157 (defvar dired-file-version-alist
)
159 (defvar dired-directory nil
160 "The directory name or shell wildcard that was used as argument to `ls'.
161 Local to each dired buffer. May be a list, in which case the car is the
162 directory name and the cdr is the actual files to list.")
164 (defvar dired-actual-switches nil
165 "The value of `dired-listing-switches' used to make this buffer's text.")
167 (defvar dired-re-inode-size
"[0-9 \t]*"
168 "Regexp for optional initial inode and file size as made by `ls -i -s'.")
170 ;; These regexps must be tested at beginning-of-line, but are also
171 ;; used to search for next matches, so neither omitting "^" nor
172 ;; replacing "^" by "\n" (to make it slightly faster) will work.
174 (defvar dired-re-mark
"^[^ \n]")
175 ;; "Regexp matching a marked line.
176 ;; Important: the match ends just after the marker."
177 (defvar dired-re-maybe-mark
"^. ")
178 (defvar dired-re-dir
(concat dired-re-maybe-mark dired-re-inode-size
"d"))
179 (defvar dired-re-sym
(concat dired-re-maybe-mark dired-re-inode-size
"l"))
180 (defvar dired-re-exe
;; match ls permission string of an executable file
183 (concat dired-re-maybe-mark dired-re-inode-size x
)))
184 '("-[-r][-w][xs][-r][-w].[-r][-w]."
185 "-[-r][-w].[-r][-w][xs][-r][-w]."
186 "-[-r][-w].[-r][-w].[-r][-w][xst]")
188 (defvar dired-re-dot
"^.* \\.\\.?$")
190 (defvar dired-subdir-alist nil
191 "Association list of subdirectories and their buffer positions.
192 Each subdirectory has an element: (DIRNAME . STARTMARKER).
193 The order of elements is the reverse of the order in the buffer.
194 In simple cases, this list contains one element.")
196 (defvar dired-subdir-regexp
"^. \\([^ \n\r]+\\)\\(:\\)[\n\r]"
197 "Regexp matching a maybe hidden subdirectory line in `ls -lR' output.
198 Subexpression 1 is the subdirectory proper, no trailing colon.
199 The match starts at the beginning of the line and ends after the end
200 of the line (\\n or \\r).
201 Subexpression 2 must end right before the \\n or \\r.")
204 ;;; Macros must be defined before they are used, for the byte compiler.
206 ;; Mark all files for which CONDITION evals to non-nil.
207 ;; CONDITION is evaluated on each line, with point at beginning of line.
208 ;; MSG is a noun phrase for the type of files being marked.
209 ;; It should end with a noun that can be pluralized by adding `s'.
210 ;; Return value is the number of files marked, or nil if none were marked.
211 (defmacro dired-mark-if
(predicate msg
)
212 (` (let (buffer-read-only count
)
215 (if (, msg
) (message "Marking %ss..." (, msg
)))
216 (goto-char (point-min))
221 (insert dired-marker-char
)
222 (setq count
(1+ count
))))
224 (if (, msg
) (message "%s %s%s %s%s."
227 (dired-plural-s count
)
228 (if (eq dired-marker-char ?
\040) "un" "")
229 (if (eq dired-marker-char dired-del-marker
)
230 "flagged" "marked"))))
231 (and (> count
0) count
))))
233 (defmacro dired-map-over-marks
(body arg
&optional show-progress
)
234 ;; "Macro: Perform BODY with point somewhere on each marked line
235 ;;and return a list of BODY's results.
236 ;;If no marked file could be found, execute BODY on the current line.
237 ;; If ARG is an integer, use the next ARG (or previous -ARG, if ARG<0)
238 ;; files instead of the marked files.
239 ;; In that case point is dragged along. This is so that commands on
240 ;; the next ARG (instead of the marked) files can be chained easily.
241 ;; If ARG is otherwise non-nil, use current file instead.
242 ;;If optional third arg SHOW-PROGRESS evaluates to non-nil,
243 ;; redisplay the dired buffer after each file is processed.
244 ;;No guarantee is made about the position on the marked line.
245 ;; BODY must ensure this itself if it depends on this.
246 ;;Search starts at the beginning of the buffer, thus the car of the list
247 ;; corresponds to the line nearest to the buffer's bottom. This
248 ;; is also true for (positive and negative) integer values of ARG.
249 ;;BODY should not be too long as it is expanded four times."
251 ;;Warning: BODY must not add new lines before point - this may cause an
253 ;;This warning should not apply any longer, sk 2-Sep-1991 14:10.
255 (let (buffer-read-only case-fold-search found results
)
257 (if (integerp (, arg
))
258 (progn;; no save-excursion, want to move point.
259 (dired-repeat-over-lines
262 (if (, show-progress
) (sit-for 0))
263 (setq results
(cons (, body
) results
)))))
267 ;; non-nil, non-integer ARG means use current file:
269 (let ((regexp (dired-marker-regexp)) next-position
)
271 (goto-char (point-min))
272 ;; remember position of next marked file before BODY
273 ;; can insert lines before the just found file,
274 ;; confusing us by finding the same marked file again
276 (setq next-position
(and (re-search-forward regexp nil t
)
278 found
(not (null next-position
)))
280 (goto-char next-position
)
281 (if (, show-progress
) (sit-for 0))
282 (setq results
(cons (, body
) results
))
283 ;; move after last match
284 (goto-char next-position
)
286 (set-marker next-position nil
)
287 (setq next-position
(and (re-search-forward regexp nil t
)
292 ;; save-excursion loses, again
293 (dired-move-to-filename))))
295 (defun dired-get-marked-files (&optional localp arg
)
296 "Return the marked files' names as list of strings.
297 The list is in the same order as the buffer, that is, the car is the
299 Values returned are normally absolute pathnames.
300 Optional arg LOCALP as in `dired-get-filename'.
301 Optional second argument ARG forces to use other files. If ARG is an
302 integer, use the next ARG files. If ARG is otherwise non-nil, use
303 current file. Usually ARG comes from the current prefix arg."
305 (nreverse (dired-map-over-marks (dired-get-filename localp
) arg
))))
308 ;; Function dired-ls is redefinable for VMS, ange-ftp, Prospero or
309 ;; other special applications.
313 (defun dired-read-dir-and-switches (str)
314 ;; For use in interactive.
316 (if current-prefix-arg
317 (read-string "Dired listing switches: "
318 dired-listing-switches
))
319 (read-file-name (format "Dired %s(directory): " str
)
320 nil default-directory nil
))))
322 ;;;###autoload (define-key ctl-x-map "d" 'dired)
324 (defun dired (dirname &optional switches
)
325 "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it.
326 Optional second argument SWITCHES specifies the `ls' options used.
327 \(Interactively, use a prefix argument to be able to specify SWITCHES.)
328 Dired displays a list of files in DIRNAME (which may also have
329 shell wildcards appended to select certain files). If DIRNAME is a cons,
330 its first element is taken as the directory name and the resr as an explicit
331 list of files to make directory entries for.
333 You can move around in it with the usual commands.
334 You can flag files for deletion with \\[dired-flag-file-deletion] and then
335 delete them by typing \\[dired-do-flagged-delete].
336 Type \\[describe-mode] after entering dired for more info.
338 If DIRNAME is already in a dired buffer, that buffer is used without refresh."
339 ;; Cannot use (interactive "D") because of wildcards.
340 (interactive (dired-read-dir-and-switches ""))
341 (switch-to-buffer (dired-noselect dirname switches
)))
343 ;;;###autoload (define-key ctl-x-4-map "d" 'dired-other-window)
345 (defun dired-other-window (dirname &optional switches
)
346 "\"Edit\" directory DIRNAME. Like `dired' but selects in another window."
347 (interactive (dired-read-dir-and-switches "in other window "))
348 (switch-to-buffer-other-window (dired-noselect dirname switches
)))
350 ;;;###autoload (define-key ctl-x-5-map "d" 'dired-other-frame)
352 (defun dired-other-frame (dirname &optional switches
)
353 "\"Edit\" directory DIRNAME. Like `dired' but makes a new frame."
354 (interactive (dired-read-dir-and-switches "in other frame "))
355 (switch-to-buffer-other-frame (dired-noselect dirname switches
)))
358 (defun dired-noselect (dir-or-list &optional switches
)
359 "Like `dired' but returns the dired buffer as value, does not select it."
360 (or dir-or-list
(setq dir-or-list default-directory
))
361 ;; This loses the distinction between "/foo/*/" and "/foo/*" that
364 (if (consp dir-or-list
)
365 (setq dirname
(car dir-or-list
))
366 (setq dirname dir-or-list
))
367 (setq dirname
(expand-file-name (directory-file-name dirname
)))
368 (if (file-directory-p dirname
)
369 (setq dirname
(file-name-as-directory dirname
)))
370 (if (consp dir-or-list
)
371 (setq dir-or-list
(cons dirname
(cdr dir-or-list
)))
372 (setq dir-or-list dirname
))
373 (dired-internal-noselect dir-or-list switches
)))
375 ;; Separate function from dired-noselect for the sake of dired-vms.el.
376 (defun dired-internal-noselect (dir-or-list &optional switches
)
377 ;; If there is an existing dired buffer for DIRNAME, just leave
378 ;; buffer as it is (don't even call dired-revert).
379 ;; This saves time especially for deep trees or with ange-ftp.
380 ;; The user can type `g'easily, and it is more consistent with find-file.
381 ;; But if SWITCHES are given they are probably different from the
382 ;; buffer's old value, so call dired-sort-other, which does
383 ;; revert the buffer.
384 ;; A pity we can't possibly do "Directory has changed - refresh? "
385 ;; like find-file does.
386 (let* ((dirname (if (consp dir-or-list
) (car dir-or-list
) dir-or-list
))
387 (buffer (dired-find-buffer-nocreate dir-or-list
))
388 ;; note that buffer already is in dired-mode, if found
389 (new-buffer-p (not buffer
))
390 (old-buf (current-buffer)))
392 (let ((default-major-mode 'fundamental-mode
))
393 ;; We don't want default-major-mode to run hooks and set auto-fill
394 ;; or whatever, now that dired-mode does not
395 ;; kill-all-local-variables any longer.
396 (setq buffer
(create-file-buffer (directory-file-name dirname
)))))
398 (if (not new-buffer-p
) ; existing buffer ...
399 (if switches
; ... but new switches
400 (dired-sort-other switches
)) ; this calls dired-revert
402 (setq default-directory
403 (abbreviate-file-name
404 (if (file-directory-p dirname
)
406 (file-name-directory dirname
))))
407 (or switches
(setq switches dired-listing-switches
))
408 (dired-mode dirname switches
)
409 ;; default-directory and dired-actual-switches are set now
410 ;; (buffer-local), so we can call dired-readin:
413 (progn (dired-readin dir-or-list buffer
)
415 ;; dired-readin can fail if parent directories are inaccessible.
416 ;; Don't leave an empty buffer around in that case.
417 (if failed
(kill-buffer buffer
))))
418 ;; No need to narrow since the whole buffer contains just
419 ;; dired-readin's output, nothing else. The hook can
420 ;; successfully use dired functions (e.g. dired-get-filename)
421 ;; as the subdir-alist has been built in dired-readin.
422 (run-hooks 'dired-after-readin-hook
)
423 (goto-char (point-min))
424 (dired-initial-position dirname
))
428 ;; This differs from dired-buffers-for-dir in that it does not consider
429 ;; subdirs of default-directory and searches for the first match only
430 (defun dired-find-buffer-nocreate (dirname)
431 (let (found (blist (buffer-list)))
434 (set-buffer (car blist
))
435 (if (and (eq major-mode
'dired-mode
)
436 (equal dired-directory dirname
))
437 (setq found
(car blist
)
439 (setq blist
(cdr blist
)))))
443 ;; Read in a new dired buffer
445 ;; dired-readin differs from dired-insert-subdir in that it accepts
446 ;; wildcards, erases the buffer, and builds the subdir-alist anew
447 ;; (including making it buffer-local and clearing it first).
448 (defun dired-readin (dir-or-list buffer
)
449 ;; default-directory and dired-actual-switches must be buffer-local
450 ;; and initialized by now.
451 ;; Thus we can test (equal default-directory dirname) instead of
452 ;; (file-directory-p dirname) and save a filesystem transaction.
453 ;; Also, we can run this hook which may want to modify the switches
454 ;; based on default-directory, e.g. with ange-ftp to a SysV host
455 ;; where ls won't understand -Al switches.
457 (if (consp dir-or-list
)
458 (setq dirname
(car dir-or-list
))
459 (setq dirname dir-or-list
))
460 (setq dirname
(expand-file-name dirname
))
461 (if (consp dir-or-list
)
462 (setq dir-or-list
(cons dirname
(cdr dir-or-list
))))
463 (run-hooks 'dired-before-readin-hook
)
465 (message "Reading directory %s..." dirname
)
467 (let (buffer-read-only (failed t
))
470 (dired-readin-insert dir-or-list
)
471 (indent-rigidly (point-min) (point-max) 2)
472 ;; We need this to make the root dir have a header line as all
473 ;; other subdirs have:
474 (goto-char (point-min))
475 (dired-insert-headerline default-directory
)
476 ;; can't run dired-after-readin-hook here, it may depend on the subdir
479 (message "Reading directory %s...done" dirname
)
480 (set-buffer-modified-p nil
)
481 ;; Must first make alist buffer local and set it to nil because
482 ;; dired-build-subdir-alist will call dired-clear-alist first
483 (set (make-local-variable 'dired-subdir-alist
) nil
)
484 (dired-build-subdir-alist))))
486 ;; Subroutines of dired-readin
488 (defun dired-readin-insert (dir-or-list)
489 ;; Just insert listing for the passed-in directory or
490 ;; directory-and-file list, assuming a clean buffer.
492 (if (consp dir-or-list
)
493 (setq dirname
(car dir-or-list
))
494 (setq dirname dir-or-list
))
495 ;; Expand before comparing in case one or both have been abbreviated.
496 (if (and (equal (expand-file-name default-directory
)
497 (expand-file-name dirname
))
498 (not (consp dir-or-list
)))
499 ;; If we are reading a whole single directory...
500 (dired-insert-directory dir-or-list dired-actual-switches nil t
)
501 (if (not (file-readable-p
502 (directory-file-name (file-name-directory dirname
))))
503 (error "Directory %s inaccessible or nonexistent" dirname
)
504 ;; Else assume it contains wildcards,
505 ;; unless it is an explicit list of files.
506 (dired-insert-directory dir-or-list dired-actual-switches
507 (not (listp dir-or-list
)))
508 (save-excursion ;; insert wildcard instead of total line:
509 (goto-char (point-min))
510 (insert "wildcard " (file-name-nondirectory dirname
) "\n"))))))
512 (defun dired-insert-directory (dir-or-list switches
&optional wildcard full-p
)
513 ;; Do the right thing whether dir-or-list is atomic or not. If it is,
514 ;; inset all files listed in the cdr (the car is the passed-in directory
516 (if (consp dir-or-list
)
519 (function (lambda (x) (insert-directory x switches wildcard full-p
)))
521 (insert-directory dir-or-list switches wildcard full-p
))
522 (setq dired-directory dir-or-list
))
524 (defun dired-insert-headerline (dir);; also used by dired-insert-subdir
525 ;; Insert DIR's headerline with no trailing slash, exactly like ls
526 ;; would, and put cursor where dired-build-subdir-alist puts subdir
528 (save-excursion (insert " " (directory-file-name dir
) ":\n")))
531 ;; Reverting a dired buffer
533 (defun dired-revert (&optional arg noconfirm
)
534 ;; Reread the dired buffer. Must also be called after
535 ;; dired-actual-switches have changed.
536 ;; Should not fail even on completely garbaged buffers.
537 ;; Preserves old cursor, marks/flags, hidden-p.
538 (widen) ; just in case user narrowed
539 (let ((opoint (point))
540 (ofile (dired-get-filename nil t
))
541 (mark-alist nil
) ; save marked files
542 (hidden-subdirs (dired-remember-hidden))
543 (old-subdir-alist (cdr (reverse dired-subdir-alist
))) ; except pwd
544 (case-fold-search nil
) ; we check for upper case ls flags
546 (goto-char (point-min))
547 (setq mark-alist
;; only after dired-remember-hidden since this unhides:
548 (dired-remember-marks (point-min) (point-max)))
549 ;; treat top level dir extra (it may contain wildcards)
551 (if (consp dired-directory
) (car dired-directory
) dired-directory
))
552 (dired-readin dired-directory
(current-buffer))
553 (let ((dired-after-readin-hook nil
))
554 ;; don't run that hook for each subdir...
555 (dired-insert-old-subdirs old-subdir-alist
))
556 (dired-mark-remembered mark-alist
) ; mark files that were marked
557 ;; ... run the hook for the whole buffer, and only after markers
558 ;; have been reinserted (else omitting in dired-x would omit marked files)
559 (run-hooks 'dired-after-readin-hook
) ; no need to narrow
560 (or (and ofile
(dired-goto-file ofile
)) ; move cursor to where it
561 (goto-char opoint
)) ; was before
562 (dired-move-to-filename)
563 (save-excursion ; hide subdirs that were hidden
564 (mapcar (function (lambda (dir)
565 (if (dired-goto-subdir dir
)
566 (dired-hide-subdir 1))))
568 ;; outside of the let scope
569 ;;; Might as well not override the user if the user changed this.
570 ;;; (setq buffer-read-only t)
573 ;; Subroutines of dired-revert
574 ;; Some of these are also used when inserting subdirs.
576 (defun dired-remember-marks (beg end
)
577 ;; Return alist of files and their marks, from BEG to END.
578 (if selective-display
; must unhide to make this work.
579 (let (buffer-read-only)
580 (subst-char-in-region beg end ?
\r ?
\n)))
584 (while (re-search-forward dired-re-mark end t
)
585 (if (setq fil
(dired-get-filename nil t
))
586 (setq chr
(preceding-char)
587 alist
(cons (cons fil chr
) alist
)))))
590 ;; Mark all files remembered in ALIST.
591 ;; Each element of ALIST looks like (FILE . MARKERCHAR).
592 (defun dired-mark-remembered (alist)
595 (setq elt
(car alist
)
599 (if (dired-goto-file fil
)
605 ;; Return a list of names of subdirs currently hidden.
606 (defun dired-remember-hidden ()
607 (let ((l dired-subdir-alist
) dir pos result
)
609 (setq dir
(car (car l
))
613 (skip-chars-forward "^\r\n")
614 (if (eq (following-char) ?
\r)
615 (setq result
(cons dir result
))))
618 ;; Try to insert all subdirs that were displayed before,
619 ;; according to the former subdir alist OLD-SUBDIR-ALIST.
620 (defun dired-insert-old-subdirs (old-subdir-alist)
621 (or (string-match "R" dired-actual-switches
)
623 (while old-subdir-alist
624 (setq elt
(car old-subdir-alist
)
625 old-subdir-alist
(cdr old-subdir-alist
)
630 (dired-insert-subdir dir
))
633 ;; Remove directory DIR from any directory cache.
634 (defun dired-uncache (dir)
635 (let ((handler (find-file-name-handler dir
)))
637 (funcall handler
'dired-uncache dir
))))
639 ;; dired mode key bindings and initialization
641 (defvar dired-mode-map nil
"Local keymap for dired-mode buffers.")
644 ;; Force `f' rather than `e' in the mode doc:
645 (defalias 'dired-advertised-find-file
'dired-find-file
)
646 ;; This looks ugly when substitute-command-keys uses C-d instead d:
647 ;; (define-key dired-mode-map "\C-d" 'dired-flag-file-deletion)
649 (setq dired-mode-map
(make-keymap))
650 (suppress-keymap dired-mode-map
)
651 ;; Commands to mark or flag certain categories of files
652 (define-key dired-mode-map
"#" 'dired-flag-auto-save-files
)
653 (define-key dired-mode-map
"*" 'dired-mark-executables
)
654 (define-key dired-mode-map
"." 'dired-clean-directory
)
655 (define-key dired-mode-map
"/" 'dired-mark-directories
)
656 (define-key dired-mode-map
"@" 'dired-mark-symlinks
)
657 (define-key dired-mode-map
"~" 'dired-flag-backup-files
)
658 ;; Upper case keys (except !) for operating on the marked files
659 (define-key dired-mode-map
"C" 'dired-do-copy
)
660 (define-key dired-mode-map
"B" 'dired-do-byte-compile
)
661 (define-key dired-mode-map
"D" 'dired-do-delete
)
662 (define-key dired-mode-map
"G" 'dired-do-chgrp
)
663 (define-key dired-mode-map
"H" 'dired-do-hardlink
)
664 (define-key dired-mode-map
"L" 'dired-do-load
)
665 (define-key dired-mode-map
"M" 'dired-do-chmod
)
666 (define-key dired-mode-map
"O" 'dired-do-chown
)
667 (define-key dired-mode-map
"P" 'dired-do-print
)
668 (define-key dired-mode-map
"R" 'dired-do-rename
)
669 (define-key dired-mode-map
"S" 'dired-do-symlink
)
670 (define-key dired-mode-map
"X" 'dired-do-shell-command
)
671 (define-key dired-mode-map
"Z" 'dired-do-compress
)
672 (define-key dired-mode-map
"!" 'dired-do-shell-command
)
673 ;; Comparison commands
674 (define-key dired-mode-map
"=" 'dired-diff
)
675 (define-key dired-mode-map
"\M-=" 'dired-backup-diff
)
676 ;; Tree Dired commands
677 (define-key dired-mode-map
"\M-\C-?" 'dired-unmark-all-files
)
678 (define-key dired-mode-map
"\M-\C-d" 'dired-tree-down
)
679 (define-key dired-mode-map
"\M-\C-u" 'dired-tree-up
)
680 (define-key dired-mode-map
"\M-\C-n" 'dired-next-subdir
)
681 (define-key dired-mode-map
"\M-\C-p" 'dired-prev-subdir
)
682 ;; move to marked files
683 (define-key dired-mode-map
"\M-{" 'dired-prev-marked-file
)
684 (define-key dired-mode-map
"\M-}" 'dired-next-marked-file
)
685 ;; Make all regexp commands share a `%' prefix:
686 (defalias 'dired-regexp-prefix
(make-sparse-keymap))
687 (define-key dired-mode-map
"%" 'dired-regexp-prefix
)
688 (define-key dired-mode-map
"%u" 'dired-upcase
)
689 (define-key dired-mode-map
"%l" 'dired-downcase
)
690 (define-key dired-mode-map
"%d" 'dired-flag-files-regexp
)
691 (define-key dired-mode-map
"%m" 'dired-mark-files-regexp
)
692 (define-key dired-mode-map
"%r" 'dired-do-rename-regexp
)
693 (define-key dired-mode-map
"%C" 'dired-do-copy-regexp
)
694 (define-key dired-mode-map
"%H" 'dired-do-hardlink-regexp
)
695 (define-key dired-mode-map
"%R" 'dired-do-rename-regexp
)
696 (define-key dired-mode-map
"%S" 'dired-do-symlink-regexp
)
697 ;; Lower keys for commands not operating on all the marked files
698 (define-key dired-mode-map
"c" 'dired-change-marks
)
699 (define-key dired-mode-map
"d" 'dired-flag-file-deletion
)
700 (define-key dired-mode-map
"e" 'dired-find-file
)
701 (define-key dired-mode-map
"f" 'dired-advertised-find-file
)
702 (define-key dired-mode-map
"g" 'revert-buffer
)
703 (define-key dired-mode-map
"h" 'describe-mode
)
704 (define-key dired-mode-map
"i" 'dired-maybe-insert-subdir
)
705 (define-key dired-mode-map
"k" 'dired-do-kill-lines
)
706 (define-key dired-mode-map
"l" 'dired-do-redisplay
)
707 (define-key dired-mode-map
"m" 'dired-mark
)
708 (define-key dired-mode-map
"n" 'dired-next-line
)
709 (define-key dired-mode-map
"o" 'dired-find-file-other-window
)
710 (define-key dired-mode-map
"\C-o" 'dired-display-file
)
711 (define-key dired-mode-map
"p" 'dired-previous-line
)
712 (define-key dired-mode-map
"q" 'dired-quit
)
713 (define-key dired-mode-map
"s" 'dired-sort-toggle-or-edit
)
714 (define-key dired-mode-map
"u" 'dired-unmark
)
715 (define-key dired-mode-map
"v" 'dired-view-file
)
716 (define-key dired-mode-map
"x" 'dired-do-flagged-delete
)
717 (define-key dired-mode-map
"+" 'dired-create-directory
)
719 (define-key dired-mode-map
"<" 'dired-prev-dirline
)
720 (define-key dired-mode-map
">" 'dired-next-dirline
)
721 (define-key dired-mode-map
"^" 'dired-up-directory
)
722 (define-key dired-mode-map
" " 'dired-next-line
)
723 (define-key dired-mode-map
"\C-n" 'dired-next-line
)
724 (define-key dired-mode-map
"\C-p" 'dired-previous-line
)
726 (define-key dired-mode-map
"$" 'dired-hide-subdir
)
727 (define-key dired-mode-map
"\M-$" 'dired-hide-all
)
729 (define-key dired-mode-map
"?" 'dired-summary
)
730 (define-key dired-mode-map
"\177" 'dired-unmark-backward
)
731 (define-key dired-mode-map
"\C-_" 'dired-undo
)
732 (define-key dired-mode-map
"\C-xu" 'dired-undo
)
735 ;; Make menu bar items.
737 ;; Get rid of the Edit menu bar item to save space.
738 (define-key dired-mode-map
[menu-bar edit
] 'undefined
)
740 (define-key dired-mode-map
[menu-bar subdir
]
741 (cons "Subdir" (make-sparse-keymap "Subdir")))
743 (define-key dired-mode-map
[menu-bar subdir hide-all
]
744 '("Hide All" . dired-hide-all
))
745 (define-key dired-mode-map
[menu-bar subdir hide-subdir
]
746 '("Hide Subdir" . dired-hide-subdir
))
747 (define-key dired-mode-map
[menu-bar subdir tree-down
]
748 '("Tree Down" . dired-tree-down
))
749 (define-key dired-mode-map
[menu-bar subdir tree-up
]
750 '("Tree Up" . dired-tree-up
))
751 (define-key dired-mode-map
[menu-bar subdir up
]
752 '("Up Directory" . dired-up-directory
))
753 (define-key dired-mode-map
[menu-bar subdir prev-subdir
]
754 '("Prev Subdir" . dired-prev-subdir
))
755 (define-key dired-mode-map
[menu-bar subdir next-subdir
]
756 '("Next Subdir" . dired-next-subdir
))
757 (define-key dired-mode-map
[menu-bar subdir prev-dirline
]
758 '("Prev Dirline" . dired-prev-dirline
))
759 (define-key dired-mode-map
[menu-bar subdir next-dirline
]
760 '("Next Dirline" . dired-next-dirline
))
761 (define-key dired-mode-map
[menu-bar subdir insert
]
762 '("Insert This Subdir" . dired-maybe-insert-subdir
))
764 (define-key dired-mode-map
[menu-bar immediate
]
765 (cons "Immediate" (make-sparse-keymap "Immediate")))
767 (define-key dired-mode-map
[menu-bar immediate backup-diff
]
768 '("Compare with Backup" . dired-backup-diff
))
769 (define-key dired-mode-map
[menu-bar immediate diff
]
770 '("Diff" . dired-diff
))
771 (define-key dired-mode-map
[menu-bar immediate view
]
772 '("View This File" . dired-view-file
))
773 (define-key dired-mode-map
[menu-bar immediate display
]
774 '("Display in Other Window" . dired-display-file
))
775 (define-key dired-mode-map
[menu-bar immediate find-file-other-window
]
776 '("Find in Other Window" . dired-find-file-other-window
))
777 (define-key dired-mode-map
[menu-bar immediate find-file
]
778 '("Find This File" . dired-find-file
))
779 (define-key dired-mode-map
[menu-bar immediate create-directory
]
780 '("Create Directory..." . dired-create-directory
))
782 (define-key dired-mode-map
[menu-bar regexp
]
783 (cons "Regexp" (make-sparse-keymap "Regexp")))
785 (define-key dired-mode-map
[menu-bar regexp downcase
]
786 '("Downcase" . dired-downcase
))
787 (define-key dired-mode-map
[menu-bar regexp upcase
]
788 '("Upcase" . dired-upcase
))
789 (define-key dired-mode-map
[menu-bar regexp hardlink
]
790 '("Hardlink..." . dired-do-hardlink-regexp
))
791 (define-key dired-mode-map
[menu-bar regexp symlink
]
792 '("Symlink..." . dired-do-symlink-regexp
))
793 (define-key dired-mode-map
[menu-bar regexp rename
]
794 '("Rename..." . dired-do-rename-regexp
))
795 (define-key dired-mode-map
[menu-bar regexp copy
]
796 '("Copy..." . dired-do-copy-regexp
))
797 (define-key dired-mode-map
[menu-bar regexp flag
]
798 '("Flag..." . dired-flag-files-regexp
))
799 (define-key dired-mode-map
[menu-bar regexp mark
]
800 '("Mark..." . dired-mark-files-regexp
))
802 (define-key dired-mode-map
[menu-bar mark
]
803 (cons "Mark" (make-sparse-keymap "Mark")))
805 (define-key dired-mode-map
[menu-bar mark prev
]
806 '("Previous Marked" . dired-prev-marked-file
))
807 (define-key dired-mode-map
[menu-bar mark next
]
808 '("Next Marked" . dired-next-marked-file
))
809 (define-key dired-mode-map
[menu-bar mark marks
]
810 '("Change Marks..." . dired-change-marks
))
811 (define-key dired-mode-map
[menu-bar mark unmark-all
]
812 '("Unmark All" . dired-unmark-all-files-no-query
))
813 (define-key dired-mode-map
[menu-bar mark symlinks
]
814 '("Mark Symlinks" . dired-mark-symlinks
))
815 (define-key dired-mode-map
[menu-bar mark directories
]
816 '("Mark Directories" . dired-mark-directories
))
817 (define-key dired-mode-map
[menu-bar mark directory
]
818 '("Mark Old Backups" . dired-clean-directory
))
819 (define-key dired-mode-map
[menu-bar mark executables
]
820 '("Mark Executables" . dired-mark-executables
))
821 (define-key dired-mode-map
[menu-bar mark backup-files
]
822 '("Flag Backup Files" . dired-flag-backup-files
))
823 (define-key dired-mode-map
[menu-bar mark auto-save-files
]
824 '("Flag Auto-save Files" . dired-flag-auto-save-files
))
825 (define-key dired-mode-map
[menu-bar mark deletion
]
826 '("Flag" . dired-flag-file-deletion
))
827 (define-key dired-mode-map
[menu-bar mark unmark
]
828 '("Unmark" . dired-unmark
))
829 (define-key dired-mode-map
[menu-bar mark mark
]
830 '("Mark" . dired-mark
))
832 (define-key dired-mode-map
[menu-bar operate
]
833 (cons "Operate" (make-sparse-keymap "Operate")))
835 (define-key dired-mode-map
[menu-bar operate chown
]
836 '("Change Owner..." . dired-do-chown
))
837 (define-key dired-mode-map
[menu-bar operate chgrp
]
838 '("Change Group..." . dired-do-chgrp
))
839 (define-key dired-mode-map
[menu-bar operate chmod
]
840 '("Change Mode..." . dired-do-chmod
))
841 (define-key dired-mode-map
[menu-bar operate load
]
842 '("Load" . dired-do-load
))
843 (define-key dired-mode-map
[menu-bar operate compile
]
844 '("Byte-compile" . dired-do-byte-compile
))
845 (define-key dired-mode-map
[menu-bar operate compress
]
846 '("Compress" . dired-do-compress
))
847 (define-key dired-mode-map
[menu-bar operate print
]
848 '("Print" . dired-do-print
))
849 (define-key dired-mode-map
[menu-bar operate hardlink
]
850 '("Hardlink to..." . dired-do-hardlink
))
851 (define-key dired-mode-map
[menu-bar operate symlink
]
852 '("Symlink to..." . dired-do-symlink
))
853 (define-key dired-mode-map
[menu-bar operate command
]
854 '("Shell Command..." . dired-do-shell-command
))
855 (define-key dired-mode-map
[menu-bar operate delete
]
856 '("Delete" . dired-do-delete
))
857 (define-key dired-mode-map
[menu-bar operate rename
]
858 '("Rename to..." . dired-do-rename
))
859 (define-key dired-mode-map
[menu-bar operate copy
]
860 '("Copy to..." . dired-do-copy
))
862 ;; Dired mode is suitable only for specially formatted data.
863 (put 'dired-mode
'mode-class
'special
)
865 (defun dired-mode (&optional dirname switches
)
867 Mode for \"editing\" directory listings.
868 In dired, you are \"editing\" a list of the files in a directory and
869 \(optionally) its subdirectories, in the format of `ls -lR'.
870 Each directory is a page: use \\[backward-page] and \\[forward-page] to move pagewise.
871 \"Editing\" means that you can run shell commands on files, visit,
872 compress, load or byte-compile them, change their file attributes
873 and insert subdirectories into the same buffer. You can \"mark\"
874 files for later commands or \"flag\" them for deletion, either file
875 by file or all files matching certain criteria.
876 You can move using the usual cursor motion commands.\\<dired-mode-map>
877 Letters no longer insert themselves. Digits are prefix arguments.
878 Instead, type \\[dired-flag-file-deletion] to flag a file for Deletion.
879 Type \\[dired-mark] to Mark a file or subdirectory for later commands.
880 Most commands operate on the marked files and use the current file
881 if no files are marked. Use a numeric prefix argument to operate on
882 the next ARG (or previous -ARG if ARG<0) files, or just `1'
883 to operate on the current file only. Prefix arguments override marks.
884 Mark-using commands display a list of failures afterwards. Type \\[dired-summary]
885 to see why something went wrong.
886 Type \\[dired-unmark] to Unmark a file or all files of a subdirectory.
887 Type \\[dired-unmark-backward] to back up one line and unflag.
888 Type \\[dired-do-flagged-delete] to eXecute the deletions requested.
889 Type \\[dired-advertised-find-file] to Find the current line's file
890 (or dired it in another buffer, if it is a directory).
891 Type \\[dired-find-file-other-window] to find file or dired directory in Other window.
892 Type \\[dired-maybe-insert-subdir] to Insert a subdirectory in this buffer.
893 Type \\[dired-do-rename] to Rename a file or move the marked files to another directory.
894 Type \\[dired-do-copy] to Copy files.
895 Type \\[dired-sort-toggle-or-edit] to toggle sorting by name/date or change the `ls' switches.
896 Type \\[revert-buffer] to read all currently expanded directories again.
897 This retains all marks and hides subdirs again that were hidden before.
898 SPC and DEL can be used to move down and up by lines.
900 If dired ever gets confused, you can either type \\[revert-buffer] \
902 directories again, type \\[dired-do-redisplay] \
903 to relist a single or the marked files or a
904 subdirectory, or type \\[dired-build-subdir-alist] to parse the buffer
905 again for the directory tree.
907 Customization variables (rename this buffer and type \\[describe-variable] on each line
910 dired-listing-switches
911 dired-trivial-filenames
915 dired-keep-marker-rename
916 dired-keep-marker-copy
917 dired-keep-marker-hardlink
918 dired-keep-marker-symlink
920 Hooks (use \\[describe-variable] to see their documentation):
922 dired-before-readin-hook
923 dired-after-readin-hook
929 ;; Not to be called interactively (e.g. dired-directory will be set
930 ;; to default-directory, which is wrong with wildcards).
931 (kill-all-local-variables)
932 (use-local-map dired-mode-map
)
933 (dired-advertise) ; default-directory is already set
934 (setq major-mode
'dired-mode
936 ;; case-fold-search nil
938 selective-display t
; for subdirectory hiding
939 mode-line-buffer-identification
'("Dired: %17b"))
940 (set (make-local-variable 'revert-buffer-function
)
941 (function dired-revert
))
942 (set (make-local-variable 'page-delimiter
)
944 (set (make-local-variable 'dired-directory
)
945 (or dirname default-directory
))
946 ;; list-buffers uses this to display the dir being edited in this buffer.
947 (set (make-local-variable 'list-buffers-directory
)
949 (set (make-local-variable 'dired-actual-switches
)
950 (or switches dired-listing-switches
))
951 (dired-sort-other dired-actual-switches t
)
952 (run-hooks 'dired-mode-hook
))
954 ;; Idiosyncratic dired commands that don't deal with marks.
957 "Bury the current dired buffer."
961 (defun dired-summary ()
962 "Summarize basic Dired commands and show recent Dired errors."
965 ;>> this should check the key-bindings and use substitute-command-keys if non-standard
967 "d-elete, u-ndelete, x-punge, f-ind, o-ther window, R-ename, C-opy, h-elp"))
970 "Undo in a dired buffer.
971 This doesn't recover lost files, it just undoes changes in the buffer itself.
972 You can use it to recover marks, killed lines or subdirs.
973 In the latter case, you have to do \\[dired-build-subdir-alist] to
974 parse the buffer again."
976 (let (buffer-read-only)
979 (defun dired-next-line (arg)
980 "Move down lines then position at filename.
981 Optional prefix ARG says how many lines to move; default is one line."
984 (dired-move-to-filename))
986 (defun dired-previous-line (arg)
987 "Move up lines then position at filename.
988 Optional prefix ARG says how many lines to move; default is one line."
991 (dired-move-to-filename))
993 (defun dired-next-dirline (arg &optional opoint
)
994 "Goto ARG'th next directory file line."
996 (or opoint
(setq opoint
(point)))
998 (re-search-forward dired-re-dir nil t arg
)
1000 (re-search-backward dired-re-dir nil t
(- arg
)))
1001 (dired-move-to-filename) ; user may type `i' or `f'
1003 (error "No more subdirectories")))
1005 (defun dired-prev-dirline (arg)
1006 "Goto ARG'th previous directory file line."
1008 (dired-next-dirline (- arg
)))
1010 (defun dired-up-directory ()
1011 "Run dired on parent directory of current directory.
1012 Find the parent directory either in this buffer or another buffer.
1013 Creates a buffer if necessary."
1015 (let* ((dir (dired-current-directory))
1016 (up (file-name-directory (directory-file-name dir
))))
1017 (or (dired-goto-file (directory-file-name dir
))
1018 ;; Only try dired-goto-subdir if buffer has more than one dir.
1019 (and (cdr dired-subdir-alist
)
1020 (dired-goto-subdir up
))
1024 (dired-goto-file dir
)))))
1026 (defun dired-find-file ()
1027 "In dired, visit the file or directory named on this line."
1029 (find-file (file-name-sans-versions (dired-get-filename) t
)))
1031 (defun dired-view-file ()
1032 "In dired, examine a file in view mode, returning to dired when done.
1033 When file is a directory, show it in this buffer if it is inserted;
1034 otherwise, display it in another buffer."
1036 (if (file-directory-p (dired-get-filename))
1037 (or (and (cdr dired-subdir-alist
)
1038 (dired-goto-subdir (dired-get-filename)))
1039 (dired (dired-get-filename)))
1040 (view-file (dired-get-filename))))
1042 (defun dired-find-file-other-window ()
1043 "In dired, visit this file or directory in another window."
1045 (find-file-other-window (file-name-sans-versions (dired-get-filename) t
)))
1047 (defun dired-display-file ()
1048 "In dired, display this file or directory in another window."
1050 (let ((file (file-name-sans-versions (dired-get-filename) t
)))
1051 (display-buffer (find-file-noselect file
))))
1053 ;;; Functions for extracting and manipulating file names in dired buffers.
1055 (defun dired-get-filename (&optional localp no-error-if-not-filep
)
1056 "In dired, return name of file mentioned on this line.
1057 Value returned normally includes the directory name.
1058 Optional arg LOCALP with value `no-dir' means don't include directory
1059 name in result. A value of t means construct name relative to
1060 `default-directory', which still may contain slashes if in a subdirectory.
1061 Optional arg NO-ERROR-IF-NOT-FILEP means return nil if no filename on
1062 this line, otherwise an error occurs."
1063 (let (case-fold-search file p1 p2
)
1065 (if (setq p1
(dired-move-to-filename (not no-error-if-not-filep
)))
1066 (setq p2
(dired-move-to-end-of-filename no-error-if-not-filep
))))
1067 ;; nil if no file on this line, but no-error-if-not-filep is t:
1068 (if (setq file
(and p1 p2
(buffer-substring p1 p2
)))
1069 ;; Check if ls quoted the names, and unquote them.
1070 ;; Using read to unquote is much faster than substituting
1071 ;; \007 (4 chars) -> ^G (1 char) etc. in a lisp loop.
1072 (cond ((string-match "b" dired-actual-switches
) ; System V ls
1073 ;; This case is about 20% slower than without -b.
1077 ;; some ls -b don't escape quotes, argh!
1078 ;; This is not needed for GNU ls, though.
1079 (or (dired-string-replace-match
1080 "\\([^\\]\\)\"" file
"\\1\\\\\"")
1083 ;; If you do this, update dired-insert-subdir-validate too
1084 ;; ((string-match "Q" dired-actual-switches) ; GNU ls
1085 ;; (setq file (read file)))
1087 (if (eq localp
'no-dir
)
1089 (and file
(concat (dired-current-directory localp
) file
)))))
1091 (defun dired-make-absolute (file &optional dir
)
1092 ;;"Convert FILE (a pathname relative to DIR) to an absolute pathname."
1093 ;; We can't always use expand-file-name as this would get rid of `.'
1094 ;; or expand in / instead default-directory if DIR=="".
1095 ;; This should be good enough for ange-ftp, but might easily be
1096 ;; redefined (for VMS?).
1097 ;; It should be reasonably fast, though, as it is called in
1098 ;; dired-get-filename.
1099 (concat (or dir default-directory
) file
))
1101 (defun dired-make-relative (file &optional dir no-error
)
1102 ;;"Convert FILE (an absolute pathname) to a pathname relative to DIR.
1103 ;; Else error (unless NO-ERROR is non-nil, then FILE is returned unchanged)
1104 ;;DIR defaults to default-directory."
1105 ;; DIR must be file-name-as-directory, as with all directory args in
1107 (or dir
(setq dir default-directory
))
1108 ;; This case comes into play if default-directory is set to
1110 (if (and (> (length dir
) 0) (= (aref dir
0) ?~
))
1111 (setq dir
(expand-file-name dir
)))
1112 (if (string-match (concat "^" (regexp-quote dir
)) file
)
1113 (substring file
(match-end 0))
1116 (error "%s: not in directory tree growing at %s" file dir
))))
1118 ;;; Functions for finding the file name in a dired buffer line.
1120 (defvar dired-move-to-filename-regexp
1121 "\\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\)[ ]+[0-9]+"
1122 "Regular expression to match a month abbreviation followed by a number.")
1124 ;; Move to first char of filename on this line.
1125 ;; Returns position (point) or nil if no filename on this line."
1126 (defun dired-move-to-filename (&optional raise-error eol
)
1127 ;; This is the UNIX version.
1128 (or eol
(setq eol
(progn (end-of-line) (point))))
1130 (if (re-search-forward
1131 dired-move-to-filename-regexp
1134 (skip-chars-forward " ") ; there is one SPC after day of month
1135 (skip-chars-forward "^ " eol
) ; move after time of day (or year)
1136 (skip-chars-forward " " eol
) ; there is space before the file name
1137 ;; Actually, if the year instead of clock time is displayed,
1138 ;; there are (only for some ls programs?) two spaces instead
1139 ;; of one before the name.
1140 ;; If we could depend on ls inserting exactly one SPC we
1141 ;; would not bomb on names _starting_ with SPC.
1144 (error "No file on this line")
1147 (defun dired-move-to-end-of-filename (&optional no-error
)
1148 ;; Assumes point is at beginning of filename,
1149 ;; thus the rwx bit re-search-backward below will succeed in *this*
1150 ;; line if at all. So, it should be called only after
1151 ;; (dired-move-to-filename t).
1152 ;; On failure, signals an error (with non-nil NO-ERROR just returns nil).
1153 ;; This is the UNIX version.
1154 (let (opoint file-type executable symlink hidden case-fold-search used-F eol
)
1155 ;; case-fold-search is nil now, so we can test for capital F:
1156 (setq used-F
(string-match "F" dired-actual-switches
)
1158 eol
(save-excursion (end-of-line) (point))
1159 hidden
(and selective-display
1160 (save-excursion (search-forward "\r" eol t
))))
1163 (save-excursion;; Find out what kind of file this is:
1164 ;; Restrict perm bits to be non-blank,
1165 ;; otherwise this matches one char to early (looking backward):
1166 ;; "l---------" (some systems make symlinks that way)
1167 ;; "----------" (plain file with zero perms)
1168 (if (re-search-backward
1169 "\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)[-r][-w]\\([^ ]\\)"
1171 (setq file-type
(char-after (match-beginning 1))
1172 symlink
(eq file-type ?l
)
1173 ;; Only with -F we need to know whether it's an executable
1177 "[xst]";; execute bit set anywhere?
1179 (buffer-substring (match-beginning 2)
1181 (buffer-substring (match-beginning 3)
1183 (buffer-substring (match-beginning 4)
1185 (or no-error
(error "No file on this line"))))
1186 ;; Move point to end of name:
1188 (if (search-forward " ->" eol t
)
1192 dired-ls-F-marks-symlinks
1193 (eq (preceding-char) ?
@);; did ls really mark the link?
1194 (forward-char -
1))))
1195 (goto-char eol
);; else not a symbolic link
1196 ;; ls -lF marks dirs, sockets and executables with exactly one
1197 ;; trailing character. (Executable bits on symlinks ain't mean
1198 ;; a thing, even to ls, but we know it's not a symlink.)
1200 (or (memq file-type
'(?d ?s
))
1202 (forward-char -
1))))
1204 (not (eq opoint
(point)))
1206 (substitute-command-keys
1207 "File line is hidden, type \\[dired-hide-subdir] to unhide")
1208 "No file on this line")))
1209 (if (eq opoint
(point))
1214 ;; Keeping Dired buffers in sync with the filesystem and with each other
1216 (defvar dired-buffers nil
1217 ;; Enlarged by dired-advertise
1218 ;; Queried by function dired-buffers-for-dir. When this detects a
1219 ;; killed buffer, it is removed from this list.
1220 "Alist of directories and their associated dired buffers.")
1222 (defun dired-buffers-for-dir (dir)
1223 ;; Return a list of buffers that dired DIR (top level or in-situ subdir).
1224 ;; The list is in reverse order of buffer creation, most recent last.
1225 ;; As a side effect, killed dired buffers for DIR are removed from
1227 (setq dir
(file-name-as-directory dir
))
1228 (let ((alist dired-buffers
) result elt
)
1230 (setq elt
(car alist
))
1231 (if (dired-in-this-tree dir
(car elt
))
1232 (let ((buf (cdr elt
)))
1233 (if (buffer-name buf
)
1234 (if (assoc dir
(save-excursion
1236 dired-subdir-alist
))
1237 (setq result
(cons buf result
)))
1238 ;; else buffer is killed - clean up:
1239 (setq dired-buffers
(delq elt dired-buffers
)))))
1240 (setq alist
(cdr alist
)))
1243 (defun dired-advertise ()
1244 ;;"Advertise in variable `dired-buffers' that we dired `default-directory'."
1245 ;; With wildcards we actually advertise too much.
1246 (if (memq (current-buffer) (dired-buffers-for-dir default-directory
))
1247 t
; we have already advertised ourselves
1249 (cons (cons default-directory
(current-buffer))
1252 (defun dired-unadvertise (dir)
1253 ;; Remove DIR from the buffer alist in variable dired-buffers.
1254 ;; This has the effect of removing any buffer whose main directory is DIR.
1255 ;; It does not affect buffers in which DIR is a subdir.
1256 ;; Removing is also done as a side-effect in dired-buffer-for-dir.
1258 (delq (assoc dir dired-buffers
) dired-buffers
)))
1262 ;;; utility functions
1264 (defun dired-in-this-tree (file dir
)
1265 ;;"Is FILE part of the directory tree starting at DIR?"
1266 (let (case-fold-search)
1267 (string-match (concat "^" (regexp-quote (expand-file-name dir
))) file
)))
1269 (defun dired-normalize-subdir (dir)
1270 ;; Prepend default-directory to DIR if relative path name.
1271 ;; dired-get-filename must be able to make a valid filename from a
1272 ;; file and its directory DIR.
1273 (file-name-as-directory
1274 (if (file-name-absolute-p dir
)
1276 (expand-file-name dir default-directory
))))
1278 (defun dired-get-subdir ()
1279 ;;"Return the subdir name on this line, or nil if not on a headerline."
1280 ;; Look up in the alist whether this is a headerline.
1282 (let ((cur-dir (dired-current-directory)))
1283 (beginning-of-line) ; alist stores b-o-l positions
1284 (and (zerop (- (point)
1285 (dired-get-subdir-min (assoc cur-dir
1286 dired-subdir-alist
))))
1289 ;(defun dired-get-subdir-min (elt)
1291 ;; can't use macro, must be redefinable for other alist format in dired-nstd.
1292 (defalias 'dired-get-subdir-min
'cdr
)
1294 (defun dired-get-subdir-max (elt)
1296 (goto-char (dired-get-subdir-min elt
))
1297 (dired-subdir-max)))
1299 (defun dired-clear-alist ()
1300 (while dired-subdir-alist
1301 (set-marker (dired-get-subdir-min (car dired-subdir-alist
)) nil
)
1302 (setq dired-subdir-alist
(cdr dired-subdir-alist
))))
1304 (defun dired-subdir-index (dir)
1305 ;; Return an index into alist for use with nth
1306 ;; for the sake of subdir moving commands.
1307 (let (found (index 0) (alist dired-subdir-alist
))
1309 (if (string= dir
(car (car alist
)))
1310 (setq alist nil found t
)
1311 (setq alist
(cdr alist
) index
(1+ index
))))
1312 (if found index nil
)))
1314 (defun dired-next-subdir (arg &optional no-error-if-not-found no-skip
)
1315 "Go to next subdirectory, regardless of level."
1316 ;; Use 0 arg to go to this directory's header line.
1317 ;; NO-SKIP prevents moving to end of header line, returning whatever
1318 ;; position was found in dired-subdir-alist.
1320 (let ((this-dir (dired-current-directory))
1322 ;; nth with negative arg does not return nil but the first element
1323 (setq index
(- (dired-subdir-index this-dir
) arg
))
1324 (setq pos
(if (>= index
0)
1325 (dired-get-subdir-min (nth index dired-subdir-alist
))))
1329 (or no-skip
(skip-chars-forward "^\n\r"))
1331 (if no-error-if-not-found
1332 nil
; return nil if not found
1333 (error "%s directory" (if (> arg
0) "Last" "First"))))))
1335 (defun dired-build-subdir-alist ()
1336 "Build `dired-subdir-alist' by parsing the buffer.
1337 Returns the new value of the alist."
1342 (buffer-read-only nil
)
1344 (goto-char (point-min))
1345 (setq dired-subdir-alist nil
)
1346 (while (re-search-forward dired-subdir-regexp nil t
)
1348 (goto-char (match-beginning 1))
1350 (expand-file-name (buffer-substring (point) (match-end 1))))
1351 (delete-region (point) (match-end 1))
1352 (insert new-dir-name
))
1353 (setq count
(1+ count
))
1354 (dired-alist-add-1 new-dir-name
1355 ;; Place a sub directory boundary between lines.
1357 (goto-char (match-beginning 0))
1361 (message "Buffer includes %d directories" count
))
1362 ;; We don't need to sort it because it is in buffer order per
1363 ;; constructionem. Return new alist:
1364 dired-subdir-alist
)))
1366 (defun dired-alist-add-1 (dir new-marker
)
1367 ;; Add new DIR at NEW-MARKER. Don't sort.
1368 (setq dired-subdir-alist
1369 (cons (cons (dired-normalize-subdir dir
) new-marker
)
1370 dired-subdir-alist
)))
1372 (defun dired-goto-next-nontrivial-file ()
1373 ;; Position point on first nontrivial file after point.
1374 (dired-goto-next-file);; so there is a file to compare with
1375 (if (stringp dired-trivial-filenames
)
1376 (while (and (not (eobp))
1377 (string-match dired-trivial-filenames
1378 (file-name-nondirectory
1379 (or (dired-get-filename nil t
) ""))))
1381 (dired-move-to-filename))))
1383 (defun dired-goto-next-file ()
1384 (let ((max (1- (dired-subdir-max))))
1385 (while (and (not (dired-move-to-filename)) (< (point) max
))
1388 (defun dired-goto-file (file)
1389 "Go to file line of FILE in this dired buffer."
1390 ;; Return value of point on success, else nil.
1391 ;; FILE must be an absolute pathname.
1392 ;; Loses if FILE contains control chars like "\007" for which ls
1393 ;; either inserts "?" or "\\007" into the buffer, so we won't find
1394 ;; it in the buffer.
1396 (prog1 ; let push-mark display its message
1397 (list (expand-file-name
1398 (read-file-name "Goto file: "
1399 (dired-current-directory))))
1401 (setq file
(directory-file-name file
)) ; does no harm if no directory
1402 (let (found case-fold-search dir
)
1403 (setq dir
(or (file-name-directory file
)
1404 (error "Need absolute pathname for %s" file
)))
1406 ;; The hair here is to get the result of dired-goto-subdir
1407 ;; without really calling it if we don't have any subdirs.
1408 (if (if (string= dir
(expand-file-name default-directory
))
1409 (goto-char (point-min))
1410 (and (cdr dired-subdir-alist
)
1411 (dired-goto-subdir dir
)))
1412 (let ((base (file-name-nondirectory file
))
1413 (boundary (dired-subdir-max)))
1414 (while (and (not found
)
1415 ;; filenames are preceded by SPC, this makes
1416 ;; the search faster (e.g. for the filename "-"!).
1417 (search-forward (concat " " base
) boundary
'move
))
1418 ;; Match could have BASE just as initial substring or
1419 ;; or in permission bits or date or
1420 ;; not be a proper filename at all:
1421 (if (equal base
(dired-get-filename 'no-dir t
))
1422 ;; Must move to filename since an (actually
1423 ;; correct) match could have been elsewhere on the
1424 ;; ;; line (e.g. "-" would match somewhere in the
1425 ;; permission bits).
1426 (setq found
(dired-move-to-filename))
1427 ;; If this isn't the right line, move forward to avoid
1428 ;; trying this line again.
1429 (forward-line 1))))))
1431 ;; return value of point (i.e., FOUND):
1432 (goto-char found
))))
1434 (defun dired-initial-position (dirname)
1435 ;; Where point should go in a new listing of DIRNAME.
1436 ;; Point assumed at beginning of new subdir line.
1437 ;; You may redefine this function as you wish, e.g. like in dired-x.el.
1439 (if dired-trivial-filenames
(dired-goto-next-nontrivial-file)))
1441 ;; These are hooks which make tree dired work.
1442 ;; They are in this file because other parts of dired need to call them.
1443 ;; But they don't call the rest of tree dired unless there are subdirs loaded.
1445 ;; This function is called for each retrieved filename.
1446 ;; It could stand to be faster, though it's mostly function call
1447 ;; overhead. Avoiding the function call seems to save about 10% in
1448 ;; dired-get-filename. Make it a defsubst?
1449 (defun dired-current-directory (&optional localp
)
1450 "Return the name of the subdirectory to which this line belongs.
1451 This returns a string with trailing slash, like `default-directory'.
1452 Optional argument means return a file name relative to `default-directory'."
1453 (let ((here (point))
1454 (alist (or dired-subdir-alist
1455 ;; probably because called in a non-dired buffer
1456 (error "No subdir-alist in %s" (current-buffer))))
1459 (setq elt
(car alist
)
1461 ;; use `<=' (not `<') as subdir line is part of subdir
1462 alist
(if (<= (dired-get-subdir-min elt
) here
)
1466 (dired-make-relative dir default-directory
)
1469 ;; Subdirs start at the beginning of their header lines and end just
1470 ;; before the beginning of the next header line (or end of buffer).
1472 (defun dired-subdir-max ()
1474 (if (or (null (cdr dired-subdir-alist
)) (not (dired-next-subdir 1 t t
)))
1480 (defun dired-do-flagged-delete ()
1481 "In dired, delete the files flagged for deletion."
1483 (let* ((dired-marker-char dired-del-marker
)
1484 (regexp (dired-marker-regexp))
1486 (if (save-excursion (goto-char (point-min))
1487 (re-search-forward regexp nil t
))
1488 (dired-internal-do-deletions
1489 ;; this can't move point since ARG is nil
1490 (dired-map-over-marks (cons (dired-get-filename) (point))
1493 (message "(No deletions requested)"))))
1495 (defun dired-do-delete (&optional arg
)
1496 "Delete all marked (or next ARG) files."
1497 ;; This is more consistent with the file marking feature than
1498 ;; dired-do-flagged-delete.
1500 (dired-internal-do-deletions
1501 ;; this may move point if ARG is an integer
1502 (dired-map-over-marks (cons (dired-get-filename) (point))
1506 (defvar dired-deletion-confirmer
'yes-or-no-p
) ; or y-or-n-p?
1508 (defun dired-internal-do-deletions (l arg
)
1509 ;; L is an alist of files to delete, with their buffer positions.
1510 ;; ARG is the prefix arg.
1511 ;; Filenames are absolute (VMS needs this for logical search paths).
1512 ;; (car L) *must* be the *last* (bottommost) file in the dired buffer.
1513 ;; That way as changes are made in the buffer they do not shift the
1514 ;; lines still to be changed, so the (point) values in L stay valid.
1515 ;; Also, for subdirs in natural order, a subdir's files are deleted
1516 ;; before the subdir itself - the other way around would not work.
1517 (let ((files (mapcar (function car
) l
))
1520 ;; canonicalize file list for pop up
1521 (setq files
(nreverse (mapcar (function dired-make-relative
) files
)))
1522 (if (dired-mark-pop-up
1523 " *Deletions*" 'delete files dired-deletion-confirmer
1524 (format "Delete %s " (dired-mark-prompt arg files
)))
1526 (let (failures);; files better be in reverse order for this loop!
1528 (goto-char (cdr (car l
)))
1529 (let (buffer-read-only)
1531 (let ((fn (car (car l
))))
1532 ;; This test is equivalent to
1533 ;; (and (file-directory-p fn) (not (file-symlink-p fn)))
1534 ;; but more efficient
1535 (if (eq t
(car (file-attributes fn
)))
1536 (delete-directory fn
)
1538 ;; if we get here, removing worked
1539 (setq succ
(1+ succ
))
1540 (message "%s of %s deletions" succ count
)
1541 (delete-region (progn (beginning-of-line) (point))
1542 (progn (forward-line 1) (point)))
1543 (dired-clean-up-after-deletion fn
))
1544 (error;; catch errors from failed deletions
1545 (dired-log "%s\n" err
)
1546 (setq failures
(cons (car (car l
)) failures
)))))
1549 (message "%d deletion%s done" count
(dired-plural-s count
))
1551 (format "%d of %d deletion%s failed"
1552 (length failures
) count
1553 (dired-plural-s count
))
1555 (message "(No deletions performed)")))
1556 (dired-move-to-filename))
1558 ;; This is a separate function for the sake of dired-x.el.
1559 (defun dired-clean-up-after-deletion (fn)
1560 ;; Clean up after a deleted file or directory FN.
1561 (save-excursion (and (cdr dired-subdir-alist
)
1562 (dired-goto-subdir fn
)
1563 (dired-kill-subdir))))
1567 (defun dired-marker-regexp ()
1568 (concat "^" (regexp-quote (char-to-string dired-marker-char
))))
1570 (defun dired-plural-s (count)
1571 (if (= 1 count
) "" "s"))
1573 (defun dired-mark-prompt (arg files
)
1574 ;; Return a string for use in a prompt, either the current file
1575 ;; name, or the marker and a count of marked files.
1576 (let ((count (length files
)))
1579 ;; more than 1 file:
1582 ;; Perhaps this is nicer, but it also takes more screen space:
1583 ;;(format "[%s %d files]" (if (> arg 0) "next" "previous")
1585 (format "[next %d files]" arg
)
1586 (format "%c [%d files]" dired-marker-char count
)))))
1588 (defun dired-pop-to-buffer (buf)
1589 ;; Pop up buffer BUF.
1590 ;; If dired-shrink-to-fit is t, make its window fit its contents.
1591 (if (not dired-shrink-to-fit
)
1592 (pop-to-buffer (get-buffer-create buf
))
1593 ;; let window shrink to fit:
1594 (let ((window (selected-window))
1596 (cond ;; if split-window-threshold is enabled, use the largest window
1597 ((and (> (window-height (setq w2
(get-largest-window)))
1598 split-height-threshold
)
1599 (= (frame-width) (window-width w2
)))
1601 ;; if the least-recently-used window is big enough, use it
1602 ((and (> (window-height (setq w2
(get-lru-window)))
1603 (* 2 window-min-height
))
1604 (= (frame-width) (window-width w2
)))
1608 (goto-char (point-max))
1609 (skip-chars-backward "\n\r\t ")
1610 (setq target-lines
(count-lines (point-min) (point)))
1611 ;; Don't forget to count the last line.
1613 (setq target-lines
(1+ target-lines
))))
1614 (if (<= (window-height window
) (* 2 window-min-height
))
1615 ;; At this point, every window on the frame is too small to split.
1616 (setq w2
(display-buffer buf
))
1617 (setq w2
(split-window window
1618 (max window-min-height
1619 (- (window-height window
)
1620 (1+ (max window-min-height target-lines
)))))))
1621 (set-window-buffer w2 buf
)
1622 (if (< (1- (window-height w2
)) target-lines
)
1625 (enlarge-window (- target-lines
(1- (window-height w2
))))))
1626 (set-window-start w2
1)
1629 (defvar dired-no-confirm nil
1630 ;; "If non-nil, list of symbols for commands dired should not confirm.
1631 ;;It can be a sublist of
1633 ;; '(byte-compile chgrp chmod chown compress copy delete hardlink load
1634 ;; move print shell symlink uncompress)"
1637 (defun dired-mark-pop-up (bufname op-symbol files function
&rest args
)
1638 ;;"Args BUFNAME OP-SYMBOL FILES FUNCTION &rest ARGS.
1639 ;;Return FUNCTION's result on ARGS after popping up a window (in a buffer
1640 ;;named BUFNAME, nil gives \" *Marked Files*\") showing the marked
1641 ;;files. Uses function `dired-pop-to-buffer' to do that.
1642 ;; FUNCTION should not manipulate files.
1643 ;; It should only read input (an argument or confirmation).
1644 ;;The window is not shown if there is just one file or
1645 ;; OP-SYMBOL is a member of the list in `dired-no-confirm'.
1646 ;;FILES is the list of marked files."
1647 (or bufname
(setq bufname
" *Marked Files*"))
1648 (if (or (memq op-symbol dired-no-confirm
)
1649 (= (length files
) 1))
1650 (apply function args
)
1652 (set-buffer (get-buffer-create bufname
))
1654 (dired-format-columns-of-files files
))
1655 (save-window-excursion
1656 (dired-pop-to-buffer bufname
)
1657 (apply function args
))))
1659 (defun dired-format-columns-of-files (files)
1660 ;; Files should be in forward order for this loop.
1661 ;; i.e., (car files) = first file in buffer.
1662 ;; Returns the number of lines used.
1663 (let* ((maxlen (+ 2 (apply 'max
(mapcar 'length files
))))
1664 (width (- (window-width (selected-window)) 2))
1665 (columns (max 1 (/ width maxlen
)))
1666 (nfiles (length files
))
1667 (rows (+ (/ nfiles columns
)
1668 (if (zerop (% nfiles columns
)) 0 1)))
1671 (setq files
(nconc (copy-sequence files
) ; fill up with empty fns
1672 (make-list (- (* columns rows
) nfiles
) "")))
1673 (setcdr (nthcdr (1- (length files
)) files
) files
) ; make circular
1675 (while (< i columns
)
1676 (indent-to (* i maxlen
))
1677 (insert (car files
))
1678 (setq files
(nthcdr rows files
)
1686 ;; Commands to mark or flag file(s) at or near current line.
1688 (defun dired-repeat-over-lines (arg function
)
1689 ;; This version skips non-file lines.
1690 (let ((pos (make-marker)))
1692 (while (and (> arg
0) (not (eobp)))
1695 (while (and (not (eobp)) (dired-between-files)) (forward-line 1))
1698 (move-marker pos
(1+ (point))))
1699 (save-excursion (funcall function
))
1700 ;; Advance to the next line--actually, to the line that *was* next.
1701 ;; (If FUNCTION inserted some new lines in between, skip them.)
1703 (while (and (< arg
0) (not (bobp)))
1706 (while (and (not (bobp)) (dired-between-files)) (forward-line -
1))
1708 (save-excursion (funcall function
)))
1709 (move-marker pos nil
)
1710 (dired-move-to-filename)))
1712 (defun dired-between-files ()
1713 ;; Point must be at beginning of line
1714 ;; Should be equivalent to (save-excursion (not (dired-move-to-filename)))
1715 ;; but is about 1.5..2.0 times as fast. (Actually that's not worth it)
1716 (or (looking-at "^$\\|^. *$\\|^. total\\|^. wildcard")
1717 (looking-at dired-subdir-regexp
)))
1719 (defun dired-next-marked-file (arg &optional wrap opoint
)
1720 "Move to the next marked file, wrapping around the end of the buffer."
1721 (interactive "p\np")
1722 (or opoint
(setq opoint
(point)));; return to where interactively started
1724 (re-search-forward dired-re-mark nil t arg
)
1726 (re-search-backward dired-re-mark nil t
(- arg
)))
1727 (dired-move-to-filename)
1731 (error "No next marked file"))
1732 (message "(Wraparound for next marked file)")
1733 (goto-char (if (> arg
0) (point-min) (point-max)))
1734 (dired-next-marked-file arg nil opoint
))))
1736 (defun dired-prev-marked-file (arg &optional wrap
)
1737 "Move to the previous marked file, wrapping around the end of the buffer."
1738 (interactive "p\np")
1739 (dired-next-marked-file (- arg
) wrap
))
1741 (defun dired-file-marker (file)
1742 ;; Return FILE's marker, or nil if unmarked.
1744 (and (dired-goto-file file
)
1747 (if (not (equal ?
\040 (following-char)))
1748 (following-char))))))
1750 (defun dired-mark-files-in-region (start end
)
1751 (let (buffer-read-only)
1753 (error "start > end"))
1754 (goto-char start
) ; assumed at beginning of line
1755 (while (< (point) end
)
1756 ;; Skip subdir line and following garbage like the `total' line:
1757 (while (and (< (point) end
) (dired-between-files))
1759 (if (and (not (looking-at dired-re-dot
))
1760 (dired-get-filename nil t
))
1763 (insert dired-marker-char
)))
1766 (defun dired-mark (arg)
1767 "Mark the current (or next ARG) files.
1768 If on a subdir headerline, mark all its files except `.' and `..'.
1770 Use \\[dired-unmark-all-files] to remove all marks
1771 and \\[dired-unmark] on a subdir to remove the marks in
1774 (if (and (cdr dired-subdir-alist
) (dired-get-subdir))
1775 (save-excursion (dired-mark-subdir-files))
1776 (let (buffer-read-only)
1777 (dired-repeat-over-lines
1778 (prefix-numeric-value arg
)
1779 (function (lambda () (delete-char 1) (insert dired-marker-char
)))))))
1781 (defun dired-unmark (arg)
1782 "Unmark the current (or next ARG) files.
1783 If looking at a subdir, unmark all its files except `.' and `..'."
1785 (let ((dired-marker-char ?
\040))
1788 (defun dired-flag-file-deletion (arg)
1789 "In dired, flag the current line's file for deletion.
1790 With prefix arg, repeat over several lines.
1792 If on a subdir headerline, mark all its files except `.' and `..'."
1794 (let ((dired-marker-char dired-del-marker
))
1797 (defun dired-unmark-backward (arg)
1798 "In dired, move up lines and remove deletion flag there.
1799 Optional prefix ARG says how many lines to unflag; default is one line."
1801 (dired-unmark (- arg
)))
1803 ;;; Commands to mark or flag files based on their characteristics or names.
1805 (defvar dired-regexp-history nil
1806 "History list of regular expressions used in Dired commands.")
1808 (defun dired-read-regexp (prompt)
1809 (read-from-minibuffer prompt nil nil nil
'dired-regexp-history
))
1811 (defun dired-mark-files-regexp (regexp &optional marker-char
)
1812 "Mark all files matching REGEXP for use in later commands.
1813 A prefix argument means to unmark them instead.
1814 `.' and `..' are never marked.
1816 REGEXP is an Emacs regexp, not a shell wildcard. Thus, use `\\.o$' for
1817 object files--just `.o' will mark more than you might think."
1819 (list (dired-read-regexp (concat (if current-prefix-arg
"Unmark" "Mark")
1820 " files (regexp): "))
1821 (if current-prefix-arg ?
\040)))
1822 (let ((dired-marker-char (or marker-char dired-marker-char
)))
1824 (and (not (looking-at dired-re-dot
))
1825 (not (eolp)) ; empty line
1826 (let ((fn (dired-get-filename nil t
)))
1827 (and fn
(string-match regexp
(file-name-nondirectory fn
)))))
1830 (defun dired-flag-files-regexp (regexp)
1831 "In dired, flag all files containing the specified REGEXP for deletion.
1832 The match is against the non-directory part of the filename. Use `^'
1833 and `$' to anchor matches. Exclude subdirs by hiding them.
1834 `.' and `..' are never flagged."
1835 (interactive (list (dired-read-regexp "Flag for deletion (regexp): ")))
1836 (dired-mark-files-regexp regexp dired-del-marker
))
1838 (defun dired-mark-symlinks (unflag-p)
1839 "Mark all symbolic links.
1840 With prefix argument, unflag all those files."
1842 (let ((dired-marker-char (if unflag-p ?
\040 dired-marker-char
)))
1843 (dired-mark-if (looking-at dired-re-sym
) "symbolic link")))
1845 (defun dired-mark-directories (unflag-p)
1846 "Mark all directory file lines except `.' and `..'.
1847 With prefix argument, unflag all those files."
1849 (let ((dired-marker-char (if unflag-p ?
\040 dired-marker-char
)))
1850 (dired-mark-if (and (looking-at dired-re-dir
)
1851 (not (looking-at dired-re-dot
)))
1854 (defun dired-mark-executables (unflag-p)
1855 "Mark all executable files.
1856 With prefix argument, unflag all those files."
1858 (let ((dired-marker-char (if unflag-p ?
\040 dired-marker-char
)))
1859 (dired-mark-if (looking-at dired-re-exe
) "executable file")))
1861 ;; dired-x.el has a dired-mark-sexp interactive command: mark
1862 ;; files for which PREDICATE returns non-nil.
1864 (defun dired-flag-auto-save-files (&optional unflag-p
)
1865 "Flag for deletion files whose names suggest they are auto save files.
1866 A prefix argument says to unflag those files instead."
1868 (let ((dired-marker-char (if unflag-p ?
\040 dired-del-marker
)))
1870 ;; It is less than general to check for # here,
1871 ;; but it's the only way this runs fast enough.
1872 (and (save-excursion (end-of-line)
1874 (eq (preceding-char) ?
#)
1875 ;; Handle executables in case of -F option.
1876 ;; We need not worry about the other kinds
1877 ;; of markings that -F makes, since they won't
1878 ;; appear on real auto-save files.
1879 (if (eq (preceding-char) ?
*)
1882 (eq (preceding-char) ?
#)))))
1883 (not (looking-at dired-re-dir
))
1884 (let ((fn (dired-get-filename t t
)))
1885 (if fn
(auto-save-file-name-p
1886 (file-name-nondirectory fn
)))))
1889 (defun dired-flag-backup-files (&optional unflag-p
)
1890 "Flag all backup files (names ending with `~') for deletion.
1891 With prefix argument, unflag these files."
1893 (let ((dired-marker-char (if unflag-p ?
\040 dired-del-marker
)))
1895 ;; It is less than general to check for ~ here,
1896 ;; but it's the only way this runs fast enough.
1897 (and (save-excursion (end-of-line)
1899 (eq (preceding-char) ?~
)
1900 ;; Handle executables in case of -F option.
1901 ;; We need not worry about the other kinds
1902 ;; of markings that -F makes, since they won't
1903 ;; appear on real backup files.
1904 (if (eq (preceding-char) ?
*)
1907 (eq (preceding-char) ?~
)))))
1908 (not (looking-at dired-re-dir
))
1909 (let ((fn (dired-get-filename t t
)))
1910 (if fn
(backup-file-name-p fn
))))
1913 (defun dired-change-marks (&optional old new
)
1914 "Change all OLD marks to NEW marks.
1915 OLD and NEW are both characters used to mark files."
1917 (let* ((cursor-in-echo-area t
)
1918 (old (progn (message "Change (old mark): ") (read-char)))
1919 (new (progn (message "Change %c marks to (new mark): " old
)
1922 (if (or (eq old ?
\r) (eq new ?
\r))
1924 (let ((string (format "\n%c" old
))
1927 (goto-char (point-min))
1928 (while (search-forward string nil t
)
1929 (subst-char-in-region (match-beginning 0)
1930 (match-end 0) old new
))))))
1932 (defun dired-unmark-all-files-no-query ()
1933 "Remove all marks from all files in the Dired buffer."
1935 (dired-unmark-all-files ?
\r))
1937 (defun dired-unmark-all-files (mark &optional arg
)
1938 "Remove a specific mark (or any mark) from every file.
1939 After this command, type the mark character to remove,
1940 or type RET to remove all marks.
1941 With prefix arg, query for each marked file.
1942 Type \\[help-command] at that time for help."
1943 (interactive "cRemove marks (RET means all): \nP")
1946 buffer-read-only case-fold-search query
1947 (string (format "\n%c" mark
))
1949 Type SPC or `y' to unmark one file, DEL or `n' to skip to next,
1950 `!' to unmark all remaining files with no more questions."))
1951 (goto-char (point-min))
1952 (while (if (eq mark ?
\r)
1953 (re-search-forward dired-re-mark nil t
)
1954 (search-forward string nil t
))
1956 (dired-query 'query
"Unmark file `%s'? "
1957 (dired-get-filename t
)))
1958 (progn (subst-char-in-region (1- (point)) (point)
1959 (preceding-char) ?\
)
1960 (setq count
(1+ count
)))))
1961 (message (if (= count
1) "1 mark removed"
1965 ;; Logging failures operating on files, and showing the results.
1967 (defvar dired-log-buffer
"*Dired log*")
1970 "Pop up a buffer with error log output from Dired.
1971 A group of errors from a single command ends with a formfeed.
1972 Thus, use \\[backward-page] to find the beginning of a group of errors."
1974 (if (get-buffer dired-log-buffer
)
1975 (let ((owindow (selected-window))
1976 (window (display-buffer (get-buffer dired-log-buffer
))))
1979 (select-window window
)
1980 (goto-char (point-max))
1982 (select-window owindow
)))))
1984 (defun dired-log (log &rest args
)
1985 ;; Log a message or the contents of a buffer.
1986 ;; If LOG is a string and there are more args, it is formatted with
1987 ;; those ARGS. Usually the LOG string ends with a \n.
1988 ;; End each bunch of errors with (dired-log t): this inserts
1989 ;; current time and buffer, and a \f (formfeed).
1990 (let ((obuf (current-buffer)))
1991 (unwind-protect ; want to move point
1993 (set-buffer (get-buffer-create dired-log-buffer
))
1994 (goto-char (point-max))
1995 (let (buffer-read-only)
1996 (cond ((stringp log
)
1998 (apply (function format
) log args
)
2001 (insert-buffer log
))
2003 (insert "\n\t" (current-time-string)
2004 "\tBuffer `" (buffer-name obuf
) "'\n\f\n")))))
2005 (set-buffer obuf
))))
2007 (defun dired-log-summary (string failures
)
2008 (message (if failures
"%s--type ? for details (%s)"
2009 "%s--type ? for details")
2011 ;; Log a summary describing a bunch of errors.
2012 (dired-log (concat "\n" string
))
2017 ;; Most ls can only sort by name or by date (with -t), nothing else.
2018 ;; GNU ls sorts on size with -S, on extension with -X, and unsorted with -U.
2019 ;; So anything that does not contain these is sort "by name".
2021 (defvar dired-ls-sorting-switches
"SXU"
2022 "String of `ls' switches (single letters) except `t' that influence sorting.")
2024 (defvar dired-sort-by-date-regexp
2025 (concat "^-[^" dired-ls-sorting-switches
2026 "]*t[^" dired-ls-sorting-switches
"]*$")
2027 "Regexp recognized by dired to set `by date' mode.")
2029 (defvar dired-sort-by-name-regexp
2030 (concat "^-[^t" dired-ls-sorting-switches
"]+$")
2031 "Regexp recognized by dired to set `by name' mode.")
2033 (defun dired-sort-set-modeline ()
2034 ;; Set modeline display according to dired-actual-switches.
2035 ;; Modeline display of "by name" or "by date" guarantees the user a
2036 ;; match with the corresponding regexps. Non-matching switches are
2039 (let (case-fold-search)
2040 (cond ((string-match dired-sort-by-name-regexp dired-actual-switches
)
2042 ((string-match dired-sort-by-date-regexp dired-actual-switches
)
2045 (concat "Dired " dired-actual-switches
)))))
2046 ;; update mode line:
2047 (set-buffer-modified-p (buffer-modified-p)))
2049 (defun dired-sort-toggle-or-edit (&optional arg
)
2050 "Toggle between sort by date/name and refresh the dired buffer.
2051 With a prefix argument you can edit the current listing switches instead."
2055 (read-string "ls switches (must contain -l): " dired-actual-switches
))
2056 (dired-sort-toggle)))
2058 (defun dired-sort-toggle ()
2059 ;; Toggle between sort by date/name. Reverts the buffer.
2060 (setq dired-actual-switches
2061 (let (case-fold-search)
2064 (dired-replace-in-string (concat "[-lt"
2065 dired-ls-sorting-switches
"]")
2067 dired-actual-switches
)
2068 (if (string-match (concat "[t" dired-ls-sorting-switches
"]")
2069 dired-actual-switches
)
2072 (dired-sort-set-modeline)
2075 (defun dired-replace-in-string (regexp newtext string
)
2076 ;; Replace REGEXP with NEWTEXT everywhere in STRING and return result.
2077 ;; NEWTEXT is taken literally---no \\DIGIT escapes will be recognized.
2078 (let ((result "") (start 0) mb me
)
2079 (while (string-match regexp string start
)
2080 (setq mb
(match-beginning 0)
2082 result
(concat result
(substring string start mb
) newtext
)
2084 (concat result
(substring string start
))))
2086 (defun dired-sort-other (switches &optional no-revert
)
2087 ;; Specify new ls SWITCHES for current dired buffer. Values matching
2088 ;; `dired-sort-by-date-regexp' or `dired-sort-by-name-regexp' set the
2089 ;; minor mode accordingly, others appear literally in the mode line.
2090 ;; With optional second arg NO-REVERT, don't refresh the listing afterwards.
2091 (setq dired-actual-switches switches
)
2092 (dired-sort-set-modeline)
2093 (or no-revert
(revert-buffer)))
2095 ;; To make this file smaller, the less common commands
2096 ;; go in a separate file. But autoload them here
2097 ;; to make the separation invisible.
2099 (autoload 'dired-diff
"dired-aux"
2100 "Compare file at point with file FILE using `diff'.
2101 FILE defaults to the file at the mark.
2102 The prompted-for file is the first file given to `diff'."
2105 (autoload 'dired-backup-diff
"dired-aux"
2106 "Diff this file with its backup file or vice versa.
2107 Uses the latest backup, if there are several numerical backups.
2108 If this file is a backup, diff it with its original.
2109 The backup file is the first file given to `diff'."
2112 (autoload 'dired-clean-directory
"dired-aux"
2113 "Flag numerical backups for deletion.
2114 Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
2115 Positive prefix arg KEEP overrides `dired-kept-versions';
2116 Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
2118 To clear the flags on these files, you can use \\[dired-flag-backup-files]
2119 with a prefix argument."
2122 (autoload 'dired-do-chmod
"dired-aux"
2123 "Change the mode of the marked (or next ARG) files.
2124 This calls chmod, thus symbolic modes like `g+w' are allowed."
2127 (autoload 'dired-do-chgrp
"dired-aux"
2128 "Change the group of the marked (or next ARG) files."
2131 (autoload 'dired-do-chown
"dired-aux"
2132 "Change the owner of the marked (or next ARG) files."
2135 (autoload 'dired-do-print
"dired-aux"
2136 "Print the marked (or next ARG) files.
2137 Uses the shell command coming from variables `lpr-command' and
2138 `lpr-switches' as default."
2141 (autoload 'dired-do-shell-command
"dired-aux"
2142 "Run a shell command COMMAND on the marked files.
2143 If no files are marked or a specific numeric prefix arg is given,
2144 the next ARG files are used. Just \\[universal-argument] means the current file.
2145 The prompt mentions the file(s) or the marker, as appropriate.
2147 If there is output, it goes to a separate buffer.
2149 Normally the command is run on each file individually.
2150 However, if there is a `*' in the command then it is run
2151 just once with the entire file list substituted there.
2153 No automatic redisplay of dired buffers is attempted, as there's no
2154 telling what files the command may have changed. Type
2155 \\[dired-do-redisplay] to redisplay the marked files.
2157 The shell command has the top level directory as working directory, so
2158 output files usually are created there instead of in a subdir."
2161 (autoload 'dired-do-kill-lines
"dired-aux"
2162 "Kill all marked lines (not the files).
2163 With a prefix arg, kill all lines not marked or flagged."
2166 (autoload 'dired-do-compress
"dired-aux"
2167 "Compress or uncompress marked (or next ARG) files."
2170 (autoload 'dired-do-byte-compile
"dired-aux"
2171 "Byte compile marked (or next ARG) Emacs Lisp files."
2174 (autoload 'dired-do-load
"dired-aux"
2175 "Load the marked (or next ARG) Emacs Lisp files."
2178 (autoload 'dired-do-redisplay
"dired-aux"
2179 "Redisplay all marked (or next ARG) files.
2180 If on a subdir line, redisplay that subdirectory. In that case,
2181 a prefix arg lets you edit the `ls' switches used for the new listing."
2184 (autoload 'dired-string-replace-match
"dired-aux"
2185 "Replace first match of REGEXP in STRING with NEWTEXT.
2186 If it does not match, nil is returned instead of the new string.
2187 Optional arg LITERAL means to take NEWTEXT literally.
2188 Optional arg GLOBAL means to replace all matches."
2191 (autoload 'dired-create-directory
"dired-aux"
2192 "Create a directory called DIRECTORY."
2195 (autoload 'dired-do-copy
"dired-aux"
2196 "Copy all marked (or next ARG) files, or copy the current file.
2197 Thus, a zero prefix argument copies nothing. But it toggles the
2198 variable `dired-copy-preserve-time' (which see)."
2201 (autoload 'dired-do-symlink
"dired-aux"
2202 "Make symbolic links to current file or all marked (or next ARG) files.
2203 When operating on just the current file, you specify the new name.
2204 When operating on multiple or marked files, you specify a directory
2205 and new symbolic links are made in that directory
2206 with the same names that the files currently have."
2209 (autoload 'dired-do-hardlink
"dired-aux"
2210 "Add names (hard links) current file or all marked (or next ARG) files.
2211 When operating on just the current file, you specify the new name.
2212 When operating on multiple or marked files, you specify a directory
2213 and new hard links are made in that directory
2214 with the same names that the files currently have."
2217 (autoload 'dired-do-rename
"dired-aux"
2218 "Rename current file or all marked (or next ARG) files.
2219 When renaming just the current file, you specify the new name.
2220 When renaming multiple or marked files, you specify a directory."
2223 (autoload 'dired-do-rename-regexp
"dired-aux"
2224 "Rename marked files containing REGEXP to NEWNAME.
2225 As each match is found, the user must type a character saying
2226 what to do with it. For directions, type \\[help-command] at that time.
2227 NEWNAME may contain \\=\\<n> or \\& as in `query-replace-regexp'.
2228 REGEXP defaults to the last regexp used.
2229 With a zero prefix arg, renaming by regexp affects the complete
2230 pathname - usually only the non-directory part of file names is used
2234 (autoload 'dired-do-copy-regexp
"dired-aux"
2235 "Copy all marked files containing REGEXP to NEWNAME.
2236 See function `dired-rename-regexp' for more info."
2239 (autoload 'dired-do-hardlink-regexp
"dired-aux"
2240 "Hardlink all marked files containing REGEXP to NEWNAME.
2241 See function `dired-rename-regexp' for more info."
2244 (autoload 'dired-do-symlink-regexp
"dired-aux"
2245 "Symlink all marked files containing REGEXP to NEWNAME.
2246 See function `dired-rename-regexp' for more info."
2249 (autoload 'dired-upcase
"dired-aux"
2250 "Rename all marked (or next ARG) files to upper case."
2253 (autoload 'dired-downcase
"dired-aux"
2254 "Rename all marked (or next ARG) files to lower case."
2257 (autoload 'dired-maybe-insert-subdir
"dired-aux"
2258 "Insert this subdirectory into the same dired buffer.
2259 If it is already present, just move to it (type \\[dired-do-redisplay] to refresh),
2260 else inserts it at its natural place (as `ls -lR' would have done).
2261 With a prefix arg, you may edit the ls switches used for this listing.
2262 You can add `R' to the switches to expand the whole tree starting at
2264 This function takes some pains to conform to `ls -lR' output."
2267 (autoload 'dired-next-subdir
"dired-aux"
2268 "Go to next subdirectory, regardless of level."
2271 (autoload 'dired-prev-subdir
"dired-aux"
2272 "Go to previous subdirectory, regardless of level.
2273 When called interactively and not on a subdir line, go to this subdir's line."
2276 (autoload 'dired-goto-subdir
"dired-aux"
2277 "Go to end of header line of DIR in this dired buffer.
2278 Return value of point on success, otherwise return nil.
2279 The next char is either \\n, or \\r if DIR is hidden."
2282 (autoload 'dired-mark-subdir-files
"dired-aux"
2283 "Mark all files except `.' and `..'."
2286 (autoload 'dired-kill-subdir
"dired-aux"
2287 "Remove all lines of current subdirectory.
2288 Lower levels are unaffected."
2291 (autoload 'dired-tree-up
"dired-aux"
2292 "Go up ARG levels in the dired tree."
2295 (autoload 'dired-tree-down
"dired-aux"
2296 "Go down in the dired tree."
2299 (autoload 'dired-hide-subdir
"dired-aux"
2300 "Hide or unhide the current subdirectory and move to next directory.
2301 Optional prefix arg is a repeat factor.
2302 Use \\[dired-hide-all] to (un)hide all directories."
2305 (autoload 'dired-hide-all
"dired-aux"
2306 "Hide all subdirectories, leaving only their header lines.
2307 If there is already something hidden, make everything visible again.
2308 Use \\[dired-hide-subdir] to (un)hide a particular subdirectory."
2311 (if (eq system-type
'vax-vms
)
2316 (run-hooks 'dired-load-hook
) ; for your customizations
2318 ;;; dired.el ends here