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

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




07.12.2015, 15:03, "Nikolay Aleksandrovich Pavlov (ZyX)" <kp-pav@xxxxxxxxx>:
> 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
>>    }
>
> And there is another possibility: considering you want to do this thing with command `foo` you need to do the following:
>
> 1. Create an alias `foo='noglob foo'`.
> 2. Create a function `foo` like this:
>
>         function foo()
>         {
>             local -a args=( "${@[@]}" )
>             local -a new_args
>             for (( I=2; I<= $#args; I++ )) ; do
>                 if [[ $args[I] != ${${args[I]}//[*?]} ]] ; then # If argument contains glob pattern
>                     args[I]+="(+gi)"
>                     new_args=( $~args[I] )
>                     args[I,I]=( $new_args )
>                     (( I += #new_args - 1 ))
>                 fi
>             done
>             command foo "${args[@]}"
>         }
>
>     . I.e. in place of leaving zsh to expand globs, expand it in your function “manually”, with necessary additions.

Though this variant is for one command. For multiple you need some adjustments:

1. `alias foo='noglob filterglob foo'`
2. Function is `filterglob`, starts with `local -r cmd="$1"; shift`, ends with `command "$cmd" "${args[@]}"`.

And I should not have used `I=2` (it initially meant to skip command) in any case, replace `I=2` with `I=1`.

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



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