Skip to content

Commit

Permalink
Add ActivateLastTab command
Browse files Browse the repository at this point in the history
This replicates `last-window` in tmux. To pull this off, I
deliberately store the last tab whenever I'm activating a new one or
spawning a new one. I had to do this explicitly rather than hooking
set_active, because we end up setting the active tab briefly for some
common operations like moving a tab.
  • Loading branch information
alexgartrell authored and wez committed Apr 2, 2021
1 parent 4e25a39 commit ee4b4b5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/src/keyassignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ pub enum KeyAssignment {
ResetFontSize,
ResetFontAndWindowSize,
ActivateTab(isize),
ActivateLastTab,
SendString(String),
Nop,
DisableDefaultAssignment,
Expand Down
14 changes: 14 additions & 0 deletions docs/config/lua/keyassignment/ActivateLastTab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# ActivateLastTab

Activate the last active tab. If there is none, it will do nothing.


```lua
return {
keys = {
{key="o", mods="LEADER|CTRL", action="ActivateLastTab"},
}
}
```


15 changes: 15 additions & 0 deletions mux/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub struct Window {
id: WindowId,
tabs: Vec<Rc<Tab>>,
active: usize,
last_active: Option<TabId>,
clipboard: Option<Arc<dyn Clipboard>>,
invalidated: bool,
}
Expand All @@ -20,6 +21,7 @@ impl Window {
id: WIN_ID.fetch_add(1, ::std::sync::atomic::Ordering::Relaxed),
tabs: vec![],
active: 0,
last_active: None,
clipboard: None,
invalidated: false,
}
Expand Down Expand Up @@ -124,6 +126,19 @@ impl Window {
self.active
}

pub fn save_last_active(&mut self) {
self.last_active = self.get_by_idx(self.active).map(|tab| tab.tab_id());
}

#[inline]
pub fn get_last_active_idx(&self) -> Option<usize> {
if let Some(tab_id) = self.last_active {
self.idx_by_id(tab_id)
} else {
None
}
}

pub fn set_active(&mut self, idx: usize) {
assert!(idx < self.tabs.len());
self.invalidated = true;
Expand Down
18 changes: 18 additions & 0 deletions wezterm-gui/src/termwindow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,9 @@ impl TermWindow {
};

if tab_idx < max {
if tab_idx != window.get_active_idx() {
window.save_last_active();
}
window.set_active(tab_idx);

drop(window);
Expand Down Expand Up @@ -1128,6 +1131,20 @@ impl TermWindow {
self.activate_tab((tab as usize % max) as isize)
}

fn activate_last_tab(&mut self) -> anyhow::Result<()> {
let mux = Mux::get().unwrap();
let window = mux
.get_window(self.mux_window_id)
.ok_or_else(|| anyhow!("no such window"))?;

let last_idx = window.get_last_active_idx();
drop(window);
match last_idx {
Some(idx) => self.activate_tab(idx as isize),
None => Ok(()),
}
}

fn move_tab(&mut self, tab_idx: usize) -> anyhow::Result<()> {
let mux = Mux::get().unwrap();
let mut window = mux
Expand Down Expand Up @@ -1387,6 +1404,7 @@ impl TermWindow {
ActivateTabRelative(n) => {
self.activate_tab_relative(*n)?;
}
ActivateLastTab => self.activate_last_tab()?,
DecreaseFontSize => self.decrease_font_size(),
IncreaseFontSize => self.increase_font_size(),
ResetFontSize => self.reset_font_size(),
Expand Down
1 change: 1 addition & 0 deletions wezterm-gui/src/termwindow/spawn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ 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);
}
Expand Down

0 comments on commit ee4b4b5

Please sign in to comment.