From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26846 invoked from network); 2 Nov 2001 17:58:57 -0000 Received: from ns2.primenet.com.au (HELO primenet.com.au) (?W/WnkJH/NYbvegbShMwkyihLMeWBuNZv?@203.24.36.3) by ns1.primenet.com.au with SMTP; 2 Nov 2001 17:58:57 -0000 Received: (qmail 15248 invoked from network); 2 Nov 2001 17:58:54 -0000 Received: from sunsite.dk (130.225.247.90) by proxy.melb.primenet.com.au with SMTP; 2 Nov 2001 17:58:54 -0000 Received: (qmail 18266 invoked by alias); 2 Nov 2001 17:58:48 -0000 Mailing-List: contact zsh-workers-help@sunsite.dk; run by ezmlm Precedence: bulk X-No-Archive: yes X-Seq: 16210 Received: (qmail 18254 invoked from network); 2 Nov 2001 17:58:47 -0000 To: zsh-workers@sunsite.dk (Zsh hackers list) Subject: Sort of PATCH: local completions Date: Fri, 02 Nov 2001 17:58:18 +0000 Message-ID: <11955.1004723898@csr.com> From: Peter Stephenson I'm sure this can be done much better. This modifies the normal way of choosing a context-sensitive completion to allow you to have a special form in a particular directory. For example, you may want to offer a particular set of options to `make' in a certain directory which it's too complicated to make the default _make dig out. Then you would create a file in that directory called `.zcompletions' and put in it something like typeset -A _localcomps _localcomps=(make _make_for_this_directory) where _make_for_this_directory is already registered as an autoloaded function. Completion for everything except make is (supposed to be) normal. There are various problems with this as it stands. It is tied to specific names and types --- .zcompletions, _localcomps. I wanted to allow the user to have arbitrary code in the function, though. For example, you can declare other local variables, though I don't know how useful that is in practice. This seemed a bit better than imposing a format and hiding the definition of _localcomps. There is no way of making styles local. This would take a certain amount of internal hacking. For example, we could add a flag to zstyle which tied the definitions to the function level in the same way as local variables. (Then the code for sourcing .zcompletions would most naturally live in _main_complete.) Using local styles could in principle allow a replacement for _localcomps, which could itself become a style that the completion system will decant into an associative array; this doesn't change the effect of the code, but makes the interface much neater, e.g. zstyle -l ':completion:*' local-completions make _make_for_this_directory (shame `-L' is already taken). (local-completions would of course be a style like any other, just not very useful the rest of the time.) I'm certainly not going to commit this for now. Index: Completion/Base/Core/_normal =================================================================== RCS file: /cvsroot/zsh/zsh/Completion/Base/Core/_normal,v retrieving revision 1.1 diff -u -r1.1 _normal --- Completion/Base/Core/_normal 2001/04/02 11:04:04 1.1 +++ Completion/Base/Core/_normal 2001/11/02 17:40:57 @@ -3,6 +3,10 @@ local comp command cmd1 cmd2 pat val name i ret=1 _compskip="$_compskip" local curcontext="$curcontext" service +if [[ -e .zcompletions ]]; then + . .zcompletions +fi + # If we get the option `-s', we don't reset `_compskip'. This ensures # that a value set in the function for the `-first-' context is kept, # but that we still use pattern functions when we were called form @@ -18,7 +22,11 @@ if [[ CURRENT -eq 1 ]]; then curcontext="${curcontext%:*:*}:-command-:" - comp="$_comps[-command-]" + if [[ ${+_localcomps} -eq 1 && -n "${_localcomps[-command-]}" ]]; then + comp="${_localcomps[-command-]}" + else + comp="$_comps[-command-]" + fi [[ -z "$comp" ]] || "$comp" && ret=0 return ret @@ -76,11 +84,22 @@ ret=1 name="$cmd1" -comp="$_comps[$cmd1]" +if [[ ${+_localcomps} -eq 1 && -n "${_localcomps[$cmd1]}" ]]; then + comp="${_localcomps[$cmd1]}" +else + comp="$_comps[$cmd1]" +fi service="${_services[$cmd1]:-$cmd1}" -[[ -z "$comp" ]] && - name="$cmd2" comp="$_comps[$cmd2]" service="${_services[$cmd2]:-$cmd2}" +if [[ -z "$comp" ]]; then + name="$cmd2" + if [[ ${+_localcomps} -eq 1 && -n "${_localcomps[$cmd2]}" ]]; then + comp="${_localcomps[$cmd2]}" + else + comp="$_comps[$cmd2]" + fi + service="${_services[$cmd2]:-$cmd2}" +fi # And generate the matches, probably using default completion. @@ -90,7 +109,11 @@ [[ "$_compskip" = (all|*patterns*) ]] && return ret elif [[ "$_compskip" != *default* ]]; then name=-default- - comp="$_comps[-default-]" + if [[ ${+_localcomps} -eq 1 && -n "${_localcomps[-default-]}" ]]; then + comp="${_localcomps[-default-]}" + else + comp="$_comps[-default-]" + fi fi if [[ "$_compskip" != (all|*patterns*) ]]; then -- Peter Stephenson Software Engineer CSR Ltd., Science Park, Milton Road, Cambridge, CB4 0WH, UK Tel: +44 (0)1223 392070 ********************************************************************** The information transmitted is intended only for the person or entity to which it is addressed and may contain confidential and/or privileged material. Any review, retransmission, dissemination or other use of, or taking of any action in reliance upon, this information by persons or entities other than the intended recipient is prohibited. If you received this in error, please contact the sender and delete the material from any computer. **********************************************************************