summary refs log tree commit diff stats
diff options
context:
space:
mode:
authordbosst <dbosst@gmail.com>2018-03-28 12:30:47 -0400
committerdbosst <dbosst@gmail.com>2018-03-28 12:30:47 -0400
commit5ed312402389b9882512f09a7a740e850ce446c3 (patch)
tree93aa62be52269df1d0e1c4f629af1eaacf4e5b48
parent88b5a20142f3c7666a78ec86db667635681adc9f (diff)
downloadranger-5ed312402389b9882512f09a7a740e850ce446c3.tar.gz
use offset and pos to shift tabs:
offset will shift the tab number as seen
pos will shift the tab to the tav number given
preserves as much as possible the tab numbers
-rw-r--r--ranger/config/rc.conf4
-rw-r--r--ranger/core/actions.py57
2 files changed, 43 insertions, 18 deletions
diff --git a/ranger/config/rc.conf b/ranger/config/rc.conf
index 8ac83cd3..53171112 100644
--- a/ranger/config/rc.conf
+++ b/ranger/config/rc.conf
@@ -478,8 +478,8 @@ map <a-6>     tab_open 6
 map <a-7>     tab_open 7
 map <a-8>     tab_open 8
 map <a-9>     tab_open 9
-map <a-p>     tab_shift 1
-map <a-o>     tab_shift -1
+map <a-r>     tab_shift 1
+map <a-l>     tab_shift -1
 
 # Sorting
 map or set sort_reverse!
diff --git a/ranger/core/actions.py b/ranger/core/actions.py
index 68551775..55226434 100644
--- a/ranger/core/actions.py
+++ b/ranger/core/actions.py
@@ -1191,31 +1191,56 @@ class Actions(  # pylint: disable=too-many-instance-attributes,too-many-public-m
                 return self.tab_open(i, path)
         return None
 
-    def tab_shift(self, offset):
+    def tab_shift(self, offset=0, pos=None):
         """Shift the tab left/right
 
-        Shift the current tab to the left or right
+        Shift the current tab to the left or right by either:
+        offset - changes the tab number by offset
+        pos - shifts the tab to the specified tab number
         """
-        assert isinstance(offset, int)
-        tablist = self.get_tab_list()
+
         oldtab_index = self.current_tab
-        old_index = tablist.index(oldtab_index)
-        new_index = (old_index + offset)
-        if new_index < 0:
-            return None
-        if new_index > (len(tablist)-1):
-            return None
-        newtab_index = tablist[new_index]
+        if pos is None:
+            assert isinstance(offset, int)
+            # enumerated index (1 to 9)
+            newtab_index = oldtab_index + offset
+            if newtab_index < 1:
+                newtab_index = 1
+            if newtab_index > 9:
+                newtab_index = 9
+        else:
+            assert isinstance(pos, int)
+            if pos < 1 or pos > 9:
+                return None
+            newtab_index = pos
+        # shift tabs without enumerating, preserve tab numbers when can
         if newtab_index != oldtab_index:
+            # the other tabs shift in the opposite direction
+            if (newtab_index - oldtab_index) > 0:
+                direction = -1
+            else:
+                direction = 1
+
+            def tabshiftreorder(source_index):
+                # shift the tabs to make source_index empty
+                if source_index in self.tabs:
+                    target_index = source_index + direction
+                    # make the target_index empty recursively
+                    tabshiftreorder(target_index)
+                    # shift the source to target
+                    source_tab = self.tabs[source_index]
+                    self.tabs[target_index] = source_tab
+                    del self.tabs[source_index]
+
+            # first remove the current tab from the dict
             oldtab = self.tabs[oldtab_index]
-            newtab = self.tabs[newtab_index]
-            self.tabs[oldtab_index] = newtab
+            del self.tabs[oldtab_index]
+            # make newtab_index empty by shifting
+            tabshiftreorder(newtab_index)
             self.tabs[newtab_index] = oldtab
             self.current_tab = newtab_index
             self.thistab = oldtab
-            self.change_mode('normal')
-            self.signal_emit('tab.change', old=oldtab, new=newtab)
-            self.signal_emit('tab.change', old=newtab, new=oldtab)
+            self.ui.titlebar.request_redraw()
             self.signal_emit('tab.layoutchange')
         return None