diff --git a/docs/changelog.md b/docs/changelog.md index 4815329cf..76e74849a 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -35,6 +35,7 @@ brief notes about them may accumulate here. * Fixed: Windows: we now compile libssh2 against openssl on all platforms to improve overall key and crypto algorithm support * Fixed: spawning a new tab via the launcher menu failed because it used the pretty printed multiplexer domain label rather than the multiplexer domain name. * Fixed: macOS: middle mouse button wasn't recognized. Thanks to [@guswynn](https://github.com/guswynn)! [#599](https://github.com/wez/wezterm/pull/599) +* New: added [ActivateLastTab](config/lua/keyassignment/ActivateLastTab.md) key assignment for jumping back to a previously active tab. Thanks to [@alexgartrell](https://github.com/alexgartrell) [#610](https://github.com/wez/wezterm/pull/610) ### 20210314-114017-04b7cedd diff --git a/docs/config/lua/keyassignment/ActivateLastTab.md b/docs/config/lua/keyassignment/ActivateLastTab.md index 896c1c40d..c5ef43e6b 100644 --- a/docs/config/lua/keyassignment/ActivateLastTab.md +++ b/docs/config/lua/keyassignment/ActivateLastTab.md @@ -1,11 +1,14 @@ # ActivateLastTab +*Since: nightly builds only* + Activate the last active tab. If there is none, it will do nothing. - ```lua return { + leader = { key="a", mods="CTRL" }, keys = { + -- CTRL-a, followed by CTRL-o will switch back to the last active tab {key="o", mods="LEADER|CTRL", action="ActivateLastTab"}, } } diff --git a/mux/src/window.rs b/mux/src/window.rs index fefa0275d..e164628dc 100644 --- a/mux/src/window.rs +++ b/mux/src/window.rs @@ -103,7 +103,7 @@ impl Window { self.tabs.remove(idx); let len = self.tabs.len(); if len > 0 && self.active == idx && idx >= len { - self.set_active(len - 1); + self.set_active_without_saving(len - 1); } true } else { @@ -139,7 +139,20 @@ impl Window { } } - pub fn set_active(&mut self, idx: usize) { + /// If `idx` is different from the current active tab, + /// save the current tabid and then make `idx` the active + /// tab position. + pub fn save_and_then_set_active(&mut self, idx: usize) { + if idx == self.get_active_idx() { + return; + } + self.save_last_active(); + self.set_active_without_saving(idx); + } + + /// Make `idx` the active tab position. + /// The saved tab id is not changed. + pub fn set_active_without_saving(&mut self, idx: usize) { assert!(idx < self.tabs.len()); self.invalidated = true; self.active = idx; diff --git a/wezterm-gui/src/overlay/tabnavigator.rs b/wezterm-gui/src/overlay/tabnavigator.rs index 137c56d7e..dcfbc9c45 100644 --- a/wezterm-gui/src/overlay/tabnavigator.rs +++ b/wezterm-gui/src/overlay/tabnavigator.rs @@ -80,7 +80,7 @@ pub fn tab_navigator( .get_window_mut(mux_window_id) .ok_or_else(|| anyhow!("no such window"))?; - window.set_active(idx); + window.save_and_then_set_active(idx); anyhow::Result::<()>::Ok(()) }) .detach(); diff --git a/wezterm-gui/src/termwindow/mod.rs b/wezterm-gui/src/termwindow/mod.rs index 71af3d985..97cae1208 100644 --- a/wezterm-gui/src/termwindow/mod.rs +++ b/wezterm-gui/src/termwindow/mod.rs @@ -1098,10 +1098,7 @@ impl TermWindow { }; if tab_idx < max { - if tab_idx != window.get_active_idx() { - window.save_last_active(); - } - window.set_active(tab_idx); + window.save_and_then_set_active(tab_idx); drop(window); @@ -1160,7 +1157,7 @@ impl TermWindow { let tab_inst = window.remove_by_idx(active); window.insert(tab_idx, &tab_inst); - window.set_active(tab_idx); + window.set_active_without_saving(tab_idx); drop(window); self.update_title(); diff --git a/wezterm-gui/src/termwindow/spawn.rs b/wezterm-gui/src/termwindow/spawn.rs index 58abea6fa..1307ec149 100644 --- a/wezterm-gui/src/termwindow/spawn.rs +++ b/wezterm-gui/src/termwindow/spawn.rs @@ -186,9 +186,8 @@ impl super::TermWindow { let mut window = mux .get_window_mut(target_window_id) .ok_or_else(|| anyhow!("no such window!?"))?; - window.save_last_active(); if let Some(idx) = window.idx_by_id(tab_id) { - window.set_active(idx); + window.save_and_then_set_active(idx); } } }