Mostly improving fzf tab on-word complete

Jonathan Hodgson 5 years ago
parent ac48d66ff8
commit f2251d9af5
  1. 3
      shells/zsh/includes/auto-notify.zsh
  2. 25
      shells/zsh/includes/completion.zsh
  3. 287
      shells/zsh/includes/fzf.zsh
  4. 10
      shells/zsh/includes/keybindings.zsh

@ -2,5 +2,4 @@ source "$ZSH_FOLDER/plugins/zsh-auto-notify/auto-notify.plugin.zsh"
export AUTO_NOTIFY_THRESHOLD=5 export AUTO_NOTIFY_THRESHOLD=5
export AUTO_NOTIFY_TITLE="Hey! %command has just finished" export AUTO_NOTIFY_TITLE="Hey! %command has just finished"
export AUTO_NOTIFY_BODY="It completed in %elapsed seconds with exit code %exit_code" export AUTO_NOTIFY_BODY="It completed in %elapsed seconds with exit code %exit_code"
AUTO_NOTIFY_IGNORE+=("bat" "zathura" "libreoffice" "lf") AUTO_NOTIFY_IGNORE+=("bat" "zathura" "libreoffice" "lf" "davmail" "neomutt" "newsboat" "w3m")
disable_auto_notify

@ -1,3 +1,5 @@
fpath=("${ZSH_FOLDER}completion" $fpath)
# Make completion work # Make completion work
autoload -U compinit autoload -U compinit
zstyle ':completion:*' menu select zstyle ':completion:*' menu select
@ -19,30 +21,11 @@ compdef sshrc=ssh
compdef v=vim compdef v=vim
compdef vi=vim compdef vi=vim
autoload bashcompinit #autoload bashcompinit
bashcompinit #bashcompinit
_wp_complete() {
local OLD_IFS="$IFS"
local cur=${COMP_WORDS[COMP_CWORD]}
IFS=$'\n'; # want to preserve spaces at the end
local opts="$(wp cli completions --line="$COMP_LINE" --point="$COMP_POINT")"
if [[ "$opts" =~ \<file\>\s* ]]
then
COMPREPLY=( $(compgen -f -- $cur) )
elif [[ $opts = "" ]]
then
COMPREPLY=( $(compgen -f -- $cur) )
else
COMPREPLY=( ${opts[*]} )
fi
IFS="$OLD_IFS"
return 0
}
complete -o nospace -F _wp_complete wp
# Include hidden files in autocomplete: # Include hidden files in autocomplete:
_comp_options+=(globdots) _comp_options+=(globdots)
fpath=("$ZSH_FOLDER/completion" $fpath)
#source "$ZSH_FOLDER/plugins/fzf-tab/fzf-tab.zsh" #source "$ZSH_FOLDER/plugins/fzf-tab/fzf-tab.zsh"

