zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: use of _arguments for hash
@ 2000-05-12 13:10 Oliver Kiddle
  0 siblings, 0 replies; 5+ messages in thread
From: Oliver Kiddle @ 2000-05-12 13:10 UTC (permalink / raw)
  To: Zsh workers

This adds _arguments based completion for the hash (and rehash)
builtins. Why does rehash accept the -v option when as far as I can
tell, it can never do anything? Would it be useful to add a -L option
to hash similar to alias -L?

I'm getting one problem with this completion: when I type hash -r<tab>,
it is completed to 'hash -d '. I would expect it to complete to
'hash -rd '. I also get 'no more arguments' duplicated after
'hash -d -v<tab>'.

I've also added the + options in _vars_eq for typeset now that the bug
with them is fixed (thanks Sven).

Oliver

Index: Completion/Builtins/_hash
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Builtins/_hash,v
retrieving revision 1.3
diff -u -r1.3 _hash
--- _hash       2000/05/02 08:23:31     1.3
+++ _hash       2000/05/12 13:05:53
@@ -1,16 +1,37 @@
-#compdef hash
+#compdef hash rehash

-local expl
+local state line expl common_args curcontext="$curcontext"
+typeset -A opt_args

-if [[ "$words[2]" = -*d* ]]; then
-  if compset -P 1 '*='; then
-    _wanted -C -d-value files expl directories _path_files -/
+common_args=( \
+  '-d[use named directory hash table]' \
+  '(-r -m -v)-f[rebuild hash table]' )
+
+case ${words[1]} in
+  hash)
+    _arguments -C -s \
+      '(-f -m -v)-r[empty hash table]' \
+      '(-f -r)-m[treat arguments as patterns]' \
+      '(-f -r -m)-v[list entires as they are added]' \
+      "${common_args[@]}" \
+      '(-d -f -r -m -v)*:hash:->hashval' && return 0
+  ;;
+  rehash)
+    _arguments -C -s ${common_args[@]} && return 0
+  ;;
+esac
+
+if [[ $state = hashval ]]; then
+  if (( $+opt_args[-d] )); then
+    if compset -P 1 '*='; then
+      _wanted -C value files expl directories _path_files -/
+    else
+      _wanted -C name named-directories expl 'named directory' \
+          compadd -q -S '=' - "${(@k)nameddirs}"
+    fi
+  elif compset -P 1 '*='; then
+    _wanted -C value values expl 'executable file' _files -g '*(-*)'
   else
-    _wanted -C -d named-directories expl 'named directory' \
-        compadd -q -S '=' - "${(@k)nameddirs}"
+    _wanted -C name commands expl command compadd -q -S '=' - "${(@k)commands}"
   fi
-elif compset -P 1 '*='; then
-  _wanted -C value values expl 'executable file' _files -g '*(-*)'
-else
-  _wanted -C name commands expl command compadd -q -S '=' - "${(@k)commands}"
 fi
Index: Completion/Builtins/_vars_eq
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Builtins/_vars_eq,v
retrieving revision 1.4
diff -u -r1.4 _vars_eq
--- _vars_eq    2000/05/09 16:53:34     1.4
+++ _vars_eq    2000/05/12 13:05:53
@@ -17,19 +17,19 @@
   Z '(-A -E -F -f -i)-Z+[right justify and fill with leading zeros]:width' \
   a '(-A -E -F -T -f -i)-a[specify that arguments refer to arrays]' \
   f '(-A -E -F -L -R -T -Z -a -g -h -i -l -r -x)-f[specify that arguments refer to functions]' \
-  g '(-T -f)-g[do not restrict parameter to local scope]' \
-  h '(-T -f)-h[hide parameter]' \
-  i '(-A -E -F -T -f)-i[represent internally as an integer]' \
+  g '(-T -f)-+g[do not restrict parameter to local scope]' \
+  h '(-T -f)-+h[hide parameter]' \
+  i '(-A -E -F -T -f)-+i[represent internally as an integer]' \
   l '(-T -f)-l[convert the value to lowercase]' \
   m '(-A -E -F -T -i)-m[treat arguments as patterns]' \
