From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Mauro Aranda Newsgroups: gmane.emacs.bugs Subject: bug#45829: 28.0.50; Some tweaks to the color widget, from wid-edit+.el Date: Sat, 16 Jan 2021 09:10:35 -0300 Message-ID: <6002d7be.1c69fb81.a7260.b538@mx.google.com> References: <5ffe1f20.1c69fb81.77469.0e30@mx.google.com> <5ffe2149.1c69fb81.b9bf6.0d1d@mx.google.com> <87v9bxg7rl.fsf@tcd.ie> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="4904"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: 45829@debbugs.gnu.org To: "Basil L. Contovounesios" Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Jan 16 13:11:12 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l0kQB-0001AL-SZ for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 16 Jan 2021 13:11:11 +0100 Original-Received: from localhost ([::1]:33738 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1l0kQA-0002jg-Fh for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 16 Jan 2021 07:11:10 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:43746) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1l0kQ2-0002jB-Ie for bug-gnu-emacs@gnu.org; Sat, 16 Jan 2021 07:11:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:59216) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1l0kQ1-0008E9-Up for bug-gnu-emacs@gnu.org; Sat, 16 Jan 2021 07:11:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1l0kQ1-0001AW-PZ for bug-gnu-emacs@gnu.org; Sat, 16 Jan 2021 07:11:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Mauro Aranda Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 16 Jan 2021 12:11:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 45829 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 45829-submit@debbugs.gnu.org id=B45829.16107990484467 (code B ref 45829); Sat, 16 Jan 2021 12:11:01 +0000 Original-Received: (at 45829) by debbugs.gnu.org; 16 Jan 2021 12:10:48 +0000 Original-Received: from localhost ([127.0.0.1]:42529 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l0kPn-00019z-SO for submit@debbugs.gnu.org; Sat, 16 Jan 2021 07:10:48 -0500 Original-Received: from mail-qk1-f180.google.com ([209.85.222.180]:41507) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1l0kPl-00019g-5y for 45829@debbugs.gnu.org; Sat, 16 Jan 2021 07:10:47 -0500 Original-Received: by mail-qk1-f180.google.com with SMTP id 19so14409890qkm.8 for <45829@debbugs.gnu.org>; Sat, 16 Jan 2021 04:10:45 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=message-id:from:to:cc:subject:references:date:in-reply-to :user-agent:mime-version; bh=spLFes6WxsdFNIJqKNxZI7E2lPYmaFqUZ4QfhRif25I=; b=baSfy7nX+4SbFeuG49aXSQdlckgxMOvxS2CDpSejRA5rh7idRpPYOrSTN6v0bLDd9q 9sbabRRwFRC9TYhrSLDDphfUIDXYAVnI0xxqHGgg3cTOOPMD4Qg18CMS3vDD/GSMPheg CATVTFsPHiGLVcEbYE1Yvswlzst4y124FQszILNB2k5L5h4wLLPesAGbpZtQnKf+YDXH tTTTzC+BOP+ZRCTX9SkRwOW6g89dNSgOJigwFFnCQlzt9yiIN/R/QSr4jmz0991RXzae rfs4vfeKwXMG29c5CYccuA42sp3lebm28g0ioR68EJ9vX9UFe7355G22quyNbdBNPvWs CVhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:from:to:cc:subject:references:date :in-reply-to:user-agent:mime-version; bh=spLFes6WxsdFNIJqKNxZI7E2lPYmaFqUZ4QfhRif25I=; b=QUtbc1+tmANHkkOVdpNBSqoaVjQClv+nUY644G53SAa6i5a07j3aGaB7yeoR5gadpU aDyPZS9nczdjMz+hZC2POagNeUD+U9MS5ChW+OIVl5IpQfjkUWdWw9bWDu/mtCxjwoEB G/BtgAStfz+b3Y2JmYGwHc4TxmFW0ax93gtiSE3T5M35p/ZoNqYPQHUv8Q+y6mimn0Q3 hk9VPSaT+CGmUc60wNDFu1852ASCWc8t2bOZ4Mn6bqN845jkZdP9oIJORGw6TcFJHTe2 Ter0DZ2FldYJoh+bKtFEi1F2jhhv6/S1Dt1bRMGwAQ5dg3hzcrcYwT2tUB/KZbXgOzl3 KnIw== X-Gm-Message-State: AOAM530RbKHnUebOnUboZp4BEBXPVlAdahAt/Rz9JNJREP74pGcH181y ljXFQzRji8JHbNig6ECtdvrXEsQED94= X-Google-Smtp-Source: ABdhPJyEi72N7YS6woCPtSGumIvFa/CUwR1Fpsdh7vW8IPFmtQgPG93u4YYYPxTcUxe1Q6VJAiS51w== X-Received: by 2002:a05:620a:22a5:: with SMTP id p5mr16665352qkh.69.1610799039375; Sat, 16 Jan 2021 04:10:39 -0800 (PST) Original-Received: from tbb-desktop ([201.213.132.172]) by smtp.gmail.com with ESMTPSA id o29sm6585500qtl.7.2021.01.16.04.10.37 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Sat, 16 Jan 2021 04:10:38 -0800 (PST) X-Google-Original-Message-ID: <87k0sdqdz8.fsf@maurooaranda@gmail.com> In-Reply-To: <87v9bxg7rl.fsf@tcd.ie> (Basil L. Contovounesios's message of "Fri, 15 Jan 2021 22:22:38 +0000") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:198021 Archived-At: --=-=-= Content-Type: text/plain "Basil L. Contovounesios" writes: > Mauro Aranda writes: > >> Opinions? > > Just some minor nits from me. Hi Basil, thanks for taking a look. >> -;; Fixme: match >> (define-widget 'color 'editable-field >> "Choose a color name (with sample)." >> :format "%{%t%}: %v (%{sample%})\n" >> :value-create 'widget-color-value-create >> - :size 10 >> + :size (1+ (apply #'max (mapcar #'length (defined-colors)))) > > Is defined-colors guaranteed to return non-nil? > If not, you need (apply #'max 0 ...). Thanks for catching this one. I modified it so as to default to 13, which would be the longest hex string. >> +(defun widget-color-match (_widget value) >> + "Non-nil if VALUE is a defined color or a RGB hex string." >> + (and (stringp value) >> + (or (color-defined-p value) >> + (string-match-p "^#\\([[:xdigit:]]\\{3\\}\\)\\{1,4\\}$" value)))) > > Shouldn't that be "\\`#[[:xdigit:]]\\{3\\}\\{1,4\\}\\'" > or at least "\\`#\\(?:[[:xdigit:]]\\{3\\}\\)\\{1,4\\}\\'" > (if you want to be explicit)? I prefer the latter. I kept the ^ and $, though. >> +(ert-deftest widget-test-color-match () >> + "Test that the :match function for the color widget works." >> + (let* ((widget (widget-convert 'color))) > > Nit: could also be let. Ah yes, the let* was just a leftover. >> + (should (widget-apply widget :match "red")) >> + (should (widget-apply widget :match "#fa3")) >> + (should (widget-apply widget :match "#ff0000")) >> + (should (widget-apply widget :match "#111222333")) >> + (should (widget-apply widget :match "#111122223333")) >> + (should-not (widget-apply widget :match "someundefinedcolorihope")) >> + (should-not (widget-apply widget :match "#11223")))) > > Thanks, New patch attached, thanks. --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Tweaks-to-the-color-widget-Bug-45829.patch Content-Description: Patch >From be6b966173a9d0d455df884d28efcd6aaa0d54f7 Mon Sep 17 00:00:00 2001 From: Drew Adams Date: Sat, 16 Jan 2021 08:56:55 -0300 Subject: [PATCH] Tweaks to the color widget (Bug#45829) * lisp/wid-edit.el (widget-color-match, widget-color-validate): New functions. (color): Use the new functions. Base size on longest defined color name, defaulting to the longest RGB hex string. --- lisp/wid-edit.el | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/lisp/wid-edit.el b/lisp/wid-edit.el index 7dda04eda2..68a0d3d235 100644 --- a/lisp/wid-edit.el +++ b/lisp/wid-edit.el @@ -4026,17 +4026,19 @@ widget-boolean-prompt-value ;;; The `color' Widget. -;; Fixme: match (define-widget 'color 'editable-field "Choose a color name (with sample)." :format "%{%t%}: %v (%{sample%})\n" :value-create 'widget-color-value-create - :size 10 + :size (1+ (apply #'max 13 ; Longest RGB hex string. + (mapcar #'length (defined-colors)))) :tag "Color" :value "black" :completions (or facemenu-color-alist (defined-colors)) :sample-face-get 'widget-color-sample-face-get :notify 'widget-color-notify + :match #'widget-color-match + :validate #'widget-color-validate :action 'widget-color-action) (defun widget-color-value-create (widget) @@ -4085,6 +4087,19 @@ widget-color-notify (overlay-put (widget-get widget :sample-overlay) 'face (widget-apply widget :sample-face-get)) (widget-default-notify widget child event)) + +(defun widget-color-match (_widget value) + "Non-nil if VALUE is a defined color or a RGB hex string." + (and (stringp value) + (or (color-defined-p value) + (string-match-p "^#\\(?:[[:xdigit:]]\\{3\\}\\)\\{1,4\\}$" value)))) + +(defun widget-color-validate (widget) + "Check that WIDGET's value is a valid color." + (let ((value (widget-value widget))) + (unless (widget-color-match widget value) + (widget-put widget :error (format "Invalid color: %S" value)) + widget))) ;;; The Help Echo -- 2.29.2 --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Add-test-for-the-widget-color-match-function-Bug-458.patch Content-Description: Patch2 >From 19efd50021fd5e415305bd367d7fbf6a001db987 Mon Sep 17 00:00:00 2001 From: Mauro Aranda Date: Tue, 12 Jan 2021 19:19:21 -0300 Subject: [PATCH] Add test for the widget-color-match function (Bug#45829) * test/lisp/wid-edit-tests.el (widget-test-color-match): New test. --- test/lisp/wid-edit-tests.el | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test/lisp/wid-edit-tests.el b/test/lisp/wid-edit-tests.el index 17fdfefce8..f843649784 100644 --- a/test/lisp/wid-edit-tests.el +++ b/test/lisp/wid-edit-tests.el @@ -322,4 +322,15 @@ widget-test-widget-move (widget-backward 1) (should (string= "Second" (widget-value (widget-at)))))) +(ert-deftest widget-test-color-match () + "Test that the :match function for the color widget works." + (let ((widget (widget-convert 'color))) + (should (widget-apply widget :match "red")) + (should (widget-apply widget :match "#fa3")) + (should (widget-apply widget :match "#ff0000")) + (should (widget-apply widget :match "#111222333")) + (should (widget-apply widget :match "#111122223333")) + (should-not (widget-apply widget :match "someundefinedcolorihope")) + (should-not (widget-apply widget :match "#11223")))) + ;;; wid-edit-tests.el ends here -- 2.29.2 --=-=-=--