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

Re: Tip of the day: some glob selectors



Peter Stephenson wrote:
> I wonder if it's a useful addition to allow
> 
> ls -l *(+nt)
> explorer ~(+cyg)
> 
> as a shorthand?  The + would swallow up, say, all characters that can
> appear in an identifier (alphanumerics plus underscore) and save it as
> the command.  This would mean you could even chain them: *(+nt+cyg).
> With arguments you're stuck with the long syntax, however.

This is the patch, if anyone is interested.

Index: Doc/Zsh/expn.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/expn.yo,v
retrieving revision 1.49
diff -u -r1.49 expn.yo
--- Doc/Zsh/expn.yo	6 Apr 2004 09:26:50 -0000	1.49
+++ Doc/Zsh/expn.yo	10 Jan 2005 10:29:00 -0000
@@ -1739,7 +1739,8 @@
 permission, and for which other users don't have read or execute
 permission.
 )
-item(tt(e)var(string))(
+xitem(tt(e)var(string))
+item(tt(PLUS())var(cmd))(
 The var(string) will be executed as shell code.  The filename will be
 included in the list if and only if the code returns a zero status (usually
 the status of the last command).  The first character after the `tt(e)'
@@ -1762,6 +1763,19 @@
 the expression `tt(*(e:'reply=(${REPLY}{1,2})':))' will cause the words
 `tt(lonely1 lonely2)' to be inserted into the command line.  Note the
 quotation marks.
+
+The form tt(PLUS())var(cmd) has the same effect, but no delimiters appear
+around var(cmd).  Instead, var(cmd) is taken as the longest sequence of
+characters following the tt(PLUS()) that are alphanumeric or underscore.
+Typically var(cmd) will be the name of a shell function that contains the
+appropriate test.  For example,
+
+example(nt() { [[ $REPLY -nt $NTREF ]] }
+NTREF=reffile
+ls -l *(+nt))
+
+lists all files in the directory that have been modified more recently than
+tt(reffile).
 )
 item(tt(d)var(dev))(
 files on the device var(dev)
Index: Src/glob.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/glob.c,v
retrieving revision 1.37
diff -u -r1.37 glob.c
--- Src/glob.c	26 Oct 2004 17:20:29 -0000	1.37
+++ Src/glob.c	10 Jan 2005 10:29:01 -0000
@@ -1426,22 +1426,43 @@
 		    s++;
 		    break;
 		}
+		case '+':
 		case 'e':
 		{
-		    char sav, *tt = get_strarg(s);
+		    char sav, *tt;
+		    int plus;
 
-		    if (!*tt) {
-			zerr("missing end of string", NULL, 0);
+		    if (s[-1] == '+') {
+			plus = 0;
+			tt = s;
+			while (iident(*tt))
+			    tt++;
+			if (tt == s)
+			{
+			    zerr("missing identifier after `+'", NULL, 0);
+			    tt = NULL;
+			}
+		    } else {
+			plus = 1;
+			tt = get_strarg(s);
+			if (!*tt)
+			{
+			    zerr("missing end of string", NULL, 0);
+			    tt = NULL;
+			}
+		    }
+
+		    if (tt == NULL) {
 			data = 0;
 		    } else {
 			sav = *tt;
 			*tt = '\0';
 			func = qualsheval;
-			sdata = dupstring(s + 1);
+			sdata = dupstring(s + plus);
 			untokenize(sdata);
 			*tt = sav;
 			if (sav)
-			    s = tt + 1;
+			    s = tt + plus;
 			else
 			    s = tt;
 		    }

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


**********************************************************************
This email and any files transmitted with it are confidential and
intended solely for the use of the individual or entity to whom they
are addressed. If you have received this email in error please notify
the system manager.

This footnote also confirms that this email message has been swept by
MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************



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