about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ranger/actions.py107
-rw-r--r--ranger/applications.py6
-rw-r--r--ranger/defaults/keys.py130
-rw-r--r--ranger/fm.py128
-rw-r--r--ranger/gui/ui.py14
-rw-r--r--ranger/main.py16
-rw-r--r--ranger/shared/__init__.py6
7 files changed, 213 insertions, 194 deletions
diff --git a/ranger/actions.py b/ranger/actions.py
new file mode 100644
index 00000000..54a88b11
--- /dev/null
+++ b/ranger/actions.py
@@ -0,0 +1,107 @@
+from ranger.shared import EnvironmentAware, SettingsAware
+
+class Actions(EnvironmentAware, SettingsAware):
+	def search_forward(self):
+		if self.env.pwd:
+			if self.env.pwd.search(self.env.last_search):
+				self.env.cf = self.env.pwd.pointed_file
+
+	def search_backward(self):
+		if self.env.pwd:
+			if self.env.pwd.search(self.env.last_search, -1):
+				self.env.cf = self.env.pwd.pointed_file
+
+	def interrupt(self):
+		import time
+		self.env.key_clear()
+		try:
+			time.sleep(0.2)
+		except KeyboardInterrupt:
+			raise SystemExit()
+
+	def resize(self):
+		self.ui.resize()
+
+	def exit(self):
+		raise SystemExit()
+
+	def enter_dir(self, path):
+		self.env.enter_dir(path)
+
+	def enter_bookmark(self, key):
+		from ranger.container.bookmarks import NonexistantBookmark
+		try:
+			destination = self.bookmarks[key]
+			current_path = self.env.pwd.path
+			if destination != current_path:
+				self.bookmarks.enter(key)
+				self.bookmarks.remember(current_path)
+		except NonexistantBookmark:
+			pass
+
+	def set_bookmark(self, key):
+		self.bookmarks[key] = self.env.pwd.path
+
+	def unset_bookmark(self, key):
+		self.bookmarks.delete(key)
+
+	def move_left(self):
+		self.env.enter_dir('..')
+	
+	def move_right(self, mode = 0):
+		cf = self.env.cf
+		if not self.env.enter_dir(cf):
+			self.execute_file(cf, mode = mode)
+
+	def history_go(self, relative):
+		self.env.history_go(relative)
+	
+	def handle_mouse(self):
+		self.ui.handle_mouse(self)
+
+	def execute_file(self, files, app = '', flags = '', mode = 0):
+		if type(files) not in (list, tuple):
+			files = [files]
+
+		self.apps.get(app)(
+				mainfile = files[0],
+				files = files,
+				flags = flags,
+				mode = mode,
+				fm = self,
+				stdin = None,
+				apps = self.apps)
+	
+	def edit_file(self):
+		if self.env.cf is None:
+			return
+		self.execute_file(self.env.cf, app = 'editor')
+
+	def open_console(self, mode = ':'):
+		if self.ui.can('open_console'):
+			self.ui.open_console(mode)
+
+	def move_pointer(self, relative = 0, absolute = None):
+		self.env.cf = self.env.pwd.move_pointer(relative, absolute)
+
+	def move_pointer_by_pages(self, relative):
+		self.env.cf = self.env.pwd.move_pointer(
+				relative = int(relative * self.env.termsize[0]))
+
+	def scroll(self, relative):
+		if self.ui.can('scroll'):
+			self.ui.scroll(relative)
+			self.env.cf = self.env.pwd.pointed_file
+
+	def redraw(self):
+		self.ui.redraw()
+
+	def reset(self):
+		old_path = self.env.pwd.path
+		self.env.directories = {}
+		self.enter_dir(old_path)
+
+	def toggle_boolean_option(self, string):
+		if isinstance(self.env.settings[string], bool):
+			self.env.settings[string] ^= True
+
diff --git a/ranger/applications.py b/ranger/applications.py
index ca934920..7f029fde 100644
--- a/ranger/applications.py
+++ b/ranger/applications.py
@@ -44,12 +44,14 @@ def run(*args, **kw):
 		kw['flags'] = ''.join(f for f in kw['flags'] if f in 'd')
 		process2 = kw['apps'].app_pager(**kw)
 		return process2
+
 	if 'd' in flags:
 		process = Popen(args, **popen_kw)
 		return process
+
 	else:
