unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Drew Adams" <drew.adams@oracle.com>
To: "Lennart Borgman \(gmail\)" <lennart.borgman@gmail.com>, <rms@gnu.org>
Cc: emacs-devel@gnu.org
Subject: RE: custom type `color' is not enforced
Date: Fri, 21 Dec 2007 10:20:44 -0800	[thread overview]
Message-ID: <BNELLINCGFJLDJIKDGACOEOOCFAA.drew.adams@oracle.com> (raw)
In-Reply-To: <476B8932.7030500@gmail.com>

> > I agree it would be good to make `color' check for a valid color.
> > I hope someone will do that.
>
> Something like this?
> (defun color-digits-p (color)
>    (save-match-data
>      (string-match (rx bos "#"
>                        (or (repeat 3 3 hex-digit)
>                            (repeat 6 6 hex-digit))
>                        eos)
>                    color)))
> (defun widget-color-validate (widget)
>    (let ((val (widget-value widget)))
>      (unless (or (member val x-colors)
>                  (and (stringp val)
>                       (color-digits-p val)))
>        (widget-put widget :error (format "Invalid color: %S"
>                                          (widget-value widget)))
>        widget)))
> (define-widget 'color 'editable-field
>    "Choose a color name (with sample)."
>    :format "%{%t%}: %v (%{sample%})\n"
>    :size 10
>    :tag "Color"
>    :validate 'widget-color-validate
>    :value "black"
>    :complete 'widget-color-complete
>    :sample-face-get 'widget-color-sample-face-get
>    :notify 'widget-color-notify
>    :action 'widget-color-action)
>
> (defcustom test-color "black" "color test" :type 'color)

Thanks, Lennart!

However:

- (defined-colors) should perhaps be used in place of `x-colors'.

- `color-digits-p' doesn't work for some legitimate colors, such as
"#FFFF0000FFFF". Any number of hex digits is legitimate, as long as it is a
multiple of 3 (RGB). FWIW, I use this regexp to check for an RGB string:

 "^#\\([a-fA-F0-9][a-fA-F0-9][a-fA-F0-9]\\)+$"

- Perhaps :size should be a bit larger? Some color names are longer than 10
chars. But I see that you took that and the rest of the `color' widget
definition from wid-edit.el: 10 is used in the Emacs definition of the
`color' widget.


Trying your fix made me realize that my type test was insufficient. I don't
understand widgets very well.

Instead of testing (widget-apply type :match val), it looks like I need to
test (and var-widget (not (widget-apply var-widget :validate))), where
var-widget is (get var 'custom-variable).

Here is what I use now; it seems to work (but suggestions are welcome). When
combined with your fix, it correctly distinguishes options whose values are
colors from those with arbitrary string values.

(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))
                 (var-widget (get variable 'custom-variable)))
             (catch 'help-type-matches
               (dolist (type types)
                 (setq type (widget-convert type))
                 (when (and var-widget
                       (not (widget-apply var-widget :validate)))
                   (throw 'help-type-matches t)))
               nil)))))

So `C-u C-h C-o' (my `describe-option-of-type') now shows, as completion
candidates, all options whose values are legitimate colors (and no others).

  reply	other threads:[~2007-12-21 18:20 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 ` custom type `color' is not enforced Drew Adams
2007-12-20  0:53   ` 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 [this message]
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=BNELLINCGFJLDJIKDGACOEOOCFAA.drew.adams@oracle.com \
    --to=drew.adams@oracle.com \
    --cc=emacs-devel@gnu.org \
    --cc=lennart.borgman@gmail.com \
    --cc=rms@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).