about summary refs log tree commit diff stats
path: root/ranger/keyapi.py
blob: 2159660a147ead664f8cd057889c00f3cb4b04d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
from curses import *
from curses.ascii import *
from ranger import RANGERDIR
from ranger.gui.widgets import console_mode as cmode
from ranger.container.bookmarks import ALLOWED_KEYS as ALLOWED_BOOKMARK_KEYS
from ranger import log

def make_abbreviations(command_list):
	def bind(*args):
		command_list.bind(args[-1], *args[:-1])
	
	def hint(*args):
		command_list.hint(args[-1], *args[:-1])

	return bind, hint

class Wrapper(object):
	def __init__(self, firstattr):
		self.__firstattr__ = firstattr

	def __getattr__(self, attr):
		def wrapper(*args, **keywords):
			def bla(command_argument):
				obj = getattr(command_argument, self.__firstattr__)
				if obj is None:
					return
				return getattr(obj, attr)(*args, **keywords)
			return bla
		return wrapper

fm = Wrapper('fm')
wdg = Wrapper('wdg')

# fm.enter_dir('~') is translated into lambda arg: arg.fm.enter_dir('~')
# this makes things like this possible:
# bind('gh', fm.enter_dir('~'))
#
# but NOT: (note the 2 dots)
# bind('H', fm.history.go(-1))
#
# for something like that, use the long version:
# bind('H', lambda arg: arg.fm.history.go(-1))


# Another wrapper for common actions which use a numerical argument:
class nwrap(object):
	@staticmethod
	def move(relative=0, absolute=None):
		if absolute is None:
			def fnc(arg):
				if arg.n is not None:
					if relative >= 0:
						arg.wdg.move(relative=arg.n)
					else:
						arg.wdg.move(relative=-arg.n)
				else:
					arg.wdg.move(relative=relative)
		else:
			def fnc(arg):
				if arg.n is not None:
					arg.wdg.move(absolute=arg.n, relative=relative)
				else:
					arg.wdg.move(absolute=absolute, relative=relative)
		return fnc