summaryrefslogtreecommitdiff
path: root/lisp/files.el
diff options
context:
space:
mode:
authorJason Rumney <jasonr@gnu.org>2008-07-02 13:19:07 +0000
committerJason Rumney <jasonr@gnu.org>2008-07-02 13:19:07 +0000
commit6cf29fe81529606336b0d1ac167af5a62b9843a3 (patch)
treece0ce54a6ec8cad9315b1a7caaf5d3036e6325c2 /lisp/files.el
parent56fd9faa2f35556996de5777734663de5ad235e3 (diff)
downloademacs-6cf29fe81529606336b0d1ac167af5a62b9843a3.tar.gz
Changes from Toru Tsuneyoshi for using Trash can when deleting files.
* files.el (backup-extract-version): Handle versioned directories. (trash-directory): New variable. (move-file-to-trash): New function. * cus-start.el (delete-by-moving-to-trash): Declare for custom. * lisp.h (Qdelete_file, Qdelete_directory): Declare extern. * fileio.c (delete_by_moving_to_trash, Qmove_file_to_trash): New vars. (syms_of_fileio): Initialize and export them. (Fdelete_directory, Fdelete_file): Optionally delete via trash. * w32fns.c (FOF_NO_CONNECTED_ELEMENTS): Define if not already. (Fsystem_move_file_to_trash): New function. (syms_of_w32fns): Export it to lisp.
Diffstat (limited to 'lisp/files.el')
-rw-r--r--lisp/files.el45
1 files changed, 43 insertions, 2 deletions
diff --git a/lisp/files.el b/lisp/files.el
index b38871096cc..cb9720dc702 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -31,7 +31,6 @@
(defvar font-lock-keywords)
-
(defgroup backup nil
"Backups of edited data files."
:group 'files)
@@ -3693,7 +3692,7 @@ You may need to redefine `file-name-sans-versions' as well."
"Given the name of a numeric backup file, FN, return the backup number.
Uses the free variable `backup-extract-version-start', whose value should be
the index in the name where the version number begins."
- (if (and (string-match "[0-9]+~$" fn backup-extract-version-start)
+ (if (and (string-match "[0-9]+~/?$" fn backup-extract-version-start)
(= (match-beginning 0) backup-extract-version-start))
(string-to-number (substring fn backup-extract-version-start -1))
0))
@@ -5783,6 +5782,48 @@ ORIG-FILE is the original file of which modes will be change."
(file-modes-symbolic-to-number value modes)))))
+;; Trash can handling.
+(defcustom trash-directory "~/.Trash"
+ "Directory for `move-file-to-trash' to move files and directories to.
+This directory is only used when the function `system-move-file-to-trash' is
+not defined. Relative paths are interpreted relative to `default-directory'.
+See also `delete-by-moving-to-trash'."
+ :type 'directory
+ :group 'auto-save
+ :version "23.1")
+
+(declare-function system-move-file-to-trash "w32fns.c" (filename))
+
+(defun move-file-to-trash (filename)
+ "Move file (or directory) name FILENAME to the trash.
+This function is called by `delete-file' and `delete-directory' when
+`delete-by-moving-to-trash' is non-nil. On platforms that define
+`system-move-file-to-trash', that function is used to move FILENAME to the
+system trash, otherwise FILENAME is moved to `trash-directory'.
+Returns nil on success."
+ (interactive "fMove file to trash: ")
+ (cond
+ ((fboundp 'system-move-file-to-trash)
+ (system-move-file-to-trash filename))
+ (t
+ (let* ((trash-dir (expand-file-name trash-directory))
+ (fn (directory-file-name (expand-file-name filename)))
+ (fn-nondir (file-name-nondirectory fn))
+ (new-fn (expand-file-name fn-nondir trash-dir)))
+ (or (file-directory-p trash-dir)
+ (make-directory trash-dir t))
+ (and (file-exists-p new-fn)
+ ;; make new-fn unique.
+ ;; example: "~/.Trash/abc.txt" -> "~/.Trash/abc.txt.~1~"
+ (let ((version-control t))
+ (setq new-fn (car (find-backup-file-name new-fn)))))
+ ;; stop processing if fn is same or parent directory of trash-dir.
+ (and (string-match fn trash-dir)
+ (error "Filename `%s' is same or parent directory of trash-directory"
+ filename))
+ (rename-file fn new-fn)))))
+
+
(define-key ctl-x-map "\C-f" 'find-file)
(define-key ctl-x-map "\C-r" 'find-file-read-only)
(define-key ctl-x-map "\C-v" 'find-alternate-file)