fork/fish-shell

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

Johannes AltmanningerPrepare to not localize private function descriptionsd835c5252

master
2.5 KiB75 linesraw
1# localization: tier1
2function alias --description 'Creates a function wrapping a command'
3    set -l options h/help s/save
4    argparse -n alias --max-args=2 $options -- $argv
5    or return
6
7    if set -q _flag_help
8        __fish_print_help alias
9        return 0
10    end
11
12    set -l name
13    set -l body
14
15    if not set -q argv[1]
16        # Print the known aliases.
17        for func in (functions -n)
18            set -l output (functions $func | string match -r -- "^function .* --description (?:'alias (.*)'|alias\\\\ (.*))\$")
19            if set -q output[2]
20                set output (string replace -r -- '^'(string escape --style=regex -- $func)'[= ]' '' $output[2])
21                echo alias $func (string escape -- $output[1])
22            end
23        end
24        return 0
25    else if not set -q argv[2]
26        # Alias definition of the form "name=value".
27        set -l tmp (string split -m 1 "=" -- $argv) ""
28        set name $tmp[1]
29        set body $tmp[2]
30    else
31        # Alias definition of the form "name value".
32        set name $argv[1]
33        set body $argv[2]
34    end
35
36    # sanity check
37    if test -z "$name"
38        printf ( _ "%s: name cannot be empty\n") alias >&2
39        return 1
40    else if test -z "$body"
41        printf ( _ "%s: body cannot be empty\n") alias >&2
42        return 1
43    end
44
45    # Extract the first command from the body.
46    printf '%s\n' $body | read -l --list words
47    set -l first_word $words[1]
48    set -l last_word $words[-1]
49
50    # Prevent the alias from immediately running into an infinite recursion if
51    # $body starts with the same command as $name.
52    if test $first_word = $name
53        if contains $name (builtin --names)
54            set body "builtin $body"
55        else
56            set body "command $body"
57        end
58    end
59    set -l cmd_string (string escape -- "alias $argv")
60
61    # Do not define wrapper completion if we have "alias foo 'foo xyz'" or "alias foo 'sudo foo'"
62    # This is to prevent completions from recursively calling themselves (#7389).
63    # The latter will have rare false positives but it's more important to
64    # prevent recursion for this high-level command.
65    set -l wraps
66    if test $first_word != $name; and test $last_word != $name
67        set wraps --wraps (string escape -- $body)
68    end
69
70    # The function definition in split in two lines to ensure that a '#' can be put in the body.
71    echo "function $name $wraps --description $cmd_string"\n"    $body \$argv"\n"end" | source
72    if set -q _flag_save
73        funcsave $name
74    end
75end