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

Re: [PATCH] run-help: ugly workaround for run-help-$X with alias for $X



On Mon,  7 Dec 2009 01:57:10 +0100
JÃrg Sommer <joerg@xxxxxxxxxxxx> wrote:
> If you're very lazy and define an alias for git, e.g. g, and have the
> function run-help-git defined, run-help will fail to strip everything up
> to the expanded command from the commandline. In the first call of
> run-help, the alias g gets expanded to git and run-help is called for
> git. But the test of the while loop will never succed, because the
> commandline fetched with getln doesn't contain the expanded command git.
> Hence everything gets shifted from the array cmd_args until shift cries
> forever
> 
> run-help:shift:101: shift count must be <= $#
> 
> I know, this solution is a dirty hack, but it's quick. The better way is
> to fix zsh to call run-help with the whole commandline where the alias
> gets expanded and this commandline gets passed to the second run-help
> call.

I meant to post this slightly tidied up version (it's basically the
same logic) but forgot.

Index: Functions/Misc/run-help
===================================================================
RCS file: /cvsroot/zsh/zsh/Functions/Misc/run-help,v
retrieving revision 1.16
diff -u -r1.16 run-help
--- Functions/Misc/run-help	5 Jun 2009 11:18:01 -0000	1.16
+++ Functions/Misc/run-help	10 Dec 2009 09:48:44 -0000
@@ -49,6 +49,7 @@
   noalias=1
 fi
 
+{
 while ((i++ < $#places))
 do
     what=$places[$i]
@@ -56,7 +57,8 @@
     builtin print -r $what
     case $what in
     (*( is an alias)*)
-	[[ ${what[(w)6]:t} != ${what[(w)1]} ]] && run-help ${what[(w)6]:t}
+	[[ ${what[(w)6]:t} != ${what[(w)1]} ]] &&
+	  run_help_orig_cmd=${what[(w)1]} run-help ${what[(w)6]:t}
 	;;
     (*( is a * function))
 	case ${what[(w)1]} in
@@ -96,9 +98,9 @@
 		builtin print -z "$cmd_args"
 		cmd_args=( ${(z)cmd_args} )
 		# Discard environment assignments, etc.
-		while [[ $cmd_args[1] != $1 ]]
+		while [[ $cmd_args[1] != ${run_help_orig_cmd:-$1} ]]
 		do
-		    shift cmd_args
+		    shift cmd_args || return 1
 		done
 		eval "run-help-$1:t ${(q@)cmd_args[2,-1]}"
 	    else
@@ -115,3 +117,6 @@
 	[[ $what == [qQ] ]] && break
     fi
 done
+} always {
+  unset run_help_orig_cmd
+}


-- 
Peter Stephenson <pws@xxxxxxx>            Software Engineer
Tel: +44 (0)1223 692070                   Cambridge Silicon Radio Limited
Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, UK


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom



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