Skip to content

Commit 8a224f9

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

File tree

8 files changed

+28
-9
lines changed

8 files changed

+28
-9
lines changed

Cargo.lock

+1-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

helix-term/Cargo.toml

+1-1
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

+5-1
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

+2-2
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

+8
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

@@ -1233,6 +1234,13 @@ impl Component for EditorView {
12331234
}
12341235

12351236
Event::Mouse(event) => self.handle_mouse_event(event, &mut cx),
1237+
Event::FocusGained => EventResult::Ignored(None),
1238+
Event::FocusLost => {
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

+1-1
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

+1-1
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"

helix-view/src/input.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,13 @@ impl<'de> Deserialize<'de> for KeyEvent {
221221

222222
#[cfg(feature = "term")]
223223
impl From<crossterm::event::KeyEvent> for KeyEvent {
224-
fn from(crossterm::event::KeyEvent { code, modifiers }: crossterm::event::KeyEvent) -> Self {
224+
fn from(
225+
crossterm::event::KeyEvent {
226+
code,
227+
modifiers,
228+
kind: _,
229+
}: crossterm::event::KeyEvent,
230+
) -> Self {
225231
if code == crossterm::event::KeyCode::BackTab {
226232
// special case for BackTab -> Shift-Tab
227233
let mut modifiers: KeyModifiers = modifiers.into();
@@ -249,11 +255,13 @@ impl From<KeyEvent> for crossterm::event::KeyEvent {
249255
crossterm::event::KeyEvent {
250256
code: crossterm::event::KeyCode::BackTab,
251257
modifiers: modifiers.into(),
258+
kind: crossterm::event::KeyEventKind::Press,
252259
}
253260
} else {
254261
crossterm::event::KeyEvent {
255262
code: code.into(),
256263
modifiers: modifiers.into(),
264+
kind: crossterm::event::KeyEventKind::Press,
257265
}
258266
}
259267
}

0 commit comments

Comments
 (0)