-
Notifications
You must be signed in to change notification settings - Fork 0
/
fz.sh
308 lines (278 loc) · 9.14 KB
/
fz.sh
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# 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/.
[[ -n "$FZ_CMD" ]] || FZ_CMD=z
[[ -n "$FZ_SUBDIR_CMD" ]] || FZ_SUBDIR_CMD=zz
[[ -n "$FZ_HISTORY_CD_CMD" ]] || FZ_HISTORY_CD_CMD=_z
[[ -n "$FZ_SUBDIR_HISTORY_CD_CMD" ]] || \
FZ_SUBDIR_HISTORY_CD_CMD="$FZ_HISTORY_CD_CMD -c"
[[ -n "$FZ_HISTORY_LIST_GENERATOR" ]] \
|| FZ_HISTORY_LIST_GENERATOR=__fz_generate_matched_history_list
[[ -n "$FZ_SUBDIR_HISTORY_LIST_GENERATOR" ]] \
|| FZ_SUBDIR_HISTORY_LIST_GENERATOR=__fz_generate_matched_subdir_history_list
[[ -n "$FZ_SUBDIR_TRAVERSAL" ]] || FZ_SUBDIR_TRAVERSAL=1
[[ -n "$FZ_CASE_INSENSITIVE" ]] || FZ_CASE_INSENSITIVE=1
[[ -n "$FZ_ABBREVIATE_HOME" ]] || FZ_ABBREVIATE_HOME=1
alias ${FZ_CMD}='_fz'
alias ${FZ_SUBDIR_CMD}='_fzz'
__fz_generate_matched_subdir_list() {
local dir seg starts_with_dir
if [[ "$1" == */ ]]; then
dir="$1"
find -L "$(cd "$dir" 2>/dev/null && pwd)" -mindepth 1 -maxdepth 1 -type d \
2>/dev/null | while read -r line; do
base="${line##*/}"
if [[ "$base" == .* ]]; then
continue
fi
echo "$line"
done
else
dir=$(dirname -- "$1")
seg=$(basename -- "$1")
if [[ "$FZ_CASE_INSENSITIVE" == "1" ]]; then
seg=$(echo "$seg" | tr '[:upper:]' '[:lower:]')
fi
starts_with_dir=$( \
find -L "$(cd "$dir" 2>/dev/null && pwd)" -mindepth 1 -maxdepth 1 \
-type d 2>/dev/null | while read -r line; do \
base="${line##*/}"
if [[ "$seg" != .* && "$base" == .* ]]; then
continue
fi
if [[ "$FZ_CASE_INSENSITIVE" != "1" ]]; then
if [[ "$base" == "$seg"* ]]; then
echo "$line"
fi
else
if [[ -n "$BASH_VERSION" ]]; then
if [[ "${base,,}" == "${seg,,}"* ]]; then
echo "$line"
fi
elif [[ -n "$ZSH_VERSION" ]]; then
if [[ "${base:l}" == "${seg:l}"* ]]; then
echo "$line"
fi
fi
fi
done
)
if [ -n "$starts_with_dir" ]; then
echo "$starts_with_dir"
else
find -L "$(cd "$dir" 2>/dev/null && pwd)" -mindepth 1 -maxdepth 1 \
-type d 2>/dev/null | while read -r line; do \
base="${line##*/}"
if [[ "$seg" != .* && "$base" == .* ]]; then
continue
fi
if [[ "$FZ_CASE_INSENSITIVE" != "1" ]]; then
if [[ "$base" == *"$seg"* ]]; then
echo "$line"
fi
else
if [[ -n "$BASH_VERSION" ]]; then
if [[ "${base,,}" == *"${seg,,}"* ]]; then
echo "$line"
fi
elif [[ -n "$ZSH_VERSION" ]]; then
if [[ "${base:l}" == *"${seg:l}"* ]]; then
echo "$line"
fi
fi
fi
done
fi
fi
}
__fz_generate_matched_history_list() {
"$FZ_HISTORY_CD_CMD" -l $@ 2>&1 | while read -r line; do
if [[ "$line" == common:* ]]; then continue; fi
# Reverse the order and cut off the scores
echo "$line"
done | sed '1!G;h;$!d' | cut -b 12-
}
__fz_generate_matched_subdir_history_list() {
__fz_generate_matched_history_list -c "$@"
}
__fz_generate_matches() {
local cmd histories subdirs
if [[ -n "$BASH_VERSION" ]]; then
cmd=$([[ "${COMP_WORDS[0]}" =~ [[:space:]]*([^[:space:]]|[^[:space:]].*[^[:space:]])[[:space:]]* ]]; \
echo -n "${BASH_REMATCH[1]}")
elif [[ -n "$ZSH_VERSION" ]]; then
cmd=(${(z)LBUFFER})
cmd=${cmd[1]}
fi
if [[ "$cmd" == "$FZ_CMD" ]]; then
if [[ "$FZ_ABBREVIATE_HOME" == "1" ]]; then
if [ "$FZ_SUBDIR_TRAVERSAL" == "1" ]; then
cat <("$FZ_HISTORY_LIST_GENERATOR" "$@") \
<(__fz_generate_matched_subdir_list "$@") \
| sed '/^$/d' | sed -e "s,^$HOME,~," | awk '!seen[$0]++'
else
cat <("$FZ_HISTORY_LIST_GENERATOR" "$@") \
| sed '/^$/d' | sed -e "s,^$HOME,~," | awk '!seen[$0]++'
fi
else
if [ "$FZ_SUBDIR_TRAVERSAL" == "1" ]; then
cat <("$FZ_HISTORY_LIST_GENERATOR" "$@") \
<(__fz_generate_matched_subdir_list "$@") \
| sed '/^$/d' | awk '!seen[$0]++'
else
cat <("$FZ_HISTORY_LIST_GENERATOR" "$@") \
| sed '/^$/d' | awk '!seen[$0]++'
fi
fi
elif [[ "$cmd" == "$FZ_SUBDIR_CMD" ]]; then
histories=$("$FZ_SUBDIR_HISTORY_LIST_GENERATOR" "$@")
if [[ "$FZ_ABBREVIATE_HOME" == "1" ]]; then
if [ "$FZ_SUBDIR_TRAVERSAL" == "1" ]; then
cat <("$FZ_SUBDIR_HISTORY_LIST_GENERATOR" "$@") \
<(__fz_generate_matched_subdir_list "$@") \
| sed '/^$/d' | sed -e "s,^$HOME,~," | awk '!seen[$0]++'
else
cat <("$FZ_SUBDIR_HISTORY_LIST_GENERATOR" "$@") \
| sed '/^$/d' | sed -e "s,^$HOME,~," | awk '!seen[$0]++'
fi
else
if [ "$FZ_SUBDIR_TRAVERSAL" == "1" ]; then
cat <("$FZ_SUBDIR_HISTORY_LIST_GENERATOR" "$@") \
<(__fz_generate_matched_subdir_list "$@") \
| sed '/^$/d' | awk '!seen[$0]++'
else
cat <("$FZ_SUBDIR_HISTORY_LIST_GENERATOR" "$@") \
| sed '/^$/d' | awk '!seen[$0]++'
fi
fi
fi
}
__fz_filter() {
FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse \
--bind 'shift-tab:up,tab:down' $FZF_DEFAULT_OPTS" fzf
}
__fz_bash_completion() {
COMPREPLY=()
local selected slug
eval "slug=${COMP_WORDS[@]:(-1)}"
if [[ "$(__fz_generate_matches "$slug" | head | wc -l)" -gt 1 ]]; then
selected=$(__fz_generate_matches "$slug" | __fz_filter)
elif [[ "$(__fz_generate_matches "$slug" | head | wc -l)" -eq 1 ]]; then
selected=$(__fz_generate_matches "$slug")
else
return
fi
if [[ -n "$selected" ]]; then
if [[ "$FZ_ABBREVIATE_HOME" == "1" ]]; then
selected=${selected/#\~/$HOME}
fi
selected=$(printf %q "$selected")
if [[ "$selected" != */ ]]; then
selected="${selected}/"
fi
if [[ "$FZ_ABBREVIATE_HOME" == "1" ]]; then
selected=${selected/#$HOME/\~}
fi
COMPREPLY=( "$selected" )
fi
printf '\e[5n'
}
__fz_zsh_completion() {
setopt localoptions noshwordsplit noksh_arrays noposixbuiltins nonomatch
local args cmd selected slug
args=(${(z)LBUFFER})
cmd=${args[1]}
if [[ "$cmd" != "$FZ_CMD" && "$cmd" != "$FZ_SUBDIR_CMD" ]] \
|| [[ "$cmd" == "$FZ_CMD" && "$LBUFFER" =~ "^\s*$FZ_CMD$" ]] \
|| [[ "$cmd" == "$FZ_SUBDIR_CMD" && "$LBUFFER" =~ "^\s*$FZ_SUBDIR_CMD$" ]]; then
zle ${__fz_zsh_default_completion:-expand-or-complete}
return
fi
if [[ "${#args}" -gt 1 ]]; then
eval "slug=${args[-1]}"
fi
if [[ "$(__fz_generate_matches "$slug" | head | wc -l)" -gt 1 ]]; then
selected=$(__fz_generate_matches "$slug" | __fz_filter)
elif [[ "$(__fz_generate_matches "$slug" | head | wc -l)" -eq 1 ]]; then
selected=$(__fz_generate_matches "$slug")
else
return
fi
if [[ -n "$selected" ]]; then
if [[ "$FZ_ABBREVIATE_HOME" == "1" ]]; then
selected=${selected/#\~/$HOME}
fi
selected="${(q)selected}"
if [[ "$selected" != */ ]]; then
selected="${selected}/"
fi
if [[ "$FZ_ABBREVIATE_HOME" == "1" ]]; then
selected=${selected/#$HOME/\~}
fi
LBUFFER="$cmd $selected"
fi
type _zsh_autosuggest_clear >/dev/null && _zsh_autosuggest_clear
zle redisplay
typeset -f zle-line-init >/dev/null && zle zle-line-init
}
__fz_init_bash_completion() {
# Enable redrawing line by printf '\e[5n', but only for interactive shells.
if [[ "$-" =~ "i" ]]; then
bind '"\e[0n": redraw-current-line'
fi
complete -o nospace -F __fz_bash_completion "$FZ_CMD"
complete -o nospace -F __fz_bash_completion "$FZ_SUBDIR_CMD"
}
__fz_init_zsh_completion() {
[ -n "$__fz_zsh_default_completion" ] || {
binding=$(bindkey '^I')
# $binding[(s: :w)2]
# The command substitution and following word splitting to determine the
# default zle widget for ^I formerly only works if the IFS parameter contains
# a space via $binding[(w)2]. Now it specifically splits at spaces, regardless
# of IFS.
# It’s not compatitable with bash so use awk instead.
[[ $binding =~ 'undefined-key' ]] \
|| __fz_zsh_default_completion=$(echo "$binding" | awk '{print $2}')
unset binding
}
zle -N __fz_zsh_completion
bindkey '^I' __fz_zsh_completion
}
_fz() {
local rc
if [[ "$($FZ_HISTORY_LIST_GENERATOR "$@" | head | wc -l)" -gt 0 ]]; then
"$FZ_HISTORY_CD_CMD" "$@"
elif [[ "$FZ_SUBDIR_TRAVERSAL" -ne 0 ]]; then
err=$(cd "${@: -1}" 2>&1)
rc=$?
if ! cd "${@: -1}" 2>/dev/null; then
echo ${err#* } >&2
return $rc
fi
fi
}
_fzz() {
local rc
if [[ "$($FZ_SUBDIR_HISTORY_LIST_GENERATOR "$@" | head | wc -l)" -gt 0 ]]; then
if [[ -n "$BASH_VERSION" ]]; then
$FZ_SUBDIR_HISTORY_CD_CMD "$@"
elif [[ -n "$ZSH_VERSION" ]]; then
${=FZ_SUBDIR_HISTORY_CD_CMD} "$@"
fi
elif [[ "$FZ_SUBDIR_TRAVERSAL" -ne 0 ]]; then
err=$(cd "${@: -1}" 2>&1)
rc=$?
if ! cd "${@: -1}" 2>/dev/null; then
echo ${err#* } >&2
return $rc
fi
fi
}
if [[ -n "$BASH_VERSION" ]]; then
__fz_init_bash_completion
elif [[ -n "$ZSH_VERSION" ]]; then
__fz_init_zsh_completion
fi