Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Tag functions with shell options?



aheading@xxxxxxxxxxxx wrote:
> So I think I need some way of marking these functions to be interpreted with
> SH_WORD_SPLIT turned on locally.  Or have I missed something simple?

This is definitely a weak point in compatibility.  Perhaps in a future
version we do want special ways of doing things like this.  Some time
ago, people were suggesting having hooks you could call when a
function was called etc.; that would do the trick too.

However, for the time being... I take it the problem is within the
function, i.e. you don't actually have problems on the command line
yourself?  In that case it would be outside the scope of the function.

Another problem arises if the functions get defined at arbitrary
points during execution (err, like the sws_locate function below, in
fact).  In that case you're basically stuck without at the least a
smart script to convert all functions defined inside in the
appropriate manner.

If they are playing fair, however, try this function.  You need
to call it with the names of all the functions you want to have run
with sh_word_split turned on.  They can be either already in memory or
autoloadable: in the latter case, it assumes they are ksh-like
autoloadable functions, i.e. the entire function definition is there
including the `function foo { ... } bit.'

It relies on the fact that `functions foo' returns an accurate
representation of foo.  I've been trying to ensure this over the
years, but there almost certainly some things we've all missed.


sws_fn () {
  # Convert a sh or ksh function to run under zsh by the addition of
  # some set of options.  The function may already be defined, or be in
  # some autoload directory.

  # This is the text we\'d like added at the top of the function.
  local optline="setopt shwordsplit localoptions"

  local func body dir fnfile

  sws_locate() {
    # Find an autoload file.
    local dir
    for dir in $fpath; do
      if [[ -f $dir/$1 ]]; then
	fnfile=$dir/$1
	return 0
      fi
    done
    return 1
  }

  for func; do
    if [[ "$(functions $func | head -1)" = *undefined* ]] &&
      sws_locate $func; then
      # We're assuming these are ksh-like autoload functions,
      # i.e. they contain the full function include definition lines.
      . $fnfile
    fi
    body="$(functions $func 2>&1 | sed -e 1d -e '$d')"
    [[ -z $body ]] && print "No such function: $func" >&2 && continue
    eval "function $func {
      $optline
      $body
    }"
  done
}

-- 
Peter Stephenson <pws@xxxxxx>       Tel: +49 33762 77366
WWW:  http://www.ifh.de/~pws/       Fax: +49 33762 77330
Deutches Electronen-Synchrotron --- Institut fuer Hochenergiephysik Zeuthen
DESY-IfH, 15735 Zeuthen, Germany.




Messages sorted by: Reverse Date, Date, Thread, Author