Skip to content

Commit

Permalink
feat(word): delimit by '&&', '|', ';', and whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
olets committed Feb 3, 2020
1 parent 1085a29 commit 44b3740
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 32 deletions.
31 changes: 4 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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.
Expand Down
27 changes: 22 additions & 5 deletions zsh-abbr.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -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"}"

Expand Down Expand Up @@ -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="$*"

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 44b3740

Please sign in to comment.