From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Lennart Borgman (gmail)" Newsgroups: gmane.emacs.devel Subject: Re: custom type `color' is not enforced Date: Sat, 22 Dec 2007 21:58:14 +0100 Message-ID: <476D7A66.2080901@gmail.com> References: <476B8932.7030500@gmail.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------060400040001060804060403" X-Trace: ger.gmane.org 1198357128 29705 80.91.229.12 (22 Dec 2007 20:58:48 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 22 Dec 2007 20:58:48 +0000 (UTC) Cc: Per Abrahamsen , drew.adams@oracle.com, emacs-devel@gnu.org To: rms@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Dec 22 21:59:01 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 1J6BQy-0002UH-5X for ged-emacs-devel@m.gmane.org; Sat, 22 Dec 2007 21:59:00 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J6BQe-0007Qu-33 for ged-emacs-devel@m.gmane.org; Sat, 22 Dec 2007 15:58:40 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1J6BQZ-0007Qf-Uz for emacs-devel@gnu.org; Sat, 22 Dec 2007 15:58:36 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1J6BQX-0007QT-Fr for emacs-devel@gnu.org; Sat, 22 Dec 2007 15:58:34 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1J6BQX-0007QQ-9c for emacs-devel@gnu.org; Sat, 22 Dec 2007 15:58:33 -0500 Original-Received: from ch-smtp01.sth.basefarm.net ([80.76.149.212]) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1J6BQS-0007Oe-Vi; Sat, 22 Dec 2007 15:58:29 -0500 Original-Received: from c83-254-148-228.bredband.comhem.se ([83.254.148.228]:60523 helo=[127.0.0.1]) by ch-smtp01.sth.basefarm.net with esmtp (Exim 4.68) (envelope-from ) id 1J6BQQ-0005yy-5s; Sat, 22 Dec 2007 21:58:27 +0100 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.9) Gecko/20071031 Thunderbird/2.0.0.9 Mnenhy/0.7.5.666 In-Reply-To: X-Antivirus: avast! (VPS 071222-0, 2007-12-22), Outbound message X-Antivirus-Status: Clean X-Originating-IP: 83.254.148.228 X-Scan-Result: No virus found in message 1J6BQQ-0005yy-5s. X-Scan-Signature: ch-smtp01.sth.basefarm.net 1J6BQQ-0005yy-5s e3c33324d5368790ba85e648a0f78089 X-detected-kernel: by monty-python.gnu.org: Linux 2.6? (barebone, rare!) 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:85382 Archived-At: This is a multi-part message in MIME format. --------------060400040001060804060403 Content-Type: text/plain; charset=ISO-8859-15; format=flowed Content-Transfer-Encoding: 7bit Richard Stallman wrote: > You have the right idea. I have tried to finish the code too now ;-) Please see the attached files. Beside a more complete widget type for colors I have also included some basic functions for testing custom types. I believe these could be useful. I have tried to make them as clean and simple as they can be at the moment. However I am unsure about how to call the :match and :validate functions. I might very well be missing something concerning the conversion from and to external values. BTW when I have been looking at this I have had a hard time to understand why there are both :match and :validate functions. I also do not understand the paramters they take. Why do the :match function have a widget parameter? Does it have something to do with external - internal conversion, or? --------------060400040001060804060403 Content-Type: text/plain; name="color-test.el" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="color-test.el" (defun color-digits-p (color) (save-match-data (string-match (rx bos "#" (1+ (repeat 3 3 hex-digit)) eos) color))) (defun widget-color-match (widget value) (or ;; I am not sure what colors to test. It might be relevant to check ;; all as I suggest here. ;;(color-defined-p val) (member value x-colors) (and (stringp value) (color-digits-p value)))) (defun widget-color-validate (widget) (let ((value (widget-value widget))) (unless (widget-color-match widget value) (widget-put widget :error (format "Invalid color: %S" value)) widget))) (define-widget 'color 'editable-field "Choose a color (with sample)." :format "%{%t%}: %v (%{sample%})\n" :size 25 ;; (length "light coldenrod yellow") = 22 :tag "Color" :match 'widget-color-match :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) (defun custom-type-symbol-p (symbol custom-type) "Return t if value of symbol SYMBOL should fit CUSTOM-TYPE." (let ((found nil) (type (get symbol 'custom-type))) (while (and (not found) type) (setq found (eq type custom-type)) (setq type (car (get type 'widget-type)))) found)) (defun custom-type-value-p (value custom-type) "Return non-nil if value of VALUE fits CUSTOM-TYPE." (let ((widget (if (listp custom-type) custom-type (list custom-type)))) (setq widget (widget-convert widget)) ;; There are (unfortunately) two different ways to test the ;; values in a widget. Some widget types use both, some just one ;; of them. We check for both, but only use one of them here. (let ((match-fun (widget-get widget :match)) (validate-fun (widget-get widget :validate))) ;;(setq match-fun nil) ;;(setq validate-fun nil) (widget-put widget :value value) ;; Fix-me: I am not sure whether widget-apply of funcall ;; should be used here, but I believe anyone of them can be ;; used. But please look into this. It might have something to ;; do with internal/external values for the widgets. (cond ;; Test the :match alternative first because this because this ;; seems most basic. (match-fun (when ;;(widget-apply widget :match value) (funcall match-fun widget value) t)) (validate-fun (let (;;(val (widget-apply widget :validate)) (val (funcall validate-fun widget))) ;; Check if :error was applied (when (not (widget-get val :error)) t))) (t (error "There is no way to check value against custom type %s" custom-type)))))) (defun custom-type-p (val-or-sym custom-type) "Return non-nil if VAL-OR-SYM fits CUSTOM-TYPE. VAL-OR-SYM may be either a variable or a symbol. If it is a variable then return non-nil if the value fits custom type CUSTOM-TYPE. If it is a symbol then return non-nil if the values this symbol's variable can have fits CUSTOM-TYPE." (if (symbolp val-or-sym) (custom-type-symbol-p val-or-sym custom-type) (custom-type-value-p val-or-sym custom-type))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; Tests ;; (custom-type-p 'test-color 'color) ;; (custom-type-p 'test-color 'edit) ;; (custom-type-p 'test-color 'editable-field) ;; (custom-type-p test-color 'color) ;; (get 'test-color 'custom-type) ;; (setq test-color "bla") ;; (setq test-color "black") (defcustom test-color "black" "color test" :type 'color) (defun max-color-length() (let ((len 0) (longest "")) (mapc (lambda (color) (when (< len (length color)) (setq len (length color)) (setq longest color))) x-colors) (cons len longest))) ;; (max-color-length) --------------060400040001060804060403 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Emacs-devel mailing list Emacs-devel@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-devel --------------060400040001060804060403--