From 7283f447427238e1ab7a44b2a2b1f1bb355afad3 Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Tue, 3 Aug 2021 18:55:21 +0200 Subject: [PATCH] i3-companion: make Alt-Tab local to a given output --- bin/i3-companion | 35 +++++++++++++++++++++++++++++++++++ config | 2 +- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/bin/i3-companion b/bin/i3-companion index 8df6b70..c550a5d 100755 --- a/bin/i3-companion +++ b/bin/i3-companion @@ -330,6 +330,41 @@ async def workspace_rename(i3, event): await i3.command(";".join(commands)) +@on(CommandEvent("previous-workspace"), I3Event.WORKSPACE_FOCUS) +@static(history=collections.defaultdict(list)) +async def previous_workspace(i3, event): + """Go to previous workspace on the same output.""" + history = previous_workspace.history + if isinstance(event, i3ipc.WorkspaceEvent) and event.old: + data = event.old.ipc_data + output, num = data["output"], data["num"] + if history[output][-1:] != [num]: + history[output].append(num) + history[output] = history[output][-2:] + logger.debug("on %s, history is %s", output, history[output]) + elif event == "previous-workspace": + workspaces = await i3.get_workspaces() + try: + focused = [w for w in workspaces if w.focused][0] + except IndexError: + return + output = focused.output + while True: + try: + previous = history[output].pop() + if [ + w + for w in workspaces + if w.num != focused.num and w.num == previous and w.output == output + ]: + break + except IndexError: + logger.debug("no previous workspace on %s", output) + return + logger.debug("switching to workspace %d on %s", previous, output) + await i3.command(f"workspace number {previous}") + + @on(CommandEvent("new-workspace"), CommandEvent("move-to-new-workspace")) async def new_workspace(i3, event): """Create a new workspace and optionally move a window to it.""" diff --git a/config b/config index b59a24d..877add5 100644 --- a/config +++ b/config @@ -112,7 +112,7 @@ bindsym $mod+7 workspace number $ws7 bindsym $mod+8 workspace number $ws8 bindsym $mod+9 workspace number $ws9 bindsym $mod+0 workspace number $ws10 -bindsym $mod+Tab workspace back_and_forth +bindsym $mod+Tab nop "previous-workspace" bindsym $mod+Ctrl+o focus output right bindsym $mod+n nop "new-workspace" focus_wrapping no