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 25, 2022
1 parent dad6d0f commit 8a224f9
Show file tree
Hide file tree
Showing 8 changed files with 28 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
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
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
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
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 @@ -1233,6 +1234,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
2 changes: 1 addition & 1 deletion helix-tui/Cargo.toml
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
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
10 changes: 9 additions & 1 deletion helix-view/src/input.rs
Expand Up @@ -221,7 +221,13 @@ impl<'de> Deserialize<'de> for KeyEvent {

#[cfg(feature = "term")]
impl From<crossterm::event::KeyEvent> for KeyEvent {
fn from(crossterm::event::KeyEvent { code, modifiers }: crossterm::event::KeyEvent) -> Self {
fn from(
crossterm::event::KeyEvent {
code,
modifiers,
kind: _,
}: crossterm::event::KeyEvent,
) -> Self {
if code == crossterm::event::KeyCode::BackTab {
// special case for BackTab -> Shift-Tab
let mut modifiers: KeyModifiers = modifiers.into();
Expand Down Expand Up @@ -249,11 +255,13 @@ impl From<KeyEvent> for crossterm::event::KeyEvent {
crossterm::event::KeyEvent {
code: crossterm::event::KeyCode::BackTab,
modifiers: modifiers.into(),
kind: crossterm::event::KeyEventKind::Press,
}
} else {
crossterm::event::KeyEvent {
code: code.into(),
modifiers: modifiers.into(),
kind: crossterm::event::KeyEventKind::Press,
}
}
}
Expand Down

0 comments on commit 8a224f9

Please sign in to comment.