Implement directional movement of windows

This commit is contained in:
Kovid Goyal 2018-09-08 23:51:09 +05:30
parent b18b705632
commit cb3671343e
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
4 changed files with 34 additions and 7 deletions

View File

@ -9,8 +9,9 @@ Changelog
- Add a new ``last_used_layout`` function that can be mapped to a shortcut to - Add a new ``last_used_layout`` function that can be mapped to a shortcut to
switch to the previously used window layout (:iss:`870`) switch to the previously used window layout (:iss:`870`)
- Add a new ``neighboring_window`` function to switch to neighboring - Add new ``neighboring_window`` and ``move_window`` functions to switch to
windows in the current layout, similar to window movement in vim (:iss:`916`) neighboring windows in the current layout, and move them around, similar to
window movement in vim (:iss:`916`)
- Linux: Ensure that the python embedded in the kitty binary build always uses - Linux: Ensure that the python embedded in the kitty binary build always uses
UTF-8 mode (:iss:`924`) UTF-8 mode (:iss:`924`)

View File

@ -151,11 +151,13 @@ Focus specific window :sc:`first_window`, :sc:`second_window` ... :sc:`ten
(clockwise from the top-left) (clockwise from the top-left)
======================== ======================= ======================== =======================
Additionally, you can define shortcuts in :file:`kitty.conf` to go to neighboring Additionally, you can define shortcuts in :file:`kitty.conf` to focus neighboring
windows (similar to window movement in vim):: windows and move windows around (similar to window movement in vim)::
map ctrl+left neighboring_window left map ctrl+left neighboring_window left
map ctrl+down neighboring_window bottom map shift+left move_window right
map ctrl+down neighboring_window down
map shift+down move_window up
... ...

View File

@ -150,6 +150,19 @@ def neighboring_window(func, rest):
return func, [rest] return func, [rest]
@func_with_args('move_window')
def move_window(func, rest):
rest = rest.lower()
rest = {'up': 'top', 'down': 'bottom'}.get(rest, rest)
try:
rest = int(rest)
except Exception:
if rest not in ('left', 'right', 'top', 'bottom'):
log_error('Invalid move_window specification: {}'.format(rest))
rest = 0
return func, [rest]
def parse_key_action(action): def parse_key_action(action):
parts = action.split(' ', 1) parts = action.split(' ', 1)
func = parts[0] func = parts[0]

View File

@ -237,14 +237,25 @@ class Layout: # {{{
return ans return ans
def move_window(self, all_windows, active_window_idx, delta=1): def move_window(self, all_windows, active_window_idx, delta=1):
# delta can be either a number or a string such as 'left', 'top', etc
# for neighborhood moves
w = all_windows[active_window_idx] w = all_windows[active_window_idx]
windows = process_overlaid_windows(all_windows)[1] windows = process_overlaid_windows(all_windows)[1]
if len(windows) < 2 or abs(delta) == 0: if len(windows) < 2 or not delta:
return active_window_idx return active_window_idx
idx = idx_for_id(w.id, windows) idx = idx_for_id(w.id, windows)
if idx is None: if idx is None:
idx = idx_for_id(w.overlay_window_id, windows) idx = idx_for_id(w.overlay_window_id, windows)
if isinstance(delta, int):
nidx = (idx + len(windows) + delta) % len(windows) nidx = (idx + len(windows) + delta) % len(windows)
else:
delta = delta.lower()
delta = {'up': 'top', 'down': 'bottom'}.get(delta, delta)
neighbors = self.neighbors_for_window(w, windows)
if not neighbors.get(delta):
return active_window_idx
nidx = idx_for_id(neighbors[delta][0].id, windows)
nw = windows[nidx] nw = windows[nidx]
nidx = idx_for_id(nw.id, all_windows) nidx = idx_for_id(nw.id, all_windows)
idx = active_window_idx idx = active_window_idx