-		fm.ui.exit()
+		if fm.ui is not None: fm.ui.exit()
 		p = Popen(args, **popen_kw)
 		waitpid_no_intr(p.pid)
-		fm.ui.initialize()
+		if fm.ui is not None: fm.ui.initialize()
 		return p
diff --git a/ranger/defaults/keys.py b/ranger/defaults/keys.py
index de58681a..2f623bca 100644
--- a/ranger/defaults/keys.py
+++ b/ranger/defaults/keys.py
@@ -1,10 +1,14 @@
-def initialize_commands(cl):
-	from ranger.fm import FM
-	from curses.ascii import ctrl
+import curses
+from curses.ascii import ctrl, ESC
+
+def initialize_commands(command_list):
+	from ranger.actions import Actions as do
 	from ranger.container.bookmarks import ALLOWED_KEYS as ALLOWED_BOOKMARK_KEYS
-	import curses
 
-	# syntax for binding keys: cl.bind(fnc, *keys)
+	def bind(fnc, *keys):
+		command_list.bind(fnc, *keys)
+
+	# syntax for binding keys: bind(fnc, *keys)
 	# fnc is a function which is called with the FM instance,
 	# keys are one or more key-combinations which are either:
 	# * a string
@@ -21,74 +25,75 @@ def initialize_commands(cl):
 	def move_pages(n):
 		return lambda fm: fm.move_pointer_by_pages(n)
 
-	cl.bind(FM.move_left,               'h', curses.KEY_BACKSPACE, 127)
-	cl.bind(FM.move_right,              'l')
-	cl.bind(c(FM.move_right, mode=1),   curses.KEY_ENTER, ctrl('j'))
-	cl.bind(c(FM.history_go, -1),       'H')
-	cl.bind(c(FM.history_go,  1),       'L')
-	cl.bind(move( relative = 1 ),       'j')
-	cl.bind(move_pages( 0.5 ),          'J')
-	cl.bind(move( relative = -1 ),      'k')
-	cl.bind(move_pages( -0.5 ),         'K')
-	cl.bind(move( absolute = 0 ),       'gg')
-	cl.bind(move( absolute = -1 ),      'G')
-	cl.bind(FM.edit_file,               'E')
+	bind(do.move_left,               'h', curses.KEY_BACKSPACE, 127)
+	bind(do.move_right,              'l')
+	bind(c(do.move_right, mode=1),   curses.KEY_ENTER, ctrl('j'))
+	bind(c(do.history_go, -1),       'H')
+	bind(c(do.history_go,  1),       'L')
+	bind(move( relative = 1 ),       'j')
+	bind(move_pages( 0.5 ),          'J')
+	bind(move( relative = -1 ),      'k')
+	bind(move_pages( -0.5 ),         'K')
+	bind(move( absolute = 0 ),       'gg')
+	bind(move( absolute = -1 ),      'G')
+	bind(do.edit_file,               'E')
 
 	# toggle options
 	def toggle_option(string):
 		return lambda fm: fm.toggle_boolean_option(string)
 
-	cl.bind(toggle_option('show_hidden'),       'th')
-	cl.bind(toggle_option('preview_files'),     'tp')
-	cl.bind(toggle_option('directories_first'), 'td')
+	bind(toggle_option('show_hidden'),       'th')
+	bind(toggle_option('preview_files'),     'tp')
+	bind(toggle_option('directories_first'), 'td')
 
 	# key combinations which change the current directory
 	def cd(path):
 		return lambda fm: fm.enter_dir(path)
 
-	cl.bind(cd("~"),          'gh')
-	cl.bind(cd("/etc"),       'ge')
-	cl.bind(cd("/usr"),       'gu')
-	cl.bind(cd("/"),          'gr')
-	cl.bind(cd("/media"),     'gm')
-	cl.bind(cd("/mnt"),       'gn')
-	cl.bind(cd("~/.trash"),   'gt')
-	cl.bind(cd("/srv"),       'gs')
+	bind(cd("~"),          'gh')
+	bind(cd("/etc"),       'ge')
+	bind(cd("/usr"),       'gu')
+	bind(cd("/"),          'gr')
+	bind(cd("/media"),     'gm')
+	bind(cd("/mnt"),       'gn')
+	bind(cd("~/.trash"),   'gt')
+	bind(cd("/srv"),       'gs')
 
-	cl.bind(FM.search_forward,  'n')
-	cl.bind(FM.search_backward, 'N')
+	bind(do.search_forward,  'n')
+	bind(do.search_backward, 'N')
 
 	# bookmarks
 	for key in ALLOWED_BOOKMARK_KEYS:
