Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add :cquit! command and prevent :cquit from ignoring unsaved changes #1414

Merged
merged 2 commits into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions book/src/generated/typable-cmd.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
| `:quit-all`, `:qa` | Close all views. |
| `:quit-all!`, `:qa!` | Close all views forcefully (ignoring unsaved changes). |
| `:cquit`, `:cq` | Quit with exit code (default 1). Accepts an optional integer exit code (:cq 2). |
| `:cquit!`, `:cq!` | Quit with exit code (default 1) forcefully (ignoring unsaved changes). Accepts an optional integer exit code (:cq! 2). |
| `:theme` | Change the editor theme. |
| `:clipboard-yank` | Yank main selection into system clipboard. |
| `:clipboard-yank-join` | Yank joined selections into system clipboard. A separator can be provided as first argument. Default value is newline. |
Expand Down
45 changes: 28 additions & 17 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2323,12 +2323,7 @@ pub mod cmd {
write_all_impl(cx, args, event, true, true)
}

fn quit_all_impl(
editor: &mut Editor,
_args: &[Cow<str>],
_event: PromptEvent,
force: bool,
) -> anyhow::Result<()> {
fn quit_all_impl(editor: &mut Editor, force: bool) -> anyhow::Result<()> {
if !force {
buffers_remaining_impl(editor)?;
}
Expand All @@ -2344,18 +2339,18 @@ pub mod cmd {

fn quit_all(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
_args: &[Cow<str>],
_event: PromptEvent,
) -> anyhow::Result<()> {
quit_all_impl(cx.editor, args, event, false)
quit_all_impl(cx.editor, false)
}

fn force_quit_all(
cx: &mut compositor::Context,
args: &[Cow<str>],
event: PromptEvent,
_args: &[Cow<str>],
_event: PromptEvent,
) -> anyhow::Result<()> {
quit_all_impl(cx.editor, args, event, true)
quit_all_impl(cx.editor, true)
}

fn cquit(
Expand All @@ -2369,12 +2364,21 @@ pub mod cmd {
.unwrap_or(1);
cx.editor.exit_code = exit_code;

let views: Vec<_> = cx.editor.tree.views().map(|(view, _)| view.id).collect();
for view_id in views {
cx.editor.close(view_id);
}
quit_all_impl(cx.editor, false)
}

Ok(())
fn force_cquit(
cx: &mut compositor::Context,
args: &[Cow<str>],
_event: PromptEvent,
) -> anyhow::Result<()> {
let exit_code = args
.first()
.and_then(|code| code.parse::<i32>().ok())
.unwrap_or(1);
cx.editor.exit_code = exit_code;

quit_all_impl(cx.editor, true)
}

fn theme(
Expand Down Expand Up @@ -2878,6 +2882,13 @@ pub mod cmd {
fun: cquit,
completer: None,
},
TypableCommand {
name: "cquit!",
aliases: &["cq!"],
doc: "Quit with exit code (default 1) forcefully (ignoring unsaved changes). Accepts an optional integer exit code (:cq! 2).",
fun: force_cquit,
completer: None,
},
TypableCommand {
name: "theme",
aliases: &[],
Expand Down