* Sort of PATCH: local completions
@ 2001-11-02 17:58 Peter Stephenson
2001-11-03 1:19 ` Bart Schaefer
0 siblings, 1 reply; 2+ messages in thread
From: Peter Stephenson @ 2001-11-02 17:58 UTC (permalink / raw)
To: Zsh hackers list
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 <pws@csr.com> 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.
**********************************************************************
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Sort of PATCH: local completions
2001-11-02 17:58 Sort of PATCH: local completions Peter Stephenson
@ 2001-11-03 1:19 ` Bart Schaefer
0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2001-11-03 1:19 UTC (permalink / raw)
To: Peter Stephenson, Zsh hackers list
On Nov 2, 5:58pm, Peter Stephenson wrote:
> Subject: Sort of PATCH: local completions
> 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. [...]
>
> There is no way of making styles local. This would take a certain amount
> of internal hacking. [...]
Hrm. I think the way I'd approach this whole "local completions" issue is
to alter the context used by the styles in the first place.
E.g., suppose the file .zcompcontext contains the string "zsh-source":
chpwd() {
[[ -f .zcompcontext ]] && curcontext="$(<.zcompcontext)":::
}
zstyle+ ':completion:zsh-source:*:make:*:targets' call-command yes
+ '' command _make_for_zsh_source
This is overloading the `function' field of the context, but what the hell.
You could still read the file with `source' instead, I suppose, and thus put
all the zstyle commands right there in the file, but the code would have to
get a whole lot more paranoid to avoid trojan horses.
--
Bart Schaefer Brass Lantern Enterprises
http://www.well.com/user/barts http://www.brasslantern.com
Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2001-11-03 1:19 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-11-02 17:58 Sort of PATCH: local completions Peter Stephenson
2001-11-03 1:19 ` Bart Schaefer
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).