diff options
author | Fabian Homborg <FHomborg@gmail.com> | 2016-03-01 20:22:21 +0100 |
---|---|---|
committer | Kurtis Rader <krader@skepticism.us> | 2016-03-02 10:59:12 -0800 |
commit | 0e8a8a7c8023e05792b8b1d32ddbe7bcf2ea3549 (patch) | |
tree | fe2e30f064215b870a9367575046bfc919c8e11b | |
parent | fbd53f2da1af69058b16320265364257a11571df (diff) |
Migrate abbrs from =-separated to space-separated
We silently upgrade existing abbreviations and change the separator when
saving.
This does not yet warn when the user is using the old syntax.
Resolves #2051
-rw-r--r-- | doc_src/abbr.txt | 4 | ||||
-rw-r--r-- | share/config.fish | 11 | ||||
-rw-r--r-- | share/functions/abbr.fish | 23 |
3 files changed, 23 insertions, 15 deletions
diff --git a/doc_src/abbr.txt b/doc_src/abbr.txt index a4e709d0..9e7ebf66 100644 --- a/doc_src/abbr.txt +++ b/doc_src/abbr.txt @@ -2,7 +2,7 @@ \subsection abbr-synopsis Synopsis \fish{synopsis} -abbr -a word="phrase" +abbr -a word phrase... abbr -s abbr -l abbr -e word @@ -26,6 +26,8 @@ The following parameters are available: - `-e WORD` or `--erase WORD` Erase the abbreviation WORD. +Note: fish version 2.1 supported `-a WORD=PHRASE`. This syntax is now deprecated but will still be converted. + \subsection abbr-example Examples \fish diff --git a/share/config.fish b/share/config.fish index 755cc23e..b7a461c3 100644 --- a/share/config.fish +++ b/share/config.fish @@ -165,3 +165,14 @@ for file in $configdir/fish/conf.d/* $__fish_sysconfdir/conf.d/* $__fish_datadir # This allows one to use e.g. symlinks to /dev/null to "mask" something (like in systemd) [ -f $file -a -r $file ]; and source $file end + +# Upgrade pre-existing abbreviations from the old "key=value" to the new "key value" syntax +# This needs to be in share/config.fish because __fish_config_interactive is called after sourcing config.fish, which might contain abbr calls +if not set -q __fish_init_2_3_0 + set -l fab + for abb in $fish_user_abbreviations + set fab $fab (string replace -r '^([^ =]+)=(.*)$' '$1 $2' -- $abb) + end + set fish_user_abbreviations $fab + set -U __fish_init_2_3_0 +end diff --git a/share/functions/abbr.fish b/share/functions/abbr.fish index 53465eb6..bb50ec02 100644 --- a/share/functions/abbr.fish +++ b/share/functions/abbr.fish @@ -72,8 +72,12 @@ function abbr --description "Manage abbreviations" switch $mode case 'add' + # Convert from old "key=value" to new "key value" syntax + if string match -qr '^[^ ]+=' -- $mode_arg + set mode_arg (string replace "=" " " -- $mode_arg) + end + # Bail out early if the exact abbr is already in - # This depends on the separator staying the same, but that's the common case (config.fish) contains -- $mode_arg $fish_user_abbreviations; and return 0 set -l key set -l value @@ -141,12 +145,8 @@ function __fish_abbr_get_by_key # Going through all entries is still quicker than calling `seq` set -l keys for kv in $fish_user_abbreviations - if string match -qr '^[^ ]+=' -- $kv - # No need for bounds-checking because we already matched before - set keys $keys (string split "=" -m 1 -- $kv)[1] - else if string match -qr '^[^ ]+ .*' -- $kv - set keys $keys (string split " " -m 1 -- $kv)[1] - end + # If this does not match, we have screwed up before and the error should be reported + set keys $keys (string split " " -m 1 -- $kv)[1] end if set -l idx (contains -i -- $argv[1] $keys) echo $idx @@ -156,11 +156,6 @@ function __fish_abbr_get_by_key end function __fish_abbr_split -a input - if string match -qr '^[^ ]+=' -- $input - string split "=" -m 1 -- $input - else if string match -qr '^[^ ]+ .*' -- $input - string split " " -m 1 -- $input - else - echo $input - end + # Because we always save space-separated, we can be certain that this will match + string split " " -m 1 -- $input end |