diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 6263b85..707d60e 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -6,3 +6,4 @@ Other contributors: * David Baumgold (singingwolfboy), * Ryan Morrissey (23maverick23), +* Carlos L. Perez (clperez), diff --git a/Context.sublime-menu b/Context.sublime-menu index 0b34627..d037596 100644 --- a/Context.sublime-menu +++ b/Context.sublime-menu @@ -79,6 +79,26 @@ { "command": "pull_file_from_pane", "args": {"direction": "right"}, "caption": "Right" }, { "command": "pull_file_from_pane", "args": {"direction": "left"}, "caption": "Left" } ] + }, + { + "caption": "Focus on", + "children": + [ + { "command": "focus_file_on_pane", "args": {"direction": "up"}, "caption": "Above" }, + { "command": "focus_file_on_pane", "args": {"direction": "down"}, "caption": "Below" }, + { "command": "focus_file_on_pane", "args": {"direction": "right"}, "caption": "Right" }, + { "command": "focus_file_on_pane", "args": {"direction": "left"}, "caption": "Left" } + ] + }, + { + "caption": "Focus or Clone to", + "children": + [ + { "command": "focus_or_clone_file_on_pane", "args": {"direction": "up"}, "caption": "Above" }, + { "command": "focus_or_clone_file_on_pane", "args": {"direction": "down"}, "caption": "Below" }, + { "command": "focus_or_clone_file_on_pane", "args": {"direction": "right"}, "caption": "Right" }, + { "command": "focus_or_clone_file_on_pane", "args": {"direction": "left"}, "caption": "Left" } + ] } ] }, diff --git a/Default (Linux).sublime-keymap b/Default (Linux).sublime-keymap index 355f2b0..9a0ccc8 100644 --- a/Default (Linux).sublime-keymap +++ b/Default (Linux).sublime-keymap @@ -16,6 +16,20 @@ // You can also create the pane automatically with the following command (insert empty parameters): // { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} } + { "keys": ["ctrl+k", "ctrl+alt+shift+up"], "command": "focus_file_on_pane", "args": {"direction": "up"} }, + { "keys": ["ctrl+k", "ctrl+alt+shift+right"], "command": "focus_file_on_pane", "args": {"direction": "right"} }, + { "keys": ["ctrl+k", "ctrl+alt+shift+down"], "command": "focus_file_on_pane", "args": {"direction": "down"} }, + { "keys": ["ctrl+k", "ctrl+alt+shift+left"], "command": "focus_file_on_pane", "args": {"direction": "left"} }, + // You can also create the pane automatically with the following command (insert empty parameters): + // { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} } + + { "keys": ["ctrl+k", "ctrl+i" , "ctrl+alt+shift+up"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "up"} }, + { "keys": ["ctrl+k", "ctrl+i" , "ctrl+alt+shift+right"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "right"} }, + { "keys": ["ctrl+k", "ctrl+i" , "ctrl+alt+shift+down"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "down"} }, + { "keys": ["ctrl+k", "ctrl+i" , "ctrl+alt+shift+left"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "left"} }, + // You can also create the pane automatically with the following command (insert empty parameters): + // { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} } + { "keys": ["ctrl+k", "ctrl+up"], "command": "create_pane", "args": {"direction": "up"} }, { "keys": ["ctrl+k", "ctrl+right"], "command": "create_pane", "args": {"direction": "right"} }, { "keys": ["ctrl+k", "ctrl+down"], "command": "create_pane", "args": {"direction": "down"} }, diff --git a/Default (OSX).sublime-keymap b/Default (OSX).sublime-keymap index 3814a81..1650b3b 100644 --- a/Default (OSX).sublime-keymap +++ b/Default (OSX).sublime-keymap @@ -16,6 +16,20 @@ // You can also create the pane automatically with the following command (insert empty parameters): // { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} } + { "keys": ["super+k", "super+alt+shift+up"], "command": "focus_file_on_pane", "args": {"direction": "up"} }, + { "keys": ["super+k", "super+alt+shift+right"], "command": "focus_file_on_pane", "args": {"direction": "right"} }, + { "keys": ["super+k", "super+alt+shift+down"], "command": "focus_file_on_pane", "args": {"direction": "down"} }, + { "keys": ["super+k", "super+alt+shift+left"], "command": "focus_file_on_pane", "args": {"direction": "left"} }, + // You can also create the pane automatically with the following command (insert empty parameters): + // { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} } + + { "keys": ["super+k", "super+i" , "super+alt+shift+up"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "up"} }, + { "keys": ["super+k", "super+i" , "super+alt+shift+right"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "right"} }, + { "keys": ["super+k", "super+i" , "super+alt+shift+down"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "down"} }, + { "keys": ["super+k", "super+i" , "super+alt+shift+left"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "left"} }, + // You can also create the pane automatically with the following command (insert empty parameters): + // { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} } + { "keys": ["super+k", "super+up"], "command": "create_pane", "args": {"direction": "up"} }, { "keys": ["super+k", "super+right"], "command": "create_pane", "args": {"direction": "right"} }, { "keys": ["super+k", "super+down"], "command": "create_pane", "args": {"direction": "down"} }, diff --git a/Default (Windows).sublime-keymap b/Default (Windows).sublime-keymap index 355f2b0..9a0ccc8 100644 --- a/Default (Windows).sublime-keymap +++ b/Default (Windows).sublime-keymap @@ -16,6 +16,20 @@ // You can also create the pane automatically with the following command (insert empty parameters): // { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} } + { "keys": ["ctrl+k", "ctrl+alt+shift+up"], "command": "focus_file_on_pane", "args": {"direction": "up"} }, + { "keys": ["ctrl+k", "ctrl+alt+shift+right"], "command": "focus_file_on_pane", "args": {"direction": "right"} }, + { "keys": ["ctrl+k", "ctrl+alt+shift+down"], "command": "focus_file_on_pane", "args": {"direction": "down"} }, + { "keys": ["ctrl+k", "ctrl+alt+shift+left"], "command": "focus_file_on_pane", "args": {"direction": "left"} }, + // You can also create the pane automatically with the following command (insert empty parameters): + // { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} } + + { "keys": ["ctrl+k", "ctrl+i" , "ctrl+alt+shift+up"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "up"} }, + { "keys": ["ctrl+k", "ctrl+i" , "ctrl+alt+shift+right"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "right"} }, + { "keys": ["ctrl+k", "ctrl+i" , "ctrl+alt+shift+down"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "down"} }, + { "keys": ["ctrl+k", "ctrl+i" , "ctrl+alt+shift+left"], "command": "focus_or_clone_file_on_pane", "args": {"direction": "left"} }, + // You can also create the pane automatically with the following command (insert empty parameters): + // { "keys": [], "command": "create_pane_with_cloned_file", "args": {"direction": ""} } + { "keys": ["ctrl+k", "ctrl+up"], "command": "create_pane", "args": {"direction": "up"} }, { "keys": ["ctrl+k", "ctrl+right"], "command": "create_pane", "args": {"direction": "right"} }, { "keys": ["ctrl+k", "ctrl+down"], "command": "create_pane", "args": {"direction": "down"} }, diff --git a/Main.sublime-menu b/Main.sublime-menu index b4cf73c..225381b 100644 --- a/Main.sublime-menu +++ b/Main.sublime-menu @@ -94,7 +94,27 @@ { "command": "pull_file_from_pane", "args": {"direction": "right"}, "caption": "Right" }, { "command": "pull_file_from_pane", "args": {"direction": "left"}, "caption": "Left" } ] - } + }, + { + "caption": "Focus on", + "children": + [ + { "command": "focus_file_on_pane", "args": {"direction": "up"}, "caption": "Above" }, + { "command": "focus_file_on_pane", "args": {"direction": "down"}, "caption": "Below" }, + { "command": "focus_file_on_pane", "args": {"direction": "right"}, "caption": "Right" }, + { "command": "focus_file_on_pane", "args": {"direction": "left"}, "caption": "Left" } + ] + }, + { + "caption": "Focus/clone to", + "children": + [ + { "command": "focus_or_clone_file_on_pane", "args": {"direction": "up"}, "caption": "Above" }, + { "command": "focus_or_clone_file_on_pane", "args": {"direction": "down"}, "caption": "Below" }, + { "command": "focus_or_clone_file_on_pane", "args": {"direction": "right"}, "caption": "Right" }, + { "command": "focus_or_clone_file_on_pane", "args": {"direction": "left"}, "caption": "Left" } + ] + }, ] }, { diff --git a/Origami.sublime-commands b/Origami.sublime-commands index 9f7b564..041d9f4 100644 --- a/Origami.sublime-commands +++ b/Origami.sublime-commands @@ -11,9 +11,20 @@ { "command": "clone_file_to_pane", "args": {"direction": "up"}, "caption": "Origami: Clone File to Pane Above" }, { "command": "clone_file_to_pane", "args": {"direction": "right"}, "caption": "Origami: Clone File to Pane on the Right" }, - { "command": "clone_file_to_pane", "args": {"direction": "down"}, "caption": "Origami: Clone File to Pane Below" }, + { "command": "clone_file_to_pane", "args": {"direction": "down"}, "cakption": "Origami: Clone File to Pane Below" }, { "command": "clone_file_to_pane", "args": {"direction": "left"}, "caption": "Origami: Clone File to Pane on the Left" }, + { "command": "focus_file_on_pane", "args": {"direction": "up"}, "caption": "Origami: Focus File on the Pane Above" }, + { "command": "focus_file_on_pane", "args": {"direction": "right"}, "caption": "Origami: Focus File on the Pane on the Right" }, + { "command": "focus_file_on_pane", "args": {"direction": "down"}, "caption": "Origami: Focus File on the Pane Below" }, + { "command": "focus_file_on_pane", "args": {"direction": "left"}, "caption": "Origami: Focus File on the Pane on the Left" }, + + { "command": "focus_or_clone_file_on_pane", "args": {"direction": "up"}, "caption": "Origami: Focus or Clone File on the Pane Above" }, + { "command": "focus_or_clone_file_on_pane", "args": {"direction": "right"}, "caption": "Origami: Focus or Clone File on the Pane on the Right" }, + { "command": "focus_or_clone_file_on_pane", "args": {"direction": "down"}, "caption": "Origami: Focus or Clone File on the Pane Below" }, + { "command": "focus_or_clone_file_on_pane", "args": {"direction": "left"}, "caption": "Origami: Focus or Clone File on the Pane on the Left" }, + { "command": "focus_or_clone_file_on_closest_pane", "caption": "Origami: Focus or Clone File on the closest panel" }, + { "command": "create_pane", "args": {"direction": "up"}, "caption": "Origami: Create Pane Above" }, { "command": "create_pane", "args": {"direction": "right"}, "caption": "Origami: Create Pane on the Right" }, { "command": "create_pane", "args": {"direction": "down"}, "caption": "Origami: Create Pane Below" }, diff --git a/origami.py b/origami.py index cc416e6..6c2bea0 100644 --- a/origami.py +++ b/origami.py @@ -106,6 +106,23 @@ def adjacent_cell(self, direction): return adjacent_cells[cell_index] return None + def get_closest_panel_direction(self): + group = self.window.active_group() + if group == None: + # If we're in an empty group, there's no active group + return + + cell = self.adjacent_cell('left') + if cell is None: + cell = self.adjacent_cell('right') + if cell is None: + return None + else: + return 'right' + else: + return 'left' + + def duplicated_views(self, original_group, duplicating_group): original_views = self.window.views_in_group(original_group) original_buffers = [v.buffer_id() for v in original_views] @@ -116,6 +133,29 @@ def duplicated_views(self, original_group, duplicating_group): dupe_views.append(pd) return dupe_views + def is_view_in_group(self, view, group): + views = self.window.views_in_group(group) + buffers = [v.buffer_id() for v in views] + + buffer_id = view.buffer_id() + buffer_exists = False + target_view = None + for current_view in views: + if current_view.buffer_id() == buffer_id: + buffer_exists = True + target_view = current_view + break + + return buffer_exists, target_view + + def clone_viewport (self, view, new_view): + new_sel = new_view.sel() + new_sel.clear() + for s in view.sel(): + new_sel.add(s) + + sublime.set_timeout(lambda : new_view.set_viewport_position(view.viewport_position(), False), 0) + def travel_to_pane(self, direction, create_new_if_necessary=False): adjacent_cell = self.adjacent_cell(direction) if adjacent_cell: @@ -130,6 +170,7 @@ def carry_file_to_pane(self, direction, create_new_if_necessary=False): if view == None: # If we're in an empty group, there's no active view return + window = self.window self.travel_to_pane(direction, create_new_if_necessary) window.set_view_index(view, window.active_group(), 0) @@ -150,14 +191,31 @@ def clone_file_to_pane(self, direction, create_new_if_necessary=False): window.set_view_index(new_view, group, original_index) # Fix the new view's selection and viewport - new_sel = new_view.sel() - new_sel.clear() - for s in view.sel(): - new_sel.add(s) - sublime.set_timeout(lambda : new_view.set_viewport_position(view.viewport_position(), False), 0) - + self.clone_viewport (view, new_view) self.carry_file_to_pane(direction, create_new_if_necessary) + def focus_file_on_pane(self, direction, create_new_if_necessary=False): + window = self.window + view = window.active_view() + if view == None: + # If we're in an empty group, there's no active view + # Therefore there is nothing to focus + return + + source_group = window.active_group() + self.travel_to_pane(direction, create_new_if_necessary) + target_group = window.active_group() + + buffer_exists, target_view = self.is_view_in_group(view, target_group) + + if buffer_exists: + self.clone_viewport (view, target_view) + window.focus_view(target_view) + else: + window.focus_group(source_group) + + return buffer_exists + def reorder_panes(self, leave_files_at_position = True): _, _, cells = self.get_layout() current_cell = cells[self.window.active_group()] @@ -520,6 +578,31 @@ def run(self, direction, create_new_if_necessary=None): create_new_if_necessary = self.settings().get('create_new_pane_if_necessary') self.carry_file_to_pane(direction, create_new_if_necessary) +class FocusFileOnPaneCommand(PaneCommand, WithSettings): + def run(self, direction, create_new_if_necessary=None): + if create_new_if_necessary is None: + create_new_if_necessary = self.settings().get('create_new_pane_if_necessary') + self.focus_file_on_pane(direction, create_new_if_necessary) + +class FocusOrCloneFileOnPaneCommand(PaneCommand, WithSettings): + def run(self, direction, create_new_if_necessary=None): + if create_new_if_necessary is None: + create_new_if_necessary = self.settings().get('create_new_pane_if_necessary') + if not self.focus_file_on_pane(direction, create_new_if_necessary): + self.clone_file_to_pane(direction, create_new_if_necessary) + +class FocusOrCloneFileOnClosestPaneCommand(PaneCommand, WithSettings): + def run(self, create_new_if_necessary=None): + if create_new_if_necessary is None: + create_new_if_necessary = self.settings().get('create_new_pane_if_necessary') + direction = self.get_closest_panel_direction() + if direction is None and create_new_if_necessary is False: + return + elif direction is None: + direction = 'right' + + if not self.focus_file_on_pane(direction, create_new_if_necessary): + self.clone_file_to_pane(direction, create_new_if_necessary) class CloneFileToPaneCommand(PaneCommand, WithSettings): def run(self, direction, create_new_if_necessary=None):