home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Shells / zsh-3.0.5-MIHS / src / Functions / multicomp < prev    next >
Encoding:
Text File  |  1996-10-10  |  2.0 KB  |  70 lines

  1. # multicomp() {
  2. # Completes all manner of files given prefixes for each path segment.
  3. # e.g. s/z/s -> src/zsh-2.4/src
  4. #
  5. # Usage: e.g.
  6. # compctl -D -f + -U -K multicomp
  7. #
  8. # Note that exactly matched directories are not expanded, e.g.
  9. # s/zsh-2.4/s<TAB> will not expand to src/zsh-2.4old/src.
  10. # Will expand glob patterns already in the word, but use complete-word,
  11. # not TAB (expand-or-complete), or you will get ordinary glob expansion.
  12. # Requires the -U option to compctl.
  13. # Menucompletion is highly recommended for ambiguous matches.
  14. # Liable to screw up escaped metacharacters royally.
  15. # $fignore is not used: feel free to add your own bit.
  16.  
  17. emulate -R zsh                # Requires zsh 3.0-pre4 or later
  18. local pref head sofar origtop newtop globdir="(-/)" wild
  19. setopt localoptions nullglob rcexpandparam globdots
  20. unsetopt markdirs globsubst shwordsplit nounset
  21.  
  22. pref="${1}$2"
  23. # Hack to allow programmable completion to select multicomp after a :
  24. # (e.g.
  25. # compctl -D -f -x 's[:]' -U -K multicomp
  26. # )
  27. pref="${pref#:}"
  28.  
  29. sofar=('')
  30. reply=('')
  31.  
  32. if [[ "$pref" = \~* ]]; then
  33.   # If the string started with ~, save the head and what it will become.
  34.   origtop="${pref%%/*}"
  35.   newtop=${~origtop}
  36.   # Save the expansion as the bit matched already
  37.   sofar=($newtop)
  38.   pref="${pref#$origtop}"
  39. fi
  40.  
  41. while [[ -n "$pref" ]]; do
  42.   [[ "$pref" = /* ]] && sofar=(${sofar}/) && pref="${pref#/}"
  43.   head="${pref%%/*}"
  44.   pref="${pref#$head}"
  45.   if [[ -n "$pref" && -z $sofar[2] && -d "${sofar}$head" ]]; then
  46.     # Exactly matched directory: don't try to glob
  47.     reply=("${sofar}$head")
  48.   else
  49.     [[ -z "$pref" ]] && globdir=
  50.     # if path segment contains wildcards, don't add another.
  51.     if [[ "$head" = *[\*\?]* ]]; then
  52.       wild=
  53.     else
  54.       wild='*'
  55.     fi
  56.     reply=(${sofar}"${head}${wild}${globdir}")
  57.     reply=(${~reply})
  58.   fi
  59.  
  60.   [[ -z $reply[1] ]] && reply=() && break
  61.   [[ -n $pref ]] && sofar=($reply)
  62. done
  63.  
  64. # Restore ~'s in front if there were any.
  65. # There had better not be anything funny in $newtop.
  66. [[ -n "$origtop" ]] && reply=("$origtop"${reply#$newtop})
  67.  
  68. # }
  69.  
  70.