File: edit_command_buffer.fish

package info (click to toggle)
fish 4.2.1-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,980 kB
  • sloc: python: 6,972; javascript: 1,407; sh: 1,009; xml: 411; ansic: 230; objc: 78; makefile: 20
file content (117 lines) | stat: -rw-r--r-- 4,579 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# localization: tier1
function edit_command_buffer --description 'Edit the command buffer in an external editor'
    set -l tmpdir (__fish_mktemp_relative -d fish)
    or return 1
    set -l f $tmpdir/command-line.fish
    command touch $f
    or return 1

    set -l editor (__fish_anyeditor)
    or return 1

    set -l indented_lines (commandline -b | fish_indent --only-indent)
    string join -- \n $indented_lines >$f
    set -l offset (commandline --cursor)
    # compute cursor line/column
    set -l lines (commandline)\n
    set -l line 1
    while test $offset -ge (string length -- $lines[1])
        set offset (math $offset - (string length -- $lines[1]))
        set line (math $line + 1)
        set -e lines[1]
    end
    set -l indent 1 + (string length -- $indented_lines[$line]) - (string length -- $lines[1])
    set -l col (math $offset + 1 + $indent)

    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
    set -l found false
    set -l cursor_from_editor
    for editor_command in $editor_basename $wrapped_commands
        switch $editor_command
            case vi vim nvim
                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 (__fish_mktemp_relative fish-edit_command_buffer)
                or return
                set -a editor +$line "+norm! $col|" $f \
                    '+au VimLeave * ++once call writefile([printf("%s %s %s", shellescape(bufname()), line("."), col("."))], "'$cursor_from_editor'")'
            case emacs emacsclient gedit
                set -a editor +$line:$col $f
            case kak
                set cursor_from_editor (__fish_mktemp_relative fish-edit_command_buffer)
                or return
                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}
                        }
                    "
            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 helix hx
                set -a editor $f:$line:$col
            case '*'
                continue
        end
        set found true
        break
    end
    if not $found
        set -a editor $f
    end

    $editor

    set -l raw_lines (command cat $f)
    set -l unindented_lines (string join -- \n $raw_lines | fish_indent --only-unindent)

    # Here we're checking the exit status of the editor.
    if test $status -eq 0 -a -s $f
        # Set the command to the output of the edited command and move the cursor to the
        # end of the edited command.
        commandline -r -- $unindented_lines
        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
    if set -q cursor_from_editor[1]
        eval set -l pos "$(command cat $cursor_from_editor)"
        if set -q pos[1] && test $pos[1] = $f
            set -l line $pos[2]
            set -l indent (math (string length -- "$raw_lines[$line]") - (string length -- "$unindented_lines[$line]"))
            set -l column (math $pos[3] - $indent)
            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
            end
        end
        command rm $cursor_from_editor
    end
    command rm -r (path dirname $f)
    # We've probably opened something that messed with the screen.
    # A repaint seems in order.
    commandline -f repaint
end