summary refs log tree commit diff stats
diff options
context:
space:
mode:
authornfnty <git@nfnty.se>2017-02-01 02:02:41 +0100
committernfnty <git@nfnty.se>2017-02-01 02:23:19 +0100
commitdf4c2ca56ff0f1aeb7b696140c8416163170fb1e (patch)
tree24922aa29f33f3fa5b97b913ac06c7b04a661349
parent9ce8839a6363d43f2d6f9ce74feb2c21ee4fed41 (diff)
downloadranger-df4c2ca56ff0f1aeb7b696140c8416163170fb1e.tar.gz
commands: rename: Fix bookmarks and tags updating
Fixes #447
Fixes #470
-rwxr-xr-xranger/config/commands.py29
-rw-r--r--ranger/container/bookmarks.py14
-rw-r--r--ranger/container/tags.py18
3 files changed, 37 insertions, 24 deletions
diff --git a/ranger/config/commands.py b/ranger/config/commands.py
index a250f8a9..2db6afa5 100755
--- a/ranger/config/commands.py
+++ b/ranger/config/commands.py
@@ -814,38 +814,21 @@ class rename(Command):
 
         new_name = self.rest(1)
 
-        tagged = {}
-        old_name = self.fm.thisfile.relative_path
-        for fobj in self.fm.tags.tags:
-            if str(fobj).startswith(self.fm.thisfile.path):
-                tagged[fobj] = self.fm.tags.tags[fobj]
-                self.fm.tags.remove(fobj)
-
         if not new_name:
             return self.fm.notify('Syntax: rename <newname>', bad=True)
 
-        if new_name == old_name:
+        if new_name == self.fm.thisfile.relative_path:
             return
 
         if access(new_name, os.F_OK):
             return self.fm.notify("Can't rename: file already exists!", bad=True)
 
         if self.fm.rename(self.fm.thisfile, new_name):
-            fobj = File(new_name)
-            # Update bookmarks that were pointing on the previous name
-            obsoletebookmarks = [b for b in self.fm.bookmarks
-                                 if b[1].path == self.fm.thisfile]
-            if obsoletebookmarks:
-                for key, _ in obsoletebookmarks:
-                    self.fm.bookmarks[key] = fobj
-                self.fm.bookmarks.update_if_outdated()
-
-            self.fm.thisdir.pointed_obj = fobj
-            self.fm.thisfile = fobj
-
-            for fobj in tagged:
-                self.fm.tags.tags[fobj.replace(old_name, new_name)] = tagged[fobj]
-                self.fm.tags.dump()
+            file_new = File(new_name)
+            self.fm.bookmarks.update_path(self.fm.thisfile.path, file_new)
+            self.fm.tags.update_path(self.fm.thisfile.path, file_new.path)
+            self.fm.thisdir.pointed_obj = file_new
+            self.fm.thisfile = file_new
 
     def tab(self, tabnum):
         return self._tab_directory_content()
diff --git a/ranger/container/bookmarks.py b/ranger/container/bookmarks.py
index 23606028..cba07367 100644
--- a/ranger/container/bookmarks.py
+++ b/ranger/container/bookmarks.py
@@ -107,6 +107,20 @@ class Bookmarks(FileManagerAware):
         """Test whether a bookmark-key is defined"""
         return key in self.dct
 
+    def update_path(self, path_old, file_new):
+        """Update bookmarks containing path"""
+        self.update_if_outdated()
+        changed = False
+        for key, bfile in self:
+            if bfile.path == path_old:
+                self.dct[key] = file_new
+                changed = True
+            elif bfile.path.startswith(path_old + os.path.sep):
+                self.dct[key] = self.bookmarktype(file_new.path + bfile.path[len(path_old):])
+                changed = True
+        if changed:
+            self.save()
+
     def update(self):
         """Update the bookmarks from the bookmark file.
 
diff --git a/ranger/container/tags.py b/ranger/container/tags.py
index 0cab299f..7aed5131 100644
--- a/ranger/container/tags.py
+++ b/ranger/container/tags.py
@@ -5,7 +5,7 @@
 
 from __future__ import (absolute_import, division, print_function)
 
-from os.path import isdir, exists, dirname, abspath, realpath, expanduser
+from os.path import isdir, exists, dirname, abspath, realpath, expanduser, sep
 import string
 import sys
 
@@ -112,6 +112,22 @@ class Tags(object):
 
         return result
 
+    def update_path(self, path_old, path_new):
+        self.sync()
+        changed = False
+        for path, tag in self.tags.items():
+            pnew = None
+            if path == path_old:
+                pnew = path_new
+            elif path.startswith(path_old + sep):
+                pnew = path_new + path[len(path_old):]
+            if pnew:
+                del self.tags[path]
+                self.tags[pnew] = tag
+                changed = True
+        if changed:
+            self.dump()
+
     def __nonzero__(self):
         return True
     __bool__ = __nonzero__
d=cc813408772738fc124130095ba4a5c33c84dea3'>^
f07bb12f ^






4c13e1f2 ^

f07bb12f ^


4c13e1f2 ^

f07bb12f ^



4c13e1f2 ^


f07bb12f ^











f07bb12f ^















4c13e1f2 ^




34a60763 ^
4c13e1f2 ^
34a60763 ^
4c13e1f2 ^



f07bb12f ^
62cd83ba ^
f07bb12f ^


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