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

Re: Updated _acroread completer



Haakon Riiser wrote:
> By the way, it uses "sed -n 's/^ver=//p' FILE" to extract the
> version.  Is it preferable to use zsh's own string functions to
> do this?  The code tends to become quite hard to read, and I usually
> need to spend much more time writing it than when I use sed or awk.

As you need to scan an entire file for this, sed is probably as good as
anything.  However, you'll see things as bad as the following, and even
worse, inside the completion code.

The zsh version of:

local ver=$(sed -n 's/^ver=//p' $commands[$words[1]] 2>/dev/null)

would be (with EXTENDED_GLOB set):

ver=${${${(f)"$(<$commands[$words[1]])"}:#^ver=*}##ver=}

which isn't fantastically easy to understand:

  "$(<$commands[$words[1]])"

Output the contents of the file given by $commands[$words[1]] as a
single string.

  ${(f)"$(<$commands[$words[1]])"}

Split the result into an array of lines.  (This forces array context
despite the scalar assignment.)

  ${${(f)"$(<$commands[$words[1]])"}:#^ver=*}

Remove all elements of the resulting array (lines of the file) that
match ^ver=*, i.e. all lines except those that match ver=*.

  ${${${(f)"$(<$commands[$words[1]])"}:#^ver=*}##ver=}

Remove the ver= from the head of the result.  At this point it will be
treated as a scalar for the assignment, but that's OK if there's just
the one match.

There's no equivalent of sed's p (do the substitution and output the
result if the substitution succeeded), which is why we need to match on
the ver= twice.  But there are still spare letters for flags...

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

**********************************************************************



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