about 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:
2' href='#n302'>302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674