1a8b4c701SRamkumar Ramachandra# perf bash and zsh completion 2a8b4c701SRamkumar Ramachandra 3a8b4c701SRamkumar Ramachandra# Taken from git.git's completion script. 4a8b4c701SRamkumar Ramachandra__my_reassemble_comp_words_by_ref() 5a8b4c701SRamkumar Ramachandra{ 6a8b4c701SRamkumar Ramachandra local exclude i j first 7a8b4c701SRamkumar Ramachandra # Which word separators to exclude? 8a8b4c701SRamkumar Ramachandra exclude="${1//[^$COMP_WORDBREAKS]}" 9a8b4c701SRamkumar Ramachandra cword_=$COMP_CWORD 10a8b4c701SRamkumar Ramachandra if [ -z "$exclude" ]; then 11a8b4c701SRamkumar Ramachandra words_=("${COMP_WORDS[@]}") 12a8b4c701SRamkumar Ramachandra return 13a8b4c701SRamkumar Ramachandra fi 14a8b4c701SRamkumar Ramachandra # List of word completion separators has shrunk; 15a8b4c701SRamkumar Ramachandra # re-assemble words to complete. 16a8b4c701SRamkumar Ramachandra for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do 17a8b4c701SRamkumar Ramachandra # Append each nonempty word consisting of just 18a8b4c701SRamkumar Ramachandra # word separator characters to the current word. 19a8b4c701SRamkumar Ramachandra first=t 20a8b4c701SRamkumar Ramachandra while 21a8b4c701SRamkumar Ramachandra [ $i -gt 0 ] && 22a8b4c701SRamkumar Ramachandra [ -n "${COMP_WORDS[$i]}" ] && 23a8b4c701SRamkumar Ramachandra # word consists of excluded word separators 24a8b4c701SRamkumar Ramachandra [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ] 25a8b4c701SRamkumar Ramachandra do 26a8b4c701SRamkumar Ramachandra # Attach to the previous token, 27a8b4c701SRamkumar Ramachandra # unless the previous token is the command name. 28a8b4c701SRamkumar Ramachandra if [ $j -ge 2 ] && [ -n "$first" ]; then 29a8b4c701SRamkumar Ramachandra ((j--)) 30a8b4c701SRamkumar Ramachandra fi 31a8b4c701SRamkumar Ramachandra first= 32a8b4c701SRamkumar Ramachandra words_[$j]=${words_[j]}${COMP_WORDS[i]} 33a8b4c701SRamkumar Ramachandra if [ $i = $COMP_CWORD ]; then 34a8b4c701SRamkumar Ramachandra cword_=$j 35a8b4c701SRamkumar Ramachandra fi 36a8b4c701SRamkumar Ramachandra if (($i < ${#COMP_WORDS[@]} - 1)); then 37a8b4c701SRamkumar Ramachandra ((i++)) 38a8b4c701SRamkumar Ramachandra else 39a8b4c701SRamkumar Ramachandra # Done. 40a8b4c701SRamkumar Ramachandra return 41a8b4c701SRamkumar Ramachandra fi 42a8b4c701SRamkumar Ramachandra done 43a8b4c701SRamkumar Ramachandra words_[$j]=${words_[j]}${COMP_WORDS[i]} 44a8b4c701SRamkumar Ramachandra if [ $i = $COMP_CWORD ]; then 45a8b4c701SRamkumar Ramachandra cword_=$j 46a8b4c701SRamkumar Ramachandra fi 47a8b4c701SRamkumar Ramachandra done 48a8b4c701SRamkumar Ramachandra} 49a8b4c701SRamkumar Ramachandra 50*1312c8a8SYunlong Song# Define preload_get_comp_words_by_ref="false", if the function 51*1312c8a8SYunlong Song# __perf_get_comp_words_by_ref() is required instead. 52*1312c8a8SYunlong Songpreload_get_comp_words_by_ref="true" 53*1312c8a8SYunlong Song 54*1312c8a8SYunlong Songif [ $preload_get_comp_words_by_ref = "true" ]; then 55a8b4c701SRamkumar Ramachandra type _get_comp_words_by_ref &>/dev/null || 56*1312c8a8SYunlong Song preload_get_comp_words_by_ref="false" 57*1312c8a8SYunlong Songfi 58*1312c8a8SYunlong Song[ $preload_get_comp_words_by_ref = "true" ] || 59*1312c8a8SYunlong Song__perf_get_comp_words_by_ref() 60a8b4c701SRamkumar Ramachandra{ 61a8b4c701SRamkumar Ramachandra local exclude cur_ words_ cword_ 62a8b4c701SRamkumar Ramachandra if [ "$1" = "-n" ]; then 63a8b4c701SRamkumar Ramachandra exclude=$2 64a8b4c701SRamkumar Ramachandra shift 2 65a8b4c701SRamkumar Ramachandra fi 66a8b4c701SRamkumar Ramachandra __my_reassemble_comp_words_by_ref "$exclude" 67a8b4c701SRamkumar Ramachandra cur_=${words_[cword_]} 68a8b4c701SRamkumar Ramachandra while [ $# -gt 0 ]; do 69a8b4c701SRamkumar Ramachandra case "$1" in 70a8b4c701SRamkumar Ramachandra cur) 71a8b4c701SRamkumar Ramachandra cur=$cur_ 72a8b4c701SRamkumar Ramachandra ;; 73a8b4c701SRamkumar Ramachandra prev) 74a8b4c701SRamkumar Ramachandra prev=${words_[$cword_-1]} 75a8b4c701SRamkumar Ramachandra ;; 76a8b4c701SRamkumar Ramachandra words) 77a8b4c701SRamkumar Ramachandra words=("${words_[@]}") 78a8b4c701SRamkumar Ramachandra ;; 79a8b4c701SRamkumar Ramachandra cword) 80a8b4c701SRamkumar Ramachandra cword=$cword_ 81a8b4c701SRamkumar Ramachandra ;; 82a8b4c701SRamkumar Ramachandra esac 83a8b4c701SRamkumar Ramachandra shift 84a8b4c701SRamkumar Ramachandra done 85a8b4c701SRamkumar Ramachandra} 86a8b4c701SRamkumar Ramachandra 87*1312c8a8SYunlong Song# Define preload__ltrim_colon_completions="false", if the function 88*1312c8a8SYunlong Song# __perf__ltrim_colon_completions() is required instead. 89*1312c8a8SYunlong Songpreload__ltrim_colon_completions="true" 90*1312c8a8SYunlong Song 91*1312c8a8SYunlong Songif [ $preload__ltrim_colon_completions = "true" ]; then 92a8b4c701SRamkumar Ramachandra type __ltrim_colon_completions &>/dev/null || 93*1312c8a8SYunlong Song preload__ltrim_colon_completions="false" 94*1312c8a8SYunlong Songfi 95*1312c8a8SYunlong Song[ $preload__ltrim_colon_completions = "true" ] || 96*1312c8a8SYunlong Song__perf__ltrim_colon_completions() 97a8b4c701SRamkumar Ramachandra{ 98a8b4c701SRamkumar Ramachandra if [[ "$1" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then 99a8b4c701SRamkumar Ramachandra # Remove colon-word prefix from COMPREPLY items 100a8b4c701SRamkumar Ramachandra local colon_word=${1%"${1##*:}"} 101a8b4c701SRamkumar Ramachandra local i=${#COMPREPLY[*]} 102a8b4c701SRamkumar Ramachandra while [[ $((--i)) -ge 0 ]]; do 103a8b4c701SRamkumar Ramachandra COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"} 104a8b4c701SRamkumar Ramachandra done 105a8b4c701SRamkumar Ramachandra fi 106a8b4c701SRamkumar Ramachandra} 107a8b4c701SRamkumar Ramachandra 108a8b4c701SRamkumar Ramachandra__perfcomp () 109a8b4c701SRamkumar Ramachandra{ 110a8b4c701SRamkumar Ramachandra COMPREPLY=( $( compgen -W "$1" -- "$2" ) ) 111a8b4c701SRamkumar Ramachandra} 112a8b4c701SRamkumar Ramachandra 113a8b4c701SRamkumar Ramachandra__perfcomp_colon () 114a8b4c701SRamkumar Ramachandra{ 115a8b4c701SRamkumar Ramachandra __perfcomp "$1" "$2" 116*1312c8a8SYunlong Song if [ $preload__ltrim_colon_completions = "true" ]; then 117a8b4c701SRamkumar Ramachandra __ltrim_colon_completions $cur 118*1312c8a8SYunlong Song else 119*1312c8a8SYunlong Song __perf__ltrim_colon_completions $cur 120*1312c8a8SYunlong Song fi 121a8b4c701SRamkumar Ramachandra} 122a8b4c701SRamkumar Ramachandra 12367afff48SYunlong Song__perf_prev_skip_opts () 12467afff48SYunlong Song{ 12567afff48SYunlong Song local i cmd_ cmds_ 12667afff48SYunlong Song 12767afff48SYunlong Song let i=cword-1 128eee200a6SYunlong Song cmds_=$($cmd $1 --list-cmds) 12967afff48SYunlong Song prev_skip_opts=() 13067afff48SYunlong Song while [ $i -ge 0 ]; do 131eee200a6SYunlong Song if [[ ${words[i]} == $1 ]]; then 132eee200a6SYunlong Song return 133eee200a6SYunlong Song fi 13467afff48SYunlong Song for cmd_ in $cmds_; do 13567afff48SYunlong Song if [[ ${words[i]} == $cmd_ ]]; then 13667afff48SYunlong Song prev_skip_opts=${words[i]} 13767afff48SYunlong Song return 13867afff48SYunlong Song fi 13967afff48SYunlong Song done 14067afff48SYunlong Song ((i--)) 14167afff48SYunlong Song done 14267afff48SYunlong Song} 1436fdd9cb7SYunlong Song 144a8b4c701SRamkumar Ramachandra__perf_main () 145a8b4c701SRamkumar Ramachandra{ 146a8b4c701SRamkumar Ramachandra local cmd 147a8b4c701SRamkumar Ramachandra 148a8b4c701SRamkumar Ramachandra cmd=${words[0]} 149a8b4c701SRamkumar Ramachandra COMPREPLY=() 150a8b4c701SRamkumar Ramachandra 15167afff48SYunlong Song # Skip options backward and find the last perf command 15267afff48SYunlong Song __perf_prev_skip_opts 153a8b4c701SRamkumar Ramachandra # List perf subcommands or long options 154e003ce54SYunlong Song if [ -z $prev_skip_opts ]; then 155a8b4c701SRamkumar Ramachandra if [[ $cur == --* ]]; then 1567335399aSYunlong Song cmds=$($cmd --list-opts) 157a8b4c701SRamkumar Ramachandra else 158a8b4c701SRamkumar Ramachandra cmds=$($cmd --list-cmds) 159a8b4c701SRamkumar Ramachandra fi 1607335399aSYunlong Song __perfcomp "$cmds" "$cur" 1616fdd9cb7SYunlong Song # List possible events for -e option 1626fdd9cb7SYunlong Song elif [[ $prev == @("-e"|"--event") && 1636fdd9cb7SYunlong Song $prev_skip_opts == @(record|stat|top) ]]; then 164a8b4c701SRamkumar Ramachandra evts=$($cmd list --raw-dump) 165a8b4c701SRamkumar Ramachandra __perfcomp_colon "$evts" "$cur" 16602fde323SYunlong Song else 1673bca2354SRamkumar Ramachandra # List subcommands for perf commands 1686fdd9cb7SYunlong Song if [[ $prev_skip_opts == @(kvm|kmem|mem|lock|sched| 1696fdd9cb7SYunlong Song |data|help|script|test|timechart|trace) ]]; then 17067afff48SYunlong Song subcmds=$($cmd $prev_skip_opts --list-cmds) 1718f2f5adaSRamkumar Ramachandra __perfcomp_colon "$subcmds" "$cur" 17202fde323SYunlong Song fi 173a8b4c701SRamkumar Ramachandra # List long option names 17402fde323SYunlong Song if [[ $cur == --* ]]; then 175eee200a6SYunlong Song subcmd=$prev_skip_opts 176eee200a6SYunlong Song __perf_prev_skip_opts $subcmd 177eee200a6SYunlong Song subcmd=$subcmd" "$prev_skip_opts 178a8b4c701SRamkumar Ramachandra opts=$($cmd $subcmd --list-opts) 179a8b4c701SRamkumar Ramachandra __perfcomp "$opts" "$cur" 180a8b4c701SRamkumar Ramachandra fi 18102fde323SYunlong Song fi 182a8b4c701SRamkumar Ramachandra} 183a8b4c701SRamkumar Ramachandra 184a8b4c701SRamkumar Ramachandraif [[ -n ${ZSH_VERSION-} ]]; then 185a8b4c701SRamkumar Ramachandra autoload -U +X compinit && compinit 186a8b4c701SRamkumar Ramachandra 187a8b4c701SRamkumar Ramachandra __perfcomp () 188a8b4c701SRamkumar Ramachandra { 189a8b4c701SRamkumar Ramachandra emulate -L zsh 190a8b4c701SRamkumar Ramachandra 191a8b4c701SRamkumar Ramachandra local c IFS=$' \t\n' 192a8b4c701SRamkumar Ramachandra local -a array 193a8b4c701SRamkumar Ramachandra 194a8b4c701SRamkumar Ramachandra for c in ${=1}; do 195a8b4c701SRamkumar Ramachandra case $c in 196a8b4c701SRamkumar Ramachandra --*=*|*.) ;; 197a8b4c701SRamkumar Ramachandra *) c="$c " ;; 198a8b4c701SRamkumar Ramachandra esac 199a8b4c701SRamkumar Ramachandra array[${#array[@]}+1]="$c" 200a8b4c701SRamkumar Ramachandra done 201a8b4c701SRamkumar Ramachandra 202a8b4c701SRamkumar Ramachandra compset -P '*[=:]' 203a8b4c701SRamkumar Ramachandra compadd -Q -S '' -a -- array && _ret=0 204a8b4c701SRamkumar Ramachandra } 205a8b4c701SRamkumar Ramachandra 206a8b4c701SRamkumar Ramachandra __perfcomp_colon () 207a8b4c701SRamkumar Ramachandra { 208a8b4c701SRamkumar Ramachandra emulate -L zsh 209a8b4c701SRamkumar Ramachandra 210a8b4c701SRamkumar Ramachandra local cur_="${2-$cur}" 211a8b4c701SRamkumar Ramachandra local c IFS=$' \t\n' 212a8b4c701SRamkumar Ramachandra local -a array 213a8b4c701SRamkumar Ramachandra 214a8b4c701SRamkumar Ramachandra if [[ "$cur_" == *:* ]]; then 215a8b4c701SRamkumar Ramachandra local colon_word=${cur_%"${cur_##*:}"} 216a8b4c701SRamkumar Ramachandra fi 217a8b4c701SRamkumar Ramachandra 218a8b4c701SRamkumar Ramachandra for c in ${=1}; do 219a8b4c701SRamkumar Ramachandra case $c in 220a8b4c701SRamkumar Ramachandra --*=*|*.) ;; 221a8b4c701SRamkumar Ramachandra *) c="$c " ;; 222a8b4c701SRamkumar Ramachandra esac 223a8b4c701SRamkumar Ramachandra array[$#array+1]=${c#"$colon_word"} 224a8b4c701SRamkumar Ramachandra done 225a8b4c701SRamkumar Ramachandra 226a8b4c701SRamkumar Ramachandra compset -P '*[=:]' 227a8b4c701SRamkumar Ramachandra compadd -Q -S '' -a -- array && _ret=0 228a8b4c701SRamkumar Ramachandra } 229a8b4c701SRamkumar Ramachandra 230a8b4c701SRamkumar Ramachandra _perf () 231a8b4c701SRamkumar Ramachandra { 232a8b4c701SRamkumar Ramachandra local _ret=1 cur cword prev 233a8b4c701SRamkumar Ramachandra cur=${words[CURRENT]} 234a8b4c701SRamkumar Ramachandra prev=${words[CURRENT-1]} 235a8b4c701SRamkumar Ramachandra let cword=CURRENT-1 236a8b4c701SRamkumar Ramachandra emulate ksh -c __perf_main 237a8b4c701SRamkumar Ramachandra let _ret && _default && _ret=0 238a8b4c701SRamkumar Ramachandra return _ret 239a8b4c701SRamkumar Ramachandra } 240a8b4c701SRamkumar Ramachandra 241a8b4c701SRamkumar Ramachandra compdef _perf perf 242a8b4c701SRamkumar Ramachandra return 243a8b4c701SRamkumar Ramachandrafi 244a8b4c701SRamkumar Ramachandra 245a8b4c701SRamkumar Ramachandratype perf &>/dev/null && 246a8b4c701SRamkumar Ramachandra_perf() 247a8b4c701SRamkumar Ramachandra{ 248a8b4c701SRamkumar Ramachandra local cur words cword prev 249*1312c8a8SYunlong Song if [ $preload_get_comp_words_by_ref = "true" ]; then 250a8b4c701SRamkumar Ramachandra _get_comp_words_by_ref -n =: cur words cword prev 251*1312c8a8SYunlong Song else 252*1312c8a8SYunlong Song __perf_get_comp_words_by_ref -n =: cur words cword prev 253*1312c8a8SYunlong Song fi 254a8b4c701SRamkumar Ramachandra __perf_main 255a8b4c701SRamkumar Ramachandra} && 256a8b4c701SRamkumar Ramachandra 257a8b4c701SRamkumar Ramachandracomplete -o bashdefault -o default -o nospace -F _perf perf 2>/dev/null \ 258a8b4c701SRamkumar Ramachandra || complete -o default -o nospace -F _perf perf 259