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__