summary refs log tree commit diff stats
path: root/ranger
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2009-12-04 18:46:42 +0100
committerhut <hut@lavabit.com>2009-12-04 18:46:42 +0100
commit6874e0ea3059b0f394ca9cf5929cb82c3fe4a09d (patch)
treed7165e3f357fb0d208310f965db8df87a5403e6e /ranger
parentcc952d63d81e410d1c7dd3bfbe91ee6cfdd3d7a8 (diff)
downloadranger-6874e0ea3059b0f394ca9cf5929cb82c3fe4a09d.tar.gz
implemented Bookmarks
Diffstat (limited to 'ranger')
-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
ODO?h=v1.9.0b1&id=039c03efad1df9ef7d9c1965454e98c46c53ef87'>039c03ef ^
af6658b3 ^
efe2d7a3 ^
f0df3fa5 ^
a986e2bd ^

3fe38754 ^

6a8d5d23 ^
aad61455 ^
7a268c8b ^

efdc7b16 ^
636f69d3 ^
d3c262a9 ^
cf8b174e ^
e02d47ed ^
48a8eab5 ^
2e631f44 ^
f3bc52e5 ^
3a917b8a ^
ff86460b ^
2f3326a4 ^
e952d6cb ^
4be8b401 ^



d955e3f0 ^
75013dc7 ^
67bb838c ^
a808a661 ^
bba8d293 ^
2a64495f ^
5e449699 ^
8895b130 ^

7b04e507 ^
87db0130 ^
dd4a4145 ^
8f2f1767 ^
db1721dd ^
b624bd94 ^
83868c7a ^
6099d9a3 ^
9207e83c ^
f7db061b ^
6099d9a3 ^
f3bc52e5 ^
f01ef1a0 ^
13370392 ^
ff86460b ^
e1d0613a ^
6f43de0a ^




fca1fc4f ^
f70ee6b2 ^
0db4c9b2 ^
338bbba4 ^

6908d0cc ^
338bbba4 ^


338bbba4 ^
c776804d ^
dab4db44 ^
cc4210ff ^
34c131ef ^
32f93303 ^
c1a9373c ^
ff86460b ^
66c5bb93 ^
338bbba4 ^





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