From 1bfafbc13437f4aa227bafc7962878aa8034ef73 Mon Sep 17 00:00:00 2001 From: Vincent Ollivier Date: Tue, 17 Sep 2024 13:55:03 +0200 Subject: [PATCH] Add support for g flag --- src/usr/edit.rs | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/usr/edit.rs b/src/usr/edit.rs index 94c36cfe..d8876e96 100644 --- a/src/usr/edit.rs +++ b/src/usr/edit.rs @@ -610,25 +610,33 @@ impl Editor { pub fn exec(&mut self) { if let Some(query) = prompt(&mut self.command_prompt, ":") { - if !query.is_empty() { - let params: Vec<&str> = query.split('/').collect(); - match params[0] { - "s" if params.len() == 4 => { // Replace current line - let re = Regex::new(params[1]); - let s = params[2]; - let y = self.offset.y + self.cursor.y; + let params: Vec<&str> = query.split('/').collect(); + match params[0] { + "s" if params.len() == 4 => { // Replace current line + let re = Regex::new(params[1]); + let s = params[2]; + let y = self.offset.y + self.cursor.y; + if params[3] == "g" { // Replace all occurrences + self.lines[y] = re.replace_all(&self.lines[y], s); + } else { self.lines[y] = re.replace(&self.lines[y], s); } - "%s" if params.len() == 4 => { // Replace all lines - let re = Regex::new(params[1]); - let s = params[2]; - let n = self.lines.len(); - for y in 0..n { + } + "%s" if params.len() == 4 => { // Replace all lines + let re = Regex::new(params[1]); + let s = params[2]; + let n = self.lines.len(); + for y in 0..n { + if params[3] == "g" { // Replace all occurrences + self.lines[y] = re.replace_all(&self.lines[y], s); + } else { self.lines[y] = re.replace(&self.lines[y], s); } } - _ => {} } + _ => {} + } + if !query.is_empty() { self.command_prompt.history.add(&query); self.command_prompt.history.save(&self.command_history); }