zsh-users
 help / color / mirror / code / Atom feed
* suffix alias
@ 2004-11-11 14:28 Thorsten Kampe
  2004-11-11 14:50 ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Thorsten Kampe @ 2004-11-11 14:28 UTC (permalink / raw)
  To: zsh-users

Hi,

I have a suffix alias "alias -s py=python". Now I can execute Python 
scripts by name that don't have a "she-bang" and aren't executable. 
That's great.

But now I noticed that I cannot execute Python scripts anymore that 
are placed in my path (/usr/bin for example) because zsh doesn't 
search the path anymore for the suffix alias.

Is there a solution for this problem?

Thorsten
-- 
For instance:
thorsten@cathedral% pwd
/home/thorsten
thorsten@cathedral% rst2html.py --version
python: can't open file 'rst2html.py'
thorsten@cathedral% unalias -s py
thorsten@cathedral% rst2html.py --version
rst2html.py (Docutils 0.3.6)

That's right - 'rst2html.py' isn't in the current working directory 
but in /usr/bin where the install put it.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: suffix alias
  2004-11-11 14:28 suffix alias Thorsten Kampe
@ 2004-11-11 14:50 ` Peter Stephenson
  2004-11-11 15:13   ` Thorsten Kampe
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2004-11-11 14:50 UTC (permalink / raw)
  To: zsh-users

Thorsten Kampe wrote:
> Hi,
> 
> I have a suffix alias "alias -s py=python". Now I can execute Python 
> scripts by name that don't have a "she-bang" and aren't executable. 
> That's great.
> 
> But now I noticed that I cannot execute Python scripts anymore that 
> are placed in my path (/usr/bin for example) because zsh doesn't 
> search the path anymore for the suffix alias.
> 
> Is there a solution for this problem?

exec-py () {
        local -a files
        files=(${^path}/$1(N))
        (( ${#files} )) && set -- $files[1] "${(@)argv[2,-1]}"
        python "$@"
}
alias -s py=exec-py

-- 
Peter Stephenson <pws@csr.com>                  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
**********************************************************************


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: suffix alias
  2004-11-11 14:50 ` Peter Stephenson
@ 2004-11-11 15:13   ` Thorsten Kampe
  2004-11-11 15:31     ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Thorsten Kampe @ 2004-11-11 15:13 UTC (permalink / raw)
  To: zsh-users

* Peter Stephenson (2004-11-11 15:50 +0100)
> Thorsten Kampe wrote:
>> I have a suffix alias "alias -s py=python". Now I can execute Python 
>> scripts by name that don't have a "she-bang" and aren't executable. 
>> That's great.
>> 
>> But now I noticed that I cannot execute Python scripts anymore that 
>> are placed in my path (/usr/bin for example) because zsh doesn't 
>> search the path anymore for the suffix alias.
>> 
>> Is there a solution for this problem?
> 
> exec-py () {
>         local -a files
>         files=(${^path}/$1(N))
>         (( ${#files} )) && set -- $files[1] "${(@)argv[2,-1]}"
>         python "$@"
> }
> alias -s py=exec-py

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

Could you explain a bit what your wrapping function does?

Thorsten


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: suffix alias
  2004-11-11 15:13   ` Thorsten Kampe
@ 2004-11-11 15:31     ` Peter Stephenson
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2004-11-11 15:31 UTC (permalink / raw)
  To: zsh-users

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@csr.com>                  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
**********************************************************************


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2004-11-11 15:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-11 14:28 suffix alias Thorsten Kampe
2004-11-11 14:50 ` Peter Stephenson
2004-11-11 15:13   ` Thorsten Kampe
2004-11-11 15:31     ` Peter Stephenson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).