Unverified Commit 3fd63fdf authored by Andrew Stone's avatar Andrew Stone Committed by GitHub

feat(zsh-interactive-cd): sync version with upstream (#11024)

parent d48cbb82
This diff is collapsed.
# zsh-interactive-cd # zsh-interactive-cd
This plugin adds a fish-like interactive tab completion for the `cd` command. ## Demo
To use it, add `zsh-interactive-cd` to the plugins array of your zshrc file: ![demo](demo.gif)
```zsh
plugins=(... zsh-interactive-cd)
```
![demo](https://user-images.githubusercontent.com/1441704/74360670-cb202900-4dc5-11ea-9734-f60caf726e85.gif) ## Installation
## Usage 1. Install [fzf](https://github.com/junegunn/fzf) by following its [installation instruction](https://github.com/junegunn/fzf#installation).
Press tab for completion as usual, it'll launch fzf automatically. Check fzf’s [readme](https://github.com/junegunn/fzf#search-syntax) for more search syntax usage.
## Requirements 2. Source `zsh-interactive-cd.plugin.zsh` in `.zshrc`.
This plugin requires [fzf](https://github.com/junegunn/fzf). Install it by following ## Usage
its [installation instructions](https://github.com/junegunn/fzf#installation).
## Author
[Henry Chang](https://github.com/changyuheng) Press tab for completion as usual, it'll launch fzf automatically. Check fzf’s [readme](https://github.com/junegunn/fzf#search-syntax) for more search syntax usage.
This diff is collapsed.
# Copyright (c) 2017 Henry Chang #!/usr/bin/env zsh
#
# Copyright 2017-2018 Henry Chang
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
__zic_fzf_prog() { __zic_fzf_prog() {
[ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] \ [ -n "$TMUX_PANE" ] && [ "${FZF_TMUX:-0}" != 0 ] && [ ${LINES:-40} -gt 15 ] \
...@@ -17,7 +23,7 @@ __zic_matched_subdir_list() { ...@@ -17,7 +23,7 @@ __zic_matched_subdir_list() {
length=0 length=0
fi fi
find -L "$dir" -mindepth 1 -maxdepth 1 -type d 2>/dev/null \ find -L "$dir" -mindepth 1 -maxdepth 1 -type d 2>/dev/null \
| cut -b $(( ${length} + 2 ))- | sed '/^$/d' | while read -r line; do | cut -b $(( ${length} + 2 ))- | command sed '/^$/d' | while read -r line; do
if [[ "${line[1]}" == "." ]]; then if [[ "${line[1]}" == "." ]]; then
continue continue
fi fi
...@@ -32,13 +38,19 @@ __zic_matched_subdir_list() { ...@@ -32,13 +38,19 @@ __zic_matched_subdir_list() {
seg=$(basename -- "$1") seg=$(basename -- "$1")
starts_with_dir=$( \ starts_with_dir=$( \
find -L "$dir" -mindepth 1 -maxdepth 1 -type d \ find -L "$dir" -mindepth 1 -maxdepth 1 -type d \
2>/dev/null | cut -b $(( ${length} + 2 ))- | sed '/^$/d' \ 2>/dev/null | cut -b $(( ${length} + 2 ))- | command sed '/^$/d' \
| while read -r line; do | while read -r line; do
if [[ "${seg[1]}" != "." && "${line[1]}" == "." ]]; then if [[ "${seg[1]}" != "." && "${line[1]}" == "." ]]; then
continue continue
fi fi
if [[ "$line" == "$seg"* ]]; then if [ "$zic_case_insensitive" = "true" ]; then
echo "$line" if [[ "$line:u" == "$seg:u"* ]]; then
echo "$line"
fi
else
if [[ "$line" == "$seg"* ]]; then
echo "$line"
fi
fi fi
done done
) )
...@@ -46,19 +58,36 @@ __zic_matched_subdir_list() { ...@@ -46,19 +58,36 @@ __zic_matched_subdir_list() {
echo "$starts_with_dir" echo "$starts_with_dir"
else else
find -L "$dir" -mindepth 1 -maxdepth 1 -type d \ find -L "$dir" -mindepth 1 -maxdepth 1 -type d \
2>/dev/null | cut -b $(( ${length} + 2 ))- | sed '/^$/d' \ 2>/dev/null | cut -b $(( ${length} + 2 ))- | command sed '/^$/d' \
| while read -r line; do | while read -r line; do
if [[ "${seg[1]}" != "." && "${line[1]}" == "." ]]; then if [[ "${seg[1]}" != "." && "${line[1]}" == "." ]]; then
continue continue
fi fi
if [[ "$line" == *"$seg"* ]]; then if [ "$zic_case_insensitive" = "true" ]; then
echo "$line" if [[ "$line:u" == *"$seg:u"* ]]; then
echo "$line"
fi
else
if [[ "$line" == *"$seg"* ]]; then
echo "$line"
fi
fi fi
done done
fi fi
fi fi
} }
__zic_fzf_bindings() {
autoload is-at-least
fzf=$(__zic_fzf_prog)
if $(is-at-least '0.21.0' $(${=fzf} --version)); then
echo 'shift-tab:up,tab:down,bspace:backward-delete-char/eof'
else
echo 'shift-tab:up,tab:down'
fi
}
_zic_list_generator() { _zic_list_generator() {
__zic_matched_subdir_list "${(Q)@[-1]}" | sort __zic_matched_subdir_list "${(Q)@[-1]}" | sort
} }
...@@ -75,6 +104,7 @@ _zic_complete() { ...@@ -75,6 +104,7 @@ _zic_complete() {
fi fi
fzf=$(__zic_fzf_prog) fzf=$(__zic_fzf_prog)
fzf_bindings=$(__zic_fzf_bindings)
if [ $(echo $l | wc -l) -eq 1 ]; then if [ $(echo $l | wc -l) -eq 1 ]; then
matches=${(q)l} matches=${(q)l}
...@@ -82,7 +112,7 @@ _zic_complete() { ...@@ -82,7 +112,7 @@ _zic_complete() {
matches=$(echo $l \ matches=$(echo $l \
| FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} \ | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} \
--reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS \ --reverse $FZF_DEFAULT_OPTS $FZF_COMPLETION_OPTS \
--bind 'shift-tab:up,tab:down'" ${=fzf} \ --bind '${fzf_bindings}'" ${=fzf} \
| while read -r item; do | while read -r item; do
echo -n "${(q)item} " echo -n "${(q)item} "
done) done)
...@@ -144,5 +174,7 @@ zic-completion() { ...@@ -144,5 +174,7 @@ zic-completion() {
} }
zle -N zic-completion zle -N zic-completion
bindkey -M emacs '^I' zic-completion if [ -z $zic_custom_binding ]; then
bindkey -M viins '^I' zic-completion zic_custom_binding='^I'
fi
bindkey "${zic_custom_binding}" zic-completion
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment