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

Re: Working with the historywords special parameter

On Aug 23, 11:54pm, Felix Rosencrantz wrote:
} I'm trying to create a completer that uses the previous word on the line
} and the special parameter historywords to determine what values current
} word should be.

Have you tried using $history instead of $historywords?  You want the
word that comes immediately after $words[CURRENT-1] in every history line
that contains $word[CURRENT-1], right?

    local w p h r
    r=( ${${(ps:\1:)h//$~p/$'\1'}%%$'\0'*} )
    compadd -a r

This assumes there are no literal NUL or ctrl-A characters in the history,
but that seems a pretty safe assumption.

} It looks to me like there is no single expression that can be used to
} get just the list of all the elements that match an expression from an
} array.

That's a different question.  You want a list of the indices of all the
elements that match $words[CURRENT-1]?

    integer n=0
    local ixs p="(#b)((#s)(${(q)words[CURRENT-1]})(#e)|*)"
    ixs=( ${${(M)${historywords//$~p/$((++n))=$match[2]}:#<->\=?##}%\=*} )

This takes advantage of the fact that alternate matches with `|' are tried
left to right; it wouldn't work with (*|...).

To get the nth element after the element that matches the word, just
start n at a larger number, e.g. n=1 for your specific example.

} Would anyone object to a new special parameter (maybe historywordsnums)
} that has corresponding elements to historywords saying with which
} history line the word is associated?

I won't object, but it's not really necessary, is it?  You can get it
from ${(k)history[(R)word]}.

On Aug 24, 10:16am, Sven Wischnowsky wrote:
} Maybe ${(Mk)historywords:#$the_word} should expand to the list of
} the indices of the words equal to $the_word.

That's an interesting thought.  It's equivalent to what I invented above
but not quite as flexible.

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   

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