Skip to content

Commit

Permalink
feat(expansion): shell grammar word splitting determines current word
Browse files Browse the repository at this point in the history
  • Loading branch information
olets committed Mar 1, 2020
1 parent dbe9ec1 commit a3c00f5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
1 change: 0 additions & 1 deletion ROADMAP.md
Expand Up @@ -83,4 +83,3 @@ More idiomatic zsh
- [ ] rework variable values to support using (( ${+var} )) instead of [[ -n "$var" ]]
- [ ] no `if [[ $var == true ]]` where just `if $var` would work
- [ ] any other places to tighten up boolean checks
- [ ] use ${(w)var: -1} for last word if it gives the same result
23 changes: 11 additions & 12 deletions zsh-abbr.zsh
Expand Up @@ -479,6 +479,7 @@ _zsh_abbr() {

function util_add() {
local abbreviation
local abbreviation_last_word
local expansion
local quote
local success=false
Expand All @@ -492,10 +493,8 @@ _zsh_abbr() {
expansion="${expansion:1:-1}"
fi

if [[ $abbreviation != $(_zsh_abbr_last_word $abbreviation) ]]; then
echo - $abbreviation
echo - $(_zsh_abbr_last_word $abbreviation)
util_error " add: ABBREVIATION ('$abbreviation') may not contain delimiting prefixes"
if [[ ${(w)#abbreviation} > 1 ]]; then
util_error " add: ABBREVIATION ('$abbreviation') must be only one word"
return
fi

Expand Down Expand Up @@ -914,26 +913,26 @@ _zsh_abbr_init() {
typeset -p ZSH_ABBR_USER_GLOBALS > "${TMPDIR:-/tmp}/zsh-user-global-abbreviations"
}

_zsh_abbr_last_word() {
# delimited by `&&`, `|`, `;`, and whitespace
echo - ${${1//*(\&\&|[;\|[:IFSSPACE:]])}}
}


# WIDGETS
# -------

_zsh_abbr_expand_widget() {
local current_word
local expansion
local word_count

current_word=$(_zsh_abbr_last_word "$LBUFFER")
current_word=$LBUFFER
word_count=${(w)#LBUFFER}

if [[ "$current_word" == "$LBUFFER" ]]; then
if [[ $word_count == 1 ]]; then
expansion=$(_zsh_abbr_cmd_expansion "$current_word")
fi

if ! [[ -n "$expansion" ]]; then
if [[ $word_count > 1 ]]; then
current_word=${${(z)LBUFFER}: -1}
fi

expansion=$(_zsh_abbr_global_expansion "$current_word")
fi

Expand Down

0 comments on commit a3c00f5

Please sign in to comment.