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

Re: Completing to a value that is completely different than the word for which completions are being requested



On 5/14/25 8:03 AM, Ross Goldberg wrote:
I'm trying to provide completion candidates for mas (https://github.com/mas-cli/mas).

A certain parameter accepts an ADAM ID, which is a UInt64 ID for an app from the App Store.

I want to search through 3 different app properties (ADAM ID, Bundle ID, and Name) for installed apps, then offer as completion candidates all apps where any of the 3 aforementioned properties case-insensitively starts with the command-line word for which completions are being requested (let's call that "the word").

Once a candidate has been selected, I want to insert the ADAM ID into the command line in place of the word.

compadd's -U option is used for this:

-U     If this flag is given, all completions are added to the set of matches and no matching will be done by the completion code. Normally this
is used in functions that do the matching themselves.

The "do(ing) he matching themselves" is inspecting the current word via $PREFIX, $SUFFIX (and possibly $IPREFIX, $ISUFFIX) and acting
accordingly.

a quick and dirty example:
_foo() {
  local ids names
  case $PREFIX in
    1(<->)#|f*)
      ids=(100)
      names=('foo:app 1')
    ;;
    [23](<->)#|b*)
      ids=(200 300)
      names=('bar:app 2' 'baz:app 3')
    ;;
    [45](<->)#|q*)
      ids=(400 500)
      names=('qux:app 4' 'quux:app 5')
  esac

# reminder to preferably use one of the many utility wrappers to compadd, zshcompsys(1)
  _describe id names ids -U
}
compdef _foo foo

foo b<tab> will present 200 and 300, with display strings bar and baz respectively.
with the dirty part being me only filtering on the first character of the word and no filtering of possible matches,
foo 3<tab> will present 200 too. for a more complete example see _email-ldap() in Completion/Unix/Type/_email_addresses.






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