New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
function for editing command line in ext editor #3627
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
function edit_command_buffer --description 'Edit the command buffer in an external editor' | ||
set -l f (mktemp) | ||
if set -q f[1] | ||
mv $f $f.fish | ||
set f $f.fish | ||
else | ||
# We should never execute this block but better to be paranoid. | ||
if set -q TMPDIR | ||
set f $TMPDIR/fish.(echo %self).fish | ||
else | ||
set f /tmp/fish.(echo %self).fish | ||
end | ||
touch $f | ||
or return 1 | ||
end | ||
|
||
# Edit the command line with the users preferred editor or vim or emacs. | ||
commandline -b >$f | ||
if set -q VISUAL | ||
eval $VISUAL $f | ||
else if set -q EDITOR | ||
eval $EDITOR $f | ||
else | ||
echo | ||
echo (_ 'External editor requested but $VISUAL or $EDITOR not set.') | ||
echo (_ 'Please set VISUAL or EDITOR to your preferred editor.') | ||
commandline -f repaint | ||
command rm $f | ||
return 1 | ||
end | ||
|
||
# Here we're checking the exit status of the editor. | ||
if test $status -eq 0 -a -s $f | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe comment that this "$status" is the status of the editor, so we need to be careful not to break that pattern here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
# Set the command to the output of the edited command and move the cursor to the | ||
# end of the edited command. | ||
commandline -r (cat $f) | ||
commandline -C 999999 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should really have a nicer way to set the cursor to the end, and commandline should probably also accept stdin. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @flaom, Perhaps you should open a new issue, and take ownership of it, rather than commenting on a closed PR where your proposal has zero chance of being implemented? |
||
else | ||
echo | ||
echo (_ "Ignoring the output of your editor since its exit status was non-zero") | ||
echo (_ "or the file was empty") | ||
end | ||
command rm $f | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you know that this is racy. Though it seems a rather theoretical criticism since this function won't be executed in a tight loop by multiple shells at the same time.
Any fix to this would require either an alternative to mktemp or a mktemp that accepts a suffix, which is unlikely to be available on BSD or macOS.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In
psub
, we usemktemp -d
and then add a file with a suffix.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if this were executed in a tight loop it is so improbable there would be collision that it can be safely ignored. It's comparable to getting two commits in the fish project with the same leading eight digits of the commit hash.