fork/fish-shell

git clone https://git.julia.blue/fork/fish-shell

Shigure Kurosakifix(edit_command_buffer): ignore `cat` aliasdad58ac20

master
4.5 KiB117 linesraw
1# localization: tier1
2function edit_command_buffer --description 'Edit the command buffer in an external editor'
3    set -l tmpdir (__fish_mktemp_relative -d fish)
4    or return 1
5    set -l f $tmpdir/command-line.fish
6    command touch $f
7    or return 1
8
9    set -l editor (__fish_anyeditor)
10    or return 1
11
12    set -l indented_lines (commandline -b | fish_indent --only-indent)
13    string join -- \n $indented_lines >$f
14    set -l offset (commandline --cursor)
15    # compute cursor line/column
16    set -l lines (commandline)\n
17    set -l line 1
18    while test $offset -ge (string length -- $lines[1])
19        set offset (math $offset - (string length -- $lines[1]))
20        set line (math $line + 1)
21        set -e lines[1]
22    end
23    set -l indent 1 + (string length -- $indented_lines[$line]) - (string length -- $lines[1])
24    set -l col (math $offset + 1 + $indent)
25
26    set -l editor_basename (string match -r '[^/]+$' -- $editor[1])
27    set -l wrapped_commands
28    for wrap_target in (complete -- $editor_basename | string replace -rf '^complete [^/]+ --wraps (.+)$' '$1')
29        set -l tmp
30        string unescape -- $wrap_target | read -at tmp
31        set -a wrapped_commands $tmp[1]
32    end
33    set -l found false
34    set -l cursor_from_editor
35    for editor_command in $editor_basename $wrapped_commands
36        switch $editor_command
37            case vi vim nvim
38                if test $editor_command = vi && not set -l vi_version "$(vi --version 2>/dev/null)"
39                    if printf %s $vi_version | grep -q BusyBox
40                        break
41                    end
42                    set -a editor +{$line} $f
43                    set found true
44                    break
45                end
46                set cursor_from_editor (__fish_mktemp_relative fish-edit_command_buffer)
47                or return
48                set -a editor +$line "+norm! $col|" $f \
49                    '+au VimLeave * ++once call writefile([printf("%s %s %s", shellescape(bufname()), line("."), col("."))], "'$cursor_from_editor'")'
50            case emacs emacsclient gedit
51                set -a editor +$line:$col $f
52            case kak
53                set cursor_from_editor (__fish_mktemp_relative fish-edit_command_buffer)
54                or return
55                set -a editor +$line:$col $f -e "
56                        hook -always -once global ClientClose %val{client} %{
57                            echo -to-file $cursor_from_editor -quoting shell \
58                                %val{buffile} %val{cursor_line} %val{cursor_column}
59                        }
60                    "
61            case nano
62                set -a editor +$line,$col $f
63            case joe ee
64                set -a editor +$line $f
65            case code code-oss
66                set -a editor --goto $f:$line:$col --wait
67            case subl
68                set -a editor $f:$line:$col --wait
69            case micro
70                set -a editor $f +$line:$col
71            case helix hx
72                set -a editor $f:$line:$col
73            case '*'
74                continue
75        end
76        set found true
77        break
78    end
79    if not $found
80        set -a editor $f
81    end
82
83    $editor
84
85    set -l raw_lines (command cat $f)
86    set -l unindented_lines (string join -- \n $raw_lines | fish_indent --only-unindent)
87
88    # Here we're checking the exit status of the editor.
89    if test $status -eq 0 -a -s $f
90        # Set the command to the output of the edited command and move the cursor to the
91        # end of the edited command.
92        commandline -r -- $unindented_lines
93        commandline -C 999999
94    else
95        echo
96        echo (_ "Ignoring the output of your editor since its exit status was non-zero")
97        echo (_ "or the file was empty")
98    end
99    if set -q cursor_from_editor[1]
100        eval set -l pos "$(command cat $cursor_from_editor)"
101        if set -q pos[1] && test $pos[1] = $f
102            set -l line $pos[2]
103            set -l indent (math (string length -- "$raw_lines[$line]") - (string length -- "$unindented_lines[$line]"))
104            set -l column (math $pos[3] - $indent)
105            if not commandline --line $line 2>/dev/null
106                commandline -f end-of-buffer
107            else
108                commandline --column $column 2>/dev/null || commandline -f end-of-line
109            end
110        end
111        command rm $cursor_from_editor
112    end
113    command rm -r (path dirname $f)
114    # We've probably opened something that messed with the screen.
115    # A repaint seems in order.
116    commandline -f repaint
117end