-  r '(-f)-r[mark parameters as readonly]' \
-  t '(-T)-t[tag parameters and turn on execution tracing for functions]' \
-  tf '(-T)-t[turn on execution tracing for functions]' \
-  tp '(-T)-t[tag parameters]' \
+  r '(-f)-+r[mark parameters as readonly]' \
+  t '(-T)-+t[tag parameters and turn on execution tracing for functions]' \
+  tf '(-T)-+t[turn on execution tracing for functions]' \
+  tp '(-T)-+t[tag parameters]' \
   u '-u[convert the value to uppercase or mark function for autoloading]' \
   uf '-u[mark function for autoloadling]' \
   up '-u[convert the value to uppercase]' \
-  x '(-f)-x[export parameter]' )
+  x '(-f)-+x[export parameter]' )

 use="AEFLRTUZafghilmrtux"


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

* Re: PATCH: use of _arguments for hash
  2000-05-12 14:33 Sven Wischnowsky
  2000-05-12 14:49 ` Bart Schaefer
@ 2000-05-14 23:24 ` Oliver Kiddle
  1 sibling, 0 replies; 5+ messages in thread
From: Oliver Kiddle @ 2000-05-14 23:24 UTC (permalink / raw)
  To: zsh-workers

Sven Wischnowsky wrote:

> Oliver Kiddle wrote:
> > Why does rehash accept the -v option when as far as I can
> > tell, it can never do anything? Would it be useful to add a -L option
> > to hash similar to alias -L?
> 
> Seems reasonable, but personally I don't care much...

I don't really care a great deal either and I probably would have never
noticed if I hadn't been doing the completion function. Anyway, it
seemed like a simple enough change so I've done it.

I've also changed _vars_eq to handle Peter's change for integer -i.

It's too late too look at this in any more detail but I've just found that
I get a seg fault when I do this:
diff  .c<tab>
     ^cursor here.

Oliver

Index: Completion/Builtins/_hash
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Builtins/_hash,v
retrieving revision 1.4
diff -u -r1.4 _hash
--- Completion/Builtins/_hash	2000/05/12 13:16:52	1.4
+++ Completion/Builtins/_hash	2000/05/15 00:23:24
@@ -13,6 +13,7 @@
       '(-f -m -v)-r[empty hash table]' \
       '(-f -r)-m[treat arguments as patterns]' \
       '(-f -r -m)-v[list entires as they are added]' \
+      '(-f -r -v)-L[list in the form of calls to hash]' \
       "${common_args[@]}" \
       '(-d -f -r -m -v)*:hash:->hashval' && return 0
   ;;
Index: Completion/Builtins/_vars_eq
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Builtins/_vars_eq,v
retrieving revision 1.5
diff -u -r1.5 _vars_eq
--- Completion/Builtins/_vars_eq	2000/05/12 13:16:52	1.5
+++ Completion/Builtins/_vars_eq	2000/05/15 00:23:24
@@ -39,7 +39,7 @@
     use="Umtu"
     func=f
   ;;
-  integer) use="ghlrtux" ;;
+  integer) use="ghilrtux" ;;
   readonly) use="${use/r/}" ;;
   local) use="${use/f/}" ;&
   export) use="${${use/g/}/x/}" ;;
Index: Doc/Zsh/builtins.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/builtins.yo,v
retrieving revision 1.8
diff -u -r1.8 builtins.yo
--- Doc/Zsh/builtins.yo	2000/05/14 22:08:41	1.8
+++ Doc/Zsh/builtins.yo	2000/05/15 00:23:32
@@ -452,7 +452,7 @@
 nonzero when there are no more options.
 )
 findex(hash)
