Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uunet!comp.vuw.ac.nz!byrd From: Mike.Williams@comp.vuw.ac.nz (Mike Williams) Newsgroups: comp.emacs Subject: Re: Dired gadget (+ general suggestion) Message-ID: Date: 21 Nov 90 14:30:20 GMT References: <6967@lifia.imag.fr> <2914@exodus.Eng.Sun.COM> Sender: news@comp.vuw.ac.nz (News Admin) Followup-To: comp.emacs Organization: Dept. of Computer Science, Victoria Uni. of Wellington, NZ. Lines: 92 Nntp-Posting-Host: embassy.comp.vuw.ac.nz In-Reply-To: rock@warp.Eng.Sun.COM's message of 16 Nov 90 00:17:59 GMT +-- In article <2914@exodus.Eng.Sun.COM> rock@warp.Eng.Sun.COM (Bill Petro) writes: | | On a more specific level, is there a way in dired of deleting a whole | directory, without having to go into the directory and deleting each | file first? The way I do this is simply to redefine dired-do-deletions to handle directories, so that you can mark and delete them in the normal way. The delete-directory function asks for confirmation if the directory is non-empty. Note that this *redefines* the function in dired.el, and so has to be loaded afterwards. ;;=== Delete a directory subtree === ;; ;; This is slightly dangerous ... perhaps I should protect it some more ?? (defun delete-directory (dir) "Delete a directory subtree (recursively)." (interactive "DDelete directory: ") (let ((fulldir (expand-file-name dir)) (path)) (string-match "\\(^.*\\)/?$" fulldir) (setq path (substring fulldir (match-beginning 1) (match-end 1))) (if (not (file-directory-p path)) (error "%s is not a directory" path)) (if (or (<= (length (directory-files path)) 2) (yes-or-no-p (format "%s is not empty - delete anyway ? " path))) (progn (call-process "rm" nil nil nil "-r" path) (message "Directory %s deleted" path) t) (error "Directory %s retained" path) nil))) ;;=== dired-do-deletions === ;; altered to remove directories using delete-directory, (defun dired-do-deletions () "In dired, delete the files flagged for deletion." (interactive) (let (delete-list answer) (save-excursion (goto-char 1) (while (re-search-forward "^D" nil t) (setq delete-list (cons (cons (dired-get-filename t) (1- (point))) delete-list)))) (if (null delete-list) (message "(No deletions requested)") (save-window-excursion (switch-to-buffer " *Deletions*") (erase-buffer) (setq fill-column 70) (let ((l (reverse delete-list))) ;; Files should be in forward order for this loop. (while l (if (> (current-column) 59) (insert ?\n) (or (bobp) (indent-to (* (/ (+ (current-column) 19) 20) 20) 1))) (insert (car (car l))) (setq l (cdr l)))) (goto-char (point-min)) (setq answer (yes-or-no-p "Delete these files? "))) (if answer (let ((l delete-list) failures) ;; Files better be in reverse order for this loop! ;; That way as changes are made in the buffer ;; they do not shift the lines still to be changed. (while l (goto-char (cdr (car l))) (let ((buffer-read-only nil)) (condition-case () (let ((fn (concat default-directory (car (car l))))) (if (and (file-directory-p fn) (not (file-symlink-p fn))) (delete-directory fn) (delete-file fn)) (delete-region (point) (progn (forward-line 1) (point)))) (error (delete-char 1) (insert " ") (setq failures (cons (car (car l)) failures))))) (setq l (cdr l))) (if failures (message "Deletions failed: %s" (prin1-to-string failures)))))))) -- /-------------------- byrd@comp.vuw.ac.nz --------------------\ | Mike Williams, Victoria University of Wellington, Aotearoa. | \-------- Yesterday, I ... no, wait, that wasn't me. ---------/