diff --git a/README.md b/README.md index 5183352d..a0a5e6eb 100644 --- a/README.md +++ b/README.md @@ -177,12 +177,13 @@ The following are equivalent: % abbr gcm git checkout master ``` -See [Delimiters](#delimiters) for details on what characters are legal in the ABBREVIATION. +A `--` may optionally follow the last option. -To include a hyphen (-) in an EXPANSION, wrap the EXPANSION in double quotation marks: +The following are not allowed in the abbreviation: `;`, `||`, `&&`, and whitespace. ```shell -abbr g- "git checkout -" +abbr a\;b c # will error +abbr 'a||b' c # will error ``` Abbreviations can also be manually added to the `ZSH_ABBR_UNIVERSALS_FILE`. @@ -408,30 +409,6 @@ bindkey "^A" _zsh_abbr_expand_space # load zsh-abbr ``` -### Delimiters - -By default, IFS whitespace and the characters comma (`,`), semicolon (`;`), pipe (`|`), ampersand (`&`) are ABBREVIATION-delimiting prefixes. That means that given the abbreviation `m my abbreviation` the following will expand: - -```shell -% x,m[Space] -% x;m[Space] -% x|m[Space] -% x&m[Space] -% x[Space]m[Space] -% x[Return] -m[Space] -% x[Tab]m[Space] -``` - -Accordingly, those characters are not legal in ABBREVIATIONs: - -```shell -% abbr g\; test -% g;[Space] # does not expand -``` - -If you want to customize the delimiting prefixes, set `ZSH_ABBR_DELIMITING_PREFIXES` to you custom match string in your `.zshrc` (can be before or after loading zsh-abbr; default is `'',;|&[:IFSSPACE:]'`). - ## Uninstalling Delete the zsh-abbr configuration directory. Note that this will permanently delete the universal abbreviations file. diff --git a/zsh-abbr.zsh b/zsh-abbr.zsh index 629fab69..5f18105b 100755 --- a/zsh-abbr.zsh +++ b/zsh-abbr.zsh @@ -11,10 +11,6 @@ # add CTRL for normal SPACE/ENTER; in incremental search mode expand on CTRL+SPACE) ZSH_ABBR_DEFAULT_BINDINGS="${ZSH_ABBR_DEFAULT_BINDINGS=true}" -# The non-whitespace characters which are considered a word break for purposes -# of recognizing abbreviations. -ZSH_ABBR_DELIMITING_PREFIXES="${ZSH_ABBR_DELIMITING_PREFIXES=',;|&[:IFSSPACE:]'}" - # File abbreviations are stored in ZSH_ABBR_UNIVERSALS_FILE="${ZSH_ABBR_UNIVERSALS_FILE="${HOME}/.config/zsh/universal-abbreviations"}" @@ -381,6 +377,12 @@ _zsh_abbr() { local abbreviation local expansion abbreviation="$1" + + if [ $(util_contains_delimiters $abbreviation) = true ]; then + util_error " add: ABBREVIATION may not contain delimiting prefixes" + return + fi + shift expansion="$*" @@ -409,6 +411,16 @@ _zsh_abbr() { util_error ": Illegal combination of options" } + function util_contains_delimiters() { + local contains + contains=false + if [[ ${=1} != $(_zsh_abbr_last_word $1) ]]; then + contains=true + fi + + echo "$contains" + } + function util_error() { printf "abbr%s\\nFor help run abbr --help\\n" "$@" should_exit=true @@ -662,6 +674,11 @@ _zsh_abbr_bind_widgets() { bindkey "^M" _zsh_abbr_expand_accept } +_zsh_abbr_last_word() { + # delimited by `&&`, `|`, `;`, and whitespace + echo ${${1//*(\&\&|[;\|[:IFSSPACE:]])}} +} + _zsh_abbr_expand_accept() { zle _zsh_abbr_expand_widget zle autosuggest-clear # if using zsh-autosuggestions, clear any suggestion @@ -717,7 +734,7 @@ _zsh_abbr_expand_widget() { local current_word local expansion - current_word="${LBUFFER/*[$ZSH_ABBR_DELIMITING_PREFIXES]/}" + current_word=$(_zsh_abbr_last_word "$LBUFFER") expansion=$(_zsh_abbr_expansion "$current_word") if [[ -n "$expansion" ]]; then