From 1dd8a113f2fade1b9ba7f410d9b647b0f53b26d9 Mon Sep 17 00:00:00 2001 From: Charles Maher <46153209+charlesmaher@users.noreply.github.com> Date: Fri, 7 Oct 2022 06:27:28 +1100 Subject: [PATCH] Add feature to fish_commandline_prepend and fix minor issue (#9261) * Prepending will now respect leading spaces instead of doubling it up. * Removing a prefix no longer sends the cursor to the end of the line. --- share/functions/fish_commandline_prepend.fish | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/share/functions/fish_commandline_prepend.fish b/share/functions/fish_commandline_prepend.fish index 478be01cc..5fe561d5a 100644 --- a/share/functions/fish_commandline_prepend.fish +++ b/share/functions/fish_commandline_prepend.fish @@ -3,14 +3,24 @@ function fish_commandline_prepend --description "Prepend the given string to the commandline -r $history[1] end - set -l escaped (string escape --style=regex -- $argv[1]) set -l process (commandline -p | string collect) - if set process (string replace -r -- "^$escaped " "" $process) - commandline --replace -p -- $process - else - set -l cursor (commandline -C) - commandline -C 0 -p - commandline -i -- "$argv[1] " - commandline -C (math $cursor + (string length -- "$argv[1] ")) + + set -l to_prepend "$argv[1] " + if string match -qr '^ ' "$process" + set to_prepend " $argv[1]" end + + set -l length_diff (string length -- "$to_prepend") + set -l cursor_location (commandline -pC) + + set -l escaped (string escape --style=regex -- $to_prepend) + if set process (string replace -r -- "^$escaped" "" $process) + commandline --replace -p -- $process + set length_diff "-$length_diff" + else + commandline -pC 0 + commandline -pi -- "$to_prepend" + end + + commandline -pC (math "max 0,($cursor_location + $length_diff)") end