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

PATCH: dirs builtin



When bash added zsh's dirs builtin, they added a few extra options.
Nothing particularly exciting but this patch adds these two:

  -c clears the stack - equivalent to dirstack=() with zsh/parameter
  -p lists stack entries on their own line (-v without the numbers)

I've not implemented -l which would expand tilde substitutions -
existing code for tilde expansions is mixed in with other expansions so
I'd have to either duplicate or pull apart existing code. Bash also
allows listing of individual stack entries with, e.g. -3. I was going
to add this but can't be bothered now.

The patch includes the documentation and completion changes. I've also
restored _directories to using _files instead of _path_files but made
it default to a description of `directory' instead of `file'. Would it
have been better to use _description instead of _wanted?

Oliver

--- Src/builtin.c	Tue Jan 29 09:59:21 2002
+++ Src/builtin.c	Sat Feb  2 16:29:08 2002
@@ -51,7 +51,7 @@
     BUILTIN("chdir", 0, bin_cd, 0, 2, BIN_CD, NULL, NULL),
     BUILTIN("continue", BINF_PSPECIAL, bin_break, 0, 1, BIN_CONTINUE,
NULL, NULL),
     BUILTIN("declare", BINF_TYPEOPTS | BINF_MAGICEQUALS |
BINF_PSPECIAL, bin_typeset, 0, -1, 0, "AEFHLRTUZafghilrtux", NULL),
-    BUILTIN("dirs", 0, bin_dirs, 0, -1, 0, "v", NULL),
+    BUILTIN("dirs", 0, bin_dirs, 0, -1, 0, "cpv", NULL),
     BUILTIN("disable", 0, bin_enable, 0, -1, BIN_DISABLE, "afmr",
NULL),
     BUILTIN("disown", 0, bin_fg, 0, -1, BIN_DISOWN, NULL, NULL),
     BUILTIN("echo", BINF_PRINTOPTS | BINF_ECHOPTS, bin_print, 0, -1,
BIN_ECHO, "neE", "-"),
@@ -607,37 +607,37 @@
 {
     LinkList l;
 
-    /* with the -v option, provide a numbered list of directories,
starting at
-       zero */
     queue_signals();
-    if (ops['v']) {
+    /* with -v, -p or no arguments display the directory stack */
+    if (!(*argv || ops['c']) || ops['v'] || ops ['p']) {
 	LinkNode node;
+	char *fmt;
 	int pos = 1;
 
-	printf("0\t");
+	/* with the -v option, display a numbered list, starting at zero */
+	if (ops['v']) {
+	    printf("0\t");
+	    fmt = "\n%d\t";
+	/* with the -p option, display entries one per line */
+	} else if (ops['p'])
+	    fmt = "\n";
+	else
+	    fmt = " ";
 	fprintdir(pwd, stdout);
 	for (node = firstnode(dirstack); node; incnode(node)) {
-	    printf("\n%d\t", pos++);
+	    printf(fmt, pos++);
 	    fprintdir(getdata(node), stdout);
 	}
-	putchar('\n');
-	unqueue_signals();
-	return 0;
-    }
-    /* given no arguments, list the stack normally */
-    if (!*argv) {
-	printdirstack();
 	unqueue_signals();
+	putchar('\n');
 	return 0;
     }
     /* replace the stack with the specified directories */
     l = znewlinklist();
-    if (*argv) {
-	while (*argv)
-	    zaddlinknode(l, ztrdup(*argv++));
-	freelinklist(dirstack, freestr);
-	dirstack = l;
-    }
+    while (*argv)
+	zaddlinknode(l, ztrdup(*argv++));
+    freelinklist(dirstack, freestr);
+    dirstack = l;
     unqueue_signals();
     return 0;
 }
--- /dev/null	Sat Apr 14 12:06:21 2001
+++ Completion/Zsh/Command/_dirs	Sat Feb  2 16:40:44 2002
@@ -0,0 +1,7 @@
+#compdef dirs
+
+_arguments -s \
+  '(-)-c[clear the directory stack]' \
+  '(* -c)-v[display numbered list of directory stack]' \
+  '(* -c)-p[display directory entries on per line]' \
+  '(-)*:directory:_directories'
--- Completion/Unix/Type/_directories   Thu Jan  3 13:01:47 2002
+++ Completion/Unix/Type/_directories   Tue Feb 12 20:23:28 2002
@@ -1,3 +1,5 @@
-#compdef rmdir df du dircmp dirs
+#compdef rmdir df du dircmp
 
-_path_files -/ "$@"
+local expl
+
+_wanted directories expl directory _files -/ "$@" -
--- Doc/Zsh/builtins.yo	Mon Jan  7 15:12:50 2002
+++ Doc/Zsh/builtins.yo	Tue Feb 12 20:25:31 2002
@@ -200,15 +200,27 @@
 alias(declare)(typeset)
 findex(dirs)
 cindex(directory stack, printing)
-item(tt(dirs) [ tt(-v) ] [ var(arg) ... ])(
+xitem(tt(dirs) [ tt(-c) ] [ var(arg) ... ])
+item(tt(dirs) [ tt(-pv) ])(
 With no arguments, print the contents of the directory stack.
-If the tt(-v) option is given, number the directories
-in the stack when printing.
 Directories are added to this stack with the tt(pushd) command,
 and removed with the tt(cd) or tt(popd) commands.
 If arguments are specified, load them onto the directory stack,
 replacing anything that was there, and push the current directory
 onto the stack.
+
+startitem()
+item(tt(-c))(
+clear the directory stack.
+)
+item(tt(-p))(
+print directory entries one per line.
+)
+item(tt(-v))(
+number the directories in the stack when printing.
+)
+enditem()
+
 )
 findex(disable)
 cindex(disabling commands)


__________________________________________________
Do You Yahoo!?
Everything you'll ever need on one web page
from News and Sport to Email and Music Charts
http://uk.my.yahoo.com



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