-		cl.bind(c(FM.enter_bookmark, key),   "`" + key, "'" + key)
-		cl.bind(c(FM.set_bookmark, key),     "m" + key)
-		cl.bind(c(FM.unset_bookmark, key),   "um" + key)
+		bind(c(do.enter_bookmark, key),   "`" + key, "'" + key)
+		bind(c(do.set_bookmark, key),     "m" + key)
+		bind(c(do.unset_bookmark, key),   "um" + key)
 
 	# system functions
-	cl.bind(FM.exit,         ctrl('D'), 'q', 'ZZ')
-	cl.bind(FM.reset,        ctrl('R'))
-	cl.bind(FM.redraw,       ctrl('L'))
-	cl.bind(FM.interrupt,    ctrl('C'))
-	cl.bind(FM.resize,       curses.KEY_RESIZE)
-	cl.bind(FM.handle_mouse, curses.KEY_MOUSE)
-	cl.bind(curry(FM.open_console, ':'), ':')
-	cl.bind(curry(FM.open_console, '/'), '/')
-	cl.bind(curry(FM.open_console, '!'), '!')
-	cl.bind(curry(FM.open_console, '@'), 'r')
+	bind(do.exit,         ctrl('D'), 'q', 'ZZ')
+	bind(do.reset,        ctrl('R'))
+	bind(do.redraw,       ctrl('L'))
+	bind(do.interrupt,    ctrl('C'))
+	bind(do.resize,       curses.KEY_RESIZE)
+	bind(do.handle_mouse, curses.KEY_MOUSE)
+	bind(curry(do.open_console, ':'), ':')
+	bind(curry(do.open_console, '/'), '/')
+	bind(curry(do.open_console, '!'), '!')
+	bind(curry(do.open_console, '@'), 'r')
 
 	def test(fm):
 		from ranger.helper import log
 		log(fm.bookmarks.dct)
-	cl.bind(test, 'x')
+	bind(test, 'x')
 
-	cl.rebuild_paths()
+	command_list.rebuild_paths()
 
 
-def initialize_console_commands(cl):
-	from ranger.fm import FM
+def initialize_console_commands(command_list):
+	from ranger.actions import Actions as do
 	from ranger.gui.wconsole import WConsole
-	from curses.ascii import ctrl, ESC
-	import curses
+
+	def bind(fnc, *keys):
+		command_list.bind(fnc, *keys)
 
 	def type_key(key):
 		return lambda con, fm: con.type_key(key)
@@ -102,21 +107,22 @@ def initialize_console_commands(cl):
 	c = curry
 
 	# movement
-	cl.bind(c(WConsole.move, relative = -1), curses.KEY_LEFT, ctrl('b'))
-	cl.bind(c(WConsole.move, relative =  1), curses.KEY_RIGHT, ctrl('f'))
-	cl.bind(c(WConsole.move, absolute = 0), curses.KEY_HOME, ctrl('a'))
-	cl.bind(c(WConsole.move, absolute = -1), curses.KEY_END, ctrl('e'))
-	cl.bind(c(WConsole.delete, 0), curses.KEY_DC, ctrl('d'))
-	cl.bind(c(WConsole.delete, -1), curses.KEY_BACKSPACE, 127, ctrl('h'))
-	cl.bind(c(WConsole.delete_rest, -1), ctrl('U'))
-	cl.bind(c(WConsole.delete_rest,  1), ctrl('K'))
+	bind(c(WConsole.move, relative = -1), curses.KEY_LEFT, ctrl('b'))
+	bind(c(WConsole.move, relative =  1), curses.KEY_RIGHT, ctrl('f'))
+	bind(c(WConsole.move, absolute = 0), curses.KEY_HOME, ctrl('a'))
+	bind(c(WConsole.move, absolute = -1), curses.KEY_END, ctrl('e'))
+	bind(c(WConsole.delete, 0), curses.KEY_DC, ctrl('d'))
+	bind(c(WConsole.delete, -1), curses.KEY_BACKSPACE, 127, ctrl('h'))
+	bind(c(WConsole.delete_rest, -1), ctrl('U'))
+	bind(c(WConsole.delete_rest,  1), ctrl('K'))
 
 	# system functions
