Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[zsh] help with completion system
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
avx
Advocate
Advocate


Joined: 21 Jun 2004
Posts: 2152

PostPosted: Mon Jan 23, 2012 7:27 am    Post subject: [zsh] help with completion system Reply with quote

So I tried to understand the quite lenghty documentation of ZSH's (new) completion system, but I don't quite get it, yet. Thus I'd like to request some help or a link to a page with examples fitting my needs.

Case 1, I've got a function to wake some boxes in the LAN, going something like this
Code:
function wake() {
  case ${1} in
    "xxx") MAC="..." ;;
    "yyy") MAC="..." ;;
          *) fail ;;
  esac
  etherwake -i eth0 ${MAC}
}


Now, in the old system (compctl), something like this works as intended
Code:
compctl -k "(xxx yyy)" wake
Now I'd like to know, how to port this to zstyle.

Case 2, command X should only operate on a given set of files, say images, thus I'd like it to complete only say *.{jpg,png,gif,...}.

Thanks.
_________________
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
Back to top
View user's profile Send private message
ppurka
Advocate
Advocate


Joined: 26 Dec 2004
Posts: 3256

PostPosted: Mon Jan 23, 2012 8:25 am    Post subject: Reply with quote

Case 1: the following should work
Code:
compdef 'compadd xxx yyy' wake


Case 2:
Code:
compdef "_arguments -s ':image files:_files -/g \"*.(jpg|png)\"'" X

Actually there might be a more direct way using zstyle for this Case 2.

EDIT: Ah. I misread what you wanted. I once used zshwiki to understand some of the completion options. But to really understand them, I had to read some of the examples installed via zsh-completion. Just do equery files zsh-completion and look at the files there. Of course, by now I have forgotten most of what I read, so I had to go on and look at some of the completions I created (3-4yrs ago) to refresh my memory :P

EDIT 2: About compadd and _files: http://zsh.sourceforge.net/Guide/zshguide06.html#l182
_________________
emerge --quiet redefined | E17 vids: I, II | Now using kde5 | e is unstable :-/
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6747

PostPosted: Mon Jan 23, 2012 8:35 am    Post subject: Reply with quote

I found that in almost all cases the _arguments function is enough. For examples how to use it, you might want to look at the zsh-completion files installed by eix (ignore the much more complex _eix). In your first example, you can extend your $fpath array and put the following file in it:
_wake wrote:
#compdef wake
_arguments : '1:MAC:(xxx yyy)'

The first ':' to mark the end of options of _arguments is not necessary in this example, but you might in other cases get unexpected effects if you forget it, so better use it as a rule.
If you do not want a separate file, you can of course define it also permanently in an init-file (although I would usually not recommend to do this):
Code:
_wake() {
 _arguments : '1:MAC:(xxx yyy)'
}
comdef _wake wake

Note that, as unfortunately most features of zsh, the printing of informal texts like "MAC" in this example is switched off by default; you must enable it first with e.g.
Code:
zstyle ':completion:*' format "%B%F{yellow}%K{blue}%d%k%f%b"

Your second example is analogous with
Quote:
_arguments : '*:picture_file:_files -g "*.(jpg|png|gif)"'
Back to top
View user's profile Send private message
avx
Advocate
Advocate


Joined: 21 Jun 2004
Posts: 2152

PostPosted: Mon Jan 23, 2012 12:00 pm    Post subject: Reply with quote

Thank you, both, that should get me somewhere I hope.

One question, though
Quote:
you can of course define it also permanently in an init-file (although I would usually not recommend to do this)
Why not?
_________________
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
Back to top
View user's profile Send private message
mv
Watchman
Watchman


Joined: 20 Apr 2005
Posts: 6747

PostPosted: Mon Jan 23, 2012 2:08 pm    Post subject: Reply with quote

Quote:
Why not?

The completion function is then permanently in memory in all your shells (or at least interactive shells, depending where you define it), although you will probably use it only occassionally.
For one short function, this is no issue, of course, but sooner or later you will have much more functions and more complex functions.
This is one of the annoying things in bash that if you save your environment (e.g. for portage), the main bulk you save is the completion system: You can avoid tihs with zsh.
Back to top
View user's profile Send private message
avx
Advocate
Advocate


Joined: 21 Jun 2004
Posts: 2152

PostPosted: Mon Jan 30, 2012 3:34 pm    Post subject: Reply with quote

Thanks, got it working now.
_________________
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum