TIP Complétion d'emerge sous ZSH

Un article de Gentoo Linux Wiki.

Cet article fait partie des Trucs et Astuces.

Terminaux / Shells CD Live X Portage Système Systèmes de fichier Autres


[modifier] Une fonction de completion ZSH pour emerge

au programme ::

  • Toutes ou presque les options en complétion.
  • complétion sur le nom du portage avec l'option -s.
  • complétion sur les pkg installés (cat/nom) avec unmerge.
  • complétion sur les pkg dispo (cat/nom) avec les autres options si elles en ont besoins.

À tester, des retours d'expérience sont fortement souhaités de la part de l'auteur.

il faut copier ça dans /usr/share/zsh/version/functions/Completion/Linux/_portage

Fichier : /usr/share/zsh/version/functions/Completion/Linux/_portage
#compdef emerge
_emerge () {
    local state
    if (( CURRENT == 2 ));then
        _arguments -s \
            '*:*:->actions' \
            "$common_args[@]" "$install_args[@]" && return 0
    elif (( CURRENT > 2 ));then
            case "$words[2]" in
            unmerge)
                _arguments -s \
                '*:installed pkg:_unmerge' "$common_args[@]" && return 0
            ;;
            clean|depclean|-c|-C|help|-h|info|regen|sync|rsync|--searchdesc|-S|inject|-j|prune|-P);;
            search|-s)
              _arguments -s \
              '*:portage:_search_list' && return 0
               ;;
            *)
                _arguments -s \
                '*:*:->install_portage' \
                "$common_args[@]" \
              "$install_args[@]" && return 0
             ;;
             esac
    fi
    while [[ -n "$state" ]]; do
        lstate=$state
        state=''
        case "$lstate" in
        actions)
            _alternative \
            ':emerge actions:_actions' \
            '*:portage:_listportage' \
            '*:Ebuild files:_files -g \*.ebuild'
        ;;
        install_portage)
            _alternative \
            '*:portage:_listportage' \
            '*:Ebuild files:_files -g \*.ebuild'
        ;;
        esac
    done
}
#function to only show installed packages "cat/name"
_unmerge(){
   installed_portage=(/var/db/pkg/*/*)
   installed_dir=/var/db/pkg/
   installed_pkg=${(M)${${installed_portage//$installed_dir/$rem}%/}:#*-*}
   _tags -s installed_pkg && { compadd "$@" -k installed_pkg || compadd "$@" ${(kv)=installed_pkg} }
}
#Function to show only the name of all available portages
_search_list(){
   _tags -s search_portage && { compadd "$@" -k search_portage || compadd "$@" ${(kv)=search_portage} }
}

#Function to show all available portage like : "cat/name" and "name"
_listportage(){
    portages_list="$portages_list world system"
   _tags -s portages_list && { compadd "$@" -k portages_list || compadd "$@" ${(kv)=portages_list} }
}

common_args=(
{'(-p)--pretend','(--pretend)-p'}'[simply  display  what would be done]'
{'(-d)--debug','(--debug)-d'}'[Tells emerge to run the emerge command in debug mode]'
)
   install_args=(
{'(--upgradeonly -U --update -u --quiet -q --onlydeps -o --oneshot --nospinner --noreplace -n --nodeps -O --noconfmem -D --deep --changelog --buildpkg -B -b --buildpkgonly --emptytree -e -f --fetchonly)-l','(--upgradeonly -U --update -u --quiet -q --onlydeps -o --nospinner --noreplace -n --nodeps -O --noconfmem -D --deep --emptytree -e -f --fetchonly -l --buildpkg -b -B --buildpkgonly )--changelog'}'[This will show the ChangeLog]'
{'(-b --buildpkgonly -B --changelog -l)--buildpkg','(--changelog -l --buildpkg --buildpkgonly -B)-b'}'[Tells emerge to build binary packages]' \
{'(-B -b --buildpkg --changelog -l)--buildpkgonly','(--changelog -l --buildpkgonly --buildpkg -b)-B'}'[Tells emerge to only build binary packages]' \
{'(-D --changelog -l)--deep','(-l --changelog --deep)-D'}'[Consider the entire dependency tree of packages]'
{'(-e -l --changelog)--emptytree','(--emptytree -l --changelog)-e'}'[Only consider glibc as installed packages]'
{'(-f -l --changelog)--fetchonly','(-l --changelog --fetchonly)-f'}'[Just perform fetches for all packages]'
'(-l --changelog)--noconfmem[Causes portage to disregard merge records]'
{'(--changelog -l -O)--nodeps','(--nodeps --changelog -l)-O'}'[Merges specified  packages  without  merging  dependencies]'
{'(-l --changelog -n)--noreplace','(-l --changelog --noreplace)-n'}'[Skip packages already installed]'
'(-l --changelog)--nospiner[Disables the spinner for the session]'
'(-l --changelog)--oneshot[Do not add package to the world profile]'
{'(--onlydeps --changelog -l)-o','(--changelog -o -l)--onlydeps'}'[Only  merge  (or pretend to merge) the dependencies]'
{'(--quiet --changelog -l)-q','(-q --changelog -l)--quiet'}'[General outcome is a reduced  or  condensed output]'
{'(--update --changelog -l)-u','(-u --changelog -l)--update'}'[Updates packages to the most  recent  version  available]'
{'(--upgradeonly --changelog -l)-U','(-U --changelog -l)--upgradeonly'}'[Do not update packages to a lower version]'
{'(--verbose)-v','(-v)--verbose'}'[Tell  emerge to run in verbose mode]'
)

_options() {
    _arguments -s \
    "$unmerge_args[@]" "$common_args[@]" "$install_args[@]"
}


_actions() {
    _values "emerge actions" \
    'sync[Initiates a portage tree update]' \
    'rsync[Initiates a portage tree update]' \
    'unmerge[Removes all matching packages]' \
    'search[Searches for matches]' \
    'regen[Causes  portage  to check and update the dependency cache]' \
    'prune[Removes  all but the latest versions of matching packages]' \
    'inject[Portage thinks that this package is installed]' \
    'info[This is a list of information to include in  bug  reports]' \
    'help[Displays help]' \
    'depclean[Clean all packages that have no reason for being installed]'\
    'clean[Cleans the system by removing packages]' \
    '-c[Cleans the system by removing packages]' \
    '-h[Displays help]' \
    '-i[Portage thinks that this package is installed]' \
    '-P[Removes  all but the latest versions of matching packages]' \
    '-s[Searches for matches]' \
    '-S[Matches the search string against the description field]' \
    '--searchdesc[Matches the search string against the description field]' \
    '-C[Removes all matching packages]' \
    'world[Represente all packages in the world profiles]' \
    'system[Represent all the system packages]'
}

portages=(/usr/portage/*/*)
portagedir=/usr/portage/
rem=''
portages_list=${${(M)${${${portages//$portagedir/$rem}%/}:#*.eclass}:#*-*}:#*.*}
search_portage=${${(M)${${${${${portages//$portagedir/$rem}:#distfi*}:#licens*}%/}:#*.eclass}:#*-*}:t}

portages_list="$portages_list $search_portage"
_emerge "$@" 

puis

Code : Ajouter les définitions de complétion
compdef -a _portage emerge


[modifier] En utilisant Portage

Un nouveau paquet est disponibles sur Portage :

Code : Installation de la complétion zsh :
emerge zsh-completion
MaJ : 20/08/2004
Source : http://forums.gentoo.org/viewtopic.php?t=91569
Auteur initial : baptux