Skip to content

Instantly share code, notes, and snippets.

@lamchau
Created December 23, 2023 06:33
Show Gist options
  • Save lamchau/ce2ad1c4b6a29f48fd5601a69a1f6781 to your computer and use it in GitHub Desktop.
Save lamchau/ce2ad1c4b6a29f48fd5601a69a1f6781 to your computer and use it in GitHub Desktop.
#!/usr/bin/env fish
# wrapper to allow for the default funced to be used
source (type --path __fish_pwd | path dirname)/funced.fish
funced $argv

extracted via dotfiles commit

fish: wrap/split funced to avoid temp files

By design, funced uses a temporary file to make changes to functions. While this is useful so users don't overwrite key functionality in share/fish/functions ensuring the edited function can be parsed by fish. However, this adds friction (e.g. save, exit, load, run) during the development of a user-defined function. Improved developmer ergonomics and fish's autoloading of functions would be (save and run) with a separate session.

Additionally, because each "save -> exit -> load -> run" forces the user to exit the $EDITOR each time. That also means we also lose the ability to have consistent undo/session history because each funced invocation creates a new temporary file.

To enable the editing of a builtin, there's a helper executable __funced that allows for default behavior but also provides a warning.

function funced --description "edit a function"
set_color brred
argparse --min 1 --max 1 -- $argv
set_color reset
if test (count $argv) -ne 1
return 1
end
set --local current_directory (status --current-filename | path dirname)
set --local name $argv[1]
if builtin --query $name
set_color bryellow
echo "warning: attempting to edit builtin `$name`"
set_color reset
fish "$current_directory/__funced.fish" $name
return 0
end
set --local checksum ""
set --local new_checksum ""
set --local filepath "$__fish_config_dir/functions/$name.fish"
if functions --query "$name"
set checksum (__fish_md5 "$filepath")
nvim "$filepath"
else
# new file
# TODO: make $EDITOR agnostic
echo "function $name --description 'description'
#
end" | nvim - +"2 | file $name.fish | set filetype=fish"
end
if test -f "$filepath"
set new_checksum (__fish_md5 "$filepath")
else
return 0
end
if test "$checksum" = "$new_checksum"
echo "function not modified: $filepath"
else
set_color --bold yellow
echo "function modified: $filepath"
set_color reset
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment