mod address;
mod bre;
mod buffer;
mod enc;
use address::Spec;
use buffer::Buffer;
use std::env;
use std::io::{self, BufRead, Write};
use std::process::ExitCode;
fn main() -> ExitCode {
let args: Vec<String> = env::args().skip(1).collect();
let (prompt, filename) = match parse_args(&args) {
Ok(r) => r,
Err(e) => {
eprintln!("ved: {e}");
eprintln!("try 'ved --help' for usage information");
return ExitCode::from(2);
}
};
run_repl(prompt.as_deref(), filename.as_deref())
}
fn parse_args(args: &[String]) -> Result<(Option<String>, Option<String>), String> {
let mut prompt: Option<String> = None;
let mut filename: Option<String> = None;
let mut i = 0;
while i < args.len() {
let arg = &args[i];
match arg.as_str() {
"-p" | "--prompt" => {
i += 1;
if i >= args.len() {
return Err(format!("{arg} requires a value"));
}
prompt = Some(args[i].clone());
}
s if s.starts_with("--prompt=") => {
prompt = Some(s["--prompt=".len()..].to_string());
}
"-h" | "--help" => {
print_usage();
std::process::exit(0);
}
"-V" | "--version" => {
println!("ved {}", env!("CARGO_PKG_VERSION"));
std::process::exit(0);
}
s if s.starts_with('-') => {
return Err(format!("unknown option: {s}"));
}
_ => {
filename = Some(arg.clone());
}
}
i += 1;
}
Ok((prompt, filename))
}
fn print_usage() {
println!("ved - the verbose ed");
println!();
println!("Usage: ved [OPTIONS] [FILE]");
println!();
println!("Options:");
println!(" -p, --prompt <STRING> Set the command prompt");
println!(" -h, --help Show this help message and exit");
println!(" -V, --version Show version information and exit");
}
fn run_repl(prompt: Option<&str>, filename: Option<&str>) -> ExitCode {
let stdin = io::stdin();
let stdout = io::stdout();
let mut out = stdout.lock();
let mut reader = stdin.lock();
let mut line = String::new();
let mut buffer = Buffer::new();
let mut input_mode = false;
let mut quit_warned = false;
if let Some(f) = filename {
match load_file(f, &mut buffer) {
Ok(LoadOutcome::Loaded(bytes)) => {
let _ = writeln!(out, "{bytes}");
}
Ok(LoadOutcome::NewFile) => {
let _ = writeln!(out, "{f}: new file");
}
Err(e) => {
let _ = writeln!(out, "? {e}");
}
}
}
loop {
if !input_mode {
if let Some(p) = prompt {
if write!(out, "{p}").is_err() || out.flush().is_err() {
return ExitCode::FAILURE;
}
}
}
line.clear();
match reader.read_line(&mut line) {
Ok(0) => return ExitCode::SUCCESS, Ok(_) => {}
Err(e) => {
eprintln!("ved: read error: {e}");
return ExitCode::FAILURE;
}
}
let trimmed = line.trim_end_matches('\n').trim_end_matches('\r');
if input_mode {
if trimmed == "." {
input_mode = false;
} else {
buffer.append_after(buffer.current(), trimmed.to_string());
}
continue;
}
match dispatch(trimmed, &mut buffer) {
Action::Quit => {
if buffer.is_modified() && !quit_warned {
let _ = writeln!(out, "? warning: buffer modified");
quit_warned = true;
} else {
return ExitCode::SUCCESS;
}
}
Action::ForceQuit => {
return ExitCode::SUCCESS;
}
Action::EnterInputMode => {
input_mode = true;
quit_warned = false;
}
Action::Done => {
quit_warned = false;
}
Action::Print(msg) => {
let _ = writeln!(out, "{msg}");
quit_warned = false;
}
Action::Error(msg) => {
let _ = writeln!(out, "? {msg}");
quit_warned = false;
}
}
}
}
enum Action {
Quit,
ForceQuit,
EnterInputMode,
Done,
Print(String),
Error(String),
}
fn dispatch(cmd: &str, buf: &mut Buffer) -> Action {
let cmd = cmd.trim_start();
if cmd.is_empty() {
if buf.is_empty() {
return Action::Error("invalid address".to_string());
}
let next = buf.current() + 1;
if next > buf.len() {
return Action::Error("invalid address".to_string());
}
buf.set_current(next);
return match buf.line(next) {
Some(s) => Action::Print(s.to_string()),
None => Action::Error("invalid address".to_string()),
};
}
let (spec, rest) = match Spec::parse(cmd) {
Ok(parsed) => parsed,
Err(e) => return Action::Error(e),
};
if rest.is_empty() {
return run_print(&spec, buf, PrintMode::Plain);
}
match rest {
"q" | "quit" => return Action::Quit,
"a" | "append" => {
if !buf.is_empty() {
if let Ok(r) = spec.resolve(buf) {
buf.set_current(r.end);
}
}
return Action::EnterInputMode;
}
"i" | "insert" => {
if !buf.is_empty() {
let addr = match spec.resolve(buf) {
Ok(r) => r.end,
Err(e) => return Action::Error(e),
};
buf.set_current(addr.saturating_sub(1));
}
return Action::EnterInputMode;
}
"c" | "change" => {
let range = match spec.resolve(buf) {
Ok(r) => r,
Err(e) => return Action::Error(e),
};
let start = range.start;
buf.delete_range(range.start, range.end);
buf.set_current(start.saturating_sub(1));
return Action::EnterInputMode;
}
"d" | "delete" => return run_delete(&spec, buf),
"p" | "print" => return run_print(&spec, buf, PrintMode::Plain),
"n" | "number" => return run_print(&spec, buf, PrintMode::Numbered),
"l" | "list" => return run_print(&spec, buf, PrintMode::List),
"j" | "join" => return run_join(&spec, buf),
"H" | "help" => return run_help(),
"Q" => return Action::ForceQuit,
_ => {}
}
let first = rest.as_bytes()[0];
let args = &rest[1..];
match first {
b'e' => run_edit(buf, args),
b'r' => run_read(&spec, buf, args),
b'g' => run_global(&spec, buf, args, false),
b'v' => run_global(&spec, buf, args, true),
b's' => run_substitute(&spec, buf, args),
b'w' => run_write(&spec, buf, args),
b'm' => run_move(&spec, buf, args),
b't' => run_transfer(&spec, buf, args),
_ => Action::Error(format!("unknown command: {rest}")),
}
}
fn run_global(spec: &Spec, buf: &mut Buffer, args: &str, invert: bool) -> Action {
let args_bytes = args.as_bytes();
if args_bytes.is_empty() {
return Action::Error("no delimiter".to_string());
}
let delim = args_bytes[0];
let after_delim = &args_bytes[1..];
let (pattern, rest) = match scan_delimited(after_delim, delim) {
Some(r) => r,
None => return Action::Error("unterminated pattern".to_string()),
};
let cmd = std::str::from_utf8(rest).unwrap_or("p");
let cmd = cmd.trim_start();
let cmd = if cmd.is_empty() { "p" } else { cmd };
let re = bre::Regex::compile(&pattern);
let range = match spec.resolve_or_whole(buf) {
Ok(r) => r,
Err(e) => return Action::Error(e),
};
let mut marked = Vec::new();
for n in range.start..=range.end {
if let Some(line) = buf.line(n) {
let matched = re.find(line.as_bytes()).is_some();
if matched != invert {
marked.push(n);
}
}
}
if marked.is_empty() {
return Action::Error("no match".to_string());
}
if cmd == "d" {
for &n in marked.iter().rev() {
buf.delete_range(n, n);
}
return Action::Done;
}
let mut output = String::new();
for &n in &marked {
buf.set_current(n);
match dispatch(cmd, buf) {
Action::Print(msg) => {
if !output.is_empty() {
output.push('\n');
}
output.push_str(&msg);
}
Action::Error(msg) => {
if !output.is_empty() {
output.push('\n');
}
output.push_str(&format!("? {msg}"));
}
Action::Done => {}
_ => {}
}
}
if output.is_empty() {
Action::Done
} else {
Action::Print(output)
}
}
fn run_help() -> Action {
Action::Print(
"\
ved commands (addresses shown in brackets are optional):
[.]a, append Append text after the addressed line. End with '.'
[.]i, insert Insert text before the addressed line. End with '.'
[.,.]c, change Replace the addressed lines with new text. End with '.'
[.,.]d, delete Delete the addressed lines
[.,.]p, print Print the addressed lines
[.,.]n, number Print with line numbers
[.,.]l, list Print with non-printing chars as \\NNN octal, ending $
[.,.+1]j, join Join the addressed lines into one (default: . and next)
[.,.]m DEST Move the addressed lines to after DEST (0 = top)
[.,.]t DEST Copy the addressed lines to after DEST (0 = top)
[.,.]s/re/new/[g] Substitute: replace regex match in addressed lines
[.,.]s Repeat the last substitute (pattern, replacement, flags)
[.,.]g/re/cmd Global: run cmd on lines matching regex
[.,.]v/re/cmd Inverse global: run cmd on lines NOT matching regex
[.,.]w [file] Write addressed lines (default: all) to file
q, quit Quit (warns if buffer modified, repeat to force)
H, help Show this help
Addresses: 1 (line 1), $ (last), . (current), +N/-N (relative), , (all), ; (current to end)
Regex: . (any char), * (zero or more), ^ $ (anchors), [abc] [^abc] [a-z] (classes)
\\(...\\) (capture group), \\1-\\9 (backreference)
Replacement: & (whole match), \\1-\\9 (group), \\& (literal &)"
.to_string(),
)
}
fn run_delete(spec: &Spec, buf: &mut Buffer) -> Action {
let range = match spec.resolve(buf) {
Ok(r) => r,
Err(e) => return Action::Error(e),
};
let count = range.end - range.start + 1;
buf.delete_range(range.start, range.end);
if count == 1 {
Action::Print(format!("deleted line {}", range.start))
} else {
Action::Print(format!(
"deleted {count} lines ({}-{})",
range.start, range.end
))
}
}
fn run_join(spec: &Spec, buf: &mut Buffer) -> Action {
let range = match spec.resolve_or_pair_with_next(buf) {
Ok(r) => r,
Err(e) => return Action::Error(e),
};
if range.start == range.end {
buf.set_current(range.start);
return Action::Print(String::new());
}
let mut joined = String::new();
for n in range.start..=range.end {
if let Some(line) = buf.line(n) {
joined.push_str(line);
}
}
buf.replace_line(range.start, joined);
buf.delete_range(range.start + 1, range.end);
buf.set_current(range.start);
let count = range.end - range.start + 1;
Action::Print(format!(
"joined {count} lines ({}-{}) into line {}",
range.start, range.end, range.start
))
}
fn run_move(spec: &Spec, buf: &mut Buffer, dest_arg: &str) -> Action {
let range = match spec.resolve(buf) {
Ok(r) => r,
Err(e) => return Action::Error(e),
};
let dest = match address::parse_dest(dest_arg, buf) {
Ok((d, _)) => d,
Err(e) => return Action::Error(e),
};
if dest >= range.start && dest <= range.end {
return Action::Error("invalid destination".to_string());
}
let lines: Vec<String> = (range.start..=range.end)
.filter_map(|n| buf.line(n).map(str::to_string))
.collect();
let count = lines.len();
buf.delete_range(range.start, range.end);
let adjusted = if dest > range.end { dest - count } else { dest };
let mut at = adjusted;
for line in lines {
buf.append_after(at, line);
at += 1;
}
buf.set_current(adjusted + count);
Action::Print(format!(
"moved {count} lines ({}-{}) to after {dest}",
range.start, range.end
))
}
fn run_transfer(spec: &Spec, buf: &mut Buffer, dest_arg: &str) -> Action {
let range = match spec.resolve(buf) {
Ok(r) => r,
Err(e) => return Action::Error(e),
};
let dest = match address::parse_dest(dest_arg, buf) {
Ok((d, _)) => d,
Err(e) => return Action::Error(e),
};
let lines: Vec<String> = (range.start..=range.end)
.filter_map(|n| buf.line(n).map(str::to_string))
.collect();
let count = lines.len();
let mut at = dest;
for line in lines {
buf.append_after(at, line);
at += 1;
}
buf.set_current(dest + count);
Action::Print(format!(
"copied {count} lines ({}-{}) to after {dest}",
range.start, range.end
))
}
enum PrintMode {
Plain,
Numbered,
List,
}
fn run_print(spec: &Spec, buf: &mut Buffer, mode: PrintMode) -> Action {
let range = match spec.resolve(buf) {
Ok(r) => r,
Err(e) => return Action::Error(e),
};
let mut output = String::new();
for n in range.start..=range.end {
if n > range.start {
output.push('\n');
}
if matches!(mode, PrintMode::Numbered) {
output.push_str(&format!("{n}\t"));
}
if let Some(line) = buf.line(n) {
if matches!(mode, PrintMode::List) {
output.push_str(&render_list_line(line));
} else {
output.push_str(line);
}
}
}
buf.set_current(range.end);
Action::Print(output)
}
fn render_list_line(line: &str) -> String {
let mut out = String::new();
for b in line.bytes() {
match b {
b'\\' => out.push_str("\\\\"),
0x20..=0x7E => out.push(b as char),
_ => out.push_str(&format!("\\{b:03o}")),
}
}
out.push('$');
out
}
enum LoadOutcome {
Loaded(usize),
NewFile,
}
fn load_file(filename: &str, buf: &mut Buffer) -> Result<LoadOutcome, String> {
let detected = enc::warn_if_non_utf8(filename);
let content = match std::fs::read_to_string(filename) {
Ok(c) => c,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => {
buf.set_filename(filename.to_string());
buf.mark_saved();
return Ok(LoadOutcome::NewFile);
}
Err(e) => return Err(format!("cannot open {filename}: {e}")),
};
let bytes = content.len();
for line in content.lines() {
buf.append_after(buf.len(), line.to_string());
}
buf.set_filename(filename.to_string());
buf.mark_saved();
if let Some(enc) = detected {
buf.mark_non_utf8_source(enc, filename.to_string());
}
Ok(LoadOutcome::Loaded(bytes))
}
fn run_edit(buf: &mut Buffer, args: &str) -> Action {
let filename_arg = args.trim_start();
let filename = if filename_arg.is_empty() {
match buf.filename() {
Some(f) => f.to_string(),
None => return Action::Error("no current filename".to_string()),
}
} else {
filename_arg.to_string()
};
*buf = Buffer::new();
match load_file(&filename, buf) {
Ok(LoadOutcome::Loaded(bytes)) => Action::Print(format!("{bytes}")),
Ok(LoadOutcome::NewFile) => Action::Print(format!("{filename}: new file")),
Err(e) => Action::Error(e),
}
}
fn run_read(spec: &Spec, buf: &mut Buffer, args: &str) -> Action {
let filename_arg = args.trim_start();
let filename = if filename_arg.is_empty() {
match buf.filename() {
Some(f) => f.to_string(),
None => return Action::Error("no current filename".to_string()),
}
} else {
filename_arg.to_string()
};
let after = if buf.is_empty() {
0
} else if spec.is_empty() {
buf.len()
} else {
match spec.resolve(buf) {
Ok(r) => r.end,
Err(e) => return Action::Error(e),
}
};
enc::warn_if_non_utf8(&filename);
let content = match std::fs::read_to_string(&filename) {
Ok(c) => c,
Err(e) => return Action::Error(format!("cannot open {filename}: {e}")),
};
let bytes = content.len();
let mut insert_at = after;
for line in content.lines() {
buf.append_after(insert_at, line.to_string());
insert_at += 1;
}
if buf.filename().is_none() {
buf.set_filename(filename);
}
Action::Print(format!("{bytes}"))
}
fn run_substitute(spec: &Spec, buf: &mut Buffer, args: &str) -> Action {
let args = args.as_bytes();
let (pattern, replacement, global) = if args.is_empty() {
match buf.last_subst() {
Some((p, r, g)) => (p.clone(), r.clone(), *g),
None => return Action::Error("no previous substitute".to_string()),
}
} else {
let delim = args[0];
let rest = &args[1..];
let (pattern, rest) = match scan_delimited(rest, delim) {
Some(r) => r,
None => return Action::Error("unterminated pattern".to_string()),
};
let (replacement, rest) = match scan_delimited(rest, delim) {
Some(r) => r,
None => return Action::Error("unterminated replacement".to_string()),
};
let global = rest.contains(&b'g');
buf.set_last_subst(pattern.clone(), replacement.clone(), global);
(pattern, replacement, global)
};
let re = bre::Regex::compile(&pattern);
let range = match spec.resolve(buf) {
Ok(r) => r,
Err(e) => return Action::Error(e),
};
let mut last_modified_line = None;
for n in range.start..=range.end {
let line = match buf.line(n) {
Some(l) => l.to_string(),
None => continue,
};
let line_bytes = line.as_bytes();
let new_line = if global {
substitute_all(&re, line_bytes, &replacement)
} else {
substitute_first(&re, line_bytes, &replacement)
};
if let Some(new_bytes) = new_line {
let new_str = String::from_utf8_lossy(&new_bytes).into_owned();
buf.replace_line(n, new_str);
last_modified_line = Some(n);
}
}
match last_modified_line {
Some(n) => {
buf.set_current(n);
match buf.line(n) {
Some(s) => Action::Print(s.to_string()),
None => Action::Error("invalid address".to_string()),
}
}
None => Action::Error("no match".to_string()),
}
}
fn scan_delimited(input: &[u8], delim: u8) -> Option<(Vec<u8>, &[u8])> {
let mut result = Vec::new();
let mut i = 0;
while i < input.len() {
if input[i] == delim {
return Some((result, &input[i + 1..]));
}
if input[i] == b'\\' && i + 1 < input.len() {
if input[i + 1] == delim {
result.push(delim);
i += 2;
} else {
result.push(input[i]);
result.push(input[i + 1]);
i += 2;
}
} else {
result.push(input[i]);
i += 1;
}
}
None }
fn substitute_first(
re: &bre::Regex,
text: &[u8],
replacement: &[u8],
) -> Option<Vec<u8>> {
let m = re.find(text)?;
let mut result = Vec::new();
result.extend_from_slice(&text[..m.start]);
result.extend_from_slice(&bre::expand_replacement(replacement, &m, text));
result.extend_from_slice(&text[m.end..]);
Some(result)
}
fn substitute_all(
re: &bre::Regex,
text: &[u8],
replacement: &[u8],
) -> Option<Vec<u8>> {
let mut result = Vec::new();
let mut pos = 0;
let mut matched = false;
while pos <= text.len() {
match re.find(&text[pos..]) {
Some(m) => {
matched = true;
let abs_start = pos + m.start;
let abs_end = pos + m.end;
result.extend_from_slice(&text[pos..abs_start]);
result.extend_from_slice(
&bre::expand_replacement(replacement, &m, &text[pos..]),
);
if abs_end == abs_start {
if pos < text.len() {
result.push(text[pos]);
}
pos = abs_start + 1;
} else {
pos = abs_end;
}
}
None => {
result.extend_from_slice(&text[pos..]);
break;
}
}
}
if matched {
Some(result)
} else {
None
}
}
fn run_write(spec: &Spec, buf: &mut Buffer, args: &str) -> Action {
let filename_arg = args.trim_start();
let filename: String = if filename_arg.is_empty() {
match buf.filename() {
Some(f) => f.to_string(),
None => return Action::Error("no current filename".to_string()),
}
} else {
filename_arg.to_string()
};
if let Some(enc) = buf.non_utf8_block(&filename) {
return Action::Error(format!(
"refusing to overwrite {filename} (loaded as {enc}); convert with iconv first, or use `w <newname>` to write elsewhere"
));
}
let (content, wrote_whole) = if buf.is_empty() && spec.is_empty() {
(String::new(), true)
} else {
let range = match spec.resolve_or_whole(buf) {
Ok(r) => r,
Err(e) => return Action::Error(e),
};
let mut s = String::new();
for n in range.start..=range.end {
if let Some(line) = buf.line(n) {
s.push_str(line);
s.push('\n');
}
}
let whole = range.start == 1 && range.end == buf.len();
(s, whole)
};
let bytes = content.len();
if let Err(e) = std::fs::write(&filename, &content) {
return Action::Error(format!("cannot write {}: {}", filename, e));
}
let msg = format!("wrote {bytes} bytes to {filename}");
buf.set_filename(filename);
if wrote_whole {
buf.mark_saved();
}
Action::Print(msg)
}
#[cfg(test)]
mod tests {
use super::*;
fn temp_path(tag: &str) -> std::path::PathBuf {
let nanos = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos();
let mut p = std::env::temp_dir();
p.push(format!("ved_test_{tag}_{}_{nanos}", std::process::id()));
p
}
#[test]
fn missing_file_loads_as_new_file() {
let path = temp_path("newfile");
assert!(!path.exists());
let mut buf = Buffer::new();
let outcome = load_file(path.to_str().unwrap(), &mut buf);
assert!(matches!(outcome, Ok(LoadOutcome::NewFile)));
assert_eq!(buf.filename(), path.to_str());
assert!(buf.is_empty());
assert!(!buf.is_modified());
assert!(!path.exists(), "opening a new file must not create it");
}
#[test]
fn directory_is_still_an_error() {
let path = temp_path("dir");
std::fs::create_dir(&path).unwrap();
let mut buf = Buffer::new();
let outcome = load_file(path.to_str().unwrap(), &mut buf);
std::fs::remove_dir(&path).unwrap();
assert!(outcome.is_err());
assert!(buf.filename().is_none());
}
#[test]
fn list_printable_ascii_passes_through() {
assert_eq!(render_list_line("hello world"), "hello world$");
}
#[test]
fn list_empty_line_is_just_dollar() {
assert_eq!(render_list_line(""), "$");
}
#[test]
fn list_tab_becomes_octal() {
assert_eq!(render_list_line("a\tb"), "a\\011b$");
}
#[test]
fn list_ascii_separators_become_octal() {
let s = "A\u{1C}B\u{1D}C\u{1E}D\u{1F}E";
assert_eq!(render_list_line(s), "A\\034B\\035C\\036D\\037E$");
}
#[test]
fn list_backslash_escapes_to_double_backslash() {
assert_eq!(render_list_line("a\\b"), "a\\\\b$");
}
#[test]
fn list_utf8_multibyte_renders_byte_by_byte() {
assert_eq!(render_list_line("ü"), "\\303\\274$");
}
#[test]
fn list_high_bit_byte_pads_to_three_digits() {
let s = "\u{7F}";
assert_eq!(render_list_line(s), "\\177$");
}
fn buf_with(lines: &[&str]) -> Buffer {
let mut b = Buffer::new();
for (i, l) in lines.iter().enumerate() {
b.append_after(i, (*l).to_string());
}
b
}
#[test]
fn join_default_joins_current_and_next() {
let mut buf = buf_with(&["foo", "bar", "baz"]);
buf.set_current(1);
let act = dispatch("j", &mut buf);
assert!(matches!(act, Action::Print(_)));
assert_eq!(buf.len(), 2);
assert_eq!(buf.line(1), Some("foobar"));
assert_eq!(buf.line(2), Some("baz"));
assert_eq!(buf.current(), 1);
}
#[test]
fn join_range_concatenates_all_lines() {
let mut buf = buf_with(&["a", "b", "c", "d", "e"]);
let act = dispatch("2,4j", &mut buf);
assert!(matches!(act, Action::Print(_)));
assert_eq!(buf.len(), 3);
assert_eq!(buf.line(2), Some("bcd"));
assert_eq!(buf.line(3), Some("e"));
assert_eq!(buf.current(), 2);
}
#[test]
fn join_single_line_is_silent_noop() {
let mut buf = buf_with(&["one", "two", "three"]);
let act = dispatch("2j", &mut buf);
if let Action::Print(s) = act {
assert!(s.is_empty());
} else {
panic!("expected silent Print on single-line join");
}
assert_eq!(buf.len(), 3);
assert_eq!(buf.line(2), Some("two"));
assert_eq!(buf.current(), 2);
}
#[test]
fn join_does_not_insert_separators() {
let mut buf = buf_with(&["hello", "world"]);
let _ = dispatch("1,2j", &mut buf);
assert_eq!(buf.line(1), Some("helloworld"));
}
#[test]
fn join_at_last_line_errors() {
let mut buf = buf_with(&["only", "lines", "here"]);
buf.set_current(3);
let act = dispatch("j", &mut buf);
assert!(matches!(act, Action::Error(_)));
}
#[test]
fn join_long_form_alias_works() {
let mut buf = buf_with(&["a", "b", "c"]);
let _ = dispatch("1,3join", &mut buf);
assert_eq!(buf.len(), 1);
assert_eq!(buf.line(1), Some("abc"));
}
#[test]
fn join_marks_buffer_modified() {
let mut buf = buf_with(&["x", "y"]);
buf.mark_saved();
assert!(!buf.is_modified());
let _ = dispatch("1,2j", &mut buf);
assert!(buf.is_modified());
}
#[test]
fn move_range_to_after_later_line() {
let mut buf = buf_with(&["a", "b", "c", "d", "e"]);
let _ = dispatch("2,3m5", &mut buf);
assert_eq!(buf.line(1), Some("a"));
assert_eq!(buf.line(2), Some("d"));
assert_eq!(buf.line(3), Some("e"));
assert_eq!(buf.line(4), Some("b"));
assert_eq!(buf.line(5), Some("c"));
assert_eq!(buf.current(), 5);
}
#[test]
fn move_range_to_after_earlier_line() {
let mut buf = buf_with(&["a", "b", "c", "d", "e"]);
let _ = dispatch("4,5m1", &mut buf);
assert_eq!(buf.line(1), Some("a"));
assert_eq!(buf.line(2), Some("d"));
assert_eq!(buf.line(3), Some("e"));
assert_eq!(buf.line(4), Some("b"));
assert_eq!(buf.line(5), Some("c"));
}
#[test]
fn move_to_top_with_dest_zero() {
let mut buf = buf_with(&["a", "b", "c"]);
let _ = dispatch("3m0", &mut buf);
assert_eq!(buf.line(1), Some("c"));
assert_eq!(buf.line(2), Some("a"));
assert_eq!(buf.line(3), Some("b"));
assert_eq!(buf.current(), 1);
}
#[test]
fn move_to_end_with_dollar() {
let mut buf = buf_with(&["a", "b", "c", "d"]);
let _ = dispatch("1m$", &mut buf);
assert_eq!(buf.line(1), Some("b"));
assert_eq!(buf.line(2), Some("c"));
assert_eq!(buf.line(3), Some("d"));
assert_eq!(buf.line(4), Some("a"));
}
#[test]
fn move_into_self_errors() {
let mut buf = buf_with(&["a", "b", "c", "d", "e"]);
let act = dispatch("2,4m3", &mut buf);
assert!(matches!(act, Action::Error(_)));
assert_eq!(buf.line(2), Some("b"));
assert_eq!(buf.line(3), Some("c"));
assert_eq!(buf.line(4), Some("d"));
}
#[test]
fn move_invalid_dest_errors() {
let mut buf = buf_with(&["a", "b"]);
let act = dispatch("1m99", &mut buf);
assert!(matches!(act, Action::Error(_)));
}
#[test]
fn transfer_copies_range_after_dest() {
let mut buf = buf_with(&["a", "b", "c"]);
let _ = dispatch("1,2t3", &mut buf);
assert_eq!(buf.len(), 5);
assert_eq!(buf.line(4), Some("a"));
assert_eq!(buf.line(5), Some("b"));
assert_eq!(buf.current(), 5);
}
#[test]
fn transfer_to_top_with_dest_zero() {
let mut buf = buf_with(&["x", "y"]);
let _ = dispatch("2t0", &mut buf);
assert_eq!(buf.line(1), Some("y"));
assert_eq!(buf.line(2), Some("x"));
assert_eq!(buf.line(3), Some("y"));
assert_eq!(buf.current(), 1);
}
#[test]
fn transfer_within_self_is_allowed() {
let mut buf = buf_with(&["a", "b", "c", "d"]);
let act = dispatch("1,3t2", &mut buf);
assert!(matches!(act, Action::Print(_)));
assert_eq!(buf.len(), 7);
}
#[test]
fn move_and_transfer_mark_buffer_modified() {
let mut buf = buf_with(&["a", "b", "c"]);
buf.mark_saved();
let _ = dispatch("1t3", &mut buf);
assert!(buf.is_modified());
let mut buf = buf_with(&["a", "b", "c"]);
buf.mark_saved();
let _ = dispatch("1m3", &mut buf);
assert!(buf.is_modified());
}
#[test]
fn bare_s_with_no_prior_substitute_errors() {
let mut buf = buf_with(&["hello world"]);
let act = dispatch("s", &mut buf);
assert!(matches!(act, Action::Error(_)));
}
#[test]
fn bare_s_repeats_last_substitute() {
let mut buf = buf_with(&["foo", "foo", "foo"]);
let _ = dispatch("1s/foo/bar/", &mut buf);
assert_eq!(buf.line(1), Some("bar"));
let _ = dispatch("2s", &mut buf);
assert_eq!(buf.line(2), Some("bar"));
let _ = dispatch("3s", &mut buf);
assert_eq!(buf.line(3), Some("bar"));
}
#[test]
fn bare_s_preserves_global_flag() {
let mut buf = buf_with(&["xxx xxx xxx"]);
let _ = dispatch("s/x/Y/g", &mut buf);
assert_eq!(buf.line(1), Some("YYY YYY YYY"));
buf.replace_line(1, "xxx xxx xxx".to_string());
let _ = dispatch("s", &mut buf);
assert_eq!(buf.line(1), Some("YYY YYY YYY"));
}
#[test]
fn bare_s_without_global_flag_stays_non_global() {
let mut buf = buf_with(&["aaa aaa"]);
let _ = dispatch("s/a/Z/", &mut buf);
assert_eq!(buf.line(1), Some("Zaa aaa"));
let _ = dispatch("s", &mut buf);
assert_eq!(buf.line(1), Some("ZZa aaa"));
}
#[test]
fn explicit_s_updates_the_remembered_state() {
let mut buf = buf_with(&["one two three"]);
let _ = dispatch("s/one/ONE/", &mut buf);
assert_eq!(buf.line(1), Some("ONE two three"));
let _ = dispatch("s/two/TWO/", &mut buf);
assert_eq!(buf.line(1), Some("ONE TWO three"));
buf.replace_line(1, "one two three".to_string());
let _ = dispatch("s", &mut buf);
assert_eq!(buf.line(1), Some("one TWO three"));
}
}