unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Completion with different values and displayed things
@ 2024-03-16  4:44 mbork
  2024-03-16 15:33 ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 2+ messages in thread
From: mbork @ 2024-03-16  4:44 UTC (permalink / raw)
  To: Help Gnu Emacs mailing list

Hi all!

I have a list of "things" and I need to use `completing-read' (or
another completion mechanism) to enable the user to select one of them.
Here's the problem.  Every "thing" has, say, three things, like
(:name "Thing" :description "The important thing" :id 1337)
I want my completion to:
- complete on :name
- display also :description (there could be two different "things" with
  the same :name's but different :description's)
- and return :id

I use Ivy, but I'd prefer to make this work with any completion
framework (or even with Emacs vanilla completion).

Does Emacs has anything like this?  More or less, I'd like an equivalent
of HTML's <select> where displayed thing is different than the "value"
attribute.  It is important though to use some sort of completion
instead of e.g. displaying all options annotated by numbers/letters and
letting the user type the number or a letter associated with the one
they want - there are too many of them for this to be comfortable.

TIA,

-- 
Marcin Borkowski
https://mbork.pl
https://crimsonelevendelightpetrichor.net/



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Completion with different values and displayed things
  2024-03-16  4:44 Completion with different values and displayed things mbork
@ 2024-03-16 15:33 ` Stefan Monnier via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 2+ messages in thread
From: Stefan Monnier via Users list for the GNU Emacs text editor @ 2024-03-16 15:33 UTC (permalink / raw)
  To: help-gnu-emacs

> I want my completion to:
> - complete on :name
> - display also :description (there could be two different "things" with
>   the same :name's but different :description's)

The completion mechanism "funnels" everything through the string the
users type (either directly or via completion), so if completion in only
based on `:name`s (i.e. the strings the user can enter will only ever
take the form of one of the `:name`s), then there is no way for the users
to specify which of several entries they meant if those entries share
the same `:name`.

IOW, make sure the thing on which they complete is unique.

Also, the completion API doesn't conveniently let you display just the
`:description`: you can use the `annotation` metadata (see
`completion-metadata`) to *add* the `:description` to the thing on which
the users complete (e.g. the `:name`), but not to replace it.

As for returning the `:id`, just do it manually after the call to
`completing-read` by searching for the matching entry and extracting its
`:id`.


        Stefan




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-03-16 15:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-16  4:44 Completion with different values and displayed things mbork
2024-03-16 15:33 ` Stefan Monnier via Users list for the GNU Emacs text editor

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 read-only IMAP folder(s) and NNTP newsgroup(s).