zsh-users
 help / color / mirror / code / Atom feed
From: "Nikolay Aleksandrovich Pavlov (ZyX)" <kp-pav@yandex.ru>
To: "vogt@linux.vnet.ibm.com" <vogt@linux.vnet.ibm.com>,
	Zsh Users <zsh-users@zsh.org>
Subject: Re: Filtering argument lists (e.g. for grep)
Date: Mon, 07 Dec 2015 15:03:44 +0300	[thread overview]
Message-ID: <531031449489824@web12j.yandex.ru> (raw)
In-Reply-To: <20151207113941.GA24545@linux.vnet.ibm.com>



07.12.2015, 14:51, "Dominik Vogt" <vogt@linux.vnet.ibm.com>:
> 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@linux.vnet.ibm.com> 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.

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


  parent reply	other threads:[~2015-12-07 12:15 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-07 10:56 Dominik Vogt
2015-12-07 11:18 ` Dominik Vogt
2015-12-07 11:23 ` Peter Stephenson
2015-12-07 11:39   ` Dominik Vogt
2015-12-07 11:56     ` Nikolay Aleksandrovich Pavlov (ZyX)
2015-12-07 12:03     ` Nikolay Aleksandrovich Pavlov (ZyX) [this message]
2015-12-07 12:06       ` Nikolay Aleksandrovich Pavlov (ZyX)
2015-12-07 12:58         ` Nikolay Aleksandrovich Pavlov (ZyX)

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=531031449489824@web12j.yandex.ru \
    --to=kp-pav@yandex.ru \
    --cc=vogt@linux.vnet.ibm.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).