Skip to content

Commit f1a0d32

Browse files
committed
Autosave all when the terminal loses focus
1 parent dad6d0f commit f1a0d32

File tree

7 files changed

+20
-9
lines changed

7 files changed

+20
-9
lines changed

Cargo.lock

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

helix-term/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ which = "4.2"
3737

3838
tokio = { version = "1", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot"] }
3939
tui = { path = "../helix-tui", package = "helix-tui", default-features = false, features = ["crossterm"] }
40-
crossterm = { version = "0.24", features = ["event-stream"] }
40+
crossterm = { version = "0.24.0", features = ["event-stream"], git = "https://github.com/groves/crossterm.git", branch = "emit_focus_events" }
4141
signal-hook = "0.3"
4242
tokio-stream = "0.1"
4343
futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false }

helix-term/src/application.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ use std::{
2828
use anyhow::{Context, Error};
2929

3030
use crossterm::{
31-
event::{DisableMouseCapture, EnableMouseCapture, Event},
31+
event::{
32+
DisableFocusChange, DisableMouseCapture, EnableFocusChange, EnableMouseCapture, Event,
33+
},
3234
execute, terminal,
3335
tty::IsTty,
3436
};
@@ -779,6 +781,7 @@ impl Application {
779781
let mut stdout = stdout();
780782
execute!(stdout, terminal::EnterAlternateScreen)?;
781783
execute!(stdout, terminal::Clear(terminal::ClearType::All))?;
784+
execute!(stdout, EnableFocusChange)?;
782785
if self.config.load().editor.mouse {
783786
execute!(stdout, EnableMouseCapture)?;
784787
}
@@ -792,6 +795,7 @@ impl Application {
792795
// Ignore errors on disabling, this might trigger on windows if we call
793796
// disable without calling enable previously
794797
let _ = execute!(stdout, DisableMouseCapture);
798+
execute!(stdout, DisableFocusChange)?;
795799
execute!(stdout, terminal::LeaveAlternateScreen)?;
796800
terminal::disable_raw_mode()?;
797801
Ok(())

helix-term/src/commands/typed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ fn write_all_impl(
594594
bail!(errors)
595595
}
596596

597-
fn write_all(
597+
pub fn write_all(
598598
cx: &mut compositor::Context,
599599
args: &[Cow<str>],
600600
event: PromptEvent,
@@ -2007,7 +2007,7 @@ pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableComma
20072007
.collect()
20082008
});
20092009

2010-
pub fn command_mode(cx: &mut Context) {
2010+
pub(super) fn command_mode(cx: &mut Context) {
20112011
let mut prompt = Prompt::new(
20122012
":".into(),
20132013
Some(':'),

helix-term/src/ui/editor.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::{
33
compositor::{Component, Context, EventResult},
44
job, key,
55
keymap::{KeymapResult, Keymaps},
6+
ui::prompt::PromptEvent,
67
ui::{Completion, ProgressSpinners},
78
};
89

@@ -25,7 +26,7 @@ use helix_view::{
2526
};
2627
use std::borrow::Cow;
2728

28-
use crossterm::event::{Event, MouseButton, MouseEvent, MouseEventKind};
29+
use crossterm::event::{Event, FocusEventKind, MouseButton, MouseEvent, MouseEventKind};
2930
use tui::buffer::Buffer as Surface;
3031

3132
use super::lsp::SignatureHelp;
@@ -1233,6 +1234,13 @@ impl Component for EditorView {
12331234
}
12341235

12351236
Event::Mouse(event) => self.handle_mouse_event(event, &mut cx),
1237+
Event::Focus(FocusEventKind::Gained) => EventResult::Ignored(None),
1238+
Event::Focus(FocusEventKind::Lost) => {
1239+
if let Err(e) = commands::typed::write_all(context, &[], PromptEvent::Validate) {
1240+
context.editor.set_error(format!("{}", e));
1241+
}
1242+
EventResult::Consumed(None)
1243+
}
12361244
}
12371245
}
12381246

helix-tui/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ default = ["crossterm"]
1919
bitflags = "1.3"
2020
cassowary = "0.3"
2121
unicode-segmentation = "1.9"
22-
crossterm = { version = "0.24", optional = true }
22+
crossterm = { version = "0.24.0", optional = true, git = "https://github.com/groves/crossterm.git", branch = "emit_focus_events" }
2323
serde = { version = "1", "optional" = true, features = ["derive"]}
2424
helix-view = { version = "0.6", path = "../helix-view", features = ["term"] }
2525
helix-core = { version = "0.6", path = "../helix-core" }

helix-view/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ anyhow = "1"
1919
helix-core = { version = "0.6", path = "../helix-core" }
2020
helix-lsp = { version = "0.6", path = "../helix-lsp" }
2121
helix-dap = { version = "0.6", path = "../helix-dap" }
22-
crossterm = { version = "0.24", optional = true }
22+
crossterm = { version = "0.24.0", optional = true, git = "https://github.com/groves/crossterm.git", branch = "emit_focus_events" }
2323

2424
# Conversion traits
2525
once_cell = "1.13"

0 commit comments

Comments
 (0)