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

Re: suffix alias



Thorsten Kampe wrote:
> 
> Thanks. So there is no "builtin" solution for this. I thought of
> "command rst2html.py" which is a "kind of solution", too.

Generally speaking, shell functions are more flexible.  There's no real
gain from adding some kind of builtin support in a case like this.
Where you would need an internal change is if you wanted to match
files by patterns rather than suffixes.

> Could you explain a bit what your wrapping function does?

I thought someone might ask.

exec-py () {
  # Collect matches in local array files.  There might
  # be more than one match in $path.
  local -a files
  # This is a very common trick in zsh for searching paths.
  # ${^path}/$1 becomes $path[1]/$1 $path[2]/$1 ...
  # i.e. trial matches that might or might not be real files.
  # The (N) qualifier removes any elements that don't correspond to files.
  files=(${^path}/$1(N))
  # If we found a file, replace $1 by the path to the first match,
  # i.e. the earliest in the $path.
  # If we didn't find the file in path, leave the arguments alone.
  # A simpler version would be:
  #   (( ${#files} )) && 1=$files[1]
  (( ${#files} )) && set -- $files[1] "${(@)argv[2,-1]}"
  # Execute python with the original command line except with
  # $1 replaced as above.
  python "$@"
}
# Use the function instead of python for .py files.
alias -s py=exec-py

-- 
Peter Stephenson <pws@xxxxxxx>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************



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