From a18d50b77759d6305b99ff59baa1ccaf9d644a88 Mon Sep 17 00:00:00 2001 From: Nathan Vegdahl Date: Mon, 21 Jun 2021 12:36:01 -0700 Subject: Add command to set the document's default line ending. --- helix-term/src/commands.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'helix-term/src') diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 5cb30da6..63a4d901 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1160,6 +1160,45 @@ mod cmd { } } + /// Sets or reports the current document's line ending setting. + fn set_line_ending(editor: &mut Editor, args: &[&str], event: PromptEvent) { + use LineEnding::*; + + // If no argument, report current line ending setting. + if args.is_empty() { + let line_ending = current!(editor).1.line_ending; + editor.set_status(match line_ending { + Crlf => "crlf".into(), + LF => "line feed".into(), + FF => "form feed".into(), + CR => "carriage return".into(), + Nel => "next line".into(), + + // These should never be a document's default line ending. + VT | LS | PS => "error".into(), + }); + return; + } + + // Attempt to parse argument as a line ending. + let line_ending = match args.get(0) { + // We check for CR first because it shares a common prefix with CRLF. + Some(arg) if "cr".starts_with(&arg.to_lowercase()) => Some(CR), + Some(arg) if "crlf".starts_with(&arg.to_lowercase()) => Some(Crlf), + Some(arg) if "lf".starts_with(&arg.to_lowercase()) => Some(LF), + Some(arg) if "ff".starts_with(&arg.to_lowercase()) => Some(FF), + Some(arg) if "nel".starts_with(&arg.to_lowercase()) => Some(Nel), + _ => None, + }; + + if let Some(le) = line_ending { + doc_mut!(editor).line_ending = le; + } else { + // Invalid argument. + editor.set_error(format!("invalid line ending '{}'", args[0],)); + } + } + fn earlier(editor: &mut Editor, args: &[&str], event: PromptEvent) { let uk = match args.join(" ").parse::() { Ok(uk) => uk, @@ -1392,6 +1431,13 @@ mod cmd { fun: set_indent_style, completer: None, }, + TypableCommand { + name: "line-ending", + alias: None, + doc: "Set the document's default line ending. Options: crlf, lf, cr, ff, nel.", + fun: set_line_ending, + completer: None, + }, TypableCommand { name: "earlier", alias: Some("ear"), -- cgit v1.2.3-70-g09d2