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

Re: Filtering argument lists (e.g. for grep)



07.12.2015, 14:51, "Dominik Vogt" <vogt@xxxxxxxxxxxxxxxxxx>:
> On Mon, Dec 07, 2015 at 11:23:54AM +0000, Peter Stephenson wrote:
>>  On Mon, 7 Dec 2015 11:56:22 +0100
>>  Dominik Vogt <vogt@xxxxxxxxxxxxxxxxxx> wrote:
>>  > Maybe grep is a bad example because this can be done with the
>>  > --exclude= option. But could zsh help filtering the names
>>  > generated by globbing in a more general way so that I could write
>>  >
>>  > $ <foo> *
>>  >
>>  > and have zsh automagically filter the results of the * (not
>>  > everywhere; only for commands that have this feature enabled) so
>>  > that the non-matching names are not passed to the command in the
>>  > first place?
>
>>  You could use a global alias, e.g.
>>
>>  alias -g '@*'='*~(*\~|\#*|ChangeLog)'
>
> Yes, but then I'd need an alias for every potential pattern, e.g.
> @*.s*, @**/*, @*.c.* etc.
>
>>  Ig you want that first * to be something more flexible you can use a
>>  glob qualifier.
>>
>>    gi () {
>>      [[ $REPLY != (*\~|\#*|ChangeLog) ]]
>>    }
>>
>>  and use
>>
>>    <foo> *(+gi)
>
> That sounds good, but is there a way to make that qualifier a
> default for certain commands? As an alternative, is it possible
> to access the command name from inside the qualifier function?
>
>   function gi () {
>     if <command should be filtered>; then
>       [[ $REPLY != (*\~|\#*|ChangeLog) ]]
>     fi
>   }

I have pseudo-alias commands like zmw and zpy which do automagic escaping of their arguments (e.g. `zpy import zsh; print(zsh.getvalue("PATH"))` transforms into `zpython "import zsh; print(zsh.getvalue(\"PATH\"))"`) by hooking accept-line zle widget:

    _-accept-line () {
    	emulate -L zsh
    	local -r autopushd=${options[autopushd]} 
    	options[autopushd]=off 
    	cd $PWD || cd
    	options[autopushd]=$autopushd 
    	if [[ ${BUFFER[1,3]} = ":h " ]]
    	then
    		_HISTLINE=$BUFFER 
    		BUFFER=":h ${(q)BUFFER[4,-1]}" 
    	elif [[ ${BUFFER[1,4]} = "zmw " ]]
    	then
    		_HISTLINE=$BUFFER 
    		BUFFER="zmw "${(j. .)${(q)${(z)BUFFER[5,-1]}}} 
    	elif [[ ${BUFFER[1,4]} = "zpy " ]]
    	then
    		_HISTLINE=$BUFFER 
    		BUFFER="zpython ${(qqq)BUFFER[5,-1]}" 
    	fi
    	zle .accept-line
    }
    zle -N accept-line                       _-accept-line

. You may use the same technique to do anything you like with the line you typed, though this “anything” will sometimes be rather tricky to implement. `(z)` parameter expansion flag will be very useful on this path.

>
> Ciao
>
> Dominik ^_^ ^_^
>
> --
>
> Dominik Vogt
> IBM Germany



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