TransWikia.com

How can I get the tag value associated with a current custom variable?

Emacs Asked by gct on November 13, 2021

I’ve got a custom variable defined:

(defcustom monorail-search-mode :issues-open
  "Current search mode for queries (controls whether we're searching all issues,
open issues, new issues, etc)"
  :type  '(choice
           (const :tag "All Issues"             :issues-all)
           (const :tag "Open Issues"            :issues-open)
           (const :tag "Open & Owned by Me"     :issues-open-owned)
           (const :tag "Open & Reported by Me"  :issues-open-reported)
           (const :tag "Open & Starred by Me"   :issues-open-starred)
           (const :tag "Open & Commented by Me" :issues-open-commented)
           (const :tag "New Issues"             :issues-new)
           (const :tag "Issues to Verify"       :issues-to-verify))
  :group 'monorail)

I’d like to be able to get the :tag value for the currently set value of monorail-search-mode for display purposes, but don’t see any easy way to do it. Is there one?

One Answer

Good question. Hopefully there's a simple answer, but from a quick look at the code, I can see this, at least:

(custom-type 'monorail-search-mode) returns this:

(choice
 (const :tag "All Issues" :issues-all)
 (const :tag "Open Issues" :issues-open)
 (const :tag "Open & Owned by Me" :issues-open-owned)
 (const :tag "Open & Reported by Me" :issues-open-reported)
 (const :tag "Open & Starred by Me" :issues-open-starred)
 (const :tag "Open & Commented by Me" :issues-open-commented)
 (const :tag "New Issues" :issues-new)
 (const :tag "Issues to Verify" :issues-to-verify))

Given that, plus the current value of the option, you can get (dig out) the corresponding tag.

But I'm sure you're really looking for a general answer, for any option - finding the tag, if there is one, for the current value.

There's also function custom-tag, but that tells you only the tag of a given symbol.

Looking forward to see a better (simple) answer.


Actually, I don't think there's a general general answer. ;-) This is because a custom :type need not have values that are exclusive, according to their :tags.

In your case, they are mutually exclusive: each possible value is associated with only one :tag. But that's not the case in general.

Consider a custom type that allows a choice between a file-name string and another kind of string, where the choice uses type string in each case. The choices in the Customize UI can have different tags, but given only a string value of the option, which tag is appropriate? You'd have to analyze the string value, to figure out which kind of string it is, to get the right tag.

And you can see that with another defcustom there might be allowable string values that match more than one choice, so more than one :tag.

And consider a very general type choice, which uses choices that use restricted-sexp. You'd need code that could figure out, from just the option value, which restricted-type choices could have led to that value.

:tag is for interactive Customize UI use. Nothing guarantees a function from a given option value to a single :tag. And the logic of getting from a value to the possibly relevant tags is not a general one, but needs to be specific to the given defcustom, I think.

Answered by Drew on November 13, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP