Subject: Re: bug#21340: 24.5; trash-directory uses FreeDesktop location on OSX

From: Alan Third <[email-masked]>
Date: Fri, 3 Dec 2021 23:02:31 +0000

On Fri, Dec 03, 2021 at 05:16:20PM +0100, Lars Ingebrigtsen wrote:
> Alan Third <[email-masked]> writes:
> 
> > It looks like there's a method available to trash items on macOS 10.8
> > and above, so I suppose that's the logical thing to use.
> >
> > https://developer.apple.com/documentation/foundation/nsfilemanager/1414306-trashitematurl?language=objc
> >
> > macOS < 10.8 uses some Carbon API, which I'd rather not bother with,
> > and GNUstep doesn't support it at all, so I imagine it's as well using
> > the XDG location or whatever the default is.
> 
> Sounds good to me.

Well, I've written a patch but the function doesn't appear in lisp and
I don't know what I've done wrong.

Patch attached.
-- 
Alan Third
>From 4f21ff07ad9b9d06960a9ea6d000e0636f49c3d1 Mon Sep 17 00:00:00 2001
From: Alan Third <[email-masked]>
Date: Fri, 3 Dec 2021 22:17:45 +0000
Subject: [PATCH] Make use of Trash on macOS (bug#21340)

* src/nsfns.m (Fsystem_move_file_to_trash): New function.
---
 src/nsfns.m | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/src/nsfns.m b/src/nsfns.m
index c2791aa15a..5e319e1915 100644
--- a/src/nsfns.m
+++ b/src/nsfns.m
@@ -2362,6 +2362,47 @@ Frames are listed from topmost (first) to bottommost (last).  */)
    ========================================================================== */

+#if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
+/* Moving files to the system recycle bin.
+   Used by `move-file-to-trash' instead of the default moving to ~/.Trash  */
+DEFUN ("system-move-file-to-trash", Fsystem_move_file_to_trash,
+       Ssystem_move_file_to_trash, 1, 1, 0,
+       doc: /* Move file or directory named FILENAME to the recycle bin.  */)
+  (Lisp_Object filename)
+{
+  Lisp_Object handler;
+  Lisp_Object operation;
+
+  operation = Qdelete_file;
+  if (!NILP (Ffile_directory_p (filename))
+      && NILP (Ffile_symlink_p (filename)))
+    {
+      operation = intern ("delete-directory");
+      filename = Fdirectory_file_name (filename);
+    }
+
+  /* Must have fully qualified file names for moving files to Trash. */
+  filename = Fexpand_file_name (filename, Qnil);
+
+  handler = Ffind_file_name_handler (filename, operation);
+  if (!NILP (handler))
+    return call2 (handler, operation, filename);
+  else
+    {
+      NSFileManager *fm = [NSFileManager defaultManager];
+      BOOL result = NO;
+      NSURL *fileURL = [NSURL fileURLWithPath:[NSString stringWithLispString:filename]
+                                  isDirectory:!NILP (Ffile_directory_p (filename))];
+      if ([fm respondsToSelector:@selector(trashItemAtURL:resultingItemURL:error:)])
+        result = [fm trashItemAtURL:fileURL resultingItemURL:nil error:nil];
+
+      if (!result)
+	report_file_error ("Removing old name", list1 (filename));
+    }
+  return Qnil;
+}
+#endif
+
 DEFUN ("xw-color-defined-p", Fxw_color_defined_p, Sxw_color_defined_p, 1, 2, 0,
        doc: /* SKIP: real doc in xfns.c.  */)
      (Lisp_Object color, Lisp_Object frame)
-- 
2.33.0