[Tcsh] Simple patch to add "first parameter" matching for "complete"

Jamie Landeg-Jones jamie at catflap.org
Sat Apr 27 23:55:40 UTC 2019


With many command line utilities now using two words for the command
(e.g. "pkg", "gpart", "svn", "git", "zfs", "zpool" etc). I've found
it useful to be able to base completion rules on the value of the
first parameter.

Your currently have 'c', 'C', 'n', 'N', 'p' etc.  I propose adding 'f'
(for "first").

Then you can have such constructs as:

complete pkg 'f|help|`tcsh-completions cmdlist`|' 'f|add|`tcsh-completions add`|' 'f|which|`tcsh-completions which`|' 'f|{fetch,install,search}|`tcsh-completions search`|'
 
.. for coping with commands like "pkg help", "pkg add" etc.

I include a patch below.. Whilst at it, I also added options '1' to '5'
to match the specific numbered parameters (obviously, '1' is the same as
'f') - I thought it may be useful, however, I've not found a use to date.

I've been running this for a few months now.

Could something like this (at least the 'f' option [I don't care if you'd
prefer to assign a different letter for it]) be added? It would be
useful as more and more programs are being released which use this "2
word command" syntax.

If accepted, I'll patch the manpage accordingly.

Cheers, Jamie

--- tcsh/tw.comp.c.orig	2017-04-15 12:13:11.420158000 +0100
+++ tcsh/tw.comp.c	2019-01-10 09:23:36.666850000 +0000
@@ -542,6 +542,22 @@
 	case 'n':
 	    pos = (wordno < 2) ? nomatch : wl[wordno - 2];
 	    break;
+	case 'f':
+	case '1':
+	    pos = wl[1];
+	    break;
+	case '2':
+	    pos = (wordno < 3) ? nomatch : wl[2];
+	    break;
+	case '3':
+	    pos = (wordno < 4) ? nomatch : wl[3];
+	    break;
+	case '4':
+	    pos = (wordno < 5) ? nomatch : wl[4];
+	    break;
+	case '5':
+	    pos = (wordno < 6) ? nomatch : wl[5];
+	    break;
 	case 'c':
 	case 'C':
 	    pos = (wordno < 1) ? nomatch : wl[wordno - 1];
@@ -617,6 +633,12 @@
 
 	case 'N':			/* match with the next-next word */
 	case 'n':			/* match with the next word */
+	case 'f':			/* match with the first word */
+	case '1':			/* match with the first word */
+	case '2':			/* match with the second word */
+	case '3':			/* match with the third word */
+	case '4':			/* match with the fourth word */
+	case '5':			/* match with the fifth word */
 	    exact = 1;
 	    /*FALLTHROUGH*/
 	case 'c':			/* match with the current word */


More information about the Tcsh mailing list