fork/fish-shell
git clone https://git.julia.blue/fork/fish-shell
d835c5252
master
1# localization: tier1 2function alias --description'Creates a function wrapping a command' 3set -l options h/help s/save 4argparse -n alias --max-args=2$options --$argv 5or return 6 7if set -q _flag_help 8__fish_print_help alias 9return 0 10end 11 12set -l name 13set -l body 14 15if not set -q argv[ 1 ] 16# Print the known aliases. 17for funcin ( functions -n) 18set -l output( functions $func | string match -r --"^function .* --description (?:'alias (.*)'|alias\\\\ (.*))\$" ) 19if set -q output[ 2 ] 20set output( string replace -r --'^' ( string escape --style=regex --$func ) '[= ]' '' $output [ 2 ]) 21echo alias$func ( string escape --$output [ 1 ]) 22end 23end 24return 0 25else if not set -q argv[ 2 ] 26# Alias definition of the form "name=value". 27set -l tmp( string split -m1 "=" --$argv ) "" 28set name$tmp [ 1 ] 29set body$tmp [ 2 ] 30else 31# Alias definition of the form "name value". 32set name$argv [ 1 ] 33set body$argv [ 2 ] 34end 35 36# sanity check 37if test -z " $name " 38printf ( _ "%s: name cannot be empty\n" ) alias>&2 39return 1 40else if test -z " $body " 41printf ( _ "%s: body cannot be empty\n" ) alias>&2 42return 1 43end 44 45# Extract the first command from the body. 46printf '%s\n' $body | read -l --list words 47set -l first_word$words [ 1 ] 48set -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. 52if test $first_word = $name 53if contains $name ( builtin --names) 54set body"builtin $body " 55else 56set body"command $body " 57end 58end 59set -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. 65set -l wraps 66if test $first_word != $name ;and test $last_word != $name 67set wraps --wraps( string escape --$body ) 68end 69 70# The function definition in split in two lines to ensure that a '#' can be put in the body. 71echo "function $name $wraps --description $cmd_string "\n" $body \$argv"\n"end" | source 72if set -q _flag_save 73funcsave $name 74end 75end