2017-04-17 15:18:02 +00:00
|
|
|
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
|
|
|
|
|
2012-06-27 03:37:48 +00:00
|
|
|
function funced --description 'Edit function definition'
|
2020-03-09 18:36:12 +00:00
|
|
|
set -l options h/help 'e/editor=' i/interactive s/save
|
2018-11-22 11:43:35 +00:00
|
|
|
argparse -n funced --max-args=1 $options -- $argv
|
2017-07-13 20:29:35 +00:00
|
|
|
or return
|
|
|
|
|
|
|
|
if set -q _flag_help
|
|
|
|
__fish_print_help funced
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2018-11-22 11:43:35 +00:00
|
|
|
if not set -q argv[1]
|
|
|
|
printf (_ "%ls: Expected at least %d args, got only %d\n") funced 1 0
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2020-05-15 05:56:06 +00:00
|
|
|
set -l funcname $argv[1]
|
2017-07-13 20:29:35 +00:00
|
|
|
|
|
|
|
# Check VISUAL first since theoretically EDITOR could be ed.
|
2015-09-02 11:55:59 +00:00
|
|
|
set -l editor
|
2017-07-13 20:29:35 +00:00
|
|
|
if set -q _flag_interactive
|
|
|
|
set editor fish
|
|
|
|
else if set -q _flag_editor
|
|
|
|
set editor $_flag_editor
|
|
|
|
else if set -q VISUAL
|
2015-09-02 11:55:59 +00:00
|
|
|
set editor $VISUAL
|
|
|
|
else if set -q EDITOR
|
|
|
|
set editor $EDITOR
|
2017-07-13 20:29:35 +00:00
|
|
|
else
|
|
|
|
set editor fish
|
2012-06-30 02:22:41 +00:00
|
|
|
end
|
2012-06-27 03:37:48 +00:00
|
|
|
|
2012-06-30 02:22:41 +00:00
|
|
|
set -l init
|
|
|
|
switch $funcname
|
|
|
|
case '-*'
|
2016-11-28 05:27:22 +00:00
|
|
|
set init function -- $funcname\n\nend
|
2012-06-30 02:22:41 +00:00
|
|
|
case '*'
|
2016-11-28 05:27:22 +00:00
|
|
|
set init function $funcname\n\nend
|
2012-06-30 02:22:41 +00:00
|
|
|
end
|
2012-06-27 03:37:48 +00:00
|
|
|
|
2013-01-24 02:24:49 +00:00
|
|
|
# Break editor up to get its first command (i.e. discard flags)
|
2018-10-15 08:13:19 +00:00
|
|
|
set -l editor_cmd
|
2019-12-03 11:18:39 +00:00
|
|
|
echo $editor | read -ta editor_cmd
|
2018-10-15 08:13:19 +00:00
|
|
|
if not type -q -f "$editor_cmd[1]"
|
|
|
|
echo (_ "funced: The value for \$EDITOR '$editor' could not be used because the command '$editor_cmd[1]' could not be found")
|
|
|
|
set editor fish
|
2013-01-27 21:14:24 +00:00
|
|
|
end
|
2016-11-28 05:27:22 +00:00
|
|
|
|
2017-07-13 20:29:35 +00:00
|
|
|
if test "$editor" = fish
|
2012-06-30 02:22:41 +00:00
|
|
|
if functions -q -- $funcname
|
2021-03-30 14:51:27 +00:00
|
|
|
functions --no-details -- $funcname | fish_indent --no-indent | read -z init
|
2012-06-30 02:22:41 +00:00
|
|
|
end
|
2007-04-22 18:55:39 +00:00
|
|
|
|
2012-06-30 02:22:41 +00:00
|
|
|
set -l prompt 'printf "%s%s%s> " (set_color green) '$funcname' (set_color normal)'
|
2018-03-09 17:55:12 +00:00
|
|
|
if read -p $prompt -c "$init" --shell cmd
|
2017-07-25 03:45:43 +00:00
|
|
|
echo -n $cmd | fish_indent | read -lz cmd
|
2018-10-28 00:19:00 +00:00
|
|
|
eval "$cmd"
|
2012-06-30 02:22:41 +00:00
|
|
|
end
|
2017-12-21 16:54:29 +00:00
|
|
|
if set -q _flag_save
|
|
|
|
funcsave $funcname
|
|
|
|
end
|
2012-06-30 02:22:41 +00:00
|
|
|
return 0
|
|
|
|
end
|
|
|
|
|
2017-07-25 03:45:43 +00:00
|
|
|
# OS X (macOS) `mktemp` is rather restricted - no suffix, no way to automatically use TMPDIR.
|
|
|
|
# Create a directory so we can use a ".fish" suffix for the file - makes editors pick up that
|
|
|
|
# it's a fish file.
|
2016-11-28 05:27:22 +00:00
|
|
|
set -q TMPDIR
|
|
|
|
or set -l TMPDIR /tmp
|
2016-05-22 22:49:09 +00:00
|
|
|
set -l tmpdir (mktemp -d $TMPDIR/fish.XXXXXX)
|
2020-11-13 15:58:45 +00:00
|
|
|
or return 1
|
2016-05-22 22:49:09 +00:00
|
|
|
set -l tmpname $tmpdir/$funcname.fish
|
2012-06-30 02:22:41 +00:00
|
|
|
|
|
|
|
if functions -q -- $funcname
|
2016-11-28 05:27:22 +00:00
|
|
|
functions -- $funcname >$tmpname
|
2012-06-30 02:22:41 +00:00
|
|
|
else
|
2016-11-28 05:27:22 +00:00
|
|
|
echo $init >$tmpname
|
2012-06-30 02:22:41 +00:00
|
|
|
end
|
2017-04-17 15:18:02 +00:00
|
|
|
|
2016-11-28 05:27:22 +00:00
|
|
|
# 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
|
2017-04-17 15:18:02 +00:00
|
|
|
set -l checksum (__funced_md5 "$tmpname")
|
|
|
|
|
2019-02-07 08:47:15 +00:00
|
|
|
if not eval $editor $tmpname
|
2017-04-17 15:18:02 +00:00
|
|
|
echo (_ "Editing failed or was cancelled")
|
2016-11-28 05:27:22 +00:00
|
|
|
else
|
2017-04-17 15:18:02 +00:00
|
|
|
# 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
|
|
|
|
|
2016-11-28 05:27:22 +00:00
|
|
|
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
|
|
|
|
set -l repeat
|
|
|
|
set -l prompt (_ 'Edit the file again\? [Y/n]')
|
2017-08-11 23:09:13 +00:00
|
|
|
read -p "echo $prompt\ " response
|
|
|
|
if test -z "$response"
|
|
|
|
or contains $response {Y,y}{E,e,}{S,s,}
|
|
|
|
continue
|
|
|
|
else if not contains $response {N,n}{O,o,}
|
|
|
|
echo "I don't understand '$response', assuming 'Yes'"
|
|
|
|
sleep 2
|
2016-11-28 05:27:22 +00:00
|
|
|
continue
|
|
|
|
end
|
2017-04-17 15:18:02 +00:00
|
|
|
echo (_ "Cancelled function editing")
|
2017-12-21 16:54:29 +00:00
|
|
|
else if set -q _flag_save
|
|
|
|
funcsave $funcname
|
2016-11-28 05:27:22 +00:00
|
|
|
end
|
2014-10-17 18:49:26 +00:00
|
|
|
end
|
2016-11-28 05:27:22 +00:00
|
|
|
break
|
|
|
|
end
|
2017-04-17 15:18:02 +00:00
|
|
|
|
2012-11-18 10:23:22 +00:00
|
|
|
set -l stat $status
|
2021-03-15 19:38:35 +00:00
|
|
|
command rm $tmpname >/dev/null
|
2016-05-28 10:34:04 +00:00
|
|
|
and rmdir $tmpdir >/dev/null
|
2012-06-30 02:22:41 +00:00
|
|
|
return $stat
|
|
|
|
end
|