Problem: to have a tab completion which takes two words and calculates the best match from them for Man, and then returns the best matches
Example: The following pseudo-code should give me at least Zsh’s reverse-menu-complete -command. Right now, I cannot search manuals inside manuals without zgrep.
man zsh:reverse <TAB>
where “:” is the separator which I want.
Initial Problem: Which files does the TAB completion run when I press TAB for one word in searching manuals by Zsh?
I will attempt to provide an insight to how zsh completion system works and an incomplete go at this problem.
The file that runs when you use TAB completion for
manin zsh is located under the/usr/share/zsh/${ZSH_VERSION}/functionsdirectory. The tree varies across distributions, but the file is named_man, and provides completion forman,aproposandwhatis.After _man is invoked, it works as following (rough description):
manand--local-filewas specified as first flag, invoke standard files completion (_files)manpathvariable from a set of defaults /$MANPATH. This is where the manpages will be searchedmanwith a section number parameter, if yes – only those sections will be searchedzstyle ':completion:*:manuals' separate-sections truewas used, separate sections in output (don’t mix between them)_man_pagesto provide a list of man pages for the match_man_pagesnow does a bit of magic withcompfiles -p pages '' '' "$matcher" '' dummy '*'.pagesis the variable with all the directories containing manpages for requested section(s). The actual globbing pattern is constructed from the implicit parameter$PREFIXand the last parameter tocompfiles–*in this case. This results in/usr/share/man/man1to be transformed into/usr/share/man/man1/foo*_man_pagesthen strips any suffixes from the files and adds them to the completion widget list of choices by usingcompaddNow, as you can see, the list of manpages is directly determined by
$PREFIXvariable. In order to makezsh:footo list only man pages ofzsh*which contain the wordfoo, it needs to be split across:character (if any).The following addition in
_man_pagespartially solve the issue (zsh 4.3.4):Original:
Modified (look for ##mod comments):
However, it’s still not fully working (if you uncomment the
#echo "$p matched $manpage_grep"line, you can see that it does build the list) – I suspect that somewhere internally, the completion system sees that, for instance, “zshcompctl” is not matched by prefix “zsh:foo”, and does not display the resulting matches. I’ve tried to keep$PREFIXas it is after stripping the grep string, but it still does not want to work.At any rate, this at least should get you started.