Skip to content

Instantly share code, notes, and snippets.

@mooffie
Created August 25, 2016 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mooffie/4111a39e18934e2b4e7001c1c0cd3213 to your computer and use it in GitHub Desktop.
Save mooffie/4111a39e18934e2b4e7001c1c0cd3213 to your computer and use it in GitHub Desktop.
mc^2: make the <F8> key delete files using the 'trash' command.
--[[
Makes the <F8> key delete files using the 'trash' command.
You can circumvent this by pressing shift-<F8>.
]]
--
-- The command to run. You may need to modify this on your system.
--
-- * The space at front should prevent most shells from storing the
-- command in the history (when not use_gui).
--
-- * The "--" makes it possible to delete files like "--help".
--
-- * The %q is replaced with a path to a temporary file listing all
-- the files to delete.
--
local command_template = " xargs -0 trash -- < %q"
--
-- The user experience:
--
-- * When "use_gui" is true, deletion will occur "inside" MC while
-- a "Trashing..." sign is shown.
--
-- * When "use_gui" is false, deletion will occur at the shell, as
-- if you typed the command at the prompt.
--
-- It will be easier for you to debug the command (if need be)
-- when "use_gui" is false.
--
local use_gui = false
ui.Panel.bind("f8", function(pnl)
if not pnl.vdir:is_local() then
-- We're inside an archive or something. Can't use trash. Hand it over to MC.
return false
end
if not pnl.marked and pnl.current == ".." then
abort(T"Please select the files to delete.")
end
local files = pnl.marked or { pnl.current }
if prompts.confirm(T"Delete %d files?":format(#files)) then
local tempf = fs.temporary_string_file(table.concat(files, "\0"))
local command = command_template:format(tempf)
if use_gui then
prompts.please_wait(T"Trashing files...", function()
os.execute(command)
pnl:reload()
end)
else
mc.execute(command)
end
fs.unlink(tempf)
end
end)
-- Bind the old behavior to shift-f8.
ui.Panel.bind("f18", function(pnl)
pnl.dialog:command "delete"
end)
-- Here's a little secret: MC uses shift-f8 to run the DeleteSingle command.
-- So we bind *this* command to another key, in case you want to use it.
ui.Panel.bind("C-x f8", function(pnl)
pnl:command "DeleteSingle"
end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment