2016-12-07 11:24:08 +08:00
|
|
|
function edit_command_buffer --description 'Edit the command buffer in an external editor'
|
|
|
|
set -l f (mktemp)
|
2020-11-13 23:58:45 +08:00
|
|
|
or return 1
|
2016-12-07 11:24:08 +08:00
|
|
|
if set -q f[1]
|
2021-08-17 10:20:43 +08:00
|
|
|
command mv $f $f.fish
|
2016-12-07 11:24:08 +08:00
|
|
|
set f $f.fish
|
|
|
|
else
|
|
|
|
# We should never execute this block but better to be paranoid.
|
|
|
|
if set -q TMPDIR
|
2018-03-16 07:31:15 +08:00
|
|
|
set f $TMPDIR/fish.$fish_pid.fish
|
2016-12-07 11:24:08 +08:00
|
|
|
else
|
2018-03-16 07:31:15 +08:00
|
|
|
set f /tmp/fish.$fish_pid.fish
|
2016-12-07 11:24:08 +08:00
|
|
|
end
|
2021-08-17 10:20:43 +08:00
|
|
|
command touch $f
|
2016-12-07 11:24:08 +08:00
|
|
|
or return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
# Edit the command line with the users preferred editor or vim or emacs.
|
2019-12-03 19:18:39 +08:00
|
|
|
set -l editor
|
2016-12-07 11:24:08 +08:00
|
|
|
if set -q VISUAL
|
2019-12-03 19:18:39 +08:00
|
|
|
echo $VISUAL | read -at editor
|
2016-12-07 11:24:08 +08:00
|
|
|
else if set -q EDITOR
|
2019-12-03 19:18:39 +08:00
|
|
|
echo $EDITOR | read -at editor
|
2016-12-07 11:24:08 +08:00
|
|
|
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
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2019-12-03 19:18:39 +08:00
|
|
|
commandline -b >$f
|
2019-11-02 21:26:44 +08:00
|
|
|
set -l offset (commandline --cursor)
|
|
|
|
# compute cursor line/column
|
|
|
|
set -l lines (commandline)\n
|
|
|
|
set -l line 1
|
2020-03-11 04:01:00 +08:00
|
|
|
while test $offset -ge (string length -- $lines[1])
|
|
|
|
set offset (math $offset - (string length -- $lines[1]))
|
2019-11-02 21:26:44 +08:00
|
|
|
set line (math $line + 1)
|
|
|
|
set -e lines[1]
|
|
|
|
end
|
|
|
|
set col (math $offset + 1)
|
|
|
|
|
|
|
|
set -l basename (string match -r '[^/]*$' -- $editor[1])
|
2020-04-28 06:40:46 +08:00
|
|
|
switch $basename
|
|
|
|
case vi vim nvim
|
|
|
|
set -a editor +$line +"norm $col|" $f
|
|
|
|
case emacs emacsclient gedit kak
|
|
|
|
set -a editor +$line:$col $f
|
|
|
|
case nano
|
|
|
|
set -a editor +$line,$col $f
|
|
|
|
case joe ee
|
|
|
|
set -a editor +$line $f
|
|
|
|
case code code-oss
|
|
|
|
set -a editor --goto $f:$line:$col --wait
|
|
|
|
case subl
|
|
|
|
set -a editor $f:$line:$col --wait
|
2020-05-07 21:17:13 +08:00
|
|
|
case micro
|
2022-04-24 21:03:37 +08:00
|
|
|
set -a editor $f +$line:$col
|
2020-04-28 06:40:46 +08:00
|
|
|
case '*'
|
|
|
|
set -a editor $f
|
2019-11-02 21:26:44 +08:00
|
|
|
end
|
|
|
|
|
2019-12-03 19:18:39 +08:00
|
|
|
__fish_disable_bracketed_paste
|
2019-11-02 21:26:44 +08:00
|
|
|
$editor
|
2019-12-03 19:18:39 +08:00
|
|
|
set -l editor_status $status
|
|
|
|
__fish_enable_bracketed_paste
|
|
|
|
|
2016-12-07 11:24:08 +08:00
|
|
|
# Here we're checking the exit status of the editor.
|
2019-12-03 19:18:39 +08:00
|
|
|
if test $editor_status -eq 0 -a -s $f
|
2016-12-07 11:24:08 +08:00
|
|
|
# Set the command to the output of the edited command and move the cursor to the
|
|
|
|
# end of the edited command.
|
2021-09-12 20:42:16 +08:00
|
|
|
commandline -r -- (command cat $f)
|
2016-12-07 11:24:08 +08:00
|
|
|
commandline -C 999999
|
|
|
|
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
|
2018-10-25 23:34:01 +08:00
|
|
|
# We've probably opened something that messed with the screen.
|
|
|
|
# A repaint seems in order.
|
|
|
|
commandline -f repaint
|
2016-12-07 11:24:08 +08:00
|
|
|
end
|