zsh-users
 help / color / mirror / code / Atom feed
From: Daniel Hahler <genml+zsh-workers@thequod.de>
To: zsh-users@zsh.org
Subject: Re: Setup git-stash completion for a function: $line is wrong
Date: Wed, 15 Mar 2017 00:59:54 +0100	[thread overview]
Message-ID: <6e020492-e5f4-eca7-c4d4-07dc4257a5ce@thequod.de> (raw)
In-Reply-To: <170312152742.ZM16252@torch.brasslantern.com>


[-- Attachment #1.1: Type: text/plain, Size: 3262 bytes --]

On 12.03.2017 23:27, Bart Schaefer wrote:
> On Mar 12,  5:37pm, Daniel Hahler wrote:
> } 
> } I am using a helper method to setup completion for functions that are
> } used as extended aliases.
> } 
> }   # Helper to setup completion for functions, e.g.
> }   # "complete_function gcf git commit --fixup" will setup completion for
> }   # "gcf" => "git commit --fixup".
> }   complete_function() {
> }     local f=$1; shift
> }     compdef -e "words=($* \"${(@)words[2,-1]}\"); ((CURRENT+=$(( $#*-1 )))); _normal" $f
> }   }
> 
> This can't be right.  Because of the double quotes around the argument
> of compdef -e, ${(@)words[2,-1]} will expand when complete_function
> is executed, not when the compdef value is needed.  Same for $(( $#*-1 ))
> which by the way is the same as $(( $# - 1 )).  Also as you are already
> inside (( )) you don't need $(( )).
> 
> Try it this way:
> 
>   complete_function() {
>     local f=$1; shift
>     compdef -e "words[1]=( ${${(qq)@}} ); (( CURRENT += $# )); _normal" $f
>   }
> 
> There's a bit of magic there using an extra ${...} around ${(qq)@} to
> force the multi-word expansion of $@ back into a single string so that
> the outer double-quotes won't split it the way "$@" is normally split.

Thank you!
It did not work initially, but luckily it seems to be just an off-by-one
error when incrementing CURRENT.  The following works:

  complete_function() {
    local f=$1; shift
    compdef -e "words[1]=( ${${(qq)@}} ); (( CURRENT += $# - 1 )); _normal" $f
  }
  complete_function gsta git stash
  gsta drop <tab>

> } Additionally, I think that zsh itself should provide a way to more
> } easily setup completion for functions (i.e. something like my wrapper
> } function above).
> 
> How would you envision this to work?  How does "zsh itself" know what
> someone is going to do inside a function body?
> 
> There's already (compdef cmd=service) e.g.
> 
>     compdef gsta=git
> 
> for wrappers that don't insert things into their argument words to act
> exactly like a pre-existing completion.

Yes, I am aware of that, and what I mean is more or less something in this
regard, e.g. by making it handle command+arguments.

    compdef gsta='git stash'

Since that would be incompatible with commands that contain spaces,
maybe a list could be used:

    compdef gsta=(git stash)

> Also, using an alias instead of a function wrapper, i.e.,
> 
>     alias gsta='git stash'
> 
> should "just work" because completion will expand the alias before it
> builds $words and looks up the completion.

I am using it with the following to automatically update a ctags tags file,
but I have other, more involving functions-as-aliases - so using an alias
directly is not feasible:

  gsta() {
    $_git_cmd stash "$@"
    local ret=$?
    if [[ -z "$1" || "$1" == 'pop' || "$1" == 'apply' ]]; then
      _update_git_ctags $ret
    fi
    return $ret
  }
  complete_function gsta git stash

btw: Daniel Shahaf suggested on IRC to use a wrapper function for "git"
altogether:

  alias gsta='git stash drop'
  git() {
    if [[ $1 == stash && $2 == drop ]]; then
      ...
    else command git "$@"; fi
  }


Thanks,
Daniel.


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

  reply	other threads:[~2017-03-15  0:00 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-03-12 16:37 Daniel Hahler
2017-03-12 22:27 ` Bart Schaefer
2017-03-14 23:59   ` Daniel Hahler [this message]
2017-03-16 21:02     ` Bart Schaefer
2017-03-16 23:54       ` Daniel Shahaf
2017-03-21  4:42         ` Bart Schaefer

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=6e020492-e5f4-eca7-c4d4-07dc4257a5ce@thequod.de \
    --to=genml+zsh-workers@thequod.de \
    --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).