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

Re: Completion for man (_man) patch to support -M

On Thu, 21 Oct 2010 13:15:14 -0200
Silas Silva <silasdb@xxxxxxxxx> wrote:
> I was studying the completion system and, since I would like to add
> support for -M for the _man completion, I've done it.  Dirty and ugly
> patch is attached.

Not sure how widespread support for that argument is, certainly man is
historically rather different on different systems, but _man already
doesn't look much like other completion functions, and the intention
here is obvious enough, so I've committed it.

(For years I've wanted to make man complete files, i.e. by direct path
to the file to be read rather than via manual path + suffix, when
supported, e.g. by GNU, but never unpicked _man enough to do it.)

>     +  if (( $words[(I)-M] == (( $CURRENT - 1 )) )); then
>     +    _directories && return 0
>     +  fi
>     +
> Here I wanted to check if the cursor position (where the user will hit
> tab) will be just after the -M option.  If true, it complete for
> directories and exit.  Is it right?  Is there any better way to make
> that check?

That will do fine.  There are probably other ways of doing it, but
CURRENT and words are the right variables.

>     +  if (( $words[(I)-M] )); then
>     +    local opt
>     +    opt=$words[(( $words[(I)-M]+1 ))]
>     +    _manpath=($_manpath $opt)
>     +  fi
>     +

There's actually a philosphical bug there.  You need $(( ... )) to return the
expanded value, not (( ... )).  However, I say it's a philosophical bug
because actually the contents of the [...] are evaluated as an
arithmetical expression anyway, so you can omit the parentheses --- and
when they're there, the simply do grouping as in any other arithmetic
expression, so actually what you've got works fine, slightly accidentally.

> _man sets a local MANPATH to make man look for man pages in the right
> places.  If the user set a man path with -M, I want it to be added to
> the _manpath variable.
> I'm just not sure if it is the better way to do it.  opt holds the
> option passed to the -M flag, but (( $words[(I)-M]+1 )) looks ugly?

Well, apart from the obvious use of a variable (and omitting the

  integer ind=$words[(I)-M]
  if (( ind )); then
    local opt
    _manpath=($_manpath $opt)

I don't see a major improvement.   (I'll commit this improvement on top.)

Peter Stephenson <p.w.stephenson@xxxxxxxxxxxx>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/

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