From: "Drew Adams" <drew.adams@oracle.com>
To: "Emacs-Devel" <emacs-devel@gnu.org>
Subject: RE: custom type `color' is not enforced
Date: Tue, 18 Dec 2007 15:00:40 -0800 [thread overview]
Message-ID: <DNEMKBNJBGPAOPIJOOICIEBDECAA.drew.adams@oracle.com> (raw)
In-Reply-To: <DNEMKBNJBGPAOPIJOOICAELMEBAA.drew.adams@oracle.com>
> From: Drew Adams Sent: Sunday, December 02, 2007 1:00 AM
> > emacs -Q
> > (defcustom foo "White" "xxxxxxx" :type (quote color))
> > M-x customize-variable foo
> >
> > In Customize, change the value to "any-non-color-string". You are able
> > to set the new value and even save it. There is no check done that the
> > value is actually a color - any string will do. The value should be
> > checked to ensure that it matches either a defined color name or "#"
> > followed by a multiple of three hex digits (for RGB).
>
> > Similarly, a variable declared to be of type `regexp' should have
> > its value tested to ensure that it is a valid regexp. Currently,
> > any string is accepted.
> >
> > In GNU Emacs 22.1.1 (i386-mingw-nt5.1.2600) of 2007-06-02 on RELEASE
> > Windowing system distributor `Microsoft Corp.', version 5.1.2600
> > configured using `configure --with-gcc (3.4) --cflags
> > -Ic:/gnuwin32/include'
I sent that to bug-gnu-emacs a little while ago (no response yet). Copying
it to emacs-devel, with some additional motivating info.
The `color' type, in particular, should be easy to test (either the string
is in the list of color names or it satisfies `#' followed by three groups
of hex digits).
Type `regexp' might be more difficult to test, but that too should be
possible, since we issue invalid-regexp errors for strings that aren't valid
regexps.
I hope this bug (missing feature) will be fixed. Types are powerful and
useful. They let you categorize and filter options.
I am also interested in this missing feature because I think the fix would
likely provide some type-checking code that I could use in other places.
I have, for instance, a command `describe-option-of-type' (which I bind to
`C-h M-o'). It is similar to `describe-variable', but it lets you use
defcustom types to narrow the set of completion candidates. I found no
existing code that handles subtyping and type equivalence, so I just made it
handle an exact type match for now, by default. But with `C-u' it also
includes as candidates options whose current values are compatible with the
specified type.
`C-h M-o color RET TAB' correctly gives all options defined with :type
`color' (rigid equality test). However, since Emacs currently does not
distinguish type `color' from type `string' (= this bug), `C-u C-h M-o'
color RET TAB' gives all string-valued variables as candidates. It would be
nice if only legitimate color-valued candidates were available for
completion. Similarly, for type `regexp'.
FWIW, so you can see what I mean and in case Emacs is interested in it, this
is my definition of the command:
(defun describe-option-of-type (type variable)
"Describe an Emacs user VARIABLE (option) of a given TYPE.
VARIABLE is defined with `defcustom' type TYPE.
With a prefix argument, either VARIABLE is defined with `defcustom'
type TYPE or its current value is compatible with TYPE.
If TYPE is nil (default value) then all `defcustom' variables are
potential candidates. That is different from using `describe-option',
because `describe-option' includes user-variable candidates not
defined with `defcustom' (with `*'-prefixed doc strings)."
(interactive
(let* ((symb (and (symbolp (variable-at-point))
(variable-at-point)))
(typ
(car
(condition-case err
(read-from-string
(let ((types ()))
(mapatoms
(lambda (cand)
(when (custom-variable-p cand)
(push
(list
(format
"%s"
(format "%S" (get cand 'custom-type))))
types))))
(completing-read "Describe option of type: "
(help-remove-duplicates types)
nil nil nil nil "nil")))
(end-of-file (error "No such custom type")))))
(pref-arg current-prefix-arg))
(list typ
(intern
(completing-read
"Option: " obarray
(lambda (var)
(and (custom-variable-p var)
(or (not typ) ; All vars if type = nil.
(if pref-arg
(help-var-is-of-type-p var (list typ))
(equal (get var 'custom-type) typ)))))
t nil nil (and symb (symbol-name symb)) t)))))
(describe-variable variable nil t))
(defun help-var-is-of-type-p (variable types)
"Return non-nil if VARIABLE is of one of the custom types in TYPES.
Non-nil means either VARIABLE's custom type is a member of list TYPES
or VARIABLE is bound and its value satisfies a type in list TYPES."
(or (memq (get variable 'custom-type) types)
(and (boundp variable)
(let ((val (symbol-value variable)))
(catch 'help-type-matches
(dolist (type types)
(setq type (widget-convert type))
(when (widget-apply type :match val)
(throw 'help-type-matches t)))
nil)))))
next parent reply other threads:[~2007-12-18 23:00 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <DNEMKBNJBGPAOPIJOOICAELMEBAA.drew.adams@oracle.com>
2007-12-18 23:00 ` Drew Adams [this message]
2007-12-20 0:53 ` custom type `color' is not enforced Richard Stallman
2007-12-20 1:27 ` Lennart Borgman (gmail)
2007-12-21 3:04 ` Richard Stallman
2007-12-20 19:11 ` Drew Adams
2007-12-20 19:27 ` Drew Adams
2007-12-21 3:59 ` Richard Stallman
2007-12-21 6:47 ` Drew Adams
2007-12-21 3:59 ` Richard Stallman
2007-12-21 6:48 ` Drew Adams
2007-12-21 9:36 ` Lennart Borgman (gmail)
2007-12-21 18:20 ` Drew Adams
2007-12-21 22:03 ` Drew Adams
2007-12-22 6:29 ` Richard Stallman
2007-12-22 20:58 ` Lennart Borgman (gmail)
2007-12-23 10:54 ` Per Abrahamsen
2007-12-23 12:46 ` Lennart Borgman (gmail)
2007-12-25 13:52 ` Per Abrahamsen
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
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=DNEMKBNJBGPAOPIJOOICIEBDECAA.drew.adams@oracle.com \
--to=drew.adams@oracle.com \
--cc=emacs-devel@gnu.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.savannah.gnu.org/cgit/emacs.git
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).