-item(tt(hash) [ tt(-dfmrv) ] [ var(name)[tt(=)var(value)] ] ...)(
+item(tt(hash) [ tt(-Ldfmrv) ] [ var(name)[tt(=)var(value)] ] ...)(
 tt(hash) can be used to directly modify the contents of the command
 hash table, and the named directory hash table.  Normally one would
 modify these tables by modifying one's tt(PATH)
@@ -494,6 +494,9 @@
 
 The tt(-v) option causes hash table entries to be listed as they are
 added by explicit specification.  If has no effect if used with tt(-f).
+
+If the tt(-L) flag is present, then each hash table entry is printed in
+the form of a call to hash.
 )
 alias(history)(fc -l)
 findex(integer)
Index: Src/builtin.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/builtin.c,v
retrieving revision 1.16
diff -u -r1.16 builtin.c
--- Src/builtin.c	2000/05/14 22:08:42	1.16
+++ Src/builtin.c	2000/05/15 00:23:44
@@ -68,7 +68,7 @@
     BUILTIN("functions", BINF_TYPEOPTS, bin_functions, 0, -1, 0, "mtuU", NULL),
     BUILTIN("getln", 0, bin_read, 0, -1, 0, "ecnAlE", "zr"),
     BUILTIN("getopts", 0, bin_getopts, 2, -1, 0, NULL, NULL),
-    BUILTIN("hash", BINF_MAGICEQUALS, bin_hash, 0, -1, 0, "dfmrv", NULL),
+    BUILTIN("hash", BINF_MAGICEQUALS, bin_hash, 0, -1, 0, "Ldfmrv", NULL),
 
 #ifdef ZSH_HASH_DEBUG
     BUILTIN("hashinfo", 0, bin_hashinfo, 0, 0, 0, NULL, NULL),
@@ -99,7 +99,7 @@
     BUILTIN("r", BINF_R, bin_fc, 0, -1, BIN_FC, "nrl", NULL),
     BUILTIN("read", 0, bin_read, 0, -1, 0, "rzu0123456789pkqecnAlE", NULL),
     BUILTIN("readonly", BINF_TYPEOPTS | BINF_MAGICEQUALS | BINF_PSPECIAL, bin_typeset, 0, -1, 0, "AEFLRTUZafghiltux", "r"),
-    BUILTIN("rehash", 0, bin_hash, 0, 0, 0, "dfv", "r"),
+    BUILTIN("rehash", 0, bin_hash, 0, 0, 0, "df", "r"),
     BUILTIN("return", BINF_PSPECIAL, bin_break, 0, 1, BIN_RETURN, NULL, NULL),
     BUILTIN("set", BINF_PSPECIAL, bin_set, 0, -1, 0, NULL, NULL),
     BUILTIN("setopt", 0, bin_setopt, 0, -1, BIN_SETOPT, NULL, NULL),
@@ -2461,6 +2461,7 @@
     Patprog pprog;
     Asgment asg;
     int returnval = 0;
+    int printflags = 0;
 
     if (ops['d'])
 	ht = nameddirtab;
@@ -2485,9 +2486,11 @@
 	return 0;
     }
 
+    if (ops['L']) printflags |= PRINT_LIST;
+
     /* Given no arguments, display current hash table. */
     if (!*argv) {
-	scanhashtable(ht, 1, 0, 0, ht->printnode, 0);
+	scanhashtable(ht, 1, 0, 0, ht->printnode, printflags);
 	return 0;
     }
 
