fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
52998635f
master
1# localization: tier1 2function funced --description'Edit function definition' 3set -l options h/help'e/editor=' i/interactive s/save 4argparse -n funced --max-args=1$options --$argv 5or return 6 7if set -q _flag_help 8__fish_print_help funced 9return 0 10end 11 12if not set -q argv[ 1 ] 13printf ( _ "%s: Expected at least %d args, got only %d\n" ) funced1 0 14return 1 15end 16 17set -l funcname$argv [ 1 ] 18 19# Check VISUAL first since theoretically EDITOR could be ed. 20set -l editor 21if set -q _flag_interactive 22set editor fish 23else if set -q _flag_editor 24echo $_flag_editor | read -at editor 25else if set -q VISUAL 26echo $VISUAL | read -at editor 27else if set -q EDITOR 28echo $EDITOR | read -at editor 29else 30set editor fish 31end 32 33set -l init 34switch $funcname 35case '-*' 36set init function --$funcname \n\n end 37case '*' 38set init function$funcname \n\n end 39end 40 41if not type -q -f" $editor [ 1 ] " 42printf >&2 \ 43( _ "funced: The value for \$%s '%s' could not be used because the command '%s' could not be found\n" ) \ 44( set -q VISUAL&& echo VISUAL|| echo EDITOR) \ 45" $editor " \ 46$editor [ 1 ] 47set editor fish 48end 49 50if test " $editor " = fish 51if functions -q --$funcname 52functions --no-details --$funcname | fish_indent --only-unindent| fish_indent --no-indent| read -z init 53end 54 55set -l prompt'printf "%s%s%s> " (set_color green) $funcname (set_color --reset)' 56if read -p$prompt -c" $init " --shell cmd 57echo -n$cmd | fish_indent --only-unindent| read -lz cmd 58eval " $cmd " 59end 60if set -q _flag_save 61funcsave $funcname 62end 63return 0 64end 65 66# Create a directory so we can use a ".fish" suffix for the file - 67# makes editors pick up that it's a fish file. 68set -l tmpdir( __fish_mktemp_relative -d fish-funced) 69or return 1 70set -l tmpname$tmpdir /$funcname .fish 71 72set -l writepath 73 74if not functions -q --$funcname 75echo $init > $tmpname 76else if not string match --quiet --regex'^(?:n/a|not-autoloaded|autoloaded)$' --( functions --details --verbose --$funcname )[ 2 ] 77# Pretend this copied function does not have a definition file. Editing the file which 78# originally defined it would not update this copy, and the file which copied it 79# would not have a function body to edit. (issue #11614) 80functions --$funcname > $tmpname 81else if functions --details --$funcname | string match --invert --quiet --regex'^(?:-|stdin|embedded:.*)$' 82set writepath( functions --details --$funcname ) 83# Use cat here rather than cp to avoid copying permissions 84cat " $writepath " > $tmpname 85else if set -l path( functions --details --$funcname | string match -rg'^embedded:(.*)$' ) 86status get-file$path > $tmpname 87else 88functions --$funcname > $tmpname 89end 90 91# Repeatedly edit until it either parses successfully, or the user cancels 92# If the editor command itself fails, we assume the user cancelled or the file 93# could not be edited, and we do not try again 94set -l checksum( __fish_md5 " $tmpname " ) 95while true 96if not $editor $tmpname 97echo ( _ "Editing failed or was cancelled" ) 98else 99# Verify the checksum (if present) to detect potential problems 100# with the editor command 101if set -q checksum[ 1 ] 102set -l new_checksum( __fish_md5 " $tmpname " ) 103if test " $new_checksum " = " $checksum " 104echo ( _ "Editor exited but the function was not modified" ) 105echo ( _ "If the editor is still running, check if it waits for completion, maybe a '--wait' option?" ) 106# Source but don't save an unmodified file. 107# (Source in case the file changed externally since we first loaded it.) 108if set -q writepath[ 1 ] 109source " $writepath " 110end 111break 112end 113end 114 115if not source < $tmpname 116# Failed to source the function file. Prompt to try again. 117echo # add a line between the parse error and the prompt 118set -l repeat 119set -l prompt( _ 'Edit the file again? [Y/n]' ) 120read -P" $prompt " response 121if test -z " $response " 122or contains $response { Y, y}{ E, e,}{ S, s,} 123continue 124else if not contains $response { N, n}{ O, o,} 125echo "I don't understand ' $response ', assuming 'Yes'" 126sleep 2 127continue 128end 129echo ( _ "Cancelled function editing" ) 130else if test -n " $writepath " 131if not set -q _flag_save 132echo ( _ "Warning: the file containing this function has not been saved. Changes may be lost when fish is closed." ) 133set -l prompt( printf ( _ 'Save function to %s? [Y/n]' ) " $writepath " ) 134read --prompt-str" $prompt " response 135if test -z " $response " 136or contains $response { Y, y}{ E, e,}{ S, s,} 137set _flag_save1 138else if not contains $response { N, n}{ O, o,} 139echo "I don't understand ' $response ', assuming 'Yes'" 140set _flag_save1 141end 142end 143if set -q _flag_save 144# try to write the file back 145# cp preserves existing permissions, though it might overwrite the owner 146if cp $tmpname " $writepath " 2>&1 147printf ( _ "Function saved to %s" ) " $writepath " 148echo 149# read it back again - this ensures that the output of `functions --details` is correct 150source " $writepath " 151else 152echo ( _ "Saving to original location failed; saving to user configuration instead." ) 153set writepath$__fish_config_dir /functions/( path basename" $writepath " ) 154if cp $tmpname " $writepath " 155printf ( _ "Function saved to %s" ) " $writepath " 156echo 157# read it back again - this ensures that the output of `functions --details` is correct 158source " $writepath " 159else 160echo ( _ "Saving to user configuration failed. Changes may be lost when fish is closed." ) 161end 162end 163end 164else if set -q _flag_save 165funcsave $funcname 166else 167printf ( _ "Run funcsave %s to save this function to the configuration directory." ) $funcname 168echo 169end 170end 171break 172end 173 174set -l stat$status 175command rm$tmpname > /dev/null 176and rmdir $tmpdir > /dev/null 177return $stat 178end