Skip to content

Commit

Permalink
Autosave all when the terminal loses focus
Browse files Browse the repository at this point in the history
  • Loading branch information
groves committed Aug 4, 2022
1 parent 2af90fa commit c5d35aa
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 5 additions & 1 deletion helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ use std::{
use anyhow::{Context, Error};

use crossterm::{
event::{DisableMouseCapture, EnableMouseCapture, Event},
event::{
DisableFocusChange, DisableMouseCapture, EnableFocusChange, EnableMouseCapture, Event,
},
execute, terminal,
tty::IsTty,
};
Expand Down Expand Up @@ -791,6 +793,7 @@ impl Application {
let mut stdout = stdout();
execute!(stdout, terminal::EnterAlternateScreen)?;
execute!(stdout, terminal::Clear(terminal::ClearType::All))?;
execute!(stdout, EnableFocusChange)?;
if self.config.load().editor.mouse {
execute!(stdout, EnableMouseCapture)?;
}
Expand All @@ -804,6 +807,7 @@ impl Application {
// Ignore errors on disabling, this might trigger on windows if we call
// disable without calling enable previously
let _ = execute!(stdout, DisableMouseCapture);
execute!(stdout, DisableFocusChange)?;
execute!(stdout, terminal::LeaveAlternateScreen)?;
terminal::disable_raw_mode()?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ fn write_all_impl(
bail!(errors)
}

fn write_all(
pub fn write_all(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
Expand Down Expand Up @@ -2007,7 +2007,7 @@ pub static TYPABLE_COMMAND_MAP: Lazy<HashMap<&'static str, &'static TypableComma
.collect()
});

pub fn command_mode(cx: &mut Context) {
pub(super) fn command_mode(cx: &mut Context) {
let mut prompt = Prompt::new(
":".into(),
Some(':'),
Expand Down
8 changes: 8 additions & 0 deletions helix-term/src/ui/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{
compositor::{Component, Context, EventResult},
job, key,
keymap::{KeymapResult, Keymaps},
ui::prompt::PromptEvent,
ui::{Completion, ProgressSpinners},
};

Expand Down Expand Up @@ -1236,6 +1237,13 @@ impl Component for EditorView {
}

Event::Mouse(event) => self.handle_mouse_event(event, &mut cx),
Event::FocusGained => EventResult::Ignored(None),
Event::FocusLost => {
if let Err(e) = commands::typed::write_all(context, &[], PromptEvent::Validate) {
context.editor.set_error(format!("{}", e));
}
EventResult::Consumed(None)
}
}
}

Expand Down

0 comments on commit c5d35aa

Please sign in to comment.