zsh-workers
 help / color / mirror / code / Atom feed
From: "Bart Schaefer" <schaefer@brasslantern.com>
To: Phil Pennock <comet@fysh.org>,
	Zsh Development Workers <zsh-workers@sunsite.auc.dk>
Subject: Re: Example function
Date: Tue, 26 Jan 1999 16:25:20 -0800	[thread overview]
Message-ID: <990126162520.ZM25560@candle.brasslantern.com> (raw)
In-Reply-To: <19990126184200.B27794@fysh.org>

On Jan 26,  6:42pm, Phil Pennock wrote:
> Subject: Example function
>       
> It's a zless which automatically determines from the file extension
> which decompressor to use.  The comments explain it.  This has evolved
> over about a year and a half, hence my needing help sorting out the
> array bit.

This is certainly interesting, but I'm really puzzled why you wanted to
do it the way you did it.  For example, why build a parallel dispfunc
array and then merge it with argv at the end, rather than just rewrite
argv in place (which you already do in some instances)?

Also the (@) flags in the variable expansions in the last line are
extraneous.

Anyway, here's how I'd write that same function (runs in 3.0.5, too, which
Phil's doesn't because of the for (( )) syntax):

function zl() {
  emulate -R zsh
  [[ $# -ge 1 ]] || return
  local lessopts
  set -A lessopts
  integer i=1 loi=1
  while ((i <= $#))
  do
    case $argv[i] in
    -zforce) argv[i,i+2]=("=($argv[i+1] \"$argv[i+2]\")"); ((++i));;
    -*) lessopts[loi++]=\"$argv[i]\"; argv[i]=(); continue;;
    *.(gz|Z)) argv[i]="=(zcat \"$argv[i]\")";;
    *.bz2) argv[i]="=(bzip2 -dc \"$argv[i]\")";;
    *.bz) argv[i]="=(bzip -dc \"$argv[i]\")";;
    esac
    ((++i))
  done
  eval command less $lessopts $*
}

Of course, this still assumes you don't have any less options or file names
that contain double-quote characters ...

(One of the things on the associative-array wishlist is "reverse pattern"
lookup, that is, treat the array keys as patterns and match them against
the subscript.  Then you could do silly stuff like

	typeset -A map
	map=('*.(gz|Z)'	zcat
	     '*.bz2' 'bzip2 -dc'
	     '*.bz' 'bzip -dc'
	     '*' '<')
	eval ${(q)map[$argv[i]]} '$argv[i]'

where I'm using (q) as the fictional reverse-pattern query flag; probably
there's a better letter.)


  reply	other threads:[~1999-01-27  0:25 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-01-26 18:42 Phil Pennock
1999-01-27  0:25 ` Bart Schaefer [this message]
1999-01-27  0:37   ` Bart Schaefer
1999-02-01 10:48 Sven Wischnowsky

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=990126162520.ZM25560@candle.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=comet@fysh.org \
    --cc=zsh-workers@sunsite.auc.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).