From 4115a2f2d115d97d77f2bd81609c6756acbeab2e Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Sun, 14 Jun 2015 14:08:10 -0700 Subject: [PATCH] Tweak and add tests for abbr 1. When run with no arguments, make abbr do the equivalent of `abbr --show` 2. Enable "implicit add", e.g. `abbr gco git checkout` 3. Teach `abbr --show` to not use quotes for simple cases 4. Teach abbr to output -- when the abbreviation has leading dashes Add some basic tests to abbr too. --- share/functions/abbr.fish | 38 +++++++++++++++++++++++------ share/tools/web_config/webconfig.py | 2 +- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/share/functions/abbr.fish b/share/functions/abbr.fish index dd67b898f..87cf3184e 100644 --- a/share/functions/abbr.fish +++ b/share/functions/abbr.fish @@ -1,9 +1,4 @@ function abbr --description "Manage abbreviations" - if not set -q argv[1] - __fish_print_help abbr - return 1 - end - # parse arguments set -l mode set -l mode_flag # the flag that was specified, for better errors @@ -56,6 +51,18 @@ function abbr --description "Manage abbreviations" printf ( _ "%s: option requires an argument -- %s\n" ) abbr $mode_flag >&2 return 1 end + + # If run with no options, treat it like --add if we have an argument, or + # --show if we do not have an argument + if test -z "$mode" + if set -q argv[1] + set mode 'add' + set mode_arg "$argv" + set -e argv + else + set mode 'show' + end + end # none of our modes want any excess arguments if set -q argv[1] @@ -106,7 +113,14 @@ function abbr --description "Manage abbreviations" for i in $fish_user_abbreviations # Disable newline splitting set -lx IFS '' - echo abbr -a \'(__fish_abbr_escape $i)\' + __fish_abbr_parse_entry $i key value + + # Check to see if either key or value has a leading dash + # If so, we need to write -- + set -l opt_double_dash '' + switch $key ; case '-*'; set opt_double_dash ' --'; end + switch $value ; case '-*'; set opt_double_dash ' --'; end + echo abbr$opt_double_dash (__fish_abbr_escape "$key") (__fish_abbr_escape "$value") end return 0 @@ -121,7 +135,17 @@ function abbr --description "Manage abbreviations" end function __fish_abbr_escape - echo $argv | sed -e s,\\\\,\\\\\\\\,g -e s,\',\\\\\',g + # Prettify the common case: if everything is alphanumeric, + # we do not need escapes. + # Do this by deleting alnum characters, and check if there's anything left. + # Note we need to preserve spaces, so spaces are not considered alnum + if test -z (echo -n "$argv" | tr -d '[:alnum:]_') + echo $argv + else + # Escape via single quotes + # printf is nice for stripping the newline that sed outputs + printf "'%s'" (echo -n $argv | sed -e s,\\\\,\\\\\\\\,g -e s,\',\\\\\',g) + end end function __fish_abbr_get_by_key diff --git a/share/tools/web_config/webconfig.py b/share/tools/web_config/webconfig.py index c122f587b..359946ae8 100755 --- a/share/tools/web_config/webconfig.py +++ b/share/tools/web_config/webconfig.py @@ -709,7 +709,7 @@ class FishConfigHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): return True def do_save_abbreviation(self, abbreviation): - out, err = run_fish_cmd('abbr --add \'%s %s\'' % (abbreviation['word'], abbreviation['phrase'])) + out, err = run_fish_cmd('abbr --add -- \'%s %s\'' % (abbreviation['word'], abbreviation['phrase'])) if err: return err else: