zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: glob qualifier to prepend a word
@ 2010-02-09 14:30 Peter Stephenson
  2010-02-09 15:56 ` Bart Schaefer
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Peter Stephenson @ 2010-02-09 14:30 UTC (permalink / raw)
  To: Zsh hackers list

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@csr.com>            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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: PATCH: glob qualifier to prepend a word
  2010-02-09 14:30 PATCH: glob qualifier to prepend a word Peter Stephenson
@ 2010-02-09 15:56 ` Bart Schaefer
  2010-02-09 17:16 ` Mikael Magnusson
  2010-02-09 20:43 ` Mikael Magnusson
  2 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2010-02-09 15:56 UTC (permalink / raw)
  To: Zsh hackers list

This is nicely done, Peter.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: PATCH: glob qualifier to prepend a word
  2010-02-09 14:30 PATCH: glob qualifier to prepend a word Peter Stephenson
  2010-02-09 15:56 ` Bart Schaefer
@ 2010-02-09 17:16 ` Mikael Magnusson
  2010-02-09 17:21   ` Peter Stephenson
  2010-02-09 20:43 ` Mikael Magnusson
  2 siblings, 1 reply; 6+ messages in thread
From: Mikael Magnusson @ 2010-02-09 17:16 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh hackers list

On 9 February 2010 15:30, Peter Stephenson <pws@csr.com> wrote:
> Done properly, this time; fairly small and localised change so it looks
> worth having.

So my theory on why this can be done after sorting but e can't is that
e can remove matches, which is done before sorting. Is that correct?

As a side thought, mplayer takes global options first on the command
line, then options given after filenames applies to their _left_, so
if you wanted to do something specific to a bunch of certain files
only but not other, you would want an append, not prepend. (I have
never wanted to do this though.. :).

Finally, there's a typo in ChangeLog for Michael's hash fix, it says
"has builtin".

And finally finally, it would be nice if you used -p for your diffs so
the hunk headers show which function we are in. (You can put "diff
-pu" in .cvsrc to make it the default).

-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: PATCH: glob qualifier to prepend a word
  2010-02-09 17:16 ` Mikael Magnusson
@ 2010-02-09 17:21   ` Peter Stephenson
  2010-02-09 20:25     ` Michael Hwang
  0 siblings, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2010-02-09 17:21 UTC (permalink / raw)
  To: Zsh hackers list

Mikael Magnusson wrote:
> So my theory on why this can be done after sorting but e can't is that
> e can remove matches, which is done before sorting. Is that correct?

"e" can play around with matches how it likes, up to and including doing
arbitrary private forms of globbing of its own, so sorting needs to come
after, yes.  I should have said so explicitly.

-- 
Peter Stephenson <pws@csr.com>            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


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: PATCH: glob qualifier to prepend a word
  2010-02-09 17:21   ` Peter Stephenson
@ 2010-02-09 20:25     ` Michael Hwang
  0 siblings, 0 replies; 6+ messages in thread
From: Michael Hwang @ 2010-02-09 20:25 UTC (permalink / raw)
  To: Zsh hackers list

[-- Attachment #1: Type: text/plain, Size: 1061 bytes --]

The zshexpn section is a bit messed up. I see this:

*P**string*.’(

I assume it is supposed to look like:

*P**string*

Michael Hwang

On Tue, Feb 9, 2010 at 12:21 PM, Peter Stephenson <pws@csr.com> wrote:
> Mikael Magnusson wrote:
>> So my theory on why this can be done after sorting but e can't is that
>> e can remove matches, which is done before sorting. Is that correct?
>
> "e" can play around with matches how it likes, up to and including doing
> arbitrary private forms of globbing of its own, so sorting needs to come
> after, yes.  I should have said so explicitly.
>
> --
> Peter Stephenson <pws@csr.com>            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
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: PATCH: glob qualifier to prepend a word
  2010-02-09 14:30 PATCH: glob qualifier to prepend a word Peter Stephenson
  2010-02-09 15:56 ` Bart Schaefer
  2010-02-09 17:16 ` Mikael Magnusson
@ 2010-02-09 20:43 ` Mikael Magnusson
  2 siblings, 0 replies; 6+ messages in thread
From: Mikael Magnusson @ 2010-02-09 20:43 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: Zsh hackers list

On 9 February 2010 15:30, Peter Stephenson <pws@csr.com> wrote:
> Done properly, this time; fairly small and localised change so it looks
> worth having.
> 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))(

This fixes Pstring to look like estring does for me:

--- a/Doc/Zsh/expn.yo
+++ b/Doc/Zsh/expn.yo
@@ -2296,7 +2296,7 @@
 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))(
+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;


-- 
Mikael Magnusson


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2010-02-09 20:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-02-09 14:30 PATCH: glob qualifier to prepend a word Peter Stephenson
2010-02-09 15:56 ` Bart Schaefer
2010-02-09 17:16 ` Mikael Magnusson
2010-02-09 17:21   ` Peter Stephenson
2010-02-09 20:25     ` Michael Hwang
2010-02-09 20:43 ` Mikael Magnusson

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).