1
0
Fork 0
mirror of git://git.code.sf.net/p/zsh/code synced 2025-01-19 23:41:31 +01:00
zsh/Functions/Zle/incremental-complete-word

137 lines
3.7 KiB
Text
Raw Permalink Normal View History

1999-07-12 19:02:40 +02:00
# Autoload this function, run `zle -N <func-name>' and bind <func-name>
# to a key.
1999-10-13 15:43:02 +02:00
1999-07-12 19:02:40 +02:00
# This allows incremental completion of a word. After starting this
1999-07-19 16:26:14 +02:00
# command, a list of completion choices can be shown after every character
# you type, which you can delete with ^h or DEL. RET will accept the
# completion so far. You can hit TAB to do normal completion, ^g to
# abort back to the state when you started, and ^d to list the matches.
1999-07-12 19:02:40 +02:00
#
1999-11-17 10:57:16 +01:00
# This works only with the new function based completion system.
1999-07-12 19:02:40 +02:00
1999-10-13 15:43:02 +02:00
# The main widget function.
incremental-complete-word() {
1999-11-17 10:57:16 +01:00
#emulate -L zsh
1999-10-13 15:43:02 +02:00
unsetopt autolist menucomplete automenu # doesn't work well
1999-12-07 17:25:53 +01:00
local key lbuf="$LBUFFER" rbuf="$RBUFFER" pmpt pstr word
2000-03-25 01:21:44 +01:00
local lastl lastr wid twid num alt post toolong
2000-02-03 18:22:40 +01:00
local curcontext="${curcontext}" stop brk
1999-10-13 15:43:02 +02:00
2000-02-03 18:22:40 +01:00
[[ -z "$curcontext" ]] && curcontext=:::
2000-02-23 18:23:09 +01:00
curcontext="incremental:${curcontext#*:}"
2000-02-03 18:22:40 +01:00
zstyle -s ":completion:${curcontext}" prompt pmpt ||
1999-12-15 03:28:14 +01:00
pmpt='incremental (%c): %u%s %l'
2000-02-03 18:22:40 +01:00
zstyle -s ":completion:${curcontext}" stop stop
zstyle -s ":completion:${curcontext}" break brk
1999-10-13 15:43:02 +02:00
2000-02-03 18:22:40 +01:00
if zstyle -t ":completion:${curcontext}" list; then
1999-10-13 15:43:02 +02:00
wid=list-choices
post=( icw-list-helper )
1999-07-12 19:02:40 +02:00
else
1999-10-13 15:43:02 +02:00
wid=complete-word
post=()
fi
comppostfuncs=( "$post[@]" )
zle $wid "$@"
LBUFFER="$lbuf"
RBUFFER="$rbuf"
num=$_lastcomp[nmatches]
2000-03-25 01:21:44 +01:00
if (( ! num )); then
num="${_lastcomp[alternate_nmatches]}"
alt=' -alt-'
fi
1999-10-13 15:43:02 +02:00
if (( ! num )); then
1999-07-12 19:02:40 +02:00
word=''
1999-07-19 16:26:14 +02:00
state='-no match-'
elif [[ "${LBUFFER}${RBUFFER}" = *${_lastcomp[unambiguous]}* ]]; then
word=''
state='-no prefix-'
1999-07-12 19:02:40 +02:00
else
word="${_lastcomp[unambiguous]}"
1999-07-19 16:26:14 +02:00
state=''
fi
2000-03-25 01:21:44 +01:00
zformat -f pstr "$pmpt" "u:${word}" "s:$state" "n:$num" "a:$alt" \
1999-12-10 15:47:55 +01:00
"l:$toolong" "c:${_lastcomp[completer][2,-1]}"
1999-12-07 17:25:53 +01:00
zle -R "$pstr"
1999-07-12 19:02:40 +02:00
read -k key
1999-10-13 15:43:02 +02:00
while [[ '#key' -ne '#\\r' && '#key' -ne '#\\n' &&
'#key' -ne '#\\C-g' ]]; do
twid=$wid
1999-11-17 10:57:16 +01:00
if [[ "$key" = ${~stop} ]]; then
1999-10-13 15:43:02 +02:00
zle -U "$key"
return
1999-11-17 10:57:16 +01:00
elif [[ "$key" = ${~brk} ]]; then
1999-10-13 15:43:02 +02:00
return
elif [[ '#key' -eq '#\\C-h' || '#key' -eq '#\\C-?' ]]; then
[[ $#LBUFFER -gt $#l ]] && LBUFFER="$LBUFFER[1,-2]"
elif [[ '#key' -eq '#\\t' ]]; then
zle complete-word "$@"
lbuf="$LBUFFER"
rbuf="$RBUFFER"
elif [[ '#key' -eq '#\\C-d' ]]; then
twid=list-choices
else
LBUFFER="$LBUFFER$key"
fi
lastl="$LBUFFER"
lastr="$RBUFFER"
[[ "$twid" = "$wid" ]] && comppostfuncs=( "$post[@]" )
toolong=''
zle $twid "$@"
LBUFFER="$lastl"
RBUFFER="$lastr"
num=$_lastcomp[nmatches]
2000-03-25 01:21:44 +01:00
if (( ! num )); then
num="${_lastcomp[alternate_nmatches]}"
alt=' -alt-'
else
alt=''
fi
1999-10-13 15:43:02 +02:00
if (( ! num )); then
word=''
state='-no match-'
elif [[ "${LBUFFER}${RBUFFER}" = *${_lastcomp[unambiguous]}* ]]; then
word=''
state='-no prefix-'
else
word="${_lastcomp[unambiguous]}"
state=''
fi
2000-03-25 01:21:44 +01:00
zformat -f pstr "$pmpt" "u:${word}" "s:$state" "n:$num" "a:$alt" \
1999-12-10 15:47:55 +01:00
"l:$toolong" "c:${_lastcomp[completer][2,-1]}"
1999-12-07 17:25:53 +01:00
zle -R "$pstr"
1999-10-13 15:43:02 +02:00
read -k key
done
if [[ '#key' -eq '#\\C-g' ]]; then
LBUFFER="$lbuf"
RBUFFER="$rbuf"
fi
zle -Rc
}
# Helper function used as a completion post-function used to make sure that
# the list of matches in only shown if it fits on the screen.
icw-list-helper() {
# +1 for the status line we will add...
if [[ compstate[list_lines]+BUFFERLINES+1 -gt LINES ]]; then
compstate[list]='list explanations'
if [[ compstate[list_lines]+BUFFERLINES+1 -gt LINES ]]; then
compstate[list]=''
compstate[force_list]=yes
fi
toolong='...'
fi
}
incremental-complete-word "$@"