-	cl.bind(c(WConsole.close),    ESC, ctrl('C'))
-	cl.bind(WConsole.execute,  curses.KEY_ENTER, ctrl('j'))
-	cl.bind(c_fm(FM.redraw), ctrl('L'))
-	cl.bind(c_fm(FM.resize), curses.KEY_RESIZE)
+	bind(c(WConsole.close),    ESC, ctrl('C'))
+	bind(WConsole.execute,  curses.KEY_ENTER, ctrl('j'))
+	bind(c_fm(do.redraw), ctrl('L'))
+	bind(c_fm(do.resize), curses.KEY_RESIZE)
 
 	for i in range(ord(' '), ord('~')):
-		cl.bind(type_key(i), i)
+		bind(type_key(i), i)
 
+	command_list.rebuild_paths()
diff --git a/ranger/fm.py b/ranger/fm.py
index 2bfecffb..2b4b32e4 100644
--- a/ranger/fm.py
+++ b/ranger/fm.py
@@ -1,16 +1,29 @@
-from ranger.shared import EnvironmentAware
+from ranger.actions import Actions
+from ranger.container import Bookmarks
 
-class FM(EnvironmentAware):
-	def __init__(self, ui, bookmarks):
+class FM(Actions):
+	def __init__(self, ui = None, bookmarks = None):
+		Actions.__init__(self)
 		self.ui = ui
-		self.apps = self.env.settings.apps.CustomApplications()
-		self.bookmarks = bookmarks
+		self.apps = self.settings.apps.CustomApplications()
+
+		if bookmarks is None:
+			self.bookmarks = Bookmarks()
+			self.bookmarks.load()
+
+		else:
+			self.bookmarks = bookmarks
 		self.bookmarks.enter_dir_function = self.enter_dir
 
 		from ranger.shared import FileManagerAware
 		FileManagerAware.fm = self
 
 	def loop(self):
+		if self.ui is None:
+			from ranger.gui.defaultui import DefaultUI
+			self.ui = DefaultUI()
+			self.ui.initialize()
+
 		self.env.enter_dir(self.env.path)
 
 		gc_tick = 0
@@ -29,108 +42,3 @@ class FM(EnvironmentAware):
 
 			except KeyboardInterrupt:
 				self.ui.press(3, self)
-	
-	def interrupt(self):
-		import time
-		self.env.key_clear()
-		try:
-			time.sleep(0.2)
-		except KeyboardInterrupt:
-			raise SystemExit()
-	
-	def search_forward(self):
-		if self.env.pwd:
-			if self.env.pwd.search(self.env.last_search):
-				self.env.cf = self.env.pwd.pointed_file
-
-	def search_backward(self):
-		if self.env.pwd:
-			if self.env.pwd.search(self.env.last_search, -1):
-				self.env.cf = self.env.pwd.pointed_file
-
-	def resize(self):
-		self.ui.resize()
-
-	def exit(self):
-		raise SystemExit()
-
-	def enter_dir(self, path):
-		self.env.enter_dir(path)
-
-	def enter_bookmark(self, key):
-		from ranger.container.bookmarks import NonexistantBookmark
-		try:
-			destination = self.bookmarks[key]
-			current_path = self.env.pwd.path
-			if destination != current_path:
-				self.bookmarks.enter(key)
-				self.bookmarks.remember(current_path)
-		except NonexistantBookmark:
-			pass
-
-	def set_bookmark(self, key):
-		self.bookmarks[key] = self.env.pwd.path
-
-	def unset_bookmark(self, key):
-		self.bookmarks.delete(key)
-
-	def move_left(self):
-		self.env.enter_dir('..')
-	
-	def move_right(self, mode = 0):
-		cf = self.env.cf
-		if not self.env.enter_dir(cf):
-			self.execute_file(cf, mode = mode)
-
-	def history_go(self, relative):
-		self.env.history_go(relative)
-	
-	def handle_mouse(self):
-		self.ui.handle_mouse(self)
-
-	def execute_file(self, files, app = '', flags = '', mode = 0):
-		if type(files) not in (list, tuple):
-			files = [files]
-
-		self.apps.get(app)(
-				mainfile = files[0],
-				files = files,
-				flags = flags,
-				mode = mode,
-				fm = self,
-				stdin = None,
-				apps = self.apps)
-	
-	def edit_file(self):
-		if self.env.cf is None:
-			return
-		self.execute_file(self.env.cf, app = 'editor')
-
-	def open_console(self, mode = ':'):
-		if self.ui.can('open_console'):
-			self.ui.open_console(mode)
-
-	def move_pointer(self, relative = 0, absolute = None):
-		self.env.cf = self.env.pwd.move_pointer(relative, absolute)
-
-	def move_pointer_by_pages(self, relative):
-		self.env.cf = self.env.pwd.move_pointer(
-				relative = int(relative * self.env.termsize[0]))
-
-	def scroll(self, relative):
-		if self.ui.can('scroll'):
-			self.ui.scroll(relative)
-			self.env.cf = self.env.pwd.pointed_file
-
-	def redraw(self):
-		self.ui.redraw()
-
-	def reset(self):
-		old_path = self.env.pwd.path
-		self.env.directories = {}
-		self.enter_dir(old_path)
-
-	def toggle_boolean_option(self, string):
-		if isinstance(self.env.settings[string], bool):
-			self.env.settings[string] ^= True
-
diff --git a/ranger/gui/ui.py b/ranger/gui/ui.py
index beedea09..c7c10cfe 100644
--- a/ranger/gui/ui.py
+++ b/ranger/gui/ui.py
@@ -17,14 +17,19 @@ class MouseEvent(object):
 		except:
 			return False
 
