zsh-users
 help / color / mirror / code / Atom feed
From: vapnik spaknik <vapniks@yahoo.com>
To: Zsh Users <zsh-users@zsh.org>
Subject: detect if command has a completion function
Date: Sat, 29 Aug 2020 16:47:08 +0000 (UTC)	[thread overview]
Message-ID: <301108111.164161.1598719628682@mail.yahoo.com> (raw)
In-Reply-To: <301108111.164161.1598719628682.ref@mail.yahoo.com>

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?



       reply	other threads:[~2020-08-29 16:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <301108111.164161.1598719628682.ref@mail.yahoo.com>
2020-08-29 16:47 ` vapnik spaknik [this message]
2020-08-29 17:21   ` Marc Cornellà
     [not found]   ` <AM6PR02MB56389D8F9832FA6C58D8EF6E84530@AM6PR02MB5638.eurprd02.prod.outlook.com>
2020-08-29 17:38     ` vapnik spaknik
2020-08-29 18:33   ` Bart Schaefer
2020-08-29 19:27     ` Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=301108111.164161.1598719628682@mail.yahoo.com \
    --to=vapniks@yahoo.com \
    --cc=zsh-users@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).