Skip to content

Commit c5d35aa

Browse files
committed
Autosave all when the terminal loses focus
1 parent 2af90fa commit c5d35aa

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

helix-term/src/application.rs

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

3131
use crossterm::{
32-
event::{DisableMouseCapture, EnableMouseCapture, Event},
32+
event::{
33+
DisableFocusChange, DisableMouseCapture, EnableFocusChange, EnableMouseCapture, Event,
34+
},
3335
execute, terminal,
3436
tty::IsTty,
3537
};
@@ -791,6 +793,7 @@ impl Application {
791793
let mut stdout = stdout();
792794
execute!(stdout, terminal::EnterAlternateScreen)?;
793795
execute!(stdout, terminal::Clear(terminal::ClearType::All))?;
796+
execute!(stdout, EnableFocusChange)?;
794797
if self.config.load().editor.mouse {
795798
execute!(stdout, EnableMouseCapture)?;
796799
}
@@ -804,6 +807,7 @@ impl Application {
804807
// Ignore errors on disabling, this might trigger on windows if we call
805808
// disable without calling enable previously
806809
let _ = execute!(stdout, DisableMouseCapture);
810+
execute!(stdout, DisableFocusChange)?;
807811
execute!(stdout, terminal::LeaveAlternateScreen)?;
808812
terminal::disable_raw_mode()?;
809813
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: 8 additions & 0 deletions
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

@@ -1236,6 +1237,13 @@ impl Component for EditorView {
12361237
}
12371238

12381239
Event::Mouse(event) => self.handle_mouse_event(event, &mut cx),
1240+
Event::FocusGained => EventResult::Ignored(None),
1241+
Event::FocusLost => {
1242+
if let Err(e) = commands::typed::write_all(context, &[], PromptEvent::Validate) {
1243+
context.editor.set_error(format!("{}", e));
1244+
}
1245+
EventResult::Consumed(None)
1246+
}
12391247
}
12401248
}
12411249

0 commit comments

Comments
 (0)