summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ranger/api/apps.py25
-rw-r--r--ranger/defaults/apps.py82
2 files changed, 45 insertions, 62 deletions
diff --git a/ranger/api/apps.py b/ranger/api/apps.py
index 90162e0b..ad598879 100644
--- a/ranger/api/apps.py
+++ b/ranger/api/apps.py
@@ -134,3 +134,28 @@ def depends_on(*args):
 		fnc.dependencies = args
 		return fnc
 	return decorator
+
+
+def _generic_app_handler(name, detached=None):
+	assert isinstance(name, str)
+	@depends_on(name)
+	def handler(self, context):
+		if detached:
+			context.flags += "d"
+		elif not detached and detached is not None:
+			context.flags += "D"
+		return tup(name, *context)
+	return handler
+
+
+def generic(simple=(), detached=()):
+	simple = tuple(simple)
+	detached = tuple(detached)
+	def class_decorator(cls):
+		for name in simple:
+			setattr(cls, "app_" + name, _generic_app_handler(name))
+		for name in detached:
+			setattr(cls, "app_" + name,
+					_generic_app_handler(name, detached=True))
+		return cls
+	return class_decorator
diff --git a/ranger/defaults/apps.py b/ranger/defaults/apps.py
index 6c09298a..c9dfd51e 100644
--- a/ranger/defaults/apps.py
+++ b/ranger/defaults/apps.py
@@ -49,6 +49,26 @@ This example modifies the behaviour of "feh" and adds a custom media player:
 from ranger.api.apps import *
 from ranger.ext.get_executables import get_executables
 
+# Often a program is just started like this: `programname filename [...]`
+# For example: `vim test.py readme.txt`.  This can be implemented like:
+#    @depends_on("programname")
+#    def app_programname(self, c):
+#        return tup("programname", *c)
+# Instead of creating such a generic function for each program, just add
+# its name into this array:
+generic_apps = '''
+vim fceux elinks wine zsnes javac
+'''.split()
+
+# Sometimes you don't want the program to block ranger's terminal. You could
+# add this line before the return statement:
+#        c.flags += "d"   # "d" for "detached"
+# Write the name of those programs into this array:
+detached_apps = '''
+opera firefox apvlv evince zathura gimp mirage eog
+'''.split()
+
+@generic(simple=generic_apps, detached=detached_apps)
 class CustomApplications(Applications):
 	def app_default(self, c):
 		"""How to determine the default application?"""
@@ -95,10 +115,6 @@ class CustomApplications(Applications):
 	def app_pager(self, c):
 		return tup('less', *c)
 
-	@depends_on('vim')
-	def app_vim(self, c):
-		return tup('vim', *c)
-
 	def app_editor(self, c):
 		try:
 			default_editor = os.environ['EDITOR']
@@ -135,16 +151,6 @@ class CustomApplications(Applications):
 		else:
 			return tup('mplayer', *c)
 
-	@depends_on("eog")
-	def app_eye_of_gnome(self, c):
-		c.flags += 'd'
-		return tup('eog', *c)
-
-	@depends_on('mirage')
-	def app_mirage(self, c):
-		c.flags += 'd'
-		return tup('mirage', *c)
-
 	@depends_on('feh')
 	def app_feh(self, c):
 		arg = {1: '--bg-scale', 2: '--bg-tile', 3: '--bg-center'}
@@ -171,10 +177,6 @@ class CustomApplications(Applications):
 		except:
 			return tup('feh', *c)
 
-	@depends_on("gimp")
-	def app_gimp(self, c):
-		return tup('gimp', *c)
-
 	@depends_on('aunpack')
 	def app_aunpack(self, c):
 		if c.mode is 0:
@@ -182,15 +184,6 @@ class CustomApplications(Applications):
 			return tup('aunpack', '-l', c.file.path)
 		return tup('aunpack', c.file.path)
 
-	@depends_on('fceux')
-	def app_fceux(self, c):
-		return tup('fceux', *c)
-
-	@depends_on('apvlv')
-	def app_apvlv(self, c):
-		c.flags += 'd'
-		return tup('apvlv', *c)
-
 	@depends_on('make')
 	def app_make(self, c):
 		if c.mode is 0:
@@ -200,25 +193,6 @@ class CustomApplications(Applications):
 		if c.mode is 2:
 			return tup("make", "clear")
 
-	@depends_on('elinks')
-	def app_elinks(self, c):
-		c.flags += 'D'
-		return tup('elinks', *c)
-
-	@depends_on('opera')
-	def app_opera(self, c):
-		c.flags += 'd'
-		return tup('opera', *c)
-
-	@depends_on('firefox')
-	def app_firefox(self, c):
-		c.flags += 'd'
-		return tup("firefox", *c)
-
-	@depends_on('javac')
-	def app_javac(self, c):
-		return tup("javac", *c)
-
 	@depends_on('java')
 	def app_java(self, c):
 		def strip_extensions(file):
@@ -228,22 +202,6 @@ class CustomApplications(Applications):
 		files_without_extensions = map(strip_extensions, c.files)
 		return tup("java", files_without_extensions)
 
-	@depends_on('zsnes')
-	def app_zsnes(self, c):
-		return tup("zsnes", c.file.path)
-
-	@depends_on('evince')
-	def app_evince(self, c):
-		return tup("evince", *c)
-
-	@depends_on('zathura')
-	def app_zathura(self, c):
-		return tup("zathura", *c)
-
-	@depends_on('wine')
-	def app_wine(self, c):
-		return tup("wine", c.file.path)
-
 	@depends_on('totem')
 	def app_totem(self, c):
 		if c.mode is 0:
='#n336'>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