summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--ranger/bookmark.py65
-rw-r--r--ranger/conf/keys.py11
-rw-r--r--ranger/fm.py22
-rw-r--r--ranger/main.py5
4 files changed, 99 insertions, 4 deletions
diff --git a/ranger/bookmark.py b/ranger/bookmark.py
new file mode 100644
index 00000000..e9bfcee1
--- /dev/null
+++ b/ranger/bookmark.py
@@ -0,0 +1,65 @@
+from string import ascii_letters, digits
+ALLOWED_KEYS = ascii_letters + digits + "`'"
+
+class NonexistantBookmark(Exception):
+	pass
+
+class Bookmarks(object):
+	def __init__(self, path = None):
+		import string, re, os
+		self.dct = {}
+		if path is None:
+			self.path = os.path.expanduser("~/.ranger/bookmarks")
+		self.load_pattern = re.compile(r"^[\d\w`']:.")
+		self.enter_dir_function = None
+
+	def load(self):
+		import os
+		self.dct.clear()
+
+		if os.access(self.path, os.R_OK):
+			f = open(self.path, 'r')
+			for line in f:
+				if self.load_pattern.match(line):
+					key, value = line[0], line[2:-1]
+					if key in ALLOWED_KEYS: 
+						self.dct[key] = value
+
+			f.close()
+
+	def enter(self, key):
+		if self.enter_dir_function is not None:
+			self.enter_dir_function(self[key])
+		else:
+			raise RuntimeError('Not specified how to enter a directory')
+
+	def remember(self, value):
+		self["`"] = value
+		self["'"] = value
+
+	def __getitem__(self, key):
+		if key in self.dct:
+			return self.dct[key]
+		else:
+			raise NonexistantBookmark()
+
+	def __setitem__(self, key, value):
+		if key in ALLOWED_KEYS:
+			self.dct[key] = value
+			self.save()
+
+	def __contains__(self, key):
+		return key in self.dct
+
+	def save(self):
+		import os
+		if os.access(self.path, os.W_OK):
+			f = open(self.path, 'w')
+
+			for key, value in self.dct.items():
+				if type(key) == str\
+						and type(value) == str \
+						and key in ALLOWED_KEYS:
+					f.write("{0}:{1}\n".format(str(key), str(value)))
+
+			f.close()
diff --git a/ranger/conf/keys.py b/ranger/conf/keys.py
index b9816919..9a9bcff3 100644
--- a/ranger/conf/keys.py
+++ b/ranger/conf/keys.py
@@ -1,6 +1,7 @@
 def initialize_commands(cl):
 	from ranger.fm import FM
 	from curses.ascii import ctrl
+	from ranger.bookmark import ALLOWED_KEYS as ALLOWED_BOOKMARK_KEYS
 	import curses
 
 	# syntax for binding keys: cl.bind(fnc, *keys)
@@ -54,6 +55,11 @@ def initialize_commands(cl):
 	cl.bind(cd("~/.trash"),   'gt')
 	cl.bind(cd("/srv"),       'gs')
 
+	# 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)
+
 	# system functions
 	cl.bind(FM.exit,         ctrl('D'), 'q', 'ZZ')
 	cl.bind(FM.reset,        ctrl('R'))
@@ -66,6 +72,11 @@ def initialize_commands(cl):
 	cl.bind(curry(FM.open_console, '!'), '!')
 	cl.bind(curry(FM.open_console, '@'), 'r')
 
+	def test(fm):
+		from ranger.helper import log
+		log(fm.bookmarks.dct)
+	cl.bind(test, 'x')
+
 	cl.rebuild_paths()
 
 
diff --git a/ranger/fm.py b/ranger/fm.py
index 95110455..3f36045d 100644
--- a/ranger/fm.py
+++ b/ranger/fm.py
@@ -3,15 +3,17 @@ from ranger.conf.apps import CustomApplications as Applications
 null = open(devnull, 'a')
 
 class FM():
-	def __init__(self, environment, ui):
+	def __init__(self, environment, ui, bookmarks):
 		self.env = environment
 		self.ui = ui
 		self.apps = Applications()
+		self.bookmarks = bookmarks
+		self.bookmarks.enter_dir_function = self.enter_dir
 
 	def run(self):
 		self.env.enter_dir(self.env.path)
 
-		while 1:
+		while True:
 			try:
 				self.ui.draw()
 				key = self.ui.get_next_key()
@@ -38,9 +40,23 @@ class FM():
 	def enter_dir(self, path):
 		self.env.enter_dir(path)
 
+	def enter_bookmark(self, key):
+		from ranger.bookmark 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 move_left(self):
 		self.env.enter_dir('..')
-
+	
 	def move_right(self, mode = 0):
 		cf = self.env.cf
 		if not self.env.enter_dir(cf):
diff --git a/ranger/main.py b/ranger/main.py
index 14953682..e6718122 100644
--- a/ranger/main.py
+++ b/ranger/main.py
@@ -6,6 +6,7 @@ from optparse import OptionParser, SUPPRESS_HELP
 from ranger.fm import FM
 from ranger.environment import Environment
 from ranger.command import CommandList
+from ranger.bookmark import Bookmarks
 from ranger.conf import keys, options
 from ranger.gui.defaultui import DefaultUI as UI
 from ranger.conf.colorschemes.snow import MyColorScheme
@@ -66,9 +67,11 @@ def main():
 	commandlist = CommandList()
 	colorscheme = MyColorScheme()
 	keys.initialize_commands(commandlist)
+	bookmarks = Bookmarks()
+	bookmarks.load()
 
 	my_ui = UI(env, commandlist, colorscheme)
-	my_fm = FM(env, my_ui)
+	my_fm = FM(env, my_ui, bookmarks)
 
 	try:
 		# Run the file manager