|
| 1 | +# The MIT License (MIT) |
| 2 | + |
| 3 | +# Copyright (c) 2017 Origami Contributors |
| 4 | + |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | + |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | + |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | +# |
| 23 | +# This file has been took from https://github.com/SublimeText/Origami and |
| 24 | +# modified to work with this plugin |
| 25 | + |
| 26 | +from __future__ import division |
| 27 | +import sublime |
| 28 | +import sublime_plugin |
| 29 | +import copy |
| 30 | +from functools import partial |
| 31 | + |
| 32 | +XMIN, YMIN, XMAX, YMAX = list(range(4)) |
| 33 | + |
| 34 | + |
| 35 | +def increment_if_greater_or_equal(x, threshold): |
| 36 | + if x >= threshold: |
| 37 | + return x+1 |
| 38 | + return x |
| 39 | + |
| 40 | + |
| 41 | +def decrement_if_greater(x, threshold): |
| 42 | + if x > threshold: |
| 43 | + return x-1 |
| 44 | + return x |
| 45 | + |
| 46 | + |
| 47 | +def pull_up_cells_after(cells, threshold): |
| 48 | + return [[x0, decrement_if_greater(y0, threshold), |
| 49 | + x1, decrement_if_greater(y1, threshold)] |
| 50 | + for (x0, y0, x1, y1) in cells] |
| 51 | + |
| 52 | + |
| 53 | +def push_right_cells_after(cells, threshold): |
| 54 | + return [[increment_if_greater_or_equal(x0, threshold), y0, |
| 55 | + increment_if_greater_or_equal(x1, threshold), y1] |
| 56 | + for (x0, y0, x1, y1) in cells] |
| 57 | + |
| 58 | + |
| 59 | +def push_down_cells_after(cells, threshold): |
| 60 | + return [[x0, increment_if_greater_or_equal(y0, threshold), |
| 61 | + x1, increment_if_greater_or_equal(y1, threshold)] |
| 62 | + for (x0, y0, x1, y1) in cells] |
| 63 | + |
| 64 | + |
| 65 | +def pull_left_cells_after(cells, threshold): |
| 66 | + return [[decrement_if_greater(x0, threshold), y0, |
| 67 | + decrement_if_greater(x1, threshold), y1] |
| 68 | + for (x0, y0, x1, y1) in cells] |
| 69 | + |
| 70 | + |
| 71 | +def opposite_direction(direction): |
| 72 | + opposites = {"up": "down", "right": "left", "down": "up", "left": "right"} |
| 73 | + return opposites[direction] |
| 74 | + |
| 75 | + |
| 76 | +def cells_adjacent_to_cell_in_direction(cells, cell, direction): |
| 77 | + fn = None |
| 78 | + if direction == "up": |
| 79 | + fn = lambda orig, check: orig[YMIN] == check[YMAX] |
| 80 | + elif direction == "right": |
| 81 | + fn = lambda orig, check: orig[XMAX] == check[XMIN] |
| 82 | + elif direction == "down": |
| 83 | + fn = lambda orig, check: orig[YMAX] == check[YMIN] |
| 84 | + elif direction == "left": |
| 85 | + fn = lambda orig, check: orig[XMIN] == check[XMAX] |
| 86 | + |
| 87 | + if fn: |
| 88 | + return [c for c in cells if fn(cell, c)] |
| 89 | + return None |
| 90 | + |
| 91 | + |
| 92 | +def fixed_set_layout(window, layout): |
| 93 | + # A bug was introduced in Sublime Text 3, sometime before 3053, in that it |
| 94 | + # changes the active group to 0 when the layout is changed. Annoying. |
| 95 | + active_group = window.active_group() |
| 96 | + window.set_layout(layout) |
| 97 | + num_groups = len(layout['cells']) |
| 98 | + window.focus_group(min(active_group, num_groups-1)) |
| 99 | + |
| 100 | + |
| 101 | +def fixed_set_layout_no_focus_change(window, layout): |
| 102 | + active_group = window.active_group() |
| 103 | + window.set_layout(layout) |
| 104 | + |
| 105 | + |
| 106 | +class WithSettings: |
| 107 | + _settings = None |
| 108 | + |
| 109 | + def settings(self): |
| 110 | + if self._settings is None: |
| 111 | + self._settings = sublime.load_settings('Origami.sublime-settings') |
| 112 | + return self._settings |
| 113 | + |
| 114 | + |
| 115 | +class PaneCommand(sublime_plugin.WindowCommand): |
| 116 | + "Abstract base class for commands." |
| 117 | + |
| 118 | + def get_layout(self): |
| 119 | + layout = self.window.get_layout() |
| 120 | + cells = layout["cells"] |
| 121 | + rows = layout["rows"] |
| 122 | + cols = layout["cols"] |
| 123 | + return rows, cols, cells |
| 124 | + |
| 125 | + def get_cells(self): |
| 126 | + return self.get_layout()[2] |
| 127 | + |
| 128 | + def adjacent_cell(self, direction): |
| 129 | + cells = self.get_cells() |
| 130 | + current_cell = cells[self.window.active_group()] |
| 131 | + adjacent_cells = cells_adjacent_to_cell_in_direction( |
| 132 | + cells, current_cell, direction) |
| 133 | + rows, cols, _ = self.get_layout() |
| 134 | + |
| 135 | + if direction in ["left", "right"]: |
| 136 | + MIN, MAX, fields = YMIN, YMAX, rows |
| 137 | + else: # up or down |
| 138 | + MIN, MAX, fields = XMIN, XMAX, cols |
| 139 | + |
| 140 | + cell_overlap = [] |
| 141 | + for cell in adjacent_cells: |
| 142 | + start = max(fields[cell[MIN]], fields[current_cell[MIN]]) |
| 143 | + end = min(fields[cell[MAX]], fields[current_cell[MAX]]) |
| 144 | + # / (fields[cell[MAX]] - fields[cell[MIN]]) |
| 145 | + overlap = (end - start) |
| 146 | + cell_overlap.append(overlap) |
| 147 | + |
| 148 | + if len(cell_overlap) != 0: |
| 149 | + cell_index = cell_overlap.index(max(cell_overlap)) |
| 150 | + return adjacent_cells[cell_index] |
| 151 | + return None |
| 152 | + |
| 153 | + def duplicated_views(self, original_group, duplicating_group): |
| 154 | + original_views = self.window.views_in_group(original_group) |
| 155 | + original_buffers = [v.buffer_id() for v in original_views] |
| 156 | + potential_dupe_views = self.window.views_in_group(duplicating_group) |
| 157 | + dupe_views = [] |
| 158 | + for pd in potential_dupe_views: |
| 159 | + if pd.buffer_id() in original_buffers: |
| 160 | + dupe_views.append(pd) |
| 161 | + return dupe_views |
| 162 | + |
| 163 | + def travel_to_pane(self, direction, create_new_if_necessary=False): |
| 164 | + adjacent_cell = self.adjacent_cell(direction) |
| 165 | + if adjacent_cell: |
| 166 | + cells = self.get_cells() |
| 167 | + new_group_index = cells.index(adjacent_cell) |
| 168 | + self.window.focus_group(new_group_index) |
| 169 | + elif create_new_if_necessary: |
| 170 | + self.create_pane(direction, True) |
| 171 | + |
| 172 | + def create_pane(self, direction, give_focus=False): |
| 173 | + window = self.window |
| 174 | + rows, cols, cells = self.get_layout() |
| 175 | + current_group = window.active_group() |
| 176 | + |
| 177 | + old_cell = cells.pop(current_group) |
| 178 | + new_cell = [] |
| 179 | + |
| 180 | + if direction in ("up", "down"): |
| 181 | + cells = push_down_cells_after(cells, old_cell[YMAX]) |
| 182 | + rows.insert(old_cell[YMAX], (rows[old_cell[YMIN]] + |
| 183 | + rows[old_cell[YMAX]]) / 2) |
| 184 | + new_cell = [old_cell[XMIN], old_cell[YMAX], |
| 185 | + old_cell[XMAX], old_cell[YMAX]+1] |
| 186 | + old_cell = [old_cell[XMIN], old_cell[ |
| 187 | + YMIN], old_cell[XMAX], old_cell[YMAX]] |
| 188 | + |
| 189 | + elif direction in ("right", "left"): |
| 190 | + cells = push_right_cells_after(cells, old_cell[XMAX]) |
| 191 | + cols.insert( |
| 192 | + old_cell[XMAX], (cols[old_cell[XMIN]] + |
| 193 | + cols[old_cell[XMAX]]) / 2) |
| 194 | + new_cell = [old_cell[XMAX], old_cell[YMIN], |
| 195 | + old_cell[XMAX]+1, old_cell[YMAX]] |
| 196 | + old_cell = [old_cell[XMIN], old_cell[ |
| 197 | + YMIN], old_cell[XMAX], old_cell[YMAX]] |
| 198 | + |
| 199 | + if new_cell: |
| 200 | + if direction in ("left", "up"): |
| 201 | + focused_cell = new_cell |
| 202 | + unfocused_cell = old_cell |
| 203 | + else: |
| 204 | + focused_cell = old_cell |
| 205 | + unfocused_cell = new_cell |
| 206 | + cells.insert(current_group, focused_cell) |
| 207 | + cells.append(unfocused_cell) |
| 208 | + layout = {"cols": cols, "rows": rows, "cells": cells} |
| 209 | + fixed_set_layout(window, layout) |
| 210 | + |
| 211 | + if give_focus: |
| 212 | + self.travel_to_pane(direction) |
| 213 | + |
| 214 | + def destroy_current_pane(self): |
| 215 | + # Out of the four adjacent panes, one was split to create this pane. |
| 216 | + # Find out which one, move to it, then destroy this pane. |
| 217 | + cells = self.get_cells() |
| 218 | + |
| 219 | + current = cells[self.window.active_group()] |
| 220 | + choices = {} |
| 221 | + choices["up"] = self.adjacent_cell("up") |
| 222 | + choices["right"] = self.adjacent_cell("right") |
| 223 | + choices["down"] = self.adjacent_cell("down") |
| 224 | + choices["left"] = self.adjacent_cell("left") |
| 225 | + |
| 226 | + target_dir = None |
| 227 | + for dir, c in choices.items(): |
| 228 | + if not c: |
| 229 | + continue |
| 230 | + if dir in ["up", "down"]: |
| 231 | + if c[XMIN] == current[XMIN] and c[XMAX] == current[XMAX]: |
| 232 | + target_dir = dir |
| 233 | + elif dir in ["left", "right"]: |
| 234 | + if c[YMIN] == current[YMIN] and c[YMAX] == current[YMAX]: |
| 235 | + target_dir = dir |
| 236 | + if target_dir: |
| 237 | + self.travel_to_pane(target_dir) |
| 238 | + self.destroy_pane(opposite_direction(target_dir)) |
| 239 | + |
| 240 | + |
| 241 | +class upiotCreatePaneCommand(PaneCommand): |
| 242 | + |
| 243 | + def run(self, direction, give_focus=False): |
| 244 | + self.create_pane(direction, give_focus) |
0 commit comments