@@ -2498,7 +2501,7 @@
 	    tokenize(*argv);  /* expand */
 	    if ((pprog = patcompile(*argv, PAT_STATIC, NULL))) {
 		/* display matching hash table elements */
-		scanmatchtable(ht, pprog, 0, 0, ht->printnode, 0);
+		scanmatchtable(ht, pprog, 0, 0, ht->printnode, printflags);
 	    } else {
 		untokenize(*argv);
 		zwarnnam(name, "bad pattern : %s", *argv, 0);
Index: Src/hashtable.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/hashtable.c,v
retrieving revision 1.6
diff -u -r1.6 hashtable.c
--- Src/hashtable.c	2000/04/30 17:09:08	1.6
+++ Src/hashtable.c	2000/05/15 00:23:48
@@ -734,6 +734,13 @@
 	return;
     }
 
+    if (printflags & PRINT_LIST) {
+	printf("hash ");
+
+	if(cn->nam[0] == '-')
+	    printf("-- ");
+    }
+
     if (cn->flags & HASHED) {
 	quotedzputs(cn->nam, stdout);
 	putchar('=');
@@ -1370,6 +1377,13 @@
 	zputs(nd->nam, stdout);
 	putchar('\n');
 	return;
+    }
+    
+    if (printflags & PRINT_LIST) {
+      printf("hash -d ");
+
+      if(nd->nam[0] == '-')
+	    printf("-- ");
     }
 
     quotedzputs(nd->nam, stdout);


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

* Re: PATCH: use of _arguments for hash
  2000-05-12 14:49 ` Bart Schaefer
@ 2000-05-12 17:46   ` Peter Stephenson
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2000-05-12 17:46 UTC (permalink / raw)
  To: Zsh hackers list

Bart wrote:
> What I've been wishing for is "zle -L".  As far as I can tell there's no
> way to report what widgets are user-defined or what functions they call.

While we're on the subject, `bindkey -l' and `bindkey -lL' aren't much use.
The current keymap is the one aliased to main (or is main linked to
it...?), but there's no indication of which that is, and the implication
that all keymaps were created with `bindkey -N new-keymap' is also wrong if
the keymap was created as a copy of another one.

I can't remember how long it is since I started using zsh, but I *still*
can't remember if doing `bindkey -v' then `bindkey -e' or vice versa
restores all the special bindings you had before.  The answer seems to be
yes.  Cool.

-- 
Peter Stephenson <pws@cambridgesiliconradio.com>
Cambridge Silicon Radio, Unit 300, Science Park, Milton Road,
Cambridge, CB4 0XL, UK                          Tel: +44 (0)1223 392070


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

* Re: PATCH: use of _arguments for hash
  2000-05-12 14:33 Sven Wischnowsky
@ 2000-05-12 14:49 ` Bart Schaefer
  2000-05-12 17:46   ` Peter Stephenson
  2000-05-14 23:24 ` Oliver Kiddle
  1 sibling, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2000-05-12 14:49 UTC (permalink / raw)
  To: zsh-workers

On May 12,  4:33pm, Sven Wischnowsky wrote:
} Subject: Re: PATCH: use of _arguments for hash
}
} Oliver Kiddle wrote:
} 
} > Would it be useful to add a -L option to hash similar to alias -L?
} 
} Seems reasonable, but personally I don't care much...

What I've been wishing for is "zle -L".  As far as I can tell there's no
way to report what widgets are user-defined or what functions they call.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


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

* Re: PATCH: use of _arguments for hash
@ 2000-05-12 14:33 Sven Wischnowsky
  2000-05-12 14:49 ` Bart Schaefer
  2000-05-14 23:24 ` Oliver Kiddle
  0 siblings, 2 replies; 5+ messages in thread
From: Sven Wischnowsky @ 2000-05-12 14:33 UTC (permalink / raw)
  To: zsh-workers


Oliver Kiddle wrote:

> This adds _arguments based completion for the hash (and rehash)
> builtins. Why does rehash accept the -v option when as far as I can
> tell, it can never do anything? Would it be useful to add a -L option
> to hash similar to alias -L?

Seems reasonable, but personally I don't care much...

> I'm getting one problem with this completion: when I type hash -r<tab>,
> it is completed to 'hash -d '. I would expect it to complete to
> 'hash -rd '.

Yes, a bit of confusion over the options that still could be
completed. The next step, completing `-rd<TAB>' wasn't correct either, 
for similar reasons.

> I also get 'no more arguments' duplicated after
> 'hash -d -v<tab>'.

Oops. That was `caused' by _approximate defining `compadd' which used
a different group.

Bye
 Sven

Index: Completion/Base/_arguments
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Base/_arguments,v
retrieving revision 1.16
diff -u -r1.16 _arguments
--- Completion/Base/_arguments	2000/05/11 11:27:40	1.16
+++ Completion/Base/_arguments	2000/05/12 14:33:08
@@ -184,12 +184,16 @@
     else
       noargs='no arguments'
     fi
-    if ! comparguments -O next direct odirect equal; then
+    if comparguments -O next direct odirect equal; then
+      opts=yes
+      _tags options
+    elif [[ $? -eq 2 ]]; then
+        compadd -Q - "${PREFIX}${SUFFIX}"
+        return 0
+    else
       _message "$noargs"
       return 1
     fi
-    opts=yes
-    _tags options
   fi
 
   context=()
Index: Completion/Core/_message
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Core/_message,v
retrieving revision 1.3
diff -u -r1.3 _message
--- Completion/Core/_message	2000/05/05 11:21:50	1.3
+++ Completion/Core/_message	2000/05/12 14:33:08
@@ -15,6 +15,6 @@
 
 if [[ -n "$format$raw" ]]; then
   [[ -z "$raw" ]] && zformat -f format "$format" "d:$1" "${(@)argv[2,-1]}"
-  compadd -x "$format"
+  builtin compadd -x "$format"
   _comp_mesg=yes
 fi
Index: Src/Zle/computil.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/computil.c,v
retrieving revision 1.20
diff -u -r1.20 computil.c
--- Src/Zle/computil.c	2000/05/11 11:27:40	1.20
+++ Src/Zle/computil.c	2000/05/12 14:33:12
@@ -1124,7 +1124,7 @@
     Cadef d;
     int nopts;
     Caarg def, ddef;
-    Caopt curopt;
+    Caopt curopt, dopt;
     int opt, arg, argbeg, optbeg, nargbeg, restbeg, curpos, argend;
     int inopt, inrest, inarg, nth, doff, singles, oopt, actopts;
     LinkList args;
@@ -1140,7 +1140,7 @@
 ca_parse_line(Cadef d, int multi)
 {
     Caarg adef, ddef;
-    Caopt ptr, wasopt;
+    Caopt ptr, wasopt, dopt;
     struct castate state;
     char *line, *pe, **argxor = NULL;
     int cur, doff, argend;
@@ -1174,7 +1174,7 @@
     state.d = d;
     state.nopts = d->nopts;
     state.def = state.ddef = NULL;
-    state.curopt = NULL;
+    state.curopt = state.dopt = NULL;
     state.argbeg = state.optbeg = state.nargbeg = state.restbeg = state.actopts =
 	state.nth = state.inopt = state.inarg = state.opt = state.arg = 1;
     state.argend = argend = arrlen(compwords) - 1;
@@ -1198,6 +1198,7 @@
     for (line = compwords[1], cur = 2, state.curopt = NULL, state.def = NULL;
 	 line; line = compwords[cur++]) {
 	ddef = adef = NULL;
+	dopt = NULL;
 	doff = state.singles = 0;
 
 	if (ca_inactive(d, argxor, cur, 0) ||
@@ -1251,9 +1252,11 @@
 	     (state.curopt->type == CAO_EQUAL ?
 	      (pe[-1] == '=' || !pe[0]) : 1))) {
 
-	    ddef = state.def = ((state.curopt->type != CAO_EQUAL ||
-				 pe[-1] == '=') ?
-				state.curopt->args : NULL);
+	    if ((ddef = state.def = ((state.curopt->type != CAO_EQUAL ||
+				      pe[-1] == '=') ?
+				     state.curopt->args : NULL)))
+		dopt = state.curopt;
+
 	    doff = pe - line;
 	    state.optbeg = state.argbeg = state.inopt = cur;
 	    state.argend = argend;
@@ -1296,6 +1299,7 @@
 	    Caopt tmpopt;
 
 	    ddef = state.def = state.curopt->args;
+	    dopt = state.curopt;
 	    doff = pe - line;
 	    state.optbeg = state.argbeg = state.inopt = cur;
 	    state.argend = argend;
@@ -1355,6 +1359,7 @@
 
 		memcpy(&ca_laststate, &state, sizeof(state));
 		ca_laststate.ddef = NULL;
+		ca_laststate.dopt = NULL;
 		ca_laststate.doff = 0;
 		break;
 	    }
@@ -1389,6 +1394,7 @@
 		    zaddlinknode(l, ztrdup(line));
 
 		ca_laststate.ddef = NULL;
+		ca_laststate.dopt = NULL;
 		ca_laststate.doff = 0;
 		break;
 	    }
