zsh support for perf completion
cover zsh support for perf completion
Hi,
I'm pleased to announce zsh support for the perf completion script, added this lazy Sunday afternoon. I'm not very good at shell scripting, so these patches can definitely use more eyes. Frankly, I'm mildly surprised that this hack even works: please test carefully and report bugs.
[1/5] to [3/5] factors things out and prepares the stage for [4/5], which is the focus of the series. [5/5] is just a pure rename.
Thanks.
Ramkumar Ramachandra (5):
perf completion: introduce a layer of indirection perf completion: factor out compgen stuff
perf completion: factor out call to __ltrim_colon_completions perf completion: introduce zsh support
perf completion: rename file to reflect zsh support
tools/perf/{bash_completion => perf-completion.sh} | 100 ++++++++++++++++++---
1 file changed, 88 insertions(+), 12 deletions(-)
rename tools/perf/{bash_completion => perf-completion.sh} (65%)Signature · 1 line
1.8.5.rc0.6.gfd75b41
PATCH 1/5 perf completion: introduce a layer of indirection
Define the variables cur, words, cword, and prev outside the main completion function so that we have a chance to override it when we introduce zsh support.
--- tools/perf/bash_completion | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
tools/perf/bash_completion +11 -6
@@ -89,15 +89,12 @@ __ltrim_colon_completions() fi } -type perf &>/dev/null && -_perf() +__perf_main () { - local cur words cword prev cmd - - COMPREPLY=() - _get_comp_words_by_ref -n =: cur words cword prev + local cmd cmd=${words[0]} + COMPREPLY=() # List perf subcommands or long options if [ $cword -eq 1 ]; then
@@ -120,6 +117,14 @@ _perf() opts=$($cmd $subcmd --list-opts) COMPREPLY=( $( compgen -W '$opts' -- "$cur" ) ) fi +} + +type perf &>/dev/null && +_perf() +{ + local cur words cword prev + _get_comp_words_by_ref -n =: cur words cword prev + __perf_main } && complete -o bashdefault -o default -o nospace -F _perf perf 2>/dev/null \
Signature · 1 line
1.8.5.rc0.6.gfd75b41
tip-bot for Ramkumar Ramachandra
merge bot[tip:perf/core] perf completion: Introduce a layer of indirection
perf completion: Introduce a layer of indirection
Commit-ID: 2cf025e69543f5f4aa68e8549d60680515fef5ad Gitweb: http://git.kernel.org/tip/2cf025e69543f5f4aa68e8549d60680515fef5ad Author: Ramkumar Ramachandra <[email protected]> AuthorDate: Sun, 17 Nov 2013 21:43:23 +0530 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Wed, 27 Nov 2013 14:58:35 -0300
perf completion: Introduce a layer of indirection
Define the variables cur, words, cword, and prev outside the main completion function so that we have a chance to override it when we introduce zsh support.
--- tools/perf/bash_completion | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-)
tools/perf/bash_completion +11 -6
@@ -89,15 +89,12 @@ __ltrim_colon_completions() fi } -type perf &>/dev/null && -_perf() +__perf_main () { - local cur words cword prev cmd - - COMPREPLY=() - _get_comp_words_by_ref -n =: cur words cword prev + local cmd cmd=${words[0]} + COMPREPLY=() # List perf subcommands or long options if [ $cword -eq 1 ]; then
@@ -120,6 +117,14 @@ _perf() opts=$($cmd $subcmd --list-opts) COMPREPLY=( $( compgen -W '$opts' -- "$cur" ) ) fi +} + +type perf &>/dev/null && +_perf() +{ + local cur words cword prev + _get_comp_words_by_ref -n =: cur words cword prev + __perf_main } && complete -o bashdefault -o default -o nospace -F _perf perf 2>/dev/null \
PATCH 2/5 perf completion: factor out compgen stuff
In reply to Ramkumar Ramachandracompgen is a bash-builtin; factor out the invocations into a separate function to give us a chance to override it with a zsh equivalent in future patches.
--- tools/perf/bash_completion | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
tools/perf/bash_completion +10 -5
@@ -89,6 +89,11 @@ __ltrim_colon_completions() fi } +__perfcomp () +{ + COMPREPLY=( $( compgen -W "$1" -- "$2" ) ) +} + __perf_main () { local cmd
@@ -99,23 +104,23 @@ __perf_main () # List perf subcommands or long options if [ $cword -eq 1 ]; then if [[ $cur == --* ]]; then - COMPREPLY=( $( compgen -W '--help --version \ + __perfcomp '--help --version \ --exec-path --html-path --paginate --no-pager \ - --perf-dir --work-tree --debugfs-dir' -- "$cur" ) ) + --perf-dir --work-tree --debugfs-dir' -- "$cur" else cmds=$($cmd --list-cmds) - COMPREPLY=( $( compgen -W '$cmds' -- "$cur" ) ) + __perfcomp "$cmds" "$cur" fi # List possible events for -e option elif [[ $prev == "-e" && "${words[1]}" == @(record|stat|top) ]]; then evts=$($cmd list --raw-dump) - COMPREPLY=( $( compgen -W '$evts' -- "$cur" ) ) + __perfcomp "$evts" "$cur" __ltrim_colon_completions $cur # List long option names elif [[ $cur == --* ]]; then subcmd=${words[1]} opts=$($cmd $subcmd --list-opts) - COMPREPLY=( $( compgen -W '$opts' -- "$cur" ) ) + __perfcomp "$opts" "$cur" fi }
Signature · 1 line
1.8.5.rc0.6.gfd75b41
tip-bot for Ramkumar Ramachandra
merge bot[tip:perf/core] perf completion: Factor out compgen stuff
perf completion: Factor out compgen stuff
Commit-ID: 12f9dd5042483698c74a133d9004ff1d1a6474f9 Gitweb: http://git.kernel.org/tip/12f9dd5042483698c74a133d9004ff1d1a6474f9 Author: Ramkumar Ramachandra <[email protected]> AuthorDate: Sun, 17 Nov 2013 21:43:24 +0530 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Wed, 27 Nov 2013 14:58:35 -0300
perf completion: Factor out compgen stuff
compgen is a bash-builtin; factor out the invocations into a separate function to give us a chance to override it with a zsh equivalent in future patches.
--- tools/perf/bash_completion | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-)
tools/perf/bash_completion +10 -5
@@ -89,6 +89,11 @@ __ltrim_colon_completions() fi } +__perfcomp () +{ + COMPREPLY=( $( compgen -W "$1" -- "$2" ) ) +} + __perf_main () { local cmd
@@ -99,23 +104,23 @@ __perf_main () # List perf subcommands or long options if [ $cword -eq 1 ]; then if [[ $cur == --* ]]; then - COMPREPLY=( $( compgen -W '--help --version \ + __perfcomp '--help --version \ --exec-path --html-path --paginate --no-pager \ - --perf-dir --work-tree --debugfs-dir' -- "$cur" ) ) + --perf-dir --work-tree --debugfs-dir' -- "$cur" else cmds=$($cmd --list-cmds) - COMPREPLY=( $( compgen -W '$cmds' -- "$cur" ) ) + __perfcomp "$cmds" "$cur" fi # List possible events for -e option elif [[ $prev == "-e" && "${words[1]}" == @(record|stat|top) ]]; then evts=$($cmd list --raw-dump) - COMPREPLY=( $( compgen -W '$evts' -- "$cur" ) ) + __perfcomp "$evts" "$cur" __ltrim_colon_completions $cur # List long option names elif [[ $cur == --* ]]; then subcmd=${words[1]} opts=$($cmd $subcmd --list-opts) - COMPREPLY=( $( compgen -W '$opts' -- "$cur" ) ) + __perfcomp "$opts" "$cur" fi }
PATCH 3/5 perf completion: factor out call to __ltrim_colon_completions
In reply to Ramkumar RamachandraIn our sole callsite, __ltrim_colon_completions is called after __perfcomp, to modify the COMPREPLY set by the invocation. This is problematic, because in the zsh equivalent (using compset/ compadd), we'll have to generate completions in one-shot. So factor out this entire callsite into a special override'able __perfcomp_colon function; we will override it when introducing zsh support.
--- tools/perf/bash_completion | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
tools/perf/bash_completion +7 -2
@@ -94,6 +94,12 @@ __perfcomp () COMPREPLY=( $( compgen -W "$1" -- "$2" ) ) } +__perfcomp_colon () +{ + __perfcomp "$1" "$2" + __ltrim_colon_completions $cur +} + __perf_main () { local cmd
@@ -114,8 +120,7 @@ __perf_main () # List possible events for -e option elif [[ $prev == "-e" && "${words[1]}" == @(record|stat|top) ]]; then evts=$($cmd list --raw-dump) - __perfcomp "$evts" "$cur" - __ltrim_colon_completions $cur + __perfcomp_colon "$evts" "$cur" # List long option names elif [[ $cur == --* ]]; then subcmd=${words[1]}
Signature · 1 line
1.8.5.rc0.6.gfd75b41
tip-bot for Ramkumar Ramachandra
merge bot[tip:perf/core] perf completion: Factor out call to __ltrim_colon_completions
perf completion: Factor out call to __ltrim_colon_completions
Commit-ID: 37e72c31061521d6f0e4b7fe47cd5748280ed691 Gitweb: http://git.kernel.org/tip/37e72c31061521d6f0e4b7fe47cd5748280ed691 Author: Ramkumar Ramachandra <[email protected]> AuthorDate: Sun, 17 Nov 2013 21:43:25 +0530 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Wed, 27 Nov 2013 14:58:35 -0300
perf completion: Factor out call to __ltrim_colon_completions
In our sole callsite, __ltrim_colon_completions is called after __perfcomp, to modify the COMPREPLY set by the invocation.
This is problematic, because in the zsh equivalent (using compset/ compadd), we'll have to generate completions in one-shot.
So factor out this entire callsite into a special override'able __perfcomp_colon function; we will override it when introducing zsh support.
--- tools/perf/bash_completion | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
tools/perf/bash_completion +7 -2
@@ -94,6 +94,12 @@ __perfcomp () COMPREPLY=( $( compgen -W "$1" -- "$2" ) ) } +__perfcomp_colon () +{ + __perfcomp "$1" "$2" + __ltrim_colon_completions $cur +} + __perf_main () { local cmd
@@ -114,8 +120,7 @@ __perf_main () # List possible events for -e option elif [[ $prev == "-e" && "${words[1]}" == @(record|stat|top) ]]; then evts=$($cmd list --raw-dump) - __perfcomp "$evts" "$cur" - __ltrim_colon_completions $cur + __perfcomp_colon "$evts" "$cur" # List long option names elif [[ $cur == --* ]]; then subcmd=${words[1]}
PATCH 4/5 perf completion: introduce zsh support
In reply to Ramkumar Ramachandra__perfcomp(), __perfcomp_colon(), and _perf() have to be overridden. Inspired by the way the git.git completion system is structured.
--- tools/perf/bash_completion | 63 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-)
tools/perf/bash_completion +62 -1
@@ -1,4 +1,4 @@ -# perf completion +# perf bash and zsh completion # Taken from git.git's completion script. __my_reassemble_comp_words_by_ref()
@@ -129,6 +129,67 @@ __perf_main () fi } +if [[ -n ${ZSH_VERSION-} ]]; then + autoload -U +X compinit && compinit + + __perfcomp () + { + emulate -L zsh + + local c IFS=$' \t\n' + local -a array + + for c in ${=1}; do + case $c in + --*=*|*.) ;; + *) c="$c " ;; + esac + array[${#array[@]}+1]="$c" + done + + compset -P '*[=:]' + compadd -Q -S '' -a -- array && _ret=0 + } + + __perfcomp_colon () + { + emulate -L zsh + + local cur_="${2-$cur}" + local c IFS=$' \t\n' + local -a array + + if [[ "$cur_" == *:* ]]; then + local colon_word=${cur_%"${cur_##*:}"} + fi + + for c in ${=1}; do + case $c in + --*=*|*.) ;; + *) c="$c " ;; + esac + array[$#array+1]=${c#"$colon_word"} + done + + compset -P '*[=:]' + compadd -Q -S '' -a -- array && _ret=0 + } + + _perf () + { + local _ret=1 cur cword prev + cur=${words[CURRENT]} + prev=${words[CURRENT-1]} + let cword=CURRENT-1 + emulate ksh -c __perf_main + let _ret && _default && _ret=0 + return _ret + } + + compdef _perf perf + return +fi + type perf &>/dev/null && _perf() {
Signature · 1 line
1.8.5.rc0.6.gfd75b41
tip-bot for Ramkumar Ramachandra
merge bot[tip:perf/core] perf completion: Introduce zsh support
perf completion: Introduce zsh support
Commit-ID: f38ab8af794c184c15f5e001d0eaa16f4a120978 Gitweb: http://git.kernel.org/tip/f38ab8af794c184c15f5e001d0eaa16f4a120978 Author: Ramkumar Ramachandra <[email protected]> AuthorDate: Sun, 17 Nov 2013 21:43:26 +0530 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Wed, 27 Nov 2013 14:58:35 -0300
perf completion: Introduce zsh support
__perfcomp(), __perfcomp_colon(), and _perf() have to be overridden. Inspired by the way the git.git completion system is structured.
--- tools/perf/bash_completion | 63 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-)
tools/perf/bash_completion +62 -1
@@ -1,4 +1,4 @@ -# perf completion +# perf bash and zsh completion # Taken from git.git's completion script. __my_reassemble_comp_words_by_ref()
@@ -129,6 +129,67 @@ __perf_main () fi } +if [[ -n ${ZSH_VERSION-} ]]; then + autoload -U +X compinit && compinit + + __perfcomp () + { + emulate -L zsh + + local c IFS=$' \t\n' + local -a array + + for c in ${=1}; do + case $c in + --*=*|*.) ;; + *) c="$c " ;; + esac + array[${#array[@]}+1]="$c" + done + + compset -P '*[=:]' + compadd -Q -S '' -a -- array && _ret=0 + } + + __perfcomp_colon () + { + emulate -L zsh + + local cur_="${2-$cur}" + local c IFS=$' \t\n' + local -a array + + if [[ "$cur_" == *:* ]]; then + local colon_word=${cur_%"${cur_##*:}"} + fi + + for c in ${=1}; do + case $c in + --*=*|*.) ;; + *) c="$c " ;; + esac + array[$#array+1]=${c#"$colon_word"} + done + + compset -P '*[=:]' + compadd -Q -S '' -a -- array && _ret=0 + } + + _perf () + { + local _ret=1 cur cword prev + cur=${words[CURRENT]} + prev=${words[CURRENT-1]} + let cword=CURRENT-1 + emulate ksh -c __perf_main + let _ret && _default && _ret=0 + return _ret + } + + compdef _perf perf + return +fi + type perf &>/dev/null && _perf() {
PATCH 5/5 perf completion: rename file to reflect zsh support
In reply to Ramkumar Ramachandra---
tools/perf/{bash_completion => perf-completion.sh} | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename tools/perf/{bash_completion => perf-completion.sh} (100%)tools/perf/perf-completion.sh renamed from tools/perf/bash_completion +0 -0
Signature · 1 line
1.8.5.rc0.6.gfd75b41
tip-bot for Ramkumar Ramachandra
merge bot[tip:perf/core] perf completion: Rename file to reflect zsh support
perf completion: Rename file to reflect zsh support
Commit-ID: a8b4c7014cadfdacd4e1f4c963128593be6f20de Gitweb: http://git.kernel.org/tip/a8b4c7014cadfdacd4e1f4c963128593be6f20de Author: Ramkumar Ramachandra <[email protected]> AuthorDate: Sun, 17 Nov 2013 21:43:27 +0530 Committer: Arnaldo Carvalho de Melo <[email protected]> CommitDate: Wed, 27 Nov 2013 14:58:35 -0300
perf completion: Rename file to reflect zsh support
---
tools/perf/Makefile.perf | 4 ++--
tools/perf/{bash_completion => perf-completion.sh} | 0
2 files changed, 2 insertions(+), 2 deletions(-)tools/perf/Makefile.perf +2 -2
@@ -840,9 +840,9 @@ ifndef NO_LIBPYTHON $(INSTALL) scripts/python/*.py -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python'; \ $(INSTALL) scripts/python/bin/* -t '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/scripts/python/bin' endif - $(call QUIET_INSTALL, bash_completion-script) \ + $(call QUIET_INSTALL, perf_completion-script) \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(sysconfdir_SQ)/bash_completion.d'; \ - $(INSTALL) bash_completion '$(DESTDIR_SQ)$(sysconfdir_SQ)/bash_completion.d/perf' + $(INSTALL) perf-completion.sh '$(DESTDIR_SQ)$(sysconfdir_SQ)/bash_completion.d/perf' $(call QUIET_INSTALL, tests) \ $(INSTALL) -d -m 755 '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests'; \ $(INSTALL) tests/attr.py '$(DESTDIR_SQ)$(perfexec_instdir_SQ)/tests'; \