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

Re: PATCH: Re: completion function for apachectl



Bart Schaefer wrote:

> On Feb 26,  6:17pm, Andrej Borsenkow wrote:
> }
> } > +_sub_commands start stop
> } 
> } Well, Linux strikes again. You can be sure only in the above; restart and
> } status are not universally supported.
> 
> One could probably pull a stunt like this:
> 
> _sub_commands $(sed -ne '/case/,/esac/{;s/^ *\([a-z|)]*\).*/\1/g;s/[|)]/ /gp;}' $words[1])
> 
> Runs a slight risk of finding case-labels that aren't actually part of the
> argument parsing -- on a RedHat 6.2 machine it finds y, n, and c in one of
> the scripts -- but for the most part it's pretty accurate.
> 
> Tuning the regex and adapting to older seds left as an exercise.

Using external programs?  To quote Peter: pah!

The thing below works for me.  To make Andrej happy it searches for a
couple of known words instead of using every line that looks like...


Bye
 Sven

Index: Completion/User/_init_d
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/User/_init_d,v
retrieving revision 1.1
diff -u -r1.1 _init_d
--- Completion/User/_init_d	2001/02/26 15:05:37	1.1
+++ Completion/User/_init_d	2001/03/08 10:15:00
@@ -1,5 +1,22 @@
 #compdef -P */(init|rc[0-9]#).d/*
 
+local magic cmds what
+
 # This should probably be system specific...
+
+# If the file starts with `#!' we hope that this is a shell script
+# and get lines looking like <space>foo|bar) with the words in $what.
+
+what='(st(art|op|atus)|re(start|load)|debug_(up|down)|dump(|_stats)|add|delete|clean|list)'
+
+read -u0k 2 magic < $words[1] && [[ $magic = '#!' ]] &&
+    cmds=( ${${(j:|:s:|:)${(M)${(f)"$(< $words[1])"}:#[[:blank:]]#(\'|)${~what}(|${~what})#(\'|)\)}}//[^a-z_]} )
+
+# This would be the pattern to use every line of the form <space>foo).
+# Some people say this might match too many lines...
+#
+#    cmds=( ${${(j:|:s:|:)${(M)${(f)"$(< $words[1])"}:#[[:blank:]]#(\'|)[a-z_|]##(\'|)\)}}//[^a-z_]} )
+
+(( $#cmds )) || cmds=(start stop)
 
-_sub_commands start stop
+_sub_commands $cmds

--
Sven Wischnowsky                         wischnow@xxxxxxxxxxxxxxxxxxxxxxx



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