fork/fish-shell

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

joveianAvoid losing work in funced when no changes between parse errors52998635f

master
7.2 KiB178 linesraw
1# localization: tier1
2function funced --description 'Edit function definition'
3    set -l options h/help 'e/editor=' i/interactive s/save
4    argparse -n funced --max-args=1 $options -- $argv
5    or return
6
7    if set -q _flag_help
8        __fish_print_help funced
9        return 0
10    end
11
12    if not set -q argv[1]
13        printf (_ "%s: Expected at least %d args, got only %d\n") funced 1 0
14        return 1
15    end
16
17    set -l funcname $argv[1]
18
19    # Check VISUAL first since theoretically EDITOR could be ed.
20    set -l editor
21    if set -q _flag_interactive
22        set editor fish
23    else if set -q _flag_editor
24        echo $_flag_editor | read -at editor
25    else if set -q VISUAL
26        echo $VISUAL | read -at editor
27    else if set -q EDITOR
28        echo $EDITOR | read -at editor
29    else
30        set editor fish
31    end
32
33    set -l init
34    switch $funcname
35        case '-*'
36            set init function -- $funcname\n\nend
37        case '*'
38            set init function $funcname\n\nend
39    end
40
41    if not type -q -f "$editor[1]"
42        printf >&2 \
43            (_ "funced: The value for \$%s '%s' could not be used because the command '%s' could not be found\n") \
44            (set -q VISUAL && echo VISUAL || echo EDITOR) \
45            "$editor" \
46            $editor[1]
47        set editor fish
48    end
49
50    if test "$editor" = fish
51        if functions -q -- $funcname
52            functions --no-details -- $funcname | fish_indent --only-unindent | fish_indent --no-indent | read -z init
53        end
54
55        set -l prompt 'printf "%s%s%s> " (set_color green) $funcname (set_color --reset)'
56        if read -p $prompt -c "$init" --shell cmd
57            echo -n $cmd | fish_indent --only-unindent | read -lz cmd
58            eval "$cmd"
59        end
60        if set -q _flag_save
61            funcsave $funcname
62        end
63        return 0
64    end
65
66    # Create a directory so we can use a ".fish" suffix for the file -
67    # makes editors pick up that it's a fish file.
68    set -l tmpdir (__fish_mktemp_relative -d fish-funced)
69    or return 1
70    set -l tmpname $tmpdir/$funcname.fish
71
72    set -l writepath
73
74    if not functions -q -- $funcname
75        echo $init >$tmpname
76    else if not string match --quiet --regex '^(?:n/a|not-autoloaded|autoloaded)$' -- (functions --details --verbose -- $funcname)[2]
77        # Pretend this copied function does not have a definition file. Editing the file which
78        # originally defined it would not update this copy, and the file which copied it
79        # would not have a function body to edit. (issue #11614)
80        functions -- $funcname >$tmpname
81    else if functions --details -- $funcname | string match --invert --quiet --regex '^(?:-|stdin|embedded:.*)$'
82        set writepath (functions --details -- $funcname)
83        # Use cat here rather than cp to avoid copying permissions
84        cat "$writepath" >$tmpname
85    else if set -l path (functions --details -- $funcname | string match -rg '^embedded:(.*)$')
86        status get-file $path >$tmpname
87    else
88        functions -- $funcname >$tmpname
89    end
90
91    # Repeatedly edit until it either parses successfully, or the user cancels
92    # If the editor command itself fails, we assume the user cancelled or the file
93    # could not be edited, and we do not try again
94    set -l checksum (__fish_md5 "$tmpname")
95    while true
96        if not $editor $tmpname
97            echo (_ "Editing failed or was cancelled")
98        else
99            # Verify the checksum (if present) to detect potential problems
100            # with the editor command
101            if set -q checksum[1]
102                set -l new_checksum (__fish_md5 "$tmpname")
103                if test "$new_checksum" = "$checksum"
104                    echo (_ "Editor exited but the function was not modified")
105                    echo (_ "If the editor is still running, check if it waits for completion, maybe a '--wait' option?")
106                    # Source but don't save an unmodified file.
107                    # (Source in case the file changed externally since we first loaded it.)
108                    if set -q writepath[1]
109                        source "$writepath"
110                    end
111                    break
112                end
113            end
114
115            if not source <$tmpname
116                # Failed to source the function file. Prompt to try again.
117                echo # add a line between the parse error and the prompt
118                set -l repeat
119                set -l prompt (_ 'Edit the file again? [Y/n]')
120                read -P "$prompt " response
121                if test -z "$response"
122                    or contains $response {Y,y}{E,e,}{S,s,}
123                    continue
124                else if not contains $response {N,n}{O,o,}
125                    echo "I don't understand '$response', assuming 'Yes'"
126                    sleep 2
127                    continue
128                end
129                echo (_ "Cancelled function editing")
130            else if test -n "$writepath"
131                if not set -q _flag_save
132                    echo (_ "Warning: the file containing this function has not been saved. Changes may be lost when fish is closed.")
133                    set -l prompt (printf (_ 'Save function to %s? [Y/n]') "$writepath")
134                    read --prompt-str "$prompt " response
135                    if test -z "$response"
136                        or contains $response {Y,y}{E,e,}{S,s,}
137                        set _flag_save 1
138                    else if not contains $response {N,n}{O,o,}
139                        echo "I don't understand '$response', assuming 'Yes'"
140                        set _flag_save 1
141                    end
142                end
143                if set -q _flag_save
144                    # try to write the file back
145                    # cp preserves existing permissions, though it might overwrite the owner
146                    if cp $tmpname "$writepath" 2>&1
147                        printf (_ "Function saved to %s") "$writepath"
148                        echo
149                        # read it back again - this ensures that the output of `functions --details` is correct
150                        source "$writepath"
151                    else
152                        echo (_ "Saving to original location failed; saving to user configuration instead.")
153                        set writepath $__fish_config_dir/functions/(path basename "$writepath")
154                        if cp $tmpname "$writepath"
155                            printf (_ "Function saved to %s") "$writepath"
156                            echo
157                            # read it back again - this ensures that the output of `functions --details` is correct
158                            source "$writepath"
159                        else
160                            echo (_ "Saving to user configuration failed. Changes may be lost when fish is closed.")
161                        end
162                    end
163                end
164            else if set -q _flag_save
165                funcsave $funcname
166            else
167                printf (_ "Run funcsave %s to save this function to the configuration directory.") $funcname
168                echo
169            end
170        end
171        break
172    end
173
174    set -l stat $status
175    command rm $tmpname >/dev/null
176    and rmdir $tmpdir >/dev/null
177    return $stat
178end