file manager
about summary refs log tree commit diff stats
path: root/ranger/defaults/keys.py
blob: 55f8331d17eca84ad682aa30b74a71a055f0e8bb (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
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
# Copyright (C) 2009, 2010  Roman Zimbelmann <romanz@lavabit.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

"""
This is the default key configuration file of ranger.
Syntax for binding keys: map(*keys, fnc)

keys are one or more key-combinations which are either:
* a string
* an integer which represents an ascii code
* a tuple of integers

fnc is a function which is called with the CommandArgument object.

The CommandArgument object has these attributes:
arg.fm: the file manager instance
arg.wdg: the widget or ui instance
arg.n: the number typed before the key combination (if allowed)
arg.keys: the string representation of the used key combination
arg.keybuffer: the keybuffer instance

Check ranger.keyapi for more information
"""

# NOTE: The "map" object used below is a callable CommandList
# object and NOT the builtin python map function!

from ranger.api.keys import *

def _vimlike_aliases(map):
	alias = map.alias

	# the key 'k' will always do the same as KEY_UP, etc.
	alias(KEY_UP, 'k')
	alias(KEY_DOWN, 'j')
	alias(KEY_LEFT, 'h')
	alias(KEY_RIGHT, 'l')

	alias(KEY_NPAGE, ctrl('f'))
	alias(KEY_PPAGE, ctrl('b'))
	alias(KEY_HOME, 'gg')
	alias(KEY_END, 'G')


def _emacs_aliases(map):
	alias = map.alias
	alias(KEY_LEFT, ctrl('b'))
	alias(KEY_RIGHT, ctrl('f'))
	alias(KEY_HOME, ctrl('a'))
	alias(KEY_END, ctrl('e'))
	alias(KEY_DC, ctrl('d'))
	alias(DEL, ctrl('h'))


def initialize_commands(map):
	"""Initialize the commands for the main user interface"""

	# -------------------------------------------------------- movement
	_vimlike_aliases(map)
	_basic_movement(map)

	map.alias(KEY_LEFT, KEY_BACKSPACE, DEL)
	map.alias(KEY_RIGHT, KEY_ENTER, ctrl('j'))

	map('%', fm.move(to=50, percentage=True))
	map(KEY_NPAGE, ctrl('f'), fm.move(down=1, pages=True))
	map(KEY_PPAGE, ctrl('b'), fm.move(up=1, pages=True))
	map(ctrl('d'), 'J', fm.move(down=0.5, pages=True))
	map(ctrl('u'), 'K', fm.move(up=0.5, pages=True))

	map(']', fm.traverse())
	map('[', fm.history_go(-1))

	# --------------------------------------------------------- history
	map('H', fm.history_go(-1))
	map('L', fm.history_go(1))

	# ----------------------------------------------- tagging / marking
	map('t', fm.tag_toggle())
	map('T', fm.tag_remove())

	map(' ', fm.mark(toggle=True))
	map('v', fm.mark(all=True, toggle=True))
	map('V', fm.mark(all=True, val=False))

	# ------------------------------------------ file system operations
	map('yy', fm.copy())
	map('dd', fm.cut())
	map('pp', fm.paste())
	map('po', fm.paste(overwrite=True))
	map('pl', fm.paste_symlink())
	map('p', hint='press //p// once again to confirm pasting' \
			', or //l// to create symlinks')

	# ---------------------------------------------------- run programs
	map('s', fm.execute_command(os.environ['SHELL']))
	map('E', fm.edit_file())
	map(',term', fm.execute_command('x-terminal-emulator', flags='d'))
	map('du', fm.execute_command('du --max-depth=1 -h | less'))

	# -------------------------------------------------- toggle options
	map('b', fm.notify('Warning: settings are now changed with z!', bad=True))
	map('z', hint="show_//h//idden //p//review_files //d//irectories_first " \
		"//c//ollapse_preview flush//i//nput ca//s//e_insensitive")
	map('zh', fm.toggle_boolean_option('show_hidden'))
	map('zp', fm.toggle_boolean_option('preview_files'))
	map('zP', fm.toggle_boolean_option('preview_directories'))
	map('zi', fm.toggle_boolean_option('flushinput'))
	map('zd', fm.toggle_boolean_option('sort_directories_first'))
	map('zc', fm.toggle_boolean_option('collapse_preview'))
	map('zs', fm.toggle_boolean_option('sort_case_insensitive'))

	# ------------------------------------------------------------ sort
	map('o', 'O', hint="//s//ize //b//ase//n//ame //m//time //t//ype //r//everse")
	sort_dict = {
		's': 'size',
		'b': 'basename',
		'n': 'basename',
		'm': 'mtime',
		't': 'type',
	}

	for key, val in sort_dict.items():
		for key, is_capital in ((key, False), (key.upper(), True)):
			# reverse if any of the two letters is capital
			map('o' + key, fm.sort(func=val, reverse=is_capital))
			map('O' + key, fm.sort(func=val, reverse=True))

	map('or', 'Or', 'oR', 'OR', lambda arg: \
			arg.fm.sort(reverse=not arg.fm.settings.sort_reverse))

	# ----------------------------------------------- console shortcuts
	@map("A")
	def append_to_filename(arg):
		command = 'rename ' + arg.fm.env.cf.basename
		arg.fm.open_console(cmode.COMMAND, command)

	map('cw', fm.open_console(cmode.COMMAND, 'rename '))
	map('cd', fm.open_console(cmode.COMMAND, 'cd '))
	map('f', fm.open_console(cmode.COMMAND_QUICK, 'find '))
	map('tf', fm.open_console(cmode.COMMAND, 'filter '))
	map('d', hint='d//u// (disk usage) d//d// (cut)')
	map('@', fm.open_console(cmode.OPEN, '@'))
	map('#', fm.open_console(cmode.OPEN, 'p!'))

	# --------------------------------------------- jump to directories
	map('gh', fm.cd('~'))
	map('ge', fm.cd('/etc'))
	map('gu', fm.cd('/usr'))
	map('gd', fm.cd('/dev'))
	map('gl', fm.cd('/lib'))
	map('go', fm.cd('/opt'))
	map('gv', fm.cd('/var'))
	map('gr', 'g/', fm.cd('/'))
	map('gm', fm.cd('/media'))
	map('gn', fm.cd('/mnt'))
	map('gt', fm.cd('/tmp'))
	map('gs', fm.cd('/srv'))
	map('gR', fm.cd(RANGERDIR))

	# ------------------------------------------------------------ tabs
	map('gc', ctrl('W'), fm.tab_close())
	map('gt', TAB, fm.tab_move(1))
	map('gT', KEY_BTAB, fm.tab_move(-1))
	map('gn', ctrl('N'), fm.tab_new())
	for n in range(10):
		map('g' + str(n), fm.tab_open(n))

	# ------------------------------------------------------- searching
	map('/', fm.open_console(cmode.SEARCH))

	map('n', fm.search())
	map('N', fm.search(forward=False))

	map('ct', fm.search(order='tag'))
	map('cc', fm.search(order='ctime'))
	map('cm', fm.search(order='mimetype'))
	map('cs', fm.search(order='size'))
	map('c', hint='//c//time //m//imetype //s//ize //t//agged')

	# ------------------------------------------------------- bookmarks
	for key in ALLOWED_BOOKMARK_KEYS:
		map("`" + key, "'" + key, fm.enter_bookmark(key))
		map("m" + key, fm.set_bookmark(key))
		map("um" + key, fm.unset_bookmark(key))
	map("`", "'", "m", "um", draw_bookmarks=True)

	# ---------------------------------------------------- change views
	map('i', fm.display_file())
	map(ctrl('p'), fm.display_log())
	map('?', KEY_F1, fm.display_help())
	map('w', lambda arg: arg.fm.ui.open_taskview())

	# ---------------------------------------------------------- custom
	# This is useful to track watched episode of a series.
	@bind(']')
	def tag_next_and_run(arg):
		fm = arg.fm
		fm.tag_remove()
		fm.tag_remove(movedown=False)
		fm.tag_toggle()
		fm.move_pointer(relative=-2)
		fm.move_right()
		fm.move_pointer(relative=1)

	# "enter" = shortcut for "1l"
	bind(KEY_ENTER, ctrl('j'), fm.move_right(mode=1))

	# ------------------------------------------------ system functions
	_system_functions(map)
	map('ZZ', 'ZQ', fm.exit())
	map(ctrl('R'), fm.reset())
	map('R', fm.reload_cwd())
	@map(ctrl('C'))
	def ctrl_c(arg):
		try:
			item = arg.fm.loader.queue[0]
		except:
			arg.fm.notify("Type Q or :quit<Enter> to exit Ranger")
		else:
			arg.fm.notify("Aborting: " + item.get_description())
			arg.fm.loader.remove(index=0)

	map(':', ';', fm.open_console(cmode.COMMAND))
	map('>', fm.open_console(cmode.COMMAND_QUICK))
	map('!', fm.open_console(cmode.OPEN))
	map('r', fm.open_console(cmode.OPEN_QUICK))

	map.rebuild_paths()


def initialize_console_commands(map):
	"""Initialize the commands for the console widget only"""

	_basic_movement(map)
	_emacs_aliases(map)

	# -------------------------------------------------------- movement
	map(KEY_UP, wdg.history_move(-1))
	map(KEY_DOWN, wdg.history_move(1))
	map(KEY_HOME, wdg.move(right=0, absolute=True))
	map(KEY_END, wdg.move(right=-1, absolute=True))

	# ----------------------------------------- deleting / pasting text
	map(KEY_DC, wdg.delete(0))
	map(KEY_BACKSPACE, DEL, wdg.delete(-1))
	map(ctrl('w'), wdg.delete_word())
	map(ctrl('k'), wdg.delete_rest(1))
	map(ctrl('u'), wdg.delete_rest(-1))
	map(ctrl('y'), wdg.paste())

	# ------------------------------------------------ system functions
	map(KEY_F1, lambda arg: arg.fm.display_command_help(arg.wdg))
	map(ctrl('c'), ESC, wdg.close())
	map(ctrl('j'), KEY_ENTER, wdg.execute())
	map(TAB, wdg.tab())
	map(KEY_BTAB, wdg.tab(-1))

	map.rebuild_paths()


def initialize_taskview_commands(map):
	"""Initialize the commands for the TaskView widget"""
	_basic_movement(map)
	_vimlike_aliases(map)
	_system_functions(map)

	# -------------------------------------------------- (re)move tasks
	map('K', wdg.task_move(0))
	map('J', wdg.task_move(-1))
	map('dd', wdg.task_remove())

	# ------------------------------------------------ system functions
	map('?', fm.display_help())
	map('w', 'q', ESC, ctrl('d'), ctrl('c'),
			lambda arg: arg.fm.ui.close_taskview())

	map.rebuild_paths()


def initialize_pager_commands(map):
	_base_pager_commands(map)
	map('q', 'i', ESC, KEY_F1, lambda arg: arg.fm.ui.close_pager())
	map.rebuild_paths()


def initialize_embedded_pager_commands(map):
	_base_pager_commands(map)
	map('q', 'i', ESC, lambda arg: arg.fm.ui.close_embedded_pager())
	map.rebuild_paths()


def _base_pager_commands(map):
	_basic_movement(map)
	_vimlike_aliases(map)
	_system_functions(map)

	# -------------------------------------------------------- movement
	map(KEY_LEFT, wdg.move(left=4))
	map(KEY_RIGHT, wdg.move(right=4))
	map(KEY_NPAGE, ctrl('f'), wdg.move(down=1, pages=True))
	map(KEY_PPAGE, ctrl('b'), wdg.move(up=1, pages=True))
	map(ctrl('d'), wdg.move(down=0.5, pages=True))
	map(ctrl('u'), wdg.move(up=0.5, pages=True))
	map(' ', wdg.move(down=0.8, pages=True))

	# ---------------------------------------------------------- others
	map('E', fm.edit_file())
	map('?', fm.display_help())

	# --------------------------------------------- less-like shortcuts
	map.alias(KEY_NPAGE, 'f')
	map.alias(KEY_PPAGE, 'b')
	map.alias(ctrl('d'), 'd')
	map.alias(ctrl('u'), 'u')


def _system_functions(map):
	map('Q', fm.exit())
	map(ctrl('L'), fm.redraw_window())


def _basic_movement(map):
	map(KEY_DOWN, wdg.move(down=1))
	map(KEY_UP, wdg.move(up=1))
	map(KEY_RIGHT, wdg.move(right=1))
	map(KEY_LEFT, wdg.move(left=1))
	map(KEY_HOME, wdg.move(to=0))
	map(KEY_END, wdg.move(to=-1))



# ------ newkey:


def base_directions():
	# Direction Keys
	map = KeyMap()
	map('<down>', dir=Direction(down=1))
	map('<up>', dir=Direction(down=-1))
	map('<left>', dir=Direction(right=-1))
	map('<right>', dir=Direction(right=1))
	map('<home>', dir=Direction(down=0, absolute=True))
	map('<end>', dir=Direction(down=-1, absolute=True))
	map('<pagedown>', dir=Direction(down=1, pages=True))
	map('<pageup>', dir=Direction(down=-1, pages=True))
	map('%<any>', dir=Direction(down=1, percentage=True, absolute=True))
	map('<space>', dir=Direction(down=1, pages=True))
	map('<CR>', dir=Direction(down=1))

	return map

def vim():
	# Direction Keys
	map = KeyMap()
	map.merge(base_directions())
	map('j', alias='<down>')
	map('k', alias='<up>')
	map('h', alias='<left>')
	map('l', alias='<right>')
	map('gg', alias='<home>')
	map('G', alias='<end>')
	map('J', dir=Direction(down=20))
	map('K', dir=Direction(down=-20))

	return map

def system_keys():
	map = KeyMap()
	map('Q', fm.exit())
	map('<mouse>', fm.handle_mouse())
	map('<C-L>', fm.redraw_window())
	map('<resize>', fm.resize())

	return map

def browser_keys():
	map = KeyMap()
	map.merge(system_keys())

	@map('<dir>')
	def move(arg):
		arg.fm.move(narg=arg.n, **arg.direction)
	map('gg', fm.move(to=0))
	map(fm.exit(), 'Q')

	map('<cr>', fm.move(dir=Direction(right=1)))

	# --------------------------------------------------------- history
	map('H', fm.history_go(-1))
	map('L', fm.history_go(1))

	# ----------------------------------------------- tagging / marking
	map('t', fm.tag_toggle())
	map('T', fm.tag_remove())

	map(' ', fm.mark(toggle=True))
	map('v', fm.mark(all=True, toggle=True))
	map('V', fm.mark(all=True, val=False))

	# ------------------------------------------ file system operations
	map('yy', fm.copy())
	map('dd', fm.cut())
	map('pp', fm.paste())
	map('po', fm.paste(overwrite=True))
	map('pl', fm.paste_symlink())
	map('p<bg>', fm.hint('press //p// once again to confirm pasting' \
			', or //l// to create symlinks'))

	# ---------------------------------------------------- run programs
	map('s', fm.execute_command(os.environ['SHELL']))
	map('E', fm.edit_file())
	map('.term', fm.execute_command('x-terminal-emulator', flags='d'))
	map('du', fm.execute_command('du --max-depth=1 -h | less'))

	# -------------------------------------------------- toggle options
	map('b<bg>', fm.hint("bind_//h//idden //p//review_files" \
		"//d//irectories_first //c//ollapse_preview flush//i//nput"))
	map('bh', fm.toggle_boolean_option('show_hidden'))
	map('bp', fm.toggle_boolean_option('preview_files'))
	map('bi', fm.toggle_boolean_option('flushinput'))
	map('bd', fm.toggle_boolean_option('directories_first'))
	map('bc', fm.toggle_boolean_option('collapse_preview'))

	# ------------------------------------------------------------ sort
	map('o<bg>', 'O<bg>', fm.hint("//s//ize //b//ase//n//ame //m//time" \
		" //t//ype //r//everse"))
	sort_dict = {
		's': 'size',
		'b': 'basename',
		'n': 'basename',
		'm': 'mtime',
		't': 'type',
	}

	for key, val in sort_dict.items():
		for key, is_capital in ((key, False), (key.upper(), True)):
			# reverse if any of the two letters is capital
			map('o' + key, fm.sort(func=val, reverse=is_capital))
			map('O' + key, fm.sort(func=val, reverse=True))

	map('or', 'Or', 'oR', 'OR', lambda arg: \
			arg.fm.sort(reverse=not arg.fm.settings.reverse))

	# ----------------------------------------------- console shortcuts
	@map("A")
	def append_to_filename(arg):
		command = 'rename ' + arg.fm.env.cf.basename
		arg.fm.open_console(cmode.COMMAND, command)

	map('cw', fm.open_console(cmode.COMMAND, 'rename '))
	map('cd', fm.open_console(cmode.COMMAND, 'cd '))
	map('f', fm.open_console(cmode.COMMAND_QUICK, 'find '))
	map('bf', fm.open_console(cmode.COMMAND, 'filter '))
	map('d<bg>', fm.hint('d//u// (disk usage) d//d// (cut)'))


	# --------------------------------------------- jump to directories
	map('gh', fm.cd('~'))
	map('ge', fm.cd('/etc'))
	map('gu', fm.cd('/usr'))
	map('gd', fm.cd('/dev'))
	map('gl', fm.cd('/lib'))
	map('go', fm.cd('/opt'))
	map('gv', fm.cd('/var'))
	map('gr', 'g/', fm.cd('/'))
	map('gm', fm.cd('/media'))
	map('gn', fm.cd('/mnt'))
	map('gt', fm.cd('/tmp'))
	map('gs', fm.cd('/srv'))
	map('gR', fm.cd(RANGERDIR))

	# ------------------------------------------------------- searching
	map('/', fm.open_console(cmode.SEARCH))

	map('n', fm.search())
	map('N', fm.search(forward=False))

	map(TAB, fm.search(order='tag'))
	map('cc', fm.search(order='ctime'))
	map('cm', fm.search(order='mimetype'))
	map('cs', fm.search(order='size'))
	map('c<bg>', fm.hint('//c//time //m//imetype //s//ize'))

	# ------------------------------------------------------- bookmarks
	for key in ALLOWED_BOOKMARK_KEYS:
		map("`" + key, "'" + key, fm.enter_bookmark(key))
		map("m" + key, fm.set_bookmark(key))
		map("um" + key, fm.unset_bookmark(key))
	map("`<bg>", "'<bg>", "m<bg>", fm.draw_bookmarks())


	map(':', ';', fm.open_console(cmode.COMMAND))

	# ---------------------------------------------------- change views
	map('i', fm.display_file())
	map(ctrl('p'), fm.display_log())
	map('?', KEY_F1, fm.display_help())
	map('w', lambda arg: arg.fm.ui.open_taskview())

	# ------------------------------------------------ system functions
	map('ZZ', fm.exit())
	map(ctrl('R'), fm.reset())
	map('R', fm.reload_cwd())
	map(ctrl('C'), fm.exit())

	map(':', ';', fm.open_console(cmode.COMMAND))
	map('>', fm.open_console(cmode.COMMAND_QUICK))
	map('!', fm.open_console(cmode.OPEN))
	map('r', fm.open_console(cmode.OPEN_QUICK))

	return map

def console_keys():
	map = KeyMap()
	map.merge(system_keys())

	@map('<any>')
	def type_key(arg):
		arg.wdg.type_key(arg.match)

	map('<up>', wdg.history_move(-1))
	map('<down>', wdg.history_move(1))
	map('<tab>', wdg.tab())

#from pprint import pprint
#pprint(browser_keys()._tree[106].__dict__)
#raise SystemExit()

ui_keys = browser_keys()
taskview_keys = ui_keys
pager_keys = ui_keys
embedded_pager_keys = ui_keys
console_keys = console_keys()
directions = vim()