Skip to content

Commit

Permalink
Make use of Trash on macOS (bug#21340)
Browse files Browse the repository at this point in the history
* src/nsfns.m (Fsystem_move_file_to_trash): New function.
  • Loading branch information
alanthird committed Dec 4, 2021
1 parent 16b8741 commit 796075e
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/nsfns.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -3243,6 +3284,10 @@ - (Lisp_Object)lispString
defsubr (&Sx_show_tip);
defsubr (&Sx_hide_tip);

#if defined (NS_IMPL_COCOA) && MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
defsubr (&Ssystem_move_file_to_trash);
#endif

as_status = 0;
as_script = Qnil;
staticpro (&as_script);
Expand Down

0 comments on commit 796075e

Please sign in to comment.