zsh-workers
 help / color / mirror / code / Atom feed
* Re: Recursive globbing shorthand (a la **.c)
       [not found] ` <20151029105343.67b579f3@pwslap01u.europe.root.pri>
@ 2015-10-30  9:30   ` Peter Stephenson
  2015-10-30 15:50     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2015-10-30  9:30 UTC (permalink / raw)
  To: Zsh Hackers' List

Here's a patch with documentation.  I've searched this for the
misspelling "shrot" I keep making.  Ist doch nicht Schrott.

pws

diff --git a/Doc/Zsh/expn.yo b/Doc/Zsh/expn.yo
index 5ea8610..20e0c8d 100644
--- a/Doc/Zsh/expn.yo
+++ b/Doc/Zsh/expn.yo
@@ -2381,6 +2381,18 @@ follow symbolic links; the alternative form `tt(***/)' does, but is
 otherwise identical.  Neither of these can be combined with other forms of
 globbing within the same path segment; in that case, the `tt(*)'
 operators revert to their usual effect.
+
+Even shorter forms are available when the option tt(GLOB_STAR_SHORT) is
+set.  In that case if no tt(/) immediately follows a tt(**) or tt(***)
+they are treated as if both a tt(/) plus a further tt(*) are present.
+Hence:
+
+example(setopt GLOBSTARSHORT
+ls **.c)
+
+is equivalent to
+
+example(ls **/*.c)
 subsect(Glob Qualifiers)
 cindex(globbing, qualifiers)
 cindex(qualifiers, globbing)
diff --git a/Doc/Zsh/options.yo b/Doc/Zsh/options.yo
index fbf65ab..60379ca 100644
--- a/Doc/Zsh/options.yo
+++ b/Doc/Zsh/options.yo
@@ -534,6 +534,21 @@ cindex(globbing, of . files)
 item(tt(GLOB_DOTS) (tt(-4)))(
 Do not require a leading `tt(.)' in a filename to be matched explicitly.
 )
+pindex(GLOB_STAR_SHORT)
+pindex(NO_GLOB_STAR_SHORT)
+pindex(GLOBSTARSHORT)
+pindex(NOGLOBSTARSHORT)
+cindex(globbing, short forms)
+cindex(globbing, ** special)
+item(tt(GLOB_STAR_SHORT))(
+When this option is set and the default zsh-style globbing is in
+effect, the pattern `tt(**/*)' can be abbreviated to `tt(**)' and the
+pattern `tt(***/*)' can be abbreviated to tt(***).  Hence `tt(**.c)'
+finds a file ending in tt(.c) in any subdirectory, and `tt(***.c)' does
+the same while also following symbolic links.  A tt(/) immediately
+after the `tt(**)' or `tt(***)' forces the pattern to be treated as the
+unabbreviated form.
+)
 pindex(GLOB_SUBST)
 pindex(NO_GLOB_SUBST)
 pindex(GLOBSUBST)
diff --git a/Src/glob.c b/Src/glob.c
index 24e60d0..51ffeb5 100644
--- a/Src/glob.c
+++ b/Src/glob.c
@@ -682,25 +682,32 @@ parsecomplist(char *instr)
     char *str;
     int compflags = gf_noglobdots ? (PAT_FILE|PAT_NOGLD) : PAT_FILE;
 
-    if (instr[0] == Star && instr[1] == Star &&
-        (instr[2] == '/' || (instr[2] == Star && instr[3] == '/'))) {
-	/* Match any number of directories. */
-	int follow;
-
-	/* with three stars, follow symbolic links */
-	follow = (instr[2] == Star);
-	instr += (3 + follow);
-
-	/* Now get the next path component if there is one. */
-	l1 = (Complist) zhalloc(sizeof *l1);
-	if ((l1->next = parsecomplist(instr)) == NULL) {
-	    errflag |= ERRFLAG_ERROR;
-	    return NULL;
+    if (instr[0] == Star && instr[1] == Star) {
+	int shortglob = 0;
+        if (instr[2] == '/' || (instr[2] == Star && instr[3] == '/')
+	    || (shortglob = isset(GLOBSTARSHORT))) {
+	    /* Match any number of directories. */
+	    int follow;
+
+	    /* with three stars, follow symbolic links */
+	    follow = (instr[2] == Star);
+	    /*
+	     * With GLOBSTARSHORT, leave a star in place for the
+	     * pattern inside the directory.
+	     */
+	    instr += ((shortglob ? 1 : 3) + follow);
+
+	    /* Now get the next path component if there is one. */
+	    l1 = (Complist) zhalloc(sizeof *l1);
+	    if ((l1->next = parsecomplist(instr)) == NULL) {
+		errflag |= ERRFLAG_ERROR;
+		return NULL;
+	    }
+	    l1->pat = patcompile(NULL, compflags | PAT_ANY, NULL);
+	    l1->closure = 1;		/* ...zero or more times. */
+	    l1->follow = follow;
+	    return l1;
 	}
-	l1->pat = patcompile(NULL, compflags | PAT_ANY, NULL);
-	l1->closure = 1;		/* ...zero or more times. */
-	l1->follow = follow;
-	return l1;
     }
 
     /* Parse repeated directories such as (dir/)# and (dir/)## */
diff --git a/Src/options.c b/Src/options.c
index 1fb102f..3bf9f39 100644
--- a/Src/options.c
+++ b/Src/options.c
@@ -140,6 +140,7 @@ static struct optname optns[] = {
 {{NULL, "globassign",	      OPT_EMULATE|OPT_CSH},	 GLOBASSIGN},
 {{NULL, "globcomplete",	      0},			 GLOBCOMPLETE},
 {{NULL, "globdots",	      OPT_EMULATE},		 GLOBDOTS},
+{{NULL, "globstarshort",      OPT_EMULATE},		 GLOBSTARSHORT},
 {{NULL, "globsubst",	      OPT_EMULATE|OPT_NONZSH},	 GLOBSUBST},
 {{NULL, "hashcmds",	      OPT_ALL},			 HASHCMDS},
 {{NULL, "hashdirs",	      OPT_ALL},			 HASHDIRS},
diff --git a/Src/zsh.h b/Src/zsh.h
index d03d171..a6f0397 100644
--- a/Src/zsh.h
+++ b/Src/zsh.h
@@ -2215,6 +2215,7 @@ enum {
     GLOBASSIGN,
     GLOBCOMPLETE,
     GLOBDOTS,
+    GLOBSTARSHORT,
     GLOBSUBST,
     HASHCMDS,
     HASHDIRS,


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

* Re: Recursive globbing shorthand (a la **.c)
  2015-10-30  9:30   ` Recursive globbing shorthand (a la **.c) Peter Stephenson
@ 2015-10-30 15:50     ` Bart Schaefer
  2015-10-31  9:24       ` Stephane Chazelas
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2015-10-30 15:50 UTC (permalink / raw)
  To: Zsh Hackers' List

On Oct 30,  9:30am, Peter Stephenson wrote:
}
} Here's a patch with documentation.

Just for the record, this is a place where I begin to agree with Kurtis's
"all things to all people" criticism.

You'd now best be sure that setting this option isn't going to break
completion six ways from Sunday.


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

* Re: Recursive globbing shorthand (a la **.c)
  2015-10-30 15:50     ` Bart Schaefer
@ 2015-10-31  9:24       ` Stephane Chazelas
  2015-10-31 20:16         ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Stephane Chazelas @ 2015-10-31  9:24 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Zsh Hackers' List

2015-10-30 08:50:35 -0700, Bart Schaefer:
> On Oct 30,  9:30am, Peter Stephenson wrote:
> }
> } Here's a patch with documentation.
> 
> Just for the record, this is a place where I begin to agree with Kurtis's
> "all things to all people" criticism.
> 
> You'd now best be sure that setting this option isn't going to break
> completion six ways from Sunday.

It reminds me of the discussion about:

*$var*/foo

or even

*"$var"*/foo

Which do a  recursive glob when $var is empty.

I suppose with the new option, things like

*$empty* would also have to be considered.

(probably not a big issue. Note that bash -O globstar and ksh93
-o globstar are already affected by that (and yash -o
extended-glob -c 'echo *$empty*/*' like zsh)).

-- 
Stephane


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

* Re: Recursive globbing shorthand (a la **.c)
  2015-10-31  9:24       ` Stephane Chazelas
@ 2015-10-31 20:16         ` Bart Schaefer
  2015-11-01 18:10           ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2015-10-31 20:16 UTC (permalink / raw)
  To: Zsh Hackers' List

On Oct 31,  9:24am, Stephane Chazelas wrote:
} 
} It reminds me of the discussion about:
} 
} *$var*/foo

That and *$~var*.c inadvertently doing a follow-symlinks if var=*

Furtermore if I've got all of globstarshort globcomplete completinword
and the cursor is on the first * out of **.c, what gets completed?  Or
if I DO NOT have completeinword?  When using expand-or-complete-prefix?

I suppose **/ already has all of these problems, but e.g. the guts of
_path_files isn't likely to accidentally cons up that form.

} (probably not a big issue. Note that bash -O globstar and ksh93
} -o globstar are already affected by that (and yash -o
} extended-glob -c 'echo *$empty*/*' like zsh)).

So we need to add globstar as an alias for globstarshort in ksh and bash
emulations?  Or since globstarshort is new should we just chop off the
"short" and extend it to the other emulations?


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

* Re: Recursive globbing shorthand (a la **.c)
  2015-10-31 20:16         ` Bart Schaefer
@ 2015-11-01 18:10           ` Peter Stephenson
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2015-11-01 18:10 UTC (permalink / raw)
  To: Zsh Hackers' List

On Sat, 31 Oct 2015 13:16:10 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:
> So we need to add globstar as an alias for globstarshort in ksh and bash
> emulations?  Or since globstarshort is new should we just chop off the
> "short" and extend it to the other emulations?

It's not quite the same thing, since globstar controls both forms
including the form limited to directories.  However, I see the basic zsh
form is left on when SH_GLOB is active, so it does at least some of the
effect.

I think it would need to have the emulation bit turned *off* ---
i.e. the difference is no longer what you're emulating, the difference
is whether you've set it explicitly in whatever shell you're emulating.
But that's not ideal, either, if you want "emulate -L zsh" to give you a
sane environment including no patter mathcing surprises.

So the conclusion is, um.

pws


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

end of thread, other threads:[~2015-11-01 18:20 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20151028065702.GA8236@linux.vnet.ibm.com>
     [not found] ` <20151029105343.67b579f3@pwslap01u.europe.root.pri>
2015-10-30  9:30   ` Recursive globbing shorthand (a la **.c) Peter Stephenson
2015-10-30 15:50     ` Bart Schaefer
2015-10-31  9:24       ` Stephane Chazelas
2015-10-31 20:16         ` Bart Schaefer
2015-11-01 18:10           ` Peter Stephenson

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