Skip to content

Commit

Permalink
feat(init,add,erase,rename): use abbr=expansion syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
olets committed Mar 1, 2020
1 parent ee4d6a8 commit 455dc75
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 46 deletions.
31 changes: 18 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ Default is user.
#### Add

```shell
abbr [(--add [(--global | -g)] | -a [(--global | -g)] )] ABBREVIATION EXPANSION
abbr [(--add [(--global | -g)] | -a [(--global | -g)] )] ABBREVIATION=EXPANSION
```

Add a new abbreviation.
Expand All @@ -168,30 +168,35 @@ To add a session abbreviation, use the **--session** flag. Otherwise, or if the
To add a global abbreviation, use the **--global** flag. Otherwise the new abbreviation will be a command abbreviation.

```shell
% abbr --add gcm git checkout master
% abbr --add gcm='git checkout master'
% gcm[Space] # expands as git checkout master
% gcm[Enter] # expands and accepts git checkout master
```

The following are equivalent:

```shell
% abbr --add --user gcm git checkout master
% abbr -a --user gcm git checkout master
% abbr --user gcm git checkout master
% abbr --add -U gcm git checkout master
% abbr -a -U gcm git checkout master
% abbr -U gcm git checkout master
% abbr gcm git checkout master
% abbr --add --user gcm='git checkout master'
% abbr -a --user gcm='git checkout master'
% abbr --user gcm='git checkout master'
% abbr --add -U gcm='git checkout master'
% abbr -a -U gcm='git checkout master'
% abbr -U gcm='git checkout master'
% abbr gcm='git checkout master'
```

A `--` may optionally follow the last option.
The following are not allowed in the abbreviation: `;`, `|`, `&&`, `=`, and whitespace.

```shell
abbr a\;b=c # will error
abbr 'a||b'=c # will error
```

