emacs-devel
[Top][All Lists]
Advanced

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

Re: Community improvements to the Emacs Widget Library manual?


From: bovine
Subject: Re: Community improvements to the Emacs Widget Library manual?
Date: Fri, 14 Jul 2023 14:50:38 -0400
User-agent: Roundcube Webmail/1.5.2

On 2023-07-14 10:41, Mauro Aranda wrote:
Bryce Carson <bovine@cyberscientist.ca> writes:

Further, I can't seem to get these functions to work as documented or
expected.

Without these defined when I'm creating a widget with the following
code, I do get a button but the internal value is displayed in the clickable button. I'd like the external value to be displayed in the button %v, just
as the menu-tag is is "externally facing".

<<widget>>=
(define-widget 'project-widget 'list
  :tag (let ((s "\n\tProject"))
         (put-text-property 0 (length s) 'face 'bold s) s)
  :format "%t\n%v "
  :offset 0
  :indent 0
  :convert-widget 'widget-types-convert-widget
  :args '((menu-choice
           :tag "EmacSQL-supported backend"
           :format "%[%t%]: %[%v%]"
           :value "sqlite"
:value-to-external <<menu-choice internal value to external lambda>> :value-to-internal <<menu-choice external value to internal lambda>>
           (choice-item :menu-tag "MySQL" :value "mysql")
           (choice-item :menu-tag "PostgreSQL" :value "postgresql")
           (choice-item :menu-tag "SQLite" :value "sqlite"))))
<<menu-choice internal value to external lambda>>=
(lambda (widget internal-value)
  "Converts lowercase, internal values to the casing of trademarks."
  (pcase internal-value
    ("mysql" "MySQL")
    ("sqlite" "SQLite")
    ("postgresql" "PostgreSQL")))
<<menu-choice external value to internal lambda>>=
(lambda (widget external-value)
"Converts the casing of trademarked names to lowercase, internal values."
  (pcase external-value
    ("MySQL" "mysql")
    ("SQLite" "sqlite")

When possible, please post code that can be evaled without making
tweaks.

Some things I noted:

- Since the super is a list widget, then you don't need to specify
:convert-widget.  The :convert-widget function is one of the few that
gets called for all supers, so you end up converting twice the widget.
In this case, it doesn't seem you want that.

- You're specifying a value in the internal format for the menu-choice.
The manual specifies that when creating a widget or defining a new
one, the ‘:value’ should be in the external format.

- I think you should give similar conversion functions to all
choice-item, and also pass the :value in external format.

Let me know if you still find problems after fixing these things.

I fixed those issues and I do not see the invalid or void state, but only nil is ever displayed in the buffer.
Below is the code I defined.

---

(define-widget 'project-widget 'list
"A mimimal example to demonstrate 'styled' choice item buttons in a choice menu."
  :tag (let ((s "\n\tProject"))
         (put-text-property 0 (length s) 'face 'bold s) s)
  :format "%t\n%v "

  :args '((menu-choice
           :tag "EmacSQL-supported backend"
           :size 50
           :format "%[%t%]: %[%v%] \n"

           :value "SQLite"
:choice (database-choice-item :menu-tag "SQLite" :value "SQLite")

           :value-to-external menu-choice-value-to-external
           :value-to-internal menu-choice-value-to-internal

           (database-choice-item :menu-tag "MySQL"      :value "MySQL")
(database-choice-item :menu-tag "PostgreSQL" :value "PostgreSQL") (database-choice-item :menu-tag "SQLite" :value "SQLite"))))

(defun menu-choice-value-to-internal (widget external-value)
"Converts the casing of trademarked names to lowercase, internal values."
  (pcase external-value
    ("MySQL" "mysql")
    ("SQLite" "sqlite")
    ("PostgreSQL" "postgresql")))

(defun menu-choice-value-to-external (widget internal-value)
  "Converts lowercase, internal values to the casing of trademarks."
  (pcase internal-value
    ("mysql" "MySQL")
    ("sqlite" "SQLite")
    ("postgresql" "PostgreSQL")))

(define-widget 'database-choice-item 'choice-item
  nil
  :value-to-external #'menu-choice-value-to-external
  :value-to-internal #'menu-choice-value-to-internal)

(defun widget-value-conversion-example ()
       (interactive)
       (switch-to-buffer "*Widget Value Conversion Example*")
       (kill-all-local-variables)
       (let ((inhibit-read-only t))
         (erase-buffer))
       (remove-overlays)
       (widget-insert "Widget value conversion example.\n\n")
       (widget-create 'repeat 'project-widget)
       (use-local-map widget-keymap)
       (widget-setup))



reply via email to

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