@ -20,184 +20,155 @@ fi
if [ "$sourced" = "True" ]; then if [ "$sourced" = "True" ]; then
# CTRL-W to select a wordlist # jhComplete(){
__fsel_wordlist() { # local comp=$(echo $1 | cut -d':' -f1)
local cmd="$FZF_DEFAULT_COMMAND --exclude \*.md --exclude \*.gif --exclude \*.jpg --exclude \*.png --exclude \*.lua --exclude \*.jar --exclude \*.pl '' /usr/share/wordlists/ | sed 's#^/usr/share/wordlists/##'" # local prompt=$(echo $1 | cut -d':' -f2)
setopt localoptions pipefail 2> /dev/null # local currentProject=$(project current --path)
eval "$cmd" | FZF_DEFAULT_OPTS="--height ${FZF_TMUX_HEIGHT:-40%} --reverse $FZF_DEFAULT_OPTS $FZF_CTRL_T_OPTS --preview 'bat --color=always {}'" $(__fzfcmd) -m "$@" | while read item; do # find "$HOME/.local/share/snippets/" -name \*.func | while read line; do
echo -n "${(q)item} " # source "$line"
done # done
local ret=$? # case "${comp}" in
echo # ip)
return $ret # if [ -n "$currentProject" ]; then
# project hosts ip --fzf
# fi
# ;;
# myip)
# ip route | grep -oE '(dev|src) [^ ]+' | sed 'N;s/\n/,/;s/src //' | awk -F',' '{print $2 " " $1}' | sort -u | fzf --no-preview | cut -d' ' -f1
# ;;
# pf)
# if [ -n "$currentProject" ]; then
# find "$currentProject" -type f | fzf
# fi
# ;;
# pd)
# if [ -n "$currentProject" ]; then
# find "$currentProject" -type d | fzf --no-preview
# fi
# ;;
# wl|wordlist)
# __fsel_wordlist
# ;;
# snip)
# snippets
# ;;
# network-interface|int)
# ip -o link show | cut -d' ' -f2- | sed -E 's/[^:]+(UP|DOWN).*/\1/' | tr ':' ' ' | fzf --preview="interface=\$(echo {} | cut -f1 -d' ');ip address show \$interface" | cut -d' ' -f1
# ;;
# *)
# if command -v "jhcomplete::$comp" > /dev/null; then
# "jhcomplete::$comp"
# else
# echo ""
# fi
# esac
# }
# on_word_replace(){
# setopt localoptions noshwordsplit noksh_arrays noposixbuiltins
# local word="${LBUFFER##* }${RBUFFER%% *}"
# if [ -n "$word" ]; then
# local changeto=$(jhswap "$word" )
# local lastWord="$changeto"
# local LWORDS=$(echo $LBUFFER | tr ' ' '\n' | wc -l)
# local RWORDS=$(echo $RBUFFER | tr ' ' '\n' | wc -l)
# if [ "$LWORDS" -gt "1" ]; then
# LBUFFER="${LBUFFER% *} $lastWord"
# else
# LBUFFER="$lastWord"
# fi
# if [ "$RWORDS" -gt "1" ]; then
# RBUFFER=" ${RBUFFER#* }"
# else
# RBUFFER=""
# fi
# zle reset-prompt
# zle -R
# return 0
# fi
# }
# Prompts the user to select a wordlist from the wordlists folder
wordlistSelect() {
fd -a --type f --hidden --follow --color=always --exclude .git --exclude \*.md --exclude \*.gif --exclude \*.jpg --exclude \*.png --exclude \*.lua --exclude \*.jar --exclude \*.pl '' /usr/share/wordlists/ | fzf --preview 'bat --color=always {}'
} }
fzf-wordlist-widget() { word_replace(){
LBUFFER="${LBUFFER}$(__fsel_wordlist)" local ret=1
local ret=$? local word="$1"
zle reset-prompt local cmd="$2"
return $ret case "$word" in
wl) wordlistSelect; return 0 ;;
myip) ip route | grep -oE '(dev|src) [^ ]+' | sed 'N;s/\n/,/;s/src //;s/dev //' | awk -F',' '{print $2 " " $1}' | sort -u | fzf -1 --no-preview | cut -d' ' -f1; return 0 ;;
esac
return "$ret"
} }
#zle -N fzf-wordlist-widget
#bindkey '^W' fzf-wordlist-widget
# CTRL-P to select an IP address from project host jh_tabcomplete(){
__fsel_ip() { local tokens cmd swap ret=1 lastWord
setopt localoptions pipefail 2> /dev/null # http://zsh.sourceforge.net/FAQ/zshfaq03.html
project hosts ip --fzf # http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags
local ret=$? # Use zsh's shell parsing to split lbuffer into items
return $ret # This takes into account quoting and escaping
} tokens=(${(z)LBUFFER})
fzf-ip-widget() { # If there isn't anythng typed, fall back to old tab binding
LBUFFER="${LBUFFER}$(project hosts ip --fzf) " if [ ${#tokens} -lt 1 ]; then
local ret=$? zle ${jhcomplete_default_completion:-expand-or-complete}
zle reset-prompt return
return $ret
}
#zle -N fzf-ip-widget
#bindkey '^P' fzf-ip-widget
jhComplete(){
local comp=$(echo $1 | cut -d':' -f1)
local prompt=$(echo $1 | cut -d':' -f2)
local currentProject=$(project current --path)
find "$HOME/.local/share/snippets/" -name \*.func | while read line; do
source "$line"
done
case "${comp}" in
ip)
if [ -n "$currentProject" ]; then
project hosts ip --fzf
fi
;;
myip)
ip route | grep -oE '(dev|src) [^ ]+' | sed 'N;s/\n/,/;s/src //' | awk -F',' '{print $2 " " $1}' | sort -u | fzf --no-preview | cut -d' ' -f1
;;
pf)
if [ -n "$currentProject" ]; then
find "$currentProject" -type f | fzf
fi
;;
pd)
if [ -n "$currentProject" ]; then
find "$currentProject" -type d | fzf --no-preview
fi
;;
wl|wordlist)
__fsel_wordlist
;;
snip)
snippets
;;
network-interface|int)
ip -o link show | cut -d' ' -f2- | sed -E 's/[^:]+(UP|DOWN).*/\1/' | tr ':' ' ' | fzf --preview="interface=\$(echo {} | cut -f1 -d' ');ip address show \$interface" | cut -d' ' -f1
;;
*)
if command -v "jhcomplete::$comp" > /dev/null; then
"jhcomplete::$comp"
else
echo ""
fi fi
esac
}
jhswap(){ # Assume the first element
local orig="$1" # TODO: make this work for multiple commands chained with | or > or && etc.
local inside=$(echo "$orig" | grep -Eo '<[^>]+>' | tr -d '<>') # TODO: make this work when command prepended with variables eg a=2 foo bar
local new=$(jhComplete "$inside") # foo is the command name
echo "${orig/$inside/$new}" | tr -d '<>' cmd="${tokens[1]}"
} lastWord="${tokens[-1]}"
on_word_replace(){ # This part checks to see if the whole "word" is replaceable
setopt localoptions noshwordsplit noksh_arrays noposixbuiltins if [ "${LBUFFER[-1]}" != " " ]; then
local word="${LBUFFER##* }${RBUFFER%% *}" swap="$(word_replace "$lastWord" "$cmd")"
if [ -n "$word" ]; then ret="$?"
local changeto=$(jhswap "$word" )
local lastWord="$changeto"
local LWORDS=$(echo $LBUFFER | tr ' ' '\n' | wc -l)
local RWORDS=$(echo $RBUFFER | tr ' ' '\n' | wc -l)
if [ "$LWORDS" -gt "1" ]; then
LBUFFER="${LBUFFER% *} $lastWord"
else
LBUFFER="$lastWord"
fi
if [ "$RWORDS" -gt "1" ]; then
RBUFFER=" ${RBUFFER#* }"
else
RBUFFER=""
fi
zle reset-prompt
zle -R
return 0
fi fi
} # This part checks if the part after an = is completable
if [ "$ret" -ne "0" ]; then
# I want my tab complete to be based on "current" word I am typing sometimes, before the command local afterEqual="${lastWord##*=}"
custom_tabcomplete(){ local beforeEqual="${lastWord%=*}"
local tokens cmd prefix trigger tail fzf matches lbuf d_cmds # If they are different, there is an equals in the word
FZF_DEFAULT_OPTS="$FZF_DEFAULT_OPTS --reverse --height 40%" if [ "$afterEqual" != "$lastWord" ]; then
setopt localoptions noshwordsplit noksh_arrays noposixbuiltins swap="${beforeEqual}=$(word_replace "$afterEqual" "$cmd")"
ret="$?"
if [ -n "$RBUFFER" ]; then
if [[ "${RBUFFER[1]}" != " " ]]; then
on_word_replace
return 0
fi fi
fi fi
if [ "$ret" -eq "0" ]; then
# http://zsh.sourceforge.net/FAQ/zshfaq03.html if [ -n "$swap" ]; then
# http://zsh.sourceforge.net/Doc/Release/Expansion.html#Parameter-Expansion-Flags tokens[-1]="$swap"
tokens=(${(z)LBUFFER}) LBUFFER="${tokens[@]}"
if [ ${#tokens} -lt 1 ]; then
zle ${fzf_default_completion:-expand-or-complete}
return
fi fi
cmd=${tokens[1]}
append=""
if [[ "$cmd" == "hashcat" ]]; then
if [[ "${tokens[-1]}" == "-m" || "${tokens[-1]}" == "--hash-type" ]]; then
append=$(hashcat --example-hashes | awk -v RS="\n\n" -F "\t" '{gsub("\n","\t",$0); print $1 "\t" $2 "\t" $3}' | sed 's/MODE: //; s/TYPE: //' | fzf -d "\t" --header="Mode Type" --with-nth='1,2' --preview='echo {3}' --preview-window=up:1 | cut -d' ' -f1)
#else
# append=$(hashcat --help | sed -n '/Options Short/,/^$/p' | sed -E 's/ +/ /g' | sed '/==============/d' | column -t -s '|' -o " " | fzf -d " " --with-nth="1,3" --tabstop=2 --header-lines="1" --no-preview | cut -d' ' -f1 | sed 's/\s*$//' | grep -Eo '\S+$')
fi
fi
if [ -n "$append" ]; then
# Make sure that we are adding a space
if [[ "${LBUFFER[-1]}" != " " ]]; then
LBUFFER="${LBUFFER} "
fi
LBUFFER="${LBUFFER}${append}"
zle reset-prompt zle reset-prompt
return $ret
return 0 return 0
fi
#local newLBuffer="${tokens:1:${#tokens[@]}-1}"
local newLBuffer
for i in $(seq 1 $((${#tokens[@]} - 1)) ); do
newLBuffer="${newLBuffer}${tokens[i]} "
done
if [[ "${LBUFFER[-1]}" == " " ]]; then
fzf-completion
else else
local new=$(jhComplete "${tokens[-1]}") zle ${jhcomplete_default_completion:-expand-or-complete}
if [ -n "$new" ]; then # Redrawing here means I can use fzf in my completion functions
LBUFFER="${newLBuffer}${new} " # This is not ideal, I would preferably run this after fzf in the completion functions but can't because they are not widgets.
local ret=$?
zle reset-prompt zle reset-prompt
return $ret return
else
fzf-completion
fi
fi fi
} }
zle -N custom_tabcomplete
bindkey '^I' custom_tabcomplete # Record what ctrl+i is currently set to
# That way we can call it if jhcompletion doesn't result in anything
[ -z "$jhcomplete_default_completion" ] && {
binding=$(bindkey '^I')
[[ $binding =~ 'undefined-key' ]] || jhcomplete_default_completion=$binding[(s: :w)2]
unset binding
}
zle -N jh_tabcomplete
bindkey '^I' jh_tabcomplete
fi fi

@ -79,3 +79,13 @@ function fg-bg() {
} }
zle -N fg-bg zle -N fg-bg
bindkey '^Z' fg-bg bindkey '^Z' fg-bg
rationalise-dot() {
if [[ $LBUFFER = *.. ]]; then
LBUFFER+=/..
else
LBUFFER+=.
fi
}
zle -N rationalise-dot
bindkey . rationalise-dot

Loading…
Cancel
Save