mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-05-31 12:21:19 -03: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:
committed by
Fabian Homborg
parent
e332573fe1
commit
dd69ca5a81
@@ -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'
|
function funced --description 'Edit function definition'
|
||||||
set -l editor
|
set -l editor
|
||||||
# Check VISUAL first since theoretically EDITOR could be ed
|
# 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
|
if test (count $funcname) -ne 1
|
||||||
set_color red
|
set_color red
|
||||||
_ "funced: You must specify one function name
|
echo (_ "funced: You must specify one function name")
|
||||||
"
|
|
||||||
set_color normal
|
set_color normal
|
||||||
return 1
|
return 1
|
||||||
end
|
end
|
||||||
@@ -58,8 +70,7 @@ function funced --description 'Edit function definition'
|
|||||||
set -l editor_cmd
|
set -l editor_cmd
|
||||||
eval set editor_cmd $editor
|
eval set editor_cmd $editor
|
||||||
if not type -q -f "$editor_cmd[1]"
|
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
|
set editor fish
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -102,14 +113,25 @@ function funced --description 'Edit function definition'
|
|||||||
else
|
else
|
||||||
echo $init >$tmpname
|
echo $init >$tmpname
|
||||||
end
|
end
|
||||||
|
|
||||||
# Repeatedly edit until it either parses successfully, or the user cancels
|
# 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
|
# If the editor command itself fails, we assume the user cancelled or the file
|
||||||
# could not be edited, and we do not try again
|
# could not be edited, and we do not try again
|
||||||
while true
|
while true
|
||||||
|
set -l checksum (__funced_md5 "$tmpname")
|
||||||
|
|
||||||
if not eval $editor $tmpname
|
if not eval $editor $tmpname
|
||||||
_ "Editing failed or was cancelled"
|
echo (_ "Editing failed or was cancelled")
|
||||||
echo
|
|
||||||
else
|
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
|
if not source $tmpname
|
||||||
# Failed to source the function file. Prompt to try again.
|
# Failed to source the function file. Prompt to try again.
|
||||||
echo # add a line between the parse error and the prompt
|
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
|
if not contains $repeat n N no NO No nO
|
||||||
continue
|
continue
|
||||||
end
|
end
|
||||||
_ "Cancelled function editing"
|
echo (_ "Cancelled function editing")
|
||||||
echo
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
|
||||||
set -l stat $status
|
set -l stat $status
|
||||||
rm $tmpname >/dev/null
|
rm $tmpname >/dev/null
|
||||||
and rmdir $tmpdir >/dev/null
|
and rmdir $tmpdir >/dev/null
|
||||||
|
|||||||
Reference in New Issue
Block a user