mirror of
https://github.com/fish-shell/fish-shell
synced 2025-01-13 13:39:02 +00:00
Warn when function is not modified by the editor after calling funced
(#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`
This commit is contained in:
parent
e332573fe1
commit
dd69ca5a81
1 changed files with 30 additions and 8 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue