summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorWojciech Siewierski <wojciech.siewierski@onet.pl>2016-01-24 15:43:21 +0100
committerWojciech Siewierski <wojciech.siewierski@onet.pl>2016-01-24 15:43:21 +0100
commitc7d79df4e519c8004c99f210b18634b8b834d2c3 (patch)
tree3986cb8dcffc2540c266e24972c68531ebdf2d22
parentb3c6256252c64eea4ab2e4649d0634fb8b08948e (diff)
downloadranger-c7d79df4e519c8004c99f210b18634b8b834d2c3.tar.gz
Allow :delete to take arguments
Issue #458 still applies.
-rw-r--r--ranger/config/commands.py60
-rw-r--r--ranger/core/actions.py7
2 files changed, 46 insertions, 21 deletions
diff --git a/ranger/config/commands.py b/ranger/config/commands.py
index 86db4bea..6148edf2 100644
--- a/ranger/config/commands.py
+++ b/ranger/config/commands.py
@@ -501,7 +501,8 @@ class terminal(Command):
 class delete(Command):
     """:delete
 
-    Tries to delete the selection.
+    Tries to delete the selection or the files passed in arguments (if any).
+    The arguments use a shell-like escaping.
 
     "Selection" is defined as all the "marked files" (by default, you
     can mark files with space or v). If there are no marked files,
@@ -512,42 +513,63 @@ class delete(Command):
     """
 
     allow_abbrev = False
+    escape_macros_for_shell = True
 
     def execute(self):
         import os
+        import shlex
+        from functools import partial
+        from ranger.container.file import File
+
+        def is_directory_with_files(f):
+            import os.path
+            if isinstance(f, str):
+                return (os.path.isdir(f) and not os.path.islink(f) \
+                    and len(os.listdir(f)) > 0)
+            else:
+                return (f.is_directory and not f.is_link \
+                    and len(os.listdir(f.path)) > 0)
+
+        files = None
         if self.rest(1):
-            self.fm.notify("Error: delete takes no arguments! It deletes "
-                    "the selected file(s).", bad=True)
-            return
+            files = shlex.split(self.rest(1))
+            many_files = (len(files) > 1 or is_directory_with_files(files[0]))
+        else:
+            cwd = self.fm.thisdir
+            cf = self.fm.thisfile
+            if not cwd or not cf:
+                self.fm.notify("Error: no file selected for deletion!", bad=True)
+                return
 
-        cwd = self.fm.thisdir
-        cf = self.fm.thisfile
-        if not cwd or not cf:
-            self.fm.notify("Error: no file selected for deletion!", bad=True)
-            return
+            many_files = (cwd.marked_items or is_directory_with_files(cf))
 
         confirm = self.fm.settings.confirm_on_delete
-        many_files = (cwd.marked_items or (cf.is_directory and not cf.is_link \
-                and len(os.listdir(cf.path)) > 0))
-
         if confirm != 'never' and (confirm != 'multiple' or many_files):
+            if files is None:
+                filename_list = (f.relative_path for f in self.fm.thistab.get_selection())
+            else:
+                filename_list = files
             self.fm.ui.console.ask("Confirm deletion of: %s (y/N)" %
-                ', '.join(f.relative_path for f in self.fm.thistab.get_selection()),
-                self._question_callback, ('n', 'N', 'y', 'Y'))
+                ', '.join(filename_list),
+                partial(self._question_callback, files), ('n', 'N', 'y', 'Y'))
         else:
             # no need for a confirmation, just delete
-            self._delete_with_tags()
+            self._delete_with_tags(files)
 
-    def _delete_with_tags(self):
+    def tab(self, tabnum):
+        return self._tab_directory_content()
+
+    def _delete_with_tags(self, files):
         # Delete the selected files and untag them.
         for f in self.fm.tags.tags:
             if str(f).startswith(self.fm.thisfile.path):
                 self.fm.tags.remove(f)
-        self.fm.delete()
+        self.fm.delete(files)
 
-    def _question_callback(self, answer):
+    def _question_callback(self, files, answer):
         if answer == 'y' or answer == 'Y':
-            self._delete_with_tags()
+            self._delete_with_tags(files)
+
 
 class mark_tag(Command):
     """:mark_tag [<tags>]
diff --git a/ranger/core/actions.py b/ranger/core/actions.py
index bd9be223..976d2f3d 100644
--- a/ranger/core/actions.py
+++ b/ranger/core/actions.py
@@ -1306,10 +1306,13 @@ class Actions(FileManagerAware, SettingsAware):
         self.loader.add(loadable, append=append)
         self.do_cut = False
 
-    def delete(self):
+    def delete(self, narg=None):
         # XXX: warn when deleting mount points/unseen marked files?
         self.notify("Deleting!")
-        selected = self.thistab.get_selection()
+        if narg is None:
+            selected = self.thistab.get_selection()
+        else:
+            selected = [File(os.path.expanduser(f)) for f in narg]
         self.copy_buffer -= set(selected)
         if selected:
             for f in selected: