From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 17369 invoked by alias); 11 Jan 2015 15:36:38 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 34231 Received: (qmail 13866 invoked from network); 11 Jan 2015 15:36:22 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,RCVD_IN_DNSWL_LOW autolearn=ham version=3.3.2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; bh=2gMHxeXTr6AX+BWSeNEvMhr6d0D3F9sVvD1OLpfjxOo=; b=YBDiKvR0UXfc44VssunUA4rPU1v4Gt2FiBIpVaCi1kf7km4apONwOoPRzo6+HmU/Wz NbtT8bAtM4PRDc+OjRQvKXtQ3ShogAjtI7QiF0g/bSU6kJKEv0l5IkfTRlKaCINdj8Ui UjHeG4XsRZ3z64/GMdqrTvdMpSOl4LQlzTh4q/5dhXBLqiOpi/p3ntDDi2msdSOh10lf veR/vW/q2YT31m0aFaeAZ8nI8OkU9/UOXN4oqNW47yTAjUoxMchTz3jEghbCgpPT4uFL EHO/hQjBAwqhoXb1n92Ny0zsxp9Bc7WEljfH2qvw1Hs7PVxf58KV8JDFf/+4BCap1BQ5 48vQ== X-Received: by 10.194.110.69 with SMTP id hy5mr50918865wjb.121.1420990098619; Sun, 11 Jan 2015 07:28:18 -0800 (PST) Message-ID: <54B29693.3090500@gmail.com> Date: Sun, 11 Jan 2015 16:28:19 +0100 From: Thomas Mitterfellner User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: zsh-workers@zsh.org Subject: Completion for qdbus X-TagToolbar-Keys: D20150111162819851 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Hello! I wrote a completion function for qdbus (a command line tool for communicating with dbus-enabled programs, like kmix, krusader, pulseaudio…) which I want to share in the hope that it will be useful. Comments, suggestions, testing, corrections etc. are welcome! Best regards, Thomas _qdbus #compdef qdbus # # Copyright (C) 2015 Thomas Mitterfellner # # This file is released under the GPLv2. # # version 0.1 # get the type of a method: 1 means function, 2 means property getType () { case "$1" in "method"*) echo 1 ;; "property"*) echo 2 ;; *) echo 0 ;; esac } # get the index of a method in an array of methods getIndex () { local e local index local type_ index=0 for e in "${@:2}"; do index=$(( index + 1 )) type_=$(getType "$e") if [[ $type_ == 1 ]] ; then if [[ "$e" == *"$1("* ]] ; then echo $index return 0 fi elif [[ $type_ == 2 ]] ; then if [[ "$e" == *"$1" ]] ; then echo $index return 0 fi fi done echo 0 } _qdbus() { local curcontext="$curcontext" state line local services local path_ local methods local names local types local properties local index typeset -A opt_args _arguments "--system[connect to system bus]"\ "--literal[print literal replies]"\ "1:service name:->service"\ "2:path:->path"\ "3:method:->method"\ "*:arguments:->arguments" case $state in service) services=( $(qdbus ${words[@]:1:-1} | grep -E '^([^:])') ) compadd "$@" $services ;; path) path_=( $(qdbus ${words[@]:1:-1} 2>/dev/null) ) compadd "$@" $path_ ;; method) IFS=$'\n' methods=( $(qdbus ${words[@]:1:-1} | \ grep -e "^method" | \ sed -r 's/method (.+) (.+)\((.*)\)/\2:\1(\3)/g' 2>/dev/null) ) properties=( $(qdbus ${words[@]:1:-1} | \ grep -e "^property" | \ sed -r 's/property (.+) (.+) (.+)/\3:\1 \2/g' 2>/dev/null) ) _describe properties properties -J properties _describe methods methods -J methods ;; arguments) IFS=$'\n' path_pos=$(( ${(M)#words:#--*} + 2)) method_pos=$(( $path_pos + 2)) names=( $(qdbus ${words[@]:1:$path_pos} | grep -e '^[mp]' 2>/dev/null) ) index=$(getIndex "${words[$method_pos]}" $names[@]) method=$names[$index] arg_pos=$(( ${#words} - $method_pos )) if [[ $(getType "$method") -eq 1 ]] ; then method_args="$(echo $method | sed -r 's/(.+) (.+) (.+)\((.*)\)/\4/g')" IFS=$',' method_args_=(${=method_args}) num_args=${#method_args_} if [[ $arg_pos -le $num_args ]] ; then _message "${method_args_[$arg_pos]}" fi else property_=$(echo "$method" |\ sed -r 's/property (.+) (.+) (.+)/\1 \2/g') if [[ $arg_pos -eq 1 ]] ; then _message ${property_} fi fi ;; esac }