-from ranger.shared import EnvironmentAware
+from ranger.shared import EnvironmentAware, SettingsAware
+from ranger.container import CommandList
 
-class UI(EnvironmentAware):
-	def __init__(self, commandlist):
+class UI(EnvironmentAware, SettingsAware):
+	def __init__(self, commandlist = None):
 		import os
 		os.environ['ESCDELAY'] = '25' # don't know a cleaner way
 
-		self.commandlist = commandlist
+		if commandlist is None:
+			self.commandlist = CommandList()
+			self.settings.keys.initialize_commands(self.commandlist)
+		else:
+			self.commandlist = commandlist
 		self.colorscheme = self.env.settings.colorscheme
 		self.is_set_up = False
 		self.win = curses.initscr()
@@ -32,7 +37,6 @@ class UI(EnvironmentAware):
 		self.widgets = []
 
 	def initialize(self):
-
 		self.win.leaveok(0)
 		self.win.keypad(1)
 
diff --git a/ranger/main.py b/ranger/main.py
index f5453187..1cac2873 100644
--- a/ranger/main.py
+++ b/ranger/main.py
@@ -4,9 +4,7 @@ from locale import setlocale, LC_ALL
 from optparse import OptionParser, SUPPRESS_HELP
 
 from ranger.fm import FM
-from ranger.container import CommandList, Bookmarks
 from ranger.environment import Environment
-from ranger.shared import SettingsAware, EnvironmentAware, FileManagerAware
 from ranger.gui.defaultui import DefaultUI as UI
 from ranger.file import File
 
@@ -53,7 +51,7 @@ def main():
 			sys.exit(1)
 		elif os.path.isfile(target):
 			thefile = File(target)
-			FM(0, 0, sys).execute_file(thefile)
+			FM().execute_file(thefile)
 			sys.exit(0)
 		else:
 			path = target
@@ -62,16 +60,10 @@ def main():
 		path = '.'
 
 	Environment(path)
-	commandlist = CommandList()
-	SettingsAware.settings.keys.initialize_commands(commandlist)
-	bookmarks = Bookmarks()
-	bookmarks.load()
-
-	my_ui = None
 
 	try:
-		my_ui = UI(commandlist)
-		my_fm = FM(my_ui, bookmarks)
+		my_ui = UI()
+		my_fm = FM(ui = my_ui)
 
 		# Run the file manager
 		my_ui.initialize()
@@ -79,7 +71,7 @@ def main():
 
 	finally:
 		# Finish, clean up
-		if my_ui:
+		if 'my_ui' in vars():
 			my_ui.exit()
 
 		if args.cd_after_exit:
diff --git a/ranger/shared/__init__.py b/ranger/shared/__init__.py
index eed206d6..4bb09e30 100644
--- a/ranger/shared/__init__.py
+++ b/ranger/shared/__init__.py
@@ -1,11 +1,11 @@
 class Awareness(object):
 	pass
 
-from .mimetype import MimeTypeAware
-from .settings import SettingsAware
-
 class EnvironmentAware(Awareness):
    env = None
 
 class FileManagerAware(Awareness):
    fm = None
+
+from .mimetype import MimeTypeAware
+from .settings import SettingsAware