@@ -135,7 +135,7 @@ pub struct Command {
137macro_rules! commands {
137macro_rules! commands {
138− ( $($name:ident, $doc:literal),* ) => {
138+ ( $($name:ident, $doc:literal,)* ) => {
140 #[allow(non_upper_case_globals)]
140 #[allow(non_upper_case_globals)]
141 pub const $name: Self = Self {
141 pub const $name: Self = Self {
@@ -317,7 +317,12 @@ impl Command {
317 dap_variables, "List variables",
317 dap_variables, "List variables",
318 dap_terminate, "End debug session",
318 dap_terminate, "End debug session",
319 dap_switch_thread, "Switch current thread",
319 dap_switch_thread, "Switch current thread",
320+ shell_pipe, "Pipe selections through shell command",
321+ shell_pipe_to, "Pipe selections into shell command, ignoring command output",
322+ shell_insert_output, "Insert output of shell command before each selection",
323+ shell_append_output, "Append output of shell command after each selection",
324+ shell_keep_pipe, "Filter selections with shell predicate",
320− suspend, "Suspend"
325+ suspend, "Suspend",
@@ -1089,7 +1094,7 @@ fn select_all(cx: &mut Context) {
1091fn select_regex(cx: &mut Context) {
1096fn select_regex(cx: &mut Context) {
1092− let prompt = ui::regex_prompt(cx, "select:".to_string(), move |view, doc, _, regex| {
1097+ let prompt = ui::regex_prompt(cx, "select:".into(), move |view, doc, _, regex| {
1093 let text = doc.text().slice(..);
1098 let text = doc.text().slice(..);
1094 if let Some(selection) = selection::select_on_matches(text, doc.selection(view.id), ®ex)
1099 if let Some(selection) = selection::select_on_matches(text, doc.selection(view.id), ®ex)
@@ -1101,7 +1106,7 @@ fn select_regex(cx: &mut Context) {
1103fn split_selection(cx: &mut Context) {
1108fn split_selection(cx: &mut Context) {
1104− let prompt = ui::regex_prompt(cx, "split:".to_string(), move |view, doc, _, regex| {
1109+ let prompt = ui::regex_prompt(cx, "split:".into(), move |view, doc, _, regex| {
1105 let text = doc.text().slice(..);
1110 let text = doc.text().slice(..);
1106 let selection = selection::split_on_matches(text, doc.selection(view.id), ®ex);
1111 let selection = selection::split_on_matches(text, doc.selection(view.id), ®ex);
1107 doc.set_selection(view.id, selection);
1112 doc.set_selection(view.id, selection);
@@ -1167,15 +1172,11 @@ fn search(cx: &mut Context) {
1167 // feed chunks into the regex yet
1172 // feed chunks into the regex yet
1168 let contents = doc.text().slice(..).to_string();
1173 let contents = doc.text().slice(..).to_string();
1170− let prompt = ui::regex_prompt(
1172− "search:".to_string(),
1173− move |view, doc, registers, regex| {
1175+ let prompt = ui::regex_prompt(cx, "search:".into(), move |view, doc, registers, regex| {
1174− search_impl(doc, view, &contents, ®ex, false);
1176+ search_impl(doc, view, &contents, ®ex, false);
1175− // TODO: only store on enter (accept), not update
1177+ // TODO: only store on enter (accept), not update
1176− registers.write('/', vec![regex.as_str().to_string()]);
1178+ registers.write('/', vec![regex.as_str().to_string()]);
1180 cx.push_layer(Box::new(prompt));
1181 cx.push_layer(Box::new(prompt));
@@ -2400,7 +2401,7 @@ mod cmd {
2401fn command_mode(cx: &mut Context) {
2402fn command_mode(cx: &mut Context) {
2402 let mut prompt = Prompt::new(
2403 let mut prompt = Prompt::new(
2403− ":".to_owned(),
2404+ ":".into(),
2404 Some(':'),
2405 Some(':'),
2405 |input: &str| {
2406 |input: &str| {
2406 // we use .this over split_whitespace() because we care about empty segments
2407 // we use .this over split_whitespace() because we care about empty segments
@@ -4009,7 +4010,7 @@ fn join_selections(cx: &mut Context) {
4010fn keep_selections(cx: &mut Context) {
4011fn keep_selections(cx: &mut Context) {
4011 // keep selections matching regex
4012 // keep selections matching regex
4012− let prompt = ui::regex_prompt(cx, "keep:".to_string(), move |view, doc, _, regex| {
4013+ let prompt = ui::regex_prompt(cx, "keep:".into(), move |view, doc, _, regex| {
4013 let text = doc.text().slice(..);
4014 let text = doc.text().slice(..);
4015 if let Some(selection) = selection::keep_matches(text, doc.selection(view.id), ®ex) {
4016 if let Some(selection) = selection::keep_matches(text, doc.selection(view.id), ®ex) {
@@ -4487,6 +4488,128 @@ fn surround_delete(cx: &mut Context) {
4491+#[derive(Eq, PartialEq)]
4492+enum ShellBehavior {
4500+fn shell_pipe(cx: &mut Context) {
4501+ shell(cx, "pipe:".into(), ShellBehavior::Replace);
4504+fn shell_pipe_to(cx: &mut Context) {
4505+ shell(cx, "pipe-to:".into(), ShellBehavior::Ignore);
4508+fn shell_insert_output(cx: &mut Context) {
4509+ shell(cx, "insert-output:".into(), ShellBehavior::Insert);
4512+fn shell_append_output(cx: &mut Context) {
4513+ shell(cx, "append-output:".into(), ShellBehavior::Append);
4516+fn shell_keep_pipe(cx: &mut Context) {
4517+ shell(cx, "keep-pipe:".into(), ShellBehavior::Filter);
4520+fn shell(cx: &mut Context, prompt: Cow<'static, str>, behavior: ShellBehavior) {
4521+ use std::io::Write;
4522+ use std::process::{Command, Stdio};
4523+ if cx.editor.config.shell.is_empty() {
4524+ cx.editor.set_error("No shell set".to_owned());
4527+ let pipe = match behavior {
4528+ ShellBehavior::Replace | ShellBehavior::Ignore | ShellBehavior::Filter => true,
4529+ ShellBehavior::Insert | ShellBehavior::Append => false,
4531+ let prompt = Prompt::new(
4534+ |_input: &str| Vec::new(),
4535+ move |cx: &mut compositor::Context, input: &str, event: PromptEvent| {
4536+ if event != PromptEvent::Validate {
4539+ let shell = &cx.editor.config.shell;
4540+ let (view, doc) = current!(cx.editor);
4541+ let selection = doc.selection(view.id);
4543+ let mut changes = Vec::with_capacity(selection.len());
4545+ for range in selection.ranges() {
4546+ let mut process = match Command::new(&shell[0])
4549+ .stdin(Stdio::piped())
4550+ .stdout(Stdio::piped())
4551+ .stderr(Stdio::piped())
4554+ Ok(process) => process,
4556+ log::error!("Failed to start shell: {}", e);
4557+ cx.editor.set_error("Failed to start shell".to_owned());
4562+ let stdin = process.stdin.as_mut().unwrap();
4563+ let fragment = range.fragment(doc.text().slice(..));
4564+ stdin.write_all(fragment.as_bytes()).unwrap();
4566+ let output = process.wait_with_output().unwrap();
4568+ if behavior != ShellBehavior::Filter {
4569+ if !output.status.success() {
4570+ if !output.stderr.is_empty() {
4571+ log::error!("Shell error: {}", String::from_utf8_lossy(&output.stderr));
4573+ cx.editor.set_error("Command failed".to_owned());
4576+ let tendril = match Tendril::try_from_byte_slice(&output.stdout) {
4577+ Ok(tendril) => tendril,
4580+ .set_error("Process did not output valid UTF-8".to_owned());
4584+ let (from, to) = match behavior {
4585+ ShellBehavior::Replace => (range.from(), range.to()),
4586+ ShellBehavior::Insert => (range.from(), range.from()),
4587+ ShellBehavior::Append => (range.to(), range.to()),
4588+ _ => (range.from(), range.from()),
4590+ changes.push((from, to, Some(tendril)));
4592+ // if the process exits successfully, keep the selection, otherwise delete it.
4593+ let keep = output.status.success();
4596+ if keep { range.from() } else { range.to() },
4602+ if behavior != ShellBehavior::Ignore {
4603+ let transaction = Transaction::change(doc.text(), changes.into_iter());
4604+ doc.apply(&transaction, view.id);
4605+ doc.append_changes_to_history(view.id);
4610+ cx.push_layer(Box::new(prompt));
4490fn suspend(_cx: &mut Context) {
4613fn suspend(_cx: &mut Context) {
4491 #[cfg(not(windows))]
4614 #[cfg(not(windows))]
4492 signal_hook::low_level::raise(signal_hook::consts::signal::SIGTSTP).unwrap();
4615 signal_hook::low_level::raise(signal_hook::consts::signal::SIGTSTP).unwrap();