Skip to content

Commit

Permalink
Warn when function is not modified by the editor after calling `funce…
Browse files Browse the repository at this point in the history
…d` (#3961)

* Check whether tmp file was modified in `funced`

* More idiomatic error messages

* Store the checksum in a local variable

* MD5 function supporting both GNU and BSD

* Use `else if` in MD5 function

* Use `string` builtin instead of `cut`
  • Loading branch information
adambyrtek authored and faho committed Apr 17, 2017
1 parent e332573 commit dd69ca5
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions 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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit dd69ca5

Please sign in to comment.