2016-12-07 03:24:08 +00:00
|
|
|
function edit_command_buffer --description 'Edit the command buffer in an external editor'
|
|
|
|
set -l f (mktemp)
|
2020-11-13 15:58:45 +00:00
|
|
|
or return 1
|
2016-12-07 03:24:08 +00:00
|
|
|
if set -q f[1]
|
2021-08-17 02:20:43 +00:00
|
|
|
command mv $f $f.fish
|
2016-12-07 03:24:08 +00:00
|
|
|
set f $f.fish
|
|
|
|
else
|
|
|
|
# We should never execute this block but better to be paranoid.
|
|
|
|
if set -q TMPDIR
|
2018-03-15 23:31:15 +00:00
|
|
|
set f $TMPDIR/fish.$fish_pid.fish
|
2016-12-07 03:24:08 +00:00
|
|
|
else
|
2018-03-15 23:31:15 +00:00
|
|
|
set f /tmp/fish.$fish_pid.fish
|
2016-12-07 03:24:08 +00:00
|
|
|
end
|
2021-08-17 02:20:43 +00:00
|
|
|
command touch $f
|
2016-12-07 03:24:08 +00:00
|
|
|
or return 1
|
|
|
|
end
|
|
|
|
|
2024-01-26 10:06:17 +00:00
|
|
|
set -l editor (__fish_anyeditor)
|
|
|
|
or return 1
|
2016-12-07 03:24:08 +00:00
|
|
|
|
2024-10-10 03:12:10 +00:00
|
|
|
set -l indented_lines (commandline -b | __fish_indent --only-indent)
|
2024-04-12 08:16:38 +00:00
|
|
|
string join -- \n $indented_lines >$f
|
2019-11-02 13:26:44 +00:00
|
|
|
set -l offset (commandline --cursor)
|
|
|
|
# compute cursor line/column
|
|
|
|
set -l lines (commandline)\n
|
|
|
|
set -l line 1
|
2020-03-10 20:01:00 +00:00
|
|
|
while test $offset -ge (string length -- $lines[1])
|
|
|
|
set offset (math $offset - (string length -- $lines[1]))
|
2019-11-02 13:26:44 +00:00
|
|
|
set line (math $line + 1)
|
|
|
|
set -e lines[1]
|
|
|
|
end
|
2024-04-12 08:16:38 +00:00
|
|
|
set -l indent 1 + (string length -- $indented_lines[$line]) - (string length -- $lines[1])
|
|
|
|
set -l col (math $offset + 1 + $indent)
|
2019-11-02 13:26:44 +00:00
|
|
|
|
2024-03-10 10:01:31 +00:00
|
|
|
set -l editor_basename (string match -r '[^/]+$' -- $editor[1])
|
|
|
|
set -l wrapped_commands
|
|
|
|
for wrap_target in (complete -- $editor_basename | string replace -rf '^complete [^/]+ --wraps (.+)$' '$1')
|
|
|
|
set -l tmp
|
|
|
|
string unescape -- $wrap_target | read -at tmp
|
|
|
|
set -a wrapped_commands $tmp[1]
|
|
|
|
end
|
2024-01-21 05:50:31 +00:00
|
|
|
set -l found false
|
2024-03-10 09:16:35 +00:00
|
|
|
set -l cursor_from_editor
|
2024-03-10 10:01:31 +00:00
|
|
|
for editor_command in $editor_basename $wrapped_commands
|
|
|
|
switch $editor_command
|
2024-01-21 05:50:31 +00:00
|
|
|
case vi vim nvim
|
2024-03-10 09:16:35 +00:00
|
|
|
if test $editor_command = vi && not set -l vi_version "$(vi --version 2>/dev/null)"
|
|
|
|
if printf %s $vi_version | grep -q BusyBox
|
|
|
|
break
|
|
|
|
end
|
|
|
|
set -a editor +{$line} $f
|
|
|
|
set found true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
set cursor_from_editor (mktemp)
|
|
|
|
set -a editor +$line "+norm! $col|" $f \
|
|
|
|
'+autocmd VimLeave * ++once call writefile(
|
|
|
|
[printf("%s %s %s", shellescape(bufname()), line("."), col("."))],
|
|
|
|
"'$cursor_from_editor'"
|
|
|
|
)'
|
|
|
|
case emacs emacsclient gedit
|
2024-01-21 05:50:31 +00:00
|
|
|
set -a editor +$line:$col $f
|
2024-03-10 09:16:35 +00:00
|
|
|
case kak
|
|
|
|
set cursor_from_editor (mktemp)
|
|
|
|
set -a editor +$line:$col $f -e "
|
|
|
|
hook -always -once global ClientClose %val{client} %{
|
|
|
|
echo -to-file $cursor_from_editor -quoting shell \
|
|
|
|
%val{buffile} %val{cursor_line} %val{cursor_column}
|
|
|
|
}
|
|
|
|
"
|
2024-01-21 05:50:31 +00:00
|
|
|
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
|
|
|
|
case micro
|
|
|
|
set -a editor $f +$line:$col
|
|
|
|
case '*'
|
|
|
|
continue
|
|
|
|
end
|
|
|
|
set found true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
if not $found
|
|
|
|
set -a editor $f
|
2019-11-02 13:26:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
$editor
|
2019-12-03 11:18:39 +00:00
|
|
|
|
2024-04-12 08:16:38 +00:00
|
|
|
set -l raw_lines (command cat $f)
|
2024-10-10 03:12:10 +00:00
|
|
|
set -l unindented_lines (string join -- \n $raw_lines | __fish_indent --only-unindent)
|
2024-04-12 08:16:38 +00:00
|
|
|
|
2016-12-07 03:24:08 +00:00
|
|
|
# Here we're checking the exit status of the editor.
|
2024-03-30 15:10:12 +00:00
|
|
|
if test $status -eq 0 -a -s $f
|
2016-12-07 03:24:08 +00:00
|
|
|
# Set the command to the output of the edited command and move the cursor to the
|
|
|
|
# end of the edited command.
|
2024-04-12 08:16:38 +00:00
|
|
|
commandline -r -- $unindented_lines
|
2016-12-07 03:24:08 +00: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
|
2024-03-10 09:16:35 +00:00
|
|
|
if set -q cursor_from_editor[1]
|
|
|
|
eval set -l pos "$(cat $cursor_from_editor)"
|
|
|
|
if set -q pos[1] && test $pos[1] = $f
|
|
|
|
set -l line $pos[2]
|
2024-11-01 18:01:50 +00:00
|
|
|
set -l indent (math (string length -- "$raw_lines[$line]") - (string length -- "$unindented_lines[$line]"))
|
2024-04-12 08:16:38 +00:00
|
|
|
set -l column (math $pos[3] - $indent)
|
2024-11-01 18:01:50 +00:00
|
|
|
if not commandline --line $line 2>/dev/null
|
|
|
|
commandline -f end-of-buffer
|
|
|
|
else
|
|
|
|
commandline --column $column 2>/dev/null || commandline -f end-of-line
|
2024-03-10 09:16:35 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
command rm $cursor_from_editor
|
|
|
|
end
|
2016-12-07 03:24:08 +00:00
|
|
|
command rm $f
|
2018-10-25 15:34:01 +00:00
|
|
|
# We've probably opened something that messed with the screen.
|
|
|
|
# A repaint seems in order.
|
|
|
|
commandline -f repaint
|
2016-12-07 03:24:08 +00:00
|
|
|
end
|