Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: zstyle to control completion of functions/parameters beginning with underscore



Following up to
   http://www.zsh.org/mla/workers/2009/msg00793.html
from almost two years ago:

>>>>> On May 28, 2009 Richard Hartmann <richih.mailinglist@xxxxxxxxx> wrote:
> On Wed, May 27, 2009 at 21:12, Greg Klanderman <gak@xxxxxxxxxxxxxx> wrote:
>> But in the same vein, what I really want is a way to configure the
>> behavior for functions, variables, etc. beginning with "_" to be like
>> filenames beginning with ".": completion should work if I have
>> explicitly typed the leading "_", but even substring matching should
>> not *generate* a leading "_".

> I think this is a good explanation of what, imo, the final goal should be.

What do you guys think of the patch below?  I'm open to better names
for the zstyle.. I've tested it a fair amount locally.

thanks,
Greg



Index: Completion/Zsh/Type/_command_names
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Zsh/Type/_command_names,v
retrieving revision 1.9
diff -u -r1.9 _command_names
--- Completion/Zsh/Type/_command_names	19 Aug 2007 23:31:31 -0000	1.9
+++ Completion/Zsh/Type/_command_names	1 Mar 2011 20:28:22 -0000
@@ -4,10 +4,20 @@
 # complete only external commands and executable files. This and a
 # `-' as the first argument is then removed from the arguments.
 
-local args defs
+local args defs hide ffilt
 
 zstyle -t ":completion:${curcontext}:commands" rehash && rehash
 
+zstyle -s ":completion:${curcontext}:functions" hide-internal hide
+case "${hide:-never}" in
+  no|false|never)
+    ffilt='';;
+  yes|true|always)
+    ffilt='[(I)[^_.]*]';;
+  auto|dtrt)
+    [[ $PREFIX = [_.]* ]] && ffilt='' || ffilt='[(I)[^_.]*]';;
+esac
+
 defs=(
   'commands:external command:_path_commands'
 )
@@ -24,7 +34,7 @@
 
   defs=( "$defs[@]"
     'builtins:builtin command:compadd -k builtins'
-    'functions:shell function:compadd -k functions'
+    "functions:shell function:compadd -k functions$ffilt"
     'aliases:alias:compadd -k aliases'
     'suffix-aliases:suffix alias:_suffix_alias_files'
     'reserved-words:reserved word:compadd -k reswords'
Index: Completion/Zsh/Type/_parameters
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Zsh/Type/_parameters,v
retrieving revision 1.1
diff -u -r1.1 _parameters
--- Completion/Zsh/Type/_parameters	2 Apr 2001 11:20:15 -0000	1.1
+++ Completion/Zsh/Type/_parameters	1 Mar 2011 20:28:22 -0000
@@ -6,7 +6,7 @@
 # If you specify a -g option with a pattern, the pattern will be used to
 # restrict the type of parameters matched.
 
-local expl pattern fakes faked tmp
+local expl pattern fakes faked tmp hide pfilt
 
 pattern=(-g \*)
 zparseopts -D -K -E g:=pattern
@@ -23,8 +23,18 @@
   done
 fi
 
+zstyle -s ":completion:${curcontext}:parameters" hide-internal hide
+case "${hide:-never}" in
+  no|false|never)
+    pfilt='';;
+  yes|true|always)
+    pfilt='[^_.]';;
+  auto|dtrt)
+    [[ $PREFIX = [_.]* ]] && pfilt='' || pfilt='[^_.]';;
+esac
+
 _wanted parameters expl parameter \
     compadd "$@" -Q - \
-        "${(@k)parameters[(R)${pattern[2]}~*local*]}" \
+        "${(@M)${(@k)parameters[(R)${pattern[2]}~*local*]}:#${~pfilt}*}" \
         "$fakes[@]" \
         "${(@)${(@M)faked:#${~pattern[2]}}%%:*}"
Index: Doc/Zsh/compsys.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/compsys.yo,v
retrieving revision 1.232
diff -u -r1.232 compsys.yo
--- Doc/Zsh/compsys.yo	17 Dec 2010 17:10:48 -0000	1.232
+++ Doc/Zsh/compsys.yo	1 Mar 2011 20:28:22 -0000
@@ -1658,6 +1658,14 @@
 completions at all, the tt(tag-order) style can be modified as described
 below.
 )
+kindex(hide-internal, completion style)
+item(tt(hide-internal))(
+This style is applicable to the tt(functions) and tt(parameters) completion
+tags.  If tt(true), never complete internal names (i.e. those whose names
+begin with '_' or '.').  If tt(false) (the default) then always complete
+internal names.  If tt(auto), then only complete internal names when the
+leading character is explicitly matched.
+)
 kindex(hosts, completion style)
 item(tt(hosts))(
 A list of names of hosts that should be completed.  If this is not set,



Messages sorted by: Reverse Date, Date, Thread, Author