661
|
1 ;;; dired.el --- DIRED commands for Emacs
|
|
2
|
|
3 ;;; Missing: P command, sorting, setting file modes.
|
|
4 ;;; Dired buffer containing multiple directories gets totally confused
|
|
5 ;;; Implement insertion of subdirectories in situ --- tree dired
|
|
6
|
|
7 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
|
|
8
|
|
9 ;; This file is part of GNU Emacs.
|
|
10
|
|
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
12 ;; it under the terms of the GNU General Public License as published by
|
|
13 ;; the Free Software Foundation; either version 1, or (at your option)
|
|
14 ;; any later version.
|
|
15
|
|
16 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 ;; GNU General Public License for more details.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
|
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
24
|
|
25
|
|
26 ;;;###autoload
|
|
27 (defvar dired-listing-switches "-al" "\
|
|
28 Switches passed to ls for dired. MUST contain the `l' option.
|
|
29 CANNOT contain the `F' option.")
|
|
30
|
|
31 (defvar dired-chown-program
|
|
32 (if (memq system-type '(hpux usg-unix-v))
|
|
33 "/bin/chown" "/etc/chown")
|
|
34 "Pathname of chown command.")
|
|
35
|
|
36 (defvar dired-directory nil)
|
|
37
|
|
38 (defun dired-readin (dirname buffer)
|
|
39 (save-excursion
|
|
40 (message "Reading directory %s..." dirname)
|
|
41 (set-buffer buffer)
|
|
42 (let ((buffer-read-only nil))
|
|
43 (widen)
|
|
44 (erase-buffer)
|
|
45 (setq dirname (expand-file-name dirname))
|
|
46 (if (eq system-type 'vax-vms)
|
|
47 (vms-read-directory dirname dired-listing-switches buffer)
|
|
48 (if (file-directory-p dirname)
|
|
49 (call-process "ls" nil buffer nil
|
|
50 dired-listing-switches dirname)
|
|
51 (if (not (file-readable-p (directory-file-name (file-name-directory dirname))))
|
|
52 (insert "Directory " dirname " inaccessible or nonexistent.\n")
|
|
53 (let ((default-directory (file-name-directory dirname)))
|
|
54 (call-process shell-file-name nil buffer nil
|
|
55 "-c" (concat "ls -d " dired-listing-switches " "
|
|
56 (file-name-nondirectory dirname)))))))
|
|
57 (goto-char (point-min))
|
|
58 (indent-rigidly (point-min) (point-max) 2))
|
|
59 (set-buffer-modified-p nil)
|
|
60 (message "Reading directory %s...done" dirname)))
|
|
61
|
|
62 (defun dired-find-buffer (dirname)
|
|
63 (let ((blist (buffer-list))
|
|
64 found)
|
|
65 (while blist
|
|
66 (save-excursion
|
|
67 (set-buffer (car blist))
|
|
68 (if (and (eq major-mode 'dired-mode)
|
|
69 (equal dired-directory dirname))
|
|
70 (setq found (car blist)
|
|
71 blist nil)
|
|
72 (setq blist (cdr blist)))))
|
|
73 (or found
|
|
74 (create-file-buffer (directory-file-name dirname)))))
|
|
75
|
|
76 ;;;###autoload
|
|
77 (defun dired (dirname)
|
|
78 "\"Edit\" directory DIRNAME--delete, rename, print, etc. some files in it.
|
|
79 Dired displays the list of files in DIRNAME.
|
|
80 You can move around in it with the usual movement commands.
|
|
81 You can flag files for deletion with \\<dired-mode-map>\\[dired-flag-file-deleted]
|
|
82 and then delete them by typing `x'.
|
|
83 Type `h' after entering dired for more info."
|
|
84 (interactive (list (read-file-name "Dired (directory): "
|
|
85 nil default-directory nil)))
|
|
86 (switch-to-buffer (dired-noselect dirname)))
|
|
87 ;;;###autoload
|
|
88 (define-key ctl-x-map "d" 'dired)
|
|
89
|
|
90 ;;;###autoload
|
|
91 (defun dired-other-window (dirname)
|
|
92 "\"Edit\" directory DIRNAME. Like `dired' but selects in another window."
|
|
93 (interactive (list (read-file-name "Dired in other window (directory): "
|
|
94 nil default-directory nil)))
|
|
95 (switch-to-buffer-other-window (dired-noselect dirname)))
|
|
96 ;;;###autoload
|
|
97 (define-key ctl-x-4-map "d" 'dired-other-window)
|
|
98
|
|
99 ;;;###autoload
|
|
100 (defun dired-noselect (dirname)
|
|
101 "Like `dired' but returns the dired buffer as value, does not select it."
|
|
102 (or dirname (setq dirname default-directory))
|
|
103 (setq dirname (expand-file-name (directory-file-name dirname)))
|
|
104 (if (file-directory-p dirname)
|
|
105 (setq dirname (file-name-as-directory dirname)))
|
|
106 (let ((buffer (dired-find-buffer dirname)))
|
|
107 (save-excursion
|
|
108 (set-buffer buffer)
|
|
109 (dired-readin dirname buffer)
|
|
110 (while (and (not (dired-move-to-filename)) (not (eobp)))
|
|
111 (forward-line 1))
|
|
112 (dired-mode dirname))
|
|
113 buffer))
|
|
114
|
|
115 (defun dired-revert (&optional arg noconfirm)
|
|
116 (let ((opoint (point))
|
|
117 (ofile (dired-get-filename t t))
|
|
118 (buffer-read-only nil)
|
|
119 delete-list already-deleted column-dots)
|
|
120 (goto-char 1)
|
|
121 (if (re-search-forward "^D" nil t)
|
|
122 (progn
|
|
123 (beginning-of-line)
|
|
124 (while (re-search-forward "^D" nil t)
|
|
125 (setq delete-list (cons (dired-get-filename t) delete-list)))))
|
|
126 (dired-readin dired-directory (current-buffer))
|
|
127 (while (and (not (dired-move-to-filename)) (not (eobp)))
|
|
128 (forward-line 1))
|
|
129 (setq column-dots (concat "^" (make-string (current-column) ?.))
|
|
130 delete-list (nreverse delete-list))
|
|
131 (while delete-list
|
|
132 ;; assumptions: the directory was reread with the files listed in the
|
|
133 ;; same order as they were originally. the string of "."s is rather silly
|
|
134 ;; but it seems the fastest way to avoid messing with -F flags and
|
|
135 ;; matches that occur in places other than the filename column
|
|
136 (if (re-search-forward
|
|
137 (concat column-dots (regexp-quote (car delete-list))) nil t)
|
|
138 (progn (beginning-of-line)
|
|
139 (delete-char 1)
|
|
140 (insert "D"))
|
|
141 (setq already-deleted (cons (car delete-list) already-deleted)))
|
|
142 (setq delete-list (cdr delete-list)))
|
|
143 (goto-char 0)
|
|
144 (or (and ofile (re-search-forward (concat column-dots (regexp-quote ofile))
|
|
145 nil t))
|
|
146 (goto-char opoint))
|
|
147 (dired-move-to-filename)
|
|
148 (if already-deleted (message "Already deleted: %s"
|
|
149 (prin1-to-string (reverse already-deleted))))))
|
|
150
|
|
151 (defvar dired-mode-map nil "Local keymap for dired-mode buffers.")
|
|
152 (if dired-mode-map
|
|
153 nil
|
|
154 (setq dired-mode-map (make-keymap))
|
|
155 (suppress-keymap dired-mode-map)
|
|
156 (define-key dired-mode-map "r" 'dired-rename-file)
|
|
157 (define-key dired-mode-map "\C-d" 'dired-flag-file-deleted)
|
|
158 (define-key dired-mode-map "d" 'dired-flag-file-deleted)
|
|
159 (define-key dired-mode-map "v" 'dired-view-file)
|
|
160 (define-key dired-mode-map "e" 'dired-find-file)
|
|
161 (define-key dired-mode-map "f" 'dired-find-file)
|
|
162 (define-key dired-mode-map "o" 'dired-find-file-other-window)
|
|
163 (define-key dired-mode-map "u" 'dired-unflag)
|
|
164 (define-key dired-mode-map "x" 'dired-do-deletions)
|
|
165 (define-key dired-mode-map "\177" 'dired-backup-unflag)
|
|
166 (define-key dired-mode-map "?" 'dired-summary)
|
|
167 (define-key dired-mode-map "c" 'dired-copy-file)
|
|
168 (define-key dired-mode-map "#" 'dired-flag-auto-save-files)
|
|
169 (define-key dired-mode-map "~" 'dired-flag-backup-files)
|
|
170 (define-key dired-mode-map "F" 'dired-flag-regexp-files)
|
|
171 (define-key dired-mode-map "." 'dired-clean-directory)
|
|
172 (define-key dired-mode-map "h" 'describe-mode)
|
|
173 (define-key dired-mode-map " " 'dired-next-line)
|
|
174 (define-key dired-mode-map "\C-n" 'dired-next-line)
|
|
175 (define-key dired-mode-map "\C-p" 'dired-previous-line)
|
|
176 (define-key dired-mode-map "n" 'dired-next-line)
|
|
177 (define-key dired-mode-map "p" 'dired-previous-line)
|
|
178 (define-key dired-mode-map "g" 'revert-buffer)
|
|
179 (define-key dired-mode-map "D" 'dired-create-directory)
|
|
180 (define-key dired-mode-map "m" 'dired-move-file)
|
|
181 (define-key dired-mode-map "C" 'dired-compress)
|
|
182 (define-key dired-mode-map "U" 'dired-uncompress)
|
|
183 (define-key dired-mode-map "B" 'dired-byte-recompile)
|
|
184 (define-key dired-mode-map "M" 'dired-chmod)
|
|
185 (define-key dired-mode-map "G" 'dired-chgrp)
|
|
186 (define-key dired-mode-map "O" 'dired-chown)
|
|
187 (define-key dired-mode-map "=" 'dired-diff)
|
|
188 (define-key dired-mode-map "<" 'dired-up-directory))
|
|
189
|
|
190
|
|
191 ;; Dired mode is suitable only for specially formatted data.
|
|
192 (put 'dired-mode 'mode-class 'special)
|
|
193
|
|
194 (defun dired-mode (&optional dirname)
|
|
195 "Mode for \"editing\" directory listings.
|
|
196 In dired, you are \"editing\" a list of the files in a directory.
|
|
197 You can move using the usual cursor motion commands.
|
|
198 Letters no longer insert themselves.
|
|
199 Instead, use the following commands:
|
|
200 \\{dired-mode-map}"
|
|
201 (interactive)
|
|
202 (kill-all-local-variables)
|
|
203 (make-local-variable 'revert-buffer-function)
|
|
204 (setq revert-buffer-function 'dired-revert)
|
|
205 (setq major-mode 'dired-mode)
|
|
206 (setq mode-name "Dired")
|
|
207 (make-local-variable 'dired-directory)
|
|
208 (setq dired-directory (or dirname default-directory))
|
|
209 (make-local-variable 'list-buffers-directory)
|
|
210 (setq list-buffers-directory dired-directory)
|
|
211 (set (make-local-variable 'dired-used-F)
|
|
212 (string-match "F" dired-listing-switches))
|
|
213 (if dirname
|
|
214 (setq default-directory
|
|
215 (if (file-directory-p dirname)
|
|
216 dirname (file-name-directory dirname))))
|
|
217 (setq mode-line-buffer-identification '("Dired: %17f"))
|
|
218 (setq case-fold-search nil)
|
|
219 (setq buffer-read-only t)
|
|
220 (use-local-map dired-mode-map)
|
|
221 (run-hooks 'dired-mode-hook))
|
|
222
|
|
223 ;; FUNCTION receives no arguments
|
|
224 ;; and should return t iff it deletes the current line from the buffer.
|
|
225 (defun dired-repeat-over-lines (arg function)
|
|
226 (beginning-of-line)
|
|
227 (while (and (> arg 0) (not (eobp)))
|
|
228 (setq arg (1- arg))
|
|
229 (let (deleted)
|
|
230 (save-excursion
|
|
231 (beginning-of-line)
|
|
232 (and (bobp) (looking-at " total")
|
|
233 (error "No file on this line"))
|
|
234 (setq deleted (funcall function)))
|
|
235 (or deleted
|
|
236 (forward-line 1)))
|
|
237 (dired-move-to-filename))
|
|
238 (while (and (< arg 0) (not (bobp)))
|
|
239 (setq arg (1+ arg))
|
|
240 (forward-line -1)
|
|
241 (dired-move-to-filename)
|
|
242 (save-excursion
|
|
243 (beginning-of-line)
|
|
244 (funcall function))))
|
|
245
|
|
246 (defun dired-flag-file-deleted (arg)
|
|
247 "In dired, flag the current line's file for deletion.
|
|
248 With prefix arg, repeat over several lines."
|
|
249 (interactive "p")
|
|
250 (dired-repeat-over-lines arg
|
|
251 '(lambda ()
|
|
252 (let ((buffer-read-only nil))
|
|
253 (delete-char 1)
|
|
254 (insert "D")
|
|
255 nil))))
|
|
256
|
|
257 (defun dired-flag-regexp-files (regexp)
|
|
258 "In dired, flag all files matching the specified REGEXP for deletion."
|
|
259 (interactive "sFlagging regexp: ")
|
|
260 (save-excursion
|
|
261 (let ((buffer-read-only nil))
|
|
262 (goto-char (point-min))
|
|
263 (while (not (eobp))
|
|
264 (and (not (looking-at " d"))
|
|
265 (not (eolp))
|
|
266 (let ((fn (dired-get-filename t t)))
|
|
267 (if fn (string-match regexp fn)))
|
|
268 (progn (beginning-of-line)
|
|
269 (delete-char 1)
|
|
270 (insert "D")))
|
|
271 (forward-line 1)))))
|
|
272
|
|
273 (defun dired-summary ()
|
|
274 (interactive)
|
|
275 ;>> this should check the key-bindings and use substitute-command-keys if non-standard
|
|
276 (message
|
|
277 "d-elete, u-ndelete, x-ecute, f-ind, o-ther window, r-ename, c-opy, v-iew"))
|
|
278
|
|
279 (defun dired-unflag (arg)
|
|
280 "In dired, remove the current line's delete flag then move to next line."
|
|
281 (interactive "p")
|
|
282 (dired-repeat-over-lines arg
|
|
283 '(lambda ()
|
|
284 (let ((buffer-read-only nil))
|
|
285 (delete-char 1)
|
|
286 (insert " ")
|
|
287 (forward-char -1)
|
|
288 nil))))
|
|
289
|
|
290 (defun dired-backup-unflag (arg)
|
|
291 "In dired, move up lines and remove deletion flag there.
|
|
292 Optional prefix ARG says how many lines to unflag; default is one line."
|
|
293 (interactive "p")
|
|
294 (dired-unflag (- arg)))
|
|
295
|
|
296 (defun dired-next-line (arg)
|
|
297 "Move down lines then position at filename.
|
|
298 Optional prefix ARG says how many lines to move; default is one line."
|
|
299 (interactive "p")
|
|
300 (next-line arg)
|
|
301 (dired-move-to-filename))
|
|
302
|
|
303 (defun dired-previous-line (arg)
|
|
304 "Move up lines then position at filename.
|
|
305 Optional prefix ARG says how many lines to move; default is one line."
|
|
306 (interactive "p")
|
|
307 (previous-line arg)
|
|
308 (dired-move-to-filename))
|
|
309
|
|
310 (defun dired-up-directory ()
|
|
311 "Run dired on the parent of the current directory."
|
|
312 (interactive)
|
|
313 (find-file ".."))
|
|
314
|
|
315 (defun dired-find-file ()
|
|
316 "In dired, visit the file or directory named on this line."
|
|
317 (interactive)
|
|
318 (find-file (dired-get-filename)))
|
|
319
|
|
320 (defun dired-view-file ()
|
|
321 "In dired, examine a file in view mode, returning to dired when done."
|
|
322 (interactive)
|
|
323 (if (file-directory-p (dired-get-filename))
|
|
324 (dired (dired-get-filename))
|
|
325 (view-file (dired-get-filename))))
|
|
326
|
|
327 (defun dired-find-file-other-window ()
|
|
328 "In dired, visit this file or directory in another window."
|
|
329 (interactive)
|
|
330 (find-file-other-window (dired-get-filename)))
|
|
331
|
|
332 (defun dired-get-filename (&optional localp no-error-if-not-filep)
|
|
333 "In dired, return name of file mentioned on this line.
|
|
334 Value returned normally includes the directory name.
|
|
335 Optional arg LOCALP means don't include it.
|
|
336 Optional arg NO-ERROR-IF-NOT-FILEP means return nil if no filename
|
|
337 on this line, otherwise an error occurs."
|
|
338 (let (eol file type ex (case-fold-search nil))
|
|
339 (save-excursion
|
|
340 (end-of-line)
|
|
341 (setq eol (point))
|
|
342 (beginning-of-line)
|
|
343 (if (eq system-type 'vax-vms)
|
|
344 ;; Non-filename lines don't match
|
|
345 ;; because they have lower case letters.
|
|
346 (if (re-search-forward "^..\\([][.A-Z-0-9_$;<>]+\\)" eol t)
|
|
347 (setq file (buffer-substring (match-beginning 1) (match-end 1))))
|
|
348 ;; Unix case
|
|
349 (if (not (re-search-forward
|
|
350 "\\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\)[ ]+[0-9]+"
|
|
351 eol t)) ()
|
|
352 (skip-chars-forward " ")
|
|
353 (skip-chars-forward "^ " eol)
|
|
354 (skip-chars-forward " " eol)
|
|
355 (setq file (buffer-substring (point) eol))
|
|
356 (re-search-backward "\\(.\\)[-r][-w]\\(.\\)[-r][-w]\\(.\\)[-r][-w]\\(.\\)")
|
|
357 (setq flag (buffer-substring (match-beginning 1) (match-end 1))
|
|
358 ex (string-match "[xst]" ;; execute bit set anywhere?
|
|
359 (concat
|
|
360 (buffer-substring (match-beginning 2) (match-end 2))
|
|
361 (buffer-substring (match-beginning 3) (match-end 3))
|
|
362 (buffer-substring (match-beginning 4) (match-end 4)))))
|
|
363 (cond
|
|
364 ((string= flag "l")
|
|
365 ;; strip the link name. Bombs if file contains " ->"
|
|
366 (if (string-match " ->" file)
|
|
367 (setq file (substring file 0 (match-beginning 0)))))
|
|
368 ((and dired-used-F ;; strip off -F stuff if there
|
|
369 (or (string= flag "d") (string= flag "s") ex))
|
|
370 (setq file (substring file 0 -1)))))))
|
|
371 (or no-error-if-not-filep file
|
|
372 (error "No file on this line"))
|
|
373 ;; ??? uses default-directory, could lose on cd, multiple.
|
|
374 (or localp (setq file (expand-file-name file default-directory)))
|
|
375 file))
|
|
376
|
|
377 (defun dired-move-to-filename ()
|
|
378 "In dired, move to first char of filename on this line.
|
|
379 Returns position (point) or nil if no filename on this line."
|
|
380 (let ((eol (progn (end-of-line) (point))))
|
|
381 (beginning-of-line)
|
|
382 (if (re-search-forward
|
|
383 "\\(Jan\\|Feb\\|Mar\\|Apr\\|May\\|Jun\\|Jul\\|Aug\\|Sep\\|Oct\\|Nov\\|Dec\\)[ ]+[0-9]+"
|
|
384 eol t)
|
|
385 (progn
|
|
386 (skip-chars-forward " ")
|
|
387 (skip-chars-forward "^ " eol)
|
|
388 (skip-chars-forward " " eol)
|
|
389 (point)))))
|
|
390
|
|
391 (defun dired-map-dired-file-lines (fn)
|
|
392 "Perform function FN with point at the end of each non-directory line.
|
|
393 The arguments given to FN are the short and long filename"
|
|
394 (save-excursion
|
|
395 (let (filename longfilename (buffer-read-only nil))
|
|
396 (goto-char (point-min))
|
|
397 (while (not (eobp))
|
|
398 (save-excursion
|
|
399 (and (not (looking-at " \\s *[0-9]*\\s *[0-9]* d"))
|
|
400 (not (eolp))
|
|
401 (setq filename (dired-get-filename t t)
|
|
402 longfilename (dired-get-filename nil t))
|
|
403 (progn (end-of-line)
|
|
404 (funcall fn filename longfilename))))
|
|
405 (forward-line 1)))))
|
|
406
|
|
407 (defun dired-flag-auto-save-files (unflag-p)
|
|
408 "Flag for deletion files whose names suggest they are auto save files.
|
|
409 A prefix argument says to unflag those files instead."
|
|
410 (interactive "P")
|
|
411 (save-excursion
|
|
412 (let ((buffer-read-only nil))
|
|
413 (goto-char (point-min))
|
|
414 (while (not (eobp))
|
|
415 (and (not (looking-at " \\s *[0-9]*\\s *[0-9]* d"))
|
|
416 (not (eolp))
|
|
417 (if (fboundp 'auto-save-file-name-p)
|
|
418 (let ((fn (dired-get-filename t t)))
|
|
419 (if fn (auto-save-file-name-p fn)))
|
|
420 (if (dired-move-to-filename)
|
|
421 (looking-at "#")))
|
|
422 (progn (beginning-of-line)
|
|
423 (delete-char 1)
|
|
424 (insert (if unflag-p " " "D"))))
|
|
425 (forward-line 1)))))
|
|
426
|
|
427 (defun dired-clean-directory (keep)
|
|
428 "Flag numerical backups for deletion.
|
|
429 Spares `dired-kept-versions' latest versions, and `kept-old-versions' oldest.
|
|
430 Positive prefix arg KEEP overrides `dired-kept-versions';
|
|
431 Negative prefix arg KEEP overrides `kept-old-versions' with KEEP made positive.
|
|
432
|
|
433 To clear the flags on these files, you can use \\[dired-flag-backup-files]
|
|
434 with a prefix argument."
|
|
435 (interactive "P")
|
|
436 (setq keep (if keep (prefix-numeric-value keep) dired-kept-versions))
|
|
437 (let ((early-retention (if (< keep 0) (- keep) kept-old-versions))
|
|
438 (late-retention (if (<= keep 0) dired-kept-versions keep))
|
|
439 (file-version-assoc-list ()))
|
|
440 ;; Look at each file.
|
|
441 ;; If the file has numeric backup versions,
|
|
442 ;; put on file-version-assoc-list an element of the form
|
|
443 ;; (FILENAME . VERSION-NUMBER-LIST)
|
|
444 (dired-map-dired-file-lines 'dired-collect-file-versions)
|
|
445 ;; Sort each VERSION-NUMBER-LIST,
|
|
446 ;; and remove the versions not to be deleted.
|
|
447 (let ((fval file-version-assoc-list))
|
|
448 (while fval
|
|
449 (let* ((sorted-v-list (cons 'q (sort (cdr (car fval)) '<)))
|
|
450 (v-count (length sorted-v-list)))
|
|
451 (if (> v-count (+ early-retention late-retention))
|
|
452 (rplacd (nthcdr early-retention sorted-v-list)
|
|
453 (nthcdr (- v-count late-retention)
|
|
454 sorted-v-list)))
|
|
455 (rplacd (car fval)
|
|
456 (cdr sorted-v-list)))
|
|
457 (setq fval (cdr fval))))
|
|
458 ;; Look at each file. If it is a numeric backup file,
|
|
459 ;; find it in a VERSION-NUMBER-LIST and maybe flag it for deletion.
|
|
460 (dired-map-dired-file-lines 'dired-trample-file-versions)))
|
|
461
|
|
462 (defun dired-collect-file-versions (ignore fn)
|
|
463 "If it looks like file FN has versions, return a list of the versions.
|
|
464 That is a list of strings which are file names.
|
|
465 The caller may want to flag some of these files for deletion."
|
|
466 (let* ((base-versions
|
|
467 (concat (file-name-nondirectory fn) ".~"))
|
|
468 (bv-length (length base-versions))
|
|
469 (possibilities (file-name-all-completions
|
|
470 base-versions
|
|
471 (file-name-directory fn)))
|
|
472 (versions (mapcar 'backup-extract-version possibilities)))
|
|
473 (if versions
|
|
474 (setq file-version-assoc-list (cons (cons fn versions)
|
|
475 file-version-assoc-list)))))
|
|
476
|
|
477 (defun dired-trample-file-versions (ignore fn)
|
|
478 (let* ((start-vn (string-match "\\.~[0-9]+~$" fn))
|
|
479 base-version-list)
|
|
480 (and start-vn
|
|
481 (setq base-version-list ; there was a base version to which
|
|
482 (assoc (substring fn 0 start-vn) ; this looks like a
|
|
483 file-version-assoc-list)) ; subversion
|
|
484 (not (memq (string-to-int (substring fn (+ 2 start-vn)))
|
|
485 base-version-list)) ; this one doesn't make the cut
|
|
486 (dired-flag-this-line-for-DEATH))))
|
|
487
|
|
488 (defun dired-flag-this-line-for-DEATH ()
|
|
489 (beginning-of-line)
|
|
490 (delete-char 1)
|
|
491 (insert "D"))
|
|
492
|
|
493 (defun dired-flag-backup-files (unflag-p)
|
|
494 "Flag all backup files (names ending with `~') for deletion.
|
|
495 With prefix argument, unflag these files."
|
|
496 (interactive "P")
|
|
497 (save-excursion
|
|
498 (let ((buffer-read-only nil))
|
|
499 (goto-char (point-min))
|
|
500 (while (not (eobp))
|
|
501 (and (not (looking-at " d"))
|
|
502 (not (eolp))
|
|
503 (if (fboundp 'backup-file-name-p)
|
|
504 (let ((fn (dired-get-filename t t)))
|
|
505 (if fn (backup-file-name-p fn)))
|
|
506 (end-of-line)
|
|
507 (forward-char -1)
|
|
508 (looking-at "~"))
|
|
509 (progn (beginning-of-line)
|
|
510 (delete-char 1)
|
|
511 (insert (if unflag-p " " "D"))))
|
|
512 (forward-line 1)))))
|
|
513
|
|
514 (defun dired-flag-backup-and-auto-save-files (unflag-p)
|
|
515 "Flag all backup and temporary files for deletion.
|
|
516 Backup files have names ending in `~'.
|
|
517 Auto save file names usually start with `#'.
|
|
518 With prefix argument, unflag these files."
|
|
519 (interactive "P")
|
|
520 (dired-flag-backup-files unflag-p)
|
|
521 (dired-flag-auto-save-files unflag-p))
|
|
522
|
|
523 (defun dired-create-directory (directory)
|
|
524 "Create a directory called DIRECTORY."
|
|
525 (interactive "FCreate directory: ")
|
|
526 (let ((expanded (expand-file-name directory)))
|
|
527 (make-directory expanded)
|
|
528 (dired-add-entry (file-name-directory expanded)
|
|
529 (file-name-nondirectory expanded))
|
|
530 (dired-next-line 1)))
|
|
531
|
|
532 (defun dired-move-file (to-dir &optional count)
|
|
533 "Move this file to directory TO-DIR.
|
|
534 Optional second argument COUNT (the prefix argument)
|
|
535 specifies moving several consecutive files."
|
|
536 (interactive
|
|
537 (let ((count (prefix-numeric-value current-prefix-arg)))
|
|
538 (list (read-file-name (format "Move %s to directory: "
|
|
539 (if (> count 1)
|
|
540 (format "%d files" count)
|
|
541 (file-name-nondirectory (dired-get-filename))))
|
|
542 nil t)
|
|
543 count)))
|
|
544 (let ((dir (file-name-as-directory (expand-file-name to-dir))))
|
|
545 (dired-repeat-over-lines
|
|
546 count
|
|
547 (function (lambda ()
|
|
548 (let ((this (dired-get-filename)))
|
|
549 (rename-file this
|
|
550 (expand-file-name (file-name-nondirectory this)
|
|
551 dir)))
|
|
552 (let ((buffer-read-only nil))
|
|
553 (beginning-of-line)
|
|
554 (delete-region (point) (progn (forward-line 1) (point))))
|
|
555 t)))))
|
|
556
|
|
557 (defun dired-rename-file (to-file)
|
|
558 "Rename the current file to TO-FILE."
|
|
559 (interactive
|
|
560 (list (read-file-name (format "Rename %s to: "
|
|
561 (file-name-nondirectory (dired-get-filename)))
|
|
562 nil (dired-get-filename))))
|
|
563 (setq to-file (expand-file-name to-file))
|
|
564 (let ((filename (dired-get-filename))
|
|
565 (buffer-read-only nil))
|
|
566 (rename-file filename to-file)
|
|
567 (beginning-of-line)
|
|
568 (delete-region (point) (progn (forward-line 1) (point)))
|
|
569 (setq to-file (expand-file-name to-file))
|
|
570 (dired-add-entry (file-name-directory to-file)
|
|
571 (file-name-nondirectory to-file))
|
|
572 ;; Optionally rename the visited file of any buffer visiting this file.
|
|
573 (and (get-file-buffer filename)
|
|
574 (y-or-n-p (message "Change visited file name of buffer %s too? "
|
|
575 (buffer-name (get-file-buffer filename))))
|
|
576 (save-excursion
|
|
577 (set-buffer (get-file-buffer filename))
|
|
578 (let ((modflag (buffer-modified-p)))
|
|
579 (set-visited-file-name to-file)
|
|
580 (set-buffer-modified-p modflag))))))
|
|
581
|
|
582 (defun dired-copy-file (to-file)
|
|
583 "Copy the current file to TO-FILE."
|
|
584 (interactive "FCopy to: ")
|
|
585 (copy-file (dired-get-filename) to-file)
|
|
586 (setq to-file (expand-file-name to-file))
|
|
587 (dired-add-entry (file-name-directory to-file)
|
|
588 (file-name-nondirectory to-file)))
|
|
589
|
|
590 (defun dired-add-entry (directory filename)
|
|
591 ;; If tree dired is implemented, this function will have to do
|
|
592 ;; something smarter with the directory. Currently, just check
|
|
593 ;; default directory, if same, add the new entry at point. With tree
|
|
594 ;; dired, should call 'dired-current-directory' or similar. Note
|
|
595 ;; that this adds the entry 'out of order' if files sorted by time,
|
|
596 ;; etc.
|
|
597 (if (string-equal directory default-directory)
|
|
598 (let ((buffer-read-only nil))
|
|
599 (beginning-of-line)
|
|
600 (call-process "ls" nil t nil
|
|
601 "-d" dired-listing-switches (concat directory filename))
|
|
602 (forward-line -1)
|
|
603 (insert " ")
|
|
604 (dired-move-to-filename)
|
|
605 (let* ((beg (point))
|
|
606 (end (progn (end-of-line) (point))))
|
|
607 (setq filename (buffer-substring beg end))
|
|
608 (delete-region beg end)
|
|
609 (insert (file-name-nondirectory filename)))
|
|
610 (beginning-of-line))))
|
|
611
|
|
612 (defun dired-diff (point mark)
|
|
613 "Compare files at POINT1 and POINT2 by running `diff'.
|
|
614 Interactively, these are the files at point and mark.
|
|
615 The file at mark (POINT2) is the first file given to `diff'.
|
|
616 See the command `diff'."
|
|
617 (interactive "d\nm")
|
|
618 (let (name1 name2)
|
|
619 (setq name2 (dired-get-filename))
|
|
620 (save-excursion
|
|
621 (goto-char mark)
|
|
622 (setq name1 (dired-get-filename)))
|
|
623 (diff name1 name2)))
|
|
624
|
|
625 (defun dired-compress ()
|
|
626 "Compress the current file."
|
|
627 (interactive)
|
|
628 (let* ((buffer-read-only nil)
|
|
629 (error-buffer (get-buffer-create " *Dired compress output*"))
|
|
630 (from-file (dired-get-filename))
|
|
631 (to-file (concat from-file ".Z")))
|
|
632 (if (string-match "\\.Z$" from-file)
|
|
633 (error "%s is already compressed!" from-file))
|
|
634 (message "Compressing %s..." from-file)
|
|
635 (unwind-protect
|
|
636 (progn
|
|
637 (save-excursion
|
|
638 (set-buffer error-buffer)
|
|
639 (erase-buffer))
|
|
640 ;; Must have default-directory of dired buffer in call-process
|
|
641 (call-process "compress" nil error-buffer nil "-f" from-file)
|
|
642 (if (save-excursion
|
|
643 (set-buffer error-buffer)
|
|
644 (= 0 (buffer-size)))
|
|
645 (progn
|
|
646 (message "Compressing %s... done" from-file)
|
|
647 (kill-buffer error-buffer))
|
|
648 (display-buffer error-buffer)
|
|
649 (setq error-buffer nil)
|
|
650 (error "Compress error on %s." from-file)))
|
|
651 (if error-buffer (kill-buffer error-buffer)))
|
|
652 (dired-redisplay to-file)))
|
|
653
|
|
654 (defun dired-uncompress ()
|
|
655 "Uncompress the current file."
|
|
656 (interactive)
|
|
657 (let* ((buffer-read-only nil)
|
|
658 (error-buffer (get-buffer-create " *Dired compress output*"))
|
|
659 (from-file (dired-get-filename))
|
|
660 (to-file (substring from-file 0 -2)))
|
|
661 (if (string-match "\\.Z$" from-file) nil
|
|
662 (error "%s is not compressed!" from-file))
|
|
663 (message "Uncompressing %s..." from-file)
|
|
664 (unwind-protect
|
|
665 (progn
|
|
666 (save-excursion
|
|
667 (set-buffer error-buffer)
|
|
668 (erase-buffer))
|
|
669 ;; Must have default-directory of dired buffer in call-process
|
|
670 (call-process "uncompress" nil error-buffer nil "-f" from-file)
|
|
671 (if (save-excursion
|
|
672 (set-buffer error-buffer)
|
|
673 (= 0 (buffer-size)))
|
|
674 (progn
|
|
675 (message "Uncompressing %s... done" from-file)
|
|
676 (kill-buffer error-buffer))
|
|
677 (display-buffer error-buffer)
|
|
678 (setq error-buffer nil)
|
|
679 (error "Uncompress error on %s." from-file)))
|
|
680 (if error-buffer (kill-buffer error-buffer)))
|
|
681 (dired-redisplay to-file)))
|
|
682
|
|
683 (defun dired-byte-recompile ()
|
|
684 "Byte recompile the current file."
|
|
685 (interactive)
|
|
686 (let* ((buffer-read-only nil)
|
|
687 (from-file (dired-get-filename))
|
|
688 (to-file (substring from-file 0 -3)))
|
|
689 (if (string-match "\\.el$" from-file) nil
|
|
690 (error "%s is uncompilable!" from-file))
|
|
691 (byte-compile-file from-file)))
|
|
692
|
|
693 (defun dired-chmod (mode)
|
|
694 "Change mode of the current file to MODE."
|
|
695 (interactive "sChange to Mode: ")
|
|
696 (let ((buffer-read-only nil)
|
|
697 (file (dired-get-filename)))
|
|
698 (call-process "/bin/chmod" nil nil nil mode file)
|
|
699 (dired-redisplay file)))
|
|
700
|
|
701 (defun dired-chgrp (group)
|
|
702 "Change group of the current file to GROUP."
|
|
703 (interactive "sChange to Group: ")
|
|
704 (let ((buffer-read-only nil)
|
|
705 (file (dired-get-filename)))
|
|
706 (call-process "/bin/chgrp" nil nil nil group file)
|
|
707 (dired-redisplay file)))
|
|
708
|
|
709 (defun dired-chown (owner)
|
|
710 "Change owner of the current file to OWNER."
|
|
711 (interactive "sChange to Owner: ")
|
|
712 (let ((buffer-read-only nil)
|
|
713 (file (dired-get-filename)))
|
|
714 (call-process dired-chown-program
|
|
715 nil nil nil owner file)
|
|
716 (dired-redisplay file)))
|
|
717
|
|
718 (defun dired-redisplay (&optional file)
|
|
719 "Delete the current line, and insert an entry for file FILE.
|
|
720 If FILE is nil, then just delete the current line."
|
|
721 (beginning-of-line)
|
|
722 (delete-region (point) (progn (forward-line 1) (point)))
|
|
723 (if file (dired-add-entry (file-name-directory file)
|
|
724 (file-name-nondirectory file)))
|
|
725 (dired-move-to-filename))
|
|
726
|
|
727 (defun dired-do-deletions ()
|
|
728 "In dired, delete the files flagged for deletion."
|
|
729 (interactive)
|
|
730 (let (delete-list answer)
|
|
731 (save-excursion
|
|
732 (goto-char 1)
|
|
733 (while (re-search-forward "^D" nil t)
|
|
734 (setq delete-list
|
|
735 (cons (cons (dired-get-filename t) (1- (point)))
|
|
736 delete-list))))
|
|
737 (if (null delete-list)
|
|
738 (message "(No deletions requested)")
|
|
739 (save-window-excursion
|
|
740 (set-buffer (get-buffer-create " *Deletions*"))
|
|
741 (funcall (if (> (length delete-list) (* (window-height) 2))
|
|
742 'switch-to-buffer 'switch-to-buffer-other-window)
|
|
743 (current-buffer))
|
|
744 (erase-buffer)
|
|
745 (setq fill-column 70)
|
|
746 (let ((l (reverse delete-list)))
|
|
747 ;; Files should be in forward order for this loop.
|
|
748 (while l
|
|
749 (if (> (current-column) 59)
|
|
750 (insert ?\n)
|
|
751 (or (bobp)
|
|
752 (indent-to (* (/ (+ (current-column) 19) 20) 20) 1)))
|
|
753 (insert (car (car l)))
|
|
754 (setq l (cdr l))))
|
|
755 (goto-char (point-min))
|
|
756 (setq answer (yes-or-no-p "Delete these files? ")))
|
|
757 (if answer
|
|
758 (let ((l delete-list)
|
|
759 failures)
|
|
760 ;; Files better be in reverse order for this loop!
|
|
761 ;; That way as changes are made in the buffer
|
|
762 ;; they do not shift the lines still to be changed.
|
|
763 (while l
|
|
764 (goto-char (cdr (car l)))
|
|
765 (let ((buffer-read-only nil))
|
|
766 (condition-case ()
|
|
767 (let ((fn (concat default-directory (car (car l)))))
|
|
768 (if (file-directory-p fn)
|
|
769 (progn
|
|
770 (remove-directory fn)
|
|
771 (if (file-exists-p fn) (delete-file fn)))
|
|
772 (delete-file fn))
|
|
773 (delete-region (point)
|
|
774 (progn (forward-line 1) (point))))
|
|
775 (error (delete-char 1)
|
|
776 (insert " ")
|
|
777 (setq failures (cons (car (car l)) failures)))))
|
|
778 (setq l (cdr l)))
|
|
779 (if failures
|
|
780 (message "Deletions failed: %s"
|
|
781 (prin1-to-string failures))))))))
|
|
782
|
|
783 (provide 'dired)
|
|
784
|
|
785 ;;; dired.el ends here
|