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

PATCH: Re: completion after ../



Tanaka Akira wrote:

> In article <rsqwvp5ucjx.fsf@xxxxxxxxxxxxxxxxx>,
>   Tanaka Akira <akr@xxxxxxxxxxx> writes:
> 
> > Hm.  In general, I don't want to complete a directory which is already
> > described in the word, with first <TAB> at least.  Most frequently
> > happened example is X/../Z where X/../Z is X.
> 
> I rethought the problem and I found another idea to describe it: A
> directory which shouldn't complete with first <TAB> is the directory
> which go back path.  Of course, this is subset of previous idea but
> it's enough for me.  The directory X/Y/Z is a such directory iff [[ X
> -ef X/Y/Z ]] where Y and Z contains no `/'.  Note that if there is no
> symlinks and in completion context, this happens only when Y == `..'.

Let's try it...

The ignore-parents style may contain a list of the words `parent',
`pwd', `..' and `directory'.

`parent' makes it search for the name of a directory-to-be-completed
in the word from the line, working even with sym-links.

`pwd' is another test that might be useful: it makes the name of the
current working directory be ignored.

If `..' is given, theses tests are only performed if the word on the
line contains the string `../' (maybe this should be the pattern
`(|*/)../*' ?).

If `directory' is given, it does this only when completing with the
pattern `*(-/)' (and only this pattern).

Note however, that there is no automatic switching to list-only and no 
way to get at the ignored directories on a second TAB or something
like this. The ignored names are put in the alternate set so that one
gets them if no other names match.


I think what sometimes irritated me can be fixed with `pwd' or maybe
`pwd ..' and Tanaka's with `parent' or `parent ..'. I hope.


Bye
 Sven

P.S.: Does anyone see a better solution than the two nested loops for
      `parent'?

diff -ru ../z.old/Completion/Builtins/_zstyle Completion/Builtins/_zstyle
--- ../z.old/Completion/Builtins/_zstyle	Fri Jan 21 12:35:43 2000
+++ Completion/Builtins/_zstyle	Fri Jan 21 14:36:44 2000
@@ -28,6 +28,7 @@
   hosts			 c:_hosts
   hosts-ports		 c:host-port
   hosts-ports-users	 c:host-port-user
+  ignore-parents         c:ignorepar
   ignored-patterns	 c:
   insert-unambiguous	 c:bool
   last-prompt		 c:bool
@@ -165,6 +166,11 @@
       else
 	_users
       fi
+      ;;
+
+    ignorepar)
+      _wanted values expl 'which parents to ignore' &&
+        compadd "$expl[@]" parent pwd .. directory
       ;;
 
     _*)
diff -ru ../z.old/Completion/Core/_path_files Completion/Core/_path_files
--- ../z.old/Completion/Core/_path_files	Fri Jan 21 12:35:46 2000
+++ Completion/Core/_path_files	Fri Jan 21 14:31:41 2000
@@ -5,7 +5,7 @@
 
 local linepath realpath donepath prepath testpath exppath
 local tmp1 tmp2 tmp3 tmp4 i orig eorig pre suf tpre tsuf opre osuf cpre
-local pats haspats=no ignore group expl addpfx addsfx remsfx
+local pats haspats=no ignore group expl addpfx addsfx remsfx rem remt
 local nm=$compstate[nmatches] menu mspec matcher mopts atmp sort match
 
 typeset -U prepaths exppaths
@@ -300,8 +300,31 @@
       tmp2=( ${^tmp1}${^~pats} )
       [[ ! -o globdots && "$PREFIX" = .* ]] &&
           tmp2=( "$tmp2[@]" ${^tmp1}.${^~pats} )
+      if (( $#tmp2 )) &&
+         zstyle -s ":completion${curcontext}:files" ignore-parents rem &&
+	 [[ ( "$rem" != *dir* || "$pats" = '*(-/)' ) &&
+	    ( "$rem" != *..* || "$tmp1" = *../* ) ]]; then
+        if [[ "$rem" = *parent* ]]; then
+	  for i in "$tmp2[@]"; do
+	    if [[ -d "$i" && "$i" = */* ]]; then
+	      remt="${i/*}"
+	      while [[ "$remt" = */* ]]; do
+		[[ "$remt" -ef "$i" ]] && break
+		remt="${remt%/*}"
+	      done
+	      [[ "$remt" = */* || "$remt" -ef "$i" ]] &&
+	          _comp_ignore=( "$_comp_ignore[@]" "${(q)i}" )
+	    fi
+	  done
+        fi
+        if [[ "$rem" = *pwd* ]]; then
+          for i in "$tmp2[@]"; do
+	    [[ "$i" -ef "$PWD" ]] && _comp_ignore=( "$_comp_ignore[@]" "${(q)i}" )
+	  done
+        fi
+      fi
       if [[ "$sopt" = *[/f]* && ( -o globdots || "$PREFIX" = .* ) ]] &&
-	 zstyle -s ":completion${curcontext}:paths" special-dirs atmp; then
+	  zstyle -s ":completion${curcontext}:paths" special-dirs atmp; then
 	if [[ "$atmp" = (yes|true|1|on) ]]; then
 	  tmp2=( "$tmp2[@]" . .. )
 	elif [[ "$atmp" = .. ]]; then
diff -ru ../z.old/Doc/Zsh/compsys.yo Doc/Zsh/compsys.yo
--- ../z.old/Doc/Zsh/compsys.yo	Fri Jan 21 12:35:26 2000
+++ Doc/Zsh/compsys.yo	Fri Jan 21 14:33:15 2000
@@ -876,6 +876,29 @@
 Like tt(hosts-ports) but used for commands like tt(telnet) and
 containing strings of the form `var(host)tt(:)var(port)tt(:)var(user)'.
 )
+item(tt(ignore-parents))(
+When completing files it is possible to make names of directories
+already mentioned on the line or the current working directory be
+ignored. The style is tested for the tt(files) tag and if its value
+contains the string tt(parent), then the name of any directory whose
+path is already contained in the word on the line is ignored. For
+example, when completing after tt(foo/../), the directory tt(foo) will
+not be considered a valid completion.
+
+If the style contains the string tt(pwd), then the name of the current 
+working directory will not be completed, so that, for example,
+completion after tt(../) will not use the name of the current
+directory.
+
+If the style contains the string tt(..) both tests will only be
+performed if the word on the line contains the substring tt(../) and
+if the value contains the string tt(directory), then the tests will
+only be performed if only names of directories are completed.
+
+Note that names of directories ignored because of one of the tests
+will be placed in the alternate set of completions so that they will
+be completed if there are no other possible completions.
+)
 item(tt(ignored-patterns))(
 This style is used with the tags used when adding matches and gives a
 number of patterns. All matches that are matched by any of these

--
Sven Wischnowsky                         wischnow@xxxxxxxxxxxxxxxxxxxxxxx



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