zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: Suggested tilde-keeping in expansions
@ 2000-06-07 16:00 Peter Stephenson
  0 siblings, 0 replies; only message in thread
From: Peter Stephenson @ 2000-06-07 16:00 UTC (permalink / raw)
  To: Zsh hackers list

How about something like this?  The style keep-tilde defaults to on and
makes _expand restore the ~/ or ~foo/ part of an expression after globbing.
It doesn't need to default to on.  It could probably usefully take a third
value indicating you would like the ~ expanded if there were no other
changes --- that might be a good default.

I won't commit this because I'm sure someone will have suggestions for
expansions, deletions, changes, or complete rewrites.  For example, you
could restore initial parameter expressions such as ${HOME}/ in a similar
way if the glob succeeded (or subst-globs-only is true).  But we could do
with something or other along these lines.

It's a different question, but maybe we could set nonomatch for the
duration of the function to handle failed matches more gracefully.  Or
maybe people who don't set nonomatch (i.e. do set no nonomatch) like it the
way it is.

Index: Completion/Core/_expand
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Core/_expand,v
retrieving revision 1.14
diff -u -r1.14 _expand
--- Completion/Core/_expand	2000/06/06 13:04:10	1.14
+++ Completion/Core/_expand	2000/06/07 15:49:15
@@ -12,6 +12,7 @@
 [[ _matcher_num -gt 1 ]] && return 1
 
 local exp word sort expr expl subd suf=" " force opt
+local tilde untilde match mbegin mend
 
 (( $# )) &&
     while getopts gsco opt; do
@@ -60,11 +61,22 @@
 
 # Now try globbing.
 
-[[ "$force" = *g* ]] ||
+if [[ "$force" = *g* ]] ||
   { { zstyle -s ":completion:${curcontext}:" glob expr ||
       { [[ "$curcontext" = expand-word:* ]] && expr=1 } } &&
-        [[ "${(e):-\$[$expr]}" -eq 1 ]] } &&
+        [[ "${(e):-\$[$expr]}" -eq 1 ]] }; then
+    if zstyle -T ":completion:${curcontext}:" keep-tilde &&
+      [[ $exp = (#b)(\~[^/]#)/* ]]; then
+      # Attempt to keep a ~dir initial segment intact:  remember
+      # its expansion so as to replace that back with the original.
+      tilde=$match[1]
+      untilde=(${~tilde})
+    fi
     exp=( ${~exp} )
+    if [[ -n $expr ]]; then
+      exp=( ${exp/(#s)$untilde/$tilde} )
+    fi
+fi
 
 # If we don't have any expansions or only one and that is the same
 # as the original string, we let other completers run.
Index: Doc/Zsh/compsys.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/compsys.yo,v
retrieving revision 1.59
diff -u -r1.59 compsys.yo
--- Doc/Zsh/compsys.yo	2000/05/29 14:48:38	1.59
+++ Doc/Zsh/compsys.yo	2000/06/07 15:49:16
@@ -1283,6 +1283,13 @@
 tt(approximate-)var(num), where var(num) is the number of errors that
 were accepted.
 )
+kindex(keep-tilde, completion style)
+item(tt(keep-tilde))(
+Used by the tt(_expand) completer.  By default, expansions of expressions
+beginning with a named directory, i.e. tt(~/) or tt(~)var(name)tt(/),
+will keep that part unchanged.  If this style is set to one of the false
+values, a full expansion will be performed instead.
+)
 kindex(last-prompt, completion style)
 item(tt(last-prompt))(
 This is used to determine if the completion code should try to put the

-- 
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] only message in thread

only message in thread, other threads:[~2000-06-07 16:01 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-06-07 16:00 PATCH: Suggested tilde-keeping in expansions 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).