[comp.emacs] Dired gadget

phs@lifia.imag.fr (Philippe Schnoebelen) (11/14/90)

Here is a little gadget for Dired:

(defun dired-delete-this-file ()
  "In dired, delete the file named on this line."
  (interactive)
  (let ((buffer-read-only nil)
	(fname (dired-get-filename)))
    (if (not (y-or-n-p (format "Delete file %s " fname)))
	(message "OK, I won't.")
	;; else, do it !
	(delete-file fname)
	(delete-region (progn (beginning-of-line) (point))
		       (progn (forward-line 1) (point)))
	(message "Done"))))

I've put:
	   (define-key dired-mode-map "\C-d" 'dired-delete-this-file)

in my dired-mode-hook. (Admittedly, the choice of \C-d is poor taste. Feel
free to suggest improvements.)

Now, you can remove a file from your directory just by hitting \C-d when
you're pointing at it in Dired. 


I very much prefer this kind of interactive behavior over the usual (in
GnuEmacs) way of marking for deletion and then deleting. A similar case
could be presented for Buffer-List, or VM-summary, ...  It would be nice if
this were added _in_a_consistent_way_ in all relevant Emacs packages. What
do others think ?
--
Philippe SCHNOEBELEN,  LIFIA,  46 Av Felix VIALLET,  38000 Grenoble,  FRANCE
						     email: phs@lifia.imag.fr
"Algebraic symbols are used when you do not know what you are talking about."

rock@warp.Eng.Sun.COM (Bill Petro) (11/16/90)

phs@lifia.imag.fr (Philippe Schnoebelen) writes:

>Here is a little gadget for Dired:

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?


--
     Bill Petro  {decwrl,hplabs,ucbvax}!sun!Eng!rock
"UNIX for the sake of the kingdom of heaven"  Matthew 19:12

hollen@megatek (Dion Hollenbeck) (11/20/90)

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?

Here is some code which I did for buffer list which you should be
able to adapt to dired mode.  It was created to mark all buffers in
the buffer list for deletion.


----------------------------------------------------------------------------
;;
;; Modifications to buffer-menu-mode
;;
;;     Dion Hollenbeck 8/28/90
;;

;;  Add key to buffer menu mode
(defun buffer-menu-mode-hook-fun ()
    "Add key mapping for Buffer-menu-mark-all-delete function"
  (define-key Buffer-menu-mode-map "a" 'Buffer-menu-mark-all-delete)
  (use-local-map Buffer-menu-mode-map)
)

(setq buffer-menu-mode-hook 'buffer-menu-mode-hook-fun)

;;  Define additional function for buffer menu mode

(defun Buffer-menu-mark-all-delete ()
  "Mark all buffers to be deleted by \\[Buffer-menu-execute] command.
    Move to the end of the buffer menu."
  (interactive)
  (goto-char (point-min))
  (while (looking-at " [-M]") (forward-line 1))
  (while (looking-at "[ .]") 
    (Buffer-menu-delete)
  )
)


--
	Dion Hollenbeck             (619) 455-5590 x2814
	Megatek Corporation, 9645 Scranton Road, San Diego, CA  92121
        uunet!megatek!hollen       or  hollen@megatek.uucp

Mike.Williams@comp.vuw.ac.nz (Mike Williams) (11/21/90)

+-- 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. ---------/