@@ -1402,12 +1408,14 @@
 	if (cur + 1 == compcurrent) {
 	    memcpy(&ca_laststate, &state, sizeof(state));
 	    ca_laststate.ddef = NULL;
+	    ca_laststate.dopt = NULL;
 	    ca_laststate.doff = 0;
 	} else if (cur == compcurrent && !ca_laststate.def) {
 	    if ((ca_laststate.def = ddef)) {
 		ca_laststate.singles = state.singles;
 		if (state.curopt && state.curopt->type == CAO_NEXT) {
 		    ca_laststate.ddef = ddef;
+		    ca_laststate.dopt = dopt;
 		    ca_laststate.def = NULL;
 		    ca_laststate.opt = 1;
 		    state.curopt->active = 1;
@@ -1418,6 +1426,7 @@
 	    } else {
 		ca_laststate.def = adef;
 		ca_laststate.ddef = NULL;
+		ca_laststate.dopt = NULL;
 		ca_laststate.optbeg = state.nargbeg;
 		ca_laststate.argbeg = state.restbeg;
 		ca_laststate.argend = state.argend;
@@ -1705,7 +1714,7 @@
 
 	    return 0;
 	}
-	return 1;
+	return (ca_laststate.singles ? 2 : 1);
     case 'L':
 	{
 	    Caopt opt = ca_get_opt(ca_laststate.d, args[1], 1, NULL);
@@ -1719,13 +1728,13 @@
 	}
     case 's':
 	if (ca_laststate.d->single && ca_laststate.singles &&
-	    ca_laststate.actopts > 1 && ca_laststate.opt) {
+	    ca_laststate.actopts && ca_laststate.opt) {
 	    setsparam(args[1],
-		      ztrdup(ca_laststate.ddef ?
-			     (ca_laststate.ddef->type == CAO_DIRECT ?
+		      ztrdup((ca_laststate.ddef && ca_laststate.dopt) ?
+			     (ca_laststate.dopt->type == CAO_DIRECT ?
 			      "direct" :
-			      ((ca_laststate.ddef->type == CAO_OEQUAL ||
-				ca_laststate.ddef->type == CAO_EQUAL) ?
+			      ((ca_laststate.dopt->type == CAO_OEQUAL ||
+				ca_laststate.dopt->type == CAO_EQUAL) ?
 			       "equal" : "next")) : ""));
 	    return 0;
 	}

--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


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

end of thread, other threads:[~2000-05-15  7:12 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-05-12 13:10 PATCH: use of _arguments for hash Oliver Kiddle
2000-05-12 14:33 Sven Wischnowsky
2000-05-12 14:49 ` Bart Schaefer
2000-05-12 17:46   ` Peter Stephenson
2000-05-14 23:24 ` Oliver Kiddle

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