From 501c3d5518ace12ae47e3eea5274a85786faa970 Mon Sep 17 00:00:00 2001 From: David Adam Date: Sun, 5 Oct 2014 12:17:46 +0800 Subject: [PATCH] abbr.fish: add abbr, a command to manipulate abbreviations Work on #731. --- doc_src/abbr.txt | 44 ++++++++++++++++++++++++ share/completions/abbr.fish | 5 +++ share/functions/abbr.fish | 68 +++++++++++++++++++++++++++++++++++++ 3 files changed, 117 insertions(+) create mode 100644 doc_src/abbr.txt create mode 100644 share/completions/abbr.fish create mode 100644 share/functions/abbr.fish diff --git a/doc_src/abbr.txt b/doc_src/abbr.txt new file mode 100644 index 000000000..a8bdd1e35 --- /dev/null +++ b/doc_src/abbr.txt @@ -0,0 +1,44 @@ +\section abbr abbr - manage fish abbreviations + +\subsection abbr-synopsis Synopsis +\fish{synopsis} +abbr -a word="phrase" +abbr -s +abbr -l +abbr -r word +\endfish + +\subsection abbr-description Description + +`abbr` manipulates the list of abbreviations that fish will expand. + +Abbreviations are user-defined character sequences or words that are replaced with longer phrases after tehy are entered. For example, a frequently-run command such as `git checkout` can be abbreviated to `gco`. After entering `gco` and pressing @key{Space} or @key{Enter}, the full text `git checkout` will appear in the command line. + +Abbreviations are stored, by default, in a universal variable. + +The following parameters are available: + +- `-a 'WORD=PHRASE'` or `--add 'WORD=PHRASE'` Adds a new abbreviation, where WORD will be expanded to PHRASE. Only a single argument is supported - protect the contents with quotes or escapes. + +- `-s` or `--show` Show all abbreviated words and their expanded phrases in a manner suitable for export and import. + +- `-l` or `--list` Lists all abbreviated words. + +- `-r WORD` or `--remove WORD` Remove the abbreviation WORD. + +\subsection abbr-example Examples + +\fish +abbr -a 'gco=git checkout' +\endfish +Add a new abbreviation where `gco` will be replaced with `git checkout`. + +\fish +abbr -r gco +\endfish +Remove the `gco` abbreviation. + +\fish +ssh another_host abbr -s | source +\endfish +Import the abbreviations defined on another_host over SSH. diff --git a/share/completions/abbr.fish b/share/completions/abbr.fish new file mode 100644 index 000000000..6671ecd68 --- /dev/null +++ b/share/completions/abbr.fish @@ -0,0 +1,5 @@ +complete -c abbr -f -s a -l add -d 'Add abbreviation' +complete -c abbr -s r -l remove -d 'Remove abbreviation' -xa '(abbr -l)' +complete -c abbr -f -s s -l show -d 'Print all abbreviations' +complete -c abbr -f -s l -l list -d 'Print all abbreviation names' +complete -c abbr -f -s h -l help -d 'Help' diff --git a/share/functions/abbr.fish b/share/functions/abbr.fish new file mode 100644 index 000000000..1be1ff074 --- /dev/null +++ b/share/functions/abbr.fish @@ -0,0 +1,68 @@ +function abbr --description "Manage abbreviations" + if test (count $argv) -lt 1 -o (count $argv) -gt 2 + printf ( _ "%s: Expected one or two arguments, got %d\n") abbr (count $argv) + __fish_print_help abbr + return 1 + end + + switch $argv[1] + case '-a' '--add' + if __fish_abbr_get_by_key "$argv[2]" >/dev/null + printf ( _ "%s: abbreviation %s already exists\n" ) abbr (__fish_abbr_print_key "$argv[2]" ) + return 2 + end + set -U fish_user_abbreviations $fish_user_abbreviations "$argv[2]" + return 0 + + case '-r' '--remove' + set -l index (__fish_abbr_get_by_key "$argv[2]") + if test $index -gt 0 + set -e fish_user_abbreviations[$index] + return 0 + else + printf ( _ "%s: no such abbreviation %s\n" ) abbr (__fish_abbr_print_key "$argv[2]" ) + return 3 + end + + case '-s' '--show' + for i in $fish_user_abbreviations + echo abbr -a \'$i\' + end + return 0 + + case '-l' '--list' + for i in $fish_user_abbreviations + __fish_abbr_print_key $i + end + return 0 + + case '-h' '--help' + __fish_print_help abbr + return 0 + + case '' '*' + printf (_ "%s: Unknown option %s\n" ) abbr $argv[1] + __fish_print_help abbr + return 1 + end +end + +function __fish_abbr_printable + echo (__fish_abbr_print_key $argv)'="'(echo $argv | cut -f 2 -d =)'"' +end + +function __fish_abbr_get_by_key + for i in (seq (count $fish_user_abbreviations)) + switch $fish_user_abbreviations[$i] + case (__fish_abbr_print_key $argv)'=*' + echo $i + return 0 + end + end + echo 0 + return 1 +end + +function __fish_abbr_print_key + echo $argv| cut -f 1 -d = +end