The following are not allowed in the abbreviation: `;`, `|`, `&&`, and whitespace.
As with aliases, to include whitespace, quotation marks, or other special characters like `;`, `|`, or `&` in the EXPANSION, quote the EXPANSION or `\`-escape the characters as necessary.
```shell
abbr a\;b c # will error
abbr 'a||b' c # will error
abbr a=b\;c # allowed
abbr a="b|c" # allowed
```
Abbreviations can also be manually added to the `ZSH_USER_ABBREVIATIONS_PATH`.
Expand Down
8 changes: 4 additions & 4 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ Zsh `abbr`, not the zsh port of fish `abbr`
- [x] in `--erase`
- [x] in `--rename`
- [x] in user file, no `-a -U`
- [ ] in user file, no `--`
- [ ] `add` syntax is `abbreviation='word'`/`abbreviation="word"`
- [ ] confirm that quotes can be included by escaping them (`abbreviation="the \"full\" \'word\'"`)
- [x] in user file, no `--`
- [x] `add` syntax is `abbreviation='word'`/`abbreviation="word"`
- [x] confirm that quotes can be included by escaping them (`abbreviation="the \"full\" \'word\'"`)
- [ ] support importing from fish / migrating from <3.x

### Other
Expand Down Expand Up @@ -63,6 +63,6 @@ Chrome
More idiomatic zsh

- [ ] don't use quotation marks when not needed
- [ ] use (( ${+var} )) instead of [[ -n "$var" ]]
- [ ] 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
71 changes: 42 additions & 29 deletions zsh-abbr.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ _zsh_abbr() {
${text_bold}Options${text_reset}
The following options are available:
o --add ABBREVIATION EXPANSION or -a ABBREVIATION EXPANSION Adds a new
o --add ABBREVIATION=EXPANSION or -a ABBREVIATION=EXPANSION Adds a new
abbreviation, causing ABBREVIATION to be expanded to EXPANSION.
o --clear-session or -E Erases all session abbreviations.
Expand Down Expand Up @@ -130,21 +130,21 @@ _zsh_abbr() {
See the 'Internals' section for more on them.
${text_bold}Examples${text_reset}
${text_bold}abbr${text_reset} -a -g gco git checkout
${text_bold}abbr${text_reset} --add --session gco git checkout
${text_bold}abbr${text_reset} -a -g gco=\"git checkout\"
${text_bold}abbr${text_reset} --add --session gco=\"git checkout\"
Add a new abbreviation where gco will be replaced with git checkout
session to the current shell. This abbreviation will not be
automatically visible to other shells unless the same command is run
in those shells.
${text_bold}abbr${text_reset} -- g- git checkout -
${text_bold}abbr${text_reset} -- g-=\"git checkout -\"
If the EXPANSION includes a hyphen (-), the --add command\'s
entire EXPANSION must be quoted.
${text_bold}abbr${text_reset} -a l less
${text_bold}abbr${text_reset} --add l less
${text_bold}abbr${text_reset} -a l=less
${text_bold}abbr${text_reset} --add l=less
Add a new abbreviation where l will be replaced with less user so
all shells. Note that you omit the -U since it is the default.
Expand Down Expand Up @@ -208,12 +208,23 @@ _zsh_abbr() {
version="zsh-abbr version 2.1.3"

function add() {
if [[ $# -lt 2 ]]; then
util_error " add: Requires at least two arguments"
local abbreviation
local expansion

if [[ $# -gt 1 ]]; then
util_error " add: Expected one argument, got $*"
return
fi

abbreviation="${1%%=*}"
expansion="${1#*=}"

if [[ -z $abbreviation || -z $expansion || $abbreviation == $1 ]]; then
util_error " add: Requires abbreviation and expansion"
return
fi

util_add $* # must not be quoted
util_add $abbreviation $expansion
}

function clear_session() {
Expand Down Expand Up @@ -444,15 +455,18 @@ _zsh_abbr() {
local expansion
local success=false

abbreviation="$1"
shift
expansion="$*"
abbreviation=$1
expansion=$2

if [[ $abbreviation != $(_zsh_abbr_last_word $abbreviation) ]]; then
util_error " add: ABBREVIATION ('$abbreviation') may not contain delimiting prefixes"
return
fi

if [[ ${abbreviation%=*} != $abbreviation ]]; then
util_error " add: ABBREVIATION ('$abbreviation') may not contain an equals sign"
fi

if $opt_session; then
if $opt_global; then
if ! (( ${+ZSH_ABBR_SESSION_GLOBALS[$1]} )); then
Expand Down Expand Up @@ -512,7 +526,7 @@ _zsh_abbr() {
function util_sync_user() {
local user_updated

if [ "$ZSH_ABBR_SYNC_USER" = false ]; then
if [[ -n "$ZSH_ABBR_NO_SYNC_USER" ]]; then
return
fi

Expand All @@ -525,11 +539,11 @@ _zsh_abbr() {
chmod 600 "$user_updated"

for abbreviation expansion in ${(kv)ZSH_ABBR_USER_COMMANDS}; do
echo "abbr -- $abbreviation $expansion" >> "$user_updated"
echo "abbr ${abbreviation}=\"$expansion\"" >> "$user_updated"
done

for abbreviation expansion in ${(kv)ZSH_ABBR_USER_GLOBALS}; do
echo "abbr -g -- $abbreviation $expansion" >> "$user_updated"
echo "abbr -g ${abbreviation}=\"$expansion\"" >> "$user_updated"
done

mv "$user_updated" "$ZSH_ABBR_USER_PATH"
Expand Down Expand Up @@ -785,10 +799,10 @@ _zsh_abbr_global_expansion() {

_zsh_abbr_init() {
local line
local shwordsplit_off
shwordsplit_off=false
local session_shwordsplit_on

ZSH_ABBR_SYNC_USER=false
session_shwordsplit_on=false
ZSH_ABBR_NO_SYNC_USER=true

typeset -gA ZSH_ABBR_USER_COMMANDS
typeset -gA ZSH_ABBR_SESSION_COMMANDS
Expand All @@ -799,8 +813,8 @@ _zsh_abbr_init() {
ZSH_ABBR_USER_GLOBALS=()
ZSH_ABBR_SESSION_GLOBALS=()

if [[ $options[shwordsplit] = off ]]; then
shwordsplit_off=true
if [[ $options[shwordsplit] = on ]]; then
session_shwordsplit_on=true
fi

# Scratch files
Expand All @@ -814,21 +828,20 @@ _zsh_abbr_init() {

# Load saved user abbreviations
if [ -f "$ZSH_ABBR_USER_PATH" ]; then
setopt shwordsplit
while read -r line; do
$line
done < $ZSH_ABBR_USER_PATH
unset ZSH_ABBR_SYNC_USER

# reset if necessary
if [ $shwordsplit_off = true ]; then
unsetopt shwordsplit
unsetopt shwordsplit

source "$ZSH_ABBR_USER_PATH"

if $session_shwordsplit_on; then
setopt shwordsplit
fi
else
mkdir -p $(dirname "$ZSH_ABBR_USER_PATH")
touch "$ZSH_ABBR_USER_PATH"
fi

unset ZSH_ABBR_NO_SYNC_USER

typeset -p ZSH_ABBR_USER_COMMANDS > "${TMPDIR:-/tmp}/zsh-user-abbreviations"
typeset -p ZSH_ABBR_USER_GLOBALS > "${TMPDIR:-/tmp}/zsh-user-global-abbreviations"
}
Expand Down

0 comments on commit 455dc75

Please sign in to comment.