Import No Gnus v0.0.
[elisp/gnus.git-] / texi / widget.texi
1 \input texinfo.tex
2
3 @c %**start of header
4 @setfilename widget
5 @settitle The Emacs Widget Library
6 @iftex
7 @afourpaper
8 @headings double
9 @end iftex
10 @c %**end of header
11
12 @node Top, Introduction, (dir), (dir)
13 @comment  node-name,  next,  previous,  up
14 @top The Emacs Widget Library
15
16 Version: 1.82
17
18 @menu
19 * Introduction::                
20 * User Interface::              
21 * Programming Example::         
22 * Setting Up the Buffer::       
23 * Basic Types::                 
24 * Sexp Types::                  
25 * Widget Properties::           
26 * Defining New Widgets::        
27 * Widget Wishlist.::            
28 @end menu
29
30 @node  Introduction, User Interface, Top, Top
31 @comment  node-name,  next,  previous,  up
32 @section Introduction
33
34 Most graphical user interface toolkits, such as Motif and XView, provide
35 a number of standard user interface controls (sometimes known as
36 `widgets' or `gadgets').  Emacs doesn't really support anything like
37 this, except for an incredible powerful text ``widget''.  On the other
38 hand, Emacs does provide the necessary primitives to implement many
39 other widgets within a text buffer.  The @code{widget} package
40 simplifies this task.
41
42 The basic widgets are:
43
44 @table @code
45 @item link
46 Areas of text with an associated action.  Intended for hypertext links
47 embedded in text.
48 @item push-button 
49 Like link, but intended for stand-alone buttons.
50 @item editable-field
51 An editable text field.  It can be either variable or fixed length.
52 @item menu-choice
53 Allows the user to choose one of multiple options from a menu, each
54 option is itself a widget.  Only the selected option will be visible in
55 the buffer.
56 @item radio-button-choice
57 Allows the user to choose one of multiple options by pushing radio
58 buttons.  The options are implemented as widgets.  All options will be
59 visible in the buffer.
60 @item item
61 A simple constant widget intended to be used in the @code{menu-choice} and
62 @code{radio-button-choice} widgets. 
63 @item choice-item
64 An button item only intended for use in choices.  When pushed, the user
65 will be asked to select another option from the choice widget.
66 @item toggle
67 A simple @samp{on}/@samp{off} switch.
68 @item checkbox
69 A checkbox (@samp{[ ]}/@samp{[X]}). 
70 @item editable-list
71 Create an editable list.  The user can insert or delete items in the
72 list.  Each list item is itself a widget.
73 @end table
74
75 Now of what possible use can support for widgets be in a text editor?
76 I'm glad you asked.  The answer is that widgets are useful for
77 implementing forms.  A @dfn{form} in emacs is a buffer where the user is
78 supposed to fill out a number of fields, each of which has a specific
79 meaning.  The user is not supposed to change or delete any of the text
80 between the fields.  Examples of forms in Emacs are the @file{forms}
81 package (of course), the customize buffers, the mail and news compose
82 modes, and the @sc{html} form support in the @file{w3} browser.  
83
84 The advantages for a programmer of using the @code{widget} package to
85 implement forms are:
86
87 @enumerate
88 @item
89 More complex field than just editable text are supported. 
90 @item
91 You can give the user immediate feedback if he enters invalid data in a
92 text field, and sometimes prevent entering invalid data.
93 @item 
94 You can have fixed sized fields, thus allowing multiple field to be
95 lined up in columns.
96 @item
97 It is simple to query or set the value of a field. 
98 @item 
99 Editing happens in buffer, not in the mini-buffer.
100 @item 
101 Packages using the library get a uniform look, making them easier for
102 the user to learn.
103 @item 
104 As support for embedded graphics improve, the widget library will
105 extended to support it.  This means that your code using the widget
106 library will also use the new graphic features by automatic.
107 @end enumerate
108
109 In order to minimize the code that is loaded by users who does not
110 create any widgets, the code has been split in two files:
111
112 @table @file
113 @item widget.el
114 This will declare the user variables, define the function
115 @code{widget-define}, and autoload the function @code{widget-create}. 
116 @item wid-edit.el
117 Everything else is here, there is no reason to load it explicitly, as
118 it will be autoloaded when needed.
119 @end table
120
121 @node User Interface, Programming Example, Introduction, Top
122 @comment  node-name,  next,  previous,  up
123 @section User Interface
124
125 A form consist of read only text for documentation and some fields,
126 where each the fields contain two parts, as tag and a value.  The tags
127 are used to identify the fields, so the documentation can refer to the
128 foo field, meaning the field tagged with @samp{Foo}. Here is an example
129 form:
130
131 @example
132 Here is some documentation.
133
134 Name: @i{My Name}     @strong{Choose}: This option
135 Address:  @i{Some Place
136 In some City
137 Some country.}
138
139 See also @b{_other work_} for more information.
140
141 Numbers: count to three below
142 @b{[INS]} @b{[DEL]} @i{One}
143 @b{[INS]} @b{[DEL]} @i{Eh, two?}
144 @b{[INS]} @b{[DEL]} @i{Five!}
145 @b{[INS]} 
146
147 Select multiple:
148
149 @b{[X]} This
150 @b{[ ]} That
151 @b{[X]} Thus
152
153 Select one:
154
155 @b{(*)} One
156 @b{( )} Another One.
157 @b{( )} A Final One.
158
159 @b{[Apply Form]} @b{[Reset Form]}
160 @end example
161
162 The top level widgets in is example are tagged @samp{Name},
163 @samp{Choose}, @samp{Address}, @samp{_other work_}, @samp{Numbers},
164 @samp{Select multiple}, @samp{Select one}, @samp{[Apply Form]}, and
165 @samp{[Reset Form]}.  There are basically two thing the user can do within
166 a form, namely editing the editable text fields and activating the
167 buttons.
168
169 @subsection Editable Text Fields
170
171 In the example, the value for the @samp{Name} is most likely displayed
172 in an editable text field, and so are values for each of the members of
173 the @samp{Numbers} list.  All the normal Emacs editing operations are
174 available for editing these fields.  The only restriction is that each
175 change you make must be contained within a single editable text field.
176 For example, capitalizing all text from the middle of one field to the
177 middle of another field is prohibited.
178
179 Editing text fields are created by the @code{editable-field} widget.
180
181 The editing text fields are highlighted with the
182 @code{widget-field-face} face, making them easy to find.
183
184 @deffn Face widget-field-face
185 Face used for other editing fields.
186 @end deffn
187
188 @subsection Buttons
189
190 Some portions of the buffer have an associated @dfn{action}, which can
191 be @dfn{activated} by a standard key or mouse command.  These portions
192 are called @dfn{buttons}.  The default commands for activating a button
193 are:
194
195 @table @kbd
196 @item @key{RET}
197 @deffn Command widget-button-press @var{pos} &optional @var{event}
198 Activate the button at @var{pos}, defaulting to point.
199 If point is not located on a button, activate the binding in
200 @code{widget-global-map} (by default the global map).
201 @end deffn
202
203 @item mouse-2
204 @deffn Command widget-button-click @var{event}
205 Activate the button at the location of the mouse pointer.  If the mouse
206 pointer is located in an editable text field, activate the binding in
207 @code{widget-global-map} (by default the global map).
208 @end deffn
209 @end table
210
211 There are several different kind of buttons, all of which are present in
212 the example:
213
214 @table @emph
215 @item The Option Field Tags.
216 When you activate one of these buttons, you will be asked to choose
217 between a number of different options.  This is how you edit an option
218 field.  Option fields are created by the @code{menu-choice} widget.  In
219 the example, @samp{@b{Choose}} is an option field tag.
220 @item The @samp{@b{[INS]}} and @samp{@b{[DEL]}} buttons.
221 Activating these will insert or delete elements from a editable list.
222 The list is created by the @code{editable-list} widget. 
223 @item Embedded Buttons.
224 The @samp{@b{_other work_}} is an example of an embedded
225 button. Embedded buttons are not associated with a fields, but can serve
226 any purpose, such as implementing hypertext references.  They are
227 usually created by the @code{link} widget.
228 @item The @samp{@b{[ ]}} and @samp{@b{[X]}} buttons.
229 Activating one of these will convert it to the other.  This is useful
230 for implementing multiple-choice fields.  You can create it wit
231 @item The @samp{@b{( )}} and @samp{@b{(*)}} buttons.
232 Only one radio button in a @code{radio-button-choice} widget can be selected at any
233 time.  When you push one of the unselected radio buttons, it will be
234 selected and the previous selected radio button will become unselected. 
235 @item The @samp{@b{[Apply Form]}} @samp{@b{[Reset Form]}} buttons.
236 These are explicit buttons made with the @code{push-button} widget.  The main
237 difference from the @code{link} widget is that the buttons are will be
238 displayed as GUI buttons when possible.
239 enough. 
240 @end table
241
242 To make them easier to locate, buttons are emphasized in the buffer.  
243
244 @deffn Face widget-button-face
245 Face used for buttons.
246 @end deffn
247
248 @defopt widget-mouse-face
249 Face used for buttons when the mouse pointer is above it.
250 @end defopt
251
252 @subsection Navigation
253
254 You can use all the normal Emacs commands to move around in a form
255 buffer, plus you will have these additional commands:
256
257 @table @kbd
258 @item @key{TAB}
259 @deffn Command widget-forward &optional count
260 Move point @var{count} buttons or editing fields forward.
261 @end deffn
262 @item @key{M-TAB}
263 @deffn Command widget-backward &optional count
264 Move point @var{count} buttons or editing fields backward.
265 @end deffn
266 @end table
267
268 @node Programming Example, Setting Up the Buffer, User Interface, Top
269 @comment  node-name,  next,  previous,  up
270 @section Programming Example
271
272 Here is the code to implement the user interface example (see @ref{User
273 Interface}).
274
275 @lisp
276 (require 'widget)
277
278 (eval-when-compile
279   (require 'wid-edit))
280
281 (defvar widget-example-repeat)
282
283 (defun widget-example ()
284   "Create the widgets from the Widget manual."
285   (interactive)
286   (switch-to-buffer "*Widget Example*")
287   (kill-all-local-variables)
288   (make-local-variable 'widget-example-repeat)
289   (let ((inhibit-read-only t))
290     (erase-buffer))
291   (widget-insert "Here is some documentation.\n\nName: ")
292   (widget-create 'editable-field
293                  :size 13
294                  "My Name")
295   (widget-create 'menu-choice
296                  :tag "Choose"
297                  :value "This"
298                  :help-echo "Choose me, please!"
299                  :notify (lambda (widget &rest ignore)
300                            (message "%s is a good choice!"
301                                     (widget-value widget)))
302                  '(item :tag "This option" :value "This")
303                  '(choice-item "That option")
304                  '(editable-field :menu-tag "No option" "Thus option"))
305   (widget-insert "Address: ")
306   (widget-create 'editable-field
307                  "Some Place\nIn some City\nSome country.")
308   (widget-insert "\nSee also ")
309   (widget-create 'link
310                  :notify (lambda (&rest ignore)
311                            (widget-value-set widget-example-repeat 
312                                              '("En" "To" "Tre"))
313                            (widget-setup))
314                  "other work")
315   (widget-insert " for more information.\n\nNumbers: count to three below\n")
316   (setq widget-example-repeat
317         (widget-create 'editable-list
318                        :entry-format "%i %d %v"
319                        :notify (lambda (widget &rest ignore)
320                                  (let ((old (widget-get widget
321                                                         ':example-length))
322                                        (new (length (widget-value widget))))
323                                    (unless (eq old new)
324                                      (widget-put widget ':example-length new)
325                                      (message "You can count to %d." new))))
326                        :value '("One" "Eh, two?" "Five!")
327                        '(editable-field :value "three")))
328   (widget-insert "\n\nSelect multiple:\n\n")
329   (widget-create 'checkbox t)
330   (widget-insert " This\n")
331   (widget-create 'checkbox nil)
332   (widget-insert " That\n")
333   (widget-create 'checkbox
334                  :notify (lambda (&rest ignore) (message "Tickle"))
335                  t)
336   (widget-insert " Thus\n\nSelect one:\n\n")
337   (widget-create 'radio-button-choice
338                  :value "One"
339                  :notify (lambda (widget &rest ignore)
340                            (message "You selected %s"
341                                     (widget-value widget)))
342                  '(item "One") '(item "Anthor One.") '(item "A Final One."))
343   (widget-insert "\n")
344   (widget-create 'push-button
345                  :notify (lambda (&rest ignore) 
346                            (if (= (length (widget-value widget-example-repeat))
347                                   3)
348                                (message "Congratulation!")
349                              (error "Three was the count!")))
350                  "Apply Form")
351   (widget-insert " ")
352   (widget-create 'push-button
353                  :notify (lambda (&rest ignore)
354                            (widget-example))
355                  "Reset Form")
356   (widget-insert "\n")
357   (use-local-map widget-keymap)
358   (widget-setup))
359 @end lisp
360
361 @node Setting Up the Buffer, Basic Types, Programming Example, Top
362 @comment  node-name,  next,  previous,  up
363 @section Setting Up the Buffer
364
365 Widgets are created with @code{widget-create}, which returns a
366 @dfn{widget} object.  This object can be queried and manipulated by
367 other widget functions, until it is deleted with @code{widget-delete}.
368 After the widgets have been created, @code{widget-setup} must be called
369 to enable them.
370
371 @defun widget-create type [ keyword argument ]@dots{}
372 Create and return a widget of type @var{type}.
373 The syntax for the @var{type} argument is described in @ref{Basic Types}.
374
375 The keyword arguments can be used to overwrite the keyword arguments
376 that are part of @var{type}.
377 @end defun
378
379 @defun widget-delete widget
380 Delete @var{widget} and remove it from the buffer.
381 @end defun
382
383 @defun widget-setup 
384 Setup a buffer to support widgets. 
385
386 This should be called after creating all the widgets and before allowing
387 the user to edit them.
388 @refill
389 @end defun
390
391 If you want to insert text outside the widgets in the form, the
392 recommended way to do that is with @code{widget-insert}.
393
394 @defun widget-insert 
395 Insert the arguments, either strings or characters, at point.
396 The inserted text will be read only.
397 @end defun
398
399 There is a standard widget keymap which you might find useful.
400
401 @defvr Const widget-keymap
402 A keymap with the global keymap as its parent.@*
403 @key{TAB} and @kbd{C-@key{TAB}} are bound to @code{widget-forward} and
404 @code{widget-backward}, respectively.  @kbd{@key{RET}} and @kbd{mouse-2}
405 are bound to @code{widget-button-press} and
406 @code{widget-button-}.@refill
407 @end defvr
408
409 @defvar widget-global-map
410 Keymap used by @code{widget-button-press} and @code{widget-button-click}
411 when not on a button.  By default this is @code{global-map}.
412 @end defvar
413
414 @node Basic Types, Sexp Types, Setting Up the Buffer, Top
415 @comment  node-name,  next,  previous,  up
416 @section Basic Types
417
418 The syntax of a type specification is given below:
419
420 @example
421 NAME ::= (NAME [KEYWORD ARGUMENT]... ARGS)
422      |   NAME
423 @end example
424
425 Where, @var{name} is a widget name, @var{keyword} is the name of a
426 property, @var{argument} is the value of the property, and @var{args}
427 are interpreted in a widget specific way.
428
429 There following keyword arguments that apply to all widgets:
430
431 @table @code
432 @item :value
433 The initial value for widgets of this type.
434
435 @item :format
436 This string will be inserted in the buffer when you create a widget.
437 The following @samp{%} escapes are available:
438
439 @table @samp
440 @item %[
441 @itemx %]
442 The text inside will be marked as a button.
443
444 @item %@{
445 @itemx %@}
446 The text inside will be displayed with the face specified by
447 @code{:sample-face}. 
448
449 @item %v
450 This will be replaces with the buffer representation of the widgets
451 value.  What this is depends on the widget type.
452
453 @item %d
454 Insert the string specified by @code{:doc} here.
455
456 @item %h
457 Like @samp{%d}, with the following modifications: If the documentation
458 string is more than one line, it will add a button which will toggle
459 between showing only the first line, and showing the full text.
460 Furthermore, if there is no @code{:doc} property in the widget, it will
461 instead examine the @code{:documentation-property} property.  If it is a
462 lambda expression, it will be called with the widget's value as an
463 argument, and the result will be used as the documentation text.
464
465 @item %t
466 Insert the string specified by @code{:tag} here, or the @code{princ}
467 representation of the value if there is no tag.
468
469 @item %%
470 Insert a literal @samp{%}. 
471 @end table
472
473 @item :button-face
474 Face used to highlight text inside %[ %] in the format.
475
476 @item :doc
477 The string inserted by the @samp{%d} escape in the format
478 string.  
479
480 @item :tag
481 The string inserted by the @samp{%t} escape in the format
482 string.  
483
484 @item :tag-glyph
485 Name of image to use instead of the string specified by `:tag' on
486 Emacsen that supports it.
487
488 @item :help-echo
489 Message displayed whenever you move to the widget with either
490 @code{widget-forward} or @code{widget-backward}.
491
492 @item :indent
493 An integer indicating the absolute number of spaces to indent children
494 of this widget.
495
496 @item :offset
497 An integer indicating how many extra spaces to add to the widget's
498 grandchildren compared to this widget.
499
500 @item :extra-offset
501 An integer indicating how many extra spaces to add to the widget's
502 children compared to this widget.
503
504 @item :notify
505 A function called each time the widget or a nested widget is changed.
506 The function is called with two or three arguments.  The first argument
507 is the widget itself, the second argument is the widget that was
508 changed, and the third argument is the event leading to the change, if
509 any. 
510
511 @item :menu-tag
512 Tag used in the menu when the widget is used as an option in a
513 @code{menu-choice} widget.
514
515 @item :menu-tag-get
516 Function used for finding the tag when the widget is used as an option
517 in a @code{menu-choice} widget.  By default, the tag used will be either the
518 @code{:menu-tag} or @code{:tag} property if present, or the @code{princ}
519 representation of the @code{:value} property if not.
520
521 @item :match
522 Should be a function called with two arguments, the widget and a value,
523 and returning non-nil if the widget can represent the specified value.
524
525 @item :validate
526 A function which takes a widget as an argument, and return nil if the
527 widgets current value is valid for the widget.  Otherwise, it should
528 return the widget containing the invalid data, and set that widgets
529 @code{:error} property to a string explaining the error.
530
531 @item :tab-order
532 Specify the order in which widgets are traversed with
533 @code{widget-forward} or @code{widget-backward}.  This is only partially
534 implemented.
535
536 @enumerate a
537 @item
538 Widgets with tabbing order @code{-1} are ignored.
539
540 @item 
541 (Unimplemented) When on a widget with tabbing order @var{n}, go to the
542 next widget in the buffer with tabbing order @var{n+1} or @code{nil},
543 whichever comes first.
544
545 @item
546 When on a widget with no tabbing order specified, go to the next widget
547 in the buffer with a positive tabbing order, or @code{nil}
548 @end enumerate
549
550 @item :parent
551 The parent of a nested widget (e.g. a @code{menu-choice} item or an
552 element of a @code{editable-list} widget).
553
554 @item :sibling-args
555 This keyword is only used for members of a @code{radio-button-choice} or
556 @code{checklist}.  The value should be a list of extra keyword
557 arguments, which will be used when creating the @code{radio-button} or
558 @code{checkbox} associated with this item.
559
560 @end table
561
562 @deffn {User Option} widget-glyph-directory
563 Directory where glyphs are found.  
564 Widget will look here for a file with the same name as specified for the
565 image, with either a @samp{.xpm} (if supported) or @samp{.xbm} extension.
566 @end deffn
567
568 @deffn{User Option} widget-glyph-enable
569 If non-nil, allow glyphs to appear on displayes where they are supported.
570 @end deffn
571
572
573 @menu
574 * link::                        
575 * url-link::                    
576 * info-link::                   
577 * push-button::                 
578 * editable-field::              
579 * text::                        
580 * menu-choice::                 
581 * radio-button-choice::         
582 * item::                        
583 * choice-item::                 
584 * toggle::                      
585 * checkbox::                    
586 * checklist::                   
587 * editable-list::               
588 @end menu
589
590 @node link, url-link, Basic Types, Basic Types
591 @comment  node-name,  next,  previous,  up
592 @subsection The @code{link} Widget
593
594 Syntax:
595
596 @example
597 TYPE ::= (link [KEYWORD ARGUMENT]...  [ VALUE ])
598 @end example
599
600 The @var{value}, if present, is used to initialize the @code{:value}
601 property.  The value should be a string, which will be inserted in the
602 buffer. 
603
604 @node url-link, info-link, link, Basic Types
605 @comment  node-name,  next,  previous,  up
606 @subsection The @code{url-link} Widget
607
608 Syntax:
609
610 @example
611 TYPE ::= (url-link [KEYWORD ARGUMENT]...  URL)
612 @end example
613
614 When this link is activated, the @sc{www} browser specified by
615 @code{browse-url-browser-function} will be called with @var{url}. 
616
617 @node info-link, push-button, url-link, Basic Types
618 @comment  node-name,  next,  previous,  up
619 @subsection The @code{info-link} Widget
620
621 Syntax:
622
623 @example
624 TYPE ::= (info-link [KEYWORD ARGUMENT]...  ADDRESS)
625 @end example
626
627 When this link is activated, the build-in info browser is started on
628 @var{address}. 
629
630 @node  push-button, editable-field, info-link, Basic Types
631 @comment  node-name,  next,  previous,  up
632 @subsection The @code{push-button} Widget
633
634 Syntax:
635
636 @example
637 TYPE ::= (push-button [KEYWORD ARGUMENT]...  [ VALUE ])
638 @end example
639
640 The @var{value}, if present, is used to initialize the @code{:value}
641 property. The value should be a string, which will be inserted in the
642 buffer. 
643
644 The following extra properties are recognized.
645
646 @table @code
647 @item :text-format
648 The format string used when the push button cannot be displayed
649 graphically.  There are two escapes, @code{%s}, which must be present
650 exactly once, will be substituted with the tag, and @code{%%} will be
651 substituted with a singe @samp{%}.
652 @end table
653
654 By default the tag will be shown in brackets.
655
656 @node editable-field, text, push-button, Basic Types
657 @comment  node-name,  next,  previous,  up
658 @subsection The @code{editable-field} Widget
659
660 Syntax:
661
662 @example
663 TYPE ::= (editable-field [KEYWORD ARGUMENT]... [ VALUE ])
664 @end example
665
666 The @var{value}, if present, is used to initialize the @code{:value}
667 property.  The value should be a string, which will be inserted in
668 field.  This widget will match all string values.
669
670 The following extra properties are recognized.
671
672 @table @code
673 @item :size
674 The width of the editable field.@*
675 By default the field will reach to the end of the line.
676
677 @item :value-face
678 Face used for highlighting the editable field.  Default is
679 @code{widget-field-face}. 
680
681 @item :secret
682 Character used to display the value.  You can set this to e.g. @code{?*}
683 if the field contains a password or other secret information.  By
684 default, the value is not secret.
685
686 @item :valid-regexp
687 By default the @code{:validate} function will match the content of the
688 field with the value of this attribute.  The default value is @code{""}
689 which matches everything.
690
691 @item :keymap
692 Keymap used in the editable field.  The default value is
693 @code{widget-field-keymap}, which allows you to use all the normal
694 editing commands, even if the buffers major mode supress some of them.
695 Pressing return activates the function specified by @code{:activate}. 
696
697 @item :hide-front-space
698 @itemx :hide-rear-space
699 In order to keep track of the editable field, emacs places an invisible
700 space character in front of the field, and for fixed sized fields also
701 in the rear end of the field.  For fields that extent to the end of the
702 line, the terminating linefeed serves that purpose instead.  
703
704 Emacs will try to make the spaces intangible when it is safe to do so.
705 Intangible means that the cursor motion commands will skip over the
706 character as if it didn't exist.  This is safe to do when the text
707 preceding or following the widget cannot possible change during the
708 lifetime of the @code{editable-field} widget.  The preferred way to tell
709 Emacs this, is to add text to the @code{:format} property around the
710 value.  For example @code{:format "Tag: %v "}.  
711
712 You can overwrite the internal safety check by setting the
713 @code{:hide-front-space} or @code{:hide-rear-space} properties to
714 non-nil.  This is not recommended.  For example, @emph{all} text that
715 belongs to a widget (i.e. is created from its @code{:format} string) will
716 change whenever the widget changes its value.
717
718 @end table
719
720 @node text, menu-choice, editable-field, Basic Types
721 @comment  node-name,  next,  previous,  up
722 @subsection The @code{text} Widget
723
724 This is just like @code{editable-field}, but intended for multiline text
725 fields.  The default @code{:keymap} is @code{widget-text-keymap}, which
726 does not rebind the return key.
727
728 @node menu-choice, radio-button-choice, text, Basic Types
729 @comment  node-name,  next,  previous,  up
730 @subsection The @code{menu-choice} Widget
731
732 Syntax:
733
734 @example
735 TYPE ::= (menu-choice [KEYWORD ARGUMENT]... TYPE ... )
736 @end example
737
738 The @var{type} arguments represents each possible choice.  The widgets
739 value of will be the value of the chosen @var{type} argument.  This
740 widget will match any value that matches at least one of the specified
741 @var{type} arguments.
742
743 @table @code
744 @item :void 
745 Widget type used as a fallback when the value does not match any of the
746 specified @var{type} arguments.
747
748 @item :case-fold
749 Set this to nil if you don't want to ignore case when prompting for a
750 choice through the minibuffer.
751
752 @item :children
753 A list whose car is the widget representing the currently chosen type in
754 the buffer. 
755
756 @item :choice
757 The current chosen type
758
759 @item :args 
760 The list of types. 
761 @end table
762
763 @node radio-button-choice, item, menu-choice, Basic Types
764 @comment  node-name,  next,  previous,  up
765 @subsection The @code{radio-button-choice} Widget
766
767 Syntax:
768
769 @example
770 TYPE ::= (radio-button-choice [KEYWORD ARGUMENT]...  TYPE ... )
771 @end example
772
773 The @var{type} arguments represents each possible choice.  The widgets
774 value of will be the value of the chosen @var{type} argument.  This
775 widget will match any value that matches at least one of the specified
776 @var{type} arguments.
777
778 The following extra properties are recognized.
779
780 @table @code
781 @item :entry-format
782 This string will be inserted for each entry in the list.
783 The following @samp{%} escapes are available:
784 @table @samp
785 @item %v
786 Replaced with the buffer representation of the @var{type} widget.
787 @item %b
788 Replace with the radio button.
789 @item %%
790 Insert a literal @samp{%}. 
791 @end table
792
793 @item button-args
794 A list of keywords to pass to the radio buttons.  Useful for setting
795 e.g. the @samp{:help-echo} for each button.
796
797 @item :buttons
798 The widgets representing the radio buttons.
799
800 @item :children
801 The widgets representing each type.
802
803 @item :choice
804 The current chosen type
805
806 @item :args 
807 The list of types. 
808 @end table
809
810 You can add extra radio button items to a @code{radio-button-choice}
811 widget after it has been created with the function
812 @code{widget-radio-add-item}. 
813
814 @defun widget-radio-add-item widget type
815 Add to @code{radio-button-choice} widget @var{widget} a new radio button item of type
816 @var{type}. 
817 @end defun
818
819 Please note that such items added after the @code{radio-button-choice}
820 widget has been created will @strong{not} be properly destructed when
821 you call @code{widget-delete}.
822
823 @node item, choice-item, radio-button-choice, Basic Types
824 @comment  node-name,  next,  previous,  up
825 @subsection The @code{item} Widget
826
827 Syntax:
828
829 @example
830 ITEM ::= (item [KEYWORD ARGUMENT]... VALUE)
831 @end example
832
833 The @var{value}, if present, is used to initialize the @code{:value}
834 property.  The value should be a string, which will be inserted in the
835 buffer.  This widget will only match the specified value.
836
837 @node choice-item, toggle, item, Basic Types
838 @comment  node-name,  next,  previous,  up
839 @subsection The @code{choice-item} Widget
840
841 Syntax:
842
843 @example
844 ITEM ::= (choice-item [KEYWORD ARGUMENT]... VALUE)
845 @end example
846
847 The @var{value}, if present, is used to initialize the @code{:value}
848 property.  The value should be a string, which will be inserted in the
849 buffer as a button.  Activating the button of a @code{choice-item} is
850 equivalent to activating the parent widget.  This widget will only match
851 the specified value. 
852
853 @node toggle, checkbox, choice-item, Basic Types
854 @comment  node-name,  next,  previous,  up
855 @subsection The @code{toggle} Widget
856
857 Syntax:
858
859 @example
860 TYPE ::= (toggle [KEYWORD ARGUMENT]...)
861 @end example
862
863 The widget has two possible states, `on' and `off', which corresponds to
864 a @code{t} or @code{nil} value.
865
866 The following extra properties are recognized.
867
868 @table @code
869 @item :on
870 String representing the `on' state.  By default the string @samp{on}.
871 @item :off 
872 String representing the `off' state.  By default the string @samp{off}.
873 @item :on-glyph
874 Name of a glyph to be used instead of the `:on' text string, on emacsen
875 that supports it.
876 @item :off-glyph
877 Name of a glyph to be used instead of the `:off' text string, on emacsen
878 that supports it.
879 @end table
880
881 @node checkbox, checklist, toggle, Basic Types
882 @comment  node-name,  next,  previous,  up
883 @subsection The @code{checkbox} Widget
884
885 The widget has two possible states, `selected' and `unselected', which
886 corresponds to a @code{t} or @code{nil} value.
887
888 Syntax:
889
890 @example
891 TYPE ::= (checkbox [KEYWORD ARGUMENT]...)
892 @end example
893
894 @node checklist, editable-list, checkbox, Basic Types
895 @comment  node-name,  next,  previous,  up
896 @subsection The @code{checklist} Widget
897
898 Syntax:
899
900 @example
901 TYPE ::= (checklist [KEYWORD ARGUMENT]...  TYPE ... )
902 @end example
903
904 The @var{type} arguments represents each checklist item.  The widgets
905 value of will be a list containing the value of each ticked @var{type}
906 argument.  The checklist widget will match a list whose elements all
907 matches at least one of the specified @var{type} arguments.
908
909 The following extra properties are recognized.
910
911 @table @code
912 @item :entry-format
913 This string will be inserted for each entry in the list.
914 The following @samp{%} escapes are available:
915 @table @samp
916 @item %v
917 Replaced with the buffer representation of the @var{type} widget.
918 @item %b
919 Replace with the checkbox.
920 @item %%
921 Insert a literal @samp{%}. 
922 @end table
923
924 @item button-args
925 A list of keywords to pass to the checkboxes.  Useful for setting
926 e.g. the @samp{:help-echo} for each checkbox.
927
928 @item :buttons
929 The widgets representing the checkboxes.
930
931 @item :children
932 The widgets representing each type.
933
934 @item :args 
935 The list of types. 
936 @end table
937
938 @node editable-list,  , checklist, Basic Types
939 @comment  node-name,  next,  previous,  up
940 @subsection The @code{editable-list} Widget
941
942 Syntax:
943
944 @example
945 TYPE ::= (editable-list [KEYWORD ARGUMENT]... TYPE)
946 @end example
947
948 The value is a list, where each member represent one widget of type
949 @var{type}. 
950
951 The following extra properties are recognized.
952
953 @table @code
954 @item :entry-format
955 This string will be inserted for each entry in the list.
956 The following @samp{%} escapes are available:
957 @table @samp
958 @item %v
959 This will be replaced with the buffer representation of the @var{type}
960 widget.
961 @item %i
962 Insert the @b{[INS]} button.
963 @item %d
964 Insert the @b{[DEL]} button.
965 @item %%
966 Insert a literal @samp{%}. 
967 @end table
968
969 @item :insert-button-args
970 A list of keyword arguments to pass to the insert buttons.
971
972 @item :delete-button-args
973 A list of keyword arguments to pass to the delete buttons.
974
975 @item :append-button-args
976 A list of keyword arguments to pass to the trailing insert button.
977
978
979 @item :buttons
980 The widgets representing the insert and delete buttons.
981
982 @item :children
983 The widgets representing the elements of the list.
984
985 @item :args
986 List whose car is the type of the list elements.
987
988 @end table
989
990 @node Sexp Types, Widget Properties, Basic Types, Top
991 @comment
992 @section Sexp Types
993
994 A number of widgets for editing s-expressions (lisp types) are also
995 available.  These basically fall in three categories: @dfn{atoms},
996 @dfn{composite types}, and @dfn{generic}.
997
998 @menu
999 * generic::                     
1000 * atoms::                       
1001 * composite::                   
1002 @end menu
1003
1004 @node generic, atoms, Sexp Types, Sexp Types
1005 @comment  node-name,  next,  previous,  up
1006 @subsection The Generic Widget.
1007
1008 The @code{const} and @code{sexp} widgets can contain any lisp
1009 expression.  In the case of the @code{const} widget the user is
1010 prohibited from editing edit it, which is mainly useful as a component
1011 of one of the composite widgets.
1012
1013 The syntax for the generic widgets is
1014
1015 @example
1016 TYPE ::= (const [KEYWORD ARGUMENT]...  [ VALUE ])
1017 @end example
1018
1019 The @var{value}, if present, is used to initialize the @code{:value}
1020 property and can be any s-expression.
1021
1022 @deffn Widget const
1023 This will display any valid s-expression in an immutable part of the
1024 buffer. 
1025 @end deffn
1026
1027 @deffn Widget sexp
1028 This will allow you to edit any valid s-expression in an editable buffer
1029 field. 
1030
1031 The @code{sexp} widget takes the same keyword arguments as the
1032 @code{editable-field} widget.
1033 @end deffn
1034
1035 @node atoms, composite, generic, Sexp Types
1036 @comment  node-name,  next,  previous,  up
1037 @subsection Atomic Sexp Widgets.
1038
1039 The atoms are s-expressions that does not consist of other
1040 s-expressions.  A string is an atom, while a list is a composite type.
1041 You can edit the value of an atom with the following widgets.  
1042
1043 The syntax for all the atoms are
1044
1045 @example
1046 TYPE ::= (NAME [KEYWORD ARGUMENT]...  [ VALUE ])
1047 @end example
1048
1049 The @var{value}, if present, is used to initialize the @code{:value}
1050 property and must be an expression of the same type as the widget.
1051 I.e. the string widget can only be initialized with a string.
1052
1053 All the atom widgets take the same keyword arguments as the @code{editable-field}
1054 widget.
1055
1056 @deffn Widget string
1057 Allows you to edit a string in an editable field.
1058 @end deffn
1059
1060 @deffn Widget file
1061 Allows you to edit a file name in an editable field.  You you activate
1062 the tag button, you can edit the file name in the mini-buffer with
1063 completion. 
1064
1065 Keywords:
1066 @table @code
1067 @item :must-match
1068 If this is set to non-nil, only existing file names will be allowed in
1069 the minibuffer.
1070 @end table
1071 @end deffn
1072
1073 @deffn Widget directory
1074 Allows you to edit a directory name in an editable field.
1075 Similar to the @code{file} widget.
1076 @end deffn
1077
1078 @deffn Widget symbol
1079 Allows you to edit a lisp symbol in an editable field.
1080 @end deffn
1081
1082 @deffn Widget integer
1083 Allows you to edit an integer in an editable field.
1084 @end deffn
1085
1086 @deffn Widget number
1087 Allows you to edit a number in an editable field.
1088 @end deffn
1089
1090 @deffn Widget boolean
1091 Allows you to edit a boolean.  In lisp this means a variable which is
1092 either nil meaning false, or non-nil meaning true.
1093 @end deffn
1094
1095
1096 @node composite,  , atoms, Sexp Types
1097 @comment  node-name,  next,  previous,  up
1098 @subsection Composite Sexp Widgets.
1099
1100 The syntax for the composite are
1101
1102 @example
1103 TYPE ::= (NAME [KEYWORD ARGUMENT]...  COMPONENT...)
1104 @end example
1105
1106 Where each @var{component} must be a widget type.  Each component widget
1107 will be displayed in the buffer, and be editable to the user.
1108
1109 @deffn Widget cons
1110 The value of a @code{cons} widget is a cons-cell where the car is the
1111 value of the first component and the cdr is the value of the second
1112 component.  There must be exactly two components. 
1113 @end deffn
1114
1115 @deffn Widget lisp
1116 The value of a @code{lisp} widget is a list containing the value of
1117 each of its component.
1118 @end deffn
1119
1120 @deffn Widget vector
1121 The value of a @code{vector} widget is a vector containing the value of
1122 each of its component.
1123 @end deffn
1124
1125 The above suffice for specifying fixed size lists and vectors.  To get
1126 variable length lists and vectors, you can use a @code{choice},
1127 @code{set} or @code{repeat} widgets together with the @code{:inline}
1128 keywords.  If any component of a composite widget has the @code{:inline}
1129 keyword set, its value must be a list which will then be spliced into
1130 the composite.  For example, to specify a list whose first element must
1131 be a file name, and whose remaining arguments should either by the
1132 symbol @code{t} or two files, you can use the following widget
1133 specification:
1134
1135 @example
1136 (list file
1137       (choice (const t)
1138               (list :inline t
1139                     :value ("foo" "bar")
1140                     string string)))
1141 @end example
1142
1143 The value of a widget of this type will either have the form 
1144 @samp{(file t)} or @code{(file string string)}.
1145
1146 This concept of inline is probably hard to understand.  It was certainly
1147 hard to implement so instead of confuse you more by trying to explain it
1148 here, I'll just suggest you meditate over it for a while.
1149
1150 @deffn Widget choice
1151 Allows you to edit a sexp which may have one of fixed set of types.  It
1152 is currently implemented with the @code{choice-menu} basic widget, and
1153 has a similar syntax.
1154 @end deffn
1155
1156 @deffn Widget set
1157 Allows you to specify a type which must be a list whose elements all
1158 belong to given set.  The elements of the list is not significant.  This
1159 is implemented on top of the @code{checklist} basic widget, and has a
1160 similar syntax. 
1161 @end deffn
1162
1163 @deffn Widget repeat
1164 Allows you to specify a variable length list whose members are all of
1165 the same type.  Implemented on top of the `editable-list' basic widget,
1166 and has a similar syntax.
1167 @end deffn
1168
1169 @node Widget Properties, Defining New Widgets, Sexp Types, Top
1170 @comment  node-name,  next,  previous,  up
1171 @section Properties
1172
1173 You can examine or set the value of a widget by using the widget object
1174 that was returned by @code{widget-create}.
1175
1176 @defun widget-value widget
1177 Return the current value contained in @var{widget}.
1178 It is an error to call this function on an uninitialized widget.
1179 @end defun
1180
1181 @defun widget-value-set widget value
1182 Set the value contained in @var{widget} to @var{value}.
1183 It is an error to call this function with an invalid @var{value}.
1184 @end defun
1185
1186 @strong{Important:} You @emph{must} call @code{widget-setup} after
1187 modifying the value of a widget before the user is allowed to edit the
1188 widget again.  It is enough to call @code{widget-setup} once if you
1189 modify multiple widgets.  This is currently only necessary if the widget
1190 contains an editing field, but may be necessary for other widgets in the
1191 future. 
1192
1193 If your application needs to associate some information with the widget
1194 objects, for example a reference to the item being edited, it can be
1195 done with @code{widget-put} and @code{widget-get}.  The property names
1196 must begin with a @samp{:}.
1197
1198 @defun widget-put widget property value
1199 In @var{widget} set @var{property} to @var{value}.
1200 @var{property} should be a symbol, while @var{value} can be anything.
1201 @end defun
1202
1203 @defun widget-get widget property
1204 In @var{widget} return the value for @var{property}.
1205 @var{property} should be a symbol, the value is what was last set by
1206 @code{widget-put} for @var{property}.
1207 @end defun
1208
1209 @defun widget-member widget property
1210 Non-nil if @var{widget} has a value (even nil) for property @var{property}.
1211 @end defun
1212
1213 Occasionally it can be useful to know which kind of widget you have,
1214 i.e. the name of the widget type you gave when the widget was created. 
1215
1216 @defun widget-type widget
1217 Return the name of @var{widget}, a symbol.
1218 @end defun
1219
1220 Widgets can be in two states: active, which means they are modifiable by
1221 the user, or inactive, which means they cannot be modified by the user.
1222 You can query or set the state with the following code:
1223
1224 @lisp
1225 ;; Examine if @var{widget} is active or not.
1226 (if (widget-apply @var{widget} :active)
1227     (message "Widget is active.")
1228   (message "Widget is inactive.")
1229
1230 ;; Make @var{widget} inactive.
1231 (widget-apply @var{widget} :deactivate)
1232
1233 ;; Make @var{widget} active.
1234 (widget-apply @var{widget} :activate)
1235 @end lisp
1236
1237 A widget is inactive if itself, or any of its ancestors (found by
1238 following the @code{:parent} link) have been deactivated.  To make sure
1239 a widget is really active, you must therefore activate both itself, and
1240 all its ancestors.
1241
1242 @lisp
1243 (while widget 
1244   (widget-apply widget :activate)
1245   (setq widget (widget-get widget :parent)))
1246 @end lisp
1247
1248 You can check if a widget has been made inactive by examining the value
1249 of @code{:inactive} keyword.  If this is non-nil, the widget itself has
1250 been deactivated.  This is different from using the @code{:active}
1251 keyword, in that the later tell you if the widget @strong{or} any of its
1252 ancestors have been deactivated.   Do not attempt to set the
1253 @code{:inactive} keyword directly.  Use the @code{:activate}
1254 @code{:deactivated} keywords instead.
1255
1256
1257 @node Defining New Widgets, Widget Wishlist., Widget Properties, Top
1258 @comment  node-name,  next,  previous,  up
1259 @section Defining New Widgets
1260
1261 You can define specialized widgets with @code{define-widget}.  It allows
1262 you to create a shorthand for more complex widgets, including specifying
1263 component widgets and default new default values for the keyword
1264 arguments. 
1265
1266 @defun widget-define name class doc &rest args
1267 Define a new widget type named @var{name} from @code{class}.
1268
1269 @var{name} and class should both be symbols, @code{class} should be one
1270 of the existing widget types. 
1271
1272 The third argument @var{DOC} is a documentation string for the widget.
1273
1274 After the new widget has been defined, the following two calls will
1275 create identical widgets:
1276
1277 @itemize @bullet
1278 @item
1279 @lisp
1280 (widget-create @var{name})
1281 @end lisp
1282
1283 @item
1284 @lisp
1285 (apply widget-create @var{class} @var{args})
1286 @end lisp
1287 @end itemize
1288
1289 @end defun
1290
1291 Using @code{widget-define} does just store the definition of the widget
1292 type in the @code{widget-type} property of @var{name}, which is what
1293 @code{widget-create} uses.
1294
1295 If you just want to specify defaults for keywords with no complex
1296 conversions, you can use @code{identity} as your conversion function.
1297
1298 The following additional keyword arguments are useful when defining new
1299 widgets: 
1300 @table @code
1301 @item :convert-widget
1302 Function to convert a widget type before creating a widget of that
1303 type.  It takes a widget type as an argument, and returns the converted
1304 widget type.  When a widget is created, this function is called for the
1305 widget type and all the widgets parent types, most derived first. 
1306
1307 @item :value-to-internal
1308 Function to convert the value to the internal format.  The function
1309 takes two arguments, a widget and an external value, and returns the
1310 internal value.  The function is called on the present @code{:value}
1311 when the widget is created, and on any value set later with
1312 @code{widget-value-set}.
1313
1314 @item :value-to-external
1315 Function to convert the value to the external format.  The function
1316 takes two arguments, a widget and an internal value, and returns the
1317 internal value.  The function is called on the present @code{:value}
1318 when the widget is created, and on any value set later with
1319 @code{widget-value-set}.
1320
1321 @item :create
1322 Function to create a widget from scratch.  The function takes one
1323 argument, a widget type, and create a widget of that type, insert it in
1324 the buffer, and return a widget object.
1325
1326 @item :delete
1327 Function to delete a widget.  The function takes one argument, a widget,
1328 and should remove all traces of the widget from the buffer.
1329
1330 @item :value-create
1331 Function to expand the @samp{%v} escape in the format string.  It will
1332 be called with the widget as its argument.  Should
1333 insert a representation of the widgets value in the buffer.
1334
1335 @item :value-delete
1336 Should remove the representation of the widgets value from the buffer.
1337 It will be called with the widget as its argument.  It doesn't have to
1338 remove the text, but it should release markers and delete nested widgets
1339 if such has been used.
1340
1341 @item :format-handler
1342 Function to handle unknown @samp{%} escapes in the format string.  It
1343 will be called with the widget and the escape character as arguments.
1344 You can set this to allow your widget to handle non-standard escapes.
1345
1346 You should end up calling @code{widget-default-format-handler} to handle
1347 unknown escape sequences, which will handle the @samp{%h} and any future
1348 escape sequences, as well as give an error for unknown escapes.
1349 @end table
1350
1351 If you want to define a new widget from scratch, use the @code{default}
1352 widget as its base.
1353
1354 @deffn Widget default [ keyword argument ]
1355 Widget used as a base for other widgets. 
1356
1357 It provides most of the functionality that is referred to as ``by
1358 default'' in this text. 
1359 @end deffn
1360
1361 @node  Widget Wishlist.,  , Defining New Widgets, Top
1362 @comment  node-name,  next,  previous,  up
1363 @section Wishlist.
1364
1365 @itemize @bullet
1366 @item 
1367 It should be possible to add or remove items from a list with @kbd{C-k}
1368 and @kbd{C-o} (suggested by @sc{rms}).
1369
1370 @item 
1371 The @samp{[INS]} and @samp{[DEL]} buttons should be replaced by a single
1372 dash (@samp{-}).  The dash should be a button that, when activated, ask
1373 whether you want to add or delete an item (@sc{rms} wanted to git rid of
1374 the ugly buttons, the dash is my idea).
1375
1376 @item
1377 Widgets such as @code{file} and @code{symbol} should prompt with completion. 
1378
1379 @item
1380 The @code{menu-choice} tag should be prettier, something like the abbreviated
1381 menus in Open Look.
1382
1383 @item
1384 The functions used in many widgets, like
1385 @code{widget-item-convert-widget}, should not have names that are
1386 specific to the first widget where I happended to use them.
1387
1388 @item
1389 Flag to make @code{widget-move} skip a specified button.
1390
1391 @item
1392 Document `helper' functions for defining new widgets.
1393
1394 @item
1395 Activate the item this is below the mouse when the button is
1396 released, not the item this is below the mouse when the button is
1397 pressed.  Dired and grep gets this right.  Give feedback if possible.
1398
1399 @item
1400 Use @samp{@@deffn Widget} to document widgets. 
1401
1402 @item
1403 Document global keywords in one place.  
1404
1405 Document keywords particular to a specific widget in the widget
1406 definition.
1407
1408 Document the `default' widget first. 
1409
1410 Split, when needed, keywords into those useful for normal
1411 customization, those primarily useful when deriving, and those who
1412 represent runtime information. 
1413
1414 @item
1415 Figure out terminology and @sc{api} for the class/type/object/super
1416 stuff. 
1417
1418 Perhaps the correct model is delegation?
1419
1420 @item
1421 Document @code{widget-browse}.
1422
1423 @item
1424 Make indentation work with glyphs and propertional fonts.
1425
1426 @item
1427 Add object and class hierarchies to the browser.
1428
1429 @end itemize
1430
1431 @contents
1432 @bye