When creating a custom value with defcustom, how can I check user records?

I am writing an elisp file that develops into a package, so I translated some of its variables into defcustom and documented them. Several of these defcustom variables are related to each other, and I would like to check the values ​​entered through the Customize system to ensure that relationships are maintained.

Here is an example of what I have:

 (defcustom widget-canonical-names '("my_widget" . "widget_assembly 8911_j4") "Documentation" :type '(alist :key-type (string :tag "Widget short name") :value-type (string :tag "Full widget name")) :risky nil :group 'widgets) (defcustom widget-colors '("my_widget" . "brown") "Documentation" :type '(alist :key-type (string :tag "Widget short name") :value-type (color :tag "color of the widget")) :risky nil :group 'widgets) (defcustom widget-paths '("my_widget" . "~/widgets") "Documentation" :type '(alist :key-type (string :tag "Widget short name") :value-type (directory :tag "support files for widget")) :risky nil :group 'widgets) 

So, there are widgets, and they have different settings, and I need to have access to arbitrary widget settings, knowing only the short name of the widget. I would like to make some kind of validation function (googling around for "emacs defcustom validate, unfortunately did not help), so if the user enters the widget name in widget-paths or widget-colors , which is missing in widget-canonical-names list, they get "are you sure?" warning and are warned about entering inappropriate names. Can I attach such a check function to my defcustom s? If so, what is the syntax for this?

Of course, that would be ideal just to make the user enter the short name once, but I cannot figure out how to do this from the Elisp documentation for Composite Types. Therefore, an even better answer to my question will tell me how to organize a defcustom that sets up a data structure similar to this Python icon:

 customized_widgets = { "my_widget": { "canonical_name": "widget_assembly 8911_j4", "widget_color": "brown", "widget_path": "~/widgets", }, "another_widget": { "canonical_name" : "widget_obsolete 11.0", "widget_color": "blue", "widget_path": "~/blue_widgets", }, } 

So: how can I get the behavior that I want, where the parameters are grouped according to the data that will be used to access them, or where the validation function alerts users when they can enter inconsistent data?

+4
source share
1 answer

This will determine the closest Emacs equivalent of this Python structure, with dicts represented as alists, and fixed internal dict keys represented as characters.

 (defcustom my-customized-widgets () "My widget customization alist" :type '(alist :tag "Widgets" :key-type (string :tag "Short name") :value-type (set :format "%v" :entry-format "%b %v" (cons :format "%v" (const :format "" widget-canonical-name) (string :tag "CName")) (cons :format "%v" (const :format "" widget-color) (color :tag "Color")) (cons :format "%v" (const :format "" widget-path) (directory :tag " Path")))) :group 'widgets) 
+4
source

Source: https://habr.com/ru/post/1434066/


All Articles