changed directories and addet fzf to zsh
This commit is contained in:
parent
0acd581d40
commit
cb928bb0b4
428 changed files with 20659 additions and 15 deletions
10
.zsh/plugins/autopairs/.editorconfig
Normal file
10
.zsh/plugins/autopairs/.editorconfig
Normal file
|
@ -0,0 +1,10 @@
|
|||
[*]
|
||||
charset = utf-8
|
||||
end_of_line = lf
|
||||
insert_final_newline = true
|
||||
trim_trailing_whitespace = true
|
||||
indent_style = space
|
||||
indent_size = 4
|
||||
|
||||
[*.md]
|
||||
indent_style = space
|
35
.zsh/plugins/autopairs/.travis.yml
Normal file
35
.zsh/plugins/autopairs/.travis.yml
Normal file
|
@ -0,0 +1,35 @@
|
|||
dist: trusty
|
||||
sudo: required
|
||||
|
||||
language: generic
|
||||
|
||||
env:
|
||||
- ZSH_VERSION=5.3.1 URL=https://downloads.sourceforge.net/project/zsh/zsh/5.3.1/zsh-5.3.1.tar.xz
|
||||
- ZSH_VERSION=5.2 URL=https://downloads.sourceforge.net/project/zsh/zsh/5.2/zsh-5.2.tar.xz
|
||||
- ZSH_VERSION=5.1.1 URL=https://downloads.sourceforge.net/project/zsh/zsh/5.1.1/zsh-5.1.1.tar.xz
|
||||
- ZSH_VERSION=5.0.8 URL=https://downloads.sourceforge.net/project/zsh/zsh/5.0.8/zsh-5.0.8.tar.gz
|
||||
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- build-essential
|
||||
|
||||
before_install:
|
||||
- export LOCAL="$(mktemp --directory --tmpdir=${TMPDIR:/tmp} local.bin.XXXXXX)"
|
||||
- wget $URL
|
||||
- tar -xf zsh-$ZSH_VERSION.tar.*
|
||||
- cd zsh-$ZSH_VERSION
|
||||
- ./configure --prefix=$LOCAL
|
||||
- make
|
||||
- make install
|
||||
- cd -
|
||||
- export PATH="$LOCAL/bin:$HOME/bin:$PATH"
|
||||
|
||||
before_script:
|
||||
- mkdir -p ~/bin
|
||||
- curl -L https://github.com/zunit-zsh/zunit/releases/download/v0.8.1/zunit > ~/bin/zunit
|
||||
- curl -L https://raw.githubusercontent.com/molovo/revolver/master/revolver > ~/bin/revolver
|
||||
- chmod u+x ~/bin/{revolver,zunit}
|
||||
|
||||
script:
|
||||
- zunit --verbose
|
8
.zsh/plugins/autopairs/.zunit.yml
Normal file
8
.zsh/plugins/autopairs/.zunit.yml
Normal file
|
@ -0,0 +1,8 @@
|
|||
tap: false
|
||||
directories:
|
||||
tests: tests
|
||||
output: tests/_output
|
||||
support: tests/_support
|
||||
time_limit: 0
|
||||
fail_fast: false
|
||||
allow_risky: false
|
22
.zsh/plugins/autopairs/LICENSE
Normal file
22
.zsh/plugins/autopairs/LICENSE
Normal file
|
@ -0,0 +1,22 @@
|
|||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016-24 Henrik Lissner.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
||||
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
249
.zsh/plugins/autopairs/README.md
Normal file
249
.zsh/plugins/autopairs/README.md
Normal file
|
@ -0,0 +1,249 @@
|
|||
[](https://github.com/hlissner/doom-emacs)
|
||||
[](./LICENSE)
|
||||

|
||||
[](https://travis-ci.org/hlissner/zsh-autopair)
|
||||
|
||||
# zsh-autopair
|
||||
A simple plugin that auto-closes, deletes and skips over matching delimiters in
|
||||
zsh intelligently. Hopefully.
|
||||
|
||||
> NOTE: zsh-autopair is untested for versions of Zsh below 5.0.2. Please report
|
||||
> any issues you have in earlier versions!
|
||||
|
||||
Specifically, zsh-autopair does 5 things for you:
|
||||
|
||||
1. It inserts matching pairs (by default, that means brackets, quotes and
|
||||
spaces):
|
||||
|
||||
e.g. `echo |` => <kbd>"</kbd> => `echo "|"`
|
||||
|
||||
2. It skips over matched pairs:
|
||||
|
||||
e.g. `cat ./*.{py,rb|}` => <kbd>}</kbd> => `cat ./*.{py,rb}|`
|
||||
|
||||
3. It auto-deletes pairs on backspace:
|
||||
|
||||
e.g. `git commit -m "|"` => <kbd>backspace</kbd> => `git commit -m |`
|
||||
|
||||
4. And does all of the above only when it makes sense to do so. e.g. when the
|
||||
pair is balanced and when the cursor isn't next to a boundary character:
|
||||
|
||||
e.g. `echo "|""` => <kbd>backspace</kbd> => `echo |""` (doesn't aggressively eat up too many quotes)
|
||||
|
||||
5. Spaces between brackets are expanded and contracted.
|
||||
|
||||
e.g. `echo [|]` => <kbd>space</kbd> => `echo [ | ]` => <kbd>backspace</kbd> => `echo [|]`
|
||||
|
||||
|
||||
<!-- markdown-toc start - Don't edit this section. Run M-x markdown-toc-refresh-toc -->
|
||||
**Table of Contents**
|
||||
|
||||
- [Install](#install)
|
||||
- [Antigen](#antigen)
|
||||
- [zgen](#zgen)
|
||||
- [zplug](#zplug)
|
||||
- [Hombrew](#homebrew)
|
||||
- [Oh My Zsh](#oh-my-zsh)
|
||||
- [Configuration](#configuration)
|
||||
- [Adding/Removing pairs](#addingremoving-pairs)
|
||||
- [Troubleshooting & compatibility issues](#troubleshooting--compatibility-issues)
|
||||
- [zgen & prezto compatibility](#zgen--prezto-compatibility)
|
||||
- [text on right-side of cursor interfere with completion](#text-on-right-side-of-cursor-interfere-with-completion)
|
||||
- [zsh-autopair & isearch?](#zsh-autopair--isearch)
|
||||
- [Midnight Commander](#midnight-commander)
|
||||
- [Other resources](#other-resources)
|
||||
|
||||
<!-- markdown-toc end -->
|
||||
|
||||
## Install
|
||||
Download and source `autopair.zsh`
|
||||
|
||||
```bash
|
||||
if [[ ! -d ~/.zsh-autopair ]]; then
|
||||
git clone https://github.com/hlissner/zsh-autopair ~/.zsh-autopair
|
||||
fi
|
||||
|
||||
source ~/.zsh-autopair/autopair.zsh
|
||||
autopair-init
|
||||
```
|
||||
|
||||
### Hoembrew
|
||||
`brew install zsh-autopair`
|
||||
|
||||
``` bash
|
||||
# Add to .zshrc
|
||||
source $HOMEBREW_PREFIX/share/zsh-autopair/autopair.zsh
|
||||
```
|
||||
|
||||
### Antigen
|
||||
`antigen bundle hlissner/zsh-autopair`
|
||||
|
||||
### zgen
|
||||
```bash
|
||||
if ! zgen saved; then
|
||||
echo "Creating a zgen save"
|
||||
|
||||
# ... other plugins
|
||||
zgen load hlissner/zsh-autopair
|
||||
|
||||
zgen save
|
||||
fi
|
||||
```
|
||||
|
||||
### zplug
|
||||
Load autopair _after compinit_, otherwise, the plugin won't work.
|
||||
```bash
|
||||
zplug "hlissner/zsh-autopair", defer:2
|
||||
```
|
||||
|
||||
### Homebrew
|
||||
For Homebrew users, you can install it through the following command
|
||||
```shell
|
||||
brew install zsh-autopair
|
||||
```
|
||||
Then source it in your `.zshrc`
|
||||
```shell
|
||||
source $(brew --prefix)/share/zsh-autopair/autopair.zsh
|
||||
```
|
||||
|
||||
### Oh My Zsh
|
||||
1. Clone this repository into `$ZSH_CUSTOM/plugins` (by default `~/.oh-my-zsh/custom/plugins`)
|
||||
|
||||
```sh
|
||||
git clone https://github.com/hlissner/zsh-autopair ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autopair
|
||||
```
|
||||
|
||||
2. Add the plugin to the list of plugins for Oh My Zsh to load (inside `~/.zshrc`):
|
||||
|
||||
```sh
|
||||
plugins=(
|
||||
# other plugins...
|
||||
zsh-autopair
|
||||
)
|
||||
```
|
||||
|
||||
3. Start a new terminal session.
|
||||
|
||||
## Configuration
|
||||
zsh-autopair sets itself up. You can prevent this by setting
|
||||
`AUTOPAIR_INHIBIT_INIT`.
|
||||
|
||||
**Options:**
|
||||
* `AUTOPAIR_BETWEEN_WHITESPACE` (default: blank): if set, regardless of whether
|
||||
delimiters are unbalanced or do not meet a boundary check, pairs will be
|
||||
auto-closed if surrounded by whitespace, BOL or EOL.
|
||||
* `AUTOPAIR_INHIBIT_INIT` (default: blank): if set, autopair will not
|
||||
automatically set up keybinds. [Check out the initialization
|
||||
code](autopair.zsh#L118) if you want to know what it does.
|
||||
* `AUTOPAIR_PAIRS` (default: ``('`' '`' "'" "'" '"' '"' '{' '}' '[' ']' '(' ')'
|
||||
' ' ' ')``): An associative array that map pairs. Only one-character pairs are
|
||||
supported. To modify this, see the "Adding/Removing pairs" section.
|
||||
* `AUTOPAIR_LBOUNDS`/`AUTOPAIR_RBOUNDS` (default: see below): Associative lists
|
||||
of regex character groups dictating the 'boundaries' for autopairing depending
|
||||
on the delimiter. These are their default values:
|
||||
|
||||
```bash
|
||||
AUTOPAIR_LBOUNDS=(all '[.:/\!]')
|
||||
AUTOPAIR_LBOUNDS+=(quotes '[]})a-zA-Z0-9]')
|
||||
AUTOPAIR_LBOUNDS+=(spaces '[^{([]')
|
||||
AUTOPAIR_LBOUNDS+=(braces '')
|
||||
AUTOPAIR_LBOUNDS+=('`' '`')
|
||||
AUTOPAIR_LBOUNDS+=('"' '"')
|
||||
AUTOPAIR_LBOUNDS+=("'" "'")
|
||||
|
||||
AUTOPAIR_RBOUNDS=(all '[[{(<,.:?/%$!a-zA-Z0-9]')
|
||||
AUTOPAIR_RBOUNDS+=(quotes '[a-zA-Z0-9]')
|
||||
AUTOPAIR_RBOUNDS+=(spaces '[^]})]')
|
||||
AUTOPAIR_RBOUNDS+=(braces '')
|
||||
```
|
||||
|
||||
For example, if `$AUTOPAIR_LBOUNDS[braces]="[a-zA-Z]"`, then braces (`{([`) won't be
|
||||
autopaired if the cursor follows an alphabetical character.
|
||||
|
||||
Individual delimiters can be used too. Setting `$AUTOPAIR_RBOUNDS['{']="[0-9]"` will
|
||||
cause <kbd>{</kbd> specifically to not be autopaired when the cursor precedes a number.
|
||||
|
||||
### Adding/Removing pairs
|
||||
You can change the designated pairs in zsh-autopair by modifying the
|
||||
`AUTOPAIR_PAIRS` envvar. This can be done _before_ initialization like so:
|
||||
|
||||
``` sh
|
||||
typeset -gA AUTOPAIR_PAIRS
|
||||
AUTOPAIR_PAIRS+=("<" ">")
|
||||
```
|
||||
|
||||
Or after initialization; however, you'll have to bind keys to `autopair-insert`
|
||||
manually:
|
||||
|
||||
```sh
|
||||
AUTOPAIR_PAIRS+=("<" ">")
|
||||
bindkey "<" autopair-insert
|
||||
# prevents breakage in isearch
|
||||
bindkey -M isearch "<" self-insert
|
||||
```
|
||||
|
||||
To _remove_ pairs, use `unset 'AUTOPAIR_PAIRS[<]'`. Unbinding is optional.
|
||||
|
||||
## Troubleshooting & compatibility issues
|
||||
### zgen & prezto compatibility
|
||||
Prezto's Editor module is known to reset autopair's bindings. A workaround is to
|
||||
_defer autopair from initializing_ (by setting `AUTOPAIR_INHIBIT_INIT=1`) and
|
||||
initialize it manually (by calling `autopair-init`):
|
||||
|
||||
``` sh
|
||||
source "$HOME/.zgen/zgen.zsh"
|
||||
|
||||
# Add this
|
||||
AUTOPAIR_INHIBIT_INIT=1
|
||||
|
||||
if ! zgen saved; then
|
||||
zgen prezto
|
||||
# ...
|
||||
zgen load hlissner/zsh-autopair 'autopair.zsh'
|
||||
#...
|
||||
zgen save
|
||||
fi
|
||||
|
||||
# And this
|
||||
autopair-init
|
||||
```
|
||||
|
||||
### text on right-side of cursor interfere with completion
|
||||
Bind <kbd>Tab</kbd> to `expand-or-complete-prefix` and completion will ignore
|
||||
what's to the right of cursor:
|
||||
|
||||
`bindkey '^I' expand-or-complete-prefix`
|
||||
|
||||
This has the unfortunate side-effect of overwriting whatever's right of the
|
||||
cursor, however.
|
||||
|
||||
### zsh-autopair & isearch?
|
||||
zsh-autopair silently disables itself in isearch, as the two are incompatible.
|
||||
|
||||
### Midnight Commander
|
||||
MC hangs when zsh-autopair tries to bind the space key. This also breaks the MC
|
||||
subshell.
|
||||
|
||||
Disable space expansion to work around this: `unset 'AUTOPAIR_PAIRS[ ]'`
|
||||
|
||||
## Other resources
|
||||
* Works wonderfully with [zsh-syntax-highlight] and
|
||||
`ZSH_HIGHLIGHT_HIGHLIGHTERS+=brackets`, but zsh-syntax-highlight must be
|
||||
loaded *after* zsh-autopair.
|
||||
* Mixes well with these vi-mode zsh modules: [surround], [select-quoted], and
|
||||
[select-bracketed] (they're built into zsh as of zsh-5.0.8)
|
||||
* Other relevant repositories of mine:
|
||||
+ [dotfiles]
|
||||
+ [emacs.d]
|
||||
+ [vimrc]
|
||||
+ [zshrc]
|
||||
|
||||
|
||||
[dotfiles]: https://github.com/hlissner/dotfiles
|
||||
[vimrc]: https://github.com/hlissner/.vim
|
||||
[emacs.d]: https://github.com/hlissner/doom-emacs
|
||||
[zshrc]: https://github.com/hlissner/dotfiles/blob/master/config/zsh/.zshrc
|
||||
[zsh-syntax-highlighting]: https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters/pattern.md
|
||||
[surround]: https://github.com/zsh-users/zsh/blob/master/Functions/Zle/surround
|
||||
[select-quoted]: https://github.com/zsh-users/zsh/blob/master/Functions/Zle/select-quoted
|
||||
[select-bracketed]: https://github.com/zsh-users/zsh/blob/master/Functions/Zle/select-bracketed
|
4
.zsh/plugins/autopairs/autopair.plugin.zsh
Normal file
4
.zsh/plugins/autopairs/autopair.plugin.zsh
Normal file
|
@ -0,0 +1,4 @@
|
|||
# <https://github.com/zdharma-continuum/Zsh-100-Commits-Club/blob/master/Zsh-Plugin-Standard.adoc>
|
||||
0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
|
||||
0="${${(M)0:#/*}:-$PWD/$0}"
|
||||
source "${0:A:h}/autopair.zsh"
|
227
.zsh/plugins/autopairs/autopair.zsh
Normal file
227
.zsh/plugins/autopairs/autopair.zsh
Normal file
|
@ -0,0 +1,227 @@
|
|||
#!/usr/bin/env zsh
|
||||
|
||||
AUTOPAIR_INHIBIT_INIT=${AUTOPAIR_INHIBIT_INIT:-}
|
||||
AUTOPAIR_BETWEEN_WHITESPACE=${AUTOPAIR_BETWEEN_WHITESPACE:-}
|
||||
AUTOPAIR_SPC_WIDGET=${AUTOPAIR_SPC_WIDGET:-"$(bindkey " " | cut -c5-)"}
|
||||
AUTOPAIR_BKSPC_WIDGET=${AUTOPAIR_BKSPC_WIDGET:-"$(bindkey "^?" | cut -c6-)"}
|
||||
AUTOPAIR_DELWORD_WIDGET=${AUTOPAIR_DELWORD_WIDGET:-"$(bindkey "^w" | cut -c6-)"}
|
||||
|
||||
typeset -gA AUTOPAIR_PAIRS
|
||||
AUTOPAIR_PAIRS=('`' '`' "'" "'" '"' '"' '{' '}' '[' ']' '(' ')' ' ' ' ')
|
||||
|
||||
typeset -gA AUTOPAIR_LBOUNDS
|
||||
AUTOPAIR_LBOUNDS=(all '[.:/\!]')
|
||||
AUTOPAIR_LBOUNDS+=(quotes '[]})a-zA-Z0-9]')
|
||||
AUTOPAIR_LBOUNDS+=(spaces '[^{([]')
|
||||
AUTOPAIR_LBOUNDS+=(braces '')
|
||||
AUTOPAIR_LBOUNDS+=('`' '`')
|
||||
AUTOPAIR_LBOUNDS+=('"' '"')
|
||||
AUTOPAIR_LBOUNDS+=("'" "'")
|
||||
|
||||
typeset -gA AUTOPAIR_RBOUNDS
|
||||
AUTOPAIR_RBOUNDS=(all '[[{(<,.:?/%$!a-zA-Z0-9]')
|
||||
AUTOPAIR_RBOUNDS+=(quotes '[a-zA-Z0-9]')
|
||||
AUTOPAIR_RBOUNDS+=(spaces '[^]})]')
|
||||
AUTOPAIR_RBOUNDS+=(braces '')
|
||||
|
||||
|
||||
### Helpers ############################
|
||||
|
||||
# Returns the other pair for $1 (a char), blank otherwise
|
||||
_ap-get-pair() {
|
||||
if [[ -n $1 ]]; then
|
||||
echo ${AUTOPAIR_PAIRS[$1]}
|
||||
elif [[ -n $2 ]]; then
|
||||
local i
|
||||
for i in ${(@k)AUTOPAIR_PAIRS}; do
|
||||
[[ $2 == ${AUTOPAIR_PAIRS[$i]} ]] && echo $i && break
|
||||
done
|
||||
fi
|
||||
}
|
||||
|
||||
# Return 0 if cursor's surroundings match either regexp: $1 (left) or $2 (right)
|
||||
_ap-boundary-p() {
|
||||
[[ -n $1 && $LBUFFER =~ "$1$" ]] || [[ -n $2 && $RBUFFER =~ "^$2" ]]
|
||||
}
|
||||
|
||||
# Return 0 if the surrounding text matches any of the AUTOPAIR_*BOUNDS regexps
|
||||
_ap-next-to-boundary-p() {
|
||||
local -a groups
|
||||
groups=(all)
|
||||
case $1 in
|
||||
\'|\"|\`) groups+=quotes ;;
|
||||
\{|\[|\(|\<) groups+=braces ;;
|
||||
" ") groups+=spaces ;;
|
||||
esac
|
||||
groups+=$1
|
||||
local group
|
||||
for group in $groups; do
|
||||
_ap-boundary-p ${AUTOPAIR_LBOUNDS[$group]} ${AUTOPAIR_RBOUNDS[$group]} && return 0
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
# Return 0 if there are the same number of $1 as there are $2 (chars; a
|
||||
# delimiter pair) in the buffer.
|
||||
_ap-balanced-p() {
|
||||
local lbuf="${LBUFFER//\\$1}"
|
||||
local rbuf="${RBUFFER//\\$2}"
|
||||
local llen="${#lbuf//[^$1]}"
|
||||
local rlen="${#rbuf//[^$2]}"
|
||||
if (( rlen == 0 && llen == 0 )); then
|
||||
return 0
|
||||
elif [[ $1 == $2 ]]; then
|
||||
if [[ $1 == " " ]]; then
|
||||
# Silence WARN_CREATE_GLOBAL errors
|
||||
local match=
|
||||
local mbegin=
|
||||
local mend=
|
||||
# Balancing spaces is unnecessary. If there is at least one space on
|
||||
# either side of the cursor, it is considered balanced.
|
||||
[[ $LBUFFER =~ "[^'\"]([ ]+)$" && $RBUFFER =~ "^${match[1]}" ]] && return 0
|
||||
return 1
|
||||
elif (( llen == rlen || (llen + rlen) % 2 == 0 )); then
|
||||
return 0
|
||||
fi
|
||||
else
|
||||
local l2len="${#lbuf//[^$2]}"
|
||||
local r2len="${#rbuf//[^$1]}"
|
||||
local ltotal=$((llen - l2len))
|
||||
local rtotal=$((rlen - r2len))
|
||||
|
||||
(( ltotal < 0 )) && ltotal=0
|
||||
(( ltotal < rtotal )) && return 1
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
# Return 0 if the last keypress can be auto-paired.
|
||||
_ap-can-pair-p() {
|
||||
local rchar="$(_ap-get-pair $KEYS)"
|
||||
|
||||
[[ -n $rchar ]] || return 1
|
||||
|
||||
if [[ $rchar != " " ]]; then
|
||||
# Force pair if surrounded by space/[BE]OL, regardless of
|
||||
# boundaries/balance
|
||||
[[ -n $AUTOPAIR_BETWEEN_WHITESPACE && \
|
||||
$LBUFFER =~ "(^|[ ])$" && \
|
||||
$RBUFFER =~ "^($|[ ])" ]] && return 0
|
||||
|
||||
# Don't pair quotes if the delimiters are unbalanced
|
||||
! _ap-balanced-p $KEYS $rchar && return 1
|
||||
elif [[ $RBUFFER =~ "^[ ]*$" ]]; then
|
||||
# Don't pair spaces surrounded by whitespace
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Don't pair when in front of characters that likely signify the start of a
|
||||
# string, path or undesirable boundary.
|
||||
_ap-next-to-boundary-p $KEYS $rchar && return 1
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# Return 0 if the adjacent character (on the right) can be safely skipped over.
|
||||
_ap-can-skip-p() {
|
||||
if [[ -z $LBUFFER ]]; then
|
||||
return 1
|
||||
elif [[ $1 == $2 ]]; then
|
||||
if [[ $1 == " " ]]; then
|
||||
return 1
|
||||
elif ! _ap-balanced-p $1 $2; then
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
if ! [[ -n $2 && ${RBUFFER[1]} == $2 && ${LBUFFER[-1]} != '\' ]]; then
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# Return 0 if the adjacent character (on the right) can be safely deleted.
|
||||
_ap-can-delete-p() {
|
||||
local lchar="${LBUFFER[-1]}"
|
||||
local rchar="$(_ap-get-pair $lchar)"
|
||||
! [[ -n $rchar && ${RBUFFER[1]} == $rchar ]] && return 1
|
||||
if [[ $lchar == $rchar ]]; then
|
||||
if [[ $lchar == ' ' && ( $LBUFFER =~ "[^{([] +$" || $RBUFFER =~ "^ +[^]})]" ) ]]; then
|
||||
# Don't collapse spaces unless in delimiters
|
||||
return 1
|
||||
elif ! _ap-balanced-p $lchar $rchar; then
|
||||
return 1
|
||||
fi
|
||||
fi
|
||||
return 0
|
||||
}
|
||||
|
||||
# Insert $1 and add $2 after the cursor
|
||||
_ap-self-insert() {
|
||||
LBUFFER+=$1
|
||||
RBUFFER="$2$RBUFFER"
|
||||
}
|
||||
|
||||
|
||||
### Widgets ############################
|
||||
|
||||
autopair-insert() {
|
||||
local rchar="$(_ap-get-pair $KEYS)"
|
||||
if [[ $KEYS == (\'|\"|\`| ) ]] && _ap-can-skip-p $KEYS $rchar; then
|
||||
zle forward-char
|
||||
elif _ap-can-pair-p; then
|
||||
_ap-self-insert $KEYS $rchar
|
||||
elif [[ $rchar == " " ]]; then
|
||||
zle ${AUTOPAIR_SPC_WIDGET:-self-insert}
|
||||
else
|
||||
zle self-insert
|
||||
fi
|
||||
}
|
||||
|
||||
autopair-close() {
|
||||
if _ap-can-skip-p "$(_ap-get-pair "" $KEYS)" $KEYS; then
|
||||
zle forward-char
|
||||
else
|
||||
zle self-insert
|
||||
fi
|
||||
}
|
||||
|
||||
autopair-delete() {
|
||||
_ap-can-delete-p && RBUFFER=${RBUFFER:1}
|
||||
zle ${AUTOPAIR_BKSPC_WIDGET:-backward-delete-char}
|
||||
}
|
||||
|
||||
autopair-delete-word() {
|
||||
_ap-can-delete-p && RBUFFER=${RBUFFER:1}
|
||||
zle ${AUTOPAIR_DELWORD_WIDGET:-backward-delete-word}
|
||||
}
|
||||
|
||||
|
||||
### Initialization #####################
|
||||
|
||||
autopair-init() {
|
||||
zle -N autopair-insert
|
||||
zle -N autopair-close
|
||||
zle -N autopair-delete
|
||||
zle -N autopair-delete-word
|
||||
|
||||
local p
|
||||
for p in ${(@k)AUTOPAIR_PAIRS}; do
|
||||
bindkey "$p" autopair-insert
|
||||
bindkey -M isearch "$p" self-insert
|
||||
|
||||
local rchar="$(_ap-get-pair $p)"
|
||||
if [[ $p != $rchar ]]; then
|
||||
bindkey "$rchar" autopair-close
|
||||
bindkey -M isearch "$rchar" self-insert
|
||||
fi
|
||||
done
|
||||
|
||||
bindkey "^?" autopair-delete
|
||||
bindkey "^h" autopair-delete
|
||||
bindkey "^w" autopair-delete-word
|
||||
bindkey -M isearch "^?" backward-delete-char
|
||||
bindkey -M isearch "^h" backward-delete-char
|
||||
bindkey -M isearch "^w" backward-delete-word
|
||||
}
|
||||
[[ -n $AUTOPAIR_INHIBIT_INIT ]] || autopair-init
|
15
.zsh/plugins/autopairs/tests/_support/bootstrap
Normal file
15
.zsh/plugins/autopairs/tests/_support/bootstrap
Normal file
|
@ -0,0 +1,15 @@
|
|||
#!/usr/bin/env zsh
|
||||
source autopair.zsh
|
||||
export KEYS=
|
||||
export LBUFFER=
|
||||
export RBUFFER=
|
||||
|
||||
assert_true() {
|
||||
run $@
|
||||
assert $state equals 0
|
||||
}
|
||||
|
||||
assert_false() {
|
||||
run $@
|
||||
assert $state equals 1
|
||||
}
|
16
.zsh/plugins/autopairs/tests/balanced-p.zunit
Normal file
16
.zsh/plugins/autopairs/tests/balanced-p.zunit
Normal file
|
@ -0,0 +1,16 @@
|
|||
#!/usr/bin/env zunit
|
||||
@test 'basic boundary tests' {
|
||||
LBUFFER="abc"
|
||||
RBUFFER="123"
|
||||
assert_false _ap-boundary-p " " " "
|
||||
assert_true _ap-boundary-p "[^3]" "[^a]"
|
||||
assert_false _ap-boundary-p "[^c]" "[^1]"
|
||||
assert_true _ap-boundary-p "c" " "
|
||||
assert_true _ap-boundary-p " " "1"
|
||||
}
|
||||
|
||||
@test 'no boundary on blank line' {
|
||||
LBUFFER=
|
||||
RBUFFER=
|
||||
assert_false _ap-next-to-boundary-p "{"
|
||||
}
|
24
.zsh/plugins/autopairs/tests/can-delete-p.zunit
Normal file
24
.zsh/plugins/autopairs/tests/can-delete-p.zunit
Normal file
|
@ -0,0 +1,24 @@
|
|||
#!/usr/bin/env zunit
|
||||
@test 'delete if next to space and pair' {
|
||||
LBUFFER="[ " RBUFFER=" ]" assert_true _ap-can-delete-p
|
||||
}
|
||||
|
||||
@test 'delete if next to homogeneous counter-pair' {
|
||||
LBUFFER="'" RBUFFER="'" assert_true _ap-can-delete-p
|
||||
}
|
||||
|
||||
@test 'delete if next to heterogeneous counter-pair' {
|
||||
LBUFFER="(" RBUFFER=")" assert_true _ap-can-delete-p
|
||||
}
|
||||
|
||||
@test 'do not delete if at eol' {
|
||||
LBUFFER="'" assert_false _ap-can-delete-p
|
||||
}
|
||||
|
||||
@test 'do not delete if within too many spaces' {
|
||||
LBUFFER="[ " RBUFFER=" ]" assert_false _ap-can-delete-p
|
||||
}
|
||||
|
||||
@test 'do not delete if only next to space' {
|
||||
LBUFFER=" " RBUFFER=" " assert_false _ap-can-delete-p
|
||||
}
|
59
.zsh/plugins/autopairs/tests/can-pair-p.zunit
Normal file
59
.zsh/plugins/autopairs/tests/can-pair-p.zunit
Normal file
|
@ -0,0 +1,59 @@
|
|||
#!/usr/bin/env zunit
|
||||
@test 'pair if blank line' {
|
||||
KEYS="'" LBUFFER="" RBUFFER="" assert_true _ap-can-pair-p
|
||||
}
|
||||
|
||||
@test 'pair if next to balanced delimiter' { # {{|}}
|
||||
KEYS="{" LBUFFER="{" RBUFFER="}" assert_true _ap-can-pair-p
|
||||
}
|
||||
|
||||
@test 'pair space if next to brackets' { # { | }
|
||||
KEYS=" "
|
||||
LBUFFER="{" RBUFFER="}" assert_true _ap-can-pair-p
|
||||
LBUFFER="[" RBUFFER="]" assert_true _ap-can-pair-p
|
||||
LBUFFER="(" RBUFFER=")" assert_true _ap-can-pair-p
|
||||
}
|
||||
|
||||
@test 'pair brackets at the end of a word' { # abc{|}
|
||||
KEYS='{' LBUFFER="hello" assert_true _ap-can-pair-p
|
||||
}
|
||||
|
||||
@test 'do not pair brackets at the beginning of a word' { # {|abc
|
||||
KEYS='{' RBUFFER="hello" assert_false _ap-can-pair-p
|
||||
}
|
||||
|
||||
@test 'do not pair quotes next to a word' {
|
||||
KEYS="'"
|
||||
LBUFFER="hello" assert_false _ap-can-pair-p # abc"|
|
||||
RBUFFER="hello" assert_false _ap-can-pair-p # "|abc
|
||||
}
|
||||
|
||||
@test 'do not pair the same quotes from inside quotes' { # ""|"
|
||||
KEYS='"' LBUFFER='"' RBUFFER='"' assert_false _ap-can-pair-p
|
||||
}
|
||||
|
||||
@test 'do not pair if delimiter is invalid' {
|
||||
KEYS="<" LBUFFER="<" RBUFFER=">" assert_false _ap-can-pair-p
|
||||
}
|
||||
|
||||
@test 'do not pair if next to unbalanced delimiter' { # {|}
|
||||
KEYS="{" LBUFFER="" RBUFFER="}" assert_false _ap-can-pair-p
|
||||
}
|
||||
|
||||
@test 'do not pair if next to unbalanced delimiter after space' { # {| }
|
||||
AUTOPAIR_BETWEEN_WHITESPACE=
|
||||
KEYS="{" LBUFFER="" RBUFFER=" }" assert_false _ap-can-pair-p
|
||||
KEYS="{" LBUFFER="" RBUFFER=" }" assert_false _ap-can-pair-p
|
||||
KEYS="{" LBUFFER=" " RBUFFER=" }" assert_false _ap-can-pair-p
|
||||
}
|
||||
|
||||
@test 'do not pair space if next to non-brackets/spaces' {
|
||||
KEYS=" "
|
||||
LBUFFER="'" RBUFFER="'" assert_false _ap-can-pair-p # ' |'
|
||||
LBUFFER="abc" RBUFFER="xyz" assert_false _ap-can-pair-p # abc |xyz'
|
||||
LBUFFER="[ " RBUFFER=" ]" assert_false _ap-can-pair-p # [ | ]
|
||||
}
|
||||
|
||||
@test 'AUTOPAIR_BETWEEN_WHITESPACE=1' {
|
||||
AUTOPAIR_BETWEEN_WHITESPACE=1 KEYS='{' LBUFFER=" " RBUFFER=" }" assert_true _ap-can-pair-p
|
||||
}
|
25
.zsh/plugins/autopairs/tests/can-skip-p.zunit
Normal file
25
.zsh/plugins/autopairs/tests/can-skip-p.zunit
Normal file
|
@ -0,0 +1,25 @@
|
|||
#!/usr/bin/env zunit
|
||||
|
||||
@test 'fail skip-check on blank line' {
|
||||
LBUFFER='' RBUFFER='' assert_false _ap-can-skip-p '{' '}'
|
||||
}
|
||||
|
||||
@test 'fail skip-check on wrong pair' {
|
||||
LBUFFER='"' RBUFFER='"' assert_false _ap-can-skip-p '{' '}'
|
||||
}
|
||||
|
||||
@test 'skip if next to homogeneous counter-pair' {
|
||||
LBUFFER='"' RBUFFER='"' assert_true _ap-can-skip-p '"' '"'
|
||||
}
|
||||
|
||||
@test 'skip if next to heterogeneous counter-pair' {
|
||||
LBUFFER='{' RBUFFER='}' assert_true _ap-can-skip-p '{' '}'
|
||||
}
|
||||
|
||||
@test 'do not skip if next to unbalanced, homogeneous counter-pair' {
|
||||
LBUFFER='' RBUFFER='"' assert_false _ap-can-skip-p '"' '"'
|
||||
}
|
||||
|
||||
@test 'do not skip if next to unbalanced, heterogeneous counter-pair' {
|
||||
LBUFFER='' RBUFFER='}' assert_false _ap-can-skip-p '{' '}'
|
||||
}
|
26
.zsh/plugins/autopairs/tests/get-pair.zunit
Normal file
26
.zsh/plugins/autopairs/tests/get-pair.zunit
Normal file
|
@ -0,0 +1,26 @@
|
|||
#!/usr/bin/env zunit
|
||||
@test 'existing pair' {
|
||||
assert $(_ap-get-pair "{") same_as "}"
|
||||
}
|
||||
|
||||
@test 'existing right-pair' {
|
||||
assert $(_ap-get-pair "" "}") same_as "{"
|
||||
}
|
||||
|
||||
@test 'non-existent pair' {
|
||||
assert $(_ap-get-pair "<") same_as ""
|
||||
}
|
||||
|
||||
@test 'non-existent right-pair' {
|
||||
assert $(_ap-get-pair "" ">") same_as ""
|
||||
}
|
||||
|
||||
@test 'all default pairs' {
|
||||
assert '"' in ${(@k)AUTOPAIR_PAIRS}
|
||||
assert "'" in ${(@k)AUTOPAIR_PAIRS}
|
||||
assert '`' in ${(@k)AUTOPAIR_PAIRS}
|
||||
assert '{' in ${(@k)AUTOPAIR_PAIRS}
|
||||
assert '[' in ${(@k)AUTOPAIR_PAIRS}
|
||||
assert '(' in ${(@k)AUTOPAIR_PAIRS}
|
||||
assert ' ' in ${(@k)AUTOPAIR_PAIRS}
|
||||
}
|
4
.zsh/plugins/autopairs/zsh-autopair.plugin.zsh
Normal file
4
.zsh/plugins/autopairs/zsh-autopair.plugin.zsh
Normal file
|
@ -0,0 +1,4 @@
|
|||
# <https://github.com/zdharma-continuum/Zsh-100-Commits-Club/blob/master/Zsh-Plugin-Standard.adoc>
|
||||
0="${${ZERO:-${0:#$ZSH_ARGZERO}}:-${(%):-%N}}"
|
||||
0="${${(M)0:#/*}:-$PWD/$0}"
|
||||
source "${0:A:h}/autopair.zsh"
|
Loading…
Add table
Add a link
Reference in a new issue