zsh-users
 help / color / mirror / code / Atom feed
* Re: using precmd() to get hostnames out of commands
@ 2001-07-29 16:33 Bart Schaefer
  2001-07-29 16:55 ` Paul Lew
  0 siblings, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2001-07-29 16:33 UTC (permalink / raw)
  To: zsh-users

[Apologies if this shows up twice.  I'm concerned that the list server
is silently discarding mail from the brasslantern.com domain for some
reason, so I'm resending from another account as a test.]

On Jul 29,  2:57pm, Alan Third wrote:
} Subject: using precmd() to get hostnames out of commands
}
} Right, a while ago I decided that I wanted my hostname completion to
} include any hostnames that I typed on the command line during a
} session
}
} Is there a better way to do this?

  histhosts() {
    if (( $+_histhosts_cache ))
    then
      set -- ${(z)history[$[${(%):-%h}-1]]}
    else
      typeset -ag _histhosts_cache
      set -- $historywords
    fi
    while (( $# ))
    do
      [[ $1 == (<1-255>.<0-255>.<0-255>.<0-255>|*.org|*.com|*.net|*.edu) ||
	 $1 == *.??~*.<-> ]] && _histhosts_cache[1,0]=$1
      shift
    done
    reply=( $_histhosts_cache )
  }

  zstyle -e ':completion:*:hosts' hosts histhosts

You don't usually want `||' in a glob pattern.  That means "or empty or".
Just one `|'.

About that `_histhosts_cache[1,0]' -- the subscript [1,0] refers to the
non-existent element to the left of element 1, so assigning to that
element is roughly equivalent to perl `unshift'.  This only works in
some late versions of 3.1.9-dev and later.

About `*.??~*.<->' -- that means any two characters that are not digits.
But I think you might be better off with `[^/]#.[[:alpha:]][[:alpha:]]',
which at least omits file paths although `foobar.tar.gz' sneaks past.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net


^ permalink raw reply	[flat|nested] 7+ messages in thread
* using precmd() to get hostnames out of commands
@ 2001-07-29 13:57 Alan Third
  0 siblings, 0 replies; 7+ messages in thread
From: Alan Third @ 2001-07-29 13:57 UTC (permalink / raw)
  To: zsh-users mailing list

Right, a while ago I decided that I wanted my hostname completion to
include any hostnames that I typed on the command line during a
session so I could do things like:


grover:/home/alan>ping  penglet.org
<snip>
grover:/home/alan>ssh pen<tab>

So I came up with this:


iplegal()
{
if [[ $1 == ([1-9]||[1-9][0-9]||1[0-9][0-9]||[2[0-4][0-9]||25[0-5]).\
([0-9]||[1-9][0-9]||1[0-9][0-9]||[2[0-4][0-9]||25[0-5]).\
([0-9]||[1-9][0-9]||1[0-9][0-9]||[2[0-4][0-9]||25[0-5]).\
([0-9]||[1-9][0-9]||1[0-9][0-9]||[2[0-4][0-9]||25[0-5]) ]]
then
        echo legal
else
        echo illegal
fi
}

precmd()
{
        # add any hostnames from the last command to the 
        # hostname completion array
        lastargs=(`fc -ln -1`)

        for stuff in $lastargs
        do
                if [[ $stuff == (*.org||*.com||*.net||*.edu||*.??~*.\
[[:digit:]][[:digit:]]) || `iplegal $stuff` == legal ]]
                then
                        hosts=($hosts $stuff)
                        zstyle ':completion:*:hosts' hosts $hosts
                fi
        done
}

It also recognises IPs and sticks them in too.

Is there a better way to do this? 

Thanks.
-- 
Alan Third

Cows go moo!


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

end of thread, other threads:[~2001-07-30  0:53 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-07-29 16:33 using precmd() to get hostnames out of commands Bart Schaefer
2001-07-29 16:55 ` Paul Lew
2001-07-29 17:13   ` Bart Schaefer
2001-07-29 23:42     ` dynamic completion like emacs (M-/) Paul Lew
2001-07-30  0:41       ` Bart Schaefer
2001-07-30  0:51       ` Bart Schaefer
  -- strict thread matches above, loose matches on Subject: below --
2001-07-29 13:57 using precmd() to get hostnames out of commands Alan Third

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