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 Jul 24, 2022
1 parent dad6d0f commit f1a0d32
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 9 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion helix-term/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ which = "4.2"

tokio = { version = "1", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot"] }
tui = { path = "../helix-tui", package = "helix-tui", default-features = false, features = ["crossterm"] }
crossterm = { version = "0.24", features = ["event-stream"] }
crossterm = { version = "0.24.0", features = ["event-stream"], git = "https://github.com/groves/crossterm.git", branch = "emit_focus_events" }
signal-hook = "0.3"
tokio-stream = "0.1"
futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false }
Expand Down
6 changes: 5 additions & 1 deletion helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,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 @@ -779,6 +781,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 @@ -792,6 +795,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
10 changes: 9 additions & 1 deletion 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 All @@ -25,7 +26,7 @@ use helix_view::{
};
use std::borrow::Cow;

use crossterm::event::{Event, MouseButton, MouseEvent, MouseEventKind};
use crossterm::event::{Event, FocusEventKind, MouseButton, MouseEvent, MouseEventKind};
use tui::buffer::Buffer as Surface;

use super::lsp::SignatureHelp;
Expand Down Expand Up @@ -1233,6 +1234,13 @@ impl Component for EditorView {
}

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

Expand Down
2 changes: 1 addition & 1 deletion helix-tui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ default = ["crossterm"]
bitflags = "1.3"
cassowary = "0.3"
unicode-segmentation = "1.9"
crossterm = { version = "0.24", optional = true }
crossterm = { version = "0.24.0", optional = true, git = "https://github.com/groves/crossterm.git", branch = "emit_focus_events" }
serde = { version = "1", "optional" = true, features = ["derive"]}
helix-view = { version = "0.6", path = "../helix-view", features = ["term"] }
helix-core = { version = "0.6", path = "../helix-core" }
2 changes: 1 addition & 1 deletion helix-view/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ anyhow = "1"
helix-core = { version = "0.6", path = "../helix-core" }
helix-lsp = { version = "0.6", path = "../helix-lsp" }
helix-dap = { version = "0.6", path = "../helix-dap" }
crossterm = { version = "0.24", optional = true }
crossterm = { version = "0.24.0", optional = true, git = "https://github.com/groves/crossterm.git", branch = "emit_focus_events" }

# Conversion traits
once_cell = "1.13"
Expand Down

0 comments on commit f1a0d32

Please sign in to comment.