From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 24430 invoked by alias); 15 Mar 2017 00:00:09 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 22573 Received: (qmail 29287 invoked from network); 15 Mar 2017 00:00:09 -0000 X-Qmail-Scanner-Diagnostics: from hahler.de by f.primenet.com.au (envelope-from , uid 7791) with qmail-scanner-2.11 (clamdscan: 0.99.2/21882. spamassassin: 3.4.1. Clear:RC:0(188.40.33.212):SA:0(0.0/5.0):. Processed in 0.709027 secs); 15 Mar 2017 00:00:09 -0000 X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=0.0 required=5.0 tests=SPF_PASS,T_DKIM_INVALID autolearn=unavailable autolearn_force=no version=3.4.1 X-Envelope-From: genml+zsh-workers@thequod.de X-Qmail-Scanner-Mime-Attachments: |signature.asc| X-Qmail-Scanner-Zip-Files: | Received-SPF: pass (ns1.primenet.com.au: SPF record at _spf.hahler.de designates 188.40.33.212 as permitted sender) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=thequod.de; h= content-type:content-type:in-reply-to:mime-version:user-agent :date:date:message-id:from:from:references:subject:subject :received:received; s=postfix2; t=1489535999; bh=ATZ1WBJuIXeGoXV XFQ78xi4d2vkdTnEMNo456OUeze8=; b=mUziJRkklOj30DwK/QyO4KXPwTMk0Q5 Cxa15cSf76JwhbMm6su4v8z5Tuy23yNO8lnSUv9/glriUfkW0kEx5811nsIwB+83 HWO9TxowUMCAGgN2OTuPtQu9QXGmQ0O3StIujnogmQ7HAEHW44FPJw3tldAqW5kb b9n76A2Cfoe0= Subject: Re: Setup git-stash completion for a function: $line is wrong To: zsh-users@zsh.org References: <5fe1f6a0-a0d1-9b21-310c-a3bb16aa7d18@thequod.de> <170312152742.ZM16252@torch.brasslantern.com> From: Daniel Hahler Message-ID: <6e020492-e5f4-eca7-c4d4-07dc4257a5ce@thequod.de> Date: Wed, 15 Mar 2017 00:59:54 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: <170312152742.ZM16252@torch.brasslantern.com> Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="MxsvUiABB5aaGwSvnwdeFkNhA42WXV5Nr" --MxsvUiABB5aaGwSvnwdeFkNhA42WXV5Nr Content-Type: multipart/mixed; boundary="FrPNe3Nx9rfrDQKD4BblqKmH40h2rsA5s"; protected-headers="v1" From: Daniel Hahler To: zsh-users@zsh.org Message-ID: <6e020492-e5f4-eca7-c4d4-07dc4257a5ce@thequod.de> Subject: Re: Setup git-stash completion for a function: $line is wrong References: <5fe1f6a0-a0d1-9b21-310c-a3bb16aa7d18@thequod.de> <170312152742.ZM16252@torch.brasslantern.com> In-Reply-To: <170312152742.ZM16252@torch.brasslantern.com> --FrPNe3Nx9rfrDQKD4BblqKmH40h2rsA5s Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: quoted-printable On 12.03.2017 23:27, Bart Schaefer wrote: > On Mar 12, 5:37pm, Daniel Hahler wrote: > }=20 > } I am using a helper method to setup completion for functions that are= > } used as extended aliases. > }=20 > } # Helper to setup completion for functions, e.g. > } # "complete_function gcf git commit --fixup" will setup completion = for > } # "gcf" =3D> "git commit --fixup". > } complete_function() { > } local f=3D$1; shift > } compdef -e "words=3D($* \"${(@)words[2,-1]}\"); ((CURRENT+=3D$(( = $#*-1 )))); _normal" $f > } } >=20 > 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 $(( )). >=20 > Try it this way: >=20 > complete_function() { > local f=3D$1; shift > compdef -e "words[1]=3D( ${${(qq)@}} ); (( CURRENT +=3D $# )); _nor= mal" $f > } >=20 > 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=3D$1; shift compdef -e "words[1]=3D( ${${(qq)@}} ); (( CURRENT +=3D $# - 1 )); _n= ormal" $f } complete_function gsta git stash gsta drop > } 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). >=20 > How would you envision this to work? How does "zsh itself" know what > someone is going to do inside a function body? >=20 > There's already (compdef cmd=3Dservice) e.g. >=20 > compdef gsta=3Dgit >=20 > 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 thi= s regard, e.g. by making it handle command+arguments. compdef gsta=3D'git stash' Since that would be incompatible with commands that contain spaces, maybe a list could be used: compdef gsta=3D(git stash) > Also, using an alias instead of a function wrapper, i.e., >=20 > alias gsta=3D'git stash' >=20 > 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 fil= e, but I have other, more involving functions-as-aliases - so using an alias= directly is not feasible: gsta() { $_git_cmd stash "$@" local ret=3D$? if [[ -z "$1" || "$1" =3D=3D 'pop' || "$1" =3D=3D '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=3D'git stash drop' git() { if [[ $1 =3D=3D stash && $2 =3D=3D drop ]]; then ... else command git "$@"; fi } Thanks, Daniel. --FrPNe3Nx9rfrDQKD4BblqKmH40h2rsA5s-- --MxsvUiABB5aaGwSvnwdeFkNhA42WXV5Nr Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- iF0EARECAB0WIQS1psIggp+uXWnrnKN8Ar+FP+Y+AAUCWMiD/gAKCRB8Ar+FP+Y+ ADdyAKCRxAH0T6h8FC4KsSgHrnAZyTpr8wCcCB5vctAMHL1wnQl13S9UTD4I1YM= =I6z6 -----END PGP SIGNATURE----- --MxsvUiABB5aaGwSvnwdeFkNhA42WXV5Nr--