zsh-users
 help / color / mirror / code / Atom feed
* Adding array content to cd completion
@ 2006-11-15  5:09 Jean-Rene David
  2006-11-15  5:27 ` arno.
  2006-11-15 11:08 ` Peter Stephenson
  0 siblings, 2 replies; 5+ messages in thread
From: Jean-Rene David @ 2006-11-15  5:09 UTC (permalink / raw)
  To: zsh-users

I have an array of directories I frequently visit.
I would like the completion of "cd" to include
those directories, as a separate group.

I put the following in my .zshrc. It works but is
ugly and crude. There must be a better way to do
it.

---------------%<----------------
mycompletion() {
   _cd
   compadd -X Jumps -a jumplist
}
compdef mycompletion cd
---------------%<----------------

Suggestions?

-- 
JR


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

* Re: Adding array content to cd completion
  2006-11-15  5:09 Adding array content to cd completion Jean-Rene David
@ 2006-11-15  5:27 ` arno.
  2006-11-15  6:09   ` Jean-Rene David
  2006-11-15 11:08 ` Peter Stephenson
  1 sibling, 1 reply; 5+ messages in thread
From: arno. @ 2006-11-15  5:27 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 1692 bytes --]

Le Wednesday 15 November 2006, à 00:09:38AM -0500, Jean-Rene a écrit : 
> I have an array of directories I frequently visit.
> I would like the completion of "cd" to include
> those directories, as a separate group.
> 
> I put the following in my .zshrc. It works but is
> ugly and crude. There must be a better way to do
> it.
> 
> ---------------%<----------------
> mycompletion() {
>    _cd
>    compadd -X Jumps -a jumplist
> }
> compdef mycompletion cd
> ---------------%<----------------
> 
> Suggestions?

Is it "named directories" you are looking for ?                                                                                              
ex : I have :                                                                                                                                
hash -d doc="/usr/share/doc"                                                                                                                 
in my .zshrc, and cd ~doc always puts me to /usr/share/doc                                                                                   
Take also a look at CDABLE_VARS option                                                                                                              
                                                                                                                                             
It's not exactly what you are asking, but may be it could help.                                                                              
                                                                                                                                             
arno. 

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]

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

* Re: Adding array content to cd completion
  2006-11-15  5:27 ` arno.
@ 2006-11-15  6:09   ` Jean-Rene David
  0 siblings, 0 replies; 5+ messages in thread
From: Jean-Rene David @ 2006-11-15  6:09 UTC (permalink / raw)
  To: zsh-users

* arno. [2006.11.15 00:30]:
> Is it "named directories" you are looking for ?                                                                                              

Thanks, but no. I already use those, the problem
is they are static (see below).

> It's not exactly what you are asking, but may be
> it could help.                                                                              

The directory array is dynamic. It is kind of like
the directory stack -- a history of the
directories I've visited -- with some differences.

But in fact the exact purpose isn't really that
important: I'm doing this to learn how to
customize the completion system.

I've figured out from various sources how to
create a completion widget but I haven't yet found
an elegant way to just add some matches to an
existing completer function. Hence my question.

Maybe just hacking _cd would be the best way to
learn...

-- 
JR


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

* Re: Adding array content to cd completion
  2006-11-15  5:09 Adding array content to cd completion Jean-Rene David
  2006-11-15  5:27 ` arno.
@ 2006-11-15 11:08 ` Peter Stephenson
  2006-11-15 14:54   ` Peter Stephenson
  1 sibling, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2006-11-15 11:08 UTC (permalink / raw)
  To: zsh-users

Jean-Rene David <jrdavid@magma.ca> wrote:
> I have an array of directories I frequently visit.
> I would like the completion of "cd" to include
> those directories, as a separate group.

It depends how much work you want to do.

If you don't mind them appearing as named directories you can do:

mydirs=(mydir1 mydir1)
zstyle -e ':completion::complete:cd:*:named-directories' fake '$mydirs'

If you really want a separate group it's harder.  The whole completion
system is written so that only a fixed set of tags are available for each
completion.

It's possible to use the tag-order to split a group into two, as described
in the manual.  You can use this with, say, the named-directories tag to
have a normal completion and your own context.  In your own context you
have to add your own matches using the fake style and then (the hairiest
part of the exercise) remove the normal matches with ignored-patterns.
Luckily in this case the matches to be ignored are parameter names
available from the associatiave array $nameddirs:

zstyle ':completion::complete:cd:*' tag-order \
  'named-directories:-mine:extra\ directories
named-directories:-normal:named\ directories *'
mydirs=(mydir1 mydir2)
zstyle -e ':completion::complete:cd:*:named-directories-mine' fake \
  'reply=($mydirs)'
zstyle -e ':completion::complete:cd:*:named-directories-mine' \
ignored-patterns 'reply=("(${(qkj.|.)nameddirs})")'

This seems to work.  It's not clear it's easier than redefining _cd.

Arguably there should be an easier way of saying only use the fake matches.
I think that's quite difficult since all the normal matches have been added
and are in the shell's internal structures by the time that style is
processed.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


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

* Re: Adding array content to cd completion
  2006-11-15 11:08 ` Peter Stephenson
@ 2006-11-15 14:54   ` Peter Stephenson
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2006-11-15 14:54 UTC (permalink / raw)
  To: zsh-users

Peter Stephenson <pws@csr.com> wrote:
> Arguably there should be an easier way of saying only use the fake matches.
> I think that's quite difficult since all the normal matches have been added
> and are in the shell's internal structures by the time that style is
> processed.

Not quite true:  we add the normal matches with the options being
specified by _description.  There's a trick we can use: if ignored-patterns
applies to the normal completions, but not to fake completions, we can
completely override normal completions by ignoring them all.  This means
you can add an extra match group anywhere you like by supplementing an
existing tag.

I've added the fake-always style to do this.

Index: Completion/Base/Core/_description
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Base/Core/_description,v
retrieving revision 1.5
diff -u -r1.5 _description
--- Completion/Base/Core/_description	21 Jul 2003 17:01:44 -0000	1.5
+++ Completion/Base/Core/_description	15 Nov 2006 14:51:46 -0000
@@ -58,7 +58,9 @@
 				  "${(@)words[CURRENT+1,-1]}" );;
     esac
 
-  (( $#_comp_ignore )) && opts=( $opts -F _comp_ignore )
+  # Ensure the ignore option is first so we can override it
+  # for fake-always.
+  (( $#_comp_ignore )) && opts=( -F _comp_ignore $opts )
 else
   _comp_ignore=()
 fi
@@ -86,15 +88,21 @@
   fi
 fi
 
-if ! (( ${funcstack[2,-1][(I)_description]} )) &&
-   zstyle -a ":completion:${curcontext}:$tag" fake match; then
-
-  local descr
-
-  descr=( "${(@M)match:#*[^\\]:*}" )
-
-  compadd "${(@P)name}" - "${(@)${(@)match:#*[^\\]:*}:s/\\:/:/}"
-  (( $#descr )) && _describe -t "$tag" '' descr "${(@P)name}"
+if ! (( ${funcstack[2,-1][(I)_description]} )); then
+  local fakestyle descr
+  for fakestyle in fake fake-always; do
+    zstyle -a ":completion:${curcontext}:$tag" $fakestyle match ||
+    continue
+
+    descr=( "${(@M)match:#*[^\\]:*}" )
+
+    opts=("${(@P)name}")
+    if [[ $fakestyle = fake-always && $opts[1,2] = "-F _comp_ignore" ]]; then
+      shift 2 opts
+    fi
+    compadd "${(@)opts}" - "${(@)${(@)match:#*[^\\]:*}:s/\\:/:/}"
+    (( $#descr )) && _describe -t "$tag" '' descr "${(@)opts}"
+  done
 fi
 
 return 0
Index: Doc/Zsh/compsys.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/compsys.yo,v
retrieving revision 1.194
diff -u -r1.194 compsys.yo
--- Doc/Zsh/compsys.yo	10 Oct 2006 09:37:19 -0000	1.194
+++ Doc/Zsh/compsys.yo	15 Nov 2006 14:51:56 -0000
@@ -1308,6 +1308,31 @@
 fake strings.  Note that the styles tt(fake-files) and tt(fake-parameters)
 provide additional features when completing files or parameters.
 )
+kindex(fake-always, completion style)
+item(tt(fake-always))(
+This works identically to the tt(fake) style except that
+the tt(ignored-patterns) style is not applied to it.  This makes it
+possible to override a set of matches completely by setting the
+ignored patterns to `tt(*)'.
+
+The following shows a way of supplementing any tag with arbitrary data, but
+having it behave for display purposes like a separate tag.  In this example
+we use the features of the tt(tag-order) style to divide the
+tt(named-directories) tag into two when performing completion with
+the standard completer tt(complete) for arguments of tt(cd).  The tag
+tt(named-directories-normal) behaves as normal, but the tag
+tt(named-directories-mine) contains a fixed set of directories.
+This has the effect of adding the match group `tt(extra directories)' with
+the given completions.
+
+example(zstyle ':completion::complete:cd:*' tag-order \ 
+  'named-directories:-mine:extra\ directories
+  named-directories:-normal:named\ directories *'
+zstyle ':completion::complete:cd:*:named-directories-mine' \ 
+  fake-always mydir1 mydir2
+zstyle ':completion::complete:cd:*:named-directories-mine' \ 
+  ignored-patterns '*')
+)
 kindex(fake-files, completion style)
 item(tt(fake-files))(
 This style is used when completing files and looked up 


-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


To access the latest news from CSR copy this link into a web browser:  http://www.csr.com/email_sig.php


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

end of thread, other threads:[~2006-11-15 14:55 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-11-15  5:09 Adding array content to cd completion Jean-Rene David
2006-11-15  5:27 ` arno.
2006-11-15  6:09   ` Jean-Rene David
2006-11-15 11:08 ` Peter Stephenson
2006-11-15 14:54   ` 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).