Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

detect if command has a completion function



Hi,
    how can I detect if a given command has a completion function, from within another function?
I want to loop over many commands quickly, so I want to avoid resorting to searching directories with find if possible.

The reason I need this capability is to help with a function I've written for cleaning up .zshrc files.
I tend to collect lots of "compdef _gnu_generic cmd1 cmd2..." lines in my .zshrc, but many of the commands flagged for completion with _gnu_generic are not present on other systems where I install the same .zshrc. I wrote a function which checks the commands mentioned in those "compdef _gnu_generic" lines, and removes any that are not present on the system:

function cleanup-compdefs() {
    local okcmds=() numcols=120
    for cmd in ${(i)$(grep -e "^compdef _gnu_generic" ~/.zshrc|sed 's/compdef _gnu_generic//')}; do
	if which ${cmd} &>/dev/null; then
	    okcmds+="$(basename ${cmd})"
	fi
    done
    local line i=1
    while [[ ${i} -le ${#okcmds} ]]; do
	line="compdef _gnu_generic"
	while [[ ${#line} -lt ${numcols} ]]; do
	    line+=" ${okcmds[${i}]}"
	    i=$((${i} + 1))
	done
	echo "${line}"
    done
}

However, I also want it to check which of the commands already have completion functions defined, so that they can also be removed.
Can anyone help?





Messages sorted by: Reverse Date, Date, Thread, Author