From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Drew Adams" Newsgroups: gmane.emacs.devel Subject: RE: custom type `color' is not enforced Date: Fri, 21 Dec 2007 10:20:44 -0800 Message-ID: References: <476B8932.7030500@gmail.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-15" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1198261362 28231 80.91.229.12 (21 Dec 2007 18:22:42 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 21 Dec 2007 18:22:42 +0000 (UTC) Cc: emacs-devel@gnu.org To: "Lennart Borgman \(gmail\)" , Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Dec 21 19:22:54 2007 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1J5mW4-0008UD-Fw for ged-emacs-devel@m.gmane.org; Fri, 21 Dec 2007 19:22:36 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J5mVk-0004mb-HS for ged-emacs-devel@m.gmane.org; Fri, 21 Dec 2007 13:22:16 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1J5mVh-0004mW-Dr for emacs-devel@gnu.org; Fri, 21 Dec 2007 13:22:13 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1J5mVf-0004mK-0s for emacs-devel@gnu.org; Fri, 21 Dec 2007 13:22:12 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J5mVe-0004mH-Pq for emacs-devel@gnu.org; Fri, 21 Dec 2007 13:22:10 -0500 Original-Received: from rgminet01.oracle.com ([148.87.113.118]) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1J5mVa-0007EP-7j; Fri, 21 Dec 2007 13:22:06 -0500 Original-Received: from agmgw2.us.oracle.com (agmgw2.us.oracle.com [152.68.180.213]) by rgminet01.oracle.com (Switch-3.2.4/Switch-3.1.6) with ESMTP id lBLIM2em030538; Fri, 21 Dec 2007 11:22:03 -0700 Original-Received: from acsmt350.oracle.com (acsmt350.oracle.com [141.146.40.150]) by agmgw2.us.oracle.com (Switch-3.2.0/Switch-3.2.0) with ESMTP id lBLBxURg015771; Fri, 21 Dec 2007 11:22:02 -0700 Original-Received: from dhcp-amer-csvpn-gw1-141-144-66-79.vpn.oracle.com by acsmt350.oracle.com with ESMTP id 3459082601198261237; Fri, 21 Dec 2007 10:20:37 -0800 X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.6604 (9.0.2911.0) In-Reply-To: <476B8932.7030500@gmail.com> Importance: Normal X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198 X-Brightmail-Tracker: AAAAAQAAAAI= X-Brightmail-Tracker: AAAAAQAAAAI= X-Whitelist: TRUE X-Whitelist: TRUE X-detected-kernel: by monty-python.gnu.org: Linux 2.4-2.6 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:85353 Archived-At: > > 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).