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

PATCH: glob qualifier to prepend a word



Done properly, this time; fairly small and localised change so it looks
worth having.

The word "prepend" doesn't exist in Chambers, but is already liberally
sprinkled around the documentation.

Index: NEWS
===================================================================
RCS file: /cvsroot/zsh/zsh/NEWS,v
retrieving revision 1.28
diff -u -r1.28 NEWS
--- NEWS	22 Oct 2009 09:28:04 -0000	1.28
+++ NEWS	9 Feb 2010 14:27:08 -0000
@@ -4,6 +4,13 @@
 
 Note also the list of incompatibilities in the README file.
 
+Changes between versions 4.3.10 and 4.3.11
+------------------------------------------
+
+The glob qualifier P can be used to add a separate word before each
+match.  For example, *(P:-f:) produces the command line
+`-f file1 -f file2 ...'.
+
 Changes between versions 4.3.9 and 4.3.10
 -----------------------------------------
 
Index: Completion/Zsh/Type/_globquals
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Zsh/Type/_globquals,v
retrieving revision 1.4
diff -u -r1.4 _globquals
--- Completion/Zsh/Type/_globquals	31 Jan 2010 19:50:44 -0000	1.4
+++ Completion/Zsh/Type/_globquals	9 Feb 2010 14:27:08 -0000
@@ -31,10 +31,10 @@
     fi
     ;;
 
-    (e)
+    ([eP])
     # complete/skip delimited command line
     if [[ -z $PREFIX ]]; then
-      _delimiters qualifer-e
+      _delimiters qualifer-$char
       return
     elif ! _globqual_delims; then
       # still completing command to eval
Index: Doc/Zsh/expn.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/expn.yo,v
retrieving revision 1.107
diff -u -r1.107 expn.yo
--- Doc/Zsh/expn.yo	19 Sep 2009 16:23:22 -0000	1.107
+++ Doc/Zsh/expn.yo	9 Feb 2010 14:27:08 -0000
@@ -2296,6 +2296,18 @@
 them count from the last match backward. E.g.: `tt(*(-OL[1,3]))'
 gives a list of the names of the three largest files.
 )
+item(tt(P))(var(string))(
+The var(string) will be prepended to each glob match as a separate
+word.  var(string) is delimited in the same way as arguments to the
+tt(e) glob qualifier described above.  The qualifier can be repeated;
+the words are prepended separately so that the resulting command
+line contains the words in the same order they were given in the
+list of glob qualifiers.
+
+A typical use for this is to prepend an option before all occurrences
+of a file name; for example, the pattern `tt(*(P:-f:))' produces the
+command line arguments `tt(-f) var(file1) tt(-f) var(file2) ...'
+)
 enditem()
 
 More than one of these lists can be combined, separated by commas. The
Index: Src/glob.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/glob.c,v
retrieving revision 1.70
diff -u -r1.70 glob.c
--- Src/glob.c	24 Mar 2009 12:52:07 -0000	1.70
+++ Src/glob.c	9 Feb 2010 14:27:08 -0000
@@ -176,6 +176,7 @@
     int gd_gf_numsort;
     int gd_gf_follow, gd_gf_sorts, gd_gf_nsorts;
     struct globsort gd_gf_sortlist[MAX_SORTS];
+    LinkList gd_gf_pre_words;
 
     char *gd_glob_pre, *gd_glob_suf;
 };
@@ -206,6 +207,7 @@
 #define gf_sorts      (curglobdata.gd_gf_sorts)
 #define gf_nsorts     (curglobdata.gd_gf_nsorts)
 #define gf_sortlist   (curglobdata.gd_gf_sortlist)
+#define gf_pre_words  (curglobdata.gd_gf_pre_words)
 
 /* and macros for save/restore */
 
@@ -1031,8 +1033,8 @@
 
 
 /*
- * Get a glob string for execution, following e or + qualifiers.
- * Pointer is character after the e or +.
+ * Get a glob string for execution, following e, P or + qualifiers.
+ * Pointer is character after the e, P or +.
  */
 
 /**/
@@ -1072,6 +1074,23 @@
     return sdata;
 }
 
+/*
+ * Insert a glob match.
+ * If there were words to prepend given by the P glob qualifier, do so.
+ */
+static void
+insert_glob_match(LinkList list, LinkNode next, char *data)
+{
+    if (gf_pre_words) {
+	LinkNode added;
+	for (added = firstnode(gf_pre_words); added; incnode(added)) {
+	    next = insertlinknode(list, next, dupstring(getdata(added)));
+	}
+    }
+
+    insertlinknode(list, next, data);
+}
+
 /* Main entry point to the globbing code for filename globbing. *
  * np points to a node in the list list which will be expanded  *
  * into a series of nodes.                                      */
@@ -1606,6 +1625,19 @@
 		    end = v.end;
 		    break;
 		}
+		case 'P':
+		{
+		    char *tt;
+		    tt = glob_exec_string(&s);
+
+		    if (tt != NULL)
+		    {
+			if (!gf_pre_words)
+			    gf_pre_words = newlinklist();
+			addlinknode(gf_pre_words, tt);
+		    }
+		    break;
+		}
 		default:
 		    zerr("unknown file attribute");
 		    restore_globstate(saved);
@@ -1816,14 +1848,14 @@
 	    matchptr = matchbuf + matchct - first - 1;
 	    while (end-- > 0) {
 		/* insert matches in the arg list */
-		insertlinknode(list, node, matchptr->name);
+		insert_glob_match(list, node, matchptr->name);
 		matchptr--;
 	    }
 	} else {
 	    matchptr = matchbuf + matchct - first - end;
 	    while (end-- > 0) {
 		/* insert matches in the arg list */
-		insertlinknode(list, node, matchptr->name);
+		insert_glob_match(list, node, matchptr->name);
 		matchptr++;
 	    }
 	}
Index: Test/D02glob.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/D02glob.ztst,v
retrieving revision 1.15
diff -u -r1.15 D02glob.ztst
--- Test/D02glob.ztst	18 Nov 2008 10:07:31 -0000	1.15
+++ Test/D02glob.ztst	9 Feb 2010 14:27:08 -0000
@@ -343,6 +343,47 @@
 0:Exclusions with complicated path specifications
 >glob.tmp/dir1 glob.tmp/dir2 glob.tmp/dir4
 
+ print -l -- glob.tmp/*(P:-f:)
+0:Prepending words to each argument
+>-f
+>glob.tmp/a
+>-f
+>glob.tmp/b
+>-f
+>glob.tmp/c
+>-f
+>glob.tmp/dir1
+>-f
+>glob.tmp/dir2
+>-f
+>glob.tmp/dir3
+>-f
+>glob.tmp/dir4
+
+ print -l -- glob.tmp/*(P:one word:P:another word:)
+0:Prepending two words to each argument
+>one word
+>another word
+>glob.tmp/a
+>one word
+>another word
+>glob.tmp/b
+>one word
+>another word
+>glob.tmp/c
+>one word
+>another word
+>glob.tmp/dir1
+>one word
+>another word
+>glob.tmp/dir2
+>one word
+>another word
+>glob.tmp/dir3
+>one word
+>another word
+>glob.tmp/dir4
+
  [[ "" = "" ]] && echo OK
 0:Empty strings
 >OK


-- 
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