fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
dad58ac20
master
1# localization: tier1 2function edit_command_buffer --description'Edit the command buffer in an external editor' 3set -l tmpdir( __fish_mktemp_relative -d fish) 4or return 1 5set -l f$tmpdir /command-line.fish 6command touch$f 7or return 1 8 9set -l editor( __fish_anyeditor ) 10or return 1 11 12set -l indented_lines( commandline -b| fish_indent --only-indent) 13string join --\n $indented_lines > $f 14set -l offset( commandline --cursor) 15# compute cursor line/column 16set -l lines( commandline ) \n 17set -l line1 18while test $offset -ge ( string length --$lines [ 1 ]) 19set offset( math $offset -( string length --$lines [ 1 ])) 20set line( math $line +1 ) 21set -e lines[ 1 ] 22end 23set -l indent1 +( string length --$indented_lines [ $line ]) -( string length --$lines [ 1 ]) 24set -l col( math $offset +1 +$indent ) 25 26set -l editor_basename( string match -r'[^/]+$' --$editor [ 1 ]) 27set -l wrapped_commands 28for wrap_targetin ( complete --$editor_basename | string replace -rf'^complete [^/]+ --wraps (.+)$' '$1' ) 29set -l tmp 30string unescape --$wrap_target | read -at tmp 31set -a wrapped_commands$tmp [ 1 ] 32end 33set -l found false 34set -l cursor_from_editor 35for editor_commandin $editor_basename $wrapped_commands 36switch $editor_command 37case vi vim nvim 38if test $editor_command = vi&& not set -l vi_version"$ ( vi --version 2> /dev/null ) " 39if printf %s$vi_version | grep -q BusyBox 40break 41end 42set -a editor +{ $line } $f 43set found true 44break 45end 46set cursor_from_editor( __fish_mktemp_relative fish-edit_command_buffer) 47or return 48set -a editor +$line "+norm! $col |" $f \ 49'+au VimLeave * ++once call writefile([printf("%s %s %s", shellescape(bufname()), line("."), col("."))], "' $cursor_from_editor '")' 50case emacs emacsclient gedit 51set -a editor +$line :$col $f 52case kak 53set cursor_from_editor( __fish_mktemp_relative fish-edit_command_buffer) 54or return 55set -a editor +$line :$col $f -e" 56hook -always -once global ClientClose %val{client} %{ 57echo -to-file $cursor_from_editor -quoting shell \ 58%val{buffile} %val{cursor_line} %val{cursor_column} 59} 60" 61case nano 62set -a editor +$line ,$col $f 63case joe ee 64set -a editor +$line $f 65case code code-oss 66set -a editor --goto$f :$line :$col --wait 67case subl 68set -a editor$f :$line :$col --wait 69case micro 70set -a editor$f +$line :$col 71case helix hx 72set -a editor$f :$line :$col 73case '*' 74continue 75end 76set found true 77break 78end 79if not $found 80set -a editor$f 81end 82 83$editor 84 85set -l raw_lines( command cat$f ) 86set -l unindented_lines( string join --\n $raw_lines | fish_indent --only-unindent) 87 88# Here we're checking the exit status of the editor. 89if test $status -eq 0 -a -s $f 90# Set the command to the output of the edited command and move the cursor to the 91# end of the edited command. 92commandline -r --$unindented_lines 93commandline -C999999 94else 95echo 96echo ( _ "Ignoring the output of your editor since its exit status was non-zero" ) 97echo ( _ "or the file was empty" ) 98end 99if set -q cursor_from_editor[ 1 ] 100eval set -l pos"$ ( command cat $cursor_from_editor ) " 101if set -q pos[ 1 ] && test $pos [ 1 ] = $f 102set -l line$pos [ 2 ] 103set -l indent( math ( string length --" $raw_lines [ $line ] " ) -( string length --" $unindented_lines [ $line ] " )) 104set -l column( math $pos [ 3 ] -$indent ) 105if not commandline --line$line 2> /dev/null 106commandline -f end-of-buffer 107else 108commandline --column$column 2> /dev/null|| commandline -f end-of-line 109end 110end 111command rm$cursor_from_editor 112end 113command rm -r( path dirname$f ) 114# We've probably opened something that messed with the screen. 115# A repaint seems in order. 116commandline -f repaint 117end