diff options
-rw-r--r-- | ChangeLog | 3 | ||||
-rw-r--r-- | Completion/Core/_expand | 52 | ||||
-rw-r--r-- | Doc/Zsh/compsys.yo | 27 |
3 files changed, 65 insertions, 17 deletions
diff --git a/ChangeLog b/ChangeLog index ad76f67ee..050655dc5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,8 @@ 2000-06-08 Sven Wischnowsky <wischnow@zsh.org> + * 11815: Completion/Core/_expand, Doc/Zsh/compsys.yo: trying to + improve _expand; new keep-prefix style, add-space gives more control + * 11814: Src/Zle/zle_tricky.c: make C-code expansion add a space only when more than one word was generated diff --git a/Completion/Core/_expand b/Completion/Core/_expand index 094806f29..25145f752 100644 --- a/Completion/Core/_expand +++ b/Completion/Core/_expand @@ -7,11 +7,11 @@ # the expansions done produce no result or do not change the original # word from the line. -setopt localoptions nullglob +setopt localoptions nullglob nonomatch [[ _matcher_num -gt 1 ]] && return 1 -local exp word sort expr expl subd suf=" " force opt +local exp word sort expr expl subd suf=" " force opt asp tmp opre pre epre (( $# )) && while getopts gsco opt; do @@ -46,8 +46,8 @@ if [[ "$force" = *s* ]] || { { zstyle -s ":completion:${curcontext}:" substitute expr || { [[ "$curcontext" = expand-word:* ]] && expr=1 } } && [[ "${(e):-\$[$expr]}" -eq 1 ]] }; then - exp=( ${(f)"$(print -lR - ${(e)exp//\\[ -]/ })"} ) 2>/dev/null + exp=( "${(e)exp//\\[ +]/ }" ) else exp=( "${exp:s/\\\$/\$}" ) fi @@ -81,18 +81,42 @@ subd=("$exp[@]") [[ "${(e):-\$[$expr]}" -eq 1 ]] } && [[ "$subd" = "$exp"(|\(N\)) ]] && return 1 +zstyle -s ":completion:${curcontext}:" keep-prefix tmp || tmp=changed +if [[ "$word" = [\~\$]*/* && "$tmp" = (yes|true|on|1|changed) ]]; then + epre=( ${(e)~${word%%/*}} ) + if [[ -n "$epre" && $#epre -eq 1 ]]; then + opre="${word%%/*}" + pre="$epre[1]" + [[ "$tmp" != changed || $#exp -gt 1 || + "${opre}${exp[1]#${pre}}" != "$word" ]] && exp=( ${opre}${^exp#${pre}} ) + fi + [[ $#exp -eq 1 && "$exp[1]" = "$word" ]] && return 1 +fi + # Now add as matches whatever the user requested. zstyle -s ":completion:${curcontext}:" sort sort [[ "$sort" = (yes|true|1|on) ]] && exp=( "${(@o)exp}" ) +if zstyle -s ":completion:${curcontext}:" add-space tmp; then + if [[ "$tmp" != *subst* || "$word" != *\$* || "$exp[1]" = *\$* ]]; then + [[ "$tmp" = *file* ]] && asp=file + [[ "$tmp" = *(yes|true|1|on|subst)* ]] && asp="yes$asp" + fi +else + asp=file +fi + # If there is only one expansion, add a suitable suffix if (( $#exp == 1 )); then - if [[ -d $exp && "$exp[1]" != */ ]]; then + if [[ -d ${exp[1]/${opre}/${pre}} && "$exp[1]" != */ ]]; then suf=/ - elif ! zstyle -T ":completion:${curcontext}:" add-space; then + elif [[ "$asp" = yes* || + ( "$asp" = *file && -f "${exp[1]/${opre}/${pre}}" ) ]]; then + suf=' ' + else suf= fi fi @@ -120,30 +144,30 @@ else compadd "$disp[@]" "$expl[@]" -UQ -qS "$suf" - "$exp" fi if [[ $#exp -gt 1 ]] && _requested expansions; then - local i normal dir + local i j normal space dir if [[ "$sort" = menu ]]; then _description expansions expl expansions "o:$word" else _description -V expansions expl expansions "o:$word" fi - if zstyle -T ":completion:${curcontext}:" add-space; then - suf=' ' - else - suf= - fi normal=() + space=() dir=() for i in "$exp[@]"; do - if [[ -d "$i" && "$i" != */ ]]; then + j="${i/${opre}/${pre}}" + if [[ -d "$j" && "$i" != */ ]]; then dir=( "$dir[@]" "$i" ) + elif [[ "$asp" = yes* || ( "$asp" = *file && -f "$j" ) ]]; then + space=( "$space[@]" "$i" ) else normal=( "$normal[@]" "$i" ) fi done (( $#dir )) && compadd "$expl[@]" -UQ -qS/ -a dir - (( $#normal )) && compadd "$expl[@]" -UQ -qS "$suf" -a normal + (( $#space )) && compadd "$expl[@]" -UQ -qS " " -a space + (( $#normal )) && compadd "$expl[@]" -UQ -qS "" -a normal fi _requested original expl original && compadd "$expl[@]" -UQ - "$word" diff --git a/Doc/Zsh/compsys.yo b/Doc/Zsh/compsys.yo index d88fbc77b..24ada7446 100644 --- a/Doc/Zsh/compsys.yo +++ b/Doc/Zsh/compsys.yo @@ -784,10 +784,15 @@ kindex(add-space, completion style) item(tt(add-space))( This style is used by the tt(_expand) completer. If it is `true' (the default), a space will be inserted after all words resulting from the -expansion (except for directory names which get a slash). +expansion (except for directory names which get a slash). The value +may also be the string `tt(file)' to make the completer add a space +only to names of existing files. Finally, the `true' values and +`tt(file)' may be combined with `tt(subst)' to keep the completer from +adding a space when the resulting words were generated by expanding a +substitution such as `tt($LPAR()...RPAR())' and `tt(${...})'. -It is also used by the tt(_prefix) completer to decide if a space should -be inserted before the suffix. +It is also used by the tt(_prefix) completer as a simple boolean value +to decide if a space should be inserted before the suffix. ) kindex(ambiguous, completion style) item(tt(ambiguous))( @@ -1283,6 +1288,22 @@ in the context name to one of tt(correct-)var(num) or tt(approximate-)var(num), where var(num) is the number of errors that were accepted. ) +kindex(keep-prefix, completion style) +item(tt(keep-prefix))( +This style is used by the tt(_expand) completer. If it is `true', the +completer will try to keep a prefix containing a tilde or parameter +expansion. I.e., the string `tt(~/f*)' would be expanded to +`tt(~/foo)' instead of `tt(/home/user/foo)'. If the style is set to +`tt(changed)' (the default), the prefix will only be left unchanged if +there were other changes between the expanded words and the original +word from the command line. Any other value makes the prefix be +expanded unconditionally. + +Note that with one of the `true' values, the tt(_expand) completer +returns if there is only one expansion and that is, after restoring +the original prefix, the same as the original word. This means that +other completers will be called immediately after tt(_expand). +) kindex(last-prompt, completion style) item(tt(last-prompt))( This is used to determine if the completion code should try to put the |