Created
August 25, 2016 14:10
mc^2: make the <F8> key delete files using the 'trash' command.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--[[ | |
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