diff --git a/share/functions/funced.fish b/share/functions/funced.fish index 686c89b7c..116e5d0d1 100644 --- a/share/functions/funced.fish +++ b/share/functions/funced.fish @@ -1,3 +1,16 @@ +function __funced_md5 + if type -q md5sum + # GNU systems + echo (md5sum $argv[1] | string split ' ')[1] + return 0 + else if type -q md5 + # BSD systems + md5 -q $argv[1] + return 0 + end + return 1 +end + function funced --description 'Edit function definition' set -l editor # Check VISUAL first since theoretically EDITOR could be ed @@ -39,8 +52,7 @@ function funced --description 'Edit function definition' if test (count $funcname) -ne 1 set_color red - _ "funced: You must specify one function name -" + echo (_ "funced: You must specify one function name") set_color normal return 1 end @@ -58,8 +70,7 @@ function funced --description 'Edit function definition' set -l editor_cmd eval set editor_cmd $editor if not type -q -f "$editor_cmd[1]" - _ "funced: The value for \$EDITOR '$editor' could not be used because the command '$editor_cmd[1]' could not be found - " + echo (_ "funced: The value for \$EDITOR '$editor' could not be used because the command '$editor_cmd[1]' could not be found") set editor fish end end @@ -102,14 +113,25 @@ function funced --description 'Edit function definition' else echo $init >$tmpname end + # Repeatedly edit until it either parses successfully, or the user cancels # If the editor command itself fails, we assume the user cancelled or the file # could not be edited, and we do not try again while true + set -l checksum (__funced_md5 "$tmpname") + if not eval $editor $tmpname - _ "Editing failed or was cancelled" - echo + echo (_ "Editing failed or was cancelled") else + # Verify the checksum (if present) to detect potential problems + # with the editor command + if set -q checksum[1] + set -l new_checksum (__funced_md5 "$tmpname") + if test "$new_checksum" = "$checksum" + echo (_ "Editor exited but the function was not modified") + end + end + if not source $tmpname # Failed to source the function file. Prompt to try again. echo # add a line between the parse error and the prompt @@ -121,12 +143,12 @@ function funced --description 'Edit function definition' if not contains $repeat n N no NO No nO continue end - _ "Cancelled function editing" - echo + echo (_ "Cancelled function editing") end end break end + set -l stat $status rm $tmpname >/dev/null and rmdir $tmpdir >/dev/null