zsh-users
 help / color / mirror / code / Atom feed
* why won't this function work?
@ 2004-07-08  2:37 gj
  2004-07-08  6:49 ` Bart Schaefer
  2004-07-08  9:48 ` Peter Stephenson
  0 siblings, 2 replies; 4+ messages in thread
From: gj @ 2004-07-08  2:37 UTC (permalink / raw)
  To: zsh-users

Hi all.

I'm trying to convert a function I wrote in bash to zsh.  How can I get this to work?

selhist () 
{
     TAB='       ';
     (( " $# < 1 " )) && {
         echo "Usage: selhist [command]";
         return 1
     };
     oldIFS=$IFS;
     IFS='
';
     cmd=("" `grep -w $1 $HISTFILE | sort | uniq | pr -tn` "");
     IFS=$oldIFS;
     printf "%s\n" "${cmd[@]}" | less -F;
     echo -n "enter number of desired command [1 - $(( ${#cmd[@]} - 2 ))]: ";
     read answer;
     eval "${cmd[$answer]#*$TAB}"
}

Thanks,
GJ


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

* Re: why won't this function work?
  2004-07-08  2:37 why won't this function work? gj
@ 2004-07-08  6:49 ` Bart Schaefer
  2004-07-08  9:48 ` Peter Stephenson
  1 sibling, 0 replies; 4+ messages in thread
From: Bart Schaefer @ 2004-07-08  6:49 UTC (permalink / raw)
  To: zsh-users

It's generally preferable if you supply some details about the difference
between what does happen and what you intend to have happen, rather than
just say "doesn't work."

However, some possible clues ...

On Thu, 8 Jul 2004 gj@freeshell.org wrote:

>     (( " $# < 1 " )) && {

Don't put quotes around the expression inside the double parens.  All that
accomplishes is to fool zsh into thinking you meant to create a subshell,
so it attempts to execute a command rather than evaluate math.

>     cmd=("" `grep -w $1 $HISTFILE | sort | uniq | pr -tn` "");

Unless you have setopt KSH_ARRAYS, zsh numbers arrays from 1, not from 0,
so the leading empty element may be throwing off your subscript later.  I
have no idea why the trailing empty element is there.


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

* Re: why won't this function work?
  2004-07-08  2:37 why won't this function work? gj
  2004-07-08  6:49 ` Bart Schaefer
@ 2004-07-08  9:48 ` Peter Stephenson
  2004-07-12 21:55   ` gj
  1 sibling, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2004-07-08  9:48 UTC (permalink / raw)
  To: gj, zsh-users

gj@freeshell.org wrote:
> I'm trying to convert a function I wrote in bash to zsh.  How can I get this 
> to work?

It's a bit more than you asked for, but here it is rewritten to use some
additional zsh features.  I've put comments where it's obscure.  With
the right options, you could get the same function to work in bash and
zsh.  I haven't bothered to try, it doesn't sound like that's what you
were after.


selhist ()
{
     # The following ensures a consistent environment for the function.
     emulate -L zsh

     # $'-style quoting avoids using explicit special characters.
     # (That works in bash, too.)
     # Added `local' variable definitions for tidiness.
     local TAB=$'\t';
     (( $# < 1 )) && {
         echo "Usage: selhist [command]"
         return 1
     };

     local -a cmd
     # Use zsh's hacky but useful split-into-lines syntax.
     # The (f) means `split input lines on newlines.'
     # This means we can avoid messing with IFS.  (That should work, too.)
     # Note the padding elements are unnecessary.
     cmd=(${(f)"$(grep -w $1 $HISTFILE | sort | uniq | pr -tn)"})
     # The following version is necessary if you are using zsh's
     # extended_history option, which puts extra information at
     # the start of history lines.  (It's harmless in other cases
     # unless you are in the habit of re-executing colon commands.)
     # cmd=(${(f)"$(sed -e 's/^:[^;]*;//' $HISTFILE | grep -w $1 |
     # sort | uniq | pr -tn)"})

     # Slightly simplified output possible in zsh, which won't
     # split variables on spaces unless sh_word_split is set.
     # (It would be simpler to use the pr -tn at this point, then
     # it doesn't have to be stripped off later.)
     print -l $cmd | less -F

     # Note the renumbering here.
     echo -n "enter number of desired command [1 - $(( ${#cmd[@]} - 1 ))]: "
     local answer
     read answer

     # The eval works, but the following is a little more flexible:
     # it loads the line into the line editor, so you can edit
     # further, or just hit return.  (It's a little like using the
     # hist_verify option with !-style history.)
     print -z "${cmd[$answer]#*$TAB}"
     # Original version.
     # eval "${cmd[$answer]#*$TAB}"
}


-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Science Park, Milton Road,
Cambridge, CB4 0WH, 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: why won't this function work?
  2004-07-08  9:48 ` Peter Stephenson
@ 2004-07-12 21:55   ` gj
  0 siblings, 0 replies; 4+ messages in thread
From: gj @ 2004-07-12 21:55 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

On Thu, Jul 08, 2004 at 10:48:43AM +0100, Peter Stephenson wrote:

> gj@freeshell.org wrote:
> > I'm trying to convert a function I wrote in bash to zsh.  How can I get
> > this to work?
> 
> 
> selhist () { # The following ensures a consistent environment for the
> function.  emulate -L zsh
> 
>      # $'-style quoting avoids using explicit special characters.  # (That
>      works in bash, too.) # Added `local' variable definitions for tidiness.
>      local TAB=$'\t'; (( $# < 1 )) && { echo "Usage: selhist [command]"
>      return 1 };
> 
>      local -a cmd # Use zsh's hacky but useful split-into-lines syntax.  #
>      The (f) means `split input lines on newlines.' # This means we can avoid
>      messing with IFS.  (That should work, too.) # Note the padding elements
>      are unnecessary.  cmd=(${(f)"$(grep -w $1 $HISTFILE | sort | uniq | pr
>      -tn)"}) # The following version is necessary if you are using zsh's #
>      extended_history option, which puts extra information at # the start of
>      history lines.  (It's harmless in other cases # unless you are in the
>      habit of re-executing colon commands.) # cmd=(${(f)"$(sed -e
>      's/^:[^;]*;//' $HISTFILE | grep -w $1 | # sort | uniq | pr -tn)"})
> 
>      # Slightly simplified output possible in zsh, which won't # split
>      variables on spaces unless sh_word_split is set.  # (It would be simpler
>      to use the pr -tn at this point, then # it doesn't have to be stripped
>      off later.) print -l $cmd | less -F
> 
>      # Note the renumbering here.  echo -n "enter number of desired command
>      [1 - $(( ${#cmd[@]} - 1 ))]: " local answer read answer
> 
>      # The eval works, but the following is a little more flexible: # it
>      loads the line into the line editor, so you can edit # further, or just
>      hit return.  (It's a little like using the # hist_verify option with
>      !-style history.) print -z "${cmd[$answer]#*$TAB}"

Wow, that is a cool option!

>      # Original version.
>      # eval "${cmd[$answer]#*$TAB}"
> }

This subroutine doesn't work when I try it on 4.0.7/sparc64 and 4.0.7/alpha.
It just hangs there like it's waiting for input, though it's not.

Thanks,
G.


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

end of thread, other threads:[~2004-07-12 21:56 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-07-08  2:37 why won't this function work? gj
2004-07-08  6:49 ` Bart Schaefer
2004-07-08  9:48 ` Peter Stephenson
2004-07-12 21:55   ` gj

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).