Most graphical user interface toolkits, such as Motif and XView, provide
a number of standard user interface controls (sometimes known as
`widgets' or `gadgets'). Emacs doesn't really support anything like
this, except for an incredible powerful text "widget". On the other
hand, Emacs does provide the necessary primitives to implement many
other widgets within a text buffer. The widget
package
simplifies this task.
The basic widgets are:
link
push-button
editable-field
menu-choice
radio-button-choice
item
menu-choice
and
radio-button-choice
widgets.
choice-item
toggle
checkbox
editable-list
Now of what possible use can support for widgets be in a text editor? I'm glad you asked. The answer is that widgets are useful for implementing forms. A form in Emacs is a buffer where the user is supposed to fill out a number of fields, each of which has a specific meaning. The user is not supposed to change or delete any of the text between the fields. Examples of forms in Emacs are the `forms' package (of course), the customize buffers, the mail and news compose modes, and the HTML form support in the `w3' browser.
The advantages for a programmer of using the widget
package to
implement forms are:
In order to minimize the code that is loaded by users who does not create any widgets, the code has been split in two files:
widget-define
, and autoload the function widget-create
.
A form consist of read only text for documentation and some fields, where each the fields contain two parts, as tag and a value. The tags are used to identify the fields, so the documentation can refer to the foo field, meaning the field tagged with `Foo'. Here is an example form:
Here is some documentation. Name: My Name Choose: This option Address: Some Place In some City Some country. See also _other work_ for more information. Numbers: count to three below [INS] [DEL] One [INS] [DEL] Eh, two? [INS] [DEL] Five! [INS] Select multiple: [X] This [ ] That [X] Thus Select one: (*) One ( ) Another One. ( ) A Final One. [Apply Form] [Reset Form]
The top level widgets in is example are tagged `Name', `Choose', `Address', `_other work_', `Numbers', `Select multiple', `Select one', `[Apply Form]', and `[Reset Form]'. There are basically two thing the user can do within a form, namely editing the editable text fields and activating the buttons.
In the example, the value for the `Name' is most likely displayed in an editable text field, and so are values for each of the members of the `Numbers' list. All the normal Emacs editing operations are available for editing these fields. The only restriction is that each change you make must be contained within a single editable text field. For example, capitalizing all text from the middle of one field to the middle of another field is prohibited.
Editing text fields are created by the editable-field
widget.
The editing text fields are highlighted with the
widget-field-face
face, making them easy to find.
Some portions of the buffer have an associated action, which can be invoked by a standard key or mouse command. These portions are called buttons. The default commands for activating a button are:
widget-global-map
(by default the global map).
widget-global-map
(by default the global map).
There are several different kind of buttons, all of which are present in the example:
menu-choice
widget. In
the example, `Choose' is an option field tag.
editable-list
widget.
link
widget.
radio-button-choice
widget can be
selected at any time. When you invoke one of the unselected radio
buttons, it will be selected and the previous selected radio button will
become unselected.
push-button
widget. The main
difference from the link
widget is that the buttons are will be
displayed as GUI buttons when possible.
enough.
To make them easier to locate, buttons are emphasized in the buffer.
You can use all the normal Emacs commands to move around in a form buffer, plus you will have these additional commands:
Here is the code to implement the user interface example (see section User Interface).
(require 'widget) (eval-when-compile (require 'wid-edit)) (defvar widget-example-repeat) (defun widget-example () "Create the widgets from the Widget manual." (interactive) (switch-to-buffer "*Widget Example*") (kill-all-local-variables) (make-local-variable 'widget-example-repeat) (let ((inhibit-read-only t)) (erase-buffer)) (widget-insert "Here is some documentation.\n\nName: ") (widget-create 'editable-field :size 13 "My Name") (widget-create 'menu-choice :tag "Choose" :value "This" :help-echo "Choose me, please!" :notify (lambda (widget &rest ignore) (message "%s is a good choice!" (widget-value widget))) '(item :tag "This option" :value "This") '(choice-item "That option") '(editable-field :menu-tag "No option" "Thus option")) (widget-insert "Address: ") (widget-create 'editable-field "Some Place\nIn some City\nSome country.") (widget-insert "\nSee also ") (widget-create 'link :notify (lambda (&rest ignore) (widget-value-set widget-example-repeat '("En" "To" "Tre")) (widget-setup)) "other work") (widget-insert " for more information.\n\nNumbers: count to three below\n") (setq widget-example-repeat (widget-create 'editable-list :entry-format "%i %d %v" :notify (lambda (widget &rest ignore) (let ((old (widget-get widget ':example-length)) (new (length (widget-value widget)))) (unless (eq old new) (widget-put widget ':example-length new) (message "You can count to %d." new)))) :value '("One" "Eh, two?" "Five!") '(editable-field :value "three"))) (widget-insert "\n\nSelect multiple:\n\n") (widget-create 'checkbox t) (widget-insert " This\n") (widget-create 'checkbox nil) (widget-insert " That\n") (widget-create 'checkbox :notify (lambda (&rest ignore) (message "Tickle")) t) (widget-insert " Thus\n\nSelect one:\n\n") (widget-create 'radio-button-choice :value "One" :notify (lambda (widget &rest ignore) (message "You selected %s" (widget-value widget))) '(item "One") '(item "Another One.") '(item "A Final One.")) (widget-insert "\n") (widget-create 'push-button :notify (lambda (&rest ignore) (if (= (length (widget-value widget-example-repeat)) 3) (message "Congratulation!") (error "Three was the count!"))) "Apply Form") (widget-insert " ") (widget-create 'push-button :notify (lambda (&rest ignore) (widget-example)) "Reset Form") (widget-insert "\n") (use-local-map widget-keymap) (widget-setup))
Widgets are created with widget-create
, which returns a
widget object. This object can be queried and manipulated by
other widget functions, until it is deleted with widget-delete
.
After the widgets have been created, widget-setup
must be called
to enable them.
The keyword arguments can be used to overwrite the keyword arguments that are part of type.
This should be called after creating all the widgets and before allowing the user to edit them.
If you want to insert text outside the widgets in the form, the
recommended way to do that is with widget-insert
.
There is a standard widget keymap which you might find useful.
widget-forward
and
widget-backward
, respectively. RET and mouse-2
are bound to widget-button-press
and
widget-button-
.
widget-button-press
and widget-button-click
when not on a button. By default this is global-map
.
The syntax of a type specification is given below:
NAME ::= (NAME [KEYWORD ARGUMENT]... ARGS) | NAME
Where, name is a widget name, keyword is the name of a property, argument is the value of the property, and args are interpreted in a widget specific way.
There following keyword arguments that apply to all widgets:
:value
:format
widget-button-face
, and
surrounded by brackets.
:sample-face
.
:doc
here.
:doc
property in the widget, it will
instead examine the :documentation-property
property. If it is a
lambda expression, it will be called with the widget's value as an
argument, and the result will be used as the documentation text.
:tag
here, or the princ
representation of the value if there is no tag.
:button-face
:button-prefix
:button-suffix
:doc
:tag
:tag-glyph
:help-echo
widget-forward
or widget-backward
.
:indent
:offset
:extra-offset
:notify
:menu-tag
menu-choice
widget.
:menu-tag-get
menu-choice
widget. By default, the tag used will be either the
:menu-tag
or :tag
property if present, or the princ
representation of the :value
property if not.
:match
:validate
:error
property to a string explaining the error.
The following predefined function can be used:
:children
of widget must be valid.
:tab-order
widget-forward
or widget-backward
. This is only partially
implemented.
-1
are ignored.
nil
,
whichever comes first.
nil
:parent
menu-choice
item or an
element of a editable-list
widget).
:sibling-args
radio-button-choice
or
checklist
. The value should be a list of extra keyword
arguments, which will be used when creating the radio-button
or
checkbox
associated with this item.
link
WidgetSyntax:
TYPE ::= (link [KEYWORD ARGUMENT]... [ VALUE ])
The value, if present, is used to initialize the :value
property. The value should be a string, which will be inserted in the
buffer.
By default the link will be shown in brackets.
url-link
WidgetSyntax:
TYPE ::= (url-link [KEYWORD ARGUMENT]... URL)
When this link is invoked, the WWW browser specified by
browse-url-browser-function
will be called with url.
info-link
WidgetSyntax:
TYPE ::= (info-link [KEYWORD ARGUMENT]... ADDRESS)
When this link is invoked, the build-in info browser is started on address.
push-button
WidgetSyntax:
TYPE ::= (push-button [KEYWORD ARGUMENT]... [ VALUE ])
The value, if present, is used to initialize the :value
property. The value should be a string, which will be inserted in the
buffer.
By default the tag will be shown in brackets.
editable-field
WidgetSyntax:
TYPE ::= (editable-field [KEYWORD ARGUMENT]... [ VALUE ])
The value, if present, is used to initialize the :value
property. The value should be a string, which will be inserted in
field. This widget will match all string values.
The following extra properties are recognized.
:size
:value-face
widget-field-face
.
:secret
?*
if the field contains a password or other secret information. By
default, the value is not secret.
:valid-regexp
:validate
function will match the content of the
field with the value of this attribute. The default value is ""
which matches everything.
:keymap
widget-field-keymap
, which allows you to use all the normal
editing commands, even if the buffers major mode suppress some of them.
Pressing return invokes the function specified by :action
.
text
Widget
This is just like editable-field
, but intended for multiline text
fields. The default :keymap
is widget-text-keymap
, which
does not rebind the return key.
menu-choice
WidgetSyntax:
TYPE ::= (menu-choice [KEYWORD ARGUMENT]... TYPE ... )
The type arguments represents each possible choice. The widgets value of will be the value of the chosen type argument. This widget will match any value that matches at least one of the specified type arguments.
:void
:case-fold
:children
:choice
:args
radio-button-choice
WidgetSyntax:
TYPE ::= (radio-button-choice [KEYWORD ARGUMENT]... TYPE ... )
The type arguments represents each possible choice. The widgets value of will be the value of the chosen type argument. This widget will match any value that matches at least one of the specified type arguments.
The following extra properties are recognized.
:entry-format
button-args
:buttons
:children
:choice
:args
You can add extra radio button items to a radio-button-choice
widget after it has been created with the function
widget-radio-add-item
.
radio-button-choice
widget widget a new radio button item of type
type.
Please note that such items added after the radio-button-choice
widget has been created will not be properly destructed when
you call widget-delete
.
item
WidgetSyntax:
ITEM ::= (item [KEYWORD ARGUMENT]... VALUE)
The value, if present, is used to initialize the :value
property. The value should be a string, which will be inserted in the
buffer. This widget will only match the specified value.
choice-item
WidgetSyntax:
ITEM ::= (choice-item [KEYWORD ARGUMENT]... VALUE)
The value, if present, is used to initialize the :value
property. The value should be a string, which will be inserted in the
buffer as a button. Activating the button of a choice-item
is
equivalent to activating the parent widget. This widget will only match
the specified value.
toggle
WidgetSyntax:
TYPE ::= (toggle [KEYWORD ARGUMENT]...)
The widget has two possible states, `on' and `off', which corresponds to
a t
or nil
value.
The following extra properties are recognized.
:on
:off
:on-glyph
:off-glyph
checkbox
Widget
The widget has two possible states, `selected' and `unselected', which
corresponds to a t
or nil
value.
Syntax:
TYPE ::= (checkbox [KEYWORD ARGUMENT]...)
checklist
WidgetSyntax:
TYPE ::= (checklist [KEYWORD ARGUMENT]... TYPE ... )
The type arguments represents each checklist item. The widgets value of will be a list containing the value of each ticked type argument. The checklist widget will match a list whose elements all matches at least one of the specified type arguments.
The following extra properties are recognized.
:entry-format
:greedy
:greedy
to
non-nil, it will allow the items to come in any sequence. However, if
you extract the value they will be in the sequence given in the
checklist. I.e. the original sequence is forgotten.
button-args
:buttons
:children
:args
editable-list
WidgetSyntax:
TYPE ::= (editable-list [KEYWORD ARGUMENT]... TYPE)
The value is a list, where each member represents one widget of type type.
The following extra properties are recognized.
:entry-format
:insert-button-args
:delete-button-args
:append-button-args
:buttons
:children
:args
group
WidgetThis widget simply group other widget together.
Syntax:
TYPE ::= (group [KEYWORD ARGUMENT]... TYPE...)
The value is a list, with one member for each type.
A number of widgets for editing s-expressions (lisp types) are also available. These basically fall in the following categories.
The const
widget can contain any lisp expression, but the user is
prohibited from editing edit it, which is mainly useful as a component
of one of the composite widgets.
The syntax for the const
widget is
TYPE ::= (const [KEYWORD ARGUMENT]... [ VALUE ])
The value, if present, is used to initialize the :value
property and can be any s-expression.
There are two variations of the const
widget, namely
variable-item
and function-item
. These should contain a
symbol with a variable or function binding. The major difference from
the const
widget is that they will allow the user to see the
variable or function documentation for the symbol.
The sexp
widget can contain any lisp expression, and allows the
user to edit it inline in the buffer.
The syntax for the sexp
widget is
TYPE ::= (sexp [KEYWORD ARGUMENT]... [ VALUE ])
The sexp
widget takes the same keyword arguments as the
editable-field
widget.
The atoms are s-expressions that does not consist of other s-expressions. A string is an atom, while a list is a composite type. You can edit the value of an atom with the following widgets.
The syntax for all the atoms are
TYPE ::= (NAME [KEYWORD ARGUMENT]... [ VALUE ])
The value, if present, is used to initialize the :value
property and must be an expression of the same type as the widget.
I.e. the string widget can only be initialized with a string.
All the atom widgets take the same keyword arguments as the
editable-field
widget.
Keywords:
:must-match
file
widget.
The syntax for the composite are
TYPE ::= (NAME [KEYWORD ARGUMENT]... COMPONENT...)
Where each component must be a widget type. Each component widget will be displayed in the buffer, and be editable to the user.
cons
widget is a cons-cell where the car is the
value of the first component and the cdr is the value of the second
component. There must be exactly two components.
list
widget is a list containing the value of
each of its component.
vector
widget is a vector containing the value of
each of its component.
The above suffice for specifying fixed size lists and vectors. To get
variable length lists and vectors, you can use a choice
,
set
or repeat
widgets together with the :inline
keywords. If any component of a composite widget has the :inline
keyword set, its value must be a list which will then be spliced into
the composite. For example, to specify a list whose first element must
be a file name, and whose remaining arguments should either by the
symbol t
or two files, you can use the following widget
specification:
(list file (choice (const t) (list :inline t :value ("foo" "bar") string string)))
The value of a widget of this type will either have the form
`(file t)' or (file string string)
.
This concept of inline is probably hard to understand. It was certainly hard to implement so instead of confuse you more by trying to explain it here, I'll just suggest you meditate over it for a while.
choice-menu
basic widget, and
has a similar syntax.
checklist
basic widget, and has a
similar syntax.
You can examine or set the value of a widget by using the widget object
that was returned by widget-create
.
Important: You must call widget-setup
after
modifying the value of a widget before the user is allowed to edit the
widget again. It is enough to call widget-setup
once if you
modify multiple widgets. This is currently only necessary if the widget
contains an editing field, but may be necessary for other widgets in the
future.
If your application needs to associate some information with the widget
objects, for example a reference to the item being edited, it can be
done with widget-put
and widget-get
. The property names
must begin with a `:'.
widget-put
for property.
Occasionally it can be useful to know which kind of widget you have, i.e. the name of the widget type you gave when the widget was created.
Widgets can be in two states: active, which means they are modifiable by the user, or inactive, which means they cannot be modified by the user. You can query or set the state with the following code:
;; Examine if widget is active or not. (if (widget-apply widget :active) (message "Widget is active.") (message "Widget is inactive.") ;; Make widget inactive. (widget-apply widget :deactivate) ;; Make widget active. (widget-apply widget :activate)
A widget is inactive if itself, or any of its ancestors (found by
following the :parent
link) have been deactivated. To make sure
a widget is really active, you must therefore activate both itself, and
all its ancestors.
(while widget (widget-apply widget :activate) (setq widget (widget-get widget :parent)))
You can check if a widget has been made inactive by examining the value
of :inactive
keyword. If this is non-nil, the widget itself has
been deactivated. This is different from using the :active
keyword, in that the later tell you if the widget or any of its
ancestors have been deactivated. Do not attempt to set the
:inactive
keyword directly. Use the :activate
:deactivated
keywords instead.
You can define specialized widgets with define-widget
. It allows
you to create a shorthand for more complex widgets, including specifying
component widgets and default new default values for the keyword
arguments.
class
.
name and class should both be symbols, class
should be one
of the existing widget types.
The third argument DOC is a documentation string for the widget.
After the new widget has been defined, the following two calls will create identical widgets:
(widget-create name)
(apply widget-create class args)
Using widget-define
does just store the definition of the widget
type in the widget-type
property of name, which is what
widget-create
uses.
If you just want to specify defaults for keywords with no complex
conversions, you can use identity
as your conversion function.
The following additional keyword arguments are useful when defining new widgets:
:convert-widget
:args
as widget types in widget.
:value
from :args
in widget.
:value-to-internal
:value
when the widget is created, and on any value set later with
widget-value-set
.
:value-to-external
:value
when the widget is created, and on any value set later with
widget-value-set
.
:create
:delete
:value-create
:value-delete
:children
and :buttons
in widget.
:value-get
:value
property of widget.
:format-handler
widget-default-format-handler
to handle
unknown escape sequences, which will handle the `%h' and any future
escape sequences, as well as give an error for unknown escapes.
:action
:notify
the parent.
The following predefined function can be used here:
:parent
of widget to handle the :action
.
Optional event is the event that triggered the action.
:prompt-value
If you want to define a new widget from scratch, use the default
widget as its base.
It provides most of the functionality that is referred to as "by default" in this text.
There is a separate package to browse widgets. This is intended to help programmers who want to examine the content of a widget. The browser shows the value of each keyword, but uses links for certain keywords such as `:parent', which avoids printing cyclic structures.
There is a minor mode for manipulating widgets in major modes that doesn't provide any support for widgets themselves. This is mostly intended to be useful for programmers doing experiments.
widget-minor-mode
.
menu-choice
tag should be prettier, something like the abbreviated
menus in Open Look.
:tab-order
.
property-list
widget.
association-list
widget.
key-binding
widget.
widget
widget for editing widget specifications.
TeX-printer-list
for an explanation.
widget-prompt-value
should give type specific help.
This document was generated on 6 November 2000 using texi2html 1.56k.