diff options
author | 2014-08-20 18:19:23 -0700 | |
---|---|---|
committer | 2014-08-22 11:40:59 -0700 | |
commit | 61ce9db4bac2da9f69487e828f627e4247ccc140 (patch) | |
tree | 1757ebf6e2f0fa312b6bbab4cd2339d481b1150d /share/functions | |
parent | 2da435712a5c7752b92b77fe9c5ca23965ceb22e (diff) |
Make the `alias` built-in function work better
The new --wraps functionality was breaking aliases of the form
`alias foo='bar baz'`. That is, aliases where the body is multiple
words. Extract the first word of the body and use that instead.
Use better errors for aliases with no name or no body.
Diffstat (limited to 'share/functions')
-rw-r--r-- | share/functions/alias.fish | 38 |
1 files changed, 28 insertions, 10 deletions
diff --git a/share/functions/alias.fish b/share/functions/alias.fish index 56beed12..2dc86b1b 100644 --- a/share/functions/alias.fish +++ b/share/functions/alias.fish @@ -12,6 +12,7 @@ function alias --description "Legacy function for creating shellscript functions set -l name set -l body set -l prefix + set -l first_word switch (count $argv) case 0 @@ -21,8 +22,9 @@ function alias --description "Legacy function for creating shellscript functions # Some seds (e.g. on Mac OS X), don't support \n in the RHS # Use a literal newline instead # http://sed.sourceforge.net/sedfaq4.html#s4.1 - set -l tmp (echo $argv|sed -e "s/\([^=]\)=/\1\\ -/") + # The extra '' at the end is so $tmp[2] is guaranteed to work + set -l tmp (echo $argv|sed -e 's/\([^=]\{0,1\}\)=/\1\ +/') '' set name $tmp[1] set body $tmp[2] @@ -35,18 +37,34 @@ function alias --description "Legacy function for creating shellscript functions return 1 end + # sanity check + if test -z "$name" + printf ( _ "%s: Name cannot be empty\n") alias + return 1 + else if test -z "$body" + printf ( _ "%s: Body cannot be empty\n") alias + return 1 + end + + # Extract the first command from the body + switch $body + case \*\ \* \*\t\* + # note: strip leading spaces if present + set first_word (echo $body|sed -e 's/^[[:space:]]\{1,\}//;s/[[:space:]].*//;q') + case '*' + set first_word $body + end # Prevent the alias from immediately running into an infinite recursion if # $body starts with the same command as $name. - switch $body - case $name $name\ \* $name\t\* - if contains $name (builtin --names) - set prefix builtin - else - set prefix command - end + if test $first_word = $name + if contains $name (builtin --names) + set prefix builtin + else + set prefix command + end end - eval "function $name --wraps $body; $prefix $body \$argv; end" + eval "function $name --wraps $first_word; $prefix $body \$argv; end" end |