-
Notifications
You must be signed in to change notification settings - Fork 9
/
zsh-fzy.plugin.zsh
137 lines (120 loc) · 2.49 KB
/
zsh-fzy.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#! /bin/zsh
#
# zsh-fzy.plugin.zsh
# Copyright (C) 2018 Adrian Perez <[email protected]>
#
# Distributed under terms of the MIT license.
#
ZSH_FZY_TMUX="${0:A:h}/fzy-tmux"
ZSH_FZY=$(command -v fzy)
if [[ -z ${ZSH_FZY} ]] ; then
echo 'fzy is not installed. See https://github.com/jhawthorn/fzy'
return 1
fi
function fzy-history-default-command
{
builtin fc -l -n -r 1
}
function fzy-file-default-command
{
command find -L . \( -path '*/\.*' -o -fstype dev -o -fstype proc \) -prune \
-o -type f -print \
-o -type d -print \
-o -type l -print 2> /dev/null | sed 1d | cut -b3-
}
function fzy-cd-default-command
{
command find -L . \( -path '*/\.*' -o -fstype dev -o -fstype proc \) -prune \
-o -type d -print 2> /dev/null | sed 1d | cut -b3-
}
function fzy-proc-default-command
{
command ps -ef | sed 1d
}
function __fzy_cmd
{
emulate -L zsh
local widget=$1
shift
local -a args=( )
local value
if zstyle -s ":fzy:${widget}" prompt value ; then
args+=( -p "${value}" )
else
args+=( -p "${widget} >> " )
fi
if zstyle -s ":fzy:${widget}" lines value ; then
if [[ ${value} = min:* ]]; then
local pos
print '\e[6n' > /dev/tty
read -rsd 'R' pos < /dev/tty
pos=${pos#*\[}
pos=${pos%;*}
value=${value#min:}
local available_lines=$(( LINES - pos - 1 ))
if [[ ${available_lines} -gt ${value} ]]; then
value=${available_lines}
fi
fi
args+=( -l "${value}" )
fi
if zstyle -t ":fzy:${widget}" show-scores ; then
args+=( -s )
fi
local -a cmd
zstyle -a ":fzy:${widget}" command cmd || cmd=( )
if [[ ${#cmd} -eq 0 ]] ; then
cmd=("fzy-${widget}-default-command")
fi
if zstyle -t :fzy:tmux enabled && [[ -n ${TMUX} ]] ; then
"${cmd[@]}" | "${ZSH_FZY_TMUX}" -- "${args[@]}" "$@"
else
"${cmd[@]}" | "${ZSH_FZY}" "${args[@]}" "$@"
fi
}
function __fzy_fsel
{
__fzy_cmd file | while read -r item ; do
echo -n "${(q)item} "
done
echo
}
function __fzy_psel
{
__fzy_cmd proc | awk '{print $2}' | tr '\n' ' '
}
function fzy-file-widget
{
emulate -L zsh
zle -I
LBUFFER="${LBUFFER}$(__fzy_fsel)"
zle reset-prompt
}
function fzy-cd-widget
{
emulate -L zsh
zle -I
cd "${$(__fzy_cmd cd):-.}"
zle reset-prompt
}
function fzy-history-widget
{
emulate -L zsh
zle -I
local S=$(__fzy_cmd history -q "${LBUFFER//$/\\$}")
if [[ -n $S ]] ; then
LBUFFER=$S
fi
zle reset-prompt
}
function fzy-proc-widget
{
emulate -L zsh
zle -I
LBUFFER="${LBUFFER}$(__fzy_psel)"
zle reset-prompt
}
zle -N fzy-file-widget
zle -N fzy-cd-widget
zle -N fzy-history-widget
zle -N fzy-proc-widget