home *** CD-ROM | disk | FTP | other *** search
/ PC-Online 1996 May / PCOnline_05_1996.bin / linux / source / ap / zsh / zsh-2.4 / zsh-2 / zsh-2.4.306 / func / multicomp < prev    next >
Text File  |  1993-09-06  |  3KB  |  82 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. local pref head rceptrue ngtrue sofar origtop newtop globdir="(-/)" wild
  18.  
  19. # No point using the toggle for rcexpandparam,
  20. # since we don't know if the option is set already.
  21. # (This is always the case, so what's the point in the toggle?)
  22. # This stuff is going to be superceded by `setopt localopts' eventually.
  23. [[ -o rcexpandparam ]] && rceptrue=1
  24. [[ -o nullglob ]] && ngtrue=1
  25. setopt rcexpandparam nullglob
  26.  
  27. pref=${1}$2
  28. # Hack to allow programmable completion to select multicomp after a :
  29. # (e.g.
  30. # compctl -D -f -x 's[:]' -U -K multicomp
  31. # )
  32. [[ $pref = :* ]] && pref=$pref[2,-1]
  33.  
  34. sofar=('')
  35. reply=('')
  36.  
  37. if [[ $pref[1] = '~' ]]; then
  38.   # If the string started with ~, save the head and what it will become.
  39.   origtop=${pref%%/*}
  40.   [[ $origtop = */ ]] && origtop[-1]=
  41.   # Next line assumes cshjunkietilde was not set.  See note on toggles above.
  42.   newtop=${~origtop}
  43.   # Save the expansion as the bit matched already
  44.   sofar=($newtop)
  45.   pref=$pref[$#origtop+1,-1]
  46. fi
  47.  
  48. while [[ -n $pref ]]; do
  49.   [[ $pref = /* ]] && sofar=(${sofar}/) && pref=$pref[2,-1]
  50.   head=${pref%%/*}
  51.   [[ $head = */ ]] && head[-1]=
  52.   pref=$pref[$#head+1,-1]
  53.   if [[ $pref = /* && -z $sofar[2] && -d ${sofar}$head ]]; then
  54.     # Exactly matched directory: don't try to glob
  55.     reply=("${sofar}$head")
  56.   else
  57.     [[ $pref = /* ]] || globdir=
  58.     # if path segment contains wildcards, don't add another.
  59.     if [[ $head = *[\*\?]* ]]; then
  60.       wild=
  61.     else
  62.       wild='*'
  63.     fi
  64.     # $sofar must be expanded with rcexpandparam here, in such a way
  65.     # that metacharacters are expanded in the eval step.
  66.     reply=(${sofar}"${head}${wild}${globdir}")
  67.     eval "reply=($reply)"
  68.   fi
  69.  
  70.   [[ -z $reply[1] ]] && reply=() && break
  71.   [[ -n $pref ]] && sofar=($reply)
  72. done
  73.  
  74. # Restore ~'s in front if there were any.
  75. # There had better not be anything funny in $newtop.
  76. [[ -n $origtop ]] && eval "reply=(\$reply:gs?$newtop?\\$origtop?)"
  77.  
  78. [[ rceptrue = 1 ]] || unsetopt rcexpandparam
  79. [[ ngtrue = 1 ]] || unsetopt nullglob
  80.  
  81. # }
  82.