diff options
author | Peter Stephenson <pws@users.sourceforge.net> | 2000-04-01 20:49:47 +0000 |
---|---|---|
committer | Peter Stephenson <pws@users.sourceforge.net> | 2000-04-01 20:49:47 +0000 |
commit | 48525452555a24b9d41748f26b4b77f160f01220 (patch) | |
tree | d814ca2f017d9d843fec7d286fefbca78244beb5 /Functions/Zle | |
parent | e025336f2f6d9f107ee1e03b9900f04af0544ba9 (diff) | |
download | zsh-48525452555a24b9d41748f26b4b77f160f01220.tar.gz zsh-48525452555a24b9d41748f26b4b77f160f01220.tar.xz zsh-48525452555a24b9d41748f26b4b77f160f01220.zip |
Updated from list as far as 10376
Diffstat (limited to 'Functions/Zle')
-rw-r--r-- | Functions/Zle/incremental-complete-word | 187 | ||||
-rw-r--r-- | Functions/Zle/predict-on | 119 |
2 files changed, 209 insertions, 97 deletions
diff --git a/Functions/Zle/incremental-complete-word b/Functions/Zle/incremental-complete-word index 2a9c1aff2..e021bf6f7 100644 --- a/Functions/Zle/incremental-complete-word +++ b/Functions/Zle/incremental-complete-word @@ -1,89 +1,124 @@ -# incremental-complete-word() { - # Autoload this function, run `zle -N <func-name>' and bind <func-name> # to a key. + # This allows incremental completion of a word. After starting this -# command, a list of completion choices is 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 and ^g to -# abort back to the state when you started. +# 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. # -# Completion keys: -# incremental_prompt Prompt to show in status line during icompletion; -# the sequence `%u' is replaced by the unambiguous -# part of all matches if there is any and it is -# different from the word on the line -# incremental_stop Pattern matching keys which will cause icompletion -# to stop and the key to be re-executed -# incremental_break Pattern matching keys which will cause icompletion -# to stop and the key to be discarded -# incremental_completer Set of completers, like the `completer' key -# incremental_list If set to a non-empty string, the matches will be -# listed on every key-press - -emulate -L zsh -unsetopt autolist menucomplete automenu # doesn't work well - -local key lbuf="$LBUFFER" rbuf="$RBUFFER" pmpt word lastl lastr wid twid - -[[ -n "$compconfig[incremental_completer]" ]] && -set ${(s.:.)compconfig[incremental_completer]} -pmpt="${compconfig[incremental_prompt]-incremental completion...}" - -if [[ -n "$compconfig[incremental_list]" ]]; then - wid=list-choices -else - wid=complete-word -fi - -zle $wid "$@" -LBUFFER="$lbuf" -RBUFFER="$rbuf" -if [[ "${LBUFFER}${RBUFFER}" = *${_lastcomp[unambiguous]}* ]]; then - word='' -else - word="${_lastcomp[unambiguous]}" -fi -zle -R "${pmpt//\\%u/$word}" -read -k key - -while [[ '#key' -ne '#\\r' && '#key' -ne '#\\n' && - '#key' -ne '#\\C-g' ]]; do - twid=$wid - if [[ "$key" = ${~compconfig[incremental_stop]} ]]; then - zle -U "$key" - return - elif [[ "$key" = ${~compconfig[incremental_break]} ]]; then - 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 +# This works only with the new function based completion system. + +# The main widget function. + +incremental-complete-word() { + #emulate -L zsh + unsetopt autolist menucomplete automenu # doesn't work well + + local key lbuf="$LBUFFER" rbuf="$RBUFFER" pmpt pstr word + local lastl lastr wid twid num post toolong + local curcontext="${curcontext}" stop brk + + [[ -z "$curcontext" ]] && curcontext=::: + curcontext="incremental:${curcontext#*:}" + + zstyle -s ":completion:${curcontext}" prompt pmpt || + pmpt='incremental (%c): %u%s %l' + zstyle -s ":completion:${curcontext}" stop stop + zstyle -s ":completion:${curcontext}" break brk + + if zstyle -t ":completion:${curcontext}" list; then + wid=list-choices + post=( icw-list-helper ) else - LBUFFER="$LBUFFER$key" + wid=complete-word + post=() fi - lastl="$LBUFFER" - lastr="$RBUFFER" - zle $twid "$@" - LBUFFER="$lastl" - RBUFFER="$lastr" - if [[ "${LBUFFER}${RBUFFER}" = *${_lastcomp[unambiguous]}* ]]; then + + comppostfuncs=( "$post[@]" ) + zle $wid "$@" + LBUFFER="$lbuf" + RBUFFER="$rbuf" + num=$_lastcomp[nmatches] + if (( ! num )); then + word='' + state='-no match-' + elif [[ "${LBUFFER}${RBUFFER}" = *${_lastcomp[unambiguous]}* ]]; then word='' + state='-no prefix-' else word="${_lastcomp[unambiguous]}" + state='' fi - zle -R "${pmpt//\\%u/$word}" + zformat -f pstr "$pmpt" "u:${word}" "s:$state" "n:$num" \ + "l:$toolong" "c:${_lastcomp[completer][2,-1]}" + zle -R "$pstr" read -k key -done -if [[ '#key' -eq '#\\C-g' ]]; then - LBUFFER="$lbuf" - RBUFFER="$rbuf" -fi -zle -Rc -# } + while [[ '#key' -ne '#\\r' && '#key' -ne '#\\n' && + '#key' -ne '#\\C-g' ]]; do + twid=$wid + if [[ "$key" = ${~stop} ]]; then + zle -U "$key" + return + elif [[ "$key" = ${~brk} ]]; then + 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] + if (( ! num )); then + word='' + state='-no match-' + elif [[ "${LBUFFER}${RBUFFER}" = *${_lastcomp[unambiguous]}* ]]; then + word='' + state='-no prefix-' + else + word="${_lastcomp[unambiguous]}" + state='' + fi + zformat -f pstr "$pmpt" "u:${word}" "s:$state" "n:$num" \ + "l:$toolong" "c:${_lastcomp[completer][2,-1]}" + zle -R "$pstr" + 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' + [[ compstate[list_lines]+BUFFERLINES+1 -gt LINES ]] && compstate[list]='' + + toolong='...' + fi +} + +incremental-complete-word "$@" diff --git a/Functions/Zle/predict-on b/Functions/Zle/predict-on index 07ce0703a..bd7212050 100644 --- a/Functions/Zle/predict-on +++ b/Functions/Zle/predict-on @@ -1,64 +1,141 @@ # This set of functions implements a sort of magic history searching. # After predict-on, typing characters causes the editor to look backward -# in the history for the first line beginning with what you have typed -# so far. After predict-off, editing returns to normal for the line found. +# in the history for the first line beginning with what you have typed so +# far. After predict-off, editing returns to normal for the line found. # In fact, you often don't even need to use predict-off, because if the -# line doesn't match something in the history, adding a key at the end -# behaves as normal --- though editing in the middle is liable to delete +# line doesn't match something in the history, adding a key performs +# standard completion --- though editing in the middle is liable to delete # the rest of the line. # +# With the function based completion system (which is needed for this), +# you should be able to type TAB at almost any point to advance the cursor +# to the next "interesting" character position (usually the end of the +# current word, but sometimes somewhere in the middle of the word). And +# of course as soon as the entire line is what you want, you can accept +# with RETURN, without needing to move the cursor to the end first. +# # To use it: # autoload -U predict-on # zle -N predict-on # zle -N predict-off # bindkey '...' predict-on # bindkey '...' predict-off -# Note that all the functions are defined when you first call type the -# predict-on key, which means typing the predict-off key before that gives -# a harmless error message. +# Note that all functions are defined when you first type the predict-on +# key, which means typing the predict-off key before that gives a harmless +# error message. predict-on() { - zle -N self-insert insert-and-predict - zle -N magic-space insert-and-predict - zle -N backward-delete-char delete-backward-and-predict + zle -N self-insert insert-and-predict + zle -N magic-space insert-and-predict + zle -N backward-delete-char delete-backward-and-predict + zle -N delete-char-or-list delete-no-predict } predict-off() { - zle -A .self-insert self-insert - zle -A .magic-space magic-space - zle -A .backward-delete-char backward-delete-char + zle -A .self-insert self-insert + zle -A .magic-space magic-space + zle -A .backward-delete-char backward-delete-char } insert-and-predict () { - emulate -L zsh - if [[ ${RBUFFER[1]} = ${KEYS[-1]} ]] + setopt localoptions noshwordsplit noksharrays + if [[ $LBUFFER = *$'\012'* ]] + then + # Editing a multiline buffer, it's unlikely prediction is wanted + zle .$WIDGET "$@" + return + elif [[ ${RBUFFER[1]} = ${KEYS[-1]} ]] then - # same as what's typed, just move on + # Same as what's typed, just move on ((++CURSOR)) else LBUFFER="$LBUFFER$KEYS" if [[ $LASTWIDGET == (self-insert|magic-space|backward-delete-char) ]] then - zle .history-beginning-search-backward || RBUFFER="" + if ! zle .history-beginning-search-backward + then + RBUFFER="" + if [[ ${KEYS[-1]} != ' ' ]] + then + unsetopt automenu recexact + integer curs=$CURSOR pos nchar=${#LBUFFER//[^${KEYS[-1]}]} + local -a +h comppostfuncs + local crs curcontext="${curcontext}" + + [[ -z "$curcontext" ]] && curcontext=::: + curcontext="predict:${curcontext#*:}" + + comppostfuncs=( predict-limit-list ) + zle complete-word + # Decide where to leave the cursor. The dummy loop is used to + # get out of that `case'. + repeat 1 + do + zstyle -s ":completion:${curcontext}:" cursor crs + case $crs in + (complete) + # At the place where the completion left it, if it is after + # the character typed. + [[ ${LBUFFER[-1]} = ${KEYS[-1]} ]] && break + ;& + (key) + # Or maybe at the n'th occurrence of the character typed. + pos=${BUFFER[(in:nchar:)${KEYS[-1]}]} + if [[ pos -gt curs ]] + then + CURSOR=$pos + break + fi + ;& + (*) + # Or else at the previous position. + CURSOR=$curs + esac + done + fi + fi fi fi return 0 } delete-backward-and-predict() { - emulate -L zsh if [[ -n "$LBUFFER" ]] then + setopt localoptions noshwordsplit noksharrays + if [[ $LBUFFER = *$'\012'* ]] then + # Editing a multiline buffer, it's unlikely prediction is wanted + zle .$WIDGET "$@" # If the last widget was e.g. a motion, then probably the intent is # to actually edit the line, not change the search prefix. - if [[ $LASTWIDGET == (self-insert|magic-space|backward-delete-char) ]] + elif [[ $LASTWIDGET == (self-insert|magic-space|backward-delete-char) ]] then ((--CURSOR)) zle .history-beginning-search-forward || RBUFFER="" return 0 else - # Depending on preference, you might call "predict-off" here, - # and also set up forward deletions to turn off prediction. + # Depending on preference, you might call "predict-off" here. LBUFFER="$LBUFFER[1,-2]" fi fi } +delete-no-predict() { + [[ $WIDGET != delete-char-or-list || -n $RBUFFER ]] && predict-off + zle .$WIDGET "$@" +} + +# This is a helper function for autocompletion to prevent long lists +# of matches from forcing a "do you wish to see all ...?" prompt. + +predict-limit-list() { + if (( compstate[list_lines]+BUFFERLINES > LINES || + ( compstate[list_max] != 0 && + compstate[nmatches] > compstate[list_max] ) )) + then + compstate[list]='' + elif zstyle -t ":completion:predict::::" list always + then + compstate[list]='force list' + fi +} + +# Handle zsh autoloading conventions [[ -o kshautoload ]] || predict-on "$@" |