From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18727 invoked from network); 25 Oct 2005 22:48:29 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by ns1.primenet.com.au with SMTP; 25 Oct 2005 22:48:29 -0000 Received: (qmail 51340 invoked from network); 25 Oct 2005 22:48:23 -0000 Received: from sunsite.dk (130.225.247.90) by a.mx.sunsite.dk with SMTP; 25 Oct 2005 22:48:23 -0000 Received: (qmail 23378 invoked by alias); 25 Oct 2005 22:48:15 -0000 Mailing-List: contact zsh-users-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 9559 Received: (qmail 23369 invoked from network); 25 Oct 2005 22:48:14 -0000 Received: from news.dotsrc.org (HELO a.mx.sunsite.dk) (130.225.247.88) by sunsite.dk with SMTP; 25 Oct 2005 22:48:14 -0000 Received: (qmail 50231 invoked from network); 25 Oct 2005 22:48:14 -0000 Received: from her-isrv.ionific.com (195.197.252.67) by a.mx.sunsite.dk with SMTP; 25 Oct 2005 22:48:12 -0000 Received: from her-gw.ionific.com ([195.197.252.66] helo=trews52.bothi.fi) by her-isrv.ionific.com with esmtp (Exim 3.35 #1 (Debian)) id 1EUXaV-00040x-00 for ; Wed, 26 Oct 2005 01:48:11 +0300 Received: from azure by trews52.bothi.fi with local (Exim 3.36 #1 (Debian)) id 1EUXaN-0005Yb-00 for ; Wed, 26 Oct 2005 01:48:03 +0300 To: Zsh Users' List Subject: Improved apt/dpkg completion and completion hacking questions Mail-copies-to: nobody From: Hannu Koivisto Date: Wed, 26 Oct 2005 01:47:53 +0300 Message-ID: <87u0f51aee.fsf@trews52.bothi.fi> User-Agent: Gnus/5.090014 (Oort Gnus v0.14) Emacs/21.4 (i386-pc-linux-gnu) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Sender: Hannu Koivisto X-Spam-Checker-Version: SpamAssassin 3.0.4 (2005-06-05) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.6 required=5.0 tests=AWL,BAYES_00 autolearn=ham version=3.0.4 --=-=-= Greetings, I noticed that the completions you get in 'apt-get install ' are not only packages available for installation but all known package names. At the same time it occurred to me that it would be very cool if package names were accompanied by their short descriptions (what's right after "Description:" in dpkg -p package-xyz). So I modified _deb_packages_update_avail to generate proper packages with descriptions (and reindented _debs_caching_policy since it seemed to be indented oddly). I didn't modify other _deb_packages_update_* functions to generate descriptions because I thought descriptions wouldn't be so valuable in those cases and also because I wasn't sure how to best generate them efficiently (then again, maybe efficiency wouldn't have been such a big issue anyway, especially if caching is used (though apparently caching isn't enabled by default)). This resulted to an if-then-else to the place where arrays generated by those functions are actually used to add completions. Patch against zsh 4.2.5 is included as an attachment. I'm using awk to generate the list of packages plus descriptions. I've seen other completion code to use awk as well, but I wonder how accepted that is generally? The change also increases memory consumption (from 800kB to ~3MB in one test case). Aside from removing the commented out old code, what would I need to do in order to get my changes accepted to the zsh distribution? This brings us to customization; not because I'm suggesting it as the way to make the change more acceptable, but for educational reasons :) I'm sure it would be possible to generate the list without awk (though I suspect not quite as efficiently) but let's pretend it isn't. But we still have the old way of generating completions without descriptions without awk. So, let's say we make the old code the default behaviour and have the description-generating awk version available via customization. How would you implement such customizability? Another unrelated question. The stock _deb_packages uses compadd directly to add the completions. And higher level code that calls _deb_packages knows this and passes parameters to be passed to compadd, including -X and its value. As far as I can see, -X's value corresponds to the DESCR parameter of _describe, which I figured would be the right choice for adding completions with descriptions. Since _describe also accepts options to be passed to compadd, I thought I wouldn't try to parse -X out of compadd parameters passed to _deb_packages and pass its value as DESCR but instead I pass an empty string as DESCR and give all compadd parameters to _describe. Apparently this works, i.e. -X in compadd parameters overrides DESCR, but since this behaviour isn't documented, I'd like to know if I'm doing to right thing here? -- Hannu --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=_deb_package.patch Content-Description: _deb_package patch --- /usr/share/zsh/4.2.5/functions/Completion/Debian/_deb_packages 2005-04-27 03:31:08.000000000 +0300 +++ _deb_packages 2005-10-16 20:21:31.000000000 +0300 @@ -6,10 +6,14 @@ if ( [[ ${+_deb_packages_cache_avail} -eq 0 ]] || _cache_invalid DEBS_avail ) && ! _retrieve_cache DEBS_avail; then + #_deb_packages_cache_avail=( + # ${(f)"$(apt-cache --generate pkgnames)"} + #) _deb_packages_cache_avail=( - ${(f)"$(apt-cache --generate pkgnames)"} + ${(f)"$(apt-cache dumpavail | \ + awk -v 'FS=Description: |Package: ' \ + '/^Package: / { package=$2 } /^Description: / { print package ":" $2 }')"} ) - _store_cache DEBS_avail _deb_packages_cache_avail fi cachevar=_deb_packages_cache_avail @@ -103,16 +107,20 @@ _deb_packages_update_$pkgset - _tags packages && compadd "$expl[@]" - "${(@P)cachevar}" + if [[ "$pkgset" == "avail" ]]; then + _tags packages && _describe '' ${cachevar} "$expl[@]" + else + _tags packages && compadd "$expl[@]" - "${(@P)cachevar}" + fi } - _debs_caching_policy () { +_debs_caching_policy () { # rebuild if cache is more than a week old - oldp=( "$1"(mw+1) ) - (( $#oldp )) && return 0 - - [[ /var/cache/apt/pkgcache.bin -nt "$1" || - /var/lib/dpkg/available -nt "$1" ]] - } + oldp=( "$1"(mw+1) ) + (( $#oldp )) && return 0 + + [[ /var/cache/apt/pkgcache.bin -nt "$1" + || /var/lib/dpkg/available -nt "$1" ]] +} _deb_packages "$@" --=-=-=--