[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: custom type `color' is not enforced

From: Drew Adams
Subject: RE: custom type `color' is not enforced
Date: Tue, 18 Dec 2007 15:00:40 -0800

> 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

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)."
   (let* ((symb (and (symbolp (variable-at-point))
            (condition-case err
                 (let ((types ()))
                    (lambda (cand)
                      (when (custom-variable-p cand)
                           (format "%S" (get cand 'custom-type))))
                   (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
             "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)))

reply via email to

[Prev in Thread] Current Thread [Next in Thread]