From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: owner@emacsbugs.donarmstrong.com (Emacs bug Tracking System) Newsgroups: gmane.emacs.bugs Subject: bug#973: marked as done (Support for moving files to freedesktop.org-style trashcan.) Date: Sun, 28 Jun 2009 05:05:09 +0000 Message-ID: References: <87skhl0x8h.fsf@stupidchicken.com> <48CB3695.9020708@harpegolden.net> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="----------=_1246165509-10316-0" X-Trace: ger.gmane.org 1246166295 9789 80.91.229.12 (28 Jun 2009 05:18:15 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 28 Jun 2009 05:18:15 +0000 (UTC) To: Chong Yidong Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sun Jun 28 07:18:08 2009 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1MKmmE-0001Rk-PD for geb-bug-gnu-emacs@m.gmane.org; Sun, 28 Jun 2009 07:18:07 +0200 Original-Received: from localhost ([127.0.0.1]:44849 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MKmmD-0003u0-7K for geb-bug-gnu-emacs@m.gmane.org; Sun, 28 Jun 2009 01:18:05 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MKmlg-0003Zp-Gl for bug-gnu-emacs@gnu.org; Sun, 28 Jun 2009 01:17:32 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MKmlb-0003X7-RU for bug-gnu-emacs@gnu.org; Sun, 28 Jun 2009 01:17:31 -0400 Original-Received: from [199.232.76.173] (port=57117 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MKmla-0003Wh-QP for bug-gnu-emacs@gnu.org; Sun, 28 Jun 2009 01:17:26 -0400 Original-Received: from rzlab.ucr.edu ([138.23.92.77]:43288) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1MKmla-0008Cs-7B for bug-gnu-emacs@gnu.org; Sun, 28 Jun 2009 01:17:26 -0400 Original-Received: from rzlab.ucr.edu (rzlab.ucr.edu [127.0.0.1]) by rzlab.ucr.edu (8.14.3/8.14.3/Debian-5) with ESMTP id n5S5HNLP012994; Sat, 27 Jun 2009 22:17:24 -0700 Original-Received: (from debbugs@localhost) by rzlab.ucr.edu (8.14.3/8.14.3/Submit) id n5S55AJH010481; Sat, 27 Jun 2009 22:05:10 -0700 X-Mailer: MIME-tools 5.427 (Entity 5.427) X-Loop: owner@emacsbugs.donarmstrong.com X-Emacs-PR-Message: closed 973 X-Emacs-PR-Package: emacs X-Emacs-PR-Keywords: patch X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 2) X-BeenThere: bug-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:29050 Archived-At: This is a multi-part message in MIME format... ------------=_1246165509-10316-0 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Your message dated Sun, 28 Jun 2009 00:59:42 -0400 with message-id <87skhl0x8h.fsf@stupidchicken.com> and subject line Re: Support for moving files to freedesktop.org-style tras= hcan has caused the Emacs bug report #973, regarding Support for moving files to freedesktop.org-style trashcan. to be marked as done. This means that you claim that the problem has been dealt with. If this is not the case it is now your responsibility to reopen the bug report if necessary, and/or fix the problem forthwith. (NB: If you are a system administrator and have no idea what this message is talking about, this may indicate a serious mail system misconfiguration somewhere. Please contact owner@emacsbugs.donarmstrong.com immediately.) --=20 973: http://emacsbugs.donarmstrong.com/cgi-bin/bugreport.cgi?bug=3D973 Emacs Bug Tracking System Contact owner@emacsbugs.donarmstrong.com with problems ------------=_1246165509-10316-0 Content-Type: message/rfc822 Content-Disposition: inline Content-Transfer-Encoding: 7bit X-Spam-Checker-Version: SpamAssassin 3.2.3-bugs.debian.org_2005_01_02 (2007-08-08) on rzlab.ucr.edu X-Spam-Level: X-Spam-Status: No, score=-7.2 required=4.0 tests=AWL,BAYES_00,FOURLA, FVGT_m_MULTI_ODD,HAS_PACKAGE,MIXEDBDN,MURPHY_DRUGS_REL8,SPF_HELO_PASS autolearn=ham version=3.2.3-bugs.debian.org_2005_01_02 Received: (at submit) by emacsbugs.donarmstrong.com; 13 Sep 2008 03:42:23 +0000 Received: from harpegolden.net (harpegolden.net [65.99.215.13]) by rzlab.ucr.edu (8.13.8/8.13.8/Debian-3) with ESMTP id m8D3gIb1026134 for ; Fri, 12 Sep 2008 20:42:19 -0700 Received: from golden1.harpegolden.net (unknown [86.45.15.116]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client CN "David De La Harpe Golden", Issuer "David De La Harpe Golden Personal CA rev 3" (verified OK)) by harpegolden.net (Postfix) with ESMTP id 073DA81AD for ; Sat, 13 Sep 2008 04:42:16 +0100 (IST) Message-ID: <48CB3695.9020708@harpegolden.net> Date: Sat, 13 Sep 2008 04:42:13 +0100 From: David De La Harpe Golden User-Agent: Mozilla-Thunderbird 2.0.0.16 (X11/20080724) MIME-Version: 1.0 To: submit@emacsbugs.donarmstrong.com Subject: Support for moving files to freedesktop.org-style trashcan. X-Enigmail-Version: 0.95.0 Content-Type: multipart/mixed; boundary="------------080203040307000007020309" This is a multi-part message in MIME format. --------------080203040307000007020309 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Package: emacs Version: 23.0.60 Severity: normal Tags: patch delete-by-moving-to-trash was recently introduced, but doesn't really work as expected for typical gnu+linux desktop users, and I had some procrastination to do, so... Attached patch adds basic support for moving files to a fd.o-style trashcan, as used in KDE/GNOME/XFCE/etc. and documented at: http://www.freedesktop.org/wiki/Specifications/trash-spec Unlike "Microsoft Windows? -> use Windows Recycle Bin" or "MacOSX? -> use MacOSX trash can", one maybe can't say "GNU/Linux/BSD? -> use freedesktop-style Trash", so whether the freedesktop-style trashcan should be preferred over the simple emacs fallback "trash-directory" scheme is decided a bit roughly. Maybe a move-file-to-trash-scheme should just be a user-visible customisation. ... I hate trashcans anyway. --------------080203040307000007020309 Content-Type: text/x-patch; name="move-file-to-trash-freedesktop_r1.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="move-file-to-trash-freedesktop_r1.diff" Index: lisp/files.el =================================================================== RCS file: /sources/emacs/emacs/lisp/files.el,v retrieving revision 1.995 diff -U 8 -r1.995 files.el --- lisp/files.el 2 Sep 2008 16:10:44 -0000 1.995 +++ lisp/files.el 13 Sep 2008 03:05:31 -0000 @@ -5793,24 +5793,165 @@ 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-freedesktop (filename) + "Attempt to move file/dir to trash the freedesktop.org way (like e.g. GNOME, +KDE and XFCE desktop environment trash facilities). Usually called automatically +by `move-file-to-trash'. + +Only moves to 'home trash', per-volume trash storage areas are never used, as +permitted by freedesktop.org trash-spec 0.7." + + (interactive "fMove file to trash: ") + (let* ((saved-default-file-modes (default-file-modes)) + (xdg-data-home-dir (directory-file-name (expand-file-name + (or (getenv "XDG_DATA_HOME") + "~/.local/share")))) + (trash-dir (concat xdg-data-home-dir "/Trash")) + (trash-files-dir (concat trash-dir "/files")) + (trash-info-dir (concat trash-dir "/info")) + (fn (directory-file-name (expand-file-name filename))) + (fn-nondir (file-name-nondirectory fn))) + + ;; trash-spec 0.7 says to check if we have permissions to + ;; delete before attempting to delete. + (unless (file-writable-p (directory-file-name (file-name-directory fn))) + (error "Cannot move file `%s' to Trash, insufficient permissions" filename)) + + ;; stop processing if fn is same or parent directory of trash-dir. + (when (or (string-match fn trash-files-dir) + (string-match fn trash-info-dir) + (string-match fn trash-dir)) + (error "Filename `%s' is same or parent directory of Trash." + filename)) + + ;; ensure trash directory exists, using appropriate permissions if creating it. + (set-default-file-modes #o700) + (make-directory trash-files-dir t) + (make-directory trash-info-dir t) + (set-default-file-modes saved-default-file-modes) + + ;; try to move to trash with associated .trashinfo undo information + (save-excursion + (with-temp-buffer + (set-buffer-file-coding-system 'utf-8-unix) + ;; url-encode path, though allow literal "/" for path separators. + ;; trash-spec 0.7 is slightly confusing on that point. At time of writing, + ;; KDE Konqueror accepts both literal and %2f-encoded slashes, XFCE Thunar + ;; only literal slashes. + ;; Using / makes .trashinfo files much more readable, and while / is a + ;; reserved character in url components, it is a valid separator in urls, + ;; so the intent was likely to allow it. + ;; yes, this is lifted from url-hexify-string + ;; but don't want to pull in url/*.el , and it's not quite the same due to / + (insert "[Trash Info]\n" + "Path=" (mapconcat + (lambda (byte) + (if (memq byte + '(?/ ?a ?b ?c ?d ?e ?f ?g ?h ?i ?j + ?k ?l ?m ?n ?o ?p ?q ?r ?s ?t + ?u ?v ?w ?x ?y ?z ?A ?B ?C ?D + ?E ?F ?G ?H ?I ?J ?K ?L ?M ?N + ?O ?P ?Q ?R ?S ?T ?U ?V ?W ?X + ?Y ?Z ?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 + ?8 ?9 ?- ?_ ?. ?! ?~ ?* ?' ?\( ?\))) + (char-to-string byte) + (format "%%%02x" byte))) + (if (multibyte-string-p fn) + (encode-coding-string fn 'utf-8) + fn) + "") + "\nDeletionDate=" (format-time-string "%Y-%m-%dT%T") "\n") + (let* ((maxtries 5) + (tries maxtries) + (success nil) + (base-fn (expand-file-name fn-nondir trash-files-dir)) + (new-fn base-fn) + (info-fn (concat new-fn ".trashinfo"))) + ;; attempt to make .trashinfo file, retries up to 5 + ;; times (arbitrarily chosen, spec just says "pick + ;; another filename"). .trashinfo file opened o_excl _is_ + ;; lock as per trash-spec 0.7 (even if that can be a + ;; problem on old NFS versions...) + (while (and (not success) (> tries 0)) + ;; make new-fn unique. + ;; Unfortunately, contemporary file managers with fd.o trashcan support + ;; do not like emacs backup file naming scheme here (as used in default emacs + ;; trash implementation) - see bug 4381 in XFCE Thunar bug tracker, + ;; bug 170956 in KDE Konqueror bug tracker. + ;; (let ((version-control t)) + ;; (setq new-fn (car (find-backup-file-name base-fn)))) + (when (< tries maxtries) + (setq new-fn (make-temp-name (concat base-fn "_")))) + (setq info-fn (concat (expand-file-name (file-name-nondirectory new-fn) + trash-info-dir) + ".trashinfo")) + (unless (condition-case nil + (progn + (write-region nil nil info-fn nil 'quiet info-fn 'excl) + (setq success t)) + (file-already-exists nil)) + (setq tries (- tries 1)) + (sleep-for 0.1))) + (unless success (error "Failed to lock Trash for filename `%s'" filename)) + ;; Finally... if we've got this far, let's + ;; try to actually move the file to the trashcan. + (let ((delete-by-moving-to-trash nil)) + (rename-file fn new-fn))))))) + +(defvar move-file-to-trash--freedesktop-p-memo nil) +(defun move-file-to-trash--freedesktop-p () + "Guess if the system should be considered freedesktop.org -oid +for `move-file-to-trash' purposes. Only used in absence of +overriding `system-move-file-to-trash' " + ;; presumably constant throughout an emacs session. + (or move-file-to-trash--freedesktop-p-memo + (setq move-file-to-trash--freedesktop-p-memo + (if (and + ;; assume macosx and windows folk are going to want their own + ;; trashcans even though people might sometimes compile + ;; and run freedesktop.org apps on them. + (not (eq system-type 'darwin)) + (not (eq system-type 'windows)) + (or + ;; dead giveaway. + (file-exists-p "~/.local/share/Trash") + (getenv "XDG_DATA_HOME") + (getenv "XDG_CONFIG_HOME") + (getenv "XDG_DATA_DIRS") + (getenv "XDG_CONFIG_DIRS") + (getenv "XDG_CACHE_HOME") + (file-exists-p "/etc/xdg") + (file-exists-p "~/.local") + (file-exists-p "~/.config"))) + t + nil)))) + (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'. +system trash, otherwise on systems that appear to be using a +freedesktop.org compliant trashcan `move-file-to-trash-freedesktop' +is used (and bound to `system-move-file-to-trash'), otherwise +FILENAME is moved to `trash-directory'. Returns nil on success." (interactive "fMove file to trash: ") + (unless (fboundp 'system-move-file-to-trash) + (when (move-file-to-trash--freedesktop-p) + (fset 'system-move-file-to-trash + (lambda (filename) + (move-file-to-trash-freedesktop filename))))) (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))) --------------080203040307000007020309 Content-Type: text/plain; name="ChangeLog.move-file-to-trash-freedesktop_r1" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="ChangeLog.move-file-to-trash-freedesktop_r1" 2008-09-13 David De La Harpe Golden * files.el: Add basic support for moving files to freedesktop.org Trash cans (as used by KDE/GNOME/XFCE/etc.). --------------080203040307000007020309-- ------------=_1246165509-10316-0 Content-Type: message/rfc822 Content-Disposition: inline Content-Transfer-Encoding: 7bit Received: (at 973-done) by emacsbugs.donarmstrong.com; 28 Jun 2009 04:59:55 +0000 X-Spam-Checker-Version: SpamAssassin 3.2.5-bugs.debian.org_2005_01_02 (2008-06-10) on rzlab.ucr.edu X-Spam-Level: X-Spam-Bayes: score:0.5 Bayes not run. spammytokens:Tokens not available. hammytokens:Tokens not available. X-Spam-Status: No, score=0.2 required=4.0 tests=AWL,URIBL_RHS_DOB autolearn=no version=3.2.5-bugs.debian.org_2005_01_02 Received: from pantheon-po31.its.yale.edu (pantheon-po31.its.yale.edu [130.132.50.82]) by rzlab.ucr.edu (8.14.3/8.14.3/Debian-5) with ESMTP id n5S4xp9i009058 for <973-done@emacsbugs.donarmstrong.com>; Sat, 27 Jun 2009 21:59:52 -0700 Received: from furry (c-71-192-161-14.hsd1.nh.comcast.net [71.192.161.14]) (authenticated bits=0) by pantheon-po31.its.yale.edu (8.12.11.20060308/8.12.11) with ESMTP id n5S4xjTA011528 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Sun, 28 Jun 2009 00:59:45 -0400 Received: by furry (Postfix, from userid 1000) id 20DCFC09B; Sun, 28 Jun 2009 00:59:42 -0400 (EDT) From: Chong Yidong To: David De La Harpe Golden Cc: 973-done@emacsbugs.donarmstrong.com Subject: Re: Support for moving files to freedesktop.org-style trashcan Date: Sun, 28 Jun 2009 00:59:42 -0400 Message-ID: <87skhl0x8h.fsf@stupidchicken.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-YaleITSMailFilter: Version 1.2c (attachment(s) not renamed) Hi David, I've checked your code for freedesktop.org-style trashing into CVS trunk, with modifications. > Unlike "Microsoft Windows? -> use Windows Recycle Bin" or "MacOSX? -> > use MacOSX trash can", one maybe can't say "GNU/Linux/BSD? -> use > freedesktop-style Trash", so whether the freedesktop-style trashcan > should be preferred over the simple emacs fallback "trash-directory" > scheme is decided a bit roughly. I changed it so that Emacs supports freedesktop.org style trashing whenever `trash-directory' is nil, the new default. (This is subject, as before, to the `system-move-file-to-trash' override used under Windows.) Fancy autodetection is not worth it, since the user has to manually enable this feature anyway. Thanks very much for writing the code. ------------=_1246165509-10316-0--